IntrAlignedBox3Cylinder3.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.10.23
  7. #pragma once
  8. #include <Mathematics/TIQuery.h>
  9. #include <Mathematics/AlignedBox.h>
  10. #include <Mathematics/LCPSolver.h>
  11. #include <Mathematics/Cylinder3.h>
  12. #include <Mathematics/Matrix3x3.h>
  13. // The query considers the cylinder and box to be solids.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class TIQuery<Real, AlignedBox3<Real>, Cylinder3<Real>>
  18. {
  19. public:
  20. struct Result
  21. {
  22. // The outcome of the mLCP.Solve(...) call.
  23. typename LCPSolverShared<Real>::Result outcome;
  24. bool intersect;
  25. // The number of iterations used by LCPSolver regardless of
  26. // whether the query is successful.
  27. int numLCPIterations;
  28. };
  29. // Default maximum iterations is 64 (n = 8, maxIterations = n*n).
  30. // If the solver fails to converge, try increasing the maximum number
  31. // of iterations.
  32. void SetMaxLCPIterations(int maxLCPIterations)
  33. {
  34. mLCP.SetMaxIterations(maxLCPIterations);
  35. }
  36. Result operator()(AlignedBox3<Real> const& box, Cylinder3<Real> const& cylinder)
  37. {
  38. Result result;
  39. // Translate the box and cylinder so that the box is in the first
  40. // octant where all points in the box have nonnegative components.
  41. Vector3<Real> corner = box.max - box.min;
  42. Vector3<Real> origin = cylinder.axis.origin - box.min;
  43. Vector3<Real> direction = cylinder.axis.direction;
  44. // Compute quantities to initialize q and M in the LCP.
  45. Real halfHeight = cylinder.height * (Real)0.5;
  46. Matrix3x3<Real> P = (Matrix3x3<Real>::Identity() - OuterProduct(direction, direction));
  47. Vector3<Real> C = -(P * origin);
  48. Real originDotDirection = Dot(origin, direction);
  49. Matrix<5, 3, Real> A;
  50. A.SetRow(0, { (Real)-1, (Real)0, (Real)0 });
  51. A.SetRow(1, { (Real)0, (Real)-1, (Real)0 });
  52. A.SetRow(2, { (Real)0, (Real)0, (Real)-1 });
  53. A.SetRow(3, direction);
  54. A.SetRow(4, -direction);
  55. Vector<5, Real> B =
  56. {
  57. -corner[0],
  58. -corner[1],
  59. -corner[2],
  60. originDotDirection - halfHeight,
  61. -originDotDirection - halfHeight
  62. };
  63. std::array<std::array<Real, 8>, 8> M;
  64. for (int r = 0; r < 3; ++r)
  65. {
  66. for (int c = 0; c < 3; ++c)
  67. {
  68. M[r][c] = P(r, c);
  69. }
  70. for (int c = 3, i = 0; c < 8; ++c, ++i)
  71. {
  72. M[r][c] = -A(i, r);
  73. }
  74. }
  75. for (int r = 3, i = 0; r < 8; ++r, ++i)
  76. {
  77. for (int c = 0; c < 3; ++c)
  78. {
  79. M[r][c] = A(i, c);
  80. }
  81. for (int c = 3; c < 8; ++c)
  82. {
  83. M[r][c] = (Real)0;
  84. }
  85. }
  86. std::array<Real, 8> q;
  87. for (int r = 0; r < 3; ++r)
  88. {
  89. q[r] = C[r];
  90. }
  91. for (int r = 3, i = 0; r < 8; ++r, ++i)
  92. {
  93. q[r] = -B[i];
  94. }
  95. std::array<Real, 8> w, z;
  96. if (mLCP.Solve(q, M, w, z, &result.outcome))
  97. {
  98. Vector3<Real> zSolution{ z[0], z[1], z[2] };
  99. Vector3<Real> diff = zSolution - origin;
  100. Real qform = Dot(diff, P * diff);
  101. result.intersect = (qform <= cylinder.radius * cylinder.radius);
  102. }
  103. else
  104. {
  105. // You should examine result.outcome. The query is valid when
  106. // the outcome is NO_SOLUTION. It is possible, however, that
  107. // the solver did not have a large enough iteration budget
  108. // (FAILED_TO_CONVERGE) or it has invalid input
  109. // (INVALID_INPUT).
  110. result.intersect = false;
  111. }
  112. result.numLCPIterations = mLCP.GetNumIterations();
  113. return result;
  114. }
  115. private:
  116. LCPSolver<Real, 8> mLCP;
  117. };
  118. }