OrientedBox.h 4.1 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.08.13
  7. #pragma once
  8. #include <Mathematics/Vector.h>
  9. // A box has center C, axis directions U[i], and extents e[i]. The set
  10. // {U[0],...,U[N-1]} is orthonormal, which means the vectors are
  11. // unit-length and mutually perpendicular. The extents are nonnegative;
  12. // zero is allowed, meaning the box is degenerate in the corresponding
  13. // direction. A point X is represented in box coordinates by
  14. // X = C + y[0]*U[0] + y[1]*U[1]. This point is inside or on the
  15. // box whenever |y[i]| <= e[i] for all i.
  16. namespace WwiseGTE
  17. {
  18. template <int N, typename Real>
  19. class OrientedBox
  20. {
  21. public:
  22. // Construction and destruction. The default constructor sets the
  23. // center to (0,...,0), axis d to Vector<N,Real>::Unit(d) and
  24. // extent d to +1.
  25. OrientedBox()
  26. {
  27. center.MakeZero();
  28. for (int i = 0; i < N; ++i)
  29. {
  30. axis[i].MakeUnit(i);
  31. extent[i] = (Real)1;
  32. }
  33. }
  34. OrientedBox(Vector<N, Real> const& inCenter,
  35. std::array<Vector<N, Real>, N> const& inAxis,
  36. Vector<N, Real> const& inExtent)
  37. :
  38. center(inCenter),
  39. axis(inAxis),
  40. extent(inExtent)
  41. {
  42. }
  43. // Compute the vertices of the box. If index i has the bit pattern
  44. // i = b[N-1]...b[0], then
  45. // vertex[i] = center + sum_{d=0}^{N-1} sign[d] * extent[d] * axis[d]
  46. // where sign[d] = 2*b[d] - 1.
  47. void GetVertices(std::array<Vector<N, Real>, (1 << N)>& vertex) const
  48. {
  49. unsigned int const dsup = static_cast<unsigned int>(N);
  50. std::array<Vector<N, Real>, N> product;
  51. for (unsigned int d = 0; d < dsup; ++d)
  52. {
  53. product[d] = extent[d] * axis[d];
  54. }
  55. int const imax = (1 << N);
  56. for (int i = 0; i < imax; ++i)
  57. {
  58. vertex[i] = center;
  59. for (unsigned int d = 0, mask = 1; d < dsup; ++d, mask <<= 1)
  60. {
  61. Real sign = (i & mask ? (Real)1 : (Real)-1);
  62. vertex[i] += sign * product[d];
  63. }
  64. }
  65. }
  66. // Public member access. It is required that extent[i] >= 0.
  67. Vector<N, Real> center;
  68. std::array<Vector<N, Real>, N> axis;
  69. Vector<N, Real> extent;
  70. public:
  71. // Comparisons to support sorted containers.
  72. bool operator==(OrientedBox const& box) const
  73. {
  74. return center == box.center && axis == box.axis && extent == box.extent;
  75. }
  76. bool operator!=(OrientedBox const& box) const
  77. {
  78. return !operator==(box);
  79. }
  80. bool operator< (OrientedBox const& box) const
  81. {
  82. if (center < box.center)
  83. {
  84. return true;
  85. }
  86. if (center > box.center)
  87. {
  88. return false;
  89. }
  90. if (axis < box.axis)
  91. {
  92. return true;
  93. }
  94. if (axis > box.axis)
  95. {
  96. return false;
  97. }
  98. return extent < box.extent;
  99. }
  100. bool operator<=(OrientedBox const& box) const
  101. {
  102. return !box.operator<(*this);
  103. }
  104. bool operator> (OrientedBox const& box) const
  105. {
  106. return box.operator<(*this);
  107. }
  108. bool operator>=(OrientedBox const& box) const
  109. {
  110. return !operator<(box);
  111. }
  112. };
  113. // Template aliases for convenience.
  114. template <typename Real>
  115. using OrientedBox2 = OrientedBox<2, Real>;
  116. template <typename Real>
  117. using OrientedBox3 = OrientedBox<3, Real>;
  118. }