Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/quaternion.cc @ 10767

Last change on this file since 10767 was 10695, checked in by snellen, 18 years ago

changes on soundengine and mover by fabian landau

File size: 9.8 KB
RevLine 
[4578]1/*
[2043]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[4578]12   main-programmer: Christian Meyer
[2551]13   co-programmer: Patrick Boenzli : Vector::scale()
14                                    Vector::abs()
[4578]15
[2190]16   Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake
[5420]17
18   2005-06-02: Benjamin Grauer: speed up, and new Functionality to Vector (mostly inline now)
[2043]19*/
20
[3590]21#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
[2043]22
[6616]23#include "quaternion.h"
[5662]24#ifdef DEBUG
[5672]25  #include "debug.h"
[5662]26#else
[5672]27  #include <stdio.h>
28  #define PRINT(x) printf
[5662]29#endif
[2043]30
[4477]31/////////////////
32/* QUATERNIONS */
33/////////////////
[3541]34/**
[7348]35 * @brief calculates a lookAt rotation
[4836]36 * @param dir: the direction you want to look
37 * @param up: specify what direction up should be
[5004]38 *
[7348]39 * Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point
40 * the same way as dir. If you want to use this with cameras, you'll have to reverse the
41 * dir Vector (Vector(0,0,0) - your viewing direction) or you'll point the wrong way. You
42 * can use this for meshes as well (then you do not have to reverse the vector), but keep
43 * in mind that if you do that, the model's front has to point in +z direction, and left
44 * and right should be -x or +x respectively or the mesh wont rotate correctly.
45 *
[5005]46 * @TODO !!! OPTIMIZE THIS !!!
[5420]47 */
[2190]48Quaternion::Quaternion (const Vector& dir, const Vector& up)
[2551]49{
[5004]50  Vector z = dir.getNormalized();
51  Vector x = up.cross(z).getNormalized();
[2190]52  Vector y = z.cross(x);
[4578]53
[2190]54  float m[4][4];
55  m[0][0] = x.x;
56  m[0][1] = x.y;
57  m[0][2] = x.z;
[9110]58  m[0][3] = 0.0;
[2190]59  m[1][0] = y.x;
60  m[1][1] = y.y;
61  m[1][2] = y.z;
[9110]62  m[1][3] = 0.0;
[2190]63  m[2][0] = z.x;
64  m[2][1] = z.y;
65  m[2][2] = z.z;
[9110]66  m[2][3] = 0.0;
67  m[3][0] = 0.0;
68  m[3][1] = 0.0;
69  m[3][2] = 0.0;
70  m[3][3] = 1.0;
[4578]71
[9110]72  this->from4x4(m);
[2190]73}
74
75/**
[7348]76 * @brief calculates a rotation from euler angles
[4836]77 * @param roll: the roll in radians
78 * @param pitch: the pitch in radians
79 * @param yaw: the yaw in radians
[5420]80 */
[10695]81Quaternion::Quaternion (float attitude, float heading, float bank)
[2190]82{
[10695]83/*
[4477]84  float cr, cp, cy, sr, sp, sy, cpcy, spsy;
[4578]85
[4477]86  // calculate trig identities
87  cr = cos(roll/2);
88  cp = cos(pitch/2);
89  cy = cos(yaw/2);
[4578]90
[4477]91  sr = sin(roll/2);
92  sp = sin(pitch/2);
93  sy = sin(yaw/2);
[4578]94
[4477]95  cpcy = cp * cy;
96  spsy = sp * sy;
[4578]97
[4477]98  w = cr * cpcy + sr * spsy;
99  v.x = sr * cpcy - cr * spsy;
100  v.y = cr * sp * cy + sr * cp * sy;
101  v.z = cr * cp * sy - sr * sp * cy;
[10695]102*/
103  float c1, c2, c3, s1, s2, s3;
104
105  c1 = cos(heading / 2);
106  c2 = cos(attitude / 2);
107  c3 = cos(bank / 2);
108
109  s1 = sin(heading / 2);
110  s2 = sin(attitude / 2);
111  s3 = sin(bank / 2);
112
113  w = c1 * c2 * c3 - s1 * s2 * s3;
114  v.x = s1 * s2 * c3 +c1 * c2 * s3;
115  v.y = s1 * c2 * c3 + c1 * s2 * s3;
116  v.z = c1 * s2 * c3 - s1 * c2 * s3;
[2190]117}
118
[10695]119
[2190]120/**
[7348]121 * @brief convert the Quaternion to a 4x4 rotational glMatrix
[4836]122 * @param m: a buffer to store the Matrix in
[5420]123 */
[2190]124void Quaternion::matrix (float m[4][4]) const
125{
[4578]126  float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
127
[2551]128  // calculate coefficients
129  x2 = v.x + v.x;
[4578]130  y2 = v.y + v.y;
[2551]131  z2 = v.z + v.z;
132  xx = v.x * x2; xy = v.x * y2; xz = v.x * z2;
133  yy = v.y * y2; yz = v.y * z2; zz = v.z * z2;
134  wx = w * x2; wy = w * y2; wz = w * z2;
[4578]135
[2551]136  m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz;
137  m[2][0] = xz + wy; m[3][0] = 0.0;
[4578]138
[2551]139  m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz);
140  m[2][1] = yz - wx; m[3][1] = 0.0;
[4578]141
[2551]142  m[0][2] = xz - wy; m[1][2] = yz + wx;
143  m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0;
[4578]144
[2551]145  m[0][3] = 0; m[1][3] = 0;
146  m[2][3] = 0; m[3][3] = 1;
[2190]147}
148
[7191]149
[8724]150
[3449]151/**
[7348]152 * @brief Slerps this QUaternion performs a smooth move.
[7191]153 * @param toQuat to this Quaternion
154 * @param t \% inth the the direction[0..1]
155 */
156void Quaternion::slerpTo(const Quaternion& toQuat, float t)
157{
158  float tol[4];
159  double omega, cosom, sinom, scale0, scale1;
160  //  float DELTA = 0.2;
161
162  cosom = this->v.x * toQuat.v.x + this->v.y * toQuat.v.y + this->v.z * toQuat.v.z + this->w * toQuat.w;
163
164  if( cosom < 0.0 )
165  {
166    cosom = -cosom;
167    tol[0] = -toQuat.v.x;
168    tol[1] = -toQuat.v.y;
169    tol[2] = -toQuat.v.z;
170    tol[3] = -toQuat.w;
171  }
172  else
173  {
174    tol[0] = toQuat.v.x;
175    tol[1] = toQuat.v.y;
176    tol[2] = toQuat.v.z;
177    tol[3] = toQuat.w;
178  }
179
180  omega = acos(cosom);
181  sinom = sin(omega);
182  scale0 = sin((1.0 - t) * omega) / sinom;
183  scale1 = sin(t * omega) / sinom;
184  this->v = Vector(scale0 * this->v.x + scale1 * tol[0],
[7348]185                   scale0 * this->v.y + scale1 * tol[1],
186                   scale0 * this->v.z + scale1 * tol[2]);
[7191]187  this->w = scale0 * this->w + scale1 * tol[3];
188}
189
190
191/**
[7348]192 * @brief performs a smooth move.
[4836]193 * @param from  where
194 * @param to where
195 * @param t the time this transformation should take value [0..1]
196 * @returns the Result of the smooth move
[5420]197 */
[4998]198Quaternion Quaternion::quatSlerp(const Quaternion& from, const Quaternion& to, float t)
[2551]199{
200  float tol[4];
201  double omega, cosom, sinom, scale0, scale1;
[3971]202  //  float DELTA = 0.2;
[2551]203
[3966]204  cosom = from.v.x * to.v.x + from.v.y * to.v.y + from.v.z * to.v.z + from.w * to.w;
[2551]205
[4578]206  if( cosom < 0.0 )
[7348]207  {
208    cosom = -cosom;
209    tol[0] = -to.v.x;
210    tol[1] = -to.v.y;
211    tol[2] = -to.v.z;
212    tol[3] = -to.w;
213  }
[2551]214  else
[7348]215  {
216    tol[0] = to.v.x;
217    tol[1] = to.v.y;
218    tol[2] = to.v.z;
219    tol[3] = to.w;
220  }
[4578]221
[3966]222  omega = acos(cosom);
223  sinom = sin(omega);
224  scale0 = sin((1.0 - t) * omega) / sinom;
225  scale1 = sin(t * omega) / sinom;
[3971]226  return Quaternion(Vector(scale0 * from.v.x + scale1 * tol[0],
[7348]227                           scale0 * from.v.y + scale1 * tol[1],
228                           scale0 * from.v.z + scale1 * tol[2]),
[4578]229                    scale0 * from.w + scale1 * tol[3]);
[2551]230}
231
[7348]232/**
[8731]233 * @returns the Heading
[7348]234 */
235float Quaternion::getHeading() const
236{
237  float pole = this->v.x*this->v.y + this->v.z*this->w;
238  if (fabsf(pole) != 0.5)
239    return atan2(2.0* (v.y*w - v.x*v.z), 1 - 2.0*(v.y*v.y - v.z*v.z));
240  else if (pole == .5) // North Pole
241    return 2.0 * atan2(v.x, w);
242  else // South Pole
243    return -2.0 * atan2(v.x, w);
244}
[2551]245
[2190]246/**
[8731]247 * @returns the Heading-Quaternion
248 */
249Quaternion Quaternion::getHeadingQuat() const
250{
251  return Quaternion(this->getHeading(), Vector(0,1,0));
252}
253
254/**
[7348]255 * @returns the Attitude
256 */
257float Quaternion::getAttitude() const
258{
259  return asin(2.0 * (v.x*v.y + v.z*w));
260}
261
262/**
[8731]263 * @returns the Attitude-Quaternion
264 */
265Quaternion Quaternion::getAttitudeQuat() const
266{
267  return Quaternion(this->getAttitude(), Vector(0,0,1));
268}
269
270
271/**
[7348]272 * @returns the Bank
273 */
274float Quaternion::getBank() const
275{
276  if (fabsf(this->v.x*this->v.y + this->v.z*this->w) != 0.5)
277    return atan2(2.0*(v.x*w-v.y*v.z) , 1 - 2.0*(v.x*v.x - v.z*v.z));
278  else
279    return 0.0f;
280}
281
[8731]282/**
283 * @returns the Bank-Quaternion
284 */
285Quaternion Quaternion::getBankQuat() const
286{
287  return Quaternion(this->getBank(), Vector(1,0,0));
288}
[7348]289
[10695]290/**
291 * @returns Bank, Attitude, Heading
292 */
293Vector Quaternion::getRotation() const
294{
295  return Vector(getAttitude(), getHeading(), getBank());
296}
[8731]297
298
[7348]299/**
300 * @brief convert a rotational 4x4 glMatrix into a Quaternion
[4836]301 * @param m: a 4x4 matrix in glMatrix order
[5420]302 */
[9110]303void Quaternion::from4x4(float m[4][4])
[2190]304{
[4578]305
[2551]306  float  tr, s, q[4];
307  int    i, j, k;
308
[9110]309  static int nxt[3] = {1, 2, 0};
[2551]310
311  tr = m[0][0] + m[1][1] + m[2][2];
312
[7348]313  // check the diagonal
[4578]314  if (tr > 0.0)
[2551]315  {
316    s = sqrt (tr + 1.0);
317    w = s / 2.0;
318    s = 0.5 / s;
319    v.x = (m[1][2] - m[2][1]) * s;
320    v.y = (m[2][0] - m[0][2]) * s;
321    v.z = (m[0][1] - m[1][0]) * s;
[7348]322  }
323  else
324  {
325    // diagonal is negative
326    i = 0;
327    if (m[1][1] > m[0][0]) i = 1;
[2551]328    if (m[2][2] > m[i][i]) i = 2;
329    j = nxt[i];
330    k = nxt[j];
331
332    s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0);
[4578]333
[2551]334    q[i] = s * 0.5;
[4578]335
[2551]336    if (s != 0.0) s = 0.5 / s;
[4578]337
[7348]338    q[3] = (m[j][k] - m[k][j]) * s;
[2551]339    q[j] = (m[i][j] + m[j][i]) * s;
340    q[k] = (m[i][k] + m[k][i]) * s;
341
[7348]342    v.x = q[0];
343    v.y = q[1];
344    v.z = q[2];
345    w = q[3];
[2190]346  }
347}
348
[8724]349
[2190]350/**
[9110]351 * applies a quaternion from a 3x3 rotation matrix.
[8724]352 * @param mat The 3x3 source rotation matrix.
353 * @return The equivalent 4 float quaternion.
354 */
[9110]355void Quaternion::from3x3(float mat[3][3])
[8724]356{
357  int   NXT[] = {1, 2, 0};
358  float q[4];
359
360  // check the diagonal
361  float tr = mat[0][0] + mat[1][1] + mat[2][2];
[9656]362  if( tr > 0.0f)
363  {
[8724]364    float s = (float)sqrtf(tr + 1.0f);
365    this->w = s * 0.5f;
366    s = 0.5f / s;
367    this->v.x = (mat[1][2] - mat[2][1]) * s;
368    this->v.y = (mat[2][0] - mat[0][2]) * s;
369    this->v.z = (mat[0][1] - mat[1][0]) * s;
370  }
371  else
372  {
373    // diagonal is negative
374    // get biggest diagonal element
375    int i = 0;
376    if (mat[1][1] > mat[0][0]) i = 1;
377    if (mat[2][2] > mat[i][i]) i = 2;
378    //setup index sequence
379    int j = NXT[i];
380    int k = NXT[j];
381
382    float s = (float)sqrtf((mat[i][i] - (mat[j][j] + mat[k][k])) + 1.0f);
383
384    q[i] = s * 0.5f;
385
386    if (s != 0.0f) s = 0.5f / s;
387
388    q[j] = (mat[i][j] + mat[j][i]) * s;
389    q[k] = (mat[i][k] + mat[k][i]) * s;
390    q[3] = (mat[j][k] - mat[k][j]) * s;
391
392    this->v.x = q[0];
393    this->v.y = q[1];
394    this->v.z = q[2];
395    this->w   = q[3];
396  }
397}
398
[9656]399Quaternion Quaternion::lookAt(Vector from, Vector to, Vector up)
400{
401  Vector n = to - from;
402  n.normalize();
403  Vector v = n.cross(up);
404  v.normalize();
405  Vector u = v.cross(n);
406
407  float matrix[3][3];
408  matrix[0][0] = v.x;
409  matrix[0][1] = v.y;
410  matrix[0][2] = v.z;
411  matrix[1][0] = u.x;
412  matrix[1][1] = u.y;
413  matrix[1][2] = u.z;
414  matrix[2][0] = -n.x;
415  matrix[2][1] = -n.y;
416  matrix[2][2] = -n.z;
417
418  Quaternion quat;
419  quat.from3x3(matrix);
420  return quat;
421}
422
423
[8724]424/**
[7348]425 * @brief outputs some nice formated debug information about this quaternion
[3541]426*/
[7003]427void Quaternion::debug() const
[3541]428{
429  PRINT(0)("real a=%f; imag: x=%f y=%f z=%f\n", w, v.x, v.y, v.z);
430}
431
[7348]432/**
433 * @brief another better Quaternion Debug Function.
434 */
[7003]435void Quaternion::debug2() const
[5000]436{
437  Vector axis = this->getSpacialAxis();
438  PRINT(0)("angle = %f, axis: ax=%f, ay=%f, az=%f\n", this->getSpacialAxisAngle(), axis.x, axis.y, axis.z );
439}
[8724]440
441
442
443
444
445
Note: See TracBrowser for help on using the repository browser.