/usr/include/QGLViewer/quaternion.h is in libqglviewer-dev-common 2.3.4-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | /****************************************************************************
Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.
This file is part of the QGLViewer library version 2.3.4.
http://www.libqglviewer.com - contact@libqglviewer.com
This file may be used under the terms of the GNU General Public License
versions 2.0 or 3.0 as published by the Free Software Foundation and
appearing in the LICENSE file included in the packaging of this file.
In addition, as a special exception, Gilles Debunne gives you certain
additional rights, described in the file GPL_EXCEPTION in this package.
libQGLViewer uses dual licensing. Commercial/proprietary software must
purchase a libQGLViewer Commercial License.
This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*****************************************************************************/
#ifndef QGLVIEWER_QUATERNION_H
#define QGLVIEWER_QUATERNION_H
#include "vec.h"
#include <math.h>
#include <iostream>
namespace qglviewer {
/*! \brief The Quaternion class represents 3D rotations and orientations.
\class Quaternion quaternion.h QGLViewer/quaternion.h
The Quaternion is an appropriate (although not very intuitive) representation for 3D rotations and
orientations. Many tools are provided to ease the definition of a Quaternion: see constructors,
setAxisAngle(), setFromRotationMatrix(), setFromRotatedBasis().
You can apply the rotation represented by the Quaternion to 3D points using rotate() and
inverseRotate(). See also the Frame class that represents a coordinate system and provides other
conversion functions like Frame::coordinatesOf() and Frame::transformOf().
You can apply the Quaternion \c q rotation to the OpenGL matrices using:
\code
glMultMatrixd(q.matrix());
// equvalent to glRotate(q.angle()*180.0/M_PI, q.axis().x, q.axis().y, q.axis().z);
\endcode
Quaternion is part of the \c qglviewer namespace, specify \c qglviewer::Quaternion or use the qglviewer
namespace: \code using namespace qglviewer; \endcode
<h3>Internal representation</h3>
The internal representation of a Quaternion corresponding to a rotation around axis \c axis, with an angle
\c alpha is made of four doubles q[i]:
\code
{q[0],q[1],q[2]} = sin(alpha/2) * {axis[0],axis[1],axis[2]}
q[3] = cos(alpha/2)
\endcode
Note that certain implementations place the cosine term in first position (instead of last here).
The Quaternion is always normalized, so that its inverse() is actually its conjugate.
See also the Vec and Frame classes' documentations.
\nosubgrouping */
class QGLVIEWER_EXPORT Quaternion
{
public:
/*! @name Defining a Quaternion */
//@{
/*! Default constructor, builds an identity rotation. */
Quaternion()
{ q[0]=q[1]=q[2]=0.0; q[3]=1.0; }
/*! Constructor from rotation axis (non null) and angle (in radians). See also setAxisAngle(). */
Quaternion(const Vec& axis, double angle)
{
setAxisAngle(axis, angle);
}
Quaternion(const Vec& from, const Vec& to);
/*! Constructor from the four values of a Quaternion. First three values are axis*sin(angle/2) and
last one is cos(angle/2).
\attention The identity Quaternion is Quaternion(0,0,0,1) and \e not Quaternion(0,0,0,0) (which is
not unitary). The default Quaternion() creates such identity Quaternion. */
Quaternion(double q0, double q1, double q2, double q3)
{ q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }
/*! Copy constructor. */
Quaternion(const Quaternion& Q)
{ for (int i=0; i<4; ++i) q[i] = Q.q[i]; }
/*! Equal operator. */
Quaternion& operator=(const Quaternion& Q)
{
for (int i=0; i<4; ++i)
q[i] = Q.q[i];
return (*this);
}
/*! Sets the Quaternion as a rotation of axis \p axis and angle \p angle (in radians).
\p axis does not need to be normalized. A null \p axis will result in an identity Quaternion. */
void setAxisAngle(const Vec& axis, double angle)
{
const double norm = axis.norm();
if (norm < 1E-8)
{
// Null rotation
q[0] = 0.0; q[1] = 0.0; q[2] = 0.0; q[3] = 1.0;
}
else
{
const double sin_half_angle = sin(angle / 2.0);
q[0] = sin_half_angle*axis[0]/norm;
q[1] = sin_half_angle*axis[1]/norm;
q[2] = sin_half_angle*axis[2]/norm;
q[3] = cos(angle / 2.0);
}
}
/*! Sets the Quaternion value. See the Quaternion(double, double, double, double) constructor documentation. */
void setValue(double q0, double q1, double q2, double q3)
{ q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }
#ifndef DOXYGEN
void setFromRotationMatrix(const float m[3][3]);
void setFromRotatedBase(const Vec& X, const Vec& Y, const Vec& Z);
#endif
void setFromRotationMatrix(const double m[3][3]);
void setFromRotatedBasis(const Vec& X, const Vec& Y, const Vec& Z);
//@}
/*! @name Accessing values */
//@{
Vec axis() const;
float angle() const;
void getAxisAngle(Vec& axis, float& angle) const;
/*! Bracket operator, with a constant return value. \p i must range in [0..3]. See the Quaternion(double, double, double, double) documentation. */
double operator[](int i) const { return q[i]; }
/*! Bracket operator returning an l-value. \p i must range in [0..3]. See the Quaternion(double, double, double, double) documentation. */
double& operator[](int i) { return q[i]; }
//@}
/*! @name Rotation computations */
//@{
/*! Returns the composition of the \p a and \p b rotations.
The order is important. When applied to a Vec \c v (see operator*(const Quaternion&, const Vec&)
and rotate()) the resulting Quaternion acts as if \p b was applied first and then \p a was
applied. This is obvious since the image \c v' of \p v by the composited rotation satisfies: \code
v'= (a*b) * v = a * (b*v) \endcode
Note that a*b usually differs from b*a.
\attention For efficiency reasons, the resulting Quaternion is not normalized. Use normalize() in
case of numerical drift with small rotation composition. */
friend Quaternion operator*(const Quaternion& a, const Quaternion& b)
{
return Quaternion(a.q[3]*b.q[0] + b.q[3]*a.q[0] + a.q[1]*b.q[2] - a.q[2]*b.q[1],
a.q[3]*b.q[1] + b.q[3]*a.q[1] + a.q[2]*b.q[0] - a.q[0]*b.q[2],
a.q[3]*b.q[2] + b.q[3]*a.q[2] + a.q[0]*b.q[1] - a.q[1]*b.q[0],
a.q[3]*b.q[3] - b.q[0]*a.q[0] - a.q[1]*b.q[1] - a.q[2]*b.q[2]);
}
/*! Quaternion rotation is composed with \p q.
See operator*(), since this is equivalent to \c this = \c this * \p q.
\note For efficiency reasons, the resulting Quaternion is not normalized.
You may normalize() it after each application in case of numerical drift. */
Quaternion& operator*=(const Quaternion &q)
{
*this = (*this)*q;
return *this;
}
/*! Returns the image of \p v by the rotation \p q.
Same as q.rotate(v). See rotate() and inverseRotate(). */
friend Vec operator*(const Quaternion& q, const Vec& v)
{
return q.rotate(v);
}
Vec rotate(const Vec& v) const;
Vec inverseRotate(const Vec& v) const;
//@}
/*! @name Inversion */
//@{
/*! Returns the inverse Quaternion (inverse rotation).
Result has a negated axis() direction and the same angle(). A composition (see operator*()) of a
Quaternion and its inverse() results in an identity function.
Use invert() to actually modify the Quaternion. */
Quaternion inverse() const { return Quaternion(-q[0], -q[1], -q[2], q[3]); }
/*! Inverses the Quaternion (same rotation angle(), but negated axis()).
See also inverse(). */
void invert() { q[0] = -q[0]; q[1] = -q[1]; q[2] = -q[2]; }
/*! Negates all the coefficients of the Quaternion.
This results in an other representation of the \e same rotation (opposite rotation angle, but with
a negated axis direction: the two cancel out). However, note that the results of axis() and
angle() are unchanged after a call to this method since angle() always returns a value in [0,pi].
This method is mainly useful for Quaternion interpolation, so that the spherical
interpolation takes the shortest path on the unit sphere. See slerp() for details. */
void negate() { invert(); q[3] = -q[3]; }
/*! Normalizes the Quaternion coefficients.
This method should not need to be called since we only deal with unit Quaternions. This is however
useful to prevent numerical drifts, especially with small rotational increments. See also
normalized(). */
double normalize()
{
const double norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
for (int i=0; i<4; ++i)
q[i] /= norm;
return norm;
}
/*! Returns a normalized version of the Quaternion.
See also normalize(). */
Quaternion normalized() const
{
double Q[4];
const double norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
for (int i=0; i<4; ++i)
Q[i] = q[i] / norm;
return Quaternion(Q[0], Q[1], Q[2], Q[3]);
}
//@}
/*! @name Associated matrix */
//@{
const GLdouble* matrix() const;
void getMatrix(GLdouble m[4][4]) const;
void getMatrix(GLdouble m[16]) const;
void getRotationMatrix(float m[3][3]) const;
const GLdouble* inverseMatrix() const;
void getInverseMatrix(GLdouble m[4][4]) const;
void getInverseMatrix(GLdouble m[16]) const;
void getInverseRotationMatrix(float m[3][3]) const;
//@}
/*! @name Slerp interpolation */
//@{
static Quaternion slerp(const Quaternion& a, const Quaternion& b, float t, bool allowFlip=true);
static Quaternion squad(const Quaternion& a, const Quaternion& tgA, const Quaternion& tgB, const Quaternion& b, float t);
/*! Returns the "dot" product of \p a and \p b: a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]. */
static double dot(const Quaternion& a, const Quaternion& b) { return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]; }
Quaternion log();
Quaternion exp();
static Quaternion lnDif(const Quaternion& a, const Quaternion& b);
static Quaternion squadTangent(const Quaternion& before, const Quaternion& center, const Quaternion& after);
//@}
/*! @name Random Quaternion */
//@{
static Quaternion randomQuaternion();
//@}
/*! @name XML representation */
//@{
explicit Quaternion(const QDomElement& element);
QDomElement domElement(const QString& name, QDomDocument& document) const;
void initFromDOMElement(const QDomElement& element);
//@}
#ifdef DOXYGEN
/*! @name Output stream */
//@{
/*! Output stream operator. Enables debugging code like:
\code
Quaternion rot(...);
cout << "Rotation=" << rot << endl;
\endcode */
std::ostream& operator<<(std::ostream& o, const qglviewer::Vec&);
//@}
#endif
private:
/*! The internal data representation is private, use operator[] to access values. */
double q[4];
};
} // namespace
std::ostream& operator<<(std::ostream& o, const qglviewer::Quaternion&);
#endif // QGLVIEWER_QUATERNION_H
|