23 #ifndef QGLVIEWER_QUATERNION_H
24 #define QGLVIEWER_QUATERNION_H
73 { q[0]=q[1]=q[2]=0.0; q[3]=1.0; }
76 Quaternion(
const Vec& axis, qreal angle)
78 setAxisAngle(axis, angle);
81 Quaternion(
const Vec& from,
const Vec& to);
88 Quaternion(qreal q0, qreal q1, qreal q2, qreal q3)
89 { q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }
93 {
for (
int i=0; i<4; ++i) q[i] = Q.
q[i]; }
98 for (
int i=0; i<4; ++i)
106 void setAxisAngle(
const Vec& axis, qreal angle)
108 const qreal norm = axis.
norm();
112 q[0] = 0.0; q[1] = 0.0; q[2] = 0.0; q[3] = 1.0;
116 const qreal sin_half_angle = sin(angle / 2.0);
117 q[0] = sin_half_angle*axis[0]/norm;
118 q[1] = sin_half_angle*axis[1]/norm;
119 q[2] = sin_half_angle*axis[2]/norm;
120 q[3] = cos(angle / 2.0);
125 void setValue(qreal q0, qreal q1, qreal q2, qreal q3)
126 { q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }
129 void setFromRotationMatrix(
const float m[3][3]);
130 void setFromRotatedBase(
const Vec& X,
const Vec& Y,
const Vec& Z);
132 void setFromRotationMatrix(
const qreal m[3][3]);
133 void setFromRotatedBasis(
const Vec& X,
const Vec& Y,
const Vec& Z);
141 void getAxisAngle(
Vec& axis, qreal& angle)
const;
144 qreal operator[](
int i)
const {
return q[i]; }
147 qreal& operator[](
int i) {
return q[i]; }
167 a.
q[3]*b.
q[1] + b.
q[3]*a.
q[1] + a.
q[2]*b.
q[0] - a.
q[0]*b.
q[2],
168 a.
q[3]*b.
q[2] + b.
q[3]*a.
q[2] + a.
q[0]*b.
q[1] - a.
q[1]*b.
q[0],
169 a.
q[3]*b.
q[3] - b.
q[0]*a.
q[0] - a.
q[1]*b.
q[1] - a.
q[2]*b.
q[2]);
180 *
this = (*this)*quat;
187 friend Vec
operator*(
const Quaternion& quat,
const Vec& v)
189 return quat.rotate(v);
192 Vec rotate(
const Vec& v)
const;
193 Vec inverseRotate(
const Vec& v)
const;
205 Quaternion inverse()
const {
return Quaternion(-q[0], -q[1], -q[2], q[3]); }
210 void invert() { q[0] = -q[0]; q[1] = -q[1]; q[2] = -q[2]; }
220 void negate() { invert(); q[3] = -q[3]; }
229 const qreal norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
230 for (
int i=0; i<4; ++i)
241 const qreal norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
242 for (
int i=0; i<4; ++i)
251 const GLdouble* matrix()
const;
252 void getMatrix(GLdouble m[4][4])
const;
253 void getMatrix(GLdouble m[16])
const;
255 void getRotationMatrix(qreal m[3][3])
const;
257 const GLdouble* inverseMatrix()
const;
258 void getInverseMatrix(GLdouble m[4][4])
const;
259 void getInverseMatrix(GLdouble m[16])
const;
261 void getInverseRotationMatrix(qreal m[3][3])
const;
270 static qreal dot(
const Quaternion& a,
const Quaternion& b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]; }
285 explicit Quaternion(
const QDomElement& element);
286 QDomElement domElement(
const QString& name, QDomDocument& document)
const;
287 void initFromDOMElement(
const QDomElement& element);
311 #endif // QGLVIEWER_QUATERNION_H