quat_operations.hpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. #ifndef BOOST_QVM_QUAT_OPERATIONS
  2. #define BOOST_QVM_QUAT_OPERATIONS
  3. // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/qvm/detail/quat_assign.hpp>
  7. #include <boost/qvm/deduce_quat.hpp>
  8. #include <boost/qvm/mat_traits.hpp>
  9. #include <boost/qvm/scalar_traits.hpp>
  10. #include <boost/qvm/math.hpp>
  11. #include <boost/qvm/assert.hpp>
  12. #include <boost/qvm/error.hpp>
  13. #include <boost/qvm/throw_exception.hpp>
  14. #include <string>
  15. #include <cmath>
  16. namespace boost { namespace qvm {
  17. namespace
  18. qvm_detail
  19. {
  20. BOOST_QVM_INLINE_CRITICAL
  21. void const *
  22. get_valid_ptr_quat_operations()
  23. {
  24. static int const obj=0;
  25. return &obj;
  26. }
  27. }
  28. ////////////////////////////////////////////////
  29. namespace
  30. msvc_parse_bug_workaround
  31. {
  32. template <class A,class B>
  33. struct
  34. quats
  35. {
  36. static bool const value=is_quat<A>::value && is_quat<B>::value;
  37. };
  38. }
  39. namespace
  40. qvm_to_string_detail
  41. {
  42. template <class T>
  43. std::string to_string( T const & x );
  44. }
  45. template <class A>
  46. inline
  47. typename enable_if_c<
  48. is_quat<A>::value,
  49. std::string>::type
  50. to_string( A const & a )
  51. {
  52. using namespace qvm_to_string_detail;
  53. return '('+
  54. to_string(quat_traits<A>::template read_element<0>(a))+','+
  55. to_string(quat_traits<A>::template read_element<1>(a))+','+
  56. to_string(quat_traits<A>::template read_element<2>(a))+','+
  57. to_string(quat_traits<A>::template read_element<3>(a))+')';
  58. }
  59. ////////////////////////////////////////////////
  60. template <class A,class B,class Cmp>
  61. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  62. typename enable_if_c<
  63. is_quat<A>::value && is_quat<B>::value,
  64. bool>::type
  65. cmp( A const & a, B const & b, Cmp f )
  66. {
  67. typedef typename quat_traits<A>::scalar_type T;
  68. typedef typename quat_traits<B>::scalar_type U;
  69. T q1[4] =
  70. {
  71. quat_traits<A>::template read_element<0>(a),
  72. quat_traits<A>::template read_element<1>(a),
  73. quat_traits<A>::template read_element<2>(a),
  74. quat_traits<A>::template read_element<3>(a)
  75. };
  76. U q2[4] =
  77. {
  78. quat_traits<B>::template read_element<0>(b),
  79. quat_traits<B>::template read_element<1>(b),
  80. quat_traits<B>::template read_element<2>(b),
  81. quat_traits<B>::template read_element<3>(b)
  82. };
  83. int i=0;
  84. for( ; i!=4; ++i )
  85. if( !f(q1[i],q2[i]) )
  86. break;
  87. if( i==4 )
  88. return true;
  89. for( i=0; i!=4; ++i )
  90. if( !f(q1[i],-q2[i]) )
  91. return false;
  92. return true;
  93. }
  94. ////////////////////////////////////////////////
  95. template <class R,class A>
  96. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  97. typename enable_if_c<
  98. is_quat<R>::value && is_quat<A>::value,
  99. R>::type
  100. convert_to( A const & a )
  101. {
  102. R r;
  103. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a));
  104. write_quat_element<1>(r,quat_traits<A>::template read_element<1>(a));
  105. write_quat_element<2>(r,quat_traits<A>::template read_element<2>(a));
  106. write_quat_element<3>(r,quat_traits<A>::template read_element<3>(a));
  107. return r;
  108. }
  109. template <class R,class A>
  110. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  111. typename enable_if_c<
  112. is_quat<R>::value && is_mat<A>::value &&
  113. mat_traits<A>::rows==3 && mat_traits<A>::cols==3,
  114. R>::type
  115. convert_to( A const & a )
  116. {
  117. typedef typename mat_traits<A>::scalar_type T;
  118. T const mat[3][3] =
  119. {
  120. { mat_traits<A>::template read_element<0,0>(a), mat_traits<A>::template read_element<0,1>(a), mat_traits<A>::template read_element<0,2>(a) },
  121. { mat_traits<A>::template read_element<1,0>(a), mat_traits<A>::template read_element<1,1>(a), mat_traits<A>::template read_element<1,2>(a) },
  122. { mat_traits<A>::template read_element<2,0>(a), mat_traits<A>::template read_element<2,1>(a), mat_traits<A>::template read_element<2,2>(a) }
  123. };
  124. R r;
  125. if( mat[0][0]+mat[1][1]+mat[2][2] > scalar_traits<T>::value(0) )
  126. {
  127. T t = mat[0][0] + mat[1][1] + mat[2][2] + scalar_traits<T>::value(1);
  128. T s = (scalar_traits<T>::value(1)/sqrt(t))/2;
  129. write_quat_element<0>(r,s*t);
  130. write_quat_element<1>(r,(mat[2][1]-mat[1][2])*s);
  131. write_quat_element<2>(r,(mat[0][2]-mat[2][0])*s);
  132. write_quat_element<3>(r,(mat[1][0]-mat[0][1])*s);
  133. }
  134. else if( mat[0][0]>mat[1][1] && mat[0][0]>mat[2][2] )
  135. {
  136. T t = mat[0][0] - mat[1][1] - mat[2][2] + scalar_traits<T>::value(1);
  137. T s = (scalar_traits<T>::value(1)/sqrt(t))/2;
  138. write_quat_element<0>(r,(mat[2][1]-mat[1][2])*s);
  139. write_quat_element<1>(r,s*t);
  140. write_quat_element<2>(r,(mat[1][0]+mat[0][1])*s);
  141. write_quat_element<3>(r,(mat[0][2]+mat[2][0])*s);
  142. }
  143. else if( mat[1][1]>mat[2][2] )
  144. {
  145. T t = - mat[0][0] + mat[1][1] - mat[2][2] + scalar_traits<T>::value(1);
  146. T s = (scalar_traits<T>::value(1)/sqrt(t))/2;
  147. write_quat_element<0>(r,(mat[0][2]-mat[2][0])*s);
  148. write_quat_element<1>(r,(mat[1][0]+mat[0][1])*s);
  149. write_quat_element<2>(r,s*t);
  150. write_quat_element<3>(r,(mat[2][1]+mat[1][2])*s);
  151. }
  152. else
  153. {
  154. T t = - mat[0][0] - mat[1][1] + mat[2][2] + scalar_traits<T>::value(1);
  155. T s = (scalar_traits<T>::value(1)/sqrt(t))/2;
  156. write_quat_element<0>(r,(mat[1][0]-mat[0][1])*s);
  157. write_quat_element<1>(r,(mat[0][2]+mat[2][0])*s);
  158. write_quat_element<2>(r,(mat[2][1]+mat[1][2])*s);
  159. write_quat_element<3>(r,s*t);
  160. }
  161. return r;
  162. }
  163. ////////////////////////////////////////////////
  164. template <class A>
  165. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  166. typename lazy_enable_if_c<
  167. is_quat<A>::value,
  168. deduce_quat<A> >::type
  169. conjugate( A const & a )
  170. {
  171. typedef typename deduce_quat<A>::type R;
  172. R r;
  173. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a));
  174. write_quat_element<1>(r,-quat_traits<A>::template read_element<1>(a));
  175. write_quat_element<2>(r,-quat_traits<A>::template read_element<2>(a));
  176. write_quat_element<3>(r,-quat_traits<A>::template read_element<3>(a));
  177. return r;
  178. }
  179. ////////////////////////////////////////////////
  180. namespace
  181. qvm_detail
  182. {
  183. template <class T>
  184. class
  185. identity_quat_
  186. {
  187. identity_quat_( identity_quat_ const & );
  188. identity_quat_ & operator=( identity_quat_ const & );
  189. ~identity_quat_();
  190. public:
  191. template <class R
  192. #if __cplusplus >= 201103L
  193. , class = typename enable_if<is_quat<R> >::type
  194. #endif
  195. >
  196. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  197. operator R() const
  198. {
  199. R r;
  200. assign(r,*this);
  201. return r;
  202. }
  203. };
  204. }
  205. template <class T>
  206. struct
  207. quat_traits< qvm_detail::identity_quat_<T> >
  208. {
  209. typedef qvm_detail::identity_quat_<T> this_quaternion;
  210. typedef T scalar_type;
  211. template <int I>
  212. static
  213. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  214. scalar_type
  215. read_element( this_quaternion const & )
  216. {
  217. BOOST_QVM_STATIC_ASSERT(I>=0);
  218. BOOST_QVM_STATIC_ASSERT(I<4);
  219. return scalar_traits<T>::value(I==0);
  220. }
  221. static
  222. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  223. scalar_type
  224. read_element_idx( int i, this_quaternion const & )
  225. {
  226. BOOST_QVM_ASSERT(i>=0);
  227. BOOST_QVM_ASSERT(i<4);
  228. return scalar_traits<T>::value(i==0);
  229. }
  230. };
  231. template <class T>
  232. struct
  233. deduce_quat< qvm_detail::identity_quat_<T> >
  234. {
  235. typedef quat<T> type;
  236. };
  237. template <class T>
  238. struct
  239. deduce_quat2< qvm_detail::identity_quat_<T>, qvm_detail::identity_quat_<T> >
  240. {
  241. typedef quat<T> type;
  242. };
  243. template <class T>
  244. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  245. qvm_detail::identity_quat_<T> const &
  246. identity_quat()
  247. {
  248. return *(qvm_detail::identity_quat_<T> const *)qvm_detail::get_valid_ptr_quat_operations();
  249. }
  250. template <class A>
  251. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  252. typename enable_if_c<
  253. is_quat<A>::value,
  254. void>::type
  255. set_identity( A & a )
  256. {
  257. typedef typename quat_traits<A>::scalar_type T;
  258. T const zero=scalar_traits<T>::value(0);
  259. T const one=scalar_traits<T>::value(1);
  260. write_quat_element<0>(a,one);
  261. write_quat_element<1>(a,zero);
  262. write_quat_element<2>(a,zero);
  263. write_quat_element<3>(a,zero);
  264. }
  265. ////////////////////////////////////////////////
  266. namespace
  267. qvm_detail
  268. {
  269. template <class OriginalType,class Scalar>
  270. class
  271. quaternion_scalar_cast_
  272. {
  273. quaternion_scalar_cast_( quaternion_scalar_cast_ const & );
  274. quaternion_scalar_cast_ & operator=( quaternion_scalar_cast_ const & );
  275. ~quaternion_scalar_cast_();
  276. public:
  277. template <class T>
  278. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  279. quaternion_scalar_cast_ &
  280. operator=( T const & x )
  281. {
  282. assign(*this,x);
  283. return *this;
  284. }
  285. template <class R
  286. #if __cplusplus >= 201103L
  287. , class = typename enable_if<is_quat<R> >::type
  288. #endif
  289. >
  290. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  291. operator R() const
  292. {
  293. R r;
  294. assign(r,*this);
  295. return r;
  296. }
  297. };
  298. template <bool> struct scalar_cast_quaternion_filter { };
  299. template <> struct scalar_cast_quaternion_filter<true> { typedef int type; };
  300. }
  301. template <class OriginalType,class Scalar>
  302. struct
  303. quat_traits< qvm_detail::quaternion_scalar_cast_<OriginalType,Scalar> >
  304. {
  305. typedef Scalar scalar_type;
  306. typedef qvm_detail::quaternion_scalar_cast_<OriginalType,Scalar> this_quaternion;
  307. template <int I>
  308. static
  309. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  310. scalar_type
  311. read_element( this_quaternion const & x )
  312. {
  313. BOOST_QVM_STATIC_ASSERT(I>=0);
  314. BOOST_QVM_STATIC_ASSERT(I<4);
  315. return scalar_type(quat_traits<OriginalType>::template read_element<I>(reinterpret_cast<OriginalType const &>(x)));
  316. }
  317. static
  318. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  319. scalar_type
  320. read_element_idx( int i, this_quaternion const & x )
  321. {
  322. BOOST_QVM_ASSERT(i>=0);
  323. BOOST_QVM_ASSERT(i<4);
  324. return scalar_type(quat_traits<OriginalType>::read_element_idx(i,reinterpret_cast<OriginalType const &>(x)));
  325. }
  326. };
  327. template <class Scalar,class T>
  328. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  329. qvm_detail::quaternion_scalar_cast_<T,Scalar> const &
  330. scalar_cast( T const & x, typename qvm_detail::scalar_cast_quaternion_filter<is_quat<T>::value>::type=0 )
  331. {
  332. return reinterpret_cast<qvm_detail::quaternion_scalar_cast_<T,Scalar> const &>(x);
  333. }
  334. ////////////////////////////////////////////////
  335. template <class A,class B>
  336. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  337. typename enable_if_c<
  338. is_quat<A>::value && is_scalar<B>::value,
  339. A &>::type
  340. operator/=( A & a, B b )
  341. {
  342. write_quat_element<0>(a,quat_traits<A>::template read_element<0>(a)/b);
  343. write_quat_element<1>(a,quat_traits<A>::template read_element<1>(a)/b);
  344. write_quat_element<2>(a,quat_traits<A>::template read_element<2>(a)/b);
  345. write_quat_element<3>(a,quat_traits<A>::template read_element<3>(a)/b);
  346. return a;
  347. }
  348. template <class A,class B>
  349. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  350. typename lazy_enable_if_c<
  351. is_quat<A>::value && is_scalar<B>::value,
  352. deduce_quat2<A,B> >::type
  353. operator/( A const & a, B b )
  354. {
  355. typedef typename deduce_quat2<A,B>::type R;
  356. R r;
  357. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a)/b);
  358. write_quat_element<1>(r,quat_traits<A>::template read_element<1>(a)/b);
  359. write_quat_element<2>(r,quat_traits<A>::template read_element<2>(a)/b);
  360. write_quat_element<3>(r,quat_traits<A>::template read_element<3>(a)/b);
  361. return r;
  362. }
  363. template <class A,class B>
  364. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  365. typename lazy_enable_if_c<
  366. is_quat<A>::value && is_quat<B>::value,
  367. deduce_scalar<typename quat_traits<A>::scalar_type,typename quat_traits<B>::scalar_type> >::type
  368. dot( A const & a, B const & b )
  369. {
  370. typedef typename quat_traits<A>::scalar_type Ta;
  371. typedef typename quat_traits<B>::scalar_type Tb;
  372. typedef typename deduce_scalar<Ta,Tb>::type Tr;
  373. Ta const a0=quat_traits<A>::template read_element<0>(a);
  374. Ta const a1=quat_traits<A>::template read_element<1>(a);
  375. Ta const a2=quat_traits<A>::template read_element<2>(a);
  376. Ta const a3=quat_traits<A>::template read_element<3>(a);
  377. Tb const b0=quat_traits<B>::template read_element<0>(b);
  378. Tb const b1=quat_traits<B>::template read_element<1>(b);
  379. Tb const b2=quat_traits<B>::template read_element<2>(b);
  380. Tb const b3=quat_traits<B>::template read_element<3>(b);
  381. Tr const dp=a0*b0+a1*b1+a2*b2+a3*b3;
  382. return dp;
  383. }
  384. template <class A,class B>
  385. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  386. typename enable_if_c<
  387. is_quat<A>::value && is_quat<B>::value,
  388. bool>::type
  389. operator==( A const & a, B const & b )
  390. {
  391. return
  392. quat_traits<A>::template read_element<0>(a)==quat_traits<B>::template read_element<0>(b) &&
  393. quat_traits<A>::template read_element<1>(a)==quat_traits<B>::template read_element<1>(b) &&
  394. quat_traits<A>::template read_element<2>(a)==quat_traits<B>::template read_element<2>(b) &&
  395. quat_traits<A>::template read_element<3>(a)==quat_traits<B>::template read_element<3>(b);
  396. }
  397. template <class A>
  398. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  399. typename lazy_enable_if_c<
  400. is_quat<A>::value,
  401. deduce_quat<A> >::type
  402. inverse( A const & a )
  403. {
  404. typedef typename deduce_quat<A>::type R;
  405. typedef typename quat_traits<A>::scalar_type TA;
  406. TA aa = quat_traits<A>::template read_element<0>(a);
  407. TA ab = quat_traits<A>::template read_element<1>(a);
  408. TA ac = quat_traits<A>::template read_element<2>(a);
  409. TA ad = quat_traits<A>::template read_element<3>(a);
  410. TA m2 = ab*ab + ac*ac + ad*ad + aa*aa;
  411. if( m2==scalar_traits<TA>::value(0) )
  412. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  413. TA rm=scalar_traits<TA>::value(1)/m2;
  414. R r;
  415. write_quat_element<0>(r,aa*rm);
  416. write_quat_element<1>(r,-ab*rm);
  417. write_quat_element<2>(r,-ac*rm);
  418. write_quat_element<3>(r,-ad*rm);
  419. return r;
  420. }
  421. template <class A>
  422. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  423. typename enable_if_c<
  424. is_quat<A>::value,
  425. typename quat_traits<A>::scalar_type>::type
  426. mag_sqr( A const & a )
  427. {
  428. typedef typename quat_traits<A>::scalar_type T;
  429. T x=quat_traits<A>::template read_element<0>(a);
  430. T y=quat_traits<A>::template read_element<1>(a);
  431. T z=quat_traits<A>::template read_element<2>(a);
  432. T w=quat_traits<A>::template read_element<3>(a);
  433. return x*x+y*y+z*z+w*w;
  434. }
  435. template <class A>
  436. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  437. typename enable_if_c<
  438. is_quat<A>::value,
  439. typename quat_traits<A>::scalar_type>::type
  440. mag( A const & a )
  441. {
  442. typedef typename quat_traits<A>::scalar_type T;
  443. T x=quat_traits<A>::template read_element<0>(a);
  444. T y=quat_traits<A>::template read_element<1>(a);
  445. T z=quat_traits<A>::template read_element<2>(a);
  446. T w=quat_traits<A>::template read_element<3>(a);
  447. return sqrt(x*x+y*y+z*z+w*w);
  448. }
  449. template <class A,class B>
  450. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  451. typename enable_if<
  452. msvc_parse_bug_workaround::quats<A,B>,
  453. A &>::type
  454. operator-=( A & a, B const & b )
  455. {
  456. write_quat_element<0>(a,quat_traits<A>::template read_element<0>(a)-quat_traits<B>::template read_element<0>(b));
  457. write_quat_element<1>(a,quat_traits<A>::template read_element<1>(a)-quat_traits<B>::template read_element<1>(b));
  458. write_quat_element<2>(a,quat_traits<A>::template read_element<2>(a)-quat_traits<B>::template read_element<2>(b));
  459. write_quat_element<3>(a,quat_traits<A>::template read_element<3>(a)-quat_traits<B>::template read_element<3>(b));
  460. return a;
  461. }
  462. template <class A,class B>
  463. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  464. typename lazy_enable_if_c<
  465. is_quat<A>::value && is_quat<B>::value,
  466. deduce_quat2<A,B> >::type
  467. operator-( A const & a, B const & b )
  468. {
  469. typedef typename deduce_quat2<A,B>::type R;
  470. R r;
  471. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a)-quat_traits<B>::template read_element<0>(b));
  472. write_quat_element<1>(r,quat_traits<A>::template read_element<1>(a)-quat_traits<B>::template read_element<1>(b));
  473. write_quat_element<2>(r,quat_traits<A>::template read_element<2>(a)-quat_traits<B>::template read_element<2>(b));
  474. write_quat_element<3>(r,quat_traits<A>::template read_element<3>(a)-quat_traits<B>::template read_element<3>(b));
  475. return r;
  476. }
  477. template <class A>
  478. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  479. typename lazy_enable_if_c<
  480. is_quat<A>::value,
  481. deduce_quat<A> >::type
  482. operator-( A const & a )
  483. {
  484. typedef typename deduce_quat<A>::type R;
  485. R r;
  486. write_quat_element<0>(r,-quat_traits<A>::template read_element<0>(a));
  487. write_quat_element<1>(r,-quat_traits<A>::template read_element<1>(a));
  488. write_quat_element<2>(r,-quat_traits<A>::template read_element<2>(a));
  489. write_quat_element<3>(r,-quat_traits<A>::template read_element<3>(a));
  490. return r;
  491. }
  492. template <class A,class B>
  493. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  494. typename enable_if<
  495. msvc_parse_bug_workaround::quats<A,B>,
  496. A &>::type
  497. operator*=( A & a, B const & b )
  498. {
  499. typedef typename quat_traits<A>::scalar_type TA;
  500. typedef typename quat_traits<B>::scalar_type TB;
  501. TA const aa=quat_traits<A>::template read_element<0>(a);
  502. TA const ab=quat_traits<A>::template read_element<1>(a);
  503. TA const ac=quat_traits<A>::template read_element<2>(a);
  504. TA const ad=quat_traits<A>::template read_element<3>(a);
  505. TB const ba=quat_traits<B>::template read_element<0>(b);
  506. TB const bb=quat_traits<B>::template read_element<1>(b);
  507. TB const bc=quat_traits<B>::template read_element<2>(b);
  508. TB const bd=quat_traits<B>::template read_element<3>(b);
  509. write_quat_element<0>(a,aa*ba - ab*bb - ac*bc - ad*bd);
  510. write_quat_element<1>(a,aa*bb + ab*ba + ac*bd - ad*bc);
  511. write_quat_element<2>(a,aa*bc + ac*ba + ad*bb - ab*bd);
  512. write_quat_element<3>(a,aa*bd + ad*ba + ab*bc - ac*bb);
  513. return a;
  514. }
  515. template <class A,class B>
  516. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  517. typename enable_if_c<
  518. is_quat<A>::value && is_scalar<B>::value,
  519. A &>::type
  520. operator*=( A & a, B b )
  521. {
  522. write_quat_element<0>(a, quat_traits<A>::template read_element<0>(a)*b);
  523. write_quat_element<1>(a, quat_traits<A>::template read_element<1>(a)*b);
  524. write_quat_element<2>(a, quat_traits<A>::template read_element<2>(a)*b);
  525. write_quat_element<3>(a, quat_traits<A>::template read_element<3>(a)*b);
  526. return a;
  527. }
  528. template <class A,class B>
  529. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  530. typename lazy_enable_if_c<
  531. is_quat<A>::value && is_quat<B>::value,
  532. deduce_quat2<A,B> >::type
  533. operator*( A const & a, B const & b )
  534. {
  535. typedef typename deduce_quat2<A,B>::type R;
  536. typedef typename quat_traits<A>::scalar_type TA;
  537. typedef typename quat_traits<B>::scalar_type TB;
  538. TA const aa=quat_traits<A>::template read_element<0>(a);
  539. TA const ab=quat_traits<A>::template read_element<1>(a);
  540. TA const ac=quat_traits<A>::template read_element<2>(a);
  541. TA const ad=quat_traits<A>::template read_element<3>(a);
  542. TB const ba=quat_traits<B>::template read_element<0>(b);
  543. TB const bb=quat_traits<B>::template read_element<1>(b);
  544. TB const bc=quat_traits<B>::template read_element<2>(b);
  545. TB const bd=quat_traits<B>::template read_element<3>(b);
  546. R r;
  547. write_quat_element<0>(r,aa*ba - ab*bb - ac*bc - ad*bd);
  548. write_quat_element<1>(r,aa*bb + ab*ba + ac*bd - ad*bc);
  549. write_quat_element<2>(r,aa*bc + ac*ba + ad*bb - ab*bd);
  550. write_quat_element<3>(r,aa*bd + ad*ba + ab*bc - ac*bb);
  551. return r;
  552. }
  553. template <class A,class B>
  554. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  555. typename lazy_enable_if_c2<
  556. is_quat<A>::value && is_scalar<B>::value,
  557. deduce_quat2<A,B> >::type
  558. operator*( A const & a, B b )
  559. {
  560. typedef typename deduce_quat2<A,B>::type R;
  561. R r;
  562. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a)*b);
  563. write_quat_element<1>(r,quat_traits<A>::template read_element<1>(a)*b);
  564. write_quat_element<2>(r,quat_traits<A>::template read_element<2>(a)*b);
  565. write_quat_element<3>(r,quat_traits<A>::template read_element<3>(a)*b);
  566. return r;
  567. }
  568. template <class A,class B>
  569. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  570. typename enable_if_c<
  571. is_quat<A>::value && is_quat<B>::value,
  572. bool>::type
  573. operator!=( A const & a, B const & b )
  574. {
  575. return
  576. quat_traits<A>::template read_element<0>(a)!=quat_traits<B>::template read_element<0>(b) ||
  577. quat_traits<A>::template read_element<1>(a)!=quat_traits<B>::template read_element<1>(b) ||
  578. quat_traits<A>::template read_element<2>(a)!=quat_traits<B>::template read_element<2>(b) ||
  579. quat_traits<A>::template read_element<3>(a)!=quat_traits<B>::template read_element<3>(b);
  580. }
  581. template <class A>
  582. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  583. typename lazy_enable_if_c<
  584. is_quat<A>::value,
  585. deduce_quat<A> >::type
  586. normalized( A const & a )
  587. {
  588. typedef typename quat_traits<A>::scalar_type T;
  589. T const a0=quat_traits<A>::template read_element<0>(a);
  590. T const a1=quat_traits<A>::template read_element<1>(a);
  591. T const a2=quat_traits<A>::template read_element<2>(a);
  592. T const a3=quat_traits<A>::template read_element<3>(a);
  593. T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
  594. if( m2==scalar_traits<typename quat_traits<A>::scalar_type>::value(0) )
  595. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  596. T const rm=scalar_traits<T>::value(1)/sqrt(m2);
  597. typedef typename deduce_quat<A>::type R;
  598. R r;
  599. write_quat_element<0>(r,a0*rm);
  600. write_quat_element<1>(r,a1*rm);
  601. write_quat_element<2>(r,a2*rm);
  602. write_quat_element<3>(r,a3*rm);
  603. return r;
  604. }
  605. template <class A>
  606. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  607. typename enable_if_c<
  608. is_quat<A>::value,
  609. void>::type
  610. normalize( A & a )
  611. {
  612. typedef typename quat_traits<A>::scalar_type T;
  613. T const a0=quat_traits<A>::template read_element<0>(a);
  614. T const a1=quat_traits<A>::template read_element<1>(a);
  615. T const a2=quat_traits<A>::template read_element<2>(a);
  616. T const a3=quat_traits<A>::template read_element<3>(a);
  617. T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
  618. if( m2==scalar_traits<typename quat_traits<A>::scalar_type>::value(0) )
  619. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  620. T const rm=scalar_traits<T>::value(1)/sqrt(m2);
  621. write_quat_element<0>(a,quat_traits<A>::template read_element<0>(a)*rm);
  622. write_quat_element<1>(a,quat_traits<A>::template read_element<1>(a)*rm);
  623. write_quat_element<2>(a,quat_traits<A>::template read_element<2>(a)*rm);
  624. write_quat_element<3>(a,quat_traits<A>::template read_element<3>(a)*rm);
  625. }
  626. template <class A,class B>
  627. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  628. typename enable_if<
  629. msvc_parse_bug_workaround::quats<A,B>,
  630. A &>::type
  631. operator+=( A & a, B const & b )
  632. {
  633. write_quat_element<0>(a,quat_traits<A>::template read_element<0>(a)+quat_traits<B>::template read_element<0>(b));
  634. write_quat_element<1>(a,quat_traits<A>::template read_element<1>(a)+quat_traits<B>::template read_element<1>(b));
  635. write_quat_element<2>(a,quat_traits<A>::template read_element<2>(a)+quat_traits<B>::template read_element<2>(b));
  636. write_quat_element<3>(a,quat_traits<A>::template read_element<3>(a)+quat_traits<B>::template read_element<3>(b));
  637. return a;
  638. }
  639. template <class A,class B>
  640. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  641. typename lazy_enable_if_c<
  642. is_quat<A>::value && is_quat<B>::value,
  643. deduce_quat2<A,B> >::type
  644. operator+( A const & a, B const & b )
  645. {
  646. typedef typename deduce_quat2<A,B>::type R;
  647. R r;
  648. write_quat_element<0>(r,quat_traits<A>::template read_element<0>(a)+quat_traits<B>::template read_element<0>(b));
  649. write_quat_element<1>(r,quat_traits<A>::template read_element<1>(a)+quat_traits<B>::template read_element<1>(b));
  650. write_quat_element<2>(r,quat_traits<A>::template read_element<2>(a)+quat_traits<B>::template read_element<2>(b));
  651. write_quat_element<3>(r,quat_traits<A>::template read_element<3>(a)+quat_traits<B>::template read_element<3>(b));
  652. return r;
  653. }
  654. template <class A,class B,class C>
  655. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  656. typename lazy_enable_if_c<
  657. is_quat<A>::value && is_quat<B>::value && is_scalar<C>::value,
  658. deduce_quat2<A,B> >::type
  659. slerp360( A const & a, B const & b, C t )
  660. {
  661. typedef typename deduce_quat2<A,B>::type R;
  662. typedef typename quat_traits<R>::scalar_type TR;
  663. TR const one = scalar_traits<TR>::value(1);
  664. TR const threshold = one - one / scalar_traits<TR>::value(2000); //0.9995
  665. TR const dp = dot(a,b);
  666. TR const abs_dp = abs(dp);
  667. if( abs_dp > threshold )
  668. return a*(one-t) + b*t;
  669. TR const th = acos(dp);
  670. TR const invsinth = one / sin(th);
  671. return a * (sin(th * (one-t)) * invsinth) + b * (sin(th * t) * invsinth);
  672. }
  673. template <class A,class B,class C>
  674. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  675. typename lazy_enable_if_c<
  676. is_quat<A>::value && is_quat<B>::value && is_scalar<C>::value,
  677. deduce_quat2<A,B> >::type
  678. slerp180( A const & a, B const & b, C t )
  679. {
  680. typedef typename deduce_quat2<A,B>::type R;
  681. typedef typename quat_traits<R>::scalar_type TR;
  682. TR const one = scalar_traits<TR>::value(1);
  683. TR const threshold = one - one / scalar_traits<TR>::value(2000); //0.9995
  684. TR const dp = dot(a,b);
  685. TR const abs_dp = abs(dp);
  686. if( abs_dp > threshold )
  687. return a*(one-t)*sign(dp) + b*t;
  688. TR const th = acos(abs_dp);
  689. TR const invsinth = one / sin(th);
  690. return a * (sin(th * (one-t)) * invsinth * sign(dp)) + b * (sin(th * t) * invsinth);
  691. }
  692. template <class A,class B,class C>
  693. BOOST_QVM_DEPRECATED("please use slerp180 or slerp360")
  694. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  695. typename lazy_enable_if_c<
  696. is_quat<A>::value && is_quat<B>::value && is_scalar<C>::value,
  697. deduce_quat2<A,B> >::type
  698. slerp( A const & a, B const & b, C t )
  699. {
  700. return slerp360(a, b, t);
  701. }
  702. ////////////////////////////////////////////////
  703. namespace
  704. qvm_detail
  705. {
  706. template <class T>
  707. class
  708. qref_
  709. {
  710. qref_( qref_ const & );
  711. qref_ & operator=( qref_ const & );
  712. ~qref_();
  713. public:
  714. template <class R>
  715. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  716. qref_ &
  717. operator=( R const & x )
  718. {
  719. assign(*this,x);
  720. return *this;
  721. }
  722. template <class R
  723. #if __cplusplus >= 201103L
  724. , class = typename enable_if<is_quat<R> >::type
  725. #endif
  726. >
  727. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  728. operator R() const
  729. {
  730. R r;
  731. assign(r,*this);
  732. return r;
  733. }
  734. };
  735. template <class Q,bool WriteElementRef=quat_write_element_ref<Q>::value>
  736. struct qref_write_traits;
  737. template <class Q>
  738. struct
  739. qref_write_traits<Q,true>
  740. {
  741. typedef typename quat_traits<Q>::scalar_type scalar_type;
  742. typedef qvm_detail::qref_<Q> this_quaternion;
  743. template <int I>
  744. static
  745. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  746. scalar_type &
  747. write_element( this_quaternion & x )
  748. {
  749. BOOST_QVM_STATIC_ASSERT(I>=0);
  750. BOOST_QVM_STATIC_ASSERT(I<4);
  751. return quat_traits<Q>::template write_element<I>(reinterpret_cast<Q &>(x));
  752. }
  753. };
  754. template <class Q>
  755. struct
  756. qref_write_traits<Q,false>
  757. {
  758. typedef typename quat_traits<Q>::scalar_type scalar_type;
  759. typedef qvm_detail::qref_<Q> this_quaternion;
  760. template <int I>
  761. static
  762. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  763. void
  764. write_element( this_quaternion & x, scalar_type s )
  765. {
  766. BOOST_QVM_STATIC_ASSERT(I>=0);
  767. BOOST_QVM_STATIC_ASSERT(I<4);
  768. quat_traits<Q>::template write_element<I>(reinterpret_cast<Q &>(x), s);
  769. }
  770. };
  771. }
  772. template <class Q>
  773. struct quat_traits;
  774. template <class Q>
  775. struct
  776. quat_traits< qvm_detail::qref_<Q> >:
  777. qvm_detail::qref_write_traits<Q>
  778. {
  779. typedef typename quat_traits<Q>::scalar_type scalar_type;
  780. typedef qvm_detail::qref_<Q> this_quaternion;
  781. template <int I>
  782. static
  783. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  784. scalar_type
  785. read_element( this_quaternion const & x )
  786. {
  787. BOOST_QVM_STATIC_ASSERT(I>=0);
  788. BOOST_QVM_STATIC_ASSERT(I<4);
  789. return quat_traits<Q>::template read_element<I>(reinterpret_cast<Q const &>(x));
  790. }
  791. };
  792. template <class Q>
  793. struct
  794. deduce_quat< qvm_detail::qref_<Q> >
  795. {
  796. typedef quat<typename quat_traits<Q>::scalar_type> type;
  797. };
  798. template <class Q>
  799. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  800. typename enable_if_c<
  801. is_quat<Q>::value,
  802. qvm_detail::qref_<Q> const &>::type
  803. qref( Q const & a )
  804. {
  805. return reinterpret_cast<qvm_detail::qref_<Q> const &>(a);
  806. }
  807. template <class Q>
  808. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  809. typename enable_if_c<
  810. is_quat<Q>::value,
  811. qvm_detail::qref_<Q> &>::type
  812. qref( Q & a )
  813. {
  814. return reinterpret_cast<qvm_detail::qref_<Q> &>(a);
  815. }
  816. ////////////////////////////////////////////////
  817. namespace
  818. qvm_detail
  819. {
  820. template <class T>
  821. class
  822. zero_q_
  823. {
  824. zero_q_( zero_q_ const & );
  825. zero_q_ & operator=( zero_q_ const & );
  826. ~zero_q_();
  827. public:
  828. template <class R
  829. #if __cplusplus >= 201103L
  830. , class = typename enable_if<is_quat<R> >::type
  831. #endif
  832. >
  833. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  834. operator R() const
  835. {
  836. R r;
  837. assign(r,*this);
  838. return r;
  839. }
  840. };
  841. }
  842. template <class T>
  843. struct
  844. quat_traits< qvm_detail::zero_q_<T> >
  845. {
  846. typedef qvm_detail::zero_q_<T> this_quaternion;
  847. typedef T scalar_type;
  848. template <int I>
  849. static
  850. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  851. scalar_type
  852. read_element( this_quaternion const & )
  853. {
  854. BOOST_QVM_STATIC_ASSERT(I>=0);
  855. BOOST_QVM_STATIC_ASSERT(I<4);
  856. return scalar_traits<scalar_type>::value(0);
  857. }
  858. static
  859. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  860. scalar_type
  861. read_element_idx( int i, this_quaternion const & )
  862. {
  863. BOOST_QVM_ASSERT(i>=0);
  864. BOOST_QVM_ASSERT(i<4);
  865. return scalar_traits<scalar_type>::value(0);
  866. }
  867. };
  868. template <class T>
  869. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  870. qvm_detail::zero_q_<T> const &
  871. zero_quat()
  872. {
  873. return *(qvm_detail::zero_q_<T> const *)qvm_detail::get_valid_ptr_quat_operations();
  874. }
  875. template <class A>
  876. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  877. typename enable_if_c<
  878. is_quat<A>::value,
  879. void>::type
  880. set_zero( A & a )
  881. {
  882. typedef typename quat_traits<A>::scalar_type T;
  883. T const zero=scalar_traits<T>::value(0);
  884. write_quat_element<0>(a,zero);
  885. write_quat_element<1>(a,zero);
  886. write_quat_element<2>(a,zero);
  887. write_quat_element<3>(a,zero);
  888. }
  889. ////////////////////////////////////////////////
  890. namespace
  891. qvm_detail
  892. {
  893. template <class V>
  894. struct
  895. rot_quat_
  896. {
  897. typedef typename vec_traits<V>::scalar_type scalar_type;
  898. scalar_type a[4];
  899. template <class Angle>
  900. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE
  901. rot_quat_( V const & axis, Angle angle )
  902. {
  903. scalar_type const x=vec_traits<V>::template read_element<0>(axis);
  904. scalar_type const y=vec_traits<V>::template read_element<1>(axis);
  905. scalar_type const z=vec_traits<V>::template read_element<2>(axis);
  906. scalar_type const m2=x*x+y*y+z*z;
  907. if( m2==scalar_traits<scalar_type>::value(0) )
  908. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  909. scalar_type const rm=scalar_traits<scalar_type>::value(1)/sqrt(m2);
  910. angle/=2;
  911. scalar_type const s=sin(angle);
  912. a[0] = cos(angle);
  913. a[1] = rm*x*s;
  914. a[2] = rm*y*s;
  915. a[3] = rm*z*s;
  916. }
  917. template <class R
  918. #if __cplusplus >= 201103L
  919. , class = typename enable_if<is_quat<R> >::type
  920. #endif
  921. >
  922. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  923. operator R() const
  924. {
  925. R r;
  926. assign(r,*this);
  927. return r;
  928. }
  929. };
  930. }
  931. template <class V>
  932. struct
  933. quat_traits< qvm_detail::rot_quat_<V> >
  934. {
  935. typedef qvm_detail::rot_quat_<V> this_quaternion;
  936. typedef typename this_quaternion::scalar_type scalar_type;
  937. template <int I>
  938. static
  939. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  940. scalar_type
  941. read_element( this_quaternion const & x )
  942. {
  943. BOOST_QVM_STATIC_ASSERT(I>=0);
  944. BOOST_QVM_STATIC_ASSERT(I<4);
  945. return x.a[I];
  946. }
  947. };
  948. template <class V>
  949. struct
  950. deduce_quat< qvm_detail::rot_quat_<V> >
  951. {
  952. typedef quat<typename vec_traits<V>::scalar_type> type;
  953. };
  954. template <class A,class Angle>
  955. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE
  956. typename enable_if_c<
  957. is_vec<A>::value && vec_traits<A>::dim==3,
  958. qvm_detail::rot_quat_<A> >::type
  959. rot_quat( A const & axis, Angle angle )
  960. {
  961. return qvm_detail::rot_quat_<A>(axis,angle);
  962. }
  963. template <class A,class B,class Angle>
  964. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  965. typename enable_if_c<
  966. is_quat<A>::value &&
  967. is_vec<B>::value && vec_traits<B>::dim==3,
  968. void>::type
  969. set_rot( A & a, B const & axis, Angle angle )
  970. {
  971. assign(a,rot_quat(axis,angle));
  972. }
  973. template <class A,class B,class Angle>
  974. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  975. typename enable_if_c<
  976. is_quat<A>::value &&
  977. is_vec<B>::value && vec_traits<B>::dim==3,
  978. void>::type
  979. rotate( A & a, B const & axis, Angle angle )
  980. {
  981. a *= rot_quat(axis,angle);
  982. }
  983. ////////////////////////////////////////////////
  984. namespace
  985. qvm_detail
  986. {
  987. template <class T>
  988. struct
  989. rotx_quat_
  990. {
  991. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  992. rotx_quat_()
  993. {
  994. }
  995. template <class R
  996. #if __cplusplus >= 201103L
  997. , class = typename enable_if<is_quat<R> >::type
  998. #endif
  999. >
  1000. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1001. operator R() const
  1002. {
  1003. R r;
  1004. assign(r,*this);
  1005. return r;
  1006. }
  1007. private:
  1008. rotx_quat_( rotx_quat_ const & );
  1009. rotx_quat_ & operator=( rotx_quat_ const & );
  1010. ~rotx_quat_();
  1011. };
  1012. template <int I>
  1013. struct
  1014. rotx_q_get
  1015. {
  1016. template <class T>
  1017. static
  1018. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1019. T
  1020. get( T const & )
  1021. {
  1022. return scalar_traits<T>::value(0);
  1023. }
  1024. };
  1025. template <>
  1026. struct
  1027. rotx_q_get<1>
  1028. {
  1029. template <class T>
  1030. static
  1031. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1032. T
  1033. get( T const & angle )
  1034. {
  1035. return sin(angle/2);
  1036. }
  1037. };
  1038. template <>
  1039. struct
  1040. rotx_q_get<0>
  1041. {
  1042. template <class T>
  1043. static
  1044. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1045. T
  1046. get( T const & angle )
  1047. {
  1048. return cos(angle/2);
  1049. }
  1050. };
  1051. }
  1052. template <class Angle>
  1053. struct
  1054. quat_traits< qvm_detail::rotx_quat_<Angle> >
  1055. {
  1056. typedef qvm_detail::rotx_quat_<Angle> this_quaternion;
  1057. typedef Angle scalar_type;
  1058. template <int I>
  1059. static
  1060. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1061. scalar_type
  1062. read_element( this_quaternion const & x )
  1063. {
  1064. BOOST_QVM_STATIC_ASSERT(I>=0);
  1065. BOOST_QVM_STATIC_ASSERT(I<4);
  1066. return qvm_detail::rotx_q_get<I>::get(reinterpret_cast<Angle const &>(x));
  1067. }
  1068. };
  1069. template <class Angle>
  1070. struct
  1071. deduce_quat< qvm_detail::rotx_quat_<Angle> >
  1072. {
  1073. typedef quat<Angle> type;
  1074. };
  1075. template <class Angle>
  1076. struct
  1077. deduce_quat2< qvm_detail::rotx_quat_<Angle>, qvm_detail::rotx_quat_<Angle> >
  1078. {
  1079. typedef quat<Angle> type;
  1080. };
  1081. template <class Angle>
  1082. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1083. qvm_detail::rotx_quat_<Angle> const &
  1084. rotx_quat( Angle const & angle )
  1085. {
  1086. return reinterpret_cast<qvm_detail::rotx_quat_<Angle> const &>(angle);
  1087. }
  1088. template <class A,class Angle>
  1089. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1090. typename enable_if_c<
  1091. is_quat<A>::value,
  1092. void>::type
  1093. set_rotx( A & a, Angle angle )
  1094. {
  1095. assign(a,rotx_quat(angle));
  1096. }
  1097. template <class A,class Angle>
  1098. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1099. typename enable_if_c<
  1100. is_quat<A>::value,
  1101. void>::type
  1102. rotate_x( A & a, Angle angle )
  1103. {
  1104. a *= rotx_quat(angle);
  1105. }
  1106. ////////////////////////////////////////////////
  1107. namespace
  1108. qvm_detail
  1109. {
  1110. template <class T>
  1111. struct
  1112. roty_quat_
  1113. {
  1114. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1115. roty_quat_()
  1116. {
  1117. }
  1118. template <class R
  1119. #if __cplusplus >= 201103L
  1120. , class = typename enable_if<is_quat<R> >::type
  1121. #endif
  1122. >
  1123. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1124. operator R() const
  1125. {
  1126. R r;
  1127. assign(r,*this);
  1128. return r;
  1129. }
  1130. private:
  1131. roty_quat_( roty_quat_ const & );
  1132. roty_quat_ & operator=( roty_quat_ const & );
  1133. ~roty_quat_();
  1134. };
  1135. template <int I>
  1136. struct
  1137. roty_q_get
  1138. {
  1139. template <class T>
  1140. static
  1141. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1142. T
  1143. get( T const & )
  1144. {
  1145. return scalar_traits<T>::value(0);
  1146. }
  1147. };
  1148. template <>
  1149. struct
  1150. roty_q_get<2>
  1151. {
  1152. template <class T>
  1153. static
  1154. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1155. T
  1156. get( T const & angle )
  1157. {
  1158. return sin(angle/2);
  1159. }
  1160. };
  1161. template <>
  1162. struct
  1163. roty_q_get<0>
  1164. {
  1165. template <class T>
  1166. static
  1167. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1168. T
  1169. get( T const & angle )
  1170. {
  1171. return cos(angle/2);
  1172. }
  1173. };
  1174. }
  1175. template <class Angle>
  1176. struct
  1177. quat_traits< qvm_detail::roty_quat_<Angle> >
  1178. {
  1179. typedef qvm_detail::roty_quat_<Angle> this_quaternion;
  1180. typedef Angle scalar_type;
  1181. template <int I>
  1182. static
  1183. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1184. scalar_type
  1185. read_element( this_quaternion const & x )
  1186. {
  1187. BOOST_QVM_STATIC_ASSERT(I>=0);
  1188. BOOST_QVM_STATIC_ASSERT(I<4);
  1189. return qvm_detail::roty_q_get<I>::get(reinterpret_cast<Angle const &>(x));
  1190. }
  1191. };
  1192. template <class Angle>
  1193. struct
  1194. deduce_quat< qvm_detail::roty_quat_<Angle> >
  1195. {
  1196. typedef quat<Angle> type;
  1197. };
  1198. template <class Angle>
  1199. struct
  1200. deduce_quat2< qvm_detail::roty_quat_<Angle>, qvm_detail::roty_quat_<Angle> >
  1201. {
  1202. typedef quat<Angle> type;
  1203. };
  1204. template <class Angle>
  1205. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1206. qvm_detail::roty_quat_<Angle> const &
  1207. roty_quat( Angle const & angle )
  1208. {
  1209. return reinterpret_cast<qvm_detail::roty_quat_<Angle> const &>(angle);
  1210. }
  1211. template <class A,class Angle>
  1212. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1213. typename enable_if_c<
  1214. is_quat<A>::value,
  1215. void>::type
  1216. set_roty( A & a, Angle angle )
  1217. {
  1218. assign(a,roty_quat(angle));
  1219. }
  1220. template <class A,class Angle>
  1221. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1222. typename enable_if_c<
  1223. is_quat<A>::value,
  1224. void>::type
  1225. rotate_y( A & a, Angle angle )
  1226. {
  1227. a *= roty_quat(angle);
  1228. }
  1229. ////////////////////////////////////////////////
  1230. namespace
  1231. qvm_detail
  1232. {
  1233. template <class T>
  1234. struct
  1235. rotz_quat_
  1236. {
  1237. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1238. rotz_quat_()
  1239. {
  1240. }
  1241. template <class R
  1242. #if __cplusplus >= 201103L
  1243. , class = typename enable_if<is_quat<R> >::type
  1244. #endif
  1245. >
  1246. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1247. operator R() const
  1248. {
  1249. R r;
  1250. assign(r,*this);
  1251. return r;
  1252. }
  1253. private:
  1254. rotz_quat_( rotz_quat_ const & );
  1255. rotz_quat_ & operator=( rotz_quat_ const & );
  1256. ~rotz_quat_();
  1257. };
  1258. template <int I>
  1259. struct
  1260. rotz_q_get
  1261. {
  1262. template <class T>
  1263. static
  1264. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1265. T
  1266. get( T const & )
  1267. {
  1268. return scalar_traits<T>::value(0);
  1269. }
  1270. };
  1271. template <>
  1272. struct
  1273. rotz_q_get<3>
  1274. {
  1275. template <class T>
  1276. static
  1277. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1278. T
  1279. get( T const & angle )
  1280. {
  1281. return sin(angle/2);
  1282. }
  1283. };
  1284. template <>
  1285. struct
  1286. rotz_q_get<0>
  1287. {
  1288. template <class T>
  1289. static
  1290. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1291. T
  1292. get( T const & angle )
  1293. {
  1294. return cos(angle/2);
  1295. }
  1296. };
  1297. }
  1298. template <class Angle>
  1299. struct
  1300. quat_traits< qvm_detail::rotz_quat_<Angle> >
  1301. {
  1302. typedef qvm_detail::rotz_quat_<Angle> this_quaternion;
  1303. typedef Angle scalar_type;
  1304. template <int I>
  1305. static
  1306. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  1307. scalar_type
  1308. read_element( this_quaternion const & x )
  1309. {
  1310. BOOST_QVM_STATIC_ASSERT(I>=0);
  1311. BOOST_QVM_STATIC_ASSERT(I<4);
  1312. return qvm_detail::rotz_q_get<I>::get(reinterpret_cast<Angle const &>(x));
  1313. }
  1314. };
  1315. template <class Angle>
  1316. struct
  1317. deduce_quat< qvm_detail::rotz_quat_<Angle> >
  1318. {
  1319. typedef quat<Angle> type;
  1320. };
  1321. template <class Angle>
  1322. struct
  1323. deduce_quat2< qvm_detail::rotz_quat_<Angle>, qvm_detail::rotz_quat_<Angle> >
  1324. {
  1325. typedef quat<Angle> type;
  1326. };
  1327. template <class Angle>
  1328. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  1329. qvm_detail::rotz_quat_<Angle> const &
  1330. rotz_quat( Angle const & angle )
  1331. {
  1332. return reinterpret_cast<qvm_detail::rotz_quat_<Angle> const &>(angle);
  1333. }
  1334. template <class A,class Angle>
  1335. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1336. typename enable_if_c<
  1337. is_quat<A>::value,
  1338. void>::type
  1339. set_rotz( A & a, Angle angle )
  1340. {
  1341. assign(a,rotz_quat(angle));
  1342. }
  1343. template <class A,class Angle>
  1344. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1345. typename enable_if_c<
  1346. is_quat<A>::value,
  1347. void>::type
  1348. rotate_z( A & a, Angle angle )
  1349. {
  1350. a *= rotz_quat(angle);
  1351. }
  1352. template <class A,class B>
  1353. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
  1354. typename enable_if_c<
  1355. is_quat<A>::value && is_vec<B>::value && vec_traits<B>::dim==3,
  1356. typename quat_traits<A>::scalar_type>::type
  1357. axis_angle( A const & a, B & b )
  1358. {
  1359. typedef typename quat_traits<A>::scalar_type T;
  1360. T a0=quat_traits<A>::template read_element<0>(a);
  1361. T a1=quat_traits<A>::template read_element<1>(a);
  1362. T a2=quat_traits<A>::template read_element<2>(a);
  1363. T a3=quat_traits<A>::template read_element<3>(a);
  1364. if( a0>1 )
  1365. {
  1366. T const m2=a0*a0+a1*a1+a2*a2+a3*a3;
  1367. if( m2==scalar_traits<T>::value(0) )
  1368. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  1369. T const s=sqrt(m2);
  1370. a0/=s;
  1371. a1/=s;
  1372. a2/=s;
  1373. a3/=s;
  1374. }
  1375. if( T s=sqrt(1-a0*a0) )
  1376. {
  1377. write_vec_element<0>(b, a1/s);
  1378. write_vec_element<1>(b, a2/s);
  1379. write_vec_element<2>(b, a3/s);
  1380. }
  1381. else
  1382. {
  1383. typedef typename vec_traits<B>::scalar_type U;
  1384. write_vec_element<0>(b, scalar_traits<U>::value(1));
  1385. write_vec_element<1>(b, scalar_traits<U>::value(0));
  1386. write_vec_element<2>(b, scalar_traits<U>::value(0));
  1387. }
  1388. return scalar_traits<T>::value(2) * qvm::acos(a0);
  1389. }
  1390. ////////////////////////////////////////////////
  1391. namespace
  1392. sfinae
  1393. {
  1394. using ::boost::qvm::assign;
  1395. using ::boost::qvm::cmp;
  1396. using ::boost::qvm::convert_to;
  1397. using ::boost::qvm::conjugate;
  1398. using ::boost::qvm::set_identity;
  1399. using ::boost::qvm::set_zero;
  1400. using ::boost::qvm::scalar_cast;
  1401. using ::boost::qvm::operator/=;
  1402. using ::boost::qvm::operator/;
  1403. using ::boost::qvm::dot;
  1404. using ::boost::qvm::operator==;
  1405. using ::boost::qvm::inverse;
  1406. using ::boost::qvm::mag_sqr;
  1407. using ::boost::qvm::mag;
  1408. using ::boost::qvm::slerp360;
  1409. using ::boost::qvm::slerp180;
  1410. using ::boost::qvm::slerp;
  1411. using ::boost::qvm::operator-=;
  1412. using ::boost::qvm::operator-;
  1413. using ::boost::qvm::operator*=;
  1414. using ::boost::qvm::operator*;
  1415. using ::boost::qvm::operator!=;
  1416. using ::boost::qvm::normalized;
  1417. using ::boost::qvm::normalize;
  1418. using ::boost::qvm::operator+=;
  1419. using ::boost::qvm::operator+;
  1420. using ::boost::qvm::qref;
  1421. using ::boost::qvm::rot_quat;
  1422. using ::boost::qvm::set_rot;
  1423. using ::boost::qvm::rotate;
  1424. using ::boost::qvm::rotx_quat;
  1425. using ::boost::qvm::set_rotx;
  1426. using ::boost::qvm::rotate_x;
  1427. using ::boost::qvm::roty_quat;
  1428. using ::boost::qvm::set_roty;
  1429. using ::boost::qvm::rotate_y;
  1430. using ::boost::qvm::rotz_quat;
  1431. using ::boost::qvm::set_rotz;
  1432. using ::boost::qvm::rotate_z;
  1433. }
  1434. } }
  1435. #endif