quaternion.inl.hpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. //
  6. // License Agreement
  7. // For Open Source Computer Vision Library
  8. //
  9. // Copyright (C) 2020, Huawei Technologies Co., Ltd. All rights reserved.
  10. // Third party copyrights are property of their respective owners.
  11. //
  12. // Licensed under the Apache License, Version 2.0 (the "License");
  13. // you may not use this file except in compliance with the License.
  14. // You may obtain a copy of the License at
  15. //
  16. // http://www.apache.org/licenses/LICENSE-2.0
  17. //
  18. // Unless required by applicable law or agreed to in writing, software
  19. // distributed under the License is distributed on an "AS IS" BASIS,
  20. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. // See the License for the specific language governing permissions and
  22. // limitations under the License.
  23. //
  24. // Author: Liangqian Kong <chargerKong@126.com>
  25. // Longbu Wang <riskiest@gmail.com>
  26. #ifndef OPENCV_CORE_QUATERNION_INL_HPP
  27. #define OPENCV_CORE_QUATERNION_INL_HPP
  28. #ifndef OPENCV_CORE_QUATERNION_HPP
  29. #error This is not a standalone header. Include quaternion.hpp instead.
  30. #endif
  31. //@cond IGNORE
  32. ///////////////////////////////////////////////////////////////////////////////////////
  33. //Implementation
  34. namespace cv {
  35. template <typename T>
  36. Quat<T>::Quat() : w(0), x(0), y(0), z(0) {}
  37. template <typename T>
  38. Quat<T>::Quat(const Vec<T, 4> &coeff):w(coeff[0]), x(coeff[1]), y(coeff[2]), z(coeff[3]){}
  39. template <typename T>
  40. Quat<T>::Quat(const T qw, const T qx, const T qy, const T qz):w(qw), x(qx), y(qy), z(qz){}
  41. template <typename T>
  42. Quat<T> Quat<T>::createFromAngleAxis(const T angle, const Vec<T, 3> &axis)
  43. {
  44. T w, x, y, z;
  45. T vNorm = std::sqrt(axis.dot(axis));
  46. if (vNorm < CV_QUAT_EPS)
  47. {
  48. CV_Error(Error::StsBadArg, "this quaternion does not represent a rotation");
  49. }
  50. const T angle_half = angle * T(0.5);
  51. w = std::cos(angle_half);
  52. const T sin_v = std::sin(angle_half);
  53. const T sin_norm = sin_v / vNorm;
  54. x = sin_norm * axis[0];
  55. y = sin_norm * axis[1];
  56. z = sin_norm * axis[2];
  57. return Quat<T>(w, x, y, z);
  58. }
  59. template <typename T>
  60. Quat<T> Quat<T>::createFromRotMat(InputArray _R)
  61. {
  62. CV_CheckTypeEQ(_R.type(), cv::traits::Type<T>::value, "");
  63. if (_R.rows() != 3 || _R.cols() != 3)
  64. {
  65. CV_Error(Error::StsBadArg, "Cannot convert matrix to quaternion: rotation matrix should be a 3x3 matrix");
  66. }
  67. Matx<T, 3, 3> R;
  68. _R.copyTo(R);
  69. T S, w, x, y, z;
  70. T trace = R(0, 0) + R(1, 1) + R(2, 2);
  71. if (trace > 0)
  72. {
  73. S = std::sqrt(trace + 1) * T(2);
  74. x = (R(1, 2) - R(2, 1)) / S;
  75. y = (R(2, 0) - R(0, 2)) / S;
  76. z = (R(0, 1) - R(1, 0)) / S;
  77. w = -T(0.25) * S;
  78. }
  79. else if (R(0, 0) > R(1, 1) && R(0, 0) > R(2, 2))
  80. {
  81. S = std::sqrt(T(1.0) + R(0, 0) - R(1, 1) - R(2, 2)) * T(2);
  82. x = -T(0.25) * S;
  83. y = -(R(1, 0) + R(0, 1)) / S;
  84. z = -(R(0, 2) + R(2, 0)) / S;
  85. w = (R(1, 2) - R(2, 1)) / S;
  86. }
  87. else if (R(1, 1) > R(2, 2))
  88. {
  89. S = std::sqrt(T(1.0) - R(0, 0) + R(1, 1) - R(2, 2)) * T(2);
  90. x = (R(0, 1) + R(1, 0)) / S;
  91. y = T(0.25) * S;
  92. z = (R(1, 2) + R(2, 1)) / S;
  93. w = (R(0, 2) - R(2, 0)) / S;
  94. }
  95. else
  96. {
  97. S = std::sqrt(T(1.0) - R(0, 0) - R(1, 1) + R(2, 2)) * T(2);
  98. x = (R(0, 2) + R(2, 0)) / S;
  99. y = (R(1, 2) + R(2, 1)) / S;
  100. z = T(0.25) * S;
  101. w = -(R(0, 1) - R(1, 0)) / S;
  102. }
  103. return Quat<T> (w, x, y, z);
  104. }
  105. template <typename T>
  106. Quat<T> Quat<T>::createFromRvec(InputArray _rvec)
  107. {
  108. if (!((_rvec.cols() == 1 && _rvec.rows() == 3) || (_rvec.cols() == 3 && _rvec.rows() == 1))) {
  109. CV_Error(Error::StsBadArg, "Cannot convert rotation vector to quaternion: The length of rotation vector should be 3");
  110. }
  111. Vec<T, 3> rvec;
  112. _rvec.copyTo(rvec);
  113. T psi = std::sqrt(rvec.dot(rvec));
  114. if (abs(psi) < CV_QUAT_EPS) {
  115. return Quat<T> (1, 0, 0, 0);
  116. }
  117. Vec<T, 3> axis = rvec / psi;
  118. return createFromAngleAxis(psi, axis);
  119. }
  120. template <typename T>
  121. inline Quat<T> Quat<T>::operator-() const
  122. {
  123. return Quat<T>(-w, -x, -y, -z);
  124. }
  125. template <typename T>
  126. inline bool Quat<T>::operator==(const Quat<T> &q) const
  127. {
  128. return (abs(w - q.w) < CV_QUAT_EPS && abs(x - q.x) < CV_QUAT_EPS && abs(y - q.y) < CV_QUAT_EPS && abs(z - q.z) < CV_QUAT_EPS);
  129. }
  130. template <typename T>
  131. inline Quat<T> Quat<T>::operator+(const Quat<T> &q1) const
  132. {
  133. return Quat<T>(w + q1.w, x + q1.x, y + q1.y, z + q1.z);
  134. }
  135. template <typename T>
  136. inline Quat<T> operator+(const T a, const Quat<T>& q)
  137. {
  138. return Quat<T>(q.w + a, q.x, q.y, q.z);
  139. }
  140. template <typename T>
  141. inline Quat<T> operator+(const Quat<T>& q, const T a)
  142. {
  143. return Quat<T>(q.w + a, q.x, q.y, q.z);
  144. }
  145. template <typename T>
  146. inline Quat<T> operator-(const T a, const Quat<T>& q)
  147. {
  148. return Quat<T>(a - q.w, -q.x, -q.y, -q.z);
  149. }
  150. template <typename T>
  151. inline Quat<T> operator-(const Quat<T>& q, const T a)
  152. {
  153. return Quat<T>(q.w - a, q.x, q.y, q.z);
  154. }
  155. template <typename T>
  156. inline Quat<T> Quat<T>::operator-(const Quat<T> &q1) const
  157. {
  158. return Quat<T>(w - q1.w, x - q1.x, y - q1.y, z - q1.z);
  159. }
  160. template <typename T>
  161. inline Quat<T>& Quat<T>::operator+=(const Quat<T> &q1)
  162. {
  163. w += q1.w;
  164. x += q1.x;
  165. y += q1.y;
  166. z += q1.z;
  167. return *this;
  168. }
  169. template <typename T>
  170. inline Quat<T>& Quat<T>::operator-=(const Quat<T> &q1)
  171. {
  172. w -= q1.w;
  173. x -= q1.x;
  174. y -= q1.y;
  175. z -= q1.z;
  176. return *this;
  177. }
  178. template <typename T>
  179. inline Quat<T> Quat<T>::operator*(const Quat<T> &q1) const
  180. {
  181. Vec<T, 4> q{w, x, y, z};
  182. Vec<T, 4> q2{q1.w, q1.x, q1.y, q1.z};
  183. return Quat<T>(q * q2);
  184. }
  185. template <typename T>
  186. Quat<T> operator*(const Quat<T> &q1, const T a)
  187. {
  188. return Quat<T>(a * q1.w, a * q1.x, a * q1.y, a * q1.z);
  189. }
  190. template <typename T>
  191. Quat<T> operator*(const T a, const Quat<T> &q1)
  192. {
  193. return Quat<T>(a * q1.w, a * q1.x, a * q1.y, a * q1.z);
  194. }
  195. template <typename T>
  196. inline Quat<T>& Quat<T>::operator*=(const Quat<T> &q1)
  197. {
  198. T qw, qx, qy, qz;
  199. qw = w * q1.w - x * q1.x - y * q1.y - z * q1.z;
  200. qx = x * q1.w + w * q1.x + y * q1.z - z * q1.y;
  201. qy = y * q1.w + w * q1.y + z * q1.x - x * q1.z;
  202. qz = z * q1.w + w * q1.z + x * q1.y - y * q1.x;
  203. w = qw;
  204. x = qx;
  205. y = qy;
  206. z = qz;
  207. return *this;
  208. }
  209. template <typename T>
  210. inline Quat<T>& Quat<T>::operator/=(const Quat<T> &q1)
  211. {
  212. Quat<T> q(*this * q1.inv());
  213. w = q.w;
  214. x = q.x;
  215. y = q.y;
  216. z = q.z;
  217. return *this;
  218. }
  219. template <typename T>
  220. Quat<T>& Quat<T>::operator*=(const T q1)
  221. {
  222. w *= q1;
  223. x *= q1;
  224. y *= q1;
  225. z *= q1;
  226. return *this;
  227. }
  228. template <typename T>
  229. inline Quat<T>& Quat<T>::operator/=(const T a)
  230. {
  231. const T a_inv = 1.0 / a;
  232. w *= a_inv;
  233. x *= a_inv;
  234. y *= a_inv;
  235. z *= a_inv;
  236. return *this;
  237. }
  238. template <typename T>
  239. inline Quat<T> Quat<T>::operator/(const T a) const
  240. {
  241. const T a_inv = T(1.0) / a;
  242. return Quat<T>(w * a_inv, x * a_inv, y * a_inv, z * a_inv);
  243. }
  244. template <typename T>
  245. inline Quat<T> Quat<T>::operator/(const Quat<T> &q) const
  246. {
  247. return *this * q.inv();
  248. }
  249. template <typename T>
  250. inline const T& Quat<T>::operator[](std::size_t n) const
  251. {
  252. switch (n) {
  253. case 0:
  254. return w;
  255. case 1:
  256. return x;
  257. case 2:
  258. return y;
  259. case 3:
  260. return z;
  261. default:
  262. CV_Error(Error::StsOutOfRange, "subscript exceeds the index range");
  263. }
  264. }
  265. template <typename T>
  266. inline T& Quat<T>::operator[](std::size_t n)
  267. {
  268. switch (n) {
  269. case 0:
  270. return w;
  271. case 1:
  272. return x;
  273. case 2:
  274. return y;
  275. case 3:
  276. return z;
  277. default:
  278. CV_Error(Error::StsOutOfRange, "subscript exceeds the index range");
  279. }
  280. }
  281. template <typename T>
  282. std::ostream & operator<<(std::ostream &os, const Quat<T> &q)
  283. {
  284. os << "Quat " << Vec<T, 4>{q.w, q.x, q.y, q.z};
  285. return os;
  286. }
  287. template <typename T>
  288. inline T Quat<T>::at(size_t index) const
  289. {
  290. return (*this)[index];
  291. }
  292. template <typename T>
  293. inline Quat<T> Quat<T>::conjugate() const
  294. {
  295. return Quat<T>(w, -x, -y, -z);
  296. }
  297. template <typename T>
  298. inline T Quat<T>::norm() const
  299. {
  300. return std::sqrt(dot(*this));
  301. }
  302. template <typename T>
  303. Quat<T> exp(const Quat<T> &q)
  304. {
  305. return q.exp();
  306. }
  307. template <typename T>
  308. Quat<T> Quat<T>::exp() const
  309. {
  310. Vec<T, 3> v{x, y, z};
  311. T normV = std::sqrt(v.dot(v));
  312. T k = normV < CV_QUAT_EPS ? 1 : std::sin(normV) / normV;
  313. return std::exp(w) * Quat<T>(std::cos(normV), v[0] * k, v[1] * k, v[2] * k);
  314. }
  315. template <typename T>
  316. Quat<T> log(const Quat<T> &q, QuatAssumeType assumeUnit)
  317. {
  318. return q.log(assumeUnit);
  319. }
  320. template <typename T>
  321. Quat<T> Quat<T>::log(QuatAssumeType assumeUnit) const
  322. {
  323. Vec<T, 3> v{x, y, z};
  324. T vNorm = std::sqrt(v.dot(v));
  325. if (assumeUnit)
  326. {
  327. T k = vNorm < CV_QUAT_EPS ? 1 : std::acos(w) / vNorm;
  328. return Quat<T>(0, v[0] * k, v[1] * k, v[2] * k);
  329. }
  330. T qNorm = norm();
  331. if (qNorm < CV_QUAT_EPS)
  332. {
  333. CV_Error(Error::StsBadArg, "Cannot apply this quaternion to log function: undefined");
  334. }
  335. T k = vNorm < CV_QUAT_EPS ? 1 : std::acos(w / qNorm) / vNorm;
  336. return Quat<T>(std::log(qNorm), v[0] * k, v[1] * k, v[2] *k);
  337. }
  338. template <typename T>
  339. inline Quat<T> power(const Quat<T> &q1, const T alpha, QuatAssumeType assumeUnit)
  340. {
  341. return q1.power(alpha, assumeUnit);
  342. }
  343. template <typename T>
  344. inline Quat<T> Quat<T>::power(const T alpha, QuatAssumeType assumeUnit) const
  345. {
  346. if (x * x + y * y + z * z > CV_QUAT_EPS)
  347. {
  348. T angle = getAngle(assumeUnit);
  349. Vec<T, 3> axis = getAxis(assumeUnit);
  350. if (assumeUnit)
  351. {
  352. return createFromAngleAxis(alpha * angle, axis);
  353. }
  354. return std::pow(norm(), alpha) * createFromAngleAxis(alpha * angle, axis);
  355. }
  356. else
  357. {
  358. return std::pow(norm(), alpha) * Quat<T>(w, x, y, z);
  359. }
  360. }
  361. template <typename T>
  362. inline Quat<T> sqrt(const Quat<T> &q, QuatAssumeType assumeUnit)
  363. {
  364. return q.sqrt(assumeUnit);
  365. }
  366. template <typename T>
  367. inline Quat<T> Quat<T>::sqrt(QuatAssumeType assumeUnit) const
  368. {
  369. return power(0.5, assumeUnit);
  370. }
  371. template <typename T>
  372. inline Quat<T> power(const Quat<T> &p, const Quat<T> &q, QuatAssumeType assumeUnit)
  373. {
  374. return p.power(q, assumeUnit);
  375. }
  376. template <typename T>
  377. inline Quat<T> Quat<T>::power(const Quat<T> &q, QuatAssumeType assumeUnit) const
  378. {
  379. return cv::exp(q * log(assumeUnit));
  380. }
  381. template <typename T>
  382. inline T Quat<T>::dot(Quat<T> q1) const
  383. {
  384. return w * q1.w + x * q1.x + y * q1.y + z * q1.z;
  385. }
  386. template <typename T>
  387. inline Quat<T> crossProduct(const Quat<T> &p, const Quat<T> &q)
  388. {
  389. return p.crossProduct(q);
  390. }
  391. template <typename T>
  392. inline Quat<T> Quat<T>::crossProduct(const Quat<T> &q) const
  393. {
  394. return Quat<T> (0, y * q.z - z * q.y, z * q.x - x * q.z, x * q.y - q.x * y);
  395. }
  396. template <typename T>
  397. inline Quat<T> Quat<T>::normalize() const
  398. {
  399. T normVal = norm();
  400. if (normVal < CV_QUAT_EPS)
  401. {
  402. CV_Error(Error::StsBadArg, "Cannot normalize this quaternion: the norm is too small.");
  403. }
  404. return Quat<T>(w / normVal, x / normVal, y / normVal, z / normVal) ;
  405. }
  406. template <typename T>
  407. inline Quat<T> inv(const Quat<T> &q, QuatAssumeType assumeUnit)
  408. {
  409. return q.inv(assumeUnit);
  410. }
  411. template <typename T>
  412. inline Quat<T> Quat<T>::inv(QuatAssumeType assumeUnit) const
  413. {
  414. if (assumeUnit)
  415. {
  416. return conjugate();
  417. }
  418. T norm2 = dot(*this);
  419. if (norm2 < CV_QUAT_EPS)
  420. {
  421. CV_Error(Error::StsBadArg, "This quaternion do not have inverse quaternion");
  422. }
  423. return conjugate() / norm2;
  424. }
  425. template <typename T>
  426. inline Quat<T> sinh(const Quat<T> &q)
  427. {
  428. return q.sinh();
  429. }
  430. template <typename T>
  431. inline Quat<T> Quat<T>::sinh() const
  432. {
  433. Vec<T, 3> v{x, y ,z};
  434. T vNorm = std::sqrt(v.dot(v));
  435. T k = vNorm < CV_QUAT_EPS ? 1 : std::cosh(w) * std::sin(vNorm) / vNorm;
  436. return Quat<T>(std::sinh(w) * std::cos(vNorm), v[0] * k, v[1] * k, v[2] * k);
  437. }
  438. template <typename T>
  439. inline Quat<T> cosh(const Quat<T> &q)
  440. {
  441. return q.cosh();
  442. }
  443. template <typename T>
  444. inline Quat<T> Quat<T>::cosh() const
  445. {
  446. Vec<T, 3> v{x, y ,z};
  447. T vNorm = std::sqrt(v.dot(v));
  448. T k = vNorm < CV_QUAT_EPS ? 1 : std::sinh(w) * std::sin(vNorm) / vNorm;
  449. return Quat<T>(std::cosh(w) * std::cos(vNorm), v[0] * k, v[1] * k, v[2] * k);
  450. }
  451. template <typename T>
  452. inline Quat<T> tanh(const Quat<T> &q)
  453. {
  454. return q.tanh();
  455. }
  456. template <typename T>
  457. inline Quat<T> Quat<T>::tanh() const
  458. {
  459. return sinh() * cosh().inv();
  460. }
  461. template <typename T>
  462. inline Quat<T> sin(const Quat<T> &q)
  463. {
  464. return q.sin();
  465. }
  466. template <typename T>
  467. inline Quat<T> Quat<T>::sin() const
  468. {
  469. Vec<T, 3> v{x, y ,z};
  470. T vNorm = std::sqrt(v.dot(v));
  471. T k = vNorm < CV_QUAT_EPS ? 1 : std::cos(w) * std::sinh(vNorm) / vNorm;
  472. return Quat<T>(std::sin(w) * std::cosh(vNorm), v[0] * k, v[1] * k, v[2] * k);
  473. }
  474. template <typename T>
  475. inline Quat<T> cos(const Quat<T> &q)
  476. {
  477. return q.cos();
  478. }
  479. template <typename T>
  480. inline Quat<T> Quat<T>::cos() const
  481. {
  482. Vec<T, 3> v{x, y ,z};
  483. T vNorm = std::sqrt(v.dot(v));
  484. T k = vNorm < CV_QUAT_EPS ? 1 : std::sin(w) * std::sinh(vNorm) / vNorm;
  485. return Quat<T>(std::cos(w) * std::cosh(vNorm), -v[0] * k, -v[1] * k, -v[2] * k);
  486. }
  487. template <typename T>
  488. inline Quat<T> tan(const Quat<T> &q)
  489. {
  490. return q.tan();
  491. }
  492. template <typename T>
  493. inline Quat<T> Quat<T>::tan() const
  494. {
  495. return sin() * cos().inv();
  496. }
  497. template <typename T>
  498. inline Quat<T> asinh(const Quat<T> &q)
  499. {
  500. return q.asinh();
  501. }
  502. template <typename T>
  503. inline Quat<T> Quat<T>::asinh() const
  504. {
  505. return cv::log(*this + cv::power(*this * *this + Quat<T>(1, 0, 0, 0), 0.5));
  506. }
  507. template <typename T>
  508. inline Quat<T> acosh(const Quat<T> &q)
  509. {
  510. return q.acosh();
  511. }
  512. template <typename T>
  513. inline Quat<T> Quat<T>::acosh() const
  514. {
  515. return cv::log(*this + cv::power(*this * *this - Quat<T>(1,0,0,0), 0.5));
  516. }
  517. template <typename T>
  518. inline Quat<T> atanh(const Quat<T> &q)
  519. {
  520. return q.atanh();
  521. }
  522. template <typename T>
  523. inline Quat<T> Quat<T>::atanh() const
  524. {
  525. Quat<T> ident(1, 0, 0, 0);
  526. Quat<T> c1 = (ident + *this).log();
  527. Quat<T> c2 = (ident - *this).log();
  528. return 0.5 * (c1 - c2);
  529. }
  530. template <typename T>
  531. inline Quat<T> asin(const Quat<T> &q)
  532. {
  533. return q.asin();
  534. }
  535. template <typename T>
  536. inline Quat<T> Quat<T>::asin() const
  537. {
  538. Quat<T> v(0, x, y, z);
  539. T vNorm = v.norm();
  540. T k = vNorm < CV_QUAT_EPS ? 1 : vNorm;
  541. return -v / k * (*this * v / k).asinh();
  542. }
  543. template <typename T>
  544. inline Quat<T> acos(const Quat<T> &q)
  545. {
  546. return q.acos();
  547. }
  548. template <typename T>
  549. inline Quat<T> Quat<T>::acos() const
  550. {
  551. Quat<T> v(0, x, y, z);
  552. T vNorm = v.norm();
  553. T k = vNorm < CV_QUAT_EPS ? 1 : vNorm;
  554. return -v / k * acosh();
  555. }
  556. template <typename T>
  557. inline Quat<T> atan(const Quat<T> &q)
  558. {
  559. return q.atan();
  560. }
  561. template <typename T>
  562. inline Quat<T> Quat<T>::atan() const
  563. {
  564. Quat<T> v(0, x, y, z);
  565. T vNorm = v.norm();
  566. T k = vNorm < CV_QUAT_EPS ? 1 : vNorm;
  567. return -v / k * (*this * v / k).atanh();
  568. }
  569. template <typename T>
  570. inline T Quat<T>::getAngle(QuatAssumeType assumeUnit) const
  571. {
  572. if (assumeUnit)
  573. {
  574. return 2 * std::acos(w);
  575. }
  576. if (norm() < CV_QUAT_EPS)
  577. {
  578. CV_Error(Error::StsBadArg, "This quaternion does not represent a rotation");
  579. }
  580. return 2 * std::acos(w / norm());
  581. }
  582. template <typename T>
  583. inline Vec<T, 3> Quat<T>::getAxis(QuatAssumeType assumeUnit) const
  584. {
  585. T angle = getAngle(assumeUnit);
  586. const T sin_v = std::sin(angle * 0.5);
  587. if (assumeUnit)
  588. {
  589. return Vec<T, 3>{x, y, z} / sin_v;
  590. }
  591. return Vec<T, 3> {x, y, z} / (norm() * sin_v);
  592. }
  593. template <typename T>
  594. Matx<T, 4, 4> Quat<T>::toRotMat4x4(QuatAssumeType assumeUnit) const
  595. {
  596. T a = w, b = x, c = y, d = z;
  597. if (!assumeUnit)
  598. {
  599. Quat<T> qTemp = normalize();
  600. a = qTemp.w;
  601. b = qTemp.x;
  602. c = qTemp.y;
  603. d = qTemp.z;
  604. }
  605. Matx<T, 4, 4> R{
  606. 1 - 2 * (c * c + d * d), 2 * (b * c - a * d) , 2 * (b * d + a * c) , 0,
  607. 2 * (b * c + a * d) , 1 - 2 * (b * b + d * d), 2 * (c * d - a * b) , 0,
  608. 2 * (b * d - a * c) , 2 * (c * d + a * b) , 1 - 2 * (b * b + c * c), 0,
  609. 0 , 0 , 0 , 1,
  610. };
  611. return R;
  612. }
  613. template <typename T>
  614. Matx<T, 3, 3> Quat<T>::toRotMat3x3(QuatAssumeType assumeUnit) const
  615. {
  616. T a = w, b = x, c = y, d = z;
  617. if (!assumeUnit)
  618. {
  619. Quat<T> qTemp = normalize();
  620. a = qTemp.w;
  621. b = qTemp.x;
  622. c = qTemp.y;
  623. d = qTemp.z;
  624. }
  625. Matx<T, 3, 3> R{
  626. 1 - 2 * (c * c + d * d), 2 * (b * c - a * d) , 2 * (b * d + a * c),
  627. 2 * (b * c + a * d) , 1 - 2 * (b * b + d * d), 2 * (c * d - a * b),
  628. 2 * (b * d - a * c) , 2 * (c * d + a * b) , 1 - 2 * (b * b + c * c)
  629. };
  630. return R;
  631. }
  632. template <typename T>
  633. Vec<T, 3> Quat<T>::toRotVec(QuatAssumeType assumeUnit) const
  634. {
  635. T angle = getAngle(assumeUnit);
  636. Vec<T, 3> axis = getAxis(assumeUnit);
  637. return angle * axis;
  638. }
  639. template <typename T>
  640. Vec<T, 4> Quat<T>::toVec() const
  641. {
  642. return Vec<T, 4>{w, x, y, z};
  643. }
  644. template <typename T>
  645. Quat<T> Quat<T>::lerp(const Quat<T> &q0, const Quat<T> &q1, const T t)
  646. {
  647. return (1 - t) * q0 + t * q1;
  648. }
  649. template <typename T>
  650. Quat<T> Quat<T>::slerp(const Quat<T> &q0, const Quat<T> &q1, const T t, QuatAssumeType assumeUnit, bool directChange)
  651. {
  652. Quat<T> v0(q0);
  653. Quat<T> v1(q1);
  654. if (!assumeUnit)
  655. {
  656. v0 = v0.normalize();
  657. v1 = v1.normalize();
  658. }
  659. T cosTheta = v0.dot(v1);
  660. constexpr T DOT_THRESHOLD = 0.995;
  661. if (std::abs(cosTheta) > DOT_THRESHOLD)
  662. {
  663. return nlerp(v0, v1, t, QUAT_ASSUME_UNIT);
  664. }
  665. if (directChange && cosTheta < 0)
  666. {
  667. v0 = -v0;
  668. cosTheta = -cosTheta;
  669. }
  670. T sinTheta = std::sqrt(1 - cosTheta * cosTheta);
  671. T angle = atan2(sinTheta, cosTheta);
  672. return (std::sin((1 - t) * angle) / (sinTheta) * v0 + std::sin(t * angle) / (sinTheta) * v1).normalize();
  673. }
  674. template <typename T>
  675. inline Quat<T> Quat<T>::nlerp(const Quat<T> &q0, const Quat<T> &q1, const T t, QuatAssumeType assumeUnit)
  676. {
  677. Quat<T> v0(q0), v1(q1);
  678. if (v1.dot(v0) < 0)
  679. {
  680. v0 = -v0;
  681. }
  682. if (assumeUnit)
  683. {
  684. return ((1 - t) * v0 + t * v1).normalize();
  685. }
  686. v0 = v0.normalize();
  687. v1 = v1.normalize();
  688. return ((1 - t) * v0 + t * v1).normalize();
  689. }
  690. template <typename T>
  691. inline bool Quat<T>::isNormal(T eps) const
  692. {
  693. double normVar = norm();
  694. if ((normVar > 1 - eps) && (normVar < 1 + eps))
  695. return true;
  696. return false;
  697. }
  698. template <typename T>
  699. inline void Quat<T>::assertNormal(T eps) const
  700. {
  701. if (!isNormal(eps))
  702. CV_Error(Error::StsBadArg, "Quaternion should be normalized");
  703. }
  704. template <typename T>
  705. inline Quat<T> Quat<T>::squad(const Quat<T> &q0, const Quat<T> &q1,
  706. const Quat<T> &q2, const Quat<T> &q3,
  707. const T t, QuatAssumeType assumeUnit,
  708. bool directChange)
  709. {
  710. Quat<T> v0(q0), v1(q1), v2(q2), v3(q3);
  711. if (!assumeUnit)
  712. {
  713. v0 = v0.normalize();
  714. v1 = v1.normalize();
  715. v2 = v2.normalize();
  716. v3 = v3.normalize();
  717. }
  718. Quat<T> c0 = slerp(v0, v3, t, assumeUnit, directChange);
  719. Quat<T> c1 = slerp(v1, v2, t, assumeUnit, directChange);
  720. return slerp(c0, c1, 2 * t * (1 - t), assumeUnit, directChange);
  721. }
  722. template <typename T>
  723. Quat<T> Quat<T>::interPoint(const Quat<T> &q0, const Quat<T> &q1,
  724. const Quat<T> &q2, QuatAssumeType assumeUnit)
  725. {
  726. Quat<T> v0(q0), v1(q1), v2(q2);
  727. if (!assumeUnit)
  728. {
  729. v0 = v0.normalize();
  730. v1 = v1.normalize();
  731. v2 = v2.normalize();
  732. }
  733. return v1 * cv::exp(-(cv::log(v1.conjugate() * v0, assumeUnit) + (cv::log(v1.conjugate() * v2, assumeUnit))) / 4);
  734. }
  735. template <typename T>
  736. Quat<T> Quat<T>::spline(const Quat<T> &q0, const Quat<T> &q1, const Quat<T> &q2, const Quat<T> &q3, const T t, QuatAssumeType assumeUnit)
  737. {
  738. Quat<T> v0(q0), v1(q1), v2(q2), v3(q3);
  739. if (!assumeUnit)
  740. {
  741. v0 = v0.normalize();
  742. v1 = v1.normalize();
  743. v2 = v2.normalize();
  744. v3 = v3.normalize();
  745. }
  746. T cosTheta;
  747. std::vector<Quat<T>> vec{v0, v1, v2, v3};
  748. for (size_t i = 0; i < 3; ++i)
  749. {
  750. cosTheta = vec[i].dot(vec[i + 1]);
  751. if (cosTheta < 0)
  752. {
  753. vec[i + 1] = -vec[i + 1];
  754. }
  755. }
  756. Quat<T> s1 = interPoint(vec[0], vec[1], vec[2], QUAT_ASSUME_UNIT);
  757. Quat<T> s2 = interPoint(vec[1], vec[2], vec[3], QUAT_ASSUME_UNIT);
  758. return squad(vec[1], s1, s2, vec[2], t, assumeUnit, QUAT_ASSUME_NOT_UNIT);
  759. }
  760. namespace detail {
  761. template <typename T> static
  762. Quat<T> createFromAxisRot(int axis, const T theta)
  763. {
  764. if (axis == 0)
  765. return Quat<T>::createFromXRot(theta);
  766. if (axis == 1)
  767. return Quat<T>::createFromYRot(theta);
  768. if (axis == 2)
  769. return Quat<T>::createFromZRot(theta);
  770. CV_Assert(0);
  771. }
  772. inline bool isIntAngleType(QuatEnum::EulerAnglesType eulerAnglesType)
  773. {
  774. return eulerAnglesType < QuatEnum::EXT_XYZ;
  775. }
  776. inline bool isTaitBryan(QuatEnum::EulerAnglesType eulerAnglesType)
  777. {
  778. return eulerAnglesType/6 == 1 || eulerAnglesType/6 == 3;
  779. }
  780. } // namespace detail
  781. template <typename T>
  782. Quat<T> Quat<T>::createFromYRot(const T theta)
  783. {
  784. return Quat<T>{std::cos(theta * 0.5f), 0, std::sin(theta * 0.5f), 0};
  785. }
  786. template <typename T>
  787. Quat<T> Quat<T>::createFromXRot(const T theta){
  788. return Quat<T>{std::cos(theta * 0.5f), std::sin(theta * 0.5f), 0, 0};
  789. }
  790. template <typename T>
  791. Quat<T> Quat<T>::createFromZRot(const T theta){
  792. return Quat<T>{std::cos(theta * 0.5f), 0, 0, std::sin(theta * 0.5f)};
  793. }
  794. template <typename T>
  795. Quat<T> Quat<T>::createFromEulerAngles(const Vec<T, 3> &angles, QuatEnum::EulerAnglesType eulerAnglesType) {
  796. CV_Assert(eulerAnglesType < QuatEnum::EulerAnglesType::EULER_ANGLES_MAX_VALUE);
  797. static const int rotationAxis[24][3] = {
  798. {0, 1, 2}, ///< Intrinsic rotations with the Euler angles type X-Y-Z
  799. {0, 2, 1}, ///< Intrinsic rotations with the Euler angles type X-Z-Y
  800. {1, 0, 2}, ///< Intrinsic rotations with the Euler angles type Y-X-Z
  801. {1, 2, 0}, ///< Intrinsic rotations with the Euler angles type Y-Z-X
  802. {2, 0, 1}, ///< Intrinsic rotations with the Euler angles type Z-X-Y
  803. {2, 1, 0}, ///< Intrinsic rotations with the Euler angles type Z-Y-X
  804. {0, 1, 0}, ///< Intrinsic rotations with the Euler angles type X-Y-X
  805. {0, 2, 0}, ///< Intrinsic rotations with the Euler angles type X-Z-X
  806. {1, 0, 1}, ///< Intrinsic rotations with the Euler angles type Y-X-Y
  807. {1, 2, 1}, ///< Intrinsic rotations with the Euler angles type Y-Z-Y
  808. {2, 0, 2}, ///< Intrinsic rotations with the Euler angles type Z-X-Z
  809. {2, 1, 2}, ///< Intrinsic rotations with the Euler angles type Z-Y-Z
  810. {0, 1, 2}, ///< Extrinsic rotations with the Euler angles type X-Y-Z
  811. {0, 2, 1}, ///< Extrinsic rotations with the Euler angles type X-Z-Y
  812. {1, 0, 2}, ///< Extrinsic rotations with the Euler angles type Y-X-Z
  813. {1, 2, 0}, ///< Extrinsic rotations with the Euler angles type Y-Z-X
  814. {2, 0, 1}, ///< Extrinsic rotations with the Euler angles type Z-X-Y
  815. {2, 1, 0}, ///< Extrinsic rotations with the Euler angles type Z-Y-X
  816. {0, 1, 0}, ///< Extrinsic rotations with the Euler angles type X-Y-X
  817. {0, 2, 0}, ///< Extrinsic rotations with the Euler angles type X-Z-X
  818. {1, 0, 1}, ///< Extrinsic rotations with the Euler angles type Y-X-Y
  819. {1, 2, 1}, ///< Extrinsic rotations with the Euler angles type Y-Z-Y
  820. {2, 0, 2}, ///< Extrinsic rotations with the Euler angles type Z-X-Z
  821. {2, 1, 2} ///< Extrinsic rotations with the Euler angles type Z-Y-Z
  822. };
  823. Quat<T> q1 = detail::createFromAxisRot(rotationAxis[eulerAnglesType][0], angles(0));
  824. Quat<T> q2 = detail::createFromAxisRot(rotationAxis[eulerAnglesType][1], angles(1));
  825. Quat<T> q3 = detail::createFromAxisRot(rotationAxis[eulerAnglesType][2], angles(2));
  826. if (detail::isIntAngleType(eulerAnglesType))
  827. {
  828. return q1 * q2 * q3;
  829. }
  830. else // (!detail::isIntAngleType<T>(eulerAnglesType))
  831. {
  832. return q3 * q2 * q1;
  833. }
  834. }
  835. template <typename T>
  836. Vec<T, 3> Quat<T>::toEulerAngles(QuatEnum::EulerAnglesType eulerAnglesType){
  837. CV_Assert(eulerAnglesType < QuatEnum::EulerAnglesType::EULER_ANGLES_MAX_VALUE);
  838. Matx33d R = toRotMat3x3();
  839. enum {
  840. C_ZERO,
  841. C_PI,
  842. C_PI_2,
  843. N_CONSTANTS,
  844. R_0_0 = N_CONSTANTS, R_0_1, R_0_2,
  845. R_1_0, R_1_1, R_1_2,
  846. R_2_0, R_2_1, R_2_2
  847. };
  848. static const T constants_[N_CONSTANTS] = {
  849. 0, // C_ZERO
  850. (T)CV_PI, // C_PI
  851. (T)(CV_PI * 0.5) // C_PI_2, -C_PI_2
  852. };
  853. static const int rotationR_[24][12] = {
  854. {+R_0_2, +R_1_0, +R_1_1, C_PI_2, +R_2_1, +R_1_1, -C_PI_2, -R_1_2, +R_2_2, +R_0_2, -R_0_1, +R_0_0}, // INT_XYZ
  855. {+R_0_1, -R_1_2, +R_2_2, -C_PI_2, +R_2_0, +R_2_2, C_PI_2, +R_2_1, +R_1_1, -R_0_1, +R_0_2, +R_0_0}, // INT_XZY
  856. {+R_1_2, -R_0_1, +R_0_0, -C_PI_2, +R_0_1, +R_0_0, C_PI_2, +R_0_2, +R_2_2, -R_1_2, +R_1_0, +R_1_1}, // INT_YXZ
  857. {+R_1_0, +R_0_2, +R_2_2, C_PI_2, +R_0_2, +R_0_1, -C_PI_2, -R_2_0, +R_0_0, +R_1_0, -R_1_2, +R_1_1}, // INT_YZX
  858. {+R_2_1, +R_1_0, +R_0_0, C_PI_2, +R_1_0, +R_0_0, -C_PI_2, -R_0_1, +R_1_1, +R_2_1, -R_2_0, +R_2_2}, // INT_ZXY
  859. {+R_2_0, -R_0_1, +R_1_1, -C_PI_2, +R_1_2, +R_1_1, C_PI_2, +R_1_0, +R_0_0, -R_2_0, +R_2_1, +R_2_2}, // INT_ZYX
  860. {+R_0_0, +R_2_1, +R_2_2, C_ZERO, +R_1_2, +R_1_1, C_PI, +R_1_0, -R_2_0, +R_0_0, +R_0_1, +R_0_2}, // INT_XYX
  861. {+R_0_0, +R_2_1, +R_2_2, C_ZERO, -R_2_1, +R_2_2, C_PI, +R_2_0, +R_1_0, +R_0_0, +R_0_2, -R_0_1}, // INT_XZX
  862. {+R_1_1, +R_0_2, +R_0_0, C_ZERO, -R_2_0, +R_0_0, C_PI, +R_0_1, +R_2_1, +R_1_1, +R_1_0, -R_1_2}, // INT_YXY
  863. {+R_1_1, +R_0_2, +R_0_0, C_ZERO, +R_0_2, -R_0_0, C_PI, +R_2_1, -R_0_1, +R_1_1, +R_1_2, +R_1_0}, // INT_YZY
  864. {+R_2_2, +R_1_0, +R_1_1, C_ZERO, +R_1_0, +R_0_0, C_PI, +R_0_2, -R_1_2, +R_2_2, +R_2_0, +R_2_1}, // INT_ZXZ
  865. {+R_2_2, +R_1_0, +R_0_0, C_ZERO, +R_1_0, +R_0_0, C_PI, +R_1_2, +R_0_2, +R_2_2, +R_2_1, -R_2_0}, // INT_ZYZ
  866. {+R_2_0, -C_PI_2, -R_0_1, +R_1_1, C_PI_2, +R_1_2, +R_1_1, +R_2_1, +R_2_2, -R_2_0, +R_1_0, +R_0_0}, // EXT_XYZ
  867. {+R_1_0, C_PI_2, +R_0_2, +R_2_2, -C_PI_2, +R_0_2, +R_0_1, -R_1_2, +R_1_1, +R_1_0, -R_2_0, +R_0_0}, // EXT_XZY
  868. {+R_2_1, C_PI_2, +R_1_0, +R_0_0, -C_PI_2, +R_1_0, +R_0_0, -R_2_0, +R_2_2, +R_2_1, -R_0_1, +R_1_1}, // EXT_YXZ
  869. {+R_0_2, -C_PI_2, -R_1_2, +R_2_2, C_PI_2, +R_2_0, +R_2_2, +R_0_2, +R_0_0, -R_0_1, +R_2_1, +R_1_1}, // EXT_YZX
  870. {+R_1_2, -C_PI_2, -R_0_1, +R_0_0, C_PI_2, +R_0_1, +R_0_0, +R_1_0, +R_1_1, -R_1_2, +R_0_2, +R_2_2}, // EXT_ZXY
  871. {+R_0_2, C_PI_2, +R_1_0, +R_1_1, -C_PI_2, +R_2_1, +R_1_1, -R_0_1, +R_0_0, +R_0_2, -R_1_2, +R_2_2}, // EXT_ZYX
  872. {+R_0_0, C_ZERO, +R_2_1, +R_2_2, C_PI, +R_1_2, +R_1_1, +R_0_1, +R_0_2, +R_0_0, +R_1_0, -R_2_0}, // EXT_XYX
  873. {+R_0_0, C_ZERO, +R_2_1, +R_2_2, C_PI, +R_2_1, +R_2_2, +R_0_2, -R_0_1, +R_0_0, +R_2_0, +R_1_0}, // EXT_XZX
  874. {+R_1_1, C_ZERO, +R_0_2, +R_0_0, C_PI, -R_2_0, +R_0_0, +R_1_0, -R_1_2, +R_1_1, +R_0_1, +R_2_1}, // EXT_YXY
  875. {+R_1_1, C_ZERO, +R_0_2, +R_0_0, C_PI, +R_0_2, -R_0_0, +R_1_2, +R_1_0, +R_1_1, +R_2_1, -R_0_1}, // EXT_YZY
  876. {+R_2_2, C_ZERO, +R_1_0, +R_1_1, C_PI, +R_1_0, +R_0_0, +R_2_0, +R_2_1, +R_2_2, +R_0_2, -R_1_2}, // EXT_ZXZ
  877. {+R_2_2, C_ZERO, +R_1_0, +R_0_0, C_PI, +R_1_0, +R_0_0, +R_2_1, -R_2_0, +R_2_2, +R_1_2, +R_0_2}, // EXT_ZYZ
  878. };
  879. T rotationR[12];
  880. for (int i = 0; i < 12; i++)
  881. {
  882. int id = rotationR_[eulerAnglesType][i];
  883. unsigned idx = std::abs(id);
  884. T value = 0.0f;
  885. if (idx < N_CONSTANTS)
  886. {
  887. value = constants_[idx];
  888. }
  889. else
  890. {
  891. unsigned r_idx = idx - N_CONSTANTS;
  892. CV_DbgAssert(r_idx < 9);
  893. value = R.val[r_idx];
  894. }
  895. bool isNegative = id < 0;
  896. if (isNegative)
  897. value = -value;
  898. rotationR[i] = value;
  899. }
  900. Vec<T, 3> angles;
  901. if (detail::isIntAngleType(eulerAnglesType))
  902. {
  903. if (abs(rotationR[0] - 1) < CV_QUAT_CONVERT_THRESHOLD)
  904. {
  905. CV_LOG_WARNING(NULL,"Gimbal Lock occurs. Euler angles are non-unique, we set the third angle to 0");
  906. angles = {std::atan2(rotationR[1], rotationR[2]), rotationR[3], 0};
  907. return angles;
  908. }
  909. else if(abs(rotationR[0] + 1) < CV_QUAT_CONVERT_THRESHOLD)
  910. {
  911. CV_LOG_WARNING(NULL,"Gimbal Lock occurs. Euler angles are non-unique, we set the third angle to 0");
  912. angles = {std::atan2(rotationR[4], rotationR[5]), rotationR[6], 0};
  913. return angles;
  914. }
  915. }
  916. else // (!detail::isIntAngleType<T>(eulerAnglesType))
  917. {
  918. if (abs(rotationR[0] - 1) < CV_QUAT_CONVERT_THRESHOLD)
  919. {
  920. CV_LOG_WARNING(NULL,"Gimbal Lock occurs. Euler angles are non-unique, we set the first angle to 0");
  921. angles = {0, rotationR[1], std::atan2(rotationR[2], rotationR[3])};
  922. return angles;
  923. }
  924. else if (abs(rotationR[0] + 1) < CV_QUAT_CONVERT_THRESHOLD)
  925. {
  926. CV_LOG_WARNING(NULL,"Gimbal Lock occurs. Euler angles are non-unique, we set the first angle to 0");
  927. angles = {0, rotationR[4], std::atan2(rotationR[5], rotationR[6])};
  928. return angles;
  929. }
  930. }
  931. angles(0) = std::atan2(rotationR[7], rotationR[8]);
  932. if (detail::isTaitBryan(eulerAnglesType))
  933. angles(1) = std::acos(rotationR[9]);
  934. else
  935. angles(1) = std::asin(rotationR[9]);
  936. angles(2) = std::atan2(rotationR[10], rotationR[11]);
  937. return angles;
  938. }
  939. } // namepsace
  940. //! @endcond
  941. #endif /*OPENCV_CORE_QUATERNION_INL_HPP*/