1 //----------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------
5 #include "Quaternion.h"
7 //----------------------------------------------------------------------------
8 // Class implementation
9 //----------------------------------------------------------------------------
10 //----------------------------------------------------------------------------
12 //----------------------------------------------------------------------------
14 Quaternion::Quaternion( )
19 double sinHalfTheta = sin(theta/2);
23 Quaternion::Quaternion(Vec3 v1, Vec3 v2)
25 // Normalize the vectors
30 float cosTheta = v1.Dot(v2);
31 theta = acos(cosTheta);
39 // Build the quaternion
41 double sinHalfTheta = sin(theta/2);
45 Quaternion::Quaternion(float nTheta, Vec3 nW)
51 // Build the quaternion
53 double sinHalfTheta = sin(theta/2);
57 Quaternion::Quaternion(float nR, float nI, float nJ, float nK)
60 im = Vec3(nI, nJ, nK);
63 double sinHalfTheta = sin(theta/2);
67 //----------------------------------------------------------------------------
69 //----------------------------------------------------------------------------
71 Quaternion::~Quaternion( )
75 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
79 float Quaternion::getR()
84 float Quaternion::getI()
89 float Quaternion::getJ()
94 float Quaternion::getK()
99 Vec3 Quaternion::getW()
104 Quaternion Quaternion::conjugate( )
106 return Quaternion(theta, -w);
109 Vec3 Quaternion::rotateVector(Vec3 vector)
111 // Quaterion representing the vector
113 Quaternion qv(0,vector[0],vector[1],vector[2]);
114 Quaternion qthis(r,im[0],im[1],im[2]);
115 Quaternion qthis_conj = qthis.conjugate();
118 cout << "Qv: [" << qv << "]" << endl;
119 cout << "Qthis: [" << qthis << "]" << endl;
120 cout << "QthisConj: [" << qthis_conj << "]" << endl;
123 Quaternion Qtemp = qthis*qv;
124 cout << "Qtemp: [" << Qtemp << "]" << endl;
126 Quaternion qf = Qtemp*qthis_conj;
128 cout << "Qf: [" << qf << "]" << endl;
131 //return Vec3(0,0,0);
134 //----------------------------------------------------------------------------
136 //----------------------------------------------------------------------------
138 const float& Quaternion::operator[](unsigned int i) const
140 return (i == 0 ? r : (im)[i-1]);
143 Quaternion Quaternion::operator*(Quaternion q)
146 float b1 = this->w[0];
147 float c1 = this->w[1];
148 float d1 = this->w[2];
155 float r_n = a1*a2 - b1*b2 - c1*c2 - d1*d2;
156 float i_n = a1*b2 + a2*b1 + c1*d2 - c2*d1;
157 float j_n = a1*c2 - b1*d2 + a2*c1 + b2*d1;
158 float k_n = a1*d2 + b1*c2 - b2*c1 + a2*d1;
160 return Quaternion(r_n, i_n, j_n, k_n);
163 std::ostream& operator<<(std::ostream& os, const Quaternion& q)
165 os << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
166 os << " " <<q.theta*180/M_PI << " " << q.w;