-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vector4f.cpp
360 lines (304 loc) · 7.62 KB
/
Vector4f.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "Vector4f.h"
#include "Vector2f.h"
#include "Vector3f.h"
Vector4f::Vector4f( float f )
{
m_elements[ 0 ] = f;
m_elements[ 1 ] = f;
m_elements[ 2 ] = f;
m_elements[ 3 ] = f;
}
Vector4f::Vector4f( float fx, float fy, float fz, float fw )
{
m_elements[0] = fx;
m_elements[1] = fy;
m_elements[2] = fz;
m_elements[3] = fw;
}
Vector4f::Vector4f( float buffer[ 4 ] )
{
m_elements[ 0 ] = buffer[ 0 ];
m_elements[ 1 ] = buffer[ 1 ];
m_elements[ 2 ] = buffer[ 2 ];
m_elements[ 3 ] = buffer[ 3 ];
}
Vector4f::Vector4f( const Vector2f& xy, float z, float w )
{
m_elements[0] = xy.x();
m_elements[1] = xy.y();
m_elements[2] = z;
m_elements[3] = w;
}
Vector4f::Vector4f( float x, const Vector2f& yz, float w )
{
m_elements[0] = x;
m_elements[1] = yz.x();
m_elements[2] = yz.y();
m_elements[3] = w;
}
Vector4f::Vector4f( float x, float y, const Vector2f& zw )
{
m_elements[0] = x;
m_elements[1] = y;
m_elements[2] = zw.x();
m_elements[3] = zw.y();
}
Vector4f::Vector4f( const Vector2f& xy, const Vector2f& zw )
{
m_elements[0] = xy.x();
m_elements[1] = xy.y();
m_elements[2] = zw.x();
m_elements[3] = zw.y();
}
Vector4f::Vector4f( const Vector3f& xyz, float w )
{
m_elements[0] = xyz.x();
m_elements[1] = xyz.y();
m_elements[2] = xyz.z();
m_elements[3] = w;
}
Vector4f::Vector4f( float x, const Vector3f& yzw )
{
m_elements[0] = x;
m_elements[1] = yzw.x();
m_elements[2] = yzw.y();
m_elements[3] = yzw.z();
}
Vector4f::Vector4f( const Vector4f& rv )
{
m_elements[0] = rv.m_elements[0];
m_elements[1] = rv.m_elements[1];
m_elements[2] = rv.m_elements[2];
m_elements[3] = rv.m_elements[3];
}
Vector4f& Vector4f::operator = ( const Vector4f& rv )
{
if( this != &rv )
{
m_elements[0] = rv.m_elements[0];
m_elements[1] = rv.m_elements[1];
m_elements[2] = rv.m_elements[2];
m_elements[3] = rv.m_elements[3];
}
return *this;
}
const float& Vector4f::operator [] ( int i ) const
{
return m_elements[ i ];
}
float& Vector4f::operator [] ( int i )
{
return m_elements[ i ];
}
float& Vector4f::x()
{
return m_elements[ 0 ];
}
float& Vector4f::y()
{
return m_elements[ 1 ];
}
float& Vector4f::z()
{
return m_elements[ 2 ];
}
float& Vector4f::w()
{
return m_elements[ 3 ];
}
float Vector4f::x() const
{
return m_elements[0];
}
float Vector4f::y() const
{
return m_elements[1];
}
float Vector4f::z() const
{
return m_elements[2];
}
float Vector4f::w() const
{
return m_elements[3];
}
Vector2f Vector4f::xy() const
{
return Vector2f( m_elements[0], m_elements[1] );
}
Vector2f Vector4f::yz() const
{
return Vector2f( m_elements[1], m_elements[2] );
}
Vector2f Vector4f::zw() const
{
return Vector2f( m_elements[2], m_elements[3] );
}
Vector2f Vector4f::wx() const
{
return Vector2f( m_elements[3], m_elements[0] );
}
Vector3f Vector4f::xyz() const
{
return Vector3f( m_elements[0], m_elements[1], m_elements[2] );
}
Vector3f Vector4f::yzw() const
{
return Vector3f( m_elements[1], m_elements[2], m_elements[3] );
}
Vector3f Vector4f::zwx() const
{
return Vector3f( m_elements[2], m_elements[3], m_elements[0] );
}
Vector3f Vector4f::wxy() const
{
return Vector3f( m_elements[3], m_elements[0], m_elements[1] );
}
Vector3f Vector4f::xyw() const
{
return Vector3f( m_elements[0], m_elements[1], m_elements[3] );
}
Vector3f Vector4f::yzx() const
{
return Vector3f( m_elements[1], m_elements[2], m_elements[0] );
}
Vector3f Vector4f::zwy() const
{
return Vector3f( m_elements[2], m_elements[3], m_elements[1] );
}
Vector3f Vector4f::wxz() const
{
return Vector3f( m_elements[3], m_elements[0], m_elements[2] );
}
float Vector4f::abs() const
{
return sqrt( m_elements[0] * m_elements[0] + m_elements[1] * m_elements[1] + m_elements[2] * m_elements[2] + m_elements[3] * m_elements[3] );
}
float Vector4f::absSquared() const
{
return( m_elements[0] * m_elements[0] + m_elements[1] * m_elements[1] + m_elements[2] * m_elements[2] + m_elements[3] * m_elements[3] );
}
void Vector4f::normalize()
{
float norm = sqrt( m_elements[0] * m_elements[0] + m_elements[1] * m_elements[1] + m_elements[2] * m_elements[2] + m_elements[3] * m_elements[3] );
m_elements[0] = m_elements[0] / norm;
m_elements[1] = m_elements[1] / norm;
m_elements[2] = m_elements[2] / norm;
m_elements[3] = m_elements[3] / norm;
}
Vector4f Vector4f::normalized() const
{
float length = abs();
return Vector4f
(
m_elements[0] / length,
m_elements[1] / length,
m_elements[2] / length,
m_elements[3] / length
);
}
void Vector4f::homogenize()
{
if( m_elements[3] != 0 )
{
m_elements[0] /= m_elements[3];
m_elements[1] /= m_elements[3];
m_elements[2] /= m_elements[3];
m_elements[3] = 1;
}
}
Vector4f Vector4f::homogenized() const
{
if( m_elements[3] != 0 )
{
return Vector4f
(
m_elements[0] / m_elements[3],
m_elements[1] / m_elements[3],
m_elements[2] / m_elements[3],
1
);
}
else
{
return Vector4f
(
m_elements[0],
m_elements[1],
m_elements[2],
m_elements[3]
);
}
}
void Vector4f::negate()
{
m_elements[0] = -m_elements[0];
m_elements[1] = -m_elements[1];
m_elements[2] = -m_elements[2];
m_elements[3] = -m_elements[3];
}
const float *Vector4f::getElements() const
{
return m_elements;
}
void Vector4f::print() const
{
printf( "< %.4f, %.4f, %.4f, %.4f >",
m_elements[0], m_elements[1], m_elements[2], m_elements[3] );
}
// static
float Vector4f::dot( const Vector4f& v0, const Vector4f& v1 )
{
return v0.x() * v1.x() + v0.y() * v1.y() + v0.z() * v1.z() + v0.w() * v1.w();
}
// static
Vector4f Vector4f::lerp( const Vector4f& v0, const Vector4f& v1, float alpha )
{
return alpha * ( v1 - v0 ) + v0;
}
//////////////////////////////////////////////////////////////////////////
// Operators
//////////////////////////////////////////////////////////////////////////
Vector4f operator + ( const Vector4f& v0, const Vector4f& v1 )
{
return Vector4f( v0.x() + v1.x(), v0.y() + v1.y(), v0.z() + v1.z(), v0.w() + v1.w() );
}
Vector4f operator - ( const Vector4f& v0, const Vector4f& v1 )
{
return Vector4f( v0.x() - v1.x(), v0.y() - v1.y(), v0.z() - v1.z(), v0.w() - v1.w() );
}
Vector4f operator * ( const Vector4f& v0, const Vector4f& v1 )
{
return Vector4f( v0.x() * v1.x(), v0.y() * v1.y(), v0.z() * v1.z(), v0.w() * v1.w() );
}
Vector4f operator / ( const Vector4f& v0, const Vector4f& v1 )
{
return Vector4f( v0.x() / v1.x(), v0.y() / v1.y(), v0.z() / v1.z(), v0.w() / v1.w() );
}
Vector4f operator - ( const Vector4f& v )
{
return Vector4f( -v.x(), -v.y(), -v.z(), -v.w() );
}
Vector4f operator * ( float f, const Vector4f& v )
{
return Vector4f( f * v.x(), f * v.y(), f * v.z(), f * v.w() );
}
Vector4f operator * ( const Vector4f& v, float f )
{
return Vector4f( f * v.x(), f * v.y(), f * v.z(), f * v.w() );
}
Vector4f operator / ( const Vector4f& v, float f )
{
return Vector4f( v[0] / f, v[1] / f, v[2] / f, v[3] / f );
}
bool operator == ( const Vector4f& v0, const Vector4f& v1 )
{
return( v0.x() == v1.x() && v0.y() == v1.y() && v0.z() == v1.z() && v0.w() == v1.w() );
}
bool operator != ( const Vector4f& v0, const Vector4f& v1 )
{
return !( v0 == v1 );
}