source: GTP/trunk/App/Demos/Vis/CHC_revisited/Vector3.h @ 2756

Revision 2756, 13.2 KB checked in by mattausch, 17 years ago (diff)
Line 
1#ifndef _Vector3_h__
2#define _Vector3_h__
3
4#include <iostream>
5#include "common.h"
6#include <math.h>
7
8
9namespace CHCDemo
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        Vector3(float v): x(v), y(v), z(v)
50        {}
51        /** Copy constructor.
52        */
53        Vector3(const Vector3 &v): x(v.x), y(v.y), z(v.z)
54        {}
55
56        // Functions to get at the std::vector components
57        float& operator[] (const int inx)
58        {
59                return (&x)[inx];
60        }
61
62        operator const float*() const
63        {
64                return (const float*) this;
65        }
66
67        const float &operator[] (const int inx) const
68        {
69                return *(&x + inx);
70        }
71
72        void ExtractVerts(float *px, float *py, int which) const;
73 
74        void SetValue(float a, float b, float c)
75        { 
76                x = a; y = b; z = c;
77        }
78
79        void SetValue(float a)
80        {
81                x = y = z = a;
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
146        //////////
147        //-- friends
148       
149        /**
150        ===> Using ArbitraryNormal() for constructing coord systems
151        ===> is obsoleted by RightHandedBase() method (<JK> 12/20/03).   
152
153        Return an arbitrary normal to `v'.
154        In fact it tries v x (0,0,1) an if the result is too small,
155        it definitely does v x (0,1,0). It will always work for
156        non-degenareted std::vector and is much faster than to use
157        TangentVectors.
158
159        @param v(in) The std::vector we want to find normal for.
160        @return The normal std::vector to v.
161        */
162        friend inline Vector3 ArbitraryNormal(const Vector3 &v);
163
164        /// Transforms a std::vector to the global coordinate frame.
165        /**
166        Given a local coordinate frame (U,V,N) (i.e. U,V,N are
167        the x,y,z axes of the local coordinate system) and
168        a std::vector 'loc' in the local coordiante system, this
169        function returns a the coordinates of the same std::vector
170        in global frame (i.e. frame (1,0,0), (0,1,0), (0,0,1).
171        */
172        friend inline Vector3 ToGlobalFrame(const Vector3& loc,
173                                                const Vector3& U,
174                                                                                const Vector3& V,
175                                                                                const Vector3& N);
176
177        /// Transforms a std::vector to a local coordinate frame.
178        /**
179        Given a local coordinate frame (U,V,N) (i.e. U,V,N are
180        the x,y,z axes of the local coordinate system) and
181        a std::vector 'loc' in the global coordiante system, this
182        function returns a the coordinates of the same std::vector
183        in the local frame.
184        */
185        friend inline Vector3 ToLocalFrame(const Vector3& loc,
186                                           const Vector3& U,
187                                                                           const Vector3& V,
188                                                                           const Vector3& N);
189
190        /// the magnitude=size of the std::vector
191        friend inline float Magnitude(const Vector3 &v);
192        /// the squared magnitude of the std::vector .. for efficiency in some cases
193        friend inline float SqrMagnitude(const Vector3 &v);
194        /// Magnitude(v1-v2)
195        friend inline float Distance(const Vector3 &v1, const Vector3 &v2);
196        /// SqrMagnitude(v1-v2)
197        friend inline float SqrDistance(const Vector3 &v1, const Vector3 &v2);
198        /// creates the std::vector of unit size corresponding to given std::vector
199        friend inline Vector3 Normalize(const Vector3 &A);
200        /// // Rotate a direction vector
201        friend Vector3 PlaneRotate(const Matrix4x4 &, const Vector3 &);
202
203        // construct view vectors .. DirAt is the main viewing direction
204        // Viewer is the coordinates of viewer location, UpL is the std::vector.
205        friend void ViewVectors(const Vector3 &DirAt,
206                                                        const Vector3 &Viewer,
207                                                        const Vector3 &UpL,
208                                                        Vector3 &ViewV,
209                                                        Vector3 &ViewU,
210                                                        Vector3 &ViewN);
211
212        // Given the intersection point `P', you have available normal `N'
213        // of unit length. Let us suppose the incoming ray has direction `D'.
214        // Then we can construct such two vectors `U' and `V' that
215        // `U',`N', and `D' are coplanar, and `V' is perpendicular
216        // to the vectors `N','D', and `V'. Then 'N', 'U', and 'V' create
217        // the orthonormal base in space R3.
218        friend void TangentVectors(Vector3 &U, Vector3 &V, // output
219                                                           const Vector3 &normal, // input
220                                                           const Vector3 &dirIncoming);
221
222
223        // Binary operators
224       
225        friend inline Vector3 operator+ (const Vector3 &A, const Vector3 &B);
226        friend inline Vector3 operator- (const Vector3 &A, const Vector3 &B);
227        friend inline Vector3 operator* (const Vector3 &A, const Vector3 &B);
228        friend inline Vector3 operator* (const Vector3 &A, float B);
229        friend inline Vector3 operator* (float A, const Vector3 &B);
230        friend Vector3 operator* (const Matrix4x4 &, const Vector3 &);
231        friend inline Vector3 operator/ (const Vector3 &A, const Vector3 &B);
232
233        friend inline int operator< (const Vector3 &A, const Vector3 &B);
234        friend inline int operator<= (const Vector3 &A, const Vector3 &B);
235
236        friend inline Vector3 operator/ (const Vector3 &A, float B);
237        friend inline int operator== (const Vector3 &A, const Vector3 &B);
238        friend inline float DotProd(const Vector3 &A, const Vector3 &B);
239        friend inline Vector3 CrossProd (const Vector3 &A, const Vector3 &B);
240
241        friend std::ostream& operator<< (std::ostream &s, const Vector3 &A);
242        friend std::istream& operator>> (std::istream &s, Vector3 &A);
243
244        friend void Minimize(Vector3 &min, const Vector3 &candidate);
245        friend void Maximize(Vector3 &max, const Vector3 &candidate);
246
247        friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr);
248        friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2);
249
250        friend Vector3 CosineRandomVector(const Vector3 &normal);
251        friend Vector3 UniformRandomVector(const Vector3 &normal);
252        friend Vector3 UniformRandomVector();
253
254        friend Vector3 UniformRandomVector(float r1, float r2);
255
256        friend Vector3 CosineRandomVector(float r1, float r2, const Vector3 &normal);
257};
258
259
260
261// forward declaration
262extern Vector3 UniformRandomVector(const Vector3 &normal);
263extern Vector3 UniformRandomVector();
264extern Vector3 UniformRandomVector(const float r1, const float r2);
265
266inline Vector3 ArbitraryNormal(const Vector3 &N)
267{
268        float dist2 = N.x * N.x + N.y * N.y;
269
270        if (dist2 > 0.0001f)
271        {
272                float inv_size = 1.0f / sqrtf(dist2);
273                return Vector3(N.y * inv_size, -N.x * inv_size, 0); // N x (0,0,1)
274        }
275
276        float inv_size = 1.0f / sqrtf(N.z * N.z + N.x * N.x);
277        return Vector3(-N.z * inv_size, 0, N.x * inv_size); // N x (0,1,0)
278}
279
280
281inline void Vector3::RightHandedBase(Vector3& U, Vector3& V) const
282{
283        // HACK
284        V = ArbitraryNormal(*this);
285        U = CrossProd(V, *this);
286}
287
288
289inline Vector3 ToGlobalFrame(const Vector3 &loc,
290                                                         const Vector3 &U,
291                                                         const Vector3 &V,
292                                                         const Vector3 &N)
293{
294        return loc.x * U + loc.y * V + loc.z * N;
295}
296
297
298inline Vector3 ToLocalFrame(const Vector3 &loc,
299                                                        const Vector3 &U,
300                                                        const Vector3 &V,
301                                                        const Vector3 &N)
302{
303        return Vector3(loc.x * U.x + loc.y * U.y + loc.z * U.z,
304                                   loc.x * V.x + loc.y * V.y + loc.z * V.z,
305                                   loc.x * N.x + loc.y * N.y + loc.z * N.z);
306}
307
308
309inline float Magnitude(const Vector3 &v)
310{
311        return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
312}
313
314
315inline float SqrMagnitude(const Vector3 &v)
316{
317  return v.x * v.x + v.y * v.y + v.z * v.z;
318}
319
320
321inline float Distance(const Vector3 &v1, const Vector3 &v2)
322{
323  return sqrtf(sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z));
324}
325
326
327inline float SqrDistance(const Vector3 &v1, const Vector3 &v2)
328{
329  return sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z);
330}
331
332
333inline Vector3 Normalize(const Vector3 &A)
334{
335  return A * (1.0f / Magnitude(A));
336}
337
338
339inline float DotProd(const Vector3 &A, const Vector3 &B)
340{
341  return A.x * B.x + A.y * B.y + A.z * B.z;
342}
343
344
345// angle between two vectors with respect to a surface normal in the
346// range [0 .. 2 * pi]
347inline float Angle(const Vector3 &A, const Vector3 &B, const Vector3 &norm)
348{
349        Vector3 cross = CrossProd(A, B);
350
351        float signedAngle;
352
353        if (DotProd(cross, norm) > 0)
354                signedAngle = atan2(-Magnitude(CrossProd(A, B)), DotProd(A, B));
355        else
356                signedAngle = atan2(Magnitude(CrossProd(A, B)), DotProd(A, B));
357
358        if (signedAngle < 0)
359                return 2 * M_PI + signedAngle;
360
361        return signedAngle;
362}
363
364
365inline Vector3 Vector3::operator+() const
366{
367        return *this;
368}
369
370
371inline Vector3 Vector3::operator-() const
372{
373  return Vector3(-x, -y, -z);
374}
375
376
377inline Vector3 &Vector3::operator+=(const Vector3 &A)
378{
379        x += A.x;  y += A.y;  z += A.z;
380        return *this;
381}
382
383
384inline Vector3& Vector3::operator-=(const Vector3 &A)
385{
386        x -= A.x;  y -= A.y;  z -= A.z;
387       
388        return *this;
389}
390
391
392inline Vector3& Vector3::operator*= (float A)
393{
394        x *= A;  y *= A;  z *= A;
395        return *this;
396}
397
398
399inline Vector3& Vector3::operator/=(float A)
400{
401        float a = 1.0f / A;
402       
403        x *= a;  y *= a;  z *= a;
404       
405        return *this;
406}
407
408
409inline Vector3& Vector3::operator*= (const Vector3 &A)
410{
411  x *= A.x;  y *= A.y;  z *= A.z;
412  return *this;
413}
414
415
416inline Vector3 operator+ (const Vector3 &A, const Vector3 &B)
417{
418        return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
419}
420
421
422inline Vector3 operator- (const Vector3 &A, const Vector3 &B)
423{
424        return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
425}
426
427
428inline Vector3 operator* (const Vector3 &A, const Vector3 &B)
429{
430        return Vector3(A.x * B.x, A.y * B.y, A.z * B.z);
431}
432
433
434inline Vector3 operator* (const Vector3 &A, float B)
435{
436  return Vector3(A.x * B, A.y * B, A.z * B);
437}
438
439
440inline Vector3 operator* (float A, const Vector3 &B)
441{
442        return Vector3(B.x * A, B.y * A, B.z * A);
443}
444
445
446inline Vector3 operator/ (const Vector3 &A, const Vector3 &B)
447{
448        return Vector3(A.x / B.x, A.y / B.y, A.z / B.z);
449}
450
451
452inline Vector3 operator/ (const Vector3 &A, float B)
453{
454        float b = 1.0f / B;
455        return Vector3(A.x * b, A.y * b, A.z * b);
456}
457
458
459inline int operator< (const Vector3 &A, const Vector3 &B)
460{
461        return A.x < B.x && A.y < B.y && A.z < B.z;
462}
463
464
465inline int operator<= (const Vector3 &A, const Vector3 &B)
466{
467        return A.x <= B.x && A.y <= B.y && A.z <= B.z;
468}
469
470
471// Might replace floating-point == with comparisons of
472// magnitudes, if needed.
473inline int operator== (const Vector3 &A, const Vector3 &B)
474{
475        return (A.x == B.x) && (A.y == B.y) && (A.z == B.z);
476}
477
478
479inline Vector3 CrossProd (const Vector3 &A, const Vector3 &B)
480{
481        return Vector3(A.y * B.z - A.z * B.y,
482                           A.z * B.x - A.x * B.z,
483                                   A.x * B.y - A.y * B.x);
484}
485
486
487inline void Vector3::Normalize()
488{
489        float sqrmag = x * x + y * y + z * z;
490       
491        if (sqrmag > 0.0f)
492                (*this) *= 1.0f / sqrtf(sqrmag);
493}
494
495
496// Overload << operator for C++-style output
497inline std::ostream& operator<< (std::ostream &s, const Vector3 &A)
498{
499        return s << "(" << A.x << ", " << A.y << ", " << A.z << ")";
500}
501
502
503// Overload >> operator for C++-style input
504inline std::istream& operator>> (std::istream &s, Vector3 &A)
505{
506        char a;
507        // read "(x, y, z)"
508        return s >> a >> A.x >> a >> A.y >> a >> A.z >> a;
509}
510
511
512inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr)
513{
514        if (fabsf(v1.x-v2.x) > thr)
515                return false;
516        if (fabsf(v1.y-v2.y) > thr)
517                return false;
518        if (fabsf(v1.z-v2.z) > thr)
519                return false;
520       
521        return true;
522}
523
524
525inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2)
526{
527        return EpsilonEqualV3(v1, v2, Limits::Small);
528}
529
530
531}
532
533#endif
Note: See TracBrowser for help on using the repository browser.