vector3.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #define HALF_SQRT_2 0.70710678118654757f
  21. // rotate a vector by a standard rotation, attempting
  22. // to use the minimum number of floating point operations
  23. template <typename T>
  24. void Vector3<T>::rotate(enum Rotation rotation)
  25. {
  26. T tmp;
  27. switch (rotation) {
  28. case ROTATION_NONE:
  29. case ROTATION_MAX:
  30. return;
  31. case ROTATION_YAW_45: {
  32. tmp = HALF_SQRT_2*(float)(x - y);
  33. y = HALF_SQRT_2*(float)(x + y);
  34. x = tmp;
  35. return;
  36. }
  37. case ROTATION_YAW_90: {
  38. tmp = x; x = -y; y = tmp;
  39. return;
  40. }
  41. case ROTATION_YAW_135: {
  42. tmp = -HALF_SQRT_2*(float)(x + y);
  43. y = HALF_SQRT_2*(float)(x - y);
  44. x = tmp;
  45. return;
  46. }
  47. case ROTATION_YAW_180:
  48. x = -x; y = -y;
  49. return;
  50. case ROTATION_YAW_225: {
  51. tmp = HALF_SQRT_2*(float)(y - x);
  52. y = -HALF_SQRT_2*(float)(x + y);
  53. x = tmp;
  54. return;
  55. }
  56. case ROTATION_YAW_270: {
  57. tmp = x; x = y; y = -tmp;
  58. return;
  59. }
  60. case ROTATION_YAW_315: {
  61. tmp = HALF_SQRT_2*(float)(x + y);
  62. y = HALF_SQRT_2*(float)(y - x);
  63. x = tmp;
  64. return;
  65. }
  66. case ROTATION_ROLL_180: {
  67. y = -y; z = -z;
  68. return;
  69. }
  70. case ROTATION_ROLL_180_YAW_45: {
  71. tmp = HALF_SQRT_2*(float)(x + y);
  72. y = HALF_SQRT_2*(float)(x - y);
  73. x = tmp; z = -z;
  74. return;
  75. }
  76. case ROTATION_ROLL_180_YAW_90: {
  77. tmp = x; x = y; y = tmp; z = -z;
  78. return;
  79. }
  80. case ROTATION_ROLL_180_YAW_135: {
  81. tmp = HALF_SQRT_2*(float)(y - x);
  82. y = HALF_SQRT_2*(float)(y + x);
  83. x = tmp; z = -z;
  84. return;
  85. }
  86. case ROTATION_PITCH_180: {
  87. x = -x; z = -z;
  88. return;
  89. }
  90. case ROTATION_ROLL_180_YAW_225: {
  91. tmp = -HALF_SQRT_2*(float)(x + y);
  92. y = HALF_SQRT_2*(float)(y - x);
  93. x = tmp; z = -z;
  94. return;
  95. }
  96. case ROTATION_ROLL_180_YAW_270: {
  97. tmp = x; x = -y; y = -tmp; z = -z;
  98. return;
  99. }
  100. case ROTATION_ROLL_180_YAW_315: {
  101. tmp = HALF_SQRT_2*(float)(x - y);
  102. y = -HALF_SQRT_2*(float)(x + y);
  103. x = tmp; z = -z;
  104. return;
  105. }
  106. case ROTATION_ROLL_90: {
  107. tmp = z; z = y; y = -tmp;
  108. return;
  109. }
  110. case ROTATION_ROLL_90_YAW_45: {
  111. tmp = z; z = y; y = -tmp;
  112. tmp = HALF_SQRT_2*(float)(x - y);
  113. y = HALF_SQRT_2*(float)(x + y);
  114. x = tmp;
  115. return;
  116. }
  117. case ROTATION_ROLL_90_YAW_90: {
  118. tmp = z; z = y; y = -tmp;
  119. tmp = x; x = -y; y = tmp;
  120. return;
  121. }
  122. case ROTATION_ROLL_90_YAW_135: {
  123. tmp = z; z = y; y = -tmp;
  124. tmp = -HALF_SQRT_2*(float)(x + y);
  125. y = HALF_SQRT_2*(float)(x - y);
  126. x = tmp;
  127. return;
  128. }
  129. case ROTATION_ROLL_270: {
  130. tmp = z; z = -y; y = tmp;
  131. return;
  132. }
  133. case ROTATION_ROLL_270_YAW_45: {
  134. tmp = z; z = -y; y = tmp;
  135. tmp = HALF_SQRT_2*(float)(x - y);
  136. y = HALF_SQRT_2*(float)(x + y);
  137. x = tmp;
  138. return;
  139. }
  140. case ROTATION_ROLL_270_YAW_90: {
  141. tmp = z; z = -y; y = tmp;
  142. tmp = x; x = -y; y = tmp;
  143. return;
  144. }
  145. case ROTATION_ROLL_270_YAW_135: {
  146. tmp = z; z = -y; y = tmp;
  147. tmp = -HALF_SQRT_2*(float)(x + y);
  148. y = HALF_SQRT_2*(float)(x - y);
  149. x = tmp;
  150. return;
  151. }
  152. case ROTATION_PITCH_90: {
  153. tmp = z; z = -x; x = tmp;
  154. return;
  155. }
  156. case ROTATION_PITCH_270: {
  157. tmp = z; z = x; x = -tmp;
  158. return;
  159. }
  160. case ROTATION_PITCH_180_YAW_90: {
  161. z = -z;
  162. tmp = -x; x = -y; y = tmp;
  163. return;
  164. }
  165. case ROTATION_PITCH_180_YAW_270: {
  166. x = -x; z = -z;
  167. tmp = x; x = y; y = -tmp;
  168. return;
  169. }
  170. case ROTATION_ROLL_90_PITCH_90: {
  171. tmp = z; z = y; y = -tmp;
  172. tmp = z; z = -x; x = tmp;
  173. return;
  174. }
  175. case ROTATION_ROLL_180_PITCH_90: {
  176. y = -y; z = -z;
  177. tmp = z; z = -x; x = tmp;
  178. return;
  179. }
  180. case ROTATION_ROLL_270_PITCH_90: {
  181. tmp = z; z = -y; y = tmp;
  182. tmp = z; z = -x; x = tmp;
  183. return;
  184. }
  185. case ROTATION_ROLL_90_PITCH_180: {
  186. tmp = z; z = y; y = -tmp;
  187. x = -x; z = -z;
  188. return;
  189. }
  190. case ROTATION_ROLL_270_PITCH_180: {
  191. tmp = z; z = -y; y = tmp;
  192. x = -x; z = -z;
  193. return;
  194. }
  195. case ROTATION_ROLL_90_PITCH_270: {
  196. tmp = z; z = y; y = -tmp;
  197. tmp = z; z = x; x = -tmp;
  198. return;
  199. }
  200. case ROTATION_ROLL_180_PITCH_270: {
  201. y = -y; z = -z;
  202. tmp = z; z = x; x = -tmp;
  203. return;
  204. }
  205. case ROTATION_ROLL_270_PITCH_270: {
  206. tmp = z; z = -y; y = tmp;
  207. tmp = z; z = x; x = -tmp;
  208. return;
  209. }
  210. case ROTATION_ROLL_90_PITCH_180_YAW_90: {
  211. tmp = z; z = y; y = -tmp;
  212. x = -x; z = -z;
  213. tmp = x; x = -y; y = tmp;
  214. return;
  215. }
  216. case ROTATION_ROLL_90_YAW_270: {
  217. tmp = z; z = y; y = -tmp;
  218. tmp = x; x = y; y = -tmp;
  219. return;
  220. }
  221. case ROTATION_ROLL_90_PITCH_68_YAW_293: {
  222. float tmpx = x;
  223. float tmpy = y;
  224. float tmpz = z;
  225. x = 0.143039f * tmpx + 0.368776f * tmpy + -0.918446f * tmpz;
  226. y = -0.332133f * tmpx + -0.856289f * tmpy + -0.395546f * tmpz;
  227. z = -0.932324f * tmpx + 0.361625f * tmpy + 0.000000f * tmpz;
  228. return;
  229. }
  230. case ROTATION_PITCH_315: {
  231. tmp = HALF_SQRT_2*(float)(x - z);
  232. z = HALF_SQRT_2*(float)(x + z);
  233. x = tmp;
  234. return;
  235. }
  236. case ROTATION_ROLL_90_PITCH_315: {
  237. tmp = z; z = y; y = -tmp;
  238. tmp = HALF_SQRT_2*(float)(x - z);
  239. z = HALF_SQRT_2*(float)(x + z);
  240. x = tmp;
  241. return;
  242. }
  243. case ROTATION_PITCH_7: {
  244. const float sin_pitch = 0.12186934340514748f; // sinf(pitch);
  245. const float cos_pitch = 0.992546151641322f; // cosf(pitch);
  246. float tmpx = x;
  247. float tmpz = z;
  248. x = cos_pitch * tmpx + sin_pitch * tmpz;
  249. z = -sin_pitch * tmpx + cos_pitch * tmpz;
  250. return;
  251. }
  252. case ROTATION_CUSTOM: // no-op; caller should perform custom rotations via matrix multiplication
  253. return;
  254. }
  255. }
  256. template <typename T>
  257. void Vector3<T>::rotate_inverse(enum Rotation rotation)
  258. {
  259. Vector3<T> x_vec(1.0f,0.0f,0.0f);
  260. Vector3<T> y_vec(0.0f,1.0f,0.0f);
  261. Vector3<T> z_vec(0.0f,0.0f,1.0f);
  262. x_vec.rotate(rotation);
  263. y_vec.rotate(rotation);
  264. z_vec.rotate(rotation);
  265. Matrix3<T> M(
  266. x_vec.x, y_vec.x, z_vec.x,
  267. x_vec.y, y_vec.y, z_vec.y,
  268. x_vec.z, y_vec.z, z_vec.z
  269. );
  270. (*this) = M.mul_transpose(*this);
  271. }
  272. // vector cross product
  273. template <typename T>
  274. Vector3<T> Vector3<T>::operator %(const Vector3<T> &v) const
  275. {
  276. Vector3<T> temp(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
  277. return temp;
  278. }
  279. // dot product
  280. template <typename T>
  281. T Vector3<T>::operator *(const Vector3<T> &v) const
  282. {
  283. return x*v.x + y*v.y + z*v.z;
  284. }
  285. template <typename T>
  286. float Vector3<T>::length(void) const
  287. {
  288. return norm(x, y, z);
  289. }
  290. template <typename T>
  291. Vector3<T> &Vector3<T>::operator *=(const T num)
  292. {
  293. x*=num; y*=num; z*=num;
  294. return *this;
  295. }
  296. template <typename T>
  297. Vector3<T> &Vector3<T>::operator /=(const T num)
  298. {
  299. x /= num; y /= num; z /= num;
  300. return *this;
  301. }
  302. template <typename T>
  303. Vector3<T> &Vector3<T>::operator -=(const Vector3<T> &v)
  304. {
  305. x -= v.x; y -= v.y; z -= v.z;
  306. return *this;
  307. }
  308. template <typename T>
  309. bool Vector3<T>::is_nan(void) const
  310. {
  311. return isnan(x) || isnan(y) || isnan(z);
  312. }
  313. template <typename T>
  314. bool Vector3<T>::is_inf(void) const
  315. {
  316. return isinf(x) || isinf(y) || isinf(z);
  317. }
  318. template <typename T>
  319. Vector3<T> &Vector3<T>::operator +=(const Vector3<T> &v)
  320. {
  321. x+=v.x; y+=v.y; z+=v.z;
  322. return *this;
  323. }
  324. template <typename T>
  325. Vector3<T> Vector3<T>::operator /(const T num) const
  326. {
  327. return Vector3<T>(x/num, y/num, z/num);
  328. }
  329. template <typename T>
  330. Vector3<T> Vector3<T>::operator *(const T num) const
  331. {
  332. return Vector3<T>(x*num, y*num, z*num);
  333. }
  334. template <typename T>
  335. Vector3<T> Vector3<T>::operator -(const Vector3<T> &v) const
  336. {
  337. return Vector3<T>(x-v.x, y-v.y, z-v.z);
  338. }
  339. template <typename T>
  340. Vector3<T> Vector3<T>::operator +(const Vector3<T> &v) const
  341. {
  342. return Vector3<T>(x+v.x, y+v.y, z+v.z);
  343. }
  344. template <typename T>
  345. Vector3<T> Vector3<T>::operator -(void) const
  346. {
  347. return Vector3<T>(-x,-y,-z);
  348. }
  349. template <typename T>
  350. bool Vector3<T>::operator ==(const Vector3<T> &v) const
  351. {
  352. return (is_equal(x,v.x) && is_equal(y,v.y) && is_equal(z,v.z));
  353. }
  354. template <typename T>
  355. bool Vector3<T>::operator !=(const Vector3<T> &v) const
  356. {
  357. return (!is_equal(x,v.x) || !is_equal(y,v.y) || !is_equal(z,v.z));
  358. }
  359. template <typename T>
  360. float Vector3<T>::angle(const Vector3<T> &v2) const
  361. {
  362. const float len = this->length() * v2.length();
  363. if (len <= 0) {
  364. return 0.0f;
  365. }
  366. const float cosv = ((*this)*v2) / len;
  367. if (fabsf(cosv) >= 1) {
  368. return 0.0f;
  369. }
  370. return acosf(cosv);
  371. }
  372. // multiplication of transpose by a vector
  373. template <typename T>
  374. Vector3<T> Vector3<T>::operator *(const Matrix3<T> &m) const
  375. {
  376. return Vector3<T>(*this * m.colx(),
  377. *this * m.coly(),
  378. *this * m.colz());
  379. }
  380. // multiply a column vector by a row vector, returning a 3x3 matrix
  381. template <typename T>
  382. Matrix3<T> Vector3<T>::mul_rowcol(const Vector3<T> &v2) const
  383. {
  384. const Vector3<T> v1 = *this;
  385. return Matrix3<T>(v1.x * v2.x, v1.x * v2.y, v1.x * v2.z,
  386. v1.y * v2.x, v1.y * v2.y, v1.y * v2.z,
  387. v1.z * v2.x, v1.z * v2.y, v1.z * v2.z);
  388. }
  389. // distance from the tip of this vector to a line segment specified by two vectors
  390. template <typename T>
  391. float Vector3<T>::distance_to_segment(const Vector3<T> &seg_start, const Vector3<T> &seg_end) const
  392. {
  393. // triangle side lengths
  394. const float a = (*this-seg_start).length();
  395. const float b = (seg_start-seg_end).length();
  396. const float c = (seg_end-*this).length();
  397. // protect against divide by zero later
  398. if (::is_zero(b)) {
  399. return 0.0f;
  400. }
  401. // semiperimeter of triangle
  402. const float s = (a+b+c) * 0.5f;
  403. float area_squared = s*(s-a)*(s-b)*(s-c);
  404. // area must be constrained above 0 because a triangle could have 3 points could be on a line and float rounding could push this under 0
  405. if (area_squared < 0.0f) {
  406. area_squared = 0.0f;
  407. }
  408. const float area = safe_sqrt(area_squared);
  409. return 2.0f*area/b;
  410. }
  411. // define for float
  412. template void Vector3<float>::rotate(enum Rotation);
  413. template void Vector3<float>::rotate_inverse(enum Rotation);
  414. template float Vector3<float>::length(void) const;
  415. template Vector3<float> Vector3<float>::operator %(const Vector3<float> &v) const;
  416. template float Vector3<float>::operator *(const Vector3<float> &v) const;
  417. template Vector3<float> Vector3<float>::operator *(const Matrix3<float> &m) const;
  418. template Matrix3<float> Vector3<float>::mul_rowcol(const Vector3<float> &v) const;
  419. template Vector3<float> &Vector3<float>::operator *=(const float num);
  420. template Vector3<float> &Vector3<float>::operator /=(const float num);
  421. template Vector3<float> &Vector3<float>::operator -=(const Vector3<float> &v);
  422. template Vector3<float> &Vector3<float>::operator +=(const Vector3<float> &v);
  423. template Vector3<float> Vector3<float>::operator /(const float num) const;
  424. template Vector3<float> Vector3<float>::operator *(const float num) const;
  425. template Vector3<float> Vector3<float>::operator +(const Vector3<float> &v) const;
  426. template Vector3<float> Vector3<float>::operator -(const Vector3<float> &v) const;
  427. template Vector3<float> Vector3<float>::operator -(void) const;
  428. template bool Vector3<float>::operator ==(const Vector3<float> &v) const;
  429. template bool Vector3<float>::operator !=(const Vector3<float> &v) const;
  430. template bool Vector3<float>::is_nan(void) const;
  431. template bool Vector3<float>::is_inf(void) const;
  432. template float Vector3<float>::angle(const Vector3<float> &v) const;
  433. template float Vector3<float>::distance_to_segment(const Vector3<float> &seg_start, const Vector3<float> &seg_end) const;
  434. // define needed ops for Vector3l
  435. template Vector3<int32_t> &Vector3<int32_t>::operator +=(const Vector3<int32_t> &v);
  436. template void Vector3<double>::rotate(enum Rotation);
  437. template void Vector3<double>::rotate_inverse(enum Rotation);
  438. template float Vector3<double>::length(void) const;
  439. template Vector3<double> Vector3<double>::operator %(const Vector3<double> &v) const;
  440. template double Vector3<double>::operator *(const Vector3<double> &v) const;
  441. template Vector3<double> Vector3<double>::operator *(const Matrix3<double> &m) const;
  442. template Matrix3<double> Vector3<double>::mul_rowcol(const Vector3<double> &v) const;
  443. template Vector3<double> &Vector3<double>::operator *=(const double num);
  444. template Vector3<double> &Vector3<double>::operator /=(const double num);
  445. template Vector3<double> &Vector3<double>::operator -=(const Vector3<double> &v);
  446. template Vector3<double> &Vector3<double>::operator +=(const Vector3<double> &v);
  447. template Vector3<double> Vector3<double>::operator /(const double num) const;
  448. template Vector3<double> Vector3<double>::operator *(const double num) const;
  449. template Vector3<double> Vector3<double>::operator +(const Vector3<double> &v) const;
  450. template Vector3<double> Vector3<double>::operator -(const Vector3<double> &v) const;
  451. template Vector3<double> Vector3<double>::operator -(void) const;
  452. template bool Vector3<double>::operator ==(const Vector3<double> &v) const;
  453. template bool Vector3<double>::operator !=(const Vector3<double> &v) const;
  454. template bool Vector3<double>::is_nan(void) const;
  455. template bool Vector3<double>::is_inf(void) const;