brutal/Brutal.Numerics/Matrix/double3x2/double3x2.numerics.gen.cs
Rian Drake b578433e37 first
2026-10-15 17:10:50 +13:00

381 lines
11 KiB
C#

// <auto-generated /> - edit Matrix.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct double3x2
{
private const double RotationEpsilon = 0.001 * double.Pi / 180; // 0.1% of a degree
/// <inheritdoc cref="Matrix3x2.Translation" />
[XmlIgnore]
public double2 Translation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Z;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Z = value;
}
/// <inheritdoc cref="Matrix3x2.CreateRotation(float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateRotation(double radians)
{
radians = double.Ieee754Remainder(radians, double.Pi * 2);
double c;
double s;
if (radians > -RotationEpsilon && radians < RotationEpsilon)
{
// Exact case for zero rotation.
c = 1;
s = 0;
}
else if (radians > double.Pi / 2 - RotationEpsilon && radians < double.Pi / 2 + RotationEpsilon)
{
// Exact case for 90 degree rotation.
c = 0;
s = 1;
}
else if (radians < -double.Pi + RotationEpsilon || radians > double.Pi - RotationEpsilon)
{
// Exact case for 180 degree rotation.
c = -1;
s = 0;
}
else if (radians > -double.Pi / 2 - RotationEpsilon && radians < -double.Pi / 2 + RotationEpsilon)
{
// Exact case for 270 degree rotation.
c = 0;
s = -1;
}
else
{
// Arbitrary rotation.
(s, c) = double.SinCos(radians);
}
// [ c s ]
// [ -s c ]
// [ 0 0 ]
double3x2 result;
result.X = new double2(c, s);
result.Y = new double2(-s, c);
result.Z = double2.Zero;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateRotation(float, Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateRotation(double radians, double2 centerPoint)
{
radians = double.Ieee754Remainder(radians, double.Pi * 2);
double c,
s;
if (radians > -RotationEpsilon && radians < RotationEpsilon)
{
// Exact case for zero rotation.
c = 1;
s = 0;
}
else if (radians > double.Pi / 2 - RotationEpsilon && radians < double.Pi / 2 + RotationEpsilon)
{
// Exact case for 90 degree rotation.
c = 0;
s = 1;
}
else if (radians < -double.Pi + RotationEpsilon || radians > double.Pi - RotationEpsilon)
{
// Exact case for 180 degree rotation.
c = -1;
s = 0;
}
else if (radians > -double.Pi / 2 - RotationEpsilon && radians < -double.Pi / 2 + RotationEpsilon)
{
// Exact case for 270 degree rotation.
c = 0;
s = -1;
}
else
{
// Arbitrary rotation.
(s, c) = double.SinCos(radians);
}
var x = centerPoint.X * (1 - c) + centerPoint.Y * s;
var y = centerPoint.Y * (1 - c) - centerPoint.X * s;
// [ c s ]
// [ -s c ]
// [ x y ]
double3x2 result;
result.X = new double2(c, s);
result.Y = new double2(-s, c);
result.Z = new double2(x, y);
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double2 scales)
{
double3x2 result;
result.X = new double2(scales.X, 0);
result.Y = new double2(0, scales.Y);
result.Z = double2.Zero;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double scaleX, double scaleY)
{
double3x2 result;
result.X = new double2(scaleX, 0);
result.Y = new double2(0, scaleY);
result.Z = double2.Zero;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(float, float, Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double scaleX, double scaleY, double2 centerPoint)
{
double3x2 result;
result.X = new double2(scaleX, 0);
result.Y = new double2(0, scaleY);
result.Z = centerPoint * (double2.One - new double2(scaleX, scaleY));
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(Vector2, Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double2 scales, double2 centerPoint)
{
double3x2 result;
result.X = new double2(scales.X, 0);
result.Y = new double2(0, scales.Y);
result.Z = centerPoint * (double2.One - scales);
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double scale)
{
double3x2 result;
result.X = new double2(scale, 0);
result.Y = new double2(0, scale);
result.Z = double2.Zero;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateScale(float, Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateScale(double scale, double2 centerPoint)
{
double3x2 result;
result.X = new double2(scale, 0);
result.Y = new double2(0, scale);
result.Z = centerPoint * (double2.One - new double2(scale));
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateSkew(double radiansX, double radiansY)
{
double3x2 result;
result.X = new double2(1, double.Tan(radiansY));
result.Y = new double2(double.Tan(radiansX), 1);
result.Z = double2.Zero;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float, Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateSkew(double radiansX, double radiansY, double2 centerPoint)
{
var xTan = double.Tan(radiansX);
var yTan = double.Tan(radiansY);
var tx = -centerPoint.Y * xTan;
var ty = -centerPoint.X * yTan;
double3x2 result;
result.X = new double2(1, yTan);
result.Y = new double2(xTan, 1);
result.Z = new double2(tx, ty);
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateTranslation(Vector2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateTranslation(double2 position)
{
double3x2 result;
result.X = double2.UnitX;
result.Y = double2.UnitY;
result.Z = position;
return result;
}
/// <inheritdoc cref="Matrix3x2.CreateTranslation(float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 CreateTranslation(double positionX, double positionY)
{
double3x2 result;
result.X = double2.UnitX;
result.Y = double2.UnitY;
result.Z = new double2(positionX, positionY);
return result;
}
/// <inheritdoc cref="Matrix3x2.Invert(Matrix3x2, out Matrix3x2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Invert(double3x2 matrix, out double3x2 result)
{
var det = matrix.X.X * matrix.Y.Y - matrix.Y.X * matrix.X.Y;
if (double.Abs(det) < double.Epsilon)
{
var vNaN = new double2(double.NaN);
result.X = vNaN;
result.Y = vNaN;
result.Z = vNaN;
return false;
}
var invDet = 1.0 / det;
result.X = new double2(+matrix.Y.Y * invDet, -matrix.X.Y * invDet);
result.Y = new double2(-matrix.Y.X * invDet, +matrix.X.X * invDet);
result.Z = new double2(
(matrix.Y.X * matrix.Z.Y - matrix.Z.X * matrix.Y.Y) * invDet,
(matrix.Z.X * matrix.X.Y - matrix.X.X * matrix.Z.Y) * invDet
);
return true;
}
/// <inheritdoc cref="Matrix3x2.Lerp(Matrix3x2, Matrix3x2, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Lerp(double3x2 a, double3x2 b, double amount)
{
double3x2 result;
result.X = double2.Lerp(a.X, b.X, amount);
result.Y = double2.Lerp(a.Y, b.Y, amount);
result.Z = double2.Lerp(a.Z, b.Z, amount);
return result;
}
/// <inheritdoc cref="Matrix3x2.GetDeterminant()" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly double GetDeterminant()
{
// There isn't actually any such thing as a determinant for a non-square matrix,
// but this 3x2 type is really just an optimization of a 3x3 where we happen to
// know the rightmost column is always (0, 0, 1). So we expand to 3x3 format:
//
// [ X.X, X.Y, 0 ]
// [ Y.X, Y.Y, 0 ]
// [ Z.X, Z.Y, 1 ]
//
// Sum the diagonal products:
// (X.X * Y.Y * 1) + (X.Y * 0 * Z.X) + (0 * Y.X * Z.Y)
//
// Subtract the opposite diagonal products:
// (Z.X * Y.Y * 0) + (Z.Y * 0 * X.X) + (1 * Y.X * X.Y)
//
// Collapse out the constants and oh look, this is just a 2x2 determinant!
return X.X * Y.Y - Y.X * X.Y;
}
/// <summary>Performs matrix multiplication to multiply a matrix by a vector to compute the product.</summary>
/// <param name="matrix">The matrix.</param>
/// <param name="vector">The vector.</param>
/// <returns>The product vector.</returns>
/// <remarks>
/// The <see cref="Multiply(double3x2, double2)" /> method defines the operation
/// of the multiplication operator for <see cref="double3x2" /> objects.
/// </remarks>
public static double3 Multiply(double3x2 matrix, double2 vector)
{
double3 result;
result.X = double2.Dot(matrix.X, vector);
result.Y = double2.Dot(matrix.Y, vector);
result.Z = double2.Dot(matrix.Z, vector);
return result;
}
/// <inheritdoc cref="Matrix3x2.Add(Matrix3x2, Matrix3x2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Add(double3x2 a, double3x2 b)
{
return a + b;
}
/// <inheritdoc cref="Matrix3x2.Multiply(Matrix3x2, Matrix3x2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Multiply(double3x2 a, double3x2 b)
{
return a * b;
}
/// <inheritdoc cref="Matrix3x2.Multiply(Matrix3x2, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Multiply(double3x2 a, double b)
{
return a * b;
}
/// <inheritdoc cref="Matrix3x2.Negate(Matrix3x2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Negate(double3x2 value)
{
return -value;
}
/// <inheritdoc cref="Matrix3x2.Subtract(Matrix3x2, Matrix3x2)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3x2 Subtract(double3x2 a, double3x2 b)
{
return a - b;
}
}