Skip to content

Commit bfe143c

Browse files
authored
Merge pull request #5 from otac0n/feature/math-3.0
Feature/math 3.0
2 parents 0f2d43c + 1c2b6f8 commit bfe143c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+12805
-17221
lines changed

sources/Maths/Maths/Box2D.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Numerics;
56
using System.Runtime.CompilerServices;
67
using System.Runtime.Serialization;
78

@@ -14,7 +15,7 @@ namespace Silk.NET.Maths
1415
[DataContract]
1516
public struct Box2D<T>
1617
: IEquatable<Box2D<T>>
17-
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
18+
where T : INumber<T>
1819
{
1920
/// <summary>
2021
/// The min.
@@ -148,9 +149,9 @@ public Box2D<T> GetScaled(Vector2D<T> scale, Vector2D<T> anchor)
148149
/// <typeparam name="TScale">The type of the scale.</typeparam>
149150
/// <returns>The calculated box.</returns>
150151
public Box2D<T> GetScaled<TScale>(Vector2D<TScale> scale, Vector2D<T> anchor)
151-
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
152+
where TScale : INumber<TScale>
152153
{
153-
return this.As<TScale>().GetScaled(scale, anchor.As<TScale>()).As<T>();
154+
return this.AsTruncating<TScale>().GetScaled(scale, anchor.AsTruncating<TScale>()).AsTruncating<T>();
154155
}
155156

156157
/// <summary>
@@ -209,9 +210,44 @@ public override int GetHashCode()
209210
/// </summary>
210211
/// <typeparam name="TOther">The type to cast to</typeparam>
211212
/// <returns>The casted box</returns>
212-
public Box2D<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
213+
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
214+
public Box2D<TOther> As<TOther>()
215+
where TOther : INumber<TOther>
213216
{
214217
return new(Min.As<TOther>(), Max.As<TOther>());
215218
}
219+
220+
/// <summary>
221+
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
222+
/// </summary>
223+
/// <typeparam name="TOther">The type to cast to</typeparam>
224+
/// <returns>The casted box</returns>
225+
public Box2D<TOther> AsChecked<TOther>()
226+
where TOther : INumber<TOther>
227+
{
228+
return new(Min.AsChecked<TOther>(), Max.AsChecked<TOther>());
229+
}
230+
231+
/// <summary>
232+
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
233+
/// </summary>
234+
/// <typeparam name="TOther">The type to cast to</typeparam>
235+
/// <returns>The casted box</returns>
236+
public Box2D<TOther> AsSaturating<TOther>()
237+
where TOther : INumber<TOther>
238+
{
239+
return new(Min.AsSaturating<TOther>(), Max.AsSaturating<TOther>());
240+
}
241+
242+
/// <summary>
243+
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
244+
/// </summary>
245+
/// <typeparam name="TOther">The type to cast to</typeparam>
246+
/// <returns>The casted box</returns>
247+
public Box2D<TOther> AsTruncating<TOther>()
248+
where TOther : INumber<TOther>
249+
{
250+
return new(Min.AsTruncating<TOther>(), Max.AsTruncating<TOther>());
251+
}
216252
}
217-
}
253+
}

sources/Maths/Maths/Box3D.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Numerics;
56
using System.Runtime.CompilerServices;
67
using System.Runtime.Serialization;
78

@@ -14,7 +15,7 @@ namespace Silk.NET.Maths
1415
[DataContract]
1516
public struct Box3D<T>
1617
: IEquatable<Box3D<T>>
17-
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
18+
where T : INumber<T>
1819
{
1920
/// <summary>
2021
/// The min.
@@ -157,12 +158,12 @@ public Box3D<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
157158
/// <param name="anchor">The anchor.</param>
158159
/// <returns>The calculated box.</returns>
159160
public Box3D<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
160-
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
161+
where TScale : INumberBase<TScale>
161162
{
162-
var convertedAnchor = anchor.As<TScale>();
163-
var min = (scale * (Min.As<TScale>() - convertedAnchor)) + convertedAnchor;
164-
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
165-
return new Box3D<T>(min.As<T>(), max.As<T>());
163+
var convertedAnchor = anchor.AsTruncating<TScale>();
164+
var min = (scale * (Min.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
165+
var max = (scale * (Max.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
166+
return new Box3D<T>(min.AsTruncating<T>(), max.AsTruncating<T>());
166167
}
167168

168169
/// <summary>
@@ -221,9 +222,11 @@ public override int GetHashCode()
221222
/// </summary>
222223
/// <typeparam name="TOther">The type to cast to</typeparam>
223224
/// <returns>The casted box</returns>
224-
public Box3D<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
225+
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
226+
public Box3D<TOther> As<TOther>()
227+
where TOther : INumber<TOther>
225228
{
226229
return new(Min.As<TOther>(), Max.As<TOther>());
227230
}
228231
}
229-
}
232+
}

sources/Maths/Maths/Circle.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Numerics;
56
using System.Runtime.Serialization;
67

78
namespace Silk.NET.Maths
@@ -12,7 +13,8 @@ namespace Silk.NET.Maths
1213
[Serializable]
1314
[DataContract]
1415
public struct Circle<T>
15-
: IEquatable<Circle<T>> where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
16+
: IEquatable<Circle<T>>
17+
where T : IRootFunctions<T>
1618
{
1719
/// <summary>
1820
/// The center.
@@ -166,15 +168,16 @@ public override int GetHashCode()
166168
{
167169
return !value1.Equals(value2);
168170
}
169-
171+
170172
/// <summary>
171173
/// Returns this circle casted to <typeparamref name="TOther"></typeparamref>
172174
/// </summary>
173175
/// <typeparam name="TOther">The type to cast to</typeparam>
174176
/// <returns>The casted circle</returns>
175-
public Circle<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
177+
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
178+
public Circle<TOther> As<TOther>() where TOther : IRootFunctions<TOther>
176179
{
177180
return new(Center.As<TOther>(), Scalar.As<T, TOther>(Radius));
178181
}
179182
}
180-
}
183+
}

sources/Maths/Maths/Cube.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Numerics;
56
using System.Runtime.CompilerServices;
67
using System.Runtime.Serialization;
78

@@ -14,7 +15,7 @@ namespace Silk.NET.Maths
1415
[DataContract]
1516
public struct Cube<T>
1617
: IEquatable<Cube<T>>
17-
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
18+
where T : INumber<T>
1819
{
1920
/// <summary>
2021
/// The origin.
@@ -169,12 +170,12 @@ public Cube<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
169170
/// <param name="anchor">The anchor.</param>
170171
/// <returns>The calculated cube.</returns>
171172
public Cube<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
172-
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
173+
where TScale : INumberBase<TScale>
173174
{
174-
var convertedAnchor = anchor.As<TScale>();
175-
var min = (scale * (Origin.As<TScale>() - convertedAnchor)) + convertedAnchor;
176-
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
177-
return new(min.As<T>(), (max - min).As<T>());
175+
var convertedAnchor = anchor.AsTruncating<TScale>();
176+
var min = (scale * (Origin.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
177+
var max = (scale * (Max.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
178+
return new(min.AsTruncating<T>(), (max - min).AsTruncating<T>());
178179
}
179180

180181
/// <summary>
@@ -229,15 +230,17 @@ public override int GetHashCode()
229230
{
230231
return !value1.Equals(value2);
231232
}
232-
233+
233234
/// <summary>
234235
/// Returns this circle casted to <typeparamref name="TOther"></typeparamref>
235236
/// </summary>
236237
/// <typeparam name="TOther">The type to cast to</typeparam>
237238
/// <returns>The casted cube</returns>
238-
public Cube<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
239+
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
240+
public Cube<TOther> As<TOther>()
241+
where TOther : INumber<TOther>
239242
{
240243
return new(Origin.As<TOther>(), Max.As<TOther>());
241244
}
242245
}
243-
}
246+
}

0 commit comments

Comments
 (0)