ScriptCore: matrix generator

This commit is contained in:
Simon Lübeß
2024-03-02 13:54:34 +01:00
parent 0a8d83ffb6
commit caaa7646ef
17 changed files with 1529 additions and 42 deletions
@@ -0,0 +1,111 @@
using System;
namespace GlitchyEngine.Math.Attributes;
/// <summary>
/// Specifies that fields for the components of a matrix should be generated as well as basic methods like constructors, type casts and equality checks.
/// </summary>
[AttributeUsage(AttributeTargets.Struct)]
public sealed class MatrixAttribute : Attribute
{
/// <summary>
/// The Type of the matrices components.
/// </summary>
public Type Type { get; }
/// <summary>
/// The number of rows the matrix has.
/// </summary>
public int Rows { get; }
/// <summary>
/// The number of columns the matrix has.
/// </summary>
public int Columns { get; }
/// <summary>
/// The base name of the matrix. (Name of matrix without the row and column count)
/// </summary>
public string BaseName { get; }
/// <summary>
/// Creates a new instance of the <see cref="MatrixAttribute"/> class.
/// </summary>
/// <param name="type"><inheritdoc cref="Type"/></param>
/// <param name="rows"><inheritdoc cref="Rows"/></param>
/// <param name="columns"><inheritdoc cref="Columns"/></param>
/// <param name="baseName"><inheritdoc cref="BaseName"/></param>
public MatrixAttribute(Type type, int rows, int columns, string baseName)
{
Type = type;
Rows = rows;
Columns = columns;
BaseName = baseName;
}
}
/// <summary>
/// Specifies that the matrix type should have comparison operators (>, <, >= and <=) generated for it.
/// </summary>
[AttributeUsage(AttributeTargets.Struct)]
public sealed class ComparableMatrixAttribute : Attribute
{
}
/// <summary>
/// Specifies that the matrix type should have component wise math operators (+, -) generated for it.
/// </summary>
[AttributeUsage(AttributeTargets.Struct)]
public class MatrixMathAttribute : Attribute { }
/// <summary>
/// Specifies that the matrix type should have component wise logic operators (&, ^ and |) generated for it.
/// </summary>
[AttributeUsage(AttributeTargets.Struct)]
public class MatrixLogicAttribute : Attribute { }
/// <summary>
/// Generates a multiplication overload for matrix type with the specified vector type.
/// </summary>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)]
public class MatrixVectorMultiplicationAttribute : Attribute
{
/// <summary>
/// The type of the vector that will be multiplied with the matrix.
/// </summary>
public Type VectorType { get; set; }
/// <summary>
/// Creates a new instance of the <see cref="MatrixVectorMultiplicationAttribute"/> class.
/// </summary>
/// <param name="vectorType"><inheritdoc cref="VectorType"/></param>
public MatrixVectorMultiplicationAttribute(Type vectorType)
{
VectorType = vectorType;
}
}
/// <summary>
/// Generates a cast cast from the matrix type to the specified target type.
/// </summary>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)]
public class MatrixCastAttribute : Attribute
{
/// <summary>
/// The type of the target matrix type.
/// </summary>
public Type TargetType { get; set; }
/// <summary>
/// If <see cref="true"/> the cast will be explicit; if <see cref="false"/> it will an implicit cast.
/// </summary>
public bool IsExplicit { get; set; }
/// <summary>
/// Creates a new instance of the <see cref="MatrixCastAttribute"/> class.
/// </summary>
/// <param name="targetType"><inheritdoc cref="TargetType"/></param>
/// <param name="isExplicit"><inheritdoc cref="IsExplicit"/></param>
public MatrixCastAttribute(Type targetType, bool isExplicit)
{
TargetType = targetType;
IsExplicit = isExplicit;
}
}
+45 -4
View File
@@ -9,7 +9,7 @@ namespace GlitchyEngine.Math;
/// <summary>
/// Provides constants and methods for trigonometric and vector calculations.
/// </summary>
public static class Math
public static partial class Math
{
/// An optimal representation of π.
public const float Pi = 3.141592654f;
@@ -35,6 +35,7 @@ public static class Math
/// Converts radians to degrees
public const float DegToRad = Pi / 180.0f;
#region any / all
/// Returns true if at least one of the components is true.
public static bool any(bool value) => value;
@@ -48,18 +49,58 @@ public static class Math
/// Returns true if at least one of the components is true.
public static bool any(bool4 value) => value.X || value.Y || value.Z || value.W;
/// Returns true if at least one of the components is true.
public static bool any(bool2x2 value) => value.M11 || value.M12 || value.M21 || value.M22;
/// Returns true if at least one of the components is true.
public static bool any(bool3x3 value) => value.M11 || value.M12 || value.M13 ||
value.M21 || value.M22 || value.M23 ||
value.M31 || value.M32 || value.M33;
/// Returns true if at least one of the components is true.
public static bool any(bool4x4 value) => value.M11 || value.M12 || value.M13 || value.M14 ||
value.M21 || value.M22 || value.M23 || value.M24 ||
value.M31 || value.M32 || value.M33 || value.M34 ||
value.M41 || value.M42 || value.M43 || value.M44;
/// Returns true if all of the components are true.
public static bool all(bool value) => value;
/// Returns true if all of the components are true.
public static bool all(bool2 value) => value.X && value.Y;
public static bool all(bool2 value) => value is { X: true, Y: true };
/// Returns true if all of the components are true.
public static bool all(bool3 value) => value.X && value.Y && value.Z;
public static bool all(bool3 value) => value is { X: true, Y: true, Z: true };
/// Returns true if all of the components are true.
public static bool all(bool4 value) => value.X && value.Y && value.Z && value.W;
public static bool all(bool4 value) => value is { X: true, Y: true, Z: true, W: true };
/// Returns true if all of the components are true.
public static bool all(bool2x2 value) => value is
{
M11: true, M12: true,
M21: true, M22: true
};
/// Returns true if all of the components are true.
public static bool all(bool3x3 value) => value is
{
M11: true, M12: true, M13: true,
M21: true, M22: true, M23: true,
M31: true, M32: true, M33: true
};
/// Returns true if all of the components are true.
public static bool all(bool4x4 value) => value is
{
M11: true, M12: true, M13: true, M14: true,
M21: true, M22: true, M23: true, M24: true,
M31: true, M32: true, M33: true, M34: true,
M41: true, M42: true, M43: true, M44: true
};
#endregion
#region abs
public static float abs(float value)
+30
View File
@@ -0,0 +1,30 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of boolean values.
/// </summary>
[Matrix(typeof(bool), 2, 2, "bool")]
[MatrixLogic]
public partial struct bool2x2
{
}
/// <summary>
/// A matrix with 3 rows and 3 columns of boolean values.
/// </summary>
[Matrix(typeof(bool), 3, 3, "bool")]
[MatrixLogic]
public partial struct bool3x3
{
}
/// <summary>
/// A matrix with 4 rows and 4 columns of boolean values.
/// </summary>
[Matrix(typeof(bool), 4, 4, "bool")]
[MatrixLogic]
public partial struct bool4x4
{
}
@@ -0,0 +1,90 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of double-precision floating-point values.
/// </summary>
[Matrix(typeof(double), 2, 2, "double")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(double2))]
[MatrixCast(typeof(float), true)]
public partial struct double2x2
{
/// <summary>
/// A 2x2 matrix whose elements are all equal to zero.
/// </summary>
public static readonly double2x2 Zero = new(0.0);
/// <summary>
/// A 2x2 matrix whose elements are all equal to one.
/// </summary>
public static readonly double2x2 One = new(1.0);
/// <summary>
/// The identity 2x2 matrix.
/// </summary>
public static readonly double2x2 Identity = new(
1.0, 0.0,
0.0, 1.0);
}
/// <summary>
/// A matrix with 3 rows and 3 columns of double-precision floating-point values.
/// </summary>
[Matrix(typeof(double), 3, 3, "double")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(double3))]
[MatrixCast(typeof(float), true)]
public partial struct double3x3
{
/// <summary>
/// A 3x3 matrix whose elements are all equal to zero.
/// </summary>
public static readonly double3x3 Zero = new(0.0);
/// <summary>
/// A 3x3 matrix whose elements are all equal to one.
/// </summary>
public static readonly double3x3 One = new(1.0);
/// <summary>
/// The identity 3x3 matrix.
/// </summary>
public static readonly double3x3 Identity = new(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0);
}
/// <summary>
/// A matrix with 4 rows and 4 columns of double-precision floating-point values.
/// </summary>
[Matrix(typeof(double), 4, 4, "double")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(double4))]
[MatrixCast(typeof(float), true)]
public partial struct double4x4
{
/// <summary>
/// A 4x4 matrix whose elements are all equal to zero.
/// </summary>
public static readonly double4x4 Zero = new(0.0);
/// <summary>
/// A 4x4 matrix whose elements are all equal to one.
/// </summary>
public static readonly double4x4 One = new(1.0);
/// <summary>
/// The identity 4x4 matrix.
/// </summary>
public static readonly double4x4 Identity = new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
}
+125
View File
@@ -0,0 +1,125 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of single-precision floating-point values.
/// </summary>
/// <remarks>
/// The matrix is stored in a column-major order.
/// The positions of the elements is the following:
/// <table>
/// <tr>
/// <td>M11</td> <td>M12</td>
/// </tr>
/// <tr>
/// <td>M21</td> <td>M22</td>
/// </tr>
/// </table>
/// The memory order is M11, M21, M12, M22.
/// </remarks>
[Matrix(typeof(float), 2, 2, "float")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(float2))]
[MatrixCast(typeof(double), true)]
[MatrixCast(typeof(Half), true)]
[MatrixCast(typeof(int), true)]
[MatrixCast(typeof(uint), true)]
public partial struct float2x2
{
/// <summary>
/// A 2x2 matrix whose elements are all equal to zero.
/// </summary>
public static readonly float2x2 Zero = new(0.0f);
/// <summary>
/// A 2x2 matrix whose elements are all equal to one.
/// </summary>
public static readonly float2x2 One = new(1.0f);
/// <summary>
/// The identity 2x2 matrix.
/// </summary>
public static readonly float2x2 Identity = new(
1.0f, 0.0f,
0.0f, 1.0f);
/// <summary>
/// Creates a new 2x2 matrix that represents a rotation by a specified angle.
/// </summary>
/// <param name="angle">The angle of rotation.</param>
/// <returns>The matrix representing the rotation.</returns>
public static float2x2 Rotation(float angle)
{
float cos = Math.cos(angle);
float sin = Math.sin(angle);
return new float2x2(cos, -sin, sin, cos);
}
}
/// <summary>
/// A matrix with 3 rows and 3 columns of single-precision floating-point values.
/// </summary>
[Matrix(typeof(float), 3, 3, "float")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(float3))]
[MatrixCast(typeof(double), true)]
[MatrixCast(typeof(Half), true)]
[MatrixCast(typeof(int), true)]
[MatrixCast(typeof(uint), true)]
public partial struct float3x3
{
/// <summary>
/// A 3x3 matrix whose elements are all equal to zero.
/// </summary>
public static readonly float3x3 Zero = new(0.0f);
/// <summary>
/// A 3x3 matrix whose elements are all equal to one.
/// </summary>
public static readonly float3x3 One = new(1.0f);
/// <summary>
/// The identity 3x3 matrix.
/// </summary>
public static readonly float3x3 Identity = new(
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f);
}
/// <summary>
/// A matrix with 4 rows and 4 columns of single-precision floating-point values.
/// </summary>
[Matrix(typeof(float), 4, 4, "float")]
[ComparableMatrix]
[MatrixMath]
[MatrixVectorMultiplication(typeof(float4))]
[MatrixCast(typeof(double), true)]
[MatrixCast(typeof(Half), true)]
[MatrixCast(typeof(int), true)]
[MatrixCast(typeof(uint), true)]
public partial struct float4x4
{
/// <summary>
/// A 4x4 matrix whose elements are all equal to zero.
/// </summary>
public static readonly float4x4 Zero = new(0.0f);
/// <summary>
/// A 4x4 matrix whose elements are all equal to one.
/// </summary>
public static readonly float4x4 One = new(1.0f);
/// <summary>
/// The identity 4x4 matrix.
/// </summary>
public static readonly float4x4 Identity = new(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
+87
View File
@@ -0,0 +1,87 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of half-precision floating-point values.
/// </summary>
[Matrix(typeof(Half), 2, 2, "half")]
[ComparableMatrix]
[MatrixMath]
[MatrixCast(typeof(float), true)]
public partial struct half2x2
{
/// <summary>
/// A 2x2 matrix whose elements are all equal to zero.
/// </summary>
public static readonly half2x2 Zero = new((Half)0.0f);
/// <summary>
/// A 2x2 matrix whose elements are all equal to one.
/// </summary>
public static readonly half2x2 One = new((Half)1.0f);
/// <summary>
/// The identity 2x2 matrix.
/// </summary>
public static readonly half2x2 Identity = new(
(Half)1.0f, (Half)0.0f,
(Half)0.0f, (Half)1.0f);
}
/// <summary>
/// A matrix with 3 rows and 3 columns of half-precision floating-point values.
/// </summary>
[Matrix(typeof(Half), 3, 3, "half")]
[ComparableMatrix]
[MatrixMath]
[MatrixCast(typeof(float), true)]
public partial struct half3x3
{
/// <summary>
/// A 3x3 matrix whose elements are all equal to zero.
/// </summary>
public static readonly half3x3 Zero = new((Half)0.0f);
/// <summary>
/// A 3x3 matrix whose elements are all equal to one.
/// </summary>
public static readonly half3x3 One = new((Half)1.0f);
/// <summary>
/// The identity 3x3 matrix.
/// </summary>
public static readonly half3x3 Identity = new(
(Half)1.0f, (Half)0.0f, (Half)0.0f,
(Half)0.0f, (Half)1.0f, (Half)0.0f,
(Half)0.0f, (Half)0.0f, (Half)1.0f);
}
/// <summary>
/// A matrix with 4 rows and 4 columns of half-precision floating-point values.
/// </summary>
[Matrix(typeof(Half), 4, 4, "half")]
[ComparableMatrix]
[MatrixMath]
[MatrixCast(typeof(float), true)]
public partial struct half4x4
{
/// <summary>
/// A 4x4 matrix whose elements are all equal to zero.
/// </summary>
public static readonly half4x4 Zero = new((Half)0.0f);
/// <summary>
/// A 4x4 matrix whose elements are all equal to one.
/// </summary>
public static readonly half4x4 One = new((Half)1.0f);
/// <summary>
/// The identity 4x4 matrix.
/// </summary>
public static readonly half4x4 Identity = new(
(Half)1.0f, (Half)0.0f, (Half)0.0f, (Half)0.0f,
(Half)0.0f, (Half)1.0f, (Half)0.0f, (Half)0.0f,
(Half)0.0f, (Half)0.0f, (Half)1.0f, (Half)0.0f,
(Half)0.0f, (Half)0.0f, (Half)0.0f, (Half)1.0f);
}
+36
View File
@@ -0,0 +1,36 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of 32-bit signed integer values.
/// </summary>
[Matrix(typeof(int), 2, 2, "int")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(uint), true)]
public partial struct int2x2
{
}
/// <summary>
/// A matrix with 3 rows and 3 columns of 32-bit signed integer values.
/// </summary>
[Matrix(typeof(int), 3, 3, "int")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(uint), true)]
public partial struct int3x3
{
}
/// <summary>
/// A matrix with 4 rows and 4 columns of 32-bit signed integer values.
/// </summary>
[Matrix(typeof(int), 4, 4, "int")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(uint), true)]
public partial struct int4x4
{
}
+36
View File
@@ -0,0 +1,36 @@
using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math;
/// <summary>
/// A matrix with 2 rows and 2 columns of 32-bit unsigned integer values.
/// </summary>
[Matrix(typeof(uint), 2, 2, "uint")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(int), true)]
public partial struct uint2x2
{
}
/// <summary>
/// A matrix with 3 rows and 3 columns of 32-bit unsigned integer values.
/// </summary>
[Matrix(typeof(uint), 3, 3, "uint")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(int), true)]
public partial struct uint3x3
{
}
/// <summary>
/// A matrix with 4 rows and 4 columns of 32-bit unsigned integer values.
/// </summary>
[Matrix(typeof(uint), 4, 4, "uint")]
[MatrixLogic]
[MatrixCast(typeof(float), true)]
[MatrixCast(typeof(int), true)]
public partial struct uint4x4
{
}