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

Revision 1883, 13.1 KB checked in by bittner, 18 years ago (diff)

mixture distribution initial coding

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 CosineRandomVector(const Vector3 &normal);
236  friend Vector3 UniformRandomVector(const Vector3 &normal);
237  friend Vector3 UniformRandomVector();
238
239  friend Vector3
240  UniformRandomVector(const float r1, const float r2);
241
242  friend Vector3 CosineRandomVector(
243                                                                        const float r1,
244                                                                        const float r2,
245                                                                        const Vector3 &normal
246                                                                        );
247
248
249};
250
251inline Vector3
252ArbitraryNormal(const Vector3 &N)
253{
254  float dist2 = N.x * N.x + N.y * N.y;
255  if (dist2 > 0.0001) {
256    float inv_size = 1.0f/sqrtf(dist2);
257    return Vector3(N.y * inv_size, -N.x * inv_size, 0); // N x (0,0,1)
258  }
259  float inv_size = 1.0f/sqrtf(N.z * N.z + N.x * N.x);
260  return Vector3(-N.z * inv_size, 0, N.x * inv_size); // N x (0,1,0)
261}
262
263
264inline Vector3
265ToGlobalFrame(const Vector3 &loc,
266              const Vector3 &U,
267              const Vector3 &V,
268              const Vector3 &N)
269{
270  return loc.x * U + loc.y * V + loc.z * N;
271}
272
273inline Vector3
274ToLocalFrame(const Vector3 &loc,
275             const Vector3 &U,
276             const Vector3 &V,
277             const Vector3 &N)
278{
279  return Vector3( loc.x * U.x + loc.y * U.y + loc.z * U.z,
280                    loc.x * V.x + loc.y * V.y + loc.z * V.z,
281                    loc.x * N.x + loc.y * N.y + loc.z * N.z);
282}
283
284inline float
285Magnitude(const Vector3 &v)
286{
287  return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
288}
289
290inline float
291SqrMagnitude(const Vector3 &v)
292{
293  return v.x * v.x + v.y * v.y + v.z * v.z;
294}
295
296inline float
297Distance(const Vector3 &v1, const Vector3 &v2)
298{
299  return sqrtf(sqr(v1.x-v2.x) + sqr(v1.y-v2.y) + sqr(v1.z-v2.z));
300}
301
302inline float
303SqrDistance(const Vector3 &v1, const Vector3 &v2)
304{
305  return sqr(v1.x-v2.x)+sqr(v1.y-v2.y)+sqr(v1.z-v2.z);
306}
307
308inline Vector3
309Normalize(const Vector3 &A)
310{
311  return A * (1.0f/Magnitude(A));
312}
313
314inline float
315DotProd(const Vector3 &A, const Vector3 &B)
316{
317  return A.x * B.x + A.y * B.y + A.z * B.z;
318}
319
320// angle between two vectors with respect to a surface normal in the
321// range [0 .. 2 * pi]
322inline float
323Angle(const Vector3 &A, const Vector3 &B, const Vector3 &norm)
324{
325        Vector3 cross = CrossProd(A, B);
326
327        float signedAngle;
328
329        if (DotProd(cross, norm) > 0)
330                signedAngle = atan2(-Magnitude(CrossProd(A, B)), DotProd(A, B));
331        else
332                signedAngle = atan2(Magnitude(CrossProd(A, B)), DotProd(A, B));
333
334        if (signedAngle < 0)
335                return 2 * PI + signedAngle;
336
337        return signedAngle;
338}
339
340inline Vector3
341Vector3::operator+() const
342{
343  return *this;
344}
345
346inline Vector3
347Vector3::operator-() const
348{
349  return Vector3(-x, -y, -z);
350}
351
352inline Vector3&
353Vector3::operator+=(const Vector3 &A)
354{
355  x += A.x;  y += A.y;  z += A.z;
356  return *this;
357}
358
359inline Vector3&
360Vector3::operator-=(const Vector3 &A)
361{
362  x -= A.x;  y -= A.y;  z -= A.z;
363  return *this;
364}
365
366inline Vector3&
367Vector3::operator*= (float A)
368{
369  x *= A;  y *= A;  z *= A;
370  return *this;
371}
372
373inline Vector3&
374Vector3::operator/=(float A)
375{
376  float a = 1.0f/A;
377  x *= a;  y *= a;  z *= a;
378  return *this;
379}
380
381inline Vector3&
382Vector3::operator*= (const Vector3 &A)
383{
384  x *= A.x;  y *= A.y;  z *= A.z;
385  return *this;
386}
387
388inline Vector3
389operator+ (const Vector3 &A, const Vector3 &B)
390{
391  return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
392}
393
394inline Vector3
395operator- (const Vector3 &A, const Vector3 &B)
396{
397  return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
398}
399
400inline Vector3
401operator* (const Vector3 &A, const Vector3 &B)
402{
403  return Vector3(A.x * B.x, A.y * B.y, A.z * B.z);
404}
405
406inline Vector3
407operator* (const Vector3 &A, float B)
408{
409  return Vector3(A.x * B, A.y * B, A.z * B);
410}
411
412inline Vector3
413operator* (float A, const Vector3 &B)
414{
415  return Vector3(B.x * A, B.y * A, B.z * A);
416}
417
418inline Vector3
419operator/ (const Vector3 &A, const Vector3 &B)
420{
421  return Vector3(A.x / B.x, A.y / B.y, A.z / B.z);
422}
423
424inline Vector3
425operator/ (const Vector3 &A, float B)
426{
427  float b = 1.0f / B;
428  return Vector3(A.x * b, A.y * b, A.z * b);
429}
430
431inline int
432operator< (const Vector3 &A, const Vector3 &B)
433{
434  return A.x < B.x && A.y < B.y && A.z < B.z;
435}
436
437inline int
438operator<= (const Vector3 &A, const Vector3 &B)
439{
440  return A.x <= B.x && A.y <= B.y && A.z <= B.z;
441}
442
443// Might replace floating-point == with comparisons of
444// magnitudes, if needed.
445inline int operator== (const Vector3 &A, const Vector3 &B)
446{
447  return (A.x == B.x) && (A.y == B.y) && (A.z == B.z);
448}
449
450inline Vector3
451CrossProd (const Vector3 &A, const Vector3 &B)
452{
453  return
454        Vector3(A.y * B.z - A.z * B.y,
455                          A.z * B.x - A.x * B.z,
456                        A.x * B.y - A.y * B.x);
457}
458
459
460inline void
461Vector3::Normalize()
462{
463  float sqrmag = x * x + y * y + z * z;
464  if (sqrmag > 0.0f)
465    (*this) *= 1.0f / sqrtf(sqrmag);
466}
467
468
469// Overload << operator for C++-style output
470inline ostream&
471operator<< (ostream &s, const Vector3 &A)
472{
473  return s << "(" << A.x << ", " << A.y << ", " << A.z << ")";
474}
475
476// Overload >> operator for C++-style input
477inline istream&
478operator>> (istream &s, Vector3 &A)
479{
480  char a;
481  // read "(x, y, z)"
482  return s >> a >> A.x >> a >> A.y >> a >> A.z >> a;
483}
484
485inline int
486EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2, float thr)
487{
488  if ( fabsf(v1.x-v2.x) > thr )
489    return false;
490  if ( fabsf(v1.y-v2.y) > thr )
491    return false;
492  if ( fabsf(v1.z-v2.z) > thr )
493    return false;
494  return true;
495}
496
497inline int
498EpsilonEqualV3(const Vector3 &v1, const Vector3 &v2)
499{
500  return EpsilonEqualV3(v1, v2, Limits::Small);
501}
502
503
504
505}
506
507#endif
Note: See TracBrowser for help on using the repository browser.