-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCVector3f.cpp
More file actions
147 lines (132 loc) · 3.51 KB
/
CVector3f.cpp
File metadata and controls
147 lines (132 loc) · 3.51 KB
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
#include "CVector3f.h"
#include "CQuaternion.h"
CVector3f::CVector3f()
{
//ctor
x = y = z = 0.0f;
}
void CVector3f::setXYZ(const FLOAT x,const FLOAT y,const FLOAT z)
{
this->x = x; this->y = y; this->z = z;
}
CVector3f::CVector3f(const FLOAT x,const FLOAT y,const FLOAT z)
{
this->x = x; this->y = y; this->z = z;
}
CVector3f::~CVector3f()
{
//dtor
}
void CVector3f::print() const
{
#ifdef FLOAT_32
printf("(%f %f %f)",x,y,z);
#endif
#ifdef FLOAT_64
printf("(%0.9f %0.9f %0.9f)",x,y,z);
#endif // FLOAT_64
}
void CVector3f::canonize()
{
pitch *= H3DMath::M_DEG2RAD;
heading *= H3DMath::M_DEG2RAD;
bank *= H3DMath::M_DEG2RAD;
pitch = WRAPPI(pitch);
if(pitch < -H3DMath::HALF_PI)
{
pitch = -(H3DMath::PI+pitch);
heading += H3DMath::PI;
bank += H3DMath::PI;
}
else if(pitch > H3DMath::HALF_PI)
{
pitch = H3DMath::PI - pitch;
heading += H3DMath::PI;
bank += H3DMath::PI;
}
//check gimbal lock
if(fabs(pitch) > (H3DMath::HALF_PI - 1e-4))
{
heading += bank;
bank = 0.0;
}
else
{
bank = WRAPPI(bank);
}
heading = WRAPPI(heading);
assert(pitch >= -H3DMath::HALF_PI && pitch <= H3DMath::HALF_PI);
assert(heading >= -H3DMath::PI && heading <= H3DMath::PI);
assert(bank >= -H3DMath::PI && bank <= H3DMath::PI);
pitch *= H3DMath::M_RAD2DEG;
heading *= H3DMath::M_RAD2DEG;
bank *= H3DMath::M_RAD2DEG;
}
CVector3f CVector3f::canonized()const
{
FLOAT tmp1 = pitch,tmp2 = heading,tmp3 = bank;
tmp1 *= H3DMath::M_DEG2RAD;
tmp2 *= H3DMath::M_DEG2RAD;
tmp3 *= H3DMath::M_DEG2RAD;
tmp1 = WRAPPI(tmp1);
if(tmp1 < -H3DMath::HALF_PI)
{
tmp1 = -(H3DMath::PI+tmp1);
tmp2 += H3DMath::PI;
tmp3 += H3DMath::PI;
}
else if(tmp1 > H3DMath::HALF_PI)
{
tmp1 = H3DMath::PI - tmp1;
tmp2 += H3DMath::PI;
tmp3 += H3DMath::PI;
}
//check gimbal lock
if(fabs(tmp1) > (H3DMath::HALF_PI - 1e-4))
{
tmp2 += tmp3;
tmp3 = 0.0;
}
else
{
tmp3 = WRAPPI(tmp3);
}
tmp2 = WRAPPI(tmp2);
assert(tmp1 >= -H3DMath::HALF_PI && tmp1 <= H3DMath::HALF_PI);
assert(tmp2 >= -H3DMath::PI && tmp2 <= H3DMath::PI);
assert(tmp3 >= -H3DMath::PI && tmp3 <= H3DMath::PI);
tmp1 *= H3DMath::M_RAD2DEG;
tmp2 *= H3DMath::M_RAD2DEG;
tmp3 *= H3DMath::M_RAD2DEG;
return CVector3f(tmp1,tmp2,tmp3);
}
CVector3f CVector3f::normalized() const
{
FLOAT squmrt = sqrt((FLOAT)x*x+(FLOAT)y*y+(FLOAT)z*z);
if( squmrt )
return CVector3f( (FLOAT)x/squmrt,(FLOAT)y/squmrt,(FLOAT)z/squmrt);
return *this;
}
void CVector3f::normalize()
{
FLOAT squmrt = sqrt((FLOAT)x*x+(FLOAT)y*y+(FLOAT)z*z);
if( squmrt )
{
x = (FLOAT)x/squmrt; y = (FLOAT)y/squmrt; z = (FLOAT)z/squmrt;
}
}
CQuaternion CVector3f::toQuat(const int type)
{
//INERTIATOOBJECT
CQuaternion rs;
FLOAT p = x*H3DMath::M_DEG2RAD,h = y*H3DMath::M_DEG2RAD,b = z*H3DMath::M_DEG2RAD;
rs.w = (FLOAT)( cos(h/2)*cos(p/2)*cos(b/2) + sin(h/2)*sin(p/2)*sin(b/2));
rs.x = (FLOAT)(-cos(h/2)*sin(p/2)*cos(b/2) - sin(h/2)*cos(p/2)*sin(b/2));
rs.y = (FLOAT)( cos(h/2)*sin(p/2)*sin(b/2) - sin(h/2)*cos(p/2)*cos(b/2));
rs.z = (FLOAT)( sin(h/2)*sin(p/2)*cos(b/2) - cos(h/2)*cos(p/2)*sin(b/2));
if(type == OBJECTTOINERTIA)
{
rs = rs.conjugated();
}
return rs;
}