#ifndef BOOST_QVM_VEC_OPERATIONS_HPP_INCLUDED #define BOOST_QVM_VEC_OPERATIONS_HPP_INCLUDED /// Copyright (c) 2008-2021 Emil Dotchevski and Reverge Studios, Inc. /// Copyright (c) 2019 agate-pris /// Distributed under the Boost Software License, Version 1.0. (See accompanying /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include namespace boost { namespace qvm { namespace qvm_detail { BOOST_QVM_INLINE_CRITICAL void const * get_valid_ptr_vec_operations() { static int const obj=0; return &obj; } } //////////////////////////////////////////////// namespace qvm_to_string_detail { template std::string to_string( T const & x ); } namespace qvm_detail { template struct to_string_v_defined { static bool const value=false; }; template struct to_string_vector_elements { template static std::string f( A const & a ) { using namespace qvm_to_string_detail; return to_string(vec_traits::template read_element(a))+','+to_string_vector_elements::f(a); } }; template struct to_string_vector_elements { template static std::string f( A const & a ) { using namespace qvm_to_string_detail; return to_string(vec_traits::template read_element(a)); } }; } template inline typename enable_if_c< is_vec::value && !qvm_detail::to_string_v_defined::dim>::value, std::string>::type to_string( A const & a ) { return '('+qvm_detail::to_string_vector_elements<0,vec_traits::dim-1>::f(a)+')'; } //////////////////////////////////////////////// namespace qvm_detail { template struct convert_to_v_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_TRIVIAL typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::convert_to_v_defined::dim>::value, R>::type convert_to( A const & a ) { R r; assign(r,a); return r; } //////////////////////////////////////////////// template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==3 && vec_traits::dim==3, deduce_vec2 >::type cross( A const & a, B const & b ) { typedef typename deduce_vec2::type R; R r; vec_traits::template write_element<0>(r)= vec_traits::template read_element<1>(a)*vec_traits::template read_element<2>(b)- vec_traits::template read_element<2>(a)*vec_traits::template read_element<1>(b); vec_traits::template write_element<1>(r)= vec_traits::template read_element<2>(a)*vec_traits::template read_element<0>(b)- vec_traits::template read_element<0>(a)*vec_traits::template read_element<2>(b); vec_traits::template write_element<2>(r)= vec_traits::template read_element<0>(a)*vec_traits::template read_element<1>(b)- vec_traits::template read_element<1>(a)*vec_traits::template read_element<0>(b); return r; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==2 && vec_traits::dim==2, deduce_scalar::scalar_type,typename vec_traits::scalar_type> >::type cross( A const & a, B const & b ) { typedef typename deduce_scalar::scalar_type,typename vec_traits::scalar_type>::type R; R const r = vec_traits::template read_element<0>(a)*vec_traits::template read_element<1>(b)- vec_traits::template read_element<1>(a)*vec_traits::template read_element<0>(b); return r; } //////////////////////////////////////////////// template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim, bool>::type cmp( A const & a, B const & b, Cmp f ) { for( int i=0; i!=vec_traits::dim; ++i ) if( !f( vec_traits::read_element_idx(i,a), vec_traits::read_element_idx(i,b)) ) return false; return true; } //////////////////////////////////////////////// namespace qvm_detail { template class zero_vec_ { zero_vec_( zero_vec_ const & ); zero_vec_ & operator=( zero_vec_ const & ); ~zero_vec_(); public: template BOOST_QVM_INLINE_TRIVIAL operator R() const { R r; assign(r,*this); return r; } }; } template struct vec_traits; template struct vec_traits< qvm_detail::zero_vec_ > { typedef qvm_detail::zero_vec_ this_vector; typedef T scalar_type; static int const dim=Dim; template static BOOST_QVM_INLINE_CRITICAL scalar_type read_element( this_vector const & ) { BOOST_QVM_STATIC_ASSERT(I>=0); BOOST_QVM_STATIC_ASSERT(I::value(0); } static BOOST_QVM_INLINE_CRITICAL scalar_type read_element_idx( int i, this_vector const & ) { BOOST_QVM_ASSERT(i>=0); BOOST_QVM_ASSERT(i::value(0); } }; template struct deduce_vec,D> { typedef vec type; }; template BOOST_QVM_INLINE_TRIVIAL qvm_detail::zero_vec_ const & zero_vec() { return *(qvm_detail::zero_vec_ const *)qvm_detail::get_valid_ptr_vec_operations(); } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value, void>::type set_zero( A & a ) { assign(a,zero_vec::scalar_type,vec_traits::dim>()); } //////////////////////////////////////////////// namespace qvm_detail { template class vector_scalar_cast_ { vector_scalar_cast_( vector_scalar_cast_ const & ); vector_scalar_cast_ & operator=( vector_scalar_cast_ const & ); ~vector_scalar_cast_(); public: template BOOST_QVM_INLINE_TRIVIAL vector_scalar_cast_ & operator=( T const & x ) { assign(*this,x); return *this; } template BOOST_QVM_INLINE_TRIVIAL operator R() const { R r; assign(r,*this); return r; } }; template struct scalar_cast_vector_filter { }; template <> struct scalar_cast_vector_filter { typedef int type; }; } template struct vec_traits< qvm_detail::vector_scalar_cast_ > { typedef Scalar scalar_type; typedef qvm_detail::vector_scalar_cast_ this_vector; static int const dim=vec_traits::dim; template static BOOST_QVM_INLINE_CRITICAL scalar_type read_element( this_vector const & x ) { BOOST_QVM_STATIC_ASSERT(I>=0); BOOST_QVM_STATIC_ASSERT(I::template read_element(reinterpret_cast(x))); } static BOOST_QVM_INLINE_CRITICAL scalar_type read_element_idx( int i, this_vector const & x ) { BOOST_QVM_ASSERT(i>=0); BOOST_QVM_ASSERT(i::read_element_idx(i,reinterpret_cast(x))); } }; template struct deduce_vec,D> { typedef vec type; }; template BOOST_QVM_INLINE_TRIVIAL qvm_detail::vector_scalar_cast_ const & scalar_cast( T const & x, typename qvm_detail::scalar_cast_vector_filter::value>::type=0 ) { return reinterpret_cast const &>(x); } //////////////////////////////////////////////// namespace qvm_detail { template struct div_eq_vs_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_scalar::value && !qvm_detail::div_eq_vs_defined::dim>::value, A &>::type operator/=( A & a, B b ) { for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,a)/=b; return a; } //////////////////////////////////////////////// namespace qvm_detail { template struct div_vs_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_scalar::value && !qvm_detail::div_vs_defined::dim>::value, deduce_vec2::dim> >::type operator/( A const & a, B b ) { typedef typename deduce_vec2::dim>::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=vec_traits::read_element_idx(i,a)/b; return r; } //////////////////////////////////////////////// namespace qvm_detail { template struct dot_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::dot_vv_defined::dim>::value, deduce_scalar::scalar_type,typename vec_traits::scalar_type> >::type dot( A const & a, B const & b ) { typedef typename deduce_scalar::scalar_type,typename vec_traits::scalar_type>::type T; T m(scalar_traits::value(0)); for( int i=0; i!=vec_traits::dim; ++i ) m+=vec_traits::read_element_idx(i,a)*vec_traits::read_element_idx(i,b); return m; } //////////////////////////////////////////////// namespace qvm_detail { template struct eq_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::eq_vv_defined::dim>::value, bool>::type operator==( A const & a, B const & b ) { for( int i=0; i!=vec_traits::dim; ++i ) if( vec_traits::read_element_idx(i,a)!=vec_traits::read_element_idx(i,b) ) return false; return true; } //////////////////////////////////////////////// namespace qvm_detail { template struct mag_sqr_v_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && !qvm_detail::mag_sqr_v_defined::dim>::value, typename vec_traits::scalar_type>::type mag_sqr( A const & a ) { typedef typename vec_traits::scalar_type T; T m(scalar_traits::value(0)); for( int i=0; i!=vec_traits::dim; ++i ) { T x=vec_traits::read_element_idx(i,a); m+=x*x; } return m; } //////////////////////////////////////////////// namespace qvm_detail { template struct mag_v_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && !qvm_detail::mag_v_defined::dim>::value, typename vec_traits::scalar_type>::type mag( A const & a ) { typedef typename vec_traits::scalar_type T; T m(scalar_traits::value(0)); for( int i=0; i!=vec_traits::dim; ++i ) { T x=vec_traits::read_element_idx(i,a); m+=x*x; } return sqrt(m); } //////////////////////////////////////////////// namespace qvm_detail { template struct minus_eq_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::minus_eq_vv_defined::dim>::value, A &>::type operator-=( A & a, B const & b ) { for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,a)-=vec_traits::read_element_idx(i,b); return a; } //////////////////////////////////////////////// namespace qvm_detail { template struct minus_v_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && !qvm_detail::minus_v_defined::dim>::value, deduce_vec >::type operator-( A const & a ) { typedef typename deduce_vec::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=-vec_traits::read_element_idx(i,a); return r; } //////////////////////////////////////////////// namespace qvm_detail { template struct minus_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::minus_vv_defined::dim>::value, deduce_vec2::dim> >::type operator-( A const & a, B const & b ) { typedef typename deduce_vec2::dim>::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=vec_traits::read_element_idx(i,a)-vec_traits::read_element_idx(i,b); return r; } //////////////////////////////////////////////// namespace qvm_detail { template struct mul_eq_vs_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_scalar::value && !qvm_detail::mul_eq_vs_defined::dim>::value, A &>::type operator*=( A & a, B b ) { for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,a)*=b; return a; } //////////////////////////////////////////////// namespace qvm_detail { template struct mul_vs_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_scalar::value && !qvm_detail::mul_vs_defined::dim>::value, deduce_vec2::dim> >::type operator*( A const & a, B b ) { typedef typename deduce_vec2::dim>::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=vec_traits::read_element_idx(i,a)*b; return r; } //////////////////////////////////////////////// namespace qvm_detail { template struct mul_sv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_scalar::value && is_vec::value && !qvm_detail::mul_sv_defined::dim>::value, deduce_vec2::dim> >::type operator*( A a, B const & b ) { typedef typename deduce_vec2::dim>::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=a*vec_traits::read_element_idx(i,b); return r; } //////////////////////////////////////////////// namespace qvm_detail { template struct neq_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::neq_vv_defined::dim>::value, bool>::type operator!=( A const & a, B const & b ) { for( int i=0; i!=vec_traits::dim; ++i ) if( vec_traits::read_element_idx(i,a)!=vec_traits::read_element_idx(i,b) ) return true; return false; } //////////////////////////////////////////////// namespace qvm_detail { template struct normalize_v_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && !qvm_detail::normalize_v_defined::dim>::value, deduce_vec >::type normalized( A const & a ) { typedef typename vec_traits::scalar_type T; T m(scalar_traits::value(0)); for( int i=0; i!=vec_traits::dim; ++i ) { T x=vec_traits::read_element_idx(i,a); m+=x*x; } if( m==scalar_traits::value(0) ) BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error()); T rm=scalar_traits::value(1)/sqrt(m); typedef typename deduce_vec::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=vec_traits::read_element_idx(i,a)*rm; return r; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && !qvm_detail::normalize_v_defined::dim>::value, void>::type normalize( A & a ) { typedef typename vec_traits::scalar_type T; T m(scalar_traits::value(0)); for( int i=0; i!=vec_traits::dim; ++i ) { T x=vec_traits::read_element_idx(i,a); m+=x*x; } if( m==scalar_traits::value(0) ) BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error()); T rm=scalar_traits::value(1)/sqrt(m); for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,a)*=rm; } //////////////////////////////////////////////// namespace qvm_detail { template struct plus_eq_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::plus_eq_vv_defined::dim>::value, A &>::type operator+=( A & a, B const & b ) { for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,a)+=vec_traits::read_element_idx(i,b); return a; } //////////////////////////////////////////////// namespace qvm_detail { template struct plus_vv_defined { static bool const value=false; }; } template BOOST_QVM_INLINE_OPERATIONS typename lazy_enable_if_c< is_vec::value && is_vec::value && vec_traits::dim==vec_traits::dim && !qvm_detail::plus_vv_defined::dim>::value, deduce_vec2::dim> >::type operator+( A const & a, B const & b ) { typedef typename deduce_vec2::dim>::type R; R r; for( int i=0; i!=vec_traits::dim; ++i ) vec_traits::write_element_idx(i,r)=vec_traits::read_element_idx(i,a)+vec_traits::read_element_idx(i,b); return r; } //////////////////////////////////////////////// namespace qvm_detail { template class vref_ { vref_( vref_ const & ); vref_ & operator=( vref_ const & ); ~vref_(); public: template BOOST_QVM_INLINE_TRIVIAL vref_ & operator=( R const & x ) { assign(*this,x); return *this; } template BOOST_QVM_INLINE_TRIVIAL operator R() const { R r; assign(r,*this); return r; } }; } template struct vec_traits< qvm_detail::vref_ > { typedef typename vec_traits::scalar_type scalar_type; typedef qvm_detail::vref_ this_vector; static int const dim=vec_traits::dim; template static BOOST_QVM_INLINE_CRITICAL scalar_type read_element( this_vector const & x ) { BOOST_QVM_STATIC_ASSERT(I>=0); BOOST_QVM_STATIC_ASSERT(I::template read_element(reinterpret_cast(x)); } template static BOOST_QVM_INLINE_CRITICAL scalar_type & write_element( this_vector & x ) { BOOST_QVM_STATIC_ASSERT(I>=0); BOOST_QVM_STATIC_ASSERT(I::template write_element(reinterpret_cast(x)); } static BOOST_QVM_INLINE_CRITICAL scalar_type read_element_idx( int i, this_vector const & x ) { BOOST_QVM_ASSERT(i>=0); BOOST_QVM_ASSERT(i::read_element_idx(i,reinterpret_cast(x)); } static BOOST_QVM_INLINE_CRITICAL scalar_type & write_element_idx( int i, this_vector & x ) { BOOST_QVM_ASSERT(i>=0); BOOST_QVM_ASSERT(i::write_element_idx(i,reinterpret_cast(x)); } }; template struct deduce_vec,D> { typedef vec::scalar_type,D> type; }; template BOOST_QVM_INLINE_TRIVIAL typename enable_if_c< is_vec::value, qvm_detail::vref_ const &>::type vref( V const & a ) { return reinterpret_cast const &>(a); } template BOOST_QVM_INLINE_TRIVIAL typename enable_if_c< is_vec::value, qvm_detail::vref_ &>::type vref( V & a ) { return reinterpret_cast &>(a); } //////////////////////////////////////////////// namespace sfinae { using ::boost::qvm::to_string; using ::boost::qvm::assign; using ::boost::qvm::convert_to; using ::boost::qvm::cross; using ::boost::qvm::cmp; using ::boost::qvm::set_zero; using ::boost::qvm::scalar_cast; using ::boost::qvm::operator/=; using ::boost::qvm::operator/; using ::boost::qvm::dot; using ::boost::qvm::operator==; using ::boost::qvm::mag_sqr; using ::boost::qvm::mag; using ::boost::qvm::operator-=; using ::boost::qvm::operator-; using ::boost::qvm::operator*=; using ::boost::qvm::operator*; using ::boost::qvm::operator!=; using ::boost::qvm::normalized; using ::boost::qvm::normalize; using ::boost::qvm::operator+=; using ::boost::qvm::operator+; using ::boost::qvm::vref; } } } #endif