ScaleHelpers.h 3.4 KB

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