Skip to content

Feature/math 3.0 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions sources/Maths/Maths/Box2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

Expand All @@ -14,7 +15,7 @@ namespace Silk.NET.Maths
[DataContract]
public struct Box2D<T>
: IEquatable<Box2D<T>>
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
where T : INumber<T>
{
/// <summary>
/// The min.
Expand Down Expand Up @@ -148,9 +149,9 @@ public Box2D<T> GetScaled(Vector2D<T> scale, Vector2D<T> anchor)
/// <typeparam name="TScale">The type of the scale.</typeparam>
/// <returns>The calculated box.</returns>
public Box2D<T> GetScaled<TScale>(Vector2D<TScale> scale, Vector2D<T> anchor)
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
where TScale : INumber<TScale>
{
return this.As<TScale>().GetScaled(scale, anchor.As<TScale>()).As<T>();
return this.AsTruncating<TScale>().GetScaled(scale, anchor.AsTruncating<TScale>()).AsTruncating<T>();
}

/// <summary>
Expand Down Expand Up @@ -209,9 +210,44 @@ public override int GetHashCode()
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted box</returns>
public Box2D<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
public Box2D<TOther> As<TOther>()
where TOther : INumber<TOther>
{
return new(Min.As<TOther>(), Max.As<TOther>());
}

/// <summary>
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted box</returns>
public Box2D<TOther> AsChecked<TOther>()
where TOther : INumber<TOther>
{
return new(Min.AsChecked<TOther>(), Max.AsChecked<TOther>());
}

/// <summary>
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted box</returns>
public Box2D<TOther> AsSaturating<TOther>()
where TOther : INumber<TOther>
{
return new(Min.AsSaturating<TOther>(), Max.AsSaturating<TOther>());
}

/// <summary>
/// Returns this box casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted box</returns>
public Box2D<TOther> AsTruncating<TOther>()
where TOther : INumber<TOther>
{
return new(Min.AsTruncating<TOther>(), Max.AsTruncating<TOther>());
}
}
}
}
19 changes: 11 additions & 8 deletions sources/Maths/Maths/Box3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

Expand All @@ -14,7 +15,7 @@ namespace Silk.NET.Maths
[DataContract]
public struct Box3D<T>
: IEquatable<Box3D<T>>
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
where T : INumber<T>
{
/// <summary>
/// The min.
Expand Down Expand Up @@ -157,12 +158,12 @@ public Box3D<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
/// <param name="anchor">The anchor.</param>
/// <returns>The calculated box.</returns>
public Box3D<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
where TScale : INumberBase<TScale>
{
var convertedAnchor = anchor.As<TScale>();
var min = (scale * (Min.As<TScale>() - convertedAnchor)) + convertedAnchor;
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
return new Box3D<T>(min.As<T>(), max.As<T>());
var convertedAnchor = anchor.AsTruncating<TScale>();
var min = (scale * (Min.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
var max = (scale * (Max.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
return new Box3D<T>(min.AsTruncating<T>(), max.AsTruncating<T>());
}

/// <summary>
Expand Down Expand Up @@ -221,9 +222,11 @@ public override int GetHashCode()
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted box</returns>
public Box3D<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
public Box3D<TOther> As<TOther>()
where TOther : INumber<TOther>
{
return new(Min.As<TOther>(), Max.As<TOther>());
}
}
}
}
11 changes: 7 additions & 4 deletions sources/Maths/Maths/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.Serialization;

namespace Silk.NET.Maths
Expand All @@ -12,7 +13,8 @@ namespace Silk.NET.Maths
[Serializable]
[DataContract]
public struct Circle<T>
: IEquatable<Circle<T>> where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
: IEquatable<Circle<T>>
where T : IRootFunctions<T>
{
/// <summary>
/// The center.
Expand Down Expand Up @@ -166,15 +168,16 @@ public override int GetHashCode()
{
return !value1.Equals(value2);
}

/// <summary>
/// Returns this circle casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted circle</returns>
public Circle<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
public Circle<TOther> As<TOther>() where TOther : IRootFunctions<TOther>
{
return new(Center.As<TOther>(), Scalar.As<T, TOther>(Radius));
}
}
}
}
21 changes: 12 additions & 9 deletions sources/Maths/Maths/Cube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

Expand All @@ -14,7 +15,7 @@ namespace Silk.NET.Maths
[DataContract]
public struct Cube<T>
: IEquatable<Cube<T>>
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
where T : INumber<T>
{
/// <summary>
/// The origin.
Expand Down Expand Up @@ -169,12 +170,12 @@ public Cube<T> GetScaled(Vector3D<T> scale, Vector3D<T> anchor)
/// <param name="anchor">The anchor.</param>
/// <returns>The calculated cube.</returns>
public Cube<T> GetScaled<TScale>(Vector3D<TScale> scale, Vector3D<T> anchor)
where TScale : unmanaged, IFormattable, IEquatable<TScale>, IComparable<TScale>
where TScale : INumberBase<TScale>
{
var convertedAnchor = anchor.As<TScale>();
var min = (scale * (Origin.As<TScale>() - convertedAnchor)) + convertedAnchor;
var max = (scale * (Max.As<TScale>() - convertedAnchor)) + convertedAnchor;
return new(min.As<T>(), (max - min).As<T>());
var convertedAnchor = anchor.AsTruncating<TScale>();
var min = (scale * (Origin.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
var max = (scale * (Max.AsTruncating<TScale>() - convertedAnchor)) + convertedAnchor;
return new(min.AsTruncating<T>(), (max - min).AsTruncating<T>());
}

/// <summary>
Expand Down Expand Up @@ -229,15 +230,17 @@ public override int GetHashCode()
{
return !value1.Equals(value2);
}

/// <summary>
/// Returns this circle casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted cube</returns>
public Cube<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
[Obsolete("Use AsChecked, AsSaturating, or AsTruncating instead.", error: false)]
public Cube<TOther> As<TOther>()
where TOther : INumber<TOther>
{
return new(Origin.As<TOther>(), Max.As<TOther>());
}
}
}
}
Loading