Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
quaternion.cpp
Go to the documentation of this file.
1 #include "quaternion.h"
2 
3 Quaternion Quaternion::from_vectors( const Vector &v1, const Vector &v2, const Vector &v3 )
4 {
5  float T = v1.i+v2.j+v3.k+1, S, W, X, Y, Z;
6  if (T > 0) {
7  S = 0.5/sqrtf( T );
8  W = 0.25/S;
9  X = (v3.j-v2.k)*S;
10  Y = (v1.k-v3.i)*S;
11  Z = (v2.i-v1.j)*S;
12  } else {
13  int max = (v1.i > v2.j) ? 1 : 2;
14  if (max == 1)
15  max = (v1.i > v3.k) ? 1 : 3;
16  else
17  max = (v2.j > v3.k) ? 2 : 3;
18  switch (max)
19  {
20  case 1:
21  //column 0
22  S = sqrtf( ( v2.i-(v2.j+v3.k) )+1 );
23  X = S*.5;
24  S = .5/S;
25  W = (v3.j-v2.k)*S;
26  Y = (v2.i+v1.j)*S;
27  Z = (v3.i+v1.k)*S;
28  break;
29  case 2:
30  //column 1
31  S = sqrtf( ( v3.j-(v3.k+v1.i) )+1 );
32  Y = 0.5*S;
33  S = .5/S;
34  W = (v1.k-v3.i);
35  Z = (v3.j+v2.k);
36  X = (v1.j+v2.i);
37  break;
38  case 3:
39  //column 2
40  S = sqrtf( ( v1.k-(v1.i+v2.j) )+1 );
41  Z = 0.5*S;
42  S = .5/S;
43  W = (v2.i-v1.j);
44  X = (v1.k+v3.i);
45  Y = (v2.k+v3.j);
46  break;
47  }
48 #if 0
49  DEPRECATED
50  /*
51  * switch(max) {
52  * case 1:
53  * //column 0
54  * S = QSQRT( (v1.j - (v2.j + v3.k ))+1);
55  * Y = 0.5 * S;
56  * S = .5 / S;
57  * Z = (v1.j + v2.i ) * S;
58  * X = (v1.k + v3.i ) * S;
59  * W = (v2.k - v3.j ) * S;
60  *
61  * break;
62  * case 2:
63  * //column 1
64  * S = QSQRT( v2.k - (v3.k + v1.i )+1);
65  * Y = 0.5 * S;
66  * S = .5 / S;
67  * Z = (v3.j + v2.k ) * S;
68  * X = (v2.i + v1.j ) * S;
69  * W = (v3.i - v1.k ) * S;
70  * break;
71  * case 3:
72  * //column 2
73  * S = QSQRT( v3.i - (v1.i + v2.j )+1);
74  * Z = 0.5 * S;
75  * S = .5 / S;
76  * X = (v1.k + v3.i ) * S;
77  * Y = (v3.j + v2.k ) * S;
78  * W = (v1.j - v2.i ) * S;
79  * break;
80  * }
81  */
82 #endif
83  }
84  return Quaternion( W, Vector( X, Y, Z ) );
85 }
86 
87 Quaternion Quaternion::from_axis_angle( const Vector &axis, float angle )
88 {
89  float sin_a = sin( angle/2 );
90  float cos_a = cos( angle/2 );
91  return Quaternion( cos_a, Vector( axis.i*sin_a,
92  axis.j*sin_a,
93  axis.k*sin_a ) );
94 }
95