Hyperellipsoid.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/SymmetricEigensolver.h>
  10. // A hyperellipsoid has center K; axis directions U[0] through U[N-1], all
  11. // unit-length vectors; and extents e[0] through e[N-1], all positive numbers.
  12. // A point X = K + sum_{d=0}^{N-1} y[d]*U[d] is on the hyperellipsoid whenever
  13. // sum_{d=0}^{N-1} (y[d]/e[d])^2 = 1. An algebraic representation for the
  14. // hyperellipsoid is (X-K)^T * M * (X-K) = 1, where M is the NxN symmetric
  15. // matrix M = sum_{d=0}^{N-1} U[d]*U[d]^T/e[d]^2, where the superscript T
  16. // denotes transpose. Observe that U[i]*U[i]^T is a matrix, not a scalar dot
  17. // product. The hyperellipsoid is also represented by a quadratic equation
  18. // 0 = C + B^T*X + X^T*A*X, where C is a scalar, B is an Nx1 vector, and A is
  19. // an NxN symmetric matrix with positive eigenvalues. The coefficients can be
  20. // stored from lowest degree to highest degree,
  21. // C = k[0]
  22. // B = k[1], ..., k[N]
  23. // A = k[N+1], ..., k[(N+1)(N+2)/2 - 1]
  24. // where the A-coefficients are the upper-triangular elements of A listed in
  25. // row-major order. For N = 2, X = (x[0],x[1]) and
  26. // 0 = k[0] +
  27. // k[1]*x[0] + k[2]*x[1] +
  28. // k[3]*x[0]*x[0] + k[4]*x[0]*x[1]
  29. // + k[5]*x[1]*x[1]
  30. // For N = 3, X = (x[0],x[1],x[2]) and
  31. // 0 = k[0] +
  32. // k[1]*x[0] + k[2]*x[1] + k[3]*x[2] +
  33. // k[4]*x[0]*x[0] + k[5]*x[0]*x[1] + k[6]*x[0]*x[2] +
  34. // + k[7]*x[1]*x[1] + k[8]*x[1]*x[2] +
  35. // + k[9]*x[2]*x[2]
  36. // This equation can be factored to the form (X-K)^T * M * (X-K) = 1, where
  37. // K = -A^{-1}*B/2, M = A/(B^T*A^{-1}*B/4-C).
  38. namespace WwiseGTE
  39. {
  40. template <int N, typename Real>
  41. class Hyperellipsoid
  42. {
  43. public:
  44. // Construction and destruction. The default constructor sets the
  45. // center to Vector<N,Real>::Zero(), the axes to
  46. // Vector<N,Real>::Unit(d), and all extents to 1.
  47. Hyperellipsoid()
  48. {
  49. center.MakeZero();
  50. for (int d = 0; d < N; ++d)
  51. {
  52. axis[d].MakeUnit(d);
  53. extent[d] = (Real)1;
  54. }
  55. }
  56. Hyperellipsoid(Vector<N, Real> const& inCenter,
  57. std::array<Vector<N, Real>, N> const inAxis,
  58. Vector<N, Real> const& inExtent)
  59. :
  60. center(inCenter),
  61. axis(inAxis),
  62. extent(inExtent)
  63. {
  64. }
  65. // Compute M = sum_{d=0}^{N-1} U[d]*U[d]^T/e[d]^2.
  66. void GetM(Matrix<N, N, Real>& M) const
  67. {
  68. M.MakeZero();
  69. for (int d = 0; d < N; ++d)
  70. {
  71. Vector<N, Real> ratio = axis[d] / extent[d];
  72. M += OuterProduct<N, N, Real>(ratio, ratio);
  73. }
  74. }
  75. // Compute M^{-1} = sum_{d=0}^{N-1} U[d]*U[d]^T*e[d]^2.
  76. void GetMInverse(Matrix<N, N, Real>& MInverse) const
  77. {
  78. MInverse.MakeZero();
  79. for (int d = 0; d < N; ++d)
  80. {
  81. Vector<N, Real> product = axis[d] * extent[d];
  82. MInverse += OuterProduct<N, N, Real>(product, product);
  83. }
  84. }
  85. // Construct the coefficients in the quadratic equation that represents
  86. // the hyperellipsoid.
  87. void ToCoefficients(std::array<Real, (N + 1) * (N + 2) / 2> & coeff) const
  88. {
  89. int const numCoefficients = (N + 1) * (N + 2) / 2;
  90. Matrix<N, N, Real> A;
  91. Vector<N, Real> B;
  92. Real C;
  93. ToCoefficients(A, B, C);
  94. Convert(A, B, C, coeff);
  95. // Arrange for one of the coefficients of the quadratic terms
  96. // to be 1.
  97. int quadIndex = numCoefficients - 1;
  98. int maxIndex = quadIndex;
  99. Real maxValue = std::fabs(coeff[quadIndex]);
  100. for (int d = 2; d < N; ++d)
  101. {
  102. quadIndex -= d;
  103. Real absValue = std::fabs(coeff[quadIndex]);
  104. if (absValue > maxValue)
  105. {
  106. maxIndex = quadIndex;
  107. maxValue = absValue;
  108. }
  109. }
  110. Real invMaxValue = (Real)1 / maxValue;
  111. for (int i = 0; i < numCoefficients; ++i)
  112. {
  113. if (i != maxIndex)
  114. {
  115. coeff[i] *= invMaxValue;
  116. }
  117. else
  118. {
  119. coeff[i] = (Real)1;
  120. }
  121. }
  122. }
  123. void ToCoefficients(Matrix<N, N, Real>& A, Vector<N, Real>& B, Real& C) const
  124. {
  125. GetM(A);
  126. Vector<N, Real> product = A * center;
  127. B = (Real)-2 * product;
  128. C = Dot(center, product) - (Real)1;
  129. }
  130. // Construct C, U[i], and e[i] from the equation. The return value is
  131. // 'true' if and only if the input coefficients represent a
  132. // hyperellipsoid. If the function returns 'false', the hyperellipsoid
  133. // data members are undefined.
  134. bool FromCoefficients(std::array<Real, (N + 1) * (N + 2) / 2> const& coeff)
  135. {
  136. Matrix<N, N, Real> A;
  137. Vector<N, Real> B;
  138. Real C;
  139. Convert(coeff, A, B, C);
  140. return FromCoefficients(A, B, C);
  141. }
  142. bool FromCoefficients(Matrix<N, N, Real> const& A, Vector<N, Real> const& B, Real C)
  143. {
  144. // Compute the center K = -A^{-1}*B/2.
  145. bool invertible;
  146. Matrix<N, N, Real> invA = Inverse(A, &invertible);
  147. if (!invertible)
  148. {
  149. return false;
  150. }
  151. center = ((Real)-0.5) * (invA * B);
  152. // Compute B^T*A^{-1}*B/4 - C = K^T*A*K - C = -K^T*B/2 - C.
  153. Real rightSide = (Real)-0.5 * Dot(center, B) - C;
  154. if (rightSide == (Real)0)
  155. {
  156. return false;
  157. }
  158. // Compute M = A/(K^T*A*K - C).
  159. Real invRightSide = (Real)1 / rightSide;
  160. Matrix<N, N, Real> M = invRightSide * A;
  161. // Factor into M = R*D*R^T. M is symmetric, so it does not matter whether
  162. // the matrix is stored in row-major or column-major order; they are
  163. // equivalent. The output R, however, is in row-major order.
  164. SymmetricEigensolver<Real> es(N, 32);
  165. Matrix<N, N, Real> rotation;
  166. std::array<Real, N> diagonal;
  167. es.Solve(&M[0], +1); // diagonal[i] are nondecreasing
  168. es.GetEigenvalues(&diagonal[0]);
  169. es.GetEigenvectors(&rotation[0]);
  170. if (es.GetEigenvectorMatrixType() == 0)
  171. {
  172. auto negLast = -rotation.GetCol(N - 1);
  173. rotation.SetCol(N - 1, negLast);
  174. }
  175. for (int d = 0; d < N; ++d)
  176. {
  177. if (diagonal[d] <= (Real)0)
  178. {
  179. return false;
  180. }
  181. extent[d] = (Real)1 / std::sqrt(diagonal[d]);
  182. axis[d] = rotation.GetCol(d);
  183. }
  184. return true;
  185. }
  186. // Public member access.
  187. Vector<N, Real> center;
  188. std::array<Vector<N, Real>, N> axis;
  189. Vector<N, Real> extent;
  190. private:
  191. static void Convert(std::array<Real, (N + 1) * (N + 2) / 2> const& coeff,
  192. Matrix<N, N, Real>& A, Vector<N, Real>& B, Real& C)
  193. {
  194. int i = 0;
  195. C = coeff[i++];
  196. for (int j = 0; j < N; ++j)
  197. {
  198. B[j] = coeff[i++];
  199. }
  200. for (int r = 0; r < N; ++r)
  201. {
  202. for (int c = 0; c < r; ++c)
  203. {
  204. A(r, c) = A(c, r);
  205. }
  206. A(r, r) = coeff[i++];
  207. for (int c = r + 1; c < N; ++c)
  208. {
  209. A(r, c) = coeff[i++] * (Real)0.5;
  210. }
  211. }
  212. }
  213. static void Convert(Matrix<N, N, Real> const& A, Vector<N, Real> const& B,
  214. Real C, std::array<Real, (N + 1) * (N + 2) / 2> & coeff)
  215. {
  216. int i = 0;
  217. coeff[i++] = C;
  218. for (int j = 0; j < N; ++j)
  219. {
  220. coeff[i++] = B[j];
  221. }
  222. for (int r = 0; r < N; ++r)
  223. {
  224. coeff[i++] = A(r, r);
  225. for (int c = r + 1; c < N; ++c)
  226. {
  227. coeff[i++] = A(r, c) * (Real)2;
  228. }
  229. }
  230. }
  231. public:
  232. // Comparisons to support sorted containers.
  233. bool operator==(Hyperellipsoid const& hyperellipsoid) const
  234. {
  235. return center == hyperellipsoid.center && axis == hyperellipsoid.axis
  236. && extent == hyperellipsoid.extent;
  237. }
  238. bool operator!=(Hyperellipsoid const& hyperellipsoid) const
  239. {
  240. return !operator==(hyperellipsoid);
  241. }
  242. bool operator< (Hyperellipsoid const& hyperellipsoid) const
  243. {
  244. if (center < hyperellipsoid.center)
  245. {
  246. return true;
  247. }
  248. if (center > hyperellipsoid.center)
  249. {
  250. return false;
  251. }
  252. if (axis < hyperellipsoid.axis)
  253. {
  254. return true;
  255. }
  256. if (axis > hyperellipsoid.axis)
  257. {
  258. return false;
  259. }
  260. return extent < hyperellipsoid.extent;
  261. }
  262. bool operator<=(Hyperellipsoid const& hyperellipsoid) const
  263. {
  264. return !hyperellipsoid.operator<(*this);
  265. }
  266. bool operator> (Hyperellipsoid const& hyperellipsoid) const
  267. {
  268. return hyperellipsoid.operator<(*this);
  269. }
  270. bool operator>=(Hyperellipsoid const& hyperellipsoid) const
  271. {
  272. return !operator<(hyperellipsoid);
  273. }
  274. };
  275. // Template aliases for convenience.
  276. template <typename Real>
  277. using Ellipse2 = Hyperellipsoid<2, Real>;
  278. template <typename Real>
  279. using Ellipsoid3 = Hyperellipsoid<3, Real>;
  280. }