// - edit Matrix.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct double4x4
{
private const double BillboardEpsilon = 1e-4;
private const double BillboardMinAngle = 1.0 - 0.1 * (double.Pi / 180.0); // 0.1 degrees
private const double DecomposeEpsilon = 0.0001;
///
[XmlIgnore]
public double3 Translation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => new(W.X, W.Y, W.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => W = new double4(value, W.W);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateBillboard(
double3 objectPosition,
double3 cameraPosition,
double3 cameraUpVector,
double3 cameraForwardVector
)
{
var axisZ = objectPosition - cameraPosition;
var norm = axisZ.LengthSquared();
if (norm < BillboardEpsilon)
axisZ = -cameraForwardVector;
else
axisZ = double3.Multiply(axisZ, 1.0 / double.Sqrt(norm));
var axisX = double3.Normalize(double3.Cross(cameraUpVector, axisZ));
var axisY = double3.Cross(axisZ, axisX);
double4x4 result;
result.X = new double4(axisX, 0);
result.Y = new double4(axisY, 0);
result.Z = new double4(axisZ, 0);
result.W = new double4(objectPosition, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateConstrainedBillboard(
double3 objectPosition,
double3 cameraPosition,
double3 rotateAxis,
double3 cameraForwardVector,
double3 objectForwardVector
)
{
// Treat the case when object and camera positions are too close.
var faceDir = objectPosition - cameraPosition;
var norm = faceDir.LengthSquared();
if (norm < BillboardEpsilon)
faceDir = -cameraForwardVector;
else
faceDir = double3.Multiply(faceDir, 1.0 / double.Sqrt(norm));
var axisY = rotateAxis;
// Treat the case when angle between faceDir and rotateAxis is too close to 0.
var dot = double3.Dot(axisY, faceDir);
if (double.Abs(dot) > BillboardMinAngle)
{
faceDir = objectForwardVector;
// Make sure passed values are useful for compute.
dot = double3.Dot(axisY, faceDir);
if (double.Abs(dot) > BillboardMinAngle)
faceDir = double.Abs(axisY.Z) > BillboardMinAngle ? double3.UnitX : new double3(0, 0, -1);
}
var axisX = double3.Normalize(double3.Cross(axisY, faceDir));
var axisZ = double3.Normalize(double3.Cross(axisX, axisY));
double4x4 result;
result.X = new double4(axisX, 0);
result.Y = new double4(axisY, 0);
result.Z = new double4(axisZ, 0);
result.W = new double4(objectPosition, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateFromAxisAngle(double3 axis, double angle)
{
var (value, w) = double.SinCos(angle * 0.5);
return CreateFromQuaternion(new doubleQuat(axis.X * value, axis.Y * value, axis.Z * value, w));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateFromQuaternion(doubleQuat quaternion)
{
var xx = quaternion.X * quaternion.X;
var yy = quaternion.Y * quaternion.Y;
var zz = quaternion.Z * quaternion.Z;
var xy = quaternion.X * quaternion.Y;
var wz = quaternion.Z * quaternion.W;
var xz = quaternion.Z * quaternion.X;
var wy = quaternion.Y * quaternion.W;
var yz = quaternion.Y * quaternion.Z;
var wx = quaternion.X * quaternion.W;
double4x4 result;
result.X = new double4(1.0 - 2.0 * (yy + zz), 2.0 * (xy + wz), 2.0 * (xz - wy), 0);
result.Y = new double4(2.0 * (xy - wz), 1.0 - 2.0 * (zz + xx), 2.0 * (yz + wx), 0);
result.Z = new double4(2.0 * (xz + wy), 2.0 * (yz - wx), 1.0 - 2.0 * (yy + xx), 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateFromYawPitchRoll(double yaw, double pitch, double roll)
{
var q = doubleQuat.CreateFromYawPitchRoll(yaw, pitch, roll);
return CreateFromQuaternion(q);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateLookTo(double3 cameraPosition, double3 cameraDirection, double3 cameraUpVector)
{
var axisZ = double3.Normalize(-cameraDirection);
var axisX = double3.Normalize(double3.Cross(cameraUpVector, axisZ));
var axisY = double3.Cross(axisZ, axisX);
var negativeCameraPosition = -cameraPosition;
double4x4 result;
result.X = new double4(axisX.X, axisY.X, axisZ.X, 0);
result.Y = new double4(axisX.Y, axisY.Y, axisZ.Y, 0);
result.Z = new double4(axisX.Z, axisY.Z, axisZ.Z, 0);
result.W = new double4(
double3.Dot(axisX, negativeCameraPosition),
double3.Dot(axisY, negativeCameraPosition),
double3.Dot(axisZ, negativeCameraPosition),
1
);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateLookToLeftHanded(
double3 cameraPosition,
double3 cameraDirection,
double3 cameraUpVector
)
{
var axisZ = double3.Normalize(cameraDirection);
var axisX = double3.Normalize(double3.Cross(cameraUpVector, axisZ));
var axisY = double3.Cross(axisZ, axisX);
var negativeCameraPosition = -cameraPosition;
double4x4 result;
result.X = new double4(axisX.X, axisY.X, axisZ.X, 0);
result.Y = new double4(axisX.Y, axisY.Y, axisZ.Y, 0);
result.Z = new double4(axisX.Z, axisY.Z, axisZ.Z, 0);
result.W = new double4(
double3.Dot(axisX, negativeCameraPosition),
double3.Dot(axisY, negativeCameraPosition),
double3.Dot(axisZ, negativeCameraPosition),
1
);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateOrthographic(double width, double height, double zNearPlane, double zFarPlane)
{
var range = 1.0 / (zNearPlane - zFarPlane);
double4x4 result;
result.X = new double4(2.0 / width, 0, 0, 0);
result.Y = new double4(0, 2.0 / height, 0, 0);
result.Z = new double4(0, 0, range, 0);
result.W = new double4(0, 0, range * zNearPlane, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateOrthographicLeftHanded(
double width,
double height,
double zNearPlane,
double zFarPlane
)
{
var range = 1.0 / (zFarPlane - zNearPlane);
double4x4 result;
result.X = new double4(2.0 / width, 0, 0, 0);
result.Y = new double4(0, 2.0 / height, 0, 0);
result.Z = new double4(0, 0, range, 0);
result.W = new double4(0, 0, -range * zNearPlane, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateOrthographicOffCenter(
double left,
double right,
double bottom,
double top,
double zNearPlane,
double zFarPlane
)
{
var reciprocalWidth = 1.0 / (right - left);
var reciprocalHeight = 1.0 / (top - bottom);
var range = 1.0 / (zNearPlane - zFarPlane);
double4x4 result;
result.X = new double4(reciprocalWidth + reciprocalWidth, 0, 0, 0);
result.Y = new double4(0, reciprocalHeight + reciprocalHeight, 0, 0);
result.Z = new double4(0, 0, range, 0);
result.W = new double4(
-(left + right) * reciprocalWidth,
-(top + bottom) * reciprocalHeight,
range * zNearPlane,
1
);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateOrthographicOffCenterLeftHanded(
double left,
double right,
double bottom,
double top,
double zNearPlane,
double zFarPlane
)
{
var reciprocalWidth = 1.0 / (right - left);
var reciprocalHeight = 1.0 / (top - bottom);
var range = 1.0 / (zFarPlane - zNearPlane);
double4x4 result;
result.X = new double4(reciprocalWidth + reciprocalWidth, 0, 0, 0);
result.Y = new double4(0, reciprocalHeight + reciprocalHeight, 0, 0);
result.Z = new double4(0, 0, range, 0);
result.W = new double4(
-(left + right) * reciprocalWidth,
-(top + bottom) * reciprocalHeight,
-range * zNearPlane,
1
);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspective(
double width,
double height,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var dblNearPlaneDistance = nearPlaneDistance + nearPlaneDistance;
var range = double.IsPositiveInfinity(farPlaneDistance)
? -1.0
: farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
double4x4 result;
result.X = new double4(dblNearPlaneDistance / width, 0, 0, 0);
result.Y = new double4(0, dblNearPlaneDistance / height, 0, 0);
result.Z = new double4(0, 0, range, -1.0);
result.W = new double4(0, 0, range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspectiveLeftHanded(
double width,
double height,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var dblNearPlaneDistance = nearPlaneDistance + nearPlaneDistance;
var range = double.IsPositiveInfinity(farPlaneDistance)
? 1.0
: farPlaneDistance / (farPlaneDistance - nearPlaneDistance);
double4x4 result;
result.X = new double4(dblNearPlaneDistance / width, 0, 0, 0);
result.Y = new double4(0, dblNearPlaneDistance / height, 0, 0);
result.Z = new double4(0, 0, range, 1.0);
result.W = new double4(0, 0, -range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspectiveFieldOfView(
double fieldOfView,
double aspectRatio,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(fieldOfView, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(fieldOfView, double.Pi);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var height = 1.0 / double.Tan(fieldOfView * 0.5);
var width = height / aspectRatio;
var range = double.IsPositiveInfinity(farPlaneDistance)
? -1.0
: farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
double4x4 result;
result.X = new double4(width, 0, 0, 0);
result.Y = new double4(0, height, 0, 0);
result.Z = new double4(0, 0, range, -1.0);
result.W = new double4(0, 0, range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspectiveFieldOfViewLeftHanded(
double fieldOfView,
double aspectRatio,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(fieldOfView, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(fieldOfView, double.Pi);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var height = 1.0 / double.Tan(fieldOfView * 0.5);
var width = height / aspectRatio;
var range = double.IsPositiveInfinity(farPlaneDistance)
? 1.0
: farPlaneDistance / (farPlaneDistance - nearPlaneDistance);
double4x4 result;
result.X = new double4(width, 0, 0, 0);
result.Y = new double4(0, height, 0, 0);
result.Z = new double4(0, 0, range, 1.0);
result.W = new double4(0, 0, -range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspectiveOffCenter(
double left,
double right,
double bottom,
double top,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var dblNearPlaneDistance = nearPlaneDistance + nearPlaneDistance;
var reciprocalWidth = 1.0 / (right - left);
var reciprocalHeight = 1.0 / (top - bottom);
var range = double.IsPositiveInfinity(farPlaneDistance)
? -1.0
: farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
double4x4 result;
result.X = new double4(dblNearPlaneDistance * reciprocalWidth, 0, 0, 0);
result.Y = new double4(0, dblNearPlaneDistance * reciprocalHeight, 0, 0);
result.Z = new double4((left + right) * reciprocalWidth, (top + bottom) * reciprocalHeight, range, -1.0);
result.W = new double4(0, 0, range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreatePerspectiveOffCenterLeftHanded(
double left,
double right,
double bottom,
double top,
double nearPlaneDistance,
double farPlaneDistance
)
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance);
var dblNearPlaneDistance = nearPlaneDistance + nearPlaneDistance;
var reciprocalWidth = 1.0 / (right - left);
var reciprocalHeight = 1.0 / (top - bottom);
var range = double.IsPositiveInfinity(farPlaneDistance)
? 1.0
: farPlaneDistance / (farPlaneDistance - nearPlaneDistance);
double4x4 result;
result.X = new double4(dblNearPlaneDistance * reciprocalWidth, 0, 0, 0);
result.Y = new double4(0, dblNearPlaneDistance * reciprocalHeight, 0, 0);
result.Z = new double4(-(left + right) * reciprocalWidth, -(top + bottom) * reciprocalHeight, range, 1.0);
result.W = new double4(0, 0, -range * nearPlaneDistance, 0);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateReflection(doublePlane value)
{
var plane = doublePlane.Normalize(value);
var vector = plane.Normal * -2.0;
double4x4 result;
result.X = new double4(vector * plane.Normal.X, 0) + double4.UnitX;
result.Y = new double4(vector * plane.Normal.Y, 0) + double4.UnitY;
result.Z = new double4(vector * plane.Normal.Z, 0) + double4.UnitZ;
result.W = new double4(vector * plane.D, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationX(double radians)
{
var (s, c) = double.SinCos(radians);
// [ 1 0 0 0 ]
// [ 0 c s 0 ]
// [ 0 -s c 0 ]
// [ 0 0 0 1 ]
double4x4 result;
result.X = double4.UnitX;
result.Y = new double4(0, c, s, 0);
result.Z = new double4(0, -s, c, 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationX(double radians, double3 centerPoint)
{
var (s, c) = double.SinCos(radians);
var y = centerPoint.Y * (1 - c) + centerPoint.Z * s;
var z = centerPoint.Z * (1 - c) - centerPoint.Y * s;
// [ 1 0 0 0 ]
// [ 0 c s 0 ]
// [ 0 -s c 0 ]
// [ 0 y z 1 ]
double4x4 result;
result.X = double4.UnitX;
result.Y = new double4(0, c, s, 0);
result.Z = new double4(0, -s, c, 0);
result.W = new double4(0, y, z, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationY(double radians)
{
var (s, c) = double.SinCos(radians);
// [ c 0 -s 0 ]
// [ 0 1 0 0 ]
// [ s 0 c 0 ]
// [ 0 0 0 1 ]
double4x4 result;
result.X = new double4(c, 0, -s, 0);
result.Y = double4.UnitY;
result.Z = new double4(s, 0, c, 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationY(double radians, double3 centerPoint)
{
var (s, c) = double.SinCos(radians);
var x = centerPoint.X * (1 - c) - centerPoint.Z * s;
var z = centerPoint.Z * (1 - c) + centerPoint.X * s;
// [ c 0 -s 0 ]
// [ 0 1 0 0 ]
// [ s 0 c 0 ]
// [ x 0 z 1 ]
double4x4 result;
result.X = new double4(c, 0, -s, 0);
result.Y = double4.UnitY;
result.Z = new double4(s, 0, c, 0);
result.W = new double4(x, 0, z, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationZ(double radians)
{
var (s, c) = double.SinCos(radians);
// [ c s 0 0 ]
// [ -s c 0 0 ]
// [ 0 0 1 0 ]
// [ 0 0 0 1 ]
double4x4 result;
result.X = new double4(c, s, 0, 0);
result.Y = new double4(-s, c, 0, 0);
result.Z = double4.UnitZ;
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateRotationZ(double radians, double3 centerPoint)
{
var (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 0 0 ]
// [ -s c 0 0 ]
// [ 0 0 1 0 ]
// [ x y 0 1 ]
double4x4 result;
result.X = new double4(c, s, 0, 0);
result.Y = new double4(-s, c, 0, 0);
result.Z = double4.UnitZ;
result.W = new double4(x, y, 0, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double scaleX, double scaleY, double scaleZ)
{
double4x4 result;
result.X = new double4(scaleX, 0, 0, 0);
result.Y = new double4(0, scaleY, 0, 0);
result.Z = new double4(0, 0, scaleZ, 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double scaleX, double scaleY, double scaleZ, double3 centerPoint)
{
double4x4 result;
result.X = new double4(scaleX, 0, 0, 0);
result.Y = new double4(0, scaleY, 0, 0);
result.Z = new double4(0, 0, scaleZ, 0);
result.W = new double4(centerPoint * (double3.One - new double3(scaleX, scaleY, scaleZ)), 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double3 scales)
{
double4x4 result;
result.X = new double4(scales.X, 0, 0, 0);
result.Y = new double4(0, scales.Y, 0, 0);
result.Z = new double4(0, 0, scales.Z, 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double3 scales, double3 centerPoint)
{
double4x4 result;
result.X = new double4(scales.X, 0, 0, 0);
result.Y = new double4(0, scales.Y, 0, 0);
result.Z = new double4(0, 0, scales.Z, 0);
result.W = new double4(centerPoint * (double3.One - scales), 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double scale)
{
double4x4 result;
result.X = new double4(scale, 0, 0, 0);
result.Y = new double4(0, scale, 0, 0);
result.Z = new double4(0, 0, scale, 0);
result.W = double4.UnitW;
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateScale(double scale, double3 centerPoint)
{
double4x4 result;
result.X = new double4(scale, 0, 0, 0);
result.Y = new double4(0, scale, 0, 0);
result.Z = new double4(0, 0, scale, 0);
result.W = new double4(centerPoint * (double3.One - new double3(scale)), 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateShadow(double3 lightDirection, doublePlane plane)
{
var p = doublePlane.Normalize(plane);
var dot = double3.Dot(lightDirection, p.Normal);
var normal = -p.Normal;
double4x4 result;
result.X = new double4(lightDirection * normal.X, 0) + new double4(dot, 0, 0, 0);
result.Y = new double4(lightDirection * normal.Y, 0) + new double4(0, dot, 0, 0);
result.Z = new double4(lightDirection * normal.Z, 0) + new double4(0, 0, dot, 0);
result.W = new double4(lightDirection * -p.D, dot);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateTranslation(double3 position)
{
double4x4 result;
result.X = double4.UnitX;
result.Y = double4.UnitY;
result.Z = double4.UnitZ;
result.W = new double4(position, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateTranslation(double positionX, double positionY, double positionZ)
{
double4x4 result;
result.X = double4.UnitX;
result.Y = double4.UnitY;
result.Z = double4.UnitZ;
result.W = new double4(positionX, positionY, positionZ, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateViewport(
double x,
double y,
double width,
double height,
double minDepth,
double maxDepth
)
{
double4x4 result;
// 4x SIMD fields to get a lot better codegen
result.W = new double4(width, height, 0, 0);
result.W *= new double4(0.5, 0.5, 0, 0);
result.X = new double4(result.W.X, 0, 0, 0);
result.Y = new double4(0, -result.W.Y, 0, 0);
result.Z = new double4(0, 0, minDepth - maxDepth, 0);
result.W += new double4(x, y, minDepth, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateViewportLeftHanded(
double x,
double y,
double width,
double height,
double minDepth,
double maxDepth
)
{
double4x4 result;
// 4x SIMD fields to get a lot better codegen
result.W = new double4(width, height, 0, 0);
result.W *= new double4(0.5, 0.5, 0, 0);
result.X = new double4(result.W.X, 0, 0, 0);
result.Y = new double4(0, -result.W.Y, 0, 0);
result.Z = new double4(0, 0, maxDepth - minDepth, 0);
result.W += new double4(x, y, minDepth, 1);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 CreateWorld(double3 position, double3 forward, double3 up)
{
var axisZ = double3.Normalize(-forward);
var axisX = double3.Normalize(double3.Cross(up, axisZ));
var axisY = double3.Cross(axisZ, axisX);
double4x4 result;
result.X = new double4(axisX, 0);
result.Y = new double4(axisY, 0);
result.Z = new double4(axisZ, 0);
result.W = new double4(position, 1);
return result;
}
///
// deliberately not AggressiveInlining: ~200 expanded call sites
public static bool Decompose(
double4x4 matrix,
out double3 scale,
out doubleQuat rotation,
out double3 translation
)
{
// well-formed scales stay in registers; anything degenerate falls through to the scalar body
if (Vector256.IsHardwareAccelerated)
{
ref readonly var rows = ref matrix.AsRows();
var basisMask = Vector256.Create(-1, -1, -1, 0).AsDouble();
var x = rows.X & basisMask;
var y = rows.Y & basisMask;
var z = rows.Z & basisMask;
var scaleX = double.Sqrt(Vector256.Dot(x, x));
var scaleY = double.Sqrt(Vector256.Dot(y, y));
var scaleZ = double.Sqrt(Vector256.Dot(z, z));
if (scaleX >= DecomposeEpsilon && scaleY >= DecomposeEpsilon && scaleZ >= DecomposeEpsilon)
{
x /= Vector256.Create(scaleX);
y /= Vector256.Create(scaleY);
z /= Vector256.Create(scaleZ);
var det = Vector256.Dot(x, Cross3(y, z));
if (det < 0)
{
/* handedness flip on the LARGEST-scale axis, matching the scalar body's
* ranking (its `a` is the max; ties resolve to the lower index there and
* here alike): negating any single axis flips det, but which axis carries
* the sign is observable in the outputs */
if (scaleX >= scaleY && scaleX >= scaleZ)
{
scaleX = -scaleX;
x = -x;
}
else if (scaleY >= scaleZ)
{
scaleY = -scaleY;
y = -y;
}
else
{
scaleZ = -scaleZ;
z = -z;
}
det = -det;
}
translation = new double3(matrix.W.X, matrix.W.Y, matrix.W.Z);
scale = new double3(scaleX, scaleY, scaleZ);
det -= 1;
det *= det;
if (DecomposeEpsilon < det)
{
// Non-SRT matrix encountered
rotation = doubleQuat.Identity;
return false;
}
var basis = new Rows(x, y, z, Vector256.Create(0.0, 0, 0, 1));
rotation = doubleQuat.CreateFromRotationMatrix(Unsafe.BitCast(basis));
return true;
}
}
return DecomposeDegenerate(in matrix, out scale, out rotation, out translation);
/* Cross product of two rows' xyz, lane 3 riding along (zero in, zero out) - the
* register-resident twin of double3.Cross. Local because the handedness
* test is its only caller. */
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Vector256 Cross3(Vector256 a, Vector256 b)
{
var ayzx = Vector256.ShuffleNative(a, Vector256.Create(1, 2, 0, 3));
var azxy = Vector256.ShuffleNative(a, Vector256.Create(2, 0, 1, 3));
var byzx = Vector256.ShuffleNative(b, Vector256.Create(1, 2, 0, 3));
var bzxy = Vector256.ShuffleNative(b, Vector256.Create(2, 0, 1, 3));
return ayzx * bzxy - azxy * byzx;
}
}
// out of line deliberately: its `fixed` pins the out params, which the fast path must not pay for
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe bool DecomposeDegenerate(
in double4x4 matrix,
out double3 scale,
out doubleQuat rotation,
out double3 translation
)
{
var result = true;
fixed (double3* scaleBase = &scale)
{
var pfScales = (double*)scaleBase;
double det;
VectorBasis vectorBasis;
var pVectorBasis = (double3**)&vectorBasis;
var matTemp = Identity;
CanonicalBasis canonicalBasis = default;
var pCanonicalBasis = &canonicalBasis.Row0;
canonicalBasis.Row0 = new double3(1.0, 0.0, 0.0);
canonicalBasis.Row1 = new double3(0.0, 1.0, 0.0);
canonicalBasis.Row2 = new double3(0.0, 0.0, 1.0);
translation = new double3(matrix.W.X, matrix.W.Y, matrix.W.Z);
pVectorBasis[0] = (double3*)&matTemp.X;
pVectorBasis[1] = (double3*)&matTemp.Y;
pVectorBasis[2] = (double3*)&matTemp.Z;
*pVectorBasis[0] = new double3(matrix.X.X, matrix.X.Y, matrix.X.Z);
*pVectorBasis[1] = new double3(matrix.Y.X, matrix.Y.Y, matrix.Y.Z);
*pVectorBasis[2] = new double3(matrix.Z.X, matrix.Z.Y, matrix.Z.Z);
scale.X = pVectorBasis[0]->Length();
scale.Y = pVectorBasis[1]->Length();
scale.Z = pVectorBasis[2]->Length();
uint a,
b,
c;
#region Ranking
var x = pfScales[0];
var y = pfScales[1];
var z = pfScales[2];
if (x < y)
{
if (y < z)
{
a = 2;
b = 1;
c = 0;
}
else
{
a = 1;
if (x < z)
{
b = 2;
c = 0;
}
else
{
b = 0;
c = 2;
}
}
}
else
{
if (x < z)
{
a = 2;
b = 0;
c = 1;
}
else
{
a = 0;
if (y < z)
{
b = 2;
c = 1;
}
else
{
b = 1;
c = 2;
}
}
}
#endregion
if (pfScales[a] < DecomposeEpsilon)
*pVectorBasis[a] = pCanonicalBasis[a];
*pVectorBasis[a] = double3.Normalize(*pVectorBasis[a]);
if (pfScales[b] < DecomposeEpsilon)
{
uint cc;
double fAbsX,
fAbsY,
fAbsZ;
fAbsX = double.Abs(pVectorBasis[a]->X);
fAbsY = double.Abs(pVectorBasis[a]->Y);
fAbsZ = double.Abs(pVectorBasis[a]->Z);
#region Ranking
if (fAbsX < fAbsY)
{
if (fAbsY < fAbsZ)
{
cc = 0;
}
else
{
if (fAbsX < fAbsZ)
cc = 0;
else
cc = 2;
}
}
else
{
if (fAbsX < fAbsZ)
{
cc = 1;
}
else
{
if (fAbsY < fAbsZ)
cc = 1;
else
cc = 2;
}
}
#endregion
*pVectorBasis[b] = double3.Cross(*pVectorBasis[a], *(pCanonicalBasis + cc));
}
*pVectorBasis[b] = double3.Normalize(*pVectorBasis[b]);
if (pfScales[c] < DecomposeEpsilon)
*pVectorBasis[c] = double3.Cross(*pVectorBasis[a], *pVectorBasis[b]);
*pVectorBasis[c] = double3.Normalize(*pVectorBasis[c]);
det = matTemp.GetDeterminant();
// use Kramer's rule to check for handedness of coordinate system
if (det < 0.0)
{
// switch coordinate system by negating the scale and inverting the basis vector on the x-axis
pfScales[a] = -pfScales[a];
*pVectorBasis[a] = -(*pVectorBasis[a]);
det = -det;
}
det -= 1.0;
det *= det;
if (DecomposeEpsilon < det)
{
// Non-SRT matrix encountered
rotation = doubleQuat.Identity;
result = false;
}
else
{
// generate the quaternion from the matrix
rotation = doubleQuat.CreateFromRotationMatrix(matTemp);
}
}
return result;
}
///
// deliberately not AggressiveInlining: ~330 expanded call sites
public static bool Invert(double4x4 matrix, out double4x4 result)
{
// This implementation is based on the DirectX Math Library XMMatrixInverse method
// https://github.com/microsoft/DirectXMath/blob/master/Inc/DirectXMathMatrix.inl
// the BCL SseImpl respelled onto portable shuffles; fused-estimate as in the original,
// so results may differ by an ulp between FMA and non-FMA hardware
if (Vector256.IsHardwareAccelerated)
{
var transposed = TransposeKernel(in matrix);
ref readonly var t = ref transposed.AsRows();
var row1 = t.X;
var row2 = t.Y;
var row3 = t.Z;
var row4 = t.W;
var V00 = Vector256.ShuffleNative(row3, Vector256.Create(0, 0, 1, 1));
var V10 = Vector256.ShuffleNative(row4, Vector256.Create(2, 3, 2, 3));
var V01 = Vector256.ShuffleNative(row1, Vector256.Create(0, 0, 1, 1));
var V11 = Vector256.ShuffleNative(row2, Vector256.Create(2, 3, 2, 3));
var V02 = Vector256.Shuffle2(row3, row1, 0, 2, 0, 2);
var V12 = Vector256.Shuffle2(row4, row2, 1, 3, 1, 3);
var D0 = V00 * V10;
var D1 = V01 * V11;
var D2 = V02 * V12;
V00 = Vector256.ShuffleNative(row3, Vector256.Create(2, 3, 2, 3));
V10 = Vector256.ShuffleNative(row4, Vector256.Create(0, 0, 1, 1));
V01 = Vector256.ShuffleNative(row1, Vector256.Create(2, 3, 2, 3));
V11 = Vector256.ShuffleNative(row2, Vector256.Create(0, 0, 1, 1));
V02 = Vector256.Shuffle2(row3, row1, 1, 3, 1, 3);
V12 = Vector256.Shuffle2(row4, row2, 0, 2, 0, 2);
D0 = Vector256.MultiplyAddEstimate(-V00, V10, D0);
D1 = Vector256.MultiplyAddEstimate(-V01, V11, D1);
D2 = Vector256.MultiplyAddEstimate(-V02, V12, D2);
// V11 = D0Y,D0W,D2Y,D2Y
V11 = Vector256.Shuffle2(D0, D2, 1, 3, 1, 1);
V00 = Vector256.ShuffleNative(row2, Vector256.Create(1, 2, 0, 1));
V10 = Vector256.Shuffle2(V11, D0, 2, 0, 3, 0);
V01 = Vector256.ShuffleNative(row1, Vector256.Create(2, 0, 1, 0));
V11 = Vector256.Shuffle2(V11, D0, 1, 2, 1, 2);
// V13 = D1Y,D1W,D2W,D2W
var V13 = Vector256.Shuffle2(D1, D2, 1, 3, 3, 3);
V02 = Vector256.ShuffleNative(row4, Vector256.Create(1, 2, 0, 1));
V12 = Vector256.Shuffle2(V13, D1, 2, 0, 3, 0);
var V03 = Vector256.ShuffleNative(row3, Vector256.Create(2, 0, 1, 0));
V13 = Vector256.Shuffle2(V13, D1, 1, 2, 1, 2);
var C0 = V00 * V10;
var C2 = V01 * V11;
var C4 = V02 * V12;
var C6 = V03 * V13;
// V11 = D0X,D0Y,D2X,D2X
V11 = Vector256.Shuffle2(D0, D2, 0, 1, 0, 0);
V00 = Vector256.ShuffleNative(row2, Vector256.Create(2, 3, 1, 2));
V10 = Vector256.Shuffle2(D0, V11, 3, 0, 1, 2);
V01 = Vector256.ShuffleNative(row1, Vector256.Create(3, 2, 3, 1));
V11 = Vector256.Shuffle2(D0, V11, 2, 1, 2, 0);
// V13 = D1X,D1Y,D2Z,D2Z
V13 = Vector256.Shuffle2(D1, D2, 0, 1, 2, 2);
V02 = Vector256.ShuffleNative(row4, Vector256.Create(2, 3, 1, 2));
V12 = Vector256.Shuffle2(D1, V13, 3, 0, 1, 2);
V03 = Vector256.ShuffleNative(row3, Vector256.Create(3, 2, 3, 1));
V13 = Vector256.Shuffle2(D1, V13, 2, 1, 2, 0);
C0 = Vector256.MultiplyAddEstimate(-V00, V10, C0);
C2 = Vector256.MultiplyAddEstimate(-V01, V11, C2);
C4 = Vector256.MultiplyAddEstimate(-V02, V12, C4);
C6 = Vector256.MultiplyAddEstimate(-V03, V13, C6);
V00 = Vector256.ShuffleNative(row2, Vector256.Create(3, 0, 3, 0));
// V10 = D0Z,D0Z,D2X,D2Y
V10 = Vector256.Shuffle2(D0, D2, 2, 2, 0, 1);
V10 = Vector256.ShuffleNative(V10, Vector256.Create(0, 3, 2, 0));
V01 = Vector256.ShuffleNative(row1, Vector256.Create(1, 3, 0, 2));
// V11 = D0X,D0W,D2X,D2Y
V11 = Vector256.Shuffle2(D0, D2, 0, 3, 0, 1);
V11 = Vector256.ShuffleNative(V11, Vector256.Create(3, 0, 1, 2));
V02 = Vector256.ShuffleNative(row4, Vector256.Create(3, 0, 3, 0));
// V12 = D1Z,D1Z,D2Z,D2W
V12 = Vector256.Shuffle2(D1, D2, 2, 2, 2, 3);
V12 = Vector256.ShuffleNative(V12, Vector256.Create(0, 3, 2, 0));
V03 = Vector256.ShuffleNative(row3, Vector256.Create(1, 3, 0, 2));
// V13 = D1X,D1W,D2Z,D2W
V13 = Vector256.Shuffle2(D1, D2, 0, 3, 2, 3);
V13 = Vector256.ShuffleNative(V13, Vector256.Create(3, 0, 1, 2));
V00 *= V10;
V01 *= V11;
V02 *= V12;
V03 *= V13;
var C1 = C0 - V00;
C0 += V00;
var C3 = C2 + V01;
C2 -= V01;
var C5 = C4 - V02;
C4 += V02;
var C7 = C6 + V03;
C6 -= V03;
C0 = Vector256.Shuffle2(C0, C1, 0, 2, 1, 3);
C2 = Vector256.Shuffle2(C2, C3, 0, 2, 1, 3);
C4 = Vector256.Shuffle2(C4, C5, 0, 2, 1, 3);
C6 = Vector256.Shuffle2(C6, C7, 0, 2, 1, 3);
C0 = Vector256.ShuffleNative(C0, Vector256.Create(0, 2, 1, 3));
C2 = Vector256.ShuffleNative(C2, Vector256.Create(0, 2, 1, 3));
C4 = Vector256.ShuffleNative(C4, Vector256.Create(0, 2, 1, 3));
C6 = Vector256.ShuffleNative(C6, Vector256.Create(0, 2, 1, 3));
var det = Vector256.Dot(C0, row1);
if (double.Abs(det) < double.Epsilon)
{
var vNaN = Vector256.Create(double.NaN);
var nan = new Rows(vNaN, vNaN, vNaN, vNaN);
result = Unsafe.BitCast(nan);
return false;
}
var invDet = Vector256.One / det;
var inverted = new Rows(C0 * invDet, C2 * invDet, C4 * invDet, C6 * invDet);
result = Unsafe.BitCast(inverted);
return true;
}
return SoftwareFallback(in matrix, out result);
static bool SoftwareFallback(in double4x4 matrix, out double4x4 result)
{
// -1
// If you have matrix M, inverse Matrix M can compute
//
// -1 1
// M = --------- A
// det(M)
//
// A is adjugate (adjoint) of M, where,
//
// T
// A = C
//
// C is Cofactor matrix of M, where,
// i + j
// C = (-1) * det(M )
// ij ij
//
// [ a b c d ]
// M = [ e f g h ]
// [ i j k l ]
// [ m n o p ]
//
// First Row
// 2 | f g h |
// C = (-1) | j k l | = + ( f ( kp - lo ) - g ( jp - ln ) + h ( jo - kn ) )
// 11 | n o p |
//
// 3 | e g h |
// C = (-1) | i k l | = - ( e ( kp - lo ) - g ( ip - lm ) + h ( io - km ) )
// 12 | m o p |
//
// 4 | e f h |
// C = (-1) | i j l | = + ( e ( jp - ln ) - f ( ip - lm ) + h ( in - jm ) )
// 13 | m n p |
//
// 5 | e f g |
// C = (-1) | i j k | = - ( e ( jo - kn ) - f ( io - km ) + g ( in - jm ) )
// 14 | m n o |
//
// Second Row
// 3 | b c d |
// C = (-1) | j k l | = - ( b ( kp - lo ) - c ( jp - ln ) + d ( jo - kn ) )
// 21 | n o p |
//
// 4 | a c d |
// C = (-1) | i k l | = + ( a ( kp - lo ) - c ( ip - lm ) + d ( io - km ) )
// 22 | m o p |
//
// 5 | a b d |
// C = (-1) | i j l | = - ( a ( jp - ln ) - b ( ip - lm ) + d ( in - jm ) )
// 23 | m n p |
//
// 6 | a b c |
// C = (-1) | i j k | = + ( a ( jo - kn ) - b ( io - km ) + c ( in - jm ) )
// 24 | m n o |
//
// Third Row
// 4 | b c d |
// C = (-1) | f g h | = + ( b ( gp - ho ) - c ( fp - hn ) + d ( fo - gn ) )
// 31 | n o p |
//
// 5 | a c d |
// C = (-1) | e g h | = - ( a ( gp - ho ) - c ( ep - hm ) + d ( eo - gm ) )
// 32 | m o p |
//
// 6 | a b d |
// C = (-1) | e f h | = + ( a ( fp - hn ) - b ( ep - hm ) + d ( en - fm ) )
// 33 | m n p |
//
// 7 | a b c |
// C = (-1) | e f g | = - ( a ( fo - gn ) - b ( eo - gm ) + c ( en - fm ) )
// 34 | m n o |
//
// Fourth Row
// 5 | b c d |
// C = (-1) | f g h | = - ( b ( gl - hk ) - c ( fl - hj ) + d ( fk - gj ) )
// 41 | j k l |
//
// 6 | a c d |
// C = (-1) | e g h | = + ( a ( gl - hk ) - c ( el - hi ) + d ( ek - gi ) )
// 42 | i k l |
//
// 7 | a b d |
// C = (-1) | e f h | = - ( a ( fl - hj ) - b ( el - hi ) + d ( ej - fi ) )
// 43 | i j l |
//
// 8 | a b c |
// C = (-1) | e f g | = + ( a ( fk - gj ) - b ( ek - gi ) + c ( ej - fi ) )
// 44 | i j k |
//
// Cost of operation
// 53 adds, 104 muls, and 1 div.
double a = matrix.X.X,
b = matrix.X.Y,
c = matrix.X.Z,
d = matrix.X.W;
double e = matrix.Y.X,
f = matrix.Y.Y,
g = matrix.Y.Z,
h = matrix.Y.W;
double i = matrix.Z.X,
j = matrix.Z.Y,
k = matrix.Z.Z,
l = matrix.Z.W;
double m = matrix.W.X,
n = matrix.W.Y,
o = matrix.W.Z,
p = matrix.W.W;
var kp_lo = k * p - l * o;
var jp_ln = j * p - l * n;
var jo_kn = j * o - k * n;
var ip_lm = i * p - l * m;
var io_km = i * o - k * m;
var in_jm = i * n - j * m;
var a11 = +(f * kp_lo - g * jp_ln + h * jo_kn);
var a12 = -(e * kp_lo - g * ip_lm + h * io_km);
var a13 = +(e * jp_ln - f * ip_lm + h * in_jm);
var a14 = -(e * jo_kn - f * io_km + g * in_jm);
var det = a * a11 + b * a12 + c * a13 + d * a14;
if (double.Abs(det) < double.Epsilon)
{
var vNaN = new double4(double.NaN);
result.X = vNaN;
result.Y = vNaN;
result.Z = vNaN;
result.W = vNaN;
return false;
}
var invDet = 1.0 / det;
result.X.X = a11 * invDet;
result.Y.X = a12 * invDet;
result.Z.X = a13 * invDet;
result.W.X = a14 * invDet;
result.X.Y = -(b * kp_lo - c * jp_ln + d * jo_kn) * invDet;
result.Y.Y = +(a * kp_lo - c * ip_lm + d * io_km) * invDet;
result.Z.Y = -(a * jp_ln - b * ip_lm + d * in_jm) * invDet;
result.W.Y = +(a * jo_kn - b * io_km + c * in_jm) * invDet;
var gp_ho = g * p - h * o;
var fp_hn = f * p - h * n;
var fo_gn = f * o - g * n;
var ep_hm = e * p - h * m;
var eo_gm = e * o - g * m;
var en_fm = e * n - f * m;
result.X.Z = +(b * gp_ho - c * fp_hn + d * fo_gn) * invDet;
result.Y.Z = -(a * gp_ho - c * ep_hm + d * eo_gm) * invDet;
result.Z.Z = +(a * fp_hn - b * ep_hm + d * en_fm) * invDet;
result.W.Z = -(a * fo_gn - b * eo_gm + c * en_fm) * invDet;
var gl_hk = g * l - h * k;
var fl_hj = f * l - h * j;
var fk_gj = f * k - g * j;
var el_hi = e * l - h * i;
var ek_gi = e * k - g * i;
var ej_fi = e * j - f * i;
result.X.W = -(b * gl_hk - c * fl_hj + d * fk_gj) * invDet;
result.Y.W = +(a * gl_hk - c * el_hi + d * ek_gi) * invDet;
result.Z.W = -(a * fl_hj - b * el_hi + d * ej_fi) * invDet;
result.W.W = +(a * fk_gj - b * ek_gi + c * ej_fi) * invDet;
return true;
}
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Lerp(double4x4 a, double4x4 b, double amount)
{
double4x4 result;
result.X = double4.Lerp(a.X, b.X, amount);
result.Y = double4.Lerp(a.Y, b.Y, amount);
result.Z = double4.Lerp(a.Z, b.Z, amount);
result.W = double4.Lerp(a.W, b.W, amount);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Transform(double4x4 value, doubleQuat rotation)
{
// Compute rotation matrix.
var x2 = rotation.X + rotation.X;
var y2 = rotation.Y + rotation.Y;
var z2 = rotation.Z + rotation.Z;
var wx2 = rotation.W * x2;
var wy2 = rotation.W * y2;
var wz2 = rotation.W * z2;
var xx2 = rotation.X * x2;
var xy2 = rotation.X * y2;
var xz2 = rotation.X * z2;
var yy2 = rotation.Y * y2;
var yz2 = rotation.Y * z2;
var zz2 = rotation.Z * z2;
var q11 = 1.0 - yy2 - zz2;
var q21 = xy2 - wz2;
var q31 = xz2 + wy2;
var q12 = xy2 + wz2;
var q22 = 1.0 - xx2 - zz2;
var q32 = yz2 - wx2;
var q13 = xz2 - wy2;
var q23 = yz2 + wx2;
var q33 = 1.0 - xx2 - yy2;
double4x4 result;
result.X = new double4(
value.X.X * q11 + value.X.Y * q21 + value.X.Z * q31,
value.X.X * q12 + value.X.Y * q22 + value.X.Z * q32,
value.X.X * q13 + value.X.Y * q23 + value.X.Z * q33,
value.X.W
);
result.Y = new double4(
value.Y.X * q11 + value.Y.Y * q21 + value.Y.Z * q31,
value.Y.X * q12 + value.Y.Y * q22 + value.Y.Z * q32,
value.Y.X * q13 + value.Y.Y * q23 + value.Y.Z * q33,
value.Y.W
);
result.Z = new double4(
value.Z.X * q11 + value.Z.Y * q21 + value.Z.Z * q31,
value.Z.X * q12 + value.Z.Y * q22 + value.Z.Z * q32,
value.Z.X * q13 + value.Z.Y * q23 + value.Z.Z * q33,
value.Z.W
);
result.W = new double4(
value.W.X * q11 + value.W.Y * q21 + value.W.Z * q31,
value.W.X * q12 + value.W.Y * q22 + value.W.Z * q32,
value.W.X * q13 + value.W.Y * q23 + value.W.Z * q33,
value.W.W
);
return result;
}
///
public static double4 Transform(double4x4 matrix, double4 vector)
{
return Multiply(matrix, vector);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Transpose(double4x4 matrix)
{
return TransposeKernel(in matrix);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly double GetDeterminant()
{
// | a b c d | | f g h | | e g h | | e f h | | e f g |
// | e f g h | = a | j k l | - b | i k l | + c | i j l | - d | i j k |
// | i j k l | | n o p | | m o p | | m n p | | m n o |
// | m n o p |
//
// | f g h |
// a | j k l | = a ( f ( kp - lo ) - g ( jp - ln ) + h ( jo - kn ) )
// | n o p |
//
// | e g h |
// b | i k l | = b ( e ( kp - lo ) - g ( ip - lm ) + h ( io - km ) )
// | m o p |
//
// | e f h |
// c | i j l | = c ( e ( jp - ln ) - f ( ip - lm ) + h ( in - jm ) )
// | m n p |
//
// | e f g |
// d | i j k | = d ( e ( jo - kn ) - f ( io - km ) + g ( in - jm ) )
// | m n o |
//
// Cost of operation
// 17 adds and 28 muls.
//
// add: 6 + 8 + 3 = 17
// mul: 12 + 16 = 28
double a = X.X,
b = X.Y,
c = X.Z,
d = X.W;
double e = Y.X,
f = Y.Y,
g = Y.Z,
h = Y.W;
double i = Z.X,
j = Z.Y,
k = Z.Z,
l = Z.W;
double m = W.X,
n = W.Y,
o = W.Z,
p = W.W;
var kp_lo = k * p - l * o;
var jp_ln = j * p - l * n;
var jo_kn = j * o - k * n;
var ip_lm = i * p - l * m;
var io_km = i * o - k * m;
var in_jm = i * n - j * m;
return a * (f * kp_lo - g * jp_ln + h * jo_kn)
- b * (e * kp_lo - g * ip_lm + h * io_km)
+ c * (e * jp_ln - f * ip_lm + h * in_jm)
- d * (e * jo_kn - f * io_km + g * in_jm);
}
private struct CanonicalBasis
{
public double3 Row0;
public double3 Row1;
public double3 Row2;
}
private unsafe struct VectorBasis
{
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
public double3* Element0;
public double3* Element1;
public double3* Element2;
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Add(double4x4 a, double4x4 b)
{
return a + b;
}
///
public static double4x4 Multiply(double4x4 a, double4x4 b)
{
return a * b;
}
/// Performs matrix multiplication to multiply a matrix by a vector to compute the product.
/// The matrix.
/// The vector.
/// The product vector.
///
/// The method defines the operation
/// of the multiplication operator for objects.
///
public static double4 Multiply(double4x4 matrix, double4 vector)
{
double4 result;
result.X = double4.Dot(matrix.X, vector);
result.Y = double4.Dot(matrix.Y, vector);
result.Z = double4.Dot(matrix.Z, vector);
result.W = double4.Dot(matrix.W, vector);
return result;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Multiply(double4x4 a, double b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Negate(double4x4 value)
{
return -value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4x4 Subtract(double4x4 a, double4x4 b)
{
return a - b;
}
///
public static double4x4 CreateLookAt(double3 cameraPosition, double3 cameraTarget, double3 cameraUpVector)
{
var cameraDirection = cameraTarget - cameraPosition;
return CreateLookTo(cameraPosition, cameraDirection, cameraUpVector);
}
///
public static double4x4 CreateLookAtLeftHanded(double3 cameraPosition, double3 cameraTarget, double3 cameraUpVector)
{
var cameraDirection = cameraTarget - cameraPosition;
return CreateLookToLeftHanded(cameraPosition, cameraDirection, cameraUpVector);
}
}