tf2 rolling
tf2 maintains the relationship between coordinate frames in a tree structure buffered in time, and lets the user transform points, vectors, etc between any two coordinate frames at any desired point in time.
Loading...
Searching...
No Matches
Quaternion.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. This notice may not be removed or altered from any source distribution.
13*/
14
15
16
17#ifndef TF2__LINEARMATH__QUATERNION_HPP_
18#define TF2__LINEARMATH__QUATERNION_HPP_
19
20#include <cmath>
21
22#include "Vector3.hpp"
23#include "QuadWord.hpp"
25
26namespace tf2
27{
28
30class Quaternion : public QuadWord {
31public:
35
36 // template <typename tf2Scalar>
37 // explicit Quaternion(const tf2Scalar *v) : Tuple4<tf2Scalar>(v) {}
40 Quaternion(const tf2Scalar& x, const tf2Scalar& y, const tf2Scalar& z, const tf2Scalar& w)
41 : QuadWord(x, y, z, w)
42 {}
48 {
50 }
57 {
59 }
64 void setRotation(const Vector3& axis, const tf2Scalar& angle)
65 {
66 tf2Scalar d = axis.length();
67 tf2Assert(d != tf2Scalar(0.0));
68 tf2Scalar s = tf2Sin(angle * tf2Scalar(0.5)) / d;
69 setValue(axis.x() * s, axis.y() * s, axis.z() * s,
70 tf2Cos(angle * tf2Scalar(0.5)));
71 }
117 {
118 m_floats[0] += q.x(); m_floats[1] += q.y(); m_floats[2] += q.z(); m_floats[3] += q.m_floats[3];
119 return *this;
120 }
121
126 {
127 m_floats[0] -= q.x(); m_floats[1] -= q.y(); m_floats[2] -= q.z(); m_floats[3] -= q.m_floats[3];
128 return *this;
129 }
130
135 {
136 m_floats[0] *= s; m_floats[1] *= s; m_floats[2] *= s; m_floats[3] *= s;
137 return *this;
138 }
139
145 {
146 setValue(m_floats[3] * q.x() + m_floats[0] * q.m_floats[3] + m_floats[1] * q.z() - m_floats[2] * q.y(),
147 m_floats[3] * q.y() + m_floats[1] * q.m_floats[3] + m_floats[2] * q.x() - m_floats[0] * q.z(),
148 m_floats[3] * q.z() + m_floats[2] * q.m_floats[3] + m_floats[0] * q.y() - m_floats[1] * q.x(),
149 m_floats[3] * q.m_floats[3] - m_floats[0] * q.x() - m_floats[1] * q.y() - m_floats[2] * q.z());
150 return *this;
151 }
155 tf2Scalar dot(const Quaternion& q) const
156 {
157 return m_floats[0] * q.x() + m_floats[1] * q.y() + m_floats[2] * q.z() + m_floats[3] * q.m_floats[3];
158 }
159
163 {
164 return dot(*this);
165 }
166
170 {
171 return tf2Sqrt(length2());
172 }
173
175 return std::isnan(m_floats[0]) || std::isnan(m_floats[1]) || std::isnan(m_floats[2]) || std::isnan(m_floats[3]);
176 }
177
182 {
183 return *this /= length();
184 }
185
189 operator*(const tf2Scalar& s) const
190 {
191 return Quaternion(x() * s, y() * s, z() * s, m_floats[3] * s);
192 }
193
194
199 {
200 tf2Assert(s != tf2Scalar(0.0));
201 return *this * (tf2Scalar(1.0) / s);
202 }
203
208 {
209 tf2Assert(s != tf2Scalar(0.0));
210 if(s == tf2Scalar(0.0))
211 {
212 this->setValue(tf2Scalar(std::numeric_limits<tf2Scalar>::quiet_NaN()),
213 tf2Scalar(std::numeric_limits<tf2Scalar>::quiet_NaN()),
214 tf2Scalar(std::numeric_limits<tf2Scalar>::quiet_NaN()),
215 tf2Scalar(std::numeric_limits<tf2Scalar>::quiet_NaN()));
216 return *this;
217 }
218 return *this *= tf2Scalar(1.0) / s;
219 }
220
224 {
225 return *this / length();
226 }
231 {
232 tf2Scalar s = tf2Sqrt(length2() * q.length2());
233 tf2Assert(s != tf2Scalar(0.0));
234 return tf2Acos(dot(q) / s);
235 }
240 {
241 tf2Scalar s = tf2Sqrt(length2() * q.length2());
242 tf2Assert(s != tf2Scalar(0.0));
243 if (dot(q) < 0) // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp
244 return tf2Acos(dot(-q) / s) * tf2Scalar(2.0);
245 else
246 return tf2Acos(dot(q) / s) * tf2Scalar(2.0);
247 }
251 {
253 return s;
254 }
255
259 {
260 tf2Scalar s;
261 if (m_floats[3] >= 0)
262 s = tf2Scalar(2.) * tf2Acos(m_floats[3]);
263 else
264 s = tf2Scalar(2.) * tf2Acos(-m_floats[3]);
265
266 return s;
267 }
268
272 {
274 if (s_squared < tf2Scalar(10.) * TF2SIMD_EPSILON) //Check for divide by zero
275 return Vector3(1.0, 0.0, 0.0); // Arbitrary
277 return Vector3(m_floats[0] / s, m_floats[1] / s, m_floats[2] / s);
278 }
279
283 {
284 return Quaternion(-m_floats[0], -m_floats[1], -m_floats[2], m_floats[3]);
285 }
286
290 operator+(const Quaternion& q2) const
291 {
292 const Quaternion& q1 = *this;
293 return Quaternion(q1.x() + q2.x(), q1.y() + q2.y(), q1.z() + q2.z(), q1.m_floats[3] + q2.m_floats[3]);
294 }
295
299 operator-(const Quaternion& q2) const
300 {
301 const Quaternion& q1 = *this;
302 return Quaternion(q1.x() - q2.x(), q1.y() - q2.y(), q1.z() - q2.z(), q1.m_floats[3] - q2.m_floats[3]);
303 }
304
308 {
309 const Quaternion& q2 = *this;
310 return Quaternion( - q2.x(), - q2.y(), - q2.z(), - q2.m_floats[3]);
311 }
314 {
316 diff = *this - qd;
317 sum = *this + qd;
318 if( diff.dot(diff) > sum.dot(sum) )
319 return qd;
320 return (-qd);
321 }
322
325 {
327 diff = *this - qd;
328 sum = *this + qd;
329 if( diff.dot(diff) < sum.dot(sum) )
330 return qd;
331 return (-qd);
332 }
333
334
340 Quaternion slerp(const Quaternion& q, const tf2Scalar& t) const
341 {
343 if (theta != tf2Scalar(0.0))
344 {
345 tf2Scalar d = tf2Scalar(1.0) / tf2Sin(theta);
346 tf2Scalar s0 = tf2Sin((tf2Scalar(1.0) - t) * theta);
347 tf2Scalar s1 = tf2Sin(t * theta);
348 if (dot(q) < 0) // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp
349 return Quaternion((m_floats[0] * s0 + -q.x() * s1) * d,
350 (m_floats[1] * s0 + -q.y() * s1) * d,
351 (m_floats[2] * s0 + -q.z() * s1) * d,
352 (m_floats[3] * s0 + -q.m_floats[3] * s1) * d);
353 else
354 return Quaternion((m_floats[0] * s0 + q.x() * s1) * d,
355 (m_floats[1] * s0 + q.y() * s1) * d,
356 (m_floats[2] * s0 + q.z() * s1) * d,
357 (m_floats[3] * s0 + q.m_floats[3] * s1) * d);
358
359 }
360 else
361 {
362 return *this;
363 }
364 }
365
367 static const Quaternion& getIdentity()
368 {
369 static const Quaternion identityQuat(tf2Scalar(0.),tf2Scalar(0.),tf2Scalar(0.),tf2Scalar(1.));
370 return identityQuat;
371 }
372
380 {
382 q.setRPY(roll, pitch, yaw);
383 return q;
384 }
385
393 {
396 return q;
397 }
398
405 {
408 return q;
409 }
410
411
412 TF2SIMD_FORCE_INLINE const tf2Scalar& getW() const { return m_floats[3]; }
413
414
415};
416
417
419TF2SIMD_FORCE_INLINE Quaternion
421{
422 return Quaternion(-q.x(), -q.y(), -q.z(), -q.w());
423}
424
425
426
428TF2SIMD_FORCE_INLINE Quaternion
430 return Quaternion(q1.w() * q2.x() + q1.x() * q2.w() + q1.y() * q2.z() - q1.z() * q2.y(),
431 q1.w() * q2.y() + q1.y() * q2.w() + q1.z() * q2.x() - q1.x() * q2.z(),
432 q1.w() * q2.z() + q1.z() * q2.w() + q1.x() * q2.y() - q1.y() * q2.x(),
433 q1.w() * q2.w() - q1.x() * q2.x() - q1.y() * q2.y() - q1.z() * q2.z());
434}
435
436TF2SIMD_FORCE_INLINE Quaternion
437operator*(const Quaternion& q, const Vector3& w)
438{
439 return Quaternion( q.w() * w.x() + q.y() * w.z() - q.z() * w.y(),
440 q.w() * w.y() + q.z() * w.x() - q.x() * w.z(),
441 q.w() * w.z() + q.x() * w.y() - q.y() * w.x(),
442 -q.x() * w.x() - q.y() * w.y() - q.z() * w.z());
443}
444
445TF2SIMD_FORCE_INLINE Quaternion
446operator*(const Vector3& w, const Quaternion& q)
447{
448 return Quaternion( w.x() * q.w() + w.y() * q.z() - w.z() * q.y(),
449 w.y() * q.w() + w.z() * q.x() - w.x() * q.z(),
450 w.z() * q.w() + w.x() * q.y() - w.y() * q.x(),
451 -w.x() * q.x() - w.y() * q.y() - w.z() * q.z());
452}
453
456dot(const Quaternion& q1, const Quaternion& q2)
457{
458 return q1.dot(q2);
459}
460
461
465{
466 return q.length();
467}
468
472{
473 return q1.angle(q2);
474}
475
479{
480 return q1.angleShortestPath(q2);
481}
482
484TF2SIMD_FORCE_INLINE Quaternion
486{
487 return q.inverse();
488}
489
495TF2SIMD_FORCE_INLINE Quaternion
496slerp(const Quaternion& q1, const Quaternion& q2, const tf2Scalar& t)
497{
498 return q1.slerp(q2, t);
499}
500
503{
505 q *= rotation.inverse();
506 return Vector3(q.getX(),q.getY(),q.getZ());
507}
508
509TF2SIMD_FORCE_INLINE Quaternion
510shortestArcQuat(const Vector3& v0, const Vector3& v1) // Game Programming Gems 2.10. make sure v0,v1 are normalized
511{
512 Vector3 c = v0.cross(v1);
513 tf2Scalar d = v0.dot(v1);
514
515 if (d < -1.0 + TF2SIMD_EPSILON)
516 {
519 return Quaternion(n.x(),n.y(),n.z(),0.0f); // just pick any vector that is orthogonal to v0
520 }
521
522 tf2Scalar s = tf2Sqrt((1.0f + d) * 2.0f);
523 tf2Scalar rs = 1.0f / s;
524
525 return Quaternion(c.getX()*rs,c.getY()*rs,c.getZ()*rs,s * 0.5f);
526}
527
528TF2SIMD_FORCE_INLINE Quaternion
535
536}
537#endif // TF2__LINEARMATH__QUATERNION_HPP_
tf2Scalar tf2Sqrt(tf2Scalar x)
Definition Scalar.hpp:177
#define TF2SIMD_EPSILON
Definition Scalar.hpp:202
tf2Scalar tf2Pow(tf2Scalar x, tf2Scalar y)
Definition Scalar.hpp:188
tf2Scalar tf2Cos(tf2Scalar x)
Definition Scalar.hpp:179
#define TF2SIMD_FORCE_INLINE
Definition Scalar.hpp:129
#define tf2Assert(x)
Definition Scalar.hpp:144
tf2Scalar tf2Acos(tf2Scalar x)
Definition Scalar.hpp:182
tf2Scalar tf2Sin(tf2Scalar x)
Definition Scalar.hpp:180
double tf2Scalar
The tf2Scalar type abstracts floating point numbers, to easily switch between double and single float...
Definition Scalar.hpp:159
The QuadWord class is base class for Vector3 and Quaternion. Some issues under PS3 Linux with IBM 2....
Definition QuadWord.hpp:38
void setValue(const tf2Scalar &x, const tf2Scalar &y, const tf2Scalar &z)
Set x,y,z and zero w.
Definition QuadWord.hpp:104
const tf2Scalar & y() const
Return the y value.
Definition QuadWord.hpp:77
const tf2Scalar & x() const
Return the x value.
Definition QuadWord.hpp:75
const tf2Scalar & w() const
Return the w value.
Definition QuadWord.hpp:81
const tf2Scalar & z() const
Return the z value.
Definition QuadWord.hpp:79
tf2Scalar m_floats[4]
Definition QuadWord.hpp:54
The Quaternion implements quaternion to perform linear algebra rotations in combination with Matrix3x...
Definition Quaternion.hpp:30
Quaternion farthest(const Quaternion &qd) const
Definition Quaternion.hpp:313
Quaternion slerp(const Quaternion &q, const tf2Scalar &t) const
Return the quaternion which is the result of Spherical Linear Interpolation between this and the othe...
Definition Quaternion.hpp:340
bool isnan() const
Definition Quaternion.hpp:174
Quaternion operator-(const Quaternion &q2) const
Return the difference between this quaternion and the other.
Definition Quaternion.hpp:299
Quaternion nearest(const Quaternion &qd) const
Definition Quaternion.hpp:324
Quaternion operator*(const tf2Scalar &s) const
Return a scaled version of this quaternion.
Definition Quaternion.hpp:189
Quaternion operator-() const
Return the negative of this quaternion This simply negates each element.
Definition Quaternion.hpp:307
Quaternion()
No initialization constructor.
Definition Quaternion.hpp:34
void setRPY(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
Set the quaternion using fixed axis RPY.
Definition Quaternion.hpp:98
Quaternion & operator+=(const Quaternion &q)
Add two quaternions.
Definition Quaternion.hpp:116
Quaternion & normalize()
Normalize the quaternion Such that x^2 + y^2 + z^2 +w^2 = 1.
Definition Quaternion.hpp:181
Quaternion(const tf2Scalar &x, const tf2Scalar &y, const tf2Scalar &z, const tf2Scalar &w)
Constructor from scalars.
Definition Quaternion.hpp:40
tf2Scalar angle(const Quaternion &q) const
Return the half angle between this quaternion and the other.
Definition Quaternion.hpp:230
static Quaternion createFromEuler(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
Creates a quaternion using Euler angles.
Definition Quaternion.hpp:392
static Quaternion createFromRotation(const Vector3 &axis, const tf2Scalar &angle)
Creates a quaternion using axis angle notation.
Definition Quaternion.hpp:404
void setEuler(const tf2Scalar &yaw, const tf2Scalar &pitch, const tf2Scalar &roll)
Set the quaternion using Euler angles.
Definition Quaternion.hpp:77
Quaternion(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
Constructor from fixed axis RPY.
Definition Quaternion.hpp:56
Quaternion normalized() const
Return a normalized version of this quaternion.
Definition Quaternion.hpp:223
tf2Scalar length2() const
Return the length squared of the quaternion.
Definition Quaternion.hpp:162
tf2Scalar getAngleShortestPath() const
Return the angle [0, Pi] of rotation represented by this quaternion along the shortest path.
Definition Quaternion.hpp:258
Quaternion operator/(const tf2Scalar &s) const
Return an inversely scaled versionof this quaternion.
Definition Quaternion.hpp:198
Quaternion & operator*=(const tf2Scalar &s)
Scale this quaternion.
Definition Quaternion.hpp:134
tf2Scalar dot(const Quaternion &q) const
Return the dot product between this quaternion and another.
Definition Quaternion.hpp:155
Quaternion & operator-=(const Quaternion &q)
Sutf2ract out a quaternion.
Definition Quaternion.hpp:125
Quaternion operator+(const Quaternion &q2) const
Return the sum of this quaternion and the other.
Definition Quaternion.hpp:290
Vector3 getAxis() const
Return the axis of the rotation represented by this quaternion.
Definition Quaternion.hpp:271
const tf2Scalar & getW() const
Definition Quaternion.hpp:412
tf2Scalar angleShortestPath(const Quaternion &q) const
Return the angle between this quaternion and the other along the shortest path.
Definition Quaternion.hpp:239
Quaternion inverse() const
Return the inverse of this quaternion.
Definition Quaternion.hpp:282
tf2Scalar getAngle() const
Return the angle [0, 2Pi] of rotation represented by this quaternion.
Definition Quaternion.hpp:250
Quaternion & operator*=(const Quaternion &q)
Multiply this quaternion by q on the right.
Definition Quaternion.hpp:144
Quaternion(const Vector3 &axis, const tf2Scalar &angle)
Axis angle Constructor.
Definition Quaternion.hpp:47
tf2Scalar length() const
Return the length of the quaternion.
Definition Quaternion.hpp:169
static const Quaternion & getIdentity()
Definition Quaternion.hpp:367
void setRotation(const Vector3 &axis, const tf2Scalar &angle)
Set the rotation using axis angle notation.
Definition Quaternion.hpp:64
Quaternion & operator/=(const tf2Scalar &s)
Inversely scale this quaternion.
Definition Quaternion.hpp:207
static Quaternion createFromRPY(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
Creates a quaternion using fixed axis RPY.
Definition Quaternion.hpp:379
tf2::Vector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16...
Definition Vector3.hpp:40
tf2Scalar dot(const Vector3 &v) const
Return the dot product.
Definition Vector3.hpp:125
const tf2Scalar & z() const
Return the z value.
Definition Vector3.hpp:269
const tf2Scalar & x() const
Return the x value.
Definition Vector3.hpp:265
Vector3 cross(const Vector3 &v) const
Return the cross product between this and another vector.
Definition Vector3.hpp:183
const tf2Scalar & y() const
Return the y value.
Definition Vector3.hpp:267
Definition buffer_core.hpp:58
Vector3 operator*(const Matrix3x3 &m, const Vector3 &v)
Definition Matrix3x3.hpp:612
void tf2PlaneSpace1(const Vector3 &n, Vector3 &p, Vector3 &q)
Definition Vector3.hpp:649
Quaternion shortestArcQuatNormalize2(Vector3 &v0, Vector3 &v1)
Definition Quaternion.hpp:529
tf2Scalar length(const Quaternion &q)
Return the length of a quaternion.
Definition Quaternion.hpp:464
tf2Scalar angle(const Quaternion &q1, const Quaternion &q2)
Return the half angle between two quaternions.
Definition Quaternion.hpp:471
B toMsg(const A &a)
Function that converts from one type to a ROS message type. It has to be implemented by each data typ...
Quaternion shortestArcQuat(const Vector3 &v0, const Vector3 &v1)
Definition Quaternion.hpp:510
tf2Scalar dot(const Quaternion &q1, const Quaternion &q2)
Calculate the dot product between two quaternions.
Definition Quaternion.hpp:456
Vector3 quatRotate(const Quaternion &rotation, const Vector3 &v)
Definition Quaternion.hpp:502
tf2Scalar angleShortestPath(const Quaternion &q1, const Quaternion &q2)
Return the shortest angle between two quaternions.
Definition Quaternion.hpp:478
Quaternion inverse(const Quaternion &q)
Return the inverse of a quaternion.
Definition Quaternion.hpp:485
Quaternion operator-(const Quaternion &q)
Return the negative of a quaternion.
Definition Quaternion.hpp:420
Quaternion slerp(const Quaternion &q1, const Quaternion &q2, const tf2Scalar &t)
Return the result of spherical linear interpolation betwen two quaternions.
Definition Quaternion.hpp:496
#define TF2_PUBLIC
Definition visibility_control.h:57