Hyperplane.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/Matrix.h>
  9. #include <Mathematics/SingularValueDecomposition.h>
  10. // The plane is represented as Dot(U,X) = c where U is a unit-length normal
  11. // vector, c is the plane constant, and X is any point on the plane. The user
  12. // must ensure that the normal vector is unit length.
  13. namespace WwiseGTE
  14. {
  15. template <int N, typename Real>
  16. class Hyperplane
  17. {
  18. public:
  19. // Construction and destruction. The default constructor sets the
  20. // normal to (0,...,0,1) and the constant to zero (plane z = 0).
  21. Hyperplane()
  22. :
  23. constant((Real)0)
  24. {
  25. normal.MakeUnit(N - 1);
  26. }
  27. // Specify U and c directly.
  28. Hyperplane(Vector<N, Real> const& inNormal, Real inConstant)
  29. :
  30. normal(inNormal),
  31. constant(inConstant)
  32. {
  33. }
  34. // U is specified, c = Dot(U,p) where p is a point on the hyperplane.
  35. Hyperplane(Vector<N, Real> const& inNormal, Vector<N, Real> const& p)
  36. :
  37. normal(inNormal),
  38. constant(Dot(inNormal, p))
  39. {
  40. }
  41. // U is a unit-length vector in the orthogonal complement of the set
  42. // {p[1]-p[0],...,p[n-1]-p[0]} and c = Dot(U,p[0]), where the p[i] are
  43. // pointson the hyperplane.
  44. Hyperplane(std::array<Vector<N, Real>, N> const& p)
  45. {
  46. Matrix<N, N - 1, Real> edge;
  47. for (int i = 0; i < N - 1; ++i)
  48. {
  49. edge.SetCol(i, p[i + 1] - p[0]);
  50. }
  51. // Compute the 1-dimensional orthogonal complement of the edges of
  52. // the simplex formed by the points p[].
  53. SingularValueDecomposition<Real> svd(N, N - 1, 32);
  54. svd.Solve(&edge[0], -1);
  55. svd.GetUColumn(N - 1, &normal[0]);
  56. constant = Dot(normal, p[0]);
  57. }
  58. // Public member access.
  59. Vector<N, Real> normal;
  60. Real constant;
  61. public:
  62. // Comparisons to support sorted containers.
  63. bool operator==(Hyperplane const& hyperplane) const
  64. {
  65. return normal == hyperplane.normal && constant == hyperplane.constant;
  66. }
  67. bool operator!=(Hyperplane const& hyperplane) const
  68. {
  69. return !operator==(hyperplane);
  70. }
  71. bool operator< (Hyperplane const& hyperplane) const
  72. {
  73. if (normal < hyperplane.normal)
  74. {
  75. return true;
  76. }
  77. if (normal > hyperplane.normal)
  78. {
  79. return false;
  80. }
  81. return constant < hyperplane.constant;
  82. }
  83. bool operator<=(Hyperplane const& hyperplane) const
  84. {
  85. return !hyperplane.operator<(*this);
  86. }
  87. bool operator> (Hyperplane const& hyperplane) const
  88. {
  89. return hyperplane.operator<(*this);
  90. }
  91. bool operator>=(Hyperplane const& hyperplane) const
  92. {
  93. return !operator<(hyperplane);
  94. }
  95. };
  96. // Template alias for convenience.
  97. template <typename Real>
  98. using Plane3 = Hyperplane<3, Real>;
  99. }