11 #ifndef CAL_QUATERNION_H
12 #define CAL_QUATERNION_H
18 #include "cal3d/global.h"
19 #include "cal3d/vector.h"
46 inline CalQuaternion() : x(0.0f), y(0.0f), z(0.0f), w(1.0f){};
48 inline CalQuaternion(
float qx,
float qy,
float qz,
float qw): x(qx), y(qy), z(qz), w(qw) {};
53 inline float& operator[](
unsigned int index)
58 inline const float& operator[](
unsigned int index)
const
79 x = qw * q.x + qx * q.w + qy * q.z - qz * q.y;
80 y = qw * q.y - qx * q.z + qy * q.w + qz * q.x;
81 z = qw * q.z + qx * q.y - qy * q.x + qz * q.w;
82 w = qw * q.w - qx * q.x - qy * q.y - qz * q.z;
85 inline void operator*=(
const CalVector& v)
93 x = qw * v.x + qy * v.z - qz * v.y;
94 y = qw * v.y - qx * v.z + qz * v.x;
95 z = qw * v.z + qx * v.y - qy * v.x;
96 w = - qx * v.x - qy * v.y - qz * v.z;
109 return !operator==(rhs);
125 norm = x * q.x + y * q.y + z * q.z + w * q.w;
137 if(1.0f - norm < 0.000001f)
144 theta = (float) acos(norm);
147 s = (float) (1.0f / sin(theta));
149 inv_d = (float) sin((1.0f - d) * theta) * s;
150 d = (float) sin(d * theta) * s;
158 x = inv_d * x + d * q.x;
159 y = inv_d * y + d * q.y;
160 z = inv_d * z + d * q.z;
161 w = inv_d * w + d * q.w;
171 inline void conjugate()
181 const float norm = (x*x) + (y*y) + (z*z) + (w*w);
183 if (norm == 0.0f)
return;
185 const float inv_norm = 1 / norm;
192 inline void set(
float qx,
float qy,
float qz,
float qw)
222 r.w * q.x + r.x * q.w + r.y * q.z - r.z * q.y,
223 r.w * q.y - r.x * q.z + r.y * q.w + r.z * q.x,
224 r.w * q.z + r.x * q.y - r.y * q.x + r.z * q.w,
225 r.w * q.w - r.x * q.x - r.y * q.y - r.z * q.z
232 float dot = from * to ;
234 dot = (float) sqrt( 2*(dot+1) ) ;
240 return CalQuaternion( cross[0], cross[1], cross[2], -dot/2 ) ;
The vector class.
Definition: vector.h:36
The quaternion class.
Definition: quaternion.h:35