Skip to content

Commit c3ac0c0

Browse files
committed
Align behind SDF-based edge functions.
1 parent 8606907 commit c3ac0c0

5 files changed

Lines changed: 96 additions & 37 deletions

File tree

sources/Maths/Maths/Circle.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ public Circle(T centerX, T centerY, T radius)
7373
/// <param name="point">The point.</param>
7474
/// <returns><c>true</c> if this circle contains the point; <c>false</c> otherwise.</returns>
7575
/// <remarks>This does consider a point on the edge contained.</remarks>
76-
public bool Contains(Vector2D<T> point)
77-
{
78-
return Vector2D.DistanceSquared(Center, point) <= SquaredRadius;
79-
}
76+
public bool Contains(Vector2D<T> point) =>
77+
Vector2D.DistanceSquared(Center, point) <= SquaredRadius;
8078

8179
/// <summary>
8280
/// Calculates whether this circle contains another circle
@@ -86,26 +84,39 @@ public bool Contains(Vector2D<T> point)
8684
/// <remarks>This does consider a circle that touches the edge contained.</remarks>
8785
public bool Contains(Circle<T> other)
8886
{
87+
if (Radius < other.Radius)
88+
{
89+
return false;
90+
}
91+
8992
var distanceSquared = Vector2D.DistanceSquared(Center, other.Center);
9093
var radiusDiff = Radius - other.Radius;
9194
return distanceSquared <= radiusDiff * radiusDiff;
9295
}
9396

9497
/// <summary>
95-
/// Calculates the squared distance to the nearest edge from the point.
98+
/// Calculates the distance to the nearest edge from the point.
9699
/// </summary>
97100
/// <param name="point">The point.</param>
98-
/// <returns>The distance squared.</returns>
99-
public readonly T GetDistanceToInteriorSquared(Vector2D<T> point) =>
100-
T.Max(Vector2D.DistanceSquared(Center, point) - SquaredRadius, T.Zero);
101+
/// <returns>The distance.</returns>
102+
public T GetSignedDistanceToEdge(Vector2D<T> point) =>
103+
Vector2D.Distance(Center, point) - Radius;
101104

102105
/// <summary>
103106
/// Calculates the distance to the nearest edge from the point.
104107
/// </summary>
105108
/// <param name="point">The point.</param>
106109
/// <returns>The distance.</returns>
110+
public T GetDistanceToEdge(Vector2D<T> point) =>
111+
T.Abs(GetSignedDistanceToEdge(point));
112+
113+
/// <summary>
114+
/// Calculates the distance to the interior from the point.
115+
/// </summary>
116+
/// <param name="point">The point.</param>
117+
/// <returns>The distance.</returns>
107118
public T GetDistanceToInterior(Vector2D<T> point) =>
108-
T.Sqrt(GetDistanceToInteriorSquared(point));
119+
T.Max(GetSignedDistanceToEdge(point), T.Zero);
109120

110121
/// <summary>
111122
/// Calculates a circle inflated to contain the given point.

sources/Maths/Maths/IExtents2D.Ops.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,32 @@ public bool Contains(Vector2D<TPoint> point)
7474
where TPoint : INumber<TPoint>, IRootFunctions<TPoint>
7575
{
7676
/// <summary>
77-
/// Calculates the distance to the interior of the region from the specified point.
77+
/// Calculates the signed distance to the nearest edge of the region from the specified point.
7878
/// </summary>
7979
/// <param name="point">The point.</param>
80-
/// <returns>The distance to the interior of the region.</returns>
81-
public TPoint GetDistanceToInterior(Vector2D<TPoint> point)
80+
/// <returns>The signed distance to the nearest edge of the region.</returns>
81+
public TPoint GetSignedDistanceToEdge(Vector2D<TPoint> point)
8282
{
83-
var min = region.Min;
84-
var max = region.Max;
85-
var dx = TPoint.Max(TPoint.Max(min.X - point.X, TPoint.Zero), point.X - max.X);
86-
var dy = TPoint.Max(TPoint.Max(min.Y - point.Y, TPoint.Zero), point.Y - max.Y);
87-
return TPoint.Sqrt((dx * dx) + (dy * dy));
83+
var q = Vector2D.Max(region.Min - point, point - region.Max);
84+
var dInner = TPoint.Max(q.X, q.Y);
85+
return dInner < TPoint.Zero ? dInner : Vector2D.Max(q, TPoint.Zero).Length;
8886
}
87+
88+
/// <summary>
89+
/// Calculates the distance to the nearest edge from the point.
90+
/// </summary>
91+
/// <param name="point">The point.</param>
92+
/// <returns>The distance.</returns>
93+
public TPoint GetDistanceToEdge(Vector2D<TPoint> point) =>
94+
TPoint.Abs(region.GetSignedDistanceToEdge(point));
95+
96+
/// <summary>
97+
/// Calculates the distance to the interior from the point.
98+
/// </summary>
99+
/// <param name="point">The point.</param>
100+
/// <returns>The distance.</returns>
101+
public TPoint GetDistanceToInterior(Vector2D<TPoint> point) =>
102+
TPoint.Max(region.GetSignedDistanceToEdge(point), TPoint.Zero);
89103
}
90104
}
91105
}

sources/Maths/Maths/IExtents3D.Ops.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,32 @@ public bool Contains(Vector3D<TPoint> point)
7474
where TPoint : INumber<TPoint>, IRootFunctions<TPoint>
7575
{
7676
/// <summary>
77-
/// Calculates the distance to the interior of the region from the specified point.
77+
/// Calculates the signed distance to the nearest edge of the region from the specified point.
7878
/// </summary>
7979
/// <param name="point">The point.</param>
80-
/// <returns>The distance to the interior of the region.</returns>
81-
public TPoint GetDistanceToInterior(Vector3D<TPoint> point)
80+
/// <returns>The signed distance to the nearest edge of the region.</returns>
81+
public TPoint GetSignedDistanceToEdge(Vector3D<TPoint> point)
8282
{
83-
var min = region.Min;
84-
var max = region.Max;
85-
var dx = TPoint.Max(TPoint.Max(min.X - point.X, TPoint.Zero), point.X - max.X);
86-
var dy = TPoint.Max(TPoint.Max(min.Y - point.Y, TPoint.Zero), point.Y - max.Y);
87-
var dz = TPoint.Max(TPoint.Max(min.Z - point.Z, TPoint.Zero), point.Z - max.Z);
88-
return TPoint.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
83+
var q = Vector3D.Max(region.Min - point, point - region.Max);
84+
var dInner = TPoint.Max(TPoint.Max(q.X, q.Y), q.Z);
85+
return dInner < TPoint.Zero ? dInner : Vector3D.Max(q, TPoint.Zero).Length;
8986
}
87+
88+
/// <summary>
89+
/// Calculates the distance to the nearest edge from the point.
90+
/// </summary>
91+
/// <param name="point">The point.</param>
92+
/// <returns>The distance.</returns>
93+
public TPoint GetDistanceToEdge(Vector3D<TPoint> point) =>
94+
TPoint.Abs(region.GetSignedDistanceToEdge(point));
95+
96+
/// <summary>
97+
/// Calculates the distance to the interior from the point.
98+
/// </summary>
99+
/// <param name="point">The point.</param>
100+
/// <returns>The distance.</returns>
101+
public TPoint GetDistanceToInterior(Vector3D<TPoint> point) =>
102+
TPoint.Max(region.GetSignedDistanceToEdge(point), TPoint.Zero);
90103
}
91104
}
92105
}

sources/Maths/Maths/PublicAPI/net10.0/PublicAPI.Unshipped.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,26 @@ Silk.NET.Maths.Circle<T>.Contains(Silk.NET.Maths.Circle<T> other) -> bool
120120
Silk.NET.Maths.Circle<T>.Contains(Silk.NET.Maths.Vector2D<T> point) -> bool
121121
Silk.NET.Maths.Circle<T>.Diameter.get -> T
122122
Silk.NET.Maths.Circle<T>.Equals(Silk.NET.Maths.Circle<T> other) -> bool
123+
Silk.NET.Maths.Circle<T>.GetDistanceToEdge(Silk.NET.Maths.Vector2D<T> point) -> T
123124
Silk.NET.Maths.Circle<T>.GetDistanceToInterior(Silk.NET.Maths.Vector2D<T> point) -> T
124-
Silk.NET.Maths.Circle<T>.GetDistanceToInteriorSquared(Silk.NET.Maths.Vector2D<T> point) -> T
125125
Silk.NET.Maths.Circle<T>.GetInflated(Silk.NET.Maths.Vector2D<T> point) -> Silk.NET.Maths.Circle<T>
126126
Silk.NET.Maths.Circle<T>.GetScaled(T scale, Silk.NET.Maths.Vector2D<T> anchor) -> Silk.NET.Maths.Circle<T>
127+
Silk.NET.Maths.Circle<T>.GetSignedDistanceToEdge(Silk.NET.Maths.Vector2D<T> point) -> T
127128
Silk.NET.Maths.Circle<T>.GetTranslated(Silk.NET.Maths.Vector2D<T> distance) -> Silk.NET.Maths.Circle<T>
128129
Silk.NET.Maths.Circle<T>.Radius -> T
129130
Silk.NET.Maths.Circle<T>.SquaredRadius.get -> T
130131
Silk.NET.Maths.Extents2D
131132
Silk.NET.Maths.Extents2D.extension<TSelf, TPoint>(TSelf)
132133
Silk.NET.Maths.Extents2D.extension<TSelf, TPoint>(TSelf).Contains(Silk.NET.Maths.Vector2D<TPoint> point) -> bool
134+
Silk.NET.Maths.Extents2D.extension<TSelf, TPoint>(TSelf).GetDistanceToEdge(Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
133135
Silk.NET.Maths.Extents2D.extension<TSelf, TPoint>(TSelf).GetDistanceToInterior(Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
136+
Silk.NET.Maths.Extents2D.extension<TSelf, TPoint>(TSelf).GetSignedDistanceToEdge(Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
134137
Silk.NET.Maths.Extents3D
135138
Silk.NET.Maths.Extents3D.extension<TSelf, TPoint>(TSelf)
136139
Silk.NET.Maths.Extents3D.extension<TSelf, TPoint>(TSelf).Contains(Silk.NET.Maths.Vector3D<TPoint> point) -> bool
140+
Silk.NET.Maths.Extents3D.extension<TSelf, TPoint>(TSelf).GetDistanceToEdge(Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
137141
Silk.NET.Maths.Extents3D.extension<TSelf, TPoint>(TSelf).GetDistanceToInterior(Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
142+
Silk.NET.Maths.Extents3D.extension<TSelf, TPoint>(TSelf).GetSignedDistanceToEdge(Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
138143
Silk.NET.Maths.IExtents2D<T>
139144
Silk.NET.Maths.IExtents2D<T>.Center.get -> Silk.NET.Maths.Vector2D<T>
140145
Silk.NET.Maths.IExtents2D<T>.Contains<TOther>(TOther other) -> bool
@@ -559,10 +564,11 @@ Silk.NET.Maths.Sphere<T>.Contains(Silk.NET.Maths.Sphere<T> other) -> bool
559564
Silk.NET.Maths.Sphere<T>.Contains(Silk.NET.Maths.Vector3D<T> point) -> bool
560565
Silk.NET.Maths.Sphere<T>.Diameter.get -> T
561566
Silk.NET.Maths.Sphere<T>.Equals(Silk.NET.Maths.Sphere<T> other) -> bool
567+
Silk.NET.Maths.Sphere<T>.GetDistanceToEdge(Silk.NET.Maths.Vector3D<T> point) -> T
562568
Silk.NET.Maths.Sphere<T>.GetDistanceToInterior(Silk.NET.Maths.Vector3D<T> point) -> T
563-
Silk.NET.Maths.Sphere<T>.GetDistanceToInteriorSquared(Silk.NET.Maths.Vector3D<T> point) -> T
564569
Silk.NET.Maths.Sphere<T>.GetInflated(Silk.NET.Maths.Vector3D<T> point) -> Silk.NET.Maths.Sphere<T>
565570
Silk.NET.Maths.Sphere<T>.GetScaled(T scale, Silk.NET.Maths.Vector3D<T> anchor) -> Silk.NET.Maths.Sphere<T>
571+
Silk.NET.Maths.Sphere<T>.GetSignedDistanceToEdge(Silk.NET.Maths.Vector3D<T> point) -> T
566572
Silk.NET.Maths.Sphere<T>.GetTranslated(Silk.NET.Maths.Vector3D<T> distance) -> Silk.NET.Maths.Sphere<T>
567573
Silk.NET.Maths.Sphere<T>.Radius -> T
568574
Silk.NET.Maths.Sphere<T>.Sphere() -> void
@@ -665,11 +671,15 @@ static Silk.NET.Maths.Circle<T>.operator !=(Silk.NET.Maths.Circle<T> left, Silk.
665671
static Silk.NET.Maths.Circle<T>.operator ==(Silk.NET.Maths.Circle<T> left, Silk.NET.Maths.Circle<T> right) -> bool
666672
static Silk.NET.Maths.Extents2D.Contains<T1, T2, TPoint>(T1 a, T2 b) -> bool
667673
static Silk.NET.Maths.Extents2D.Contains<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector2D<TPoint> point) -> bool
674+
static Silk.NET.Maths.Extents2D.GetDistanceToEdge<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
668675
static Silk.NET.Maths.Extents2D.GetDistanceToInterior<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
676+
static Silk.NET.Maths.Extents2D.GetSignedDistanceToEdge<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector2D<TPoint> point) -> TPoint
669677
static Silk.NET.Maths.Extents2D.Intersects<T1, T2, TPoint>(T1 a, T2 b) -> bool
670678
static Silk.NET.Maths.Extents3D.Contains<T1, T2, TPoint>(T1 a, T2 b) -> bool
671679
static Silk.NET.Maths.Extents3D.Contains<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector3D<TPoint> point) -> bool
680+
static Silk.NET.Maths.Extents3D.GetDistanceToEdge<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
672681
static Silk.NET.Maths.Extents3D.GetDistanceToInterior<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
682+
static Silk.NET.Maths.Extents3D.GetSignedDistanceToEdge<TSelf, TPoint>(this TSelf region, Silk.NET.Maths.Vector3D<TPoint> point) -> TPoint
673683
static Silk.NET.Maths.Extents3D.Intersects<T1, T2, TPoint>(T1 a, T2 b) -> bool
674684
static Silk.NET.Maths.Matrix2X2.Add<T>(Silk.NET.Maths.Matrix2X2<T> left, Silk.NET.Maths.Matrix2X2<T> right) -> Silk.NET.Maths.Matrix2X2<T>
675685
static Silk.NET.Maths.Matrix2X2.Lerp<T>(Silk.NET.Maths.Matrix2X2<T> value1, Silk.NET.Maths.Matrix2X2<T> value2, T amount) -> Silk.NET.Maths.Matrix2X2<T>

sources/Maths/Maths/Sphere.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ public Sphere(T centerX, T centerY, T centerZ, T radius)
6868
/// <param name="point">The point.</param>
6969
/// <returns><c>true</c> if this sphere contains the point; <c>false</c> otherwise.</returns>
7070
/// <remarks>This does consider a point on the edge contained.</remarks>
71-
public bool Contains(Vector3D<T> point)
72-
{
73-
return Vector3D.DistanceSquared(Center, point) <= SquaredRadius;
74-
}
71+
public bool Contains(Vector3D<T> point) =>
72+
Vector3D.DistanceSquared(Center, point) <= SquaredRadius;
7573

7674
/// <summary>
7775
/// Calculates whether this sphere contains another sphere
@@ -81,26 +79,39 @@ public bool Contains(Vector3D<T> point)
8179
/// <remarks>This does consider a sphere that touches the edge contained.</remarks>
8280
public bool Contains(Sphere<T> other)
8381
{
82+
if (Radius < other.Radius)
83+
{
84+
return false;
85+
}
86+
8487
var distanceSquared = Vector3D.DistanceSquared(Center, other.Center);
8588
var radiusDiff = Radius - other.Radius;
8689
return distanceSquared <= radiusDiff * radiusDiff;
8790
}
8891

8992
/// <summary>
90-
/// Calculates the squared distance to the nearest edge from the point.
93+
/// Calculates the signed distance to the nearest edge from the point.
9194
/// </summary>
9295
/// <param name="point">The point.</param>
93-
/// <returns>The distance squared.</returns>
94-
public T GetDistanceToInteriorSquared(Vector3D<T> point) =>
95-
T.Max(Vector3D.DistanceSquared(Center, point) - SquaredRadius, T.Zero);
96+
/// <returns>The distance.</returns>
97+
public T GetSignedDistanceToEdge(Vector3D<T> point) =>
98+
Vector3D.Distance(Center, point) - Radius;
9699

97100
/// <summary>
98101
/// Calculates the distance to the nearest edge from the point.
99102
/// </summary>
100103
/// <param name="point">The point.</param>
101104
/// <returns>The distance.</returns>
105+
public T GetDistanceToEdge(Vector3D<T> point) =>
106+
T.Abs(GetSignedDistanceToEdge(point));
107+
108+
/// <summary>
109+
/// Calculates the distance to the interior from the point.
110+
/// </summary>
111+
/// <param name="point">The point.</param>
112+
/// <returns>The distance.</returns>
102113
public T GetDistanceToInterior(Vector3D<T> point) =>
103-
T.Sqrt(GetDistanceToInteriorSquared(point));
114+
T.Max(GetSignedDistanceToEdge(point), T.Zero);
104115

105116
/// <summary>
106117
/// Calculates a sphere inflated to contain the given point.

0 commit comments

Comments
 (0)