Struct Fixed3x3
- Namespace
- FixedMathSharp
- Assembly
- FixedMathSharp.dll
Represents a 3x3 matrix used for linear transformations in 2D and 3D space, such as rotation, scaling, and shearing.
[MemoryPackable(GenerateType.Object)]
public struct Fixed3x3 : IEquatable<Fixed3x3>, ISpanFormattable, IFormattable, IMemoryPackable<Fixed3x3>, IMemoryPackFormatterRegister
- Implements
-
IMemoryPackable<Fixed3x3>IMemoryPackFormatterRegister
- Inherited Members
- Extension Methods
Remarks
A 3x3 matrix handles only linear transformations and is typically used when translation is not needed. It operates on directions, orientations, and vectors within a given space without affecting position. This matrix is more lightweight compared to a 4x4 matrix, making it ideal when translation and perspective are unnecessary.
Use Cases:
- Rotating or scaling objects around the origin in 2D and 3D space.
- Transforming vectors and normals (e.g., in lighting calculations).
- Used in physics engines for inertia tensors or to represent local orientations.
- Useful when optimizing transformations, as it omits the overhead of translation and perspective.
Constructors
Fixed3x3(Fixed64, Fixed64, Fixed64, Fixed64, Fixed64, Fixed64, Fixed64, Fixed64, Fixed64)
Initializes a new FixedMatrix3x3 with the specified elements.
public Fixed3x3(Fixed64 m11, Fixed64 m12, Fixed64 m13, Fixed64 m21, Fixed64 m22, Fixed64 m23, Fixed64 m31, Fixed64 m32, Fixed64 m33)
Parameters
m11Fixed64m12Fixed64m13Fixed64m21Fixed64m22Fixed64m23Fixed64m31Fixed64m32Fixed64m33Fixed64
Fixed3x3(Vector3d, Vector3d, Vector3d)
Initializes a new FixedMatrix3x3 using three Vector3d values representing the rows.
public Fixed3x3(Vector3d m11_m12_m13, Vector3d m21_m22_m23, Vector3d m31_m32_m33)
Parameters
Fields
Identity
Returns the identity matrix (no scaling, rotation, or translation).
public static readonly Fixed3x3 Identity
Field Value
M11
Represents the element in the first row and first column of the matrix.
[JsonInclude]
[MemoryPackOrder(0)]
public Fixed64 M11
Field Value
M12
Represents the element in the first row and second column of the matrix.
[JsonInclude]
[MemoryPackOrder(1)]
public Fixed64 M12
Field Value
M13
Represents the element in the first row and third column of the matrix.
[JsonInclude]
[MemoryPackOrder(2)]
public Fixed64 M13
Field Value
M21
Represents the element in the second row and first column of the matrix.
[JsonInclude]
[MemoryPackOrder(3)]
public Fixed64 M21
Field Value
M22
Represents the element in the second row and second column of the matrix.
[JsonInclude]
[MemoryPackOrder(4)]
public Fixed64 M22
Field Value
M23
Represents the element in the second row and third column of the matrix.
[JsonInclude]
[MemoryPackOrder(5)]
public Fixed64 M23
Field Value
M31
Represents the element in the third row and first column of the matrix.
[JsonInclude]
[MemoryPackOrder(6)]
public Fixed64 M31
Field Value
M32
Represents the element in the third row and second column of the matrix.
[JsonInclude]
[MemoryPackOrder(7)]
public Fixed64 M32
Field Value
M33
Represents the element in the third row and third column of the matrix.
[JsonInclude]
[MemoryPackOrder(8)]
public Fixed64 M33
Field Value
Zero
Returns a matrix with all elements set to zero.
public static readonly Fixed3x3 Zero
Field Value
Properties
this[int]
Gets or sets the matrix element at the specified index.
[JsonIgnore]
[MemoryPackIgnore]
public Fixed64 this[int index] { get; set; }
Parameters
indexintThe zero-based index of the matrix element to get or set. Valid values are 0, 1, 2, 4, 5, 6, 8, 9, and 10.
Property Value
- Fixed64
The matrix element at the specified index.
Remarks
The mapping between indices and matrix elements is non-sequential. Ensure that the index corresponds to a valid matrix element.
Exceptions
- IndexOutOfRangeException
Thrown when the specified index is not one of the valid matrix element indices.
Methods
CreateBarycentricProductSums(Vector3d, Vector3d, Vector3d)
Creates a symmetric matrix containing second-order barycentric product sums for three vectors.
public static Fixed3x3 CreateBarycentricProductSums(Vector3d a, Vector3d b, Vector3d c)
Parameters
Returns
Remarks
The diagonal contains squared component sums, and the off-diagonal elements contain cross-component sums.
CreateRotationX(Fixed64)
Creates a 3x3 matrix representing a rotation around the X-axis.
public static Fixed3x3 CreateRotationX(Fixed64 angle)
Parameters
angleFixed64The angle of rotation in radians.
Returns
- Fixed3x3
A 3x3 rotation matrix.
CreateRotationY(Fixed64)
Creates a 3x3 matrix representing a rotation around the Y-axis.
public static Fixed3x3 CreateRotationY(Fixed64 angle)
Parameters
angleFixed64The angle of rotation in radians.
Returns
- Fixed3x3
A 3x3 rotation matrix.
CreateRotationZ(Fixed64)
Creates a 3x3 matrix representing a rotation around the Z-axis.
public static Fixed3x3 CreateRotationZ(Fixed64 angle)
Parameters
angleFixed64The angle of rotation in radians.
Returns
- Fixed3x3
A 3x3 rotation matrix.
CreateScale(Fixed64)
Creates a uniform scaling matrix with the same scale factor on all axes.
public static Fixed3x3 CreateScale(Fixed64 scaleFactor)
Parameters
scaleFactorFixed64The uniform scale factor.
Returns
- Fixed3x3
A 3x3 scaling matrix with uniform scaling.
CreateScale(Vector3d)
Creates a scaling matrix that applies a uniform or non-uniform scale transformation.
public static Fixed3x3 CreateScale(Vector3d scale)
Parameters
scaleVector3dThe scale factors along the X, Y, and Z axes.
Returns
- Fixed3x3
A 3x3 scaling matrix.
CreateShear(Fixed64, Fixed64, Fixed64)
Creates a 3x3 shear matrix.
public static Fixed3x3 CreateShear(Fixed64 shX, Fixed64 shY, Fixed64 shZ)
Parameters
shXFixed64Shear factor along the X-axis.
shYFixed64Shear factor along the Y-axis.
shZFixed64Shear factor along the Z-axis.
Returns
- Fixed3x3
A 3x3 shear matrix.
Equals(Fixed3x3)
public bool Equals(Fixed3x3 other)
Parameters
otherFixed3x3
Returns
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
ExtractLossyScale(Fixed3x3)
Extracts canonical signed lossy scale from the matrix basis rows.
public static Vector3d ExtractLossyScale(Fixed3x3 matrix)
Parameters
matrixFixed3x3
Returns
Remarks
A reflected basis assigns its one recoverable negative sign to X.
ExtractScaleMagnitudes(Fixed3x3)
Extracts the unsigned magnitudes of the matrix basis rows.
public static Vector3d ExtractScaleMagnitudes(Fixed3x3 matrix)
Parameters
matrixFixed3x3
Returns
- Vector3d
The nonnegative basis magnitudes along X, Y, and Z.
GetDeterminant()
Calculates the determinant of a 3x3 matrix.
public Fixed64 GetDeterminant()
Returns
GetHashCode()
public override int GetHashCode()
Returns
GetNormalized(Fixed3x3)
Normalizes the basis vectors of a 3x3 matrix to ensure they are orthogonal and unit length.
public static Fixed3x3 GetNormalized(Fixed3x3 matrix)
Parameters
matrixFixed3x3
Returns
Remarks
This method recalculates and normalizes the X, Y, and Z basis vectors of the matrix to avoid numerical drift that can occur after multiple transformations. It also ensures that the Z-axis is recomputed to maintain orthogonality by taking the cross-product of the normalized X and Y axes.
Use Cases:
- Ensuring stability and correctness after repeated transformations involving rotation and scaling.
- Useful in physics calculations where orthogonal matrices are required (e.g., inertia tensors or rotations).
InverseTransformDirection(Fixed3x3, Vector3d)
Transforms a direction from world space into the local space of the matrix. Ignores translation.
public static Vector3d InverseTransformDirection(Fixed3x3 matrix, Vector3d direction)
Parameters
Returns
- Vector3d
The transformed local-space direction.
Invert(Fixed3x3, out Fixed3x3?)
Attempts to invert the matrix. If the determinant is zero, returns false and sets result to null.
public static bool Invert(Fixed3x3 matrix, out Fixed3x3? result)
Parameters
Returns
InvertDiagonal()
Inverts the diagonal elements of the matrix.
public Fixed3x3 InvertDiagonal()
Returns
Remarks
protects against the case where you would have an infinite value on the diagonal, which would cause problems in subsequent computations. If m00 or m22 are zero, handle that as a special case and manually set the inverse to zero, since for a theoretical object with no inertia along those axes, it would be impossible to impart a rotation in those directions
bear in mind that having a zero on the inertia tensor's diagonal isn't generally valid for real, 3-dimensional objects (unless they are "infinitely thin" along one axis), so if you end up with such a tensor, it's a sign that something else might be wrong in your setup.
Lerp(Fixed3x3, Fixed3x3, Fixed64)
Linearly interpolates between two matrices.
public static Fixed3x3 Lerp(Fixed3x3 a, Fixed3x3 b, Fixed64 t)
Parameters
Returns
NormalizeInPlace()
Normalizes the basis vectors of a 3x3 matrix to ensure they are orthogonal and unit length.
public Fixed3x3 NormalizeInPlace()
Returns
Remarks
This method recalculates and normalizes the X, Y, and Z basis vectors of the matrix to avoid numerical drift that can occur after multiple transformations. It also ensures that the Z-axis is recomputed to maintain orthogonality by taking the cross-product of the normalized X and Y axes.
Use Cases:
- Ensuring stability and correctness after repeated transformations involving rotation and scaling.
- Useful in physics calculations where orthogonal matrices are required (e.g., inertia tensors or rotations).
ToString()
Returns a string that represents the current matrix in a readable format.
public override string ToString()
Returns
- string
A string containing the matrix elements formatted as "[m00, m01, m02; m10, m11, m12; m20, m21, m22]".
Remarks
This method is useful for debugging or logging the contents of the matrix. The returned string lists the matrix elements in row-major order.
ToString(string?, IFormatProvider?)
Returns a string that represents the current matrix in a readable format.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringformatProviderIFormatProvider
Returns
TransformDirection(Fixed3x3, Vector3d)
Transforms a direction vector from local space to world space using this transformation matrix. Ignores translation.
public static Vector3d TransformDirection(Fixed3x3 matrix, Vector3d direction)
Parameters
Returns
- Vector3d
The transformed direction in world space.
Remarks
FixedMathSharp applies matrices using a row-vector convention: direction * matrix.
Transpose(Fixed3x3)
Transposes the matrix (swaps rows and columns).
public static Fixed3x3 Transpose(Fixed3x3 matrix)
Parameters
matrixFixed3x3
Returns
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Formats this matrix into the provided destination buffer.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
destinationSpan<char>charsWrittenintformatReadOnlySpan<char>providerIFormatProvider
Returns
TryTransformDirection(Fixed3x3, Vector3d, out Vector3d)
Attempts to transform a direction with one final round-half-to-even conversion per component and no intermediate saturation.
public static bool TryTransformDirection(Fixed3x3 matrix, Vector3d direction, out Vector3d result)
Parameters
Returns
Operators
operator +(Fixed3x3, Fixed3x3)
Adds two Fixed3x3 matrices element-wise.
public static Fixed3x3 operator +(Fixed3x3 a, Fixed3x3 b)
Parameters
Returns
- Fixed3x3
A Fixed3x3 matrix whose elements are the sums of the corresponding elements of the input matrices.
operator /(Fixed3x3, int)
Divides each element of the specified matrix by the given scalar value.
public static Fixed3x3 operator /(Fixed3x3 a, int divisor)
Parameters
aFixed3x3The matrix whose elements are to be divided.
divisorintThe scalar value by which to divide each element of the matrix.
Returns
- Fixed3x3
A new Fixed3x3 matrix whose elements are the result of dividing the corresponding elements of the input matrix by the specified scalar.
Remarks
Division is performed element-wise. The result may lose precision if the divisor does not evenly divide the matrix elements.
operator ==(Fixed3x3, Fixed3x3)
Determines whether two Fixed3x3 instances are equal.
public static bool operator ==(Fixed3x3 left, Fixed3x3 right)
Parameters
leftFixed3x3The first Fixed3x3 instance to compare.
rightFixed3x3The second Fixed3x3 instance to compare.
Returns
- bool
true if the specified Fixed3x3 instances are equal; otherwise, false.
operator !=(Fixed3x3, Fixed3x3)
Determines whether two Fixed3x3 instances are not equal.
public static bool operator !=(Fixed3x3 left, Fixed3x3 right)
Parameters
leftFixed3x3The first Fixed3x3 instance to compare.
rightFixed3x3The second Fixed3x3 instance to compare.
Returns
- bool
true if the specified Fixed3x3 instances are not equal; otherwise, false.
operator *(Fixed3x3, Fixed3x3)
Performs matrix multiplication on two 3x3 matrices.
public static Fixed3x3 operator *(Fixed3x3 a, Fixed3x3 b)
Parameters
Returns
- Fixed3x3
A new Fixed3x3 instance that is the product of the two input matrices.
Remarks
Matrix multiplication is not commutative; the order of operands affects the result.
operator *(Fixed3x3, Fixed64)
Multiplies each element of the specified matrix by the given scalar value.
public static Fixed3x3 operator *(Fixed3x3 a, Fixed64 scalar)
Parameters
aFixed3x3The matrix whose elements are to be multiplied.
scalarFixed64The scalar value by which to multiply each element of the matrix.
Returns
- Fixed3x3
A new Fixed3x3 matrix whose elements are the result of multiplying each element of the input matrix by the scalar value.
operator *(Fixed64, Fixed3x3)
Multiplies each element of the specified matrix by the given scalar value.
public static Fixed3x3 operator *(Fixed64 scalar, Fixed3x3 a)
Parameters
scalarFixed64The scalar value by which to multiply each element of the matrix.
aFixed3x3The matrix whose elements are to be multiplied.
Returns
- Fixed3x3
A new Fixed3x3 matrix whose elements are the result of multiplying each element of the input matrix by the scalar value.
operator -(Fixed3x3, Fixed3x3)
Subtracts each corresponding element of one Fixed3x3 matrix from another.
public static Fixed3x3 operator -(Fixed3x3 a, Fixed3x3 b)
Parameters
aFixed3x3The first Fixed3x3 matrix (the minuend).
bFixed3x3The second Fixed3x3 matrix (the subtrahend).
Returns
- Fixed3x3
A Fixed3x3 matrix whose elements are the result of subtracting each element of parameter b from the corresponding element of parameter a.
operator -(Fixed3x3)
Negates all elements of the matrix.
public static Fixed3x3 operator -(Fixed3x3 a)
Parameters
aFixed3x3