source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h @ 3219

Revision 3219, 13.5 KB checked in by mattausch, 16 years ago (diff)
Line 
1#ifndef _Vector3_h__
2#define _Vector3_h__
3
4
5#include "common.h"
6#include <math.h>
7
8
9namespace CHCDemoEngine
10{
11
12// Forward-declare some other classes.
13class Matrix4x4;
14
15
16/** Vector class.
17*/
18class Vector3
19{
20public:
21
22        ///////////
23        //-- members;
24
25        float x;
26        float y;
27        float z;
28
29
30        /////////////////////////
31
32        void  SetX(float q) { x = q; }
33        void  SetY(float q) { y = q; }
34        void  SetZ(float q) { z = q; }
35
36        float GetX() const { return x; }
37        float GetY() const { return y; }
38        float GetZ() const { return z; }
39
40
41        //////////////
42        //-- constructors
43
44        Vector3() { }
45
46        Vector3(float x, float y, float z): x(x), y(y), z(z)
47        {}
48
49        explicit Vector3(float v): x(v), y(v), z(v){}
50        /** Copy constructor.
51        */
52        Vector3(const Vector3 &v): x(v.x), y(v.y), z(v.z)
53        {}
54
55        // Functions to get at the std::vector components
56        float& operator[] (const int inx)
57        {
58                return (&x)[inx];
59        }
60
61        operator const float*() const
62        {
63                return (const float*) this;
64        }
65
66        const float &operator[] (const int inx) const
67        {
68                return *(&x + inx);
69        }
70
71        void ExtractVerts(float *px, float *py, int which) const;
72 
73        void SetValue(float a, float b, float c)
74        { 
75                x = a; y = b; z = c;
76        }
77
78        void SetValue(float a)
79        {
80                x = y = z = a;
81        }
82 
83       
84        /** Returns the axis, where the std::vector has the largest value.
85        */
86        int DrivingAxis(void) const;
87        /** returns the axis, where the std::vector has the smallest value
88        */
89        int TinyAxis(void) const;
90        /** Returns largest component in this vector
91        */
92        inline float MaxComponent(void) const
93        {
94                return (x > y) ? ( (x > z) ? x : z) : ( (y > z) ? y : z);
95        }
96        /** Returns copy of this vector where all components are positiv.
97        */
98        inline Vector3 Abs(void) const
99        {
100                return Vector3(fabs(x), fabs(y), fabs(z));
101        }
102        /** normalizes the std::vector of unit size corresponding to given std::vector
103        */
104        inline void Normalize();
105        /** Returns false if this std::vector has a nan component.
106        */
107        bool CheckValidity() const;
108        /**
109        Find a right handed coordinate system with (*this) being
110        the z-axis. For a right-handed system, U x V = (*this) holds.
111        This implementation is here to avoid inconsistence and confusion
112        when construction coordinate systems using ArbitraryNormal():
113        In fact:
114        V = ArbitraryNormal(N);
115        U = CrossProd(V,N);
116        constructs a right-handed coordinate system as well, BUT:
117        1) bugs can be introduced if one mistakenly constructs a
118        left handed sytems e.g. by doing
119        U = ArbitraryNormal(N);
120        V = CrossProd(U,N);
121        2) this implementation gives non-negative base vectors
122        for (*this)==(0,0,1) |  (0,1,0) | (1,0,0), which is
123        good for debugging and is not the case with the implementation
124        using ArbitraryNormal()
125
126        ===> Using ArbitraryNormal() for constructing coord systems
127        is obsoleted by this method (<JK> 12/20/03).   
128        */
129        void RightHandedBase(Vector3& U, Vector3& V) const;
130       
131       
132        // Unary operators
133
134        Vector3 operator+ () const;
135        Vector3 operator- () const;
136
137        // Assignment operators
138       
139        Vector3& operator+= (const Vector3 &A);
140        Vector3& operator-= (const Vector3 &A);
141        Vector3& operator*= (const Vector3 &A);
142        Vector3& operator*= (float A);
143        Vector3& operator/= (float A);
144
145        static inline Vector3 UNIT_X() { return Vector3(1, 0, 0); }
146        static inline Vector3 UNIT_Y() { return Vector3(0, 1, 0); }
147        static inline Vector3 UNIT_Z() { return Vector3(0, 0, 1); }
148        static inline Vector3 ZERO() { return Vector3(0, 0, 0); }
149
150
151        //////////
152        //-- friends
153       
154        /**
155        ===> Using ArbitraryNormal() for constructing coord systems
156        ===> is obsoleted by RightHandedBase() method (<JK> 12/20/03).   
157
158        Return an arbitrary normal to `v'.
159        In fact it tries v x (0,0,1) an if the result is too small,
160        it definitely does v x (0,1,0). It will always work for
161        non-degenareted std::vector and is much faster than to use
162        TangentVectors.
163
164        @param v(in) The std::vector we want to find normal for.
165        @return The normal std::vector to v.
166        */
167        friend inline Vector3 ArbitraryNormal(const Vector3 &v);
168
169        /// Transforms a std::vector to the global coordinate frame.
170        /**
171        Given a local coordinate frame (U,V,N) (i.e. U,V,N are
172        the x,y,z axes of the local coordinate system) and
173        a std::vector 'loc' in the local coordiante system, this
174        function returns a the coordinates of the same std::vector
175        in global frame (i.e. frame (1,0,0), (0,1,0), (0,0,1).
176        */
177        friend inline Vector3 ToGlobalFrame(const Vector3& loc,
178                                                const Vector3& U,
179                                                                                const Vector3& V,
180                                                                                const Vector3& N);
181
182        /// Transforms a std::vector to a local coordinate frame.
183        /**
184        Given a local coordinate frame (U,V,N) (i.e. U,V,N are
185        the x,y,z axes of the local coordinate system) and
186        a std::vector 'loc' in the global coordiante system, this
187        function returns a the coordinates of the same std::vector
188        in the local frame.
189        */
190        friend inline Vector3 ToLocalFrame(const Vector3& loc,
191                                           const Vector3& U,
192                                                                           const Vector3& V,
193                                                                           const Vector3& N);
194
195        /// the magnitude=size of the std::vector
196        friend inline float Magnitude(const Vector3 &v);
197        /// the squared magnitude of the std::vector .. for efficiency in some cases
198        friend inline float SqrMagnitude(const Vector3 &v);
199        /// Magnitude(v1-v2)
200        friend inline float Distance(const Vector3 &v1, const Vector3 &v2);
201        /// SqrMagnitude(v1-v2)
202        friend inline float SqrDistance(const Vector3 &v1, const Vector3 &v2);
203        /// creates the std::vector of unit size corresponding to given std::vector
204        friend inline Vector3 Normalize(const Vector3 &A);
205        /// // Rotate a direction vector
206        friend Vector3 PlaneRotate(const Matrix4x4 &, const Vector3 &);
207
208        // construct view vectors .. DirAt is the main viewing direction
209        // Viewer is the coordinates of viewer location, UpL is the std::vector.
210        friend void ViewVectors(const Vector3 &DirAt,
211                                                        const Vector3 &Viewer,
212                                                        const Vector3 &UpL,
213                                                        Vector3 &ViewV,
214                                                        Vector3 &ViewU,
215                                                        Vector3 &ViewN);
216
217        // Given the intersection point `P', you have available normal `N'
218        // of unit length. Let us suppose the incoming ray has direction `D'.
219        // Then we can construct such two vectors `U' and `V' that
220        // `U',`N', and `D' are coplanar, and `V' is perpendicular
221        // to the vectors `N','D', and `V'. Then 'N', 'U', and 'V' create
222        // the orthonormal base in space R3.
223        friend void TangentVectors(Vector3 &U, Vector3 &V, // output
224                                                           const Vector3 &normal, // input
225                                                           const Vector3 &dirIncoming);
226
227
228        // Binary operators
229       
230        friend inline Vector3 operator+ (const Vector3 &A, const Vector3 &B);
231        friend inline Vector3 operator- (const Vector3 &A, const Vector3 &B);
232        friend inline Vector3 operator* (const Vector3 &A, const Vector3 &B);
233        friend inline Vector3 operator* (const Vector3 &A, float B);
234        friend inline Vector3 operator* (float A, const Vector3 &B);
235        friend Vector3 operator* (const Matrix4x4 &, const Vector3 &);
236        friend inline Vector3 operator/ (const Vector3 &A, const Vector3 &B);
237
238        friend inline int operator< (const Vector3 &A, const Vector3 &B);
239        friend inline int operator<= (const Vector3 &A, const Vector3 &B);
240
241        friend inline Vector3 operator/ (const Vector3 &A, float B);
242        friend inline int operator== (const Vector3 &A, const Vector3 &B);
243        friend inline float DotProd(const Vector3 &A, const Vector3 &B);
244        friend inline Vector3 CrossProd (const Vector3 &A, const Vector3 &B);
245
246        friend std::ostream& operator<< (std::ostream &s, const Vector3 &A);
247        friend std::istream& operator>> (std::istream &s, Vector3 &A);
248
249        friend void Minimize(Vector3 &min, const Vector3 &candidate);
250        friend void Maximize(Vector3 &max, const Vector3 &candidate);
251
252        friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr);
253        friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2);
254
255        friend Vector3 CosineRandomVector(const Vector3 &normal);
256        friend Vector3 UniformRandomVector(const Vector3 &normal);
257        friend Vector3 UniformRandomVector();
258
259        friend Vector3 UniformRandomVector(float r1, float r2);
260
261        friend Vector3 CosineRandomVector(float r1, float r2, const Vector3 &normal);
262};
263
264
265
266// forward declaration
267extern Vector3 UniformRandomVector(const Vector3 &normal);
268extern Vector3 UniformRandomVector();
269extern Vector3 UniformRandomVector(const float r1, const float r2);
270
271inline Vector3 ArbitraryNormal(const Vector3 &N)
272{
273        float dist2 = N.x * N.x + N.y * N.y;
274
275        if (dist2 > 0.0001f)
276        {
277                float inv_size = 1.0f / sqrtf(dist2);
278                return Vector3(N.y * inv_size, -N.x * inv_size, 0); // N x (0,0,1)
279        }
280
281        float inv_size = 1.0f / sqrtf(N.z * N.z + N.x * N.x);
282        return Vector3(-N.z * inv_size, 0, N.x * inv_size); // N x (0,1,0)
283}
284
285
286inline void Vector3::RightHandedBase(Vector3& U, Vector3& V) const
287{
288        // HACK
289        V = ArbitraryNormal(*this);
290        U = CrossProd(V, *this);
291}
292
293
294inline Vector3 ToGlobalFrame(const Vector3 &loc,
295                                                         const Vector3 &U,
296                                                         const Vector3 &V,
297                                                         const Vector3 &N)
298{
299        return loc.x * U + loc.y * V + loc.z * N;
300}
301
302
303inline Vector3 ToLocalFrame(const Vector3 &loc,
304                                                        const Vector3 &U,
305                                                        const Vector3 &V,
306                                                        const Vector3 &N)
307{
308        return Vector3(loc.x * U.x + loc.y * U.y + loc.z * U.z,
309                                   loc.x * V.x + loc.y * V.y + loc.z * V.z,
310                                   loc.x * N.x + loc.y * N.y + loc.z * N.z);
311}
312
313
314inline float Magnitude(const Vector3 &v)
315{
316        return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
317}
318
319
320inline float SqrMagnitude(const Vector3 &v)
321{
322        return v.x * v.x + v.y * v.y + v.z * v.z;
323}
324
325
326inline float Distance(const Vector3 &v1, const Vector3 &v2)
327{
328        //return sqrtf(sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z));
329        return Magnitude(v1 - v2);
330}
331
332
333inline float SqrDistance(const Vector3 &v1, const Vector3 &v2)
334{
335        //return sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z);
336        return SqrMagnitude(v1 - v2);
337}
338
339
340inline Vector3 Normalize(const Vector3 &A)
341{
342        return A * (1.0f / Magnitude(A));
343}
344
345
346inline float DotProd(const Vector3 &A, const Vector3 &B)
347{
348        return A.x * B.x + A.y * B.y + A.z * B.z;
349}
350
351
352// angle between two vectors with respect to a surface normal in the
353// range [0 .. 2 * pi]
354inline float Angle(const Vector3 &A, const Vector3 &B, const Vector3 &norm)
355{
356        Vector3 cross = CrossProd(A, B);
357
358        float signedAngle;
359
360        if (DotProd(cross, norm) > 0)
361                signedAngle = atan2(-Magnitude(CrossProd(A, B)), DotProd(A, B));
362        else
363                signedAngle = atan2(Magnitude(CrossProd(A, B)), DotProd(A, B));
364
365        if (signedAngle < 0)
366                return 2 * M_PI + signedAngle;
367
368        return signedAngle;
369}
370
371
372inline Vector3 Vector3::operator+() const
373{
374        return *this;
375}
376
377
378inline Vector3 Vector3::operator-() const
379{
380  return Vector3(-x, -y, -z);
381}
382
383
384inline Vector3 &Vector3::operator+=(const Vector3 &A)
385{
386        x += A.x;  y += A.y;  z += A.z;
387        return *this;
388}
389
390
391inline Vector3& Vector3::operator-=(const Vector3 &A)
392{
393        x -= A.x;  y -= A.y;  z -= A.z;
394       
395        return *this;
396}
397
398
399inline Vector3& Vector3::operator*= (float A)
400{
401        x *= A;  y *= A;  z *= A;
402        return *this;
403}
404
405
406inline Vector3& Vector3::operator/=(float A)
407{
408        float a = 1.0f / A;
409       
410        x *= a;  y *= a;  z *= a;
411       
412        return *this;
413}
414
415
416inline Vector3& Vector3::operator*= (const Vector3 &A)
417{
418  x *= A.x;  y *= A.y;  z *= A.z;
419  return *this;
420}
421
422
423inline Vector3 operator+ (const Vector3 &A, const Vector3 &B)
424{
425        return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
426}
427
428
429inline Vector3 operator- (const Vector3 &A, const Vector3 &B)
430{
431        return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
432}
433
434
435inline Vector3 operator* (const Vector3 &A, const Vector3 &B)
436{
437        return Vector3(A.x * B.x, A.y * B.y, A.z * B.z);
438}
439
440
441inline Vector3 operator* (const Vector3 &A, float B)
442{
443  return Vector3(A.x * B, A.y * B, A.z * B);
444}
445
446
447inline Vector3 operator* (float A, const Vector3 &B)
448{
449        return Vector3(B.x * A, B.y * A, B.z * A);
450}
451
452
453inline Vector3 operator/ (const Vector3 &A, const Vector3 &B)
454{
455        return Vector3(A.x / B.x, A.y / B.y, A.z / B.z);
456}
457
458
459inline Vector3 operator/ (const Vector3 &A, float B)
460{
461        float b = 1.0f / B;
462        return Vector3(A.x * b, A.y * b, A.z * b);
463}
464
465
466inline int operator< (const Vector3 &A, const Vector3 &B)
467{
468        return A.x < B.x && A.y < B.y && A.z < B.z;
469}
470
471
472inline int operator<= (const Vector3 &A, const Vector3 &B)
473{
474        return A.x <= B.x && A.y <= B.y && A.z <= B.z;
475}
476
477
478// Might replace floating-point == with comparisons of
479// magnitudes, if needed.
480inline int operator== (const Vector3 &A, const Vector3 &B)
481{
482        return (A.x == B.x) && (A.y == B.y) && (A.z == B.z);
483}
484
485
486inline Vector3 CrossProd (const Vector3 &A, const Vector3 &B)
487{
488        return Vector3(A.y * B.z - A.z * B.y,
489                           A.z * B.x - A.x * B.z,
490                                   A.x * B.y - A.y * B.x);
491}
492
493
494inline void Vector3::Normalize()
495{
496        float sqrmag = x * x + y * y + z * z;
497       
498        if (sqrmag > 0.0f)
499                (*this) *= 1.0f / sqrtf(sqrmag);
500}
501
502
503// Overload << operator for C++-style output
504inline std::ostream& operator<< (std::ostream &s, const Vector3 &A)
505{
506        return s << "(" << A.x << ", " << A.y << ", " << A.z << ")";
507}
508
509
510// Overload >> operator for C++-style input
511inline std::istream& operator>> (std::istream &s, Vector3 &A)
512{
513        char a;
514        // read "(x, y, z)"
515        return s >> a >> A.x >> a >> A.y >> a >> A.z >> a;
516        //return s >> A.x >> A.y >> A.z;
517}
518
519
520inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr)
521{
522        if (fabsf(v1.x-v2.x) > thr)
523                return false;
524        if (fabsf(v1.y-v2.y) > thr)
525                return false;
526        if (fabsf(v1.z-v2.z) > thr)
527                return false;
528       
529        return true;
530}
531
532
533inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2)
534{
535        return EpsilonEqualV3(v1, v2, Limits::Small);
536}
537
538
539}
540
541#endif
Note: See TracBrowser for help on using the repository browser.