AlignedBox.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // The box is aligned with the standard coordinate axes, which allows us to
  10. // represent it using minimum and maximum values along each axis. Some
  11. // algorithms prefer the centered representation that is used for oriented
  12. // boxes. The center is C and the extents are the half-lengths in each
  13. // coordinate-axis direction.
  14. namespace WwiseGTE
  15. {
  16. template <int N, typename Real>
  17. class AlignedBox
  18. {
  19. public:
  20. // Construction and destruction. The default constructor sets the
  21. // minimum values to -1 and the maximum values to +1.
  22. AlignedBox()
  23. {
  24. for (int i = 0; i < N; ++i)
  25. {
  26. min[i] = (Real)-1;
  27. max[i] = (Real)+1;
  28. }
  29. }
  30. // Please ensure that inMin[i] <= inMax[i] for all i.
  31. AlignedBox(Vector<N, Real> const& inMin, Vector<N, Real> const& inMax)
  32. {
  33. for (int i = 0; i < N; ++i)
  34. {
  35. min[i] = inMin[i];
  36. max[i] = inMax[i];
  37. }
  38. }
  39. // Compute the centered representation. NOTE: If you set the minimum
  40. // and maximum values, compute C and extents, and then recompute the
  41. // minimum and maximum values, the numerical round-off errors can lead
  42. // to results different from what you started with.
  43. void GetCenteredForm(Vector<N, Real>& center, Vector<N, Real>& extent) const
  44. {
  45. center = (max + min) * (Real)0.5;
  46. extent = (max - min) * (Real)0.5;
  47. }
  48. // Public member access. It is required that min[i] <= max[i].
  49. Vector<N, Real> min, max;
  50. public:
  51. // Comparisons to support sorted containers.
  52. bool operator==(AlignedBox const& box) const
  53. {
  54. return min == box.min && max == box.max;
  55. }
  56. bool operator!=(AlignedBox const& box) const
  57. {
  58. return !operator==(box);
  59. }
  60. bool operator< (AlignedBox const& box) const
  61. {
  62. if (min < box.min)
  63. {
  64. return true;
  65. }
  66. if (min > box.min)
  67. {
  68. return false;
  69. }
  70. return max < box.max;
  71. }
  72. bool operator<=(AlignedBox const& box) const
  73. {
  74. return !box.operator<(*this);
  75. }
  76. bool operator> (AlignedBox const& box) const
  77. {
  78. return box.operator<(*this);
  79. }
  80. bool operator>=(AlignedBox const& box) const
  81. {
  82. return !operator<(box);
  83. }
  84. };
  85. // Template aliases for convenience.
  86. template <typename Real>
  87. using AlignedBox2 = AlignedBox<2, Real>;
  88. template <typename Real>
  89. using AlignedBox3 = AlignedBox<3, Real>;
  90. }