1 #ifndef __DualQuaternion_hpp__
2 #define __DualQuaternion_hpp__
31 typename std::enable_if<not std::is_floating_point<Q>::value>::type* =
nullptr>
41 typename std::enable_if<std::is_floating_point<F>::value>::type* =
nullptr>
45 template <
typename V3>
48 const float halfAngle = thetaRadians / 2.f;
49 const float sa = std::sin(halfAngle);
50 const float ca = std::cos(halfAngle);
67 template <
typename V3>
70 return V3(q.
x, q.
y, q.
z);
74 template <
typename V3>
83 template <
typename M3>
91 m(0,0) = 1.f - 2.f*(y*y + z*
z);
92 m(0,1) = 2.f*(x*y - w*
z);
93 m(0,2) = 2.f*(x*z + w*
y);
95 m(1,0) = 2.f*(x*y + w*
z);
96 m(1,1) = 1.f - 2.f*(x*x + z*
z);
97 m(1,2) = 2.f*(y*z - w*
x);
99 m(2,0) = 2.f*(x*z - w*
y);
100 m(2,1) = 2.f*(y*z + w*
x);
101 m(2,2) = 1.f - 2.f*(x*x + y*
y);
108 template <
typename M3>
112 const float trace = m(0,0) + m(1,1) + m(2,2);
114 const float s = std::sqrt(trace + 1.f) * 2.f;
116 q.
x = (m(2,1) - m(1,2)) / s;
117 q.
y = (m(0,2) - m(2,0)) / s;
118 q.
z = (m(1,0) - m(0,1)) / s;
119 }
else if ((m(0,0) > m(1,1)) && (m(0,0) > m(2,2))) {
120 const float s = std::sqrt(m(0,0) - m(1,1) - m(2,2) + 1.f) * 2.f;
121 q.
w = (m(2,1) - m(1,2)) / s;
123 q.
y = (m(0,1) + m(1,0)) / s;
124 q.
z = (m(0,2) + m(2,0)) / s;
125 }
else if (m(1,1) > m(2,2)) {
126 const float s = std::sqrt(m(1,1) - m(0,0) - m(2,2) + 1.f) * 2.f;
127 q.
w = (m(0,2) - m(2,0)) / s;
128 q.
x = (m(0,1) + m(1,0)) / s;
130 q.
z = (m(1,2) + m(2,1)) / s;
132 const float s = std::sqrt(m(2,2) - m(0,0) - m(1,1) + 1.f) * 2.f;
133 q.
w = (m(1,0) - m(0,1)) / s;
134 q.
x = (m(0,2) + m(2,0)) / s;
135 q.
y = (m(1,2) + m(2,1)) / s;
160 a.
z*b.
x - a.
x*b.
z + a.
w*b.
y + a.
y*b.
w,
161 a.
x*b.
y - a.
y*b.
x + a.
w*b.
z + a.
z*b.
w,
162 a.
w*b.
w - a.
x*b.
x - a.
y*b.
y - a.
z*b.
z);
175 real(real), dual(dual) {}
177 template <
typename V3>
182 dual =
Quaternion(translation.x / 2.f, translation.y / 2.f, translation.z / 2.f, 0.f) *
real;
191 template <
typename V3>
195 Quaternion(vector.x, vector.y, vector.z, 0.f));
198 template <
typename V3>
204 template <
typename M4>
215 template <
typename M4>
218 M4 m = Quaternion::toMatrix<M4>(dq.
real);
220 m(0,3) = translation.x;
221 m(1,3) = translation.y;
222 m(2,3) = translation.z;
233 static_assert(
sizeof(
DualQuaternion) == 8*4,
"DualQuaternion is of unexpected size!");
234 static_assert(std::is_pod<DualQuaternion>::value ==
true,
"DualQuaternion not POD!");
235 static_assert(std::is_trivial<DualQuaternion>::value ==
true,
"DualQuaternion not trivial!");
236 static_assert(std::is_standard_layout<DualQuaternion>::value ==
true,
"DualQuaternion not standard layout!");
Quaternions are an extension of real numbers used to represent rotations in 3D.
Definition: DualQuaternion.hpp:22
Quaternion(const Q &q)
Definition: DualQuaternion.hpp:32
static const DualQuaternion fromMatrix(const M4 &m)
Definition: DualQuaternion.hpp:205
static const Quaternion fromVector(const V3 &v)
Constructs a quaternion by embedding (identifying with) a 3D vector.
Definition: DualQuaternion.hpp:75
DualQuaternion(const V3 &translation, const Quaternion &rotation)
Definition: DualQuaternion.hpp:178
const Quaternion operator*(const Quaternion &a, const Quaternion &b)
Quaternion multiplication.
Definition: DualQuaternion.hpp:157
Quaternion(float x, float y, float z, float w)
Definition: DualQuaternion.hpp:26
static const DualQuaternion identity()
Definition: DualQuaternion.hpp:185
static const Quaternion identity()
Definition: DualQuaternion.hpp:59
const Quaternion operator+(const Quaternion &a, const Quaternion &b)
Returns a Quaternion that is the sum of quaternions a and b (component-wise sum). ...
Definition: DualQuaternion.hpp:148
Quaternion dual
Definition: DualQuaternion.hpp:231
static const DualQuaternion fromVector(const V3 &vector)
Definition: DualQuaternion.hpp:192
static const V3 toVector(const Quaternion &q)
Definition: DualQuaternion.hpp:68
static const Quaternion fromMatrix(const M3 &m)
Definition: DualQuaternion.hpp:109
const DualQuaternion conjugateDual(const DualQuaternion &dq)
Definition: DualQuaternion.hpp:253
const Quaternion conjugate(const Quaternion &q)
Definition: DualQuaternion.hpp:165
Definition: DualQuaternion.hpp:170
Quaternion(F f)
Definition: DualQuaternion.hpp:42
DualQuaternion(const Quaternion &real, const Quaternion &dual)
Definition: DualQuaternion.hpp:174
nv::matrix4f translation(const nv::vec3f &t)
Definition: AngryDudeApp.cpp:191
float y
Definition: DualQuaternion.hpp:141
float x
Definition: DualQuaternion.hpp:141
static const M3 toMatrix(const Quaternion &q)
Definition: DualQuaternion.hpp:84
static const V3 toVector(const DualQuaternion &dq)
Definition: DualQuaternion.hpp:199
static const M4 toMatrix(const DualQuaternion &dq)
Definition: DualQuaternion.hpp:216
Quaternion real
Definition: DualQuaternion.hpp:231
Quaternion(const V3 &unitAxis, float thetaRadians)
Constructs a unit quaternion from the specified unit 3D vector and rotation angle.
Definition: DualQuaternion.hpp:46
float w
Scalar part of the quaternion.
Definition: DualQuaternion.hpp:144
float z
Vector part of the quaternion.
Definition: DualQuaternion.hpp:141