/usr/include/crystalspace-2.0/csgeom/quaternion.h is in libcrystalspace-dev 2.0+dfsg-1build1.
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 312 313 314 315 316 317 318 319 | /*
Copyright (C) 2000 by Norman Kramer
2006 by Marten Svanfeldt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_QUATERNION_H__
#define __CS_QUATERNION_H__
/**\file
* Quaternions.
*/
/**
* \addtogroup geom_utils
* @{ */
#include "csextern.h"
#include "csqsqrt.h"
#include "csgeom/vector3.h"
class csMatrix3;
/**
* Class for a quaternion.
* A SE3 rotation represented as a normalized quaternion
* \sa csDualQuaternion
*/
class CS_CRYSTALSPACE_EXPORT csQuaternion
{
public:
// Constructors
/// Initialize with identity
csQuaternion ()
: v (0.0f), w (1.0f)
{}
/// Initialize with given values. Does not normalize
csQuaternion (float x, float y, float z, float w)
: v (x, y, z), w (w)
{}
/// Construct from a vector and given w value
csQuaternion (const csVector3& v, float w)
: v (v), w (w)
{}
/// Copy-constructor
csQuaternion (const csQuaternion& q)
: v (q.v), w (q.w)
{}
/**
* Set the components
*/
inline void Set (float x, float y, float z, float w)
{
v.x = x;
v.y = y;
v.z = z;
this->w = w;
}
/// Set quaternion to identity rotation
inline void SetIdentity ()
{
v.Set (0.0f); w = 1.0f;
}
/// Add two quaternions
inline friend csQuaternion operator+ (const csQuaternion& q1,
const csQuaternion& q2)
{
return csQuaternion (q1.v+q2.v, q1.w+q2.w);
}
/// Add quaternion to this one
inline csQuaternion& operator+= (const csQuaternion& q)
{
v += q.v; w += q.w;
return *this;
}
/// Subtract two quaternions
inline friend csQuaternion operator- (const csQuaternion& q1,
const csQuaternion& q2)
{
return csQuaternion (q1.v-q2.v, q1.w-q2.w);
}
/// Subtract quaternion from this one
inline csQuaternion& operator-= (const csQuaternion& q)
{
v -= q.v; w -= q.w;
return *this;
}
/// Get the negative quaternion (unary minus)
inline friend csQuaternion operator- (const csQuaternion& q)
{
return csQuaternion (-q.v, -q.w);
}
/// Multiply two quaternions, Grassmann product
inline friend csQuaternion operator* (const csQuaternion& q1,
const csQuaternion& q2)
{
return csQuaternion (q1.v*q2.w + q1.w*q2.v + q1.v%q2.v,
q1.w*q2.w - q1.v*q2.v);
}
/// Multiply this quaternion by another
inline csQuaternion& operator*= (const csQuaternion& q)
{
csVector3 newV = v*q.w + w*q.v + v%q.v;
w = w*q.w - v*q.v;
v = newV;
return *this;
}
/// Multiply by scalar
inline friend csQuaternion operator* (const csQuaternion& q, float f)
{
return csQuaternion (q.v*f, q.w*f);
}
/// Multiply by scalar
inline friend csQuaternion operator* (float f, const csQuaternion& q)
{
return csQuaternion (q.v*f, q.w*f);
}
/// Multiply by scalar
inline csQuaternion& operator*= (float f)
{
v *= f;
w *= f;
return *this;
}
/// Divide by scalar
inline friend csQuaternion operator/ (const csQuaternion& q, float f)
{
float invF = 1.0f/f;
return csQuaternion (q.v*invF, q.w*invF);
}
/// Divide by scalar
inline friend csQuaternion operator/ (float f, const csQuaternion& q)
{
float invF = 1.0f/f;
return csQuaternion (q.v*invF, q.w*invF);
}
/// Divide by scalar
inline csQuaternion& operator/= (float f)
{
float invF = 1.0f/f;
v *= invF;
w *= invF;
return *this;
}
/// Get the conjugate quaternion
inline csQuaternion GetConjugate () const
{
return csQuaternion (-v, w);
}
/// Set this quaternion to its own conjugate
inline void Conjugate ()
{
v = -v;
}
/// Return euclidian inner-product (dot)
inline float Dot (const csQuaternion& q) const
{
return v*q.v + w*q.w;
}
/// Get the squared norm of this quaternion (equals dot with itself)
inline float SquaredNorm () const
{
return Dot (*this);
}
/// Get the norm of this quaternion
inline float Norm () const
{
return csQsqrt (SquaredNorm ());
}
/**
* Return a unit-lenght version of this quaternion (also called sgn)
* Attempting to normalize a zero-length quaternion will result in a divide by
* zero error. This is as it should be... fix the calling code.
*/
inline csQuaternion Unit () const
{
return (*this) / Norm ();
}
/**
* Rotate vector by quaternion.
*/
inline csVector3 Rotate (const csVector3& src) const
{
csQuaternion p (src, 0);
csQuaternion q = *this * p;
q *= GetConjugate ();
return q.v;
}
/**
* Set a quaternion using axis-angle representation
* \param axis
* Rotation axis. Should be normalized before calling this function.
* \param angle
* Angle to rotate about axis (in radians)
*/
inline void SetAxisAngle (const csVector3& axis, float angle)
{
v = axis * sinf (angle / 2.0f);
w = cosf (angle / 2.0f);
}
/**
* Get a quaternion as axis-angle representation
* \param axis
* Rotation axis.
* \param angle
* Angle to rotate about axis (in radians)
*/
inline void GetAxisAngle (csVector3& axis, float& angle) const
{
angle = 2.0f * acosf (w);
if (v.SquaredNorm () != 0)
axis = v.Unit ();
else
axis.Set (1.0f, 0.0f, 0.0f);
}
/**
* Set quaternion using Euler angles X, Y, Z, expressed in radians
*/
void SetEulerAngles (const csVector3& angles);
/**
* Get quaternion as three Euler angles X, Y, Z, expressed in radians
*/
csVector3 GetEulerAngles () const;
/**
* Set quaternion using 3x3 rotation matrix
*/
void SetMatrix (const csMatrix3& matrix);
/**
* Get quaternion as a 3x3 rotation matrix
*/
csMatrix3 GetMatrix () const;
/**
* Interpolate this quaternion with another using normalized linear
* interpolation (nlerp) using given interpolation factor.
*/
csQuaternion NLerp (const csQuaternion& q2, float t) const;
/**
* Interpolate this quaternion with another using spherical linear
* interpolation (slerp) using given interpolation factor.
*/
csQuaternion SLerp (const csQuaternion& q2, float t) const;
/**
* Get the logarithm of this quaternion
*/
csQuaternion Log () const;
/**
* Get the exponential of this quaternion
*/
csQuaternion Exp () const;
/**
* Interpolate this quaternion with another (q) using cubic linear
* interpolation (squad) using given interpolation factor (t)
* and tangents (t1 and t2)
*/
csQuaternion Squad (const csQuaternion & t1, const csQuaternion & t2,
const csQuaternion & q, float t) const;
/// x, y and z components of the quaternion
csVector3 v;
/// w component of the quaternion
float w;
};
/** @} */
#endif // __CS_QUATERNION_H__
|