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

Revision 1420, 12.9 KB checked in by mattausch, 18 years ago (diff)

corrected raycasting bug for triangles because of ill defined triangles

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  /** Returns false if this vector has a nan component.
101  */
102  bool CheckValidity() const;
103
104  /**
105    ===> Using ArbitraryNormal() for constructing coord systems
106    ===> is obsoleted by RightHandedBase() method (<JK> 12/20/03).   
107
108     Return an arbitrary normal to `v'.
109     In fact it tries v x (0,0,1) an if the result is too small,
110     it definitely does v x (0,1,0). It will always work for
111     non-degenareted vector and is much faster than to use
112     TangentVectors.
113
114     @param v(in) The vector we want to find normal for.
115     @return The normal vector to v.
116  */
117  friend inline Vector3 ArbitraryNormal(const Vector3 &v);
118
119  /**
120    Find a right handed coordinate system with (*this) being
121    the z-axis. For a right-handed system, U x V = (*this) holds.
122    This implementation is here to avoid inconsistence and confusion
123    when construction coordinate systems using ArbitraryNormal():
124    In fact:
125      V = ArbitraryNormal(N);
126      U = CrossProd(V,N);
127    constructs a right-handed coordinate system as well, BUT:
128    1) bugs can be introduced if one mistakenly constructs a
129       left handed sytems e.g. by doing
130       U = ArbitraryNormal(N);
131       V = CrossProd(U,N);
132    2) this implementation gives non-negative base vectors
133       for (*this)==(0,0,1) |  (0,1,0) | (1,0,0), which is
134       good for debugging and is not the case with the implementation
135       using ArbitraryNormal()
136
137    ===> Using ArbitraryNormal() for constructing coord systems
138             is obsoleted by this method (<JK> 12/20/03).   
139  */
140  void RightHandedBase(Vector3& U, Vector3& V) const;
141
142  /// Transforms a vector to the global coordinate frame.
143  /**
144    Given a local coordinate frame (U,V,N) (i.e. U,V,N are
145    the x,y,z axes of the local coordinate system) and
146    a vector 'loc' in the local coordiante system, this
147    function returns a the coordinates of the same vector
148    in global frame (i.e. frame (1,0,0), (0,1,0), (0,0,1).
149  */
150  friend inline Vector3 ToGlobalFrame(const Vector3& loc,
151          const Vector3& U,
152          const Vector3& V,
153          const Vector3& N);
154 
155  /// Transforms a vector to a local coordinate frame.
156  /**
157    Given a local coordinate frame (U,V,N) (i.e. U,V,N are
158    the x,y,z axes of the local coordinate system) and
159    a vector 'loc' in the global coordiante system, this
160    function returns a the coordinates of the same vector
161    in the local frame.
162  */
163  friend inline Vector3 ToLocalFrame(const Vector3& loc,
164          const Vector3& U,
165          const Vector3& V,
166          const Vector3& N);
167
168  /// the magnitude=size of the vector
169  friend inline float Magnitude(const Vector3 &v);
170  /// the squared magnitude of the vector .. for efficiency in some cases
171  friend inline float SqrMagnitude(const Vector3 &v);
172  /// Magnitude(v1-v2)
173  friend inline float Distance(const Vector3 &v1, const Vector3 &v2);
174  /// SqrMagnitude(v1-v2)
175  friend inline float SqrDistance(const Vector3 &v1, const Vector3 &v2);
176
177  // creates the vector of unit size corresponding to given vector
178  friend inline Vector3 Normalize(const Vector3 &A);
179
180  // Rotate a normal vector.
181  friend Vector3 PlaneRotate(const Matrix4x4 &, const Vector3 &);
182
183  // construct view vectors .. DirAt is the main viewing direction
184  // Viewer is the coordinates of viewer location, UpL is the vector.
185  friend void ViewVectors(const Vector3 &DirAt, const Vector3 &Viewer,
186                          const Vector3 &UpL, Vector3 &ViewV,
187                          Vector3 &ViewU, Vector3 &ViewN );
188
189  // Given the intersection point `P', you have available normal `N'
190  // of unit length. Let us suppose the incoming ray has direction `D'.
191  // Then we can construct such two vectors `U' and `V' that
192  // `U',`N', and `D' are coplanar, and `V' is perpendicular
193  // to the vectors `N','D', and `V'. Then 'N', 'U', and 'V' create
194  // the orthonormal base in space R3.
195  friend void TangentVectors(Vector3 &U, Vector3 &V, // output
196                             const Vector3 &normal, // input
197                             const Vector3 &dirIncoming);
198  // Unary operators
199  Vector3 operator+ () const;
200  Vector3 operator- () const;
201
202  // Assignment operators
203  Vector3& operator+= (const Vector3 &A);
204  Vector3& operator-= (const Vector3 &A);
205  Vector3& operator*= (const Vector3 &A);
206  Vector3& operator*= (float A);
207  Vector3& operator/= (float A);
208
209  // Binary operators
210  friend inline Vector3 operator+ (const Vector3 &A, const Vector3 &B);
211  friend inline Vector3 operator- (const Vector3 &A, const Vector3 &B);
212  friend inline Vector3 operator* (const Vector3 &A, const Vector3 &B);
213  friend inline Vector3 operator* (const Vector3 &A, float B);
214  friend inline Vector3 operator* (float A, const Vector3 &B);
215  friend Vector3 operator* (const Matrix4x4 &, const Vector3 &);
216  friend inline Vector3 operator/ (const Vector3 &A, const Vector3 &B);
217
218  friend inline int operator< (const Vector3 &A, const Vector3 &B);
219  friend inline int operator<= (const Vector3 &A, const Vector3 &B);
220
221  friend inline Vector3 operator/ (const Vector3 &A, float B);
222  friend inline int operator== (const Vector3 &A, const Vector3 &B);
223  friend inline float DotProd(const Vector3 &A, const Vector3 &B);
224  friend inline Vector3 CrossProd (const Vector3 &A, const Vector3 &B);
225
226  friend ostream& operator<< (ostream &s, const Vector3 &A);
227  friend istream& operator>> (istream &s, Vector3 &A);
228   
229  friend void Minimize(Vector3 &min, const Vector3 &candidate);
230  friend void Maximize(Vector3 &max, const Vector3 &candidate);
231
232  friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr);
233  friend inline int EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2);
234
235  friend Vector3 UniformRandomVector(const Vector3 &normal);
236  friend Vector3 UniformRandomVector();
237 
238
239};
240
241inline Vector3
242ArbitraryNormal(const Vector3 &N)
243{
244  float dist2 = N.x * N.x + N.y * N.y;
245  if (dist2 > 0.0001) {
246    float inv_size = 1.0f/sqrtf(dist2);
247    return Vector3(N.y * inv_size, -N.x * inv_size, 0); // N x (0,0,1)
248  }
249  float inv_size = 1.0f/sqrtf(N.z * N.z + N.x * N.x);
250  return Vector3(-N.z * inv_size, 0, N.x * inv_size); // N x (0,1,0)
251}
252
253
254inline Vector3
255ToGlobalFrame(const Vector3 &loc,
256              const Vector3 &U,
257              const Vector3 &V,
258              const Vector3 &N)
259{
260  return loc.x * U + loc.y * V + loc.z * N;
261}
262
263inline Vector3
264ToLocalFrame(const Vector3 &loc,
265             const Vector3 &U,
266             const Vector3 &V,
267             const Vector3 &N)
268{
269  return Vector3( loc.x * U.x + loc.y * U.y + loc.z * U.z,
270                    loc.x * V.x + loc.y * V.y + loc.z * V.z,
271                    loc.x * N.x + loc.y * N.y + loc.z * N.z);
272}
273
274inline float
275Magnitude(const Vector3 &v)
276{
277  return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
278}
279
280inline float
281SqrMagnitude(const Vector3 &v)
282{
283  return v.x * v.x + v.y * v.y + v.z * v.z;
284}
285
286inline float
287Distance(const Vector3 &v1, const Vector3 &v2)
288{
289  return sqrtf(sqr(v1.x-v2.x) + sqr(v1.y-v2.y) + sqr(v1.z-v2.z));
290}
291
292inline float
293SqrDistance(const Vector3 &v1, const Vector3 &v2)
294{
295  return sqr(v1.x-v2.x)+sqr(v1.y-v2.y)+sqr(v1.z-v2.z);
296}
297
298inline Vector3
299Normalize(const Vector3 &A)
300{
301  return A * (1.0f/Magnitude(A));
302}
303
304inline float
305DotProd(const Vector3 &A, const Vector3 &B)
306{
307  return A.x * B.x + A.y * B.y + A.z * B.z;
308}
309
310// angle between two vectors with respect to a surface normal in the
311// range [0 .. 2 * pi]
312inline float
313Angle(const Vector3 &A, const Vector3 &B, const Vector3 &norm)
314{
315        Vector3 cross = CrossProd(A, B);
316
317        float signedAngle;
318
319        if (DotProd(cross, norm) > 0)
320                signedAngle = atan2(-Magnitude(CrossProd(A, B)), DotProd(A, B));
321        else
322                signedAngle = atan2(Magnitude(CrossProd(A, B)), DotProd(A, B));
323
324        if (signedAngle < 0)
325                return 2 * PI + signedAngle;
326
327        return signedAngle;
328}
329
330inline Vector3
331Vector3::operator+() const
332{
333  return *this;
334}
335
336inline Vector3
337Vector3::operator-() const
338{
339  return Vector3(-x, -y, -z);
340}
341
342inline Vector3&
343Vector3::operator+=(const Vector3 &A)
344{
345  x += A.x;  y += A.y;  z += A.z;
346  return *this;
347}
348
349inline Vector3&
350Vector3::operator-=(const Vector3 &A)
351{
352  x -= A.x;  y -= A.y;  z -= A.z;
353  return *this;
354}
355
356inline Vector3&
357Vector3::operator*= (float A)
358{
359  x *= A;  y *= A;  z *= A;
360  return *this;
361}
362
363inline Vector3&
364Vector3::operator/=(float A)
365{
366  float a = 1.0f/A;
367  x *= a;  y *= a;  z *= a;
368  return *this;
369}
370
371inline Vector3&
372Vector3::operator*= (const Vector3 &A)
373{
374  x *= A.x;  y *= A.y;  z *= A.z;
375  return *this;
376}
377
378inline Vector3
379operator+ (const Vector3 &A, const Vector3 &B)
380{
381  return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
382}
383
384inline Vector3
385operator- (const Vector3 &A, const Vector3 &B)
386{
387  return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
388}
389
390inline Vector3
391operator* (const Vector3 &A, const Vector3 &B)
392{
393  return Vector3(A.x * B.x, A.y * B.y, A.z * B.z);
394}
395
396inline Vector3
397operator* (const Vector3 &A, float B)
398{
399  return Vector3(A.x * B, A.y * B, A.z * B);
400}
401
402inline Vector3
403operator* (float A, const Vector3 &B)
404{
405  return Vector3(B.x * A, B.y * A, B.z * A);
406}
407
408inline Vector3
409operator/ (const Vector3 &A, const Vector3 &B)
410{
411  return Vector3(A.x / B.x, A.y / B.y, A.z / B.z);
412}
413
414inline Vector3
415operator/ (const Vector3 &A, float B)
416{
417  float b = 1.0f / B;
418  return Vector3(A.x * b, A.y * b, A.z * b);
419}
420
421inline int
422operator< (const Vector3 &A, const Vector3 &B)
423{
424  return A.x < B.x && A.y < B.y && A.z < B.z;
425}
426
427inline int
428operator<= (const Vector3 &A, const Vector3 &B)
429{
430  return A.x <= B.x && A.y <= B.y && A.z <= B.z;
431}
432
433// Might replace floating-point == with comparisons of
434// magnitudes, if needed.
435inline int operator== (const Vector3 &A, const Vector3 &B)
436{
437  return (A.x == B.x) && (A.y == B.y) && (A.z == B.z);
438}
439
440inline Vector3
441CrossProd (const Vector3 &A, const Vector3 &B)
442{
443  return
444          Vector3(A.y * B.z - A.z * B.y,
445                          A.z * B.x - A.x * B.z,
446                          A.x * B.y - A.y * B.x);
447}
448
449inline void
450Vector3::Normalize()
451{
452  float sqrmag = x * x + y * y + z * z;
453  if (sqrmag > 0.0f)
454    (*this) *= 1.0f / sqrtf(sqrmag);
455}
456
457
458// Overload << operator for C++-style output
459inline ostream&
460operator<< (ostream &s, const Vector3 &A)
461{
462  return s << "(" << A.x << ", " << A.y << ", " << A.z << ")";
463}
464
465// Overload >> operator for C++-style input
466inline istream&
467operator>> (istream &s, Vector3 &A)
468{
469  char a;
470  // read "(x, y, z)"
471  return s >> a >> A.x >> a >> A.y >> a >> A.z >> a;
472}
473
474inline int
475EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr)
476{
477  if ( fabsf(v1.x-v2.x) > thr )
478    return false;
479  if ( fabsf(v1.y-v2.y) > thr )
480    return false;
481  if ( fabsf(v1.z-v2.z) > thr )
482    return false;
483  return true;
484}
485
486inline int
487EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2)
488{
489  return EpsilonEqualV3(v1, v2, Limits::Small);
490}
491
492
493
494}
495
496#endif
Note: See TracBrowser for help on using the repository browser.