DistTriangle3OrientedBox3.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/DCPQuery.h>
  9. #include <Mathematics/LCPSolver.h>
  10. #include <Mathematics/OrientedBox.h>
  11. #include <Mathematics/Triangle.h>
  12. #include <Mathematics/Vector3.h>
  13. // Compute the distance between a triangle and an oriented box in 3D. The
  14. // algorithm is based on using an LCP solver for the convex quadratic
  15. // programming problem. For details, see
  16. // https://www.geometrictools.com/Documentation/ConvexQuadraticProgramming.pdf
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class DCPQuery<Real, Triangle3<Real>, OrientedBox3<Real>>
  21. {
  22. public:
  23. struct Result
  24. {
  25. bool queryIsSuccessful;
  26. // These members are valid only when queryIsSuccessful is true;
  27. // otherwise, they are all set to zero.
  28. Real distance, sqrDistance;
  29. std::array<Real, 3> triangleParameter, boxParameter;
  30. Vector3<Real> closestPoint[2];
  31. // The number of iterations used by LCPSolver regardless of
  32. // whether the query is successful.
  33. int numLCPIterations;
  34. };
  35. // The default maximum iterations is 81 (n = 9, maxIterations = n*n).
  36. // If the solver fails to converge, try increasing the maximum number
  37. // of iterations.
  38. void SetMaxLCPIterations(int maxLCPIterations)
  39. {
  40. mLCP.SetMaxIterations(maxLCPIterations);
  41. }
  42. Result operator()(Triangle3<Real> const& triangle, OrientedBox3<Real> const& box)
  43. {
  44. Result result;
  45. // Rigidly transform the triangle and oriented box so that the
  46. // oriented box becomes a canonical box.
  47. Vector3<Real> K = box.extent * (Real)2;
  48. Vector3<Real> tempV = triangle.v[0] - box.center;
  49. Vector3<Real> tempE0 = triangle.v[1] - triangle.v[0];
  50. Vector3<Real> tempE1 = triangle.v[2] - triangle.v[0];
  51. Vector3<Real> V, E0, E1;
  52. for (int i = 0; i < 3; ++i)
  53. {
  54. V[i] = Dot(box.axis[i], tempV) + box.extent[i];
  55. E0[i] = Dot(box.axis[i], tempE0);
  56. E1[i] = Dot(box.axis[i], tempE1);
  57. }
  58. // Compute quantities to initialize q and M in the LCP.
  59. Real dotVE0 = Dot(V, E0);
  60. Real dotVE1 = Dot(V, E1);
  61. Real dotE0E0 = Dot(E0, E0);
  62. Real dotE0E1 = Dot(E0, E1);
  63. Real dotE1E1 = Dot(E1, E1);
  64. // The LCP has 5 variables and 4 (nontrivial) inequality
  65. // constraints.
  66. std::array<Real, 9> q =
  67. {
  68. -V[0], -V[1], -V[2], dotVE0, dotVE1, K[0], K[1], K[2], (Real)1
  69. };
  70. std::array<std::array<Real, 9>, 9> M;
  71. M[0] = { (Real)1, (Real)0, (Real)0, -E0[0], -E1[0], (Real)1, (Real)0, (Real)0, (Real)0 };
  72. M[1] = { (Real)0, (Real)1, (Real)0, -E0[1], -E1[1], (Real)0, (Real)1, (Real)0, (Real)0 };
  73. M[2] = { (Real)0, (Real)0, (Real)1, -E0[2], -E1[2], (Real)0, (Real)0, (Real)1, (Real)0 };
  74. M[3] = { -E0[0], -E0[1], -E0[2], dotE0E0, dotE0E1, (Real)0, (Real)0, (Real)0, (Real)1 };
  75. M[4] = { -E1[0], -E1[1], -E1[2], dotE0E1, dotE1E1, (Real)0, (Real)0, (Real)0, (Real)1 };
  76. M[5] = { (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
  77. M[6] = { (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
  78. M[7] = { (Real)0, (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
  79. M[8] = { (Real)0, (Real)0, (Real)0, (Real)-1, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0 };
  80. std::array<Real, 9> w, z;
  81. if (mLCP.Solve(q, M, w, z))
  82. {
  83. result.queryIsSuccessful = true;
  84. result.triangleParameter[0] = (Real)1 - z[3] - z[4];
  85. result.triangleParameter[1] = z[3];
  86. result.triangleParameter[2] = z[4];
  87. result.closestPoint[0] = triangle.v[0] + z[3] * tempE0 + z[4] * tempE1;
  88. result.closestPoint[1] = box.center;
  89. for (int i = 0; i < 3; ++i)
  90. {
  91. result.boxParameter[i] = z[i] - box.extent[i];
  92. result.closestPoint[1] += result.boxParameter[i] * box.axis[i];
  93. }
  94. Vector3<Real> diff = result.closestPoint[1] - result.closestPoint[0];
  95. result.sqrDistance = Dot(diff, diff);
  96. result.distance = std::sqrt(result.sqrDistance);
  97. }
  98. else
  99. {
  100. // If you reach this case, the maximum number of iterations
  101. // was not specified to be large enough or there is a problem
  102. // due to floating-point rounding errors. If you believe the
  103. // latter is true, file a bug report.
  104. result.queryIsSuccessful = false;
  105. for (int i = 0; i < 3; ++i)
  106. {
  107. result.triangleParameter[i] = (Real)0;
  108. result.boxParameter[i] = (Real)0;
  109. result.closestPoint[0][i] = (Real)0;
  110. result.closestPoint[1][i] = (Real)0;
  111. }
  112. result.distance = (Real)0;
  113. result.sqrDistance = (Real)0;
  114. }
  115. result.numLCPIterations = mLCP.GetNumIterations();
  116. return result;
  117. }
  118. private:
  119. LCPSolver<Real, 9> mLCP;
  120. };
  121. }