Skip to content

Commit

Permalink
Further fixes to Polytree nesting (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusJohnson committed Jul 19, 2023
1 parent 562ef2d commit 866e402
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 26 deletions.
5 changes: 3 additions & 2 deletions CPP/Clipper2Lib/src/clipper.engine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 19 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -2842,7 +2842,8 @@ namespace Clipper2Lib {
{
for (auto split : *splits)
{
if(split == outrec || split == outrec->owner) continue;
split = GetRealOutRec(split);
if(!split || split == outrec || split == outrec->owner) continue;
else if (split->splits && CheckSplitOwner(outrec, split->splits)) return true;
else if (CheckBounds(split) && split->bounds.Contains(outrec->bounds) &&
Path1InsidePath2(outrec->pts, split->pts))
Expand Down
44 changes: 35 additions & 9 deletions CPP/Examples/VariableOffset/VariableOffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ void test1() {
Paths64 solution;
co.Execute(1.0, solution);

std::string filename = "test1.svg";
SvgWriter svg;
SvgAddOpenSubject(svg, subject, FillRule::NonZero);
SvgAddSolution(svg, solution, FillRule::NonZero, false);
SvgSaveToFile(svg, "c:\\temp\\tmp1.svg", 400, 400);
System("c:\\temp\\tmp1.svg");
SvgSaveToFile(svg, filename, 400, 400);
System(filename);
}

void test2() {
Expand All @@ -68,11 +69,12 @@ void test2() {
Paths64 solution;
co.Execute(1.0, solution);

std::string filename = "test2.svg";
SvgWriter svg;
SvgAddOpenSubject(svg, subject, FillRule::NonZero);
SvgAddSolution(svg, solution, FillRule::NonZero, false);
SvgSaveToFile(svg, "c:\\temp\\tmp2.svg", 400, 400);
System("c:\\temp\\tmp2.svg");
SvgSaveToFile(svg, filename, 400, 400);
System(filename);
}

void test3() {
Expand All @@ -95,11 +97,12 @@ void test3() {
Paths64 solution;
co.Execute(1.0, solution);

std::string filename = "test3.svg";
SvgWriter svg;
SvgAddSubject(svg, subject, FillRule::NonZero);
SvgAddSolution(svg, solution, FillRule::NonZero, false);
SvgSaveToFile(svg, "c:\\temp\\tmp3.svg", 400, 400);
System("c:\\temp\\tmp3.svg");
SvgSaveToFile(svg, filename, 400, 400);
System(filename);
}

void test4() {
Expand All @@ -118,14 +121,36 @@ void test4() {
//double vertex_angle = std::atan2(vertex_sin_a, vertex_cos_a);
//double edge_angle = std::atan2(path_norms[curr_idx].y, path_norms[curr_idx].x);
double sin_edge = path_norms[curr_idx].y;
return Sqr(sin_edge) * 3 * scale; }
return Sqr(sin_edge) * 3 * scale; }
, solution);

std::string filename = "test4.svg";
SvgWriter svg;
SvgAddOpenSubject(svg, subject, FillRule::NonZero);
SvgAddSolution(svg, solution, FillRule::NonZero, false);
SvgSaveToFile(svg, "c:\\temp\\tmp4.svg", 400, 400);
System("c:\\temp\\tmp4.svg");
SvgSaveToFile(svg, filename, 400, 400);
System(filename);
}

void test5() {

Paths64 solution;
Paths64 subject = { MakePath({0,0, 20,0, 40,0, 60,0, 80,0, 100,0}) };

ClipperOffset co;
co.AddPaths(subject, JoinType::Round, EndType::Butt);
co.Execute(
[](const Path64& path,
const PathD& path_norms, size_t curr_idx, size_t prev_idx) {
return double(curr_idx * curr_idx + 10); }
, solution);

SvgWriter svg;
std::string filename = "test5.svg";
SvgAddOpenSubject(svg, subject, FillRule::NonZero);
SvgAddSolution(svg, solution, FillRule::NonZero, false);
SvgSaveToFile(svg, filename, 400, 400);
System(filename);
}


Expand All @@ -135,5 +160,6 @@ int main() {
test2();
test3();
test4();
test5();
return 0;
}
39 changes: 30 additions & 9 deletions CSharp/Clipper2Lib.Examples/InflateDemo/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 16 September 2022 *
* Date : 19 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2022 *
* License : http://www.boost.org/LICENSE_1_0.txt *
Expand All @@ -20,13 +20,14 @@ public static void Main()
{
DoSimpleShapes();
DoRabbit();
DoVariableOffset();
}

public static void DoSimpleShapes()
{
//triangle offset - with large miter
Paths64 p = new() { Clipper.MakePath(new int[] { 30, 150, 60, 350, 0, 350 }) };
Paths64 pp = new ();
Paths64 pp = new();
pp.AddRange(p);

for (int i = 0; i < 5; ++i)
Expand All @@ -42,18 +43,19 @@ public static void DoSimpleShapes()
pp.AddRange(p);
//nb: using the ClipperOffest class directly here to control
//different join types within the same offset operation
ClipperOffset co = new ();
ClipperOffset co = new();
co.AddPaths(p, JoinType.Square, EndType.Joined);
p = Clipper.TranslatePaths(p, 120, 100);
pp.AddRange(p);
co.AddPaths(p, JoinType.Round, EndType.Joined);
co.Execute(20, p);
pp.AddRange(p);

SvgWriter svg = new ();
string filename = "../../../inflate.svg";
SvgWriter svg = new();
SvgUtils.AddSolution(svg, pp, false);
SvgUtils.SaveToFile(svg, "../../../inflate.svg", FillRule.EvenOdd, 800, 600, 20);
ClipperFileIO.OpenFileWithDefaultApp("../../../inflate.svg");
SvgUtils.SaveToFile(svg, filename, FillRule.EvenOdd, 800, 600, 20);
ClipperFileIO.OpenFileWithDefaultApp(filename);
}

public static void DoRabbit()
Expand All @@ -71,10 +73,11 @@ public static void DoRabbit()
solution.AddRange(pd);
}

string filename = "../../../rabbit.svg";
SvgWriter svg = new ();
SvgUtils.AddSolution(svg, solution, false);
SvgUtils.SaveToFile(svg, "../../../rabbit2.svg", FillRule.EvenOdd, 450, 720, 10);
ClipperFileIO.OpenFileWithDefaultApp("../../../rabbit2.svg");
SvgUtils.SaveToFile(svg, filename, FillRule.EvenOdd, 450, 720, 10);
ClipperFileIO.OpenFileWithDefaultApp(filename);
}

public static PathsD LoadPathsFromResource(string resourceName)
Expand All @@ -99,7 +102,25 @@ public static PathsD LoadPathsFromResource(string resourceName)
}
return result;
}


public static void DoVariableOffset()
{
Paths64 p = new() { Clipper.MakePath(new int[] { 0,50, 20,50, 40,50, 60,50, 80,50, 100,50 }) };
Paths64 solution = new();
ClipperOffset co = new();
co.AddPaths(p, JoinType.Square, EndType.Butt);
co.Execute(
delegate (Path64 path, PathD path_norms, int currPt, int prevPt)
{ return currPt* currPt + 10; } , solution);

string filename = "../../../variable_offset.svg";
SvgWriter svg = new();
SvgUtils.AddOpenSubject(svg, p);
SvgUtils.AddSolution(svg, solution, true);
SvgUtils.SaveToFile(svg, filename, FillRule.EvenOdd, 500, 500, 60);
ClipperFileIO.OpenFileWithDefaultApp(filename);
}

} //end Application

} //namespace
6 changes: 3 additions & 3 deletions CSharp/Clipper2Lib/Clipper.Engine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 19 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -3022,8 +3022,8 @@ private bool CheckSplitOwner(OutRec outrec, List<int>? splits)
{
foreach (int i in splits!)
{
OutRec? split = _outrecList[i];
if (split == outrec || split == outrec.owner) continue;
OutRec? split = GetRealOutRec(_outrecList[i]);
if (split == null || split == outrec || split == outrec.owner) continue;
if (split!.splits != null && CheckSplitOwner(outrec, split.splits)) return true;
if (CheckBounds(split) && split.bounds.Contains(outrec.bounds) &&
Path1InsidePath2(outrec.pts!, split.pts!))
Expand Down
7 changes: 4 additions & 3 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 : 16 July 2023 *
* Date : 19 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This is the main polygon clipping module *
Expand Down Expand Up @@ -3716,8 +3716,9 @@ function TClipperBase.CheckSplitOwner(outrec: POutRec; const splits: TOutRecArra
Result := false;
for i := 0 to High(splits) do
begin
split := splits[i];
if (split = outrec) or (split = outrec.owner) then Continue
split := GetRealOutRec(splits[i]);
if not assigned(split) or (split = outrec) or (split = outrec.owner) then
Continue
else if Assigned(split.splits) and
CheckSplitOwner(outrec, split.splits) then
begin
Expand Down

0 comments on commit 866e402

Please sign in to comment.