cheshirekow  v0.1.0
simplex.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
26 #ifndef EDELSBRUNNER96_SIMPLEX_H_
27 #define EDELSBRUNNER96_SIMPLEX_H_
28 
29 #include <array>
30 #include <bitset>
31 #include <cstdint>
32 #include <initializer_list>
33 #include <map>
34 #include <set>
35 
36 #include <Eigen/Dense>
37 
38 namespace edelsbrunner96 {
39 namespace simplex {
40 
42 
48 enum MarkedBits {
60 };
61 
62 } // namespace simplex
63 
66 
88 template<class Traits>
89 struct SimplexBase {
90  typedef typename Traits::Scalar Scalar;
91  typedef typename Traits::Point Point;
92  typedef typename Traits::PointRef PointRef;
93  typedef typename Traits::Simplex Simplex;
94  typedef typename Traits::SimplexRef SimplexRef;
95  typedef typename Traits::Storage Storage;
96 
98  typedef std::bitset<simplex::NUM_BITS> MarkedVec;
99 
101  // TODO(bialkowski): I think version number is overkill and we can add a
102  // "retired" flag to the bitset instead.
103  uint16_t version;
104 
106  std::array<PointRef, Traits::NDim + 1> V;
107 
109  std::array<SimplexRef, Traits::NDim + 1> N;
110 
113 
115  SimplexBase();
116 
118  template <typename... PointRefs>
119  void SetVertices(PointRefs... refs);
120 
122  template <typename Container>
123  void SetVertices(Container refs);
124 
127  void SetVertices(std::initializer_list<PointRef> refs);
128 
130 
134 
136 
140 
142  uint8_t IndexOf(PointRef v) const;
143 
147  Eigen::Matrix<typename Traits::Scalar, Traits::NDim, 1>
148  CoordinatesOf(Storage& storage, const typename Traits::Point& xq) const;
149 
154  Eigen::Matrix<typename Traits::Scalar, Traits::NDim + 1, 1>
156  const typename Traits::Point& xq) const;
157 
160  bool Contains(Storage& storage, const typename Traits::Point& xq) const;
161 
163  void ComputeCenter(Storage& storage);
164 };
165 
167 template <class Traits>
168 class SimplexFill {
169  public:
170  typedef typename Traits::Storage Storage;
171  typedef typename Traits::PointRef PointRef;
172  typedef typename Traits::Simplex Simplex;
173 
175  : index_(0),
176  simplex_(s) {
177  }
178 
179  void PushBack(PointRef p) {
180  simplex_.V[index_++] = p;
181  }
182 
183  private:
184  uint8_t index_;
186 };
187 
190 
196 template <class Traits, class Derived, class Iterator>
198  typename Traits::Storage& storage, const typename Traits::Point& x,
199  Iterator begin, Iterator end,
200  Eigen::MatrixBase<Derived>* L,
201  typename Traits::Point* x_proj);
202 
205 
218 template <class Traits, class OutputIterator>
219 typename Traits::Scalar SimplexDistance(
220  typename Traits::Storage& storage,
221  const typename Traits::Point& x,
222  const typename Traits::SimplexRef s_ref,
223  typename Traits::Point* x_proj,
224  OutputIterator V_out);
225 
229 template <class Traits>
231  public:
232  typedef typename Traits::Storage Storage;
233  typedef typename Traits::Scalar Scalar;
234  typedef typename Traits::Point Point;
235  typedef typename Traits::PointRef PointRef;
236 
238  V_feature_.reserve(Traits::NDim+1);
239  V_best_feature_.reserve(Traits::NDim+1);
240  min_distance_ = std::numeric_limits<Scalar>::max();
241  }
242 
243  template <typename InputIterator>
245  InputIterator begin,
246  InputIterator end) {
247  for (InputIterator iter = begin; iter != end; iter++) {
248  V_feature_.push_back(*iter);
249  BarycentricProjection<Traits>(storage, x, V_feature_.begin(),
250  V_feature_.end(), &L_, &x_proj_);
251  if (L_.minCoeff() >= 0) {
252  distance_ = (x_proj_ - x).squaredNorm();
253  if (distance_ < min_distance_) {
257  }
258  }
259 
260  Compute(storage, x, iter + 1, end);
261  V_feature_.pop_back();
262  }
263  return *this;
264  }
265 
266  template <typename OutputIterator>
267  void GetResult(Scalar* distance, Point* x_proj, OutputIterator V_out) {
268  *distance = std::sqrt(min_distance_);
269  *x_proj = x_best_proj_;
270  std::copy(V_best_feature_.begin(), V_best_feature_.end(), V_out);
271  }
272 
273  private:
274  std::vector<PointRef> V_feature_;
275  Eigen::Matrix<Scalar, Eigen::Dynamic, 1, 0, Traits::NDim + 1, 1> L_;
278 
279  std::vector<PointRef> V_best_feature_;
282 };
283 
284 } // namespace edelsbrunner96
285 
286 #endif // EDELSBRUNNER96_SIMPLEX_H_
Traits::Point Point
Definition: simplex.h:91
Point c
circumcenter
Definition: simplex.h:111
std::bitset< simplex::NUM_BITS > MarkedVec
type for marked bit flags
Definition: simplex.h:98
std::vector< PointRef > V_best_feature_
Definition: simplex.h:279
std::array< PointRef, Traits::NDim+1 > V
Definition: simplex.h:106
simplex has been touched during a fuzzy walk
Definition: simplex.h:57
Traits::Scalar Scalar
Definition: simplex.h:90
Eigen::Matrix< typename Traits::Scalar, Traits::NDim+1, 1 > BarycentricCoordinates(Storage &storage, const typename Traits::Point &xq) const
Given a simplex as the convex hull of the points in V, compute the barycentric coordinates of the poi...
Definition: simplex.hpp:123
A convenience for setting vertices of a simplex.
Definition: simplex.h:168
Scalar r2
circumradius squared
Definition: simplex.h:112
Traits::Simplex Simplex
Definition: simplex.h:93
std::array< SimplexRef, Traits::NDim+1 > N
neighbors, mapped to vertices
Definition: simplex.h:109
has been queud in breadth-first search
Definition: simplex.h:51
ExhaustiveSimplexDistance< Traits > & Compute(Storage &storage, const Point &x, InputIterator begin, InputIterator end)
Definition: simplex.h:244
is a member of the induced subcomplex
Definition: simplex.h:50
Eigen::Matrix< typename Traits::Scalar, Traits::NDim, 1 > CoordinatesOf(Storage &storage, const typename Traits::Point &xq) const
Given a simplex as the convex hull of the points in V, compute the coordinates of the point xq on the...
Definition: simplex.hpp:103
Traits::Simplex Simplex
Definition: simplex.h:172
simplex has been touched during feature walk
Definition: simplex.h:53
MarkedBits
enum defining bits for various markings of a simplex
Definition: simplex.h:48
Traits::Storage Storage
Definition: simplex.h:95
void SetNeighborAcross(PointRef q, SimplexRef n)
set the neighbor across the specified vertex
Definition: simplex.hpp:89
std::vector< PointRef > V_feature_
Definition: simplex.h:274
A simplex in the triangulation. A simplex is the convex hull of Ndim+1 points in R^NDim.
Definition: simplex.h:89
Computes the distance of a point to a simplex by expanding all combinations of vertices and evaluatin...
Definition: simplex.h:230
simplex has been touched during hull walk
Definition: simplex.h:55
Traits::PointRef PointRef
Definition: simplex.h:171
MarkedVec marked
flags for marking
Definition: simplex.h:100
void GetResult(Scalar *distance, Point *x_proj, OutputIterator V_out)
Definition: simplex.h:267
Traits::Storage Storage
Definition: simplex.h:170
Traits::PointRef PointRef
Definition: simplex.h:92
void ComputeCenter(Storage &storage)
called after vertices are filled computes the circumcenter
Definition: simplex.hpp:153
SimplexBase()
initializes version and flags; in debug mode, also zeros out arrays
Definition: simplex.hpp:37
simpex is a member of the visible hull
Definition: simplex.h:56
bool Contains(Storage &storage, const typename Traits::Point &xq) const
Interference test, return true if the query point lies inside the closed hull of the point set...
Definition: simplex.hpp:146
simplex has been marked for removal
Definition: simplex.h:52
simplex has been removed during insertion of a point
Definition: simplex.h:58
Traits::SimplexRef SimplexRef
Definition: simplex.h:94
Char8_t * copy(const Char8_t *s)
queued in induced subcomplex search
Definition: simplex.h:49
void SetVertices(PointRefs...refs)
Set the vertices of a simplex directly from a list of point refs.
Definition: simplex.hpp:51
uint8_t IndexOf(PointRef v) const
return the index of the specified vertex
Definition: simplex.hpp:95
void PushBack(PointRef p)
Definition: simplex.h:179
void BarycentricProjection(typename Traits::Storage &storage, const typename Traits::Point &x, Iterator begin, Iterator end, Eigen::MatrixBase< Derived > *L, typename Traits::Point *x_proj)
Given a query point, x and a set of vertex points, V, return the point y in hull(V) which is closest ...
Definition: simplex.hpp:177
uint16_t version
incremented when flipped
Definition: simplex.h:103
SimplexRef NeighborAcross(PointRef q) const
return the neighbor across the specified vertex
Definition: simplex.hpp:82
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, 0, Traits::NDim+1, 1 > L_
Definition: simplex.h:275
Traits::Scalar SimplexDistance(typename Traits::Storage &storage, const typename Traits::Point &x, const typename Traits::SimplexRef s_ref, typename Traits::Point *x_proj, OutputIterator V_out)
Given a set of <= NDim+1 vertices, compute the distance of the query point to the convex hull of the ...
Definition: simplex.hpp:213
simplex has been touched during line walk
Definition: simplex.h:54