vec_operations.hpp 24 KB

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