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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
|
/**
\file Vector3.h
3D vector class
\maintainer Morgan McGuire, http://graphics.cs.williams.edu
\created 2001-06-02
\edited 2010-12-25
Copyright 2000-2012, Morgan McGuire.
All rights reserved.
*/
#ifndef G3D_Vector3_h
#define G3D_Vector3_h
#include "G3D/platform.h"
#include "G3D/g3dmath.h"
#include "G3D/Vector2.h"
#include "G3D/HashTrait.h"
#include "G3D/PositionTrait.h"
#include "G3D/Vector2.h"
#include <iostream>
#include <string>
namespace G3D {
class Vector2;
class Vector4;
class Vector4int8;
class Vector3int32;
class Any;
/**
<B>Swizzles</B>
Vector classes have swizzle operators, e.g. <CODE>v.xy()</CODE>, that
allow selection of arbitrary sub-fields. These cannot be used as write
masks. Examples
<PRE>
Vector3 v(1, 2, 3);
Vector3 j;
Vector2 b;
b = v.xz();
j = b.xx();
</PRE>
<B>Warning</B>
Do not subclass-- this implementation makes assumptions about the
memory layout.
*/
class Vector3 {
public:
// coordinates
float x, y, z;
private:
// Hidden operators
bool operator<(const Vector3&) const;
bool operator>(const Vector3&) const;
bool operator<=(const Vector3&) const;
bool operator>=(const Vector3&) const;
public:
/** Initializes to zero */
Vector3();
/**
\param any Must either Vector3(#, #, #) or Vector3 {x = #, y = #, z = #}.
Because Point3 is a typedef for Vector3 in the current implementation,
this constructor accepts Point3(#, #, #), etc. as well.
*/
explicit Vector3(const Any& any);
/** Converts the Vector3 to an Any, using the specified \a name instead of "Vector3" */
Any toAny(const std::string& name) const;
/** Converts the Vector3 to an Any. */
Any toAny() const;
/** Divides by 127 */
Vector3(const Vector4int8&);
Vector3(const class Vector2& v, float z);
Vector3(const class Vector3int32& v);
explicit Vector3(class BinaryInput& b);
Vector3(float _x, float _y, float _z);
explicit Vector3(float coordinate[3]);
explicit Vector3(double coordinate[3]);
Vector3(const class Vector3int16& v);
explicit Vector3(class TextInput& t);
explicit Vector3(const class Color3& c);
/** Format is three float32's */
void serialize(class BinaryOutput& b) const;
void deserialize(class BinaryInput& b);
/** Format is "(%f, %f, %f)" */
void serialize(class TextOutput& t) const;
void deserialize(class TextInput& t);
// access vector V as V[0] = V.x, V[1] = V.y, V[2] = V.z
//
// WARNING. These member functions rely on
// (1) Vector3 not having virtual functions
// (2) the data packed in a 3*sizeof(float) memory block
const float& __fastcall operator[] (int i) const;
float& operator[] (int i);
bool nonZero() const {
return (x != 0) || (y != 0) || (z != 0);
}
enum Axis {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, DETECT_AXIS=-1};
/**
Returns the largest dimension. Particularly convenient for determining
which plane to project a triangle onto for point-in-polygon tests.
*/
Axis primaryAxis() const;
// assignment and comparison
Vector3& operator=(const Any& a);
bool operator== (const Vector3& rkVector) const;
bool operator!= (const Vector3& rkVector) const;
size_t hashCode() const;
bool fuzzyEq(const Vector3& other) const;
bool fuzzyNe(const Vector3& other) const;
/** Returns true if this vector has finite length. */
bool isFinite() const;
/** True if any field is nan */
bool isNaN() const;
/** Returns true if this vector has length ~= 0 */
bool isZero() const;
/** Returns true if this vector has length ~= 1 */
bool isUnit() const;
/** Returns a vector that is \a this translated towards \a goal with a maximum translation of \a maxTranslation. */
Vector3 movedTowards(const Vector3& goal, float maxTranslation) const;
void moveTowards(const Vector3& goal, float maxTranslation);
// arithmetic operations
Vector3 __fastcall operator+ (const Vector3& v) const;
Vector3 __fastcall operator- (const Vector3& v) const;
Vector3 __fastcall operator* (float s) const;
inline Vector3 __fastcall operator/ (float s) const {
return *this * (1.0f / s);
}
Vector3 __fastcall operator* (const Vector3& v) const;
Vector3 __fastcall operator/ (const Vector3& v) const;
Vector3 __fastcall operator- () const;
// arithmetic updates
Vector3& __fastcall operator+= (const Vector3& v);
Vector3& __fastcall operator-= (const Vector3& v);
Vector3& __fastcall operator*= (float s);
inline Vector3& __fastcall operator/= (float s) {
return (*this *= (1.0f / s));
}
Vector3& __fastcall operator*= (const Vector3& v);
Vector3& __fastcall operator/= (const Vector3& v);
/** Same as magnitude */
float length() const;
float magnitude() const;
/** Raise each component of this vector to a power */
Vector3 pow(float p) const {
return Vector3(powf(x, p), powf(y, p), powf(z, p));
}
/**
Returns a unit-length version of this vector.
Returns nan if length is almost zero.
*/
Vector3 direction() const;
/**
Potentially less accurate but faster than direction().
Only works if System::hasSSE is true.
*/
Vector3 fastDirection() const;
/**
Reflect this vector about the (not necessarily unit) normal.
Assumes that both the before and after vectors point away from
the base of the normal.
Note that if used for a collision or ray reflection you
must negate the resulting vector to get a direction pointing
<I>away</I> from the collision.
<PRE>
V' N V
r ^ -,
\ | /
\|/
</PRE>
See also Vector3::reflectionDirection
*/
Vector3 reflectAbout(const Vector3& normal) const;
/**
See also G3D::Ray::reflect.
The length is 1.
<PRE>
V' N V
r ^ /
\ | /
\|'-
</PRE>
*/
Vector3 reflectionDirection(const Vector3& normal) const;
/**
Returns Vector3::zero() if the length is nearly zero, otherwise
returns a unit vector.
*/
inline Vector3 directionOrZero() const {
float mag = magnitude();
if (mag < 0.0000001f) {
return Vector3::zero();
} else if (mag < 1.00001f && mag > 0.99999f) {
return *this;
} else {
return *this * (1.0f / mag);
}
}
/**
Returns the direction of a refracted ray,
where iExit is the index of refraction for the
previous material and iEnter is the index of refraction
for the new material. Like Vector3::reflectionDirection,
the result has length 1 and is
pointed <I>away</I> from the intersection.
Returns Vector3::zero() in the case of total internal refraction.
@param iOutside The index of refraction (eta) outside
(on the <I>positive</I> normal side) of the surface.
@param iInside The index of refraction (eta) inside
(on the <I>negative</I> normal side) of the surface.
See also G3D::Ray::refract.
<PRE>
N V
^ /
| /
|'-
__--
V'<--
</PRE>
*/
Vector3 refractionDirection(
const Vector3& normal,
float iInside,
float iOutside) const;
/** Synonym for direction */
inline Vector3 unit() const {
return direction();
}
/** Returns a normalized vector. May be computed with lower
precision than unit */
inline Vector3 fastUnit() const {
return fastDirection();
}
/** Same as squaredMagnitude */
float squaredLength() const;
float squaredMagnitude () const;
float __fastcall dot(const Vector3& rkVector) const;
/** Cross product. Note that two cross products in a row
can be computed more cheaply: v1 x (v2 x v3) = (v1 dot v3) v2 - (v1 dot v2) v3.
*/
Vector3 __fastcall cross(const Vector3& rkVector) const;
Vector3 unitCross(const Vector3& rkVector) const;
/**
Returns a matrix such that v.cross() * w = v.cross(w).
<PRE>
[ 0 -v.z v.y ]
[ v.z 0 -v.x ]
[ -v.y v.x 0 ]
</PRE>
*/
class Matrix3 cross() const;
Vector3 __fastcall min(const Vector3 &v) const;
Vector3 __fastcall max(const Vector3 &v) const;
/** Smallest element */
inline float min() const {
return G3D::min(G3D::min(x, y), z);
}
/** Largest element */
inline float max() const {
return G3D::max(G3D::max(x, y), z);
}
std::string toString() const;
inline Vector3 clamp(const Vector3& low, const Vector3& high) const {
return Vector3(
G3D::clamp(x, low.x, high.x),
G3D::clamp(y, low.y, high.y),
G3D::clamp(z, low.z, high.z));
}
inline Vector3 clamp(float low, float high) const {
return Vector3(
G3D::clamp(x, low, high),
G3D::clamp(y, low, high),
G3D::clamp(z, low, high));
}
inline Vector3 floor() const {
return G3D::Vector3(::floor(x), ::floor(y), ::floor(z));
}
inline Vector3 round() const {
return Vector3(G3D::round(x), G3D::round(y), G3D::round(z));
}
/**
Linear interpolation
*/
inline Vector3 lerp(const Vector3& v, float alpha) const {
return (*this) + (v - *this) * alpha;
}
/** Gram-Schmidt orthonormalization. */
static void orthonormalize (Vector3 akVector[3]);
/** \brief Random unit vector, uniformly distributed on the sphere.
Distribution rendered by G3D::DirectionHistogram:
\image html vector3-random.png
*/
static Vector3 random(Random& r = commonRandom());
/** \brief Random unit vector, distributed according to \f$\max(\cos \theta,0)\f$.
That is, so that the probability of \f$\vec{V}\f$ is proportional
to \f$\max(\vec{v} \cdot \vec{n}, 0)\f$. Useful in photon mapping for
Lambertian scattering.
Distribution rendered by G3D::DirectionHistogram:
\image html vector3-coshemirandom.png
\param n Unit vector at the center of the distribution.
@cite Henrik Wann Jensen, Realistic Image Synthesis using Photon Mapping eqn 2.24
*/
static Vector3 cosHemiRandom(const Vector3& n, Random& r = commonRandom());
static Vector3 cosSphereRandom(const Vector3& n, Random& r = commonRandom());
/** \brief Random unit vector, distributed according to \f$\max(\cos^k \theta,0)\f$.
That is, so that the probability of \f$\vec{V}\f$ is
proportional to \f$\max((\vec{v} \cdot \vec{n})^k, 0)\f$.
Useful in photon mapping for glossy scattering.
Distribution rendered by G3D::DirectionHistogram:
\image html vector3-cospowhemirandom.png
\param n Unit vector at the center of the distribution.
@cite Ashikhmin and Shirley, An anisotropic Phong BRDF model, Journal of Graphics Tools, 2002
*/
static Vector3 cosPowHemiRandom(const Vector3& n, const float k, Random& r = commonRandom());
/**
\brief Random vector distributed over the hemisphere about normal.
Distribution rendered by G3D::DirectionHistogram:
\image html vector3-hemirandom.png
*/
static Vector3 hemiRandom(const Vector3& normal, Random& r = commonRandom());
inline float sum() const {
return x + y + z;
}
inline float average() const {
return sum() / 3.0f;
}
// Special values.
static const Vector3& zero();
static const Vector3& one();
static const Vector3& unitX();
static const Vector3& unitY();
static const Vector3& unitZ();
static const Vector3& inf();
static const Vector3& nan();
/** Smallest (most negative) representable vector */
static const Vector3& minFinite();
/** Largest representable vector */
static const Vector3& maxFinite();
/** Creates two orthonormal tangent vectors X and Y such that
if Z = this, X x Y = Z.*/
inline void getTangents(Vector3& X, Vector3& Y) const {
debugAssertM(G3D::fuzzyEq(length(), 1.0f),
"makeAxes requires Z to have unit length");
// Choose another vector not perpendicular
X = (abs(x) < 0.9f) ? Vector3::unitX() : Vector3::unitY();
// Remove the part that is parallel to Z
X -= *this * this->dot(X);
X /= X.length();
Y = this->cross(X);
}
// 2-char swizzles
Vector2 xx() const;
Vector2 yx() const;
Vector2 zx() const;
Vector2 xy() const;
Vector2 yy() const;
Vector2 zy() const;
Vector2 xz() const;
Vector2 yz() const;
Vector2 zz() const;
// 3-char swizzles
Vector3 xxx() const;
Vector3 yxx() const;
Vector3 zxx() const;
Vector3 xyx() const;
Vector3 yyx() const;
Vector3 zyx() const;
Vector3 xzx() const;
Vector3 yzx() const;
Vector3 zzx() const;
Vector3 xxy() const;
Vector3 yxy() const;
Vector3 zxy() const;
Vector3 xyy() const;
Vector3 yyy() const;
Vector3 zyy() const;
Vector3 xzy() const;
Vector3 yzy() const;
Vector3 zzy() const;
Vector3 xxz() const;
Vector3 yxz() const;
Vector3 zxz() const;
Vector3 xyz() const;
Vector3 yyz() const;
Vector3 zyz() const;
Vector3 xzz() const;
Vector3 yzz() const;
Vector3 zzz() const;
// 4-char swizzles
Vector4 xxxx() const;
Vector4 yxxx() const;
Vector4 zxxx() const;
Vector4 xyxx() const;
Vector4 yyxx() const;
Vector4 zyxx() const;
Vector4 xzxx() const;
Vector4 yzxx() const;
Vector4 zzxx() const;
Vector4 xxyx() const;
Vector4 yxyx() const;
Vector4 zxyx() const;
Vector4 xyyx() const;
Vector4 yyyx() const;
Vector4 zyyx() const;
Vector4 xzyx() const;
Vector4 yzyx() const;
Vector4 zzyx() const;
Vector4 xxzx() const;
Vector4 yxzx() const;
Vector4 zxzx() const;
Vector4 xyzx() const;
Vector4 yyzx() const;
Vector4 zyzx() const;
Vector4 xzzx() const;
Vector4 yzzx() const;
Vector4 zzzx() const;
Vector4 xxxy() const;
Vector4 yxxy() const;
Vector4 zxxy() const;
Vector4 xyxy() const;
Vector4 yyxy() const;
Vector4 zyxy() const;
Vector4 xzxy() const;
Vector4 yzxy() const;
Vector4 zzxy() const;
Vector4 xxyy() const;
Vector4 yxyy() const;
Vector4 zxyy() const;
Vector4 xyyy() const;
Vector4 yyyy() const;
Vector4 zyyy() const;
Vector4 xzyy() const;
Vector4 yzyy() const;
Vector4 zzyy() const;
Vector4 xxzy() const;
Vector4 yxzy() const;
Vector4 zxzy() const;
Vector4 xyzy() const;
Vector4 yyzy() const;
Vector4 zyzy() const;
Vector4 xzzy() const;
Vector4 yzzy() const;
Vector4 zzzy() const;
Vector4 xxxz() const;
Vector4 yxxz() const;
Vector4 zxxz() const;
Vector4 xyxz() const;
Vector4 yyxz() const;
Vector4 zyxz() const;
Vector4 xzxz() const;
Vector4 yzxz() const;
Vector4 zzxz() const;
Vector4 xxyz() const;
Vector4 yxyz() const;
Vector4 zxyz() const;
Vector4 xyyz() const;
Vector4 yyyz() const;
Vector4 zyyz() const;
Vector4 xzyz() const;
Vector4 yzyz() const;
Vector4 zzyz() const;
Vector4 xxzz() const;
Vector4 yxzz() const;
Vector4 zxzz() const;
Vector4 xyzz() const;
Vector4 yyzz() const;
Vector4 zyzz() const;
Vector4 xzzz() const;
Vector4 yzzz() const;
Vector4 zzzz() const;
/** Can be passed to ignore a vector3 parameter */
static Vector3& ignore();
};
inline G3D::Vector3 operator*(float s, const G3D::Vector3& v) {
return v * s;
}
inline G3D::Vector3 operator*(double s, const G3D::Vector3& v) {
return v * (float)s;
}
inline G3D::Vector3 operator*(int s, const G3D::Vector3& v) {
return v * (float)s;
}
std::ostream& operator<<(std::ostream& os, const Vector3&);
void serialize(const Vector3::Axis& a, class BinaryOutput& bo);
void deserialize(Vector3::Axis& a, class BinaryInput& bo);
//----------------------------------------------------------------------------
inline Vector3::Vector3() : x(0.0f), y(0.0f), z(0.0f) {
}
//----------------------------------------------------------------------------
inline Vector3::Vector3 (float fX, float fY, float fZ) : x(fX), y(fY), z(fZ) {
}
//----------------------------------------------------------------------------
inline Vector3::Vector3 (float V[3]) : x(V[0]), y(V[1]), z(V[2]){
}
//----------------------------------------------------------------------------
inline Vector3::Vector3 (double V[3]) : x((float)V[0]), y((float)V[1]), z((float)V[2]){
}
//----------------------------------------------------------------------------
inline const float& Vector3::operator[] (int i) const {
return ((float*)this)[i];
}
inline float& Vector3::operator[] (int i) {
return ((float*)this)[i];
}
//----------------------------------------------------------------------------
inline bool Vector3::fuzzyEq(const Vector3& other) const {
return G3D::fuzzyEq((*this - other).squaredMagnitude(), 0);
}
//----------------------------------------------------------------------------
inline bool Vector3::fuzzyNe(const Vector3& other) const {
return G3D::fuzzyNe((*this - other).squaredMagnitude(), 0);
}
//----------------------------------------------------------------------------
inline bool Vector3::isFinite() const {
return G3D::isFinite(x) && G3D::isFinite(y) && G3D::isFinite(z);
}
//----------------------------------------------------------------------------
inline bool Vector3::operator== (const Vector3& rkVector) const {
return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
}
//----------------------------------------------------------------------------
inline bool Vector3::operator!= (const Vector3& rkVector) const {
return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::operator+ (const Vector3& rkVector) const {
return Vector3(x + rkVector.x, y + rkVector.y, z + rkVector.z);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::operator- (const Vector3& rkVector) const {
return Vector3(x - rkVector.x, y - rkVector.y, z - rkVector.z);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::operator* (const Vector3& rkVector) const {
return Vector3(x * rkVector.x, y * rkVector.y, z * rkVector.z);
}
inline Vector3 Vector3::operator*(float f) const {
return Vector3(x * f, y * f, z * f);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::operator/ (const Vector3& rkVector) const {
return Vector3(x / rkVector.x, y / rkVector.y, z / rkVector.z);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::operator- () const {
return Vector3(-x, -y, -z);
}
//----------------------------------------------------------------------------
inline Vector3& Vector3::operator+= (const Vector3& rkVector) {
x += rkVector.x;
y += rkVector.y;
z += rkVector.z;
return *this;
}
//----------------------------------------------------------------------------
inline Vector3& Vector3::operator-= (const Vector3& rkVector) {
x -= rkVector.x;
y -= rkVector.y;
z -= rkVector.z;
return *this;
}
//----------------------------------------------------------------------------
inline Vector3& Vector3::operator*= (float fScalar) {
x *= fScalar;
y *= fScalar;
z *= fScalar;
return *this;
}
//----------------------------------------------------------------------------
inline Vector3& Vector3::operator*= (const Vector3& rkVector) {
x *= rkVector.x;
y *= rkVector.y;
z *= rkVector.z;
return *this;
}
//----------------------------------------------------------------------------
inline Vector3& Vector3::operator/= (const Vector3& rkVector) {
x /= rkVector.x;
y /= rkVector.y;
z /= rkVector.z;
return *this;
}
//----------------------------------------------------------------------------
inline float Vector3::squaredMagnitude () const {
return x*x + y*y + z*z;
}
//----------------------------------------------------------------------------
inline float Vector3::squaredLength () const {
return squaredMagnitude();
}
//----------------------------------------------------------------------------
inline float Vector3::magnitude() const {
return ::sqrtf(x*x + y*y + z*z);
}
//----------------------------------------------------------------------------
inline float Vector3::length() const {
return magnitude();
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::direction () const {
const float lenSquared = squaredMagnitude();
const float invSqrt = 1.0f / sqrtf(lenSquared);
return Vector3(x * invSqrt, y * invSqrt, z * invSqrt);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::fastDirection () const {
float lenSquared = x * x + y * y + z * z;
float invSqrt = rsq(lenSquared);
return Vector3(x * invSqrt, y * invSqrt, z * invSqrt);
}
//----------------------------------------------------------------------------
inline float Vector3::dot (const Vector3& rkVector) const {
return x*rkVector.x + y*rkVector.y + z*rkVector.z;
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::cross (const Vector3& rkVector) const {
return Vector3(y*rkVector.z - z*rkVector.y, z*rkVector.x - x*rkVector.z,
x*rkVector.y - y*rkVector.x);
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::unitCross (const Vector3& rkVector) const {
Vector3 kCross(y*rkVector.z - z*rkVector.y, z*rkVector.x - x*rkVector.z,
x*rkVector.y - y*rkVector.x);
return kCross.direction();
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::min(const Vector3 &v) const {
return Vector3(G3D::min(v.x, x), G3D::min(v.y, y), G3D::min(v.z, z));
}
//----------------------------------------------------------------------------
inline Vector3 Vector3::max(const Vector3 &v) const {
return Vector3(G3D::max(v.x, x), G3D::max(v.y, y), G3D::max(v.z, z));
}
//----------------------------------------------------------------------------
inline bool Vector3::isZero() const {
return G3D::fuzzyEq(fabsf(x) + fabsf(y) + fabsf(z), 0.0f);
}
//----------------------------------------------------------------------------
inline bool Vector3::isUnit() const {
return G3D::fuzzyEq(squaredMagnitude(), 1.0f);
}
/**
Points are technically distinct mathematical entities from vectors.
Actually distinguishing them at the class level tends to add lots of
boilerplate (e.g., (P - Point3::zero()).direction()
vs. P.direction()), so many programmers prefer use a single class,
as GLSL does.
G3D provides this typedef as a way of documenting arguments that are
locations in space and not directions. Beware that points and
vectors are interchangable from the compiler's point of view, and
that the programmer must track which is really which. */
typedef Vector3 Point3;
void serialize(const Vector3& v, class BinaryOutput& b);
void deserialize(Vector3& v, class BinaryInput& b);
} // namespace G3D
template <>
struct HashTrait<G3D::Vector3> {
static size_t hashCode(const G3D::Vector3& key) {
return key.hashCode();
}
};
template<> struct PositionTrait<class G3D::Vector2> {
static void getPosition(const G3D::Vector2& v, G3D::Vector3& p) { p = G3D::Vector3(v, 0); }
};
template<> struct PositionTrait<class G3D::Vector3> {
static void getPosition(const G3D::Vector3& v, G3D::Vector3& p) { p = v; }
};
#endif
|