DistPoint3Tetrahedron3.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/DistPointTriangle.h>
  9. #include <Mathematics/Tetrahedron3.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class DCPQuery<Real, Vector3<Real>, Tetrahedron3<Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. Real distance, sqrDistance;
  19. Vector3<Real> tetrahedronClosestPoint;
  20. };
  21. Result operator()(Vector3<Real> const& point, Tetrahedron3<Real> const& tetrahedron)
  22. {
  23. Result result;
  24. // Construct the planes for the faces of the tetrahedron. The
  25. // normals are outer pointing, but specified not to be unit
  26. // length. We only need to know sidedness of the query point,
  27. // so we will save cycles by not computing unit-length normals.
  28. Plane3<Real> planes[4];
  29. tetrahedron.GetPlanes(planes);
  30. // Determine which faces are visible to the query point. Only
  31. // these need to be processed by point-to-triangle distance
  32. // queries.
  33. result.sqrDistance = std::numeric_limits<Real>::max();
  34. result.tetrahedronClosestPoint = Vector3<Real>::Zero();
  35. for (int i = 0; i < 4; ++i)
  36. {
  37. if (Dot(planes[i].normal, point) >= planes[i].constant)
  38. {
  39. int indices[3] = { 0, 0, 0 };
  40. tetrahedron.GetFaceIndices(i, indices);
  41. Triangle3<Real> triangle(
  42. tetrahedron.v[indices[0]],
  43. tetrahedron.v[indices[1]],
  44. tetrahedron.v[indices[2]]);
  45. DCPQuery<Real, Vector3<Real>, Triangle3<Real>> query;
  46. auto ptResult = query(point, triangle);
  47. if (ptResult.sqrDistance < result.sqrDistance)
  48. {
  49. result.sqrDistance = ptResult.sqrDistance;
  50. result.tetrahedronClosestPoint = ptResult.closest;
  51. }
  52. }
  53. }
  54. if (result.sqrDistance == std::numeric_limits<Real>::max())
  55. {
  56. // The query point is inside the solid tetrahedron. Report a
  57. // zero distance. The closest points are identical.
  58. result.sqrDistance = (Real)0;
  59. result.tetrahedronClosestPoint = point;
  60. }
  61. result.distance = std::sqrt(result.sqrDistance);
  62. return result;
  63. }
  64. };
  65. }