Skip to content

Commit

Permalink
Fixed backward zebra polygon (#834)
Browse files Browse the repository at this point in the history
fixed two more bugs
  • Loading branch information
elalish authored Jun 9, 2024
1 parent 12b9494 commit 3afe846
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/polygon/src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ class EarClip {
return left->InsideEdge(left->right, precision, true);
}

// Subtly different from !IsConvex because IsConvex will return true for
// colinear non-folded verts, while IsReflex will always check until actual
// certainty is determined.
bool IsReflex(float precision) const {
return !left->InsideEdge(left->right, precision, true);
}

// This function is the core of finding a proper place to keyhole. It runs
// on this Vert, which represents the edge from this to right. It returns
// an iterator to the vert to connect to (either this or right) and a bool
Expand All @@ -368,7 +375,8 @@ class EarClip {
std::pair<VertItr, bool> InterpY2X(glm::vec2 start, int onTop,
float precision) const {
const auto none = std::make_pair(left, false);
if (pos.y < start.y && right->pos.y >= start.y) {
if (pos.y < start.y && right->pos.y >= start.y &&
(pos.x > start.x - precision || right->pos.x > start.x - precision)) {
return std::make_pair(left->right, true);
} else if (onTop != 0 && pos.x > start.x - precision &&
pos.y > start.y - precision && pos.y < start.y + precision &&
Expand Down Expand Up @@ -532,13 +540,6 @@ class EarClip {
// triangulations of polygons with holes, due to vert duplication.
triangles_.push_back(
{ear->left->mesh_idx, ear->mesh_idx, ear->right->mesh_idx});
#ifdef MANIFOLD_DEBUG
if (params.verbose) {
std::cout << "output tri: " << ear->mesh_idx << ", "
<< ear->right->mesh_idx << ", " << ear->left->mesh_idx
<< std::endl;
}
#endif
} else {
PRINT("Topological degenerate!");
}
Expand Down Expand Up @@ -704,7 +705,7 @@ class EarClip {
vert->pos.y * above > start->pos.y * above - precision_ &&
(inside > 0 || (inside == 0 && vert->pos.x < best->pos.x)) &&
vert->InsideEdge(edge, precision_, true) &&
!vert->IsConvex(precision_)) {
vert->IsReflex(precision_)) {
if (vert->pos.y > start->pos.y - precision_ &&
vert->pos.y < start->pos.y + precision_) {
if (onTop > 0 && vert->left->pos.x < vert->pos.x &&
Expand Down
12 changes: 5 additions & 7 deletions test/polygon_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,18 @@ Polygons Duplicate(Polygons polys) {
}

void TestPoly(const Polygons &polys, int expectedNumTri,
float precision = -1.0f, bool onlyBasic = false) {
float precision = -1.0f) {
PolygonParams().verbose = options.params.verbose;

std::vector<glm::ivec3> triangles;
EXPECT_NO_THROW(triangles = Triangulate(polys, precision));
EXPECT_EQ(triangles.size(), expectedNumTri) << "Basic";

if (!onlyBasic) {
EXPECT_NO_THROW(triangles = Triangulate(Turn180(polys), precision));
EXPECT_EQ(triangles.size(), expectedNumTri) << "Turn 180";
EXPECT_NO_THROW(triangles = Triangulate(Turn180(polys), precision));
EXPECT_EQ(triangles.size(), expectedNumTri) << "Turn 180";

EXPECT_NO_THROW(triangles = Triangulate(Duplicate(polys), precision));
EXPECT_EQ(triangles.size(), 2 * expectedNumTri) << "Duplicate";
}
EXPECT_NO_THROW(triangles = Triangulate(Duplicate(polys), precision));
EXPECT_EQ(triangles.size(), 2 * expectedNumTri) << "Duplicate";

PolygonParams().verbose = false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/zebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62639,7 +62639,7 @@ TEST(Polygon, Zebra) {
{5.59076977, 14.00315}, //
{5.59097004, 14.00315}, //
});
TestPoly(polys, 62597, -1, true);
TestPoly(polys, 62597);
}

TEST(Polygon, Zebra1) {
Expand Down

0 comments on commit 3afe846

Please sign in to comment.