Vector2.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.2020.01.10
  7. #pragma once
  8. #include <Mathematics/Vector.h>
  9. namespace WwiseGTE
  10. {
  11. // Template alias for convenience.
  12. template <typename Real>
  13. using Vector2 = Vector<2, Real>;
  14. // Compute the perpendicular using the formal determinant,
  15. // perp = det{{e0,e1},{x0,x1}} = (x1,-x0)
  16. // where e0 = (1,0), e1 = (0,1), and v = (x0,x1).
  17. template <typename Real>
  18. Vector2<Real> Perp(Vector2<Real> const& v)
  19. {
  20. return Vector2<Real>{ v[1], -v[0] };
  21. }
  22. // Compute the normalized perpendicular.
  23. template <typename Real>
  24. Vector2<Real> UnitPerp(Vector2<Real> const& v, bool robust = false)
  25. {
  26. Vector2<Real> unitPerp{ v[1], -v[0] };
  27. Normalize(unitPerp, robust);
  28. return unitPerp;
  29. }
  30. // Compute Dot((x0,x1),Perp(y0,y1)) = x0*y1 - x1*y0, where v0 = (x0,x1)
  31. // and v1 = (y0,y1).
  32. template <typename Real>
  33. Real DotPerp(Vector2<Real> const& v0, Vector2<Real> const& v1)
  34. {
  35. return Dot(v0, Perp(v1));
  36. }
  37. // Compute a right-handed orthonormal basis for the orthogonal complement
  38. // of the input vectors. The function returns the smallest length of the
  39. // unnormalized vectors computed during the process. If this value is
  40. // nearly zero, it is possible that the inputs are linearly dependent
  41. // (within numerical round-off errors). On input, numInputs must be 1 and
  42. // v[0] must be initialized. On output, the vectors v[0] and v[1] form an
  43. // orthonormal set.
  44. template <typename Real>
  45. Real ComputeOrthogonalComplement(int numInputs, Vector2<Real>* v, bool robust = false)
  46. {
  47. if (numInputs == 1)
  48. {
  49. v[1] = -Perp(v[0]);
  50. return Orthonormalize<2, Real>(2, v, robust);
  51. }
  52. return (Real)0;
  53. }
  54. // Compute the barycentric coordinates of the point P with respect to the
  55. // triangle <V0,V1,V2>, P = b0*V0 + b1*V1 + b2*V2, where b0 + b1 + b2 = 1.
  56. // The return value is 'true' iff {V0,V1,V2} is a linearly independent
  57. // set. Numerically, this is measured by |det[V0 V1 V2]| <= epsilon. The
  58. // values bary[] are valid only when the return value is 'true' but set to
  59. // zero when the return value is 'false'.
  60. template <typename Real>
  61. bool ComputeBarycentrics(Vector2<Real> const& p, Vector2<Real> const& v0,
  62. Vector2<Real> const& v1, Vector2<Real> const& v2, Real bary[3],
  63. Real epsilon = (Real)0)
  64. {
  65. // Compute the vectors relative to V2 of the triangle.
  66. Vector2<Real> diff[3] = { v0 - v2, v1 - v2, p - v2 };
  67. Real det = DotPerp(diff[0], diff[1]);
  68. if (det < -epsilon || det > epsilon)
  69. {
  70. Real invDet = (Real)1 / det;
  71. bary[0] = DotPerp(diff[2], diff[1]) * invDet;
  72. bary[1] = DotPerp(diff[0], diff[2]) * invDet;
  73. bary[2] = (Real)1 - bary[0] - bary[1];
  74. return true;
  75. }
  76. for (int i = 0; i < 3; ++i)
  77. {
  78. bary[i] = (Real)0;
  79. }
  80. return false;
  81. }
  82. // Get intrinsic information about the input array of vectors. The return
  83. // value is 'true' iff the inputs are valid (numVectors > 0, v is not
  84. // null, and epsilon >= 0), in which case the class members are valid.
  85. template <typename Real>
  86. class IntrinsicsVector2
  87. {
  88. public:
  89. // The constructor sets the class members based on the input set.
  90. IntrinsicsVector2(int numVectors, Vector2<Real> const* v, Real inEpsilon)
  91. :
  92. epsilon(inEpsilon),
  93. dimension(0),
  94. maxRange((Real)0),
  95. origin{ (Real)0, (Real)0 },
  96. extremeCCW(false)
  97. {
  98. min[0] = (Real)0;
  99. min[1] = (Real)0;
  100. direction[0] = { (Real)0, (Real)0 };
  101. direction[1] = { (Real)0, (Real)0 };
  102. extreme[0] = 0;
  103. extreme[1] = 0;
  104. extreme[2] = 0;
  105. if (numVectors > 0 && v && epsilon >= (Real)0)
  106. {
  107. // Compute the axis-aligned bounding box for the input
  108. // vectors. Keep track of the indices into 'vectors' for the
  109. // current min and max.
  110. int j, indexMin[2], indexMax[2];
  111. for (j = 0; j < 2; ++j)
  112. {
  113. min[j] = v[0][j];
  114. max[j] = min[j];
  115. indexMin[j] = 0;
  116. indexMax[j] = 0;
  117. }
  118. int i;
  119. for (i = 1; i < numVectors; ++i)
  120. {
  121. for (j = 0; j < 2; ++j)
  122. {
  123. if (v[i][j] < min[j])
  124. {
  125. min[j] = v[i][j];
  126. indexMin[j] = i;
  127. }
  128. else if (v[i][j] > max[j])
  129. {
  130. max[j] = v[i][j];
  131. indexMax[j] = i;
  132. }
  133. }
  134. }
  135. // Determine the maximum range for the bounding box.
  136. maxRange = max[0] - min[0];
  137. extreme[0] = indexMin[0];
  138. extreme[1] = indexMax[0];
  139. Real range = max[1] - min[1];
  140. if (range > maxRange)
  141. {
  142. maxRange = range;
  143. extreme[0] = indexMin[1];
  144. extreme[1] = indexMax[1];
  145. }
  146. // The origin is either the vector of minimum x0-value or
  147. // vector of minimum x1-value.
  148. origin = v[extreme[0]];
  149. // Test whether the vector set is (nearly) a vector.
  150. if (maxRange <= epsilon)
  151. {
  152. dimension = 0;
  153. for (j = 0; j < 2; ++j)
  154. {
  155. extreme[j + 1] = extreme[0];
  156. }
  157. return;
  158. }
  159. // Test whether the vector set is (nearly) a line segment. We
  160. // need direction[1] to span the orthogonal complement of
  161. // direction[0].
  162. direction[0] = v[extreme[1]] - origin;
  163. Normalize(direction[0], false);
  164. direction[1] = -Perp(direction[0]);
  165. // Compute the maximum distance of the points from the line
  166. // origin+t*direction[0].
  167. Real maxDistance = (Real)0;
  168. Real maxSign = (Real)0;
  169. extreme[2] = extreme[0];
  170. for (i = 0; i < numVectors; ++i)
  171. {
  172. Vector2<Real> diff = v[i] - origin;
  173. Real distance = Dot(direction[1], diff);
  174. Real sign = (distance > (Real)0 ? (Real)1 :
  175. (distance < (Real)0 ? (Real)-1 : (Real)0));
  176. distance = std::fabs(distance);
  177. if (distance > maxDistance)
  178. {
  179. maxDistance = distance;
  180. maxSign = sign;
  181. extreme[2] = i;
  182. }
  183. }
  184. if (maxDistance <= epsilon * maxRange)
  185. {
  186. // The points are (nearly) on the line
  187. // origin + t * direction[0].
  188. dimension = 1;
  189. extreme[2] = extreme[1];
  190. return;
  191. }
  192. dimension = 2;
  193. extremeCCW = (maxSign > (Real)0);
  194. return;
  195. }
  196. }
  197. // A nonnegative tolerance that is used to determine the intrinsic
  198. // dimension of the set.
  199. Real epsilon;
  200. // The intrinsic dimension of the input set, computed based on the
  201. // nonnegative tolerance mEpsilon.
  202. int dimension;
  203. // Axis-aligned bounding box of the input set. The maximum range is
  204. // the larger of max[0]-min[0] and max[1]-min[1].
  205. Real min[2], max[2];
  206. Real maxRange;
  207. // Coordinate system. The origin is valid for any dimension d. The
  208. // unit-length direction vector is valid only for 0 <= i < d. The
  209. // extreme index is relative to the array of input points, and is also
  210. // valid only for 0 <= i < d. If d = 0, all points are effectively
  211. // the same, but the use of an epsilon may lead to an extreme index
  212. // that is not zero. If d = 1, all points effectively lie on a line
  213. // segment. If d = 2, the points are not collinear.
  214. Vector2<Real> origin;
  215. Vector2<Real> direction[2];
  216. // The indices that define the maximum dimensional extents. The
  217. // values extreme[0] and extreme[1] are the indices for the points
  218. // that define the largest extent in one of the coordinate axis
  219. // directions. If the dimension is 2, then extreme[2] is the index
  220. // for the point that generates the largest extent in the direction
  221. // perpendicular to the line through the points corresponding to
  222. // extreme[0] and extreme[1]. The triangle formed by the points
  223. // V[extreme[0]], V[extreme[1]], and V[extreme[2]] is clockwise or
  224. // counterclockwise, the condition stored in extremeCCW.
  225. int extreme[3];
  226. bool extremeCCW;
  227. };
  228. }