btVector3.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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_VECTOR3_H
  13. #define BT_VECTOR3_H
  14. #include "btMinMax.h"
  15. #include "btScalar.h"
  16. #ifdef BT_USE_DOUBLE_PRECISION
  17. #define btVector3Data btVector3DoubleData
  18. #define btVector3DataName "btVector3DoubleData"
  19. #else
  20. #define btVector3Data btVector3FloatData
  21. #define btVector3DataName "btVector3FloatData"
  22. #endif //BT_USE_DOUBLE_PRECISION
  23. // -- GODOT start --
  24. namespace VHACD {
  25. // -- GODOT end --
  26. /**@brief btVector3 can be used to represent 3D points and vectors.
  27. * It has an un-used w component to suit 16-byte alignment when btVector3 is stored in containers. This extra component can be used by derived classes (Quaternion?) or by user
  28. * Ideally, this class should be replaced by a platform optimized SIMD version that keeps the data in registers
  29. */
  30. ATTRIBUTE_ALIGNED16(class)
  31. btVector3
  32. {
  33. public:
  34. #if defined(__SPU__) && defined(__CELLOS_LV2__)
  35. btScalar m_floats[4];
  36. public:
  37. SIMD_FORCE_INLINE const vec_float4& get128() const
  38. {
  39. return *((const vec_float4*)&m_floats[0]);
  40. }
  41. public:
  42. #else //__CELLOS_LV2__ __SPU__
  43. #ifdef BT_USE_SSE // _WIN32
  44. union {
  45. __m128 mVec128;
  46. btScalar m_floats[4];
  47. };
  48. SIMD_FORCE_INLINE __m128 get128() const
  49. {
  50. return mVec128;
  51. }
  52. SIMD_FORCE_INLINE void set128(__m128 v128)
  53. {
  54. mVec128 = v128;
  55. }
  56. #else
  57. btScalar m_floats[4];
  58. #endif
  59. #endif //__CELLOS_LV2__ __SPU__
  60. public:
  61. /**@brief No initialization constructor */
  62. SIMD_FORCE_INLINE btVector3() {}
  63. /**@brief Constructor from scalars
  64. * @param x X value
  65. * @param y Y value
  66. * @param z Z value
  67. */
  68. SIMD_FORCE_INLINE btVector3(const btScalar& x, const btScalar& y, const btScalar& z)
  69. {
  70. m_floats[0] = x;
  71. m_floats[1] = y;
  72. m_floats[2] = z;
  73. m_floats[3] = btScalar(0.);
  74. }
  75. /**@brief Add a vector to this one
  76. * @param The vector to add to this one */
  77. SIMD_FORCE_INLINE btVector3& operator+=(const btVector3& v)
  78. {
  79. m_floats[0] += v.m_floats[0];
  80. m_floats[1] += v.m_floats[1];
  81. m_floats[2] += v.m_floats[2];
  82. return *this;
  83. }
  84. /**@brief Subtract a vector from this one
  85. * @param The vector to subtract */
  86. SIMD_FORCE_INLINE btVector3& operator-=(const btVector3& v)
  87. {
  88. m_floats[0] -= v.m_floats[0];
  89. m_floats[1] -= v.m_floats[1];
  90. m_floats[2] -= v.m_floats[2];
  91. return *this;
  92. }
  93. /**@brief Scale the vector
  94. * @param s Scale factor */
  95. SIMD_FORCE_INLINE btVector3& operator*=(const btScalar& s)
  96. {
  97. m_floats[0] *= s;
  98. m_floats[1] *= s;
  99. m_floats[2] *= s;
  100. return *this;
  101. }
  102. /**@brief Inversely scale the vector
  103. * @param s Scale factor to divide by */
  104. SIMD_FORCE_INLINE btVector3& operator/=(const btScalar& s)
  105. {
  106. btFullAssert(s != btScalar(0.0));
  107. return * this *= btScalar(1.0) / s;
  108. }
  109. /**@brief Return the dot product
  110. * @param v The other vector in the dot product */
  111. SIMD_FORCE_INLINE btScalar dot(const btVector3& v) const
  112. {
  113. return m_floats[0] * v.m_floats[0] + m_floats[1] * v.m_floats[1] + m_floats[2] * v.m_floats[2];
  114. }
  115. /**@brief Return the length of the vector squared */
  116. SIMD_FORCE_INLINE btScalar length2() const
  117. {
  118. return dot(*this);
  119. }
  120. /**@brief Return the length of the vector */
  121. SIMD_FORCE_INLINE btScalar length() const
  122. {
  123. return btSqrt(length2());
  124. }
  125. /**@brief Return the distance squared between the ends of this and another vector
  126. * This is symantically treating the vector like a point */
  127. SIMD_FORCE_INLINE btScalar distance2(const btVector3& v) const;
  128. /**@brief Return the distance between the ends of this and another vector
  129. * This is symantically treating the vector like a point */
  130. SIMD_FORCE_INLINE btScalar distance(const btVector3& v) const;
  131. SIMD_FORCE_INLINE btVector3& safeNormalize()
  132. {
  133. btVector3 absVec = this->absolute();
  134. int32_t maxIndex = absVec.maxAxis();
  135. if (absVec[maxIndex] > 0) {
  136. *this /= absVec[maxIndex];
  137. return * this /= length();
  138. }
  139. setValue(1, 0, 0);
  140. return *this;
  141. }
  142. /**@brief Normalize this vector
  143. * x^2 + y^2 + z^2 = 1 */
  144. SIMD_FORCE_INLINE btVector3& normalize()
  145. {
  146. return * this /= length();
  147. }
  148. /**@brief Return a normalized version of this vector */
  149. SIMD_FORCE_INLINE btVector3 normalized() const;
  150. /**@brief Return a rotated version of this vector
  151. * @param wAxis The axis to rotate about
  152. * @param angle The angle to rotate by */
  153. SIMD_FORCE_INLINE btVector3 rotate(const btVector3& wAxis, const btScalar angle) const;
  154. /**@brief Return the angle between this and another vector
  155. * @param v The other vector */
  156. SIMD_FORCE_INLINE btScalar angle(const btVector3& v) const
  157. {
  158. btScalar s = btSqrt(length2() * v.length2());
  159. btFullAssert(s != btScalar(0.0));
  160. return btAcos(dot(v) / s);
  161. }
  162. /**@brief Return a vector will the absolute values of each element */
  163. SIMD_FORCE_INLINE btVector3 absolute() const
  164. {
  165. return btVector3(
  166. btFabs(m_floats[0]),
  167. btFabs(m_floats[1]),
  168. btFabs(m_floats[2]));
  169. }
  170. /**@brief Return the cross product between this and another vector
  171. * @param v The other vector */
  172. SIMD_FORCE_INLINE btVector3 cross(const btVector3& v) const
  173. {
  174. return btVector3(
  175. m_floats[1] * v.m_floats[2] - m_floats[2] * v.m_floats[1],
  176. m_floats[2] * v.m_floats[0] - m_floats[0] * v.m_floats[2],
  177. m_floats[0] * v.m_floats[1] - m_floats[1] * v.m_floats[0]);
  178. }
  179. SIMD_FORCE_INLINE btScalar triple(const btVector3& v1, const btVector3& v2) const
  180. {
  181. return m_floats[0] * (v1.m_floats[1] * v2.m_floats[2] - v1.m_floats[2] * v2.m_floats[1]) + m_floats[1] * (v1.m_floats[2] * v2.m_floats[0] - v1.m_floats[0] * v2.m_floats[2]) + m_floats[2] * (v1.m_floats[0] * v2.m_floats[1] - v1.m_floats[1] * v2.m_floats[0]);
  182. }
  183. /**@brief Return the axis with the smallest value
  184. * Note return values are 0,1,2 for x, y, or z */
  185. SIMD_FORCE_INLINE int32_t minAxis() const
  186. {
  187. return m_floats[0] < m_floats[1] ? (m_floats[0] < m_floats[2] ? 0 : 2) : (m_floats[1] < m_floats[2] ? 1 : 2);
  188. }
  189. /**@brief Return the axis with the largest value
  190. * Note return values are 0,1,2 for x, y, or z */
  191. SIMD_FORCE_INLINE int32_t maxAxis() const
  192. {
  193. return m_floats[0] < m_floats[1] ? (m_floats[1] < m_floats[2] ? 2 : 1) : (m_floats[0] < m_floats[2] ? 2 : 0);
  194. }
  195. SIMD_FORCE_INLINE int32_t furthestAxis() const
  196. {
  197. return absolute().minAxis();
  198. }
  199. SIMD_FORCE_INLINE int32_t closestAxis() const
  200. {
  201. return absolute().maxAxis();
  202. }
  203. SIMD_FORCE_INLINE void setInterpolate3(const btVector3& v0, const btVector3& v1, btScalar rt)
  204. {
  205. btScalar s = btScalar(1.0) - rt;
  206. m_floats[0] = s * v0.m_floats[0] + rt * v1.m_floats[0];
  207. m_floats[1] = s * v0.m_floats[1] + rt * v1.m_floats[1];
  208. m_floats[2] = s * v0.m_floats[2] + rt * v1.m_floats[2];
  209. //don't do the unused w component
  210. // m_co[3] = s * v0[3] + rt * v1[3];
  211. }
  212. /**@brief Return the linear interpolation between this and another vector
  213. * @param v The other vector
  214. * @param t The ration of this to v (t = 0 => return this, t=1 => return other) */
  215. SIMD_FORCE_INLINE btVector3 lerp(const btVector3& v, const btScalar& t) const
  216. {
  217. return btVector3(m_floats[0] + (v.m_floats[0] - m_floats[0]) * t,
  218. m_floats[1] + (v.m_floats[1] - m_floats[1]) * t,
  219. m_floats[2] + (v.m_floats[2] - m_floats[2]) * t);
  220. }
  221. /**@brief Elementwise multiply this vector by the other
  222. * @param v The other vector */
  223. SIMD_FORCE_INLINE btVector3& operator*=(const btVector3& v)
  224. {
  225. m_floats[0] *= v.m_floats[0];
  226. m_floats[1] *= v.m_floats[1];
  227. m_floats[2] *= v.m_floats[2];
  228. return *this;
  229. }
  230. /**@brief Return the x value */
  231. SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; }
  232. /**@brief Return the y value */
  233. SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; }
  234. /**@brief Return the z value */
  235. SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; }
  236. /**@brief Set the x value */
  237. SIMD_FORCE_INLINE void setX(btScalar x) { m_floats[0] = x; };
  238. /**@brief Set the y value */
  239. SIMD_FORCE_INLINE void setY(btScalar y) { m_floats[1] = y; };
  240. /**@brief Set the z value */
  241. SIMD_FORCE_INLINE void setZ(btScalar z) { m_floats[2] = z; };
  242. /**@brief Set the w value */
  243. SIMD_FORCE_INLINE void setW(btScalar w) { m_floats[3] = w; };
  244. /**@brief Return the x value */
  245. SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; }
  246. /**@brief Return the y value */
  247. SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; }
  248. /**@brief Return the z value */
  249. SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; }
  250. /**@brief Return the w value */
  251. SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; }
  252. //SIMD_FORCE_INLINE btScalar& operator[](int32_t i) { return (&m_floats[0])[i]; }
  253. //SIMD_FORCE_INLINE const btScalar& operator[](int32_t i) const { return (&m_floats[0])[i]; }
  254. ///operator btScalar*() replaces operator[], using implicit conversion. We added operator != and operator == to avoid pointer comparisons.
  255. SIMD_FORCE_INLINE operator btScalar*() { return &m_floats[0]; }
  256. SIMD_FORCE_INLINE operator const btScalar*() const { return &m_floats[0]; }
  257. SIMD_FORCE_INLINE bool operator==(const btVector3& other) const
  258. {
  259. return ((m_floats[3] == other.m_floats[3]) && (m_floats[2] == other.m_floats[2]) && (m_floats[1] == other.m_floats[1]) && (m_floats[0] == other.m_floats[0]));
  260. }
  261. SIMD_FORCE_INLINE bool operator!=(const btVector3& other) const
  262. {
  263. return !(*this == other);
  264. }
  265. /**@brief Set each element to the max of the current values and the values of another btVector3
  266. * @param other The other btVector3 to compare with
  267. */
  268. SIMD_FORCE_INLINE void setMax(const btVector3& other)
  269. {
  270. btSetMax(m_floats[0], other.m_floats[0]);
  271. btSetMax(m_floats[1], other.m_floats[1]);
  272. btSetMax(m_floats[2], other.m_floats[2]);
  273. btSetMax(m_floats[3], other.w());
  274. }
  275. /**@brief Set each element to the min of the current values and the values of another btVector3
  276. * @param other The other btVector3 to compare with
  277. */
  278. SIMD_FORCE_INLINE void setMin(const btVector3& other)
  279. {
  280. btSetMin(m_floats[0], other.m_floats[0]);
  281. btSetMin(m_floats[1], other.m_floats[1]);
  282. btSetMin(m_floats[2], other.m_floats[2]);
  283. btSetMin(m_floats[3], other.w());
  284. }
  285. SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z)
  286. {
  287. m_floats[0] = x;
  288. m_floats[1] = y;
  289. m_floats[2] = z;
  290. m_floats[3] = btScalar(0.);
  291. }
  292. void getSkewSymmetricMatrix(btVector3 * v0, btVector3 * v1, btVector3 * v2) const
  293. {
  294. v0->setValue(0., -z(), y());
  295. v1->setValue(z(), 0., -x());
  296. v2->setValue(-y(), x(), 0.);
  297. }
  298. void setZero()
  299. {
  300. setValue(btScalar(0.), btScalar(0.), btScalar(0.));
  301. }
  302. SIMD_FORCE_INLINE bool isZero() const
  303. {
  304. return m_floats[0] == btScalar(0) && m_floats[1] == btScalar(0) && m_floats[2] == btScalar(0);
  305. }
  306. SIMD_FORCE_INLINE bool fuzzyZero() const
  307. {
  308. return length2() < SIMD_EPSILON;
  309. }
  310. SIMD_FORCE_INLINE void serialize(struct btVector3Data & dataOut) const;
  311. SIMD_FORCE_INLINE void deSerialize(const struct btVector3Data& dataIn);
  312. SIMD_FORCE_INLINE void serializeFloat(struct btVector3FloatData & dataOut) const;
  313. SIMD_FORCE_INLINE void deSerializeFloat(const struct btVector3FloatData& dataIn);
  314. SIMD_FORCE_INLINE void serializeDouble(struct btVector3DoubleData & dataOut) const;
  315. SIMD_FORCE_INLINE void deSerializeDouble(const struct btVector3DoubleData& dataIn);
  316. };
  317. /**@brief Return the sum of two vectors (Point symantics)*/
  318. SIMD_FORCE_INLINE btVector3
  319. operator+(const btVector3& v1, const btVector3& v2)
  320. {
  321. return btVector3(v1.m_floats[0] + v2.m_floats[0], v1.m_floats[1] + v2.m_floats[1], v1.m_floats[2] + v2.m_floats[2]);
  322. }
  323. /**@brief Return the elementwise product of two vectors */
  324. SIMD_FORCE_INLINE btVector3
  325. operator*(const btVector3& v1, const btVector3& v2)
  326. {
  327. return btVector3(v1.m_floats[0] * v2.m_floats[0], v1.m_floats[1] * v2.m_floats[1], v1.m_floats[2] * v2.m_floats[2]);
  328. }
  329. /**@brief Return the difference between two vectors */
  330. SIMD_FORCE_INLINE btVector3
  331. operator-(const btVector3& v1, const btVector3& v2)
  332. {
  333. return btVector3(v1.m_floats[0] - v2.m_floats[0], v1.m_floats[1] - v2.m_floats[1], v1.m_floats[2] - v2.m_floats[2]);
  334. }
  335. /**@brief Return the negative of the vector */
  336. SIMD_FORCE_INLINE btVector3
  337. operator-(const btVector3& v)
  338. {
  339. return btVector3(-v.m_floats[0], -v.m_floats[1], -v.m_floats[2]);
  340. }
  341. /**@brief Return the vector scaled by s */
  342. SIMD_FORCE_INLINE btVector3
  343. operator*(const btVector3& v, const btScalar& s)
  344. {
  345. return btVector3(v.m_floats[0] * s, v.m_floats[1] * s, v.m_floats[2] * s);
  346. }
  347. /**@brief Return the vector scaled by s */
  348. SIMD_FORCE_INLINE btVector3
  349. operator*(const btScalar& s, const btVector3& v)
  350. {
  351. return v * s;
  352. }
  353. /**@brief Return the vector inversely scaled by s */
  354. SIMD_FORCE_INLINE btVector3
  355. operator/(const btVector3& v, const btScalar& s)
  356. {
  357. btFullAssert(s != btScalar(0.0));
  358. return v * (btScalar(1.0) / s);
  359. }
  360. /**@brief Return the vector inversely scaled by s */
  361. SIMD_FORCE_INLINE btVector3
  362. operator/(const btVector3& v1, const btVector3& v2)
  363. {
  364. return btVector3(v1.m_floats[0] / v2.m_floats[0], v1.m_floats[1] / v2.m_floats[1], v1.m_floats[2] / v2.m_floats[2]);
  365. }
  366. /**@brief Return the dot product between two vectors */
  367. SIMD_FORCE_INLINE btScalar
  368. btDot(const btVector3& v1, const btVector3& v2)
  369. {
  370. return v1.dot(v2);
  371. }
  372. /**@brief Return the distance squared between two vectors */
  373. SIMD_FORCE_INLINE btScalar
  374. btDistance2(const btVector3& v1, const btVector3& v2)
  375. {
  376. return v1.distance2(v2);
  377. }
  378. /**@brief Return the distance between two vectors */
  379. SIMD_FORCE_INLINE btScalar
  380. btDistance(const btVector3& v1, const btVector3& v2)
  381. {
  382. return v1.distance(v2);
  383. }
  384. /**@brief Return the angle between two vectors */
  385. SIMD_FORCE_INLINE btScalar
  386. btAngle(const btVector3& v1, const btVector3& v2)
  387. {
  388. return v1.angle(v2);
  389. }
  390. /**@brief Return the cross product of two vectors */
  391. SIMD_FORCE_INLINE btVector3
  392. btCross(const btVector3& v1, const btVector3& v2)
  393. {
  394. return v1.cross(v2);
  395. }
  396. SIMD_FORCE_INLINE btScalar
  397. btTriple(const btVector3& v1, const btVector3& v2, const btVector3& v3)
  398. {
  399. return v1.triple(v2, v3);
  400. }
  401. /**@brief Return the linear interpolation between two vectors
  402. * @param v1 One vector
  403. * @param v2 The other vector
  404. * @param t The ration of this to v (t = 0 => return v1, t=1 => return v2) */
  405. SIMD_FORCE_INLINE btVector3
  406. lerp(const btVector3& v1, const btVector3& v2, const btScalar& t)
  407. {
  408. return v1.lerp(v2, t);
  409. }
  410. SIMD_FORCE_INLINE btScalar btVector3::distance2(const btVector3& v) const
  411. {
  412. return (v - *this).length2();
  413. }
  414. SIMD_FORCE_INLINE btScalar btVector3::distance(const btVector3& v) const
  415. {
  416. return (v - *this).length();
  417. }
  418. SIMD_FORCE_INLINE btVector3 btVector3::normalized() const
  419. {
  420. return *this / length();
  421. }
  422. SIMD_FORCE_INLINE btVector3 btVector3::rotate(const btVector3& wAxis, const btScalar angle) const
  423. {
  424. // wAxis must be a unit lenght vector
  425. btVector3 o = wAxis * wAxis.dot(*this);
  426. btVector3 x = *this - o;
  427. btVector3 y;
  428. y = wAxis.cross(*this);
  429. return (o + x * btCos(angle) + y * btSin(angle));
  430. }
  431. class btVector4 : public btVector3 {
  432. public:
  433. SIMD_FORCE_INLINE btVector4() {}
  434. SIMD_FORCE_INLINE btVector4(const btScalar& x, const btScalar& y, const btScalar& z, const btScalar& w)
  435. : btVector3(x, y, z)
  436. {
  437. m_floats[3] = w;
  438. }
  439. SIMD_FORCE_INLINE btVector4 absolute4() const
  440. {
  441. return btVector4(
  442. btFabs(m_floats[0]),
  443. btFabs(m_floats[1]),
  444. btFabs(m_floats[2]),
  445. btFabs(m_floats[3]));
  446. }
  447. btScalar getW() const { return m_floats[3]; }
  448. SIMD_FORCE_INLINE int32_t maxAxis4() const
  449. {
  450. int32_t maxIndex = -1;
  451. btScalar maxVal = btScalar(-BT_LARGE_FLOAT);
  452. if (m_floats[0] > maxVal) {
  453. maxIndex = 0;
  454. maxVal = m_floats[0];
  455. }
  456. if (m_floats[1] > maxVal) {
  457. maxIndex = 1;
  458. maxVal = m_floats[1];
  459. }
  460. if (m_floats[2] > maxVal) {
  461. maxIndex = 2;
  462. maxVal = m_floats[2];
  463. }
  464. if (m_floats[3] > maxVal) {
  465. maxIndex = 3;
  466. }
  467. return maxIndex;
  468. }
  469. SIMD_FORCE_INLINE int32_t minAxis4() const
  470. {
  471. int32_t minIndex = -1;
  472. btScalar minVal = btScalar(BT_LARGE_FLOAT);
  473. if (m_floats[0] < minVal) {
  474. minIndex = 0;
  475. minVal = m_floats[0];
  476. }
  477. if (m_floats[1] < minVal) {
  478. minIndex = 1;
  479. minVal = m_floats[1];
  480. }
  481. if (m_floats[2] < minVal) {
  482. minIndex = 2;
  483. minVal = m_floats[2];
  484. }
  485. if (m_floats[3] < minVal) {
  486. minIndex = 3;
  487. }
  488. return minIndex;
  489. }
  490. SIMD_FORCE_INLINE int32_t closestAxis4() const
  491. {
  492. return absolute4().maxAxis4();
  493. }
  494. /**@brief Set x,y,z and zero w
  495. * @param x Value of x
  496. * @param y Value of y
  497. * @param z Value of z
  498. */
  499. /* void getValue(btScalar *m) const
  500. {
  501. m[0] = m_floats[0];
  502. m[1] = m_floats[1];
  503. m[2] =m_floats[2];
  504. }
  505. */
  506. /**@brief Set the values
  507. * @param x Value of x
  508. * @param y Value of y
  509. * @param z Value of z
  510. * @param w Value of w
  511. */
  512. SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z, const btScalar& w)
  513. {
  514. m_floats[0] = x;
  515. m_floats[1] = y;
  516. m_floats[2] = z;
  517. m_floats[3] = w;
  518. }
  519. };
  520. ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  521. SIMD_FORCE_INLINE void btSwapScalarEndian(const btScalar& sourceVal, btScalar& destVal)
  522. {
  523. #ifdef BT_USE_DOUBLE_PRECISION
  524. unsigned char* dest = (unsigned char*)&destVal;
  525. unsigned char* src = (unsigned char*)&sourceVal;
  526. dest[0] = src[7];
  527. dest[1] = src[6];
  528. dest[2] = src[5];
  529. dest[3] = src[4];
  530. dest[4] = src[3];
  531. dest[5] = src[2];
  532. dest[6] = src[1];
  533. dest[7] = src[0];
  534. #else
  535. unsigned char* dest = (unsigned char*)&destVal;
  536. unsigned char* src = (unsigned char*)&sourceVal;
  537. dest[0] = src[3];
  538. dest[1] = src[2];
  539. dest[2] = src[1];
  540. dest[3] = src[0];
  541. #endif //BT_USE_DOUBLE_PRECISION
  542. }
  543. ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  544. SIMD_FORCE_INLINE void btSwapVector3Endian(const btVector3& sourceVec, btVector3& destVec)
  545. {
  546. for (int32_t i = 0; i < 4; i++) {
  547. btSwapScalarEndian(sourceVec[i], destVec[i]);
  548. }
  549. }
  550. ///btUnSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  551. SIMD_FORCE_INLINE void btUnSwapVector3Endian(btVector3& vector)
  552. {
  553. btVector3 swappedVec;
  554. for (int32_t i = 0; i < 4; i++) {
  555. btSwapScalarEndian(vector[i], swappedVec[i]);
  556. }
  557. vector = swappedVec;
  558. }
  559. template <class T>
  560. SIMD_FORCE_INLINE void btPlaneSpace1(const T& n, T& p, T& q)
  561. {
  562. if (btFabs(n[2]) > SIMDSQRT12) {
  563. // choose p in y-z plane
  564. btScalar a = n[1] * n[1] + n[2] * n[2];
  565. btScalar k = btRecipSqrt(a);
  566. p[0] = 0;
  567. p[1] = -n[2] * k;
  568. p[2] = n[1] * k;
  569. // set q = n x p
  570. q[0] = a * k;
  571. q[1] = -n[0] * p[2];
  572. q[2] = n[0] * p[1];
  573. }
  574. else {
  575. // choose p in x-y plane
  576. btScalar a = n[0] * n[0] + n[1] * n[1];
  577. btScalar k = btRecipSqrt(a);
  578. p[0] = -n[1] * k;
  579. p[1] = n[0] * k;
  580. p[2] = 0;
  581. // set q = n x p
  582. q[0] = -n[2] * p[1];
  583. q[1] = n[2] * p[0];
  584. q[2] = a * k;
  585. }
  586. }
  587. struct btVector3FloatData {
  588. float m_floats[4];
  589. };
  590. struct btVector3DoubleData {
  591. double m_floats[4];
  592. };
  593. SIMD_FORCE_INLINE void btVector3::serializeFloat(struct btVector3FloatData& dataOut) const
  594. {
  595. ///could also do a memcpy, check if it is worth it
  596. for (int32_t i = 0; i < 4; i++)
  597. dataOut.m_floats[i] = float(m_floats[i]);
  598. }
  599. SIMD_FORCE_INLINE void btVector3::deSerializeFloat(const struct btVector3FloatData& dataIn)
  600. {
  601. for (int32_t i = 0; i < 4; i++)
  602. m_floats[i] = btScalar(dataIn.m_floats[i]);
  603. }
  604. SIMD_FORCE_INLINE void btVector3::serializeDouble(struct btVector3DoubleData& dataOut) const
  605. {
  606. ///could also do a memcpy, check if it is worth it
  607. for (int32_t i = 0; i < 4; i++)
  608. dataOut.m_floats[i] = double(m_floats[i]);
  609. }
  610. SIMD_FORCE_INLINE void btVector3::deSerializeDouble(const struct btVector3DoubleData& dataIn)
  611. {
  612. for (int32_t i = 0; i < 4; i++)
  613. m_floats[i] = btScalar(dataIn.m_floats[i]);
  614. }
  615. SIMD_FORCE_INLINE void btVector3::serialize(struct btVector3Data& dataOut) const
  616. {
  617. ///could also do a memcpy, check if it is worth it
  618. for (int32_t i = 0; i < 4; i++)
  619. dataOut.m_floats[i] = m_floats[i];
  620. }
  621. SIMD_FORCE_INLINE void btVector3::deSerialize(const struct btVector3Data& dataIn)
  622. {
  623. for (int32_t i = 0; i < 4; i++)
  624. m_floats[i] = dataIn.m_floats[i];
  625. }
  626. // -- GODOT start --
  627. }; // namespace VHACD
  628. // -- GODOT end --
  629. #endif //BT_VECTOR3_H