This repository has been archived by the owner on Aug 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVector.h
232 lines (197 loc) · 4.99 KB
/
CVector.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: sdk/CVector.h
* PURPOSE: 3D vector math implementation
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#ifndef __CVector_H
#define __CVector_H
#include <math.h>
#define FLOAT_EPSILON 0.0001f
/**
* CVector Structure used to store a 3D vertex.
*/
class CVector
{
private:
bool IsNearZeroFloat( const float param )
{
//if(!_finite(param))
// return 1;
return (fabs(param) < FLOAT_EPSILON);
}
public:
float fX, fY, fZ;
CVector ()
{
this->fX = 0;
this->fY = 0;
this->fZ = 0;
};
CVector ( float fX, float fY, float fZ)
{
this->fX = fX;
this->fY = fY;
this->fZ = fZ;
}
float Normalize ( void )
{
double t = sqrt(fX*fX + fY*fY + fZ*fZ);
if ( t > FLOAT_EPSILON )
{
double fX2 = fX / t;
double fY2 = fY / t;
double fZ2 = fZ / t;
fX = (float)fX2;
fY = (float)fY2;
fZ = (float)fZ2;
}
else
t = 0;
return static_cast < float > ( t );
}
float Length ( void ) const
{
return sqrt ( (fX*fX) + (fY*fY) + (fZ*fZ) );
}
float DotProduct ( const CVector * param ) const
{
return fX*param->fX + fY*param->fY + fZ*param->fZ;
}
void CrossProduct ( const CVector * param )
{
float _fX = fX, _fY = fY, _fZ = fZ;
fX = _fY * param->fZ - param->fY * _fZ;
fY = _fZ * param->fX - param->fZ * _fX;
fZ = _fX * param->fY - param->fX * _fY;
}
bool IsNearZero ( void )
{
//if(!isfinite(fX) || !isfinite(fY) || !isfinite(fZ))
// return 1;
return (IsNearZeroFloat(fX) && IsNearZeroFloat(fY) && IsNearZeroFloat(fZ));
}
void Zero ( void )
{
fX = 0.0f;
fY = 0.0f;
fZ = 0.0f;
}
void ZeroNearZero ( void )
{
if ( IsNearZeroFloat(fX) )
fX = 0.0f;
if ( IsNearZeroFloat(fY) )
fY = 0.0f;
if ( IsNearZeroFloat(fZ) )
fZ = 0.0f;
}
float GetAngleRadians ( void )
{
return -atan2(fY, -fX);
}
float GetAngleDegrees ( void )
{
static float radtodeg = 57.324840764331210191082802547771f; // 180/pi
float ret = (atan2(fY, -fX) * radtodeg) + 270.0f;
if (ret >= 360.0f) ret -= 360.0f;
return ret;
}
CVector operator + ( const CVector& vecRight ) const
{
return CVector ( fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ );
}
CVector operator - ( const CVector& vecRight ) const
{
return CVector ( fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ );
}
CVector operator * ( const CVector& vecRight ) const
{
return CVector ( fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ );
}
CVector operator * ( float fRight ) const
{
return CVector ( fX * fRight, fY * fRight, fZ * fRight );
}
CVector operator / ( const CVector& vecRight ) const
{
return CVector ( fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ );
}
CVector operator / ( float fRight ) const
{
return CVector ( fX / fRight, fY / fRight, fZ / fRight );
}
CVector operator - () const
{
return CVector ( -fX, -fY, -fZ );
}
void operator += ( float fRight )
{
fX += fRight;
fY += fRight;
fZ += fRight;
}
void operator += ( const CVector& vecRight )
{
fX += vecRight.fX;
fY += vecRight.fY;
fZ += vecRight.fZ;
}
void operator -= ( float fRight )
{
fX -= fRight;
fY -= fRight;
fZ -= fRight;
}
void operator -= ( const CVector& vecRight )
{
fX -= vecRight.fX;
fY -= vecRight.fY;
fZ -= vecRight.fZ;
}
void operator *= ( float fRight )
{
fX *= fRight;
fY *= fRight;
fZ *= fRight;
}
void operator *= ( const CVector& vecRight )
{
fX *= vecRight.fX;
fY *= vecRight.fY;
fZ *= vecRight.fZ;
}
void operator /= ( float fRight )
{
fX /= fRight;
fY /= fRight;
fZ /= fRight;
}
void operator /= ( const CVector& vecRight )
{
fX /= vecRight.fX;
fY /= vecRight.fY;
fZ /= vecRight.fZ;
}
bool operator== ( const CVector& param ) const
{
return ( ( fabs ( fX - param.fX ) < FLOAT_EPSILON ) &&
( fabs ( fY - param.fY ) < FLOAT_EPSILON ) &&
( fabs ( fZ - param.fZ ) < FLOAT_EPSILON ) );
}
bool operator!= ( const CVector& param ) const
{
return ( ( fabs ( fX - param.fX ) >= FLOAT_EPSILON ) ||
( fabs ( fY - param.fY ) >= FLOAT_EPSILON ) ||
( fabs ( fZ - param.fZ ) >= FLOAT_EPSILON ) );
}
};
// global vector normals
CVector const g_vecFrontNormal ( 1.0f, 0.0f, 0.0f );
CVector const g_vecRightNormal ( 0.0f, 1.0f, 0.0f );
CVector const g_vecUpNormal ( 0.0f, 0.0f, 1.0f );
#endif