Elaztek Developer Hub
Blamite Game Engine - blam!  00453.06.08.26.0624.blamite
The core library for the Blamite Game Engine.
OgreEuler.h
Go to the documentation of this file.
1 
12 #ifndef HGUARD_OGRE_MATHS_EULER_H
13 #define HGUARD_OGRE_MATHS_EULER_H
14 
15 #include <OGRE/OgreMath.h>
16 #include <OGRE/OgreVector3.h>
17 #include <OGRE/OgreQuaternion.h>
18 #include <OGRE/OgreMatrix3.h>
19 
20 namespace Ogre {
21 
29  class Euler
30  {
31  public:
34  : mYaw(Radian(0.0f)), mPitch(Radian(0.0f)), mRoll(Radian(0.0f)), mChanged(true)
35  {
36  }
37 
44  Euler(const Radian& y, const Radian& p = Radian(0.0f), const Radian& r = Radian(0.0f))
45  : mYaw(y), mPitch(p), mRoll(r), mChanged(true)
46  {
47  }
48 
55  Euler(Real y, Real p = 0.0f, Real r = 0.0f)
56  : mYaw(Radian(y)), mPitch(Radian(p)), mRoll(Radian(r)), mChanged(true)
57  {
58  }
59 
64  explicit Euler(const Quaternion& quaternion)
65  {
66  fromQuaternion(quaternion);
67  }
68 
69  explicit Euler(const Matrix3& matrix)
70  {
71  fromMatrix3(matrix);
72  }
73 
75  inline Radian yaw() const { return mYaw; }
76 
78  inline Radian pitch() const { return mPitch; }
79 
81  inline Radian roll() const { return mRoll; }
82 
87  inline Euler& setYaw(Radian y)
88  {
89  mYaw = y;
90  mChanged = true;
91  return *this;
92  }
93 
98  inline Euler& setPitch(Radian p)
99  {
100  mPitch = p;
101  mChanged = true;
102  return *this;
103  }
104 
109  inline Euler& setRoll(Radian r)
110  {
111  mRoll = r;
112  mChanged = true;
113  return *this;
114  }
115 
122  inline Euler& orientation(const Radian& y, const Radian& p, const Radian& r)
123  {
124  mYaw = y;
125  mPitch = p;
126  mRoll = r;
127  mChanged = true;
128  return *this;
129  }
130 
135  inline Euler& yaw(const Radian& y)
136  {
137  mYaw += y;
138  mChanged = true;
139  return *this;
140  }
141 
146  inline Euler& pitch(const Radian& p)
147  {
148  mPitch += p;
149  mChanged = true;
150  return *this;
151  }
152 
157  inline Euler& roll(const Radian& r)
158  {
159  mRoll += r;
160  mChanged = true;
161  return *this;
162  }
163 
170  inline Euler& rotate(const Radian& y, const Radian& p, const Radian& r)
171  {
172  mYaw += y;
173  mPitch += p;
174  mRoll += r;
175  mChanged = true;
176  return *this;
177  }
178 
180  inline Vector3 forward() const { return toQuaternion() * Vector3::NEGATIVE_UNIT_Z; }
181 
183  inline Vector3 right() const { return toQuaternion() * Vector3::UNIT_X; }
184 
186  inline Vector3 up() const { return toQuaternion() * Vector3::UNIT_Y; }
187 
192  inline Quaternion toQuaternion() const
193  {
194  if (mChanged)
195  {
196  mCachedQuaternion = Quaternion(mYaw, Vector3::UNIT_Y) * Quaternion(mPitch, Vector3::UNIT_X) * Quaternion(mRoll, Vector3::UNIT_Z);
197  mChanged = false;
198  }
199  return mCachedQuaternion;
200  }
201 
203  inline operator Quaternion() const
204  {
205  return toQuaternion();
206  }
207 
212  inline Euler& fromQuaternion(const Quaternion& quaternion)
213  {
214  Matrix3 rotmat;
215  quaternion.ToRotationMatrix(rotmat);
216  fromMatrix3(rotmat);
217  return *this;
218  }
219 
224  inline Euler& fromMatrix3(const Matrix3& matrix)
225  {
226  matrix.ToEulerAnglesYXZ(mYaw, mPitch, mRoll);
227  mChanged = true;
228  return *this;
229  }
230 
237  inline Euler& direction(const Vector3& v, bool setYaw = true, bool setPitch = true)
238  {
239  Vector3 d(v.normalisedCopy());
240  if (setPitch)
241  mPitch = Math::ASin(d.y);
242  if (setYaw)
243  mYaw = Math::ATan2(-d.x, -d.z);
244  mChanged = setYaw || setPitch;
245  return *this;
246  }
247 
255  inline Euler& normalise(bool normYaw = true, bool normPitch = true, bool normRoll = true)
256  {
257  if (normYaw)
258  wrapAngle(mYaw);
259 
260  if (normPitch)
261  wrapAngle(mPitch);
262 
263  if (normRoll)
264  wrapAngle(mRoll);
265 
266  return *this;
267  }
268 
280  inline Euler rotationTo(const Vector3& dir, bool setYaw = true, bool setPitch = true, bool shortest = true) const
281  {
282  Euler t1;
283  Euler t2;
284  t1.direction(dir, setYaw, setPitch);
285  t2 = t1 - *this;
286  if (shortest && setYaw)
287  {
288  t2.normalise();
289  }
290  return t2;
291  }
292 
294  inline Euler& limitYaw(const Radian& limit)
295  {
296  limitAngle(mYaw, limit);
297  return *this;
298  }
299 
301  inline Euler& limitPitch(const Radian& limit)
302  {
303  limitAngle(mPitch, limit);
304  return *this;
305  }
306 
308  inline Euler& limitRoll(const Radian& limit)
309  {
310  limitAngle(mRoll, limit);
311  return *this;
312  }
313 
315  inline friend std::ostream& operator<<(std::ostream& o, const Euler& e)
316  {
317  o << "<Y:" << e.mYaw << ", P:" << e.mPitch << ", R:" << e.mRoll << ">";
318  return o;
319  }
320 
322  inline Euler operator+(const Euler& rhs) const
323  {
324  return Euler(mYaw + rhs.mYaw, mPitch + rhs.mPitch, mRoll + rhs.mRoll);
325  }
326 
331  inline Euler operator-(const Euler& rhs) const
332  {
333  return Euler(mYaw - rhs.mYaw, mPitch - rhs.mPitch, mRoll - rhs.mRoll);
334  }
335 
337  inline Euler operator*(Real rhs) const
338  {
339  return Euler(mYaw * rhs, mPitch * rhs, mRoll * rhs);
340  }
341 
343  inline friend Euler operator*(Real lhs, const Euler& rhs)
344  {
345  return Euler(lhs * rhs.mYaw, lhs * rhs.mPitch, lhs * rhs.mRoll);
346  }
347 
353  inline Quaternion operator*(Euler rhs) const
354  {
355  Euler e1(*this), e2(rhs);
356  return e1.toQuaternion() * e2.toQuaternion();
357  }
358 
359 
361  inline Vector3 operator*(const Vector3& rhs) const
362  {
363  return toQuaternion() * rhs;
364  }
365 
366 
368  inline Euler& operator=(const Euler& src)
369  {
370  orientation(src.yaw(), src.pitch(), src.roll());
371  return *this;
372  }
373 
375  inline Euler& operator=(const Quaternion& quaternion)
376  {
377  fromQuaternion(quaternion);
378  return *this;
379  }
380 
382  inline Euler& operator=(const Matrix3& matrix)
383  {
384  fromMatrix3(matrix);
385  return *this;
386  }
387 
388  inline friend bool operator==(const Euler& left, const Euler& right)
389  {
390  return left.mYaw == right.mYaw
391  && left.mPitch == right.mPitch
392  && left.mRoll == right.mRoll
393  ;
394  }
395 
396  inline friend bool operator!=(const Euler& left, const Euler& right)
397  {
398  return !(left == right);
399  }
400 
401  inline friend bool sameOrientation(const Euler& left, const Euler& right)
402  {
403  // I'm comparing resulting vectors to avoid having to compare angles that are the same but in different values.
404  // Only the resulting oriented vectors really have any meaning in the end.
405  return left.forward().positionEquals(right.forward())
406  && left.up().positionEquals(right.up())
407  ;
408  }
409 
410  protected:
411  Radian mYaw;
412  Radian mPitch;
413  Radian mRoll;
414  mutable Quaternion mCachedQuaternion;
415  mutable bool mChanged;
416 
417  inline void wrapAngle(Radian& angle)
418  {
419  Real rangle = angle.valueRadians();
420  if (rangle < -Math::PI)
421  {
422  rangle = fmod(rangle, -Math::TWO_PI);
423  if (rangle < -Math::PI)
424  {
425  rangle += Math::TWO_PI;
426  }
427  angle = rangle;
428  mChanged = true;
429  }
430  else if (rangle > Math::PI)
431  {
432  rangle = fmod(rangle, Math::TWO_PI);
433  if (rangle > Math::PI)
434  {
435  rangle -= Math::TWO_PI;
436  }
437  angle = rangle;
438  mChanged = true;
439  }
440 
441  }
442 
443  inline void limitAngle(Radian& angle, const Radian& limit)
444  {
445  if (angle > limit)
446  {
447  angle = limit;
448  mChanged = true;
449  }
450  else if (angle < -limit)
451  {
452  angle = -limit;
453  mChanged = true;
454  }
455  }
456  };
457 
458 }
459 
460 #endif
Ogre::Euler::wrapAngle
void wrapAngle(Radian &angle)
Definition: OgreEuler.h:417
Ogre::Euler::setRoll
Euler & setRoll(Radian r)
Set the roll.
Definition: OgreEuler.h:109
Ogre::Euler::up
Vector3 up() const
Get a vector pointing up.
Definition: OgreEuler.h:186
Ogre::Euler::rotationTo
Euler rotationTo(const Vector3 &dir, bool setYaw=true, bool setPitch=true, bool shortest=true) const
Return the relative euler angles required to rotate from the current forward direction to the specifi...
Definition: OgreEuler.h:280
Real
@ Real
Definition: globals.h:25
Ogre::Euler::operator*
Euler operator*(Real rhs) const
Interpolate the euler angles by rhs.
Definition: OgreEuler.h:337
Ogre::Euler::Euler
Euler(const Radian &y, const Radian &p=Radian(0.0f), const Radian &r=Radian(0.0f))
Constructor which takes yaw, pitch and roll values.
Definition: OgreEuler.h:44
Ogre::Euler::right
Vector3 right() const
Get a vector pointing to the right.
Definition: OgreEuler.h:183
Ogre::Euler::yaw
Radian yaw() const
Get the Yaw angle.
Definition: OgreEuler.h:75
Ogre::Euler::Euler
Euler(const Quaternion &quaternion)
Default constructor with presets.
Definition: OgreEuler.h:64
Ogre::Euler::limitRoll
Euler & limitRoll(const Radian &limit)
Clamp the roll angle to a range of +/-limit.
Definition: OgreEuler.h:308
Ogre::Euler::operator<<
friend std::ostream & operator<<(std::ostream &o, const Euler &e)
Stream operator, for printing the euler component angles to a stream.
Definition: OgreEuler.h:315
Ogre::Euler::fromQuaternion
Euler & fromQuaternion(const Quaternion &quaternion)
Calculate the current euler angles of a given quaternion object.
Definition: OgreEuler.h:212
Ogre::Euler::toQuaternion
Quaternion toQuaternion() const
Calculate the quaternion of the euler object.
Definition: OgreEuler.h:192
Ogre::Euler::pitch
Radian pitch() const
Get the Pitch angle.
Definition: OgreEuler.h:78
Ogre::Euler::mPitch
Radian mPitch
Rotation around the X axis.
Definition: OgreEuler.h:412
Ogre::Euler::limitPitch
Euler & limitPitch(const Radian &limit)
Clamp the pitch angle to a range of +/-limit.
Definition: OgreEuler.h:301
Ogre::Euler::operator-
Euler operator-(const Euler &rhs) const
Subtract two euler objects.
Definition: OgreEuler.h:331
y
font DisplayOffset y
Definition: README.txt:68
Ogre::Euler::operator=
Euler & operator=(const Matrix3 &matrix)
Copy assignment operator (Matrix3)
Definition: OgreEuler.h:382
Ogre::Euler::direction
Euler & direction(const Vector3 &v, bool setYaw=true, bool setPitch=true)
Set the yaw and pitch to face in the given direction.
Definition: OgreEuler.h:237
Ogre::Euler::mCachedQuaternion
Quaternion mCachedQuaternion
Cached quaternion equivalent of this euler object.
Definition: OgreEuler.h:414
Ogre::Euler::roll
Euler & roll(const Radian &r)
Apply a relative roll.
Definition: OgreEuler.h:157
Ogre::Euler::pitch
Euler & pitch(const Radian &p)
Apply a relative pitch.
Definition: OgreEuler.h:146
Ogre::Euler::setYaw
Euler & setYaw(Radian y)
Set the yaw.
Definition: OgreEuler.h:87
Ogre::Euler::forward
Vector3 forward() const
Get a vector pointing forwards.
Definition: OgreEuler.h:180
Ogre::Euler::operator*
friend Euler operator*(Real lhs, const Euler &rhs)
Interpolate the euler angle by lhs.
Definition: OgreEuler.h:343
Ogre::Euler::sameOrientation
friend bool sameOrientation(const Euler &left, const Euler &right)
Definition: OgreEuler.h:401
Ogre::Euler::mRoll
Radian mRoll
Rotation around the Z axis.
Definition: OgreEuler.h:413
Ogre::Euler::setPitch
Euler & setPitch(Radian p)
Set the pitch.
Definition: OgreEuler.h:98
Ogre::Euler::mYaw
Radian mYaw
Rotation around the Y axis.
Definition: OgreEuler.h:411
Ogre::Euler::limitAngle
void limitAngle(Radian &angle, const Radian &limit)
Definition: OgreEuler.h:443
Ogre::Euler::operator+
Euler operator+(const Euler &rhs) const
Add two euler objects.
Definition: OgreEuler.h:322
Ogre::Euler::orientation
Euler & orientation(const Radian &y, const Radian &p, const Radian &r)
Set all rotations at once.
Definition: OgreEuler.h:122
Ogre::Euler::roll
Radian roll() const
Get the Roll angle.
Definition: OgreEuler.h:81
Ogre::Euler::operator*
Vector3 operator*(const Vector3 &rhs) const
Apply the euler rotation to the vector rhs.
Definition: OgreEuler.h:361
Ogre::Euler::fromMatrix3
Euler & fromMatrix3(const Matrix3 &matrix)
Calculate the current euler angles of a given matrix object.
Definition: OgreEuler.h:224
Ogre::Euler::operator=
Euler & operator=(const Quaternion &quaternion)
Copy assignment operator (Quaternion)
Definition: OgreEuler.h:375
Ogre::Euler::normalise
Euler & normalise(bool normYaw=true, bool normPitch=true, bool normRoll=true)
Normalise the selected rotations to be within the +/-180 degree range.
Definition: OgreEuler.h:255
Ogre::Euler::yaw
Euler & yaw(const Radian &y)
Apply a relative yaw.
Definition: OgreEuler.h:135
Ogre::Euler::mChanged
bool mChanged
Is the cached quaternion out of date?
Definition: OgreEuler.h:415
Ogre
Definition: world_objects.h:23
Ogre::Euler::operator==
friend bool operator==(const Euler &left, const Euler &right)
Definition: OgreEuler.h:388
BlamTagFieldType::Vector3
@ Vector3
Indicates that the field is a vector3.
Ogre::Euler::operator=
Euler & operator=(const Euler &src)
Copy assignment operator (Euler)
Definition: OgreEuler.h:368
Ogre::Euler::rotate
Euler & rotate(const Radian &y, const Radian &p, const Radian &r)
Apply all relative rotations at once.
Definition: OgreEuler.h:170
Ogre::Euler::operator*
Quaternion operator*(Euler rhs) const
Multiply two eulers.
Definition: OgreEuler.h:353
Ogre::Euler::limitYaw
Euler & limitYaw(const Radian &limit)
Clamp the yaw angle to a range of +/-limit.
Definition: OgreEuler.h:294
Ogre::Euler::operator!=
friend bool operator!=(const Euler &left, const Euler &right)
Definition: OgreEuler.h:396
Ogre::Euler
Class for Euler rotations.
Definition: OgreEuler.h:29
Ogre::Euler::Euler
Euler(Real y, Real p=0.0f, Real r=0.0f)
Constructor which takes yaw, pitch and roll values as reals (radians).
Definition: OgreEuler.h:55
Ogre::Euler::Euler
Euler()
Default constructor.
Definition: OgreEuler.h:33
Ogre::Euler::Euler
Euler(const Matrix3 &matrix)
Definition: OgreEuler.h:69