source: GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h @ 1328

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