Tetrahedron3.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/Hyperplane.h>
  9. #include <Mathematics/Vector3.h>
  10. // The tetrahedron is represented as an array of four vertices: V0, V1, V2,
  11. // and V3. The vertices are ordered so that the triangular faces are
  12. // counterclockwise-ordered triangles when viewed by an observer outside the
  13. // tetrahedron:
  14. // face 0 = <V[0],V[2],V[1]>
  15. // face 1 = <V[0],V[1],V[3]>
  16. // face 2 = <V[0],V[3],V[2]>
  17. // face 3 = <V[1],V[2],V[3]>
  18. namespace WwiseGTE
  19. {
  20. template <typename Real>
  21. class Tetrahedron3
  22. {
  23. public:
  24. // Construction and destruction. The default constructor sets the
  25. // vertices to (0,0,0), (1,0,0), (0,1,0), and (0,0,1).
  26. Tetrahedron3()
  27. :
  28. v{ Vector3<Real>::Zero(), Vector3<Real>::Unit(0),
  29. Vector3<Real>::Unit(1), Vector3<Real>::Unit(2) }
  30. {
  31. }
  32. Tetrahedron3(Vector3<Real> const& v0, Vector3<Real> const& v1,
  33. Vector3<Real> const& v2, Vector3<Real> const& v3)
  34. :
  35. v{ v0, v1, v2, v3 }
  36. {
  37. }
  38. Tetrahedron3(std::array<Vector3<Real>, 4> const& inV)
  39. :
  40. v(inV)
  41. {
  42. }
  43. // Get the vertex indices for the specified face. The input 'face'
  44. // must be in {0,1,2,3}.
  45. void GetFaceIndices(int face, int index[3]) const
  46. {
  47. if (face == 0)
  48. {
  49. index[0] = 0;
  50. index[1] = 2;
  51. index[2] = 1;
  52. }
  53. else if (face == 1)
  54. {
  55. index[0] = 0;
  56. index[1] = 1;
  57. index[2] = 3;
  58. }
  59. else if (face == 2)
  60. {
  61. index[0] = 0;
  62. index[1] = 3;
  63. index[2] = 2;
  64. }
  65. else // face == 3 (no index validation is performed)
  66. {
  67. index[0] = 1;
  68. index[1] = 2;
  69. index[2] = 3;
  70. }
  71. }
  72. // Construct the planes of the faces. The planes have outer pointing
  73. // normal vectors. The plane indexing is the same as the face
  74. // indexing mentioned previously.
  75. void GetPlanes(Plane3<Real> plane[4]) const
  76. {
  77. Vector3<Real> edge10 = v[1] - v[0];
  78. Vector3<Real> edge20 = v[2] - v[0];
  79. Vector3<Real> edge30 = v[3] - v[0];
  80. Vector3<Real> edge21 = v[2] - v[1];
  81. Vector3<Real> edge31 = v[3] - v[1];
  82. plane[0].normal = UnitCross(edge20, edge10); // <v0,v2,v1>
  83. plane[1].normal = UnitCross(edge10, edge30); // <v0,v1,v3>
  84. plane[2].normal = UnitCross(edge30, edge20); // <v0,v3,v2>
  85. plane[3].normal = UnitCross(edge21, edge31); // <v1,v2,v3>
  86. Real det = Dot(edge10, plane[3].normal);
  87. if (det < (Real)0)
  88. {
  89. // The normals are inner pointing, reverse their directions.
  90. for (int i = 0; i < 4; ++i)
  91. {
  92. plane[i].normal = -plane[i].normal;
  93. }
  94. }
  95. for (int i = 0; i < 4; ++i)
  96. {
  97. plane[i].constant = Dot(v[i], plane[i].normal);
  98. }
  99. }
  100. // Public member access.
  101. std::array<Vector3<Real>, 4> v;
  102. public:
  103. // Comparisons to support sorted containers.
  104. bool operator==(Tetrahedron3 const& tetrahedron) const
  105. {
  106. return v == tetrahedron.v;
  107. }
  108. bool operator!=(Tetrahedron3 const& tetrahedron) const
  109. {
  110. return v != tetrahedron.v;
  111. }
  112. bool operator< (Tetrahedron3 const& tetrahedron) const
  113. {
  114. return v < tetrahedron.v;
  115. }
  116. bool operator<=(Tetrahedron3 const& tetrahedron) const
  117. {
  118. return v <= tetrahedron.v;
  119. }
  120. bool operator> (Tetrahedron3 const& tetrahedron) const
  121. {
  122. return v > tetrahedron.v;
  123. }
  124. bool operator>=(Tetrahedron3 const& tetrahedron) const
  125. {
  126. return v >= tetrahedron.v;
  127. }
  128. };
  129. }