vec_operations.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. //Copyright (c) 2008-2016 Emil Dotchevski and Reverge Studios, Inc.
  2. //Copyright (c) 2019 agate-pris
  3. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_QVM_384AFF3AD23A11DFA80B754FE0D72085
  6. #define BOOST_QVM_384AFF3AD23A11DFA80B754FE0D72085
  7. #include <boost/qvm/detail/vec_assign.hpp>
  8. #include <boost/qvm/vec_operations2.hpp>
  9. #include <boost/qvm/vec_operations3.hpp>
  10. #include <boost/qvm/vec_operations4.hpp>
  11. #include <boost/qvm/assert.hpp>
  12. #include <boost/qvm/scalar_traits.hpp>
  13. #include <string>
  14. namespace
  15. boost
  16. {
  17. namespace
  18. qvm
  19. {
  20. namespace
  21. qvm_detail
  22. {
  23. BOOST_QVM_INLINE_CRITICAL
  24. void const *
  25. get_valid_ptr_vec_operations()
  26. {
  27. static int const obj=0;
  28. return &obj;
  29. }
  30. }
  31. ////////////////////////////////////////////////
  32. namespace
  33. qvm_to_string_detail
  34. {
  35. template <class T>
  36. std::string to_string( T const & x );
  37. }
  38. namespace
  39. qvm_detail
  40. {
  41. template <int D>
  42. struct
  43. to_string_v_defined
  44. {
  45. static bool const value=false;
  46. };
  47. template <int I,int DimMinusOne>
  48. struct
  49. to_string_vector_elements
  50. {
  51. template <class A>
  52. static
  53. std::string
  54. f( A const & a )
  55. {
  56. using namespace qvm_to_string_detail;
  57. return to_string(vec_traits<A>::template read_element<I>(a))+','+to_string_vector_elements<I+1,DimMinusOne>::f(a);
  58. }
  59. };
  60. template <int DimMinusOne>
  61. struct
  62. to_string_vector_elements<DimMinusOne,DimMinusOne>
  63. {
  64. template <class A>
  65. static
  66. std::string
  67. f( A const & a )
  68. {
  69. using namespace qvm_to_string_detail;
  70. return to_string(vec_traits<A>::template read_element<DimMinusOne>(a));
  71. }
  72. };
  73. }
  74. template <class A>
  75. inline
  76. typename boost::enable_if_c<
  77. is_vec<A>::value &&
  78. !qvm_detail::to_string_v_defined<vec_traits<A>::dim>::value,
  79. std::string>::type
  80. to_string( A const & a )
  81. {
  82. return '('+qvm_detail::to_string_vector_elements<0,vec_traits<A>::dim-1>::f(a)+')';
  83. }
  84. ////////////////////////////////////////////////
  85. namespace
  86. qvm_detail
  87. {
  88. template <int D>
  89. struct
  90. convert_to_v_defined
  91. {
  92. static bool const value=false;
  93. };
  94. }
  95. template <class R,class A>
  96. BOOST_QVM_INLINE_TRIVIAL
  97. typename enable_if_c<
  98. is_vec<R>::value && is_vec<A>::value &&
  99. vec_traits<R>::dim==vec_traits<A>::dim &&
  100. !qvm_detail::convert_to_v_defined<vec_traits<R>::dim>::value,
  101. R>::type
  102. convert_to( A const & a )
  103. {
  104. R r; assign(r,a);
  105. return r;
  106. }
  107. ////////////////////////////////////////////////
  108. template <class A,class B>
  109. BOOST_QVM_INLINE_OPERATIONS
  110. typename lazy_enable_if_c<
  111. is_vec<A>::value && is_vec<B>::value &&
  112. vec_traits<A>::dim==3 && vec_traits<B>::dim==3,
  113. deduce_vec2<A,B,3> >::type
  114. cross( A const & a, B const & b )
  115. {
  116. typedef typename deduce_vec2<A,B,3>::type R;
  117. R r;
  118. vec_traits<R>::template write_element<0>(r)=
  119. vec_traits<A>::template read_element<1>(a)*vec_traits<B>::template read_element<2>(b)-
  120. vec_traits<A>::template read_element<2>(a)*vec_traits<B>::template read_element<1>(b);
  121. vec_traits<R>::template write_element<1>(r)=
  122. vec_traits<A>::template read_element<2>(a)*vec_traits<B>::template read_element<0>(b)-
  123. vec_traits<A>::template read_element<0>(a)*vec_traits<B>::template read_element<2>(b);
  124. vec_traits<R>::template write_element<2>(r)=
  125. vec_traits<A>::template read_element<0>(a)*vec_traits<B>::template read_element<1>(b)-
  126. vec_traits<A>::template read_element<1>(a)*vec_traits<B>::template read_element<0>(b);
  127. return r;
  128. }
  129. ////////////////////////////////////////////////
  130. template <class A,class B,class Cmp>
  131. BOOST_QVM_INLINE_OPERATIONS
  132. typename enable_if_c<
  133. is_vec<A>::value && is_vec<B>::value &&
  134. vec_traits<A>::dim==vec_traits<B>::dim,
  135. bool>::type
  136. cmp( A const & a, B const & b, Cmp f )
  137. {
  138. for( int i=0; i!=vec_traits<A>::dim; ++i )
  139. if( !f(
  140. vec_traits<A>::read_element_idx(i,a),
  141. vec_traits<B>::read_element_idx(i,b)) )
  142. return false;
  143. return true;
  144. }
  145. ////////////////////////////////////////////////
  146. namespace
  147. qvm_detail
  148. {
  149. template <class T,int Dim>
  150. class
  151. zero_vec_
  152. {
  153. zero_vec_( zero_vec_ const & );
  154. zero_vec_ & operator=( zero_vec_ const & );
  155. ~zero_vec_();
  156. public:
  157. template <class R>
  158. BOOST_QVM_INLINE_TRIVIAL
  159. operator R() const
  160. {
  161. R r;
  162. assign(r,*this);
  163. return r;
  164. }
  165. };
  166. }
  167. template <class V>
  168. struct vec_traits;
  169. template <class T,int Dim>
  170. struct
  171. vec_traits< qvm_detail::zero_vec_<T,Dim> >
  172. {
  173. typedef qvm_detail::zero_vec_<T,Dim> this_vector;
  174. typedef T scalar_type;
  175. static int const dim=Dim;
  176. template <int I>
  177. static
  178. BOOST_QVM_INLINE_CRITICAL
  179. scalar_type
  180. read_element( this_vector const & )
  181. {
  182. BOOST_QVM_STATIC_ASSERT(I>=0);
  183. BOOST_QVM_STATIC_ASSERT(I<Dim);
  184. return scalar_traits<scalar_type>::value(0);
  185. }
  186. static
  187. BOOST_QVM_INLINE_CRITICAL
  188. scalar_type
  189. read_element_idx( int i, this_vector const & )
  190. {
  191. BOOST_QVM_ASSERT(i>=0);
  192. BOOST_QVM_ASSERT(i<Dim);
  193. return scalar_traits<scalar_type>::value(0);
  194. }
  195. };
  196. template <class T,int Dim,int D>
  197. struct
  198. deduce_vec<qvm_detail::zero_vec_<T,Dim>,D>
  199. {
  200. typedef vec<T,D> type;
  201. };
  202. template <class T,int Dim>
  203. BOOST_QVM_INLINE_TRIVIAL
  204. qvm_detail::zero_vec_<T,Dim> const &
  205. zero_vec()
  206. {
  207. return *(qvm_detail::zero_vec_<T,Dim> const *)qvm_detail::get_valid_ptr_vec_operations();
  208. }
  209. template <class A>
  210. BOOST_QVM_INLINE_OPERATIONS
  211. typename enable_if_c<
  212. is_vec<A>::value,
  213. void>::type
  214. set_zero( A & a )
  215. {
  216. assign(a,zero_vec<typename vec_traits<A>::scalar_type,vec_traits<A>::dim>());
  217. }
  218. ////////////////////////////////////////////////
  219. namespace
  220. qvm_detail
  221. {
  222. template <class OriginalType,class Scalar>
  223. class
  224. vector_scalar_cast_
  225. {
  226. vector_scalar_cast_( vector_scalar_cast_ const & );
  227. vector_scalar_cast_ & operator=( vector_scalar_cast_ const & );
  228. ~vector_scalar_cast_();
  229. public:
  230. template <class T>
  231. BOOST_QVM_INLINE_TRIVIAL
  232. vector_scalar_cast_ &
  233. operator=( T const & x )
  234. {
  235. assign(*this,x);
  236. return *this;
  237. }
  238. template <class R>
  239. BOOST_QVM_INLINE_TRIVIAL
  240. operator R() const
  241. {
  242. R r;
  243. assign(r,*this);
  244. return r;
  245. }
  246. };
  247. template <bool> struct scalar_cast_vector_filter { };
  248. template <> struct scalar_cast_vector_filter<true> { typedef int type; };
  249. }
  250. template <class OriginalType,class Scalar>
  251. struct
  252. vec_traits< qvm_detail::vector_scalar_cast_<OriginalType,Scalar> >
  253. {
  254. typedef Scalar scalar_type;
  255. typedef qvm_detail::vector_scalar_cast_<OriginalType,Scalar> this_vector;
  256. static int const dim=vec_traits<OriginalType>::dim;
  257. template <int I>
  258. static
  259. BOOST_QVM_INLINE_CRITICAL
  260. scalar_type
  261. read_element( this_vector const & x )
  262. {
  263. BOOST_QVM_STATIC_ASSERT(I>=0);
  264. BOOST_QVM_STATIC_ASSERT(I<dim);
  265. return scalar_type(vec_traits<OriginalType>::template read_element<I>(reinterpret_cast<OriginalType const &>(x)));
  266. }
  267. static
  268. BOOST_QVM_INLINE_CRITICAL
  269. scalar_type
  270. read_element_idx( int i, this_vector const & x )
  271. {
  272. BOOST_QVM_ASSERT(i>=0);
  273. BOOST_QVM_ASSERT(i<dim);
  274. return scalar_type(vec_traits<OriginalType>::read_element_idx(i,reinterpret_cast<OriginalType const &>(x)));
  275. }
  276. };
  277. template <class OriginalType,class Scalar,int D>
  278. struct
  279. deduce_vec<qvm_detail::vector_scalar_cast_<OriginalType,Scalar>,D>
  280. {
  281. typedef vec<Scalar,D> type;
  282. };
  283. template <class Scalar,class T>
  284. BOOST_QVM_INLINE_TRIVIAL
  285. qvm_detail::vector_scalar_cast_<T,Scalar> const &
  286. scalar_cast( T const & x, typename qvm_detail::scalar_cast_vector_filter<is_vec<T>::value>::type=0 )
  287. {
  288. return reinterpret_cast<qvm_detail::vector_scalar_cast_<T,Scalar> const &>(x);
  289. }
  290. ////////////////////////////////////////////////
  291. namespace
  292. qvm_detail
  293. {
  294. template <int D>
  295. struct
  296. div_eq_vs_defined
  297. {
  298. static bool const value=false;
  299. };
  300. }
  301. template <class A,class B>
  302. BOOST_QVM_INLINE_OPERATIONS
  303. typename enable_if_c<
  304. is_vec<A>::value && is_scalar<B>::value &&
  305. !qvm_detail::div_eq_vs_defined<vec_traits<A>::dim>::value,
  306. A &>::type
  307. operator/=( A & a, B b )
  308. {
  309. for( int i=0; i!=vec_traits<A>::dim; ++i )
  310. vec_traits<A>::write_element_idx(i,a)/=b;
  311. return a;
  312. }
  313. ////////////////////////////////////////////////
  314. namespace
  315. qvm_detail
  316. {
  317. template <int D>
  318. struct
  319. div_vs_defined
  320. {
  321. static bool const value=false;
  322. };
  323. }
  324. template <class A,class B>
  325. BOOST_QVM_INLINE_OPERATIONS
  326. typename lazy_enable_if_c<
  327. is_vec<A>::value && is_scalar<B>::value &&
  328. !qvm_detail::div_vs_defined<vec_traits<A>::dim>::value,
  329. deduce_vec<A> >::type
  330. operator/( A const & a, B b )
  331. {
  332. typedef typename deduce_vec<A>::type R;
  333. R r;
  334. for( int i=0; i!=vec_traits<A>::dim; ++i )
  335. vec_traits<R>::write_element_idx(i,r)=vec_traits<A>::read_element_idx(i,a)/b;
  336. return r;
  337. }
  338. ////////////////////////////////////////////////
  339. namespace
  340. qvm_detail
  341. {
  342. template <int D>
  343. struct
  344. dot_vv_defined
  345. {
  346. static bool const value=false;
  347. };
  348. }
  349. template <class A,class B>
  350. BOOST_QVM_INLINE_OPERATIONS
  351. typename lazy_enable_if_c<
  352. is_vec<A>::value && is_vec<B>::value &&
  353. vec_traits<A>::dim==vec_traits<B>::dim &&
  354. !qvm_detail::dot_vv_defined<vec_traits<A>::dim>::value,
  355. deduce_scalar<typename vec_traits<A>::scalar_type,typename vec_traits<B>::scalar_type> >::type
  356. dot( A const & a, B const & b )
  357. {
  358. typedef typename deduce_scalar<typename vec_traits<A>::scalar_type,typename vec_traits<B>::scalar_type>::type T;
  359. T m(scalar_traits<T>::value(0));
  360. for( int i=0; i!=vec_traits<A>::dim; ++i )
  361. m+=vec_traits<A>::read_element_idx(i,a)*vec_traits<B>::read_element_idx(i,b);
  362. return m;
  363. }
  364. ////////////////////////////////////////////////
  365. namespace
  366. qvm_detail
  367. {
  368. template <int D>
  369. struct
  370. eq_vv_defined
  371. {
  372. static bool const value=false;
  373. };
  374. }
  375. template <class A,class B>
  376. BOOST_QVM_INLINE_OPERATIONS
  377. typename enable_if_c<
  378. is_vec<A>::value && is_vec<B>::value &&
  379. vec_traits<A>::dim==vec_traits<B>::dim &&
  380. !qvm_detail::eq_vv_defined<vec_traits<A>::dim>::value,
  381. bool>::type
  382. operator==( A const & a, B const & b )
  383. {
  384. for( int i=0; i!=vec_traits<A>::dim; ++i )
  385. if( vec_traits<A>::read_element_idx(i,a)!=vec_traits<B>::read_element_idx(i,b) )
  386. return false;
  387. return true;
  388. }
  389. ////////////////////////////////////////////////
  390. namespace
  391. qvm_detail
  392. {
  393. template <int D>
  394. struct
  395. mag_sqr_v_defined
  396. {
  397. static bool const value=false;
  398. };
  399. }
  400. template <class A>
  401. BOOST_QVM_INLINE_OPERATIONS
  402. typename enable_if_c<
  403. is_vec<A>::value &&
  404. !qvm_detail::mag_sqr_v_defined<vec_traits<A>::dim>::value,
  405. typename vec_traits<A>::scalar_type>::type
  406. mag_sqr( A const & a )
  407. {
  408. typedef typename vec_traits<A>::scalar_type T;
  409. T m(scalar_traits<T>::value(0));
  410. for( int i=0; i!=vec_traits<A>::dim; ++i )
  411. {
  412. T x=vec_traits<A>::read_element_idx(i,a);
  413. m+=x*x;
  414. }
  415. return m;
  416. }
  417. ////////////////////////////////////////////////
  418. namespace
  419. qvm_detail
  420. {
  421. template <int D>
  422. struct
  423. mag_v_defined
  424. {
  425. static bool const value=false;
  426. };
  427. }
  428. template <class A>
  429. BOOST_QVM_INLINE_OPERATIONS
  430. typename enable_if_c<
  431. is_vec<A>::value &&
  432. !qvm_detail::mag_v_defined<vec_traits<A>::dim>::value,
  433. typename vec_traits<A>::scalar_type>::type
  434. mag( A const & a )
  435. {
  436. typedef typename vec_traits<A>::scalar_type T;
  437. T m(scalar_traits<T>::value(0));
  438. for( int i=0; i!=vec_traits<A>::dim; ++i )
  439. {
  440. T x=vec_traits<A>::read_element_idx(i,a);
  441. m+=x*x;
  442. }
  443. return sqrt<T>(m);
  444. }
  445. ////////////////////////////////////////////////
  446. namespace
  447. qvm_detail
  448. {
  449. template <int D>
  450. struct
  451. minus_eq_vv_defined
  452. {
  453. static bool const value=false;
  454. };
  455. }
  456. template <class A,class B>
  457. BOOST_QVM_INLINE_OPERATIONS
  458. typename enable_if_c<
  459. is_vec<A>::value && is_vec<B>::value &&
  460. vec_traits<A>::dim==vec_traits<B>::dim &&
  461. !qvm_detail::minus_eq_vv_defined<vec_traits<A>::dim>::value,
  462. A &>::type
  463. operator-=( A & a, B const & b )
  464. {
  465. for( int i=0; i!=vec_traits<A>::dim; ++i )
  466. vec_traits<A>::write_element_idx(i,a)-=vec_traits<B>::read_element_idx(i,b);
  467. return a;
  468. }
  469. ////////////////////////////////////////////////
  470. namespace
  471. qvm_detail
  472. {
  473. template <int D>
  474. struct
  475. minus_v_defined
  476. {
  477. static bool const value=false;
  478. };
  479. }
  480. template <class A>
  481. BOOST_QVM_INLINE_OPERATIONS
  482. typename lazy_enable_if_c<
  483. is_vec<A>::value &&
  484. !qvm_detail::minus_v_defined<vec_traits<A>::dim>::value,
  485. deduce_vec<A> >::type
  486. operator-( A const & a )
  487. {
  488. typedef typename deduce_vec<A>::type R;
  489. R r;
  490. for( int i=0; i!=vec_traits<A>::dim; ++i )
  491. vec_traits<R>::write_element_idx(i,r)=-vec_traits<A>::read_element_idx(i,a);
  492. return r;
  493. }
  494. ////////////////////////////////////////////////
  495. namespace
  496. qvm_detail
  497. {
  498. template <int D>
  499. struct
  500. minus_vv_defined
  501. {
  502. static bool const value=false;
  503. };
  504. }
  505. template <class A,class B>
  506. BOOST_QVM_INLINE_OPERATIONS
  507. typename lazy_enable_if_c<
  508. is_vec<A>::value && is_vec<B>::value &&
  509. vec_traits<A>::dim==vec_traits<B>::dim &&
  510. !qvm_detail::minus_vv_defined<vec_traits<A>::dim>::value,
  511. deduce_vec2<A,B,vec_traits<A>::dim> >::type
  512. operator-( A const & a, B const & b )
  513. {
  514. typedef typename deduce_vec2<A,B,vec_traits<A>::dim>::type R;
  515. R r;
  516. for( int i=0; i!=vec_traits<A>::dim; ++i )
  517. vec_traits<R>::write_element_idx(i,r)=vec_traits<A>::read_element_idx(i,a)-vec_traits<B>::read_element_idx(i,b);
  518. return r;
  519. }
  520. ////////////////////////////////////////////////
  521. namespace
  522. qvm_detail
  523. {
  524. template <int D>
  525. struct
  526. mul_eq_vs_defined
  527. {
  528. static bool const value=false;
  529. };
  530. }
  531. template <class A,class B>
  532. BOOST_QVM_INLINE_OPERATIONS
  533. typename enable_if_c<
  534. is_vec<A>::value && is_scalar<B>::value &&
  535. !qvm_detail::mul_eq_vs_defined<vec_traits<A>::dim>::value,
  536. A &>::type
  537. operator*=( A & a, B b )
  538. {
  539. for( int i=0; i!=vec_traits<A>::dim; ++i )
  540. vec_traits<A>::write_element_idx(i,a)*=b;
  541. return a;
  542. }
  543. ////////////////////////////////////////////////
  544. namespace
  545. qvm_detail
  546. {
  547. template <int D>
  548. struct
  549. mul_vs_defined
  550. {
  551. static bool const value=false;
  552. };
  553. }
  554. template <class A,class B>
  555. BOOST_QVM_INLINE_OPERATIONS
  556. typename lazy_enable_if_c<
  557. is_vec<A>::value && is_scalar<B>::value &&
  558. !qvm_detail::mul_vs_defined<vec_traits<A>::dim>::value,
  559. deduce_vec<A> >::type
  560. operator*( A const & a, B b )
  561. {
  562. typedef typename deduce_vec<A>::type R;
  563. R r;
  564. for( int i=0; i!=vec_traits<A>::dim; ++i )
  565. vec_traits<R>::write_element_idx(i,r)=vec_traits<A>::read_element_idx(i,a)*b;
  566. return r;
  567. }
  568. ////////////////////////////////////////////////
  569. namespace
  570. qvm_detail
  571. {
  572. template <int D>
  573. struct
  574. mul_sv_defined
  575. {
  576. static bool const value=false;
  577. };
  578. }
  579. template <class A,class B>
  580. BOOST_QVM_INLINE_OPERATIONS
  581. typename lazy_enable_if_c<
  582. is_scalar<A>::value && is_vec<B>::value &&
  583. !qvm_detail::mul_sv_defined<vec_traits<B>::dim>::value,
  584. deduce_vec<B> >::type
  585. operator*( A a, B const & b )
  586. {
  587. typedef typename deduce_vec<B>::type R;
  588. R r;
  589. for( int i=0; i!=vec_traits<B>::dim; ++i )
  590. vec_traits<R>::write_element_idx(i,r)=a*vec_traits<B>::read_element_idx(i,b);
  591. return r;
  592. }
  593. ////////////////////////////////////////////////
  594. namespace
  595. qvm_detail
  596. {
  597. template <int D>
  598. struct
  599. neq_vv_defined
  600. {
  601. static bool const value=false;
  602. };
  603. }
  604. template <class A,class B>
  605. BOOST_QVM_INLINE_OPERATIONS
  606. typename enable_if_c<
  607. is_vec<A>::value && is_vec<B>::value &&
  608. vec_traits<A>::dim==vec_traits<B>::dim &&
  609. !qvm_detail::neq_vv_defined<vec_traits<A>::dim>::value,
  610. bool>::type
  611. operator!=( A const & a, B const & b )
  612. {
  613. for( int i=0; i!=vec_traits<A>::dim; ++i )
  614. if( vec_traits<A>::read_element_idx(i,a)!=vec_traits<B>::read_element_idx(i,b) )
  615. return true;
  616. return false;
  617. }
  618. ////////////////////////////////////////////////
  619. namespace
  620. qvm_detail
  621. {
  622. template <int D>
  623. struct
  624. normalize_v_defined
  625. {
  626. static bool const value=false;
  627. };
  628. }
  629. template <class A>
  630. BOOST_QVM_INLINE_OPERATIONS
  631. typename lazy_enable_if_c<
  632. is_vec<A>::value &&
  633. !qvm_detail::normalize_v_defined<vec_traits<A>::dim>::value,
  634. deduce_vec<A> >::type
  635. normalized( A const & a )
  636. {
  637. typedef typename vec_traits<A>::scalar_type T;
  638. T m(scalar_traits<T>::value(0));
  639. for( int i=0; i!=vec_traits<A>::dim; ++i )
  640. {
  641. T x=vec_traits<A>::read_element_idx(i,a);
  642. m+=x*x;
  643. }
  644. if( m==scalar_traits<T>::value(0) )
  645. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  646. T rm=scalar_traits<T>::value(1)/sqrt<T>(m);
  647. typedef typename deduce_vec<A>::type R;
  648. R r;
  649. for( int i=0; i!=vec_traits<A>::dim; ++i )
  650. vec_traits<R>::write_element_idx(i,r)=vec_traits<A>::read_element_idx(i,a)*rm;
  651. return r;
  652. }
  653. template <class A>
  654. BOOST_QVM_INLINE_OPERATIONS
  655. typename enable_if_c<
  656. is_vec<A>::value &&
  657. !qvm_detail::normalize_v_defined<vec_traits<A>::dim>::value,
  658. void>::type
  659. normalize( A & a )
  660. {
  661. typedef typename vec_traits<A>::scalar_type T;
  662. T m(scalar_traits<T>::value(0));
  663. for( int i=0; i!=vec_traits<A>::dim; ++i )
  664. {
  665. T x=vec_traits<A>::read_element_idx(i,a);
  666. m+=x*x;
  667. }
  668. if( m==scalar_traits<T>::value(0) )
  669. BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());
  670. T rm=scalar_traits<T>::value(1)/sqrt<T>(m);
  671. for( int i=0; i!=vec_traits<A>::dim; ++i )
  672. vec_traits<A>::write_element_idx(i,a)*=rm;
  673. }
  674. ////////////////////////////////////////////////
  675. namespace
  676. qvm_detail
  677. {
  678. template <int D>
  679. struct
  680. plus_eq_vv_defined
  681. {
  682. static bool const value=false;
  683. };
  684. }
  685. template <class A,class B>
  686. BOOST_QVM_INLINE_OPERATIONS
  687. typename enable_if_c<
  688. is_vec<A>::value && is_vec<B>::value &&
  689. vec_traits<A>::dim==vec_traits<B>::dim &&
  690. !qvm_detail::plus_eq_vv_defined<vec_traits<A>::dim>::value,
  691. A &>::type
  692. operator+=( A & a, B const & b )
  693. {
  694. for( int i=0; i!=vec_traits<A>::dim; ++i )
  695. vec_traits<A>::write_element_idx(i,a)+=vec_traits<B>::read_element_idx(i,b);
  696. return a;
  697. }
  698. ////////////////////////////////////////////////
  699. namespace
  700. qvm_detail
  701. {
  702. template <int D>
  703. struct
  704. plus_vv_defined
  705. {
  706. static bool const value=false;
  707. };
  708. }
  709. template <class A,class B>
  710. BOOST_QVM_INLINE_OPERATIONS
  711. typename lazy_enable_if_c<
  712. is_vec<A>::value && is_vec<B>::value &&
  713. vec_traits<A>::dim==vec_traits<B>::dim &&
  714. !qvm_detail::plus_vv_defined<vec_traits<A>::dim>::value,
  715. deduce_vec2<A,B,vec_traits<A>::dim> >::type
  716. operator+( A const & a, B const & b )
  717. {
  718. typedef typename deduce_vec2<A,B,vec_traits<A>::dim>::type R;
  719. R r;
  720. for( int i=0; i!=vec_traits<A>::dim; ++i )
  721. vec_traits<R>::write_element_idx(i,r)=vec_traits<A>::read_element_idx(i,a)+vec_traits<B>::read_element_idx(i,b);
  722. return r;
  723. }
  724. ////////////////////////////////////////////////
  725. namespace
  726. qvm_detail
  727. {
  728. template <class T>
  729. class
  730. vref_
  731. {
  732. vref_( vref_ const & );
  733. vref_ & operator=( vref_ const & );
  734. ~vref_();
  735. public:
  736. template <class R>
  737. BOOST_QVM_INLINE_TRIVIAL
  738. vref_ &
  739. operator=( R const & x )
  740. {
  741. assign(*this,x);
  742. return *this;
  743. }
  744. template <class R>
  745. BOOST_QVM_INLINE_TRIVIAL
  746. operator R() const
  747. {
  748. R r;
  749. assign(r,*this);
  750. return r;
  751. }
  752. };
  753. }
  754. template <class V>
  755. struct
  756. vec_traits< qvm_detail::vref_<V> >
  757. {
  758. typedef typename vec_traits<V>::scalar_type scalar_type;
  759. typedef qvm_detail::vref_<V> this_vector;
  760. static int const dim=vec_traits<V>::dim;
  761. template <int I>
  762. static
  763. BOOST_QVM_INLINE_CRITICAL
  764. scalar_type
  765. read_element( this_vector const & x )
  766. {
  767. BOOST_QVM_STATIC_ASSERT(I>=0);
  768. BOOST_QVM_STATIC_ASSERT(I<dim);
  769. return vec_traits<V>::template read_element<I>(reinterpret_cast<V const &>(x));
  770. }
  771. template <int I>
  772. static
  773. BOOST_QVM_INLINE_CRITICAL
  774. scalar_type &
  775. write_element( this_vector & x )
  776. {
  777. BOOST_QVM_STATIC_ASSERT(I>=0);
  778. BOOST_QVM_STATIC_ASSERT(I<dim);
  779. return vec_traits<V>::template write_element<I>(reinterpret_cast<V &>(x));
  780. }
  781. static
  782. BOOST_QVM_INLINE_CRITICAL
  783. scalar_type
  784. read_element_idx( int i, this_vector const & x )
  785. {
  786. BOOST_QVM_ASSERT(i>=0);
  787. BOOST_QVM_ASSERT(i<dim);
  788. return vec_traits<V>::read_element_idx(i,reinterpret_cast<V const &>(x));
  789. }
  790. static
  791. BOOST_QVM_INLINE_CRITICAL
  792. scalar_type &
  793. write_element_idx( int i, this_vector & x )
  794. {
  795. BOOST_QVM_ASSERT(i>=0);
  796. BOOST_QVM_ASSERT(i<dim);
  797. return vec_traits<V>::write_element_idx(i,reinterpret_cast<V &>(x));
  798. }
  799. };
  800. template <class V,int D>
  801. struct
  802. deduce_vec<qvm_detail::vref_<V>,D>
  803. {
  804. typedef vec<typename vec_traits<V>::scalar_type,D> type;
  805. };
  806. template <class V>
  807. BOOST_QVM_INLINE_TRIVIAL
  808. typename enable_if_c<
  809. is_vec<V>::value,
  810. qvm_detail::vref_<V> const &>::type
  811. vref( V const & a )
  812. {
  813. return reinterpret_cast<qvm_detail::vref_<V> const &>(a);
  814. }
  815. template <class V>
  816. BOOST_QVM_INLINE_TRIVIAL
  817. typename enable_if_c<
  818. is_vec<V>::value,
  819. qvm_detail::vref_<V> &>::type
  820. vref( V & a )
  821. {
  822. return reinterpret_cast<qvm_detail::vref_<V> &>(a);
  823. }
  824. ////////////////////////////////////////////////
  825. namespace
  826. sfinae
  827. {
  828. using ::boost::qvm::to_string;
  829. using ::boost::qvm::assign;
  830. using ::boost::qvm::convert_to;
  831. using ::boost::qvm::cross;
  832. using ::boost::qvm::cmp;
  833. using ::boost::qvm::set_zero;
  834. using ::boost::qvm::scalar_cast;
  835. using ::boost::qvm::operator/=;
  836. using ::boost::qvm::operator/;
  837. using ::boost::qvm::dot;
  838. using ::boost::qvm::operator==;
  839. using ::boost::qvm::mag_sqr;
  840. using ::boost::qvm::mag;
  841. using ::boost::qvm::operator-=;
  842. using ::boost::qvm::operator-;
  843. using ::boost::qvm::operator*=;
  844. using ::boost::qvm::operator*;
  845. using ::boost::qvm::operator!=;
  846. using ::boost::qvm::normalized;
  847. using ::boost::qvm::normalize;
  848. using ::boost::qvm::operator+=;
  849. using ::boost::qvm::operator+;
  850. using ::boost::qvm::vref;
  851. }
  852. ////////////////////////////////////////////////
  853. }
  854. }
  855. #endif