34 SLQuat4 (
const T xRotRAD,
const T yRotRAD,
const T zRotRAD);
36 T
x ()
const {
return _x; }
37 T
y ()
const {
return _y; }
38 T
z ()
const {
return _z; }
39 T
w ()
const {
return _w; }
44 T axisX, T axisY, T axisZ);
121 fromAngleAxis(angleDEG *
DEG2RAD, axis.
x, axis.
y, axis.
z);
135 fromEulerAngles(xRotRAD, yRotRAD, zRotRAD);
154 const int next[3] = {1, 2, 0};
162 root = sqrt(trace + (T)1);
164 root = ((T)0.5)/root;
165 _x = (m(2,1) - m(1,2)) * root;
166 _y = (m(0,2) - m(2,0)) * root;
167 _z = (m(1,0) - m(0,1)) * root;
173 if (m(1,1) > m(0,0)) i = 1;
174 if (m(2,2) > m(i,i)) i = 2;
178 root = sqrt(m(i,i) - m(j,j) - m(k,k) + (T)1);
179 T* quat[3] = { &_x, &_y, &_z };
180 *quat[i] = ((T)0.5)*root;
181 root = ((T)0.5)/root;
182 _w = (m(k,j) - m(j,k))*root;
183 *quat[j] = (m(j,i) + m(i,j))*root;
184 *quat[k] = (m(k,i) + m(i,k))*root;
196 fromAngleAxis(
PI, 1, 0, 0);
203 T
s = sqrt((1 + d) * (T)2);
218 vector2.
cross(up, vector);
221 vector3.
cross(vector, vector2);
223 float m00 = vector2.
x;
224 float m01 = vector2.
y;
225 float m02 = vector2.
z;
226 float m10 = vector3.
x;
227 float m11 = vector3.
y;
228 float m12 = vector3.
z;
229 float m20 = vector.
x;
230 float m21 = vector.
y;
231 float m22 = vector.
z;
233 float num8 = (m00 + m11) + m22;
237 float num = (float)sqrt(num8 + 1.0f);
238 quaternion.
_w = num * 0.5f;
240 quaternion.
_x = (m12 - m21) * num;
241 quaternion.
_y = (m20 - m02) * num;
242 quaternion.
_z = (m01 - m10) * num;
244 else if ((m00 >= m11) && (m00 >= m22))
246 float num7 = (float)sqrt(((1.0f + m00) - m11) - m22);
247 float num4 = 0.5f / num7;
248 quaternion.
_x = 0.5f * num7;
249 quaternion.
_y = (m01 + m10) * num4;
250 quaternion.
_z = (m02 + m20) * num4;
251 quaternion.
_w = (m12 - m21) * num4;
255 float num6 = (float)sqrt(((1.0f + m11) - m00) - m22);
256 float num3 = 0.5f / num6;
257 quaternion.
_x = (m10+ m01) * num3;
258 quaternion.
_y = 0.5f * num6;
259 quaternion.
_z = (m21 + m12) * num3;
260 quaternion.
_w = (m20 - m02) * num3;
264 float num5 = (float)sqrt(((1.0f + m22) - m00) - m11);
265 float num2 = 0.5f / num5;
266 quaternion.
_x = (m20 + m02) * num2;
267 quaternion.
_y = (m21 + m12) * num2;
268 quaternion.
_z = 0.5f * num5;
269 quaternion.
_w = (m01 - m10) * num2;
276 const T axisX,
const T axisY,
const T axisZ)
278 _w = (T)cos(angleRAD * (T)0.5);
279 _x = _y = _z = (T)sin(angleRAD * (T)0.5);
299 T x = xRotRAD * (T)0.5;
300 T y = yRotRAD * (T)0.5;
301 T z = zRotRAD * (T)0.5;
310 _x = Sx*Cy*Cz + Cx*Sy*Sz;
311 _y = Cx*Sy*Cz - Sx*Cy*Sz;
312 _z = Cx*Cy*Sz + Sx*Sy*Cx;
313 _w = Cx*Cy*Cz - Sx*Sy*Sz;
324 T wx2 = _w * x2; T wy2 = _w * y2; T wz2 = _w * z2;
325 T xx2 = _x * x2; T xy2 = _x * y2; T xz2 = _x * z2;
326 T yy2 = _y * y2; T yz2 = _y * z2; T zz2 = _z * z2;
328 SLMat3<T> m(1 -(yy2 + zz2), xy2 - wz2, xz2 + wy2,
329 xy2 + wz2, 1 -(xx2 + zz2), yz2 - wx2,
330 xz2 - wy2, yz2 + wx2, 1 -(xx2 + yy2));
343 T wx2 = _w * x2; T wy2 = _w * y2; T wz2 = _w * z2;
344 T xx2 = _x * x2; T xy2 = _x * y2; T xz2 = _x * z2;
345 T yy2 = _y * y2; T yz2 = _y * z2; T zz2 = _z * z2;
347 SLMat4<T> m(1 -(yy2 + zz2), xy2 - wz2, xz2 + wy2, 0,
348 xy2 + wz2, 1 -(xx2 + zz2), yz2 - wx2, 0,
349 xz2 - wy2, yz2 + wx2, 1 -(xx2 + yy2), 0,
362 template <
typename T>
368 T sqrLen = _x*_x + _y*_y + _z*_z;
370 if (sqrLen > FLT_EPSILON)
372 angleDEG = ((T)2) * acos(_w) *
RAD2DEG;
373 T len = sqrt(sqrLen);
388 template <
typename T>
391 double sinz = +2.0 * (_w * _z + _x * _y);
392 double cosz = +1.0 - 2.0 * (_y *_y + _z *_z);
393 zRotRAD = (T)atan2(sinz, cosz);
395 double siny = +2.0 * (_w *_y - _z *_x);
397 yRotRAD = (T)copysign(
PI / 2, siny);
399 yRotRAD = (T)asin(siny);
401 double sinx = +2.0 * (_w * _x + _y * _z);
402 double cosx = +1.0 - 2.0 * (_x * _x + _y * _y);
403 xRotRAD = (T)atan2(sinx, cosx);
406 template <
typename T>
409 double sinx = -(T)2 * (_z * _y - _w * _x);
410 double cosx = +1.0 - (T)2 * (_x * _x + _y * _y);
411 xRotRAD = (T)atan2(sinx, cosx);
413 double siny = (T)2 * (_w * _y + _x * _z);
415 yRotRAD = (T)copysign(
PI / 2, siny);
417 yRotRAD = (T)asin(siny);
419 double sinz = -(T)2 * (_x * _y - _w * _z);
420 double cosz = +1.0 - (T)2 * (_y *_y + _z *_z);
421 zRotRAD = (T)atan2(sinz, cosz);
424 template <
typename T>
427 double sinz = -(T)2 * (_x * _y - _w * _z);
428 double cosz = 1 - (T)2 * (_x * _x + _z * _z);
429 zRotRAD = (T)atan2(sinz, cosz);
431 double sinx = (T)2 * (_y * _z + _w * _x);
433 xRotRAD = (T)copysign(
PI / 2, sinx);
435 xRotRAD = (T)asin(sinx);
437 double siny = -(T)2 * (_x * _z - _w * _y);
438 double cosy = 1 - (T)2 * (_x * _x + _y * _y);
439 yRotRAD = (T)atan2(siny, cosy);
486 uv = qvec.crossProduct(v);
487 uuv = qvec.crossProduct(uv);
498 return _x == q.
_x && _y == q.
_y && _z == q.
_z && _w == q.
_w;
505 return !(*
this == q);
538 return _x * q.
_x + _y * q.
_y + _z * q.
_z + _w * q.
_w;
545 return sqrt(_x*_x + _y*_y + _z*_z + _w*_w);
555 if (len > FLT_EPSILON)
557 T invLen = ((T)1)/len;
558 norm.
_x = _x * invLen;
559 norm.
_y = _y * invLen;
560 norm.
_z = _z * invLen;
561 norm.
_w = _w * invLen;
579 if (len > FLT_EPSILON)
581 T invLen = ((T)1)/len;
603 T norm = _x*_x + _y*_y + _z*_z + _w*_w;
608 T invNorm = ((T)1) / norm;
609 inverse.
_x = -_x * invNorm;
610 inverse.
_y = -_y * invNorm;
611 inverse.
_z = -_z * invNorm;
612 inverse.
_w = _w * invNorm;
629 T norm = _x*_x + _y*_y + _z*_z + _w*_w;
634 T invNorm = ((T)1) / norm;
654 return SLQuat4(-_x, -_y, -_z, _w);
750 T cosom = _x * q2.
_x + _y * q2.
_y + _z * q2.
_z + _w * q2.
_w;
754 if( cosom <
static_cast<T
>(0.0))
757 endCpy.
_x = -endCpy.
_x;
758 endCpy.
_y = -endCpy.
_y;
759 endCpy.
_z = -endCpy.
_z;
760 endCpy.
_w = -endCpy.
_w;
765 if( (
static_cast<T
>(1.0) - cosom) >
static_cast<T
>(0.0001))
769 omega = acos( cosom);
771 sclp = sin( (
static_cast<T
>(1.0) - factor) * omega) / sinom;
772 sclq = sin( factor * omega) / sinom;
776 sclp =
static_cast<T
>(1.0) - factor;
781 out.
_x = sclp * _x + sclq * endCpy.
_x;
782 out.
_y = sclp * _y + sclq * endCpy.
_y;
783 out.
_z = sclp * _z + sclq * endCpy.
_z;
784 out.
_w = sclp * _w + sclq * endCpy.
_w;
821 assert(t>=0 && t<=1 &&
"Wrong t in SLQuat4::slerp");
823 T cosAngle = q1.
dot(q2);
825 if (cosAngle > 1 - FLT_EPSILON)
827 *
this = q2 + (q1 - q2).scaled(t);
832 if (cosAngle < 0) cosAngle = 0;
833 if (cosAngle > 1) cosAngle = 1;
835 T theta0 = acos(cosAngle);
836 T theta = theta0 * t;
SLQuat4< SLfloat > SLQuat4f
3x3 matrix template class
4x4 matrix template class
Quaternion class for angle-axis rotation representation.
SLQuat4< T > inverted() const
SLbool operator==(const SLQuat4< T > &q) const
void fromEulerAngles(const T xRotRAD, const T yRotRAD, const T zRotRAD)
SLQuat4< T > operator*(const SLQuat4< T > &q) const
SLQuat4< T > operator+(const SLQuat4< T > &q) const
SLQuat4< T > rotated(const SLQuat4< T > &b) const
SLQuat4< T > scaled(T scale) const
void toEulerAnglesZYX(T &zRotRAD, T &yRotRAD, T &xRotRAD) const
void fromAngleAxis(T angleRAD, T axisX, T axisY, T axisZ)
void lerp(const SLQuat4< T > &q1, const SLQuat4< T > &q2, T t)
Linear interpolation.
void set(T x, T y, T z, T w)
SLQuat4< T > slerp(const SLQuat4< T > &q2, T t) const
SLQuat4< T > & operator*=(const SLQuat4< T > &q2)
SLQuat4< T > conjugated() const
void toEulerAnglesZXY(T &zRotRAD, T &xRotRAD, T &yRotRAD) const
SLMat3< T > toMat3() const
SLQuat4(const SLMat3< T > &m)
SLQuat4< T > lerp(const SLQuat4< T > &q2, T t) const
Linear interpolation.
SLQuat4(T angleDEG, const SLVec3< T > &axis)
SLQuat4(const T xRotRAD, const T yRotRAD, const T zRotRAD)
void rotate(const SLQuat4< T > &q)
SLbool operator!=(const SLQuat4< T > &q) const
SLQuat4< T > operator-(const SLQuat4< T > &q) const
void fromMat3(const SLMat3< T > &m)
SLQuat4(const SLVec3< T > &v0, const SLVec3< T > &v1)
SLQuat4(T x, T y, T z, T w)
void slerp(const SLQuat4< T > &q1, const SLQuat4< T > &q2, T t)
Spherical linear interpolation.
void toAngleAxis(T &angleDEG, SLVec3< T > &axis) const
T dot(const SLQuat4< T > &q) const
static SLQuat4< T > fromLookRotation(const SLVec3< T > &forward, const SLVec3< T > &up)
SLVec3< T > rotate(const SLVec3< T > &vec) const
SLQuat4< T > normalized() const
SLVec4< T > toVec4() const
SLQuat4< T > & operator=(SLQuat4< T > q)
void toEulerAnglesXYZ(T &xRotRAD, T &yRotRAD, T &zRotRAD) const
void fromVec3(const SLVec3< T > &v0, const SLVec3< T > &v1)
SLMat4< T > toMat4() const
The SLScene class represents the top level instance holding the scene structure.
3D vector template class for standard 3D vector algebra.
void cross(const SLVec3 &a, const SLVec3 &b)
T dot(const SLVec3 &v) const
4D vector template class for standard 4D vector algebra.
static const float DEG2RAD
static const float RAD2DEG