-
Notifications
You must be signed in to change notification settings - Fork 60
/
cyQuat.h
160 lines (136 loc) · 6.77 KB
/
cyQuat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// cyCodeBase by Cem Yuksel
// [www.cemyuksel.com]
//-------------------------------------------------------------------------------
//! \file cyQuat.h
//! \author Cem Yuksel
//!
//! \brief Quaternion class
//!
//! This file includes a quaternion class that can be used to rotate 3D vectors.
//! It works with Vec3, Matrix3, and Matrix4 classes.
//! Special thanks to Can Yuksel for his contributions in writing this class.
//!
//-------------------------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//-------------------------------------------------------------------------------
#ifndef _CY_QUAT_H_INCLUDED_
#define _CY_QUAT_H_INCLUDED_
//-------------------------------------------------------------------------------
#include "cyVector.h"
#include "cyMatrix.h"
//-------------------------------------------------------------------------------
namespace cy {
//-------------------------------------------------------------------------------
//! Quaternion class
template <typename T>
class Quat
{
friend Quat operator*( T f, Quat const &q ) { return Quat( q.s*f, q.v*f); }
public:
T s; //!< scaler part
Vec3<T> v; //!< vector part
//!@name Constructors
Quat() CY_CLASS_FUNCTION_DEFAULT
Quat( T _s, Vec3<T> const &_v ) : s(_s), v(_v) {}
Quat( T _s, T _x, T _y, T _z ) : s(_s), v(_x,_y,_z) {}
Quat( Quat const &q ) : s(q.s), v(q.v) {}
//!@name Set & Get value functions
void Zero() { s=0; v.Zero(); }
void Reset() { s=1; v.Zero(); } //!< Sets the scalar part to 1 and vector part to zero vector.
void Set( T _s, Vec3<T> const &_v ) { s=_s; v=_v; }
void Set( T _s, T _x, T _y, T _z ) { s=_s; v.Set(_x,_y,_z); }
void Set( T *array ) { s=array[0]; v.Set(&array[1]); }
void SetRotation( T angle, Vec3<T> const &axis ) { s=cos(angle*0.5f); v=(T)sin(angle*0.5f)*(axis.GetNormalized()); }
void SetRotation( T angle, T axisX, T axisY, T axisZ ) { SetRotation( angle, Vec3<T>(axisX,axisY,axisZ) ); }
void Get( T *array ) { array[0]=s; v.Get(&array[1]); }
T GetRotationAngle() { return 2.0f*(T)acos(s); } //!< Returns rotation angle in radiants
Vec3<T> GetRotationAxis () { return v.GetNormalized(); }
//!@name Length and Normalize functions
void Normalize () { T f=1.0f/Length(); *this *= f; }
Quat GetNormalized() const { T f=1.0f/Length(); return *this*f; }
T LengthSquared() const { return s*s + v.LengthSquared(); }
T Length () const { return (T) sqrt(LengthSquared()); }
//!@name Matrix conversion functions
void ToMatrix3( Matrix3<T> &m ) const { FillMatrix( m.cell, &m.cell[3], &m.cell[6] ); }
void ToMatrix4( Matrix4<T> &m ) const { FillMatrix( m.cell, &m.cell[4], &m.cell[8] ); m.cell[3]=m.cell[7]=m.cell[11]=m.cell[12]=m.cell[13]=m.cell[14]=0; m.cell[15]=1; }
Matrix3<T> ToMatrix3() const { Matrix3<T> m; ToMatrix3(m); return m; }
Matrix4<T> ToMatrix4() const { Matrix4<T> m; ToMatrix4(m); return m; }
//!@name Unary operators
Quat operator - () const { return Quat(-s,-v); }
//!@name Binary operators
Quat operator * ( Quat const &q ) const { return Quat( s*q.s - v.Dot(q.v), s*q.v + q.s*v + v.Cross(q.v)); }
Quat operator + ( Quat const &q ) const { return Quat( s + q.s, v + q.v ); }
Quat operator - ( Quat const &q ) const { return Quat( s - q.s, v - q.v ); }
Quat operator * ( T const &f ) const { return Quat( s*f, v*f); }
//!@name Assignment operators
Quat& operator *= ( Quat const &q ) { Set( s*q.s - v.Dot(q.v), s*q.v + q.s*v + v.Cross(q.v)); return *this; }
Quat& operator += ( Quat const &q ) { s+=q.s; v+=q.v; return *this; }
Quat& operator -= ( Quat const &q ) { s-=q.s; v-=q.v; return *this; }
Quat& operator *= ( T const &f ) { s*=f; v*=f; return *this; }
//!@name Test operators
int operator == ( Quat const &q ) const { return ( (q.s==s) && (q.v==v) ); }
int operator != ( Quat const &q ) const { return ( (q.s!=s) || (q.v!=v) ); }
//!@name Vector rotations
//! Rotates the given vector using the quaternion.
//! Note that the quaternion must be a unit quaternion.
Vec3<T> GetRotatedVector( Vec3<T> const &p )
{
T t2 = s*v.x;
T t3 = s*v.y;
T t4 = s*v.z;
T t5 = -v.x*v.x;
T t6 = v.x*v.y;
T t7 = v.x*v.z;
T t8 = -v.y*v.y;
T t9 = v.y*v.z;
T t10 = -v.z*v.z;
Vec3<T> pnew;
pnew.x = 2*( (t8 + t10)*p.x + (t6 - t4)*p.y + (t3 + t7)*p.z ) + p.x;
pnew.y = 2*( (t4 + t6)*p.x + (t5 + t10)*p.y + (t9 - t2)*p.z ) + p.y;
pnew.z = 2*( (t7 - t3)*p.x + (t2 + t9)*p.y + (t5 + t8)*p.z ) + p.z;
return pnew;
}
void RotateVector( Vec3<T> &p ) { p = GetRotatedVector(p); }
private:
//! \internal
void FillMatrix( T *col1, T *col2, T *col3 ) const
{
col1[0] = s*s + v.x*v.x - v.y*v.y - v.z*v.z;
col1[1] = 2.0f*v.x*v.y + 2.0f*s*v.z;
col1[2] = 2.0f*v.x*v.z - 2.0f*s*v.y;
col2[0] = 2.0f*v.x*v.y - 2.0f*s*v.z;
col2[1] = s*s - v.x*v.x + v.y*v.y - v.z*v.z;
col2[2] = 2.0f*v.y*v.z + 2.0f*s*v.x;
col3[0] = 2.0f*v.x*v.z + 2.0f*s*v.y;
col3[1] = 2.0f*v.y*v.z - 2.0f*s*v.x;
col3[2] = s*s - v.x*v.x - v.y*v.y + v.z*v.z;
}
};
//-------------------------------------------------------------------------------
typedef Quat<float> Quatf; //!< Quaternion class with float elements
typedef Quat<double> Quatd; //!< Quaternion class with double elements
//-------------------------------------------------------------------------------
} // namespace cy
//-------------------------------------------------------------------------------
typedef cy::Quatf cyQuatf; //!< Quaternion class with float elements
typedef cy::Quatd cyQuatd; //!< Quaternion class with double elements
//-------------------------------------------------------------------------------
#endif