Resync internal poly2tri library

This commit is contained in:
Nyall Dawson 2024-02-05 14:05:14 +10:00
parent 9e6e373b79
commit 7d92c8b08d
12 changed files with 1326 additions and 1275 deletions

32
external/poly2tri/common/dll_symbol.h vendored Normal file
View File

@ -0,0 +1,32 @@
/*
* Poly2Tri Copyright (c) 2009-2018, Poly2Tri Contributors
* https://github.com/jhasse/poly2tri
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Poly2Tri nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define P2T_DLL_SYMBOL

View File

@ -40,44 +40,44 @@ Point::Point(double x, double y) : x(x), y(y)
} }
std::ostream& operator<<(std::ostream& out, const Point& point) { std::ostream& operator<<(std::ostream& out, const Point& point) {
return out << point.x << "," << point.y; return out << point.x << "," << point.y;
} }
Triangle::Triangle(Point& a, Point& b, Point& c) Triangle::Triangle(Point& a, Point& b, Point& c)
{ {
points_[0] = &a; points_[1] = &b; points_[2] = &c; points_[0] = &a; points_[1] = &b; points_[2] = &c;
neighbors_[0] = nullptr; neighbors_[1] = nullptr; neighbors_[2] = nullptr; neighbors_[0] = nullptr; neighbors_[1] = nullptr; neighbors_[2] = nullptr;
constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false; constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false; delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
interior_ = false; interior_ = false;
} }
// Update neighbor pointers // Update neighbor pointers
void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t) void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t)
{ {
if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2])) if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2]))
neighbors_[0] = t; neighbors_[0] = t;
else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0])) else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0]))
neighbors_[1] = t; neighbors_[1] = t;
else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0])) else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
neighbors_[2] = t; neighbors_[2] = t;
else else
assert(0); assert(0);
} }
// Exhaustive search to update neighbor pointers // Exhaustive search to update neighbor pointers
void Triangle::MarkNeighbor(Triangle& t) void Triangle::MarkNeighbor(Triangle& t)
{ {
if (t.Contains(points_[1], points_[2])) { if (t.Contains(points_[1], points_[2])) {
neighbors_[0] = &t; neighbors_[0] = &t;
t.MarkNeighbor(points_[1], points_[2], this); t.MarkNeighbor(points_[1], points_[2], this);
} else if (t.Contains(points_[0], points_[2])) { } else if (t.Contains(points_[0], points_[2])) {
neighbors_[1] = &t; neighbors_[1] = &t;
t.MarkNeighbor(points_[0], points_[2], this); t.MarkNeighbor(points_[0], points_[2], this);
} else if (t.Contains(points_[0], points_[1])) { } else if (t.Contains(points_[0], points_[1])) {
neighbors_[2] = &t; neighbors_[2] = &t;
t.MarkNeighbor(points_[0], points_[1], this); t.MarkNeighbor(points_[0], points_[1], this);
} }
} }
/** /**
@ -86,12 +86,10 @@ void Triangle::MarkNeighbor(Triangle& t)
void Triangle::Clear() void Triangle::Clear()
{ {
Triangle *t; Triangle *t;
for( int i=0; i<3; i++ ) for (auto& neighbor : neighbors_) {
{ t = neighbor;
t = neighbors_[i]; if (t != nullptr) {
if( t != nullptr ) t->ClearNeighbor(this);
{
t->ClearNeighbor( this );
} }
} }
ClearNeighbors(); ClearNeighbors();
@ -116,298 +114,298 @@ void Triangle::ClearNeighbor(const Triangle *triangle )
void Triangle::ClearNeighbors() void Triangle::ClearNeighbors()
{ {
neighbors_[0] = nullptr; neighbors_[0] = nullptr;
neighbors_[1] = nullptr; neighbors_[1] = nullptr;
neighbors_[2] = nullptr; neighbors_[2] = nullptr;
} }
void Triangle::ClearDelunayEdges() void Triangle::ClearDelunayEdges()
{ {
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false; delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
} }
Point* Triangle::OppositePoint(Triangle& t, const Point& p) Point* Triangle::OppositePoint(Triangle& t, const Point& p)
{ {
Point *cw = t.PointCW(p); Point *cw = t.PointCW(p);
return PointCW(*cw); return PointCW(*cw);
} }
// Legalized triangle by rotating clockwise around point(0) // Legalized triangle by rotating clockwise around point(0)
void Triangle::Legalize(Point& point) void Triangle::Legalize(Point& point)
{ {
points_[1] = points_[0]; points_[1] = points_[0];
points_[0] = points_[2]; points_[0] = points_[2];
points_[2] = &point; points_[2] = &point;
} }
// Legalize triagnle by rotating clockwise around oPoint // Legalize triagnle by rotating clockwise around oPoint
void Triangle::Legalize(Point& opoint, Point& npoint) void Triangle::Legalize(Point& opoint, Point& npoint)
{ {
if (&opoint == points_[0]) { if (&opoint == points_[0]) {
points_[1] = points_[0]; points_[1] = points_[0];
points_[0] = points_[2]; points_[0] = points_[2];
points_[2] = &npoint; points_[2] = &npoint;
} else if (&opoint == points_[1]) { } else if (&opoint == points_[1]) {
points_[2] = points_[1]; points_[2] = points_[1];
points_[1] = points_[0]; points_[1] = points_[0];
points_[0] = &npoint; points_[0] = &npoint;
} else if (&opoint == points_[2]) { } else if (&opoint == points_[2]) {
points_[0] = points_[2]; points_[0] = points_[2];
points_[2] = points_[1]; points_[2] = points_[1];
points_[1] = &npoint; points_[1] = &npoint;
} else { } else {
assert(0); assert(0);
} }
} }
int Triangle::Index(const Point* p) int Triangle::Index(const Point* p)
{ {
if (p == points_[0]) { if (p == points_[0]) {
return 0; return 0;
} else if (p == points_[1]) { } else if (p == points_[1]) {
return 1; return 1;
} else if (p == points_[2]) { } else if (p == points_[2]) {
return 2; return 2;
} }
assert(0); assert(0);
return -1; return -1;
} }
int Triangle::EdgeIndex(const Point* p1, const Point* p2) int Triangle::EdgeIndex(const Point* p1, const Point* p2)
{ {
if (points_[0] == p1) { if (points_[0] == p1) {
if (points_[1] == p2) { if (points_[1] == p2) {
return 2; return 2;
} else if (points_[2] == p2) { } else if (points_[2] == p2) {
return 1; return 1;
}
} else if (points_[1] == p1) {
if (points_[2] == p2) {
return 0;
} else if (points_[0] == p2) {
return 2;
}
} else if (points_[2] == p1) {
if (points_[0] == p2) {
return 1;
} else if (points_[1] == p2) {
return 0;
}
} }
} else if (points_[1] == p1) { return -1;
if (points_[2] == p2) {
return 0;
} else if (points_[0] == p2) {
return 2;
}
} else if (points_[2] == p1) {
if (points_[0] == p2) {
return 1;
} else if (points_[1] == p2) {
return 0;
}
}
return -1;
} }
void Triangle::MarkConstrainedEdge(int index) void Triangle::MarkConstrainedEdge(int index)
{ {
constrained_edge[index] = true; constrained_edge[index] = true;
} }
void Triangle::MarkConstrainedEdge(Edge& edge) void Triangle::MarkConstrainedEdge(Edge& edge)
{ {
MarkConstrainedEdge(edge.p, edge.q); MarkConstrainedEdge(edge.p, edge.q);
} }
// Mark edge as constrained // Mark edge as constrained
void Triangle::MarkConstrainedEdge(Point* p, Point* q) void Triangle::MarkConstrainedEdge(Point* p, Point* q)
{ {
if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) { if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
constrained_edge[2] = true; constrained_edge[2] = true;
} else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) { } else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
constrained_edge[1] = true; constrained_edge[1] = true;
} else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) { } else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
constrained_edge[0] = true; constrained_edge[0] = true;
} }
} }
// The point counter-clockwise to given point // The point counter-clockwise to given point
Point* Triangle::PointCW(const Point& point) Point* Triangle::PointCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return points_[2]; return points_[2];
} else if (&point == points_[1]) { } else if (&point == points_[1]) {
return points_[0]; return points_[0];
} else if (&point == points_[2]) { } else if (&point == points_[2]) {
return points_[1]; return points_[1];
} }
assert(0); assert(0);
return nullptr; return nullptr;
} }
// The point counter-clockwise to given point // The point counter-clockwise to given point
Point* Triangle::PointCCW(const Point& point) Point* Triangle::PointCCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return points_[1]; return points_[1];
} else if (&point == points_[1]) { } else if (&point == points_[1]) {
return points_[2]; return points_[2];
} else if (&point == points_[2]) { } else if (&point == points_[2]) {
return points_[0]; return points_[0];
} }
assert(0); assert(0);
return nullptr; return nullptr;
} }
// The neighbor across to given point // The neighbor across to given point
Triangle* Triangle::NeighborAcross(const Point& point) Triangle* Triangle::NeighborAcross(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return neighbors_[0]; return neighbors_[0];
} else if (&point == points_[1]) { } else if (&point == points_[1]) {
return neighbors_[1]; return neighbors_[1];
} }
return neighbors_[2]; return neighbors_[2];
} }
// The neighbor clockwise to given point // The neighbor clockwise to given point
Triangle* Triangle::NeighborCW(const Point& point) Triangle* Triangle::NeighborCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return neighbors_[1]; return neighbors_[1];
} else if (&point == points_[1]) { } else if (&point == points_[1]) {
return neighbors_[2]; return neighbors_[2];
} }
return neighbors_[0]; return neighbors_[0];
} }
// The neighbor counter-clockwise to given point // The neighbor counter-clockwise to given point
Triangle* Triangle::NeighborCCW(const Point& point) Triangle* Triangle::NeighborCCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return neighbors_[2]; return neighbors_[2];
} else if (&point == points_[1]) { } else if (&point == points_[1]) {
return neighbors_[0]; return neighbors_[0];
} }
return neighbors_[1]; return neighbors_[1];
} }
bool Triangle::GetConstrainedEdgeCCW(const Point& p) bool Triangle::GetConstrainedEdgeCCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return constrained_edge[2]; return constrained_edge[2];
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
return constrained_edge[0]; return constrained_edge[0];
} }
return constrained_edge[1]; return constrained_edge[1];
} }
bool Triangle::GetConstrainedEdgeCW(const Point& p) bool Triangle::GetConstrainedEdgeCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return constrained_edge[1]; return constrained_edge[1];
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
return constrained_edge[2]; return constrained_edge[2];
} }
return constrained_edge[0]; return constrained_edge[0];
} }
void Triangle::SetConstrainedEdgeCCW(const Point& p, bool ce) void Triangle::SetConstrainedEdgeCCW(const Point& p, bool ce)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
constrained_edge[2] = ce; constrained_edge[2] = ce;
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
constrained_edge[0] = ce; constrained_edge[0] = ce;
} else { } else {
constrained_edge[1] = ce; constrained_edge[1] = ce;
} }
} }
void Triangle::SetConstrainedEdgeCW(const Point& p, bool ce) void Triangle::SetConstrainedEdgeCW(const Point& p, bool ce)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
constrained_edge[1] = ce; constrained_edge[1] = ce;
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
constrained_edge[2] = ce; constrained_edge[2] = ce;
} else { } else {
constrained_edge[0] = ce; constrained_edge[0] = ce;
} }
} }
bool Triangle::GetDelunayEdgeCCW(const Point& p) bool Triangle::GetDelunayEdgeCCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return delaunay_edge[2]; return delaunay_edge[2];
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
return delaunay_edge[0]; return delaunay_edge[0];
} }
return delaunay_edge[1]; return delaunay_edge[1];
} }
bool Triangle::GetDelunayEdgeCW(const Point& p) bool Triangle::GetDelunayEdgeCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return delaunay_edge[1]; return delaunay_edge[1];
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
return delaunay_edge[2]; return delaunay_edge[2];
} }
return delaunay_edge[0]; return delaunay_edge[0];
} }
void Triangle::SetDelunayEdgeCCW(const Point& p, bool e) void Triangle::SetDelunayEdgeCCW(const Point& p, bool e)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
delaunay_edge[2] = e; delaunay_edge[2] = e;
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
delaunay_edge[0] = e; delaunay_edge[0] = e;
} else { } else {
delaunay_edge[1] = e; delaunay_edge[1] = e;
} }
} }
void Triangle::SetDelunayEdgeCW(const Point& p, bool e) void Triangle::SetDelunayEdgeCW(const Point& p, bool e)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
delaunay_edge[1] = e; delaunay_edge[1] = e;
} else if (&p == points_[1]) { } else if (&p == points_[1]) {
delaunay_edge[2] = e; delaunay_edge[2] = e;
} else { } else {
delaunay_edge[0] = e; delaunay_edge[0] = e;
} }
} }
void Triangle::DebugPrint() void Triangle::DebugPrint()
{ {
std::cout << *points_[0] << " " << *points_[1] << " " << *points_[2] << std::endl; std::cout << *points_[0] << " " << *points_[1] << " " << *points_[2] << std::endl;
} }
bool Triangle::CircumcicleContains(const Point& point) const bool Triangle::CircumcicleContains(const Point& point) const
{ {
assert(IsCounterClockwise()); assert(IsCounterClockwise());
const double dx = points_[0]->x - point.x; const double dx = points_[0]->x - point.x;
const double dy = points_[0]->y - point.y; const double dy = points_[0]->y - point.y;
const double ex = points_[1]->x - point.x; const double ex = points_[1]->x - point.x;
const double ey = points_[1]->y - point.y; const double ey = points_[1]->y - point.y;
const double fx = points_[2]->x - point.x; const double fx = points_[2]->x - point.x;
const double fy = points_[2]->y - point.y; const double fy = points_[2]->y - point.y;
const double ap = dx * dx + dy * dy; const double ap = dx * dx + dy * dy;
const double bp = ex * ex + ey * ey; const double bp = ex * ex + ey * ey;
const double cp = fx * fx + fy * fy; const double cp = fx * fx + fy * fy;
return (dx * (fy * bp - cp * ey) - dy * (fx * bp - cp * ex) + ap * (fx * ey - fy * ex)) < 0; return (dx * (fy * bp - cp * ey) - dy * (fx * bp - cp * ex) + ap * (fx * ey - fy * ex)) < 0;
} }
bool Triangle::IsCounterClockwise() const bool Triangle::IsCounterClockwise() const
{ {
return (points_[1]->x - points_[0]->x) * (points_[2]->y - points_[0]->y) - return (points_[1]->x - points_[0]->x) * (points_[2]->y - points_[0]->y) -
(points_[2]->x - points_[0]->x) * (points_[1]->y - points_[0]->y) > (points_[2]->x - points_[0]->x) * (points_[1]->y - points_[0]->y) >
0; 0;
} }
bool IsDelaunay(const std::vector<p2t::Triangle*>& triangles) bool IsDelaunay(const std::vector<p2t::Triangle*>& triangles)
{ {
for (const auto triangle : triangles) { for (const auto triangle : triangles) {
for (const auto other : triangles) { for (const auto other : triangles) {
if (triangle == other) { if (triangle == other) {
continue; continue;
} }
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
if (triangle->CircumcicleContains(*other->GetPoint(i))) { if (triangle->CircumcicleContains(*other->GetPoint(i))) {
return false; return false;
}
}
} }
}
} }
} return true;
return true;
} }
} } // namespace p2t

View File

@ -29,9 +29,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
// Include guard #pragma once
#ifndef SHAPES_H
#define SHAPES_H #include "dll_symbol.h"
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
@ -42,290 +42,288 @@ namespace p2t {
struct Edge; struct Edge;
struct Point { struct P2T_DLL_SYMBOL Point {
double x, y; double x, y;
/// Default constructor does nothing (for performance). /// Default constructor does nothing (for performance).
Point() Point()
{ {
x = 0.0; x = 0.0;
y = 0.0; y = 0.0;
} }
/// The edges this point constitutes an upper ending point /// The edges this point constitutes an upper ending point
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
/// Construct using coordinates. /// Construct using coordinates.
Point(double x, double y); Point(double x, double y);
/// Set this point to all zeros. /// Set this point to all zeros.
void set_zero() void set_zero()
{ {
x = 0.0; x = 0.0;
y = 0.0; y = 0.0;
} }
/// Set this point to some specified coordinates. /// Set this point to some specified coordinates.
void set(double x_, double y_) void set(double x_, double y_)
{ {
x = x_; x = x_;
y = y_; y = y_;
} }
/// Negate this point. /// Negate this point.
Point operator -() const Point operator -() const
{ {
Point v; Point v;
v.set(-x, -y); v.set(-x, -y);
return v; return v;
} }
/// Add a point to this point. /// Add a point to this point.
void operator +=(const Point& v) void operator +=(const Point& v)
{ {
x += v.x; x += v.x;
y += v.y; y += v.y;
} }
/// Subtract a point from this point. /// Subtract a point from this point.
void operator -=(const Point& v) void operator -=(const Point& v)
{ {
x -= v.x; x -= v.x;
y -= v.y; y -= v.y;
} }
/// Multiply this point by a scalar. /// Multiply this point by a scalar.
void operator *=(double a) void operator *=(double a)
{ {
x *= a; x *= a;
y *= a; y *= a;
} }
/// Get the length of this point (the norm). /// Get the length of this point (the norm).
double Length() const double Length() const
{ {
return sqrt(x * x + y * y); return sqrt(x * x + y * y);
} }
/// Convert this point into a unit point. Returns the Length. /// Convert this point into a unit point. Returns the Length.
double Normalize() double Normalize()
{ {
const double len = Length(); const double len = Length();
x /= len; x /= len;
y /= len; y /= len;
return len; return len;
} }
}; };
std::ostream& operator<<(std::ostream&, const Point&); P2T_DLL_SYMBOL std::ostream& operator<<(std::ostream&, const Point&);
// Represents a simple polygon's edge // Represents a simple polygon's edge
struct Edge { struct P2T_DLL_SYMBOL Edge {
Point* p, *q; Point* p, *q;
/// Constructor /// Constructor
Edge(Point& p1, Point& p2) : p(&p1), q(&p2) Edge(Point& p1, Point& p2) : p(&p1), q(&p2)
{ {
if (p1.y > p2.y) { if (p1.y > p2.y) {
q = &p1; q = &p1;
p = &p2; p = &p2;
} else if (p1.y == p2.y) { } else if (p1.y == p2.y) {
if (p1.x > p2.x) { if (p1.x > p2.x) {
q = &p1; q = &p1;
p = &p2; p = &p2;
} else if (p1.x == p2.x) { } else if (p1.x == p2.x) {
// Repeat points // Repeat points
throw std::runtime_error("Edge::Edge: p1 == p2"); throw std::runtime_error("Edge::Edge: p1 == p2");
} }
}
q->edge_list.push_back(this);
} }
q->edge_list.push_back(this);
}
}; };
// Triangle-based data structures are know to have better performance than quad-edge structures // Triangle-based data structures are know to have better performance than quad-edge structures
// See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator" // See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator"
// "Triangulations in CGAL" // "Triangulations in CGAL"
class Triangle { class P2T_DLL_SYMBOL Triangle {
public: public:
/// Constructor /// Constructor
Triangle(Point& a, Point& b, Point& c); Triangle(Point& a, Point& b, Point& c);
/// Flags to determine if an edge is a Constrained edge /// Flags to determine if an edge is a Constrained edge
bool constrained_edge[3]; bool constrained_edge[3];
/// Flags to determine if an edge is a Delauney edge /// Flags to determine if an edge is a Delauney edge
bool delaunay_edge[3]; bool delaunay_edge[3];
Point* GetPoint(int index); Point* GetPoint(int index);
Point* PointCW(const Point& point); Point* PointCW(const Point& point);
Point* PointCCW(const Point& point); Point* PointCCW(const Point& point);
Point* OppositePoint(Triangle& t, const Point& p); Point* OppositePoint(Triangle& t, const Point& p);
Triangle* GetNeighbor(int index); Triangle* GetNeighbor(int index);
void MarkNeighbor(Point* p1, Point* p2, Triangle* t); void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
void MarkNeighbor(Triangle& t); void MarkNeighbor(Triangle& t);
void MarkConstrainedEdge(int index); void MarkConstrainedEdge(int index);
void MarkConstrainedEdge(Edge& edge); void MarkConstrainedEdge(Edge& edge);
void MarkConstrainedEdge(Point* p, Point* q); void MarkConstrainedEdge(Point* p, Point* q);
int Index(const Point* p); int Index(const Point* p);
int EdgeIndex(const Point* p1, const Point* p2); int EdgeIndex(const Point* p1, const Point* p2);
Triangle* NeighborAcross(const Point& point); Triangle* NeighborAcross(const Point& point);
Triangle* NeighborCW(const Point& point); Triangle* NeighborCW(const Point& point);
Triangle* NeighborCCW(const Point& point); Triangle* NeighborCCW(const Point& point);
bool GetConstrainedEdgeCCW(const Point& p); bool GetConstrainedEdgeCCW(const Point& p);
bool GetConstrainedEdgeCW(const Point& p); bool GetConstrainedEdgeCW(const Point& p);
void SetConstrainedEdgeCCW(const Point& p, bool ce); void SetConstrainedEdgeCCW(const Point& p, bool ce);
void SetConstrainedEdgeCW(const Point& p, bool ce); void SetConstrainedEdgeCW(const Point& p, bool ce);
bool GetDelunayEdgeCCW(const Point& p); bool GetDelunayEdgeCCW(const Point& p);
bool GetDelunayEdgeCW(const Point& p); bool GetDelunayEdgeCW(const Point& p);
void SetDelunayEdgeCCW(const Point& p, bool e); void SetDelunayEdgeCCW(const Point& p, bool e);
void SetDelunayEdgeCW(const Point& p, bool e); void SetDelunayEdgeCW(const Point& p, bool e);
bool Contains(const Point* p); bool Contains(const Point* p);
bool Contains(const Edge& e); bool Contains(const Edge& e);
bool Contains(const Point* p, const Point* q); bool Contains(const Point* p, const Point* q);
void Legalize(Point& point); void Legalize(Point& point);
void Legalize(Point& opoint, Point& npoint); void Legalize(Point& opoint, Point& npoint);
/** /**
* Clears all references to all other triangles and points * Clears all references to all other triangles and points
*/ */
void Clear(); void Clear();
void ClearNeighbor(const Triangle *triangle); void ClearNeighbor(const Triangle *triangle);
void ClearNeighbors(); void ClearNeighbors();
void ClearDelunayEdges(); void ClearDelunayEdges();
inline bool IsInterior(); inline bool IsInterior();
inline void IsInterior(bool b); inline void IsInterior(bool b);
void DebugPrint(); void DebugPrint();
bool CircumcicleContains(const Point&) const; bool CircumcicleContains(const Point&) const;
private: private:
bool IsCounterClockwise() const; bool IsCounterClockwise() const;
/// Triangle points /// Triangle points
Point* points_[3]; Point* points_[3];
/// Neighbor list /// Neighbor list
Triangle* neighbors_[3]; Triangle* neighbors_[3];
/// Has this triangle been marked as an interior triangle? /// Has this triangle been marked as an interior triangle?
bool interior_; bool interior_;
}; };
inline bool cmp(const Point* a, const Point* b) inline bool cmp(const Point* a, const Point* b)
{ {
if (a->y < b->y) { if (a->y < b->y) {
return true; return true;
} else if (a->y == b->y) { } else if (a->y == b->y) {
// Make sure q is point with greater x value // Make sure q is point with greater x value
if (a->x < b->x) { if (a->x < b->x) {
return true; return true;
}
} }
} return false;
return false;
} }
/// Add two points_ component-wise. /// Add two points_ component-wise.
inline Point operator +(const Point& a, const Point& b) inline Point operator +(const Point& a, const Point& b)
{ {
return Point(a.x + b.x, a.y + b.y); return Point(a.x + b.x, a.y + b.y);
} }
/// Subtract two points_ component-wise. /// Subtract two points_ component-wise.
inline Point operator -(const Point& a, const Point& b) inline Point operator -(const Point& a, const Point& b)
{ {
return Point(a.x - b.x, a.y - b.y); return Point(a.x - b.x, a.y - b.y);
} }
/// Multiply point by scalar /// Multiply point by scalar
inline Point operator *(double s, const Point& a) inline Point operator *(double s, const Point& a)
{ {
return Point(s * a.x, s * a.y); return Point(s * a.x, s * a.y);
} }
inline bool operator ==(const Point& a, const Point& b) inline bool operator ==(const Point& a, const Point& b)
{ {
return a.x == b.x && a.y == b.y; return a.x == b.x && a.y == b.y;
} }
inline bool operator !=(const Point& a, const Point& b) inline bool operator !=(const Point& a, const Point& b)
{ {
return !(a.x == b.x) || !(a.y == b.y); return !(a.x == b.x) || !(a.y == b.y);
} }
/// Peform the dot product on two vectors. /// Peform the dot product on two vectors.
inline double Dot(const Point& a, const Point& b) inline double Dot(const Point& a, const Point& b)
{ {
return a.x * b.x + a.y * b.y; return a.x * b.x + a.y * b.y;
} }
/// Perform the cross product on two vectors. In 2D this produces a scalar. /// Perform the cross product on two vectors. In 2D this produces a scalar.
inline double Cross(const Point& a, const Point& b) inline double Cross(const Point& a, const Point& b)
{ {
return a.x * b.y - a.y * b.x; return a.x * b.y - a.y * b.x;
} }
/// Perform the cross product on a point and a scalar. In 2D this produces /// Perform the cross product on a point and a scalar. In 2D this produces
/// a point. /// a point.
inline Point Cross(const Point& a, double s) inline Point Cross(const Point& a, double s)
{ {
return Point(s * a.y, -s * a.x); return Point(s * a.y, -s * a.x);
} }
/// Perform the cross product on a scalar and a point. In 2D this produces /// Perform the cross product on a scalar and a point. In 2D this produces
/// a point. /// a point.
inline Point Cross(double s, const Point& a) inline Point Cross(double s, const Point& a)
{ {
return Point(-s * a.y, s * a.x); return Point(-s * a.y, s * a.x);
} }
inline Point* Triangle::GetPoint(int index) inline Point* Triangle::GetPoint(int index)
{ {
return points_[index]; return points_[index];
} }
inline Triangle* Triangle::GetNeighbor(int index) inline Triangle* Triangle::GetNeighbor(int index)
{ {
return neighbors_[index]; return neighbors_[index];
} }
inline bool Triangle::Contains(const Point* p) inline bool Triangle::Contains(const Point* p)
{ {
return p == points_[0] || p == points_[1] || p == points_[2]; return p == points_[0] || p == points_[1] || p == points_[2];
} }
inline bool Triangle::Contains(const Edge& e) inline bool Triangle::Contains(const Edge& e)
{ {
return Contains(e.p) && Contains(e.q); return Contains(e.p) && Contains(e.q);
} }
inline bool Triangle::Contains(const Point* p, const Point* q) inline bool Triangle::Contains(const Point* p, const Point* q)
{ {
return Contains(p) && Contains(q); return Contains(p) && Contains(q);
} }
inline bool Triangle::IsInterior() inline bool Triangle::IsInterior()
{ {
return interior_; return interior_;
} }
inline void Triangle::IsInterior(bool b) inline void Triangle::IsInterior(bool b)
{ {
interior_ = b; interior_ = b;
} }
/// Is this set a valid delaunay triangulation? /// Is this set a valid delaunay triangulation?
bool IsDelaunay(const std::vector<p2t::Triangle*>&); P2T_DLL_SYMBOL bool IsDelaunay(const std::vector<p2t::Triangle*>&);
} }
#endif

View File

@ -29,13 +29,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef UTILS_H #pragma once
#define UTILS_H
// Otherwise #defines like M_PI are undeclared under Visual Studio // Otherwise #defines like M_PI are undeclared under Visual Studio
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#endif
#include "shapes.h" #include "shapes.h"
@ -67,15 +64,19 @@ enum Orientation { CW, CCW, COLLINEAR };
*/ */
Orientation Orient2d(const Point& pa, const Point& pb, const Point& pc) Orientation Orient2d(const Point& pa, const Point& pb, const Point& pc)
{ {
double detleft = (pa.x - pc.x) * (pb.y - pc.y); double detleft = (pa.x - pc.x) * (pb.y - pc.y);
double detright = (pa.y - pc.y) * (pb.x - pc.x); double detright = (pa.y - pc.y) * (pb.x - pc.x);
double val = detleft - detright; double val = detleft - detright;
if (val > -EPSILON && val < EPSILON) {
return COLLINEAR; // Using a tolerance here fails on concave-by-subepsilon boundaries
} else if (val > 0) { // if (val > -EPSILON && val < EPSILON) {
return CCW; // Using == on double makes -Wfloat-equal warnings yell at us
} if (std::fpclassify(val) == FP_ZERO) {
return CW; return COLLINEAR;
} else if (val > 0) {
return CCW;
}
return CW;
} }
/* /*
@ -114,18 +115,16 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd)
bool InScanArea(const Point& pa, const Point& pb, const Point& pc, const Point& pd) bool InScanArea(const Point& pa, const Point& pb, const Point& pc, const Point& pd)
{ {
double oadb = (pa.x - pb.x)*(pd.y - pb.y) - (pd.x - pb.x)*(pa.y - pb.y); double oadb = (pa.x - pb.x)*(pd.y - pb.y) - (pd.x - pb.x)*(pa.y - pb.y);
if (oadb >= -EPSILON) { if (oadb >= -EPSILON) {
return false; return false;
} }
double oadc = (pa.x - pc.x)*(pd.y - pc.y) - (pd.x - pc.x)*(pa.y - pc.y); double oadc = (pa.x - pc.x)*(pd.y - pc.y) - (pd.x - pc.x)*(pa.y - pc.y);
if (oadc <= EPSILON) { if (oadc <= EPSILON) {
return false; return false;
} }
return true; return true;
} }
} }
#endif

View File

@ -36,75 +36,75 @@ namespace p2t {
AdvancingFront::AdvancingFront(Node& head, Node& tail) AdvancingFront::AdvancingFront(Node& head, Node& tail)
{ {
head_ = &head; head_ = &head;
tail_ = &tail; tail_ = &tail;
search_node_ = &head; search_node_ = &head;
} }
Node* AdvancingFront::LocateNode(double x) Node* AdvancingFront::LocateNode(double x)
{ {
Node* node = search_node_; Node* node = search_node_;
if (x < node->value) { if (x < node->value) {
while ((node = node->prev) != nullptr) { while ((node = node->prev) != nullptr) {
if (x >= node->value) { if (x >= node->value) {
search_node_ = node; search_node_ = node;
return node; return node;
} }
}
} else {
while ((node = node->next) != nullptr) {
if (x < node->value) {
search_node_ = node->prev;
return node->prev;
}
}
} }
} else { return nullptr;
while ((node = node->next) != nullptr) {
if (x < node->value) {
search_node_ = node->prev;
return node->prev;
}
}
}
return nullptr;
} }
Node* AdvancingFront::FindSearchNode(double x) Node* AdvancingFront::FindSearchNode(double x)
{ {
(void)x; // suppress compiler warnings "unused parameter 'x'" (void)x; // suppress compiler warnings "unused parameter 'x'"
// TODO: implement BST index // TODO: implement BST index
return search_node_; return search_node_;
} }
Node* AdvancingFront::LocatePoint(const Point* point) Node* AdvancingFront::LocatePoint(const Point* point)
{ {
const double px = point->x; const double px = point->x;
Node* node = FindSearchNode(px); Node* node = FindSearchNode(px);
const double nx = node->point->x; const double nx = node->point->x;
if (px == nx) { if (px == nx) {
if (point != node->point) { if (point != node->point) {
// We might have two nodes with same x value for a short time // We might have two nodes with same x value for a short time
if (point == node->prev->point) { if (point == node->prev->point) {
node = node->prev; node = node->prev;
} else if (point == node->next->point) { } else if (point == node->next->point) {
node = node->next; node = node->next;
} else { } else {
assert(0); assert(0);
} }
}
} else if (px < nx) {
while ((node = node->prev) != nullptr) {
if (point == node->point) {
break;
}
}
} else {
while ((node = node->next) != nullptr) {
if (point == node->point)
break;
}
} }
} else if (px < nx) { if(node) search_node_ = node;
while ((node = node->prev) != nullptr) { return node;
if (point == node->point) {
break;
}
}
} else {
while ((node = node->next) != nullptr) {
if (point == node->point)
break;
}
}
if(node) search_node_ = node;
return node;
} }
AdvancingFront::~AdvancingFront() AdvancingFront::~AdvancingFront()
{ {
} }
} } // namespace p2t

View File

@ -29,8 +29,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef ADVANCED_FRONT_H #pragma once
#define ADVANCED_FRONT_H
#include "../common/shapes.h" #include "../common/shapes.h"
@ -40,21 +39,21 @@ struct Node;
// Advancing front node // Advancing front node
struct Node { struct Node {
Point* point; Point* point;
Triangle* triangle; Triangle* triangle;
Node* next; Node* next;
Node* prev; Node* prev;
double value; double value;
Node(Point& p) : point(&p), triangle(NULL), next(NULL), prev(NULL), value(p.x) Node(Point& p) : point(&p), triangle(NULL), next(NULL), prev(NULL), value(p.x)
{ {
} }
Node(Point& p, Triangle& t) : point(&p), triangle(&t), next(NULL), prev(NULL), value(p.x) Node(Point& p, Triangle& t) : point(&p), triangle(&t), next(NULL), prev(NULL), value(p.x)
{ {
} }
}; };
@ -62,57 +61,55 @@ struct Node {
class AdvancingFront { class AdvancingFront {
public: public:
AdvancingFront(Node& head, Node& tail); AdvancingFront(Node& head, Node& tail);
// Destructor // Destructor
~AdvancingFront(); ~AdvancingFront();
Node* head(); Node* head();
void set_head(Node* node); void set_head(Node* node);
Node* tail(); Node* tail();
void set_tail(Node* node); void set_tail(Node* node);
Node* search(); Node* search();
void set_search(Node* node); void set_search(Node* node);
/// Locate insertion point along advancing front /// Locate insertion point along advancing front
Node* LocateNode(double x); Node* LocateNode(double x);
Node* LocatePoint(const Point* point); Node* LocatePoint(const Point* point);
private: private:
Node* head_, *tail_, *search_node_; Node* head_, *tail_, *search_node_;
Node* FindSearchNode(double x); Node* FindSearchNode(double x);
}; };
inline Node* AdvancingFront::head() inline Node* AdvancingFront::head()
{ {
return head_; return head_;
} }
inline void AdvancingFront::set_head(Node* node) inline void AdvancingFront::set_head(Node* node)
{ {
head_ = node; head_ = node;
} }
inline Node* AdvancingFront::tail() inline Node* AdvancingFront::tail()
{ {
return tail_; return tail_;
} }
inline void AdvancingFront::set_tail(Node* node) inline void AdvancingFront::set_tail(Node* node)
{ {
tail_ = node; tail_ = node;
} }
inline Node* AdvancingFront::search() inline Node* AdvancingFront::search()
{ {
return search_node_; return search_node_;
} }
inline void AdvancingFront::set_search(Node* node) inline void AdvancingFront::set_search(Node* node)
{ {
search_node_ = node; search_node_ = node;
} }
} }
#endif

View File

@ -34,38 +34,38 @@ namespace p2t {
CDT::CDT(const std::vector<Point*>& polyline) CDT::CDT(const std::vector<Point*>& polyline)
{ {
sweep_context_ = new SweepContext(polyline); sweep_context_ = new SweepContext(polyline);
sweep_ = new Sweep; sweep_ = new Sweep;
} }
void CDT::AddHole(const std::vector<Point*>& polyline) void CDT::AddHole(const std::vector<Point*>& polyline)
{ {
sweep_context_->AddHole(polyline); sweep_context_->AddHole(polyline);
} }
void CDT::AddPoint(Point* point) { void CDT::AddPoint(Point* point) {
sweep_context_->AddPoint(point); sweep_context_->AddPoint(point);
} }
void CDT::Triangulate() void CDT::Triangulate()
{ {
sweep_->Triangulate(*sweep_context_); sweep_->Triangulate(*sweep_context_);
} }
std::vector<p2t::Triangle*> CDT::GetTriangles() std::vector<p2t::Triangle*> CDT::GetTriangles()
{ {
return sweep_context_->GetTriangles(); return sweep_context_->GetTriangles();
} }
std::list<p2t::Triangle*> CDT::GetMap() std::list<p2t::Triangle*> CDT::GetMap()
{ {
return sweep_context_->GetMap(); return sweep_context_->GetMap();
} }
CDT::~CDT() CDT::~CDT()
{ {
delete sweep_context_; delete sweep_context_;
delete sweep_; delete sweep_;
} }
} // namespace p2t } // namespace p2t

View File

@ -29,13 +29,14 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef CDT_H #pragma once
#define CDT_H
#include "advancing_front.h" #include "advancing_front.h"
#include "sweep_context.h" #include "sweep_context.h"
#include "sweep.h" #include "sweep.h"
#include "../common/dll_symbol.h"
/** /**
* *
* @author Mason Green <mason.green@gmail.com> * @author Mason Green <mason.green@gmail.com>
@ -44,62 +45,60 @@
namespace p2t { namespace p2t {
class CDT class P2T_DLL_SYMBOL CDT
{ {
public: public:
/** /**
* Constructor - add polyline with non repeating points * Constructor - add polyline with non repeating points
* *
* @param polyline * @param polyline
*/ */
CDT(const std::vector<Point*>& polyline); CDT(const std::vector<Point*>& polyline);
/** /**
* Destructor - clean up memory * Destructor - clean up memory
*/ */
~CDT(); ~CDT();
/** /**
* Add a hole * Add a hole
* *
* @param polyline * @param polyline
*/ */
void AddHole(const std::vector<Point*>& polyline); void AddHole(const std::vector<Point*>& polyline);
/** /**
* Add a steiner point * Add a steiner point
* *
* @param point * @param point
*/ */
void AddPoint(Point* point); void AddPoint(Point* point);
/** /**
* Triangulate - do this AFTER you've added the polyline, holes, and Steiner points * Triangulate - do this AFTER you've added the polyline, holes, and Steiner points
*/ */
void Triangulate(); void Triangulate();
/** /**
* Get CDT triangles * Get CDT triangles
*/ */
std::vector<Triangle*> GetTriangles(); std::vector<Triangle*> GetTriangles();
/** /**
* Get triangle map * Get triangle map
*/ */
std::list<Triangle*> GetMap(); std::list<Triangle*> GetMap();
private: private:
/** /**
* Internals * Internals
*/ */
SweepContext* sweep_context_; SweepContext* sweep_context_;
Sweep* sweep_; Sweep* sweep_;
}; };
} }
#endif

File diff suppressed because it is too large Load Diff

View File

@ -36,8 +36,7 @@
* "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com * "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com
*/ */
#ifndef SWEEP_H #pragma once
#define SWEEP_H
#include <vector> #include <vector>
@ -53,28 +52,28 @@ class Sweep
{ {
public: public:
/** /**
* Triangulate * Triangulate
* *
* @param tcx * @param tcx
*/ */
void Triangulate(SweepContext& tcx); void Triangulate(SweepContext& tcx);
/** /**
* Destructor - clean up memory * Destructor - clean up memory
*/ */
~Sweep(); ~Sweep();
private: private:
/** /**
* Start sweeping the Y-sorted point set from bottom to top * Start sweeping the Y-sorted point set from bottom to top
* *
* @param tcx * @param tcx
*/ */
void SweepPoints(SweepContext& tcx); void SweepPoints(SweepContext& tcx);
/** /**
* Find closes node to the left of the new point and * Find closes node to the left of the new point and
* create a new triangle. If needed new holes and basins * create a new triangle. If needed new holes and basins
* will be filled to. * will be filled to.
@ -83,20 +82,20 @@ private:
* @param point * @param point
* @return * @return
*/ */
Node& PointEvent(SweepContext& tcx, Point& point); Node& PointEvent(SweepContext& tcx, Point& point);
/** /**
* *
* *
* @param tcx * @param tcx
* @param edge * @param edge
* @param node * @param node
*/ */
void EdgeEvent(SweepContext& tcx, Edge* edge, Node* node); void EdgeEvent(SweepContext& tcx, Edge* edge, Node* node);
void EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point); void EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point);
/** /**
* Creates a new front triangle and legalize it * Creates a new front triangle and legalize it
* *
* @param tcx * @param tcx
@ -104,21 +103,21 @@ private:
* @param node * @param node
* @return * @return
*/ */
Node& NewFrontTriangle(SweepContext& tcx, Point& point, Node& node); Node& NewFrontTriangle(SweepContext& tcx, Point& point, Node& node);
/** /**
* Adds a triangle to the advancing front to fill a hole. * Adds a triangle to the advancing front to fill a hole.
* @param tcx * @param tcx
* @param node - middle node, that is the bottom of the hole * @param node - middle node, that is the bottom of the hole
*/ */
void Fill(SweepContext& tcx, Node& node); void Fill(SweepContext& tcx, Node& node);
/** /**
* Returns true if triangle was legalized * Returns true if triangle was legalized
*/ */
bool Legalize(SweepContext& tcx, Triangle& t); bool Legalize(SweepContext& tcx, Triangle& t);
/** /**
* <b>Requirement</b>:<br> * <b>Requirement</b>:<br>
* 1. a,b and c form a triangle.<br> * 1. a,b and c form a triangle.<br>
* 2. a and d is know to be on opposite side of bc<br> * 2. a and d is know to be on opposite side of bc<br>
@ -142,9 +141,9 @@ private:
* @param d - point opposite a * @param d - point opposite a
* @return true if d is inside circle, false if on circle edge * @return true if d is inside circle, false if on circle edge
*/ */
bool Incircle(const Point& pa, const Point& pb, const Point& pc, const Point& pd) const; bool Incircle(const Point& pa, const Point& pb, const Point& pc, const Point& pd) const;
/** /**
* Rotates a triangle pair one vertex CW * Rotates a triangle pair one vertex CW
*<pre> *<pre>
* n2 n2 * n2 n2
@ -158,37 +157,38 @@ private:
* n4 n4 * n4 n4
* </pre> * </pre>
*/ */
void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) const; void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) const;
/** /**
* Fills holes in the Advancing Front * Fills holes in the Advancing Front
* *
* *
* @param tcx * @param tcx
* @param n * @param n
*/ */
void FillAdvancingFront(SweepContext& tcx, Node& n); void FillAdvancingFront(SweepContext& tcx, Node& n);
// Decision-making about when to Fill hole. // Decision-making about when to Fill hole.
// Contributed by ToolmakerSteve2 // Contributed by ToolmakerSteve2
bool LargeHole_DontFill(const Node* node) const; bool LargeHole_DontFill(const Node* node) const;
bool AngleExceeds90Degrees(const Point* origin, const Point* pa, const Point* pb) const; bool AngleIsNegative(const Point* origin, const Point* pa, const Point* pb) const;
bool AngleExceedsPlus90DegreesOrIsNegative(const Point* origin, const Point* pa, const Point* pb) const; bool AngleExceeds90Degrees(const Point* origin, const Point* pa, const Point* pb) const;
double Angle(const Point* origin, const Point* pa, const Point* pb) const; bool AngleExceedsPlus90DegreesOrIsNegative(const Point* origin, const Point* pa, const Point* pb) const;
double Angle(const Point* origin, const Point* pa, const Point* pb) const;
/** /**
* *
* @param node - middle node * @param node - middle node
* @return the angle between 3 front nodes * @return the angle between 3 front nodes
*/ */
double HoleAngle(const Node& node) const; double HoleAngle(const Node& node) const;
/** /**
* The basin angle is decided against the horizontal line [1,0] * The basin angle is decided against the horizontal line [1,0]
*/ */
double BasinAngle(const Node& node) const; double BasinAngle(const Node& node) const;
/** /**
* Fills a basin that has formed on the Advancing Front to the right * Fills a basin that has formed on the Advancing Front to the right
* of given node.<br> * of given node.<br>
* First we decide a left,bottom and right node that forms the * First we decide a left,bottom and right node that forms the
@ -197,42 +197,42 @@ private:
* @param tcx * @param tcx
* @param node - starting node, this or next node will be left node * @param node - starting node, this or next node will be left node
*/ */
void FillBasin(SweepContext& tcx, Node& node); void FillBasin(SweepContext& tcx, Node& node);
/** /**
* Recursive algorithm to fill a Basin with triangles * Recursive algorithm to fill a Basin with triangles
* *
* @param tcx * @param tcx
* @param node - bottom_node * @param node - bottom_node
* @param cnt - counter used to alternate on even and odd numbers * @param cnt - counter used to alternate on even and odd numbers
*/ */
void FillBasinReq(SweepContext& tcx, Node* node); void FillBasinReq(SweepContext& tcx, Node* node);
bool IsShallow(SweepContext& tcx, Node& node); bool IsShallow(SweepContext& tcx, Node& node);
bool IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq); bool IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq);
void FillEdgeEvent(SweepContext& tcx, Edge* edge, Node* node); void FillEdgeEvent(SweepContext& tcx, Edge* edge, Node* node);
void FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node); void FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node);
void FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node); void FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node);
void FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node); void FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
void FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p); void FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p);
/** /**
* After a flip we have two triangles and know that only one will still be * After a flip we have two triangles and know that only one will still be
* intersecting the edge. So decide which to contiune with and legalize the other * intersecting the edge. So decide which to contiune with and legalize the other
* *
@ -244,9 +244,9 @@ private:
* @param op - another point shared by both triangles * @param op - another point shared by both triangles
* @return returns the triangle still intersecting the edge * @return returns the triangle still intersecting the edge
*/ */
Triangle& NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op); Triangle& NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op);
/** /**
* When we need to traverse from one triangle to the next we need * When we need to traverse from one triangle to the next we need
* the point in current triangle that is the opposite point to the next * the point in current triangle that is the opposite point to the next
* triangle. * triangle.
@ -257,9 +257,9 @@ private:
* @param op * @param op
* @return * @return
*/ */
Point& NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op); Point& NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op);
/** /**
* Scan part of the FlipScan algorithm<br> * Scan part of the FlipScan algorithm<br>
* When a triangle pair isn't flippable we will scan for the next * When a triangle pair isn't flippable we will scan for the next
* point that is inside the flip triangle scan area. When found * point that is inside the flip triangle scan area. When found
@ -272,14 +272,12 @@ private:
* @param t * @param t
* @param p * @param p
*/ */
void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p); void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p);
void FinalizationPolygon(SweepContext& tcx); void FinalizationPolygon(SweepContext& tcx);
std::vector<Node*> nodes_; std::vector<Node*> nodes_;
}; };
} }
#endif

View File

@ -1,5 +1,5 @@
/* /*
* Poly2Tri Copyright (c) 2009-2018, Poly2Tri Contributors * Poly2Tri Copyright (c) 2009-2022, Poly2Tri Contributors
* https://github.com/jhasse/poly2tri * https://github.com/jhasse/poly2tri
* *
* All rights reserved. * All rights reserved.
@ -34,152 +34,152 @@
namespace p2t { namespace p2t {
SweepContext::SweepContext(const std::vector<Point*>& polyline) : points_(polyline), SweepContext::SweepContext(std::vector<Point*> polyline) : points_(std::move(polyline)),
front_(nullptr), front_(nullptr),
head_(nullptr), head_(nullptr),
tail_(nullptr), tail_(nullptr),
af_head_(nullptr), af_head_(nullptr),
af_middle_(nullptr), af_middle_(nullptr),
af_tail_(nullptr) af_tail_(nullptr)
{ {
InitEdges(points_); InitEdges(points_);
} }
void SweepContext::AddHole(const std::vector<Point*>& polyline) void SweepContext::AddHole(const std::vector<Point*>& polyline)
{ {
InitEdges(polyline); InitEdges(polyline);
for(unsigned int i = 0; i < polyline.size(); i++) { for (auto i : polyline) {
points_.push_back(polyline[i]); points_.push_back(i);
} }
} }
void SweepContext::AddPoint(Point* point) { void SweepContext::AddPoint(Point* point) {
points_.push_back(point); points_.push_back(point);
} }
std::vector<Triangle*> &SweepContext::GetTriangles() std::vector<Triangle*> &SweepContext::GetTriangles()
{ {
return triangles_; return triangles_;
} }
std::list<Triangle*> &SweepContext::GetMap() std::list<Triangle*> &SweepContext::GetMap()
{ {
return map_; return map_;
} }
void SweepContext::InitTriangulation() void SweepContext::InitTriangulation()
{ {
double xmax(points_[0]->x), xmin(points_[0]->x); double xmax(points_[0]->x), xmin(points_[0]->x);
double ymax(points_[0]->y), ymin(points_[0]->y); double ymax(points_[0]->y), ymin(points_[0]->y);
// Calculate bounds. // Calculate bounds.
for (unsigned int i = 0; i < points_.size(); i++) { for (auto& point : points_) {
Point& p = *points_[i]; Point& p = *point;
if (p.x > xmax) if (p.x > xmax)
xmax = p.x; xmax = p.x;
if (p.x < xmin) if (p.x < xmin)
xmin = p.x; xmin = p.x;
if (p.y > ymax) if (p.y > ymax)
ymax = p.y; ymax = p.y;
if (p.y < ymin) if (p.y < ymin)
ymin = p.y; ymin = p.y;
} }
double dx = kAlpha * (xmax - xmin); double dx = kAlpha * (xmax - xmin);
double dy = kAlpha * (ymax - ymin); double dy = kAlpha * (ymax - ymin);
head_ = new Point(xmin - dx, ymin - dy); head_ = new Point(xmin - dx, ymin - dy);
tail_ = new Point(xmax + dx, ymin - dy); tail_ = new Point(xmax + dx, ymin - dy);
// Sort points along y-axis // Sort points along y-axis
std::sort(points_.begin(), points_.end(), cmp); std::sort(points_.begin(), points_.end(), cmp);
} }
void SweepContext::InitEdges(const std::vector<Point*>& polyline) void SweepContext::InitEdges(const std::vector<Point*>& polyline)
{ {
size_t num_points = polyline.size(); size_t num_points = polyline.size();
for (size_t i = 0; i < num_points; i++) { for (size_t i = 0; i < num_points; i++) {
size_t j = i < num_points - 1 ? i + 1 : 0; size_t j = i < num_points - 1 ? i + 1 : 0;
edge_list.push_back(new Edge(*polyline[i], *polyline[j])); edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
} }
} }
Point* SweepContext::GetPoint(size_t index) Point* SweepContext::GetPoint(size_t index)
{ {
return points_[index]; return points_[index];
} }
void SweepContext::AddToMap(Triangle* triangle) void SweepContext::AddToMap(Triangle* triangle)
{ {
map_.push_back(triangle); map_.push_back(triangle);
} }
Node* SweepContext::LocateNode(const Point& point) Node* SweepContext::LocateNode(const Point& point)
{ {
// TODO implement search tree // TODO implement search tree
return front_->LocateNode(point.x); return front_->LocateNode(point.x);
} }
void SweepContext::CreateAdvancingFront() void SweepContext::CreateAdvancingFront()
{ {
// Initial triangle // Initial triangle
Triangle* triangle = new Triangle(*points_[0], *head_, *tail_); Triangle* triangle = new Triangle(*points_[0], *head_, *tail_);
map_.push_back(triangle); map_.push_back(triangle);
af_head_ = new Node(*triangle->GetPoint(1), *triangle); af_head_ = new Node(*triangle->GetPoint(1), *triangle);
af_middle_ = new Node(*triangle->GetPoint(0), *triangle); af_middle_ = new Node(*triangle->GetPoint(0), *triangle);
af_tail_ = new Node(*triangle->GetPoint(2)); af_tail_ = new Node(*triangle->GetPoint(2));
front_ = new AdvancingFront(*af_head_, *af_tail_); front_ = new AdvancingFront(*af_head_, *af_tail_);
// TODO: More intuitive if head is middles next and not previous? // TODO: More intuitive if head is middles next and not previous?
// so swap head and tail // so swap head and tail
af_head_->next = af_middle_; af_head_->next = af_middle_;
af_middle_->next = af_tail_; af_middle_->next = af_tail_;
af_middle_->prev = af_head_; af_middle_->prev = af_head_;
af_tail_->prev = af_middle_; af_tail_->prev = af_middle_;
} }
void SweepContext::RemoveNode(Node* node) void SweepContext::RemoveNode(Node* node)
{ {
delete node; delete node;
} }
void SweepContext::MapTriangleToNodes(Triangle& t) void SweepContext::MapTriangleToNodes(Triangle& t)
{ {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (!t.GetNeighbor(i)) { if (!t.GetNeighbor(i)) {
Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i))); Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
if (n) if (n)
n->triangle = &t; n->triangle = &t;
}
} }
}
} }
void SweepContext::RemoveFromMap(Triangle* triangle) void SweepContext::RemoveFromMap(Triangle* triangle)
{ {
map_.remove(triangle); map_.remove(triangle);
} }
void SweepContext::MeshClean(Triangle& triangle) void SweepContext::MeshClean(Triangle& triangle)
{ {
std::vector<Triangle *> triangles; std::vector<Triangle *> triangles;
triangles.push_back(&triangle); triangles.push_back(&triangle);
while(!triangles.empty()){ while(!triangles.empty()){
Triangle *t = triangles.back(); Triangle *t = triangles.back();
triangles.pop_back(); triangles.pop_back();
if (t != nullptr && !t->IsInterior()) { if (t != nullptr && !t->IsInterior()) {
t->IsInterior(true); t->IsInterior(true);
triangles_.push_back(t); triangles_.push_back(t);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (!t->constrained_edge[i]) if (!t->constrained_edge[i])
triangles.push_back(t->GetNeighbor(i)); triangles.push_back(t->GetNeighbor(i));
} }
}
} }
}
} }
SweepContext::~SweepContext() SweepContext::~SweepContext()
@ -194,17 +194,13 @@ SweepContext::~SweepContext()
delete af_middle_; delete af_middle_;
delete af_tail_; delete af_tail_;
typedef std::list<Triangle*> type_list; for (auto ptr : map_) {
for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
Triangle* ptr = *iter;
delete ptr; delete ptr;
} }
for(unsigned int i = 0; i < edge_list.size(); i++) { for (auto& i : edge_list) {
delete edge_list[i]; delete i;
} }
} }
} // namespace p2t } // namespace p2t

View File

@ -1,5 +1,5 @@
/* /*
* Poly2Tri Copyright (c) 2009-2018, Poly2Tri Contributors * Poly2Tri Copyright (c) 2009-2022, Poly2Tri Contributors
* https://github.com/jhasse/poly2tri * https://github.com/jhasse/poly2tri
* *
* All rights reserved. * All rights reserved.
@ -29,8 +29,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef SWEEP_CONTEXT_H #pragma once
#define SWEEP_CONTEXT_H
#include <list> #include <list>
#include <vector> #include <vector>
@ -51,136 +50,135 @@ class AdvancingFront;
class SweepContext { class SweepContext {
public: public:
/// Constructor /// Constructor
SweepContext(const std::vector<Point*>& polyline); explicit SweepContext(std::vector<Point*> polyline);
/// Destructor /// Destructor
~SweepContext(); ~SweepContext();
void set_head(Point* p1); void set_head(Point* p1);
Point* head() const; Point* head() const;
void set_tail(Point* p1); void set_tail(Point* p1);
Point* tail() const; Point* tail() const;
size_t point_count() const; size_t point_count() const;
Node* LocateNode(const Point& point); Node* LocateNode(const Point& point);
void RemoveNode(Node* node); void RemoveNode(Node* node);
void CreateAdvancingFront(); void CreateAdvancingFront();
/// Try to map a node to all sides of this triangle that don't have a neighbor /// Try to map a node to all sides of this triangle that don't have a neighbor
void MapTriangleToNodes(Triangle& t); void MapTriangleToNodes(Triangle& t);
void AddToMap(Triangle* triangle); void AddToMap(Triangle* triangle);
Point* GetPoint(size_t index); Point* GetPoint(size_t index);
Point* GetPoints(); Point* GetPoints();
void RemoveFromMap(Triangle* triangle); void RemoveFromMap(Triangle* triangle);
void AddHole(const std::vector<Point*>& polyline); void AddHole(const std::vector<Point*>& polyline);
void AddPoint(Point* point); void AddPoint(Point* point);
AdvancingFront* front() const; AdvancingFront* front() const;
void MeshClean(Triangle& triangle); void MeshClean(Triangle& triangle);
std::vector<Triangle*> &GetTriangles(); std::vector<Triangle*> &GetTriangles();
std::list<Triangle*> &GetMap(); std::list<Triangle*> &GetMap();
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
struct Basin { struct Basin {
Node* left_node; Node* left_node;
Node* bottom_node; Node* bottom_node;
Node* right_node; Node* right_node;
double width; double width;
bool left_highest; bool left_highest;
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL), width(0.0), left_highest(false) Basin()
{ : left_node(nullptr), bottom_node(nullptr), right_node(nullptr), width(0.0), left_highest(false)
} {
}
void Clear() void Clear()
{ {
left_node = NULL; left_node = nullptr;
bottom_node = NULL; bottom_node = nullptr;
right_node = NULL; right_node = nullptr;
width = 0.0; width = 0.0;
left_highest = false; left_highest = false;
} }
}; };
struct EdgeEvent { struct EdgeEvent {
Edge* constrained_edge; Edge* constrained_edge;
bool right; bool right;
EdgeEvent() : constrained_edge(NULL), right(false) EdgeEvent() : constrained_edge(NULL), right(false)
{ {
} }
}; };
Basin basin; Basin basin;
EdgeEvent edge_event; EdgeEvent edge_event;
private: private:
friend class Sweep; friend class Sweep;
std::vector<Triangle*> triangles_; std::vector<Triangle*> triangles_;
std::list<Triangle*> map_; std::list<Triangle*> map_;
std::vector<Point*> points_; std::vector<Point*> points_;
// Advancing front // Advancing front
AdvancingFront* front_; AdvancingFront* front_;
// head point used with advancing front // head point used with advancing front
Point* head_; Point* head_;
// tail point used with advancing front // tail point used with advancing front
Point* tail_; Point* tail_;
Node *af_head_, *af_middle_, *af_tail_; Node *af_head_, *af_middle_, *af_tail_;
void InitTriangulation(); void InitTriangulation();
void InitEdges(const std::vector<Point*>& polyline); void InitEdges(const std::vector<Point*>& polyline);
}; };
inline AdvancingFront* SweepContext::front() const inline AdvancingFront* SweepContext::front() const
{ {
return front_; return front_;
} }
inline size_t SweepContext::point_count() const inline size_t SweepContext::point_count() const
{ {
return points_.size(); return points_.size();
} }
inline void SweepContext::set_head(Point* p1) inline void SweepContext::set_head(Point* p1)
{ {
head_ = p1; head_ = p1;
} }
inline Point* SweepContext::head() const inline Point* SweepContext::head() const
{ {
return head_; return head_;
} }
inline void SweepContext::set_tail(Point* p1) inline void SweepContext::set_tail(Point* p1)
{ {
tail_ = p1; tail_ = p1;
} }
inline Point* SweepContext::tail() const inline Point* SweepContext::tail() const
{ {
return tail_; return tail_;
} }
} }
#endif