5 #ifndef DECOMP_GEOMETRIC_UTILS_H 6 #define DECOMP_GEOMETRIC_UTILS_H 10 #include <Eigen/Eigenvalues> 15 Eigen::SelfAdjointEigenSolver<Matf<Dim, Dim>> es(A);
16 return es.eigenvalues();
23 R << cos(yaw), -sin(yaw),
28 inline Mat3f vec3_to_rotation(
const Vec3f &v) {
30 Vec3f rpy(0, std::atan2(-v(2), v.topRows<2>().norm()),
31 std::atan2(v(1), v(0)));
32 Quatf qx(cos(rpy(0) / 2), sin(rpy(0) / 2), 0, 0);
33 Quatf qy(cos(rpy(1) / 2), 0, sin(rpy(1) / 2), 0);
34 Quatf qz(cos(rpy(2) / 2), 0, 0, sin(rpy(2) / 2));
35 return Mat3f(qz * qy * qx);
44 Vec2f avg = Vec2f::Zero();
45 for (
const auto& pt : pts)
51 pts_valued.resize(pts.size());
52 for (
unsigned int i = 0; i < pts.size(); i++) {
53 decimal_t theta = atan2(pts[i](1) - avg(1), pts[i](0) - avg(0));
54 pts_valued[i] = std::make_pair(theta, pts[i]);
57 std::sort(pts_valued.begin(), pts_valued.end(),
58 [](
const std::pair<decimal_t, Vec2f> &i,
59 const std::pair<decimal_t, Vec2f> &j) {
60 return i.first < j.first;});
62 for (
size_t i = 0; i < pts_valued.size(); i++)
63 pts_sorted[i] = pts_valued[i].second;
70 const std::pair<Vec2f, Vec2f> &v2,
74 decimal_t c1 = a1 * v1.second(0) + b1 * v1.second(1);
78 decimal_t c2 = a2 * v2.second(0) + b2 * v2.second(1);
80 decimal_t x = (c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1);
81 decimal_t y = (c1 * a2 - c2 * a1) / (a2 * b1 - a1 * b2);
83 if (std::isnan(x) || std::isnan(y) || std::isinf(x) || std::isinf(y))
96 for (
unsigned int i = 0; i < lines.size(); i++) {
97 for (
unsigned int j = i + 1; j < lines.size(); j++) {
111 for (
unsigned int i = 0; i < vs.size(); i++) {
113 Vec2f v(-n(1), n(0));
116 lines.push_back(std::make_pair(v, vs[i].p_));
138 for (
unsigned int i = 0; i < vts.size(); i++) {
139 const Vec3f t = vts[i].p_;
140 const Vec3f n = vts[i].n_;
141 const Quatf q = Quatf::FromTwoVectors(
Vec3f(0, 0, 1), n);
144 for (
unsigned int j = 0; j < vts.size(); j++) {
147 Vec3f nw = vts[j].n_;
148 Vec3f nb = R.transpose() * nw;
149 decimal_t bb = vts[j].p_.dot(nw) - nw.dot(t);
150 Vec2f v =
Vec3f(0, 0, 1).cross(nb).topRows<2>();
158 lines.push_back(std::make_pair(v, p));
165 for (
const auto& it : pts) {
168 pts_inside.push_back(it);
171 if(pts_inside.size() > 2) {
177 for (
auto &it : pts_inside)
178 points_valid.push_back(R *
Vec3f(it(0), it(1), 0) + t);
181 bds.push_back(points_valid);
Matf< 3, 3 > Mat3f
3x3 Matrix in float
Definition: data_type.h:99
bool inside(const Vecf< Dim > &pt) const
Check if the point is inside polyhedron, non-exclusive.
Definition: polyhedron.h:54
Provide a few widely used function for basic type.
Polyhedron class.
Definition: polyhedron.h:41
Mat2f vec2_to_rotation(const Vec2f &v)
Calculate rotation matrix from a vector (aligned with x-axis)
Definition: geometric_utils.h:20
vec_Vec2f sort_pts(const vec_Vec2f &pts)
Sort points on the same plane in the counter-clockwise order.
Definition: geometric_utils.h:39
vec_Vecf< Dim > points_inside(const vec_Vecf< Dim > &O) const
Calculate points inside polyhedron, non-exclusive.
Definition: polyhedron.h:65
vec_E< Hyperplane< Dim > > hyperplanes() const
Get the hyperplane array.
Definition: polyhedron.h:83
Vecf< 3 > Vec3f
Eigen 1D float vector of size 3.
Definition: data_type.h:79
Matf< 2, 2 > Mat2f
2x2 Matrix in float
Definition: data_type.h:97
Eigen::Matrix< decimal_t, M, N > Matf
MxN Eigen matrix.
Definition: data_type.h:63
bool line_intersect(const std::pair< Vec2f, Vec2f > &v1, const std::pair< Vec2f, Vec2f > &v2, Vec2f &pi)
Find intersection between two lines on the same plane, return false if they are not intersected...
Definition: geometric_utils.h:69
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:50
std::vector< T, Eigen::aligned_allocator< T >> vec_E
Pre-allocated std::vector for Eigen using vec_E.
Definition: data_type.h:54
vec_E< vec_Vec3f > cal_vertices(const Polyhedron3D &poly)
Find extreme points of Polyhedron3D.
Definition: geometric_utils.h:134
Vecf< 2 > Vec2f
Eigen 1D float vector of size 2.
Definition: data_type.h:75
Eigen::Quaternion< decimal_t > Quatf
Allias of Eigen::Quaterniond.
Definition: data_type.h:120
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector.
Definition: data_type.h:57
vec_Vec2f line_intersects(const vec_E< std::pair< Vec2f, Vec2f >> &lines)
Find intersection between multiple lines.
Definition: geometric_utils.h:94
vec_E< Vec3f > vec_Vec3f
Vector of type Vec3f.
Definition: data_type.h:92
Vecf< Dim > eigen_value(const Matf< Dim, Dim > &A)
Calculate eigen values.
Definition: geometric_utils.h:14
vec_E< Vec2f > vec_Vec2f
Vector of type Vec2f.
Definition: data_type.h:88