source: trunk/VUT/GtpVisibilityPreprocessor/src/Vector3.h @ 162

Revision 162, 11.7 KB checked in by bittner, 19 years ago (diff)

functional raycasting version

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