vector2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * vector3.cpp
  3. * Copyright (C) Andrew Tridgell 2012
  4. *
  5. * This file is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma GCC optimize("O2")
  19. #include "AP_Math.h"
  20. template <typename T>
  21. float Vector2<T>::length_squared() const
  22. {
  23. return (float)(x*x + y*y);
  24. }
  25. template <typename T>
  26. float Vector2<T>::length(void) const
  27. {
  28. return norm(x, y);
  29. }
  30. // dot product
  31. template <typename T>
  32. T Vector2<T>::operator *(const Vector2<T> &v) const
  33. {
  34. return x*v.x + y*v.y;
  35. }
  36. // cross product
  37. template <typename T>
  38. T Vector2<T>::operator %(const Vector2<T> &v) const
  39. {
  40. return x*v.y - y*v.x;
  41. }
  42. template <typename T>
  43. Vector2<T> &Vector2<T>::operator *=(const T num)
  44. {
  45. x*=num; y*=num;
  46. return *this;
  47. }
  48. template <typename T>
  49. Vector2<T> &Vector2<T>::operator /=(const T num)
  50. {
  51. x /= num; y /= num;
  52. return *this;
  53. }
  54. template <typename T>
  55. Vector2<T> &Vector2<T>::operator -=(const Vector2<T> &v)
  56. {
  57. x -= v.x; y -= v.y;
  58. return *this;
  59. }
  60. template <typename T>
  61. bool Vector2<T>::is_nan(void) const
  62. {
  63. return isnan(x) || isnan(y);
  64. }
  65. template <typename T>
  66. bool Vector2<T>::is_inf(void) const
  67. {
  68. return isinf(x) || isinf(y);
  69. }
  70. template <typename T>
  71. Vector2<T> &Vector2<T>::operator +=(const Vector2<T> &v)
  72. {
  73. x+=v.x; y+=v.y;
  74. return *this;
  75. }
  76. template <typename T>
  77. Vector2<T> Vector2<T>::operator /(const T num) const
  78. {
  79. return Vector2<T>(x/num, y/num);
  80. }
  81. template <typename T>
  82. Vector2<T> Vector2<T>::operator *(const T num) const
  83. {
  84. return Vector2<T>(x*num, y*num);
  85. }
  86. template <typename T>
  87. Vector2<T> Vector2<T>::operator -(const Vector2<T> &v) const
  88. {
  89. return Vector2<T>(x-v.x, y-v.y);
  90. }
  91. template <typename T>
  92. Vector2<T> Vector2<T>::operator +(const Vector2<T> &v) const
  93. {
  94. return Vector2<T>(x+v.x, y+v.y);
  95. }
  96. template <typename T>
  97. Vector2<T> Vector2<T>::operator -(void) const
  98. {
  99. return Vector2<T>(-x,-y);
  100. }
  101. template <typename T>
  102. bool Vector2<T>::operator ==(const Vector2<T> &v) const
  103. {
  104. return (is_equal(x,v.x) && is_equal(y,v.y));
  105. }
  106. template <typename T>
  107. bool Vector2<T>::operator !=(const Vector2<T> &v) const
  108. {
  109. return (!is_equal(x,v.x) || !is_equal(y,v.y));
  110. }
  111. template <typename T>
  112. float Vector2<T>::angle(const Vector2<T> &v2) const
  113. {
  114. const float len = this->length() * v2.length();
  115. if (len <= 0) {
  116. return 0.0f;
  117. }
  118. const float cosv = ((*this)*v2) / len;
  119. if (cosv >= 1) {
  120. return 0.0f;
  121. }
  122. if (cosv <= -1) {
  123. return M_PI;
  124. }
  125. return acosf(cosv);
  126. }
  127. template <typename T>
  128. float Vector2<T>::angle(void) const
  129. {
  130. return M_PI_2 + atan2f(-x, y);
  131. }
  132. // find the intersection between two line segments
  133. // returns true if they intersect, false if they do not
  134. // the point of intersection is returned in the intersection argument
  135. template <typename T>
  136. bool Vector2<T>::segment_intersection(const Vector2<T>& seg1_start, const Vector2<T>& seg1_end, const Vector2<T>& seg2_start, const Vector2<T>& seg2_end, Vector2<T>& intersection)
  137. {
  138. // implementation borrowed from http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
  139. const Vector2<T> r1 = seg1_end - seg1_start;
  140. const Vector2<T> r2 = seg2_end - seg2_start;
  141. const Vector2<T> ss2_ss1 = seg2_start - seg1_start;
  142. const float r1xr2 = r1 % r2;
  143. const float q_pxr = ss2_ss1 % r1;
  144. if (fabsf(r1xr2) < FLT_EPSILON) {
  145. // either collinear or parallel and non-intersecting
  146. return false;
  147. } else {
  148. // t = (q - p) * s / (r * s)
  149. // u = (q - p) * r / (r * s)
  150. const float t = (ss2_ss1 % r2) / r1xr2;
  151. const float u = q_pxr / r1xr2;
  152. if ((u >= 0) && (u <= 1) && (t >= 0) && (t <= 1)) {
  153. // lines intersect
  154. // t can be any non-negative value because (p, p + r) is a ray
  155. // u must be between 0 and 1 because (q, q + s) is a line segment
  156. intersection = seg1_start + (r1*t);
  157. return true;
  158. } else {
  159. // non-parallel and non-intersecting
  160. return false;
  161. }
  162. }
  163. }
  164. // find the intersection between a line segment and a circle
  165. // returns true if they intersect and intersection argument is updated with intersection closest to seg_start
  166. // solution adapted from http://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
  167. template <typename T>
  168. bool Vector2<T>::circle_segment_intersection(const Vector2<T>& seg_start, const Vector2<T>& seg_end, const Vector2<T>& circle_center, float radius, Vector2<T>& intersection)
  169. {
  170. // calculate segment start and end as offsets from circle's center
  171. const Vector2f seg_start_local = seg_start - circle_center;
  172. // calculate vector from start to end
  173. const Vector2f seg_end_minus_start = seg_end - seg_start;
  174. const float a = sq(seg_end_minus_start.x) + sq(seg_end_minus_start.y);
  175. const float b = 2 * ((seg_end_minus_start.x * seg_start_local.x) + (seg_end_minus_start.y * seg_start_local.y));
  176. const float c = sq(seg_start_local.x) + sq(seg_start_local.y) - sq(radius);
  177. const float delta = sq(b) - (4.0f * a * c);
  178. // check for invalid data
  179. if (::is_zero(a)) {
  180. return false;
  181. }
  182. if (isnan(a) || isnan(b) || isnan(c) || isnan(delta)) {
  183. return false;
  184. }
  185. // check for invalid delta (i.e. discriminant)
  186. if (delta < 0.0f) {
  187. return false;
  188. }
  189. const float delta_sqrt = sqrtf(delta);
  190. const float t1 = (-b + delta_sqrt) / (2.0f * a);
  191. const float t2 = (-b - delta_sqrt) / (2.0f * a);
  192. // Three hit cases:
  193. // -o-> --|--> | | --|->
  194. // Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit),
  195. // Three miss cases:
  196. // -> o o -> | -> |
  197. // FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1)
  198. // intersection = new Vector3(E.x + t1 * d.x, secondPoint.y, E.y + t1 * d.y);
  199. // intersection.x = seg_start.x + t1 * seg_end_minus_start.x;
  200. // intersection.y = seg_start.y + t1 * seg_end_minus_start.y;
  201. if ((t1 >= 0.0f) && (t1 <= 1.0f)) {
  202. // t1 is the intersection, and it is closer than t2 (since t1 uses -b - discriminant)
  203. // Impale, Poke
  204. intersection = seg_start + (seg_end_minus_start * t1);
  205. return true;
  206. }
  207. // here t1 did not intersect so we are either started inside the sphere or completely past it
  208. if ((t2 >= 0.0f) && (t2 <= 1.0f)) {
  209. // ExitWound
  210. intersection = seg_start + (seg_end_minus_start * t2);
  211. return true;
  212. }
  213. // no intersection: FallShort, Past or CompletelyInside
  214. return false;
  215. }
  216. // normalizes this vector
  217. template <typename T>
  218. void Vector2<T>::normalize()
  219. {
  220. *this /= length();
  221. }
  222. // returns the normalized vector
  223. template <typename T>
  224. Vector2<T> Vector2<T>::normalized() const
  225. {
  226. return *this/length();
  227. }
  228. // reflects this vector about n
  229. template <typename T>
  230. void Vector2<T>::reflect(const Vector2<T> &n)
  231. {
  232. const Vector2<T> orig(*this);
  233. project(n);
  234. *this = *this*2 - orig;
  235. }
  236. // projects this vector onto v
  237. template <typename T>
  238. void Vector2<T>::project(const Vector2<T> &v)
  239. {
  240. *this= v * (*this * v)/(v*v);
  241. }
  242. // returns this vector projected onto v
  243. template <typename T>
  244. Vector2<T> Vector2<T>::projected(const Vector2<T> &v)
  245. {
  246. return v * (*this * v)/(v*v);
  247. }
  248. // given a position pos_delta and a velocity v1 produce a vector
  249. // perpendicular to v1 maximising distance from p1
  250. template <typename T>
  251. Vector2<T> Vector2<T>::perpendicular(const Vector2<T> &pos_delta, const Vector2<T> &v1)
  252. {
  253. const Vector2<T> perpendicular1 = Vector2<T>(-v1[1], v1[0]);
  254. const Vector2<T> perpendicular2 = Vector2<T>(v1[1], -v1[0]);
  255. const T d1 = perpendicular1 * pos_delta;
  256. const T d2 = perpendicular2 * pos_delta;
  257. if (d1 > d2) {
  258. return perpendicular1;
  259. }
  260. return perpendicular2;
  261. }
  262. /*
  263. * Returns the point closest to p on the line segment (v,w).
  264. *
  265. * Comments and implementation taken from
  266. * http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
  267. */
  268. template <typename T>
  269. Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &v, const Vector2<T> &w)
  270. {
  271. // length squared of line segment
  272. const float l2 = (v - w).length_squared();
  273. if (l2 < FLT_EPSILON) {
  274. // v == w case
  275. return v;
  276. }
  277. // Consider the line extending the segment, parameterized as v + t (w - v).
  278. // We find projection of point p onto the line.
  279. // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  280. // We clamp t from [0,1] to handle points outside the segment vw.
  281. const float t = ((p - v) * (w - v)) / l2;
  282. if (t <= 0) {
  283. return v;
  284. } else if (t >= 1) {
  285. return w;
  286. } else {
  287. return v + (w - v)*t;
  288. }
  289. }
  290. /*
  291. * Returns the point closest to p on the line segment (0,w).
  292. *
  293. * this is a simplification of closest point with a general segment, with v=(0,0)
  294. */
  295. template <typename T>
  296. Vector2<T> Vector2<T>::closest_point(const Vector2<T> &p, const Vector2<T> &w)
  297. {
  298. // length squared of line segment
  299. const float l2 = w.length_squared();
  300. if (l2 < FLT_EPSILON) {
  301. // v == w case
  302. return w;
  303. }
  304. const float t = (p * w) / l2;
  305. if (t <= 0) {
  306. return Vector2<T>(0,0);
  307. } else if (t >= 1) {
  308. return w;
  309. } else {
  310. return w*t;
  311. }
  312. }
  313. // closest distance between a line segment and a point
  314. // https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
  315. template <typename T>
  316. float Vector2<T>::closest_distance_between_line_and_point_squared(const Vector2<T> &w1,
  317. const Vector2<T> &w2,
  318. const Vector2<T> &p)
  319. {
  320. return closest_distance_between_radial_and_point_squared(w2-w1, p-w1);
  321. }
  322. // w1 and w2 define a line segment
  323. // p is a point
  324. // returns the closest distance between the line segment and the point
  325. template <typename T>
  326. float Vector2<T>::closest_distance_between_line_and_point(const Vector2<T> &w1,
  327. const Vector2<T> &w2,
  328. const Vector2<T> &p)
  329. {
  330. return sqrtf(closest_distance_between_line_and_point_squared(w1, w2, p));
  331. }
  332. // a1->a2 and b2->v2 define two line segments
  333. // returns the square of the closest distance between the two line segments
  334. // see https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
  335. template <typename T>
  336. float Vector2<T>::closest_distance_between_lines_squared(const Vector2<T> &a1,
  337. const Vector2<T> &a2,
  338. const Vector2<T> &b1,
  339. const Vector2<T> &b2)
  340. {
  341. const float dist1 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a1);
  342. const float dist2 = Vector2<T>::closest_distance_between_line_and_point_squared(b1,b2,a2);
  343. const float dist3 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b1);
  344. const float dist4 = Vector2<T>::closest_distance_between_line_and_point_squared(a1,a2,b2);
  345. const float m1 = MIN(dist1,dist2);
  346. const float m2 = MIN(dist3,dist4);
  347. return MIN(m1,m2);
  348. }
  349. // w defines a line segment from the origin
  350. // p is a point
  351. // returns the square of the closest distance between the radial and the point
  352. template <typename T>
  353. float Vector2<T>::closest_distance_between_radial_and_point_squared(const Vector2<T> &w,
  354. const Vector2<T> &p)
  355. {
  356. const Vector2<T> closest = closest_point(p, w);
  357. return (closest - p).length_squared();
  358. }
  359. // w defines a line segment from the origin
  360. // p is a point
  361. // returns the closest distance between the radial and the point
  362. template <typename T>
  363. float Vector2<T>::closest_distance_between_radial_and_point(const Vector2<T> &w,
  364. const Vector2<T> &p)
  365. {
  366. return sqrtf(closest_distance_between_radial_and_point_squared(w,p));
  367. }
  368. // only define for float
  369. template float Vector2<float>::length_squared(void) const;
  370. template float Vector2<float>::length(void) const;
  371. template Vector2<float> Vector2<float>::normalized() const;
  372. template void Vector2<float>::normalize();
  373. template float Vector2<float>::operator *(const Vector2<float> &v) const;
  374. template float Vector2<float>::operator %(const Vector2<float> &v) const;
  375. template Vector2<float> &Vector2<float>::operator *=(const float num);
  376. template Vector2<float> &Vector2<float>::operator /=(const float num);
  377. template Vector2<float> &Vector2<float>::operator -=(const Vector2<float> &v);
  378. template Vector2<float> &Vector2<float>::operator +=(const Vector2<float> &v);
  379. template Vector2<float> Vector2<float>::operator /(const float num) const;
  380. template Vector2<float> Vector2<float>::operator *(const float num) const;
  381. template Vector2<float> Vector2<float>::operator +(const Vector2<float> &v) const;
  382. template Vector2<float> Vector2<float>::operator -(const Vector2<float> &v) const;
  383. template Vector2<float> Vector2<float>::operator -(void) const;
  384. template bool Vector2<float>::operator ==(const Vector2<float> &v) const;
  385. template bool Vector2<float>::operator !=(const Vector2<float> &v) const;
  386. template bool Vector2<float>::is_nan(void) const;
  387. template bool Vector2<float>::is_inf(void) const;
  388. template float Vector2<float>::angle(const Vector2<float> &v) const;
  389. template float Vector2<float>::angle(void) const;
  390. template bool Vector2<float>::segment_intersection(const Vector2<float>& seg1_start, const Vector2<float>& seg1_end, const Vector2<float>& seg2_start, const Vector2<float>& seg2_end, Vector2<float>& intersection);
  391. template bool Vector2<float>::circle_segment_intersection(const Vector2<float>& seg_start, const Vector2<float>& seg_end, const Vector2<float>& circle_center, float radius, Vector2<float>& intersection);
  392. template Vector2<float> Vector2<float>::perpendicular(const Vector2<float> &pos_delta, const Vector2<float> &v1);
  393. template Vector2<float> Vector2<float>::closest_point(const Vector2<float> &p, const Vector2<float> &v, const Vector2<float> &w);
  394. template float Vector2<float>::closest_distance_between_radial_and_point_squared(const Vector2<float> &w, const Vector2<float> &p);
  395. template float Vector2<float>::closest_distance_between_radial_and_point(const Vector2<float> &w, const Vector2<float> &p);
  396. template float Vector2<float>::closest_distance_between_line_and_point(const Vector2<float> &w1, const Vector2<float> &w2, const Vector2<float> &p);
  397. template float Vector2<float>::closest_distance_between_line_and_point_squared(const Vector2<float> &w1, const Vector2<float> &w2, const Vector2<float> &p);
  398. template float Vector2<float>::closest_distance_between_lines_squared(const Vector2<float> &a1,const Vector2<float> &a2,const Vector2<float> &b1,const Vector2<float> &b2);
  399. template void Vector2<float>::reflect(const Vector2<float> &n);
  400. template bool Vector2<long>::operator ==(const Vector2<long> &v) const;
  401. // define for int
  402. template bool Vector2<int>::operator ==(const Vector2<int> &v) const;