Skip to content

Commit

Permalink
Fix simplification of open path (#714)
Browse files Browse the repository at this point in the history
* Add failing simplification test

A slightly wiggly line from (0,0) to (0,100), which we expect to
simplify to a straight line of length 100 with just the two endpoints
remaining.

Instead it simplifies to a much shorter line.

* Don't recompute distance for open endpoint
  • Loading branch information
klauslh authored Nov 17, 2023
1 parent 2c775d6 commit 7a481e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ endif()
Tests/TestPolytreeUnion.cpp
Tests/TestRandomPaths.cpp
Tests/TestRectClip.cpp
Tests/TestSimplifyPath.cpp
Tests/TestTrimCollinear.cpp
Tests/TestWindows.cpp
)
Expand Down
3 changes: 2 additions & 1 deletion CPP/Clipper2Lib/include/clipper2/clipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ namespace Clipper2Lib {
curr = next;
next = GetNext(next, high, flags);
prior2 = GetPrior(prior, high, flags);
distSqr[curr] = PerpendicDistFromLineSqrd(path[curr], path[prior], path[next]);
if (curr != high || isClosedPath)
distSqr[curr] = PerpendicDistFromLineSqrd(path[curr], path[prior], path[next]);
if (prior != 0 || isClosedPath)
distSqr[prior] = PerpendicDistFromLineSqrd(path[prior], path[prior2], path[curr]);
}
Expand Down
14 changes: 14 additions & 0 deletions CPP/Tests/TestSimplifyPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "clipper2/clipper.h"
#include <gtest/gtest.h>

using namespace Clipper2Lib;

TEST(Clipper2Tests, TestSimplifyPath) {

Path64 input1 = MakePath({
0,0, 1,1, 0,20, 0,21, 1,40, 0,41, 0,60, 0,61, 0,80, 1,81, 0,100
});
Path64 output1 = SimplifyPath(input1, 2, false);
EXPECT_EQ(Length(output1), 100);
EXPECT_EQ(output1.size(), 2);
}

0 comments on commit 7a481e8

Please sign in to comment.