1
+ /*
2
+ * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
3
+ * http://code.google.com/p/poly2tri/
4
+ *
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without modification,
8
+ * are permitted provided that the following conditions are met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ * * Neither the name of Poly2Tri nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without specific
17
+ * prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ #include " shapes.h"
32
+ #include < iostream>
33
+
34
+ namespace p2t {
35
+
36
+ Triangle::Triangle (Point& a, Point& b, Point& c)
37
+ {
38
+ points_[0 ] = &a; points_[1 ] = &b; points_[2 ] = &c;
39
+ neighbors_[0 ] = NULL ; neighbors_[1 ] = NULL ; neighbors_[2 ] = NULL ;
40
+ constrained_edge[0 ] = constrained_edge[1 ] = constrained_edge[2 ] = false ;
41
+ delaunay_edge[0 ] = delaunay_edge[1 ] = delaunay_edge[2 ] = false ;
42
+ interior_ = false ;
43
+ }
44
+
45
+ // Update neighbor pointers
46
+ void Triangle::MarkNeighbor (Point* p1, Point* p2, Triangle* t)
47
+ {
48
+ if ((p1 == points_[2 ] && p2 == points_[1 ]) || (p1 == points_[1 ] && p2 == points_[2 ]))
49
+ neighbors_[0 ] = t;
50
+ else if ((p1 == points_[0 ] && p2 == points_[2 ]) || (p1 == points_[2 ] && p2 == points_[0 ]))
51
+ neighbors_[1 ] = t;
52
+ else if ((p1 == points_[0 ] && p2 == points_[1 ]) || (p1 == points_[1 ] && p2 == points_[0 ]))
53
+ neighbors_[2 ] = t;
54
+ else
55
+ assert (0 );
56
+ }
57
+
58
+ // Exhaustive search to update neighbor pointers
59
+ void Triangle::MarkNeighbor (Triangle& t)
60
+ {
61
+ if (t.Contains (points_[1 ], points_[2 ])) {
62
+ neighbors_[0 ] = &t;
63
+ t.MarkNeighbor (points_[1 ], points_[2 ], this );
64
+ } else if (t.Contains (points_[0 ], points_[2 ])) {
65
+ neighbors_[1 ] = &t;
66
+ t.MarkNeighbor (points_[0 ], points_[2 ], this );
67
+ } else if (t.Contains (points_[0 ], points_[1 ])) {
68
+ neighbors_[2 ] = &t;
69
+ t.MarkNeighbor (points_[0 ], points_[1 ], this );
70
+ }
71
+ }
72
+
73
+ /* *
74
+ * Clears all references to all other triangles and points
75
+ */
76
+ void Triangle::Clear ()
77
+ {
78
+ Triangle *t;
79
+ for ( int i=0 ; i<3 ; i++ )
80
+ {
81
+ t = neighbors_[i];
82
+ if ( t != NULL )
83
+ {
84
+ t->ClearNeighbor ( this );
85
+ }
86
+ }
87
+ ClearNeighbors ();
88
+ points_[0 ]=points_[1 ]=points_[2 ] = NULL ;
89
+ }
90
+
91
+ void Triangle::ClearNeighbor (const Triangle *triangle )
92
+ {
93
+ if ( neighbors_[0 ] == triangle )
94
+ {
95
+ neighbors_[0 ] = NULL ;
96
+ }
97
+ else if ( neighbors_[1 ] == triangle )
98
+ {
99
+ neighbors_[1 ] = NULL ;
100
+ }
101
+ else
102
+ {
103
+ neighbors_[2 ] = NULL ;
104
+ }
105
+ }
106
+
107
+ void Triangle::ClearNeighbors ()
108
+ {
109
+ neighbors_[0 ] = NULL ;
110
+ neighbors_[1 ] = NULL ;
111
+ neighbors_[2 ] = NULL ;
112
+ }
113
+
114
+ void Triangle::ClearDelunayEdges ()
115
+ {
116
+ delaunay_edge[0 ] = delaunay_edge[1 ] = delaunay_edge[2 ] = false ;
117
+ }
118
+
119
+ Point* Triangle::OppositePoint (Triangle& t, const Point& p)
120
+ {
121
+ Point *cw = t.PointCW (p);
122
+ return PointCW (*cw);
123
+ }
124
+
125
+ // Legalized triangle by rotating clockwise around point(0)
126
+ void Triangle::Legalize (Point& point)
127
+ {
128
+ points_[1 ] = points_[0 ];
129
+ points_[0 ] = points_[2 ];
130
+ points_[2 ] = &point;
131
+ }
132
+
133
+ // Legalize triagnle by rotating clockwise around oPoint
134
+ void Triangle::Legalize (Point& opoint, Point& npoint)
135
+ {
136
+ if (&opoint == points_[0 ]) {
137
+ points_[1 ] = points_[0 ];
138
+ points_[0 ] = points_[2 ];
139
+ points_[2 ] = &npoint;
140
+ } else if (&opoint == points_[1 ]) {
141
+ points_[2 ] = points_[1 ];
142
+ points_[1 ] = points_[0 ];
143
+ points_[0 ] = &npoint;
144
+ } else if (&opoint == points_[2 ]) {
145
+ points_[0 ] = points_[2 ];
146
+ points_[2 ] = points_[1 ];
147
+ points_[1 ] = &npoint;
148
+ } else {
149
+ assert (0 );
150
+ }
151
+ }
152
+
153
+ int Triangle::Index (const Point* p)
154
+ {
155
+ if (p == points_[0 ]) {
156
+ return 0 ;
157
+ } else if (p == points_[1 ]) {
158
+ return 1 ;
159
+ } else if (p == points_[2 ]) {
160
+ return 2 ;
161
+ }
162
+ assert (0 );
163
+ return -1 ;
164
+ }
165
+
166
+ int Triangle::EdgeIndex (const Point* p1, const Point* p2)
167
+ {
168
+ if (points_[0 ] == p1) {
169
+ if (points_[1 ] == p2) {
170
+ return 2 ;
171
+ } else if (points_[2 ] == p2) {
172
+ return 1 ;
173
+ }
174
+ } else if (points_[1 ] == p1) {
175
+ if (points_[2 ] == p2) {
176
+ return 0 ;
177
+ } else if (points_[0 ] == p2) {
178
+ return 2 ;
179
+ }
180
+ } else if (points_[2 ] == p1) {
181
+ if (points_[0 ] == p2) {
182
+ return 1 ;
183
+ } else if (points_[1 ] == p2) {
184
+ return 0 ;
185
+ }
186
+ }
187
+ return -1 ;
188
+ }
189
+
190
+ void Triangle::MarkConstrainedEdge (int index)
191
+ {
192
+ constrained_edge[index] = true ;
193
+ }
194
+
195
+ void Triangle::MarkConstrainedEdge (Edge& edge)
196
+ {
197
+ MarkConstrainedEdge (edge.p , edge.q );
198
+ }
199
+
200
+ // Mark edge as constrained
201
+ void Triangle::MarkConstrainedEdge (Point* p, Point* q)
202
+ {
203
+ if ((q == points_[0 ] && p == points_[1 ]) || (q == points_[1 ] && p == points_[0 ])) {
204
+ constrained_edge[2 ] = true ;
205
+ } else if ((q == points_[0 ] && p == points_[2 ]) || (q == points_[2 ] && p == points_[0 ])) {
206
+ constrained_edge[1 ] = true ;
207
+ } else if ((q == points_[1 ] && p == points_[2 ]) || (q == points_[2 ] && p == points_[1 ])) {
208
+ constrained_edge[0 ] = true ;
209
+ }
210
+ }
211
+
212
+ // The point counter-clockwise to given point
213
+ Point* Triangle::PointCW (const Point& point)
214
+ {
215
+ if (&point == points_[0 ]) {
216
+ return points_[2 ];
217
+ } else if (&point == points_[1 ]) {
218
+ return points_[0 ];
219
+ } else if (&point == points_[2 ]) {
220
+ return points_[1 ];
221
+ }
222
+ assert (0 );
223
+ return NULL ;
224
+ }
225
+
226
+ // The point counter-clockwise to given point
227
+ Point* Triangle::PointCCW (const Point& point)
228
+ {
229
+ if (&point == points_[0 ]) {
230
+ return points_[1 ];
231
+ } else if (&point == points_[1 ]) {
232
+ return points_[2 ];
233
+ } else if (&point == points_[2 ]) {
234
+ return points_[0 ];
235
+ }
236
+ assert (0 );
237
+ return NULL ;
238
+ }
239
+
240
+ // The neighbor clockwise to given point
241
+ Triangle* Triangle::NeighborCW (const Point& point)
242
+ {
243
+ if (&point == points_[0 ]) {
244
+ return neighbors_[1 ];
245
+ } else if (&point == points_[1 ]) {
246
+ return neighbors_[2 ];
247
+ }
248
+ return neighbors_[0 ];
249
+ }
250
+
251
+ // The neighbor counter-clockwise to given point
252
+ Triangle* Triangle::NeighborCCW (const Point& point)
253
+ {
254
+ if (&point == points_[0 ]) {
255
+ return neighbors_[2 ];
256
+ } else if (&point == points_[1 ]) {
257
+ return neighbors_[0 ];
258
+ }
259
+ return neighbors_[1 ];
260
+ }
261
+
262
+ bool Triangle::GetConstrainedEdgeCCW (const Point& p)
263
+ {
264
+ if (&p == points_[0 ]) {
265
+ return constrained_edge[2 ];
266
+ } else if (&p == points_[1 ]) {
267
+ return constrained_edge[0 ];
268
+ }
269
+ return constrained_edge[1 ];
270
+ }
271
+
272
+ bool Triangle::GetConstrainedEdgeCW (const Point& p)
273
+ {
274
+ if (&p == points_[0 ]) {
275
+ return constrained_edge[1 ];
276
+ } else if (&p == points_[1 ]) {
277
+ return constrained_edge[2 ];
278
+ }
279
+ return constrained_edge[0 ];
280
+ }
281
+
282
+ void Triangle::SetConstrainedEdgeCCW (const Point& p, bool ce)
283
+ {
284
+ if (&p == points_[0 ]) {
285
+ constrained_edge[2 ] = ce;
286
+ } else if (&p == points_[1 ]) {
287
+ constrained_edge[0 ] = ce;
288
+ } else {
289
+ constrained_edge[1 ] = ce;
290
+ }
291
+ }
292
+
293
+ void Triangle::SetConstrainedEdgeCW (const Point& p, bool ce)
294
+ {
295
+ if (&p == points_[0 ]) {
296
+ constrained_edge[1 ] = ce;
297
+ } else if (&p == points_[1 ]) {
298
+ constrained_edge[2 ] = ce;
299
+ } else {
300
+ constrained_edge[0 ] = ce;
301
+ }
302
+ }
303
+
304
+ bool Triangle::GetDelunayEdgeCCW (const Point& p)
305
+ {
306
+ if (&p == points_[0 ]) {
307
+ return delaunay_edge[2 ];
308
+ } else if (&p == points_[1 ]) {
309
+ return delaunay_edge[0 ];
310
+ }
311
+ return delaunay_edge[1 ];
312
+ }
313
+
314
+ bool Triangle::GetDelunayEdgeCW (const Point& p)
315
+ {
316
+ if (&p == points_[0 ]) {
317
+ return delaunay_edge[1 ];
318
+ } else if (&p == points_[1 ]) {
319
+ return delaunay_edge[2 ];
320
+ }
321
+ return delaunay_edge[0 ];
322
+ }
323
+
324
+ void Triangle::SetDelunayEdgeCCW (const Point& p, bool e)
325
+ {
326
+ if (&p == points_[0 ]) {
327
+ delaunay_edge[2 ] = e;
328
+ } else if (&p == points_[1 ]) {
329
+ delaunay_edge[0 ] = e;
330
+ } else {
331
+ delaunay_edge[1 ] = e;
332
+ }
333
+ }
334
+
335
+ void Triangle::SetDelunayEdgeCW (const Point& p, bool e)
336
+ {
337
+ if (&p == points_[0 ]) {
338
+ delaunay_edge[1 ] = e;
339
+ } else if (&p == points_[1 ]) {
340
+ delaunay_edge[2 ] = e;
341
+ } else {
342
+ delaunay_edge[0 ] = e;
343
+ }
344
+ }
345
+
346
+ // The neighbor across to given point
347
+ Triangle& Triangle::NeighborAcross (const Point& opoint)
348
+ {
349
+ if (&opoint == points_[0 ]) {
350
+ return *neighbors_[0 ];
351
+ } else if (&opoint == points_[1 ]) {
352
+ return *neighbors_[1 ];
353
+ }
354
+ return *neighbors_[2 ];
355
+ }
356
+
357
+ void Triangle::DebugPrint ()
358
+ {
359
+ using namespace std ;
360
+ cout << points_[0 ]->x << " ," << points_[0 ]->y << " " ;
361
+ cout << points_[1 ]->x << " ," << points_[1 ]->y << " " ;
362
+ cout << points_[2 ]->x << " ," << points_[2 ]->y << endl;
363
+ }
364
+
365
+ }
0 commit comments