Skip to content

Commit be95c6b

Browse files
committed
Update API & Constraints to use generic math in place of the Scalar type.
1 parent de7b9f1 commit be95c6b

15 files changed

Lines changed: 227 additions & 172 deletions

sources/Maths/Maths/Box2D.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,6 @@ public bool Contains(Box2D<T> other)
107107
=> (other.Min.X >= Min.X) && (other.Min.Y >= Min.Y)
108108
&& (other.Max.X <= Max.X) && (other.Max.Y <= Max.Y);
109109

110-
/// <summary>
111-
/// Calculates the distance to the nearest edge from the point.
112-
/// </summary>
113-
/// <param name="point">The point.</param>
114-
/// <returns>The distance.</returns>
115-
public T GetDistanceToNearestEdge(Vector2D<T> point)
116-
{
117-
var dx = T.Max(T.Max(Min.X - point.X, T.Zero), point.X - Max.X);
118-
var dy = T.Max(T.Max(Min.Y - point.Y, T.Zero), point.Y - Max.Y);
119-
return T.Sqrt((dx * dx) + (dy * dy));
120-
}
121-
122110
/// <summary>
123111
/// Calculates this box translated by a given distance.
124112
/// </summary>
@@ -251,4 +239,24 @@ public Box2D<TOther> AsTruncating<TOther>()
251239
return new(Min.AsTruncating<TOther>(), Max.AsTruncating<TOther>());
252240
}
253241
}
242+
243+
/// <summary>
244+
/// Helper methods to work with <see cref="Box2D{T}"/>
245+
/// </summary>
246+
public static class Box2D
247+
{
248+
/// <summary>
249+
/// Calculates the distance to the nearest edge from the point.
250+
/// </summary>
251+
/// <param name="box">The box.</param>
252+
/// <param name="point">The point.</param>
253+
/// <returns>The distance.</returns>
254+
public static T GetDistanceToNearestEdge<T>(this Box2D<T> box, Vector2D<T> point)
255+
where T : INumber<T>, IRootFunctions<T>
256+
{
257+
var dx = T.Max(T.Max(box.Min.X - point.X, T.Zero), point.X - box.Max.X);
258+
var dy = T.Max(T.Max(box.Min.Y - point.Y, T.Zero), point.Y - box.Max.Y);
259+
return T.Sqrt((dx * dx) + (dy * dy));
260+
}
261+
}
254262
}

sources/Maths/Maths/Box3D.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,6 @@ public bool Contains(Box3D<T> other)
111111
=> (other.Min.X >= this.Min.X) && (other.Min.Y >= this.Min.Y) && (other.Min.Z >= this.Min.Z)
112112
&& (other.Max.X <= this.Max.X) && (other.Max.Y <= this.Max.Y) && (other.Max.Z <= this.Max.Z);
113113

114-
/// <summary>
115-
/// Calculates the distance to the nearest edge from the point.
116-
/// </summary>
117-
/// <param name="point">The point.</param>
118-
/// <returns>The distance.</returns>
119-
public T GetDistanceToNearestEdge(Vector3D<T> point)
120-
{
121-
var dx = T.Max(T.Max(Min.X - point.X, T.Zero), point.X - Max.X);
122-
var dy = T.Max(T.Max(Min.Y - point.Y, T.Zero), point.Y - Max.Y);
123-
var dz = T.Max(T.Max(Min.Z - point.Z, T.Zero), point.Z - Max.Z);
124-
return T.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
125-
}
126-
127114
/// <summary>
128115
/// Calculates this box translated by a given distance.
129116
/// </summary>
@@ -226,4 +213,25 @@ public Box3D<TOther> As<TOther>()
226213
return new(Min.As<TOther>(), Max.As<TOther>());
227214
}
228215
}
216+
217+
/// <summary>
218+
/// Helper methods to work with <see cref="Box3D{T}"/>
219+
/// </summary>
220+
public static class Box3D
221+
{
222+
/// <summary>
223+
/// Calculates the distance to the nearest edge from the point.
224+
/// </summary>
225+
/// <param name="box">The box.</param>
226+
/// <param name="point">The point.</param>
227+
/// <returns>The distance.</returns>
228+
public static T GetDistanceToNearestEdge<T>(this Box3D<T> box, Vector3D<T> point)
229+
where T : INumber<T>, IRootFunctions<T>
230+
{
231+
var dx = T.Max(T.Max(box.Min.X - point.X, T.Zero), point.X - box.Max.X);
232+
var dy = T.Max(T.Max(box.Min.Y - point.Y, T.Zero), point.Y - box.Max.Y);
233+
var dz = T.Max(T.Max(box.Min.Z - point.Z, T.Zero), point.Z - box.Max.Z);
234+
return T.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
235+
}
236+
}
229237
}

sources/Maths/Maths/Circle.cs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,6 @@ public Circle(T centerX, T centerY, T radius)
6868
[IgnoreDataMember]
6969
public T Circumference => T.Tau * Radius;
7070

71-
/// <summary>
72-
/// Calculates whether this circle contains a point.
73-
/// </summary>
74-
/// <param name="point">The point.</param>
75-
/// <returns>True if this circle contains the point; False otherwise.</returns>
76-
/// <remarks>This does consider a point on the edge contained.</remarks>
77-
public bool Contains(Vector2D<T> point)
78-
{
79-
return Vector2D.DistanceSquared(point, Center) <= Radius;
80-
}
81-
82-
/// <summary>
83-
/// Calculates whether this circle contains another circle
84-
/// </summary>
85-
/// <param name="other">The circle.</param>
86-
/// <returns>True if this circle contains the given circle; False otherwise.</returns>
87-
/// <remarks>This does consider a circle that touches the edge contained.</remarks>
88-
public bool Contains(Circle<T> other)
89-
{
90-
var distanceSquared = Vector2D.DistanceSquared(Center, other.Center);
91-
var radiusDiff = Radius - other.Radius;
92-
return distanceSquared <= radiusDiff * radiusDiff;
93-
}
94-
9571
/// <summary>
9672
/// Calculates the squared distance to the nearest edge from the point.
9773
/// </summary>
@@ -119,16 +95,6 @@ public Circle<T> GetTranslated(Vector2D<T> distance)
11995
return new(Center + distance, Radius);
12096
}
12197

122-
/// <summary>
123-
/// Calculates a circle inflated to contain the given point.
124-
/// </summary>
125-
/// <param name="point">The point.</param>
126-
/// <returns>The circle.</returns>
127-
public Circle<T> GetInflated(Vector2D<T> point)
128-
{
129-
return new(Center, T.Max(Radius, Vector2D.Distance(Center, point)));
130-
}
131-
13298
/// <summary>Returns a boolean indicating whether the given Circle is equal to this Circle instance.</summary>
13399
/// <param name="other">The Circle to compare this instance to.</param>
134100
/// <returns>True if the other Circle is equal to this instance; False otherwise.</returns>
@@ -181,4 +147,50 @@ public Circle<TOther> As<TOther>() where TOther : IRootFunctions<TOther>
181147
return new(Center.As<TOther>(), TOther.CreateTruncating(Radius));
182148
}
183149
}
150+
151+
/// <summary>
152+
/// Helper methods to work with <see cref="Circle{T}"/>
153+
/// </summary>
154+
public static class Circle
155+
{
156+
/// <summary>
157+
/// Calculates whether this circle contains a point.
158+
/// </summary>
159+
/// <param name="circle">The circle.</param>
160+
/// <param name="point">The point.</param>
161+
/// <returns>True if this circle contains the point; False otherwise.</returns>
162+
/// <remarks>This does consider a point on the edge contained.</remarks>
163+
public static bool Contains<T>(this Circle<T> circle, Vector2D<T> point)
164+
where T : INumber<T>, IRootFunctions<T>
165+
{
166+
return Vector2D.DistanceSquared(point, circle.Center) <= circle.Radius;
167+
}
168+
169+
/// <summary>
170+
/// Calculates whether this circle contains another circle
171+
/// </summary>
172+
/// <param name="circle">The circle.</param>
173+
/// <param name="other">The other circle.</param>
174+
/// <returns>True if this circle contains the given circle; False otherwise.</returns>
175+
/// <remarks>This does consider a circle that touches the edge contained.</remarks>
176+
public static bool Contains<T>(this Circle<T> circle, Circle<T> other)
177+
where T : INumber<T>, IRootFunctions<T>
178+
{
179+
var distanceSquared = Vector2D.DistanceSquared(circle.Center, other.Center);
180+
var radiusDiff = circle.Radius - other.Radius;
181+
return distanceSquared <= radiusDiff * radiusDiff;
182+
}
183+
184+
/// <summary>
185+
/// Calculates a circle inflated to contain the given point.
186+
/// </summary>
187+
/// <param name="circle">The circle.</param>
188+
/// <param name="point">The point.</param>
189+
/// <returns>The circle.</returns>
190+
public static Circle<T> GetInflated<T>(this Circle<T> circle, Vector2D<T> point)
191+
where T : INumber<T>, IRootFunctions<T>
192+
{
193+
return new(circle.Center, T.Max(circle.Radius, Vector2D.Distance(circle.Center, point)));
194+
}
195+
}
184196
}

sources/Maths/Maths/Cube.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,6 @@ public bool Contains(Cube<T> other)
123123
&& (oMax.X <= tMax.X) && (oMax.Y <= tMax.Y) && (oMax.Y <= tMax.Y);
124124
}
125125

126-
/// <summary>
127-
/// Calculates the distance to the nearest edge from the point.
128-
/// </summary>
129-
/// <param name="point">The point.</param>
130-
/// <returns>The distance.</returns>
131-
public T GetDistanceToNearestEdge(Vector3D<T> point)
132-
{
133-
var max = Max;
134-
var dx = T.Max(T.Max(Origin.X - point.X, T.Zero), point.X - max.X);
135-
var dy = T.Max(T.Max(Origin.Y - point.Y, T.Zero), point.Y - max.Y);
136-
var dz = T.Max(T.Max(Origin.Z - point.Z, T.Zero), point.Z - max.Z);
137-
return T.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
138-
}
139-
140126
/// <summary>
141127
/// Calculates a new cube translated by a given distance.
142128
/// </summary>
@@ -241,4 +227,26 @@ public Cube<TOther> As<TOther>()
241227
return new(Origin.As<TOther>(), Max.As<TOther>());
242228
}
243229
}
230+
231+
/// <summary>
232+
/// Helper methods to work with <see cref="Cube{T}"/>
233+
/// </summary>
234+
public static class Cube
235+
{
236+
/// <summary>
237+
/// Calculates the distance to the nearest edge from the point.
238+
/// </summary>
239+
/// <param name="cube">The cube.</param>
240+
/// <param name="point">The point.</param>
241+
/// <returns>The distance.</returns>
242+
public static T GetDistanceToNearestEdge<T>(Cube<T> cube, Vector3D<T> point)
243+
where T : INumber<T>, IRootFunctions<T>
244+
{
245+
var max = cube.Max;
246+
var dx = T.Max(T.Max(cube.Origin.X - point.X, T.Zero), point.X - max.X);
247+
var dy = T.Max(T.Max(cube.Origin.Y - point.Y, T.Zero), point.Y - max.Y);
248+
var dz = T.Max(T.Max(cube.Origin.Z - point.Z, T.Zero), point.Z - max.Z);
249+
return T.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
250+
}
251+
}
244252
}

sources/Maths/Maths/Matrix2X3.Ops.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static partial class Matrix2X3
2121
/// <param name="cameraForwardVector">The forward vector of the camera.</param>
2222
/// <returns>The created billboard matrix</returns>
2323
public static Matrix2X3<T> CreateBillboard<T>(Vector3D<T> objectPosition, Vector3D<T> cameraPosition, Vector3D<T> cameraUpVector, Vector3D<T> cameraForwardVector)
24-
where T : IRootFunctions<T>
24+
where T : INumber<T>, IRootFunctions<T>
2525
{
2626
Vector3D<T> zaxis = objectPosition - cameraPosition;
2727
var norm = zaxis.LengthSquared;
@@ -46,7 +46,7 @@ public static Matrix2X3<T> CreateBillboard<T>(Vector3D<T> objectPosition, Vector
4646
/// <param name="angle">The angle to rotate around the given axis, in radians.</param>
4747
/// <returns>The rotation matrix.</returns>
4848
public static Matrix2X3<T> CreateFromAxisAngle<T>(Vector3D<T> axis, T angle)
49-
where T : INumberBase<T>
49+
where T : ITrigonometricFunctions<T>
5050
{
5151
// a: angle
5252
// x, y, z: unit vector for axis.
@@ -95,7 +95,7 @@ public static Matrix2X3<T> CreateFromAxisAngle<T>(Vector3D<T> axis, T angle)
9595
/// <param name="quaternion">The source Quaternion.</param>
9696
/// <returns>The rotation matrix.</returns>
9797
public static Matrix2X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
98-
where T : ITrigonometricFunctions<T>
98+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
9999
{
100100
Matrix2X3<T> result = Matrix2X3<T>.Identity;
101101

@@ -128,7 +128,7 @@ public static Matrix2X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
128128
/// <param name="roll">Angle of rotation, in radians, around the Z-axis.</param>
129129
/// <returns>The rotation matrix.</returns>
130130
public static Matrix2X3<T> CreateFromYawPitchRoll<T>(T yaw, T pitch, T roll)
131-
where T : ITrigonometricFunctions<T>
131+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
132132
{
133133
var q = Quaternion<T>.CreateFromYawPitchRoll(yaw, pitch, roll);
134134
return CreateFromQuaternion(q);
@@ -139,7 +139,7 @@ public static Matrix2X3<T> CreateFromYawPitchRoll<T>(T yaw, T pitch, T roll)
139139
/// <param name="rotation">The rotation to apply.</param>
140140
/// <returns>The transformed matrix.</returns>
141141
public static Matrix2X3<T> Transform<T>(Matrix2X3<T> value, Quaternion<T> rotation)
142-
where T : ITrigonometricFunctions<T>
142+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
143143
{
144144
// Compute rotation matrix.
145145
T x2 = rotation.X + rotation.X;

sources/Maths/Maths/Matrix3X2.Ops.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static partial class Matrix3X2
2222
/// <param name="radians">The amount of rotation, in radians.</param>
2323
/// <returns>A rotation matrix.</returns>
2424
public static Matrix3X2<T> CreateRotation<T>(T radians)
25-
where T : INumberBase<T>
25+
where T : IFloatingPointIeee754<T>
2626
{
2727
radians = T.Ieee754Remainder(radians, T.Tau);
2828

@@ -113,7 +113,7 @@ public static Matrix3X2<T> CreateRotation<T>(T radians)
113113
/// <param name="centerPoint">The center point.</param>
114114
/// <returns>A rotation matrix.</returns>
115115
public static Matrix3X2<T> CreateRotation<T>(T radians, Vector2D<T> centerPoint)
116-
where T : INumberBase<T>
116+
where T : IFloatingPointIeee754<T>
117117
{
118118
radians = T.Ieee754Remainder(radians, T.Tau);
119119

@@ -307,7 +307,7 @@ public static Matrix3X2<T> CreateScale<T>(T scale, Vector2D<T> centerPoint)
307307
/// <param name="radiansY">The Y angle, in radians.</param>
308308
/// <returns>A skew matrix.</returns>
309309
public static Matrix3X2<T> CreateSkew<T>(T radiansX, T radiansY)
310-
where T : INumberBase<T>
310+
where T : ITrigonometricFunctions<T>
311311
{
312312
Matrix3X2<T> result = Matrix3X2<T>.Identity;
313313

@@ -326,7 +326,7 @@ public static Matrix3X2<T> CreateSkew<T>(T radiansX, T radiansY)
326326
/// <param name="centerPoint">The center point.</param>
327327
/// <returns>A skew matrix.</returns>
328328
public static Matrix3X2<T> CreateSkew<T>(T radiansX, T radiansY, Vector2D<T> centerPoint)
329-
where T : INumberBase<T>
329+
where T : ITrigonometricFunctions<T>
330330
{
331331
Matrix3X2<T> result = Matrix3X2<T>.Identity;
332332

sources/Maths/Maths/Matrix3X3.Ops.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private struct VectorBasis<T>
4242
/// <param name="cameraForwardVector">The forward vector of the camera.</param>
4343
/// <returns>The created billboard matrix</returns>
4444
public static Matrix3X3<T> CreateBillboard<T>(Vector3D<T> objectPosition, Vector3D<T> cameraPosition, Vector3D<T> cameraUpVector, Vector3D<T> cameraForwardVector)
45-
where T : IRootFunctions<T>
45+
where T : INumber<T>, IRootFunctions<T>
4646
{
4747
Vector3D<T> zaxis = objectPosition - cameraPosition;
4848
var norm = zaxis.LengthSquared;
@@ -67,7 +67,7 @@ public static Matrix3X3<T> CreateBillboard<T>(Vector3D<T> objectPosition, Vector
6767
/// <param name="angle">The angle to rotate around the given axis, in radians.</param>
6868
/// <returns>The rotation matrix.</returns>
6969
public static Matrix3X3<T> CreateFromAxisAngle<T>(Vector3D<T> axis, T angle)
70-
where T : INumberBase<T>
70+
where T : ITrigonometricFunctions<T>
7171
{
7272
// a: angle
7373
// x, y, z: unit vector for axis.
@@ -120,7 +120,7 @@ public static Matrix3X3<T> CreateFromAxisAngle<T>(Vector3D<T> axis, T angle)
120120
/// <param name="quaternion">The source Quaternion.</param>
121121
/// <returns>The rotation matrix.</returns>
122122
public static Matrix3X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
123-
where T : ITrigonometricFunctions<T>
123+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
124124
{
125125
Matrix3X3<T> result = Matrix3X3<T>.Identity;
126126

@@ -157,7 +157,7 @@ public static Matrix3X3<T> CreateFromQuaternion<T>(Quaternion<T> quaternion)
157157
/// <param name="roll">Angle of rotation, in radians, around the Z-axis.</param>
158158
/// <returns>The rotation matrix.</returns>
159159
public static Matrix3X3<T> CreateFromYawPitchRoll<T>(T yaw, T pitch, T roll)
160-
where T : ITrigonometricFunctions<T>
160+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
161161
{
162162
var q = Quaternion<T>.CreateFromYawPitchRoll(yaw, pitch, roll);
163163
return CreateFromQuaternion(q);
@@ -167,7 +167,7 @@ public static Matrix3X3<T> CreateFromYawPitchRoll<T>(T yaw, T pitch, T roll)
167167
/// <param name="radians">The amount, in radians, by which to rotate around the X-axis.</param>
168168
/// <returns>The rotation matrix.</returns>
169169
public static Matrix3X3<T> CreateRotationX<T>(T radians)
170-
where T : INumberBase<T>
170+
where T : ITrigonometricFunctions<T>
171171
{
172172
Matrix3X3<T> result = Matrix3X3<T>.Identity;
173173

@@ -191,7 +191,7 @@ public static Matrix3X3<T> CreateRotationX<T>(T radians)
191191
/// <param name="radians">The amount, in radians, by which to rotate around the Y-axis.</param>
192192
/// <returns>The rotation matrix.</returns>
193193
public static Matrix3X3<T> CreateRotationY<T>(T radians)
194-
where T : INumberBase<T>
194+
where T : ITrigonometricFunctions<T>
195195
{
196196
Matrix3X3<T> result = Matrix3X3<T>.Identity;
197197

@@ -214,7 +214,7 @@ public static Matrix3X3<T> CreateRotationY<T>(T radians)
214214
/// <param name="radians">The amount, in radians, by which to rotate around the Z-axis.</param>
215215
/// <returns>The rotation matrix.</returns>
216216
public static Matrix3X3<T> CreateRotationZ<T>(T radians)
217-
where T : INumberBase<T>
217+
where T : ITrigonometricFunctions<T>
218218
{
219219
Matrix3X3<T> result = Matrix3X3<T>.Identity;
220220

@@ -476,7 +476,7 @@ public static bool Decompose<T>(Matrix3X3<T> matrix, out Vector3D<T> scale, out
476476
/// <param name="rotation">The rotation to apply.</param>
477477
/// <returns>The transformed matrix.</returns>
478478
public static Matrix3X3<T> Transform<T>(Matrix3X3<T> value, Quaternion<T> rotation)
479-
where T : ITrigonometricFunctions<T>
479+
where T : INumber<T>, IRootFunctions<T>, ITrigonometricFunctions<T>
480480
{
481481
// Compute rotation matrix.
482482
T x2 = rotation.X + rotation.X;

0 commit comments

Comments
 (0)