Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
Quaternion.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__QUATERNION_H
13#define TT_INCLUDE__QUATERNION_H
14
15
16
17class Vector2;
18class Vector3;
19class Matrix3;
20class Matrix3D;
21class Matrix4;
22#include "Vector3.h"
23
24
25class Quaternion
26{
27
28public:
29
30 static const Quaternion IDENTITY;
31
32 union
33 {
34 struct
35 {
36 float X;
37 float Y;
38 float Z;
39 float W;
40 };
41 struct
42 {
43 float i;
44 float j;
45 float k;
46 float t;
47 };
48 };
49
50
51 TT_INLINE Quaternion(void) {};
52 TT_INLINE explicit Quaternion(bool init) { if (init) { X = 0.0f; Y = 0.0f; Z = 0.0f; W = 1.0f; } }
53 TT_INLINE explicit Quaternion(float a, float b, float c, float d) { X=a; Y=b; Z=c; W=d; }
54 TT_INLINE Quaternion(float angle, const Vector3& axis);
55 void Normalize();
56 float & operator [](int i) { return (&X)[i]; }
57 const float & operator [](int i) const { return (&X)[i]; }
58
59 Quaternion conjugate() { return Quaternion(t, -i, -j, -k); }
60
61 Matrix3 Build_Matrix3();
62 Matrix3D Build_Matrix3D();
63 Matrix4 Build_Matrix4();
64
65 static Quaternion Trackball(float x0, float y0, float x1, float y1, float sphsize);
66 void Scale(float s) { X = (float)(s*X); Y = (float)(s*Y); Z = (float)(s*Z); W = (float)(s*W); }
67
68 friend TT_INLINE Quaternion operator* (const Vector3& a, const Quaternion& b);
69 friend TT_INLINE Quaternion operator* (const Quaternion& a, const Quaternion& b);
70 bool Is_Valid(void) const;
71 TT_INLINE void Make_Identity(void) { Set(); };
72 TT_INLINE void Set(float a = 0.0, float b = 0.0, float c = 0.0, float d = 1.0) { X = a; Y = b; Z = c; W = d; }
73 TT_INLINE Vector3 Rotate_Vector(const Vector3 & v) const;
74};
75
76
77
78TT_INLINE Quaternion operator * (const Vector3& a, const Quaternion& b)
79{
80 return Quaternion(a.X, a.Y, a.Z, 0) * b;
81}
82
83
84
85TT_INLINE Quaternion operator * (const Quaternion& a, const Quaternion& b)
86{
87 // Rv = Ar*Bv + Br*Av + Av cross Bv
88 // Rr = Ar*Br + Av dot Bv
89
90 return Quaternion
91 (
92 a.W*b.X + b.W*a.X + (a.Y*b.Z - a.Z*b.Y),
93 a.W*b.Y + b.W*a.Y + (a.Z*b.X - a.X*b.Z),
94 a.W*b.Z + b.W*a.Z + (a.X*b.Y - a.Y*b.X),
95 a.W*b.W - (a.X*b.X + a.Y*b.Y + a.Z*b.Z)
96 );
97}
98
99
100TT_INLINE bool Quaternion::Is_Valid(void) const
101{
102 return ( Is_Valid_Float(X) &&
103 Is_Valid_Float(Y) &&
104 Is_Valid_Float(Z) &&
105 Is_Valid_Float(W) );
106}
107
108TT_INLINE Vector3 Quaternion::Rotate_Vector(const Vector3 & v) const
109{
110 float x = W*v.X + (Y*v.Z - v.Y*Z);
111 float y = W*v.Y - (X*v.Z - v.X*Z);
112 float z = W*v.Z + (X*v.Y - v.X*Y);
113 float w = -(X*v.X + Y*v.Y + Z*v.Z);
114 return Vector3
115 (
116 w*(-X) + W*x + (y*(-Z) - (-Y)*z),
117 w*(-Y) + W*y - (x*(-Z) - (-X)*z),
118 w*(-Z) + W*z + (x*(-Y) - (-X)*y)
119 );
120}
121
122TT_INLINE Quaternion Inverse(const Quaternion & a)
123{
124 return Quaternion(-a[0],-a[1],-a[2],a[3]);
125}
126Quaternion Build_Quaternion(const Matrix3D & mat);
127
128TT_INLINE Quaternion operator / (const Quaternion & a,const Quaternion & b)
129{
130 return a * Inverse(b);
131}
132
133TT_INLINE Quaternion operator * (float scl, const Quaternion & a)
134{
135 return Quaternion(scl*a[0], scl*a[1], scl*a[2], scl*a[3]);
136}
137
138TT_INLINE Quaternion operator * (const Quaternion & a, float scl)
139{
140 return scl*a;
141}
142
143struct SlerpInfoStruct
144{
145 float SinT;
146 float Theta;
147 bool Flip;
148 bool Linear;
149};
150void Slerp_Setup(const Quaternion & p,const Quaternion & q,SlerpInfoStruct * slerpinfo);
151void Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo,Quaternion * set_q);
152Quaternion Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo);
153void __cdecl Fast_Slerp(Quaternion& result, const Quaternion & a,const Quaternion & b,float t);
154Matrix3 Build_Matrix3(const Quaternion & quat);
155#endif