Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10555 was 9656, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy bache back with no conflicts

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