📄 quaternion.hpp
字号:
template<typename X> quaternion<T> & operator += (quaternion<X> const & rhs) { T at = a + static_cast<T>(rhs.R_component_1()); // exception guard T bt = b + static_cast<T>(rhs.R_component_2()); // exception guard T ct = c + static_cast<T>(rhs.R_component_3()); // exception guard T dt = d + static_cast<T>(rhs.R_component_4()); // exception guard a = at; b = bt; c = ct; d = dt; return(*this); } quaternion<T> & operator -= (T const & rhs) { T at = a - rhs; // exception guard a = at; return(*this); } quaternion<T> & operator -= (::std::complex<T> const & rhs) { T at = a - rhs.real(); // exception guard T bt = b - rhs.imag(); // exception guard a = at; b = bt; return(*this); } template<typename X> quaternion<T> & operator -= (quaternion<X> const & rhs) { T at = a - static_cast<T>(rhs.R_component_1()); // exception guard T bt = b - static_cast<T>(rhs.R_component_2()); // exception guard T ct = c - static_cast<T>(rhs.R_component_3()); // exception guard T dt = d - static_cast<T>(rhs.R_component_4()); // exception guard a = at; b = bt; c = ct; d = dt; return(*this); } quaternion<T> & operator *= (T const & rhs) { T at = a * rhs; // exception guard T bt = b * rhs; // exception guard T ct = c * rhs; // exception guard T dt = d * rhs; // exception guard a = at; b = bt; c = ct; d = dt; return(*this); } quaternion<T> & operator *= (::std::complex<T> const & rhs) { T ar = rhs.real(); T br = rhs.imag(); T at = +a*ar-b*br; T bt = +a*br+b*ar; T ct = +c*ar+d*br; T dt = -c*br+d*ar; a = at; b = bt; c = ct; d = dt; return(*this); } template<typename X> quaternion<T> & operator *= (quaternion<X> const & rhs) { T ar = static_cast<T>(rhs.R_component_1()); T br = static_cast<T>(rhs.R_component_2()); T cr = static_cast<T>(rhs.R_component_3()); T dr = static_cast<T>(rhs.R_component_4()); T at = +a*ar-b*br-c*cr-d*dr; T bt = +a*br+b*ar+c*dr-d*cr; //(a*br+ar*b)+(c*dr-cr*d); T ct = +a*cr-b*dr+c*ar+d*br; //(a*cr+ar*c)+(d*br-dr*b); T dt = +a*dr+b*cr-c*br+d*ar; //(a*dr+ar*d)+(b*cr-br*c); a = at; b = bt; c = ct; d = dt; return(*this); } quaternion<T> & operator /= (T const & rhs) { T at = a / rhs; // exception guard T bt = b / rhs; // exception guard T ct = c / rhs; // exception guard T dt = d / rhs; // exception guard a = at; b = bt; c = ct; d = dt; return(*this); } quaternion<T> & operator /= (::std::complex<T> const & rhs) { T ar = rhs.real(); T br = rhs.imag(); T denominator = ar*ar+br*br; T at = (+a*ar+b*br)/denominator; //(a*ar+b*br)/denominator; T bt = (-a*br+b*ar)/denominator; //(ar*b-a*br)/denominator; T ct = (+c*ar-d*br)/denominator; //(ar*c-d*br)/denominator; T dt = (+c*br+d*ar)/denominator; //(ar*d+br*c)/denominator; a = at; b = bt; c = ct; d = dt; return(*this); } template<typename X> quaternion<T> & operator /= (quaternion<X> const & rhs) { T ar = static_cast<T>(rhs.R_component_1()); T br = static_cast<T>(rhs.R_component_2()); T cr = static_cast<T>(rhs.R_component_3()); T dr = static_cast<T>(rhs.R_component_4()); T denominator = ar*ar+br*br+cr*cr+dr*dr; T at = (+a*ar+b*br+c*cr+d*dr)/denominator; //(a*ar+b*br+c*cr+d*dr)/denominator; T bt = (-a*br+b*ar-c*dr+d*cr)/denominator; //((ar*b-a*br)+(cr*d-c*dr))/denominator; T ct = (-a*cr+b*dr+c*ar-d*br)/denominator; //((ar*c-a*cr)+(dr*b-d*br))/denominator; T dt = (-a*dr-b*cr+c*br+d*ar)/denominator; //((ar*d-a*dr)+(br*c-b*cr))/denominator; a = at; b = bt; c = ct; d = dt; return(*this); } protected: BOOST_QUATERNION_MEMBER_DATA_GENERATOR(T) private: }; // declaration of quaternion specialization template<> class quaternion<float>; template<> class quaternion<double>; template<> class quaternion<long double>; // helper templates for converting copy constructors (declaration) namespace detail { template< typename T, typename U > quaternion<T> quaternion_type_converter(quaternion<U> const & rhs); } // implementation of quaternion specialization #define BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(type) \ explicit quaternion( type const & requested_a = static_cast<type>(0), \ type const & requested_b = static_cast<type>(0), \ type const & requested_c = static_cast<type>(0), \ type const & requested_d = static_cast<type>(0)) \ : a(requested_a), \ b(requested_b), \ c(requested_c), \ d(requested_d) \ { \ } \ \ explicit quaternion( ::std::complex<type> const & z0, \ ::std::complex<type> const & z1 = ::std::complex<type>()) \ : a(z0.real()), \ b(z0.imag()), \ c(z1.real()), \ d(z1.imag()) \ { \ } #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1(type) \ quaternion<type> & operator += (type const & rhs) \ { \ a += rhs; \ \ return(*this); \ } #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2(type) \ quaternion<type> & operator += (::std::complex<type> const & rhs) \ { \ a += rhs.real(); \ b += rhs.imag(); \ \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -