i6engine  1.0
i6eQuaternion.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 /*
26  * A class to "transport" Quaternion
27  *
28  * its better to send only 1 "type" to the game then many
29  */
30 
31 #ifndef __I6ENGINE_MATH_I6EQUATERNION_H__
32 #define __I6ENGINE_MATH_I6EQUATERNION_H__
33 
34 #include <map>
35 #include <string>
36 
38 
39 namespace Ogre {
40  class Quaternion;
41 } /* namespace Ogre */
42 class btQuaternion;
43 
44 namespace i6e {
45 namespace math {
46 
47  class i6eVector;
48 
59  static const double EPSILON;
60 
61  public:
62  static const i6eQuaternion IDENTITY;
63 
67  i6eQuaternion() : _w(1.0), _x(0.0), _y(0.0), _z(0.0) {
68  }
69 
74  i6eQuaternion(const double w, const double x, const double y, const double z) : _w(w), _x(x), _y(y), _z(z) {
75  }
76 
83  i6eQuaternion(const i6eVector & axis, const double angle);
84 
90  explicit i6eQuaternion(const Ogre::Quaternion & ogre);
91 
97  explicit i6eQuaternion(const btQuaternion & bullet);
98 
99  /*
100  * \brief Constructs a quaternion from an attributeMap
101  *
102  * \param[in] params the attributeMap containing informations for the quaternion
103  */
104  i6eQuaternion(const std::map<std::string, std::string> & params, const std::string & prefix);
105 
106  /*
107  * \brief Constructs a quaternion from a string
108  */
109  explicit i6eQuaternion(const std::string & str);
110 
117  }
118 
122  double getW() const { return _w; }
123  double getX() const { return _x; }
124  double getY() const { return _y; }
125  double getZ() const { return _z; }
126 
130  void setW(const double w) { _w = w; }
131  void setX(const double x) { _x = x; }
132  void setY(const double y) { _y = y; }
133  void setZ(const double z) { _z = z; }
134 
139  return i6eQuaternion(_w + b.getW(), _x + b.getX(), _y + b.getY(), _z + b.getZ());
140  }
141 
146  return i6eQuaternion(_w - b.getW(), _x - b.getX(), _y - b.getY(), _z - b.getZ());
147  }
148 
153  return i6eQuaternion(
154  _w * q.getW() - _x * q.getX() - _y * q.getY() - _z * q.getZ(),
155  _w * q.getX() + _x * q.getW() + _y * q.getZ() - _z * q.getY(),
156  _w * q.getY() + _y * q.getW() + _z * q.getX() - _x * q.getZ(),
157  _w * q.getZ() + _z * q.getW() + _x * q.getY() - _y * q.getX());
158  }
159 
163  i6eQuaternion operator*(const double & v) const {
164  return i6eQuaternion(_w * v, _x * v, _y * v, _z * v);
165  }
166 
170  i6eQuaternion operator/(const double & v) const {
171  return i6eQuaternion(_w / v, _x / v, _y / v, _z / v);
172  }
173 
178  setW(getW() + b.getW());
179  setX(getX() + b.getX());
180  setY(getY() + b.getY());
181  setZ(getZ() + b.getZ());
182  return *this;
183  }
184 
188  void insertInMap(const std::string & prefix, std::map<std::string, std::string> & map) const;
189 
194  return i6eQuaternion(_w, -_x, -_y, -_z);
195  }
196 
200  double length() const;
201 
205  i6eQuaternion normalize() const;
206 
212  bool equals(const i6eQuaternion & q, double eps = i6eQuaternion::EPSILON) const;
213 
219  void toAxisAngle(i6eVector & axis, double & angle) const;
220 
224  bool operator==(const i6eQuaternion & b) const;
225 
229  bool operator!=(const i6eQuaternion & b) const {
230  return !(*this == b);
231  }
232 
237  i6eVector toVector() const;
238 
244  Ogre::Quaternion toOgre() const;
245 
251  btQuaternion toBullet() const;
252 
256  std::string toString() const;
257 
261  template<class Archive>
262  void serialize(Archive & ar, const unsigned int) {
263  ar & _w;
264  ar & _x;
265  ar & _y;
266  ar & _z;
267  }
268 
269  private:
270  double _w, _x, _y, _z;
271  };
272 
276  ISIXE_MATH_API std::ostream & operator<<(std::ostream & stream, const i6eQuaternion & q);
277 
281  ISIXE_MATH_API i6eQuaternion operator*(const i6eQuaternion & q, const i6eVector & w);
282 
286  ISIXE_MATH_API i6eQuaternion operator*(const i6eVector & w, const i6eQuaternion & q);
287 
291  ISIXE_MATH_API double dotProduct(const i6eQuaternion & p, const i6eQuaternion & q);
292 
296  ISIXE_MATH_API double scalProd(const i6eQuaternion & p, const i6eQuaternion & q);
297 
298 } /* namespace math */
299 } /* namespace i6e */
300 
302 
303 #endif /* __I6ENGINE_MATH_I6EQUATERNION_H__ */
304 
ISIXE_MATH_API double scalProd(const i6eQuaternion &p, const i6eQuaternion &q)
calculates scalar product of current Quaternion and p
Class describing a 3d rotation.
Definition: i6eQuaternion.h:58
std::enable_if< std::is_integral< T >::value, bool >::type operator==(const i6eVector2< T > &first, const i6eVector2< T > &second)
Definition: i6eVector2.h:261
#define ISIXE_MATH_API
i6eQuaternion operator+=(const i6eQuaternion &b)
Operator '+=' for Quaternions.
i6eQuaternion operator/(const double &v) const
divides a quaternion by a scalar
i6eQuaternion inverse() const
return the inverted vector of the quaternion
i6e::math::i6eQuaternion Quaternion
double getW() const
getters for the values of the quaternion
i6eQuaternion(const double w, const double x, const double y, const double z)
Creates a new vector with given values. Be careful with the ordering. i6eQuaternion expects W first...
Definition: i6eQuaternion.h:74
Implements 3-dimensional vectors.
Definition: i6eVector.h:48
i6eQuaternion operator-(const i6eQuaternion &b) const
Operator '-' for Quaternions.
ISIXE_MATH_API i6eQuaternion operator*(const i6eQuaternion &q, const i6eVector &w)
multiplies quaternion with i6eVector
bool operator!=(const i6eQuaternion &b) const
Operator '!=' for Quaternions.
i6eQuaternion()
Creates a new vector with all values set to 0.
Definition: i6eQuaternion.h:67
i6eQuaternion operator+(const i6eQuaternion &b) const
Operator '+' for Quaternions.
static const i6eQuaternion IDENTITY
Definition: i6eQuaternion.h:62
ISIXE_MATH_API double dotProduct(const i6eQuaternion &p, const i6eQuaternion &q)
creates dot product out of two quaternions
void serialize(Archive &ar, const unsigned int)
serialize method for quaternion;
void setX(const double x)
i6eQuaternion operator*(const double &v) const
multiplies a quaternion with a scalar
void setY(const double y)
void setZ(const double z)
ISIXE_MATH_API std::ostream & operator<<(std::ostream &stream, const i6eQuaternion &q)
stream operator for quaternion
void setW(const double w)
setters for the values of the quaternion
i6eQuaternion operator*(const i6eQuaternion &q) const
Operator '*' for Quaternions.