b3Matrix3x3.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. Copyright (c) 2003-2013 Gino van den Bergen / Erwin Coumans http://bulletphysics.org
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef B3_MATRIX3x3_H
  13. #define B3_MATRIX3x3_H
  14. #include "b3Vector3.h"
  15. #include "b3Quaternion.h"
  16. #include <stdio.h>
  17. #ifdef B3_USE_SSE
  18. //const __m128 B3_ATTRIBUTE_ALIGNED16(b3v2220) = {2.0f, 2.0f, 2.0f, 0.0f};
  19. const __m128 B3_ATTRIBUTE_ALIGNED16(b3vMPPP) = {-0.0f, +0.0f, +0.0f, +0.0f};
  20. #endif
  21. #if defined(B3_USE_SSE) || defined(B3_USE_NEON)
  22. const b3SimdFloat4 B3_ATTRIBUTE_ALIGNED16(b3v1000) = {1.0f, 0.0f, 0.0f, 0.0f};
  23. const b3SimdFloat4 B3_ATTRIBUTE_ALIGNED16(b3v0100) = {0.0f, 1.0f, 0.0f, 0.0f};
  24. const b3SimdFloat4 B3_ATTRIBUTE_ALIGNED16(b3v0010) = {0.0f, 0.0f, 1.0f, 0.0f};
  25. #endif
  26. #ifdef B3_USE_DOUBLE_PRECISION
  27. #define b3Matrix3x3Data b3Matrix3x3DoubleData
  28. #else
  29. #define b3Matrix3x3Data b3Matrix3x3FloatData
  30. #endif //B3_USE_DOUBLE_PRECISION
  31. /**@brief The b3Matrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with b3Quaternion, b3Transform and b3Vector3.
  32. * Make sure to only include a pure orthogonal matrix without scaling. */
  33. B3_ATTRIBUTE_ALIGNED16(class)
  34. b3Matrix3x3
  35. {
  36. ///Data storage for the matrix, each vector is a row of the matrix
  37. b3Vector3 m_el[3];
  38. public:
  39. /** @brief No initializaion constructor */
  40. b3Matrix3x3() {}
  41. // explicit b3Matrix3x3(const b3Scalar *m) { setFromOpenGLSubMatrix(m); }
  42. /**@brief Constructor from Quaternion */
  43. explicit b3Matrix3x3(const b3Quaternion& q) { setRotation(q); }
  44. /*
  45. template <typename b3Scalar>
  46. Matrix3x3(const b3Scalar& yaw, const b3Scalar& pitch, const b3Scalar& roll)
  47. {
  48. setEulerYPR(yaw, pitch, roll);
  49. }
  50. */
  51. /** @brief Constructor with row major formatting */
  52. b3Matrix3x3(const b3Scalar& xx, const b3Scalar& xy, const b3Scalar& xz,
  53. const b3Scalar& yx, const b3Scalar& yy, const b3Scalar& yz,
  54. const b3Scalar& zx, const b3Scalar& zy, const b3Scalar& zz)
  55. {
  56. setValue(xx, xy, xz,
  57. yx, yy, yz,
  58. zx, zy, zz);
  59. }
  60. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  61. B3_FORCE_INLINE b3Matrix3x3(const b3SimdFloat4 v0, const b3SimdFloat4 v1, const b3SimdFloat4 v2)
  62. {
  63. m_el[0].mVec128 = v0;
  64. m_el[1].mVec128 = v1;
  65. m_el[2].mVec128 = v2;
  66. }
  67. B3_FORCE_INLINE b3Matrix3x3(const b3Vector3& v0, const b3Vector3& v1, const b3Vector3& v2)
  68. {
  69. m_el[0] = v0;
  70. m_el[1] = v1;
  71. m_el[2] = v2;
  72. }
  73. // Copy constructor
  74. B3_FORCE_INLINE b3Matrix3x3(const b3Matrix3x3& rhs)
  75. {
  76. m_el[0].mVec128 = rhs.m_el[0].mVec128;
  77. m_el[1].mVec128 = rhs.m_el[1].mVec128;
  78. m_el[2].mVec128 = rhs.m_el[2].mVec128;
  79. }
  80. // Assignment Operator
  81. B3_FORCE_INLINE b3Matrix3x3& operator=(const b3Matrix3x3& m)
  82. {
  83. m_el[0].mVec128 = m.m_el[0].mVec128;
  84. m_el[1].mVec128 = m.m_el[1].mVec128;
  85. m_el[2].mVec128 = m.m_el[2].mVec128;
  86. return *this;
  87. }
  88. #else
  89. /** @brief Copy constructor */
  90. B3_FORCE_INLINE b3Matrix3x3(const b3Matrix3x3& other)
  91. {
  92. m_el[0] = other.m_el[0];
  93. m_el[1] = other.m_el[1];
  94. m_el[2] = other.m_el[2];
  95. }
  96. /** @brief Assignment Operator */
  97. B3_FORCE_INLINE b3Matrix3x3& operator=(const b3Matrix3x3& other)
  98. {
  99. m_el[0] = other.m_el[0];
  100. m_el[1] = other.m_el[1];
  101. m_el[2] = other.m_el[2];
  102. return *this;
  103. }
  104. #endif
  105. /** @brief Get a column of the matrix as a vector
  106. * @param i Column number 0 indexed */
  107. B3_FORCE_INLINE b3Vector3 getColumn(int i) const
  108. {
  109. return b3MakeVector3(m_el[0][i], m_el[1][i], m_el[2][i]);
  110. }
  111. /** @brief Get a row of the matrix as a vector
  112. * @param i Row number 0 indexed */
  113. B3_FORCE_INLINE const b3Vector3& getRow(int i) const
  114. {
  115. b3FullAssert(0 <= i && i < 3);
  116. return m_el[i];
  117. }
  118. /** @brief Get a mutable reference to a row of the matrix as a vector
  119. * @param i Row number 0 indexed */
  120. B3_FORCE_INLINE b3Vector3& operator[](int i)
  121. {
  122. b3FullAssert(0 <= i && i < 3);
  123. return m_el[i];
  124. }
  125. /** @brief Get a const reference to a row of the matrix as a vector
  126. * @param i Row number 0 indexed */
  127. B3_FORCE_INLINE const b3Vector3& operator[](int i) const
  128. {
  129. b3FullAssert(0 <= i && i < 3);
  130. return m_el[i];
  131. }
  132. /** @brief Multiply by the target matrix on the right
  133. * @param m Rotation matrix to be applied
  134. * Equivilant to this = this * m */
  135. b3Matrix3x3& operator*=(const b3Matrix3x3& m);
  136. /** @brief Adds by the target matrix on the right
  137. * @param m matrix to be applied
  138. * Equivilant to this = this + m */
  139. b3Matrix3x3& operator+=(const b3Matrix3x3& m);
  140. /** @brief Substractss by the target matrix on the right
  141. * @param m matrix to be applied
  142. * Equivilant to this = this - m */
  143. b3Matrix3x3& operator-=(const b3Matrix3x3& m);
  144. /** @brief Set from the rotational part of a 4x4 OpenGL matrix
  145. * @param m A pointer to the beginning of the array of scalars*/
  146. void setFromOpenGLSubMatrix(const b3Scalar* m)
  147. {
  148. m_el[0].setValue(m[0], m[4], m[8]);
  149. m_el[1].setValue(m[1], m[5], m[9]);
  150. m_el[2].setValue(m[2], m[6], m[10]);
  151. }
  152. /** @brief Set the values of the matrix explicitly (row major)
  153. * @param xx Top left
  154. * @param xy Top Middle
  155. * @param xz Top Right
  156. * @param yx Middle Left
  157. * @param yy Middle Middle
  158. * @param yz Middle Right
  159. * @param zx Bottom Left
  160. * @param zy Bottom Middle
  161. * @param zz Bottom Right*/
  162. void setValue(const b3Scalar& xx, const b3Scalar& xy, const b3Scalar& xz,
  163. const b3Scalar& yx, const b3Scalar& yy, const b3Scalar& yz,
  164. const b3Scalar& zx, const b3Scalar& zy, const b3Scalar& zz)
  165. {
  166. m_el[0].setValue(xx, xy, xz);
  167. m_el[1].setValue(yx, yy, yz);
  168. m_el[2].setValue(zx, zy, zz);
  169. }
  170. /** @brief Set the matrix from a quaternion
  171. * @param q The Quaternion to match */
  172. void setRotation(const b3Quaternion& q)
  173. {
  174. b3Scalar d = q.length2();
  175. b3FullAssert(d != b3Scalar(0.0));
  176. b3Scalar s = b3Scalar(2.0) / d;
  177. #if defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)
  178. __m128 vs, Q = q.get128();
  179. __m128i Qi = b3CastfTo128i(Q);
  180. __m128 Y, Z;
  181. __m128 V1, V2, V3;
  182. __m128 V11, V21, V31;
  183. __m128 NQ = _mm_xor_ps(Q, b3vMzeroMask);
  184. __m128i NQi = b3CastfTo128i(NQ);
  185. V1 = b3CastiTo128f(_mm_shuffle_epi32(Qi, B3_SHUFFLE(1, 0, 2, 3))); // Y X Z W
  186. V2 = _mm_shuffle_ps(NQ, Q, B3_SHUFFLE(0, 0, 1, 3)); // -X -X Y W
  187. V3 = b3CastiTo128f(_mm_shuffle_epi32(Qi, B3_SHUFFLE(2, 1, 0, 3))); // Z Y X W
  188. V1 = _mm_xor_ps(V1, b3vMPPP); // change the sign of the first element
  189. V11 = b3CastiTo128f(_mm_shuffle_epi32(Qi, B3_SHUFFLE(1, 1, 0, 3))); // Y Y X W
  190. V21 = _mm_unpackhi_ps(Q, Q); // Z Z W W
  191. V31 = _mm_shuffle_ps(Q, NQ, B3_SHUFFLE(0, 2, 0, 3)); // X Z -X -W
  192. V2 = V2 * V1; //
  193. V1 = V1 * V11; //
  194. V3 = V3 * V31; //
  195. V11 = _mm_shuffle_ps(NQ, Q, B3_SHUFFLE(2, 3, 1, 3)); // -Z -W Y W
  196. V11 = V11 * V21; //
  197. V21 = _mm_xor_ps(V21, b3vMPPP); // change the sign of the first element
  198. V31 = _mm_shuffle_ps(Q, NQ, B3_SHUFFLE(3, 3, 1, 3)); // W W -Y -W
  199. V31 = _mm_xor_ps(V31, b3vMPPP); // change the sign of the first element
  200. Y = b3CastiTo128f(_mm_shuffle_epi32(NQi, B3_SHUFFLE(3, 2, 0, 3))); // -W -Z -X -W
  201. Z = b3CastiTo128f(_mm_shuffle_epi32(Qi, B3_SHUFFLE(1, 0, 1, 3))); // Y X Y W
  202. vs = _mm_load_ss(&s);
  203. V21 = V21 * Y;
  204. V31 = V31 * Z;
  205. V1 = V1 + V11;
  206. V2 = V2 + V21;
  207. V3 = V3 + V31;
  208. vs = b3_splat3_ps(vs, 0);
  209. // s ready
  210. V1 = V1 * vs;
  211. V2 = V2 * vs;
  212. V3 = V3 * vs;
  213. V1 = V1 + b3v1000;
  214. V2 = V2 + b3v0100;
  215. V3 = V3 + b3v0010;
  216. m_el[0] = b3MakeVector3(V1);
  217. m_el[1] = b3MakeVector3(V2);
  218. m_el[2] = b3MakeVector3(V3);
  219. #else
  220. b3Scalar xs = q.getX() * s, ys = q.getY() * s, zs = q.getZ() * s;
  221. b3Scalar wx = q.getW() * xs, wy = q.getW() * ys, wz = q.getW() * zs;
  222. b3Scalar xx = q.getX() * xs, xy = q.getX() * ys, xz = q.getX() * zs;
  223. b3Scalar yy = q.getY() * ys, yz = q.getY() * zs, zz = q.getZ() * zs;
  224. setValue(
  225. b3Scalar(1.0) - (yy + zz), xy - wz, xz + wy,
  226. xy + wz, b3Scalar(1.0) - (xx + zz), yz - wx,
  227. xz - wy, yz + wx, b3Scalar(1.0) - (xx + yy));
  228. #endif
  229. }
  230. /** @brief Set the matrix from euler angles using YPR around YXZ respectively
  231. * @param yaw Yaw about Y axis
  232. * @param pitch Pitch about X axis
  233. * @param roll Roll about Z axis
  234. */
  235. void setEulerYPR(const b3Scalar& yaw, const b3Scalar& pitch, const b3Scalar& roll)
  236. {
  237. setEulerZYX(roll, pitch, yaw);
  238. }
  239. /** @brief Set the matrix from euler angles YPR around ZYX axes
  240. * @param eulerX Roll about X axis
  241. * @param eulerY Pitch around Y axis
  242. * @param eulerZ Yaw aboud Z axis
  243. *
  244. * These angles are used to produce a rotation matrix. The euler
  245. * angles are applied in ZYX order. I.e a vector is first rotated
  246. * about X then Y and then Z
  247. **/
  248. void setEulerZYX(b3Scalar eulerX, b3Scalar eulerY, b3Scalar eulerZ)
  249. {
  250. ///@todo proposed to reverse this since it's labeled zyx but takes arguments xyz and it will match all other parts of the code
  251. b3Scalar ci(b3Cos(eulerX));
  252. b3Scalar cj(b3Cos(eulerY));
  253. b3Scalar ch(b3Cos(eulerZ));
  254. b3Scalar si(b3Sin(eulerX));
  255. b3Scalar sj(b3Sin(eulerY));
  256. b3Scalar sh(b3Sin(eulerZ));
  257. b3Scalar cc = ci * ch;
  258. b3Scalar cs = ci * sh;
  259. b3Scalar sc = si * ch;
  260. b3Scalar ss = si * sh;
  261. setValue(cj * ch, sj * sc - cs, sj * cc + ss,
  262. cj * sh, sj * ss + cc, sj * cs - sc,
  263. -sj, cj * si, cj * ci);
  264. }
  265. /**@brief Set the matrix to the identity */
  266. void setIdentity()
  267. {
  268. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  269. m_el[0] = b3MakeVector3(b3v1000);
  270. m_el[1] = b3MakeVector3(b3v0100);
  271. m_el[2] = b3MakeVector3(b3v0010);
  272. #else
  273. setValue(b3Scalar(1.0), b3Scalar(0.0), b3Scalar(0.0),
  274. b3Scalar(0.0), b3Scalar(1.0), b3Scalar(0.0),
  275. b3Scalar(0.0), b3Scalar(0.0), b3Scalar(1.0));
  276. #endif
  277. }
  278. static const b3Matrix3x3& getIdentity()
  279. {
  280. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  281. static const b3Matrix3x3
  282. identityMatrix(b3v1000, b3v0100, b3v0010);
  283. #else
  284. static const b3Matrix3x3
  285. identityMatrix(
  286. b3Scalar(1.0), b3Scalar(0.0), b3Scalar(0.0),
  287. b3Scalar(0.0), b3Scalar(1.0), b3Scalar(0.0),
  288. b3Scalar(0.0), b3Scalar(0.0), b3Scalar(1.0));
  289. #endif
  290. return identityMatrix;
  291. }
  292. /**@brief Fill the rotational part of an OpenGL matrix and clear the shear/perspective
  293. * @param m The array to be filled */
  294. void getOpenGLSubMatrix(b3Scalar * m) const
  295. {
  296. #if defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)
  297. __m128 v0 = m_el[0].mVec128;
  298. __m128 v1 = m_el[1].mVec128;
  299. __m128 v2 = m_el[2].mVec128; // x2 y2 z2 w2
  300. __m128* vm = (__m128*)m;
  301. __m128 vT;
  302. v2 = _mm_and_ps(v2, b3vFFF0fMask); // x2 y2 z2 0
  303. vT = _mm_unpackhi_ps(v0, v1); // z0 z1 * *
  304. v0 = _mm_unpacklo_ps(v0, v1); // x0 x1 y0 y1
  305. v1 = _mm_shuffle_ps(v0, v2, B3_SHUFFLE(2, 3, 1, 3)); // y0 y1 y2 0
  306. v0 = _mm_shuffle_ps(v0, v2, B3_SHUFFLE(0, 1, 0, 3)); // x0 x1 x2 0
  307. v2 = b3CastdTo128f(_mm_move_sd(b3CastfTo128d(v2), b3CastfTo128d(vT))); // z0 z1 z2 0
  308. vm[0] = v0;
  309. vm[1] = v1;
  310. vm[2] = v2;
  311. #elif defined(B3_USE_NEON)
  312. // note: zeros the w channel. We can preserve it at the cost of two more vtrn instructions.
  313. static const uint32x2_t zMask = (const uint32x2_t){-1, 0};
  314. float32x4_t* vm = (float32x4_t*)m;
  315. float32x4x2_t top = vtrnq_f32(m_el[0].mVec128, m_el[1].mVec128); // {x0 x1 z0 z1}, {y0 y1 w0 w1}
  316. float32x2x2_t bl = vtrn_f32(vget_low_f32(m_el[2].mVec128), vdup_n_f32(0.0f)); // {x2 0 }, {y2 0}
  317. float32x4_t v0 = vcombine_f32(vget_low_f32(top.val[0]), bl.val[0]);
  318. float32x4_t v1 = vcombine_f32(vget_low_f32(top.val[1]), bl.val[1]);
  319. float32x2_t q = (float32x2_t)vand_u32((uint32x2_t)vget_high_f32(m_el[2].mVec128), zMask);
  320. float32x4_t v2 = vcombine_f32(vget_high_f32(top.val[0]), q); // z0 z1 z2 0
  321. vm[0] = v0;
  322. vm[1] = v1;
  323. vm[2] = v2;
  324. #else
  325. m[0] = b3Scalar(m_el[0].getX());
  326. m[1] = b3Scalar(m_el[1].getX());
  327. m[2] = b3Scalar(m_el[2].getX());
  328. m[3] = b3Scalar(0.0);
  329. m[4] = b3Scalar(m_el[0].getY());
  330. m[5] = b3Scalar(m_el[1].getY());
  331. m[6] = b3Scalar(m_el[2].getY());
  332. m[7] = b3Scalar(0.0);
  333. m[8] = b3Scalar(m_el[0].getZ());
  334. m[9] = b3Scalar(m_el[1].getZ());
  335. m[10] = b3Scalar(m_el[2].getZ());
  336. m[11] = b3Scalar(0.0);
  337. #endif
  338. }
  339. /**@brief Get the matrix represented as a quaternion
  340. * @param q The quaternion which will be set */
  341. void getRotation(b3Quaternion & q) const
  342. {
  343. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  344. b3Scalar trace = m_el[0].getX() + m_el[1].getY() + m_el[2].getZ();
  345. b3Scalar s, x;
  346. union {
  347. b3SimdFloat4 vec;
  348. b3Scalar f[4];
  349. } temp;
  350. if (trace > b3Scalar(0.0))
  351. {
  352. x = trace + b3Scalar(1.0);
  353. temp.f[0] = m_el[2].getY() - m_el[1].getZ();
  354. temp.f[1] = m_el[0].getZ() - m_el[2].getX();
  355. temp.f[2] = m_el[1].getX() - m_el[0].getY();
  356. temp.f[3] = x;
  357. //temp.f[3]= s * b3Scalar(0.5);
  358. }
  359. else
  360. {
  361. int i, j, k;
  362. if (m_el[0].getX() < m_el[1].getY())
  363. {
  364. if (m_el[1].getY() < m_el[2].getZ())
  365. {
  366. i = 2;
  367. j = 0;
  368. k = 1;
  369. }
  370. else
  371. {
  372. i = 1;
  373. j = 2;
  374. k = 0;
  375. }
  376. }
  377. else
  378. {
  379. if (m_el[0].getX() < m_el[2].getZ())
  380. {
  381. i = 2;
  382. j = 0;
  383. k = 1;
  384. }
  385. else
  386. {
  387. i = 0;
  388. j = 1;
  389. k = 2;
  390. }
  391. }
  392. x = m_el[i][i] - m_el[j][j] - m_el[k][k] + b3Scalar(1.0);
  393. temp.f[3] = (m_el[k][j] - m_el[j][k]);
  394. temp.f[j] = (m_el[j][i] + m_el[i][j]);
  395. temp.f[k] = (m_el[k][i] + m_el[i][k]);
  396. temp.f[i] = x;
  397. //temp.f[i] = s * b3Scalar(0.5);
  398. }
  399. s = b3Sqrt(x);
  400. q.set128(temp.vec);
  401. s = b3Scalar(0.5) / s;
  402. q *= s;
  403. #else
  404. b3Scalar trace = m_el[0].getX() + m_el[1].getY() + m_el[2].getZ();
  405. b3Scalar temp[4];
  406. if (trace > b3Scalar(0.0))
  407. {
  408. b3Scalar s = b3Sqrt(trace + b3Scalar(1.0));
  409. temp[3] = (s * b3Scalar(0.5));
  410. s = b3Scalar(0.5) / s;
  411. temp[0] = ((m_el[2].getY() - m_el[1].getZ()) * s);
  412. temp[1] = ((m_el[0].getZ() - m_el[2].getX()) * s);
  413. temp[2] = ((m_el[1].getX() - m_el[0].getY()) * s);
  414. }
  415. else
  416. {
  417. int i = m_el[0].getX() < m_el[1].getY() ? (m_el[1].getY() < m_el[2].getZ() ? 2 : 1) : (m_el[0].getX() < m_el[2].getZ() ? 2 : 0);
  418. int j = (i + 1) % 3;
  419. int k = (i + 2) % 3;
  420. b3Scalar s = b3Sqrt(m_el[i][i] - m_el[j][j] - m_el[k][k] + b3Scalar(1.0));
  421. temp[i] = s * b3Scalar(0.5);
  422. s = b3Scalar(0.5) / s;
  423. temp[3] = (m_el[k][j] - m_el[j][k]) * s;
  424. temp[j] = (m_el[j][i] + m_el[i][j]) * s;
  425. temp[k] = (m_el[k][i] + m_el[i][k]) * s;
  426. }
  427. q.setValue(temp[0], temp[1], temp[2], temp[3]);
  428. #endif
  429. }
  430. /**@brief Get the matrix represented as euler angles around YXZ, roundtrip with setEulerYPR
  431. * @param yaw Yaw around Y axis
  432. * @param pitch Pitch around X axis
  433. * @param roll around Z axis */
  434. void getEulerYPR(b3Scalar & yaw, b3Scalar & pitch, b3Scalar & roll) const
  435. {
  436. // first use the normal calculus
  437. yaw = b3Scalar(b3Atan2(m_el[1].getX(), m_el[0].getX()));
  438. pitch = b3Scalar(b3Asin(-m_el[2].getX()));
  439. roll = b3Scalar(b3Atan2(m_el[2].getY(), m_el[2].getZ()));
  440. // on pitch = +/-HalfPI
  441. if (b3Fabs(pitch) == B3_HALF_PI)
  442. {
  443. if (yaw > 0)
  444. yaw -= B3_PI;
  445. else
  446. yaw += B3_PI;
  447. if (roll > 0)
  448. roll -= B3_PI;
  449. else
  450. roll += B3_PI;
  451. }
  452. };
  453. /**@brief Get the matrix represented as euler angles around ZYX
  454. * @param yaw Yaw around X axis
  455. * @param pitch Pitch around Y axis
  456. * @param roll around X axis
  457. * @param solution_number Which solution of two possible solutions ( 1 or 2) are possible values*/
  458. void getEulerZYX(b3Scalar & yaw, b3Scalar & pitch, b3Scalar & roll, unsigned int solution_number = 1) const
  459. {
  460. struct Euler
  461. {
  462. b3Scalar yaw;
  463. b3Scalar pitch;
  464. b3Scalar roll;
  465. };
  466. Euler euler_out;
  467. Euler euler_out2; //second solution
  468. //get the pointer to the raw data
  469. // Check that pitch is not at a singularity
  470. if (b3Fabs(m_el[2].getX()) >= 1)
  471. {
  472. euler_out.yaw = 0;
  473. euler_out2.yaw = 0;
  474. // From difference of angles formula
  475. b3Scalar delta = b3Atan2(m_el[0].getX(), m_el[0].getZ());
  476. if (m_el[2].getX() > 0) //gimbal locked up
  477. {
  478. euler_out.pitch = B3_PI / b3Scalar(2.0);
  479. euler_out2.pitch = B3_PI / b3Scalar(2.0);
  480. euler_out.roll = euler_out.pitch + delta;
  481. euler_out2.roll = euler_out.pitch + delta;
  482. }
  483. else // gimbal locked down
  484. {
  485. euler_out.pitch = -B3_PI / b3Scalar(2.0);
  486. euler_out2.pitch = -B3_PI / b3Scalar(2.0);
  487. euler_out.roll = -euler_out.pitch + delta;
  488. euler_out2.roll = -euler_out.pitch + delta;
  489. }
  490. }
  491. else
  492. {
  493. euler_out.pitch = -b3Asin(m_el[2].getX());
  494. euler_out2.pitch = B3_PI - euler_out.pitch;
  495. euler_out.roll = b3Atan2(m_el[2].getY() / b3Cos(euler_out.pitch),
  496. m_el[2].getZ() / b3Cos(euler_out.pitch));
  497. euler_out2.roll = b3Atan2(m_el[2].getY() / b3Cos(euler_out2.pitch),
  498. m_el[2].getZ() / b3Cos(euler_out2.pitch));
  499. euler_out.yaw = b3Atan2(m_el[1].getX() / b3Cos(euler_out.pitch),
  500. m_el[0].getX() / b3Cos(euler_out.pitch));
  501. euler_out2.yaw = b3Atan2(m_el[1].getX() / b3Cos(euler_out2.pitch),
  502. m_el[0].getX() / b3Cos(euler_out2.pitch));
  503. }
  504. if (solution_number == 1)
  505. {
  506. yaw = euler_out.yaw;
  507. pitch = euler_out.pitch;
  508. roll = euler_out.roll;
  509. }
  510. else
  511. {
  512. yaw = euler_out2.yaw;
  513. pitch = euler_out2.pitch;
  514. roll = euler_out2.roll;
  515. }
  516. }
  517. /**@brief Create a scaled copy of the matrix
  518. * @param s Scaling vector The elements of the vector will scale each column */
  519. b3Matrix3x3 scaled(const b3Vector3& s) const
  520. {
  521. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  522. return b3Matrix3x3(m_el[0] * s, m_el[1] * s, m_el[2] * s);
  523. #else
  524. return b3Matrix3x3(
  525. m_el[0].getX() * s.getX(), m_el[0].getY() * s.getY(), m_el[0].getZ() * s.getZ(),
  526. m_el[1].getX() * s.getX(), m_el[1].getY() * s.getY(), m_el[1].getZ() * s.getZ(),
  527. m_el[2].getX() * s.getX(), m_el[2].getY() * s.getY(), m_el[2].getZ() * s.getZ());
  528. #endif
  529. }
  530. /**@brief Return the determinant of the matrix */
  531. b3Scalar determinant() const;
  532. /**@brief Return the adjoint of the matrix */
  533. b3Matrix3x3 adjoint() const;
  534. /**@brief Return the matrix with all values non negative */
  535. b3Matrix3x3 absolute() const;
  536. /**@brief Return the transpose of the matrix */
  537. b3Matrix3x3 transpose() const;
  538. /**@brief Return the inverse of the matrix */
  539. b3Matrix3x3 inverse() const;
  540. b3Matrix3x3 transposeTimes(const b3Matrix3x3& m) const;
  541. b3Matrix3x3 timesTranspose(const b3Matrix3x3& m) const;
  542. B3_FORCE_INLINE b3Scalar tdotx(const b3Vector3& v) const
  543. {
  544. return m_el[0].getX() * v.getX() + m_el[1].getX() * v.getY() + m_el[2].getX() * v.getZ();
  545. }
  546. B3_FORCE_INLINE b3Scalar tdoty(const b3Vector3& v) const
  547. {
  548. return m_el[0].getY() * v.getX() + m_el[1].getY() * v.getY() + m_el[2].getY() * v.getZ();
  549. }
  550. B3_FORCE_INLINE b3Scalar tdotz(const b3Vector3& v) const
  551. {
  552. return m_el[0].getZ() * v.getX() + m_el[1].getZ() * v.getY() + m_el[2].getZ() * v.getZ();
  553. }
  554. /**@brief diagonalizes this matrix by the Jacobi method.
  555. * @param rot stores the rotation from the coordinate system in which the matrix is diagonal to the original
  556. * coordinate system, i.e., old_this = rot * new_this * rot^T.
  557. * @param threshold See iteration
  558. * @param iteration The iteration stops when all off-diagonal elements are less than the threshold multiplied
  559. * by the sum of the absolute values of the diagonal, or when maxSteps have been executed.
  560. *
  561. * Note that this matrix is assumed to be symmetric.
  562. */
  563. void diagonalize(b3Matrix3x3 & rot, b3Scalar threshold, int maxSteps)
  564. {
  565. rot.setIdentity();
  566. for (int step = maxSteps; step > 0; step--)
  567. {
  568. // find off-diagonal element [p][q] with largest magnitude
  569. int p = 0;
  570. int q = 1;
  571. int r = 2;
  572. b3Scalar max = b3Fabs(m_el[0][1]);
  573. b3Scalar v = b3Fabs(m_el[0][2]);
  574. if (v > max)
  575. {
  576. q = 2;
  577. r = 1;
  578. max = v;
  579. }
  580. v = b3Fabs(m_el[1][2]);
  581. if (v > max)
  582. {
  583. p = 1;
  584. q = 2;
  585. r = 0;
  586. max = v;
  587. }
  588. b3Scalar t = threshold * (b3Fabs(m_el[0][0]) + b3Fabs(m_el[1][1]) + b3Fabs(m_el[2][2]));
  589. if (max <= t)
  590. {
  591. if (max <= B3_EPSILON * t)
  592. {
  593. return;
  594. }
  595. step = 1;
  596. }
  597. // compute Jacobi rotation J which leads to a zero for element [p][q]
  598. b3Scalar mpq = m_el[p][q];
  599. b3Scalar theta = (m_el[q][q] - m_el[p][p]) / (2 * mpq);
  600. b3Scalar theta2 = theta * theta;
  601. b3Scalar cos;
  602. b3Scalar sin;
  603. if (theta2 * theta2 < b3Scalar(10 / B3_EPSILON))
  604. {
  605. t = (theta >= 0) ? 1 / (theta + b3Sqrt(1 + theta2))
  606. : 1 / (theta - b3Sqrt(1 + theta2));
  607. cos = 1 / b3Sqrt(1 + t * t);
  608. sin = cos * t;
  609. }
  610. else
  611. {
  612. // approximation for large theta-value, i.e., a nearly diagonal matrix
  613. t = 1 / (theta * (2 + b3Scalar(0.5) / theta2));
  614. cos = 1 - b3Scalar(0.5) * t * t;
  615. sin = cos * t;
  616. }
  617. // apply rotation to matrix (this = J^T * this * J)
  618. m_el[p][q] = m_el[q][p] = 0;
  619. m_el[p][p] -= t * mpq;
  620. m_el[q][q] += t * mpq;
  621. b3Scalar mrp = m_el[r][p];
  622. b3Scalar mrq = m_el[r][q];
  623. m_el[r][p] = m_el[p][r] = cos * mrp - sin * mrq;
  624. m_el[r][q] = m_el[q][r] = cos * mrq + sin * mrp;
  625. // apply rotation to rot (rot = rot * J)
  626. for (int i = 0; i < 3; i++)
  627. {
  628. b3Vector3& row = rot[i];
  629. mrp = row[p];
  630. mrq = row[q];
  631. row[p] = cos * mrp - sin * mrq;
  632. row[q] = cos * mrq + sin * mrp;
  633. }
  634. }
  635. }
  636. /**@brief Calculate the matrix cofactor
  637. * @param r1 The first row to use for calculating the cofactor
  638. * @param c1 The first column to use for calculating the cofactor
  639. * @param r1 The second row to use for calculating the cofactor
  640. * @param c1 The second column to use for calculating the cofactor
  641. * See http://en.wikipedia.org/wiki/Cofactor_(linear_algebra) for more details
  642. */
  643. b3Scalar cofac(int r1, int c1, int r2, int c2) const
  644. {
  645. return m_el[r1][c1] * m_el[r2][c2] - m_el[r1][c2] * m_el[r2][c1];
  646. }
  647. void serialize(struct b3Matrix3x3Data & dataOut) const;
  648. void serializeFloat(struct b3Matrix3x3FloatData & dataOut) const;
  649. void deSerialize(const struct b3Matrix3x3Data& dataIn);
  650. void deSerializeFloat(const struct b3Matrix3x3FloatData& dataIn);
  651. void deSerializeDouble(const struct b3Matrix3x3DoubleData& dataIn);
  652. };
  653. B3_FORCE_INLINE b3Matrix3x3&
  654. b3Matrix3x3::operator*=(const b3Matrix3x3& m)
  655. {
  656. #if defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)
  657. __m128 rv00, rv01, rv02;
  658. __m128 rv10, rv11, rv12;
  659. __m128 rv20, rv21, rv22;
  660. __m128 mv0, mv1, mv2;
  661. rv02 = m_el[0].mVec128;
  662. rv12 = m_el[1].mVec128;
  663. rv22 = m_el[2].mVec128;
  664. mv0 = _mm_and_ps(m[0].mVec128, b3vFFF0fMask);
  665. mv1 = _mm_and_ps(m[1].mVec128, b3vFFF0fMask);
  666. mv2 = _mm_and_ps(m[2].mVec128, b3vFFF0fMask);
  667. // rv0
  668. rv00 = b3_splat_ps(rv02, 0);
  669. rv01 = b3_splat_ps(rv02, 1);
  670. rv02 = b3_splat_ps(rv02, 2);
  671. rv00 = _mm_mul_ps(rv00, mv0);
  672. rv01 = _mm_mul_ps(rv01, mv1);
  673. rv02 = _mm_mul_ps(rv02, mv2);
  674. // rv1
  675. rv10 = b3_splat_ps(rv12, 0);
  676. rv11 = b3_splat_ps(rv12, 1);
  677. rv12 = b3_splat_ps(rv12, 2);
  678. rv10 = _mm_mul_ps(rv10, mv0);
  679. rv11 = _mm_mul_ps(rv11, mv1);
  680. rv12 = _mm_mul_ps(rv12, mv2);
  681. // rv2
  682. rv20 = b3_splat_ps(rv22, 0);
  683. rv21 = b3_splat_ps(rv22, 1);
  684. rv22 = b3_splat_ps(rv22, 2);
  685. rv20 = _mm_mul_ps(rv20, mv0);
  686. rv21 = _mm_mul_ps(rv21, mv1);
  687. rv22 = _mm_mul_ps(rv22, mv2);
  688. rv00 = _mm_add_ps(rv00, rv01);
  689. rv10 = _mm_add_ps(rv10, rv11);
  690. rv20 = _mm_add_ps(rv20, rv21);
  691. m_el[0].mVec128 = _mm_add_ps(rv00, rv02);
  692. m_el[1].mVec128 = _mm_add_ps(rv10, rv12);
  693. m_el[2].mVec128 = _mm_add_ps(rv20, rv22);
  694. #elif defined(B3_USE_NEON)
  695. float32x4_t rv0, rv1, rv2;
  696. float32x4_t v0, v1, v2;
  697. float32x4_t mv0, mv1, mv2;
  698. v0 = m_el[0].mVec128;
  699. v1 = m_el[1].mVec128;
  700. v2 = m_el[2].mVec128;
  701. mv0 = (float32x4_t)vandq_s32((int32x4_t)m[0].mVec128, b3vFFF0Mask);
  702. mv1 = (float32x4_t)vandq_s32((int32x4_t)m[1].mVec128, b3vFFF0Mask);
  703. mv2 = (float32x4_t)vandq_s32((int32x4_t)m[2].mVec128, b3vFFF0Mask);
  704. rv0 = vmulq_lane_f32(mv0, vget_low_f32(v0), 0);
  705. rv1 = vmulq_lane_f32(mv0, vget_low_f32(v1), 0);
  706. rv2 = vmulq_lane_f32(mv0, vget_low_f32(v2), 0);
  707. rv0 = vmlaq_lane_f32(rv0, mv1, vget_low_f32(v0), 1);
  708. rv1 = vmlaq_lane_f32(rv1, mv1, vget_low_f32(v1), 1);
  709. rv2 = vmlaq_lane_f32(rv2, mv1, vget_low_f32(v2), 1);
  710. rv0 = vmlaq_lane_f32(rv0, mv2, vget_high_f32(v0), 0);
  711. rv1 = vmlaq_lane_f32(rv1, mv2, vget_high_f32(v1), 0);
  712. rv2 = vmlaq_lane_f32(rv2, mv2, vget_high_f32(v2), 0);
  713. m_el[0].mVec128 = rv0;
  714. m_el[1].mVec128 = rv1;
  715. m_el[2].mVec128 = rv2;
  716. #else
  717. setValue(
  718. m.tdotx(m_el[0]), m.tdoty(m_el[0]), m.tdotz(m_el[0]),
  719. m.tdotx(m_el[1]), m.tdoty(m_el[1]), m.tdotz(m_el[1]),
  720. m.tdotx(m_el[2]), m.tdoty(m_el[2]), m.tdotz(m_el[2]));
  721. #endif
  722. return *this;
  723. }
  724. B3_FORCE_INLINE b3Matrix3x3&
  725. b3Matrix3x3::operator+=(const b3Matrix3x3& m)
  726. {
  727. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  728. m_el[0].mVec128 = m_el[0].mVec128 + m.m_el[0].mVec128;
  729. m_el[1].mVec128 = m_el[1].mVec128 + m.m_el[1].mVec128;
  730. m_el[2].mVec128 = m_el[2].mVec128 + m.m_el[2].mVec128;
  731. #else
  732. setValue(
  733. m_el[0][0] + m.m_el[0][0],
  734. m_el[0][1] + m.m_el[0][1],
  735. m_el[0][2] + m.m_el[0][2],
  736. m_el[1][0] + m.m_el[1][0],
  737. m_el[1][1] + m.m_el[1][1],
  738. m_el[1][2] + m.m_el[1][2],
  739. m_el[2][0] + m.m_el[2][0],
  740. m_el[2][1] + m.m_el[2][1],
  741. m_el[2][2] + m.m_el[2][2]);
  742. #endif
  743. return *this;
  744. }
  745. B3_FORCE_INLINE b3Matrix3x3
  746. operator*(const b3Matrix3x3& m, const b3Scalar& k)
  747. {
  748. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  749. __m128 vk = b3_splat_ps(_mm_load_ss((float*)&k), 0x80);
  750. return b3Matrix3x3(
  751. _mm_mul_ps(m[0].mVec128, vk),
  752. _mm_mul_ps(m[1].mVec128, vk),
  753. _mm_mul_ps(m[2].mVec128, vk));
  754. #elif defined(B3_USE_NEON)
  755. return b3Matrix3x3(
  756. vmulq_n_f32(m[0].mVec128, k),
  757. vmulq_n_f32(m[1].mVec128, k),
  758. vmulq_n_f32(m[2].mVec128, k));
  759. #else
  760. return b3Matrix3x3(
  761. m[0].getX() * k, m[0].getY() * k, m[0].getZ() * k,
  762. m[1].getX() * k, m[1].getY() * k, m[1].getZ() * k,
  763. m[2].getX() * k, m[2].getY() * k, m[2].getZ() * k);
  764. #endif
  765. }
  766. B3_FORCE_INLINE b3Matrix3x3
  767. operator+(const b3Matrix3x3& m1, const b3Matrix3x3& m2)
  768. {
  769. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  770. return b3Matrix3x3(
  771. m1[0].mVec128 + m2[0].mVec128,
  772. m1[1].mVec128 + m2[1].mVec128,
  773. m1[2].mVec128 + m2[2].mVec128);
  774. #else
  775. return b3Matrix3x3(
  776. m1[0][0] + m2[0][0],
  777. m1[0][1] + m2[0][1],
  778. m1[0][2] + m2[0][2],
  779. m1[1][0] + m2[1][0],
  780. m1[1][1] + m2[1][1],
  781. m1[1][2] + m2[1][2],
  782. m1[2][0] + m2[2][0],
  783. m1[2][1] + m2[2][1],
  784. m1[2][2] + m2[2][2]);
  785. #endif
  786. }
  787. B3_FORCE_INLINE b3Matrix3x3
  788. operator-(const b3Matrix3x3& m1, const b3Matrix3x3& m2)
  789. {
  790. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  791. return b3Matrix3x3(
  792. m1[0].mVec128 - m2[0].mVec128,
  793. m1[1].mVec128 - m2[1].mVec128,
  794. m1[2].mVec128 - m2[2].mVec128);
  795. #else
  796. return b3Matrix3x3(
  797. m1[0][0] - m2[0][0],
  798. m1[0][1] - m2[0][1],
  799. m1[0][2] - m2[0][2],
  800. m1[1][0] - m2[1][0],
  801. m1[1][1] - m2[1][1],
  802. m1[1][2] - m2[1][2],
  803. m1[2][0] - m2[2][0],
  804. m1[2][1] - m2[2][1],
  805. m1[2][2] - m2[2][2]);
  806. #endif
  807. }
  808. B3_FORCE_INLINE b3Matrix3x3&
  809. b3Matrix3x3::operator-=(const b3Matrix3x3& m)
  810. {
  811. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  812. m_el[0].mVec128 = m_el[0].mVec128 - m.m_el[0].mVec128;
  813. m_el[1].mVec128 = m_el[1].mVec128 - m.m_el[1].mVec128;
  814. m_el[2].mVec128 = m_el[2].mVec128 - m.m_el[2].mVec128;
  815. #else
  816. setValue(
  817. m_el[0][0] - m.m_el[0][0],
  818. m_el[0][1] - m.m_el[0][1],
  819. m_el[0][2] - m.m_el[0][2],
  820. m_el[1][0] - m.m_el[1][0],
  821. m_el[1][1] - m.m_el[1][1],
  822. m_el[1][2] - m.m_el[1][2],
  823. m_el[2][0] - m.m_el[2][0],
  824. m_el[2][1] - m.m_el[2][1],
  825. m_el[2][2] - m.m_el[2][2]);
  826. #endif
  827. return *this;
  828. }
  829. B3_FORCE_INLINE b3Scalar
  830. b3Matrix3x3::determinant() const
  831. {
  832. return b3Triple((*this)[0], (*this)[1], (*this)[2]);
  833. }
  834. B3_FORCE_INLINE b3Matrix3x3
  835. b3Matrix3x3::absolute() const
  836. {
  837. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  838. return b3Matrix3x3(
  839. _mm_and_ps(m_el[0].mVec128, b3vAbsfMask),
  840. _mm_and_ps(m_el[1].mVec128, b3vAbsfMask),
  841. _mm_and_ps(m_el[2].mVec128, b3vAbsfMask));
  842. #elif defined(B3_USE_NEON)
  843. return b3Matrix3x3(
  844. (float32x4_t)vandq_s32((int32x4_t)m_el[0].mVec128, b3v3AbsMask),
  845. (float32x4_t)vandq_s32((int32x4_t)m_el[1].mVec128, b3v3AbsMask),
  846. (float32x4_t)vandq_s32((int32x4_t)m_el[2].mVec128, b3v3AbsMask));
  847. #else
  848. return b3Matrix3x3(
  849. b3Fabs(m_el[0].getX()), b3Fabs(m_el[0].getY()), b3Fabs(m_el[0].getZ()),
  850. b3Fabs(m_el[1].getX()), b3Fabs(m_el[1].getY()), b3Fabs(m_el[1].getZ()),
  851. b3Fabs(m_el[2].getX()), b3Fabs(m_el[2].getY()), b3Fabs(m_el[2].getZ()));
  852. #endif
  853. }
  854. B3_FORCE_INLINE b3Matrix3x3
  855. b3Matrix3x3::transpose() const
  856. {
  857. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  858. __m128 v0 = m_el[0].mVec128;
  859. __m128 v1 = m_el[1].mVec128;
  860. __m128 v2 = m_el[2].mVec128; // x2 y2 z2 w2
  861. __m128 vT;
  862. v2 = _mm_and_ps(v2, b3vFFF0fMask); // x2 y2 z2 0
  863. vT = _mm_unpackhi_ps(v0, v1); // z0 z1 * *
  864. v0 = _mm_unpacklo_ps(v0, v1); // x0 x1 y0 y1
  865. v1 = _mm_shuffle_ps(v0, v2, B3_SHUFFLE(2, 3, 1, 3)); // y0 y1 y2 0
  866. v0 = _mm_shuffle_ps(v0, v2, B3_SHUFFLE(0, 1, 0, 3)); // x0 x1 x2 0
  867. v2 = b3CastdTo128f(_mm_move_sd(b3CastfTo128d(v2), b3CastfTo128d(vT))); // z0 z1 z2 0
  868. return b3Matrix3x3(v0, v1, v2);
  869. #elif defined(B3_USE_NEON)
  870. // note: zeros the w channel. We can preserve it at the cost of two more vtrn instructions.
  871. static const uint32x2_t zMask = (const uint32x2_t){-1, 0};
  872. float32x4x2_t top = vtrnq_f32(m_el[0].mVec128, m_el[1].mVec128); // {x0 x1 z0 z1}, {y0 y1 w0 w1}
  873. float32x2x2_t bl = vtrn_f32(vget_low_f32(m_el[2].mVec128), vdup_n_f32(0.0f)); // {x2 0 }, {y2 0}
  874. float32x4_t v0 = vcombine_f32(vget_low_f32(top.val[0]), bl.val[0]);
  875. float32x4_t v1 = vcombine_f32(vget_low_f32(top.val[1]), bl.val[1]);
  876. float32x2_t q = (float32x2_t)vand_u32((uint32x2_t)vget_high_f32(m_el[2].mVec128), zMask);
  877. float32x4_t v2 = vcombine_f32(vget_high_f32(top.val[0]), q); // z0 z1 z2 0
  878. return b3Matrix3x3(v0, v1, v2);
  879. #else
  880. return b3Matrix3x3(m_el[0].getX(), m_el[1].getX(), m_el[2].getX(),
  881. m_el[0].getY(), m_el[1].getY(), m_el[2].getY(),
  882. m_el[0].getZ(), m_el[1].getZ(), m_el[2].getZ());
  883. #endif
  884. }
  885. B3_FORCE_INLINE b3Matrix3x3
  886. b3Matrix3x3::adjoint() const
  887. {
  888. return b3Matrix3x3(cofac(1, 1, 2, 2), cofac(0, 2, 2, 1), cofac(0, 1, 1, 2),
  889. cofac(1, 2, 2, 0), cofac(0, 0, 2, 2), cofac(0, 2, 1, 0),
  890. cofac(1, 0, 2, 1), cofac(0, 1, 2, 0), cofac(0, 0, 1, 1));
  891. }
  892. B3_FORCE_INLINE b3Matrix3x3
  893. b3Matrix3x3::inverse() const
  894. {
  895. b3Vector3 co = b3MakeVector3(cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1));
  896. b3Scalar det = (*this)[0].dot(co);
  897. b3FullAssert(det != b3Scalar(0.0));
  898. b3Scalar s = b3Scalar(1.0) / det;
  899. return b3Matrix3x3(co.getX() * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  900. co.getY() * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  901. co.getZ() * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  902. }
  903. B3_FORCE_INLINE b3Matrix3x3
  904. b3Matrix3x3::transposeTimes(const b3Matrix3x3& m) const
  905. {
  906. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  907. // zeros w
  908. // static const __m128i xyzMask = (const __m128i){ -1ULL, 0xffffffffULL };
  909. __m128 row = m_el[0].mVec128;
  910. __m128 m0 = _mm_and_ps(m.getRow(0).mVec128, b3vFFF0fMask);
  911. __m128 m1 = _mm_and_ps(m.getRow(1).mVec128, b3vFFF0fMask);
  912. __m128 m2 = _mm_and_ps(m.getRow(2).mVec128, b3vFFF0fMask);
  913. __m128 r0 = _mm_mul_ps(m0, _mm_shuffle_ps(row, row, 0));
  914. __m128 r1 = _mm_mul_ps(m0, _mm_shuffle_ps(row, row, 0x55));
  915. __m128 r2 = _mm_mul_ps(m0, _mm_shuffle_ps(row, row, 0xaa));
  916. row = m_el[1].mVec128;
  917. r0 = _mm_add_ps(r0, _mm_mul_ps(m1, _mm_shuffle_ps(row, row, 0)));
  918. r1 = _mm_add_ps(r1, _mm_mul_ps(m1, _mm_shuffle_ps(row, row, 0x55)));
  919. r2 = _mm_add_ps(r2, _mm_mul_ps(m1, _mm_shuffle_ps(row, row, 0xaa)));
  920. row = m_el[2].mVec128;
  921. r0 = _mm_add_ps(r0, _mm_mul_ps(m2, _mm_shuffle_ps(row, row, 0)));
  922. r1 = _mm_add_ps(r1, _mm_mul_ps(m2, _mm_shuffle_ps(row, row, 0x55)));
  923. r2 = _mm_add_ps(r2, _mm_mul_ps(m2, _mm_shuffle_ps(row, row, 0xaa)));
  924. return b3Matrix3x3(r0, r1, r2);
  925. #elif defined B3_USE_NEON
  926. // zeros w
  927. static const uint32x4_t xyzMask = (const uint32x4_t){-1, -1, -1, 0};
  928. float32x4_t m0 = (float32x4_t)vandq_u32((uint32x4_t)m.getRow(0).mVec128, xyzMask);
  929. float32x4_t m1 = (float32x4_t)vandq_u32((uint32x4_t)m.getRow(1).mVec128, xyzMask);
  930. float32x4_t m2 = (float32x4_t)vandq_u32((uint32x4_t)m.getRow(2).mVec128, xyzMask);
  931. float32x4_t row = m_el[0].mVec128;
  932. float32x4_t r0 = vmulq_lane_f32(m0, vget_low_f32(row), 0);
  933. float32x4_t r1 = vmulq_lane_f32(m0, vget_low_f32(row), 1);
  934. float32x4_t r2 = vmulq_lane_f32(m0, vget_high_f32(row), 0);
  935. row = m_el[1].mVec128;
  936. r0 = vmlaq_lane_f32(r0, m1, vget_low_f32(row), 0);
  937. r1 = vmlaq_lane_f32(r1, m1, vget_low_f32(row), 1);
  938. r2 = vmlaq_lane_f32(r2, m1, vget_high_f32(row), 0);
  939. row = m_el[2].mVec128;
  940. r0 = vmlaq_lane_f32(r0, m2, vget_low_f32(row), 0);
  941. r1 = vmlaq_lane_f32(r1, m2, vget_low_f32(row), 1);
  942. r2 = vmlaq_lane_f32(r2, m2, vget_high_f32(row), 0);
  943. return b3Matrix3x3(r0, r1, r2);
  944. #else
  945. return b3Matrix3x3(
  946. m_el[0].getX() * m[0].getX() + m_el[1].getX() * m[1].getX() + m_el[2].getX() * m[2].getX(),
  947. m_el[0].getX() * m[0].getY() + m_el[1].getX() * m[1].getY() + m_el[2].getX() * m[2].getY(),
  948. m_el[0].getX() * m[0].getZ() + m_el[1].getX() * m[1].getZ() + m_el[2].getX() * m[2].getZ(),
  949. m_el[0].getY() * m[0].getX() + m_el[1].getY() * m[1].getX() + m_el[2].getY() * m[2].getX(),
  950. m_el[0].getY() * m[0].getY() + m_el[1].getY() * m[1].getY() + m_el[2].getY() * m[2].getY(),
  951. m_el[0].getY() * m[0].getZ() + m_el[1].getY() * m[1].getZ() + m_el[2].getY() * m[2].getZ(),
  952. m_el[0].getZ() * m[0].getX() + m_el[1].getZ() * m[1].getX() + m_el[2].getZ() * m[2].getX(),
  953. m_el[0].getZ() * m[0].getY() + m_el[1].getZ() * m[1].getY() + m_el[2].getZ() * m[2].getY(),
  954. m_el[0].getZ() * m[0].getZ() + m_el[1].getZ() * m[1].getZ() + m_el[2].getZ() * m[2].getZ());
  955. #endif
  956. }
  957. B3_FORCE_INLINE b3Matrix3x3
  958. b3Matrix3x3::timesTranspose(const b3Matrix3x3& m) const
  959. {
  960. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  961. __m128 a0 = m_el[0].mVec128;
  962. __m128 a1 = m_el[1].mVec128;
  963. __m128 a2 = m_el[2].mVec128;
  964. b3Matrix3x3 mT = m.transpose(); // we rely on transpose() zeroing w channel so that we don't have to do it here
  965. __m128 mx = mT[0].mVec128;
  966. __m128 my = mT[1].mVec128;
  967. __m128 mz = mT[2].mVec128;
  968. __m128 r0 = _mm_mul_ps(mx, _mm_shuffle_ps(a0, a0, 0x00));
  969. __m128 r1 = _mm_mul_ps(mx, _mm_shuffle_ps(a1, a1, 0x00));
  970. __m128 r2 = _mm_mul_ps(mx, _mm_shuffle_ps(a2, a2, 0x00));
  971. r0 = _mm_add_ps(r0, _mm_mul_ps(my, _mm_shuffle_ps(a0, a0, 0x55)));
  972. r1 = _mm_add_ps(r1, _mm_mul_ps(my, _mm_shuffle_ps(a1, a1, 0x55)));
  973. r2 = _mm_add_ps(r2, _mm_mul_ps(my, _mm_shuffle_ps(a2, a2, 0x55)));
  974. r0 = _mm_add_ps(r0, _mm_mul_ps(mz, _mm_shuffle_ps(a0, a0, 0xaa)));
  975. r1 = _mm_add_ps(r1, _mm_mul_ps(mz, _mm_shuffle_ps(a1, a1, 0xaa)));
  976. r2 = _mm_add_ps(r2, _mm_mul_ps(mz, _mm_shuffle_ps(a2, a2, 0xaa)));
  977. return b3Matrix3x3(r0, r1, r2);
  978. #elif defined B3_USE_NEON
  979. float32x4_t a0 = m_el[0].mVec128;
  980. float32x4_t a1 = m_el[1].mVec128;
  981. float32x4_t a2 = m_el[2].mVec128;
  982. b3Matrix3x3 mT = m.transpose(); // we rely on transpose() zeroing w channel so that we don't have to do it here
  983. float32x4_t mx = mT[0].mVec128;
  984. float32x4_t my = mT[1].mVec128;
  985. float32x4_t mz = mT[2].mVec128;
  986. float32x4_t r0 = vmulq_lane_f32(mx, vget_low_f32(a0), 0);
  987. float32x4_t r1 = vmulq_lane_f32(mx, vget_low_f32(a1), 0);
  988. float32x4_t r2 = vmulq_lane_f32(mx, vget_low_f32(a2), 0);
  989. r0 = vmlaq_lane_f32(r0, my, vget_low_f32(a0), 1);
  990. r1 = vmlaq_lane_f32(r1, my, vget_low_f32(a1), 1);
  991. r2 = vmlaq_lane_f32(r2, my, vget_low_f32(a2), 1);
  992. r0 = vmlaq_lane_f32(r0, mz, vget_high_f32(a0), 0);
  993. r1 = vmlaq_lane_f32(r1, mz, vget_high_f32(a1), 0);
  994. r2 = vmlaq_lane_f32(r2, mz, vget_high_f32(a2), 0);
  995. return b3Matrix3x3(r0, r1, r2);
  996. #else
  997. return b3Matrix3x3(
  998. m_el[0].dot(m[0]), m_el[0].dot(m[1]), m_el[0].dot(m[2]),
  999. m_el[1].dot(m[0]), m_el[1].dot(m[1]), m_el[1].dot(m[2]),
  1000. m_el[2].dot(m[0]), m_el[2].dot(m[1]), m_el[2].dot(m[2]));
  1001. #endif
  1002. }
  1003. B3_FORCE_INLINE b3Vector3
  1004. operator*(const b3Matrix3x3& m, const b3Vector3& v)
  1005. {
  1006. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE)) || defined(B3_USE_NEON)
  1007. return v.dot3(m[0], m[1], m[2]);
  1008. #else
  1009. return b3MakeVector3(m[0].dot(v), m[1].dot(v), m[2].dot(v));
  1010. #endif
  1011. }
  1012. B3_FORCE_INLINE b3Vector3
  1013. operator*(const b3Vector3& v, const b3Matrix3x3& m)
  1014. {
  1015. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  1016. const __m128 vv = v.mVec128;
  1017. __m128 c0 = b3_splat_ps(vv, 0);
  1018. __m128 c1 = b3_splat_ps(vv, 1);
  1019. __m128 c2 = b3_splat_ps(vv, 2);
  1020. c0 = _mm_mul_ps(c0, _mm_and_ps(m[0].mVec128, b3vFFF0fMask));
  1021. c1 = _mm_mul_ps(c1, _mm_and_ps(m[1].mVec128, b3vFFF0fMask));
  1022. c0 = _mm_add_ps(c0, c1);
  1023. c2 = _mm_mul_ps(c2, _mm_and_ps(m[2].mVec128, b3vFFF0fMask));
  1024. return b3MakeVector3(_mm_add_ps(c0, c2));
  1025. #elif defined(B3_USE_NEON)
  1026. const float32x4_t vv = v.mVec128;
  1027. const float32x2_t vlo = vget_low_f32(vv);
  1028. const float32x2_t vhi = vget_high_f32(vv);
  1029. float32x4_t c0, c1, c2;
  1030. c0 = (float32x4_t)vandq_s32((int32x4_t)m[0].mVec128, b3vFFF0Mask);
  1031. c1 = (float32x4_t)vandq_s32((int32x4_t)m[1].mVec128, b3vFFF0Mask);
  1032. c2 = (float32x4_t)vandq_s32((int32x4_t)m[2].mVec128, b3vFFF0Mask);
  1033. c0 = vmulq_lane_f32(c0, vlo, 0);
  1034. c1 = vmulq_lane_f32(c1, vlo, 1);
  1035. c2 = vmulq_lane_f32(c2, vhi, 0);
  1036. c0 = vaddq_f32(c0, c1);
  1037. c0 = vaddq_f32(c0, c2);
  1038. return b3MakeVector3(c0);
  1039. #else
  1040. return b3MakeVector3(m.tdotx(v), m.tdoty(v), m.tdotz(v));
  1041. #endif
  1042. }
  1043. B3_FORCE_INLINE b3Matrix3x3
  1044. operator*(const b3Matrix3x3& m1, const b3Matrix3x3& m2)
  1045. {
  1046. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  1047. __m128 m10 = m1[0].mVec128;
  1048. __m128 m11 = m1[1].mVec128;
  1049. __m128 m12 = m1[2].mVec128;
  1050. __m128 m2v = _mm_and_ps(m2[0].mVec128, b3vFFF0fMask);
  1051. __m128 c0 = b3_splat_ps(m10, 0);
  1052. __m128 c1 = b3_splat_ps(m11, 0);
  1053. __m128 c2 = b3_splat_ps(m12, 0);
  1054. c0 = _mm_mul_ps(c0, m2v);
  1055. c1 = _mm_mul_ps(c1, m2v);
  1056. c2 = _mm_mul_ps(c2, m2v);
  1057. m2v = _mm_and_ps(m2[1].mVec128, b3vFFF0fMask);
  1058. __m128 c0_1 = b3_splat_ps(m10, 1);
  1059. __m128 c1_1 = b3_splat_ps(m11, 1);
  1060. __m128 c2_1 = b3_splat_ps(m12, 1);
  1061. c0_1 = _mm_mul_ps(c0_1, m2v);
  1062. c1_1 = _mm_mul_ps(c1_1, m2v);
  1063. c2_1 = _mm_mul_ps(c2_1, m2v);
  1064. m2v = _mm_and_ps(m2[2].mVec128, b3vFFF0fMask);
  1065. c0 = _mm_add_ps(c0, c0_1);
  1066. c1 = _mm_add_ps(c1, c1_1);
  1067. c2 = _mm_add_ps(c2, c2_1);
  1068. m10 = b3_splat_ps(m10, 2);
  1069. m11 = b3_splat_ps(m11, 2);
  1070. m12 = b3_splat_ps(m12, 2);
  1071. m10 = _mm_mul_ps(m10, m2v);
  1072. m11 = _mm_mul_ps(m11, m2v);
  1073. m12 = _mm_mul_ps(m12, m2v);
  1074. c0 = _mm_add_ps(c0, m10);
  1075. c1 = _mm_add_ps(c1, m11);
  1076. c2 = _mm_add_ps(c2, m12);
  1077. return b3Matrix3x3(c0, c1, c2);
  1078. #elif defined(B3_USE_NEON)
  1079. float32x4_t rv0, rv1, rv2;
  1080. float32x4_t v0, v1, v2;
  1081. float32x4_t mv0, mv1, mv2;
  1082. v0 = m1[0].mVec128;
  1083. v1 = m1[1].mVec128;
  1084. v2 = m1[2].mVec128;
  1085. mv0 = (float32x4_t)vandq_s32((int32x4_t)m2[0].mVec128, b3vFFF0Mask);
  1086. mv1 = (float32x4_t)vandq_s32((int32x4_t)m2[1].mVec128, b3vFFF0Mask);
  1087. mv2 = (float32x4_t)vandq_s32((int32x4_t)m2[2].mVec128, b3vFFF0Mask);
  1088. rv0 = vmulq_lane_f32(mv0, vget_low_f32(v0), 0);
  1089. rv1 = vmulq_lane_f32(mv0, vget_low_f32(v1), 0);
  1090. rv2 = vmulq_lane_f32(mv0, vget_low_f32(v2), 0);
  1091. rv0 = vmlaq_lane_f32(rv0, mv1, vget_low_f32(v0), 1);
  1092. rv1 = vmlaq_lane_f32(rv1, mv1, vget_low_f32(v1), 1);
  1093. rv2 = vmlaq_lane_f32(rv2, mv1, vget_low_f32(v2), 1);
  1094. rv0 = vmlaq_lane_f32(rv0, mv2, vget_high_f32(v0), 0);
  1095. rv1 = vmlaq_lane_f32(rv1, mv2, vget_high_f32(v1), 0);
  1096. rv2 = vmlaq_lane_f32(rv2, mv2, vget_high_f32(v2), 0);
  1097. return b3Matrix3x3(rv0, rv1, rv2);
  1098. #else
  1099. return b3Matrix3x3(
  1100. m2.tdotx(m1[0]), m2.tdoty(m1[0]), m2.tdotz(m1[0]),
  1101. m2.tdotx(m1[1]), m2.tdoty(m1[1]), m2.tdotz(m1[1]),
  1102. m2.tdotx(m1[2]), m2.tdoty(m1[2]), m2.tdotz(m1[2]));
  1103. #endif
  1104. }
  1105. /*
  1106. B3_FORCE_INLINE b3Matrix3x3 b3MultTransposeLeft(const b3Matrix3x3& m1, const b3Matrix3x3& m2) {
  1107. return b3Matrix3x3(
  1108. m1[0][0] * m2[0][0] + m1[1][0] * m2[1][0] + m1[2][0] * m2[2][0],
  1109. m1[0][0] * m2[0][1] + m1[1][0] * m2[1][1] + m1[2][0] * m2[2][1],
  1110. m1[0][0] * m2[0][2] + m1[1][0] * m2[1][2] + m1[2][0] * m2[2][2],
  1111. m1[0][1] * m2[0][0] + m1[1][1] * m2[1][0] + m1[2][1] * m2[2][0],
  1112. m1[0][1] * m2[0][1] + m1[1][1] * m2[1][1] + m1[2][1] * m2[2][1],
  1113. m1[0][1] * m2[0][2] + m1[1][1] * m2[1][2] + m1[2][1] * m2[2][2],
  1114. m1[0][2] * m2[0][0] + m1[1][2] * m2[1][0] + m1[2][2] * m2[2][0],
  1115. m1[0][2] * m2[0][1] + m1[1][2] * m2[1][1] + m1[2][2] * m2[2][1],
  1116. m1[0][2] * m2[0][2] + m1[1][2] * m2[1][2] + m1[2][2] * m2[2][2]);
  1117. }
  1118. */
  1119. /**@brief Equality operator between two matrices
  1120. * It will test all elements are equal. */
  1121. B3_FORCE_INLINE bool operator==(const b3Matrix3x3& m1, const b3Matrix3x3& m2)
  1122. {
  1123. #if (defined(B3_USE_SSE_IN_API) && defined(B3_USE_SSE))
  1124. __m128 c0, c1, c2;
  1125. c0 = _mm_cmpeq_ps(m1[0].mVec128, m2[0].mVec128);
  1126. c1 = _mm_cmpeq_ps(m1[1].mVec128, m2[1].mVec128);
  1127. c2 = _mm_cmpeq_ps(m1[2].mVec128, m2[2].mVec128);
  1128. c0 = _mm_and_ps(c0, c1);
  1129. c0 = _mm_and_ps(c0, c2);
  1130. return (0x7 == _mm_movemask_ps((__m128)c0));
  1131. #else
  1132. return (m1[0][0] == m2[0][0] && m1[1][0] == m2[1][0] && m1[2][0] == m2[2][0] &&
  1133. m1[0][1] == m2[0][1] && m1[1][1] == m2[1][1] && m1[2][1] == m2[2][1] &&
  1134. m1[0][2] == m2[0][2] && m1[1][2] == m2[1][2] && m1[2][2] == m2[2][2]);
  1135. #endif
  1136. }
  1137. ///for serialization
  1138. struct b3Matrix3x3FloatData
  1139. {
  1140. b3Vector3FloatData m_el[3];
  1141. };
  1142. ///for serialization
  1143. struct b3Matrix3x3DoubleData
  1144. {
  1145. b3Vector3DoubleData m_el[3];
  1146. };
  1147. B3_FORCE_INLINE void b3Matrix3x3::serialize(struct b3Matrix3x3Data& dataOut) const
  1148. {
  1149. for (int i = 0; i < 3; i++)
  1150. m_el[i].serialize(dataOut.m_el[i]);
  1151. }
  1152. B3_FORCE_INLINE void b3Matrix3x3::serializeFloat(struct b3Matrix3x3FloatData& dataOut) const
  1153. {
  1154. for (int i = 0; i < 3; i++)
  1155. m_el[i].serializeFloat(dataOut.m_el[i]);
  1156. }
  1157. B3_FORCE_INLINE void b3Matrix3x3::deSerialize(const struct b3Matrix3x3Data& dataIn)
  1158. {
  1159. for (int i = 0; i < 3; i++)
  1160. m_el[i].deSerialize(dataIn.m_el[i]);
  1161. }
  1162. B3_FORCE_INLINE void b3Matrix3x3::deSerializeFloat(const struct b3Matrix3x3FloatData& dataIn)
  1163. {
  1164. for (int i = 0; i < 3; i++)
  1165. m_el[i].deSerializeFloat(dataIn.m_el[i]);
  1166. }
  1167. B3_FORCE_INLINE void b3Matrix3x3::deSerializeDouble(const struct b3Matrix3x3DoubleData& dataIn)
  1168. {
  1169. for (int i = 0; i < 3; i++)
  1170. m_el[i].deSerializeDouble(dataIn.m_el[i]);
  1171. }
  1172. #endif //B3_MATRIX3x3_H