MRSL DecompUtil Library  0.1
An implementaion of convex decomposition over point cloud
polyhedron.h
1 
6 #ifndef DECOMP_POLYGON_H
7 #define DECOMP_POLYGON_H
8 
10 
12 template <int Dim>
13 struct Hyperplane {
14  Hyperplane() {}
15  Hyperplane(const Vecf<Dim>& p, const Vecf<Dim>& n) : p_(p), n_(n) {}
16 
18  decimal_t signed_dist(const Vecf<Dim>& pt) const {
19  return n_.dot(pt - p_);
20  }
21 
23  decimal_t dist(const Vecf<Dim>& pt) const {
24  return std::abs(signed_dist(pt));
25  }
26 
31 };
32 
37 
38 
40 template <int Dim>
41 struct Polyhedron {
45  Polyhedron(const vec_E<Hyperplane<Dim>>& vs) : vs_(vs) {}
46 
47 
49  void add(const Hyperplane<Dim>& v) {
50  vs_.push_back(v);
51  }
52 
54  bool inside(const Vecf<Dim>& pt) const {
55  for (const auto& v : vs_) {
56  if (v.signed_dist(pt) > epsilon_) {
57  //printf("rejected pt: (%f, %f), d: %f\n",pt(0), pt(1), v.signed_dist(pt));
58  return false;
59  }
60  }
61  return true;
62  }
63 
66  vec_Vecf<Dim> new_O;
67  for (const auto &it : O) {
68  if (inside(it))
69  new_O.push_back(it);
70  }
71  return new_O;
72  }
73 
76  vec_E<std::pair<Vecf<Dim>, Vecf<Dim>>> ns(vs_.size());
77  for (size_t i = 0; i < vs_.size(); i++)
78  ns[i] = std::make_pair(vs_[i].p_, vs_[i].n_); // fist is point, second is normal
79  return ns;
80  }
81 
84  return vs_;
85  }
86 
88  vec_E<Hyperplane<Dim>> vs_; // normal must go outside
89 
90 };
91 
96 
98 template <int Dim>
103  LinearConstraint(const MatDNf<Dim>& A, const VecDf& b) : A_(A), b_(b) {}
110  const unsigned int size = vs.size();
111  MatDNf<Dim> A(size, Dim);
112  VecDf b(size);
113 
114  for (unsigned int i = 0; i < size; i++) {
115  const auto n = vs[i].n_;
116  decimal_t c = vs[i].p_.dot(n);
117  if (n.dot(p0) - c > 0) {
118  n = -n;
119  c = -c;
120  }
121  A.row(i) = n;
122  b(i) = c;
123  }
124 
125  A_ = A;
126  b_ = b;
127  }
128 
130  bool inside(const Vecf<Dim> &pt) {
131  VecDf d = A_ * pt - b_;
132  for (unsigned int i = 0; i < d.rows(); i++) {
133  if (d(i) > 0)
134  return false;
135  }
136  return true;
137  }
138 
139  MatDNf<Dim> A_;
140  VecDf b_;
141 };
142 
147 
148 #endif
vec_E< Vecf< N >> vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:69
bool inside(const Vecf< Dim > &pt)
Check if the point is inside polyhedron using linear constraint.
Definition: polyhedron.h:130
bool inside(const Vecf< Dim > &pt) const
Check if the point is inside polyhedron, non-exclusive.
Definition: polyhedron.h:54
decimal_t signed_dist(const Vecf< Dim > &pt) const
Calculate the signed distance from point.
Definition: polyhedron.h:18
LinearConstraint(const Vecf< Dim > p0, const vec_E< Hyperplane< Dim >> &vs)
Construct from a inside point and hyperplane array.
Definition: polyhedron.h:109
[A, b] for
Definition: polyhedron.h:99
Vecf< Dim > p_
Point on the plane.
Definition: polyhedron.h:28
Polyhedron class.
Definition: polyhedron.h:41
constexpr decimal_t epsilon_
Compensate for numerical error.
Definition: data_type.h:126
void add(const Hyperplane< Dim > &v)
Append Hyperplane.
Definition: polyhedron.h:49
Vecf< Eigen::Dynamic > VecDf
Dynamic Nx1 Eigen float vector.
Definition: data_type.h:106
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
vec_E< Hyperplane< Dim > > vs_
Hyperplane array.
Definition: polyhedron.h:88
Eigen::Matrix< decimal_t, Eigen::Dynamic, N > MatDNf
MxN Eigen matrix with M unknown.
Definition: data_type.h:66
decimal_t dist(const Vecf< Dim > &pt) const
Calculate the distance from point.
Definition: polyhedron.h:23
Polyhedron()
Null constructor.
Definition: polyhedron.h:43
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
Vecf< Dim > n_
Normal of the plane, directional.
Definition: polyhedron.h:30
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
Polyhedron(const vec_E< Hyperplane< Dim >> &vs)
Construct from Hyperplane array.
Definition: polyhedron.h:45
vec_E< std::pair< Vecf< Dim >, Vecf< Dim > > > cal_normals() const
Calculate normals, used for visualization.
Definition: polyhedron.h:75
LinearConstraint(const MatDNf< Dim > &A, const VecDf &b)
Construct from directly, s.t .
Definition: polyhedron.h:103
LinearConstraint()
Null constructor.
Definition: polyhedron.h:101