Rectangle.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Points are R(s0,s1) = C + s0*A0 + s1*A1, where C is the center of the
  10. // rectangle and A0 and A1 are unit-length and perpendicular axes. The
  11. // parameters s0 and s1 are constrained by |s0| <= e0 and |s1| <= e1,
  12. // where e0 > 0 and e1 > 0 are the extents of the rectangle.
  13. namespace WwiseGTE
  14. {
  15. template <int N, typename Real>
  16. class Rectangle
  17. {
  18. public:
  19. // Construction and destruction. The default constructor sets the
  20. // origin to (0,...,0), axis A0 to (1,0,...,0), axis A1 to
  21. // (0,1,0,...0) and both extents to 1.
  22. Rectangle()
  23. {
  24. center.MakeZero();
  25. for (int i = 0; i < 2; ++i)
  26. {
  27. axis[i].MakeUnit(i);
  28. extent[i] = (Real)1;
  29. }
  30. }
  31. Rectangle(Vector<N, Real> const& inCenter,
  32. std::array<Vector<N, Real>, 2> const& inAxis,
  33. Vector<2, Real> const& inExtent)
  34. :
  35. center(inCenter),
  36. axis(inAxis),
  37. extent(inExtent)
  38. {
  39. }
  40. // Compute the vertices of the rectangle. If index i has the bit
  41. // pattern i = b[1]b[0], then
  42. // vertex[i] = center + sum_{d=0}^{1} sign[d] * extent[d] * axis[d]
  43. // where sign[d] = 2*b[d] - 1.
  44. void GetVertices(std::array<Vector<N, Real>, 4>& vertex) const
  45. {
  46. Vector<N, Real> product0 = extent[0] * axis[0];
  47. Vector<N, Real> product1 = extent[1] * axis[1];
  48. Vector<N, Real> sum = product0 + product1;
  49. Vector<N, Real> dif = product0 - product1;
  50. vertex[0] = center - sum;
  51. vertex[1] = center + dif;
  52. vertex[2] = center - dif;
  53. vertex[3] = center + sum;
  54. }
  55. Vector<N, Real> center;
  56. std::array<Vector<N, Real>, 2> axis;
  57. Vector<2, Real> extent;
  58. public:
  59. // Comparisons to support sorted containers.
  60. bool operator==(Rectangle const& rectangle) const
  61. {
  62. if (center != rectangle.center)
  63. {
  64. return false;
  65. }
  66. for (int i = 0; i < 2; ++i)
  67. {
  68. if (axis[i] != rectangle.axis[i])
  69. {
  70. return false;
  71. }
  72. }
  73. for (int i = 0; i < 2; ++i)
  74. {
  75. if (extent[i] != rectangle.extent[i])
  76. {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. bool operator!=(Rectangle const& rectangle) const
  83. {
  84. return !operator==(rectangle);
  85. }
  86. bool operator< (Rectangle const& rectangle) const
  87. {
  88. if (center < rectangle.center)
  89. {
  90. return true;
  91. }
  92. if (center > rectangle.center)
  93. {
  94. return false;
  95. }
  96. if (axis < rectangle.axis)
  97. {
  98. return true;
  99. }
  100. if (axis > rectangle.axis)
  101. {
  102. return false;
  103. }
  104. return extent < rectangle.extent;
  105. }
  106. bool operator<=(Rectangle const& rectangle) const
  107. {
  108. return !rectangle.operator<(*this);
  109. }
  110. bool operator> (Rectangle const& rectangle) const
  111. {
  112. return rectangle.operator<(*this);
  113. }
  114. bool operator>=(Rectangle const& rectangle) const
  115. {
  116. return !operator<(rectangle);
  117. }
  118. };
  119. // Template alias for convenience.
  120. template <typename Real>
  121. using Rectangle3 = Rectangle<3, Real>;
  122. }