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