| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace FixedMathSharp; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a 4x4 matrix used for transformations in 3D space, including translation, rotation, scaling, and perspect |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// A 4x4 matrix is the standard structure for 3D transformations because it can handle both linear transformations (rot |
| | | 13 | | /// and affine transformations (translation, shearing, and perspective projections). |
| | | 14 | | /// It is commonly used in graphics pipelines, game engines, and 3D rendering systems. |
| | | 15 | | /// |
| | | 16 | | /// Use Cases: |
| | | 17 | | /// - Transforming objects in 3D space (position, orientation, and size). |
| | | 18 | | /// - Combining multiple transformations (e.g., model-view-projection matrices). |
| | | 19 | | /// - Applying translations, which require an extra dimension for homogeneous coordinates. |
| | | 20 | | /// - Useful in animation, physics engines, and 3D rendering for full transformation control. |
| | | 21 | | /// </remarks> |
| | | 22 | | [Serializable] |
| | | 23 | | [MemoryPackable] |
| | | 24 | | public partial struct Fixed4x4 : IEquatable<Fixed4x4> |
| | | 25 | | { |
| | | 26 | | #region Static Readonly Fields |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Returns the identity matrix (diagonal elements set to 1). |
| | | 30 | | /// </summary> |
| | | 31 | | public static readonly Fixed4x4 Identity = new( |
| | | 32 | | Fixed64.One, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | | 33 | | Fixed64.Zero, Fixed64.One, Fixed64.Zero, Fixed64.Zero, |
| | | 34 | | Fixed64.Zero, Fixed64.Zero, Fixed64.One, Fixed64.Zero, |
| | | 35 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Returns a matrix with all elements set to zero. |
| | | 39 | | /// </summary> |
| | | 40 | | public static readonly Fixed4x4 Zero = new( |
| | | 41 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | | 42 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | | 43 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | | 44 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero); |
| | | 45 | | |
| | | 46 | | #endregion |
| | | 47 | | |
| | | 48 | | #region Fields and Constants |
| | | 49 | | |
| | | 50 | | // First row |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Represents the element in the first row and first column of the matrix. |
| | | 54 | | /// </summary> |
| | | 55 | | [JsonInclude] |
| | | 56 | | [MemoryPackOrder(0)] |
| | | 57 | | public Fixed64 m00; |
| | | 58 | | /// <summary> |
| | | 59 | | /// Represents the element in the first row and second column of the matrix. |
| | | 60 | | /// </summary> |
| | | 61 | | [JsonInclude] |
| | | 62 | | [MemoryPackOrder(1)] |
| | | 63 | | public Fixed64 m01; |
| | | 64 | | /// <summary> |
| | | 65 | | /// Represents the element in the first row and third column of the matrix. |
| | | 66 | | /// </summary> |
| | | 67 | | [JsonInclude] |
| | | 68 | | [MemoryPackOrder(2)] |
| | | 69 | | public Fixed64 m02; |
| | | 70 | | /// <summary> |
| | | 71 | | /// Represents the element in the first row and fourth column of the matrix. |
| | | 72 | | /// </summary> |
| | | 73 | | [JsonInclude] |
| | | 74 | | [MemoryPackOrder(3)] |
| | | 75 | | public Fixed64 m03; |
| | | 76 | | |
| | | 77 | | // Second row |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Represents the element in the second row and first column of the matrix. |
| | | 81 | | /// </summary> |
| | | 82 | | [JsonInclude] |
| | | 83 | | [MemoryPackOrder(4)] |
| | | 84 | | public Fixed64 m10; |
| | | 85 | | /// <summary> |
| | | 86 | | /// Represents the element in the second row and second column of the matrix. |
| | | 87 | | /// </summary> |
| | | 88 | | [JsonInclude] |
| | | 89 | | [MemoryPackOrder(5)] |
| | | 90 | | public Fixed64 m11; |
| | | 91 | | /// <summary> |
| | | 92 | | /// Represents the element in the second row and third column of the matrix. |
| | | 93 | | /// </summary> |
| | | 94 | | [JsonInclude] |
| | | 95 | | [MemoryPackOrder(6)] |
| | | 96 | | public Fixed64 m12; |
| | | 97 | | /// <summary> |
| | | 98 | | /// Represents the element in the second row and fourth column of the matrix. |
| | | 99 | | /// </summary> |
| | | 100 | | [JsonInclude] |
| | | 101 | | [MemoryPackOrder(7)] |
| | | 102 | | public Fixed64 m13; |
| | | 103 | | |
| | | 104 | | // Third row |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Represents the element in the third row and first column of the matrix. |
| | | 108 | | /// </summary> |
| | | 109 | | [JsonInclude] |
| | | 110 | | [MemoryPackOrder(8)] |
| | | 111 | | public Fixed64 m20; |
| | | 112 | | /// <summary> |
| | | 113 | | /// Represents the element in the third row and second column of the matrix. |
| | | 114 | | /// </summary> |
| | | 115 | | [JsonInclude] |
| | | 116 | | [MemoryPackOrder(9)] |
| | | 117 | | public Fixed64 m21; |
| | | 118 | | /// <summary> |
| | | 119 | | /// Represents the element in the third row and third column of the matrix. |
| | | 120 | | /// </summary> |
| | | 121 | | [JsonInclude] |
| | | 122 | | [MemoryPackOrder(10)] |
| | | 123 | | public Fixed64 m22; |
| | | 124 | | /// <summary> |
| | | 125 | | /// Represents the element in the third row and fourth column of the matrix. |
| | | 126 | | /// </summary> |
| | | 127 | | [JsonInclude] |
| | | 128 | | [MemoryPackOrder(11)] |
| | | 129 | | public Fixed64 m23; |
| | | 130 | | |
| | | 131 | | // Fourth row |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Represents the element in the fourth row and first column of the matrix. |
| | | 135 | | /// </summary> |
| | | 136 | | [JsonInclude] |
| | | 137 | | [MemoryPackOrder(12)] |
| | | 138 | | public Fixed64 m30; |
| | | 139 | | /// <summary> |
| | | 140 | | /// Represents the element in the fourth row and second column of the matrix. |
| | | 141 | | /// </summary> |
| | | 142 | | [JsonInclude] |
| | | 143 | | [MemoryPackOrder(13)] |
| | | 144 | | public Fixed64 m31; |
| | | 145 | | /// <summary> |
| | | 146 | | /// Represents the element in the fourth row and third column of the matrix. |
| | | 147 | | /// </summary> |
| | | 148 | | [JsonInclude] |
| | | 149 | | [MemoryPackOrder(14)] |
| | | 150 | | public Fixed64 m32; |
| | | 151 | | /// <summary> |
| | | 152 | | /// Represents the element in the fourth row and fourth column of the matrix. |
| | | 153 | | /// </summary> |
| | | 154 | | [JsonInclude] |
| | | 155 | | [MemoryPackOrder(15)] |
| | | 156 | | public Fixed64 m33; |
| | | 157 | | |
| | | 158 | | #endregion |
| | | 159 | | |
| | | 160 | | #region Constructors |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Initializes a new FixedMatrix4x4 with individual elements. |
| | | 164 | | /// </summary> |
| | | 165 | | public Fixed4x4( |
| | | 166 | | Fixed64 m00, Fixed64 m01, Fixed64 m02, Fixed64 m03, |
| | | 167 | | Fixed64 m10, Fixed64 m11, Fixed64 m12, Fixed64 m13, |
| | | 168 | | Fixed64 m20, Fixed64 m21, Fixed64 m22, Fixed64 m23, |
| | | 169 | | Fixed64 m30, Fixed64 m31, Fixed64 m32, Fixed64 m33 |
| | | 170 | | ) |
| | 217 | 171 | | { |
| | 868 | 172 | | this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03; |
| | 868 | 173 | | this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13; |
| | 868 | 174 | | this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23; |
| | 868 | 175 | | this.m30 = m30; this.m31 = m31; this.m32 = m32; this.m33 = m33; |
| | 217 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Creates a matrix from four row vectors. |
| | | 180 | | /// </summary> |
| | | 181 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 182 | | public static Fixed4x4 FromRows(Vector4d row0, Vector4d row1, Vector4d row2, Vector4d row3) |
| | 1 | 183 | | { |
| | 1 | 184 | | return new Fixed4x4( |
| | 1 | 185 | | row0.x, row0.y, row0.z, row0.w, |
| | 1 | 186 | | row1.x, row1.y, row1.z, row1.w, |
| | 1 | 187 | | row2.x, row2.y, row2.z, row2.w, |
| | 1 | 188 | | row3.x, row3.y, row3.z, row3.w); |
| | 1 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Creates a matrix from four column vectors. |
| | | 193 | | /// </summary> |
| | | 194 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 195 | | public static Fixed4x4 FromColumns(Vector4d column0, Vector4d column1, Vector4d column2, Vector4d column3) |
| | 1 | 196 | | { |
| | 1 | 197 | | return new Fixed4x4( |
| | 1 | 198 | | column0.x, column1.x, column2.x, column3.x, |
| | 1 | 199 | | column0.y, column1.y, column2.y, column3.y, |
| | 1 | 200 | | column0.z, column1.z, column2.z, column3.z, |
| | 1 | 201 | | column0.w, column1.w, column2.w, column3.w); |
| | 1 | 202 | | } |
| | | 203 | | |
| | | 204 | | #endregion |
| | | 205 | | |
| | | 206 | | #region Properties |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Gets a value indicating whether the matrix represents an affine transformation. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <remarks> |
| | | 212 | | /// An affine transformation is one where the bottom row is (0, 0, 0, 1), |
| | | 213 | | /// allowing for efficient operations such as translation, scaling, rotation, and shearing without perspective disto |
| | | 214 | | /// </remarks> |
| | | 215 | | [JsonIgnore] |
| | | 216 | | [MemoryPackIgnore] |
| | 149 | 217 | | public readonly bool IsAffine => m33 == Fixed64.One |
| | 149 | 218 | | && m03 == Fixed64.Zero |
| | 149 | 219 | | && m13 == Fixed64.Zero |
| | 149 | 220 | | && m23 == Fixed64.Zero; |
| | | 221 | | |
| | | 222 | | /// <inheritdoc cref="ExtractTranslation(Fixed4x4)" /> |
| | | 223 | | [JsonIgnore] |
| | | 224 | | [MemoryPackIgnore] |
| | 53 | 225 | | public readonly Vector3d Translation => ExtractTranslation(this); |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Gets the right direction vector for this instance. |
| | | 229 | | /// </summary> |
| | | 230 | | [JsonIgnore] |
| | | 231 | | [MemoryPackIgnore] |
| | 46 | 232 | | public readonly Vector3d Right => ExtractRight(this); |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Gets the left direction vector for this instance. |
| | | 236 | | /// </summary> |
| | | 237 | | [JsonIgnore] |
| | | 238 | | [MemoryPackIgnore] |
| | 45 | 239 | | public readonly Vector3d Left => -ExtractRight(this); |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Gets the upward direction vector for this instance. |
| | | 243 | | /// </summary> |
| | | 244 | | [JsonIgnore] |
| | | 245 | | [MemoryPackIgnore] |
| | 46 | 246 | | public readonly Vector3d Up => ExtractUp(this); |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Gets the downward direction vector for this instance. |
| | | 250 | | /// </summary> |
| | | 251 | | [JsonIgnore] |
| | | 252 | | [MemoryPackIgnore] |
| | 45 | 253 | | public readonly Vector3d Down => -ExtractUp(this); |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Gets the forward direction vector for this instance. |
| | | 257 | | /// </summary> |
| | | 258 | | [JsonIgnore] |
| | | 259 | | [MemoryPackIgnore] |
| | 46 | 260 | | public readonly Vector3d Forward => ExtractForward(this); |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Gets the backward direction vector for this instance. |
| | | 264 | | /// </summary> |
| | | 265 | | [JsonIgnore] |
| | | 266 | | [MemoryPackIgnore] |
| | 45 | 267 | | public readonly Vector3d Backward => -ExtractForward(this); |
| | | 268 | | |
| | | 269 | | /// <inheritdoc cref="ExtractScale(Fixed4x4)" /> |
| | | 270 | | [JsonIgnore] |
| | | 271 | | [MemoryPackIgnore] |
| | 51 | 272 | | public readonly Vector3d Scale => ExtractScale(this); |
| | | 273 | | |
| | | 274 | | /// <inheritdoc cref="ExtractRotation(Fixed4x4)" /> |
| | | 275 | | [JsonIgnore] |
| | | 276 | | [MemoryPackIgnore] |
| | 50 | 277 | | public readonly FixedQuaternion Rotation => ExtractRotation(this); |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Gets or sets the matrix element at the specified linear index. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <remarks>Matrix elements are indexed in row-major order from 0 to 15.</remarks> |
| | | 283 | | /// <param name="index">The zero-based linear index of the matrix element to get or set. Must be in the range 0 to 1 |
| | | 284 | | /// <returns>The matrix element at the specified index.</returns> |
| | | 285 | | /// <exception cref="IndexOutOfRangeException">Thrown when the specified index is less than 0 or greater than 15.</e |
| | | 286 | | [JsonIgnore] |
| | | 287 | | [MemoryPackIgnore] |
| | | 288 | | public Fixed64 this[int index] |
| | | 289 | | { |
| | | 290 | | get |
| | 18 | 291 | | { |
| | 18 | 292 | | return index switch |
| | 18 | 293 | | { |
| | 1 | 294 | | 0 => m00, |
| | 1 | 295 | | 1 => m10, |
| | 1 | 296 | | 2 => m20, |
| | 1 | 297 | | 3 => m30, |
| | 1 | 298 | | 4 => m01, |
| | 1 | 299 | | 5 => m11, |
| | 1 | 300 | | 6 => m21, |
| | 1 | 301 | | 7 => m31, |
| | 1 | 302 | | 8 => m02, |
| | 1 | 303 | | 9 => m12, |
| | 1 | 304 | | 10 => m22, |
| | 1 | 305 | | 11 => m32, |
| | 1 | 306 | | 12 => m03, |
| | 1 | 307 | | 13 => m13, |
| | 1 | 308 | | 14 => m23, |
| | 1 | 309 | | 15 => m33, |
| | 2 | 310 | | _ => throw new IndexOutOfRangeException("Invalid matrix index!"), |
| | 18 | 311 | | }; |
| | 16 | 312 | | } |
| | | 313 | | set |
| | 18 | 314 | | { |
| | 18 | 315 | | switch (index) |
| | | 316 | | { |
| | | 317 | | case 0: |
| | 1 | 318 | | m00 = value; |
| | 1 | 319 | | break; |
| | | 320 | | case 1: |
| | 1 | 321 | | m10 = value; |
| | 1 | 322 | | break; |
| | | 323 | | case 2: |
| | 1 | 324 | | m20 = value; |
| | 1 | 325 | | break; |
| | | 326 | | case 3: |
| | 1 | 327 | | m30 = value; |
| | 1 | 328 | | break; |
| | | 329 | | case 4: |
| | 1 | 330 | | m01 = value; |
| | 1 | 331 | | break; |
| | | 332 | | case 5: |
| | 1 | 333 | | m11 = value; |
| | 1 | 334 | | break; |
| | | 335 | | case 6: |
| | 1 | 336 | | m21 = value; |
| | 1 | 337 | | break; |
| | | 338 | | case 7: |
| | 1 | 339 | | m31 = value; |
| | 1 | 340 | | break; |
| | | 341 | | case 8: |
| | 1 | 342 | | m02 = value; |
| | 1 | 343 | | break; |
| | | 344 | | case 9: |
| | 1 | 345 | | m12 = value; |
| | 1 | 346 | | break; |
| | | 347 | | case 10: |
| | 1 | 348 | | m22 = value; |
| | 1 | 349 | | break; |
| | | 350 | | case 11: |
| | 1 | 351 | | m32 = value; |
| | 1 | 352 | | break; |
| | | 353 | | case 12: |
| | 1 | 354 | | m03 = value; |
| | 1 | 355 | | break; |
| | | 356 | | case 13: |
| | 1 | 357 | | m13 = value; |
| | 1 | 358 | | break; |
| | | 359 | | case 14: |
| | 1 | 360 | | m23 = value; |
| | 1 | 361 | | break; |
| | | 362 | | case 15: |
| | 1 | 363 | | m33 = value; |
| | 1 | 364 | | break; |
| | | 365 | | default: |
| | 2 | 366 | | throw new IndexOutOfRangeException("Invalid matrix index!"); |
| | | 367 | | } |
| | 16 | 368 | | } |
| | | 369 | | } |
| | | 370 | | |
| | | 371 | | #endregion |
| | | 372 | | |
| | | 373 | | #region Methods (Instance) |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Calculates the determinant of a 4x4 matrix. |
| | | 377 | | /// </summary> |
| | | 378 | | public Fixed64 GetDeterminant() |
| | 20 | 379 | | { |
| | 20 | 380 | | if (IsAffine) |
| | 12 | 381 | | { |
| | 12 | 382 | | return m00 * (m11 * m22 - m12 * m21) |
| | 12 | 383 | | - m01 * (m10 * m22 - m12 * m20) |
| | 12 | 384 | | + m02 * (m10 * m21 - m11 * m20); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | // Process as full 4x4 matrix |
| | 8 | 388 | | Fixed64 minor0 = m22 * m33 - m23 * m32; |
| | 8 | 389 | | Fixed64 minor1 = m21 * m33 - m23 * m31; |
| | 8 | 390 | | Fixed64 minor2 = m21 * m32 - m22 * m31; |
| | 8 | 391 | | Fixed64 cofactor0 = m20 * m33 - m23 * m30; |
| | 8 | 392 | | Fixed64 cofactor1 = m20 * m32 - m22 * m30; |
| | 8 | 393 | | Fixed64 cofactor2 = m20 * m31 - m21 * m30; |
| | 8 | 394 | | return m00 * (m11 * minor0 - m12 * minor1 + m13 * minor2) |
| | 8 | 395 | | - m01 * (m10 * minor0 - m12 * cofactor0 + m13 * cofactor1) |
| | 8 | 396 | | + m02 * (m10 * minor1 - m11 * cofactor0 + m13 * cofactor2) |
| | 8 | 397 | | - m03 * (m10 * minor2 - m11 * cofactor1 + m12 * cofactor2); |
| | 20 | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <inheritdoc cref="Fixed4x4.ResetScaleToIdentity(Fixed4x4)" /> |
| | | 401 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 402 | | public Fixed4x4 ResetScaleToIdentity() |
| | 2 | 403 | | { |
| | 2 | 404 | | return this = ResetScaleToIdentity(this); |
| | 2 | 405 | | } |
| | | 406 | | |
| | | 407 | | /// <summary> |
| | | 408 | | /// Sets the translation, scale, and rotation components onto the matrix. |
| | | 409 | | /// </summary> |
| | | 410 | | /// <param name="translation">The translation vector.</param> |
| | | 411 | | /// <param name="scale">The scale vector.</param> |
| | | 412 | | /// <param name="rotation">The rotation quaternion.</param> |
| | | 413 | | public void SetTransform(Vector3d translation, FixedQuaternion rotation, Vector3d scale) |
| | 5 | 414 | | { |
| | 5 | 415 | | this = CreateTransform(translation, rotation, scale); |
| | 5 | 416 | | } |
| | | 417 | | |
| | | 418 | | #endregion |
| | | 419 | | |
| | | 420 | | #region Static Matrix Generators and Transformations |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Creates a translation matrix from the specified 3-dimensional vector. |
| | | 424 | | /// </summary> |
| | | 425 | | /// <param name="position"></param> |
| | | 426 | | /// <returns>The translation matrix.</returns> |
| | | 427 | | public static Fixed4x4 CreateTranslation(Vector3d position) |
| | 14 | 428 | | { |
| | 14 | 429 | | Fixed4x4 result = default; |
| | 14 | 430 | | result.m00 = Fixed64.One; |
| | 14 | 431 | | result.m01 = Fixed64.Zero; |
| | 14 | 432 | | result.m02 = Fixed64.Zero; |
| | 14 | 433 | | result.m03 = Fixed64.Zero; |
| | 14 | 434 | | result.m10 = Fixed64.Zero; |
| | 14 | 435 | | result.m11 = Fixed64.One; |
| | 14 | 436 | | result.m12 = Fixed64.Zero; |
| | 14 | 437 | | result.m13 = Fixed64.Zero; |
| | 14 | 438 | | result.m20 = Fixed64.Zero; |
| | 14 | 439 | | result.m21 = Fixed64.Zero; |
| | 14 | 440 | | result.m22 = Fixed64.One; |
| | 14 | 441 | | result.m23 = Fixed64.Zero; |
| | 14 | 442 | | result.m30 = position.x; |
| | 14 | 443 | | result.m31 = position.y; |
| | 14 | 444 | | result.m32 = position.z; |
| | 14 | 445 | | result.m33 = Fixed64.One; |
| | 14 | 446 | | return result; |
| | 14 | 447 | | } |
| | | 448 | | |
| | | 449 | | /// <summary> |
| | | 450 | | /// Creates a translation matrix from the specified coordinates. |
| | | 451 | | /// </summary> |
| | | 452 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 453 | | public static Fixed4x4 CreateTranslation(Fixed64 x, Fixed64 y, Fixed64 z) |
| | 1 | 454 | | { |
| | 1 | 455 | | return CreateTranslation(new Vector3d(x, y, z)); |
| | 1 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Creates a rotation matrix from a quaternion. |
| | | 460 | | /// </summary> |
| | | 461 | | /// <param name="rotation">The quaternion representing the rotation.</param> |
| | | 462 | | /// <returns>A 4x4 matrix representing the rotation.</returns> |
| | | 463 | | public static Fixed4x4 CreateRotation(FixedQuaternion rotation) |
| | 26 | 464 | | { |
| | 26 | 465 | | Fixed3x3 rotationMatrix = rotation.ToMatrix3x3(); |
| | | 466 | | |
| | 26 | 467 | | return new Fixed4x4( |
| | 26 | 468 | | rotationMatrix.m00, rotationMatrix.m01, rotationMatrix.m02, Fixed64.Zero, |
| | 26 | 469 | | rotationMatrix.m10, rotationMatrix.m11, rotationMatrix.m12, Fixed64.Zero, |
| | 26 | 470 | | rotationMatrix.m20, rotationMatrix.m21, rotationMatrix.m22, Fixed64.Zero, |
| | 26 | 471 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One |
| | 26 | 472 | | ); |
| | 26 | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// Creates a rotation matrix around the X axis. |
| | | 477 | | /// </summary> |
| | | 478 | | public static Fixed4x4 CreateRotationX(Fixed64 angle) |
| | 1 | 479 | | { |
| | 1 | 480 | | return FromRotationMatrix(Fixed3x3.CreateRotationX(angle)); |
| | 1 | 481 | | } |
| | | 482 | | |
| | | 483 | | /// <summary> |
| | | 484 | | /// Creates a rotation matrix around the Y axis. |
| | | 485 | | /// </summary> |
| | | 486 | | public static Fixed4x4 CreateRotationY(Fixed64 angle) |
| | 1 | 487 | | { |
| | 1 | 488 | | return FromRotationMatrix(Fixed3x3.CreateRotationY(angle)); |
| | 1 | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <summary> |
| | | 492 | | /// Creates a rotation matrix around the Z axis. |
| | | 493 | | /// </summary> |
| | | 494 | | public static Fixed4x4 CreateRotationZ(Fixed64 angle) |
| | 1 | 495 | | { |
| | 1 | 496 | | return FromRotationMatrix(Fixed3x3.CreateRotationZ(angle)); |
| | 1 | 497 | | } |
| | | 498 | | |
| | | 499 | | /// <summary> |
| | | 500 | | /// Creates a rotation matrix from an axis and angle. |
| | | 501 | | /// </summary> |
| | | 502 | | public static Fixed4x4 CreateFromAxisAngle(Vector3d axis, Fixed64 angle) |
| | 1 | 503 | | { |
| | 1 | 504 | | return CreateRotation(FixedQuaternion.FromAxisAngle(axis, angle)); |
| | 1 | 505 | | } |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// Creates a rotation matrix from pitch, yaw, and roll angles in radians. |
| | | 509 | | /// </summary> |
| | | 510 | | public static Fixed4x4 CreateFromEulerAngles(Fixed64 pitch, Fixed64 yaw, Fixed64 roll) |
| | 1 | 511 | | { |
| | 1 | 512 | | return CreateRotation(FixedQuaternion.FromEulerAngles(pitch, yaw, roll)); |
| | 1 | 513 | | } |
| | | 514 | | |
| | | 515 | | /// <summary> |
| | | 516 | | /// Creates a scale matrix from a 3-dimensional vector. |
| | | 517 | | /// </summary> |
| | | 518 | | /// <param name="scale">The vector representing the scale along each axis.</param> |
| | | 519 | | /// <returns>A 4x4 matrix representing the scale transformation.</returns> |
| | | 520 | | public static Fixed4x4 CreateScale(Vector3d scale) |
| | 22 | 521 | | { |
| | 22 | 522 | | return new Fixed4x4( |
| | 22 | 523 | | scale.x, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 22 | 524 | | Fixed64.Zero, scale.y, Fixed64.Zero, Fixed64.Zero, |
| | 22 | 525 | | Fixed64.Zero, Fixed64.Zero, scale.z, Fixed64.Zero, |
| | 22 | 526 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One |
| | 22 | 527 | | ); |
| | 22 | 528 | | } |
| | | 529 | | |
| | | 530 | | /// <summary> |
| | | 531 | | /// Creates a uniform scale matrix. |
| | | 532 | | /// </summary> |
| | | 533 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 534 | | public static Fixed4x4 CreateScale(Fixed64 scale) |
| | 0 | 535 | | { |
| | 0 | 536 | | return CreateScale(new Vector3d(scale, scale, scale)); |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | /// <summary> |
| | | 540 | | /// Creates a non-uniform scale matrix from individual scale components. |
| | | 541 | | /// </summary> |
| | | 542 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 543 | | public static Fixed4x4 CreateScale(Fixed64 x, Fixed64 y, Fixed64 z) |
| | 0 | 544 | | { |
| | 0 | 545 | | return CreateScale(new Vector3d(x, y, z)); |
| | 0 | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Creates a view matrix looking from a camera position toward a target. |
| | | 550 | | /// </summary> |
| | | 551 | | public static Fixed4x4 CreateLookAt(Vector3d cameraPosition, Vector3d cameraTarget, Vector3d cameraUpVector) |
| | 4 | 552 | | { |
| | 4 | 553 | | Vector3d forward = cameraTarget - cameraPosition; |
| | 4 | 554 | | if (forward.SqrMagnitude == Fixed64.Zero) |
| | 1 | 555 | | throw new ArgumentException("Camera position and target must be different.", nameof(cameraTarget)); |
| | | 556 | | |
| | 3 | 557 | | forward = forward.Normalize(); |
| | 3 | 558 | | Vector3d right = Vector3d.Cross(cameraUpVector, forward); |
| | 3 | 559 | | if (right.SqrMagnitude == Fixed64.Zero) |
| | 1 | 560 | | throw new ArgumentException("Camera up vector must not be parallel to the view direction.", nameof(cameraUpV |
| | | 561 | | |
| | 2 | 562 | | right = right.Normalize(); |
| | 2 | 563 | | Vector3d up = Vector3d.Cross(forward, right).Normalize(); |
| | | 564 | | |
| | 2 | 565 | | return new Fixed4x4( |
| | 2 | 566 | | right.x, right.y, right.z, Fixed64.Zero, |
| | 2 | 567 | | up.x, up.y, up.z, Fixed64.Zero, |
| | 2 | 568 | | forward.x, forward.y, forward.z, Fixed64.Zero, |
| | 2 | 569 | | -Vector3d.Dot(right, cameraPosition), |
| | 2 | 570 | | -Vector3d.Dot(up, cameraPosition), |
| | 2 | 571 | | -Vector3d.Dot(forward, cameraPosition), |
| | 2 | 572 | | Fixed64.One); |
| | 2 | 573 | | } |
| | | 574 | | |
| | | 575 | | /// <summary> |
| | | 576 | | /// Creates an orthographic projection matrix centered on the origin. |
| | | 577 | | /// </summary> |
| | | 578 | | public static Fixed4x4 CreateOrthographic(Fixed64 width, Fixed64 height, Fixed64 zNearPlane, Fixed64 zFarPlane) |
| | 5 | 579 | | { |
| | 5 | 580 | | if (width <= Fixed64.Zero) |
| | 1 | 581 | | throw new ArgumentOutOfRangeException(nameof(width), width, "Width must be greater than zero."); |
| | 4 | 582 | | if (height <= Fixed64.Zero) |
| | 1 | 583 | | throw new ArgumentOutOfRangeException(nameof(height), height, "Height must be greater than zero."); |
| | | 584 | | |
| | 3 | 585 | | Fixed64 halfWidth = width * Fixed64.Half; |
| | 3 | 586 | | Fixed64 halfHeight = height * Fixed64.Half; |
| | 3 | 587 | | return CreateOrthographicOffCenter(-halfWidth, halfWidth, -halfHeight, halfHeight, zNearPlane, zFarPlane); |
| | 1 | 588 | | } |
| | | 589 | | |
| | | 590 | | /// <summary> |
| | | 591 | | /// Creates an off-center orthographic projection matrix. |
| | | 592 | | /// </summary> |
| | | 593 | | public static Fixed4x4 CreateOrthographicOffCenter( |
| | | 594 | | Fixed64 left, |
| | | 595 | | Fixed64 right, |
| | | 596 | | Fixed64 bottom, |
| | | 597 | | Fixed64 top, |
| | | 598 | | Fixed64 zNearPlane, |
| | | 599 | | Fixed64 zFarPlane) |
| | 8 | 600 | | { |
| | 8 | 601 | | if (left == right) |
| | 1 | 602 | | throw new ArgumentOutOfRangeException(nameof(right), right, "Right must be different from left."); |
| | 7 | 603 | | if (bottom == top) |
| | 1 | 604 | | throw new ArgumentOutOfRangeException(nameof(top), top, "Top must be different from bottom."); |
| | 6 | 605 | | ValidateDepthRange(zNearPlane, zFarPlane); |
| | | 606 | | |
| | 2 | 607 | | Fixed64 width = right - left; |
| | 2 | 608 | | Fixed64 height = top - bottom; |
| | 2 | 609 | | Fixed64 depth = zFarPlane - zNearPlane; |
| | | 610 | | |
| | 2 | 611 | | return new Fixed4x4( |
| | 2 | 612 | | Fixed64.Two / width, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 2 | 613 | | Fixed64.Zero, Fixed64.Two / height, Fixed64.Zero, Fixed64.Zero, |
| | 2 | 614 | | Fixed64.Zero, Fixed64.Zero, Fixed64.One / depth, Fixed64.Zero, |
| | 2 | 615 | | (left + right) / (left - right), |
| | 2 | 616 | | (top + bottom) / (bottom - top), |
| | 2 | 617 | | -zNearPlane / depth, |
| | 2 | 618 | | Fixed64.One); |
| | 2 | 619 | | } |
| | | 620 | | |
| | | 621 | | /// <summary> |
| | | 622 | | /// Creates a perspective projection matrix centered on the near plane. |
| | | 623 | | /// </summary> |
| | | 624 | | public static Fixed4x4 CreatePerspective( |
| | | 625 | | Fixed64 width, |
| | | 626 | | Fixed64 height, |
| | | 627 | | Fixed64 nearPlaneDistance, |
| | | 628 | | Fixed64 farPlaneDistance) |
| | 5 | 629 | | { |
| | 5 | 630 | | if (width <= Fixed64.Zero) |
| | 1 | 631 | | throw new ArgumentOutOfRangeException(nameof(width), width, "Width must be greater than zero."); |
| | 4 | 632 | | if (height <= Fixed64.Zero) |
| | 1 | 633 | | throw new ArgumentOutOfRangeException(nameof(height), height, "Height must be greater than zero."); |
| | 3 | 634 | | ValidatePerspectiveDepthRange(nearPlaneDistance, farPlaneDistance); |
| | | 635 | | |
| | 1 | 636 | | Fixed64 depth = farPlaneDistance - nearPlaneDistance; |
| | 1 | 637 | | Fixed64 twoNear = Fixed64.Two * nearPlaneDistance; |
| | | 638 | | |
| | 1 | 639 | | return new Fixed4x4( |
| | 1 | 640 | | twoNear / width, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 1 | 641 | | Fixed64.Zero, twoNear / height, Fixed64.Zero, Fixed64.Zero, |
| | 1 | 642 | | Fixed64.Zero, Fixed64.Zero, farPlaneDistance / depth, Fixed64.One, |
| | 1 | 643 | | Fixed64.Zero, Fixed64.Zero, -(nearPlaneDistance * farPlaneDistance) / depth, Fixed64.Zero); |
| | 1 | 644 | | } |
| | | 645 | | |
| | | 646 | | /// <summary> |
| | | 647 | | /// Creates a perspective projection matrix from a vertical field of view. |
| | | 648 | | /// </summary> |
| | | 649 | | public static Fixed4x4 CreatePerspectiveFieldOfView( |
| | | 650 | | Fixed64 fieldOfView, |
| | | 651 | | Fixed64 aspectRatio, |
| | | 652 | | Fixed64 nearPlaneDistance, |
| | | 653 | | Fixed64 farPlaneDistance) |
| | 7 | 654 | | { |
| | 7 | 655 | | if (fieldOfView <= Fixed64.Zero || fieldOfView >= FixedMath.PI) |
| | 2 | 656 | | throw new ArgumentOutOfRangeException(nameof(fieldOfView), fieldOfView, "Field of view must be greater than |
| | 5 | 657 | | if (aspectRatio <= Fixed64.Zero) |
| | 1 | 658 | | throw new ArgumentOutOfRangeException(nameof(aspectRatio), aspectRatio, "Aspect ratio must be greater than z |
| | 4 | 659 | | ValidatePerspectiveDepthRange(nearPlaneDistance, farPlaneDistance); |
| | | 660 | | |
| | 2 | 661 | | Fixed64 yScale = Fixed64.One / FixedMath.Tan(fieldOfView * Fixed64.Half); |
| | 2 | 662 | | Fixed64 xScale = yScale / aspectRatio; |
| | 2 | 663 | | Fixed64 depth = farPlaneDistance - nearPlaneDistance; |
| | | 664 | | |
| | 2 | 665 | | return new Fixed4x4( |
| | 2 | 666 | | xScale, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 2 | 667 | | Fixed64.Zero, yScale, Fixed64.Zero, Fixed64.Zero, |
| | 2 | 668 | | Fixed64.Zero, Fixed64.Zero, farPlaneDistance / depth, Fixed64.One, |
| | 2 | 669 | | Fixed64.Zero, Fixed64.Zero, -(nearPlaneDistance * farPlaneDistance) / depth, Fixed64.Zero); |
| | 2 | 670 | | } |
| | | 671 | | |
| | | 672 | | /// <summary> |
| | | 673 | | /// Creates an off-center perspective projection matrix. |
| | | 674 | | /// </summary> |
| | | 675 | | public static Fixed4x4 CreatePerspectiveOffCenter( |
| | | 676 | | Fixed64 left, |
| | | 677 | | Fixed64 right, |
| | | 678 | | Fixed64 bottom, |
| | | 679 | | Fixed64 top, |
| | | 680 | | Fixed64 nearPlaneDistance, |
| | | 681 | | Fixed64 farPlaneDistance) |
| | 5 | 682 | | { |
| | 5 | 683 | | if (left == right) |
| | 1 | 684 | | throw new ArgumentOutOfRangeException(nameof(right), right, "Right must be different from left."); |
| | 4 | 685 | | if (bottom == top) |
| | 1 | 686 | | throw new ArgumentOutOfRangeException(nameof(top), top, "Top must be different from bottom."); |
| | 3 | 687 | | ValidatePerspectiveDepthRange(nearPlaneDistance, farPlaneDistance); |
| | | 688 | | |
| | 1 | 689 | | Fixed64 width = right - left; |
| | 1 | 690 | | Fixed64 height = top - bottom; |
| | 1 | 691 | | Fixed64 depth = farPlaneDistance - nearPlaneDistance; |
| | 1 | 692 | | Fixed64 twoNear = Fixed64.Two * nearPlaneDistance; |
| | | 693 | | |
| | 1 | 694 | | return new Fixed4x4( |
| | 1 | 695 | | twoNear / width, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, |
| | 1 | 696 | | Fixed64.Zero, twoNear / height, Fixed64.Zero, Fixed64.Zero, |
| | 1 | 697 | | (left + right) / (left - right), |
| | 1 | 698 | | (top + bottom) / (bottom - top), |
| | 1 | 699 | | farPlaneDistance / depth, |
| | 1 | 700 | | Fixed64.One, |
| | 1 | 701 | | Fixed64.Zero, Fixed64.Zero, -(nearPlaneDistance * farPlaneDistance) / depth, Fixed64.Zero); |
| | 1 | 702 | | } |
| | | 703 | | |
| | | 704 | | /// <summary> |
| | | 705 | | /// Creates a world matrix from a position and orientation basis. |
| | | 706 | | /// </summary> |
| | | 707 | | public static Fixed4x4 CreateWorld(Vector3d position, Vector3d forward, Vector3d up) |
| | 3 | 708 | | { |
| | 3 | 709 | | if (forward.SqrMagnitude == Fixed64.Zero) |
| | 1 | 710 | | throw new ArgumentException("Forward vector must be non-zero.", nameof(forward)); |
| | | 711 | | |
| | 2 | 712 | | forward = forward.Normalize(); |
| | 2 | 713 | | Vector3d right = Vector3d.Cross(up, forward); |
| | 2 | 714 | | if (right.SqrMagnitude == Fixed64.Zero) |
| | 1 | 715 | | throw new ArgumentException("Up vector must not be parallel to forward.", nameof(up)); |
| | | 716 | | |
| | 1 | 717 | | right = right.Normalize(); |
| | 1 | 718 | | up = Vector3d.Cross(forward, right).Normalize(); |
| | | 719 | | |
| | 1 | 720 | | return new Fixed4x4( |
| | 1 | 721 | | right.x, right.y, right.z, Fixed64.Zero, |
| | 1 | 722 | | up.x, up.y, up.z, Fixed64.Zero, |
| | 1 | 723 | | forward.x, forward.y, forward.z, Fixed64.Zero, |
| | 1 | 724 | | position.x, position.y, position.z, Fixed64.One); |
| | 1 | 725 | | } |
| | | 726 | | |
| | | 727 | | /// <summary> |
| | | 728 | | /// Constructs a transformation matrix from translation, scale, and rotation. |
| | | 729 | | /// This method ensures that the rotation is properly normalized, applies the scale to the |
| | | 730 | | /// rotational basis, and sets the translation component separately. |
| | | 731 | | /// </summary> |
| | | 732 | | /// <remarks> |
| | | 733 | | /// - Uses a normalized rotation matrix to maintain numerical stability. |
| | | 734 | | /// - Applies non-uniform scaling to the rotation before setting translation. |
| | | 735 | | /// - Preferred when ensuring transformations remain mathematically correct. |
| | | 736 | | /// - If the rotation is already normalized and combined transformations are needed, consider using <see cref="Scale |
| | | 737 | | /// </remarks> |
| | | 738 | | /// <param name="translation">The translation vector.</param> |
| | | 739 | | /// <param name="scale">The scale vector.</param> |
| | | 740 | | /// <param name="rotation">The rotation quaternion.</param> |
| | | 741 | | /// <returns>A transformation matrix incorporating translation, rotation, and scale.</returns> |
| | | 742 | | public static Fixed4x4 CreateTransform(Vector3d translation, FixedQuaternion rotation, Vector3d scale) |
| | 10 | 743 | | { |
| | | 744 | | // Create the rotation matrix and normalize it |
| | 10 | 745 | | Fixed4x4 rotationMatrix = CreateRotation(rotation); |
| | 10 | 746 | | rotationMatrix = NormalizeRotationMatrix(rotationMatrix); |
| | | 747 | | |
| | | 748 | | // Apply scale directly to the rotation matrix |
| | 10 | 749 | | rotationMatrix = ApplyScaleToRotation(rotationMatrix, scale); |
| | | 750 | | |
| | | 751 | | // Apply the translation to the combined matrix |
| | 10 | 752 | | rotationMatrix = SetTranslation(rotationMatrix, translation); |
| | | 753 | | |
| | 10 | 754 | | return rotationMatrix; |
| | 10 | 755 | | } |
| | | 756 | | |
| | | 757 | | /// <summary> |
| | | 758 | | /// Constructs a transformation matrix from translation, rotation, and scale by multiplying |
| | | 759 | | /// separate matrices in the order: Scale * Rotation * Translation. |
| | | 760 | | /// </summary> |
| | | 761 | | /// <remarks> |
| | | 762 | | /// - This method directly multiplies the scale, rotation, and translation matrices. |
| | | 763 | | /// - Ensures that scale is applied first to preserve correct axis scaling. |
| | | 764 | | /// - Then rotation is applied so that rotation is not affected by non-uniform scaling. |
| | | 765 | | /// - Finally, translation moves the object to its correct world position. |
| | | 766 | | /// </remarks> |
| | | 767 | | public static Fixed4x4 ScaleRotateTranslate(Vector3d translation, FixedQuaternion rotation, Vector3d scale) |
| | 8 | 768 | | { |
| | | 769 | | // Create translation matrix |
| | 8 | 770 | | Fixed4x4 translationMatrix = CreateTranslation(translation); |
| | | 771 | | |
| | | 772 | | // Create rotation matrix using the quaternion |
| | 8 | 773 | | Fixed4x4 rotationMatrix = CreateRotation(rotation); |
| | | 774 | | |
| | | 775 | | // Create scaling matrix |
| | 8 | 776 | | Fixed4x4 scalingMatrix = CreateScale(scale); |
| | | 777 | | |
| | | 778 | | // Combine all transformations |
| | 8 | 779 | | return (scalingMatrix * rotationMatrix) * translationMatrix; |
| | 8 | 780 | | } |
| | | 781 | | |
| | | 782 | | /// <summary> |
| | | 783 | | /// Constructs a transformation matrix from translation, rotation, and scale by multiplying |
| | | 784 | | /// matrices in the order: Translation * Rotation * Scale (T * R * S). |
| | | 785 | | /// </summary> |
| | | 786 | | /// <remarks> |
| | | 787 | | /// - Use this method when transformations need to be applied **relative to an object's local origin**. |
| | | 788 | | /// - Example use cases include **animation systems**, **hierarchical transformations**, and **UI transformations**. |
| | | 789 | | /// - If you need to apply world-space transformations, use <see cref="CreateTransform"/> instead. |
| | | 790 | | /// </remarks> |
| | | 791 | | public static Fixed4x4 TranslateRotateScale(Vector3d translation, FixedQuaternion rotation, Vector3d scale) |
| | 1 | 792 | | { |
| | | 793 | | // Create translation matrix |
| | 1 | 794 | | Fixed4x4 translationMatrix = CreateTranslation(translation); |
| | | 795 | | |
| | | 796 | | // Create rotation matrix using the quaternion |
| | 1 | 797 | | Fixed4x4 rotationMatrix = CreateRotation(rotation); |
| | | 798 | | |
| | | 799 | | // Create scaling matrix |
| | 1 | 800 | | Fixed4x4 scalingMatrix = CreateScale(scale); |
| | | 801 | | |
| | | 802 | | // Combine all transformations |
| | 1 | 803 | | return (translationMatrix * rotationMatrix) * scalingMatrix; |
| | 1 | 804 | | } |
| | | 805 | | |
| | | 806 | | #endregion |
| | | 807 | | |
| | | 808 | | #region Decomposition, Extraction, and Setters |
| | | 809 | | |
| | | 810 | | /// <summary> |
| | | 811 | | /// Extracts the translation component from the 4x4 matrix. |
| | | 812 | | /// </summary> |
| | | 813 | | /// <param name="matrix">The matrix from which to extract the translation.</param> |
| | | 814 | | /// <returns>A Vector3d representing the translation component.</returns> |
| | | 815 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 816 | | public static Vector3d ExtractTranslation(Fixed4x4 matrix) |
| | 53 | 817 | | { |
| | 53 | 818 | | return new Vector3d(matrix.m30, matrix.m31, matrix.m32); |
| | 53 | 819 | | } |
| | | 820 | | |
| | | 821 | | /// <summary> |
| | | 822 | | /// Extracts the right direction from the 4x4 matrix. |
| | | 823 | | /// </summary> |
| | | 824 | | public static Vector3d ExtractRight(Fixed4x4 matrix) |
| | 91 | 825 | | { |
| | 91 | 826 | | return new Vector3d(matrix.m00, matrix.m01, matrix.m02).Normalize(); |
| | 91 | 827 | | } |
| | | 828 | | |
| | | 829 | | /// <summary> |
| | | 830 | | /// Extracts the up direction from the 4x4 matrix. |
| | | 831 | | /// </summary> |
| | | 832 | | /// <remarks> |
| | | 833 | | /// This is the surface normal if the matrix represents ground orientation. |
| | | 834 | | /// </remarks> |
| | | 835 | | /// <param name="matrix"></param> |
| | | 836 | | /// <returns>A <see cref="Vector3d"/> representing the up direction.</returns> |
| | | 837 | | public static Vector3d ExtractUp(Fixed4x4 matrix) |
| | 91 | 838 | | { |
| | 91 | 839 | | return new Vector3d(matrix.m10, matrix.m11, matrix.m12).Normalize(); |
| | 91 | 840 | | } |
| | | 841 | | |
| | | 842 | | /// <summary> |
| | | 843 | | /// Extracts the forward direction from the 4x4 matrix. |
| | | 844 | | /// </summary> |
| | | 845 | | public static Vector3d ExtractForward(Fixed4x4 matrix) |
| | 91 | 846 | | { |
| | 91 | 847 | | return new Vector3d(matrix.m20, matrix.m21, matrix.m22).Normalize(); |
| | 91 | 848 | | } |
| | | 849 | | |
| | | 850 | | /// <summary> |
| | | 851 | | /// Extracts the scaling factors from the matrix by calculating the magnitudes of the basis vectors (non-lossy). |
| | | 852 | | /// </summary> |
| | | 853 | | /// <returns>A Vector3d representing the precise scale along the X, Y, and Z axes.</returns> |
| | | 854 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 855 | | public static Vector3d ExtractScale(Fixed4x4 matrix) |
| | 108 | 856 | | { |
| | 108 | 857 | | return new Vector3d( |
| | 108 | 858 | | new Vector3d(matrix.m00, matrix.m01, matrix.m02).Magnitude, // X scale |
| | 108 | 859 | | new Vector3d(matrix.m10, matrix.m11, matrix.m12).Magnitude, // Y scale |
| | 108 | 860 | | new Vector3d(matrix.m20, matrix.m21, matrix.m22).Magnitude // Z scale |
| | 108 | 861 | | ); |
| | 108 | 862 | | } |
| | | 863 | | |
| | | 864 | | /// <summary> |
| | | 865 | | /// Extracts the scaling factors from the matrix by returning the diagonal elements (lossy). |
| | | 866 | | /// </summary> |
| | | 867 | | /// <returns>A Vector3d representing the scale along X, Y, and Z axes (lossy).</returns> |
| | | 868 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 869 | | public static Vector3d ExtractLossyScale(Fixed4x4 matrix) |
| | 3 | 870 | | { |
| | 3 | 871 | | return new Vector3d(matrix.m00, matrix.m11, matrix.m22); |
| | 3 | 872 | | } |
| | | 873 | | |
| | | 874 | | /// <summary> |
| | | 875 | | /// Extracts the rotation component from the 4x4 matrix by normalizing the rotation matrix. |
| | | 876 | | /// </summary> |
| | | 877 | | /// <param name="matrix">The matrix from which to extract the rotation.</param> |
| | | 878 | | /// <returns>A FixedQuaternion representing the rotation component.</returns> |
| | | 879 | | public static FixedQuaternion ExtractRotation(Fixed4x4 matrix) |
| | 51 | 880 | | { |
| | 51 | 881 | | Vector3d scale = ExtractScale(matrix); |
| | | 882 | | |
| | | 883 | | // prevent divide by zero exception |
| | 51 | 884 | | Fixed64 scaleX = scale.x == Fixed64.Zero ? Fixed64.One : scale.x; |
| | 51 | 885 | | Fixed64 scaleY = scale.y == Fixed64.Zero ? Fixed64.One : scale.y; |
| | 51 | 886 | | Fixed64 scaleZ = scale.z == Fixed64.Zero ? Fixed64.One : scale.z; |
| | | 887 | | |
| | 51 | 888 | | Fixed4x4 normalizedMatrix = new( |
| | 51 | 889 | | matrix.m00 / scaleX, matrix.m01 / scaleX, matrix.m02 / scaleX, Fixed64.Zero, |
| | 51 | 890 | | matrix.m10 / scaleY, matrix.m11 / scaleY, matrix.m12 / scaleY, Fixed64.Zero, |
| | 51 | 891 | | matrix.m20 / scaleZ, matrix.m21 / scaleZ, matrix.m22 / scaleZ, Fixed64.Zero, |
| | 51 | 892 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One |
| | 51 | 893 | | ); |
| | | 894 | | |
| | 51 | 895 | | return FixedQuaternion.FromMatrix(normalizedMatrix); |
| | 51 | 896 | | } |
| | | 897 | | |
| | | 898 | | /// <summary> |
| | | 899 | | /// Decomposes a 4x4 matrix into its translation, scale, and rotation components. |
| | | 900 | | /// </summary> |
| | | 901 | | /// <param name="matrix">The 4x4 matrix to decompose.</param> |
| | | 902 | | /// <param name="scale">The extracted scale component.</param> |
| | | 903 | | /// <param name="rotation">The extracted rotation component as a quaternion.</param> |
| | | 904 | | /// <param name="translation">The extracted translation component.</param> |
| | | 905 | | /// <returns>True if decomposition was successful, otherwise false.</returns> |
| | | 906 | | public static bool Decompose( |
| | | 907 | | Fixed4x4 matrix, |
| | | 908 | | out Vector3d scale, |
| | | 909 | | out FixedQuaternion rotation, |
| | | 910 | | out Vector3d translation) |
| | 4 | 911 | | { |
| | | 912 | | // Extract scale by calculating the magnitudes of the basis vectors |
| | 4 | 913 | | scale = ExtractScale(matrix); |
| | | 914 | | |
| | | 915 | | // prevent divide by zero exception |
| | 4 | 916 | | scale = new Vector3d( |
| | 4 | 917 | | scale.x == Fixed64.Zero ? Fixed64.One : scale.x, |
| | 4 | 918 | | scale.y == Fixed64.Zero ? Fixed64.One : scale.y, |
| | 4 | 919 | | scale.z == Fixed64.Zero ? Fixed64.One : scale.z); |
| | | 920 | | |
| | | 921 | | // normalize rotation and scaling |
| | 4 | 922 | | Fixed4x4 normalizedMatrix = ApplyScaleToRotation(matrix, Vector3d.One / scale); |
| | | 923 | | |
| | | 924 | | // Extract translation |
| | 4 | 925 | | translation = new Vector3d(normalizedMatrix.m30, normalizedMatrix.m31, normalizedMatrix.m32); |
| | | 926 | | |
| | | 927 | | // Check the determinant to ensure correct handedness |
| | 4 | 928 | | Fixed64 determinant = normalizedMatrix.GetDeterminant(); |
| | 4 | 929 | | if (determinant < Fixed64.Zero) |
| | 1 | 930 | | { |
| | | 931 | | // Adjust for left-handed coordinate system by flipping one of the axes |
| | 1 | 932 | | scale.x = -scale.x; |
| | 1 | 933 | | normalizedMatrix.m00 = -normalizedMatrix.m00; |
| | 1 | 934 | | normalizedMatrix.m01 = -normalizedMatrix.m01; |
| | 1 | 935 | | normalizedMatrix.m02 = -normalizedMatrix.m02; |
| | 1 | 936 | | } |
| | | 937 | | |
| | | 938 | | // Extract the rotation component from the orthogonalized matrix |
| | 4 | 939 | | rotation = FixedQuaternion.FromMatrix(normalizedMatrix); |
| | | 940 | | |
| | 4 | 941 | | return true; |
| | 4 | 942 | | } |
| | | 943 | | |
| | | 944 | | /// <summary> |
| | | 945 | | /// Sets the translation component of the 4x4 matrix. |
| | | 946 | | /// </summary> |
| | | 947 | | /// <param name="matrix">The matrix to modify.</param> |
| | | 948 | | /// <param name="translation">The new translation vector.</param> |
| | | 949 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 950 | | public static Fixed4x4 SetTranslation(Fixed4x4 matrix, Vector3d translation) |
| | 11 | 951 | | { |
| | 11 | 952 | | matrix.m30 = translation.x; |
| | 11 | 953 | | matrix.m31 = translation.y; |
| | 11 | 954 | | matrix.m32 = translation.z; |
| | 11 | 955 | | return matrix; |
| | 11 | 956 | | } |
| | | 957 | | |
| | | 958 | | /// <summary> |
| | | 959 | | /// Sets the scale component of the 4x4 matrix by assigning the provided scale vector to the matrix's diagonal eleme |
| | | 960 | | /// </summary> |
| | | 961 | | /// <param name="matrix">The matrix to modify. Typically an identity or transformation matrix.</param> |
| | | 962 | | /// <param name="scale">The new scale vector to apply along the X, Y, and Z axes.</param> |
| | | 963 | | /// <remarks> |
| | | 964 | | /// Best used for applying scale to an identity matrix or resetting the scale on an existing matrix. |
| | | 965 | | /// For non-uniform scaling in combination with rotation, use <see cref="ApplyScaleToRotation"/>. |
| | | 966 | | /// </remarks> |
| | | 967 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 968 | | public static Fixed4x4 SetScale(Fixed4x4 matrix, Vector3d scale) |
| | 1 | 969 | | { |
| | 1 | 970 | | matrix.m00 = scale.x; |
| | 1 | 971 | | matrix.m11 = scale.y; |
| | 1 | 972 | | matrix.m22 = scale.z; |
| | 1 | 973 | | return matrix; |
| | 1 | 974 | | } |
| | | 975 | | |
| | | 976 | | /// <summary> |
| | | 977 | | /// Applies non-uniform scaling to the 4x4 matrix by multiplying the scale vector with the rotation matrix's basis v |
| | | 978 | | /// </summary> |
| | | 979 | | /// <param name="matrix">The matrix to modify. Should already contain a valid rotation component.</param> |
| | | 980 | | /// <param name="scale">The scale vector to apply along the X, Y, and Z axes.</param> |
| | | 981 | | /// <remarks> |
| | | 982 | | /// Use this method when scaling is required in combination with an existing rotation, ensuring proper axis alignmen |
| | | 983 | | /// </remarks> |
| | | 984 | | public static Fixed4x4 ApplyScaleToRotation(Fixed4x4 matrix, Vector3d scale) |
| | 16 | 985 | | { |
| | | 986 | | // Scale each row of the rotation matrix |
| | 16 | 987 | | matrix.m00 *= scale.x; |
| | 16 | 988 | | matrix.m01 *= scale.x; |
| | 16 | 989 | | matrix.m02 *= scale.x; |
| | | 990 | | |
| | 16 | 991 | | matrix.m10 *= scale.y; |
| | 16 | 992 | | matrix.m11 *= scale.y; |
| | 16 | 993 | | matrix.m12 *= scale.y; |
| | | 994 | | |
| | 16 | 995 | | matrix.m20 *= scale.z; |
| | 16 | 996 | | matrix.m21 *= scale.z; |
| | 16 | 997 | | matrix.m22 *= scale.z; |
| | | 998 | | |
| | 16 | 999 | | return matrix; |
| | 16 | 1000 | | } |
| | | 1001 | | |
| | | 1002 | | /// <summary> |
| | | 1003 | | /// Resets the scaling part of the matrix to identity (1,1,1). |
| | | 1004 | | /// </summary> |
| | | 1005 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1006 | | public static Fixed4x4 ResetScaleToIdentity(Fixed4x4 matrix) |
| | 2 | 1007 | | { |
| | 2 | 1008 | | matrix.m00 = Fixed64.One; // X scale |
| | 2 | 1009 | | matrix.m11 = Fixed64.One; // Y scale |
| | 2 | 1010 | | matrix.m22 = Fixed64.One; // Z scale |
| | | 1011 | | |
| | 2 | 1012 | | return matrix; |
| | 2 | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | /// <summary> |
| | | 1016 | | /// Sets the global scale of an object using a 4x4 transformation matrix. |
| | | 1017 | | /// </summary> |
| | | 1018 | | /// <param name="matrix">The transformation matrix representing the object's global state.</param> |
| | | 1019 | | /// <param name="globalScale">The desired global scale as a vector.</param> |
| | | 1020 | | /// <remarks> |
| | | 1021 | | /// The method extracts the current global scale from the matrix and computes the new local scale |
| | | 1022 | | /// by dividing the desired global scale by the current global scale. |
| | | 1023 | | /// The new local scale is then applied to the matrix. |
| | | 1024 | | /// </remarks> |
| | | 1025 | | public static Fixed4x4 SetGlobalScale(Fixed4x4 matrix, Vector3d globalScale) |
| | 2 | 1026 | | { |
| | | 1027 | | // normalize the matrix to avoid drift in the rotation component |
| | 2 | 1028 | | matrix = NormalizeRotationMatrix(matrix); |
| | | 1029 | | |
| | | 1030 | | // Reset the local scaling portion of the matrix |
| | 2 | 1031 | | matrix.ResetScaleToIdentity(); |
| | | 1032 | | |
| | | 1033 | | // Compute the new local scale by dividing the desired global scale by the current scale (which was reset to (1, |
| | 2 | 1034 | | Vector3d newLocalScale = new( |
| | 2 | 1035 | | globalScale.x / Fixed64.One, |
| | 2 | 1036 | | globalScale.y / Fixed64.One, |
| | 2 | 1037 | | globalScale.z / Fixed64.One |
| | 2 | 1038 | | ); |
| | | 1039 | | |
| | | 1040 | | // Apply the new local scale directly to the matrix |
| | 2 | 1041 | | return ApplyScaleToRotation(matrix, newLocalScale); |
| | 2 | 1042 | | } |
| | | 1043 | | |
| | | 1044 | | /// <summary> |
| | | 1045 | | /// Replaces the rotation component of the 4x4 matrix using the provided quaternion, without affecting the translati |
| | | 1046 | | /// </summary> |
| | | 1047 | | /// <param name="matrix">The matrix to modify. The rotation will replace the upper-left 3x3 portion of the matrix.</ |
| | | 1048 | | /// <param name="rotation">The quaternion representing the new rotation to apply.</param> |
| | | 1049 | | /// <remarks> |
| | | 1050 | | /// This method preserves the matrix's translation component. For complete transformation updates, use <see cref="Se |
| | | 1051 | | /// </remarks> |
| | | 1052 | | public static Fixed4x4 SetRotation(Fixed4x4 matrix, FixedQuaternion rotation) |
| | 2 | 1053 | | { |
| | 2 | 1054 | | Fixed3x3 rotationMatrix = rotation.ToMatrix3x3(); |
| | | 1055 | | |
| | 2 | 1056 | | Vector3d scale = ExtractScale(matrix); |
| | | 1057 | | |
| | | 1058 | | // Apply rotation to the upper-left 3x3 matrix |
| | | 1059 | | |
| | 2 | 1060 | | matrix.m00 = rotationMatrix.m00 * scale.x; |
| | 2 | 1061 | | matrix.m01 = rotationMatrix.m01 * scale.x; |
| | 2 | 1062 | | matrix.m02 = rotationMatrix.m02 * scale.x; |
| | | 1063 | | |
| | 2 | 1064 | | matrix.m10 = rotationMatrix.m10 * scale.y; |
| | 2 | 1065 | | matrix.m11 = rotationMatrix.m11 * scale.y; |
| | 2 | 1066 | | matrix.m12 = rotationMatrix.m12 * scale.y; |
| | | 1067 | | |
| | 2 | 1068 | | matrix.m20 = rotationMatrix.m20 * scale.z; |
| | 2 | 1069 | | matrix.m21 = rotationMatrix.m21 * scale.z; |
| | 2 | 1070 | | matrix.m22 = rotationMatrix.m22 * scale.z; |
| | | 1071 | | |
| | 2 | 1072 | | return matrix; |
| | 2 | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | /// <summary> |
| | | 1076 | | /// Normalizes the rotation component of a 4x4 matrix by ensuring the basis vectors are orthogonal and unit length. |
| | | 1077 | | /// </summary> |
| | | 1078 | | /// <remarks> |
| | | 1079 | | /// This method recalculates the X, Y, and Z basis vectors from the upper-left 3x3 portion of the matrix, ensuring t |
| | | 1080 | | /// The remaining components of the matrix are reset to maintain a valid transformation structure. |
| | | 1081 | | /// |
| | | 1082 | | /// Use Cases: |
| | | 1083 | | /// - Ensuring the rotation component remains stable and accurate after multiple transformations. |
| | | 1084 | | /// - Used in 3D transformations to prevent numerical drift from affecting the orientation over time. |
| | | 1085 | | /// - Essential for cases where precise orientation is required, such as animations or physics simulations. |
| | | 1086 | | /// </remarks> |
| | | 1087 | | public static Fixed4x4 NormalizeRotationMatrix(Fixed4x4 matrix) |
| | 13 | 1088 | | { |
| | 13 | 1089 | | Vector3d basisX = new Vector3d(matrix.m00, matrix.m01, matrix.m02).Normalize(); |
| | 13 | 1090 | | Vector3d basisY = new Vector3d(matrix.m10, matrix.m11, matrix.m12).Normalize(); |
| | 13 | 1091 | | Vector3d basisZ = new Vector3d(matrix.m20, matrix.m21, matrix.m22).Normalize(); |
| | | 1092 | | |
| | 13 | 1093 | | return new Fixed4x4( |
| | 13 | 1094 | | basisX.x, basisX.y, basisX.z, Fixed64.Zero, |
| | 13 | 1095 | | basisY.x, basisY.y, basisY.z, Fixed64.Zero, |
| | 13 | 1096 | | basisZ.x, basisZ.y, basisZ.z, Fixed64.Zero, |
| | 13 | 1097 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One |
| | 13 | 1098 | | ); |
| | 13 | 1099 | | } |
| | | 1100 | | |
| | | 1101 | | #endregion |
| | | 1102 | | |
| | | 1103 | | #region Static Matrix Operators |
| | | 1104 | | |
| | | 1105 | | /// <summary> |
| | | 1106 | | /// Linearly interpolates between two matrices component-wise. |
| | | 1107 | | /// </summary> |
| | | 1108 | | public static Fixed4x4 Lerp(Fixed4x4 a, Fixed4x4 b, Fixed64 t) |
| | 1 | 1109 | | { |
| | 1 | 1110 | | return new Fixed4x4( |
| | 1 | 1111 | | FixedMath.LinearInterpolate(a.m00, b.m00, t), |
| | 1 | 1112 | | FixedMath.LinearInterpolate(a.m01, b.m01, t), |
| | 1 | 1113 | | FixedMath.LinearInterpolate(a.m02, b.m02, t), |
| | 1 | 1114 | | FixedMath.LinearInterpolate(a.m03, b.m03, t), |
| | 1 | 1115 | | FixedMath.LinearInterpolate(a.m10, b.m10, t), |
| | 1 | 1116 | | FixedMath.LinearInterpolate(a.m11, b.m11, t), |
| | 1 | 1117 | | FixedMath.LinearInterpolate(a.m12, b.m12, t), |
| | 1 | 1118 | | FixedMath.LinearInterpolate(a.m13, b.m13, t), |
| | 1 | 1119 | | FixedMath.LinearInterpolate(a.m20, b.m20, t), |
| | 1 | 1120 | | FixedMath.LinearInterpolate(a.m21, b.m21, t), |
| | 1 | 1121 | | FixedMath.LinearInterpolate(a.m22, b.m22, t), |
| | 1 | 1122 | | FixedMath.LinearInterpolate(a.m23, b.m23, t), |
| | 1 | 1123 | | FixedMath.LinearInterpolate(a.m30, b.m30, t), |
| | 1 | 1124 | | FixedMath.LinearInterpolate(a.m31, b.m31, t), |
| | 1 | 1125 | | FixedMath.LinearInterpolate(a.m32, b.m32, t), |
| | 1 | 1126 | | FixedMath.LinearInterpolate(a.m33, b.m33, t)); |
| | 1 | 1127 | | } |
| | | 1128 | | |
| | | 1129 | | /// <summary> |
| | | 1130 | | /// Transposes the matrix by swapping rows and columns. |
| | | 1131 | | /// </summary> |
| | | 1132 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1133 | | public static Fixed4x4 Transpose(Fixed4x4 matrix) |
| | 1 | 1134 | | { |
| | 1 | 1135 | | return new Fixed4x4( |
| | 1 | 1136 | | matrix.m00, matrix.m10, matrix.m20, matrix.m30, |
| | 1 | 1137 | | matrix.m01, matrix.m11, matrix.m21, matrix.m31, |
| | 1 | 1138 | | matrix.m02, matrix.m12, matrix.m22, matrix.m32, |
| | 1 | 1139 | | matrix.m03, matrix.m13, matrix.m23, matrix.m33); |
| | 1 | 1140 | | } |
| | | 1141 | | |
| | | 1142 | | /// <summary> |
| | | 1143 | | /// Divides each component of one matrix by the corresponding component of another matrix. |
| | | 1144 | | /// </summary> |
| | | 1145 | | /// <exception cref="DivideByZeroException"> |
| | | 1146 | | /// Thrown when any divisor component is zero. |
| | | 1147 | | /// </exception> |
| | | 1148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1149 | | public static Fixed4x4 ComponentDivide(Fixed4x4 dividend, Fixed4x4 divisor) |
| | 2 | 1150 | | { |
| | 2 | 1151 | | return new Fixed4x4( |
| | 2 | 1152 | | dividend.m00 / divisor.m00, |
| | 2 | 1153 | | dividend.m01 / divisor.m01, |
| | 2 | 1154 | | dividend.m02 / divisor.m02, |
| | 2 | 1155 | | dividend.m03 / divisor.m03, |
| | 2 | 1156 | | dividend.m10 / divisor.m10, |
| | 2 | 1157 | | dividend.m11 / divisor.m11, |
| | 2 | 1158 | | dividend.m12 / divisor.m12, |
| | 2 | 1159 | | dividend.m13 / divisor.m13, |
| | 2 | 1160 | | dividend.m20 / divisor.m20, |
| | 2 | 1161 | | dividend.m21 / divisor.m21, |
| | 2 | 1162 | | dividend.m22 / divisor.m22, |
| | 2 | 1163 | | dividend.m23 / divisor.m23, |
| | 2 | 1164 | | dividend.m30 / divisor.m30, |
| | 2 | 1165 | | dividend.m31 / divisor.m31, |
| | 2 | 1166 | | dividend.m32 / divisor.m32, |
| | 2 | 1167 | | dividend.m33 / divisor.m33); |
| | 1 | 1168 | | } |
| | | 1169 | | |
| | | 1170 | | /// <summary> |
| | | 1171 | | /// Divides one matrix by another using inverse matrix division. |
| | | 1172 | | /// </summary> |
| | | 1173 | | /// <remarks> |
| | | 1174 | | /// This is equivalent to <c>dividend * Invert(divisor)</c>. Use <see cref="ComponentDivide"/> |
| | | 1175 | | /// when each matrix component should be divided by the corresponding component of another matrix. |
| | | 1176 | | /// </remarks> |
| | | 1177 | | /// <exception cref="InvalidOperationException"> |
| | | 1178 | | /// Thrown when <paramref name="divisor"/> is not invertible. |
| | | 1179 | | /// </exception> |
| | | 1180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1181 | | public static Fixed4x4 Divide(Fixed4x4 dividend, Fixed4x4 divisor) |
| | 2 | 1182 | | { |
| | 2 | 1183 | | if (!Invert(divisor, out Fixed4x4 inverseDivisor)) |
| | 1 | 1184 | | throw new InvalidOperationException("Matrix divisor is not invertible."); |
| | | 1185 | | |
| | 1 | 1186 | | return dividend * inverseDivisor; |
| | 1 | 1187 | | } |
| | | 1188 | | |
| | | 1189 | | /// <summary> |
| | | 1190 | | /// Inverts the matrix if it is invertible (i.e., if the determinant is not zero). |
| | | 1191 | | /// </summary> |
| | | 1192 | | /// <remarks> |
| | | 1193 | | /// To Invert a FixedMatrix4x4, we need to calculate the inverse for each element. |
| | | 1194 | | /// This involves computing the cofactor for each element, |
| | | 1195 | | /// which is the determinant of the submatrix when the row and column of that element are removed, |
| | | 1196 | | /// multiplied by a sign based on the element's position. |
| | | 1197 | | /// After computing all cofactors, the result is transposed to get the inverse matrix. |
| | | 1198 | | /// </remarks> |
| | | 1199 | | public static bool Invert(Fixed4x4 matrix, out Fixed4x4 result) |
| | 13 | 1200 | | { |
| | 13 | 1201 | | if (!matrix.IsAffine) |
| | 7 | 1202 | | return FullInvert(matrix, out result); |
| | | 1203 | | |
| | 6 | 1204 | | Fixed64 det = matrix.GetDeterminant(); |
| | | 1205 | | |
| | 6 | 1206 | | if (det == Fixed64.Zero) |
| | 1 | 1207 | | { |
| | 1 | 1208 | | result = Identity; |
| | 1 | 1209 | | return false; |
| | | 1210 | | } |
| | | 1211 | | |
| | 5 | 1212 | | Fixed64 invDet = Fixed64.One / det; |
| | | 1213 | | |
| | | 1214 | | // Invert the 3×3 upper-left rotation/scale matrix |
| | 5 | 1215 | | result = new Fixed4x4( |
| | 5 | 1216 | | (matrix.m11 * matrix.m22 - matrix.m12 * matrix.m21) * invDet, |
| | 5 | 1217 | | (matrix.m02 * matrix.m21 - matrix.m01 * matrix.m22) * invDet, |
| | 5 | 1218 | | (matrix.m01 * matrix.m12 - matrix.m02 * matrix.m11) * invDet, Fixed64.Zero, |
| | 5 | 1219 | | |
| | 5 | 1220 | | (matrix.m12 * matrix.m20 - matrix.m10 * matrix.m22) * invDet, |
| | 5 | 1221 | | (matrix.m00 * matrix.m22 - matrix.m02 * matrix.m20) * invDet, |
| | 5 | 1222 | | (matrix.m02 * matrix.m10 - matrix.m00 * matrix.m12) * invDet, Fixed64.Zero, |
| | 5 | 1223 | | |
| | 5 | 1224 | | (matrix.m10 * matrix.m21 - matrix.m11 * matrix.m20) * invDet, |
| | 5 | 1225 | | (matrix.m01 * matrix.m20 - matrix.m00 * matrix.m21) * invDet, |
| | 5 | 1226 | | (matrix.m00 * matrix.m11 - matrix.m01 * matrix.m10) * invDet, Fixed64.Zero, |
| | 5 | 1227 | | |
| | 5 | 1228 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One // Ensure homogeneous coordinate stays valid |
| | 5 | 1229 | | ); |
| | | 1230 | | |
| | 5 | 1231 | | Fixed3x3 rotationScaleInverse = new( |
| | 5 | 1232 | | result.m00, result.m01, result.m02, |
| | 5 | 1233 | | result.m10, result.m11, result.m12, |
| | 5 | 1234 | | result.m20, result.m21, result.m22 |
| | 5 | 1235 | | ); |
| | | 1236 | | |
| | | 1237 | | // Correct translation component |
| | 5 | 1238 | | Vector3d transformedTranslation = new(matrix.m30, matrix.m31, matrix.m32); |
| | 5 | 1239 | | transformedTranslation = -Fixed3x3.TransformDirection(rotationScaleInverse, transformedTranslation); |
| | | 1240 | | |
| | 5 | 1241 | | result.m30 = transformedTranslation.x; |
| | 5 | 1242 | | result.m31 = transformedTranslation.y; |
| | 5 | 1243 | | result.m32 = transformedTranslation.z; |
| | 5 | 1244 | | result.m33 = Fixed64.One; |
| | | 1245 | | |
| | 5 | 1246 | | return true; |
| | 13 | 1247 | | } |
| | | 1248 | | |
| | | 1249 | | private static bool FullInvert(Fixed4x4 matrix, out Fixed4x4 result) |
| | 7 | 1250 | | { |
| | 7 | 1251 | | Fixed64 det = matrix.GetDeterminant(); |
| | | 1252 | | |
| | 7 | 1253 | | if (det == Fixed64.Zero) |
| | 3 | 1254 | | { |
| | 3 | 1255 | | result = Fixed4x4.Identity; |
| | 3 | 1256 | | return false; |
| | | 1257 | | } |
| | | 1258 | | |
| | 4 | 1259 | | Fixed64 invDet = Fixed64.One / det; |
| | | 1260 | | |
| | | 1261 | | // Inversion using cofactors and determinants of 3x3 submatrices |
| | 4 | 1262 | | result = new Fixed4x4 |
| | 4 | 1263 | | { |
| | 4 | 1264 | | // First row |
| | 4 | 1265 | | m00 = invDet * ((matrix.m11 * matrix.m22 * matrix.m33 + matrix.m12 * matrix.m23 * matrix.m31 + matrix.m13 * |
| | 4 | 1266 | | - (matrix.m13 * matrix.m22 * matrix.m31 + matrix.m11 * matrix.m23 * matrix.m32 + matrix.m12 * |
| | 4 | 1267 | | m01 = invDet * ((matrix.m01 * matrix.m23 * matrix.m32 + matrix.m02 * matrix.m21 * matrix.m33 + matrix.m03 * |
| | 4 | 1268 | | - (matrix.m03 * matrix.m21 * matrix.m32 + matrix.m01 * matrix.m22 * matrix.m33 + matrix.m02 * |
| | 4 | 1269 | | m02 = invDet * ((matrix.m01 * matrix.m12 * matrix.m33 + matrix.m02 * matrix.m13 * matrix.m31 + matrix.m03 * |
| | 4 | 1270 | | - (matrix.m03 * matrix.m12 * matrix.m31 + matrix.m01 * matrix.m13 * matrix.m32 + matrix.m02 * |
| | 4 | 1271 | | m03 = invDet * ((matrix.m01 * matrix.m13 * matrix.m22 + matrix.m02 * matrix.m11 * matrix.m23 + matrix.m03 * |
| | 4 | 1272 | | - (matrix.m03 * matrix.m11 * matrix.m22 + matrix.m01 * matrix.m12 * matrix.m23 + matrix.m02 * |
| | 4 | 1273 | | |
| | 4 | 1274 | | // Second row |
| | 4 | 1275 | | m10 = invDet * ((matrix.m10 * matrix.m23 * matrix.m32 + matrix.m12 * matrix.m20 * matrix.m33 + matrix.m13 * |
| | 4 | 1276 | | - (matrix.m13 * matrix.m20 * matrix.m32 + matrix.m10 * matrix.m22 * matrix.m33 + matrix.m12 * |
| | 4 | 1277 | | m11 = invDet * ((matrix.m00 * matrix.m22 * matrix.m33 + matrix.m02 * matrix.m23 * matrix.m30 + matrix.m03 * |
| | 4 | 1278 | | - (matrix.m03 * matrix.m20 * matrix.m32 + matrix.m00 * matrix.m23 * matrix.m32 + matrix.m02 * |
| | 4 | 1279 | | m12 = invDet * ((matrix.m00 * matrix.m13 * matrix.m32 + matrix.m02 * matrix.m10 * matrix.m33 + matrix.m03 * |
| | 4 | 1280 | | - (matrix.m03 * matrix.m10 * matrix.m32 + matrix.m00 * matrix.m12 * matrix.m33 + matrix.m02 * |
| | 4 | 1281 | | m13 = invDet * ((matrix.m00 * matrix.m12 * matrix.m23 + matrix.m02 * matrix.m13 * matrix.m20 + matrix.m03 * |
| | 4 | 1282 | | - (matrix.m03 * matrix.m10 * matrix.m22 + matrix.m00 * matrix.m13 * matrix.m22 + matrix.m02 * |
| | 4 | 1283 | | |
| | 4 | 1284 | | // Third row |
| | 4 | 1285 | | m20 = invDet * ((matrix.m10 * matrix.m21 * matrix.m33 + matrix.m11 * matrix.m23 * matrix.m30 + matrix.m13 * |
| | 4 | 1286 | | - (matrix.m13 * matrix.m20 * matrix.m31 + matrix.m10 * matrix.m23 * matrix.m31 + matrix.m11 * |
| | 4 | 1287 | | m21 = invDet * ((matrix.m00 * matrix.m23 * matrix.m31 + matrix.m01 * matrix.m20 * matrix.m33 + matrix.m03 * |
| | 4 | 1288 | | - (matrix.m03 * matrix.m20 * matrix.m31 + matrix.m00 * matrix.m21 * matrix.m33 + matrix.m01 * |
| | 4 | 1289 | | m22 = invDet * ((matrix.m00 * matrix.m11 * matrix.m33 + matrix.m01 * matrix.m13 * matrix.m30 + matrix.m03 * |
| | 4 | 1290 | | - (matrix.m03 * matrix.m10 * matrix.m31 + matrix.m00 * matrix.m13 * matrix.m31 + matrix.m01 * |
| | 4 | 1291 | | m23 = invDet * ((matrix.m00 * matrix.m13 * matrix.m21 + matrix.m01 * matrix.m10 * matrix.m23 + matrix.m03 * |
| | 4 | 1292 | | - (matrix.m03 * matrix.m10 * matrix.m21 + matrix.m00 * matrix.m11 * matrix.m23 + matrix.m01 * |
| | 4 | 1293 | | |
| | 4 | 1294 | | // Fourth row |
| | 4 | 1295 | | m30 = invDet * ((matrix.m10 * matrix.m22 * matrix.m31 + matrix.m11 * matrix.m20 * matrix.m32 + matrix.m12 * |
| | 4 | 1296 | | - (matrix.m12 * matrix.m20 * matrix.m31 + matrix.m10 * matrix.m21 * matrix.m32 + matrix.m11 * |
| | 4 | 1297 | | m31 = invDet * ((matrix.m00 * matrix.m21 * matrix.m32 + matrix.m01 * matrix.m22 * matrix.m30 + matrix.m02 * |
| | 4 | 1298 | | - (matrix.m02 * matrix.m20 * matrix.m31 + matrix.m00 * matrix.m22 * matrix.m31 + matrix.m01 * |
| | 4 | 1299 | | m32 = invDet * ((matrix.m00 * matrix.m12 * matrix.m31 + matrix.m01 * matrix.m10 * matrix.m32 + matrix.m02 * |
| | 4 | 1300 | | - (matrix.m02 * matrix.m10 * matrix.m31 + matrix.m00 * matrix.m11 * matrix.m32 + matrix.m01 * |
| | 4 | 1301 | | m33 = invDet * ((matrix.m00 * matrix.m11 * matrix.m22 + matrix.m01 * matrix.m12 * matrix.m20 + matrix.m02 * |
| | 4 | 1302 | | - (matrix.m02 * matrix.m10 * matrix.m21 + matrix.m00 * matrix.m12 * matrix.m21 + matrix.m01 * |
| | 4 | 1303 | | }; |
| | | 1304 | | |
| | 4 | 1305 | | return true; |
| | 7 | 1306 | | } |
| | | 1307 | | |
| | | 1308 | | /// <summary> |
| | | 1309 | | /// Transforms a 4D vector by a 4x4 matrix, preserving the computed W component. |
| | | 1310 | | /// </summary> |
| | | 1311 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1312 | | public static Vector4d Transform(Fixed4x4 matrix, Vector4d vector) |
| | 2 | 1313 | | { |
| | 2 | 1314 | | return Vector4d.Transform(matrix, vector); |
| | 2 | 1315 | | } |
| | | 1316 | | |
| | | 1317 | | /// <summary> |
| | | 1318 | | /// Transforms a point from local space to world space using this transformation matrix. |
| | | 1319 | | /// </summary> |
| | | 1320 | | /// <remarks> |
| | | 1321 | | /// This is the same as doing `<see cref="Fixed4x4"/> a * <see cref="Vector3d"/> b` |
| | | 1322 | | /// </remarks> |
| | | 1323 | | /// <param name="matrix">The transformation matrix.</param> |
| | | 1324 | | /// <param name="point">The local-space point.</param> |
| | | 1325 | | /// <returns>The transformed point in world space.</returns> |
| | | 1326 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1327 | | public static Vector3d TransformPoint(Fixed4x4 matrix, Vector3d point) |
| | 13 | 1328 | | { |
| | 13 | 1329 | | if (matrix.IsAffine) |
| | 8 | 1330 | | { |
| | 8 | 1331 | | return new Vector3d( |
| | 8 | 1332 | | matrix.m00 * point.x + matrix.m01 * point.y + matrix.m02 * point.z + matrix.m30, |
| | 8 | 1333 | | matrix.m10 * point.x + matrix.m11 * point.y + matrix.m12 * point.z + matrix.m31, |
| | 8 | 1334 | | matrix.m20 * point.x + matrix.m21 * point.y + matrix.m22 * point.z + matrix.m32 |
| | 8 | 1335 | | ); |
| | | 1336 | | } |
| | | 1337 | | |
| | 5 | 1338 | | return FullTransformPoint(matrix, point); |
| | 13 | 1339 | | } |
| | | 1340 | | |
| | | 1341 | | private static Vector3d FullTransformPoint(Fixed4x4 matrix, Vector3d point) |
| | 5 | 1342 | | { |
| | | 1343 | | // Full 4×4 transformation (needed for perspective projections) |
| | 5 | 1344 | | Fixed64 w = matrix.m03 * point.x + matrix.m13 * point.y + matrix.m23 * point.z + matrix.m33; |
| | 6 | 1345 | | if (w == Fixed64.Zero) w = Fixed64.One; // Prevent divide-by-zero |
| | | 1346 | | |
| | 5 | 1347 | | return new Vector3d( |
| | 5 | 1348 | | (matrix.m00 * point.x + matrix.m01 * point.y + matrix.m02 * point.z + matrix.m30) / w, |
| | 5 | 1349 | | (matrix.m10 * point.x + matrix.m11 * point.y + matrix.m12 * point.z + matrix.m31) / w, |
| | 5 | 1350 | | (matrix.m20 * point.x + matrix.m21 * point.y + matrix.m22 * point.z + matrix.m32) / w |
| | 5 | 1351 | | ); |
| | 5 | 1352 | | } |
| | | 1353 | | |
| | | 1354 | | /// <summary> |
| | | 1355 | | /// Transforms a point from world space into the local space of the matrix. |
| | | 1356 | | /// </summary> |
| | | 1357 | | /// <param name="matrix">The transformation matrix.</param> |
| | | 1358 | | /// <param name="point">The world-space point.</param> |
| | | 1359 | | /// <returns>The local-space point relative to the transformation matrix.</returns> |
| | | 1360 | | public static Vector3d InverseTransformPoint(Fixed4x4 matrix, Vector3d point) |
| | 7 | 1361 | | { |
| | | 1362 | | // Invert the transformation matrix |
| | 7 | 1363 | | if (!Invert(matrix, out Fixed4x4 inverseMatrix)) |
| | 1 | 1364 | | throw new InvalidOperationException("Matrix is not invertible."); |
| | | 1365 | | |
| | 6 | 1366 | | if (inverseMatrix.IsAffine) |
| | 3 | 1367 | | { |
| | 3 | 1368 | | return new Vector3d( |
| | 3 | 1369 | | inverseMatrix.m00 * point.x + inverseMatrix.m01 * point.y + inverseMatrix.m02 * point.z + inverseMatrix. |
| | 3 | 1370 | | inverseMatrix.m10 * point.x + inverseMatrix.m11 * point.y + inverseMatrix.m12 * point.z + inverseMatrix. |
| | 3 | 1371 | | inverseMatrix.m20 * point.x + inverseMatrix.m21 * point.y + inverseMatrix.m22 * point.z + inverseMatrix. |
| | 3 | 1372 | | ); |
| | | 1373 | | } |
| | | 1374 | | |
| | 3 | 1375 | | return FullInverseTransformPoint(inverseMatrix, point); |
| | 6 | 1376 | | } |
| | | 1377 | | |
| | | 1378 | | private static Vector3d FullInverseTransformPoint(Fixed4x4 matrix, Vector3d point) |
| | 3 | 1379 | | { |
| | | 1380 | | // Full 4×4 transformation (needed for perspective projections) |
| | 3 | 1381 | | Fixed64 w = matrix.m03 * point.x + matrix.m13 * point.y + matrix.m23 * point.z + matrix.m33; |
| | 4 | 1382 | | if (w == Fixed64.Zero) w = Fixed64.One; // Prevent divide-by-zero |
| | | 1383 | | |
| | 3 | 1384 | | return new Vector3d( |
| | 3 | 1385 | | (matrix.m00 * point.x + matrix.m01 * point.y + matrix.m02 * point.z + matrix.m30) / w, |
| | 3 | 1386 | | (matrix.m10 * point.x + matrix.m11 * point.y + matrix.m12 * point.z + matrix.m31) / w, |
| | 3 | 1387 | | (matrix.m20 * point.x + matrix.m21 * point.y + matrix.m22 * point.z + matrix.m32) / w |
| | 3 | 1388 | | ); |
| | 3 | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | #endregion |
| | | 1392 | | |
| | | 1393 | | #region Operators |
| | | 1394 | | |
| | | 1395 | | /// <summary> |
| | | 1396 | | /// Negates the specified matrix by multiplying all its values by -1. |
| | | 1397 | | /// </summary> |
| | | 1398 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1399 | | public static Fixed4x4 operator -(Fixed4x4 value) |
| | 1 | 1400 | | { |
| | 1 | 1401 | | Fixed4x4 result = default; |
| | 1 | 1402 | | result.m00 = -value.m00; |
| | 1 | 1403 | | result.m01 = -value.m01; |
| | 1 | 1404 | | result.m02 = -value.m02; |
| | 1 | 1405 | | result.m03 = -value.m03; |
| | 1 | 1406 | | result.m10 = -value.m10; |
| | 1 | 1407 | | result.m11 = -value.m11; |
| | 1 | 1408 | | result.m12 = -value.m12; |
| | 1 | 1409 | | result.m13 = -value.m13; |
| | 1 | 1410 | | result.m20 = -value.m20; |
| | 1 | 1411 | | result.m21 = -value.m21; |
| | 1 | 1412 | | result.m22 = -value.m22; |
| | 1 | 1413 | | result.m23 = -value.m23; |
| | 1 | 1414 | | result.m30 = -value.m30; |
| | 1 | 1415 | | result.m31 = -value.m31; |
| | 1 | 1416 | | result.m32 = -value.m32; |
| | 1 | 1417 | | result.m33 = -value.m33; |
| | 1 | 1418 | | return result; |
| | 1 | 1419 | | } |
| | | 1420 | | |
| | | 1421 | | /// <summary> |
| | | 1422 | | /// Adds two matrices element-wise. |
| | | 1423 | | /// </summary> |
| | | 1424 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1425 | | public static Fixed4x4 operator +(Fixed4x4 lhs, Fixed4x4 rhs) |
| | 3 | 1426 | | { |
| | 3 | 1427 | | return new Fixed4x4( |
| | 3 | 1428 | | lhs.m00 + rhs.m00, lhs.m01 + rhs.m01, lhs.m02 + rhs.m02, lhs.m03 + rhs.m03, |
| | 3 | 1429 | | lhs.m10 + rhs.m10, lhs.m11 + rhs.m11, lhs.m12 + rhs.m12, lhs.m13 + rhs.m13, |
| | 3 | 1430 | | lhs.m20 + rhs.m20, lhs.m21 + rhs.m21, lhs.m22 + rhs.m22, lhs.m23 + rhs.m23, |
| | 3 | 1431 | | lhs.m30 + rhs.m30, lhs.m31 + rhs.m31, lhs.m32 + rhs.m32, lhs.m33 + rhs.m33); |
| | 3 | 1432 | | } |
| | | 1433 | | |
| | | 1434 | | /// <summary> |
| | | 1435 | | /// Subtracts two matrices element-wise. |
| | | 1436 | | /// </summary> |
| | | 1437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1438 | | public static Fixed4x4 operator -(Fixed4x4 lhs, Fixed4x4 rhs) |
| | 1 | 1439 | | { |
| | 1 | 1440 | | return new Fixed4x4( |
| | 1 | 1441 | | lhs.m00 - rhs.m00, lhs.m01 - rhs.m01, lhs.m02 - rhs.m02, lhs.m03 - rhs.m03, |
| | 1 | 1442 | | lhs.m10 - rhs.m10, lhs.m11 - rhs.m11, lhs.m12 - rhs.m12, lhs.m13 - rhs.m13, |
| | 1 | 1443 | | lhs.m20 - rhs.m20, lhs.m21 - rhs.m21, lhs.m22 - rhs.m22, lhs.m23 - rhs.m23, |
| | 1 | 1444 | | lhs.m30 - rhs.m30, lhs.m31 - rhs.m31, lhs.m32 - rhs.m32, lhs.m33 - rhs.m33); |
| | 1 | 1445 | | } |
| | | 1446 | | |
| | | 1447 | | /// <summary> |
| | | 1448 | | /// Multiplies two 4x4 matrices using standard matrix multiplication. |
| | | 1449 | | /// </summary> |
| | | 1450 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1451 | | public static Fixed4x4 operator *(Fixed4x4 lhs, Fixed4x4 rhs) |
| | 23 | 1452 | | { |
| | 23 | 1453 | | if (lhs.IsAffine && rhs.IsAffine) |
| | 21 | 1454 | | { |
| | | 1455 | | // Optimized affine multiplication (skips full 4×4 multiplication) |
| | 21 | 1456 | | return new Fixed4x4( |
| | 21 | 1457 | | lhs.m00 * rhs.m00 + lhs.m01 * rhs.m10 + lhs.m02 * rhs.m20, |
| | 21 | 1458 | | lhs.m00 * rhs.m01 + lhs.m01 * rhs.m11 + lhs.m02 * rhs.m21, |
| | 21 | 1459 | | lhs.m00 * rhs.m02 + lhs.m01 * rhs.m12 + lhs.m02 * rhs.m22, |
| | 21 | 1460 | | Fixed64.Zero, |
| | 21 | 1461 | | |
| | 21 | 1462 | | lhs.m10 * rhs.m00 + lhs.m11 * rhs.m10 + lhs.m12 * rhs.m20, |
| | 21 | 1463 | | lhs.m10 * rhs.m01 + lhs.m11 * rhs.m11 + lhs.m12 * rhs.m21, |
| | 21 | 1464 | | lhs.m10 * rhs.m02 + lhs.m11 * rhs.m12 + lhs.m12 * rhs.m22, |
| | 21 | 1465 | | Fixed64.Zero, |
| | 21 | 1466 | | |
| | 21 | 1467 | | lhs.m20 * rhs.m00 + lhs.m21 * rhs.m10 + lhs.m22 * rhs.m20, |
| | 21 | 1468 | | lhs.m20 * rhs.m01 + lhs.m21 * rhs.m11 + lhs.m22 * rhs.m21, |
| | 21 | 1469 | | lhs.m20 * rhs.m02 + lhs.m21 * rhs.m12 + lhs.m22 * rhs.m22, |
| | 21 | 1470 | | Fixed64.Zero, |
| | 21 | 1471 | | |
| | 21 | 1472 | | lhs.m30 * rhs.m00 + lhs.m31 * rhs.m10 + lhs.m32 * rhs.m20 + rhs.m30, |
| | 21 | 1473 | | lhs.m30 * rhs.m01 + lhs.m31 * rhs.m11 + lhs.m32 * rhs.m21 + rhs.m31, |
| | 21 | 1474 | | lhs.m30 * rhs.m02 + lhs.m31 * rhs.m12 + lhs.m32 * rhs.m22 + rhs.m32, |
| | 21 | 1475 | | Fixed64.One |
| | 21 | 1476 | | ); |
| | | 1477 | | } |
| | | 1478 | | |
| | | 1479 | | // Full 4×4 multiplication (fallback for perspective matrices) |
| | 2 | 1480 | | return new Fixed4x4( |
| | 2 | 1481 | | // Upper-left 3×3 matrix multiplication (rotation & scale) |
| | 2 | 1482 | | lhs.m00 * rhs.m00 + lhs.m01 * rhs.m10 + lhs.m02 * rhs.m20 + lhs.m03 * rhs.m30, |
| | 2 | 1483 | | lhs.m00 * rhs.m01 + lhs.m01 * rhs.m11 + lhs.m02 * rhs.m21 + lhs.m03 * rhs.m31, |
| | 2 | 1484 | | lhs.m00 * rhs.m02 + lhs.m01 * rhs.m12 + lhs.m02 * rhs.m22 + lhs.m03 * rhs.m32, |
| | 2 | 1485 | | lhs.m00 * rhs.m03 + lhs.m01 * rhs.m13 + lhs.m02 * rhs.m23 + lhs.m03 * rhs.m33, |
| | 2 | 1486 | | |
| | 2 | 1487 | | lhs.m10 * rhs.m00 + lhs.m11 * rhs.m10 + lhs.m12 * rhs.m20 + lhs.m13 * rhs.m30, |
| | 2 | 1488 | | lhs.m10 * rhs.m01 + lhs.m11 * rhs.m11 + lhs.m12 * rhs.m21 + lhs.m13 * rhs.m31, |
| | 2 | 1489 | | lhs.m10 * rhs.m02 + lhs.m11 * rhs.m12 + lhs.m12 * rhs.m22 + lhs.m13 * rhs.m32, |
| | 2 | 1490 | | lhs.m10 * rhs.m03 + lhs.m11 * rhs.m13 + lhs.m12 * rhs.m23 + lhs.m13 * rhs.m33, |
| | 2 | 1491 | | |
| | 2 | 1492 | | lhs.m20 * rhs.m00 + lhs.m21 * rhs.m10 + lhs.m22 * rhs.m20 + lhs.m23 * rhs.m30, |
| | 2 | 1493 | | lhs.m20 * rhs.m01 + lhs.m21 * rhs.m11 + lhs.m22 * rhs.m21 + lhs.m23 * rhs.m31, |
| | 2 | 1494 | | lhs.m20 * rhs.m02 + lhs.m21 * rhs.m12 + lhs.m22 * rhs.m22 + lhs.m23 * rhs.m32, |
| | 2 | 1495 | | lhs.m20 * rhs.m03 + lhs.m21 * rhs.m13 + lhs.m22 * rhs.m23 + lhs.m23 * rhs.m33, |
| | 2 | 1496 | | |
| | 2 | 1497 | | // Compute new translation |
| | 2 | 1498 | | lhs.m30 * rhs.m00 + lhs.m31 * rhs.m10 + lhs.m32 * rhs.m20 + lhs.m33 * rhs.m30, |
| | 2 | 1499 | | lhs.m30 * rhs.m01 + lhs.m31 * rhs.m11 + lhs.m32 * rhs.m21 + lhs.m33 * rhs.m31, |
| | 2 | 1500 | | lhs.m30 * rhs.m02 + lhs.m31 * rhs.m12 + lhs.m32 * rhs.m22 + lhs.m33 * rhs.m32, |
| | 2 | 1501 | | lhs.m30 * rhs.m03 + lhs.m31 * rhs.m13 + lhs.m32 * rhs.m23 + lhs.m33 * rhs.m33 |
| | 2 | 1502 | | ); |
| | 23 | 1503 | | } |
| | | 1504 | | |
| | | 1505 | | /// <summary> |
| | | 1506 | | /// Multiplies every matrix component by a scalar. |
| | | 1507 | | /// </summary> |
| | | 1508 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1509 | | public static Fixed4x4 operator *(Fixed4x4 matrix, Fixed64 scalar) |
| | 5 | 1510 | | { |
| | 5 | 1511 | | return new Fixed4x4( |
| | 5 | 1512 | | matrix.m00 * scalar, matrix.m01 * scalar, matrix.m02 * scalar, matrix.m03 * scalar, |
| | 5 | 1513 | | matrix.m10 * scalar, matrix.m11 * scalar, matrix.m12 * scalar, matrix.m13 * scalar, |
| | 5 | 1514 | | matrix.m20 * scalar, matrix.m21 * scalar, matrix.m22 * scalar, matrix.m23 * scalar, |
| | 5 | 1515 | | matrix.m30 * scalar, matrix.m31 * scalar, matrix.m32 * scalar, matrix.m33 * scalar); |
| | 5 | 1516 | | } |
| | | 1517 | | |
| | | 1518 | | /// <summary> |
| | | 1519 | | /// Multiplies every matrix component by a scalar. |
| | | 1520 | | /// </summary> |
| | | 1521 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1522 | | public static Fixed4x4 operator *(Fixed64 scalar, Fixed4x4 matrix) |
| | 1 | 1523 | | { |
| | 1 | 1524 | | return matrix * scalar; |
| | 1 | 1525 | | } |
| | | 1526 | | |
| | | 1527 | | /// <summary> |
| | | 1528 | | /// Divides every matrix component by a scalar. |
| | | 1529 | | /// </summary> |
| | | 1530 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1531 | | public static Fixed4x4 operator /(Fixed4x4 matrix, Fixed64 scalar) |
| | 2 | 1532 | | { |
| | 2 | 1533 | | Fixed64 inverse = Fixed64.One / scalar; |
| | 2 | 1534 | | return matrix * inverse; |
| | 2 | 1535 | | } |
| | | 1536 | | |
| | | 1537 | | /// <summary> |
| | | 1538 | | /// Determines whether two Fixed4x4 instances are equal. |
| | | 1539 | | /// </summary> |
| | | 1540 | | /// <param name="left">The first Fixed4x4 instance to compare.</param> |
| | | 1541 | | /// <param name="right">The second Fixed4x4 instance to compare.</param> |
| | | 1542 | | /// <returns>true if the specified Fixed4x4 instances are equal; otherwise, false.</returns> |
| | | 1543 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1544 | | public static bool operator ==(Fixed4x4 left, Fixed4x4 right) |
| | 2 | 1545 | | { |
| | 2 | 1546 | | return left.Equals(right); |
| | 2 | 1547 | | } |
| | | 1548 | | |
| | | 1549 | | /// <summary> |
| | | 1550 | | /// Determines whether two Fixed4x4 instances are not equal. |
| | | 1551 | | /// </summary> |
| | | 1552 | | /// <param name="left">The first Fixed4x4 instance to compare.</param> |
| | | 1553 | | /// <param name="right">The second Fixed4x4 instance to compare.</param> |
| | | 1554 | | /// <returns>true if the specified Fixed4x4 instances are not equal; otherwise, false.</returns> |
| | | 1555 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1556 | | public static bool operator !=(Fixed4x4 left, Fixed4x4 right) |
| | 1 | 1557 | | { |
| | 1 | 1558 | | return !(left == right); |
| | 1 | 1559 | | } |
| | | 1560 | | |
| | | 1561 | | #endregion |
| | | 1562 | | |
| | | 1563 | | #region Equality and HashCode Overrides |
| | | 1564 | | |
| | | 1565 | | /// <inheritdoc/> |
| | | 1566 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1567 | | public override bool Equals(object? obj) |
| | 5 | 1568 | | { |
| | 5 | 1569 | | return obj is Fixed4x4 x && Equals(x); |
| | 5 | 1570 | | } |
| | | 1571 | | |
| | | 1572 | | /// <inheritdoc/> |
| | | 1573 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1574 | | public bool Equals(Fixed4x4 other) |
| | 32 | 1575 | | { |
| | 32 | 1576 | | return m00 == other.m00 && m01 == other.m01 && m02 == other.m02 && m03 == other.m03 && |
| | 32 | 1577 | | m10 == other.m10 && m11 == other.m11 && m12 == other.m12 && m13 == other.m13 && |
| | 32 | 1578 | | m20 == other.m20 && m21 == other.m21 && m22 == other.m22 && m23 == other.m23 && |
| | 32 | 1579 | | m30 == other.m30 && m31 == other.m31 && m32 == other.m32 && m33 == other.m33; |
| | 32 | 1580 | | } |
| | | 1581 | | |
| | | 1582 | | /// <inheritdoc/> |
| | | 1583 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1584 | | public override int GetHashCode() |
| | 4 | 1585 | | { |
| | | 1586 | | unchecked |
| | 4 | 1587 | | { |
| | 4 | 1588 | | int hash = 17; |
| | 4 | 1589 | | hash = hash * 23 + m00.GetHashCode(); |
| | 4 | 1590 | | hash = hash * 23 + m01.GetHashCode(); |
| | 4 | 1591 | | hash = hash * 23 + m02.GetHashCode(); |
| | 4 | 1592 | | hash = hash * 23 + m03.GetHashCode(); |
| | 4 | 1593 | | hash = hash * 23 + m10.GetHashCode(); |
| | 4 | 1594 | | hash = hash * 23 + m11.GetHashCode(); |
| | 4 | 1595 | | hash = hash * 23 + m12.GetHashCode(); |
| | 4 | 1596 | | hash = hash * 23 + m13.GetHashCode(); |
| | 4 | 1597 | | hash = hash * 23 + m20.GetHashCode(); |
| | 4 | 1598 | | hash = hash * 23 + m21.GetHashCode(); |
| | 4 | 1599 | | hash = hash * 23 + m22.GetHashCode(); |
| | 4 | 1600 | | hash = hash * 23 + m23.GetHashCode(); |
| | 4 | 1601 | | hash = hash * 23 + m30.GetHashCode(); |
| | 4 | 1602 | | hash = hash * 23 + m31.GetHashCode(); |
| | 4 | 1603 | | hash = hash * 23 + m32.GetHashCode(); |
| | 4 | 1604 | | hash = hash * 23 + m33.GetHashCode(); |
| | 4 | 1605 | | return hash; |
| | | 1606 | | } |
| | 4 | 1607 | | } |
| | | 1608 | | |
| | | 1609 | | #endregion |
| | | 1610 | | |
| | | 1611 | | #region Private Helpers |
| | | 1612 | | |
| | | 1613 | | private static Fixed4x4 FromRotationMatrix(Fixed3x3 matrix) |
| | 3 | 1614 | | { |
| | 3 | 1615 | | return new Fixed4x4( |
| | 3 | 1616 | | matrix.m00, matrix.m01, matrix.m02, Fixed64.Zero, |
| | 3 | 1617 | | matrix.m10, matrix.m11, matrix.m12, Fixed64.Zero, |
| | 3 | 1618 | | matrix.m20, matrix.m21, matrix.m22, Fixed64.Zero, |
| | 3 | 1619 | | Fixed64.Zero, Fixed64.Zero, Fixed64.Zero, Fixed64.One); |
| | 3 | 1620 | | } |
| | | 1621 | | |
| | | 1622 | | private static void ValidateDepthRange(Fixed64 nearPlaneDistance, Fixed64 farPlaneDistance) |
| | 6 | 1623 | | { |
| | 6 | 1624 | | if (nearPlaneDistance < Fixed64.Zero) |
| | 2 | 1625 | | throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance), nearPlaneDistance, "Near plane distance mus |
| | | 1626 | | |
| | 4 | 1627 | | if (farPlaneDistance <= nearPlaneDistance) |
| | 2 | 1628 | | throw new ArgumentOutOfRangeException(nameof(farPlaneDistance), farPlaneDistance, "Far plane distance must b |
| | 2 | 1629 | | } |
| | | 1630 | | |
| | | 1631 | | private static void ValidatePerspectiveDepthRange(Fixed64 nearPlaneDistance, Fixed64 farPlaneDistance) |
| | 10 | 1632 | | { |
| | 10 | 1633 | | if (nearPlaneDistance <= Fixed64.Zero) |
| | 3 | 1634 | | throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance), nearPlaneDistance, "Near plane distance mus |
| | | 1635 | | |
| | 7 | 1636 | | if (farPlaneDistance <= nearPlaneDistance) |
| | 3 | 1637 | | throw new ArgumentOutOfRangeException(nameof(farPlaneDistance), farPlaneDistance, "Far plane distance must b |
| | 4 | 1638 | | } |
| | | 1639 | | |
| | | 1640 | | #endregion |
| | | 1641 | | } |