Halfspace.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 halfspace is represented as Dot(N,X) >= c where N is a unit-length
  10. // normal vector, c is the plane constant, and X is any point in space.
  11. // The user must ensure that the normal vector is unit length.
  12. namespace WwiseGTE
  13. {
  14. template <int N, typename Real>
  15. class Halfspace
  16. {
  17. public:
  18. // Construction and destruction. The default constructor sets the
  19. // normal to (0,...,0,1) and the constant to zero (halfspace
  20. // x[N-1] >= 0).
  21. Halfspace()
  22. :
  23. constant((Real)0)
  24. {
  25. normal.MakeUnit(N - 1);
  26. }
  27. // Specify N and c directly.
  28. Halfspace(Vector<N, Real> const& inNormal, Real inConstant)
  29. :
  30. normal(inNormal),
  31. constant(inConstant)
  32. {
  33. }
  34. // Public member access.
  35. Vector<N, Real> normal;
  36. Real constant;
  37. public:
  38. // Comparisons to support sorted containers.
  39. bool operator==(Halfspace const& halfspace) const
  40. {
  41. return normal == halfspace.normal && constant == halfspace.constant;
  42. }
  43. bool operator!=(Halfspace const& halfspace) const
  44. {
  45. return !operator==(halfspace);
  46. }
  47. bool operator< (Halfspace const& halfspace) const
  48. {
  49. if (normal < halfspace.normal)
  50. {
  51. return true;
  52. }
  53. if (normal > halfspace.normal)
  54. {
  55. return false;
  56. }
  57. return constant < halfspace.constant;
  58. }
  59. bool operator<=(Halfspace const& halfspace) const
  60. {
  61. return !halfspace.operator<(*this);
  62. }
  63. bool operator> (Halfspace const& halfspace) const
  64. {
  65. return halfspace.operator<(*this);
  66. }
  67. bool operator>=(Halfspace const& halfspace) const
  68. {
  69. return !operator<(halfspace);
  70. }
  71. };
  72. // Template alias for convenience.
  73. template <typename Real>
  74. using Halfspace3 = Halfspace<3, Real>;
  75. }