Skip to content

Commit

Permalink
Fixed a minor bug in RectClip (#597)
Browse files Browse the repository at this point in the history
Fixed a minor bug in merging solution polygons (#606)
  • Loading branch information
AngusJohnson committed Aug 6, 2023
1 parent 3867aab commit 6222af4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
4 changes: 1 addition & 3 deletions CPP/Clipper2Lib/src/clipper.engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,6 @@ namespace Clipper2Lib {
#endif
AddTrialHorzJoin(op);
}
OutRec* currHorzOutrec = horz.outrec;

while (true) // loop through consec. horizontal edges
{
Expand Down Expand Up @@ -2579,9 +2578,8 @@ namespace Clipper2Lib {
e = horz.prev_in_ael;
}

if (horz.outrec && horz.outrec != currHorzOutrec)
if (horz.outrec)
{
currHorzOutrec = horz.outrec;
//nb: The outrec containining the op returned by IntersectEdges
//above may no longer be associated with horzEdge.
AddTrialHorzJoin(GetLastOp(horz));
Expand Down
4 changes: 2 additions & 2 deletions CPP/Clipper2Lib/src/clipper.rectclip.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 6 August 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : FAST rectangular clipping *
Expand Down Expand Up @@ -474,7 +474,7 @@ namespace Clipper2Lib {
// intersect pt but we'll also need the first intersect pt (ip2)
loc = prev;
GetIntersection(rect_as_path_, prev_pt, path[i], loc, ip2);
if (crossing_prev != Location::Inside)
if (crossing_prev != Location::Inside && crossing_prev != loc) //579
AddCorner(crossing_prev, loc);

if (first_cross_ == Location::Inside)
Expand Down
11 changes: 11 additions & 0 deletions CPP/Tests/TestRectClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ TEST(Clipper2Tests, TestRectClip)
EXPECT_EQ(solBounds.Width(), rect.Width());
EXPECT_EQ(solBounds.Height(), rect.Height());

}

TEST(Clipper2Tests, TestRectClip2) //#597
{
Clipper2Lib::Rect64 rect(54690, 0, 65628, 6000);
Clipper2Lib::Paths64 subject {{{700000, 6000}, { 0, 6000 }, { 0, 5925 }, { 700000, 5925 }}};

Clipper2Lib::Paths64 solution = Clipper2Lib::RectClip(rect, subject);

//std::cout << solution << std::endl;
EXPECT_TRUE(solution.size() == 1 && solution[0].size() == 4);
}
8 changes: 2 additions & 6 deletions CSharp/Clipper2Lib/Clipper.Engine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 26 July 2023 *
* Date : 6 August 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -2173,7 +2173,6 @@ private void DoHorizontal(Active horz)
#endif
AddToHorzSegList(op);
}
OutRec? currOutrec = horz.outrec!;

for (; ; )
{
Expand Down Expand Up @@ -2248,11 +2247,8 @@ private void DoHorizontal(Active horz)
ae = horz.prevInAEL;
}

if (IsHotEdge(horz) && (horz.outrec != currOutrec))
{
currOutrec = horz.outrec;
if (IsHotEdge(horz))
AddToHorzSegList(GetLastOp(horz));
}

} // we've reached the end of this horizontal

Expand Down
4 changes: 2 additions & 2 deletions CSharp/Clipper2Lib/Clipper.RectClip.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 30 May 2023 *
* Date : 6 August 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : FAST rectangular clipping *
Expand Down Expand Up @@ -544,7 +544,7 @@ private void ExecuteInternal(Path64 path)
loc = prev;
GetIntersection(rectPath_,
prevPt, path[i], ref loc, out Point64 ip2);
if (prevCrossLoc != Location.inside)
if (prevCrossLoc != Location.inside && prevCrossLoc != loc) //#597
AddCorner(prevCrossLoc, loc);

if (firstCross == Location.inside)
Expand Down
7 changes: 2 additions & 5 deletions Delphi/Clipper2Lib/Clipper.Engine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(*******************************************************************************
* Author : Angus Johnson *
* Date : 26 July 2023 *
* Date : 6 August 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -3401,7 +3401,6 @@ procedure TClipperBase.DoHorizontal(horzEdge: PActive);
e: PActive;
pt: TPoint64;
op: POutPt;
currOr: POutRec;
isLeftToRight, horzIsOpen: Boolean;
begin
(*******************************************************************************
Expand Down Expand Up @@ -3443,7 +3442,6 @@ procedure TClipperBase.DoHorizontal(horzEdge: PActive);
{$ENDIF}
FHorzSegList.Add(op);
end;
currOr := horzEdge.outrec;

while true do // loop through consec. horizontal edges
begin
Expand Down Expand Up @@ -3520,9 +3518,8 @@ procedure TClipperBase.DoHorizontal(horzEdge: PActive);
horzEdge.currX := e.currX;
e := horzEdge.prevInAEL;
end;
if IsHotEdge(horzEdge) and (horzEdge.outrec <> currOr) then
if IsHotEdge(horzEdge) then
begin
currOr := horzEdge.outrec;
//nb: The outrec containining the op returned by IntersectEdges
//above may no longer be associated with horzEdge.
FHorzSegList.Add(GetLastOp(horzEdge));
Expand Down
4 changes: 2 additions & 2 deletions Delphi/Clipper2Lib/Clipper.RectClip.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(*******************************************************************************
* Author : Angus Johnson *
* Date : 30 May 2023 *
* Date : 6 August 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : FAST rectangular clipping *
Expand Down Expand Up @@ -700,7 +700,7 @@ procedure TRectClip64.ExecuteInternal(const path: TPath64);
// intersect pt but we'll also need the first intersect pt (ip2)
loc := prevLoc;
GetIntersection(fRectPath, prevPt, path[i], loc, ip2);
if (prevCrossLoc <> locInside) then
if (prevCrossLoc <> locInside) and (prevCrossLoc <> loc) then //#579
AddCorner(prevCrossLoc, loc);

if (firstCrossLoc = locInside) then
Expand Down

0 comments on commit 6222af4

Please sign in to comment.