Home | History | Annotate | Download | only in Imath
      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_IMATHSHEAR_H
     38 #define INCLUDED_IMATHSHEAR_H
     39 
     40 //----------------------------------------------------
     41 //
     42 //	Shear6 class template.
     43 //
     44 //----------------------------------------------------
     45 
     46 #include "ImathExc.h"
     47 #include "ImathLimits.h"
     48 #include "ImathMath.h"
     49 #include "ImathVec.h"
     50 
     51 #include <iostream>
     52 
     53 
     54 namespace Imath {
     55 
     56 
     57 
     58 
     59 template <class T> class Shear6
     60 {
     61   public:
     62 
     63     //-------------------
     64     // Access to elements
     65     //-------------------
     66 
     67     T			xy, xz, yz, yx, zx, zy;
     68 
     69     T &			operator [] (int i);
     70     const T &		operator [] (int i) const;
     71 
     72 
     73     //-------------
     74     // Constructors
     75     //-------------
     76 
     77     Shear6 ();			   // (0 0 0 0 0 0)
     78     Shear6 (T XY, T XZ, T YZ);	   // (XY XZ YZ 0 0 0)
     79     Shear6 (const Vec3<T> &v);     // (v.x v.y v.z 0 0 0)
     80     template <class S>             // (v.x v.y v.z 0 0 0)
     81     Shear6 (const Vec3<S> &v);
     82     Shear6 (T XY, T XZ, T YZ,      // (XY XZ YZ YX ZX ZY)
     83         T YX, T ZX, T ZY);
     84 
     85 
     86     //---------------------------------
     87     // Copy constructors and assignment
     88     //---------------------------------
     89 
     90     Shear6 (const Shear6 &h);
     91     template <class S> Shear6 (const Shear6<S> &h);
     92 
     93     const Shear6 &	operator = (const Shear6 &h);
     94     template <class S>
     95     const Shear6 &	operator = (const Vec3<S> &v);
     96 
     97 
     98     //----------------------
     99     // Compatibility with Sb
    100     //----------------------
    101 
    102     template <class S>
    103     void		setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY);
    104 
    105     template <class S>
    106     void		setValue (const Shear6<S> &h);
    107 
    108     template <class S>
    109     void		getValue (S &XY, S &XZ, S &YZ,
    110                   S &YX, S &ZX, S &ZY) const;
    111 
    112     template <class S>
    113     void		getValue (Shear6<S> &h) const;
    114 
    115     T *			getValue();
    116     const T *		getValue() const;
    117 
    118 
    119     //---------
    120     // Equality
    121     //---------
    122 
    123     template <class S>
    124     bool		operator == (const Shear6<S> &h) const;
    125 
    126     template <class S>
    127     bool		operator != (const Shear6<S> &h) const;
    128 
    129     //-----------------------------------------------------------------------
    130     // Compare two shears and test if they are "approximately equal":
    131     //
    132     // equalWithAbsError (h, e)
    133     //
    134     //	    Returns true if the coefficients of this and h are the same with
    135     //	    an absolute error of no more than e, i.e., for all i
    136     //
    137     //      abs (this[i] - h[i]) <= e
    138     //
    139     // equalWithRelError (h, e)
    140     //
    141     //	    Returns true if the coefficients of this and h are the same with
    142     //	    a relative error of no more than e, i.e., for all i
    143     //
    144     //      abs (this[i] - h[i]) <= e * abs (this[i])
    145     //-----------------------------------------------------------------------
    146 
    147     bool		equalWithAbsError (const Shear6<T> &h, T e) const;
    148     bool		equalWithRelError (const Shear6<T> &h, T e) const;
    149 
    150 
    151     //------------------------
    152     // Component-wise addition
    153     //------------------------
    154 
    155     const Shear6 &	operator += (const Shear6 &h);
    156     Shear6		operator + (const Shear6 &h) const;
    157 
    158 
    159     //---------------------------
    160     // Component-wise subtraction
    161     //---------------------------
    162 
    163     const Shear6 &	operator -= (const Shear6 &h);
    164     Shear6		operator - (const Shear6 &h) const;
    165 
    166 
    167     //------------------------------------
    168     // Component-wise multiplication by -1
    169     //------------------------------------
    170 
    171     Shear6		operator - () const;
    172     const Shear6 &	negate ();
    173 
    174 
    175     //------------------------------
    176     // Component-wise multiplication
    177     //------------------------------
    178 
    179     const Shear6 &	operator *= (const Shear6 &h);
    180     const Shear6 &	operator *= (T a);
    181     Shear6		operator * (const Shear6 &h) const;
    182     Shear6		operator * (T a) const;
    183 
    184 
    185     //------------------------
    186     // Component-wise division
    187     //------------------------
    188 
    189     const Shear6 &	operator /= (const Shear6 &h);
    190     const Shear6 &	operator /= (T a);
    191     Shear6		operator / (const Shear6 &h) const;
    192     Shear6		operator / (T a) const;
    193 
    194 
    195     //----------------------------------------------------------
    196     // Number of dimensions, i.e. number of elements in a Shear6
    197     //----------------------------------------------------------
    198 
    199     static unsigned int	dimensions() {return 6;}
    200 
    201 
    202     //-------------------------------------------------
    203     // Limitations of type T (see also class limits<T>)
    204     //-------------------------------------------------
    205 
    206     static T		baseTypeMin()		{return limits<T>::min();}
    207     static T		baseTypeMax()		{return limits<T>::max();}
    208     static T		baseTypeSmallest()	{return limits<T>::smallest();}
    209     static T		baseTypeEpsilon()	{return limits<T>::epsilon();}
    210 
    211 
    212     //--------------------------------------------------------------
    213     // Base type -- in templates, which accept a parameter, V, which
    214     // could be either a Vec2<T> or a Shear6<T>, you can refer to T as
    215     // V::BaseType
    216     //--------------------------------------------------------------
    217 
    218     typedef T		BaseType;
    219 };
    220 
    221 
    222 //--------------
    223 // Stream output
    224 //--------------
    225 
    226 template <class T>
    227 std::ostream &	operator << (std::ostream &s, const Shear6<T> &h);
    228 
    229 
    230 //----------------------------------------------------
    231 // Reverse multiplication: scalar * Shear6<T>
    232 //----------------------------------------------------
    233 
    234 template <class S, class T> Shear6<T>	operator * (S a, const Shear6<T> &h);
    235 
    236 
    237 //-------------------------
    238 // Typedefs for convenience
    239 //-------------------------
    240 
    241 typedef Vec3   <float>  Shear3f;
    242 typedef Vec3   <double> Shear3d;
    243 typedef Shear6 <float>  Shear6f;
    244 typedef Shear6 <double> Shear6d;
    245 
    246 
    247 
    248 
    249 //-----------------------
    250 // Implementation of Shear6
    251 //-----------------------
    252 
    253 template <class T>
    254 inline T &
    255 Shear6<T>::operator [] (int i)
    256 {
    257     return (&xy)[i];
    258 }
    259 
    260 template <class T>
    261 inline const T &
    262 Shear6<T>::operator [] (int i) const
    263 {
    264     return (&xy)[i];
    265 }
    266 
    267 template <class T>
    268 inline
    269 Shear6<T>::Shear6 ()
    270 {
    271     xy = xz = yz = yx = zx = zy = 0;
    272 }
    273 
    274 template <class T>
    275 inline
    276 Shear6<T>::Shear6 (T XY, T XZ, T YZ)
    277 {
    278     xy = XY;
    279     xz = XZ;
    280     yz = YZ;
    281     yx = 0;
    282     zx = 0;
    283     zy = 0;
    284 }
    285 
    286 template <class T>
    287 inline
    288 Shear6<T>::Shear6 (const Vec3<T> &v)
    289 {
    290     xy = v.x;
    291     xz = v.y;
    292     yz = v.z;
    293     yx = 0;
    294     zx = 0;
    295     zy = 0;
    296 }
    297 
    298 template <class T>
    299 template <class S>
    300 inline
    301 Shear6<T>::Shear6 (const Vec3<S> &v)
    302 {
    303     xy = T (v.x);
    304     xz = T (v.y);
    305     yz = T (v.z);
    306     yx = 0;
    307     zx = 0;
    308     zy = 0;
    309 }
    310 
    311 template <class T>
    312 inline
    313 Shear6<T>::Shear6 (T XY, T XZ, T YZ, T YX, T ZX, T ZY)
    314 {
    315     xy = XY;
    316     xz = XZ;
    317     yz = YZ;
    318     yx = YX;
    319     zx = ZX;
    320     zy = ZY;
    321 }
    322 
    323 template <class T>
    324 inline
    325 Shear6<T>::Shear6 (const Shear6 &h)
    326 {
    327     xy = h.xy;
    328     xz = h.xz;
    329     yz = h.yz;
    330     yx = h.yx;
    331     zx = h.zx;
    332     zy = h.zy;
    333 }
    334 
    335 template <class T>
    336 template <class S>
    337 inline
    338 Shear6<T>::Shear6 (const Shear6<S> &h)
    339 {
    340     xy = T (h.xy);
    341     xz = T (h.xz);
    342     yz = T (h.yz);
    343     yx = T (h.yx);
    344     zx = T (h.zx);
    345     zy = T (h.zy);
    346 }
    347 
    348 template <class T>
    349 inline const Shear6<T> &
    350 Shear6<T>::operator = (const Shear6 &h)
    351 {
    352     xy = h.xy;
    353     xz = h.xz;
    354     yz = h.yz;
    355     yx = h.yx;
    356     zx = h.zx;
    357     zy = h.zy;
    358     return *this;
    359 }
    360 
    361 template <class T>
    362 template <class S>
    363 inline const Shear6<T> &
    364 Shear6<T>::operator = (const Vec3<S> &v)
    365 {
    366     xy = T (v.x);
    367     xz = T (v.y);
    368     yz = T (v.z);
    369     yx = 0;
    370     zx = 0;
    371     zy = 0;
    372     return *this;
    373 }
    374 
    375 template <class T>
    376 template <class S>
    377 inline void
    378 Shear6<T>::setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY)
    379 {
    380     xy = T (XY);
    381     xz = T (XZ);
    382     yz = T (YZ);
    383     yx = T (YX);
    384     zx = T (ZX);
    385     zy = T (ZY);
    386 }
    387 
    388 template <class T>
    389 template <class S>
    390 inline void
    391 Shear6<T>::setValue (const Shear6<S> &h)
    392 {
    393     xy = T (h.xy);
    394     xz = T (h.xz);
    395     yz = T (h.yz);
    396     yx = T (h.yx);
    397     zx = T (h.zx);
    398     zy = T (h.zy);
    399 }
    400 
    401 template <class T>
    402 template <class S>
    403 inline void
    404 Shear6<T>::getValue (S &XY, S &XZ, S &YZ, S &YX, S &ZX, S &ZY) const
    405 {
    406     XY = S (xy);
    407     XZ = S (xz);
    408     YZ = S (yz);
    409     YX = S (yx);
    410     ZX = S (zx);
    411     ZY = S (zy);
    412 }
    413 
    414 template <class T>
    415 template <class S>
    416 inline void
    417 Shear6<T>::getValue (Shear6<S> &h) const
    418 {
    419     h.xy = S (xy);
    420     h.xz = S (xz);
    421     h.yz = S (yz);
    422     h.yx = S (yx);
    423     h.zx = S (zx);
    424     h.zy = S (zy);
    425 }
    426 
    427 template <class T>
    428 inline T *
    429 Shear6<T>::getValue()
    430 {
    431     return (T *) &xy;
    432 }
    433 
    434 template <class T>
    435 inline const T *
    436 Shear6<T>::getValue() const
    437 {
    438     return (const T *) &xy;
    439 }
    440 
    441 template <class T>
    442 template <class S>
    443 inline bool
    444 Shear6<T>::operator == (const Shear6<S> &h) const
    445 {
    446     return xy == h.xy  &&  xz == h.xz  &&  yz == h.yz  &&
    447        yx == h.yx  &&  zx == h.zx  &&  zy == h.zy;
    448 }
    449 
    450 template <class T>
    451 template <class S>
    452 inline bool
    453 Shear6<T>::operator != (const Shear6<S> &h) const
    454 {
    455     return xy != h.xy  ||  xz != h.xz  ||  yz != h.yz  ||
    456        yx != h.yx  ||  zx != h.zx  ||  zy != h.zy;
    457 }
    458 
    459 template <class T>
    460 bool
    461 Shear6<T>::equalWithAbsError (const Shear6<T> &h, T e) const
    462 {
    463     for (int i = 0; i < 6; i++)
    464     if (!Imath::equalWithAbsError ((*this)[i], h[i], e))
    465         return false;
    466 
    467     return true;
    468 }
    469 
    470 template <class T>
    471 bool
    472 Shear6<T>::equalWithRelError (const Shear6<T> &h, T e) const
    473 {
    474     for (int i = 0; i < 6; i++)
    475     if (!Imath::equalWithRelError ((*this)[i], h[i], e))
    476         return false;
    477 
    478     return true;
    479 }
    480 
    481 
    482 template <class T>
    483 inline const Shear6<T> &
    484 Shear6<T>::operator += (const Shear6 &h)
    485 {
    486     xy += h.xy;
    487     xz += h.xz;
    488     yz += h.yz;
    489     yx += h.yx;
    490     zx += h.zx;
    491     zy += h.zy;
    492     return *this;
    493 }
    494 
    495 template <class T>
    496 inline Shear6<T>
    497 Shear6<T>::operator + (const Shear6 &h) const
    498 {
    499     return Shear6 (xy + h.xy, xz + h.xz, yz + h.yz,
    500            yx + h.yx, zx + h.zx, zy + h.zy);
    501 }
    502 
    503 template <class T>
    504 inline const Shear6<T> &
    505 Shear6<T>::operator -= (const Shear6 &h)
    506 {
    507     xy -= h.xy;
    508     xz -= h.xz;
    509     yz -= h.yz;
    510     yx -= h.yx;
    511     zx -= h.zx;
    512     zy -= h.zy;
    513     return *this;
    514 }
    515 
    516 template <class T>
    517 inline Shear6<T>
    518 Shear6<T>::operator - (const Shear6 &h) const
    519 {
    520     return Shear6 (xy - h.xy, xz - h.xz, yz - h.yz,
    521            yx - h.yx, zx - h.zx, zy - h.zy);
    522 }
    523 
    524 template <class T>
    525 inline Shear6<T>
    526 Shear6<T>::operator - () const
    527 {
    528     return Shear6 (-xy, -xz, -yz, -yx, -zx, -zy);
    529 }
    530 
    531 template <class T>
    532 inline const Shear6<T> &
    533 Shear6<T>::negate ()
    534 {
    535     xy = -xy;
    536     xz = -xz;
    537     yz = -yz;
    538     yx = -yx;
    539     zx = -zx;
    540     zy = -zy;
    541     return *this;
    542 }
    543 
    544 template <class T>
    545 inline const Shear6<T> &
    546 Shear6<T>::operator *= (const Shear6 &h)
    547 {
    548     xy *= h.xy;
    549     xz *= h.xz;
    550     yz *= h.yz;
    551     yx *= h.yx;
    552     zx *= h.zx;
    553     zy *= h.zy;
    554     return *this;
    555 }
    556 
    557 template <class T>
    558 inline const Shear6<T> &
    559 Shear6<T>::operator *= (T a)
    560 {
    561     xy *= a;
    562     xz *= a;
    563     yz *= a;
    564     yx *= a;
    565     zx *= a;
    566     zy *= a;
    567     return *this;
    568 }
    569 
    570 template <class T>
    571 inline Shear6<T>
    572 Shear6<T>::operator * (const Shear6 &h) const
    573 {
    574     return Shear6 (xy * h.xy, xz * h.xz, yz * h.yz,
    575            yx * h.yx, zx * h.zx, zy * h.zy);
    576 }
    577 
    578 template <class T>
    579 inline Shear6<T>
    580 Shear6<T>::operator * (T a) const
    581 {
    582     return Shear6 (xy * a, xz * a, yz * a,
    583            yx * a, zx * a, zy * a);
    584 }
    585 
    586 template <class T>
    587 inline const Shear6<T> &
    588 Shear6<T>::operator /= (const Shear6 &h)
    589 {
    590     xy /= h.xy;
    591     xz /= h.xz;
    592     yz /= h.yz;
    593     yx /= h.yx;
    594     zx /= h.zx;
    595     zy /= h.zy;
    596     return *this;
    597 }
    598 
    599 template <class T>
    600 inline const Shear6<T> &
    601 Shear6<T>::operator /= (T a)
    602 {
    603     xy /= a;
    604     xz /= a;
    605     yz /= a;
    606     yx /= a;
    607     zx /= a;
    608     zy /= a;
    609     return *this;
    610 }
    611 
    612 template <class T>
    613 inline Shear6<T>
    614 Shear6<T>::operator / (const Shear6 &h) const
    615 {
    616     return Shear6 (xy / h.xy, xz / h.xz, yz / h.yz,
    617            yx / h.yx, zx / h.zx, zy / h.zy);
    618 }
    619 
    620 template <class T>
    621 inline Shear6<T>
    622 Shear6<T>::operator / (T a) const
    623 {
    624     return Shear6 (xy / a, xz / a, yz / a,
    625            yx / a, zx / a, zy / a);
    626 }
    627 
    628 
    629 //-----------------------------
    630 // Stream output implementation
    631 //-----------------------------
    632 
    633 template <class T>
    634 std::ostream &
    635 operator << (std::ostream &s, const Shear6<T> &h)
    636 {
    637     return s << '('
    638          << h.xy << ' ' << h.xz << ' ' << h.yz
    639          << h.yx << ' ' << h.zx << ' ' << h.zy
    640          << ')';
    641 }
    642 
    643 
    644 //-----------------------------------------
    645 // Implementation of reverse multiplication
    646 //-----------------------------------------
    647 
    648 template <class S, class T>
    649 inline Shear6<T>
    650 operator * (S a, const Shear6<T> &h)
    651 {
    652     return Shear6<T> (a * h.xy, a * h.xz, a * h.yz,
    653               a * h.yx, a * h.zx, a * h.zy);
    654 }
    655 
    656 
    657 } // namespace Imath
    658 
    659 #endif
    660