rbpoly2tri 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,334 @@
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
+ void Triangle::ClearNeighbors()
74
+ {
75
+ neighbors_[0] = NULL;
76
+ neighbors_[1] = NULL;
77
+ neighbors_[2] = NULL;
78
+ }
79
+
80
+ void Triangle::ClearDelunayEdges()
81
+ {
82
+ delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
83
+ }
84
+
85
+ Point* Triangle::OppositePoint(Triangle& t, Point& p)
86
+ {
87
+ Point *cw = t.PointCW(p);
88
+ double x = cw->x;
89
+ double y = cw->y;
90
+ x = p.x;
91
+ y = p.y;
92
+ Point* ham = PointCW(*cw);
93
+ return PointCW(*cw);
94
+ }
95
+
96
+ // Legalized triangle by rotating clockwise around point(0)
97
+ void Triangle::Legalize(Point& point)
98
+ {
99
+ points_[1] = points_[0];
100
+ points_[0] = points_[2];
101
+ points_[2] = &point;
102
+ }
103
+
104
+ // Legalize triagnle by rotating clockwise around oPoint
105
+ void Triangle::Legalize(Point& opoint, Point& npoint)
106
+ {
107
+ if (&opoint == points_[0]) {
108
+ points_[1] = points_[0];
109
+ points_[0] = points_[2];
110
+ points_[2] = &npoint;
111
+ } else if (&opoint == points_[1]) {
112
+ points_[2] = points_[1];
113
+ points_[1] = points_[0];
114
+ points_[0] = &npoint;
115
+ } else if (&opoint == points_[2]) {
116
+ points_[0] = points_[2];
117
+ points_[2] = points_[1];
118
+ points_[1] = &npoint;
119
+ } else {
120
+ assert(0);
121
+ }
122
+ }
123
+
124
+ int Triangle::Index(const Point* p)
125
+ {
126
+ if (p == points_[0]) {
127
+ return 0;
128
+ } else if (p == points_[1]) {
129
+ return 1;
130
+ } else if (p == points_[2]) {
131
+ return 2;
132
+ }
133
+ assert(0);
134
+ }
135
+
136
+ int Triangle::EdgeIndex(const Point* p1, const Point* p2)
137
+ {
138
+ if (points_[0] == p1) {
139
+ if (points_[1] == p2) {
140
+ return 2;
141
+ } else if (points_[2] == p2) {
142
+ return 1;
143
+ }
144
+ } else if (points_[1] == p1) {
145
+ if (points_[2] == p2) {
146
+ return 0;
147
+ } else if (points_[0] == p2) {
148
+ return 2;
149
+ }
150
+ } else if (points_[2] == p1) {
151
+ if (points_[0] == p2) {
152
+ return 1;
153
+ } else if (points_[1] == p2) {
154
+ return 0;
155
+ }
156
+ }
157
+ return -1;
158
+ }
159
+
160
+ void Triangle::MarkConstrainedEdge(const int index)
161
+ {
162
+ constrained_edge[index] = true;
163
+ }
164
+
165
+ void Triangle::MarkConstrainedEdge(Edge& edge)
166
+ {
167
+ MarkConstrainedEdge(edge.p, edge.q);
168
+ }
169
+
170
+ // Mark edge as constrained
171
+ void Triangle::MarkConstrainedEdge(Point* p, Point* q)
172
+ {
173
+ if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
174
+ constrained_edge[2] = true;
175
+ } else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
176
+ constrained_edge[1] = true;
177
+ } else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
178
+ constrained_edge[0] = true;
179
+ }
180
+ }
181
+
182
+ // The point counter-clockwise to given point
183
+ Point* Triangle::PointCW(Point& point)
184
+ {
185
+ if (&point == points_[0]) {
186
+ return points_[2];
187
+ } else if (&point == points_[1]) {
188
+ return points_[0];
189
+ } else if (&point == points_[2]) {
190
+ return points_[1];
191
+ }
192
+ assert(0);
193
+ }
194
+
195
+ // The point counter-clockwise to given point
196
+ Point* Triangle::PointCCW(Point& point)
197
+ {
198
+ if (&point == points_[0]) {
199
+ return points_[1];
200
+ } else if (&point == points_[1]) {
201
+ return points_[2];
202
+ } else if (&point == points_[2]) {
203
+ return points_[0];
204
+ }
205
+ assert(0);
206
+ }
207
+
208
+ // The neighbor clockwise to given point
209
+ Triangle* Triangle::NeighborCW(Point& point)
210
+ {
211
+ if (&point == points_[0]) {
212
+ return neighbors_[1];
213
+ } else if (&point == points_[1]) {
214
+ return neighbors_[2];
215
+ }
216
+ return neighbors_[0];
217
+ }
218
+
219
+ // The neighbor counter-clockwise to given point
220
+ Triangle* Triangle::NeighborCCW(Point& point)
221
+ {
222
+ if (&point == points_[0]) {
223
+ return neighbors_[2];
224
+ } else if (&point == points_[1]) {
225
+ return neighbors_[0];
226
+ }
227
+ return neighbors_[1];
228
+ }
229
+
230
+ bool Triangle::GetConstrainedEdgeCCW(Point& p)
231
+ {
232
+ if (&p == points_[0]) {
233
+ return constrained_edge[2];
234
+ } else if (&p == points_[1]) {
235
+ return constrained_edge[0];
236
+ }
237
+ return constrained_edge[1];
238
+ }
239
+
240
+ bool Triangle::GetConstrainedEdgeCW(Point& p)
241
+ {
242
+ if (&p == points_[0]) {
243
+ return constrained_edge[1];
244
+ } else if (&p == points_[1]) {
245
+ return constrained_edge[2];
246
+ }
247
+ return constrained_edge[0];
248
+ }
249
+
250
+ void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce)
251
+ {
252
+ if (&p == points_[0]) {
253
+ constrained_edge[2] = ce;
254
+ } else if (&p == points_[1]) {
255
+ constrained_edge[0] = ce;
256
+ } else {
257
+ constrained_edge[1] = ce;
258
+ }
259
+ }
260
+
261
+ void Triangle::SetConstrainedEdgeCW(Point& p, bool ce)
262
+ {
263
+ if (&p == points_[0]) {
264
+ constrained_edge[1] = ce;
265
+ } else if (&p == points_[1]) {
266
+ constrained_edge[2] = ce;
267
+ } else {
268
+ constrained_edge[0] = ce;
269
+ }
270
+ }
271
+
272
+ bool Triangle::GetDelunayEdgeCCW(Point& p)
273
+ {
274
+ if (&p == points_[0]) {
275
+ return delaunay_edge[2];
276
+ } else if (&p == points_[1]) {
277
+ return delaunay_edge[0];
278
+ }
279
+ return delaunay_edge[1];
280
+ }
281
+
282
+ bool Triangle::GetDelunayEdgeCW(Point& p)
283
+ {
284
+ if (&p == points_[0]) {
285
+ return delaunay_edge[1];
286
+ } else if (&p == points_[1]) {
287
+ return delaunay_edge[2];
288
+ }
289
+ return delaunay_edge[0];
290
+ }
291
+
292
+ void Triangle::SetDelunayEdgeCCW(Point& p, bool e)
293
+ {
294
+ if (&p == points_[0]) {
295
+ delaunay_edge[2] = e;
296
+ } else if (&p == points_[1]) {
297
+ delaunay_edge[0] = e;
298
+ } else {
299
+ delaunay_edge[1] = e;
300
+ }
301
+ }
302
+
303
+ void Triangle::SetDelunayEdgeCW(Point& p, bool e)
304
+ {
305
+ if (&p == points_[0]) {
306
+ delaunay_edge[1] = e;
307
+ } else if (&p == points_[1]) {
308
+ delaunay_edge[2] = e;
309
+ } else {
310
+ delaunay_edge[0] = e;
311
+ }
312
+ }
313
+
314
+ // The neighbor across to given point
315
+ Triangle& Triangle::NeighborAcross(Point& opoint)
316
+ {
317
+ if (&opoint == points_[0]) {
318
+ return *neighbors_[0];
319
+ } else if (&opoint == points_[1]) {
320
+ return *neighbors_[1];
321
+ }
322
+ return *neighbors_[2];
323
+ }
324
+
325
+ void Triangle::DebugPrint()
326
+ {
327
+ using namespace std;
328
+ cout << points_[0]->x << "," << points_[0]->y << " ";
329
+ cout << points_[1]->x << "," << points_[1]->y << " ";
330
+ cout << points_[2]->x << "," << points_[2]->y << endl;
331
+ }
332
+
333
+ }
334
+
@@ -0,0 +1,320 @@
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
+
32
+ // Include guard
33
+ #ifndef SHAPES_H
34
+ #define SHAPES_H
35
+
36
+ #include <vector>
37
+ #include <cstddef>
38
+ #include <assert.h>
39
+ #include <cmath>
40
+
41
+ namespace p2t {
42
+
43
+ struct Edge;
44
+
45
+ struct Point {
46
+
47
+ double x, y;
48
+
49
+ /// Default constructor does nothing (for performance).
50
+ Point()
51
+ {
52
+ x = 0.0;
53
+ y = 0.0;
54
+ }
55
+
56
+ /// The edges this point constitutes an upper ending point
57
+ std::vector<Edge*> edge_list;
58
+
59
+ /// Construct using coordinates.
60
+ Point(double x, double y) : x(x), y(y) {}
61
+
62
+ /// Set this point to all zeros.
63
+ void set_zero()
64
+ {
65
+ x = 0.0f;
66
+ y = 0.0f;
67
+ }
68
+
69
+ /// Set this point to some specified coordinates.
70
+ void set(double x_, double y_)
71
+ {
72
+ x = x_;
73
+ y = y_;
74
+ }
75
+
76
+ /// Negate this point.
77
+ Point operator -() const
78
+ {
79
+ Point v;
80
+ v.set(-x, -y);
81
+ return v;
82
+ }
83
+
84
+ /// Add a point to this point.
85
+ void operator +=(const Point& v)
86
+ {
87
+ x += v.x;
88
+ y += v.y;
89
+ }
90
+
91
+ /// Subtract a point from this point.
92
+ void operator -=(const Point& v)
93
+ {
94
+ x -= v.x;
95
+ y -= v.y;
96
+ }
97
+
98
+ /// Multiply this point by a scalar.
99
+ void operator *=(double a)
100
+ {
101
+ x *= a;
102
+ y *= a;
103
+ }
104
+
105
+ /// Get the length of this point (the norm).
106
+ double Length() const
107
+ {
108
+ return sqrt(x * x + y * y);
109
+ }
110
+
111
+ /// Convert this point into a unit point. Returns the Length.
112
+ double Normalize()
113
+ {
114
+ double len = Length();
115
+ x /= len;
116
+ y /= len;
117
+ return len;
118
+ }
119
+
120
+ };
121
+
122
+ // Represents a simple polygon's edge
123
+ struct Edge {
124
+
125
+ Point* p, *q;
126
+
127
+ /// Constructor
128
+ Edge(Point& p1, Point& p2) : p(&p1), q(&p2)
129
+ {
130
+ if (p1.y > p2.y) {
131
+ q = &p1;
132
+ p = &p2;
133
+ } else if (p1.y == p2.y) {
134
+ if (p1.x > p2.x) {
135
+ q = &p1;
136
+ p = &p2;
137
+ } else if (p1.x == p2.x) {
138
+ // Repeat points
139
+ assert(false);
140
+ }
141
+ }
142
+
143
+ q->edge_list.push_back(this);
144
+ }
145
+ };
146
+
147
+ // Triangle-based data structures are know to have better performance than quad-edge structures
148
+ // See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator"
149
+ // "Triangulations in CGAL"
150
+ class Triangle {
151
+ public:
152
+
153
+ /// Constructor
154
+ Triangle(Point& a, Point& b, Point& c);
155
+
156
+ /// Flags to determine if an edge is a Constrained edge
157
+ bool constrained_edge[3];
158
+ /// Flags to determine if an edge is a Delauney edge
159
+ bool delaunay_edge[3];
160
+
161
+ Point* GetPoint(const int& index);
162
+ Point* PointCW(Point& point);
163
+ Point* PointCCW(Point& point);
164
+ Point* OppositePoint(Triangle& t, Point& p);
165
+
166
+ Triangle* GetNeighbor(const int& index);
167
+ void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
168
+ void MarkNeighbor(Triangle& t);
169
+
170
+ void MarkConstrainedEdge(const int index);
171
+ void MarkConstrainedEdge(Edge& edge);
172
+ void MarkConstrainedEdge(Point* p, Point* q);
173
+
174
+ int Index(const Point* p);
175
+ int EdgeIndex(const Point* p1, const Point* p2);
176
+
177
+ Triangle* NeighborCW(Point& point);
178
+ Triangle* NeighborCCW(Point& point);
179
+ bool GetConstrainedEdgeCCW(Point& p);
180
+ bool GetConstrainedEdgeCW(Point& p);
181
+ void SetConstrainedEdgeCCW(Point& p, bool ce);
182
+ void SetConstrainedEdgeCW(Point& p, bool ce);
183
+ bool GetDelunayEdgeCCW(Point& p);
184
+ bool GetDelunayEdgeCW(Point& p);
185
+ void SetDelunayEdgeCCW(Point& p, bool e);
186
+ void SetDelunayEdgeCW(Point& p, bool e);
187
+
188
+ bool Contains(Point* p);
189
+ bool Contains(const Edge& e);
190
+ bool Contains(Point* p, Point* q);
191
+ void Legalize(Point& point);
192
+ void Legalize(Point& opoint, Point& npoint);
193
+ void ClearNeighbors();
194
+ void ClearDelunayEdges();
195
+
196
+ inline bool IsInterior();
197
+ inline void IsInterior(bool b);
198
+
199
+ Triangle& NeighborAcross(Point& opoint);
200
+
201
+ void DebugPrint();
202
+
203
+ private:
204
+
205
+ /// Triangle points
206
+ Point* points_[3];
207
+ /// Neighbor list
208
+ Triangle* neighbors_[3];
209
+
210
+ /// Has this triangle been marked as an interior triangle?
211
+ bool interior_;
212
+ };
213
+
214
+ inline bool cmp(const Point* a, const Point* b)
215
+ {
216
+ if (a->y < b->y) {
217
+ return true;
218
+ } else if (a->y == b->y) {
219
+ // Make sure q is point with greater x value
220
+ if (a->x < b->x) {
221
+ return true;
222
+ }
223
+ }
224
+ return false;
225
+ }
226
+
227
+ /// Add two points_ component-wise.
228
+ inline Point operator +(const Point& a, const Point& b)
229
+ {
230
+ return Point(a.x + b.x, a.y + b.y);
231
+ }
232
+
233
+ /// Subtract two points_ component-wise.
234
+ inline Point operator -(const Point& a, const Point& b)
235
+ {
236
+ return Point(a.x - b.x, a.y - b.y);
237
+ }
238
+
239
+ /// Multiply point by scalar
240
+ inline Point operator *(double s, const Point& a)
241
+ {
242
+ return Point(s * a.x, s * a.y);
243
+ }
244
+
245
+ inline bool operator ==(const Point& a, const Point& b)
246
+ {
247
+ return a.x == b.x && a.y == b.y;
248
+ }
249
+
250
+ inline bool operator !=(const Point& a, const Point& b)
251
+ {
252
+ return a.x != b.x && a.y != b.y;
253
+ }
254
+
255
+ /// Peform the dot product on two vectors.
256
+ inline double Dot(const Point& a, const Point& b)
257
+ {
258
+ return a.x * b.x + a.y * b.y;
259
+ }
260
+
261
+ /// Perform the cross product on two vectors. In 2D this produces a scalar.
262
+ inline double Cross(const Point& a, const Point& b)
263
+ {
264
+ return a.x * b.y - a.y * b.x;
265
+ }
266
+
267
+ /// Perform the cross product on a point and a scalar. In 2D this produces
268
+ /// a point.
269
+ inline Point Cross(const Point& a, double s)
270
+ {
271
+ return Point(s * a.y, -s * a.x);
272
+ }
273
+
274
+ /// Perform the cross product on a scalar and a point. In 2D this produces
275
+ /// a point.
276
+ inline Point Cross(const double s, const Point& a)
277
+ {
278
+ return Point(-s * a.y, s * a.x);
279
+ }
280
+
281
+ inline Point* Triangle::GetPoint(const int& index)
282
+ {
283
+ return points_[index];
284
+ }
285
+
286
+ inline Triangle* Triangle::GetNeighbor(const int& index)
287
+ {
288
+ return neighbors_[index];
289
+ }
290
+
291
+ inline bool Triangle::Contains(Point* p)
292
+ {
293
+ return p == points_[0] || p == points_[1] || p == points_[2];
294
+ }
295
+
296
+ inline bool Triangle::Contains(const Edge& e)
297
+ {
298
+ return Contains(e.p) && Contains(e.q);
299
+ }
300
+
301
+ inline bool Triangle::Contains(Point* p, Point* q)
302
+ {
303
+ return Contains(p) && Contains(q);
304
+ }
305
+
306
+ inline bool Triangle::IsInterior()
307
+ {
308
+ return interior_;
309
+ }
310
+
311
+ inline void Triangle::IsInterior(bool b)
312
+ {
313
+ interior_ = b;
314
+ }
315
+
316
+ }
317
+
318
+ #endif
319
+
320
+