- Cal3D 0.11 API Reference -

quaternion.h
1 //****************************************************************************//
2 // quaternion.h //
3 // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger //
4 //****************************************************************************//
5 // This library is free software; you can redistribute it and/or modify it //
6 // under the terms of the GNU Lesser General Public License as published by //
7 // the Free Software Foundation; either version 2.1 of the License, or (at //
8 // your option) any later version. //
9 //****************************************************************************//
10 
11 #ifndef CAL_QUATERNION_H
12 #define CAL_QUATERNION_H
13 
14 //****************************************************************************//
15 // Includes //
16 //****************************************************************************//
17 
18 #include "cal3d/global.h"
19 #include "cal3d/vector.h"
20 
21 //****************************************************************************//
22 // Forward declarations //
23 //****************************************************************************//
24 
25 //class CalVector;
26 
27 //****************************************************************************//
28 // Class declaration //
29 //****************************************************************************//
30 
31  /*****************************************************************************/
35 class CAL3D_API CalQuaternion
36 {
37  // member variables
38 public:
39  float x;
40  float y;
41  float z;
42  float w;
43 
44  // constructors/destructor
45 public:
46  inline CalQuaternion() : x(0.0f), y(0.0f), z(0.0f), w(1.0f){};
47  inline CalQuaternion(const CalQuaternion& q): x(q.x), y(q.y), z(q.z), w(q.w) {};
48  inline CalQuaternion(float qx, float qy, float qz, float qw): x(qx), y(qy), z(qz), w(qw) {};
49  inline ~CalQuaternion() {};
50 
51  // member functions
52 public:
53  inline float& operator[](unsigned int index)
54  {
55  return (&x)[index];
56  }
57 
58  inline const float& operator[](unsigned int index) const
59  {
60  return (&x)[index];
61  }
62 
63  inline void operator=(const CalQuaternion& q)
64  {
65  x = q.x;
66  y = q.y;
67  z = q.z;
68  w = q.w;
69  }
70 
71  inline void operator*=(const CalQuaternion& q)
72  {
73  float mx( x ), my( y ), mz( z ), mw( w );
74  float qx(q.x), qy(q.y), qz(q.z), qw(q.w);
75 
76  x = mw * qx + mx * qw + my * qz - mz * qy;
77  y = mw * qy - mx * qz + my * qw + mz * qx;
78  z = mw * qz + mx * qy - my * qx + mz * qw;
79  w = mw * qw - mx * qx - my * qy - mz * qz;
80  }
81 
82  inline void operator+=(const CalQuaternion& q)
83  {
84  x += q.x;
85  y += q.y;
86  z += q.z;
87  w += q.w;
88  }
89 
90  inline void operator*=(const CalVector& v)
91  {
92  float qx, qy, qz, qw;
93  qx = x;
94  qy = y;
95  qz = z;
96  qw = w;
97 
98  x = qw * v.x + qy * v.z - qz * v.y;
99  y = qw * v.y - qx * v.z + qz * v.x;
100  z = qw * v.z + qx * v.y - qy * v.x;
101  w = - qx * v.x - qy * v.y - qz * v.z;
102  }
103 
104  inline void operator*=( float s )
105  {
106  x *= s;
107  y *= s;
108  z *= s;
109  w *= s;
110  }
111 
112  inline bool operator==(const CalQuaternion& rhs) const
113  {
114  return x == rhs.x &&
115  y == rhs.y &&
116  z == rhs.z &&
117  w == rhs.w;
118  }
119 
120  inline bool operator!=(const CalQuaternion& rhs) const
121  {
122  return !operator==(rhs);
123  }
124 /*
125  static inline CalQuaternion operator*(const CalQuaternion& q, const CalQuaternion& r)
126  {
127  return CalQuaternion(
128  r.w * q.x + r.x * q.w + r.y * q.z - r.z * q.y,
129  r.w * q.y - r.x * q.z + r.y * q.w + r.z * q.x,
130  r.w * q.z + r.x * q.y - r.y * q.x + r.z * q.w,
131  r.w * q.w - r.x * q.x - r.y * q.y - r.z * q.z
132  );
133  }
134 */
135  inline void blend(float d, const CalQuaternion& q)
136  {
137  float norm;
138  norm = x * q.x + y * q.y + z * q.z + w * q.w;
139 
140  bool bFlip;
141  bFlip = false;
142 
143  if(norm < 0.0f)
144  {
145  norm = -norm;
146  bFlip = true;
147  }
148 
149  float inv_d;
150  if(1.0f - norm < 0.000001f)
151  {
152  inv_d = 1.0f - d;
153  }
154  else
155  {
156  float theta;
157  theta = (float) acos(norm);
158 
159  float s;
160  s = (float) (1.0f / sin(theta));
161 
162  inv_d = (float) sin((1.0f - d) * theta) * s;
163  d = (float) sin(d * theta) * s;
164  }
165 
166  if(bFlip)
167  {
168  d = -d;
169  }
170 
171  x = inv_d * x + d * q.x;
172  y = inv_d * y + d * q.y;
173  z = inv_d * z + d * q.z;
174  w = inv_d * w + d * q.w;
175  }
176 
177  inline void clear()
178  {
179  x = 0.0f;
180  y = 0.0f;
181  z = 0.0f;
182  w = 1.0f;
183  }
184  inline void conjugate()
185  {
186  x = -x;
187  y = -y;
188  z = -z;
189  }
190 
191  inline void invert()
192  {
193  conjugate();
194  const float norm = (x*x) + (y*y) + (z*z) + (w*w);
195 
196  if (norm == 0.0f) return;
197 
198  const float inv_norm = 1 / norm;
199  *this *= inv_norm;
200  }
201 
202  inline void set(float qx, float qy, float qz, float qw)
203  {
204  x = qx;
205  y = qy;
206  z = qz;
207  w = qw;
208  }
209 
210  void compress(short &s0, short &s1, short &s2);
211  void decompress(short &s0, short &s1, short &s2);
212 
213 /*
214  static inline CalQuaternion shortestArc( const CalVector& from, const CalVector& to )
215  {
216  CalVector cross = from % to; //Compute vector cross product
217  float dot = from * to ; //Compute dot product
218 
219  dot = (float) sqrt( 2*(dot+1) ) ; //We will use this equation twice
220 
221  cross /= dot ; //Get the x, y, z components
222 
223  //Return with the w component (Note that w is inverted because Cal3D has
224  // left-handed rotations )
225  return CalQuaternion( cross[0], cross[1], cross[2], -dot/2 ) ;
226 
227  }
228 
229  */
230 };
231 
232 
233 static inline CalQuaternion operator*(const CalQuaternion& q, const CalQuaternion& r)
234 {
235  return CalQuaternion(
236  r.w * q.x + r.x * q.w + r.y * q.z - r.z * q.y,
237  r.w * q.y - r.x * q.z + r.y * q.w + r.z * q.x,
238  r.w * q.z + r.x * q.y - r.y * q.x + r.z * q.w,
239  r.w * q.w - r.x * q.x - r.y * q.y - r.z * q.z
240  );
241 }
242 
243 static inline float dot( const CalQuaternion& q, const CalQuaternion& r )
244 {
245  return q.x * r.x +
246  q.y * r.y +
247  q.z * r.z +
248  q.w * r.w;
249 }
250 
251 
252 static inline CalQuaternion shortestArc( const CalVector& from, const CalVector& to )
253 {
254  CalVector cross = from % to; //Compute vector cross product
255  float dot = from * to ; //Compute dot product
256 
257  dot = (float) sqrt( 2*(dot+1) ) ; //We will use this equation twice
258 
259  cross /= dot ; //Get the x, y, z components
260 
261  //Return with the w component (Note that w is inverted because Cal3D has
262  // left-handed rotations )
263  return CalQuaternion( cross[0], cross[1], cross[2], -dot/2 ) ;
264 
265 }
266 
267 
268 #endif
269 
270 //****************************************************************************//
The vector class.
Definition: vector.h:36
The quaternion class.
Definition: quaternion.h:35

Generated by The Cal3D Team with Doxygen 1.8.14