[855] | 1 | /////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // |
---|
| 3 | // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas |
---|
| 4 | // Digital Ltd. LLC |
---|
| 5 | // |
---|
| 6 | // All rights reserved. |
---|
| 7 | // |
---|
| 8 | // Redistribution and use in source and binary forms, with or without |
---|
| 9 | // modification, are permitted provided that the following conditions are |
---|
| 10 | // met: |
---|
| 11 | // * Redistributions of source code must retain the above copyright |
---|
| 12 | // notice, this list of conditions and the following disclaimer. |
---|
| 13 | // * Redistributions in binary form must reproduce the above |
---|
| 14 | // copyright notice, this list of conditions and the following disclaimer |
---|
| 15 | // in the documentation and/or other materials provided with the |
---|
| 16 | // distribution. |
---|
| 17 | // * Neither the name of Industrial Light & Magic nor the names of |
---|
| 18 | // its contributors may be used to endorse or promote products derived |
---|
| 19 | // from this software without specific prior written permission. |
---|
| 20 | // |
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | // |
---|
| 33 | /////////////////////////////////////////////////////////////////////////// |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #ifndef INCLUDED_IMATHVEC_H |
---|
| 38 | #define INCLUDED_IMATHVEC_H |
---|
| 39 | |
---|
| 40 | //---------------------------------------------------- |
---|
| 41 | // |
---|
| 42 | // 2D and 3D point/vector class templates! |
---|
| 43 | // |
---|
| 44 | //---------------------------------------------------- |
---|
| 45 | |
---|
| 46 | #include <ImathExc.h> |
---|
| 47 | #include <ImathLimits.h> |
---|
| 48 | #include <ImathMath.h> |
---|
| 49 | |
---|
| 50 | #include <iostream> |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | namespace Imath { |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | template <class T> class Vec2 |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | |
---|
| 60 | //------------------- |
---|
| 61 | // Access to elements |
---|
| 62 | //------------------- |
---|
| 63 | |
---|
| 64 | T x, y; |
---|
| 65 | |
---|
| 66 | T & operator [] (int i); |
---|
| 67 | const T & operator [] (int i) const; |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | //------------- |
---|
| 71 | // Constructors |
---|
| 72 | //------------- |
---|
| 73 | |
---|
| 74 | Vec2 (); // no initialization |
---|
| 75 | explicit Vec2 (T a); // (a a) |
---|
| 76 | Vec2 (T a, T b); // (a b) |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | //--------------------------------- |
---|
| 80 | // Copy constructors and assignment |
---|
| 81 | //--------------------------------- |
---|
| 82 | |
---|
| 83 | Vec2 (const Vec2 &v); |
---|
| 84 | template <class S> Vec2 (const Vec2<S> &v); |
---|
| 85 | |
---|
| 86 | const Vec2 & operator = (const Vec2 &v); |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | //---------------------- |
---|
| 90 | // Compatibility with Sb |
---|
| 91 | //---------------------- |
---|
| 92 | |
---|
| 93 | template <class S> |
---|
| 94 | void setValue (S a, S b); |
---|
| 95 | |
---|
| 96 | template <class S> |
---|
| 97 | void setValue (const Vec2<S> &v); |
---|
| 98 | |
---|
| 99 | template <class S> |
---|
| 100 | void getValue (S &a, S &b) const; |
---|
| 101 | |
---|
| 102 | template <class S> |
---|
| 103 | void getValue (Vec2<S> &v) const; |
---|
| 104 | |
---|
| 105 | T * getValue (); |
---|
| 106 | const T * getValue () const; |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | //--------- |
---|
| 110 | // Equality |
---|
| 111 | //--------- |
---|
| 112 | |
---|
| 113 | template <class S> |
---|
| 114 | bool operator == (const Vec2<S> &v) const; |
---|
| 115 | |
---|
| 116 | template <class S> |
---|
| 117 | bool operator != (const Vec2<S> &v) const; |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | //----------------------------------------------------------------------- |
---|
| 121 | // Compare two vectors and test if they are "approximately equal": |
---|
| 122 | // |
---|
| 123 | // equalWithAbsError (v, e) |
---|
| 124 | // |
---|
| 125 | // Returns true if the coefficients of this and v are the same with |
---|
| 126 | // an absolute error of no more than e, i.e., for all i |
---|
| 127 | // |
---|
| 128 | // abs (this[i] - v[i]) <= e |
---|
| 129 | // |
---|
| 130 | // equalWithRelError (v, e) |
---|
| 131 | // |
---|
| 132 | // Returns true if the coefficients of this and v are the same with |
---|
| 133 | // a relative error of no more than e, i.e., for all i |
---|
| 134 | // |
---|
| 135 | // abs (this[i] - v[i]) <= e * abs (this[i]) |
---|
| 136 | //----------------------------------------------------------------------- |
---|
| 137 | |
---|
| 138 | bool equalWithAbsError (const Vec2<T> &v, T e) const; |
---|
| 139 | bool equalWithRelError (const Vec2<T> &v, T e) const; |
---|
| 140 | |
---|
| 141 | //------------ |
---|
| 142 | // Dot product |
---|
| 143 | //------------ |
---|
| 144 | |
---|
| 145 | T dot (const Vec2 &v) const; |
---|
| 146 | T operator ^ (const Vec2 &v) const; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | //------------------------------------------------ |
---|
| 150 | // Right-handed cross product, i.e. z component of |
---|
| 151 | // Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0) |
---|
| 152 | //------------------------------------------------ |
---|
| 153 | |
---|
| 154 | T cross (const Vec2 &v) const; |
---|
| 155 | T operator % (const Vec2 &v) const; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | //------------------------ |
---|
| 159 | // Component-wise addition |
---|
| 160 | //------------------------ |
---|
| 161 | |
---|
| 162 | const Vec2 & operator += (const Vec2 &v); |
---|
| 163 | Vec2 operator + (const Vec2 &v) const; |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | //--------------------------- |
---|
| 167 | // Component-wise subtraction |
---|
| 168 | //--------------------------- |
---|
| 169 | |
---|
| 170 | const Vec2 & operator -= (const Vec2 &v); |
---|
| 171 | Vec2 operator - (const Vec2 &v) const; |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | //------------------------------------ |
---|
| 175 | // Component-wise multiplication by -1 |
---|
| 176 | //------------------------------------ |
---|
| 177 | |
---|
| 178 | Vec2 operator - () const; |
---|
| 179 | const Vec2 & negate (); |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | //------------------------------ |
---|
| 183 | // Component-wise multiplication |
---|
| 184 | //------------------------------ |
---|
| 185 | |
---|
| 186 | const Vec2 & operator *= (const Vec2 &v); |
---|
| 187 | const Vec2 & operator *= (T a); |
---|
| 188 | Vec2 operator * (const Vec2 &v) const; |
---|
| 189 | Vec2 operator * (T a) const; |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | //------------------------ |
---|
| 193 | // Component-wise division |
---|
| 194 | //------------------------ |
---|
| 195 | |
---|
| 196 | const Vec2 & operator /= (const Vec2 &v); |
---|
| 197 | const Vec2 & operator /= (T a); |
---|
| 198 | Vec2 operator / (const Vec2 &v) const; |
---|
| 199 | Vec2 operator / (T a) const; |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | //---------------------------------------------------------------- |
---|
| 203 | // Length and normalization: If v.length() is 0.0, v.normalize() |
---|
| 204 | // and v.normalized() produce a null vector; v.normalizeExc() and |
---|
| 205 | // v.normalizedExc() throw a NullVecExc. |
---|
| 206 | // v.normalizeNonNull() and v.normalizedNonNull() are slightly |
---|
| 207 | // faster than the other normalization routines, but if v.length() |
---|
| 208 | // is 0.0, the result is undefined. |
---|
| 209 | //---------------------------------------------------------------- |
---|
| 210 | |
---|
| 211 | T length () const; |
---|
| 212 | T length2 () const; |
---|
| 213 | |
---|
| 214 | const Vec2 & normalize (); // modifies *this |
---|
| 215 | const Vec2 & normalizeExc () throw (Iex::MathExc); |
---|
| 216 | const Vec2 & normalizeNonNull (); |
---|
| 217 | |
---|
| 218 | Vec2<T> normalized () const; // does not modify *this |
---|
| 219 | Vec2<T> normalizedExc () const throw (Iex::MathExc); |
---|
| 220 | Vec2<T> normalizedNonNull () const; |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | //-------------------------------------------------------- |
---|
| 224 | // Number of dimensions, i.e. number of elements in a Vec2 |
---|
| 225 | //-------------------------------------------------------- |
---|
| 226 | |
---|
| 227 | static unsigned int dimensions() {return 2;} |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | //------------------------------------------------- |
---|
| 231 | // Limitations of type T (see also class limits<T>) |
---|
| 232 | //------------------------------------------------- |
---|
| 233 | |
---|
| 234 | static T baseTypeMin() {return limits<T>::min();} |
---|
| 235 | static T baseTypeMax() {return limits<T>::max();} |
---|
| 236 | static T baseTypeSmallest() {return limits<T>::smallest();} |
---|
| 237 | static T baseTypeEpsilon() {return limits<T>::epsilon();} |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | //-------------------------------------------------------------- |
---|
| 241 | // Base type -- in templates, which accept a parameter, V, which |
---|
| 242 | // could be either a Vec2<T> or a Vec3<T>, you can refer to T as |
---|
| 243 | // V::BaseType |
---|
| 244 | //-------------------------------------------------------------- |
---|
| 245 | |
---|
| 246 | typedef T BaseType; |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | template <class T> class Vec3 |
---|
| 251 | { |
---|
| 252 | public: |
---|
| 253 | |
---|
| 254 | //------------------- |
---|
| 255 | // Access to elements |
---|
| 256 | //------------------- |
---|
| 257 | |
---|
| 258 | T x, y, z; |
---|
| 259 | |
---|
| 260 | T & operator [] (int i); |
---|
| 261 | const T & operator [] (int i) const; |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | //------------- |
---|
| 265 | // Constructors |
---|
| 266 | //------------- |
---|
| 267 | |
---|
| 268 | Vec3 (); // no initialization |
---|
| 269 | explicit Vec3 (T a); // (a a a) |
---|
| 270 | Vec3 (T a, T b, T c); // (a b c) |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | //--------------------------------- |
---|
| 274 | // Copy constructors and assignment |
---|
| 275 | //--------------------------------- |
---|
| 276 | |
---|
| 277 | Vec3 (const Vec3 &v); |
---|
| 278 | template <class S> Vec3 (const Vec3<S> &v); |
---|
| 279 | |
---|
| 280 | const Vec3 & operator = (const Vec3 &v); |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | //---------------------- |
---|
| 284 | // Compatibility with Sb |
---|
| 285 | //---------------------- |
---|
| 286 | |
---|
| 287 | template <class S> |
---|
| 288 | void setValue (S a, S b, S c); |
---|
| 289 | |
---|
| 290 | template <class S> |
---|
| 291 | void setValue (const Vec3<S> &v); |
---|
| 292 | |
---|
| 293 | template <class S> |
---|
| 294 | void getValue (S &a, S &b, S &c) const; |
---|
| 295 | |
---|
| 296 | template <class S> |
---|
| 297 | void getValue (Vec3<S> &v) const; |
---|
| 298 | |
---|
| 299 | T * getValue(); |
---|
| 300 | const T * getValue() const; |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | //--------- |
---|
| 304 | // Equality |
---|
| 305 | //--------- |
---|
| 306 | |
---|
| 307 | template <class S> |
---|
| 308 | bool operator == (const Vec3<S> &v) const; |
---|
| 309 | |
---|
| 310 | template <class S> |
---|
| 311 | bool operator != (const Vec3<S> &v) const; |
---|
| 312 | |
---|
| 313 | //----------------------------------------------------------------------- |
---|
| 314 | // Compare two vectors and test if they are "approximately equal": |
---|
| 315 | // |
---|
| 316 | // equalWithAbsError (v, e) |
---|
| 317 | // |
---|
| 318 | // Returns true if the coefficients of this and v are the same with |
---|
| 319 | // an absolute error of no more than e, i.e., for all i |
---|
| 320 | // |
---|
| 321 | // abs (this[i] - v[i]) <= e |
---|
| 322 | // |
---|
| 323 | // equalWithRelError (v, e) |
---|
| 324 | // |
---|
| 325 | // Returns true if the coefficients of this and v are the same with |
---|
| 326 | // a relative error of no more than e, i.e., for all i |
---|
| 327 | // |
---|
| 328 | // abs (this[i] - v[i]) <= e * abs (this[i]) |
---|
| 329 | //----------------------------------------------------------------------- |
---|
| 330 | |
---|
| 331 | bool equalWithAbsError (const Vec3<T> &v, T e) const; |
---|
| 332 | bool equalWithRelError (const Vec3<T> &v, T e) const; |
---|
| 333 | |
---|
| 334 | //------------ |
---|
| 335 | // Dot product |
---|
| 336 | //------------ |
---|
| 337 | |
---|
| 338 | T dot (const Vec3 &v) const; |
---|
| 339 | T operator ^ (const Vec3 &v) const; |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | //--------------------------- |
---|
| 343 | // Right-handed cross product |
---|
| 344 | //--------------------------- |
---|
| 345 | |
---|
| 346 | Vec3 cross (const Vec3 &v) const; |
---|
| 347 | const Vec3 & operator %= (const Vec3 &v); |
---|
| 348 | Vec3 operator % (const Vec3 &v) const; |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | //------------------------ |
---|
| 352 | // Component-wise addition |
---|
| 353 | //------------------------ |
---|
| 354 | |
---|
| 355 | const Vec3 & operator += (const Vec3 &v); |
---|
| 356 | Vec3 operator + (const Vec3 &v) const; |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | //--------------------------- |
---|
| 360 | // Component-wise subtraction |
---|
| 361 | //--------------------------- |
---|
| 362 | |
---|
| 363 | const Vec3 & operator -= (const Vec3 &v); |
---|
| 364 | Vec3 operator - (const Vec3 &v) const; |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | //------------------------------------ |
---|
| 368 | // Component-wise multiplication by -1 |
---|
| 369 | //------------------------------------ |
---|
| 370 | |
---|
| 371 | Vec3 operator - () const; |
---|
| 372 | const Vec3 & negate (); |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | //------------------------------ |
---|
| 376 | // Component-wise multiplication |
---|
| 377 | //------------------------------ |
---|
| 378 | |
---|
| 379 | const Vec3 & operator *= (const Vec3 &v); |
---|
| 380 | const Vec3 & operator *= (T a); |
---|
| 381 | Vec3 operator * (const Vec3 &v) const; |
---|
| 382 | Vec3 operator * (T a) const; |
---|
| 383 | |
---|
| 384 | |
---|
| 385 | //------------------------ |
---|
| 386 | // Component-wise division |
---|
| 387 | //------------------------ |
---|
| 388 | |
---|
| 389 | const Vec3 & operator /= (const Vec3 &v); |
---|
| 390 | const Vec3 & operator /= (T a); |
---|
| 391 | Vec3 operator / (const Vec3 &v) const; |
---|
| 392 | Vec3 operator / (T a) const; |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | //---------------------------------------------------------------- |
---|
| 396 | // Length and normalization: If v.length() is 0.0, v.normalize() |
---|
| 397 | // and v.normalized() produce a null vector; v.normalizeExc() and |
---|
| 398 | // v.normalizedExc() throw a NullVecExc. |
---|
| 399 | // v.normalizeNonNull() and v.normalizedNonNull() are slightly |
---|
| 400 | // faster than the other normalization routines, but if v.length() |
---|
| 401 | // is 0.0, the result is undefined. |
---|
| 402 | //---------------------------------------------------------------- |
---|
| 403 | |
---|
| 404 | T length () const; |
---|
| 405 | T length2 () const; |
---|
| 406 | |
---|
| 407 | const Vec3 & normalize (); // modifies *this |
---|
| 408 | const Vec3 & normalizeExc () throw (Iex::MathExc); |
---|
| 409 | const Vec3 & normalizeNonNull (); |
---|
| 410 | |
---|
| 411 | Vec3<T> normalized () const; // does not modify *this |
---|
| 412 | Vec3<T> normalizedExc () const throw (Iex::MathExc); |
---|
| 413 | Vec3<T> normalizedNonNull () const; |
---|
| 414 | |
---|
| 415 | |
---|
| 416 | //-------------------------------------------------------- |
---|
| 417 | // Number of dimensions, i.e. number of elements in a Vec3 |
---|
| 418 | //-------------------------------------------------------- |
---|
| 419 | |
---|
| 420 | static unsigned int dimensions() {return 3;} |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | //------------------------------------------------- |
---|
| 424 | // Limitations of type T (see also class limits<T>) |
---|
| 425 | //------------------------------------------------- |
---|
| 426 | |
---|
| 427 | static T baseTypeMin() {return limits<T>::min();} |
---|
| 428 | static T baseTypeMax() {return limits<T>::max();} |
---|
| 429 | static T baseTypeSmallest() {return limits<T>::smallest();} |
---|
| 430 | static T baseTypeEpsilon() {return limits<T>::epsilon();} |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | //-------------------------------------------------------------- |
---|
| 434 | // Base type -- in templates, which accept a parameter, V, which |
---|
| 435 | // could be either a Vec2<T> or a Vec3<T>, you can refer to T as |
---|
| 436 | // V::BaseType |
---|
| 437 | //-------------------------------------------------------------- |
---|
| 438 | |
---|
| 439 | typedef T BaseType; |
---|
| 440 | }; |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | //-------------- |
---|
| 444 | // Stream output |
---|
| 445 | //-------------- |
---|
| 446 | |
---|
| 447 | template <class T> |
---|
| 448 | std::ostream & operator << (std::ostream &s, const Vec2<T> &v); |
---|
| 449 | |
---|
| 450 | template <class T> |
---|
| 451 | std::ostream & operator << (std::ostream &s, const Vec3<T> &v); |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | //---------------------------------------------------- |
---|
| 455 | // Reverse multiplication: S * Vec2<T> and S * Vec3<T> |
---|
| 456 | //---------------------------------------------------- |
---|
| 457 | |
---|
| 458 | template <class T> Vec2<T> operator * (T a, const Vec2<T> &v); |
---|
| 459 | template <class T> Vec3<T> operator * (T a, const Vec3<T> &v); |
---|
| 460 | |
---|
| 461 | |
---|
| 462 | //------------------------- |
---|
| 463 | // Typedefs for convenience |
---|
| 464 | //------------------------- |
---|
| 465 | |
---|
| 466 | typedef Vec2 <short> V2s; |
---|
| 467 | typedef Vec2 <int> V2i; |
---|
| 468 | typedef Vec2 <float> V2f; |
---|
| 469 | typedef Vec2 <double> V2d; |
---|
| 470 | typedef Vec3 <short> V3s; |
---|
| 471 | typedef Vec3 <int> V3i; |
---|
| 472 | typedef Vec3 <float> V3f; |
---|
| 473 | typedef Vec3 <double> V3d; |
---|
| 474 | |
---|
| 475 | |
---|
| 476 | //------------------------------------------------------------------- |
---|
| 477 | // Specializations for Vec2<short>, Vec2<int>, Vec3<short>, Vec3<int> |
---|
| 478 | //------------------------------------------------------------------- |
---|
| 479 | |
---|
| 480 | // Vec2<short> |
---|
| 481 | |
---|
| 482 | template <> short |
---|
| 483 | Vec2<short>::length () const; |
---|
| 484 | |
---|
| 485 | template <> const Vec2<short> & |
---|
| 486 | Vec2<short>::normalize (); |
---|
| 487 | |
---|
| 488 | template <> const Vec2<short> & |
---|
| 489 | Vec2<short>::normalizeExc () throw (Iex::MathExc); |
---|
| 490 | |
---|
| 491 | template <> const Vec2<short> & |
---|
| 492 | Vec2<short>::normalizeNonNull (); |
---|
| 493 | |
---|
| 494 | template <> Vec2<short> |
---|
| 495 | Vec2<short>::normalized () const; |
---|
| 496 | |
---|
| 497 | template <> Vec2<short> |
---|
| 498 | Vec2<short>::normalizedExc () const throw (Iex::MathExc); |
---|
| 499 | |
---|
| 500 | template <> Vec2<short> |
---|
| 501 | Vec2<short>::normalizedNonNull () const; |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | // Vec2<int> |
---|
| 505 | |
---|
| 506 | template <> int |
---|
| 507 | Vec2<int>::length () const; |
---|
| 508 | |
---|
| 509 | template <> const Vec2<int> & |
---|
| 510 | Vec2<int>::normalize (); |
---|
| 511 | |
---|
| 512 | template <> const Vec2<int> & |
---|
| 513 | Vec2<int>::normalizeExc () throw (Iex::MathExc); |
---|
| 514 | |
---|
| 515 | template <> const Vec2<int> & |
---|
| 516 | Vec2<int>::normalizeNonNull (); |
---|
| 517 | |
---|
| 518 | template <> Vec2<int> |
---|
| 519 | Vec2<int>::normalized () const; |
---|
| 520 | |
---|
| 521 | template <> Vec2<int> |
---|
| 522 | Vec2<int>::normalizedExc () const throw (Iex::MathExc); |
---|
| 523 | |
---|
| 524 | template <> Vec2<int> |
---|
| 525 | Vec2<int>::normalizedNonNull () const; |
---|
| 526 | |
---|
| 527 | |
---|
| 528 | // Vec3<short> |
---|
| 529 | |
---|
| 530 | template <> short |
---|
| 531 | Vec3<short>::length () const; |
---|
| 532 | |
---|
| 533 | template <> const Vec3<short> & |
---|
| 534 | Vec3<short>::normalize (); |
---|
| 535 | |
---|
| 536 | template <> const Vec3<short> & |
---|
| 537 | Vec3<short>::normalizeExc () throw (Iex::MathExc); |
---|
| 538 | |
---|
| 539 | template <> const Vec3<short> & |
---|
| 540 | Vec3<short>::normalizeNonNull (); |
---|
| 541 | |
---|
| 542 | template <> Vec3<short> |
---|
| 543 | Vec3<short>::normalized () const; |
---|
| 544 | |
---|
| 545 | template <> Vec3<short> |
---|
| 546 | Vec3<short>::normalizedExc () const throw (Iex::MathExc); |
---|
| 547 | |
---|
| 548 | template <> Vec3<short> |
---|
| 549 | Vec3<short>::normalizedNonNull () const; |
---|
| 550 | |
---|
| 551 | |
---|
| 552 | // Vec3<int> |
---|
| 553 | |
---|
| 554 | template <> int |
---|
| 555 | Vec3<int>::length () const; |
---|
| 556 | |
---|
| 557 | template <> const Vec3<int> & |
---|
| 558 | Vec3<int>::normalize (); |
---|
| 559 | |
---|
| 560 | template <> const Vec3<int> & |
---|
| 561 | Vec3<int>::normalizeExc () throw (Iex::MathExc); |
---|
| 562 | |
---|
| 563 | template <> const Vec3<int> & |
---|
| 564 | Vec3<int>::normalizeNonNull (); |
---|
| 565 | |
---|
| 566 | template <> Vec3<int> |
---|
| 567 | Vec3<int>::normalized () const; |
---|
| 568 | |
---|
| 569 | template <> Vec3<int> |
---|
| 570 | Vec3<int>::normalizedExc () const throw (Iex::MathExc); |
---|
| 571 | |
---|
| 572 | template <> Vec3<int> |
---|
| 573 | Vec3<int>::normalizedNonNull () const; |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | //------------------------ |
---|
| 577 | // Implementation of Vec2: |
---|
| 578 | //------------------------ |
---|
| 579 | |
---|
| 580 | template <class T> |
---|
| 581 | inline T & |
---|
| 582 | Vec2<T>::operator [] (int i) |
---|
| 583 | { |
---|
| 584 | return (&x)[i]; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | template <class T> |
---|
| 588 | inline const T & |
---|
| 589 | Vec2<T>::operator [] (int i) const |
---|
| 590 | { |
---|
| 591 | return (&x)[i]; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | template <class T> |
---|
| 595 | inline |
---|
| 596 | Vec2<T>::Vec2 () |
---|
| 597 | { |
---|
| 598 | // empty |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | template <class T> |
---|
| 602 | inline |
---|
| 603 | Vec2<T>::Vec2 (T a) |
---|
| 604 | { |
---|
| 605 | x = y = a; |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | template <class T> |
---|
| 609 | inline |
---|
| 610 | Vec2<T>::Vec2 (T a, T b) |
---|
| 611 | { |
---|
| 612 | x = a; |
---|
| 613 | y = b; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | template <class T> |
---|
| 617 | inline |
---|
| 618 | Vec2<T>::Vec2 (const Vec2 &v) |
---|
| 619 | { |
---|
| 620 | x = v.x; |
---|
| 621 | y = v.y; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | template <class T> |
---|
| 625 | template <class S> |
---|
| 626 | inline |
---|
| 627 | Vec2<T>::Vec2 (const Vec2<S> &v) |
---|
| 628 | { |
---|
| 629 | x = T (v.x); |
---|
| 630 | y = T (v.y); |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | template <class T> |
---|
| 634 | inline const Vec2<T> & |
---|
| 635 | Vec2<T>::operator = (const Vec2 &v) |
---|
| 636 | { |
---|
| 637 | x = v.x; |
---|
| 638 | y = v.y; |
---|
| 639 | return *this; |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | template <class T> |
---|
| 643 | template <class S> |
---|
| 644 | inline void |
---|
| 645 | Vec2<T>::setValue (S a, S b) |
---|
| 646 | { |
---|
| 647 | x = T (a); |
---|
| 648 | y = T (b); |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | template <class T> |
---|
| 652 | template <class S> |
---|
| 653 | inline void |
---|
| 654 | Vec2<T>::setValue (const Vec2<S> &v) |
---|
| 655 | { |
---|
| 656 | x = T (v.x); |
---|
| 657 | y = T (v.y); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | template <class T> |
---|
| 661 | template <class S> |
---|
| 662 | inline void |
---|
| 663 | Vec2<T>::getValue (S &a, S &b) const |
---|
| 664 | { |
---|
| 665 | a = S (x); |
---|
| 666 | b = S (y); |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | template <class T> |
---|
| 670 | template <class S> |
---|
| 671 | inline void |
---|
| 672 | Vec2<T>::getValue (Vec2<S> &v) const |
---|
| 673 | { |
---|
| 674 | v.x = S (x); |
---|
| 675 | v.y = S (y); |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | template <class T> |
---|
| 679 | inline T * |
---|
| 680 | Vec2<T>::getValue() |
---|
| 681 | { |
---|
| 682 | return (T *) &x; |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | template <class T> |
---|
| 686 | inline const T * |
---|
| 687 | Vec2<T>::getValue() const |
---|
| 688 | { |
---|
| 689 | return (const T *) &x; |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | template <class T> |
---|
| 693 | template <class S> |
---|
| 694 | inline bool |
---|
| 695 | Vec2<T>::operator == (const Vec2<S> &v) const |
---|
| 696 | { |
---|
| 697 | return x == v.x && y == v.y; |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | template <class T> |
---|
| 701 | template <class S> |
---|
| 702 | inline bool |
---|
| 703 | Vec2<T>::operator != (const Vec2<S> &v) const |
---|
| 704 | { |
---|
| 705 | return x != v.x || y != v.y; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | template <class T> |
---|
| 709 | bool |
---|
| 710 | Vec2<T>::equalWithAbsError (const Vec2<T> &v, T e) const |
---|
| 711 | { |
---|
| 712 | for (int i = 0; i < 2; i++) |
---|
| 713 | if (!Imath::equalWithAbsError ((*this)[i], v[i], e)) |
---|
| 714 | return false; |
---|
| 715 | |
---|
| 716 | return true; |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | template <class T> |
---|
| 720 | bool |
---|
| 721 | Vec2<T>::equalWithRelError (const Vec2<T> &v, T e) const |
---|
| 722 | { |
---|
| 723 | for (int i = 0; i < 2; i++) |
---|
| 724 | if (!Imath::equalWithRelError ((*this)[i], v[i], e)) |
---|
| 725 | return false; |
---|
| 726 | |
---|
| 727 | return true; |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | template <class T> |
---|
| 731 | inline T |
---|
| 732 | Vec2<T>::dot (const Vec2 &v) const |
---|
| 733 | { |
---|
| 734 | return x * v.x + y * v.y; |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | template <class T> |
---|
| 738 | inline T |
---|
| 739 | Vec2<T>::operator ^ (const Vec2 &v) const |
---|
| 740 | { |
---|
| 741 | return dot (v); |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | template <class T> |
---|
| 745 | inline T |
---|
| 746 | Vec2<T>::cross (const Vec2 &v) const |
---|
| 747 | { |
---|
| 748 | return x * v.y - y * v.x; |
---|
| 749 | |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | template <class T> |
---|
| 753 | inline T |
---|
| 754 | Vec2<T>::operator % (const Vec2 &v) const |
---|
| 755 | { |
---|
| 756 | return x * v.y - y * v.x; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | template <class T> |
---|
| 760 | inline const Vec2<T> & |
---|
| 761 | Vec2<T>::operator += (const Vec2 &v) |
---|
| 762 | { |
---|
| 763 | x += v.x; |
---|
| 764 | y += v.y; |
---|
| 765 | return *this; |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | template <class T> |
---|
| 769 | inline Vec2<T> |
---|
| 770 | Vec2<T>::operator + (const Vec2 &v) const |
---|
| 771 | { |
---|
| 772 | return Vec2 (x + v.x, y + v.y); |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | template <class T> |
---|
| 776 | inline const Vec2<T> & |
---|
| 777 | Vec2<T>::operator -= (const Vec2 &v) |
---|
| 778 | { |
---|
| 779 | x -= v.x; |
---|
| 780 | y -= v.y; |
---|
| 781 | return *this; |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | template <class T> |
---|
| 785 | inline Vec2<T> |
---|
| 786 | Vec2<T>::operator - (const Vec2 &v) const |
---|
| 787 | { |
---|
| 788 | return Vec2 (x - v.x, y - v.y); |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | template <class T> |
---|
| 792 | inline Vec2<T> |
---|
| 793 | Vec2<T>::operator - () const |
---|
| 794 | { |
---|
| 795 | return Vec2 (-x, -y); |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | template <class T> |
---|
| 799 | inline const Vec2<T> & |
---|
| 800 | Vec2<T>::negate () |
---|
| 801 | { |
---|
| 802 | x = -x; |
---|
| 803 | y = -y; |
---|
| 804 | return *this; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | template <class T> |
---|
| 808 | inline const Vec2<T> & |
---|
| 809 | Vec2<T>::operator *= (const Vec2 &v) |
---|
| 810 | { |
---|
| 811 | x *= v.x; |
---|
| 812 | y *= v.y; |
---|
| 813 | return *this; |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | template <class T> |
---|
| 817 | inline const Vec2<T> & |
---|
| 818 | Vec2<T>::operator *= (T a) |
---|
| 819 | { |
---|
| 820 | x *= a; |
---|
| 821 | y *= a; |
---|
| 822 | return *this; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | template <class T> |
---|
| 826 | inline Vec2<T> |
---|
| 827 | Vec2<T>::operator * (const Vec2 &v) const |
---|
| 828 | { |
---|
| 829 | return Vec2 (x * v.x, y * v.y); |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | template <class T> |
---|
| 833 | inline Vec2<T> |
---|
| 834 | Vec2<T>::operator * (T a) const |
---|
| 835 | { |
---|
| 836 | return Vec2 (x * a, y * a); |
---|
| 837 | } |
---|
| 838 | |
---|
| 839 | template <class T> |
---|
| 840 | inline const Vec2<T> & |
---|
| 841 | Vec2<T>::operator /= (const Vec2 &v) |
---|
| 842 | { |
---|
| 843 | x /= v.x; |
---|
| 844 | y /= v.y; |
---|
| 845 | return *this; |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | template <class T> |
---|
| 849 | inline const Vec2<T> & |
---|
| 850 | Vec2<T>::operator /= (T a) |
---|
| 851 | { |
---|
| 852 | x /= a; |
---|
| 853 | y /= a; |
---|
| 854 | return *this; |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | template <class T> |
---|
| 858 | inline Vec2<T> |
---|
| 859 | Vec2<T>::operator / (const Vec2 &v) const |
---|
| 860 | { |
---|
| 861 | return Vec2 (x / v.x, y / v.y); |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | template <class T> |
---|
| 865 | inline Vec2<T> |
---|
| 866 | Vec2<T>::operator / (T a) const |
---|
| 867 | { |
---|
| 868 | return Vec2 (x / a, y / a); |
---|
| 869 | } |
---|
| 870 | |
---|
| 871 | template <class T> |
---|
| 872 | inline T |
---|
| 873 | Vec2<T>::length () const |
---|
| 874 | { |
---|
| 875 | return Math<T>::sqrt (dot (*this)); |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | template <class T> |
---|
| 879 | inline T |
---|
| 880 | Vec2<T>::length2 () const |
---|
| 881 | { |
---|
| 882 | return dot (*this); |
---|
| 883 | } |
---|
| 884 | |
---|
| 885 | template <class T> |
---|
| 886 | const Vec2<T> & |
---|
| 887 | Vec2<T>::normalize () |
---|
| 888 | { |
---|
| 889 | T l = length(); |
---|
| 890 | |
---|
| 891 | if (l != 0) |
---|
| 892 | { |
---|
| 893 | x /= l; |
---|
| 894 | y /= l; |
---|
| 895 | } |
---|
| 896 | |
---|
| 897 | return *this; |
---|
| 898 | } |
---|
| 899 | |
---|
| 900 | template <class T> |
---|
| 901 | const Vec2<T> & |
---|
| 902 | Vec2<T>::normalizeExc () throw (Iex::MathExc) |
---|
| 903 | { |
---|
| 904 | T l = length(); |
---|
| 905 | |
---|
| 906 | if (l == 0) |
---|
| 907 | throw NullVecExc ("Cannot normalize null vector."); |
---|
| 908 | |
---|
| 909 | x /= l; |
---|
| 910 | y /= l; |
---|
| 911 | return *this; |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | template <class T> |
---|
| 915 | inline |
---|
| 916 | const Vec2<T> & |
---|
| 917 | Vec2<T>::normalizeNonNull () |
---|
| 918 | { |
---|
| 919 | T l = length(); |
---|
| 920 | x /= l; |
---|
| 921 | y /= l; |
---|
| 922 | return *this; |
---|
| 923 | } |
---|
| 924 | |
---|
| 925 | template <class T> |
---|
| 926 | Vec2<T> |
---|
| 927 | Vec2<T>::normalized () const |
---|
| 928 | { |
---|
| 929 | T l = length(); |
---|
| 930 | |
---|
| 931 | if (l == 0) |
---|
| 932 | return Vec2 (T (0)); |
---|
| 933 | |
---|
| 934 | return Vec2 (x / l, y / l); |
---|
| 935 | } |
---|
| 936 | |
---|
| 937 | template <class T> |
---|
| 938 | Vec2<T> |
---|
| 939 | Vec2<T>::normalizedExc () const throw (Iex::MathExc) |
---|
| 940 | { |
---|
| 941 | T l = length(); |
---|
| 942 | |
---|
| 943 | if (l == 0) |
---|
| 944 | throw NullVecExc ("Cannot normalize null vector."); |
---|
| 945 | |
---|
| 946 | return Vec2 (x / l, y / l); |
---|
| 947 | } |
---|
| 948 | |
---|
| 949 | template <class T> |
---|
| 950 | inline |
---|
| 951 | Vec2<T> |
---|
| 952 | Vec2<T>::normalizedNonNull () const |
---|
| 953 | { |
---|
| 954 | T l = length(); |
---|
| 955 | return Vec2 (x / l, y / l); |
---|
| 956 | } |
---|
| 957 | |
---|
| 958 | |
---|
| 959 | //----------------------- |
---|
| 960 | // Implementation of Vec3 |
---|
| 961 | //----------------------- |
---|
| 962 | |
---|
| 963 | template <class T> |
---|
| 964 | inline T & |
---|
| 965 | Vec3<T>::operator [] (int i) |
---|
| 966 | { |
---|
| 967 | return (&x)[i]; |
---|
| 968 | } |
---|
| 969 | |
---|
| 970 | template <class T> |
---|
| 971 | inline const T & |
---|
| 972 | Vec3<T>::operator [] (int i) const |
---|
| 973 | { |
---|
| 974 | return (&x)[i]; |
---|
| 975 | } |
---|
| 976 | |
---|
| 977 | template <class T> |
---|
| 978 | inline |
---|
| 979 | Vec3<T>::Vec3 () |
---|
| 980 | { |
---|
| 981 | // empty |
---|
| 982 | } |
---|
| 983 | |
---|
| 984 | template <class T> |
---|
| 985 | inline |
---|
| 986 | Vec3<T>::Vec3 (T a) |
---|
| 987 | { |
---|
| 988 | x = y = z = a; |
---|
| 989 | } |
---|
| 990 | |
---|
| 991 | template <class T> |
---|
| 992 | inline |
---|
| 993 | Vec3<T>::Vec3 (T a, T b, T c) |
---|
| 994 | { |
---|
| 995 | x = a; |
---|
| 996 | y = b; |
---|
| 997 | z = c; |
---|
| 998 | } |
---|
| 999 | |
---|
| 1000 | template <class T> |
---|
| 1001 | inline |
---|
| 1002 | Vec3<T>::Vec3 (const Vec3 &v) |
---|
| 1003 | { |
---|
| 1004 | x = v.x; |
---|
| 1005 | y = v.y; |
---|
| 1006 | z = v.z; |
---|
| 1007 | } |
---|
| 1008 | |
---|
| 1009 | template <class T> |
---|
| 1010 | template <class S> |
---|
| 1011 | inline |
---|
| 1012 | Vec3<T>::Vec3 (const Vec3<S> &v) |
---|
| 1013 | { |
---|
| 1014 | x = T (v.x); |
---|
| 1015 | y = T (v.y); |
---|
| 1016 | z = T (v.z); |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | template <class T> |
---|
| 1020 | inline const Vec3<T> & |
---|
| 1021 | Vec3<T>::operator = (const Vec3 &v) |
---|
| 1022 | { |
---|
| 1023 | x = v.x; |
---|
| 1024 | y = v.y; |
---|
| 1025 | z = v.z; |
---|
| 1026 | return *this; |
---|
| 1027 | } |
---|
| 1028 | |
---|
| 1029 | template <class T> |
---|
| 1030 | template <class S> |
---|
| 1031 | inline void |
---|
| 1032 | Vec3<T>::setValue (S a, S b, S c) |
---|
| 1033 | { |
---|
| 1034 | x = T (a); |
---|
| 1035 | y = T (b); |
---|
| 1036 | z = T (c); |
---|
| 1037 | } |
---|
| 1038 | |
---|
| 1039 | template <class T> |
---|
| 1040 | template <class S> |
---|
| 1041 | inline void |
---|
| 1042 | Vec3<T>::setValue (const Vec3<S> &v) |
---|
| 1043 | { |
---|
| 1044 | x = T (v.x); |
---|
| 1045 | y = T (v.y); |
---|
| 1046 | z = T (v.z); |
---|
| 1047 | } |
---|
| 1048 | |
---|
| 1049 | template <class T> |
---|
| 1050 | template <class S> |
---|
| 1051 | inline void |
---|
| 1052 | Vec3<T>::getValue (S &a, S &b, S &c) const |
---|
| 1053 | { |
---|
| 1054 | a = S (x); |
---|
| 1055 | b = S (y); |
---|
| 1056 | c = S (z); |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | template <class T> |
---|
| 1060 | template <class S> |
---|
| 1061 | inline void |
---|
| 1062 | Vec3<T>::getValue (Vec3<S> &v) const |
---|
| 1063 | { |
---|
| 1064 | v.x = S (x); |
---|
| 1065 | v.y = S (y); |
---|
| 1066 | v.z = S (z); |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | template <class T> |
---|
| 1070 | inline T * |
---|
| 1071 | Vec3<T>::getValue() |
---|
| 1072 | { |
---|
| 1073 | return (T *) &x; |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | template <class T> |
---|
| 1077 | inline const T * |
---|
| 1078 | Vec3<T>::getValue() const |
---|
| 1079 | { |
---|
| 1080 | return (const T *) &x; |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | template <class T> |
---|
| 1084 | template <class S> |
---|
| 1085 | inline bool |
---|
| 1086 | Vec3<T>::operator == (const Vec3<S> &v) const |
---|
| 1087 | { |
---|
| 1088 | return x == v.x && y == v.y && z == v.z; |
---|
| 1089 | } |
---|
| 1090 | |
---|
| 1091 | template <class T> |
---|
| 1092 | template <class S> |
---|
| 1093 | inline bool |
---|
| 1094 | Vec3<T>::operator != (const Vec3<S> &v) const |
---|
| 1095 | { |
---|
| 1096 | return x != v.x || y != v.y || z != v.z; |
---|
| 1097 | } |
---|
| 1098 | |
---|
| 1099 | template <class T> |
---|
| 1100 | bool |
---|
| 1101 | Vec3<T>::equalWithAbsError (const Vec3<T> &v, T e) const |
---|
| 1102 | { |
---|
| 1103 | for (int i = 0; i < 3; i++) |
---|
| 1104 | if (!Imath::equalWithAbsError ((*this)[i], v[i], e)) |
---|
| 1105 | return false; |
---|
| 1106 | |
---|
| 1107 | return true; |
---|
| 1108 | } |
---|
| 1109 | |
---|
| 1110 | template <class T> |
---|
| 1111 | bool |
---|
| 1112 | Vec3<T>::equalWithRelError (const Vec3<T> &v, T e) const |
---|
| 1113 | { |
---|
| 1114 | for (int i = 0; i < 3; i++) |
---|
| 1115 | if (!Imath::equalWithRelError ((*this)[i], v[i], e)) |
---|
| 1116 | return false; |
---|
| 1117 | |
---|
| 1118 | return true; |
---|
| 1119 | } |
---|
| 1120 | |
---|
| 1121 | template <class T> |
---|
| 1122 | inline T |
---|
| 1123 | Vec3<T>::dot (const Vec3 &v) const |
---|
| 1124 | { |
---|
| 1125 | return x * v.x + y * v.y + z * v.z; |
---|
| 1126 | } |
---|
| 1127 | |
---|
| 1128 | template <class T> |
---|
| 1129 | inline T |
---|
| 1130 | Vec3<T>::operator ^ (const Vec3 &v) const |
---|
| 1131 | { |
---|
| 1132 | return dot (v); |
---|
| 1133 | } |
---|
| 1134 | |
---|
| 1135 | template <class T> |
---|
| 1136 | inline Vec3<T> |
---|
| 1137 | Vec3<T>::cross (const Vec3 &v) const |
---|
| 1138 | { |
---|
| 1139 | return Vec3 (y * v.z - z * v.y, |
---|
| 1140 | z * v.x - x * v.z, |
---|
| 1141 | x * v.y - y * v.x); |
---|
| 1142 | } |
---|
| 1143 | |
---|
| 1144 | template <class T> |
---|
| 1145 | inline const Vec3<T> & |
---|
| 1146 | Vec3<T>::operator %= (const Vec3 &v) |
---|
| 1147 | { |
---|
| 1148 | T a = y * v.z - z * v.y; |
---|
| 1149 | T b = z * v.x - x * v.z; |
---|
| 1150 | T c = x * v.y - y * v.x; |
---|
| 1151 | x = a; |
---|
| 1152 | y = b; |
---|
| 1153 | z = c; |
---|
| 1154 | return *this; |
---|
| 1155 | } |
---|
| 1156 | |
---|
| 1157 | template <class T> |
---|
| 1158 | inline Vec3<T> |
---|
| 1159 | Vec3<T>::operator % (const Vec3 &v) const |
---|
| 1160 | { |
---|
| 1161 | return Vec3 (y * v.z - z * v.y, |
---|
| 1162 | z * v.x - x * v.z, |
---|
| 1163 | x * v.y - y * v.x); |
---|
| 1164 | } |
---|
| 1165 | |
---|
| 1166 | template <class T> |
---|
| 1167 | inline const Vec3<T> & |
---|
| 1168 | Vec3<T>::operator += (const Vec3 &v) |
---|
| 1169 | { |
---|
| 1170 | x += v.x; |
---|
| 1171 | y += v.y; |
---|
| 1172 | z += v.z; |
---|
| 1173 | return *this; |
---|
| 1174 | } |
---|
| 1175 | |
---|
| 1176 | template <class T> |
---|
| 1177 | inline Vec3<T> |
---|
| 1178 | Vec3<T>::operator + (const Vec3 &v) const |
---|
| 1179 | { |
---|
| 1180 | return Vec3 (x + v.x, y + v.y, z + v.z); |
---|
| 1181 | } |
---|
| 1182 | |
---|
| 1183 | template <class T> |
---|
| 1184 | inline const Vec3<T> & |
---|
| 1185 | Vec3<T>::operator -= (const Vec3 &v) |
---|
| 1186 | { |
---|
| 1187 | x -= v.x; |
---|
| 1188 | y -= v.y; |
---|
| 1189 | z -= v.z; |
---|
| 1190 | return *this; |
---|
| 1191 | } |
---|
| 1192 | |
---|
| 1193 | template <class T> |
---|
| 1194 | inline Vec3<T> |
---|
| 1195 | Vec3<T>::operator - (const Vec3 &v) const |
---|
| 1196 | { |
---|
| 1197 | return Vec3 (x - v.x, y - v.y, z - v.z); |
---|
| 1198 | } |
---|
| 1199 | |
---|
| 1200 | template <class T> |
---|
| 1201 | inline Vec3<T> |
---|
| 1202 | Vec3<T>::operator - () const |
---|
| 1203 | { |
---|
| 1204 | return Vec3 (-x, -y, -z); |
---|
| 1205 | } |
---|
| 1206 | |
---|
| 1207 | template <class T> |
---|
| 1208 | inline const Vec3<T> & |
---|
| 1209 | Vec3<T>::negate () |
---|
| 1210 | { |
---|
| 1211 | x = -x; |
---|
| 1212 | y = -y; |
---|
| 1213 | z = -z; |
---|
| 1214 | return *this; |
---|
| 1215 | } |
---|
| 1216 | |
---|
| 1217 | template <class T> |
---|
| 1218 | inline const Vec3<T> & |
---|
| 1219 | Vec3<T>::operator *= (const Vec3 &v) |
---|
| 1220 | { |
---|
| 1221 | x *= v.x; |
---|
| 1222 | y *= v.y; |
---|
| 1223 | z *= v.z; |
---|
| 1224 | return *this; |
---|
| 1225 | } |
---|
| 1226 | |
---|
| 1227 | template <class T> |
---|
| 1228 | inline const Vec3<T> & |
---|
| 1229 | Vec3<T>::operator *= (T a) |
---|
| 1230 | { |
---|
| 1231 | x *= a; |
---|
| 1232 | y *= a; |
---|
| 1233 | z *= a; |
---|
| 1234 | return *this; |
---|
| 1235 | } |
---|
| 1236 | |
---|
| 1237 | template <class T> |
---|
| 1238 | inline Vec3<T> |
---|
| 1239 | Vec3<T>::operator * (const Vec3 &v) const |
---|
| 1240 | { |
---|
| 1241 | return Vec3 (x * v.x, y * v.y, z * v.z); |
---|
| 1242 | } |
---|
| 1243 | |
---|
| 1244 | template <class T> |
---|
| 1245 | inline Vec3<T> |
---|
| 1246 | Vec3<T>::operator * (T a) const |
---|
| 1247 | { |
---|
| 1248 | return Vec3 (x * a, y * a, z * a); |
---|
| 1249 | } |
---|
| 1250 | |
---|
| 1251 | template <class T> |
---|
| 1252 | inline const Vec3<T> & |
---|
| 1253 | Vec3<T>::operator /= (const Vec3 &v) |
---|
| 1254 | { |
---|
| 1255 | x /= v.x; |
---|
| 1256 | y /= v.y; |
---|
| 1257 | z /= v.z; |
---|
| 1258 | return *this; |
---|
| 1259 | } |
---|
| 1260 | |
---|
| 1261 | template <class T> |
---|
| 1262 | inline const Vec3<T> & |
---|
| 1263 | Vec3<T>::operator /= (T a) |
---|
| 1264 | { |
---|
| 1265 | x /= a; |
---|
| 1266 | y /= a; |
---|
| 1267 | z /= a; |
---|
| 1268 | return *this; |
---|
| 1269 | } |
---|
| 1270 | |
---|
| 1271 | template <class T> |
---|
| 1272 | inline Vec3<T> |
---|
| 1273 | Vec3<T>::operator / (const Vec3 &v) const |
---|
| 1274 | { |
---|
| 1275 | return Vec3 (x / v.x, y / v.y, z / v.z); |
---|
| 1276 | } |
---|
| 1277 | |
---|
| 1278 | template <class T> |
---|
| 1279 | inline Vec3<T> |
---|
| 1280 | Vec3<T>::operator / (T a) const |
---|
| 1281 | { |
---|
| 1282 | return Vec3 (x / a, y / a, z / a); |
---|
| 1283 | } |
---|
| 1284 | |
---|
| 1285 | |
---|
| 1286 | template <class T> |
---|
| 1287 | inline T |
---|
| 1288 | Vec3<T>::length () const |
---|
| 1289 | { |
---|
| 1290 | return Math<T>::sqrt (dot (*this)); |
---|
| 1291 | } |
---|
| 1292 | |
---|
| 1293 | template <class T> |
---|
| 1294 | inline T |
---|
| 1295 | Vec3<T>::length2 () const |
---|
| 1296 | { |
---|
| 1297 | return dot (*this); |
---|
| 1298 | } |
---|
| 1299 | |
---|
| 1300 | template <class T> |
---|
| 1301 | const Vec3<T> & |
---|
| 1302 | Vec3<T>::normalize () |
---|
| 1303 | { |
---|
| 1304 | T l = length(); |
---|
| 1305 | |
---|
| 1306 | if (l != 0) |
---|
| 1307 | { |
---|
| 1308 | x /= l; |
---|
| 1309 | y /= l; |
---|
| 1310 | z /= l; |
---|
| 1311 | } |
---|
| 1312 | |
---|
| 1313 | return *this; |
---|
| 1314 | } |
---|
| 1315 | |
---|
| 1316 | template <class T> |
---|
| 1317 | const Vec3<T> & |
---|
| 1318 | Vec3<T>::normalizeExc () throw (Iex::MathExc) |
---|
| 1319 | { |
---|
| 1320 | T l = length(); |
---|
| 1321 | |
---|
| 1322 | if (l == 0) |
---|
| 1323 | throw NullVecExc ("Cannot normalize null vector."); |
---|
| 1324 | |
---|
| 1325 | x /= l; |
---|
| 1326 | y /= l; |
---|
| 1327 | z /= l; |
---|
| 1328 | return *this; |
---|
| 1329 | } |
---|
| 1330 | |
---|
| 1331 | template <class T> |
---|
| 1332 | inline |
---|
| 1333 | const Vec3<T> & |
---|
| 1334 | Vec3<T>::normalizeNonNull () |
---|
| 1335 | { |
---|
| 1336 | T l = length(); |
---|
| 1337 | x /= l; |
---|
| 1338 | y /= l; |
---|
| 1339 | z /= l; |
---|
| 1340 | return *this; |
---|
| 1341 | } |
---|
| 1342 | |
---|
| 1343 | template <class T> |
---|
| 1344 | Vec3<T> |
---|
| 1345 | Vec3<T>::normalized () const |
---|
| 1346 | { |
---|
| 1347 | T l = length(); |
---|
| 1348 | |
---|
| 1349 | if (l == 0) |
---|
| 1350 | return Vec3 (T (0)); |
---|
| 1351 | |
---|
| 1352 | return Vec3 (x / l, y / l, z / l); |
---|
| 1353 | } |
---|
| 1354 | |
---|
| 1355 | template <class T> |
---|
| 1356 | Vec3<T> |
---|
| 1357 | Vec3<T>::normalizedExc () const throw (Iex::MathExc) |
---|
| 1358 | { |
---|
| 1359 | T l = length(); |
---|
| 1360 | |
---|
| 1361 | if (l == 0) |
---|
| 1362 | throw NullVecExc ("Cannot normalize null vector."); |
---|
| 1363 | |
---|
| 1364 | return Vec3 (x / l, y / l, z / l); |
---|
| 1365 | } |
---|
| 1366 | |
---|
| 1367 | template <class T> |
---|
| 1368 | inline |
---|
| 1369 | Vec3<T> |
---|
| 1370 | Vec3<T>::normalizedNonNull () const |
---|
| 1371 | { |
---|
| 1372 | T l = length(); |
---|
| 1373 | return Vec3 (x / l, y / l, z / l); |
---|
| 1374 | } |
---|
| 1375 | |
---|
| 1376 | |
---|
| 1377 | //----------------------------- |
---|
| 1378 | // Stream output implementation |
---|
| 1379 | //----------------------------- |
---|
| 1380 | |
---|
| 1381 | template <class T> |
---|
| 1382 | std::ostream & |
---|
| 1383 | operator << (std::ostream &s, const Vec2<T> &v) |
---|
| 1384 | { |
---|
| 1385 | return s << '(' << v.x << ' ' << v.y << ')'; |
---|
| 1386 | } |
---|
| 1387 | |
---|
| 1388 | template <class T> |
---|
| 1389 | std::ostream & |
---|
| 1390 | operator << (std::ostream &s, const Vec3<T> &v) |
---|
| 1391 | { |
---|
| 1392 | return s << '(' << v.x << ' ' << v.y << ' ' << v.z << ')'; |
---|
| 1393 | } |
---|
| 1394 | |
---|
| 1395 | |
---|
| 1396 | //----------------------------------------- |
---|
| 1397 | // Implementation of reverse multiplication |
---|
| 1398 | //----------------------------------------- |
---|
| 1399 | |
---|
| 1400 | template <class T> |
---|
| 1401 | inline Vec2<T> |
---|
| 1402 | operator * (T a, const Vec2<T> &v) |
---|
| 1403 | { |
---|
| 1404 | return Vec2<T> (a * v.x, a * v.y); |
---|
| 1405 | } |
---|
| 1406 | |
---|
| 1407 | template <class T> |
---|
| 1408 | inline Vec3<T> |
---|
| 1409 | operator * (T a, const Vec3<T> &v) |
---|
| 1410 | { |
---|
| 1411 | return Vec3<T> (a * v.x, a * v.y, a * v.z); |
---|
| 1412 | } |
---|
| 1413 | |
---|
| 1414 | |
---|
| 1415 | } // namespace Imath |
---|
| 1416 | |
---|
| 1417 | #endif |
---|