2
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
3
* http://code.google.com/p/poly2tri/
7
* Redistribution and use in source and binary forms, with or without modification,
8
* are permitted provided that the following conditions are met:
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.
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.
49
/// Default constructor does nothing (for performance).
56
/// The edges this point constitutes an upper ending point
57
std::vector<Edge*> edge_list;
59
/// Construct using coordinates.
60
Point(double x, double y) : x(x), y(y) {}
62
/// Set this point to all zeros.
69
/// Set this point to some specified coordinates.
70
void set(double x_, double y_)
76
/// Negate this point.
77
Point operator -() const
84
/// Add a point to this point.
85
void operator +=(const Point& v)
91
/// Subtract a point from this point.
92
void operator -=(const Point& v)
98
/// Multiply this point by a scalar.
99
void operator *=(double a)
105
/// Get the length of this point (the norm).
106
double Length() const
108
return sqrt(x * x + y * y);
111
/// Convert this point into a unit point. Returns the Length.
114
const double len = Length();
122
// Represents a simple polygon's edge
128
Edge(Point& p1, Point& p2) : p(&p1), q(&p2)
133
} else if (p1.y == p2.y) {
137
} else if (p1.x == p2.x) {
143
q->edge_list.push_back(this);
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"
154
Triangle(Point& a, Point& b, Point& c);
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];
161
Point* GetPoint(int index);
162
Point* PointCW(const Point& point);
163
Point* PointCCW(const Point& point);
164
Point* OppositePoint(Triangle& t, const Point& p);
166
Triangle* GetNeighbor(int index);
167
void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
168
void MarkNeighbor(Triangle& t);
170
void MarkConstrainedEdge(int index);
171
void MarkConstrainedEdge(Edge& edge);
172
void MarkConstrainedEdge(Point* p, Point* q);
174
int Index(const Point* p);
175
int EdgeIndex(const Point* p1, const Point* p2);
177
Triangle* NeighborCW(const Point& point);
178
Triangle* NeighborCCW(const Point& point);
179
bool GetConstrainedEdgeCCW(const Point& p);
180
bool GetConstrainedEdgeCW(const Point& p);
181
void SetConstrainedEdgeCCW(const Point& p, bool ce);
182
void SetConstrainedEdgeCW(const Point& p, bool ce);
183
bool GetDelunayEdgeCCW(const Point& p);
184
bool GetDelunayEdgeCW(const Point& p);
185
void SetDelunayEdgeCCW(const Point& p, bool e);
186
void SetDelunayEdgeCW(const Point& p, bool e);
188
bool Contains(const Point* p);
189
bool Contains(const Edge& e);
190
bool Contains(const Point* p, const Point* q);
191
void Legalize(Point& point);
192
void Legalize(Point& opoint, Point& npoint);
194
* Clears all references to all other triangles and points
197
void ClearNeighbor(const Triangle *triangle);
198
void ClearNeighbors();
199
void ClearDelunayEdges();
201
inline bool IsInterior();
202
inline void IsInterior(bool b);
204
Triangle& NeighborAcross(const Point& opoint);
213
Triangle* neighbors_[3];
215
/// Has this triangle been marked as an interior triangle?
219
inline bool cmp(const Point* a, const Point* b)
223
} else if (a->y == b->y) {
224
// Make sure q is point with greater x value
232
/// Add two points_ component-wise.
233
inline Point operator +(const Point& a, const Point& b)
235
return Point(a.x + b.x, a.y + b.y);
238
/// Subtract two points_ component-wise.
239
inline Point operator -(const Point& a, const Point& b)
241
return Point(a.x - b.x, a.y - b.y);
244
/// Multiply point by scalar
245
inline Point operator *(double s, const Point& a)
247
return Point(s * a.x, s * a.y);
250
inline bool operator ==(const Point& a, const Point& b)
252
return a.x == b.x && a.y == b.y;
255
inline bool operator !=(const Point& a, const Point& b)
257
return !(a.x == b.x) && !(a.y == b.y);
260
/// Peform the dot product on two vectors.
261
inline double Dot(const Point& a, const Point& b)
263
return a.x * b.x + a.y * b.y;
266
/// Perform the cross product on two vectors. In 2D this produces a scalar.
267
inline double Cross(const Point& a, const Point& b)
269
return a.x * b.y - a.y * b.x;
272
/// Perform the cross product on a point and a scalar. In 2D this produces
274
inline Point Cross(const Point& a, double s)
276
return Point(s * a.y, -s * a.x);
279
/// Perform the cross product on a scalar and a point. In 2D this produces
281
inline Point Cross(double s, const Point& a)
283
return Point(-s * a.y, s * a.x);
286
inline Point* Triangle::GetPoint(int index)
288
return points_[index];
291
inline Triangle* Triangle::GetNeighbor(int index)
293
return neighbors_[index];
296
inline bool Triangle::Contains(const Point* p)
298
return p == points_[0] || p == points_[1] || p == points_[2];
301
inline bool Triangle::Contains(const Edge& e)
303
return Contains(e.p) && Contains(e.q);
306
inline bool Triangle::Contains(const Point* p, const Point* q)
308
return Contains(p) && Contains(q);
311
inline bool Triangle::IsInterior()
316
inline void Triangle::IsInterior(bool b)
b'\\ No newline at end of file'