1 | /** |
---|
2 | @file Vector4.h |
---|
3 | |
---|
4 | Homogeneous vector class. |
---|
5 | |
---|
6 | @maintainer Morgan McGuire, matrix@graphics3d.com |
---|
7 | |
---|
8 | @created 2002-07-09 |
---|
9 | @edited 2005-03-28 |
---|
10 | |
---|
11 | Copyright 2000-2006, Morgan McGuire. |
---|
12 | All rights reserved. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifndef G3D_VECTOR4_H |
---|
16 | #define G3D_VECTOR4_H |
---|
17 | |
---|
18 | #include "G3D/platform.h" |
---|
19 | #include "G3D/g3dmath.h" |
---|
20 | #include "G3D/Vector3.h" |
---|
21 | #include "G3D/Vector2.h" |
---|
22 | #include <string> |
---|
23 | |
---|
24 | namespace G3D { |
---|
25 | |
---|
26 | class Vector2; |
---|
27 | class Vector3; |
---|
28 | class Vector4; |
---|
29 | |
---|
30 | /** |
---|
31 | Do not subclass-- this implementation makes assumptions about the |
---|
32 | memory layout. |
---|
33 | */ |
---|
34 | class Vector4 { |
---|
35 | private: |
---|
36 | // Hidden operators |
---|
37 | bool operator<(const Vector4&) const; |
---|
38 | bool operator>(const Vector4&) const; |
---|
39 | bool operator<=(const Vector4&) const; |
---|
40 | bool operator>=(const Vector4&) const; |
---|
41 | |
---|
42 | public: |
---|
43 | // construction |
---|
44 | Vector4(); |
---|
45 | Vector4(float fX, float fY, float fZ, float fW); |
---|
46 | Vector4(float afCoordinate[4]); |
---|
47 | Vector4(const Vector4& rkVector); |
---|
48 | Vector4(const class Color4& c); |
---|
49 | Vector4(const Vector3& rkVector, float fW); |
---|
50 | Vector4(const Vector2& v1, const Vector2& v2); |
---|
51 | Vector4(const Vector2& v1, float fz, float fw); |
---|
52 | |
---|
53 | // coordinates |
---|
54 | float x, y, z, w; |
---|
55 | |
---|
56 | // access vector V as V[0] = V.x, V[1] = V.y, V[2] = V.z, etc. |
---|
57 | // |
---|
58 | // WARNING. These member functions rely on |
---|
59 | // (1) Vector4 not having virtual functions |
---|
60 | // (2) the data packed in a 4*sizeof(float) memory block |
---|
61 | float& operator[] (int i); |
---|
62 | const float& operator[] (int i) const; |
---|
63 | operator float* (); |
---|
64 | operator const float* () const; |
---|
65 | |
---|
66 | // assignment and comparison |
---|
67 | Vector4& operator= (const Vector4& rkVector); |
---|
68 | bool operator== (const Vector4& rkVector) const; |
---|
69 | bool operator!= (const Vector4& rkVector) const; |
---|
70 | |
---|
71 | inline void set(float _x, float _y, float _z, float _w) { |
---|
72 | x = _x; |
---|
73 | y = _y; |
---|
74 | z = _z; |
---|
75 | w = _w; |
---|
76 | } |
---|
77 | |
---|
78 | inline void set(const Vector3& v, float _w) { |
---|
79 | x = v.x; |
---|
80 | y = v.y; |
---|
81 | z = v.z; |
---|
82 | w = _w; |
---|
83 | } |
---|
84 | |
---|
85 | inline void set(const Vector2& v, float _z, float _w) { |
---|
86 | x = v.x; |
---|
87 | y = v.y; |
---|
88 | z = _z; |
---|
89 | w = _w; |
---|
90 | } |
---|
91 | |
---|
92 | unsigned int hashCode() const; |
---|
93 | bool fuzzyEq(const Vector4& other) const; |
---|
94 | bool fuzzyNe(const Vector4& other) const; |
---|
95 | |
---|
96 | inline static const Vector4& inf() { static Vector4 v((float)G3D::inf(), (float)G3D::inf(), (float)G3D::inf(), (float)G3D::inf()); return v; } |
---|
97 | inline static const Vector4& nan() { static Vector4 v((float)G3D::nan(), (float)G3D::nan(), (float)G3D::nan(), (float)G3D::nan()); return v; } |
---|
98 | |
---|
99 | /** sqrt(this->dot(*this)) */ |
---|
100 | float length() const; |
---|
101 | float squaredLength() const; |
---|
102 | |
---|
103 | inline float sum() const { |
---|
104 | return x + y + z + w; |
---|
105 | } |
---|
106 | |
---|
107 | /** Returns true if this vector has finite length */ |
---|
108 | bool isFinite() const; |
---|
109 | |
---|
110 | /** Returns true if this vector has length == 0 */ |
---|
111 | bool isZero() const; |
---|
112 | |
---|
113 | /** Returns true if this vector has length == 1 */ |
---|
114 | bool isUnit() const; |
---|
115 | |
---|
116 | // arithmetic operations |
---|
117 | Vector4 operator+ (const Vector4& rkVector) const; |
---|
118 | Vector4 operator- (const Vector4& rkVector) const; |
---|
119 | |
---|
120 | inline Vector4 operator*(const Vector4& rkVector) const { |
---|
121 | return Vector4(x * rkVector.x, y * rkVector.y, z * rkVector.z, w * rkVector.w); |
---|
122 | } |
---|
123 | |
---|
124 | inline Vector4 operator/(const Vector4& rkVector) const { |
---|
125 | return Vector4(x / rkVector.x, y / rkVector.y, z / rkVector.z, w / rkVector.w); |
---|
126 | } |
---|
127 | |
---|
128 | Vector4 operator* (float fScalar) const; |
---|
129 | Vector4 operator/ (float fScalar) const; |
---|
130 | Vector4 operator- () const; |
---|
131 | friend Vector4 operator* (float, const Vector4& rkVector); |
---|
132 | |
---|
133 | // arithmetic updates |
---|
134 | Vector4& operator+= (const Vector4& rkVector); |
---|
135 | Vector4& operator-= (const Vector4& rkVector); |
---|
136 | Vector4& operator*= (float fScalar); |
---|
137 | Vector4& operator/= (float fScalar); |
---|
138 | |
---|
139 | inline Vector4 clamp(const Vector4& low, const Vector4& high) const { |
---|
140 | return Vector4( |
---|
141 | G3D::clamp(x, low.x, high.x), |
---|
142 | G3D::clamp(y, low.y, high.y), |
---|
143 | G3D::clamp(z, low.z, high.z), |
---|
144 | G3D::clamp(w, low.w, high.w)); |
---|
145 | } |
---|
146 | |
---|
147 | inline Vector4 clamp(float low, float high) const { |
---|
148 | return Vector4( |
---|
149 | G3D::clamp(x, low, high), |
---|
150 | G3D::clamp(y, low, high), |
---|
151 | G3D::clamp(z, low, high), |
---|
152 | G3D::clamp(w, low, high)); |
---|
153 | } |
---|
154 | |
---|
155 | float dot (const Vector4& rkVector) const; |
---|
156 | |
---|
157 | Vector4 min(const Vector4& v) const; |
---|
158 | Vector4 max(const Vector4& v) const; |
---|
159 | |
---|
160 | std::string toString() const; |
---|
161 | |
---|
162 | /** |
---|
163 | Linear interpolation |
---|
164 | */ |
---|
165 | Vector4 lerp(const Vector4& v, float alpha) const; |
---|
166 | |
---|
167 | // 2-char swizzles |
---|
168 | |
---|
169 | Vector2 xx() const; |
---|
170 | Vector2 yx() const; |
---|
171 | Vector2 zx() const; |
---|
172 | Vector2 wx() const; |
---|
173 | Vector2 xy() const; |
---|
174 | Vector2 yy() const; |
---|
175 | Vector2 zy() const; |
---|
176 | Vector2 wy() const; |
---|
177 | Vector2 xz() const; |
---|
178 | Vector2 yz() const; |
---|
179 | Vector2 zz() const; |
---|
180 | Vector2 wz() const; |
---|
181 | Vector2 xw() const; |
---|
182 | Vector2 yw() const; |
---|
183 | Vector2 zw() const; |
---|
184 | Vector2 ww() const; |
---|
185 | |
---|
186 | // 3-char swizzles |
---|
187 | |
---|
188 | Vector3 xxx() const; |
---|
189 | Vector3 yxx() const; |
---|
190 | Vector3 zxx() const; |
---|
191 | Vector3 wxx() const; |
---|
192 | Vector3 xyx() const; |
---|
193 | Vector3 yyx() const; |
---|
194 | Vector3 zyx() const; |
---|
195 | Vector3 wyx() const; |
---|
196 | Vector3 xzx() const; |
---|
197 | Vector3 yzx() const; |
---|
198 | Vector3 zzx() const; |
---|
199 | Vector3 wzx() const; |
---|
200 | Vector3 xwx() const; |
---|
201 | Vector3 ywx() const; |
---|
202 | Vector3 zwx() const; |
---|
203 | Vector3 wwx() const; |
---|
204 | Vector3 xxy() const; |
---|
205 | Vector3 yxy() const; |
---|
206 | Vector3 zxy() const; |
---|
207 | Vector3 wxy() const; |
---|
208 | Vector3 xyy() const; |
---|
209 | Vector3 yyy() const; |
---|
210 | Vector3 zyy() const; |
---|
211 | Vector3 wyy() const; |
---|
212 | Vector3 xzy() const; |
---|
213 | Vector3 yzy() const; |
---|
214 | Vector3 zzy() const; |
---|
215 | Vector3 wzy() const; |
---|
216 | Vector3 xwy() const; |
---|
217 | Vector3 ywy() const; |
---|
218 | Vector3 zwy() const; |
---|
219 | Vector3 wwy() const; |
---|
220 | Vector3 xxz() const; |
---|
221 | Vector3 yxz() const; |
---|
222 | Vector3 zxz() const; |
---|
223 | Vector3 wxz() const; |
---|
224 | Vector3 xyz() const; |
---|
225 | Vector3 yyz() const; |
---|
226 | Vector3 zyz() const; |
---|
227 | Vector3 wyz() const; |
---|
228 | Vector3 xzz() const; |
---|
229 | Vector3 yzz() const; |
---|
230 | Vector3 zzz() const; |
---|
231 | Vector3 wzz() const; |
---|
232 | Vector3 xwz() const; |
---|
233 | Vector3 ywz() const; |
---|
234 | Vector3 zwz() const; |
---|
235 | Vector3 wwz() const; |
---|
236 | Vector3 xxw() const; |
---|
237 | Vector3 yxw() const; |
---|
238 | Vector3 zxw() const; |
---|
239 | Vector3 wxw() const; |
---|
240 | Vector3 xyw() const; |
---|
241 | Vector3 yyw() const; |
---|
242 | Vector3 zyw() const; |
---|
243 | Vector3 wyw() const; |
---|
244 | Vector3 xzw() const; |
---|
245 | Vector3 yzw() const; |
---|
246 | Vector3 zzw() const; |
---|
247 | Vector3 wzw() const; |
---|
248 | Vector3 xww() const; |
---|
249 | Vector3 yww() const; |
---|
250 | Vector3 zww() const; |
---|
251 | Vector3 www() const; |
---|
252 | |
---|
253 | // 4-char swizzles |
---|
254 | |
---|
255 | Vector4 xxxx() const; |
---|
256 | Vector4 yxxx() const; |
---|
257 | Vector4 zxxx() const; |
---|
258 | Vector4 wxxx() const; |
---|
259 | Vector4 xyxx() const; |
---|
260 | Vector4 yyxx() const; |
---|
261 | Vector4 zyxx() const; |
---|
262 | Vector4 wyxx() const; |
---|
263 | Vector4 xzxx() const; |
---|
264 | Vector4 yzxx() const; |
---|
265 | Vector4 zzxx() const; |
---|
266 | Vector4 wzxx() const; |
---|
267 | Vector4 xwxx() const; |
---|
268 | Vector4 ywxx() const; |
---|
269 | Vector4 zwxx() const; |
---|
270 | Vector4 wwxx() const; |
---|
271 | Vector4 xxyx() const; |
---|
272 | Vector4 yxyx() const; |
---|
273 | Vector4 zxyx() const; |
---|
274 | Vector4 wxyx() const; |
---|
275 | Vector4 xyyx() const; |
---|
276 | Vector4 yyyx() const; |
---|
277 | Vector4 zyyx() const; |
---|
278 | Vector4 wyyx() const; |
---|
279 | Vector4 xzyx() const; |
---|
280 | Vector4 yzyx() const; |
---|
281 | Vector4 zzyx() const; |
---|
282 | Vector4 wzyx() const; |
---|
283 | Vector4 xwyx() const; |
---|
284 | Vector4 ywyx() const; |
---|
285 | Vector4 zwyx() const; |
---|
286 | Vector4 wwyx() const; |
---|
287 | Vector4 xxzx() const; |
---|
288 | Vector4 yxzx() const; |
---|
289 | Vector4 zxzx() const; |
---|
290 | Vector4 wxzx() const; |
---|
291 | Vector4 xyzx() const; |
---|
292 | Vector4 yyzx() const; |
---|
293 | Vector4 zyzx() const; |
---|
294 | Vector4 wyzx() const; |
---|
295 | Vector4 xzzx() const; |
---|
296 | Vector4 yzzx() const; |
---|
297 | Vector4 zzzx() const; |
---|
298 | Vector4 wzzx() const; |
---|
299 | Vector4 xwzx() const; |
---|
300 | Vector4 ywzx() const; |
---|
301 | Vector4 zwzx() const; |
---|
302 | Vector4 wwzx() const; |
---|
303 | Vector4 xxwx() const; |
---|
304 | Vector4 yxwx() const; |
---|
305 | Vector4 zxwx() const; |
---|
306 | Vector4 wxwx() const; |
---|
307 | Vector4 xywx() const; |
---|
308 | Vector4 yywx() const; |
---|
309 | Vector4 zywx() const; |
---|
310 | Vector4 wywx() const; |
---|
311 | Vector4 xzwx() const; |
---|
312 | Vector4 yzwx() const; |
---|
313 | Vector4 zzwx() const; |
---|
314 | Vector4 wzwx() const; |
---|
315 | Vector4 xwwx() const; |
---|
316 | Vector4 ywwx() const; |
---|
317 | Vector4 zwwx() const; |
---|
318 | Vector4 wwwx() const; |
---|
319 | Vector4 xxxy() const; |
---|
320 | Vector4 yxxy() const; |
---|
321 | Vector4 zxxy() const; |
---|
322 | Vector4 wxxy() const; |
---|
323 | Vector4 xyxy() const; |
---|
324 | Vector4 yyxy() const; |
---|
325 | Vector4 zyxy() const; |
---|
326 | Vector4 wyxy() const; |
---|
327 | Vector4 xzxy() const; |
---|
328 | Vector4 yzxy() const; |
---|
329 | Vector4 zzxy() const; |
---|
330 | Vector4 wzxy() const; |
---|
331 | Vector4 xwxy() const; |
---|
332 | Vector4 ywxy() const; |
---|
333 | Vector4 zwxy() const; |
---|
334 | Vector4 wwxy() const; |
---|
335 | Vector4 xxyy() const; |
---|
336 | Vector4 yxyy() const; |
---|
337 | Vector4 zxyy() const; |
---|
338 | Vector4 wxyy() const; |
---|
339 | Vector4 xyyy() const; |
---|
340 | Vector4 yyyy() const; |
---|
341 | Vector4 zyyy() const; |
---|
342 | Vector4 wyyy() const; |
---|
343 | Vector4 xzyy() const; |
---|
344 | Vector4 yzyy() const; |
---|
345 | Vector4 zzyy() const; |
---|
346 | Vector4 wzyy() const; |
---|
347 | Vector4 xwyy() const; |
---|
348 | Vector4 ywyy() const; |
---|
349 | Vector4 zwyy() const; |
---|
350 | Vector4 wwyy() const; |
---|
351 | Vector4 xxzy() const; |
---|
352 | Vector4 yxzy() const; |
---|
353 | Vector4 zxzy() const; |
---|
354 | Vector4 wxzy() const; |
---|
355 | Vector4 xyzy() const; |
---|
356 | Vector4 yyzy() const; |
---|
357 | Vector4 zyzy() const; |
---|
358 | Vector4 wyzy() const; |
---|
359 | Vector4 xzzy() const; |
---|
360 | Vector4 yzzy() const; |
---|
361 | Vector4 zzzy() const; |
---|
362 | Vector4 wzzy() const; |
---|
363 | Vector4 xwzy() const; |
---|
364 | Vector4 ywzy() const; |
---|
365 | Vector4 zwzy() const; |
---|
366 | Vector4 wwzy() const; |
---|
367 | Vector4 xxwy() const; |
---|
368 | Vector4 yxwy() const; |
---|
369 | Vector4 zxwy() const; |
---|
370 | Vector4 wxwy() const; |
---|
371 | Vector4 xywy() const; |
---|
372 | Vector4 yywy() const; |
---|
373 | Vector4 zywy() const; |
---|
374 | Vector4 wywy() const; |
---|
375 | Vector4 xzwy() const; |
---|
376 | Vector4 yzwy() const; |
---|
377 | Vector4 zzwy() const; |
---|
378 | Vector4 wzwy() const; |
---|
379 | Vector4 xwwy() const; |
---|
380 | Vector4 ywwy() const; |
---|
381 | Vector4 zwwy() const; |
---|
382 | Vector4 wwwy() const; |
---|
383 | Vector4 xxxz() const; |
---|
384 | Vector4 yxxz() const; |
---|
385 | Vector4 zxxz() const; |
---|
386 | Vector4 wxxz() const; |
---|
387 | Vector4 xyxz() const; |
---|
388 | Vector4 yyxz() const; |
---|
389 | Vector4 zyxz() const; |
---|
390 | Vector4 wyxz() const; |
---|
391 | Vector4 xzxz() const; |
---|
392 | Vector4 yzxz() const; |
---|
393 | Vector4 zzxz() const; |
---|
394 | Vector4 wzxz() const; |
---|
395 | Vector4 xwxz() const; |
---|
396 | Vector4 ywxz() const; |
---|
397 | Vector4 zwxz() const; |
---|
398 | Vector4 wwxz() const; |
---|
399 | Vector4 xxyz() const; |
---|
400 | Vector4 yxyz() const; |
---|
401 | Vector4 zxyz() const; |
---|
402 | Vector4 wxyz() const; |
---|
403 | Vector4 xyyz() const; |
---|
404 | Vector4 yyyz() const; |
---|
405 | Vector4 zyyz() const; |
---|
406 | Vector4 wyyz() const; |
---|
407 | Vector4 xzyz() const; |
---|
408 | Vector4 yzyz() const; |
---|
409 | Vector4 zzyz() const; |
---|
410 | Vector4 wzyz() const; |
---|
411 | Vector4 xwyz() const; |
---|
412 | Vector4 ywyz() const; |
---|
413 | Vector4 zwyz() const; |
---|
414 | Vector4 wwyz() const; |
---|
415 | Vector4 xxzz() const; |
---|
416 | Vector4 yxzz() const; |
---|
417 | Vector4 zxzz() const; |
---|
418 | Vector4 wxzz() const; |
---|
419 | Vector4 xyzz() const; |
---|
420 | Vector4 yyzz() const; |
---|
421 | Vector4 zyzz() const; |
---|
422 | Vector4 wyzz() const; |
---|
423 | Vector4 xzzz() const; |
---|
424 | Vector4 yzzz() const; |
---|
425 | Vector4 zzzz() const; |
---|
426 | Vector4 wzzz() const; |
---|
427 | Vector4 xwzz() const; |
---|
428 | Vector4 ywzz() const; |
---|
429 | Vector4 zwzz() const; |
---|
430 | Vector4 wwzz() const; |
---|
431 | Vector4 xxwz() const; |
---|
432 | Vector4 yxwz() const; |
---|
433 | Vector4 zxwz() const; |
---|
434 | Vector4 wxwz() const; |
---|
435 | Vector4 xywz() const; |
---|
436 | Vector4 yywz() const; |
---|
437 | Vector4 zywz() const; |
---|
438 | Vector4 wywz() const; |
---|
439 | Vector4 xzwz() const; |
---|
440 | Vector4 yzwz() const; |
---|
441 | Vector4 zzwz() const; |
---|
442 | Vector4 wzwz() const; |
---|
443 | Vector4 xwwz() const; |
---|
444 | Vector4 ywwz() const; |
---|
445 | Vector4 zwwz() const; |
---|
446 | Vector4 wwwz() const; |
---|
447 | Vector4 xxxw() const; |
---|
448 | Vector4 yxxw() const; |
---|
449 | Vector4 zxxw() const; |
---|
450 | Vector4 wxxw() const; |
---|
451 | Vector4 xyxw() const; |
---|
452 | Vector4 yyxw() const; |
---|
453 | Vector4 zyxw() const; |
---|
454 | Vector4 wyxw() const; |
---|
455 | Vector4 xzxw() const; |
---|
456 | Vector4 yzxw() const; |
---|
457 | Vector4 zzxw() const; |
---|
458 | Vector4 wzxw() const; |
---|
459 | Vector4 xwxw() const; |
---|
460 | Vector4 ywxw() const; |
---|
461 | Vector4 zwxw() const; |
---|
462 | Vector4 wwxw() const; |
---|
463 | Vector4 xxyw() const; |
---|
464 | Vector4 yxyw() const; |
---|
465 | Vector4 zxyw() const; |
---|
466 | Vector4 wxyw() const; |
---|
467 | Vector4 xyyw() const; |
---|
468 | Vector4 yyyw() const; |
---|
469 | Vector4 zyyw() const; |
---|
470 | Vector4 wyyw() const; |
---|
471 | Vector4 xzyw() const; |
---|
472 | Vector4 yzyw() const; |
---|
473 | Vector4 zzyw() const; |
---|
474 | Vector4 wzyw() const; |
---|
475 | Vector4 xwyw() const; |
---|
476 | Vector4 ywyw() const; |
---|
477 | Vector4 zwyw() const; |
---|
478 | Vector4 wwyw() const; |
---|
479 | Vector4 xxzw() const; |
---|
480 | Vector4 yxzw() const; |
---|
481 | Vector4 zxzw() const; |
---|
482 | Vector4 wxzw() const; |
---|
483 | Vector4 xyzw() const; |
---|
484 | Vector4 yyzw() const; |
---|
485 | Vector4 zyzw() const; |
---|
486 | Vector4 wyzw() const; |
---|
487 | Vector4 xzzw() const; |
---|
488 | Vector4 yzzw() const; |
---|
489 | Vector4 zzzw() const; |
---|
490 | Vector4 wzzw() const; |
---|
491 | Vector4 xwzw() const; |
---|
492 | Vector4 ywzw() const; |
---|
493 | Vector4 zwzw() const; |
---|
494 | Vector4 wwzw() const; |
---|
495 | Vector4 xxww() const; |
---|
496 | Vector4 yxww() const; |
---|
497 | Vector4 zxww() const; |
---|
498 | Vector4 wxww() const; |
---|
499 | Vector4 xyww() const; |
---|
500 | Vector4 yyww() const; |
---|
501 | Vector4 zyww() const; |
---|
502 | Vector4 wyww() const; |
---|
503 | Vector4 xzww() const; |
---|
504 | Vector4 yzww() const; |
---|
505 | Vector4 zzww() const; |
---|
506 | Vector4 wzww() const; |
---|
507 | Vector4 xwww() const; |
---|
508 | Vector4 ywww() const; |
---|
509 | Vector4 zwww() const; |
---|
510 | Vector4 wwww() const; |
---|
511 | |
---|
512 | }; |
---|
513 | |
---|
514 | } |
---|
515 | |
---|
516 | inline G3D::Vector4 operator* (float s, const G3D::Vector4& v) { |
---|
517 | return v * s; |
---|
518 | } |
---|
519 | |
---|
520 | unsigned int hashCode(const G3D::Vector4& v); |
---|
521 | |
---|
522 | #include "Vector4.inl" |
---|
523 | |
---|
524 | #endif |
---|