Mathter
A configurable 3D math library for game developers.
QuaternionArithmetic.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "QuaternionImpl.hpp"
4 
5 namespace mathter {
6 
7 
8  namespace impl {
9  template <class T, bool Packed>
10  typename std::enable_if<!Quaternion<T, Packed>::SimdAccelerated, Quaternion<T, Packed>>::type Product(const Quaternion<T, Packed>& lhs, const Quaternion<T, Packed>& rhs) {
12  ret.w = lhs.s * rhs.s - lhs.x * rhs.x - lhs.y * rhs.y - lhs.z * rhs.z;
13  ret.x = lhs.s * rhs.x + lhs.x * rhs.s + lhs.y * rhs.z - lhs.z * rhs.y;
14  ret.y = lhs.s * rhs.y - lhs.x * rhs.z + lhs.y * rhs.s + lhs.z * rhs.x;
15  ret.z = lhs.s * rhs.z + lhs.x * rhs.y - lhs.y * rhs.x + lhs.z * rhs.s;
16  return ret;
17  }
18 
19 
20  template <class T, bool Packed>
21  typename std::enable_if<Quaternion<T, Packed>::SimdAccelerated, Quaternion<T, Packed>>::type Product(const Quaternion<T, Packed>& lhs, const Quaternion<T, Packed>& rhs) {
23  using SimdT = Simd<T, 4>;
24 
25  SimdT dabc = lhs.vec.simd;
26  SimdT wxyz = rhs.vec.simd;
27  SimdT alternate;
28  alternate.v[0] = -1;
29  alternate.v[1] = 1;
30  alternate.v[2] = -1;
31  alternate.v[3] = 1;
32 
33  // [ 3, 2, 1, 0 ]
34  // [ 0, 3, 2, 1 ]
35  SimdT t0 = SimdT::template shuffle<0, 0, 0, 0>(dabc);
36  SimdT t1 = SimdT::template shuffle<3, 0, 1, 2>(wxyz);
37 
38  SimdT t2 = SimdT::template shuffle<1, 1, 1, 1>(dabc);
39  SimdT t3 = SimdT::template shuffle<2, 1, 0, 3>(wxyz);
40 
41  SimdT t4 = SimdT::template shuffle<2, 2, 2, 2>(dabc);
42  SimdT t5 = SimdT::template shuffle<3, 1, 0, 2>(wxyz);
43 
44  SimdT m0 = SimdT::mul(t0, t1);
45  SimdT m1 = SimdT::mul(t2, t3);
46  SimdT m2 = SimdT::mul(t4, t5);
47 
48  SimdT t6 = SimdT::template shuffle<3, 3, 3, 3>(dabc);
49  SimdT t7 = SimdT::template shuffle<0, 3, 1, 2>(wxyz);
50 
51  SimdT m3 = SimdT::mul(t6, t7);
52 
53  SimdT e = SimdT::add(m0, SimdT::mul(alternate, m1));
54  e = SimdT::template shuffle<1, 3, 0, 2>(e);
55  e = SimdT::add(e, SimdT::mul(alternate, m2));
56  e = SimdT::template shuffle<2, 0, 1, 3>(e);
57  e = SimdT::add(e, SimdT::mul(alternate, m3));
58  e = SimdT::template shuffle<3, 1, 0, 2>(e);
59 
60  ret.vec.simd = e;
61  return ret;
62  }
63 } // namespace impl
64 
65 
66 
67 template <class T, bool Packed>
69  lhs.vec += rhs.vec;
70  return lhs;
71 } // Helpers to write quaternion in paper-style such as (1 + 2_i + 3_j + 4_k). Slow performance, be careful.
72 template <class T, bool Packed>
74  lhs.vec -= rhs.vec;
75  return lhs;
76 }
77 
78 template <class T, bool Packed>
80  lhs = impl::Product(lhs, rhs);
81  return lhs;
82 }
83 
84 template <class T, bool Packed>
86  lhs.vec *= s;
87  return lhs;
88 }
89 
90 template <class T, bool Packed>
92  lhs *= T(1) / s;
93  return lhs;
94 }
95 
96 template <class T, bool Packed>
98  Quaternion<T, Packed> copy(lhs);
99  copy += rhs;
100  return copy;
101 }
102 
103 template <class T, bool Packed>
105  Quaternion<T, Packed> copy(lhs);
106  copy -= rhs;
107  return copy;
108 }
109 
110 template <class T, bool Packed>
112  Quaternion<T, Packed> copy(lhs);
113  copy *= rhs;
114  return copy;
115 }
116 
117 template <class T, bool Packed>
119  Quaternion<T, Packed> copy(lhs);
120  copy *= s;
121  return copy;
122 }
123 
124 template <class T, bool Packed>
126  Quaternion<T, Packed> copy(lhs);
127  copy /= s;
128  return copy;
129 }
130 
131 template <class T, bool Packed>
133  return arg;
134 }
135 
136 template <class T, bool Packed>
138  return Quaternion(-arg.vec);
139 }
140 
141 
143 template <class T, bool Packed, class U, class = typename std::enable_if<!std::is_same<U, Quaternion<T, Packed>>::value>::type>
145  return rhs * s;
146 }
148 template <class T, bool Packed, class U, class = typename std::enable_if<!std::is_same<U, Quaternion<T, Packed>>::value>::type>
150  return rhs / s;
151 }
152 
154 template <class T, bool Packed, class U, class = typename std::enable_if<!traits::IsQuaternion<U>::value>::type>
156  return Quaternion<T, Packed>(rhs.w + lhs, rhs.x, rhs.y, rhs.z);
157 }
158 
159 
160 
161 } // namespace mathter
Matrix< U, Rows, Columns, Order1, Layout1, Packed > & operator+=(Matrix< T, Rows, Columns, Order1, Layout1, Packed > &lhs, const Matrix< U, Rows, Columns, Order2, Layout2, Packed > &rhs)
Performs matrix addition and stores result in this.
Definition: MatrixArithmetic.hpp:287
Swizzle< ST, 3, 0, 1, 2 > wxyz
Definition: Swizzle_4.inc.hpp:279
Allows you to do quaternion math and represent rotation in a compact way.
Definition: Definitions.hpp:69
T x
Definition: QuaternionImpl.hpp:39
auto operator*(const Matrix< T, Rows1, Match, Order1, eMatrixLayout::ROW_MAJOR, Packed > &lhs, const Matrix< U, Match, Columns2, Order2, eMatrixLayout::ROW_MAJOR, Packed > &rhs) -> Matrix< V, Rows1, Columns2, Order1, eMatrixLayout::ROW_MAJOR, Packed >
Definition: MatrixArithmetic.hpp:78
Definition: Approx.hpp:11
Matrix< T, Rows, Columns, Order, Layout, Packed > operator/(T s, const Matrix< T, Rows, Columns, Order, Layout, Packed > &mat)
Definition: MatrixArithmetic.hpp:333
std::enable_if<!Quaternion< T, Packed >::SimdAccelerated, Quaternion< T, Packed > >::type Product(const Quaternion< T, Packed > &lhs, const Quaternion< T, Packed > &rhs)
Definition: QuaternionArithmetic.hpp:10
Matrix< U, Rows, Columns, Order1, Layout1, Packed > & operator-=(Matrix< T, Rows, Columns, Order1, Layout1, Packed > &lhs, const Matrix< U, Rows, Columns, Order2, Layout2, Packed > &rhs)
Performs matrix subtraction and stores result in this.
Definition: MatrixArithmetic.hpp:296
T z
Definition: QuaternionImpl.hpp:39
2,4 or 8 dimension float or double parameters accepted. Uses SSE2 or AVX acceleration if enabled in t...
Definition: Simd.hpp:22
Matrix< U, Rows, Columns, Order1, SameLayout, Packed > operator-(const Matrix< T, Rows, Columns, Order1, SameLayout, Packed > &lhs, const Matrix< U, Rows, Columns, Order2, SameLayout, Packed > &rhs)
Definition: MatrixArithmetic.hpp:239
Matrix< T1, Dim, Dim, Order1, Layout1, Packed > & operator*=(Matrix< T1, Dim, Dim, Order1, Layout1, Packed > &lhs, const Matrix< T2, Dim, Dim, Order2, Layout2, Packed > &rhs)
Definition: MatrixArithmetic.hpp:198
Vector< T, 4, Packed > vec
Definition: QuaternionImpl.hpp:41
T y
Definition: QuaternionImpl.hpp:39
Matrix< T, Rows, Columns, Order, Layout, Packed > & operator/=(Matrix< T, Rows, Columns, Order, Layout, Packed > &mat, T s)
Divides all elements of the matrix by scalar.
Definition: MatrixArithmetic.hpp:321
Matrix< U, Rows, Columns, Order1, SameLayout, Packed > operator+(const Matrix< T, Rows, Columns, Order1, SameLayout, Packed > &lhs, const Matrix< U, Rows, Columns, Order2, SameLayout, Packed > &rhs)
Definition: MatrixArithmetic.hpp:222
T w
Definition: QuaternionImpl.hpp:39
T s
Definition: QuaternionImpl.hpp:36