quaternion.hpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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_HPP
  27. #define OPENCV_CORE_QUATERNION_HPP
  28. #include <opencv2/core.hpp>
  29. #include <opencv2/core/utils/logger.hpp>
  30. #include <iostream>
  31. namespace cv
  32. {
  33. //! @addtogroup core
  34. //! @{
  35. //! Unit quaternion flag
  36. enum QuatAssumeType
  37. {
  38. /**
  39. * This flag is specified by default.
  40. * If this flag is specified, the input quaternions are assumed to be not unit quaternions.
  41. * It can guarantee the correctness of the calculations,
  42. * although the calculation speed will be slower than the flag QUAT_ASSUME_UNIT.
  43. */
  44. QUAT_ASSUME_NOT_UNIT,
  45. /**
  46. * If this flag is specified, the input quaternions are assumed to be unit quaternions which
  47. * will save some computations. However, if this flag is specified without unit quaternion,
  48. * the program correctness of the result will not be guaranteed.
  49. */
  50. QUAT_ASSUME_UNIT
  51. };
  52. class QuatEnum
  53. {
  54. public:
  55. /** @brief Enum of Euler angles type.
  56. *
  57. * Without considering the possibility of using two different convertions for the definition of the rotation axes ,
  58. * there exists twelve possible sequences of rotation axes, divided into two groups:
  59. * - Proper Euler angles (Z-X-Z, X-Y-X, Y-Z-Y, Z-Y-Z, X-Z-X, Y-X-Y)
  60. * - Tait–Bryan angles (X-Y-Z, Y-Z-X, Z-X-Y, X-Z-Y, Z-Y-X, Y-X-Z).
  61. *
  62. * The three elemental rotations may be [extrinsic](https://en.wikipedia.org/wiki/Euler_angles#Definition_by_extrinsic_rotations)
  63. * (rotations about the axes *xyz* of the original coordinate system, which is assumed to remain motionless),
  64. * or [intrinsic](https://en.wikipedia.org/wiki/Euler_angles#Definition_by_intrinsic_rotations)(rotations about the axes of the rotating coordinate system *XYZ*, solidary with the moving body, which changes its orientation after each elemental rotation).
  65. *
  66. *
  67. * Extrinsic and intrinsic rotations are relevant.
  68. *
  69. * The definition of the Euler angles is as following,
  70. * - \f$\theta_1 \f$ represents the first rotation angle,
  71. * - \f$\theta_2 \f$ represents the second rotation angle,
  72. * - \f$\theta_3 \f$ represents the third rotation angle.
  73. *
  74. * For intrinsic rotations in the order of X-Y-Z, the rotation matrix R can be calculated by:\f[R =X(\theta_1) Y(\theta_2) Z(\theta_3) \f]
  75. * For extrinsic rotations in the order of X-Y-Z, the rotation matrix R can be calculated by:\f[R =Z({\theta_3}) Y({\theta_2}) X({\theta_1})\f]
  76. * where
  77. * \f[X({\theta_1})={\begin{bmatrix}1&0&0\\0&\cos {\theta_1} &-\sin {\theta_1} \\0&\sin {\theta_1} &\cos {\theta_1} \\\end{bmatrix}},
  78. * Y({\theta_2})={\begin{bmatrix}\cos \theta_{2}&0&\sin \theta_{2}\\0&1 &0 \\\ -sin \theta_2& 0&\cos \theta_{2} \\\end{bmatrix}},
  79. * Z({\theta_3})={\begin{bmatrix}\cos\theta_{3} &-\sin \theta_3&0\\\sin \theta_3 &\cos \theta_3 &0\\0&0&1\\\end{bmatrix}}.
  80. * \f]
  81. *
  82. * The function is designed according to this set of conventions:
  83. * - [Right handed](https://en.wikipedia.org/wiki/Right_hand_rule) reference frames are adopted, and the [right hand rule](https://en.wikipedia.org/wiki/Right_hand_rule) is used to determine the sign of angles.
  84. * - Each matrix is meant to represent an [active rotation](https://en.wikipedia.org/wiki/Active_and_passive_transformation) (the composing and composed matrices
  85. * are supposed to act on the coordinates of vectors defined in the initial fixed reference frame and give as a result the coordinates of a rotated vector defined in the same reference frame).
  86. * - For \f$\theta_1\f$ and \f$\theta_3\f$, the valid range is (−π, π].
  87. *
  88. * For \f$\theta_2\f$, the valid range is [−π/2, π/2] or [0, π].
  89. *
  90. * For Tait–Bryan angles, the valid range of \f$\theta_2\f$ is [−π/2, π/2]. When transforming a quaternion to Euler angles, the solution of Euler angles is unique in condition of \f$ \theta_2 \in (−π/2, π/2)\f$ .
  91. * If \f$\theta_2 = −π/2 \f$ or \f$ \theta_2 = π/2\f$, there are infinite solutions. The common name for this situation is gimbal lock.
  92. * For Proper Euler angles,the valid range of \f$\theta_2\f$ is in [0, π]. The solutions of Euler angles are unique in condition of \f$ \theta_2 \in (0, π)\f$ . If \f$\theta_2 =0 \f$ or \f$\theta_2 =π \f$,
  93. * there are infinite solutions and gimbal lock will occur.
  94. */
  95. enum EulerAnglesType
  96. {
  97. INT_XYZ, ///< Intrinsic rotations with the Euler angles type X-Y-Z
  98. INT_XZY, ///< Intrinsic rotations with the Euler angles type X-Z-Y
  99. INT_YXZ, ///< Intrinsic rotations with the Euler angles type Y-X-Z
  100. INT_YZX, ///< Intrinsic rotations with the Euler angles type Y-Z-X
  101. INT_ZXY, ///< Intrinsic rotations with the Euler angles type Z-X-Y
  102. INT_ZYX, ///< Intrinsic rotations with the Euler angles type Z-Y-X
  103. INT_XYX, ///< Intrinsic rotations with the Euler angles type X-Y-X
  104. INT_XZX, ///< Intrinsic rotations with the Euler angles type X-Z-X
  105. INT_YXY, ///< Intrinsic rotations with the Euler angles type Y-X-Y
  106. INT_YZY, ///< Intrinsic rotations with the Euler angles type Y-Z-Y
  107. INT_ZXZ, ///< Intrinsic rotations with the Euler angles type Z-X-Z
  108. INT_ZYZ, ///< Intrinsic rotations with the Euler angles type Z-Y-Z
  109. EXT_XYZ, ///< Extrinsic rotations with the Euler angles type X-Y-Z
  110. EXT_XZY, ///< Extrinsic rotations with the Euler angles type X-Z-Y
  111. EXT_YXZ, ///< Extrinsic rotations with the Euler angles type Y-X-Z
  112. EXT_YZX, ///< Extrinsic rotations with the Euler angles type Y-Z-X
  113. EXT_ZXY, ///< Extrinsic rotations with the Euler angles type Z-X-Y
  114. EXT_ZYX, ///< Extrinsic rotations with the Euler angles type Z-Y-X
  115. EXT_XYX, ///< Extrinsic rotations with the Euler angles type X-Y-X
  116. EXT_XZX, ///< Extrinsic rotations with the Euler angles type X-Z-X
  117. EXT_YXY, ///< Extrinsic rotations with the Euler angles type Y-X-Y
  118. EXT_YZY, ///< Extrinsic rotations with the Euler angles type Y-Z-Y
  119. EXT_ZXZ, ///< Extrinsic rotations with the Euler angles type Z-X-Z
  120. EXT_ZYZ, ///< Extrinsic rotations with the Euler angles type Z-Y-Z
  121. #ifndef CV_DOXYGEN
  122. EULER_ANGLES_MAX_VALUE
  123. #endif
  124. };
  125. };
  126. template <typename _Tp> class Quat;
  127. template <typename _Tp> std::ostream& operator<<(std::ostream&, const Quat<_Tp>&);
  128. /**
  129. * Quaternion is a number system that extends the complex numbers. It can be expressed as a
  130. * rotation in three-dimensional space.
  131. * A quaternion is generally represented in the form:
  132. * \f[q = w + x\boldsymbol{i} + y\boldsymbol{j} + z\boldsymbol{k}\f]
  133. * \f[q = [w, x, y, z]\f]
  134. * \f[q = [w, \boldsymbol{v}] \f]
  135. * \f[q = ||q||[\cos\psi, u_x\sin\psi,u_y\sin\psi, u_z\sin\psi].\f]
  136. * \f[q = ||q||[\cos\psi, \boldsymbol{u}\sin\psi]\f]
  137. * where \f$\psi = \frac{\theta}{2}\f$, \f$\theta\f$ represents rotation angle,
  138. * \f$\boldsymbol{u} = [u_x, u_y, u_z]\f$ represents normalized rotation axis,
  139. * and \f$||q||\f$ represents the norm of \f$q\f$.
  140. *
  141. * A unit quaternion is usually represents rotation, which has the form:
  142. * \f[q = [\cos\psi, u_x\sin\psi,u_y\sin\psi, u_z\sin\psi].\f]
  143. *
  144. * To create a quaternion representing the rotation around the axis \f$\boldsymbol{u}\f$
  145. * with angle \f$\theta\f$, you can use
  146. * ```
  147. * using namespace cv;
  148. * double angle = CV_PI;
  149. * Vec3d axis = {0, 0, 1};
  150. * Quatd q = Quatd::createFromAngleAxis(angle, axis);
  151. * ```
  152. *
  153. * You can simply use four same type number to create a quaternion
  154. * ```
  155. * Quatd q(1, 2, 3, 4);
  156. * ```
  157. * Or use a Vec4d or Vec4f vector.
  158. * ```
  159. * Vec4d vec{1, 2, 3, 4};
  160. * Quatd q(vec);
  161. * ```
  162. *
  163. * ```
  164. * Vec4f vec{1, 2, 3, 4};
  165. * Quatf q(vec);
  166. * ```
  167. *
  168. * If you already have a 3x3 rotation matrix R, then you can use
  169. * ```
  170. * Quatd q = Quatd::createFromRotMat(R);
  171. * ```
  172. *
  173. * If you already have a rotation vector rvec which has the form of `angle * axis`, then you can use
  174. * ```
  175. * Quatd q = Quatd::createFromRvec(rvec);
  176. * ```
  177. *
  178. * To extract the rotation matrix from quaternion, see toRotMat3x3()
  179. *
  180. * To extract the Vec4d or Vec4f, see toVec()
  181. *
  182. * To extract the rotation vector, see toRotVec()
  183. *
  184. * If there are two quaternions \f$q_0, q_1\f$ are needed to interpolate, you can use nlerp(), slerp() or spline()
  185. * ```
  186. * Quatd::nlerp(q0, q1, t)
  187. *
  188. * Quatd::slerp(q0, q1, t)
  189. *
  190. * Quatd::spline(q0, q0, q1, q1, t)
  191. * ```
  192. * spline can smoothly connect rotations of multiple quaternions
  193. *
  194. * Three ways to get an element in Quaternion
  195. * ```
  196. * Quatf q(1,2,3,4);
  197. * std::cout << q.w << std::endl; // w=1, x=2, y=3, z=4
  198. * std::cout << q[0] << std::endl; // q[0]=1, q[1]=2, q[2]=3, q[3]=4
  199. * std::cout << q.at(0) << std::endl;
  200. * ```
  201. */
  202. template <typename _Tp>
  203. class Quat
  204. {
  205. static_assert(std::is_floating_point<_Tp>::value, "Quaternion only make sense with type of float or double");
  206. using value_type = _Tp;
  207. public:
  208. static constexpr _Tp CV_QUAT_EPS = (_Tp)1.e-6;
  209. static constexpr _Tp CV_QUAT_CONVERT_THRESHOLD = (_Tp)1.e-6;
  210. Quat();
  211. /**
  212. * @brief From Vec4d or Vec4f.
  213. */
  214. explicit Quat(const Vec<_Tp, 4> &coeff);
  215. /**
  216. * @brief from four numbers.
  217. */
  218. Quat(_Tp w, _Tp x, _Tp y, _Tp z);
  219. /**
  220. * @brief from an angle, axis. Axis will be normalized in this function. And
  221. * it generates
  222. * \f[q = [\cos\psi, u_x\sin\psi,u_y\sin\psi, u_z\sin\psi].\f]
  223. * where \f$\psi = \frac{\theta}{2}\f$, \f$\theta\f$ is the rotation angle.
  224. */
  225. static Quat<_Tp> createFromAngleAxis(const _Tp angle, const Vec<_Tp, 3> &axis);
  226. /**
  227. * @brief from a 3x3 rotation matrix.
  228. */
  229. static Quat<_Tp> createFromRotMat(InputArray R);
  230. /**
  231. * @brief from a rotation vector
  232. * \f$r\f$ has the form \f$\theta \cdot \boldsymbol{u}\f$, where \f$\theta\f$
  233. * represents rotation angle and \f$\boldsymbol{u}\f$ represents normalized rotation axis.
  234. *
  235. * Angle and axis could be easily derived as:
  236. * \f[
  237. * \begin{equation}
  238. * \begin{split}
  239. * \psi &= ||r||\\
  240. * \boldsymbol{u} &= \frac{r}{\theta}
  241. * \end{split}
  242. * \end{equation}
  243. * \f]
  244. * Then a quaternion can be calculated by
  245. * \f[q = [\cos\psi, \boldsymbol{u}\sin\psi]\f]
  246. * where \f$\psi = \theta / 2 \f$
  247. */
  248. static Quat<_Tp> createFromRvec(InputArray rvec);
  249. /**
  250. * @brief
  251. * from Euler angles
  252. *
  253. * A quaternion can be generated from Euler angles by combining the quaternion representations of the Euler rotations.
  254. *
  255. * For example, if we use intrinsic rotations in the order of X-Y-Z,\f$\theta_1 \f$ is rotation around the X-axis, \f$\theta_2 \f$ is rotation around the Y-axis,
  256. * \f$\theta_3 \f$ is rotation around the Z-axis. The final quaternion q can be calculated by
  257. *
  258. * \f[ {q} = q_{X, \theta_1} q_{Y, \theta_2} q_{Z, \theta_3}\f]
  259. * where \f$ q_{X, \theta_1} \f$ is created from @ref createFromXRot, \f$ q_{Y, \theta_2} \f$ is created from @ref createFromYRot,
  260. * \f$ q_{Z, \theta_3} \f$ is created from @ref createFromZRot.
  261. * @param angles the Euler angles in a vector of length 3
  262. * @param eulerAnglesType the convertion Euler angles type
  263. */
  264. static Quat<_Tp> createFromEulerAngles(const Vec<_Tp, 3> &angles, QuatEnum::EulerAnglesType eulerAnglesType);
  265. /**
  266. * @brief get a quaternion from a rotation about the Y-axis by \f$\theta\f$ .
  267. * \f[q = \cos(\theta/2)+0 i+ sin(\theta/2) j +0k \f]
  268. */
  269. static Quat<_Tp> createFromYRot(const _Tp theta);
  270. /**
  271. * @brief get a quaternion from a rotation about the X-axis by \f$\theta\f$ .
  272. * \f[q = \cos(\theta/2)+sin(\theta/2) i +0 j +0 k \f]
  273. */
  274. static Quat<_Tp> createFromXRot(const _Tp theta);
  275. /**
  276. * @brief get a quaternion from a rotation about the Z-axis by \f$\theta\f$.
  277. * \f[q = \cos(\theta/2)+0 i +0 j +sin(\theta/2) k \f]
  278. */
  279. static Quat<_Tp> createFromZRot(const _Tp theta);
  280. /**
  281. * @brief a way to get element.
  282. * @param index over a range [0, 3].
  283. *
  284. * A quaternion q
  285. *
  286. * q.at(0) is equivalent to q.w,
  287. *
  288. * q.at(1) is equivalent to q.x,
  289. *
  290. * q.at(2) is equivalent to q.y,
  291. *
  292. * q.at(3) is equivalent to q.z.
  293. */
  294. _Tp at(size_t index) const;
  295. /**
  296. * @brief return the conjugate of this quaternion.
  297. * \f[q.conjugate() = (w, -x, -y, -z).\f]
  298. */
  299. Quat<_Tp> conjugate() const;
  300. /**
  301. *
  302. * @brief return the value of exponential value.
  303. * \f[\exp(q) = e^w (\cos||\boldsymbol{v}||+ \frac{v}{||\boldsymbol{v}||})\sin||\boldsymbol{v}||\f]
  304. * where \f$\boldsymbol{v} = [x, y, z].\f$
  305. * @param q a quaternion.
  306. *
  307. * For example:
  308. * ```
  309. * Quatd q{1,2,3,4};
  310. * cout << exp(q) << endl;
  311. * ```
  312. */
  313. template <typename T>
  314. friend Quat<T> exp(const Quat<T> &q);
  315. /**
  316. * @brief return the value of exponential value.
  317. * \f[\exp(q) = e^w (\cos||\boldsymbol{v}||+ \frac{v}{||\boldsymbol{v}||}\sin||\boldsymbol{v}||)\f]
  318. * where \f$\boldsymbol{v} = [x, y, z].\f$
  319. *
  320. * For example
  321. * ```
  322. * Quatd q{1,2,3,4};
  323. * cout << q.exp() << endl;
  324. * ```
  325. */
  326. Quat<_Tp> exp() const;
  327. /**
  328. * @brief return the value of logarithm function.
  329. * \f[\ln(q) = \ln||q|| + \frac{\boldsymbol{v}}{||\boldsymbol{v}||}\arccos\frac{w}{||q||}.\f]
  330. * where \f$\boldsymbol{v} = [x, y, z].\f$
  331. * @param q a quaternion.
  332. * @param assumeUnit if QUAT_ASSUME_UNIT, q assume to be a unit quaternion and this function will save some computations.
  333. *
  334. * For example
  335. * ```
  336. * Quatd q1{1,2,3,4};
  337. * cout << log(q1) << endl;
  338. * ```
  339. */
  340. template <typename T>
  341. friend Quat<T> log(const Quat<T> &q, QuatAssumeType assumeUnit);
  342. /**
  343. * @brief return the value of logarithm function.
  344. * \f[\ln(q) = \ln||q|| + \frac{\boldsymbol{v}}{||\boldsymbol{v}||}\arccos\frac{w}{||q||}\f].
  345. * where \f$\boldsymbol{v} = [x, y, z].\f$
  346. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and this function will save some computations.
  347. *
  348. * For example
  349. * ```
  350. * Quatd q(1,2,3,4);
  351. * q.log();
  352. *
  353. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  354. * Quatd q1(1,2,3,4);
  355. * q1.normalize().log(assumeUnit);
  356. * ```
  357. */
  358. Quat<_Tp> log(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  359. /**
  360. * @brief return the value of power function with index \f$x\f$.
  361. * \f[q^x = ||q||(cos(x\theta) + \boldsymbol{u}sin(x\theta))).\f]
  362. * @param q a quaternion.
  363. * @param x index of exponentiation.
  364. * @param assumeUnit if QUAT_ASSUME_UNIT, quaternion q assume to be a unit quaternion and this function will save some computations.
  365. *
  366. * For example
  367. * ```
  368. * Quatd q(1,2,3,4);
  369. * power(q, 2.0);
  370. *
  371. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  372. * double angle = CV_PI;
  373. * Vec3d axis{0, 0, 1};
  374. * Quatd q1 = Quatd::createFromAngleAxis(angle, axis); //generate a unit quat by axis and angle
  375. * power(q1, 2.0, assumeUnit);//This assumeUnit means q1 is a unit quaternion.
  376. * ```
  377. * @note the type of the index should be the same as the quaternion.
  378. */
  379. template <typename T>
  380. friend Quat<T> power(const Quat<T> &q, const T x, QuatAssumeType assumeUnit);
  381. /**
  382. * @brief return the value of power function with index \f$x\f$.
  383. * \f[q^x = ||q||(\cos(x\theta) + \boldsymbol{u}\sin(x\theta))).\f]
  384. * @param x index of exponentiation.
  385. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and this function will save some computations.
  386. *
  387. * For example
  388. * ```
  389. * Quatd q(1,2,3,4);
  390. * q.power(2.0);
  391. *
  392. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  393. * double angle = CV_PI;
  394. * Vec3d axis{0, 0, 1};
  395. * Quatd q1 = Quatd::createFromAngleAxis(angle, axis); //generate a unit quat by axis and angle
  396. * q1.power(2.0, assumeUnit); //This assumeUnt means q1 is a unit quaternion
  397. * ```
  398. */
  399. Quat<_Tp> power(const _Tp x, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  400. /**
  401. * @brief return \f$\sqrt{q}\f$.
  402. * @param q a quaternion.
  403. * @param assumeUnit if QUAT_ASSUME_UNIT, quaternion q assume to be a unit quaternion and this function will save some computations.
  404. *
  405. * For example
  406. * ```
  407. * Quatf q(1,2,3,4);
  408. * sqrt(q);
  409. *
  410. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  411. * q = {1,0,0,0};
  412. * sqrt(q, assumeUnit); //This assumeUnit means q is a unit quaternion.
  413. * ```
  414. */
  415. template <typename T>
  416. friend Quat<T> sqrt(const Quat<T> &q, QuatAssumeType assumeUnit);
  417. /**
  418. * @brief return \f$\sqrt{q}\f$.
  419. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and this function will save some computations.
  420. *
  421. * For example
  422. * ```
  423. * Quatf q(1,2,3,4);
  424. * q.sqrt();
  425. *
  426. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  427. * q = {1,0,0,0};
  428. * q.sqrt(assumeUnit); //This assumeUnit means q is a unit quaternion
  429. * ```
  430. */
  431. Quat<_Tp> sqrt(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  432. /**
  433. * @brief return the value of power function with quaternion \f$q\f$.
  434. * \f[p^q = e^{q\ln(p)}.\f]
  435. * @param p base quaternion of power function.
  436. * @param q index quaternion of power function.
  437. * @param assumeUnit if QUAT_ASSUME_UNIT, quaternion \f$p\f$ assume to be a unit quaternion and this function will save some computations.
  438. *
  439. * For example
  440. * ```
  441. * Quatd p(1,2,3,4);
  442. * Quatd q(5,6,7,8);
  443. * power(p, q);
  444. *
  445. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  446. * p = p.normalize();
  447. * power(p, q, assumeUnit); //This assumeUnit means p is a unit quaternion
  448. * ```
  449. */
  450. template <typename T>
  451. friend Quat<T> power(const Quat<T> &p, const Quat<T> &q, QuatAssumeType assumeUnit);
  452. /**
  453. * @brief return the value of power function with quaternion \f$q\f$.
  454. * \f[p^q = e^{q\ln(p)}.\f]
  455. * @param q index quaternion of power function.
  456. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and this function will save some computations.
  457. *
  458. * For example
  459. * ```
  460. * Quatd p(1,2,3,4);
  461. * Quatd q(5,6,7,8);
  462. * p.power(q);
  463. *
  464. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  465. * p = p.normalize();
  466. * p.power(q, assumeUnit); //This assumeUnit means p is a unit quaternion
  467. * ```
  468. */
  469. Quat<_Tp> power(const Quat<_Tp> &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  470. /**
  471. * @brief return the crossProduct between \f$p = (a, b, c, d) = (a, \boldsymbol{u})\f$ and \f$q = (w, x, y, z) = (w, \boldsymbol{v})\f$.
  472. * \f[p \times q = \frac{pq- qp}{2}\f]
  473. * \f[p \times q = \boldsymbol{u} \times \boldsymbol{v}\f]
  474. * \f[p \times q = (cz-dy)i + (dx-bz)j + (by-xc)k \f]
  475. *
  476. * For example
  477. * ```
  478. * Quatd q{1,2,3,4};
  479. * Quatd p{5,6,7,8};
  480. * crossProduct(p, q);
  481. * ```
  482. */
  483. template <typename T>
  484. friend Quat<T> crossProduct(const Quat<T> &p, const Quat<T> &q);
  485. /**
  486. * @brief return the crossProduct between \f$p = (a, b, c, d) = (a, \boldsymbol{u})\f$ and \f$q = (w, x, y, z) = (w, \boldsymbol{v})\f$.
  487. * \f[p \times q = \frac{pq- qp}{2}.\f]
  488. * \f[p \times q = \boldsymbol{u} \times \boldsymbol{v}.\f]
  489. * \f[p \times q = (cz-dy)i + (dx-bz)j + (by-xc)k. \f]
  490. *
  491. * For example
  492. * ```
  493. * Quatd q{1,2,3,4};
  494. * Quatd p{5,6,7,8};
  495. * p.crossProduct(q)
  496. * ```
  497. */
  498. Quat<_Tp> crossProduct(const Quat<_Tp> &q) const;
  499. /**
  500. * @brief return the norm of quaternion.
  501. * \f[||q|| = \sqrt{w^2 + x^2 + y^2 + z^2}.\f]
  502. */
  503. _Tp norm() const;
  504. /**
  505. * @brief return a normalized \f$p\f$.
  506. * \f[p = \frac{q}{||q||}\f]
  507. * where \f$p\f$ satisfies \f$(p.x)^2 + (p.y)^2 + (p.z)^2 + (p.w)^2 = 1.\f$
  508. */
  509. Quat<_Tp> normalize() const;
  510. /**
  511. * @brief return \f$q^{-1}\f$ which is an inverse of \f$q\f$
  512. * which satisfies \f$q * q^{-1} = 1\f$.
  513. * @param q a quaternion.
  514. * @param assumeUnit if QUAT_ASSUME_UNIT, quaternion q assume to be a unit quaternion and this function will save some computations.
  515. *
  516. * For example
  517. * ```
  518. * Quatd q(1,2,3,4);
  519. * inv(q);
  520. *
  521. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  522. * q = q.normalize();
  523. * inv(q, assumeUnit);//This assumeUnit means p is a unit quaternion
  524. * ```
  525. */
  526. template <typename T>
  527. friend Quat<T> inv(const Quat<T> &q, QuatAssumeType assumeUnit);
  528. /**
  529. * @brief return \f$q^{-1}\f$ which is an inverse of \f$q\f$
  530. * satisfying \f$q * q^{-1} = 1\f$.
  531. * @param assumeUnit if QUAT_ASSUME_UNIT, quaternion q assume to be a unit quaternion and this function will save some computations.
  532. *
  533. * For example
  534. * ```
  535. * Quatd q(1,2,3,4);
  536. * q.inv();
  537. *
  538. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  539. * q = q.normalize();
  540. * q.inv(assumeUnit); //assumeUnit means p is a unit quaternion
  541. * ```
  542. */
  543. Quat<_Tp> inv(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  544. /**
  545. * @brief return sinh value of quaternion q, sinh could be calculated as:
  546. * \f[\sinh(p) = \sin(w)\cos(||\boldsymbol{v}||) + \cosh(w)\frac{v}{||\boldsymbol{v}||}\sin||\boldsymbol{v}||\f]
  547. * where \f$\boldsymbol{v} = [x, y, z].\f$
  548. * @param q a quaternion.
  549. *
  550. * For example
  551. * ```
  552. * Quatd q(1,2,3,4);
  553. * sinh(q);
  554. * ```
  555. */
  556. template <typename T>
  557. friend Quat<T> sinh(const Quat<T> &q);
  558. /**
  559. * @brief return sinh value of this quaternion, sinh could be calculated as:
  560. * \f$\sinh(p) = \sin(w)\cos(||\boldsymbol{v}||) + \cosh(w)\frac{v}{||\boldsymbol{v}||}\sin||\boldsymbol{v}||\f$
  561. * where \f$\boldsymbol{v} = [x, y, z].\f$
  562. *
  563. * For example
  564. * ```
  565. * Quatd q(1,2,3,4);
  566. * q.sinh();
  567. * ```
  568. */
  569. Quat<_Tp> sinh() const;
  570. /**
  571. * @brief return cosh value of quaternion q, cosh could be calculated as:
  572. * \f[\cosh(p) = \cosh(w) * \cos(||\boldsymbol{v}||) + \sinh(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}\sin(||\boldsymbol{v}||)\f]
  573. * where \f$\boldsymbol{v} = [x, y, z].\f$
  574. * @param q a quaternion.
  575. *
  576. * For example
  577. * ```
  578. * Quatd q(1,2,3,4);
  579. * cosh(q);
  580. * ```
  581. */
  582. template <typename T>
  583. friend Quat<T> cosh(const Quat<T> &q);
  584. /**
  585. * @brief return cosh value of this quaternion, cosh could be calculated as:
  586. * \f[\cosh(p) = \cosh(w) * \cos(||\boldsymbol{v}||) + \sinh(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}sin(||\boldsymbol{v}||)\f]
  587. * where \f$\boldsymbol{v} = [x, y, z].\f$
  588. *
  589. * For example
  590. * ```
  591. * Quatd q(1,2,3,4);
  592. * q.cosh();
  593. * ```
  594. */
  595. Quat<_Tp> cosh() const;
  596. /**
  597. * @brief return tanh value of quaternion q, tanh could be calculated as:
  598. * \f[ \tanh(q) = \frac{\sinh(q)}{\cosh(q)}.\f]
  599. * @param q a quaternion.
  600. *
  601. * For example
  602. * ```
  603. * Quatd q(1,2,3,4);
  604. * tanh(q);
  605. * ```
  606. * @sa sinh, cosh
  607. */
  608. template <typename T>
  609. friend Quat<T> tanh(const Quat<T> &q);
  610. /**
  611. * @brief return tanh value of this quaternion, tanh could be calculated as:
  612. * \f[ \tanh(q) = \frac{\sinh(q)}{\cosh(q)}.\f]
  613. *
  614. * For example
  615. * ```
  616. * Quatd q(1,2,3,4);
  617. * q.tanh();
  618. * ```
  619. * @sa sinh, cosh
  620. */
  621. Quat<_Tp> tanh() const;
  622. /**
  623. * @brief return tanh value of quaternion q, sin could be calculated as:
  624. * \f[\sin(p) = \sin(w) * \cosh(||\boldsymbol{v}||) + \cos(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}\sinh(||\boldsymbol{v}||)\f]
  625. * where \f$\boldsymbol{v} = [x, y, z].\f$
  626. * @param q a quaternion.
  627. *
  628. * For example
  629. * ```
  630. * Quatd q(1,2,3,4);
  631. * sin(q);
  632. * ```
  633. */
  634. template <typename T>
  635. friend Quat<T> sin(const Quat<T> &q);
  636. /**
  637. * @brief return sin value of this quaternion, sin could be calculated as:
  638. * \f[\sin(p) = \sin(w) * \cosh(||\boldsymbol{v}||) + \cos(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}\sinh(||\boldsymbol{v}||)\f]
  639. * where \f$\boldsymbol{v} = [x, y, z].\f$
  640. *
  641. * For example
  642. * ```
  643. * Quatd q(1,2,3,4);
  644. * q.sin();
  645. * ```
  646. */
  647. Quat<_Tp> sin() const;
  648. /**
  649. * @brief return sin value of quaternion q, cos could be calculated as:
  650. * \f[\cos(p) = \cos(w) * \cosh(||\boldsymbol{v}||) - \sin(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}\sinh(||\boldsymbol{v}||)\f]
  651. * where \f$\boldsymbol{v} = [x, y, z].\f$
  652. * @param q a quaternion.
  653. *
  654. * For example
  655. * ```
  656. * Quatd q(1,2,3,4);
  657. * cos(q);
  658. * ```
  659. */
  660. template <typename T>
  661. friend Quat<T> cos(const Quat<T> &q);
  662. /**
  663. * @brief return cos value of this quaternion, cos could be calculated as:
  664. * \f[\cos(p) = \cos(w) * \cosh(||\boldsymbol{v}||) - \sin(w)\frac{\boldsymbol{v}}{||\boldsymbol{v}||}\sinh(||\boldsymbol{v}||)\f]
  665. * where \f$\boldsymbol{v} = [x, y, z].\f$
  666. *
  667. * For example
  668. * ```
  669. * Quatd q(1,2,3,4);
  670. * q.cos();
  671. * ```
  672. */
  673. Quat<_Tp> cos() const;
  674. /**
  675. * @brief return tan value of quaternion q, tan could be calculated as:
  676. * \f[\tan(q) = \frac{\sin(q)}{\cos(q)}.\f]
  677. * @param q a quaternion.
  678. *
  679. * For example
  680. * ```
  681. * Quatd q(1,2,3,4);
  682. * tan(q);
  683. * ```
  684. */
  685. template <typename T>
  686. friend Quat<T> tan(const Quat<T> &q);
  687. /**
  688. * @brief return tan value of this quaternion, tan could be calculated as:
  689. * \f[\tan(q) = \frac{\sin(q)}{\cos(q)}.\f]
  690. *
  691. * For example
  692. * ```
  693. * Quatd q(1,2,3,4);
  694. * q.tan();
  695. * ```
  696. */
  697. Quat<_Tp> tan() const;
  698. /**
  699. * @brief return arcsin value of quaternion q, arcsin could be calculated as:
  700. * \f[\arcsin(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arcsinh(q\frac{\boldsymbol{v}}{||\boldsymbol{v}||})\f]
  701. * where \f$\boldsymbol{v} = [x, y, z].\f$
  702. * @param q a quaternion.
  703. *
  704. * For example
  705. * ```
  706. * Quatd q(1,2,3,4);
  707. * asin(q);
  708. * ```
  709. */
  710. template <typename T>
  711. friend Quat<T> asin(const Quat<T> &q);
  712. /**
  713. * @brief return arcsin value of this quaternion, arcsin could be calculated as:
  714. * \f[\arcsin(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arcsinh(q\frac{\boldsymbol{v}}{||\boldsymbol{v}||})\f]
  715. * where \f$\boldsymbol{v} = [x, y, z].\f$
  716. *
  717. * For example
  718. * ```
  719. * Quatd q(1,2,3,4);
  720. * q.asin();
  721. * ```
  722. */
  723. Quat<_Tp> asin() const;
  724. /**
  725. * @brief return arccos value of quaternion q, arccos could be calculated as:
  726. * \f[\arccos(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arccosh(q)\f]
  727. * where \f$\boldsymbol{v} = [x, y, z].\f$
  728. * @param q a quaternion.
  729. *
  730. * For example
  731. * ```
  732. * Quatd q(1,2,3,4);
  733. * acos(q);
  734. * ```
  735. */
  736. template <typename T>
  737. friend Quat<T> acos(const Quat<T> &q);
  738. /**
  739. * @brief return arccos value of this quaternion, arccos could be calculated as:
  740. * \f[\arccos(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arccosh(q)\f]
  741. * where \f$\boldsymbol{v} = [x, y, z].\f$
  742. *
  743. * For example
  744. * ```
  745. * Quatd q(1,2,3,4);
  746. * q.acos();
  747. * ```
  748. */
  749. Quat<_Tp> acos() const;
  750. /**
  751. * @brief return arctan value of quaternion q, arctan could be calculated as:
  752. * \f[\arctan(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arctanh(q\frac{\boldsymbol{v}}{||\boldsymbol{v}||})\f]
  753. * where \f$\boldsymbol{v} = [x, y, z].\f$
  754. * @param q a quaternion.
  755. *
  756. * For example
  757. * ```
  758. * Quatd q(1,2,3,4);
  759. * atan(q);
  760. * ```
  761. */
  762. template <typename T>
  763. friend Quat<T> atan(const Quat<T> &q);
  764. /**
  765. * @brief return arctan value of this quaternion, arctan could be calculated as:
  766. * \f[\arctan(q) = -\frac{\boldsymbol{v}}{||\boldsymbol{v}||}arctanh(q\frac{\boldsymbol{v}}{||\boldsymbol{v}||})\f]
  767. * where \f$\boldsymbol{v} = [x, y, z].\f$
  768. *
  769. * For example
  770. * ```
  771. * Quatd q(1,2,3,4);
  772. * q.atan();
  773. * ```
  774. */
  775. Quat<_Tp> atan() const;
  776. /**
  777. * @brief return arcsinh value of quaternion q, arcsinh could be calculated as:
  778. * \f[arcsinh(q) = \ln(q + \sqrt{q^2 + 1})\f].
  779. * @param q a quaternion.
  780. *
  781. * For example
  782. * ```
  783. * Quatd q(1,2,3,4);
  784. * asinh(q);
  785. * ```
  786. */
  787. template <typename T>
  788. friend Quat<T> asinh(const Quat<T> &q);
  789. /**
  790. * @brief return arcsinh value of this quaternion, arcsinh could be calculated as:
  791. * \f[arcsinh(q) = \ln(q + \sqrt{q^2 + 1})\f].
  792. *
  793. * For example
  794. * ```
  795. * Quatd q(1,2,3,4);
  796. * q.asinh();
  797. * ```
  798. */
  799. Quat<_Tp> asinh() const;
  800. /**
  801. * @brief return arccosh value of quaternion q, arccosh could be calculated as:
  802. * \f[arccosh(q) = \ln(q + \sqrt{q^2 - 1})\f].
  803. * @param q a quaternion.
  804. *
  805. * For example
  806. * ```
  807. * Quatd q(1,2,3,4);
  808. * acosh(q);
  809. * ```
  810. */
  811. template <typename T>
  812. friend Quat<T> acosh(const Quat<T> &q);
  813. /**
  814. * @brief return arccosh value of this quaternion, arccosh could be calculated as:
  815. * \f[arcosh(q) = \ln(q + \sqrt{q^2 - 1})\f].
  816. *
  817. * For example
  818. * ```
  819. * Quatd q(1,2,3,4);
  820. * q.acosh();
  821. * ```
  822. */
  823. Quat<_Tp> acosh() const;
  824. /**
  825. * @brief return arctanh value of quaternion q, arctanh could be calculated as:
  826. * \f[arctanh(q) = \frac{\ln(q + 1) - \ln(1 - q)}{2}\f].
  827. * @param q a quaternion.
  828. *
  829. * For example
  830. * ```
  831. * Quatd q(1,2,3,4);
  832. * atanh(q);
  833. * ```
  834. */
  835. template <typename T>
  836. friend Quat<T> atanh(const Quat<T> &q);
  837. /**
  838. * @brief return arctanh value of this quaternion, arctanh could be calculated as:
  839. * \f[arcsinh(q) = \frac{\ln(q + 1) - \ln(1 - q)}{2}\f].
  840. *
  841. * For example
  842. * ```
  843. * Quatd q(1,2,3,4);
  844. * q.atanh();
  845. * ```
  846. */
  847. Quat<_Tp> atanh() const;
  848. /**
  849. * @brief return true if this quaternion is a unit quaternion.
  850. * @param eps tolerance scope of normalization. The eps could be defined as
  851. *
  852. * \f[eps = |1 - dotValue|\f] where \f[dotValue = (this.w^2 + this.x^2 + this,y^2 + this.z^2).\f]
  853. * And this function will consider it is normalized when the dotValue over a range \f$[1-eps, 1+eps]\f$.
  854. */
  855. bool isNormal(_Tp eps=CV_QUAT_EPS) const;
  856. /**
  857. * @brief to throw an error if this quaternion is not a unit quaternion.
  858. * @param eps tolerance scope of normalization.
  859. * @sa isNormal
  860. */
  861. void assertNormal(_Tp eps=CV_QUAT_EPS) const;
  862. /**
  863. * @brief transform a quaternion to a 3x3 rotation matrix.
  864. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and
  865. * this function will save some computations. Otherwise, this function will normalize this
  866. * quaternion at first then do the transformation.
  867. *
  868. * @note Matrix A which is to be rotated should have the form
  869. * \f[\begin{bmatrix}
  870. * x_0& x_1& x_2&...&x_n\\
  871. * y_0& y_1& y_2&...&y_n\\
  872. * z_0& z_1& z_2&...&z_n
  873. * \end{bmatrix}\f]
  874. * where the same subscript represents a point. The shape of A assume to be [3, n]
  875. * The points matrix A can be rotated by toRotMat3x3() * A.
  876. * The result has 3 rows and n columns too.
  877. * For example
  878. * ```
  879. * double angle = CV_PI;
  880. * Vec3d axis{0,0,1};
  881. * Quatd q_unit = Quatd::createFromAngleAxis(angle, axis); //quaternion could also be get by interpolation by two or more quaternions.
  882. *
  883. * //assume there is two points (1,0,0) and (1,0,1) to be rotated
  884. * Mat pointsA = (Mat_<double>(2, 3) << 1,0,0,1,0,1);
  885. * //change the shape
  886. * pointsA = pointsA.t();
  887. * // rotate 180 degrees around the z axis
  888. * Mat new_point = q_unit.toRotMat3x3() * pointsA;
  889. * // print two points
  890. * cout << new_point << endl;
  891. * ```
  892. */
  893. Matx<_Tp, 3, 3> toRotMat3x3(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  894. /**
  895. * @brief transform a quaternion to a 4x4 rotation matrix.
  896. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and
  897. * this function will save some computations. Otherwise, this function will normalize this
  898. * quaternion at first then do the transformation.
  899. *
  900. * The operations is similar as toRotMat3x3
  901. * except that the points matrix should have the form
  902. * \f[\begin{bmatrix}
  903. * x_0& x_1& x_2&...&x_n\\
  904. * y_0& y_1& y_2&...&y_n\\
  905. * z_0& z_1& z_2&...&z_n\\
  906. * 0&0&0&...&0
  907. * \end{bmatrix}\f]
  908. *
  909. * @sa toRotMat3x3
  910. */
  911. Matx<_Tp, 4, 4> toRotMat4x4(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  912. /**
  913. * @brief transform the this quaternion to a Vec<T, 4>.
  914. *
  915. * For example
  916. * ```
  917. * Quatd q(1,2,3,4);
  918. * q.toVec();
  919. * ```
  920. */
  921. Vec<_Tp, 4> toVec() const;
  922. /**
  923. * @brief transform this quaternion to a Rotation vector.
  924. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and
  925. * this function will save some computations.
  926. * Rotation vector rVec is defined as:
  927. * \f[ rVec = [\theta v_x, \theta v_y, \theta v_z]\f]
  928. * where \f$\theta\f$ represents rotation angle, and \f$\boldsymbol{v}\f$ represents the normalized rotation axis.
  929. *
  930. * For example
  931. * ```
  932. * Quatd q(1,2,3,4);
  933. * q.toRotVec();
  934. *
  935. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  936. * q.normalize().toRotVec(assumeUnit); //answer is same as q.toRotVec().
  937. * ```
  938. */
  939. Vec<_Tp, 3> toRotVec(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  940. /**
  941. * @brief get the angle of quaternion, it returns the rotation angle.
  942. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and
  943. * this function will save some computations.
  944. * \f[\psi = 2 *arccos(\frac{w}{||q||})\f]
  945. *
  946. * For example
  947. * ```
  948. * Quatd q(1,2,3,4);
  949. * q.getAngle();
  950. *
  951. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  952. * q.normalize().getAngle(assumeUnit);//same as q.getAngle().
  953. * ```
  954. * @note It always return the value between \f$[0, 2\pi]\f$.
  955. */
  956. _Tp getAngle(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  957. /**
  958. * @brief get the axis of quaternion, it returns a vector of length 3.
  959. * @param assumeUnit if QUAT_ASSUME_UNIT, this quaternion assume to be a unit quaternion and
  960. * this function will save some computations.
  961. *
  962. * the unit axis \f$\boldsymbol{u}\f$ is defined by
  963. * \f[\begin{equation}
  964. * \begin{split}
  965. * \boldsymbol{v}
  966. * &= \boldsymbol{u} ||\boldsymbol{v}||\\
  967. * &= \boldsymbol{u}||q||sin(\frac{\theta}{2})
  968. * \end{split}
  969. * \end{equation}\f]
  970. * where \f$v=[x, y ,z]\f$ and \f$\theta\f$ represents rotation angle.
  971. *
  972. *
  973. * For example
  974. * ```
  975. * Quatd q(1,2,3,4);
  976. * q.getAxis();
  977. *
  978. * QuatAssumeType assumeUnit = QUAT_ASSUME_UNIT;
  979. * q.normalize().getAxis(assumeUnit);//same as q.getAxis()
  980. * ```
  981. */
  982. Vec<_Tp, 3> getAxis(QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT) const;
  983. /**
  984. * @brief return the dot between quaternion \f$q\f$ and this quaternion.
  985. *
  986. * dot(p, q) is a good metric of how close the quaternions are.
  987. * Indeed, consider the unit quaternion difference \f$p^{-1} * q\f$, its real part is dot(p, q).
  988. * At the same time its real part is equal to \f$\cos(\beta/2)\f$ where \f$\beta\f$ is
  989. * an angle of rotation between p and q, i.e.,
  990. * Therefore, the closer dot(p, q) to 1,
  991. * the smaller rotation between them.
  992. * \f[p \cdot q = p.w \cdot q.w + p.x \cdot q.x + p.y \cdot q.y + p.z \cdot q.z\f]
  993. * @param q the other quaternion.
  994. *
  995. * For example
  996. * ```
  997. * Quatd q(1,2,3,4);
  998. * Quatd p(5,6,7,8);
  999. * p.dot(q);
  1000. * ```
  1001. */
  1002. _Tp dot(Quat<_Tp> q) const;
  1003. /**
  1004. * @brief To calculate the interpolation from \f$q_0\f$ to \f$q_1\f$ by Linear Interpolation(Nlerp)
  1005. * For two quaternions, this interpolation curve can be displayed as:
  1006. * \f[Lerp(q_0, q_1, t) = (1 - t)q_0 + tq_1.\f]
  1007. * Obviously, the lerp will interpolate along a straight line if we think of \f$q_0\f$ and \f$q_1\f$ as a vector
  1008. * in a two-dimensional space. When \f$t = 0\f$, it returns \f$q_0\f$ and when \f$t= 1\f$, it returns \f$q_1\f$.
  1009. * \f$t\f$ should to be ranged in \f$[0, 1]\f$ normally.
  1010. * @param q0 a quaternion used in linear interpolation.
  1011. * @param q1 a quaternion used in linear interpolation.
  1012. * @param t percent of vector \f$\overrightarrow{q_0q_1}\f$ over a range [0, 1].
  1013. * @note it returns a non-unit quaternion.
  1014. */
  1015. static Quat<_Tp> lerp(const Quat<_Tp> &q0, const Quat &q1, const _Tp t);
  1016. /**
  1017. * @brief To calculate the interpolation from \f$q_0\f$ to \f$q_1\f$ by Normalized Linear Interpolation(Nlerp).
  1018. * it returns a normalized quaternion of Linear Interpolation(Lerp).
  1019. * \f[ Nlerp(q_0, q_1, t) = \frac{(1 - t)q_0 + tq_1}{||(1 - t)q_0 + tq_1||}.\f]
  1020. * The interpolation will always choose the shortest path but the constant speed is not guaranteed.
  1021. * @param q0 a quaternion used in normalized linear interpolation.
  1022. * @param q1 a quaternion used in normalized linear interpolation.
  1023. * @param t percent of vector \f$\overrightarrow{q_0q_1}\f$ over a range [0, 1].
  1024. * @param assumeUnit if QUAT_ASSUME_UNIT, all input quaternions assume to be unit quaternion. Otherwise, all inputs
  1025. quaternion will be normalized inside the function.
  1026. * @sa lerp
  1027. */
  1028. static Quat<_Tp> nlerp(const Quat<_Tp> &q0, const Quat &q1, const _Tp t, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1029. /**
  1030. @brief To calculate the interpolation between \f$q_0\f$ and \f$q_1\f$ by Spherical Linear
  1031. Interpolation(Slerp), which can be defined as:
  1032. \f[ Slerp(q_0, q_1, t) = \frac{\sin((1-t)\theta)}{\sin(\theta)}q_0 + \frac{\sin(t\theta)}{\sin(\theta)}q_1\f]
  1033. where \f$\theta\f$ can be calculated as:
  1034. \f[\theta=cos^{-1}(q_0\cdot q_1)\f]
  1035. resulting from the both of their norm is unit.
  1036. @param q0 a quaternion used in Slerp.
  1037. @param q1 a quaternion used in Slerp.
  1038. @param t percent of angle between \f$q_0\f$ and \f$q_1\f$ over a range [0, 1].
  1039. @param assumeUnit if QUAT_ASSUME_UNIT, all input quaternions assume to be unit quaternions. Otherwise, all input
  1040. quaternions will be normalized inside the function.
  1041. @param directChange if QUAT_ASSUME_UNIT, the interpolation will choose the nearest path.
  1042. @note If the interpolation angle is small, the error between Nlerp and Slerp is not so large. To improve efficiency and
  1043. avoid zero division error, we use Nlerp instead of Slerp.
  1044. */
  1045. static Quat<_Tp> slerp(const Quat<_Tp> &q0, const Quat &q1, const _Tp t, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT, bool directChange=true);
  1046. /**
  1047. * @brief To calculate the interpolation between \f$q_0\f$,\f$q_1\f$,\f$q_2\f$,\f$q_3\f$ by Spherical and quadrangle(Squad). This could be defined as:
  1048. * \f[Squad(q_i, s_i, s_{i+1}, q_{i+1}, t) = Slerp(Slerp(q_i, q_{i+1}, t), Slerp(s_i, s_{i+1}, t), 2t(1-t))\f]
  1049. * where
  1050. * \f[s_i = q_i\exp(-\frac{\log(q^*_iq_{i+1}) + \log(q^*_iq_{i-1})}{4})\f]
  1051. *
  1052. * The Squad expression is analogous to the \f$B\acute{e}zier\f$ curve, but involves spherical linear
  1053. * interpolation instead of simple linear interpolation. Each \f$s_i\f$ needs to be calculated by three
  1054. * quaternions.
  1055. *
  1056. * @param q0 the first quaternion.
  1057. * @param s0 the second quaternion.
  1058. * @param s1 the third quaternion.
  1059. * @param q1 thr fourth quaternion.
  1060. * @param t interpolation parameter of quadratic and linear interpolation over a range \f$[0, 1]\f$.
  1061. * @param assumeUnit if QUAT_ASSUME_UNIT, all input quaternions assume to be unit quaternion. Otherwise, all input
  1062. * quaternions will be normalized inside the function.
  1063. * @param directChange if QUAT_ASSUME_UNIT, squad will find the nearest path to interpolate.
  1064. * @sa interPoint, spline
  1065. */
  1066. static Quat<_Tp> squad(const Quat<_Tp> &q0, const Quat<_Tp> &s0,
  1067. const Quat<_Tp> &s1, const Quat<_Tp> &q1,
  1068. const _Tp t, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT,
  1069. bool directChange=true);
  1070. /**
  1071. * @brief This is the part calculation of squad.
  1072. * To calculate the intermedia quaternion \f$s_i\f$ between each three quaternion
  1073. * \f[s_i = q_i\exp(-\frac{\log(q^*_iq_{i+1}) + \log(q^*_iq_{i-1})}{4}).\f]
  1074. * @param q0 the first quaternion.
  1075. * @param q1 the second quaternion.
  1076. * @param q2 the third quaternion.
  1077. * @param assumeUnit if QUAT_ASSUME_UNIT, all input quaternions assume to be unit quaternion. Otherwise, all input
  1078. * quaternions will be normalized inside the function.
  1079. * @sa squad
  1080. */
  1081. static Quat<_Tp> interPoint(const Quat<_Tp> &q0, const Quat<_Tp> &q1,
  1082. const Quat<_Tp> &q2, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1083. /**
  1084. * @brief to calculate a quaternion which is the result of a \f$C^1\f$ continuous
  1085. * spline curve constructed by squad at the ratio t. Here, the interpolation values are
  1086. * between \f$q_1\f$ and \f$q_2\f$. \f$q_0\f$ and \f$q_2\f$ are used to ensure the \f$C^1\f$
  1087. * continuity. if t = 0, it returns \f$q_1\f$, if t = 1, it returns \f$q_2\f$.
  1088. * @param q0 the first input quaternion to ensure \f$C^1\f$ continuity.
  1089. * @param q1 the second input quaternion.
  1090. * @param q2 the third input quaternion.
  1091. * @param q3 the fourth input quaternion the same use of \f$q1\f$.
  1092. * @param t ratio over a range [0, 1].
  1093. * @param assumeUnit if QUAT_ASSUME_UNIT, \f$q_0, q_1, q_2, q_3\f$ assume to be unit quaternion. Otherwise, all input
  1094. * quaternions will be normalized inside the function.
  1095. *
  1096. * For example:
  1097. *
  1098. * If there are three double quaternions \f$v_0, v_1, v_2\f$ waiting to be interpolated.
  1099. *
  1100. * Interpolation between \f$v_0\f$ and \f$v_1\f$ with a ratio \f$t_0\f$ could be calculated as
  1101. * ```
  1102. * Quatd::spline(v0, v0, v1, v2, t0);
  1103. * ```
  1104. * Interpolation between \f$v_1\f$ and \f$v_2\f$ with a ratio \f$t_0\f$ could be calculated as
  1105. * ```
  1106. * Quatd::spline(v0, v1, v2, v2, t0);
  1107. * ```
  1108. * @sa squad, slerp
  1109. */
  1110. static Quat<_Tp> spline(const Quat<_Tp> &q0, const Quat<_Tp> &q1,
  1111. const Quat<_Tp> &q2, const Quat<_Tp> &q3,
  1112. const _Tp t, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1113. /**
  1114. * @brief Return opposite quaternion \f$-p\f$
  1115. * which satisfies \f$p + (-p) = 0.\f$
  1116. *
  1117. * For example
  1118. * ```
  1119. * Quatd q{1, 2, 3, 4};
  1120. * std::cout << -q << std::endl; // [-1, -2, -3, -4]
  1121. * ```
  1122. */
  1123. Quat<_Tp> operator-() const;
  1124. /**
  1125. * @brief return true if two quaternions p and q are nearly equal, i.e. when the absolute
  1126. * value of each \f$p_i\f$ and \f$q_i\f$ is less than CV_QUAT_EPS.
  1127. */
  1128. bool operator==(const Quat<_Tp>&) const;
  1129. /**
  1130. * @brief Addition operator of two quaternions p and q.
  1131. * It returns a new quaternion that each value is the sum of \f$p_i\f$ and \f$q_i\f$.
  1132. *
  1133. * For example
  1134. * ```
  1135. * Quatd p{1, 2, 3, 4};
  1136. * Quatd q{5, 6, 7, 8};
  1137. * std::cout << p + q << std::endl; //[6, 8, 10, 12]
  1138. * ```
  1139. */
  1140. Quat<_Tp> operator+(const Quat<_Tp>&) const;
  1141. /**
  1142. * @brief Addition assignment operator of two quaternions p and q.
  1143. * It adds right operand to the left operand and assign the result to left operand.
  1144. *
  1145. * For example
  1146. * ```
  1147. * Quatd p{1, 2, 3, 4};
  1148. * Quatd q{5, 6, 7, 8};
  1149. * p += q; // equivalent to p = p + q
  1150. * std::cout << p << std::endl; //[6, 8, 10, 12]
  1151. *
  1152. * ```
  1153. */
  1154. Quat<_Tp>& operator+=(const Quat<_Tp>&);
  1155. /**
  1156. * @brief Subtraction operator of two quaternions p and q.
  1157. * It returns a new quaternion that each value is the sum of \f$p_i\f$ and \f$-q_i\f$.
  1158. *
  1159. * For example
  1160. * ```
  1161. * Quatd p{1, 2, 3, 4};
  1162. * Quatd q{5, 6, 7, 8};
  1163. * std::cout << p - q << std::endl; //[-4, -4, -4, -4]
  1164. * ```
  1165. */
  1166. Quat<_Tp> operator-(const Quat<_Tp>&) const;
  1167. /**
  1168. * @brief Subtraction assignment operator of two quaternions p and q.
  1169. * It subtracts right operand from the left operand and assign the result to left operand.
  1170. *
  1171. * For example
  1172. * ```
  1173. * Quatd p{1, 2, 3, 4};
  1174. * Quatd q{5, 6, 7, 8};
  1175. * p -= q; // equivalent to p = p - q
  1176. * std::cout << p << std::endl; //[-4, -4, -4, -4]
  1177. *
  1178. * ```
  1179. */
  1180. Quat<_Tp>& operator-=(const Quat<_Tp>&);
  1181. /**
  1182. * @brief Multiplication assignment operator of two quaternions q and p.
  1183. * It multiplies right operand with the left operand and assign the result to left operand.
  1184. *
  1185. * Rule of quaternion multiplication:
  1186. * \f[
  1187. * \begin{equation}
  1188. * \begin{split}
  1189. * p * q &= [p_0, \boldsymbol{u}]*[q_0, \boldsymbol{v}]\\
  1190. * &=[p_0q_0 - \boldsymbol{u}\cdot \boldsymbol{v}, p_0\boldsymbol{v} + q_0\boldsymbol{u}+ \boldsymbol{u}\times \boldsymbol{v}].
  1191. * \end{split}
  1192. * \end{equation}
  1193. * \f]
  1194. * where \f$\cdot\f$ means dot product and \f$\times \f$ means cross product.
  1195. *
  1196. * For example
  1197. * ```
  1198. * Quatd p{1, 2, 3, 4};
  1199. * Quatd q{5, 6, 7, 8};
  1200. * p *= q; // equivalent to p = p * q
  1201. * std::cout << p << std::endl; //[-60, 12, 30, 24]
  1202. * ```
  1203. */
  1204. Quat<_Tp>& operator*=(const Quat<_Tp>&);
  1205. /**
  1206. * @brief Multiplication assignment operator of a quaternions and a scalar.
  1207. * It multiplies right operand with the left operand and assign the result to left operand.
  1208. *
  1209. * Rule of quaternion multiplication with a scalar:
  1210. * \f[
  1211. * \begin{equation}
  1212. * \begin{split}
  1213. * p * s &= [w, x, y, z] * s\\
  1214. * &=[w * s, x * s, y * s, z * s].
  1215. * \end{split}
  1216. * \end{equation}
  1217. * \f]
  1218. *
  1219. * For example
  1220. * ```
  1221. * Quatd p{1, 2, 3, 4};
  1222. * double s = 2.0;
  1223. * p *= s; // equivalent to p = p * s
  1224. * std::cout << p << std::endl; //[2.0, 4.0, 6.0, 8.0]
  1225. * ```
  1226. * @note the type of scalar should be equal to the quaternion.
  1227. */
  1228. Quat<_Tp>& operator*=(const _Tp s);
  1229. /**
  1230. * @brief Multiplication operator of two quaternions q and p.
  1231. * Multiplies values on either side of the operator.
  1232. *
  1233. * Rule of quaternion multiplication:
  1234. * \f[
  1235. * \begin{equation}
  1236. * \begin{split}
  1237. * p * q &= [p_0, \boldsymbol{u}]*[q_0, \boldsymbol{v}]\\
  1238. * &=[p_0q_0 - \boldsymbol{u}\cdot \boldsymbol{v}, p_0\boldsymbol{v} + q_0\boldsymbol{u}+ \boldsymbol{u}\times \boldsymbol{v}].
  1239. * \end{split}
  1240. * \end{equation}
  1241. * \f]
  1242. * where \f$\cdot\f$ means dot product and \f$\times \f$ means cross product.
  1243. *
  1244. * For example
  1245. * ```
  1246. * Quatd p{1, 2, 3, 4};
  1247. * Quatd q{5, 6, 7, 8};
  1248. * std::cout << p * q << std::endl; //[-60, 12, 30, 24]
  1249. * ```
  1250. */
  1251. Quat<_Tp> operator*(const Quat<_Tp>&) const;
  1252. /**
  1253. * @brief Division operator of a quaternions and a scalar.
  1254. * It divides left operand with the right operand and assign the result to left operand.
  1255. *
  1256. * Rule of quaternion division with a scalar:
  1257. * \f[
  1258. * \begin{equation}
  1259. * \begin{split}
  1260. * p / s &= [w, x, y, z] / s\\
  1261. * &=[w/s, x/s, y/s, z/s].
  1262. * \end{split}
  1263. * \end{equation}
  1264. * \f]
  1265. *
  1266. * For example
  1267. * ```
  1268. * Quatd p{1, 2, 3, 4};
  1269. * double s = 2.0;
  1270. * p /= s; // equivalent to p = p / s
  1271. * std::cout << p << std::endl; //[0.5, 1, 1.5, 2]
  1272. * ```
  1273. * @note the type of scalar should be equal to this quaternion.
  1274. */
  1275. Quat<_Tp> operator/(const _Tp s) const;
  1276. /**
  1277. * @brief Division operator of two quaternions p and q.
  1278. * Divides left hand operand by right hand operand.
  1279. *
  1280. * Rule of quaternion division with a scalar:
  1281. * \f[
  1282. * \begin{equation}
  1283. * \begin{split}
  1284. * p / q &= p * q.inv()\\
  1285. * \end{split}
  1286. * \end{equation}
  1287. * \f]
  1288. *
  1289. * For example
  1290. * ```
  1291. * Quatd p{1, 2, 3, 4};
  1292. * Quatd q{5, 6, 7, 8};
  1293. * std::cout << p / q << std::endl; // equivalent to p * q.inv()
  1294. * ```
  1295. */
  1296. Quat<_Tp> operator/(const Quat<_Tp>&) const;
  1297. /**
  1298. * @brief Division assignment operator of a quaternions and a scalar.
  1299. * It divides left operand with the right operand and assign the result to left operand.
  1300. *
  1301. * Rule of quaternion division with a scalar:
  1302. * \f[
  1303. * \begin{equation}
  1304. * \begin{split}
  1305. * p / s &= [w, x, y, z] / s\\
  1306. * &=[w / s, x / s, y / s, z / s].
  1307. * \end{split}
  1308. * \end{equation}
  1309. * \f]
  1310. *
  1311. * For example
  1312. * ```
  1313. * Quatd p{1, 2, 3, 4};
  1314. * double s = 2.0;;
  1315. * p /= s; // equivalent to p = p / s
  1316. * std::cout << p << std::endl; //[0.5, 1.0, 1.5, 2.0]
  1317. * ```
  1318. * @note the type of scalar should be equal to the quaternion.
  1319. */
  1320. Quat<_Tp>& operator/=(const _Tp s);
  1321. /**
  1322. * @brief Division assignment operator of two quaternions p and q;
  1323. * It divides left operand with the right operand and assign the result to left operand.
  1324. *
  1325. * Rule of quaternion division with a quaternion:
  1326. * \f[
  1327. * \begin{equation}
  1328. * \begin{split}
  1329. * p / q&= p * q.inv()\\
  1330. * \end{split}
  1331. * \end{equation}
  1332. * \f]
  1333. *
  1334. * For example
  1335. * ```
  1336. * Quatd p{1, 2, 3, 4};
  1337. * Quatd q{5, 6, 7, 8};
  1338. * p /= q; // equivalent to p = p * q.inv()
  1339. * std::cout << p << std::endl;
  1340. * ```
  1341. */
  1342. Quat<_Tp>& operator/=(const Quat<_Tp>&);
  1343. _Tp& operator[](std::size_t n);
  1344. const _Tp& operator[](std::size_t n) const;
  1345. /**
  1346. * @brief Subtraction operator of a scalar and a quaternions.
  1347. * Subtracts right hand operand from left hand operand.
  1348. *
  1349. * For example
  1350. * ```
  1351. * Quatd p{1, 2, 3, 4};
  1352. * double scalar = 2.0;
  1353. * std::cout << scalar - p << std::endl; //[1.0, -2, -3, -4]
  1354. * ```
  1355. * @note the type of scalar should be equal to the quaternion.
  1356. */
  1357. template <typename T>
  1358. friend Quat<T> cv::operator-(const T s, const Quat<T>&);
  1359. /**
  1360. * @brief Subtraction operator of a quaternions and a scalar.
  1361. * Subtracts right hand operand from left hand operand.
  1362. *
  1363. * For example
  1364. * ```
  1365. * Quatd p{1, 2, 3, 4};
  1366. * double scalar = 2.0;
  1367. * std::cout << p - scalar << std::endl; //[-1.0, 2, 3, 4]
  1368. * ```
  1369. * @note the type of scalar should be equal to the quaternion.
  1370. */
  1371. template <typename T>
  1372. friend Quat<T> cv::operator-(const Quat<T>&, const T s);
  1373. /**
  1374. * @brief Addition operator of a quaternions and a scalar.
  1375. * Adds right hand operand from left hand operand.
  1376. *
  1377. * For example
  1378. * ```
  1379. * Quatd p{1, 2, 3, 4};
  1380. * double scalar = 2.0;
  1381. * std::cout << scalar + p << std::endl; //[3.0, 2, 3, 4]
  1382. * ```
  1383. * @note the type of scalar should be equal to the quaternion.
  1384. */
  1385. template <typename T>
  1386. friend Quat<T> cv::operator+(const T s, const Quat<T>&);
  1387. /**
  1388. * @brief Addition operator of a quaternions and a scalar.
  1389. * Adds right hand operand from left hand operand.
  1390. *
  1391. * For example
  1392. * ```
  1393. * Quatd p{1, 2, 3, 4};
  1394. * double scalar = 2.0;
  1395. * std::cout << p + scalar << std::endl; //[3.0, 2, 3, 4]
  1396. * ```
  1397. * @note the type of scalar should be equal to the quaternion.
  1398. */
  1399. template <typename T>
  1400. friend Quat<T> cv::operator+(const Quat<T>&, const T s);
  1401. /**
  1402. * @brief Multiplication operator of a scalar and a quaternions.
  1403. * It multiplies right operand with the left operand and assign the result to left operand.
  1404. *
  1405. * Rule of quaternion multiplication with a scalar:
  1406. * \f[
  1407. * \begin{equation}
  1408. * \begin{split}
  1409. * p * s &= [w, x, y, z] * s\\
  1410. * &=[w * s, x * s, y * s, z * s].
  1411. * \end{split}
  1412. * \end{equation}
  1413. * \f]
  1414. *
  1415. * For example
  1416. * ```
  1417. * Quatd p{1, 2, 3, 4};
  1418. * double s = 2.0;
  1419. * std::cout << s * p << std::endl; //[2.0, 4.0, 6.0, 8.0]
  1420. * ```
  1421. * @note the type of scalar should be equal to the quaternion.
  1422. */
  1423. template <typename T>
  1424. friend Quat<T> cv::operator*(const T s, const Quat<T>&);
  1425. /**
  1426. * @brief Multiplication operator of a quaternion and a scalar.
  1427. * It multiplies right operand with the left operand and assign the result to left operand.
  1428. *
  1429. * Rule of quaternion multiplication with a scalar:
  1430. * \f[
  1431. * \begin{equation}
  1432. * \begin{split}
  1433. * p * s &= [w, x, y, z] * s\\
  1434. * &=[w * s, x * s, y * s, z * s].
  1435. * \end{split}
  1436. * \end{equation}
  1437. * \f]
  1438. *
  1439. * For example
  1440. * ```
  1441. * Quatd p{1, 2, 3, 4};
  1442. * double s = 2.0;
  1443. * std::cout << p * s << std::endl; //[2.0, 4.0, 6.0, 8.0]
  1444. * ```
  1445. * @note the type of scalar should be equal to the quaternion.
  1446. */
  1447. template <typename T>
  1448. friend Quat<T> cv::operator*(const Quat<T>&, const T s);
  1449. template <typename S>
  1450. friend std::ostream& cv::operator<<(std::ostream&, const Quat<S>&);
  1451. /**
  1452. * @brief Transform a quaternion q to Euler angles.
  1453. *
  1454. *
  1455. * When transforming a quaternion \f$q = w + x\boldsymbol{i} + y\boldsymbol{j} + z\boldsymbol{k}\f$ to Euler angles, rotation matrix M can be calculated by:
  1456. * \f[ \begin{aligned} {M} &={\begin{bmatrix}1-2(y^{2}+z^{2})&2(xy-zx)&2(xz+yw)\\2(xy+zw)&1-2(x^{2}+z^{2})&2(yz-xw)\\2(xz-yw)&2(yz+xw)&1-2(x^{2}+y^{2})\end{bmatrix}}\end{aligned}.\f]
  1457. * On the other hand, the rotation matrix can be obtained from Euler angles.
  1458. * Using intrinsic rotations with Euler angles type XYZ as an example,
  1459. * \f$\theta_1 \f$, \f$\theta_2 \f$, \f$\theta_3 \f$ are three angles for Euler angles, the rotation matrix R can be calculated by:\f[R =X(\theta_1)Y(\theta_2)Z(\theta_3)
  1460. * ={\begin{bmatrix}\cos\theta_{2}\cos\theta_{3}&-\cos\theta_{2}\sin\theta_{3}&\sin\theta_{2}\\\cos\theta_{1}\sin\theta_{3}+\cos\theta_{3}\sin\theta_{1}\sin\theta_{2}&\cos\theta_{1}\cos\theta_{3}-\sin\theta_{1}\sin\theta_{2}\sin\theta_{3}&-\cos\theta_{2}\sin\theta_{1}\\\sin\theta_{1}\sin\theta_{3}-\cos\theta_{1}\cos\theta_{3}\sin\theta_{2}&\cos\theta_{3}\sin\theta_{1}+\cos\theta_{1}\sin\theta_{2}\sin\theta_{3}&\cos\theta_{1}\cos_{2}\end{bmatrix}}\f]
  1461. * Rotation matrix M and R are equal. As long as \f$ s_{2} \neq 1 \f$, by comparing each element of two matrices ,the solution is\f$\begin{cases} \theta_1 = \arctan2(-m_{23},m_{33})\\\theta_2 = arcsin(m_{13}) \\\theta_3 = \arctan2(-m_{12},m_{11}) \end{cases}\f$.
  1462. *
  1463. * When \f$ s_{2}=1\f$ or \f$ s_{2}=-1\f$, the gimbal lock occurs. The function will prompt "WARNING: Gimbal Lock will occur. Euler angles is non-unique. For intrinsic rotations, we set the third angle to 0, and for external rotation, we set the first angle to 0.".
  1464. *
  1465. * When \f$ s_{2}=1\f$ ,
  1466. * The rotation matrix R is \f$R = {\begin{bmatrix}0&0&1\\\sin(\theta_1+\theta_3)&\cos(\theta_1+\theta_3)&0\\-\cos(\theta_1+\theta_3)&\sin(\theta_1+\theta_3)&0\end{bmatrix}}\f$.
  1467. *
  1468. * The number of solutions is infinite with the condition \f$\begin{cases} \theta_1+\theta_3 = \arctan2(m_{21},m_{22})\\ \theta_2=\pi/2 \end{cases}\ \f$.
  1469. *
  1470. * We set \f$ \theta_3 = 0\f$, the solution is \f$\begin{cases} \theta_1=\arctan2(m_{21},m_{22})\\ \theta_2=\pi/2\\ \theta_3=0 \end{cases}\f$.
  1471. *
  1472. * When \f$ s_{2}=-1\f$,
  1473. * The rotation matrix R is \f$X_{1}Y_{2}Z_{3}={\begin{bmatrix}0&0&-1\\-\sin(\theta_1-\theta_3)&\cos(\theta_1-\theta_3)&0\\\cos(\theta_1-\theta_3)&\sin(\theta_1-\theta_3)&0\end{bmatrix}}\f$.
  1474. *
  1475. * The number of solutions is infinite with the condition \f$\begin{cases} \theta_1+\theta_3 = \arctan2(m_{32},m_{22})\\ \theta_2=\pi/2 \end{cases}\ \f$.
  1476. *
  1477. * We set \f$ \theta_3 = 0\f$, the solution is \f$ \begin{cases}\theta_1=\arctan2(m_{32},m_{22}) \\ \theta_2=-\pi/2\\ \theta_3=0\end{cases}\f$.
  1478. *
  1479. * Since \f$ sin \theta\in [-1,1] \f$ and \f$ cos \theta \in [-1,1] \f$, the unnormalized quaternion will cause computational troubles. For this reason, this function will normalize the quaternion at first and @ref QuatAssumeType is not needed.
  1480. *
  1481. * When the gimbal lock occurs, we set \f$\theta_3 = 0\f$ for intrinsic rotations or \f$\theta_1 = 0\f$ for extrinsic rotations.
  1482. *
  1483. * As a result, for every Euler angles type, we can get solution as shown in the following table.
  1484. * EulerAnglesType | Ordinary | \f$\theta_2 = π/2\f$ | \f$\theta_2 = -π/2\f$
  1485. * ------------- | -------------| -------------| -------------
  1486. * INT_XYZ|\f$ \theta_1 = \arctan2(-m_{23},m_{33})\\\theta_2 = \arcsin(m_{13}) \\\theta_3= \arctan2(-m_{12},m_{11}) \f$|\f$ \theta_1=\arctan2(m_{21},m_{22})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(m_{32},m_{22})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1487. * INT_XZY|\f$ \theta_1 = \arctan2(m_{32},m_{22})\\\theta_2 = -\arcsin(m_{12}) \\\theta_3= \arctan2(m_{13},m_{11}) \f$|\f$ \theta_1=\arctan2(m_{31},m_{33})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(-m_{23},m_{33})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1488. * INT_YXZ|\f$ \theta_1 = \arctan2(m_{13},m_{33})\\\theta_2 = -\arcsin(m_{23}) \\\theta_3= \arctan2(m_{21},m_{22}) \f$|\f$ \theta_1=\arctan2(m_{12},m_{11})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(-m_{12},m_{11})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1489. * INT_YZX|\f$ \theta_1 = \arctan2(-m_{31},m_{11})\\\theta_2 = \arcsin(m_{21}) \\\theta_3= \arctan2(-m_{23},m_{22}) \f$|\f$ \theta_1=\arctan2(m_{13},m_{33})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(m_{13},m_{12})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1490. * INT_ZXY|\f$ \theta_1 = \arctan2(-m_{12},m_{22})\\\theta_2 = \arcsin(m_{32}) \\\theta_3= \arctan2(-m_{31},m_{33}) \f$|\f$ \theta_1=\arctan2(m_{21},m_{11})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(m_{21},m_{11})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1491. * INT_ZYX|\f$ \theta_1 = \arctan2(m_{21},m_{11})\\\theta_2 = \arcsin(-m_{31}) \\\theta_3= \arctan2(m_{32},m_{33}) \f$|\f$ \theta_1=\arctan2(m_{23},m_{22})\\ \theta_2=\pi/2\\ \theta_3=0 \f$|\f$ \theta_1=\arctan2(-m_{12},m_{22})\\ \theta_2=-\pi/2\\ \theta_3=0 \f$
  1492. * EXT_XYZ|\f$ \theta_1 = \arctan2(m_{32},m_{33})\\\theta_2 = \arcsin(-m_{31}) \\\ \theta_3 = \arctan2(m_{21},m_{11})\f$|\f$ \theta_1= 0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{23},m_{22}) \f$|\f$ \theta_1=0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(-m_{12},m_{22}) \f$
  1493. * EXT_XZY|\f$ \theta_1 = \arctan2(-m_{23},m_{22})\\\theta_2 = \arcsin(m_{21}) \\\theta_3= \arctan2(-m_{31},m_{11})\f$|\f$ \theta_1= 0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{13},m_{33}) \f$|\f$ \theta_1=0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(m_{13},m_{12}) \f$
  1494. * EXT_YXZ|\f$ \theta_1 = \arctan2(-m_{31},m_{33}) \\\theta_2 = \arcsin(m_{32}) \\\theta_3= \arctan2(-m_{12},m_{22})\f$|\f$ \theta_1= 0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{21},m_{11}) \f$|\f$ \theta_1=0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(m_{21},m_{11}) \f$
  1495. * EXT_YZX|\f$ \theta_1 = \arctan2(m_{13},m_{11})\\\theta_2 = -\arcsin(m_{12}) \\\theta_3= \arctan2(m_{32},m_{22})\f$|\f$ \theta_1= 0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{31},m_{33}) \f$|\f$ \theta_1=0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(-m_{23},m_{33}) \f$
  1496. * EXT_ZXY|\f$ \theta_1 = \arctan2(m_{21},m_{22})\\\theta_2 = -\arcsin(m_{23}) \\\theta_3= \arctan2(m_{13},m_{33})\f$|\f$ \theta_1= 0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{12},m_{11}) \f$|\f$ \theta_1= 0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(-m_{12},m_{11}) \f$
  1497. * EXT_ZYX|\f$ \theta_1 = \arctan2(-m_{12},m_{11})\\\theta_2 = \arcsin(m_{13}) \\\theta_3= \arctan2(-m_{23},m_{33})\f$|\f$ \theta_1=0\\ \theta_2=\pi/2\\ \theta_3=\arctan2(m_{21},m_{22}) \f$|\f$ \theta_1=0\\ \theta_2=-\pi/2\\ \theta_3=\arctan2(m_{32},m_{22}) \f$
  1498. *
  1499. * EulerAnglesType | Ordinary | \f$\theta_2 = 0\f$ | \f$\theta_2 = π\f$
  1500. * ------------- | -------------| -------------| -------------
  1501. * INT_XYX| \f$ \theta_1 = \arctan2(m_{21},-m_{31})\\\theta_2 =\arccos(m_{11}) \\\theta_3 = \arctan2(m_{12},m_{13}) \f$| \f$ \theta_1=\arctan2(m_{32},m_{33})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(m_{23},m_{22})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1502. * INT_XZX| \f$ \theta_1 = \arctan2(m_{31},m_{21})\\\theta_2 = \arccos(m_{11}) \\\theta_3 = \arctan2(m_{13},-m_{12}) \f$| \f$ \theta_1=\arctan2(m_{32},m_{33})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(-m_{32},m_{33})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1503. * INT_YXY| \f$ \theta_1 = \arctan2(m_{12},m_{32})\\\theta_2 = \arccos(m_{22}) \\\theta_3 = \arctan2(m_{21},-m_{23}) \f$| \f$ \theta_1=\arctan2(m_{13},m_{11})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(-m_{31},m_{11})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1504. * INT_YZY| \f$ \theta_1 = \arctan2(m_{32},-m_{12})\\\theta_2 = \arccos(m_{22}) \\\theta_3 =\arctan2(m_{23},m_{21}) \f$| \f$ \theta_1=\arctan2(m_{13},m_{11})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(m_{13},-m_{11})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1505. * INT_ZXZ| \f$ \theta_1 = \arctan2(-m_{13},m_{23})\\\theta_2 = \arccos(m_{33}) \\\theta_3 =\arctan2(m_{31},m_{32}) \f$| \f$ \theta_1=\arctan2(m_{21},m_{22})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(m_{21},m_{11})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1506. * INT_ZYZ| \f$ \theta_1 = \arctan2(m_{23},m_{13})\\\theta_2 = \arccos(m_{33}) \\\theta_3 = \arctan2(m_{32},-m_{31}) \f$| \f$ \theta_1=\arctan2(m_{21},m_{11})\\ \theta_2=0\\ \theta_3=0 \f$| \f$ \theta_1=\arctan2(m_{21},m_{11})\\ \theta_2=\pi\\ \theta_3=0 \f$
  1507. * EXT_XYX| \f$ \theta_1 = \arctan2(m_{12},m_{13}) \\\theta_2 = \arccos(m_{11}) \\\theta_3 = \arctan2(m_{21},-m_{31})\f$| \f$ \theta_1=0\\ \theta_2=0\\ \theta_3=\arctan2(m_{32},m_{33}) \f$| \f$ \theta_1= 0\\ \theta_2=\pi\\ \theta_3= \arctan2(m_{23},m_{22}) \f$
  1508. * EXT_XZX| \f$ \theta_1 = \arctan2(m_{13},-m_{12})\\\theta_2 = \arccos(m_{11}) \\\theta_3 = \arctan2(m_{31},m_{21})\f$| \f$ \theta_1= 0\\ \theta_2=0\\ \theta_3=\arctan2(m_{32},m_{33}) \f$| \f$ \theta_1= 0\\ \theta_2=\pi\\ \theta_3=\arctan2(-m_{32},m_{33}) \f$
  1509. * EXT_YXY| \f$ \theta_1 = \arctan2(m_{21},-m_{23})\\\theta_2 = \arccos(m_{22}) \\\theta_3 = \arctan2(m_{12},m_{32}) \f$| \f$ \theta_1= 0\\ \theta_2=0\\ \theta_3=\arctan2(m_{13},m_{11}) \f$| \f$ \theta_1= 0\\ \theta_2=\pi\\ \theta_3=\arctan2(-m_{31},m_{11}) \f$
  1510. * EXT_YZY| \f$ \theta_1 = \arctan2(m_{23},m_{21}) \\\theta_2 = \arccos(m_{22}) \\\theta_3 = \arctan2(m_{32},-m_{12}) \f$| \f$ \theta_1= 0\\ \theta_2=0\\ \theta_3=\arctan2(m_{13},m_{11}) \f$| \f$ \theta_1=0\\ \theta_2=\pi\\ \theta_3=\arctan2(m_{13},-m_{11}) \f$
  1511. * EXT_ZXZ| \f$ \theta_1 = \arctan2(m_{31},m_{32}) \\\theta_2 = \arccos(m_{33}) \\\theta_3 = \arctan2(-m_{13},m_{23})\f$| \f$ \theta_1=0\\ \theta_2=0\\ \theta_3=\arctan2(m_{21},m_{22}) \f$| \f$ \theta_1= 0\\ \theta_2=\pi\\ \theta_3=\arctan2(m_{21},m_{11}) \f$
  1512. * EXT_ZYZ| \f$ \theta_1 = \arctan2(m_{32},-m_{31})\\\theta_2 = \arccos(m_{33}) \\\theta_3 = \arctan2(m_{23},m_{13}) \f$| \f$ \theta_1=0\\ \theta_2=0\\ \theta_3=\arctan2(m_{21},m_{11}) \f$| \f$ \theta_1= 0\\ \theta_2=\pi\\ \theta_3=\arctan2(m_{21},m_{11}) \f$
  1513. *
  1514. * @param eulerAnglesType the convertion Euler angles type
  1515. */
  1516. Vec<_Tp, 3> toEulerAngles(QuatEnum::EulerAnglesType eulerAnglesType);
  1517. _Tp w, x, y, z;
  1518. };
  1519. template <typename T>
  1520. Quat<T> inv(const Quat<T> &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1521. template <typename T>
  1522. Quat<T> sinh(const Quat<T> &q);
  1523. template <typename T>
  1524. Quat<T> cosh(const Quat<T> &q);
  1525. template <typename T>
  1526. Quat<T> tanh(const Quat<T> &q);
  1527. template <typename T>
  1528. Quat<T> sin(const Quat<T> &q);
  1529. template <typename T>
  1530. Quat<T> cos(const Quat<T> &q);
  1531. template <typename T>
  1532. Quat<T> tan(const Quat<T> &q);
  1533. template <typename T>
  1534. Quat<T> asinh(const Quat<T> &q);
  1535. template <typename T>
  1536. Quat<T> acosh(const Quat<T> &q);
  1537. template <typename T>
  1538. Quat<T> atanh(const Quat<T> &q);
  1539. template <typename T>
  1540. Quat<T> asin(const Quat<T> &q);
  1541. template <typename T>
  1542. Quat<T> acos(const Quat<T> &q);
  1543. template <typename T>
  1544. Quat<T> atan(const Quat<T> &q);
  1545. template <typename T>
  1546. Quat<T> power(const Quat<T> &q, const Quat<T> &p, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1547. template <typename T>
  1548. Quat<T> exp(const Quat<T> &q);
  1549. template <typename T>
  1550. Quat<T> log(const Quat<T> &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1551. template <typename T>
  1552. Quat<T> power(const Quat<T>& q, const T x, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1553. template <typename T>
  1554. Quat<T> crossProduct(const Quat<T> &p, const Quat<T> &q);
  1555. template <typename S>
  1556. Quat<S> sqrt(const Quat<S> &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT);
  1557. template <typename T>
  1558. Quat<T> operator*(const T, const Quat<T>&);
  1559. template <typename T>
  1560. Quat<T> operator*(const Quat<T>&, const T);
  1561. template <typename S>
  1562. std::ostream& operator<<(std::ostream&, const Quat<S>&);
  1563. using Quatd = Quat<double>;
  1564. using Quatf = Quat<float>;
  1565. //! @} core
  1566. }
  1567. #include "opencv2/core/quaternion.inl.hpp"
  1568. #endif /* OPENCV_CORE_QUATERNION_HPP */