QuaternionT.hh 11.1 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
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
Jan Möbius's avatar
Jan Möbius committed
8
9
 *                                                                           *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
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
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
42
43
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
44
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
45
46
47
48
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
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



//=============================================================================
//
//  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:

85
86
87
88
#define W Base::data()[0]
#define X Base::data()[1]
#define Y Base::data()[2]
#define Z Base::data()[3]
Jan Möbius's avatar
 
Jan Möbius committed
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


  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];
  }

David Bommes's avatar
David Bommes committed
122
123
124
125
  /// construct from rotation matrix (only valid for rotation matrices!)
  template <class MatrixT>
  QuaternionT(const MatrixT& _rot)
  { init_from_matrix( _rot); }
126
  
Jan Möbius's avatar
 
Jan Möbius committed
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

  /// 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
156
157
  template <class Vec3T>
  Vec3T rotate(const Vec3T& _v) const
Jan Möbius's avatar
Jan Möbius committed
158
  { 
159
160
    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
161
  }
Jan Möbius's avatar
 
Jan Möbius committed
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


  /// 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;
  }



210
  /*
Jan Möbius's avatar
 
Jan Möbius committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  /// 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;
  }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  */
  /// get matrix for mult from right (p*q = Qp)
  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 (q*p = Qp)
  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;
  }
Jan Möbius's avatar
 
Jan Möbius committed
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
  /// 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
303
  Quaternion exponential() const
304
305
306
307
308
309
310
311
312
313
314
  {
    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
315
    return Quaternion( cos_theta, n[0], n[1], n[2]);
316
317
318
319
  }


  /// quaternion logarithm (for unit quaternions)
David Bommes's avatar
   
David Bommes committed
320
  Quaternion logarithm() const
321
  {
David Bommes's avatar
David Bommes committed
322
323
324
325
    // clamp to valid input
    double w = W;
    if( w > 1.0) w = 1.0;
    else if( w < -1.0) w = -1.0;
326

David Bommes's avatar
David Bommes committed
327
    Scalar theta_half = acos(w);
328

David Bommes's avatar
David Bommes committed
329
330
331
332
333
334
335
    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);
336

David Bommes's avatar
David Bommes committed
337
    return Quaternion( 0, n[0], n[1], n[2]);
338
339
  }

340
341
342
343
344
345
346
347
348
349
350
  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;
351
    std::cerr << "axis, angle: " << axis << ", " << angle*180.0/M_PI << "\n";
352
353
354
355
    std::cerr << "rot matrix :\n";
    std::cerr << m << std::endl;
  }

356

Jan Möbius's avatar
 
Jan Möbius committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#undef W
#undef X
#undef Y
#undef Z
};


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


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