WhiteBoxMathUtil.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "WhiteBoxMathUtil.h"
  9. #include <AzCore/Math/Matrix3x3.h>
  10. #include <AzCore/Math/Transform.h>
  11. #include <AzCore/Math/Vector3.h>
  12. namespace WhiteBox
  13. {
  14. //! Reference: Real-Time Collision Detection - 5.3.7 Intersecting Ray or Segment Against Cylinder
  15. //! @note the parameters and style here are copied verbatim from the book
  16. //! to make comparison and bug finding trivial.
  17. bool IntersectSegmentCylinder(
  18. const AZ::Vector3& sa, const AZ::Vector3& sb, const AZ::Vector3& p, const AZ::Vector3& q, float r, float& t)
  19. {
  20. // clang-format off
  21. const AZ::Vector3 d = q - p;
  22. const AZ::Vector3 m = sa - p;
  23. const AZ::Vector3 n = sb - sa;
  24. const float md = m.Dot(d);
  25. const float nd = n.Dot(d);
  26. const float dd = d.Dot(d);
  27. if (md < 0.0f && md + nd < 0.0f) return false;
  28. if (md > dd && md + nd > dd) return false;
  29. const float nn = n.Dot(n);
  30. const float mn = m.Dot(n);
  31. const float a = dd * nn - nd * nd;
  32. const float k = m.Dot(m) - r * r;
  33. const float c = dd * k - md * md;
  34. if (AZ::GetAbs(a) < AZ::Constants::FloatEpsilon) {
  35. if (c > 0.0f) return false;
  36. if (md < 0.0f) t = -mn / nn;
  37. else if (md > dd) t = -mn / nn;
  38. else t = 0.0f;
  39. return true;
  40. }
  41. const float b = dd * mn - nd * md;
  42. const float discr = b * b - a * c;
  43. if (discr < 0.0f) return false;
  44. t = (-b - sqrt(discr)) / a;
  45. if (t < 0.0f || t > 1.0f) return false;
  46. if (md + t * nd < 0.0f) {
  47. if (nd <= 0.0f) return false;
  48. t = -md / nd;
  49. return k + 2 * t * (mn + t * nn) <= 0.0f;
  50. }
  51. else if (md + t * nd > dd) {
  52. if (nd >= 0.0f) return false;
  53. t = (dd - md) / nd;
  54. return k + dd - 2 * md + t * (2 * (mn - nd) + t * nn) <= 0.0f;
  55. }
  56. return true;
  57. // clang-format on
  58. }
  59. AZ::Vector3 ScalePosition(const float scale, const AZ::Vector3& localPosition, const AZ::Transform& localFromSpace)
  60. {
  61. const AZ::Transform spaceFromLocal = localFromSpace.GetInverse();
  62. const AZ::Vector3 spacePosition = spaceFromLocal.TransformPoint(localPosition);
  63. const AZ::Vector3 spaceScaledPosition =
  64. AZ::Transform::CreateUniformScale(scale).TransformPoint(spacePosition);
  65. return localFromSpace.TransformPoint(spaceScaledPosition);
  66. }
  67. void CalculateOrthonormalBasis(const AZ::Vector3& n, AZ::Vector3& b1, AZ::Vector3& b2)
  68. {
  69. // choose a vector orthogonal to n as the direction of b2
  70. if (fabsf(n.GetX()) > fabs(n.GetZ()))
  71. {
  72. b2 = AZ::Vector3(-n.GetY(), n.GetX(), 0.0f);
  73. }
  74. else
  75. {
  76. b2 = AZ::Vector3(0.0f, -n.GetZ(), n.GetY());
  77. }
  78. b2.Normalize();
  79. b1 = b2.Cross(n);
  80. }
  81. AZ::Quaternion CalculateLocalOrientation(const AZ::Vector3& normal)
  82. {
  83. AZ::Vector3 b1, b2;
  84. CalculateOrthonormalBasis(normal, b1, b2);
  85. const AZ::Matrix3x3 mat = AZ::Matrix3x3::CreateFromColumns(normal, b1, b2);
  86. return AZ::Quaternion::CreateFromMatrix3x3(mat);
  87. }
  88. } // namespace WhiteBox