From ce4c338ba6a064e28198ed9f03442a1dc7cb638b Mon Sep 17 00:00:00 2001 From: philstopford Date: Tue, 2 Apr 2024 16:09:46 -0500 Subject: [PATCH] Minor tweaks (#801) * 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 https://github.com/AngusJohnson/Clipper2/issues/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. --- CSharp/Clipper2Lib/Clipper.Core.cs | 7 +------ CSharp/Clipper2Lib/Clipper.Engine.cs | 14 ++++++-------- CSharp/Clipper2Lib/Clipper.Offset.cs | 10 ++++------ CSharp/Clipper2Lib/Clipper.RectClip.cs | 8 ++++---- CSharp/Clipper2Lib/Clipper.cs | 10 ++++++---- CSharp/Utils/ClipFileIO/Clipper.FileIO.cs | 12 ++++++------ 6 files changed, 27 insertions(+), 34 deletions(-) diff --git a/CSharp/Clipper2Lib/Clipper.Core.cs b/CSharp/Clipper2Lib/Clipper.Core.cs index 55d0ae30..d0128ff0 100644 --- a/CSharp/Clipper2Lib/Clipper.Core.cs +++ b/CSharp/Clipper2Lib/Clipper.Core.cs @@ -11,7 +11,6 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; namespace Clipper2Lib { @@ -474,7 +473,6 @@ public readonly PathD AsPath() public class Path64 : List { - private Path64() : base() { } public Path64(int capacity = 0) : base(capacity) { } public Path64(IEnumerable path) : base(path) { } public override string ToString() @@ -488,21 +486,19 @@ public override string ToString() public class Paths64 : List { - private Paths64() : base() { } public Paths64(int capacity = 0) : base(capacity) { } public Paths64(IEnumerable 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 { - private PathD() : base() { } public PathD(int capacity = 0) : base(capacity) { } public PathD(IEnumerable path) : base(path) { } public string ToString(int precision = 2) @@ -516,7 +512,6 @@ public string ToString(int precision = 2) public class PathsD : List { - private PathsD() : base() { } public PathsD(int capacity = 0) : base(capacity) { } public PathsD(IEnumerable paths) : base(paths) { } public string ToString(int precision = 2) diff --git a/CSharp/Clipper2Lib/Clipper.Engine.cs b/CSharp/Clipper2Lib/Clipper.Engine.cs index d967a77f..d34b9332 100644 --- a/CSharp/Clipper2Lib/Clipper.Engine.cs +++ b/CSharp/Clipper2Lib/Clipper.Engine.cs @@ -13,7 +13,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Reflection.Emit; using System.Runtime.CompilerServices; namespace Clipper2Lib @@ -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? splits = null; + public List? splits; public OutRec? recursiveSplit; }; @@ -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)) { @@ -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); } @@ -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; @@ -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); @@ -3050,7 +3048,7 @@ private bool CheckSplitOwner(OutRec outrec, List? 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) && diff --git a/CSharp/Clipper2Lib/Clipper.Offset.cs b/CSharp/Clipper2Lib/Clipper.Offset.cs index 81ad74fd..9d6d4090 100644 --- a/CSharp/Clipper2Lib/Clipper.Offset.cs +++ b/CSharp/Clipper2Lib/Clipper.Offset.cs @@ -9,7 +9,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Runtime.CompilerServices; namespace Clipper2Lib @@ -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; @@ -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 @@ -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) @@ -689,7 +688,6 @@ private void DoGroupOffset(Group group) _stepsPerRad = stepsPer360 / (2 * Math.PI); } - double min_area = Math.PI * Clipper.Sqr(_groupDelta); using List.Enumerator pathIt = group.inPaths.GetEnumerator(); while (pathIt.MoveNext()) { diff --git a/CSharp/Clipper2Lib/Clipper.RectClip.cs b/CSharp/Clipper2Lib/Clipper.RectClip.cs index 70b34098..716ab079 100644 --- a/CSharp/Clipper2Lib/Clipper.RectClip.cs +++ b/CSharp/Clipper2Lib/Clipper.RectClip.cs @@ -41,7 +41,7 @@ protected enum Location protected Rect64 pathBounds_; protected List results_; protected List[] edges_; - protected int currIdx_ = -1; + protected int currIdx_; internal RectClip64(Rect64 rect) { currIdx_ = -1; @@ -195,7 +195,7 @@ private static bool HasVertOverlap(Point64 top1, Point64 bottom1, private static void AddToEdge(List edge, OutPt2 op) { if (op.edge != null) return; - op.edge = edge!; + op.edge = edge; edge.Add(op); } @@ -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); @@ -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; diff --git a/CSharp/Clipper2Lib/Clipper.cs b/CSharp/Clipper2Lib/Clipper.cs index 912ab461..152af315 100644 --- a/CSharp/Clipper2Lib/Clipper.cs +++ b/CSharp/Clipper2Lib/Clipper.cs @@ -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) @@ -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) @@ -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(); } diff --git a/CSharp/Utils/ClipFileIO/Clipper.FileIO.cs b/CSharp/Utils/ClipFileIO/Clipper.FileIO.cs index 2efad9c8..6c49c7a8 100644 --- a/CSharp/Utils/ClipFileIO/Clipper.FileIO.cs +++ b/CSharp/Utils/ClipFileIO/Clipper.FileIO.cs @@ -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(); @@ -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(); @@ -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) @@ -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; @@ -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