CDT  1.4.5
C++ library for constrained Delaunay triangulation
Loading...
Searching...
No Matches
CDTUtils.hpp
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
9#ifndef CDT_zWlHcbQZQyBBqPgxvFDT
10#define CDT_zWlHcbQZQyBBqPgxvFDT
11
12#include "CDTUtils.h"
13
14#include "predicates.h" // robust predicates: orient, in-circle
15
16#include <stdexcept>
17
18namespace CDT
19{
20
21CDT_INLINE_IF_HEADER_ONLY Index ccw(Index i)
22{
23 return Index((i + 1) % 3);
24}
25
26CDT_INLINE_IF_HEADER_ONLY Index cw(Index i)
27{
28 return Index((i + 2) % 3);
29}
30
31CDT_INLINE_IF_HEADER_ONLY bool isOnEdge(const PtTriLocation::Enum location)
32{
33 return location == PtTriLocation::OnEdge1 ||
34 location == PtTriLocation::OnEdge2 ||
35 location == PtTriLocation::OnEdge3;
36}
37
38CDT_INLINE_IF_HEADER_ONLY Index edgeNeighbor(const PtTriLocation::Enum location)
39{
40 assert(isOnEdge(location));
41 return static_cast<Index>(location - PtTriLocation::OnEdge1);
42}
43
44template <typename T>
45T orient2D(const V2d<T>& p, const V2d<T>& v1, const V2d<T>& v2)
46{
47 return predicates::adaptive::orient2d(v1.x, v1.y, v2.x, v2.y, p.x, p.y);
48}
49
50template <typename T>
52 const V2d<T>& p,
53 const V2d<T>& v1,
54 const V2d<T>& v2,
55 const T orientationTolerance)
56{
57 return classifyOrientation(orient2D(p, v1, v2), orientationTolerance);
58}
59
60template <typename T>
62classifyOrientation(const T orientation, const T orientationTolerance)
63{
64 if(orientation < -orientationTolerance)
65 return PtLineLocation::Right;
66 if(orientation > orientationTolerance)
67 return PtLineLocation::Left;
68 return PtLineLocation::OnLine;
69}
70
71template <typename T>
73 const V2d<T>& p,
74 const V2d<T>& v1,
75 const V2d<T>& v2,
76 const V2d<T>& v3)
77{
78 using namespace predicates::adaptive;
79 PtTriLocation::Enum result = PtTriLocation::Inside;
80 PtLineLocation::Enum edgeCheck = locatePointLine(p, v1, v2);
81 if(edgeCheck == PtLineLocation::Right)
82 return PtTriLocation::Outside;
83 if(edgeCheck == PtLineLocation::OnLine)
84 result = PtTriLocation::OnEdge1;
85 edgeCheck = locatePointLine(p, v2, v3);
86 if(edgeCheck == PtLineLocation::Right)
87 return PtTriLocation::Outside;
88 if(edgeCheck == PtLineLocation::OnLine)
89 {
90 result = (result == PtTriLocation::Inside) ? PtTriLocation::OnEdge2
91 : PtTriLocation::OnVertex;
92 }
93 edgeCheck = locatePointLine(p, v3, v1);
94 if(edgeCheck == PtLineLocation::Right)
95 return PtTriLocation::Outside;
96 if(edgeCheck == PtLineLocation::OnLine)
97 {
98 result = (result == PtTriLocation::Inside) ? PtTriLocation::OnEdge3
99 : PtTriLocation::OnVertex;
100 }
101 return result;
102}
103
104CDT_INLINE_IF_HEADER_ONLY Index opoNbr(const Index vertIndex)
105{
106 if(vertIndex == Index(0))
107 return Index(1);
108 if(vertIndex == Index(1))
109 return Index(2);
110 if(vertIndex == Index(2))
111 return Index(0);
112 assert(false && "Invalid vertex index");
113 handleException(std::runtime_error("Invalid vertex index"));
114 return invalidIndex;
115}
116
117CDT_INLINE_IF_HEADER_ONLY Index opoVrt(const Index neighborIndex)
118{
119 if(neighborIndex == Index(0))
120 return Index(2);
121 if(neighborIndex == Index(1))
122 return Index(0);
123 if(neighborIndex == Index(2))
124 return Index(1);
125 assert(false && "Invalid neighbor index");
126 handleException(std::runtime_error("Invalid neighbor index"));
127 return invalidIndex;
128}
129
130CDT_INLINE_IF_HEADER_ONLY Index
132{
133 assert(vv[0] == iVert || vv[1] == iVert || vv[2] == iVert);
134 if(vv[0] == iVert)
135 return Index(1);
136 if(vv[1] == iVert)
137 return Index(2);
138 return Index(0);
139}
140
141CDT_INLINE_IF_HEADER_ONLY Index edgeNeighborInd(
142 const VerticesArr3& vv,
143 const VertInd iVedge1,
144 const VertInd iVedge2)
145{
146 assert(vv[0] == iVedge1 || vv[1] == iVedge1 || vv[2] == iVedge1);
147 assert(vv[0] == iVedge2 || vv[1] == iVedge2 || vv[2] == iVedge2);
148 assert(
149 (vv[0] != iVedge1 && vv[0] != iVedge2) ||
150 (vv[1] != iVedge1 && vv[1] != iVedge2) ||
151 (vv[2] != iVedge1 && vv[2] != iVedge2));
152 /*
153 * vv[2]
154 * /\
155 * n[2]/ \n[1]
156 * /____\
157 * vv[0] n[0] vv[1]
158 */
159 if(vv[0] == iVedge1)
160 {
161 if(vv[1] == iVedge2)
162 return Index(0);
163 return Index(2);
164 }
165 if(vv[0] == iVedge2)
166 {
167 if(vv[1] == iVedge1)
168 return Index(0);
169 return Index(2);
170 }
171 return Index(1);
172}
173
174CDT_INLINE_IF_HEADER_ONLY Index
175opposedVertexInd(const NeighborsArr3& nn, const TriInd iTopo)
176{
177 assert(nn[0] == iTopo || nn[1] == iTopo || nn[2] == iTopo);
178 if(nn[0] == iTopo)
179 return Index(2);
180 if(nn[1] == iTopo)
181 return Index(0);
182 return Index(1);
183}
184
185CDT_INLINE_IF_HEADER_ONLY Index
186vertexInd(const VerticesArr3& vv, const VertInd iV)
187{
188 assert(vv[0] == iV || vv[1] == iV || vv[2] == iV);
189 if(vv[0] == iV)
190 return Index(0);
191 if(vv[1] == iV)
192 return Index(1);
193 return Index(2);
194}
195
196CDT_INLINE_IF_HEADER_ONLY TriInd
197opposedTriangle(const Triangle& tri, const VertInd iVert)
198{
199 return tri.neighbors[opposedTriangleInd(tri.vertices, iVert)];
200}
201
202CDT_INLINE_IF_HEADER_ONLY VertInd
203opposedVertex(const Triangle& tri, const TriInd iTopo)
204{
205 return tri.vertices[opposedVertexInd(tri.neighbors, iTopo)];
206}
207
209CDT_INLINE_IF_HEADER_ONLY TriInd
210edgeNeighbor(const Triangle& tri, VertInd iVedge1, VertInd iVedge2)
211{
212 return tri.neighbors[edgeNeighborInd(tri.vertices, iVedge1, iVedge2)];
213}
214
215template <typename T>
217 const V2d<T>& p,
218 const V2d<T>& v1,
219 const V2d<T>& v2,
220 const V2d<T>& v3)
221{
222 using namespace predicates::adaptive;
223 return incircle(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, p.x, p.y) > T(0);
224}
225
226CDT_INLINE_IF_HEADER_ONLY
227bool verticesShareEdge(const TriIndVec& aTris, const TriIndVec& bTris)
228{
229 for(TriIndVec::const_iterator it = aTris.begin(); it != aTris.end(); ++it)
230 if(std::find(bTris.begin(), bTris.end(), *it) != bTris.end())
231 return true;
232 return false;
233}
234
235template <typename T>
236T distanceSquared(const T ax, const T ay, const T bx, const T by)
237{
238 const T dx = bx - ax;
239 const T dy = by - ay;
240 return dx * dx + dy * dy;
241}
242
243template <typename T>
244T distance(const T ax, const T ay, const T bx, const T by)
245{
246 return std::sqrt(distanceSquared(ax, ay, bx, by));
247}
248
249template <typename T>
250T distance(const V2d<T>& a, const V2d<T>& b)
251{
252 return distance(a.x, a.y, b.x, b.y);
253}
254
255template <typename T>
256T distanceSquared(const V2d<T>& a, const V2d<T>& b)
257{
258 return distanceSquared(a.x, a.y, b.x, b.y);
259}
260
262{
263 return t.vertices[0] < 3 || t.vertices[1] < 3 || t.vertices[2] < 3;
264}
265
266} // namespace CDT
267
268#endif // header-guard
Utilities and helpers.
Namespace containing triangulation functionality.
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:203
std::vector< TriInd > TriIndVec
Vector of triangle indices.
Definition CDTUtils.h:225
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:141
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:131
CDT_EXPORT PtLineLocation::Enum classifyOrientation(T orientation, T orientationTolerance=T(0))
Classify value of orient2d predicate.
Definition CDTUtils.hpp:62
array< TriInd, 3 > NeighborsArr3
array of three neighbors
Definition CDTUtils.h:227
CDT_EXPORT T distance(const V2d< T > &a, const V2d< T > &b)
Distance between two 2D points.
Definition CDTUtils.hpp:250
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:207
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:227
CDT_EXPORT Index cw(Index i)
Advance vertex or neighbor index clockwise.
Definition CDTUtils.hpp:26
array< VertInd, 3 > VerticesArr3
array of three vertex indices
Definition CDTUtils.h:226
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:261
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opoVrt(Index neighborIndex)
Opposed vertex index from neighbor index.
Definition CDTUtils.hpp:117
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opoNbr(Index vertIndex)
Opposed neighbor index from vertex index.
Definition CDTUtils.hpp:104
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:186
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:45
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:51
CDT_EXPORT T distanceSquared(const V2d< T > &a, const V2d< T > &b)
Squared distance between two 2D points.
Definition CDTUtils.hpp:256
CDT_EXPORT Index ccw(Index i)
Advance vertex or neighbor index counter-clockwise.
Definition CDTUtils.hpp:21
CDT_EXPORT Index edgeNeighbor(PtTriLocation::Enum location)
Neighbor index from a on-edge location.
Definition CDTUtils.hpp:38
unsigned char Index
Index in triangle.
Definition CDTUtils.h:205
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:197
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:216
CDT_EXPORT bool isOnEdge(PtTriLocation::Enum location)
Check if location is classified as on any of three edges.
Definition CDTUtils.hpp:31
IndexSizeType TriInd
Triangle index.
Definition CDTUtils.h:209
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:72
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:175
Triangulation triangle (counter-clockwise winding)
Definition CDTUtils.h:360
VerticesArr3 vertices
triangle's three vertices
Definition CDTUtils.h:361
NeighborsArr3 neighbors
triangle's three neighbors
Definition CDTUtils.h:362
2D vector
Definition CDTUtils.h:145
T y
Y-coordinate.
Definition CDTUtils.h:147
T x
X-coordinate.
Definition CDTUtils.h:146