Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46022 from nirvn/poly2tri_up
Poly2tri library update to fix crash with self-intersecting ring & fix random marker fill
  • Loading branch information
nirvn committed Nov 16, 2021
2 parents 0cfee7f + 29859bb commit 7495f57
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 152 deletions.
48 changes: 26 additions & 22 deletions external/poly2tri/common/shapes.cc
Expand Up @@ -35,14 +35,18 @@

namespace p2t {

Point::Point(double x, double y) : x(x), y(y)
{
}

std::ostream& operator<<(std::ostream& out, const Point& point) {
return out << point.x << "," << point.y;
}

Triangle::Triangle(Point& a, Point& b, Point& c)
{
points_[0] = &a; points_[1] = &b; points_[2] = &c;
neighbors_[0] = NULL; neighbors_[1] = NULL; neighbors_[2] = NULL;
neighbors_[0] = nullptr; neighbors_[1] = nullptr; neighbors_[2] = nullptr;
constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
interior_ = false;
Expand Down Expand Up @@ -85,36 +89,36 @@ void Triangle::Clear()
for( int i=0; i<3; i++ )
{
t = neighbors_[i];
if( t != NULL )
if( t != nullptr )
{
t->ClearNeighbor( this );
}
}
ClearNeighbors();
points_[0]=points_[1]=points_[2] = NULL;
points_[0]=points_[1]=points_[2] = nullptr;
}

void Triangle::ClearNeighbor(const Triangle *triangle )
{
if( neighbors_[0] == triangle )
{
neighbors_[0] = NULL;
neighbors_[0] = nullptr;
}
else if( neighbors_[1] == triangle )
{
neighbors_[1] = NULL;
neighbors_[1] = nullptr;
}
else
{
neighbors_[2] = NULL;
neighbors_[2] = nullptr;
}
}

void Triangle::ClearNeighbors()
{
neighbors_[0] = NULL;
neighbors_[1] = NULL;
neighbors_[2] = NULL;
neighbors_[0] = nullptr;
neighbors_[1] = nullptr;
neighbors_[2] = nullptr;
}

void Triangle::ClearDelunayEdges()
Expand Down Expand Up @@ -226,7 +230,7 @@ Point* Triangle::PointCW(const Point& point)
return points_[1];
}
assert(0);
return NULL;
return nullptr;
}

// The point counter-clockwise to given point
Expand All @@ -240,7 +244,18 @@ Point* Triangle::PointCCW(const Point& point)
return points_[0];
}
assert(0);
return NULL;
return nullptr;
}

// The neighbor across to given point
Triangle* Triangle::NeighborAcross(const Point& point)
{
if (&point == points_[0]) {
return neighbors_[0];
} else if (&point == points_[1]) {
return neighbors_[1];
}
return neighbors_[2];
}

// The neighbor clockwise to given point
Expand Down Expand Up @@ -349,17 +364,6 @@ void Triangle::SetDelunayEdgeCW(const Point& p, bool e)
}
}

// The neighbor across to given point
Triangle& Triangle::NeighborAcross(const Point& opoint)
{
if (&opoint == points_[0]) {
return *neighbors_[0];
} else if (&opoint == points_[1]) {
return *neighbors_[1];
}
return *neighbors_[2];
}

void Triangle::DebugPrint()
{
std::cout << *points_[0] << " " << *points_[1] << " " << *points_[2] << std::endl;
Expand Down
7 changes: 3 additions & 4 deletions external/poly2tri/common/shapes.h
Expand Up @@ -57,7 +57,7 @@ struct Point {
std::vector<Edge*> edge_list;

/// Construct using coordinates.
Point(double x, double y) : x(x), y(y) {}
Point(double x, double y);

/// Set this point to all zeros.
void set_zero()
Expand Down Expand Up @@ -176,6 +176,7 @@ void MarkConstrainedEdge(Point* p, Point* q);
int Index(const Point* p);
int EdgeIndex(const Point* p1, const Point* p2);

Triangle* NeighborAcross(const Point& point);
Triangle* NeighborCW(const Point& point);
Triangle* NeighborCCW(const Point& point);
bool GetConstrainedEdgeCCW(const Point& p);
Expand Down Expand Up @@ -203,8 +204,6 @@ void ClearDelunayEdges();
inline bool IsInterior();
inline void IsInterior(bool b);

Triangle& NeighborAcross(const Point& opoint);

void DebugPrint();

bool CircumcicleContains(const Point&) const;
Expand Down Expand Up @@ -260,7 +259,7 @@ 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.
Expand Down
10 changes: 5 additions & 5 deletions external/poly2tri/sweep/advancing_front.cc
Expand Up @@ -46,21 +46,21 @@ Node* AdvancingFront::LocateNode(double x)
Node* node = search_node_;

if (x < node->value) {
while ((node = node->prev) != NULL) {
while ((node = node->prev) != nullptr) {
if (x >= node->value) {
search_node_ = node;
return node;
}
}
} else {
while ((node = node->next) != NULL) {
while ((node = node->next) != nullptr) {
if (x < node->value) {
search_node_ = node->prev;
return node->prev;
}
}
}
return NULL;
return nullptr;
}

Node* AdvancingFront::FindSearchNode(double x)
Expand Down Expand Up @@ -88,13 +88,13 @@ Node* AdvancingFront::LocatePoint(const Point* point)
}
}
} else if (px < nx) {
while ((node = node->prev) != NULL) {
while ((node = node->prev) != nullptr) {
if (point == node->point) {
break;
}
}
} else {
while ((node = node->next) != NULL) {
while ((node = node->next) != nullptr) {
if (point == node->point)
break;
}
Expand Down
4 changes: 2 additions & 2 deletions external/poly2tri/sweep/cdt.cc
@@ -1,5 +1,5 @@
/*
* Poly2Tri Copyright (c) 2009-2018, Poly2Tri Contributors
* Poly2Tri Copyright (c) 2009-2021, Poly2Tri Contributors
* https://github.com/jhasse/poly2tri
*
* All rights reserved.
Expand Down Expand Up @@ -68,4 +68,4 @@ CDT::~CDT()
delete sweep_;
}

}
} // namespace p2t

0 comments on commit 7495f57

Please sign in to comment.