Skip to content

Commit

Permalink
Triangulator: shift on merge (#528)
Browse files Browse the repository at this point in the history
* allow shift on merge

* fixed more tests with proper uncertainty

* fixed another test
  • Loading branch information
elalish authored Aug 14, 2023
1 parent 9290d42 commit 8011fb6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
48 changes: 38 additions & 10 deletions src/polygon/src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,15 @@ class Monotones {
struct EdgePair {
VertItr vWest, vEast, vMerge;
PairItr nextPair;
bool westCertain, eastCertain, startCertain;
bool westCertain, eastCertain;

int WestOf(VertItr vert, float precision) const {
if (vEast->pos.x + precision < vert->pos.x &&
vEast->right->pos.x + precision < vert->pos.x)
return 1;
if (vEast->pos.x - precision > vert->pos.x &&
vEast->right->pos.x - precision > vert->pos.x)
return -1;
int westOf = CCW(vEast->right->pos, vEast->pos, vert->pos, precision);
if (westOf == 0 && !vert->right->Processed())
westOf =
Expand All @@ -394,6 +400,12 @@ class Monotones {
}

int EastOf(VertItr vert, float precision) const {
if (vWest->pos.x - precision > vert->pos.x &&
vWest->left->pos.x - precision > vert->pos.x)
return 1;
if (vWest->pos.x + precision < vert->pos.x &&
vWest->left->pos.x + precision < vert->pos.x)
return -1;
int eastOf = CCW(vWest->pos, vWest->left->pos, vert->pos, precision);
if (eastOf == 0 && !vert->right->Processed())
eastOf = CCW(vWest->pos, vWest->left->pos, vert->right->pos, precision);
Expand Down Expand Up @@ -536,8 +548,13 @@ class Monotones {
PRINT("End");
CloseEnd(vert);
return End;
} else if (westPair != activePairs_.end() &&
std::next(westPair) == eastPair) {
} else if (!eastPair->westCertain || !westPair->eastCertain ||
(westPair != activePairs_.end() &&
std::next(westPair) == eastPair)) {
if (!eastPair->westCertain)
activePairs_.splice(std::next(westPair), activePairs_, eastPair);
if (!westPair->eastCertain)
activePairs_.splice(eastPair, activePairs_, westPair);
// facing out
PRINT("Merge");
CloseEnd(vert);
Expand Down Expand Up @@ -747,8 +764,13 @@ class Monotones {
}
if (isHole) return true;

activePairs_.splice(activePairs_.end(), activePairs_, inputPair);
inputPair->eastCertain = true;
if (activePairs_.size() > 1) {
activePairs_.splice(activePairs_.end(), activePairs_, inputPair);
inputPair->eastCertain =
std::prev(inputPair)->WestOf(vert, precision_) > 0;
} else {
inputPair->eastCertain = true;
}
return false;
}

Expand Down Expand Up @@ -780,9 +802,15 @@ class Monotones {
}
if (isHole) return true;

if (inputPair != activePairs_.begin())
activePairs_.splice(activePairs_.begin(), activePairs_, inputPair);
inputPair->westCertain = true;
activePairs_.splice(activePairs_.begin(), activePairs_, inputPair);

const PairItr eastPair = std::next(inputPair);
if (eastPair != activePairs_.end()) {
inputPair->westCertain = eastPair->EastOf(vert, precision_) > 0;
} else {
inputPair->westCertain = true;
}

return false;
}

Expand Down Expand Up @@ -845,8 +873,8 @@ class Monotones {
bool isHole = false;
if (type == Start) {
newPair = activePairs_.insert(
activePairs_.begin(), {vert, vert, monotones_.end(),
activePairs_.end(), false, false, false});
activePairs_.begin(),
{vert, vert, monotones_.end(), activePairs_.end(), false, false});
SetVWest(newPair, vert);
SetVEast(newPair, vert);
const int hole = IsHole(vert);
Expand Down
34 changes: 15 additions & 19 deletions test/polygon_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ TEST(Polygon, Sliver7) {
TestPoly(polys, 6);
}

TEST(Polygon, DISABLED_Sliver8) {
TEST(Polygon, Sliver8) {
Polygons polys;
polys.push_back({
{27.9279995, 4.9749999}, //
Expand All @@ -531,7 +531,7 @@ TEST(Polygon, DISABLED_Sliver8) {
TestPoly(polys, 5);
}

TEST(Polygon, DISABLED_Sliver9) {
TEST(Polygon, Sliver9) {
Polygons polys;
polys.push_back({
{1.49183154, -0.4795963766194}, //
Expand Down Expand Up @@ -883,7 +883,7 @@ TEST(Polygon, Precision3) {
TestPoly(polys, 3, 0.00068);
};

TEST(Polygon, DISABLED_Sweep) {
TEST(Polygon, Sweep) {
Polygons polys;
polys.push_back({
{0, 0.637057483}, //
Expand Down Expand Up @@ -3087,7 +3087,7 @@ TEST(Polygon, BigSponge) {
TestPoly(polys, 1771);
}

TEST(Polygon, DISABLED_Woodgrain1) {
TEST(Polygon, Woodgrain1) {
Polygons polys;
polys.push_back({
{72.5, -50}, //
Expand All @@ -3108,24 +3108,20 @@ TEST(Polygon, DISABLED_Woodgrain1) {
TestPoly(polys, 12, 0.00145);
}

TEST(Polygon, DISABLED_Woodgrain2) {
TEST(Polygon, Woodgrain2) {
Polygons polys;
polys.push_back({
{-15.694437, 33.8253479}, //
{13.2702093, -3.28426218}, //
{53.4751701, -32.0106316}, //
{61.4174232, 44.4299698}, //
{22.7295628, -49.5001106}, //
{-21.1059895, -49.9855423}, //
{-35.0938339, 10.1798563}, //
{-72.5, -50}, //
{-12.7149582, -50}, //
});
polys.push_back({
{17.61759, 25.2365837}, //
{17.6270828, 25.2370052}, //
{17.1236248, 26.0772629}, //
{20.4602833, -7.37109232}, //
{-40.1850586, -49.9840584}, //
{-40.2071075, -49.9845123}, //
{-40.3756485, -49.9855652}, //
{-40.0327415, -19.857235}, //
});
polys.push_back({
{44.9942932, 39.2275734}, //
{45.120945, 29.0997658}, //
{44.896656, 25.2351685}, //
});
TestPoly(polys, 13, 0.00145);
TestPoly(polys, 9, 0.00145);
}

0 comments on commit 8011fb6

Please sign in to comment.