Move math utilities to OpenNest.Math namespace

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 12:51:16 -05:00
parent 2210f60656
commit 8d9aebb83f
40 changed files with 172 additions and 140 deletions

View File

@@ -1,4 +1,6 @@
namespace OpenNest
using OpenNest.Math;
namespace OpenNest
{
public class Box
{

View File

@@ -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;

View File

@@ -1,4 +1,6 @@
namespace OpenNest.CNC
using OpenNest.Math;
namespace OpenNest.CNC
{
public class SubProgramCall : ICode
{

View File

@@ -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))

View File

@@ -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));
}
/// <summary>
@@ -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));
}
/// <summary>
@@ -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
/// </summary>
public override double Length
{
get { return Diameter * Math.PI * SweepAngle() / Angle.TwoPI; }
get { return Diameter * System.Math.PI * SweepAngle() / Angle.TwoPI; }
}
/// <summary>
@@ -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
{

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using OpenNest.Math;
namespace OpenNest.Geometry
{
@@ -84,7 +85,7 @@ namespace OpenNest.Geometry
/// <returns></returns>
public double Area()
{
return Math.PI * Radius * Radius;
return System.Math.PI * Radius * Radius;
}
/// <summary>
@@ -93,7 +94,7 @@ namespace OpenNest.Geometry
/// <returns></returns>
public double Circumference()
{
return Math.PI * Diameter;
return System.Math.PI * Diameter;
}
/// <summary>
@@ -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);
}
/// <summary>
@@ -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));
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using OpenNest.Math;
namespace OpenNest.Geometry
{

View File

@@ -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)));
}
/// <summary>
@@ -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);

View File

@@ -73,7 +73,7 @@ namespace OpenNest.Geometry
if (Vertices.Count < 3)
return 0.0;
return Math.Abs(CalculateArea());
return System.Math.Abs(CalculateArea());
}
/// <summary>

View File

@@ -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
/// <returns></returns>
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;
}
/// <summary>
@@ -28,7 +29,7 @@ namespace OpenNest
/// <returns></returns>
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;
}
/// <summary>
@@ -39,7 +40,7 @@ namespace OpenNest
/// <returns></returns>
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<Arc> 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<Vector>();
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))

View File

@@ -1,28 +1,28 @@
using System;
namespace OpenNest
namespace OpenNest.Math
{
public static class Angle
{
/// <summary>
/// Number of radians equal to 1 degree.
/// </summary>
public const double RadPerDeg = Math.PI / 180.0;
public const double RadPerDeg = System.Math.PI / 180.0;
/// <summary>
/// Number of degrees equal to 1 radian.
/// </summary>
public const double DegPerRad = 180.0 / Math.PI;
public const double DegPerRad = 180.0 / System.Math.PI;
/// <summary>
/// Half of PI.
/// </summary>
public const double HalfPI = Math.PI * 0.5;
public const double HalfPI = System.Math.PI * 0.5;
/// <summary>
/// 2 x PI
/// </summary>
public const double TwoPI = Math.PI * 2.0;
public const double TwoPI = System.Math.PI * 2.0;
/// <summary>
/// Converts radians to degrees.

View File

@@ -1,4 +1,4 @@
namespace OpenNest
namespace OpenNest.Math
{
public static class EvenOdd
{

View File

@@ -1,4 +1,4 @@
namespace OpenNest
namespace OpenNest.Math
{
public static class Generic
{

View File

@@ -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;
}
}
}

View File

@@ -1,6 +1,6 @@
using System;
namespace OpenNest
namespace OpenNest.Math
{
public static class Trigonometry
{
@@ -12,7 +12,7 @@ namespace OpenNest
/// <returns></returns>
public static double Base(double height, double hypotenuse)
{
return Math.Sqrt(hypotenuse * hypotenuse - height * height);
return System.Math.Sqrt(hypotenuse * hypotenuse - height * height);
}
/// <summary>
@@ -23,7 +23,7 @@ namespace OpenNest
/// <returns></returns>
public static double Height(double bottom, double hypotenuse)
{
return Math.Sqrt(hypotenuse * hypotenuse - bottom * bottom);
return System.Math.Sqrt(hypotenuse * hypotenuse - bottom * bottom);
}
/// <summary>
@@ -34,7 +34,7 @@ namespace OpenNest
/// <returns></returns>
public static double Hypotenuse(double height, double bottom)
{
return Math.Sqrt(height * height + bottom * bottom);
return System.Math.Sqrt(height * height + bottom * bottom);
}
}
}

View File

@@ -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;

View File

@@ -39,7 +39,7 @@
<ItemGroup>
<Compile Include="Align.cs" />
<Compile Include="AlignType.cs" />
<Compile Include="Angle.cs" />
<Compile Include="Math\Angle.cs" />
<Compile Include="CNC\KerfType.cs" />
<Compile Include="CNC\CodeType.cs" />
<Compile Include="CNC\LayerType.cs" />
@@ -47,7 +47,7 @@
<Compile Include="CutParameters.cs" />
<Compile Include="Drawing.cs" />
<Compile Include="Collections\DrawingCollection.cs" />
<Compile Include="EvenOdd.cs" />
<Compile Include="Math\EvenOdd.cs" />
<Compile Include="ConvertGeometry.cs" />
<Compile Include="CNC\ArcMove.cs" />
<Compile Include="CNC\Comment.cs" />
@@ -58,7 +58,7 @@
<Compile Include="CNC\Feedrate.cs" />
<Compile Include="CNC\Kerf.cs" />
<Compile Include="CNC\SubProgramCall.cs" />
<Compile Include="Generic.cs" />
<Compile Include="Math\Generic.cs" />
<Compile Include="Geometry\Arc.cs" />
<Compile Include="BoundingBox.cs" />
<Compile Include="Box.cs" />
@@ -77,7 +77,7 @@
<Compile Include="RotationType.cs" />
<Compile Include="Geometry\Shape.cs" />
<Compile Include="Size.cs" />
<Compile Include="Tolerance.cs" />
<Compile Include="Math\Tolerance.cs" />
<Compile Include="Units.cs" />
<Compile Include="Vector.cs" />
<Compile Include="IPostProcessor.cs" />
@@ -93,11 +93,11 @@
<Compile Include="CNC\Program.cs" />
<Compile Include="ConvertProgram.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Quantity.cs" />
<Compile Include="DwgQty.cs" />
<Compile Include="Spacing.cs" />
<Compile Include="Timing.cs" />
<Compile Include="TimingInfo.cs" />
<Compile Include="Trigonometry.cs" />
<Compile Include="Math\Trigonometry.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenNest.Collections;
using OpenNest.Math;
namespace OpenNest
{
@@ -113,7 +114,7 @@ namespace OpenNest
/// <param name="keepSameQuadrant"></param>
public void Rotate90(RotationType rotationDirection, bool keepSameQuadrant = true)
{
const double oneAndHalfPI = Math.PI * 1.5;
const double oneAndHalfPI = System.Math.PI * 1.5;
Size = new Size(Size.Height, Size.Width);
@@ -217,11 +218,11 @@ namespace OpenNest
return;
}
Rotate(Math.PI, centerpt);
Rotate(System.Math.PI, centerpt);
}
else
{
Rotate(Math.PI);
Rotate(System.Math.PI);
Quadrant = (Quadrant + 2) % 4;
if (Quadrant == 0)
@@ -386,23 +387,23 @@ namespace OpenNest
switch (Quadrant)
{
case 1:
width = Math.Abs(bounds.Right) + EdgeSpacing.Right;
height = Math.Abs(bounds.Top) + EdgeSpacing.Top;
width = System.Math.Abs(bounds.Right) + EdgeSpacing.Right;
height = System.Math.Abs(bounds.Top) + EdgeSpacing.Top;
break;
case 2:
width = Math.Abs(bounds.Left) + EdgeSpacing.Left;
height = Math.Abs(bounds.Top) + EdgeSpacing.Top;
width = System.Math.Abs(bounds.Left) + EdgeSpacing.Left;
height = System.Math.Abs(bounds.Top) + EdgeSpacing.Top;
break;
case 3:
width = Math.Abs(bounds.Left) + EdgeSpacing.Left;
height = Math.Abs(bounds.Bottom) + EdgeSpacing.Bottom;
width = System.Math.Abs(bounds.Left) + EdgeSpacing.Left;
height = System.Math.Abs(bounds.Bottom) + EdgeSpacing.Bottom;
break;
case 4:
width = Math.Abs(bounds.Right) + EdgeSpacing.Right;
height = Math.Abs(bounds.Bottom) + EdgeSpacing.Bottom;
width = System.Math.Abs(bounds.Right) + EdgeSpacing.Right;
height = System.Math.Abs(bounds.Bottom) + EdgeSpacing.Bottom;
break;
default:

View File

@@ -49,7 +49,7 @@ namespace OpenNest
public string ToString(int decimalPlaces)
{
return string.Format("{0} x {1}", Math.Round(Height, decimalPlaces), Math.Round(Width, decimalPlaces));
return string.Format("{0} x {1}", System.Math.Round(Height, decimalPlaces), System.Math.Round(Width, decimalPlaces));
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using OpenNest.Math;
namespace OpenNest
{
@@ -21,7 +22,7 @@ namespace OpenNest
var vx = pt.X - X;
var vy = pt.Y - Y;
return Math.Sqrt(vx * vx + vy * vy);
return System.Math.Sqrt(vx * vx + vy * vy);
}
public double DistanceTo(double x, double y)
@@ -29,7 +30,7 @@ namespace OpenNest
var vx = x - X;
var vy = y - Y;
return Math.Sqrt(vx * vx + vy * vy);
return System.Math.Sqrt(vx * vx + vy * vy);
}
public double DotProduct(Vector pt)
@@ -39,7 +40,7 @@ namespace OpenNest
public double Angle()
{
return OpenNest.Angle.NormalizeRad(Math.Atan2(Y, X));
return OpenNest.Math.Angle.NormalizeRad(System.Math.Atan2(Y, X));
}
/// <summary>
@@ -75,7 +76,7 @@ namespace OpenNest
var dot = v1.X * v2.X + v1.Y + v2.Y;
var det = v1.X * v2.X - v1.Y + v2.Y;
return Math.Atan2(det, dot);
return System.Math.Atan2(det, dot);
}
public static Vector operator +(Vector pt1, Vector pt2)
@@ -137,8 +138,8 @@ namespace OpenNest
{
var v = new Vector();
var cos = Math.Cos(angle);
var sin = Math.Sin(angle);
var cos = System.Math.Cos(angle);
var sin = System.Math.Sin(angle);
v.X = X * cos - Y * sin;
v.Y = X * sin + Y * cos;
@@ -151,8 +152,8 @@ namespace OpenNest
var v = new Vector();
var pt = this - origin;
var cos = Math.Cos(angle);
var sin = Math.Sin(angle);
var cos = System.Math.Cos(angle);
var sin = System.Math.Sin(angle);
v.X = pt.X * cos - pt.Y * sin + origin.X;
v.Y = pt.X * sin + pt.Y * cos + origin.Y;