quat_access.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef BOOST_QVM_QUAT_ACCESS_HPP_INCLUDED
  2. #define BOOST_QVM_QUAT_ACCESS_HPP_INCLUDED
  3. /// Copyright (c) 2008-2021 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/inline.hpp>
  7. #include <boost/qvm/quat_traits.hpp>
  8. #include <boost/qvm/deduce_vec.hpp>
  9. #include <boost/qvm/static_assert.hpp>
  10. #include <boost/qvm/enable_if.hpp>
  11. namespace boost { namespace qvm {
  12. namespace
  13. qvm_detail
  14. {
  15. template <class Q>
  16. struct
  17. quat_v_
  18. {
  19. template <class R>
  20. operator R() const
  21. {
  22. R r;
  23. assign(r,*this);
  24. return r;
  25. }
  26. private:
  27. quat_v_( quat_v_ const & );
  28. quat_v_ const & operator=( quat_v_ const & );
  29. ~quat_v_();
  30. };
  31. }
  32. template <class V>
  33. struct vec_traits;
  34. template <class Q>
  35. struct
  36. vec_traits< qvm_detail::quat_v_<Q> >
  37. {
  38. typedef qvm_detail::quat_v_<Q> this_vector;
  39. typedef typename quat_traits<Q>::scalar_type scalar_type;
  40. static int const dim=3;
  41. template <int I>
  42. BOOST_QVM_INLINE_CRITICAL
  43. static
  44. scalar_type
  45. read_element( this_vector const & q )
  46. {
  47. BOOST_QVM_STATIC_ASSERT(I>=0);
  48. BOOST_QVM_STATIC_ASSERT(I<dim);
  49. return quat_traits<Q>::template read_element<I+1>( reinterpret_cast<Q const &>(q) );
  50. }
  51. template <int I>
  52. BOOST_QVM_INLINE_CRITICAL
  53. static
  54. scalar_type &
  55. write_element( this_vector & q )
  56. {
  57. BOOST_QVM_STATIC_ASSERT(I>=0);
  58. BOOST_QVM_STATIC_ASSERT(I<dim);
  59. return quat_traits<Q>::template write_element<I+1>( reinterpret_cast<Q &>(q) );
  60. }
  61. };
  62. template <class Q,int D>
  63. struct
  64. deduce_vec<qvm_detail::quat_v_<Q>,D>
  65. {
  66. typedef vec<typename quat_traits<Q>::scalar_type,D> type;
  67. };
  68. template <class Q,int D>
  69. struct
  70. deduce_vec2<qvm_detail::quat_v_<Q>,qvm_detail::quat_v_<Q>,D>
  71. {
  72. typedef vec<typename quat_traits<Q>::scalar_type,D> type;
  73. };
  74. template <class Q>
  75. BOOST_QVM_INLINE_TRIVIAL
  76. typename enable_if_c<
  77. is_quat<Q>::value,
  78. qvm_detail::quat_v_<Q> const &>::type
  79. V( Q const & a )
  80. {
  81. return reinterpret_cast<qvm_detail::quat_v_<Q> const &>(a);
  82. }
  83. template <class Q>
  84. BOOST_QVM_INLINE_TRIVIAL
  85. typename enable_if_c<
  86. is_quat<Q>::value,
  87. qvm_detail::quat_v_<Q> &>::type
  88. V( Q & a )
  89. {
  90. return reinterpret_cast<qvm_detail::quat_v_<Q> &>(a);
  91. }
  92. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type>::type S( Q const & a ) { return quat_traits<Q>::template read_element<0>(a); }
  93. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type>::type X( Q const & a ) { return quat_traits<Q>::template read_element<1>(a); }
  94. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type>::type Y( Q const & a ) { return quat_traits<Q>::template read_element<2>(a); }
  95. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type>::type Z( Q const & a ) { return quat_traits<Q>::template read_element<3>(a); }
  96. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type &>::type S( Q & a ) { return quat_traits<Q>::template write_element<0>(a); }
  97. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type &>::type X( Q & a ) { return quat_traits<Q>::template write_element<1>(a); }
  98. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type &>::type Y( Q & a ) { return quat_traits<Q>::template write_element<2>(a); }
  99. template <class Q> BOOST_QVM_INLINE_TRIVIAL typename enable_if_c<is_quat<Q>::value,typename quat_traits<Q>::scalar_type &>::type Z( Q & a ) { return quat_traits<Q>::template write_element<3>(a); }
  100. } }
  101. #endif