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