btMatrix3x3.h 43 KB

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