MRSL DecompUtil Library  0.1
An implementaion of convex decomposition over point cloud
ellipsoid.h
Go to the documentation of this file.
1 
6 #ifndef DECOMP_ELLIPSOID_H
7 #define DECOMP_ELLIPSOID_H
8 
9 #include <iostream>
10 #include <decomp_basis/data_type.h>
11 #include <decomp_geometry/polyhedron.h>
12 
13 template <int Dim>
14 struct Ellipsoid {
15  Ellipsoid() {}
16  Ellipsoid(const Matf<Dim, Dim>& C, const Vecf<Dim>& d) : C_(C), d_(d) {}
17 
19  decimal_t dist(const Vecf<Dim>& pt) const {
20  return (C_.inverse() * (pt - d_)).norm();
21  }
22 
24  bool inside(const Vecf<Dim>& pt) const {
25  return dist(pt) <= 1;
26  }
27 
30  vec_Vecf<Dim> new_O;
31  for (const auto &it : O) {
32  if (inside(it))
33  new_O.push_back(it);
34  }
35  return new_O;
36  }
37 
41  decimal_t min_dist = std::numeric_limits<decimal_t>::max();
42  for (const auto &it : O) {
43  decimal_t d = dist(it);
44  if (d < min_dist) {
45  min_dist = d;
46  pt = it;
47  }
48  }
49  return pt;
50  }
51 
54  const auto closest_pt = closest_point(O);
55  const auto n = C_.inverse() * C_.inverse().transpose() *
56  (closest_pt - d_);
57  return Hyperplane<Dim>(closest_pt, n.normalized());
58  }
59 
61  template<int U = Dim>
62  typename std::enable_if<U == 2, vec_Vecf<U>>::type
63  sample(int num) const {
64  vec_Vecf<Dim> pts;
65  decimal_t dyaw = M_PI*2/num;
66  for(decimal_t yaw = 0; yaw < M_PI*2; yaw+=dyaw) {
67  Vecf<Dim> pt;
68  pt << cos(yaw), sin(yaw);
69  pts.push_back(C_ * pt + d_);
70  }
71  return pts;
72  }
73 
74  void print() const {
75  std::cout << "C: " << C_ << std::endl;
76  std::cout << "d: " << d_ << std::endl;
77  }
78 
80  decimal_t volume() const {
81  return C_.determinant();
82  }
83 
85  Matf<Dim, Dim> C() const {
86  return C_;
87  }
88 
90  Vecf<Dim> d() const {
91  return d_;
92  }
93 
94  Matf<Dim, Dim> C_;
95  Vecf<Dim> d_;
96 };
97 
99 
100 typedef Ellipsoid<3> Ellipsoid3D;
101 
102 #endif
vec_E< Vecf< N >> vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:69
bool inside(const Vecf< Dim > &pt) const
Check if the point is inside, non-exclusive.
Definition: ellipsoid.h:24
decimal_t dist(const Vecf< Dim > &pt) const
Calculate distance to the center.
Definition: ellipsoid.h:19
Definition: ellipsoid.h:14
vec_Vecf< Dim > points_inside(const vec_Vecf< Dim > &O) const
Calculate points inside ellipsoid, non-exclusive.
Definition: ellipsoid.h:29
Hyperplane< Dim > closest_hyperplane(const vec_Vecf< Dim > &O) const
Find the closest hyperplane from the closest point.
Definition: ellipsoid.h:53
Vecf< Dim > closest_point(const vec_Vecf< Dim > &O) const
Find the closest point.
Definition: ellipsoid.h:39
Eigen::Matrix< decimal_t, M, N > Matf
MxN Eigen matrix.
Definition: data_type.h:63
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:50
std::enable_if< U==2, vec_Vecf< U > >::type sample(int num) const
Sample n points along the contour.
Definition: ellipsoid.h:63
Defines all data types used in this lib.
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector.
Definition: data_type.h:57
Hyperplane class.
Definition: polyhedron.h:13
Vecf< Dim > d() const
Get center.
Definition: ellipsoid.h:90
Matf< Dim, Dim > C() const
Get C matrix.
Definition: ellipsoid.h:85
decimal_t volume() const
Get ellipsoid volume.
Definition: ellipsoid.h:80