ScaleHelpers.h 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/PhysicsSettings.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Helper functions to get properties of a scaling vector
  8. namespace ScaleHelpers
  9. {
  10. /// Minimum valid scale value. This is used to prevent division by zero when scaling a shape with a zero scale.
  11. static constexpr float cMinScale = 1.0e-6f;
  12. /// The tolerance used to check if components of the scale vector are the same
  13. static constexpr float cScaleToleranceSq = 1.0e-8f;
  14. /// Test if a scale is identity
  15. inline bool IsNotScaled(Vec3Arg inScale) { return inScale.IsClose(Vec3::sOne(), cScaleToleranceSq); }
  16. /// Test if a scale is uniform
  17. inline bool IsUniformScale(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>().IsClose(inScale, cScaleToleranceSq); }
  18. /// Test if a scale is uniform in XZ
  19. inline bool IsUniformScaleXZ(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>().IsClose(inScale, ScaleHelpers::cScaleToleranceSq); }
  20. /// Scale the convex radius of an object
  21. inline float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }
  22. /// Test if a scale flips an object inside out (which requires flipping all normals and polygon windings)
  23. inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }
  24. /// Test if any of the components of the scale have a value below cMinScale
  25. inline bool IsZeroScale(Vec3Arg inScale) { return Vec3::sLess(inScale.Abs(), Vec3::sReplicate(cMinScale)).TestAnyXYZTrue(); }
  26. /// Ensure that the scale for each component is at least cMinScale
  27. inline Vec3 MakeNonZeroScale(Vec3Arg inScale) { return inScale.GetSign() * Vec3::sMax(inScale.Abs(), Vec3::sReplicate(cMinScale)); }
  28. /// Get the average scale if inScale, used to make the scale uniform when a shape doesn't support non-uniform scale
  29. inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }
  30. /// Average the scale in XZ, used to make the scale uniform when a shape doesn't support non-uniform scale in the XZ plane
  31. inline Vec3 MakeUniformScaleXZ(Vec3Arg inScale) { return 0.5f * (inScale + inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>()); }
  32. /// Checks in scale can be rotated to child shape
  33. /// @param inRotation Rotation of child shape
  34. /// @param inScale Scale in local space of parent shape
  35. /// @return True if the scale is valid (no shearing introduced)
  36. inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
  37. {
  38. // inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.
  39. // when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).
  40. // Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation
  41. // If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.
  42. Mat44 r = Mat44::sRotation(inRotation);
  43. Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));
  44. // Get the columns, but zero the diagonal
  45. Vec4 zero = Vec4::sZero();
  46. Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();
  47. Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();
  48. Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();
  49. // Check if all elements are less than epsilon
  50. Vec4 epsilon = Vec4::sReplicate(1.0e-6f);
  51. return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();
  52. }
  53. /// Adjust scale for rotated child shape
  54. /// @param inRotation Rotation of child shape
  55. /// @param inScale Scale in local space of parent shape
  56. /// @return Rotated scale
  57. inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
  58. {
  59. // Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)
  60. Mat44 r = Mat44::sRotation(inRotation);
  61. return r.Multiply3x3LeftTransposed(r.PostScaled(inScale)).GetDiagonal3();
  62. }
  63. }
  64. JPH_NAMESPACE_END