wfmath 1.0.3
A math library for the Worldforge system.
quaternion.h
1// quaternion.h (based on the Quaternion class from eris)
2//
3// The WorldForge Project
4// Copyright (C) 2002 The WorldForge Project
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19//
20// For information about WorldForge and its authors, please contact
21// the Worldforge Web Site at http://www.worldforge.org.
22//
23
24// Author: Ron Steinke
25
26#ifndef WFMATH_QUATERNION_H
27#define WFMATH_QUATERNION_H
28
29#include <wfmath/vector.h>
30#include <wfmath/rotmatrix.h>
31
32namespace WFMath {
33
36{
37 public:
38
44 static const Quaternion& IDENTITY();
45
46 class Identity {};
48 Quaternion(const Identity &) : m_w(1), m_vec(WFMath::Vector<3>::ZERO()), m_valid(true), m_age(0) {
49 }
51 Quaternion() : m_w(0), m_vec{}, m_valid(false), m_age(0){}
53
56 Quaternion (CoordType w_in, CoordType x_in, CoordType y_in, CoordType z_in);
58 Quaternion (int axis, CoordType angle) : m_w(0), m_vec(), m_valid(false),
59 m_age(0)
60 {rotation(axis, angle);}
62 Quaternion (const Vector<3>& axis, CoordType angle) : m_w(0), m_vec(),
63 m_valid(false),
64 m_age(0)
65 {rotation(axis, angle);}
67
70 explicit Quaternion (const Vector<3>& axis) : m_w(0), m_vec(),
71 m_valid(false), m_age(0)
72 {rotation(axis);} // angle == axis.mag()
74 Quaternion (const Quaternion& p) = default;
76 explicit Quaternion (const AtlasInType& a) : m_w(0), m_vec(),
77 m_valid(false), m_age(0)
78 {fromAtlas(a);}
79
80 ~Quaternion() = default;
81
82 friend std::ostream& operator<<(std::ostream& os, const Quaternion& p);
83 friend std::istream& operator>>(std::istream& is, Quaternion& p);
84
86 AtlasOutType toAtlas() const;
88 void fromAtlas(const AtlasInType& a);
89
90 Quaternion& operator= (const Quaternion& rhs) = default;
91
92 // This regards q and -1*q as equal, since they give the
93 // same RotMatrix<3>
94 bool isEqualTo(const Quaternion &q, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
95
96 bool operator== (const Quaternion& rhs) const {return isEqualTo(rhs);}
97 bool operator!= (const Quaternion& rhs) const {return !isEqualTo(rhs);}
98
99 bool isValid() const {return m_valid;}
100
102 Quaternion& identity() {m_w = 1; m_vec.zero(); m_valid = true; m_age = 0; return *this;} // Set to null rotation
103
104 // Operators
105
107 Quaternion& operator*= (const Quaternion& rhs);
109 Quaternion& operator/= (const Quaternion& rhs);
111 Quaternion operator* (const Quaternion& rhs) const {
112 Quaternion out(*this);
113 out *= rhs;
114 return out;
115 }
117 Quaternion operator/ (const Quaternion& rhs) const {
118 Quaternion out(*this);
119 out /= rhs;
120 return out;
121 }
122
123 // Functions
124
125 // Returns "not_flip", similar to RotMatrix<>.toEuler()
127
136 bool fromRotMatrix(const RotMatrix<3>& m);
137
139 Quaternion inverse() const;
140
142 Quaternion& rotate(const RotMatrix<3>&);
143
145 Quaternion& rotate(const Quaternion& q) {return operator*=(q);}
146
148 Quaternion& rotation(int axis, CoordType angle);
150 Quaternion& rotation(const Vector<3>& axis, CoordType angle);
152
155 Quaternion& rotation(const Vector<3>& axis); // angle == axis.mag()
156
167 Quaternion& rotation(const Vector<3>& from, const Vector<3>& to);
168
177 Quaternion& rotation(const Vector<3>& from, const Vector<3>& to, const Vector<3>& fallbackAxis);
178
180 CoordType scalar() const {return m_w;}
182 const Vector<3>& vector() const {return m_vec;}
183
185 void normalize();
187 unsigned age() const {return m_age;}
188
189 private:
190 Quaternion(bool valid) : m_w(0), m_vec(), m_valid(valid), m_age(1) {}
191 void checkNormalization() {if(m_age >= WFMATH_MAX_NORM_AGE && m_valid) normalize();}
192 CoordType m_w;
193 Vector<3> m_vec;
194 bool m_valid;
195 unsigned m_age;
196};
197
198} // namespace WFMath
199
200#endif // WFMATH_QUATERNION_H
A normalized quaternion.
Definition: quaternion.h:36
Quaternion(const AtlasInType &a)
Construct a Quaternion from an Atlas::Message::Object.
Definition: quaternion.h:76
Quaternion inverse() const
returns the inverse of the Quaternion
Definition: quaternion.cpp:189
CoordType scalar() const
returns the scalar (w) part of the Quaternion
Definition: quaternion.h:180
void normalize()
normalize to remove accumulated round-off error
Definition: quaternion.cpp:322
bool fromRotMatrix(const RotMatrix< 3 > &m)
set a Quaternion's value from a RotMatrix
Definition: quaternion.cpp:138
void fromAtlas(const AtlasInType &a)
Set the Quaternion's value to that given by an Atlas object.
Definition: atlasconv.h:127
Quaternion(const Quaternion &p)=default
Construct a copy of a Quaternion.
Quaternion & rotate(const Quaternion &q)
rotate the quaternion using another quaternion
Definition: quaternion.h:145
Quaternion & rotate(const RotMatrix< 3 > &)
Rotate quaternion using the matrix.
Definition: quaternion.cpp:198
Quaternion(const Vector< 3 > &axis, CoordType angle)
Construct a Quaternion giving a rotation around the Vector axis by angle.
Definition: quaternion.h:62
unsigned age() const
current round-off age
Definition: quaternion.h:187
static const Quaternion & IDENTITY()
Definition: quaternion.cpp:64
Quaternion & rotation(int axis, CoordType angle)
sets the Quaternion to a rotation by angle around axis
Definition: quaternion.cpp:207
Quaternion(int axis, CoordType angle)
Construct a Quaternion giving a rotation around axis by angle.
Definition: quaternion.h:58
Quaternion()
Construct a Quaternion.
Definition: quaternion.h:51
AtlasOutType toAtlas() const
Create an Atlas object from the Quaternion.
Definition: atlasconv.h:172
const Vector< 3 > & vector() const
returns the Vector (x, y, z) part of the quaternion
Definition: quaternion.h:182
Quaternion(const Vector< 3 > &axis)
Construct a Quaternion giving a rotation around the Vector axis.
Definition: quaternion.h:70
Quaternion & identity()
Set the Quaternion to the identity rotation.
Definition: quaternion.h:102
A dim dimensional vector.
Definition: vector.h:121
Vector & zero()
Zero the components of a vector.
Definition: vector_funcs.h:148
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140