CDT  1.4.1
C++ library for constrained Delaunay triangulation
Loading...
Searching...
No Matches
CDTUtils.h
Go to the documentation of this file.
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
10#ifndef CDT_obwOaxOTdAWcLNTlNnaq
11#define CDT_obwOaxOTdAWcLNTlNnaq
12
13#ifdef CDT_DONT_USE_BOOST_RTREE
14// CDT_DONT_USE_BOOST_RTREE was replaced with CDT_USE_BOOST
15typedef char CDT_DONT_USE_BOOST_RTREE__was__replaced__with__CDT_USE_BOOST[-1];
16#endif
17
18// #define CDT_USE_STRONG_TYPING // strong type checks on indices
19
20// check if c++11 is supported
21#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
22#define CDT_CXX11_IS_SUPPORTED
23#elif !defined(__cplusplus) && !defined(_MSC_VER)
25#endif
26
27// Functions defined outside the class need to be 'inline'
28// if CDT is configured to be used as header-only library:
29// single-definition rule is violated otherwise
30#ifdef CDT_USE_AS_COMPILED_LIBRARY
31#define CDT_INLINE_IF_HEADER_ONLY
32#include "cdt_export.h" // automatically generated by CMake
33#else
38#define CDT_INLINE_IF_HEADER_ONLY inline
40#define CDT_EXPORT
41#endif
42
43#include <algorithm>
44#include <cassert>
45#include <cmath>
46#include <limits>
47#include <vector>
48
49#ifdef CDT_USE_STRONG_TYPING
50#include <boost/serialization/strong_typedef.hpp>
51#endif
52
53// use fall-backs for c++11 features
54#ifdef CDT_CXX11_IS_SUPPORTED
55
56#include <array>
57#include <functional>
58#include <tuple>
59#include <unordered_map>
60#include <unordered_set>
61namespace CDT
62{
63using std::array;
64using std::get;
65using std::make_tuple;
66using std::tie;
67using std::tuple;
68using std::unordered_map;
69using std::unordered_set;
70} // namespace CDT
71
72#else
73#include <boost/array.hpp>
74#include <boost/functional/hash.hpp>
75#include <boost/tuple/tuple.hpp>
76#include <boost/unordered_map.hpp>
77#include <boost/unordered_set.hpp>
78namespace CDT
79{
80using boost::array;
81using boost::get;
82using boost::make_tuple;
83using boost::tie;
84using boost::tuple;
85using boost::unordered_map;
86using boost::unordered_set;
87} // namespace CDT
88#endif
89
90namespace CDT
91{
92
94template <typename T>
96{
97 T x;
98 T y;
99
101 static V2d make(T x, T y);
102};
103
105template <typename T>
106const T& getX_V2d(const V2d<T>& v)
107{
108 return v.x;
109}
110
112template <typename T>
113const T& getY_V2d(const V2d<T>& v)
114{
115 return v.y;
116}
117
119template <typename T>
120bool operator==(const CDT::V2d<T>& lhs, const CDT::V2d<T>& rhs)
121{
122 return lhs.x == rhs.x && lhs.y == rhs.y;
123}
124
125#ifdef CDT_USE_64_BIT_INDEX_TYPE
126typedef unsigned long long IndexSizeType;
127#else
128typedef unsigned int IndexSizeType;
129#endif
130
131#ifdef CDT_USE_STRONG_TYPING
133BOOST_STRONG_TYPEDEF(unsigned char, Index);
135BOOST_STRONG_TYPEDEF(IndexSizeType, VertInd);
137BOOST_STRONG_TYPEDEF(IndexSizeType, TriInd);
138#else
140typedef unsigned char Index;
142typedef IndexSizeType VertInd;
144typedef IndexSizeType TriInd;
145#endif
146
148const static IndexSizeType
149 invalidIndex(std::numeric_limits<IndexSizeType>::max());
151const static TriInd noNeighbor(invalidIndex);
153const static VertInd noVertex(invalidIndex);
154
155typedef std::vector<TriInd> TriIndVec;
156typedef array<VertInd, 3> VerticesArr3;
157typedef array<TriInd, 3> NeighborsArr3;
158
160template <typename T>
162{
165
167 void envelopPoint(const V2d<T>& p)
168 {
169 envelopPoint(p.x, p.y);
170 }
172 void envelopPoint(const T x, const T y)
173 {
174 min.x = std::min(x, min.x);
175 max.x = std::max(x, max.x);
176 min.y = std::min(y, min.y);
177 max.y = std::max(y, max.y);
178 }
179};
180
182template <
183 typename T,
184 typename TVertexIter,
185 typename TGetVertexCoordX,
186 typename TGetVertexCoordY>
188 TVertexIter first,
189 TVertexIter last,
190 TGetVertexCoordX getX,
191 TGetVertexCoordY getY)
192{
193 const T max = std::numeric_limits<T>::max();
194 Box2d<T> box = {{max, max}, {-max, -max}};
195 for(; first != last; ++first)
196 {
197 box.envelopPoint(getX(*first), getY(*first));
198 }
199 return box;
200}
201
203template <typename T>
204CDT_EXPORT Box2d<T> envelopBox(const std::vector<V2d<T> >& vertices);
205
209{
211 Edge(VertInd iV1, VertInd iV2);
213 bool operator==(const Edge& other) const;
215 bool operator!=(const Edge& other) const;
217 VertInd v1() const;
219 VertInd v2() const;
221 const std::pair<VertInd, VertInd>& verts() const;
222
223private:
224 std::pair<VertInd, VertInd> m_vertices;
225};
226
228inline VertInd edge_get_v1(const Edge& e)
229{
230 return e.v1();
231}
232
234inline VertInd edge_get_v2(const Edge& e)
235{
236 return e.v2();
237}
238
241{
242 return Edge(iV1, iV2);
243}
244
245typedef std::vector<Edge> EdgeVec;
246typedef unordered_set<Edge> EdgeUSet;
247typedef unordered_set<TriInd> TriIndUSet;
248typedef unordered_map<TriInd, TriInd> TriIndUMap;
249
251/*
252 * v3
253 * /\
254 * n3/ \n2
255 * /____\
256 * v1 n1 v2
257 */
259{
262
268 static Triangle
269 make(const array<VertInd, 3>& vertices, const array<TriInd, 3>& neighbors)
270 {
271 Triangle t = {vertices, neighbors};
272 return t;
273 }
274
277 std::pair<TriInd, VertInd> next(const VertInd i) const
278 {
279 assert(vertices[0] == i || vertices[1] == i || vertices[2] == i);
280 if(vertices[0] == i)
281 {
282 return std::make_pair(neighbors[0], vertices[1]);
283 }
284 if(vertices[1] == i)
285 {
286 return std::make_pair(neighbors[1], vertices[2]);
287 }
288 return std::make_pair(neighbors[2], vertices[0]);
289 }
292 std::pair<TriInd, VertInd> prev(const VertInd i) const
293 {
294 assert(vertices[0] == i || vertices[1] == i || vertices[2] == i);
295 if(vertices[0] == i)
296 return std::make_pair(neighbors[2], vertices[2]);
297 if(vertices[1] == i)
298 return std::make_pair(neighbors[0], vertices[0]);
299 return std::make_pair(neighbors[1], vertices[1]);
300 }
301
302 bool containsVertex(const VertInd i) const
303 {
304 return std::find(vertices.begin(), vertices.end(), i) != vertices.end();
305 }
306};
307
308typedef std::vector<Triangle> TriangleVec;
309
312
315
318{
320 enum Enum
321 {
322 Inside,
323 Outside,
324 OnEdge1,
325 OnEdge2,
326 OnEdge3,
327 OnVertex,
328 };
329};
330
333
337
340{
342 enum Enum
343 {
344 Left,
345 Right,
346 OnLine,
347 };
348};
349
351template <typename T>
352CDT_EXPORT T orient2D(const V2d<T>& p, const V2d<T>& v1, const V2d<T>& v2);
353
355template <typename T>
357 const V2d<T>& p,
358 const V2d<T>& v1,
359 const V2d<T>& v2,
360 T orientationTolerance = T(0));
361
363template <typename T>
365classifyOrientation(T orientation, T orientationTolerance = T(0));
366
368template <typename T>
370 const V2d<T>& p,
371 const V2d<T>& v1,
372 const V2d<T>& v2,
373 const V2d<T>& v3);
374
377
380
383opposedTriangleInd(const VerticesArr3& vv, VertInd iVert);
384
387edgeNeighborInd(const VerticesArr3& vv, VertInd iVedge1, VertInd iVedge2);
388
391opposedVertexInd(const NeighborsArr3& nn, TriInd iTopo);
392
395vertexInd(const VerticesArr3& vv, VertInd iV);
396
399opposedTriangle(const Triangle& tri, VertInd iVert);
400
403edgeNeighbor(const Triangle& tri, VertInd iVedge1, VertInd iVedge2);
404
407opposedVertex(const Triangle& tri, TriInd iTopo);
408
410template <typename T>
412 const V2d<T>& p,
413 const V2d<T>& v1,
414 const V2d<T>& v2,
415 const V2d<T>& v3);
416
419verticesShareEdge(const TriIndVec& aTris, const TriIndVec& bTris);
420
422template <typename T>
423CDT_EXPORT T distance(const V2d<T>& a, const V2d<T>& b);
424
426template <typename T>
427CDT_EXPORT T distanceSquared(const V2d<T>& a, const V2d<T>& b);
428
430CDT_INLINE_IF_HEADER_ONLY bool touchesSuperTriangle(const Triangle& t);
431
432} // namespace CDT
433
434#ifndef CDT_USE_AS_COMPILED_LIBRARY
435#include "CDTUtils.hpp"
436#endif
437
438//*****************************************************************************
439// Specialize hash functions
440//*****************************************************************************
441#ifdef CDT_CXX11_IS_SUPPORTED
442namespace std
443#else
444namespace boost
445#endif
446{
447
448#ifdef CDT_USE_STRONG_TYPING
449
451template <>
452struct hash<CDT::VertInd>
453{
455 std::size_t operator()(const CDT::VertInd& vi) const
456 {
457 return std::hash<std::size_t>()(vi.t);
458 }
459};
460
462template <>
463struct hash<CDT::TriInd>
464{
466 std::size_t operator()(const CDT::TriInd& vi) const
467 {
468 return std::hash<std::size_t>()(vi.t);
469 }
470};
471
472#endif // CDT_USE_STRONG_TYPING
473
475template <>
476struct hash<CDT::Edge>
477{
479 std::size_t operator()(const CDT::Edge& e) const
480 {
481 return hashEdge(e);
482 }
483
484private:
485 static void hashCombine(std::size_t& seed, const CDT::VertInd& key)
486 {
487#ifdef CDT_CXX11_IS_SUPPORTED
488 typedef std::hash<CDT::VertInd> Hasher;
489#else
490 typedef boost::hash<CDT::VertInd> Hasher;
491#endif
492 seed ^= Hasher()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
493 }
494 static std::size_t hashEdge(const CDT::Edge& e)
495 {
496 const std::pair<CDT::VertInd, CDT::VertInd>& vv = e.verts();
497 std::size_t seed1(0);
498 hashCombine(seed1, vv.first);
499 hashCombine(seed1, vv.second);
500 std::size_t seed2(0);
501 hashCombine(seed2, vv.second);
502 hashCombine(seed2, vv.first);
503 return std::min(seed1, seed2);
504 }
505};
506} // namespace std/boost
507
508#endif // header guard
#define CDT_EXPORT
Export not needed in header-only mode.
Definition CDTUtils.h:40
char couldnt_parse_cxx_standard[-1]
Error: couldn't parse standard.
Definition CDTUtils.h:24
#define CDT_INLINE_IF_HEADER_ONLY
Macro for inlining non-template functions when in header-only mode to avoid multiple declaration erro...
Definition CDTUtils.h:38
Utilities and helpers - implementation.
Namespace containing triangulation functionality.
std::vector< Edge > EdgeVec
Vector of edges.
Definition CDTUtils.h:245
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY VertInd opposedVertex(const Triangle &tri, TriInd iTopo)
Given two triangles, return vertex of first triangle opposed to the second.
Definition CDTUtils.hpp:255
unordered_set< Edge > EdgeUSet
Hash table of edges.
Definition CDTUtils.h:246
VertInd edge_get_v2(const Edge &e)
Get edge second vertex.
Definition CDTUtils.h:234
std::vector< TriInd > TriIndVec
Vector of triangle indices.
Definition CDTUtils.h:155
Box2d< T > envelopBox(TVertexIter first, TVertexIter last, TGetVertexCoordX getX, TGetVertexCoordY getY)
Bounding box of a collection of custom 2D points given coordinate getters.
Definition CDTUtils.h:187
CDT_INLINE_IF_HEADER_ONLY Index edgeNeighborInd(const VerticesArr3 &vv, VertInd iVedge1, VertInd iVedge2)
Index of triangle's neighbor opposed to an edge.
Definition CDTUtils.hpp:193
VertInd edge_get_v1(const Edge &e)
Get edge first vertex.
Definition CDTUtils.h:228
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opposedTriangleInd(const VerticesArr3 &vv, VertInd iVert)
Index of triangle's neighbor opposed to a vertex.
Definition CDTUtils.hpp:183
CDT_EXPORT PtLineLocation::Enum classifyOrientation(T orientation, T orientationTolerance=T(0))
Classify value of orient2d predicate.
Definition CDTUtils.hpp:116
array< TriInd, 3 > NeighborsArr3
array of three neighbors
Definition CDTUtils.h:157
CDT_EXPORT T distance(const V2d< T > &a, const V2d< T > &b)
Distance between two 2D points.
Definition CDTUtils.hpp:302
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:142
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY bool verticesShareEdge(const TriIndVec &aTris, const TriIndVec &bTris)
Test if two vertices share at least one common triangle.
Definition CDTUtils.hpp:279
CDT_EXPORT Index cw(Index i)
Advance vertex or neighbor index clockwise.
Definition CDTUtils.hpp:80
array< VertInd, 3 > VerticesArr3
array of three vertex indices
Definition CDTUtils.h:156
CDT_INLINE_IF_HEADER_ONLY bool touchesSuperTriangle(const Triangle &t)
Check if any of triangle's vertices belongs to a super-triangle.
Definition CDTUtils.hpp:313
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opoVrt(Index neighborIndex)
Opposed vertex index from neighbor index.
Definition CDTUtils.hpp:170
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opoNbr(Index vertIndex)
Opposed neighbor index from vertex index.
Definition CDTUtils.hpp:158
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index vertexInd(const VerticesArr3 &vv, VertInd iV)
If triangle has a given vertex return vertex-index.
Definition CDTUtils.hpp:238
CDT_EXPORT T orient2D(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2)
Orient p against line v1-v2 2D: robust geometric predicate.
Definition CDTUtils.hpp:99
CDT_EXPORT PtLineLocation::Enum locatePointLine(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, T orientationTolerance=T(0))
Check if point lies to the left of, to the right of, or on a line.
Definition CDTUtils.hpp:105
CDT_EXPORT T distanceSquared(const V2d< T > &a, const V2d< T > &b)
Squared distance between two 2D points.
Definition CDTUtils.hpp:308
unordered_set< TriInd > TriIndUSet
Hash table of triangles.
Definition CDTUtils.h:247
CDT_EXPORT Index ccw(Index i)
Advance vertex or neighbor index counter-clockwise.
Definition CDTUtils.hpp:75
Edge edge_make(VertInd iV1, VertInd iV2)
Get edge second vertex.
Definition CDTUtils.h:240
CDT_EXPORT Index edgeNeighbor(PtTriLocation::Enum location)
Neighbor index from a on-edge location.
Definition CDTUtils.hpp:92
unordered_map< TriInd, TriInd > TriIndUMap
Triangle hash map.
Definition CDTUtils.h:248
const T & getX_V2d(const V2d< T > &v)
X- coordinate getter for V2d.
Definition CDTUtils.h:106
bool operator==(const CDT::V2d< T > &lhs, const CDT::V2d< T > &rhs)
If two 2D vectors are exactly equal.
Definition CDTUtils.h:120
unsigned char Index
Index in triangle.
Definition CDTUtils.h:140
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY TriInd opposedTriangle(const Triangle &tri, VertInd iVert)
Given triangle and a vertex find opposed triangle.
Definition CDTUtils.hpp:249
CDT_EXPORT bool isInCircumcircle(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, const V2d< T > &v3)
Test if point lies in a circumscribed circle of a triangle.
Definition CDTUtils.hpp:268
CDT_EXPORT bool isOnEdge(PtTriLocation::Enum location)
Check if location is classified as on any of three edges.
Definition CDTUtils.hpp:85
IndexSizeType TriInd
Triangle index.
Definition CDTUtils.h:144
CDT_EXPORT PtTriLocation::Enum locatePointTriangle(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, const V2d< T > &v3)
Check if point a lies inside of, outside of, or on an edge of a triangle.
Definition CDTUtils.hpp:126
std::vector< Triangle > TriangleVec
Vector of triangles.
Definition CDTUtils.h:308
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opposedVertexInd(const NeighborsArr3 &nn, TriInd iTopo)
Index of triangle's vertex opposed to a triangle.
Definition CDTUtils.hpp:227
const T & getY_V2d(const V2d< T > &v)
Y-coordinate getter for V2d.
Definition CDTUtils.h:113
2D bounding box
Definition CDTUtils.h:162
void envelopPoint(const T x, const T y)
Envelop box around a point with given coordinates.
Definition CDTUtils.h:172
V2d< T > max
max box corner
Definition CDTUtils.h:164
V2d< T > min
min box corner
Definition CDTUtils.h:163
void envelopPoint(const V2d< T > &p)
Envelop box around a point.
Definition CDTUtils.h:167
Edge connecting two vertices: vertex with smaller index is always first.
Definition CDTUtils.h:209
const std::pair< VertInd, VertInd > & verts() const
Edges' vertices.
Definition CDTUtils.hpp:67
VertInd v2() const
V2 getter.
Definition CDTUtils.hpp:62
VertInd v1() const
V1 getter.
Definition CDTUtils.hpp:57
Relative location of point to a line.
Definition CDTUtils.h:340
Location of point on a triangle.
Definition CDTUtils.h:318
Triangulation triangle (counter-clockwise winding)
Definition CDTUtils.h:259
VerticesArr3 vertices
triangle's three vertices
Definition CDTUtils.h:260
std::pair< TriInd, VertInd > prev(const VertInd i) const
Previous triangle adjacent to a vertex (counter-clockwise)
Definition CDTUtils.h:292
static Triangle make(const array< VertInd, 3 > &vertices, const array< TriInd, 3 > &neighbors)
Factory method.
Definition CDTUtils.h:269
NeighborsArr3 neighbors
triangle's three neighbors
Definition CDTUtils.h:261
std::pair< TriInd, VertInd > next(const VertInd i) const
Next triangle adjacent to a vertex (clockwise)
Definition CDTUtils.h:277
2D vector
Definition CDTUtils.h:96
T y
Y-coordinate.
Definition CDTUtils.h:98
T x
X-coordinate.
Definition CDTUtils.h:97
std::size_t operator()(const CDT::Edge &e) const
Hash operator.
Definition CDTUtils.h:479