Skip to content

Commit

Permalink
Minor tweaks (#801)
Browse files Browse the repository at this point in the history
* Floating point comparison

* Clean-up

The variables are initialized in the constructor, and this using line seems to be unused.

* Update Clipper.RectClip.cs

Variable initialized in constructor.

* This looked to be unnecessary

* group is not used, remove unused using statement.

min_area also not used.
_solutionTree initialized in constructor.

* Null check corrections

Assuming that the user can supply nulls as input. This addresses parts of #802

One remaining issue is that there is a null check against a value returned from PathFromStr(), but that method always returns a non-null value, so the check would appear to be meaningless.

* Clean up null checks

These seem to be redundant due to outer checks.

* More redundant null checks

Also remove one unused variable (result)

* Remove redundant base constructors

Remove redundant .ToString()

* Readability change

The long while statement made this confusing to understand. Use braces to make the end of the statement and the action more apparent.
  • Loading branch information
philstopford authored Apr 2, 2024
1 parent 7c91a18 commit ce4c338
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
7 changes: 1 addition & 6 deletions CSharp/Clipper2Lib/Clipper.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Clipper2Lib
{
Expand Down Expand Up @@ -474,7 +473,6 @@ public readonly PathD AsPath()

public class Path64 : List<Point64>
{
private Path64() : base() { }
public Path64(int capacity = 0) : base(capacity) { }
public Path64(IEnumerable<Point64> path) : base(path) { }
public override string ToString()
Expand All @@ -488,21 +486,19 @@ public override string ToString()

public class Paths64 : List<Path64>
{
private Paths64() : base() { }
public Paths64(int capacity = 0) : base(capacity) { }
public Paths64(IEnumerable<Path64> paths) : base(paths) { }
public override string ToString()
{
string s = "";
foreach (Path64 p in this)
s = s + p.ToString() + "\n";
s = s + p + "\n";
return s;
}
}

public class PathD : List<PointD>
{
private PathD() : base() { }
public PathD(int capacity = 0) : base(capacity) { }
public PathD(IEnumerable<PointD> path) : base(path) { }
public string ToString(int precision = 2)
Expand All @@ -516,7 +512,6 @@ public string ToString(int precision = 2)

public class PathsD : List<PathD>
{
private PathsD() : base() { }
public PathsD(int capacity = 0) : base(capacity) { }
public PathsD(IEnumerable<PathD> paths) : base(paths) { }
public string ToString(int precision = 2)
Expand Down
14 changes: 6 additions & 8 deletions CSharp/Clipper2Lib/Clipper.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;

namespace Clipper2Lib
Expand Down Expand Up @@ -147,10 +146,10 @@ internal class OutRec
public Active? backEdge;
public OutPt? pts;
public PolyPathBase? polypath;
public Rect64 bounds = new Rect64();
public Rect64 bounds;
public Path64 path = new Path64();
public bool isOpen;
public List<int>? splits = null;
public List<int>? splits;
public OutRec? recursiveSplit;
};

Expand Down Expand Up @@ -2158,7 +2157,7 @@ private void DoHorizontal(Active horz)
if (ae.vertexTop == vertex_max)
{
// do this first!!
if (IsHotEdge(horz) && IsJoined(ae!)) Split(ae, ae.top);
if (IsHotEdge(horz) && IsJoined(ae)) Split(ae, ae.top);

if (IsHotEdge(horz))
{
Expand Down Expand Up @@ -2444,7 +2443,7 @@ private static void FixOutRecPts(OutRec outrec)
OutPt op = outrec.pts!;
do
{
op!.outrec = outrec;
op.outrec = outrec;
op = op.next!;
} while (op != outrec.pts);
}
Expand Down Expand Up @@ -2715,7 +2714,7 @@ private void ProcessHorzJoins()
OutRec or2 = GetRealOutRec(j.op2!.outrec)!;

OutPt op1b = j.op1.next!;
OutPt op2b = j.op2.prev!;
OutPt op2b = j.op2.prev;
j.op1.next = j.op2;
j.op2.prev = j.op1;
op1b.prev = op2b;
Expand Down Expand Up @@ -2851,7 +2850,6 @@ private void DoSplitOp(OutRec outrec, OutPt splitOp)
OutPt prevOp = splitOp.prev;
OutPt nextNextOp = splitOp.next!.next!;
outrec.pts = prevOp;
OutPt result = prevOp;

InternalClipper.GetSegmentIntersectPt(
prevOp.pt, splitOp.pt, splitOp.next.pt, nextNextOp.pt, out Point64 ip);
Expand Down Expand Up @@ -3050,7 +3048,7 @@ private bool CheckSplitOwner(OutRec outrec, List<int>? splits)
OutRec? split = GetRealOutRec(_outrecList[i]);
if (split == null || split == outrec || split.recursiveSplit == outrec) continue;
split.recursiveSplit = outrec; //#599
if (split!.splits != null && CheckSplitOwner(outrec, split.splits)) return true;
if (split.splits != null && CheckSplitOwner(outrec, split.splits)) return true;
if (IsValidOwner(outrec, split) &&
CheckBounds(split) &&
split.bounds.Contains(outrec.bounds) &&
Expand Down
10 changes: 4 additions & 6 deletions CSharp/Clipper2Lib/Clipper.Offset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;

namespace Clipper2Lib
Expand Down Expand Up @@ -74,7 +73,7 @@ public Group(Paths64 paths, JoinType joinType, EndType endType = EndType.Polygon
private Path64 pathOut = new Path64();
private readonly PathD _normals = new PathD();
private Paths64 _solution = new Paths64();
private PolyTree64? _solutionTree = null;
private PolyTree64? _solutionTree;

private double _groupDelta; //*0.5 for open paths; *-1.0 for negative areas
private double _delta;
Expand Down Expand Up @@ -454,7 +453,7 @@ private void DoSquare(Path64 path, int j, int k)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DoMiter(Group group, Path64 path, int j, int k, double cosA)
private void DoMiter(Path64 path, int j, int k, double cosA)
{
double q = _groupDelta / (cosA + 1);
#if USINGZ
Expand Down Expand Up @@ -558,12 +557,12 @@ private void OffsetPoint(Group group, Path64 path, int j, ref int k)
else if ((cosA > 0.999) && (_joinType != JoinType.Round))
{
// almost straight - less than 2.5 degree (#424, #482, #526 & #724)
DoMiter(group, path, j, k, cosA);
DoMiter(path, j, k, cosA);
}
else if (_joinType == JoinType.Miter)
{
// miter unless the angle is sufficiently acute to exceed ML
if (cosA > _mitLimSqr - 1) DoMiter(group, path, j, k, cosA);
if (cosA > _mitLimSqr - 1) DoMiter(path, j, k, cosA);
else DoSquare(path, j, k);
}
else if (_joinType == JoinType.Round)
Expand Down Expand Up @@ -689,7 +688,6 @@ private void DoGroupOffset(Group group)
_stepsPerRad = stepsPer360 / (2 * Math.PI);
}

double min_area = Math.PI * Clipper.Sqr(_groupDelta);
using List<Path64>.Enumerator pathIt = group.inPaths.GetEnumerator();
while (pathIt.MoveNext())
{
Expand Down
8 changes: 4 additions & 4 deletions CSharp/Clipper2Lib/Clipper.RectClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected enum Location
protected Rect64 pathBounds_;
protected List<OutPt2?> results_;
protected List<OutPt2?>[] edges_;
protected int currIdx_ = -1;
protected int currIdx_;
internal RectClip64(Rect64 rect)
{
currIdx_ = -1;
Expand Down Expand Up @@ -195,7 +195,7 @@ private static bool HasVertOverlap(Point64 top1, Point64 bottom1,
private static void AddToEdge(List<OutPt2?> edge, OutPt2 op)
{
if (op.edge != null) return;
op.edge = edge!;
op.edge = edge;
edge.Add(op);
}

Expand Down Expand Up @@ -931,7 +931,7 @@ private Path64 GetPath(OutPt2? op)
while (op2 != null && op2 != op)
{
if (InternalClipper.CrossProduct(
op2!.prev!.pt, op2.pt, op2!.next!.pt) == 0)
op2.prev!.pt, op2.pt, op2.next!.pt) == 0)
{
op = op2.prev;
op2 = UnlinkOp(op2);
Expand Down Expand Up @@ -995,7 +995,7 @@ private Path64 GetPath(OutPt2? op)
OutPt2 op2 = op.next!;
while (op2 != op)
{
result.Add(op2!.pt);
result.Add(op2.pt);
op2 = op2.next!;
}
return result;
Expand Down
10 changes: 6 additions & 4 deletions CSharp/Clipper2Lib/Clipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public static RectD GetBounds(PathD path)
if (pt.y < result.top) result.top = pt.y;
if (pt.y > result.bottom) result.bottom = pt.y;
}
return result.left == double.MaxValue ? new RectD() : result;
return Math.Abs(result.left - double.MaxValue) < InternalClipper.floatingPointTolerance ? new RectD() : result;
}

public static RectD GetBounds(PathsD paths)
Expand All @@ -603,7 +603,7 @@ public static RectD GetBounds(PathsD paths)
if (pt.y < result.top) result.top = pt.y;
if (pt.y > result.bottom) result.bottom = pt.y;
}
return result.left == double.MaxValue ? new RectD() : result;
return Math.Abs(result.left - double.MaxValue) < InternalClipper.floatingPointTolerance ? new RectD() : result;
}

public static Path64 MakePath(int[] arr)
Expand Down Expand Up @@ -1054,8 +1054,10 @@ public static Path64 TrimCollinear(Path64 path, bool isOpen = false)
else
{
while (result.Count > 2 && InternalClipper.CrossProduct(
result[result.Count - 1], result[result.Count - 2], result[0]) == 0)
result.RemoveAt(result.Count - 1);
result[result.Count - 1], result[result.Count - 2], result[0]) == 0)
{
result.RemoveAt(result.Count - 1);
}
if (result.Count < 3)
result.Clear();
}
Expand Down
12 changes: 6 additions & 6 deletions CSharp/Utils/ClipFileIO/Clipper.FileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Clipper2Lib

public static class ClipperFileIO
{
public static Paths64 PathFromStr(string s)
public static Paths64 PathFromStr(string? s)
{
if (s == null) return new Paths64();
Path64 p = new Path64();
Expand Down Expand Up @@ -71,7 +71,7 @@ public static Paths64 PathFromStr(string s)
//------------------------------------------------------------------------------

public static bool LoadTestNum(string filename, int num,
Paths64 subj, Paths64 subj_open, Paths64 clip,
Paths64? subj, Paths64? subj_open, Paths64? clip,
out ClipType ct, out FillRule fillRule, out long area, out int count, out string caption)
{
if (subj == null) subj = new Paths64(); else subj.Clear();
Expand All @@ -96,7 +96,7 @@ public static bool LoadTestNum(string filename, int num,
}
while (true)
{
string s = reader.ReadLine();
string? s = reader.ReadLine();
if (s == null) break;

if (s.IndexOf("CAPTION: ", StringComparison.Ordinal) == 0)
Expand Down Expand Up @@ -150,7 +150,7 @@ public static bool LoadTestNum(string filename, int num,
{
s = reader.ReadLine();
if (s == null) break;
Paths64 paths = PathFromStr(s); //0 or 1 path
Paths64? paths = PathFromStr(s); //0 or 1 path
if (paths == null || paths.Count == 0)
{
if (GetIdx == 3) return result;
Expand All @@ -168,8 +168,8 @@ public static bool LoadTestNum(string filename, int num,
}
//-----------------------------------------------------------------------

public static void SaveClippingOp(string filename, Paths64 subj,
Paths64 subj_open, Paths64 clip, ClipType ct, FillRule fillRule, bool append)
public static void SaveClippingOp(string filename, Paths64? subj,
Paths64? subj_open, Paths64? clip, ClipType ct, FillRule fillRule, bool append)
{
StreamWriter writer;
try
Expand Down

0 comments on commit ce4c338

Please sign in to comment.