Quat.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #ifndef ANKI_MATH_QUAT_H
  2. #define ANKI_MATH_QUAT_H
  3. #include "anki/math/CommonIncludes.h"
  4. namespace anki {
  5. /// @addtogroup Math
  6. /// @{
  7. /// Quaternion. Used in rotations
  8. template<typename T>
  9. ANKI_ATTRIBUTE_ALIGNED(class, 16) TQuat
  10. {
  11. public:
  12. /// @name Constructors
  13. /// @{
  14. explicit TQuat()
  15. {
  16. x() = y() = z() = w() = 0.0;
  17. }
  18. explicit TQuat(const T f)
  19. {
  20. x() = y() = z() = w() = f;
  21. }
  22. explicit TQuat(const T x_, const T y_, const T z_, const T w_)
  23. {
  24. x() = x_;
  25. y() = y_;
  26. z() = z_;
  27. w() = w_;
  28. }
  29. explicit TQuat(const TVec2<T>& v2, const T z_, const T w_)
  30. {
  31. x() = v2.x();
  32. y() = v2.y();
  33. z() = z_;
  34. w() = w_;
  35. }
  36. explicit TQuat(const TVec3<T>& v3, const T w_)
  37. {
  38. x() = v3.x();
  39. y() = v3.y();
  40. z() = v3.z();
  41. w() = w_;
  42. }
  43. explicit TQuat(const TVec4<T>& v4)
  44. {
  45. x() = v4.x();
  46. y() = v4.y();
  47. z() = v4.z();
  48. w() = v4.w();
  49. }
  50. TQuat(const TQuat& b)
  51. {
  52. x() = b.x();
  53. y() = b.y();
  54. z() = b.z();
  55. w() = b.w();
  56. }
  57. explicit TQuat(const TMat3<T>& m3)
  58. {
  59. T trace = m3(0, 0) + m3(1, 1) + m3(2, 2) + 1.0;
  60. if(trace > getEpsilon<T>())
  61. {
  62. T s = 0.5 / sqrt<T>(trace);
  63. w() = 0.25 / s;
  64. x() = (m3(2, 1) - m3(1, 2)) * s;
  65. y() = (m3(0, 2) - m3(2, 0)) * s;
  66. z() = (m3(1, 0) - m3(0, 1)) * s;
  67. }
  68. else
  69. {
  70. if(m3(0, 0) > m3(1, 1) && m3(0, 0) > m3(2, 2))
  71. {
  72. T s = 0.5 / sqrt<T>(1.0 + m3(0, 0) - m3(1, 1) - m3(2, 2));
  73. w() = (m3(1, 2) - m3(2, 1)) * s;
  74. x() = 0.25 / s;
  75. y() = (m3(0, 1) + m3(1, 0)) * s;
  76. z() = (m3(0, 2) + m3(2, 0)) * s;
  77. }
  78. else if(m3(1, 1) > m3(2, 2))
  79. {
  80. T s = 0.5 / sqrt<T>(1.0 + m3(1, 1) - m3(0, 0) - m3(2, 2));
  81. w() = (m3(0, 2) - m3(2, 0)) * s;
  82. x() = (m3(0, 1) + m3(1, 0)) * s;
  83. y() = 0.25 / s;
  84. z() = (m3(1, 2) + m3(2, 1)) * s;
  85. }
  86. else
  87. {
  88. T s = 0.5 / sqrt<T>(1.0 + m3(2, 2) - m3(0, 0) - m3(1, 1));
  89. w() = (m3(0, 1) - m3(1, 0)) * s;
  90. x() = (m3(0, 2) + m3(2, 0)) * s;
  91. y() = (m3(1, 2) + m3(2, 1)) * s;
  92. z() = 0.25 / s;
  93. }
  94. }
  95. }
  96. explicit TQuat(const TEuler<T>& eu)
  97. {
  98. T cx, sx;
  99. sinCos(eu.y() * 0.5, sx, cx);
  100. T cy, sy;
  101. sinCos(eu.z() * 0.5, sy, cy);
  102. T cz, sz;
  103. sinCos(eu.x() * 0.5, sz, cz);
  104. T cxcy = cx * cy;
  105. T sxsy = sx * sy;
  106. x() = cxcy * sz + sxsy * cz;
  107. y() = sx * cy * cz + cx * sy * sz;
  108. z() = cx * sy * cz - sx * cy * sz;
  109. w() = cxcy * cz - sxsy * sz;
  110. }
  111. explicit TQuat(const TAxisang<T>& axisang)
  112. {
  113. T lengthsq = axisang.getAxis().getLengthSquared();
  114. if(isZero<T>(lengthsq))
  115. {
  116. (*this) = getIdentity();
  117. return;
  118. }
  119. T rad = axisang.getAngle() * 0.5;
  120. T sintheta, costheta;
  121. sinCos(rad, sintheta, costheta);
  122. T scalefactor = sintheta / sqrt(lengthsq);
  123. x() = scalefactor * axisang.getAxis().x();
  124. y() = scalefactor * axisang.getAxis().y();
  125. z() = scalefactor * axisang.getAxis().z();
  126. w() = costheta;
  127. }
  128. /// @}
  129. /// @name Accessors
  130. /// @{
  131. T x() const
  132. {
  133. return vec.x;
  134. }
  135. T& x()
  136. {
  137. return vec.x;
  138. }
  139. T y() const
  140. {
  141. return vec.y;
  142. }
  143. T& y()
  144. {
  145. return vec.y;
  146. }
  147. T z() const
  148. {
  149. return vec.z;
  150. }
  151. T& z()
  152. {
  153. return vec.z;
  154. }
  155. T w() const
  156. {
  157. return vec.w;
  158. }
  159. T& w()
  160. {
  161. return vec.w;
  162. }
  163. /// @}
  164. /// Operators with same type
  165. /// @{
  166. TQuat& operator=(const TQuat& b)
  167. {
  168. x() = b.x();
  169. y() = b.y();
  170. z() = b.z();
  171. w() = b.w();
  172. return *this;
  173. }
  174. /// 16 muls, 12 adds
  175. TQuat operator*(const TQuat& b) const
  176. {
  177. // XXX See if this can be optimized
  178. TQuat out;
  179. out.vec.x = x() * b.w() + y() * b.z() - z() * b.y() + w() * b.x();
  180. out.vec.y = -x() * b.z() + y() * b.w() + z() * b.x() + w() * b.y();
  181. out.vec.z = x() * b.y() - y() * b.x() + z() * b.w() + w() * b.z();
  182. out.vec.w = -x() * b.x() - y() * b.y() - z() * b.z() + w() * b.w();
  183. return out;
  184. }
  185. TQuat& operator*=(const TQuat& b)
  186. {
  187. (*this) = (*this) * b;
  188. return (*this);
  189. }
  190. Bool operator==(const TQuat& b) const
  191. {
  192. return isZero<T>(x() - b.x()) &&
  193. isZero<T>(y() - b.y()) &&
  194. isZero<T>(z() - b.z()) &&
  195. isZero<T>(w() - b.w());
  196. }
  197. Bool operator!=(const TQuat& b) const
  198. {
  199. return !((*this) == b);
  200. }
  201. /// @}
  202. /// @name Other
  203. /// @{
  204. /// Calculates the rotation from TVec3 "from" to "to"
  205. void setFrom2Vec3(const TVec3<T>& from, const TVec3<T>& to)
  206. {
  207. TVec3<T> axis(from.cross(to));
  208. (*this) = TQuat(axis.x(), axis.y(), axis.z(), from.dot(to));
  209. normalize();
  210. w() += 1.0;
  211. if(w() <= getEpsilon<T>())
  212. {
  213. if(from.z() * from.z() > from.x() * from.x())
  214. {
  215. (*this) = TQuat(0.0, from.z(), -from.y(), 0.0);
  216. }
  217. else
  218. {
  219. (*this) = TQuat(from.y(), -from.x(), 0.0, 0.0);
  220. }
  221. }
  222. normalize();
  223. }
  224. T getLength() const
  225. {
  226. return sqrt<T>(w() * w() + x() * x() + y() * y() + z() * z());
  227. }
  228. TQuat getInverted() const
  229. {
  230. T norm = w() * w() + x() * x() + y() * y() + z() * z();
  231. ANKI_ASSERT(!isZero<T>(norm)); // Norm is zero
  232. T normi = 1.0 / norm;
  233. return TQuat(-normi * x(), -normi * y(), -normi * z(), normi * w());
  234. }
  235. void invert()
  236. {
  237. (*this) = getInverted();
  238. }
  239. void conjugate()
  240. {
  241. x() = -x();
  242. y() = -y();
  243. z() = -z();
  244. }
  245. TQuat getConjugated() const
  246. {
  247. return TQuat(-x(), -y(), -z(), w());
  248. }
  249. void normalize()
  250. {
  251. (*this) = getNormalized();
  252. }
  253. TQuat getNormalized() const
  254. {
  255. return TQuat(TVec4<T>(*this).getNormalized());
  256. }
  257. T dot(const TQuat& b) const
  258. {
  259. return w() * b.w() + x() * b.x() + y() * b.y() + z() * b.z();
  260. }
  261. /// Returns slerp(this, q1, t)
  262. TQuat slerp(const TQuat& q1_, const T t) const
  263. {
  264. TVec4<T> q0(*this);
  265. TVec4<T> q1(q1_);
  266. T cosHalfTheta = q0.dot(q1);
  267. if(cosHalfTheta < 0.0)
  268. {
  269. q1 = -q1; // quat changes
  270. cosHalfTheta = -cosHalfTheta;
  271. }
  272. if(fabs<T>(cosHalfTheta) >= 1.0)
  273. {
  274. return TQuat(q0);
  275. }
  276. T halfTheta = acos<T>(cosHalfTheta);
  277. T sinHalfTheta = sqrt<T>(1.0 - cosHalfTheta * cosHalfTheta);
  278. if(fabs<T>(sinHalfTheta) < 0.001)
  279. {
  280. return TQuat((q0 + q1) * 0.5);
  281. }
  282. T ratioA = sin<T>((1.0 - t) * halfTheta) / sinHalfTheta;
  283. T ratio_b = sin<T>(t * halfTheta) / sinHalfTheta;
  284. TVec4<T> tmp, tmp1, sum;
  285. tmp = q0 * ratioA;
  286. tmp1 = q1 * ratio_b;
  287. sum = tmp + tmp1;
  288. sum.normalize();
  289. return TQuat(sum);
  290. }
  291. /// The same as TQuat * TQuat
  292. TQuat getRotated(const TQuat& b) const
  293. {
  294. return (*this) * b;
  295. }
  296. /// @see getRotated
  297. void rotate(const TQuat& b)
  298. {
  299. (*this) = getRotated(b);
  300. }
  301. void setIdentity()
  302. {
  303. x() = y() = z() = 0.0;
  304. w() = 1.0;
  305. }
  306. static const TQuat& getIdentity()
  307. {
  308. static TQuat ident(0.0, 0.0, 0.0, 1.0);
  309. return ident;
  310. }
  311. std::string toString() const
  312. {
  313. return std::to_string(x()) + " " + std::to_string(y()) + " "
  314. + std::to_string(z()) + " " + std::to_string(w());
  315. }
  316. /// @}
  317. private:
  318. /// @name Data
  319. /// @{
  320. struct
  321. {
  322. T x, y, z, w;
  323. } vec;
  324. /// @}
  325. };
  326. /// F32 quaternion
  327. typedef TQuat<F32> Quat;
  328. /// @}
  329. } // end namespace anki
  330. #endif