QuaternionT.hh 9.93 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
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
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper 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 Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38 39 40 41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
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



//=============================================================================
//
//  CLASS Quaternion
//
//=============================================================================

#ifndef ACG_QUATERNION_HH
#define ACG_QUATERNION_HH


//== INCLUDES =================================================================

#include "VectorT.hh"
#include "Matrix4x4T.hh"


//== NAMESPACES  ==============================================================

namespace ACG {


//== CLASS DEFINITION =========================================================


/**
    Quaternion class for representing rotations.
*/

template <class Scalar>
class QuaternionT : public VectorT<Scalar,4>
{
public:

#define W Base::values_[0]
#define X Base::values_[1]
#define Y Base::values_[2]
#define Z Base::values_[3]


  typedef VectorT<Scalar,4>    Base;
  typedef QuaternionT<Scalar>  Quaternion;
  typedef VectorT<Scalar,3>    Vec3;
  typedef VectorT<Scalar,4>    Vec4;
  typedef Matrix4x4T<Scalar>   Matrix;


  /// construct from 4 Scalars (default)
  QuaternionT(Scalar _w=1.0, Scalar _x=0.0, Scalar _y=0.0, Scalar _z=0.0)
    : Vec4(_w, _x, _y, _z) {}

  /// construct from 3D point (pure imaginary quaternion)
  QuaternionT(const Vec3& _p)
    : Vec4(0, _p[0], _p[1], _p[2]) {}

  /// construct from 4D vector
  QuaternionT(const Vec4& _p)
    : Vec4(_p[0], _p[1], _p[2], _p[3]) {}

  /// construct from rotation axis and angle (in radians)
  QuaternionT(Vec3 _axis, Scalar _angle)
  {
    _axis.normalize();
    Scalar theta = 0.5 * _angle;
    Scalar sin_theta = sin(theta);
    W = cos(theta);
    X = sin_theta * _axis[0];
    Y = sin_theta * _axis[1];
    Z = sin_theta * _axis[2];
  }

115 116 117 118
//   /// construct from rotation matrix (only valid for rotation matrices!)
//   template <class MatrixT>
//   QuaternionT(const MatrixT& _rot)
//   { init_from_matrix( _rot); }
119
  
Jan Möbius's avatar
 
Jan Möbius committed
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

  /// identity rotation
  void identity() { W=1.0; X=Y=Z=0.0; }


  /// conjugate quaternion
  Quaternion conjugate() const
  { return Quaternion(W, -X, -Y, -Z); }


  /// invert quaternion
  Quaternion invert() const
  { return conjugate()/Base::sqrnorm(); }


  /// quaternion * quaternion
  Quaternion operator*(const Quaternion& _q) const
  { return Quaternion(W*_q.W - X*_q.X - Y*_q.Y - Z*_q.Z,
		      W*_q.X + X*_q.W + Y*_q.Z - Z*_q.Y,
		      W*_q.Y - X*_q.Z + Y*_q.W + Z*_q.X,
		      W*_q.Z + X*_q.Y - Y*_q.X + Z*_q.W); }


  /// quaternion *= quaternion
  Quaternion& operator*=(const Quaternion& _q)
  { return *this = *this * _q; }


  /// rotate vector
149 150
  template <class Vec3T>
  Vec3T rotate(const Vec3T& _v) const
Jan Möbius's avatar
Jan Möbius committed
151
  { 
152 153
    Quaternion q = *this * Quaternion(0,_v[0],_v[1],_v[2]) * conjugate();
    return Vec3T(q[1], q[2], q[3]);
Jan Möbius's avatar
Jan Möbius committed
154
  }
Jan Möbius's avatar
 
Jan Möbius committed
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


  /// get rotation axis and angle (only valid for unit quaternions!)
  void axis_angle(Vec3& _axis, Scalar& _angle) const
  {
    if (fabs(W) > 0.999999)
    {
      _axis  = Vec3(1,0,0);
      _angle = 0;
    }
    else
    {
      _angle = 2.0 * acos(W);
      _axis  = Vec3(X, Y, Z).normalize();
    }
  }



  /// cast to rotation matrix
  Matrix rotation_matrix() const
  {
    Scalar
      ww(W*W), xx(X*X), yy(Y*Y), zz(Z*Z), wx(W*X),
      wy(W*Y), wz(W*Z), xy(X*Y), xz(X*Z), yz(Y*Z);

    Matrix m;

    m(0,0) = ww + xx - yy - zz;
    m(1,0) = 2.0*(xy + wz);
    m(2,0) = 2.0*(xz - wy);

    m(0,1) = 2.0*(xy - wz);
    m(1,1) = ww - xx + yy - zz;
    m(2,1) = 2.0*(yz + wx);

    m(0,2) = 2.0*(xz + wy);
    m(1,2) = 2.0*(yz - wx);
    m(2,2) = ww - xx - yy + zz;

    m(0,3) = m(1,3) = m(2,3) = m(3,0) = m(3,1) = m(3,2) = 0.0;
    m(3,3) = 1.0;

    return m;
  }



  /// get matrix for mult from right (Qr = q*r)
  Matrix right_mult_matrix() const
  {
    Matrix m;
    m(0,0) =  W; m(0,1) = -X; m(0,2) = -Y; m(0,3) = -Z;
    m(1,0) =  X; m(1,1) =  W; m(1,2) = -Z; m(1,3) =  Y;
    m(2,0) =  Y; m(2,1) =  Z; m(2,2) =  W; m(2,3) = -X;
    m(3,0) =  Z; m(3,1) = -Y; m(3,2) =  X; m(3,3) =  W;
    return m;
  }


  /// get matrix for mult from left (lQ = l*q)
  Matrix left_mult_matrix() const
  {
    Matrix m;
    m(0,0) =  W; m(0,1) = -X; m(0,2) = -Y; m(0,3) = -Z;
    m(1,0) =  X; m(1,1) =  W; m(1,2) =  Z; m(1,3) = -Y;
    m(2,0) =  Y; m(2,1) = -Z; m(2,2) =  W; m(2,3) =  X;
    m(3,0) =  Z; m(3,1) =  Y; m(3,2) = -X; m(3,3) =  W;
    return m;
  }


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
  /// get quaternion from rotation matrix
  template<class MatrixT>
  void init_from_matrix( const MatrixT& _rot)
  {
    Scalar trace = _rot(0,0) + _rot(1,1) + _rot(2,2); 
    if( trace > 0.0 ) 
    {
      Scalar s = 0.5 / sqrt(trace + 1.0);
      W = 0.25 / s;
      X = ( _rot(2,1) - _rot(1,2) ) * s;
      Y = ( _rot(0,2) - _rot(2,0) ) * s;
      Z = ( _rot(1,0) - _rot(0,1) ) * s;
    } 
    else
    {
      if ( _rot(0,0) > _rot(1,1) && _rot(0,0) > _rot(2,2) )
      {
	Scalar s = 2.0 * sqrt( 1.0 + _rot(0,0) - _rot(1,1) - _rot(2,2));
	W = (_rot(2,1) - _rot(1,2) ) / s;
	X = 0.25 * s;
	Y = (_rot(0,1) + _rot(1,0) ) / s;
	Z = (_rot(0,2) + _rot(2,0) ) / s;
      } 
      else 
	if (_rot(1,1) > _rot(2,2))
	{
	  Scalar s = 2.0 * sqrt( 1.0 + _rot(1,1) - _rot(0,0) - _rot(2,2));
	  W = (_rot(0,2) - _rot(2,0) ) / s;
	  X = (_rot(0,1) + _rot(1,0) ) / s;
	  Y = 0.25 * s;
	  Z = (_rot(1,2) + _rot(2,1) ) / s;
	} 
	else
	{
	  Scalar s = 2.0 * sqrt( 1.0 + _rot(2,2) - _rot(0,0) - _rot(1,1) );
	  W = (_rot(1,0) - _rot(0,1) ) / s;
	  X = (_rot(0,2) + _rot(2,0) ) / s;
	  Y = (_rot(1,2) + _rot(2,1) ) / s;
	  Z = 0.25 * s;
	}
    }
  }


  /// quaternion exponential (for unit quaternions)
David Bommes's avatar
 
David Bommes committed
272
  Quaternion exponential() const
273 274 275 276 277 278 279 280 281 282 283
  {
    Vec3   n(X,Y,Z);
    Scalar theta( n.norm());
    Scalar sin_theta = sin(theta);
    Scalar cos_theta = cos(theta);

    if( theta > 1e-6 )
      n *= sin_theta/theta;
    else
      n = Vec3(0,0,0);

David Bommes's avatar
 
David Bommes committed
284
    return Quaternion( cos_theta, n[0], n[1], n[2]);
285 286 287 288
  }


  /// quaternion logarithm (for unit quaternions)
David Bommes's avatar
 
David Bommes committed
289
  Quaternion logarithm() const
290
  {
David Bommes's avatar
David Bommes committed
291 292 293 294
    // clamp to valid input
    double w = W;
    if( w > 1.0) w = 1.0;
    else if( w < -1.0) w = -1.0;
295

David Bommes's avatar
David Bommes committed
296
    Scalar theta_half = acos(w);
297

David Bommes's avatar
David Bommes committed
298 299 300 301 302 303 304
    Vec3   n(X,Y,Z);
    Scalar n_norm( n.norm());
    
    if( n_norm > 1e-6 )
      n *= theta_half/n_norm;
    else
      n = Vec3(0,0,0);
305

David Bommes's avatar
David Bommes committed
306
    return Quaternion( 0, n[0], n[1], n[2]);
307 308
  }

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  void print_info()
  {
    // get axis, angle and matrix
    Vec3 axis; 
    Scalar angle;
    this->axis_angle( axis, angle);
    Matrix m;
    m = this->rotation_matrix();

    std::cerr << "quaternion : " << (*this)      << std::endl;
    std::cerr << "length     : " << this->norm() << std::endl;
    std::cerr << "axis, angle: " << axis << ", " << angle*180.0/M_PI << "\n";
    std::cerr << "rot matrix :\n";
    std::cerr << m << std::endl;
  }

325

Jan Möbius's avatar
 
Jan Möbius committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#undef W
#undef X
#undef Y
#undef Z
};


typedef QuaternionT<float>  Quaternionf;
typedef QuaternionT<double> Quaterniond;


//=============================================================================
} // namespace ACG
//=============================================================================
#endif // ACG_QUATERNION_HH defined
//=============================================================================