| | | 1 | | //======================================================================= |
| | | 2 | | // FixedTransform.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a mutable deterministic transform snapshot with optional hierarchy composition. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// This engine-neutral shell stores local components and a parent reference only. It does not |
| | | 17 | | /// own children, scene state, synchronization, caches, or dirty propagation. |
| | | 18 | | /// </remarks> |
| | | 19 | | public class FixedTransform |
| | | 20 | | { |
| | | 21 | | private Vector3d _localPosition; |
| | | 22 | | private FixedQuaternion _localRotation; |
| | | 23 | | private Vector3d _localScale; |
| | | 24 | | private FixedTransform? _parent; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a transform from authoritative 3D local components. |
| | | 28 | | /// </summary> |
| | 10084 | 29 | | public FixedTransform( |
| | 10084 | 30 | | Vector3d localPosition, |
| | 10084 | 31 | | FixedQuaternion localRotation, |
| | 10084 | 32 | | Vector3d localScale, |
| | 10084 | 33 | | FixedTransform? parent = null) |
| | | 34 | | { |
| | 10084 | 35 | | _localPosition = localPosition; |
| | 10084 | 36 | | _localRotation = localRotation.Normalized; |
| | 10084 | 37 | | _localScale = localScale; |
| | 10084 | 38 | | _parent = parent; |
| | 10084 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes an X/Z local transform at zero Y position with unit Y scale. |
| | | 43 | | /// </summary> |
| | | 44 | | public FixedTransform( |
| | | 45 | | Vector2d localPositionXZ, |
| | | 46 | | Fixed64 localRotationXZRadians, |
| | | 47 | | Vector2d localScaleXZ, |
| | | 48 | | FixedTransform? parent = null) |
| | 13 | 49 | | : this( |
| | 13 | 50 | | localPositionXZ.ToVector3d(Fixed64.Zero), |
| | 13 | 51 | | FixedQuaternion.FromAxisAngle(Vector3d.Up, -localRotationXZRadians), |
| | 13 | 52 | | localScaleXZ.ToVector3d(Fixed64.One), |
| | 13 | 53 | | parent) |
| | 13 | 54 | | { } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets the authoritative local translation. |
| | | 58 | | /// </summary> |
| | | 59 | | public Vector3d LocalPosition |
| | | 60 | | { |
| | 29 | 61 | | get => _localPosition; |
| | 2 | 62 | | set => _localPosition = value; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets the authoritative normalized local rotation. |
| | | 67 | | /// </summary> |
| | | 68 | | public FixedQuaternion LocalRotation |
| | | 69 | | { |
| | 36 | 70 | | get => _localRotation; |
| | 4 | 71 | | set => _localRotation = value.Normalized; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets or sets the authoritative exact signed local scale. |
| | | 76 | | /// </summary> |
| | | 77 | | public Vector3d LocalScale |
| | | 78 | | { |
| | 24 | 79 | | get => _localScale; |
| | 2 | 80 | | set => _localScale = value; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets or sets the local rotation as Euler angles in degrees. |
| | | 85 | | /// </summary> |
| | | 86 | | public Vector3d LocalEulerAngles |
| | | 87 | | { |
| | 1 | 88 | | get => _localRotation.EulerAngles; |
| | 1 | 89 | | set => LocalRotation = FixedQuaternion.FromEulerAnglesInDegrees(value.X, value.Y, value.Z); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets or sets local X/Z position while preserving local Y. |
| | | 94 | | /// </summary> |
| | | 95 | | public Vector2d LocalPositionXZ |
| | | 96 | | { |
| | 1 | 97 | | get => _localPosition.ToVector2d(); |
| | 1 | 98 | | set => _localPosition = value.ToVector3d(_localPosition.Y); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Gets or sets local X/Z rotation in radians from planar right toward planar forward. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <remarks>The setter replaces pitch and roll with a pure Y-axis rotation.</remarks> |
| | | 105 | | public Fixed64 LocalRotationXZRadians |
| | | 106 | | { |
| | | 107 | | get |
| | | 108 | | { |
| | 8 | 109 | | Vector3d localRight = _localRotation.Rotate(Vector3d.Right); |
| | 8 | 110 | | return FixedMath.Atan2(localRight.Z, localRight.X); |
| | | 111 | | } |
| | 2 | 112 | | set => LocalRotation = FixedQuaternion.FromAxisAngle(Vector3d.Up, -value); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets or sets local X/Z scale while preserving local Y. |
| | | 117 | | /// </summary> |
| | | 118 | | public Vector2d LocalScaleXZ |
| | | 119 | | { |
| | 1 | 120 | | get => _localScale.ToVector2d(); |
| | 1 | 121 | | set => _localScale = value.ToVector3d(_localScale.Y); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets the optional parent transform. |
| | | 126 | | /// </summary> |
| | 15 | 127 | | public FixedTransform? Parent => _parent; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the matrix rebuilt from the authoritative local components. |
| | | 131 | | /// </summary> |
| | 28505 | 132 | | public Fixed4x4 LocalMatrix => Fixed4x4.CreateTransform(_localPosition, _localRotation, _localScale); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the iteratively composed local-to-world matrix. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <remarks> |
| | | 138 | | /// Row-vector order multiplies this local matrix by its parent local matrix and then each |
| | | 139 | | /// ancestor local matrix. Reading is linear in hierarchy depth and allocation-free. |
| | | 140 | | /// </remarks> |
| | | 141 | | public Fixed4x4 LocalToWorldMatrix |
| | | 142 | | { |
| | | 143 | | get |
| | | 144 | | { |
| | 2086 | 145 | | Fixed4x4 matrix = LocalMatrix; |
| | 2086 | 146 | | FixedTransform? ancestor = _parent; |
| | 28493 | 147 | | while (ancestor != null) |
| | | 148 | | { |
| | 26407 | 149 | | matrix *= ancestor.LocalMatrix; |
| | 26407 | 150 | | ancestor = ancestor._parent; |
| | | 151 | | } |
| | | 152 | | |
| | 2086 | 153 | | return matrix; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Attempts to get the iteratively composed local-to-world matrix without intermediate saturation. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <remarks>Failure returns a zero matrix and does not mutate this transform or any ancestor.</remarks> |
| | | 161 | | public bool TryGetLocalToWorldMatrix(out Fixed4x4 matrix) |
| | | 162 | | { |
| | 4391 | 163 | | if (!TryGetStrictLocalMatrix(out matrix)) |
| | 6 | 164 | | return false; |
| | | 165 | | |
| | 4385 | 166 | | FixedTransform? ancestor = _parent; |
| | 4399 | 167 | | while (ancestor != null) |
| | | 168 | | { |
| | 17 | 169 | | if (!ancestor.TryGetStrictLocalMatrix(out Fixed4x4 localMatrix) |
| | 17 | 170 | | || !Fixed4x4.TryMultiply(matrix, localMatrix, out Fixed4x4 combined)) |
| | | 171 | | { |
| | 3 | 172 | | matrix = Fixed4x4.Zero; |
| | 3 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | |
| | 14 | 176 | | matrix = combined; |
| | 14 | 177 | | ancestor = ancestor._parent; |
| | | 178 | | } |
| | | 179 | | |
| | 4382 | 180 | | return true; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets world translation from the composed matrix. |
| | | 185 | | /// </summary> |
| | 7 | 186 | | public Vector3d WorldPosition => _parent == null |
| | 7 | 187 | | ? _localPosition |
| | 7 | 188 | | : LocalToWorldMatrix.Translation; |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets the normalized rotational parent-to-child quaternion chain. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <remarks> |
| | | 194 | | /// This is an orientation chain derived only from stored local quaternions. It is independent |
| | | 195 | | /// of scale, reflections, and any shear in <see cref="LocalToWorldMatrix"/>, so it is not a |
| | | 196 | | /// decomposition of that matrix. |
| | | 197 | | /// </remarks> |
| | | 198 | | public FixedQuaternion WorldRotation |
| | | 199 | | { |
| | | 200 | | get |
| | | 201 | | { |
| | 8 | 202 | | if (_parent == null) |
| | 3 | 203 | | return _localRotation; |
| | | 204 | | |
| | 5 | 205 | | FixedQuaternion rotation = _localRotation; |
| | 5 | 206 | | FixedTransform? ancestor = _parent; |
| | 12 | 207 | | while (ancestor != null) |
| | | 208 | | { |
| | 7 | 209 | | rotation = ancestor._localRotation * rotation; |
| | 7 | 210 | | ancestor = ancestor._parent; |
| | | 211 | | } |
| | | 212 | | |
| | 5 | 213 | | return rotation.Normalized; |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Gets canonical signed lossy scale from the composed matrix. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <remarks>Zero magnitudes are preserved and reflected handedness is canonicalized to negative X.</remarks> |
| | 5 | 221 | | public Vector3d LossyScale => _parent == null |
| | 5 | 222 | | && _localScale.X >= Fixed64.Zero |
| | 5 | 223 | | && _localScale.Y >= Fixed64.Zero |
| | 5 | 224 | | && _localScale.Z >= Fixed64.Zero |
| | 5 | 225 | | ? _localScale |
| | 5 | 226 | | : LocalToWorldMatrix.LossyScale; |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Attempts to get canonical signed lossy scale from strict hierarchy composition. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <remarks>Failure returns zero and does not mutate this transform or any ancestor.</remarks> |
| | | 232 | | public bool TryGetLossyScale(out Vector3d scale) |
| | | 233 | | { |
| | 6 | 234 | | if (_parent == null |
| | 6 | 235 | | && _localScale.X >= Fixed64.Zero |
| | 6 | 236 | | && _localScale.Y >= Fixed64.Zero |
| | 6 | 237 | | && _localScale.Z >= Fixed64.Zero) |
| | | 238 | | { |
| | 1 | 239 | | scale = _localScale; |
| | 1 | 240 | | return true; |
| | | 241 | | } |
| | | 242 | | |
| | 5 | 243 | | if (!TryGetLocalToWorldMatrix(out Fixed4x4 matrix)) |
| | | 244 | | { |
| | 2 | 245 | | scale = Vector3d.Zero; |
| | 2 | 246 | | return false; |
| | | 247 | | } |
| | | 248 | | |
| | 3 | 249 | | return Fixed4x4.TryExtractLossyScale(matrix, out scale); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Gets world position projected onto X/Z. |
| | | 254 | | /// </summary> |
| | 2 | 255 | | public Vector2d WorldPositionXZ => WorldPosition.ToVector2d(); |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Gets world X/Z rotation in radians from planar right toward planar forward. |
| | | 259 | | /// </summary> |
| | | 260 | | public Fixed64 WorldRotationXZRadians |
| | | 261 | | { |
| | | 262 | | get |
| | | 263 | | { |
| | 1 | 264 | | Vector3d worldRight = WorldRotation.Rotate(Vector3d.Right); |
| | 1 | 265 | | return FixedMath.Atan2(worldRight.Z, worldRight.X); |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Attempts to get a verified inverse of the composed local-to-world matrix. |
| | | 271 | | /// </summary> |
| | | 272 | | /// <remarks> |
| | | 273 | | /// The inverse candidate must multiply to identity in both orders within |
| | | 274 | | /// <see cref="FixedMath.CanonicalSinCosErrorBound"/>. Singular, saturated, |
| | | 275 | | /// and unrepresentable candidates return identity and false. |
| | | 276 | | /// </remarks> |
| | | 277 | | public bool TryGetWorldToLocalMatrix(out Fixed4x4 matrix) |
| | | 278 | | { |
| | 1099 | 279 | | if (!TryGetLocalToWorldMatrix(out Fixed4x4 world)) |
| | | 280 | | { |
| | 1 | 281 | | matrix = Fixed4x4.Identity; |
| | 1 | 282 | | return false; |
| | | 283 | | } |
| | | 284 | | |
| | 1098 | 285 | | return TryGetVerifiedInverse(world, out matrix); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Transforms a point from local space to world space through the complete composed affine hierarchy. |
| | | 290 | | /// </summary> |
| | | 291 | | /// <exception cref="InvalidOperationException"> |
| | | 292 | | /// The composed transform or final world-space point is not representable. |
| | | 293 | | /// </exception> |
| | | 294 | | public Vector3d TransformPoint(Vector3d point) |
| | | 295 | | { |
| | 2 | 296 | | if (!TryTransformPoint(point, out Vector3d result)) |
| | 1 | 297 | | throw new InvalidOperationException( |
| | 1 | 298 | | "The composed transform or final world-space point is not representable."); |
| | | 299 | | |
| | 1 | 300 | | return result; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Attempts to transform a point from local space to world space through the complete composed affine hierarchy. |
| | | 305 | | /// </summary> |
| | | 306 | | public bool TryTransformPoint(Vector3d point, out Vector3d result) |
| | | 307 | | { |
| | 1093 | 308 | | if (!TryGetLocalToWorldMatrix(out Fixed4x4 matrix)) |
| | | 309 | | { |
| | 2 | 310 | | result = default; |
| | 2 | 311 | | return false; |
| | | 312 | | } |
| | | 313 | | |
| | 1091 | 314 | | return Fixed4x4.TryTransformAffinePoint(matrix, point, out result); |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Transforms a point from world space to local space through the complete composed affine hierarchy. |
| | | 319 | | /// </summary> |
| | | 320 | | /// <exception cref="InvalidOperationException"> |
| | | 321 | | /// The composed transform is singular or unrepresentable, or the final local-space point is not representable. |
| | | 322 | | /// </exception> |
| | | 323 | | public Vector3d InverseTransformPoint(Vector3d point) |
| | | 324 | | { |
| | 2 | 325 | | if (!TryInverseTransformPoint(point, out Vector3d result)) |
| | 1 | 326 | | throw new InvalidOperationException( |
| | 1 | 327 | | "The composed transform is singular or unrepresentable, or the final local-space point is not representa |
| | | 328 | | |
| | 1 | 329 | | return result; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Attempts to transform a point from world space to local space through the complete composed affine hierarchy. |
| | | 334 | | /// </summary> |
| | | 335 | | public bool TryInverseTransformPoint(Vector3d point, out Vector3d result) |
| | | 336 | | { |
| | 1092 | 337 | | if (!TryGetWorldToLocalMatrix(out Fixed4x4 matrix)) |
| | | 338 | | { |
| | 2 | 339 | | result = default; |
| | 2 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | 1090 | 343 | | return Fixed4x4.TryTransformAffinePoint(matrix, point, out result); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Transforms a point from local X/Z space to world X/Z space through a plane-preserving affine hierarchy. |
| | | 348 | | /// </summary> |
| | | 349 | | /// <exception cref="InvalidOperationException"> |
| | | 350 | | /// The hierarchy couples X/Z with Y, or the composed transform or final world-space point is not representable. |
| | | 351 | | /// </exception> |
| | | 352 | | public Vector2d TransformPointXZ(Vector2d point) |
| | | 353 | | { |
| | 2 | 354 | | if (!TryTransformPointXZ(point, out Vector2d result)) |
| | 1 | 355 | | throw new InvalidOperationException( |
| | 1 | 356 | | "The hierarchy must preserve the X/Z plane and produce a representable world-space point."); |
| | | 357 | | |
| | 1 | 358 | | return result; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Attempts to transform a point from local X/Z space to world X/Z space through a plane-preserving affine hierarch |
| | | 363 | | /// </summary> |
| | | 364 | | public bool TryTransformPointXZ(Vector2d point, out Vector2d result) |
| | | 365 | | { |
| | 1094 | 366 | | if (!TryGetPlanarXZMatrix(out Fixed4x4 matrix) |
| | 1094 | 367 | | || !Fixed4x4.TryTransformAffinePoint( |
| | 1094 | 368 | | matrix, |
| | 1094 | 369 | | point.ToVector3d(Fixed64.Zero), |
| | 1094 | 370 | | out Vector3d transformed)) |
| | | 371 | | { |
| | 4 | 372 | | result = default; |
| | 4 | 373 | | return false; |
| | | 374 | | } |
| | | 375 | | |
| | 1090 | 376 | | result = transformed.ToVector2d(); |
| | 1090 | 377 | | return true; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// Transforms a point from world X/Z space to local X/Z space through a plane-preserving affine hierarchy. |
| | | 382 | | /// </summary> |
| | | 383 | | /// <exception cref="InvalidOperationException"> |
| | | 384 | | /// The hierarchy couples X/Z with Y, is singular or unrepresentable, or the final local-space point is not represen |
| | | 385 | | /// </exception> |
| | | 386 | | public Vector2d InverseTransformPointXZ(Vector2d point) |
| | | 387 | | { |
| | 2 | 388 | | if (!TryInverseTransformPointXZ(point, out Vector2d result)) |
| | 1 | 389 | | throw new InvalidOperationException( |
| | 1 | 390 | | "The hierarchy must preserve an invertible X/Z plane and produce a representable local-space point."); |
| | | 391 | | |
| | 1 | 392 | | return result; |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Attempts to transform a point from world X/Z space to local X/Z space through a plane-preserving affine hierarch |
| | | 397 | | /// </summary> |
| | | 398 | | public bool TryInverseTransformPointXZ(Vector2d point, out Vector2d result) |
| | | 399 | | { |
| | 1095 | 400 | | if (!TryGetPlanarXZMatrix(out Fixed4x4 matrix) |
| | 1095 | 401 | | || !TryGetVerifiedInverse(matrix, out Fixed4x4 inverse) |
| | 1095 | 402 | | || !Fixed4x4.TryTransformAffinePoint( |
| | 1095 | 403 | | inverse, |
| | 1095 | 404 | | point.ToVector3d(Fixed64.Zero), |
| | 1095 | 405 | | out Vector3d transformed)) |
| | | 406 | | { |
| | 5 | 407 | | result = default; |
| | 5 | 408 | | return false; |
| | | 409 | | } |
| | | 410 | | |
| | 1090 | 411 | | result = transformed.ToVector2d(); |
| | 1090 | 412 | | return true; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Changes the parent without changing any local component. |
| | | 417 | | /// </summary> |
| | | 418 | | /// <exception cref="ArgumentException">The proposed parent would create a cycle.</exception> |
| | | 419 | | public void SetParentKeepingLocal(FixedTransform? parent) |
| | | 420 | | { |
| | 5 | 421 | | if (ReferenceEquals(_parent, parent)) |
| | 1 | 422 | | return; |
| | | 423 | | |
| | 4 | 424 | | ValidateParent(parent); |
| | 2 | 425 | | _parent = parent; |
| | 2 | 426 | | } |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Attempts to change the parent while preserving the complete world matrix. |
| | | 430 | | /// </summary> |
| | | 431 | | /// <remarks>Failure leaves the parent and every local component unchanged.</remarks> |
| | | 432 | | /// <exception cref="ArgumentException">The proposed parent would create a cycle.</exception> |
| | | 433 | | public bool TrySetParentKeepingWorld(FixedTransform? parent) |
| | | 434 | | { |
| | 7 | 435 | | if (ReferenceEquals(_parent, parent)) |
| | 1 | 436 | | return true; |
| | | 437 | | |
| | 6 | 438 | | ValidateParent(parent); |
| | 5 | 439 | | Fixed4x4 world = LocalToWorldMatrix; |
| | 5 | 440 | | Fixed4x4 parentWorld = parent?.LocalToWorldMatrix ?? Fixed4x4.Identity; |
| | | 441 | | Fixed4x4 localMatrix; |
| | 5 | 442 | | if (parent == null) |
| | | 443 | | { |
| | 1 | 444 | | localMatrix = world; |
| | | 445 | | } |
| | | 446 | | else |
| | | 447 | | { |
| | 4 | 448 | | if (!TryGetVerifiedInverse(parentWorld, out Fixed4x4 parentInverse)) |
| | 1 | 449 | | return false; |
| | | 450 | | |
| | 3 | 451 | | localMatrix = world * parentInverse; |
| | | 452 | | } |
| | | 453 | | |
| | 4 | 454 | | if (!Fixed4x4.Decompose( |
| | 4 | 455 | | localMatrix, |
| | 4 | 456 | | out Vector3d localPosition, |
| | 4 | 457 | | out FixedQuaternion localRotation, |
| | 4 | 458 | | out Vector3d localScale)) |
| | | 459 | | { |
| | 1 | 460 | | return false; |
| | | 461 | | } |
| | | 462 | | |
| | 3 | 463 | | Fixed4x4 recomposedWorld = Fixed4x4.CreateTransform(localPosition, localRotation, localScale) * parentWorld; |
| | 3 | 464 | | if (!recomposedWorld.FuzzyEqualAbsolute(world, Fixed64.Epsilon)) |
| | 1 | 465 | | return false; |
| | | 466 | | |
| | 2 | 467 | | _parent = parent; |
| | 2 | 468 | | _localPosition = localPosition; |
| | 2 | 469 | | _localRotation = localRotation; |
| | 2 | 470 | | _localScale = localScale; |
| | 2 | 471 | | return true; |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | /// <summary> |
| | | 475 | | /// Attempts to set world position through the verified parent inverse. |
| | | 476 | | /// </summary> |
| | | 477 | | /// <remarks>Failure leaves local position unchanged.</remarks> |
| | | 478 | | public bool TrySetWorldPosition(Vector3d position) |
| | | 479 | | { |
| | 4 | 480 | | if (_parent == null) |
| | | 481 | | { |
| | 1 | 482 | | _localPosition = position; |
| | 1 | 483 | | return true; |
| | | 484 | | } |
| | | 485 | | |
| | 3 | 486 | | Fixed4x4 parentWorld = _parent.LocalToWorldMatrix; |
| | 3 | 487 | | if (!TryGetVerifiedInverse(parentWorld, out Fixed4x4 parentInverse)) |
| | 1 | 488 | | return false; |
| | | 489 | | |
| | 2 | 490 | | Vector3d localPosition = Fixed4x4.TransformPoint(parentInverse, position); |
| | 2 | 491 | | if (!Fixed4x4.TransformPoint(parentWorld, localPosition).FuzzyEqualAbsolute(position, Fixed64.Epsilon)) |
| | 1 | 492 | | return false; |
| | | 493 | | |
| | 1 | 494 | | _localPosition = localPosition; |
| | 1 | 495 | | return true; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | /// <summary> |
| | | 499 | | /// Attempts to set world position and normalized world rotation atomically. |
| | | 500 | | /// </summary> |
| | | 501 | | /// <remarks> |
| | | 502 | | /// Relative rotation is derived from the parent's stored rotational hierarchy, independent |
| | | 503 | | /// of scale and shear. Failure leaves both local position and local rotation unchanged. |
| | | 504 | | /// </remarks> |
| | | 505 | | public bool TrySetWorldPose(Vector3d position, FixedQuaternion rotation) |
| | | 506 | | { |
| | 4 | 507 | | FixedQuaternion normalizedRotation = rotation.Normalized; |
| | 4 | 508 | | if (_parent == null) |
| | | 509 | | { |
| | 1 | 510 | | _localPosition = position; |
| | 1 | 511 | | _localRotation = normalizedRotation; |
| | 1 | 512 | | return true; |
| | | 513 | | } |
| | | 514 | | |
| | 3 | 515 | | Fixed4x4 parentWorld = _parent.LocalToWorldMatrix; |
| | 3 | 516 | | if (!TryGetVerifiedInverse(parentWorld, out Fixed4x4 parentInverse)) |
| | 1 | 517 | | return false; |
| | | 518 | | |
| | 2 | 519 | | Vector3d localPosition = Fixed4x4.TransformPoint(parentInverse, position); |
| | 2 | 520 | | if (!Fixed4x4.TransformPoint(parentWorld, localPosition).FuzzyEqualAbsolute(position, Fixed64.Epsilon)) |
| | 1 | 521 | | return false; |
| | | 522 | | |
| | 1 | 523 | | FixedQuaternion parentWorldRotation = _parent.WorldRotation; |
| | 1 | 524 | | FixedQuaternion localRotation = (parentWorldRotation.Inverse() * normalizedRotation).Normalized; |
| | | 525 | | |
| | 1 | 526 | | _localPosition = localPosition; |
| | 1 | 527 | | _localRotation = localRotation; |
| | 1 | 528 | | return true; |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Attempts to create a transform from a strict, nonsingular local TRS matrix. |
| | | 533 | | /// </summary> |
| | | 534 | | /// <remarks>Perspective, shear, singular, and non-round-trippable inputs return null and false.</remarks> |
| | | 535 | | public static bool TryCreateFromLocalMatrix( |
| | | 536 | | Fixed4x4 localMatrix, |
| | | 537 | | out FixedTransform? transform, |
| | | 538 | | FixedTransform? parent = null) |
| | | 539 | | { |
| | 4 | 540 | | if (!Fixed4x4.Decompose( |
| | 4 | 541 | | localMatrix, |
| | 4 | 542 | | out Vector3d localPosition, |
| | 4 | 543 | | out FixedQuaternion localRotation, |
| | 4 | 544 | | out Vector3d localScale)) |
| | | 545 | | { |
| | 3 | 546 | | transform = null; |
| | 3 | 547 | | return false; |
| | | 548 | | } |
| | | 549 | | |
| | 1 | 550 | | transform = new FixedTransform(localPosition, localRotation, localScale, parent); |
| | 1 | 551 | | return true; |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | private static bool TryGetVerifiedInverse(Fixed4x4 world, out Fixed4x4 inverse) |
| | | 555 | | { |
| | 2200 | 556 | | if (Fixed4x4.Invert(world, out Fixed4x4 candidate) |
| | 2200 | 557 | | && Fixed4x4.TryMultiply(world, candidate, out Fixed4x4 worldRoundTrip) |
| | 2200 | 558 | | && Fixed4x4.TryMultiply(candidate, world, out Fixed4x4 localRoundTrip) |
| | 2200 | 559 | | && worldRoundTrip.FuzzyEqualAbsolute( |
| | 2200 | 560 | | Fixed4x4.Identity, |
| | 2200 | 561 | | FixedMath.CanonicalSinCosErrorBound) |
| | 2200 | 562 | | && localRoundTrip.FuzzyEqualAbsolute( |
| | 2200 | 563 | | Fixed4x4.Identity, |
| | 2200 | 564 | | FixedMath.CanonicalSinCosErrorBound)) |
| | | 565 | | { |
| | 2193 | 566 | | inverse = candidate; |
| | 2193 | 567 | | return true; |
| | | 568 | | } |
| | | 569 | | |
| | 7 | 570 | | inverse = Fixed4x4.Identity; |
| | 7 | 571 | | return false; |
| | | 572 | | } |
| | | 573 | | |
| | | 574 | | private bool TryGetPlanarXZMatrix(out Fixed4x4 matrix) |
| | | 575 | | { |
| | 2189 | 576 | | if (!TryGetLocalToWorldMatrix(out Fixed4x4 world) |
| | 2189 | 577 | | || ((world.M12 != Fixed64.Zero) |
| | 2189 | 578 | | | (world.M21 != Fixed64.Zero) |
| | 2189 | 579 | | | (world.M23 != Fixed64.Zero) |
| | 2189 | 580 | | | (world.M32 != Fixed64.Zero))) |
| | | 581 | | { |
| | 7 | 582 | | matrix = Fixed4x4.Zero; |
| | 7 | 583 | | return false; |
| | | 584 | | } |
| | | 585 | | |
| | 2182 | 586 | | matrix = new Fixed4x4( |
| | 2182 | 587 | | world.M11, Fixed64.Zero, world.M13, Fixed64.Zero, |
| | 2182 | 588 | | Fixed64.Zero, Fixed64.One, Fixed64.Zero, Fixed64.Zero, |
| | 2182 | 589 | | world.M31, Fixed64.Zero, world.M33, Fixed64.Zero, |
| | 2182 | 590 | | world.M41, Fixed64.Zero, world.M43, Fixed64.One); |
| | 2182 | 591 | | return true; |
| | | 592 | | } |
| | | 593 | | |
| | | 594 | | private bool TryGetStrictLocalMatrix(out Fixed4x4 matrix) |
| | | 595 | | { |
| | 4408 | 596 | | Fixed3x3 rotation = _localRotation.ToMatrix3x3(); |
| | 4408 | 597 | | bool representable = Fixed64.TryMultiplyAdd(rotation.M11, _localScale.X, Fixed64.Zero, out Fixed64 m11) |
| | 4408 | 598 | | & Fixed64.TryMultiplyAdd(rotation.M12, _localScale.X, Fixed64.Zero, out Fixed64 m12) |
| | 4408 | 599 | | & Fixed64.TryMultiplyAdd(rotation.M13, _localScale.X, Fixed64.Zero, out Fixed64 m13) |
| | 4408 | 600 | | & Fixed64.TryMultiplyAdd(rotation.M21, _localScale.Y, Fixed64.Zero, out Fixed64 m21) |
| | 4408 | 601 | | & Fixed64.TryMultiplyAdd(rotation.M22, _localScale.Y, Fixed64.Zero, out Fixed64 m22) |
| | 4408 | 602 | | & Fixed64.TryMultiplyAdd(rotation.M23, _localScale.Y, Fixed64.Zero, out Fixed64 m23) |
| | 4408 | 603 | | & Fixed64.TryMultiplyAdd(rotation.M31, _localScale.Z, Fixed64.Zero, out Fixed64 m31) |
| | 4408 | 604 | | & Fixed64.TryMultiplyAdd(rotation.M32, _localScale.Z, Fixed64.Zero, out Fixed64 m32) |
| | 4408 | 605 | | & Fixed64.TryMultiplyAdd(rotation.M33, _localScale.Z, Fixed64.Zero, out Fixed64 m33); |
| | 4408 | 606 | | if (!representable) |
| | | 607 | | { |
| | 7 | 608 | | matrix = Fixed4x4.Zero; |
| | 7 | 609 | | return false; |
| | | 610 | | } |
| | | 611 | | |
| | 4401 | 612 | | matrix = new Fixed4x4( |
| | 4401 | 613 | | m11, m12, m13, Fixed64.Zero, |
| | 4401 | 614 | | m21, m22, m23, Fixed64.Zero, |
| | 4401 | 615 | | m31, m32, m33, Fixed64.Zero, |
| | 4401 | 616 | | _localPosition.X, _localPosition.Y, _localPosition.Z, Fixed64.One); |
| | 4401 | 617 | | return true; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | private void ValidateParent(FixedTransform? parent) |
| | | 621 | | { |
| | 10 | 622 | | FixedTransform? ancestor = parent; |
| | 19 | 623 | | while (ancestor != null) |
| | | 624 | | { |
| | 12 | 625 | | if (ReferenceEquals(ancestor, this)) |
| | 3 | 626 | | throw new ArgumentException("Parent assignment would create a transform cycle.", nameof(parent)); |
| | | 627 | | |
| | 9 | 628 | | ancestor = ancestor._parent; |
| | | 629 | | } |
| | 7 | 630 | | } |
| | | 631 | | } |