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