From 8d9aebb83ffbeb52e56d6224a2999550715744b2 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 6 Mar 2026 12:51:16 -0500 Subject: [PATCH] Move math utilities to OpenNest.Math namespace Co-Authored-By: Claude Opus 4.6 --- OpenNest.Core/Box.cs | 4 ++- OpenNest.Core/CNC/Program.cs | 5 ++-- OpenNest.Core/CNC/SubProgramCall.cs | 4 ++- OpenNest.Core/ConvertProgram.cs | 3 ++- OpenNest.Core/Geometry/Arc.cs | 23 +++++++++-------- OpenNest.Core/Geometry/Circle.cs | 15 +++++------ OpenNest.Core/Geometry/Entity.cs | 1 + OpenNest.Core/Geometry/Line.cs | 11 ++++---- OpenNest.Core/Geometry/Polygon.cs | 2 +- OpenNest.Core/Helper.cs | 15 +++++------ OpenNest.Core/{ => Math}/Angle.cs | 10 ++++---- OpenNest.Core/{ => Math}/EvenOdd.cs | 2 +- OpenNest.Core/{ => Math}/Generic.cs | 2 +- OpenNest.Core/{ => Math}/Tolerance.cs | 4 +-- OpenNest.Core/{ => Math}/Trigonometry.cs | 8 +++--- OpenNest.Core/NestConstraints.cs | 3 ++- OpenNest.Core/OpenNest.Core.csproj | 12 ++++----- OpenNest.Core/Plate.cs | 23 +++++++++-------- OpenNest.Core/Size.cs | 2 +- OpenNest.Core/Vector.cs | 17 +++++++------ OpenNest.Engine/BestCombination.cs | 9 ++++--- OpenNest.Engine/CirclePacking/FillEndEven.cs | 3 ++- OpenNest.Engine/CirclePacking/FillEndOdd.cs | 5 ++-- OpenNest.Engine/NestEngine.cs | 3 ++- .../RectanglePacking/FillBestFit.cs | 9 ++++--- .../RectanglePacking/FillNoRotation.cs | 17 +++++++------ .../RectanglePacking/FillSameRotation.cs | 3 ++- OpenNest.Engine/RectanglePacking/Item.cs | 1 + .../RectanglePacking/PackBottomLeft.cs | 1 + OpenNest/Controls/DrawingListBox.cs | 4 +-- OpenNest/Controls/EntityView.cs | 7 +++--- OpenNest/Controls/LayoutViewGL.cs | 25 ++++++++++--------- OpenNest/Controls/PlateView.cs | 7 +++--- OpenNest/Forms/EditNestForm.cs | 5 ++-- OpenNest/Forms/TimingForm.cs | 4 +-- OpenNest/GraphicsHelper.cs | 7 +++--- OpenNest/IO/DxfExporter.cs | 9 ++++--- OpenNest/IO/Extensions.cs | 1 + OpenNest/IO/NestReader.cs | 1 + OpenNest/IO/NestWriter.cs | 25 ++++++++++--------- 40 files changed, 172 insertions(+), 140 deletions(-) rename OpenNest.Core/{ => Math}/Angle.cs (92%) rename OpenNest.Core/{ => Math}/EvenOdd.cs (90%) rename OpenNest.Core/{ => Math}/Generic.cs (86%) rename OpenNest.Core/{ => Math}/Tolerance.cs (73%) rename OpenNest.Core/{ => Math}/Trigonometry.cs (78%) diff --git a/OpenNest.Core/Box.cs b/OpenNest.Core/Box.cs index 12710de..58d5418 100644 --- a/OpenNest.Core/Box.cs +++ b/OpenNest.Core/Box.cs @@ -1,4 +1,6 @@ -namespace OpenNest +using OpenNest.Math; + +namespace OpenNest { public class Box { diff --git a/OpenNest.Core/CNC/Program.cs b/OpenNest.Core/CNC/Program.cs index 008367b..447a2cd 100644 --- a/OpenNest.Core/CNC/Program.cs +++ b/OpenNest.Core/CNC/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using OpenNest.Geometry; +using OpenNest.Math; namespace OpenNest.CNC { @@ -398,10 +399,10 @@ namespace OpenNest.CNC if (Angle.IsBetweenRad(Angle.HalfPI, startAngle, endAngle)) maxY1 = centerpt.Y + radius; - if (Angle.IsBetweenRad(Math.PI, startAngle, endAngle)) + if (Angle.IsBetweenRad(System.Math.PI, startAngle, endAngle)) minX1 = centerpt.X - radius; - const double oneHalfPI = Math.PI * 1.5; + const double oneHalfPI = System.Math.PI * 1.5; if (Angle.IsBetweenRad(oneHalfPI, startAngle, endAngle)) minY1 = centerpt.Y - radius; diff --git a/OpenNest.Core/CNC/SubProgramCall.cs b/OpenNest.Core/CNC/SubProgramCall.cs index 9988242..07246f1 100644 --- a/OpenNest.Core/CNC/SubProgramCall.cs +++ b/OpenNest.Core/CNC/SubProgramCall.cs @@ -1,4 +1,6 @@ -namespace OpenNest.CNC +using OpenNest.Math; + +namespace OpenNest.CNC { public class SubProgramCall : ICode { diff --git a/OpenNest.Core/ConvertProgram.cs b/OpenNest.Core/ConvertProgram.cs index 2562f73..2f1312f 100644 --- a/OpenNest.Core/ConvertProgram.cs +++ b/OpenNest.Core/ConvertProgram.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using OpenNest.CNC; using OpenNest.Geometry; +using OpenNest.Math; namespace OpenNest { @@ -99,7 +100,7 @@ namespace OpenNest var dx = endpt.X - center.X; var dy = endpt.Y - center.Y; - var radius = Math.Sqrt(dx * dx + dy * dy); + var radius = System.Math.Sqrt(dx * dx + dy * dy); var layer = ConvertLayer(arcMove.Layer); if (startAngle.IsEqualTo(endAngle)) diff --git a/OpenNest.Core/Geometry/Arc.cs b/OpenNest.Core/Geometry/Arc.cs index 31b0cb2..faaa91e 100644 --- a/OpenNest.Core/Geometry/Arc.cs +++ b/OpenNest.Core/Geometry/Arc.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using OpenNest.Math; namespace OpenNest.Geometry { @@ -139,8 +140,8 @@ namespace OpenNest.Geometry public Vector StartPoint() { return new Vector( - Center.X + Radius * Math.Cos(StartAngle), - Center.Y + Radius * Math.Sin(StartAngle)); + Center.X + Radius * System.Math.Cos(StartAngle), + Center.Y + Radius * System.Math.Sin(StartAngle)); } /// @@ -150,8 +151,8 @@ namespace OpenNest.Geometry public Vector EndPoint() { return new Vector( - Center.X + Radius * Math.Cos(EndAngle), - Center.Y + Radius * Math.Sin(EndAngle)); + Center.X + Radius * System.Math.Cos(EndAngle), + Center.Y + Radius * System.Math.Sin(EndAngle)); } /// @@ -201,8 +202,8 @@ namespace OpenNest.Geometry var angle = stepAngle * i + StartAngle; points.Add(new Vector( - Math.Cos(angle) * Radius + Center.X, - Math.Sin(angle) * Radius + Center.Y)); + System.Math.Cos(angle) * Radius + Center.X, + System.Math.Sin(angle) * Radius + Center.Y)); } return points; @@ -213,7 +214,7 @@ namespace OpenNest.Geometry /// public override double Length { - get { return Diameter * Math.PI * SweepAngle() / Angle.TwoPI; } + get { return Diameter * System.Math.PI * SweepAngle() / Angle.TwoPI; } } /// @@ -356,10 +357,10 @@ namespace OpenNest.Geometry if (Angle.IsBetweenRad(Angle.HalfPI, angle1, angle2)) maxY = Center.Y + Radius; - if (Angle.IsBetweenRad(Math.PI, angle1, angle2)) + if (Angle.IsBetweenRad(System.Math.PI, angle1, angle2)) minX = Center.X - Radius; - const double oneHalfPI = Math.PI * 1.5; + const double oneHalfPI = System.Math.PI * 1.5; if (Angle.IsBetweenRad(oneHalfPI, angle1, angle2)) minY = Center.Y - Radius; @@ -405,8 +406,8 @@ namespace OpenNest.Geometry if (Angle.IsBetweenRad(angle, StartAngle, EndAngle, IsReversed)) { return new Vector( - Math.Cos(angle) * Radius + Center.X, - Math.Sin(angle) * Radius + Center.Y); + System.Math.Cos(angle) * Radius + Center.X, + System.Math.Sin(angle) * Radius + Center.Y); } else { diff --git a/OpenNest.Core/Geometry/Circle.cs b/OpenNest.Core/Geometry/Circle.cs index 1ee74de..fdada85 100644 --- a/OpenNest.Core/Geometry/Circle.cs +++ b/OpenNest.Core/Geometry/Circle.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using OpenNest.Math; namespace OpenNest.Geometry { @@ -84,7 +85,7 @@ namespace OpenNest.Geometry /// public double Area() { - return Math.PI * Radius * Radius; + return System.Math.PI * Radius * Radius; } /// @@ -93,7 +94,7 @@ namespace OpenNest.Geometry /// public double Circumference() { - return Math.PI * Diameter; + return System.Math.PI * Diameter; } /// @@ -131,8 +132,8 @@ namespace OpenNest.Geometry var angle = stepAngle * i; points.Add(new Vector( - Math.Cos(angle) * Radius + Center.X, - Math.Sin(angle) * Radius + Center.Y)); + System.Math.Cos(angle) * Radius + Center.X, + System.Math.Sin(angle) * Radius + Center.Y)); } return points; @@ -290,8 +291,8 @@ namespace OpenNest.Geometry var angle = Center.AngleTo(pt); return new Vector( - Math.Cos(angle) * Radius + Center.X, - Math.Sin(angle) * Radius + Center.Y); + System.Math.Cos(angle) * Radius + Center.X, + System.Math.Sin(angle) * Radius + Center.Y); } /// @@ -324,7 +325,7 @@ namespace OpenNest.Geometry public override bool Intersects(Circle circle) { var dist = Center.DistanceTo(circle.Center); - return (dist < (Radius + circle.Radius) && dist > Math.Abs(Radius - circle.Radius)); + return (dist < (Radius + circle.Radius) && dist > System.Math.Abs(Radius - circle.Radius)); } /// diff --git a/OpenNest.Core/Geometry/Entity.cs b/OpenNest.Core/Geometry/Entity.cs index 20aef49..f3dc781 100644 --- a/OpenNest.Core/Geometry/Entity.cs +++ b/OpenNest.Core/Geometry/Entity.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using OpenNest.Math; namespace OpenNest.Geometry { diff --git a/OpenNest.Core/Geometry/Line.cs b/OpenNest.Core/Geometry/Line.cs index e411cea..ba1300e 100644 --- a/OpenNest.Core/Geometry/Line.cs +++ b/OpenNest.Core/Geometry/Line.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using OpenNest.Math; namespace OpenNest.Geometry { @@ -219,7 +220,7 @@ namespace OpenNest.Geometry { var m1 = Slope(); var m2 = line.Slope(); - return Math.Atan(Math.Abs((m2 - m1) / (1 + m2 * m1))); + return System.Math.Atan(System.Math.Abs((m2 - m1) / (1 + m2 * m1))); } /// @@ -252,7 +253,7 @@ namespace OpenNest.Geometry { var x = EndPoint.X - StartPoint.X; var y = EndPoint.Y - StartPoint.Y; - return Math.Sqrt(x * x + y * y); + return System.Math.Sqrt(x * x + y * y); } } @@ -391,10 +392,10 @@ namespace OpenNest.Geometry public override Entity OffsetEntity(double distance, OffsetSide side) { - var angle = OpenNest.Angle.NormalizeRad(Angle() + OpenNest.Angle.HalfPI); + var angle = OpenNest.Math.Angle.NormalizeRad(Angle() + OpenNest.Math.Angle.HalfPI); - var x = Math.Cos(angle) * distance; - var y = Math.Sin(angle) * distance; + var x = System.Math.Cos(angle) * distance; + var y = System.Math.Sin(angle) * distance; var pt = new Vector(x, y); diff --git a/OpenNest.Core/Geometry/Polygon.cs b/OpenNest.Core/Geometry/Polygon.cs index 6eec98f..662ade4 100644 --- a/OpenNest.Core/Geometry/Polygon.cs +++ b/OpenNest.Core/Geometry/Polygon.cs @@ -73,7 +73,7 @@ namespace OpenNest.Geometry if (Vertices.Count < 3) return 0.0; - return Math.Abs(CalculateArea()); + return System.Math.Abs(CalculateArea()); } /// diff --git a/OpenNest.Core/Helper.cs b/OpenNest.Core/Helper.cs index 61b43d5..bd891a5 100644 --- a/OpenNest.Core/Helper.cs +++ b/OpenNest.Core/Helper.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using OpenNest.Geometry; +using OpenNest.Math; namespace OpenNest { @@ -17,7 +18,7 @@ namespace OpenNest /// public static double RoundDownToNearest(double num, double factor) { - return factor.IsEqualTo(0) ? num : Math.Floor(num / factor) * factor; + return factor.IsEqualTo(0) ? num : System.Math.Floor(num / factor) * factor; } /// @@ -28,7 +29,7 @@ namespace OpenNest /// public static double RoundUpToNearest(double num, double factor) { - return factor.IsEqualTo(0) ? num : Math.Ceiling(num / factor) * factor; + return factor.IsEqualTo(0) ? num : System.Math.Ceiling(num / factor) * factor; } /// @@ -39,7 +40,7 @@ namespace OpenNest /// public static double RoundToNearest(double num, double factor) { - return factor.IsEqualTo(0) ? num : Math.Round(num / factor) * factor; + return factor.IsEqualTo(0) ? num : System.Math.Round(num / factor) * factor; } public static void Optimize(IList arcs) @@ -483,7 +484,7 @@ namespace OpenNest } // check if one circle contains the other - if (distance < Math.Abs(circle1.Radius - circle2.Radius)) + if (distance < System.Math.Abs(circle1.Radius - circle2.Radius)) { pts = new List(); return false; @@ -491,7 +492,7 @@ namespace OpenNest var d = circle2.Center - circle1.Center; var a = (circle1.Radius * circle1.Radius - circle2.Radius * circle2.Radius + distance * distance) / (2.0 * distance); - var h = Math.Sqrt(circle1.Radius * circle1.Radius - a * a); + var h = System.Math.Sqrt(circle1.Radius * circle1.Radius - a * a); var pt = new Vector( circle1.Center.X + (a * d.X) / distance, @@ -541,13 +542,13 @@ namespace OpenNest return true; } - t = (-b + Math.Sqrt(det)) / (2 * a); + t = (-b + System.Math.Sqrt(det)) / (2 * a); var pt2 = new Vector(line.StartPoint.X + t * d1.X, line.StartPoint.Y + t * d1.Y); if (line.BoundingBox.Contains(pt2)) pts.Add(pt2); - t = (-b - Math.Sqrt(det)) / (2 * a); + t = (-b - System.Math.Sqrt(det)) / (2 * a); var pt3 = new Vector(line.StartPoint.X + t * d1.X, line.StartPoint.Y + t * d1.Y); if (line.BoundingBox.Contains(pt3)) diff --git a/OpenNest.Core/Angle.cs b/OpenNest.Core/Math/Angle.cs similarity index 92% rename from OpenNest.Core/Angle.cs rename to OpenNest.Core/Math/Angle.cs index fd70723..d052660 100644 --- a/OpenNest.Core/Angle.cs +++ b/OpenNest.Core/Math/Angle.cs @@ -1,28 +1,28 @@ using System; -namespace OpenNest +namespace OpenNest.Math { public static class Angle { /// /// Number of radians equal to 1 degree. /// - public const double RadPerDeg = Math.PI / 180.0; + public const double RadPerDeg = System.Math.PI / 180.0; /// /// Number of degrees equal to 1 radian. /// - public const double DegPerRad = 180.0 / Math.PI; + public const double DegPerRad = 180.0 / System.Math.PI; /// /// Half of PI. /// - public const double HalfPI = Math.PI * 0.5; + public const double HalfPI = System.Math.PI * 0.5; /// /// 2 x PI /// - public const double TwoPI = Math.PI * 2.0; + public const double TwoPI = System.Math.PI * 2.0; /// /// Converts radians to degrees. diff --git a/OpenNest.Core/EvenOdd.cs b/OpenNest.Core/Math/EvenOdd.cs similarity index 90% rename from OpenNest.Core/EvenOdd.cs rename to OpenNest.Core/Math/EvenOdd.cs index 247c043..682c475 100644 --- a/OpenNest.Core/EvenOdd.cs +++ b/OpenNest.Core/Math/EvenOdd.cs @@ -1,4 +1,4 @@ -namespace OpenNest +namespace OpenNest.Math { public static class EvenOdd { diff --git a/OpenNest.Core/Generic.cs b/OpenNest.Core/Math/Generic.cs similarity index 86% rename from OpenNest.Core/Generic.cs rename to OpenNest.Core/Math/Generic.cs index 86f0301..f63001e 100644 --- a/OpenNest.Core/Generic.cs +++ b/OpenNest.Core/Math/Generic.cs @@ -1,4 +1,4 @@ -namespace OpenNest +namespace OpenNest.Math { public static class Generic { diff --git a/OpenNest.Core/Tolerance.cs b/OpenNest.Core/Math/Tolerance.cs similarity index 73% rename from OpenNest.Core/Tolerance.cs rename to OpenNest.Core/Math/Tolerance.cs index 15924b0..eeb12b8 100644 --- a/OpenNest.Core/Tolerance.cs +++ b/OpenNest.Core/Math/Tolerance.cs @@ -1,6 +1,6 @@ using System; -namespace OpenNest +namespace OpenNest.Math { public static class Tolerance { @@ -8,7 +8,7 @@ namespace OpenNest public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon) { - return Math.Abs(b - a) <= tolerance; + return System.Math.Abs(b - a) <= tolerance; } } } diff --git a/OpenNest.Core/Trigonometry.cs b/OpenNest.Core/Math/Trigonometry.cs similarity index 78% rename from OpenNest.Core/Trigonometry.cs rename to OpenNest.Core/Math/Trigonometry.cs index b3c3778..4349f5f 100644 --- a/OpenNest.Core/Trigonometry.cs +++ b/OpenNest.Core/Math/Trigonometry.cs @@ -1,6 +1,6 @@ using System; -namespace OpenNest +namespace OpenNest.Math { public static class Trigonometry { @@ -12,7 +12,7 @@ namespace OpenNest /// public static double Base(double height, double hypotenuse) { - return Math.Sqrt(hypotenuse * hypotenuse - height * height); + return System.Math.Sqrt(hypotenuse * hypotenuse - height * height); } /// @@ -23,7 +23,7 @@ namespace OpenNest /// public static double Height(double bottom, double hypotenuse) { - return Math.Sqrt(hypotenuse * hypotenuse - bottom * bottom); + return System.Math.Sqrt(hypotenuse * hypotenuse - bottom * bottom); } /// @@ -34,7 +34,7 @@ namespace OpenNest /// public static double Hypotenuse(double height, double bottom) { - return Math.Sqrt(height * height + bottom * bottom); + return System.Math.Sqrt(height * height + bottom * bottom); } } } diff --git a/OpenNest.Core/NestConstraints.cs b/OpenNest.Core/NestConstraints.cs index f35e468..89827ed 100644 --- a/OpenNest.Core/NestConstraints.cs +++ b/OpenNest.Core/NestConstraints.cs @@ -1,4 +1,5 @@ using System; +using OpenNest.Math; namespace OpenNest { @@ -37,7 +38,7 @@ namespace OpenNest if (diff.IsEqualTo(Angle.TwoPI)) return false; - if ((diff > Math.PI || diff.IsEqualTo(Math.PI)) && Allow180Equivalent) + if ((diff > System.Math.PI || diff.IsEqualTo(System.Math.PI)) && Allow180Equivalent) return false; return true; diff --git a/OpenNest.Core/OpenNest.Core.csproj b/OpenNest.Core/OpenNest.Core.csproj index 18bae4b..bd2d247 100644 --- a/OpenNest.Core/OpenNest.Core.csproj +++ b/OpenNest.Core/OpenNest.Core.csproj @@ -39,7 +39,7 @@ - + @@ -47,7 +47,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -77,7 +77,7 @@ - + @@ -93,11 +93,11 @@ - + - +