WFMath  1.0.2
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., 675 Mass Ave, Cambridge, MA 02139, 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 
32 namespace WFMath {
33 
36 {
37  public:
38  class Identity {};
40  Quaternion(const Identity &) : m_w(1), m_vec(), m_valid(true), m_age(0) {
41  m_vec.zero();
42  }
44  Quaternion () : m_w(0), m_vec(), m_valid(false), m_age(0) {}
46 
49  Quaternion (CoordType w_in, CoordType x_in, CoordType y_in, CoordType z_in);
51  Quaternion (int axis, CoordType angle) : m_w(0), m_vec(), m_valid(false),
52  m_age(0)
53  {rotation(axis, angle);}
55  Quaternion (const Vector<3>& axis, CoordType angle) : m_w(0), m_vec(),
56  m_valid(false),
57  m_age(0)
58  {rotation(axis, angle);}
60 
63  explicit Quaternion (const Vector<3>& axis) : m_w(0), m_vec(),
64  m_valid(false), m_age(0)
65  {rotation(axis);} // angle == axis.mag()
67  Quaternion (const Quaternion& p) : m_w(p.m_w), m_vec(p.m_vec),
68  m_valid(p.m_valid), m_age(p.m_age) {}
70  explicit Quaternion (const AtlasInType& a) : m_w(0), m_vec(),
71  m_valid(false), m_age(0)
72  {fromAtlas(a);}
73 
74  ~Quaternion() {}
75 
76  friend std::ostream& operator<<(std::ostream& os, const Quaternion& p);
77  friend std::istream& operator>>(std::istream& is, Quaternion& p);
78 
80  AtlasOutType toAtlas() const;
82  void fromAtlas(const AtlasInType& a);
83 
84  Quaternion& operator= (const Quaternion& rhs)
85  {m_w = rhs.m_w; m_vec = rhs.m_vec; m_valid = rhs.m_valid; m_age = rhs.m_age; return *this;}
86 
87  // This regards q and -1*q as equal, since they give the
88  // same RotMatrix<3>
89  bool isEqualTo(const Quaternion &q, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
90 
91  bool operator== (const Quaternion& rhs) const {return isEqualTo(rhs);}
92  bool operator!= (const Quaternion& rhs) const {return !isEqualTo(rhs);}
93 
94  bool isValid() const {return m_valid;}
95 
97  Quaternion& identity() {m_w = 1; m_vec.zero(); m_valid = true; m_age = 0; return *this;} // Set to null rotation
98 
99  // Operators
100 
102  Quaternion& operator*= (const Quaternion& rhs);
104  Quaternion& operator/= (const Quaternion& rhs);
106  Quaternion operator* (const Quaternion& rhs) const {
107  Quaternion out(*this);
108  out *= rhs;
109  return out;
110  }
112  Quaternion operator/ (const Quaternion& rhs) const {
113  Quaternion out(*this);
114  out /= rhs;
115  return out;
116  }
117 
118  // Functions
119 
120  // Returns "not_flip", similar to RotMatrix<>.toEuler()
122 
131  bool fromRotMatrix(const RotMatrix<3>& m);
132 
134  Quaternion inverse() const;
135 
137  Quaternion& rotate(const RotMatrix<3>&);
138 
140  Quaternion& rotate(const Quaternion& q) {return operator*=(q);}
141 
143  Quaternion& rotation(int axis, CoordType angle);
145  Quaternion& rotation(const Vector<3>& axis, CoordType angle);
147 
150  Quaternion& rotation(const Vector<3>& axis); // angle == axis.mag()
151 
153  Quaternion& rotation(const Vector<3>& from, const Vector<3>& to);
154 
156  CoordType scalar() const {return m_w;}
158  const Vector<3>& vector() const {return m_vec;}
159 
161  void normalize();
163  unsigned age() const {return m_age;}
164 
165  private:
166  Quaternion(bool valid) : m_w(0), m_vec(), m_valid(valid), m_age(1) {}
167  void checkNormalization() {if(m_age >= WFMATH_MAX_NORM_AGE && m_valid) normalize();}
168  CoordType m_w;
169  Vector<3> m_vec;
170  bool m_valid;
171  unsigned m_age;
172 };
173 
174 } // namespace WFMath
175 
176 #endif // WFMATH_QUATERNION_H
Generic library namespace.
Definition: atlasconv.h:45
Quaternion(const Vector< 3 > &axis)
Construct a Quaternion giving a rotation around the Vector axis.
Definition: quaternion.h:63
Quaternion(const Quaternion &p)
Construct a copy of a Quaternion.
Definition: quaternion.h:67
AtlasOutType toAtlas() const
Create an Atlas object from the Quaternion.
Definition: atlasconv.h:154
Quaternion & rotate(const Quaternion &q)
rotate the quaternion using another quaternion
Definition: quaternion.h:140
bool fromRotMatrix(const RotMatrix< 3 > &m)
set a Quaternion&#39;s value from a RotMatrix
Quaternion inverse() const
returns the inverse of the Quaternion
unsigned age() const
current round-off age
Definition: quaternion.h:163
Vector & zero()
Zero the components of a vector.
Definition: vector_funcs.h:165
float CoordType
Basic floating point type.
Definition: const.h:140
Quaternion(const AtlasInType &a)
Construct a Quaternion from an Atlas::Message::Object.
Definition: quaternion.h:70
void normalize()
normalize to remove accumulated round-off error
CoordType scalar() const
returns the scalar (w) part of the Quaternion
Definition: quaternion.h:156
Quaternion()
Construct a Quatertion.
Definition: quaternion.h:44
Quaternion(int axis, CoordType angle)
Construct a Quaternion giving a rotation around axis by angle.
Definition: quaternion.h:51
Quaternion & rotate(const RotMatrix< 3 > &)
Rotate quaternion using the matrix.
void fromAtlas(const AtlasInType &a)
Set the Quaternion&#39;s value to that given by an Atlas object.
Definition: atlasconv.h:121
Quaternion & rotation(int axis, CoordType angle)
sets the Quaternion to a rotation by angle around axis
A normalized quaterion.
Definition: quaternion.h:35
Quaternion(const Vector< 3 > &axis, CoordType angle)
Construct a Quaternion giving a rotation around the Vector axis by angle.
Definition: quaternion.h:55
const Vector< 3 > & vector() const
returns the Vector (x, y, z) part of the quaternion
Definition: quaternion.h:158
Quaternion & identity()
Set the Quaternion to the identity rotation.
Definition: quaternion.h:97