-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vector3f.cpp
317 lines (263 loc) · 6.42 KB
/
Vector3f.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "Vector3f.h"
#include "Vector2f.h"
//////////////////////////////////////////////////////////////////////////
// Public
//////////////////////////////////////////////////////////////////////////
// static
const Vector3f Vector3f::ZERO = Vector3f( 0, 0, 0 );
// static
const Vector3f Vector3f::UP = Vector3f( 0, 1, 0 );
// static
const Vector3f Vector3f::RIGHT = Vector3f( 1, 0, 0 );
// static
const Vector3f Vector3f::FORWARD = Vector3f( 0, 0, -1 );
Vector3f::Vector3f( float f )
{
m_elements[0] = f;
m_elements[1] = f;
m_elements[2] = f;
}
Vector3f::Vector3f( float x, float y, float z )
{
m_elements[0] = x;
m_elements[1] = y;
m_elements[2] = z;
}
Vector3f::Vector3f( const Vector2f& xy, float z )
{
m_elements[0] = xy.x();
m_elements[1] = xy.y();
m_elements[2] = z;
}
Vector3f::Vector3f( float x, const Vector2f& yz )
{
m_elements[0] = x;
m_elements[1] = yz.x();
m_elements[2] = yz.y();
}
Vector3f::Vector3f( const Vector3f& rv )
{
m_elements[0] = rv[0];
m_elements[1] = rv[1];
m_elements[2] = rv[2];
}
Vector3f& Vector3f::operator = ( const Vector3f& rv )
{
if( this != &rv )
{
m_elements[0] = rv[0];
m_elements[1] = rv[1];
m_elements[2] = rv[2];
}
return *this;
}
const float& Vector3f::operator [] ( int i ) const
{
return m_elements[i];
}
float& Vector3f::operator [] ( int i )
{
return m_elements[i];
}
float& Vector3f::x()
{
return m_elements[0];
}
float& Vector3f::y()
{
return m_elements[1];
}
float& Vector3f::z()
{
return m_elements[2];
}
float Vector3f::x() const
{
return m_elements[0];
}
float Vector3f::y() const
{
return m_elements[1];
}
float Vector3f::z() const
{
return m_elements[2];
}
Vector2f Vector3f::xy() const
{
return Vector2f( m_elements[0], m_elements[1] );
}
Vector2f Vector3f::xz() const
{
return Vector2f( m_elements[0], m_elements[2] );
}
Vector2f Vector3f::yz() const
{
return Vector2f( m_elements[1], m_elements[2] );
}
Vector3f Vector3f::xyz() const
{
return Vector3f( m_elements[0], m_elements[1], m_elements[2] );
}
Vector3f Vector3f::yzx() const
{
return Vector3f( m_elements[1], m_elements[2], m_elements[0] );
}
Vector3f Vector3f::zxy() const
{
return Vector3f( m_elements[2], m_elements[0], m_elements[1] );
}
float Vector3f::abs() const
{
return sqrt( m_elements[0] * m_elements[0] + m_elements[1] * m_elements[1] + m_elements[2] * m_elements[2] );
}
float Vector3f::absSquared() const
{
return
(
m_elements[0] * m_elements[0] +
m_elements[1] * m_elements[1] +
m_elements[2] * m_elements[2]
);
}
void Vector3f::normalize()
{
float norm = abs();
m_elements[0] /= norm;
m_elements[1] /= norm;
m_elements[2] /= norm;
}
Vector3f Vector3f::normalized() const
{
float norm = abs();
return Vector3f
(
m_elements[0] / norm,
m_elements[1] / norm,
m_elements[2] / norm
);
}
Vector2f Vector3f::homogenized() const
{
return Vector2f
(
m_elements[ 0 ] / m_elements[ 2 ],
m_elements[ 1 ] / m_elements[ 2 ]
);
}
void Vector3f::negate()
{
m_elements[0] = -m_elements[0];
m_elements[1] = -m_elements[1];
m_elements[2] = -m_elements[2];
}
const float *Vector3f::getElements() const
{
return m_elements;
}
void Vector3f::print() const
{
printf( "< %.4f, %.4f, %.4f >",
m_elements[0], m_elements[1], m_elements[2] );
}
Vector3f& Vector3f::operator += ( const Vector3f& v )
{
m_elements[ 0 ] += v.m_elements[ 0 ];
m_elements[ 1 ] += v.m_elements[ 1 ];
m_elements[ 2 ] += v.m_elements[ 2 ];
return *this;
}
Vector3f& Vector3f::operator -= ( const Vector3f& v )
{
m_elements[ 0 ] -= v.m_elements[ 0 ];
m_elements[ 1 ] -= v.m_elements[ 1 ];
m_elements[ 2 ] -= v.m_elements[ 2 ];
return *this;
}
Vector3f& Vector3f::operator *= ( float f )
{
m_elements[ 0 ] *= f;
m_elements[ 1 ] *= f;
m_elements[ 2 ] *= f;
return *this;
}
// static
float Vector3f::dot( const Vector3f& v0, const Vector3f& v1 )
{
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
}
// static
Vector3f Vector3f::cross( const Vector3f& v0, const Vector3f& v1 )
{
return Vector3f
(
v0.y() * v1.z() - v0.z() * v1.y(),
v0.z() * v1.x() - v0.x() * v1.z(),
v0.x() * v1.y() - v0.y() * v1.x()
);
}
// static
Vector3f Vector3f::lerp( const Vector3f& v0, const Vector3f& v1, float alpha )
{
return alpha * ( v1 - v0 ) + v0;
}
// static
Vector3f Vector3f::cubicInterpolate( const Vector3f& p0, const Vector3f& p1, const Vector3f& p2, const Vector3f& p3, float t )
{
// geometric construction:
// t
// (t+1)/2 t/2
// t+1 t t-1
// bottom level
Vector3f p0p1 = Vector3f::lerp( p0, p1, t + 1 );
Vector3f p1p2 = Vector3f::lerp( p1, p2, t );
Vector3f p2p3 = Vector3f::lerp( p2, p3, t - 1 );
// middle level
Vector3f p0p1_p1p2 = Vector3f::lerp( p0p1, p1p2, 0.5f * ( t + 1 ) );
Vector3f p1p2_p2p3 = Vector3f::lerp( p1p2, p2p3, 0.5f * t );
// top level
return Vector3f::lerp( p0p1_p1p2, p1p2_p2p3, t );
}
Vector3f operator + ( const Vector3f& v0, const Vector3f& v1 )
{
return Vector3f( v0[0] + v1[0], v0[1] + v1[1], v0[2] + v1[2] );
}
Vector3f operator - ( const Vector3f& v0, const Vector3f& v1 )
{
return Vector3f( v0[0] - v1[0], v0[1] - v1[1], v0[2] - v1[2] );
}
Vector3f operator * ( const Vector3f& v0, const Vector3f& v1 )
{
return Vector3f( v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2] );
}
Vector3f operator / ( const Vector3f& v0, const Vector3f& v1 )
{
return Vector3f( v0[0] / v1[0], v0[1] / v1[1], v0[2] / v1[2] );
}
Vector3f operator - ( const Vector3f& v )
{
return Vector3f( -v[0], -v[1], -v[2] );
}
Vector3f operator * ( float f, const Vector3f& v )
{
return Vector3f( v[0] * f, v[1] * f, v[2] * f );
}
Vector3f operator * ( const Vector3f& v, float f )
{
return Vector3f( v[0] * f, v[1] * f, v[2] * f );
}
Vector3f operator / ( const Vector3f& v, float f )
{
return Vector3f( v[0] / f, v[1] / f, v[2] / f );
}
bool operator == ( const Vector3f& v0, const Vector3f& v1 )
{
return( v0.x() == v1.x() && v0.y() == v1.y() && v0.z() == v1.z() );
}
bool operator != ( const Vector3f& v0, const Vector3f& v1 )
{
return !( v0 == v1 );
}