License Public Domain
Lines 84
Keywords
class (8) OpenGL (15) quaternion (2) vector (5)
Included in this Library
Permissions
Viewable by Everyone
Editable by Michael Sutherland

quaternion.h Atom Feed 0

In Brief This class performs operations on quaternions. It references the vector class already submitted under the library vector.
# 's
  1#ifndef QUATERNION_H
2#define QUATERNION_H
3
4//code from: http://www.lboro.ac.uk/departments/ma/gallery/quat/src.html
5// by Andy Burbanks.
6
7#include <iostream> // stream library
8#include "vector3.h"
9
10class Quaternion
11{
12 friend int operator==(const Quaternion& a, const Quaternion& b);
13 friend int operator!=(const Quaternion& a, const Quaternion& b);
14 friend Quaternion operator-(Quaternion& x);
15 friend Quaternion operator+(const Quaternion& x, const Quaternion& y);
16 friend Quaternion operator-(const Quaternion& x, const Quaternion& y);
17 friend Quaternion operator*(const Quaternion& x, const float y);
18 friend Quaternion operator*(const Quaternion& x, const Quaternion& y);
19 friend Quaternion operator/(const Quaternion& x, const Quaternion& y);
20 friend Quaternion sqrt(const Quaternion& x);
21 friend std::ostream& operator<<(std::ostream& ost, const Quaternion& x);
22 private:
23 float m_r;
24 float m_i;
25 float m_j;
26 float m_k;
27 public:
28 Quaternion();// uninitialized
29
30 Quaternion(float rr) : m_r(rr),m_i(0),m_j(0),m_k(0) {}
31
32 Quaternion( float, float, float, float );
33
34 // copy constructor
35 Quaternion(const Quaternion& b): m_r(b.m_r),m_i(b.m_i),m_j(b.m_j),m_k(b.m_k) {}
36
37 float R() const { return m_r ;}
38 float I() const { return m_i ;}
39 float J() const { return m_j ;}
40 float K() const { return m_k ;}
41 void setR(float r) { m_r = r; }
42 void setI(float i) { m_i = i; }
43 void setJ(float j) { m_j = j; }
44 void setK(float k) { m_k = k; }
45
46 float norm() const;
47 float abs() const;
48
49 void quat2AxisRot(float& angle, float& x, float& y, float& z);
50 static Quaternion axisRot2Quat(float angle, float x, float y, float z);
51 void setQuatAxisRot(float angle, float x, float y, float z);
52 void addRot(float angle, float x, float y, float z);
53 Vector3 transVec(const Vector3& pos);
54 Vector3 invTransVec(const Vector3& pos);
55
56 Quaternion& operator=(const Quaternion& b) {m_r=b.m_r; m_i=b.m_i; m_j=b.m_j; m_k=b.m_k; return *this;}
57
58 Quaternion& negate();
59 Quaternion& conjugate();
60 Quaternion& square();
61
62 Quaternion& invert();
63
64 Quaternion& operator+=(const Quaternion& b);
65 Quaternion& operator-=(const Quaternion& b);
66 Quaternion& operator*=(const float b);
67 Quaternion& operator/=(const float b);
68 Quaternion& operator*=(const Quaternion& b);
69 Quaternion& operator/=(const Quaternion& b);
70};
71
72inline Quaternion operator+(const Quaternion& x, const Quaternion& y)
73{
74 Quaternion z = x;
75 return z += y; // derived operator
76}
77
78inline Quaternion operator-(const Quaternion& x, const Quaternion& y)
79{
80 Quaternion z = x;
81 return z -= y; // derived operator
82}
83
84inline Quaternion operator*(const Quaternion& x, const float y)
85{
86 Quaternion z = x;
87 return z *= y; // derived operator
88}
89
90inline Quaternion operator*(const Quaternion& x, const Quaternion& y)
91{
92 Quaternion z = x;
93 return z *= y; // derived operator
94}
95
96inline Quaternion operator/(const Quaternion& x, const Quaternion& y)
97{
98 Quaternion z = x;
99 return z /= y; // derived operator
100}
101
102#endif

This class performs operations on quaternions. It references the vector class already submitted under the library vector.