Frustum3.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/Vector3.h>
  9. // Orthogonal frustum. Let E be the origin, D be the direction vector, U be
  10. // the up vector, and R be the right vector. Let u > 0 and r > 0 be the
  11. // extents in the U and R directions, respectively. Let n and f be the
  12. // extents in the D direction with 0 < n < f. The four corners of the frustum
  13. // in the near plane are E + n*D + s0*u*U + s1*r*R where |s0| = |s1| = 1 (four
  14. // choices). The four corners of the frustum in the far plane are
  15. // E + f*D + (f/n)*(s0*u*U + s1*r*R) where |s0| = |s1| = 1 (four choices).
  16. namespace WwiseGTE
  17. {
  18. template <typename Real>
  19. class Frustum3
  20. {
  21. public:
  22. // Construction and destruction. The default constructor sets the
  23. // following values: origin (E) to (0,0,0), dVector (D) to (0,0,1),
  24. // uVector (U) to (0,1,0), rVector (R) to (1,0,0), dMin (n) to 1,
  25. // dMax (f) to 2, uBound (u) to 1, and rBound (r) to 1.
  26. Frustum3()
  27. :
  28. origin(Vector3<Real>::Zero()),
  29. dVector(Vector3<Real>::Unit(2)),
  30. uVector(Vector3<Real>::Unit(1)),
  31. rVector(Vector3<Real>::Unit(0)),
  32. dMin((Real)1),
  33. dMax((Real)2),
  34. uBound((Real)1),
  35. rBound((Real)1)
  36. {
  37. Update();
  38. }
  39. Frustum3(Vector3<Real> const& inOrigin, Vector3<Real> const& inDVector,
  40. Vector3<Real> const& inUVector, Vector3<Real> const& inRVector,
  41. Real inDMin, Real inDMax, Real inUBound, Real inRBound)
  42. :
  43. origin(inOrigin),
  44. dVector(inDVector),
  45. uVector(inUVector),
  46. rVector(inRVector),
  47. dMin(inDMin),
  48. dMax(inDMax),
  49. uBound(inUBound),
  50. rBound(inRBound)
  51. {
  52. Update();
  53. }
  54. // The Update() function must be called whenever changes are made to
  55. // dMin, dMax, uBound or rBound. The values mDRatio, mMTwoUF and
  56. // mMTwoRF are dependent on the changes, so call the Get*() accessors
  57. // only after the Update() call.
  58. void Update()
  59. {
  60. mDRatio = dMax / dMin;
  61. mMTwoUF = (Real)-2 * uBound * dMax;
  62. mMTwoRF = (Real)-2 * rBound * dMax;
  63. }
  64. inline Real GetDRatio() const
  65. {
  66. return mDRatio;
  67. }
  68. inline Real GetMTwoUF() const
  69. {
  70. return mMTwoUF;
  71. }
  72. inline Real GetMTwoRF() const
  73. {
  74. return mMTwoRF;
  75. }
  76. void ComputeVertices(std::array<Vector3<Real>, 8>& vertex) const
  77. {
  78. Vector3<Real> dScaled = dMin * dVector;
  79. Vector3<Real> uScaled = uBound * uVector;
  80. Vector3<Real> rScaled = rBound * rVector;
  81. vertex[0] = dScaled - uScaled - rScaled;
  82. vertex[1] = dScaled - uScaled + rScaled;
  83. vertex[2] = dScaled + uScaled + rScaled;
  84. vertex[3] = dScaled + uScaled - rScaled;
  85. for (int i = 0, ip = 4; i < 4; ++i, ++ip)
  86. {
  87. vertex[ip] = origin + mDRatio * vertex[i];
  88. vertex[i] += origin;
  89. }
  90. }
  91. Vector3<Real> origin, dVector, uVector, rVector;
  92. Real dMin, dMax, uBound, rBound;
  93. public:
  94. // Comparisons to support sorted containers.
  95. bool operator==(Frustum3 const& frustum) const
  96. {
  97. return origin == frustum.origin
  98. && dVector == frustum.dVector
  99. && uVector == frustum.uVector
  100. && rVector == frustum.rVector
  101. && dMin == frustum.dMin
  102. && dMax == frustum.dMax
  103. && uBound == frustum.uBound
  104. && rBound == frustum.rBound;
  105. }
  106. bool operator!=(Frustum3 const& frustum) const
  107. {
  108. return !operator==(frustum);
  109. }
  110. bool operator< (Frustum3 const& frustum) const
  111. {
  112. if (origin < frustum.origin)
  113. {
  114. return true;
  115. }
  116. if (origin > frustum.origin)
  117. {
  118. return false;
  119. }
  120. if (dVector < frustum.dVector)
  121. {
  122. return true;
  123. }
  124. if (dVector > frustum.dVector)
  125. {
  126. return false;
  127. }
  128. if (uVector < frustum.uVector)
  129. {
  130. return true;
  131. }
  132. if (uVector > frustum.uVector)
  133. {
  134. return false;
  135. }
  136. if (rVector < frustum.rVector)
  137. {
  138. return true;
  139. }
  140. if (rVector > frustum.rVector)
  141. {
  142. return false;
  143. }
  144. if (dMin < frustum.dMin)
  145. {
  146. return true;
  147. }
  148. if (dMin > frustum.dMin)
  149. {
  150. return false;
  151. }
  152. if (dMax < frustum.dMax)
  153. {
  154. return true;
  155. }
  156. if (dMax > frustum.dMax)
  157. {
  158. return false;
  159. }
  160. if (uBound < frustum.uBound)
  161. {
  162. return true;
  163. }
  164. if (uBound > frustum.uBound)
  165. {
  166. return false;
  167. }
  168. return rBound < frustum.rBound;
  169. }
  170. bool operator<=(Frustum3 const& frustum) const
  171. {
  172. return !frustum.operator<(*this);
  173. }
  174. bool operator> (Frustum3 const& frustum) const
  175. {
  176. return frustum.operator<(*this);
  177. }
  178. bool operator>=(Frustum3 const& frustum) const
  179. {
  180. return !operator<(frustum);
  181. }
  182. protected:
  183. // Quantities derived from the constructor inputs.
  184. Real mDRatio, mMTwoUF, mMTwoRF;
  185. };
  186. }