FrenetFrame.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/ParametricCurve.h>
  9. #include <Mathematics/Vector2.h>
  10. #include <Mathematics/Vector3.h>
  11. #include <memory>
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class FrenetFrame2
  16. {
  17. public:
  18. // Construction. The curve must persist as long as the FrenetFrame2
  19. // object does.
  20. FrenetFrame2(std::shared_ptr<ParametricCurve<2, Real>> const& curve)
  21. :
  22. mCurve(curve)
  23. {
  24. }
  25. // The normal is perpendicular to the tangent, rotated clockwise by
  26. // pi/2 radians.
  27. void operator()(Real t, Vector2<Real>& position, Vector2<Real>& tangent,
  28. Vector2<Real>& normal) const
  29. {
  30. std::array<Vector2<Real>, 2> jet;
  31. mCurve->Evaluate(t, 1, jet.data());
  32. position = jet[0];
  33. tangent = jet[1];
  34. Normalize(tangent);
  35. normal = Perp(tangent);
  36. }
  37. Real GetCurvature(Real t) const
  38. {
  39. std::array<Vector2<Real>, 3> jet;
  40. mCurve->Evaluate(t, 2, jet.data());
  41. Real speedSqr = Dot(jet[1], jet[1]);
  42. if (speedSqr > (Real)0)
  43. {
  44. Real numer = DotPerp(jet[1], jet[2]);
  45. Real denom = std::pow(speedSqr, (Real)1.5);
  46. return numer / denom;
  47. }
  48. else
  49. {
  50. // Curvature is indeterminate, just return 0.
  51. return (Real)0;
  52. }
  53. }
  54. private:
  55. std::shared_ptr<ParametricCurve<2, Real>> mCurve;
  56. };
  57. template <typename Real>
  58. class FrenetFrame3
  59. {
  60. public:
  61. // Construction. The curve must persist as long as the FrenetFrame3
  62. // object does.
  63. FrenetFrame3(std::shared_ptr<ParametricCurve<3, Real>> const& curve)
  64. :
  65. mCurve(curve)
  66. {
  67. }
  68. // The binormal is Cross(tangent, normal).
  69. void operator()(Real t, Vector3<Real>& position, Vector3<Real>& tangent,
  70. Vector3<Real>& normal, Vector3<Real>& binormal) const
  71. {
  72. std::array<Vector3<Real>, 3> jet;
  73. mCurve->Evaluate(t, 2, jet.data());
  74. position = jet[0];
  75. Real VDotV = Dot(jet[1], jet[1]);
  76. Real VDotA = Dot(jet[1], jet[2]);
  77. normal = VDotV * jet[2] - VDotA * jet[1];
  78. Normalize(normal);
  79. tangent = jet[1];
  80. Normalize(tangent);
  81. binormal = Cross(tangent, normal);
  82. }
  83. Real GetCurvature(Real t) const
  84. {
  85. std::array<Vector3<Real>, 3> jet;
  86. mCurve->Evaluate(t, 2, jet.data());
  87. Real speedSqr = Dot(jet[1], jet[1]);
  88. if (speedSqr > (Real)0)
  89. {
  90. Real numer = Length(Cross(jet[1], jet[2]));
  91. Real denom = std::pow(speedSqr, (Real)1.5);
  92. return numer / denom;
  93. }
  94. else
  95. {
  96. // Curvature is indeterminate, just return 0.
  97. return (Real)0;
  98. }
  99. }
  100. Real GetTorsion(Real t) const
  101. {
  102. std::array<Vector3<Real>, 4> jet;
  103. mCurve->Evaluate(t, 3, jet.data());
  104. Vector3<Real> cross = Cross(jet[1], jet[2]);
  105. Real denom = Dot(cross, cross);
  106. if (denom > (Real)0)
  107. {
  108. Real numer = Dot(cross, jet[3]);
  109. return numer / denom;
  110. }
  111. else
  112. {
  113. // Torsion is indeterminate, just return 0.
  114. return (Real)0;
  115. }
  116. }
  117. private:
  118. std::shared_ptr<ParametricCurve<3, Real>> mCurve;
  119. };
  120. }