1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
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
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.
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.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHSHEAR_H
38 #define INCLUDED_IMATHSHEAR_H
40 //----------------------------------------------------
42 // Shear6 class template.
44 //----------------------------------------------------
47 #include "ImathLimits.h"
48 #include "ImathMath.h"
59 template <class T> class Shear6
67 T xy, xz, yz, yx, zx, zy;
69 T & operator [] (int i);
70 const T & operator [] (int i) const;
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)
86 //---------------------------------
87 // Copy constructors and assignment
88 //---------------------------------
90 Shear6 (const Shear6 &h);
91 template <class S> Shear6 (const Shear6<S> &h);
93 const Shear6 & operator = (const Shear6 &h);
95 const Shear6 & operator = (const Vec3<S> &v);
98 //----------------------
99 // Compatibility with Sb
100 //----------------------
103 void setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY);
106 void setValue (const Shear6<S> &h);
109 void getValue (S &XY, S &XZ, S &YZ,
110 S &YX, S &ZX, S &ZY) const;
113 void getValue (Shear6<S> &h) const;
116 const T * getValue() const;
124 bool operator == (const Shear6<S> &h) const;
127 bool operator != (const Shear6<S> &h) const;
129 //-----------------------------------------------------------------------
130 // Compare two shears and test if they are "approximately equal":
132 // equalWithAbsError (h, e)
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
137 // abs (this[i] - h[i]) <= e
139 // equalWithRelError (h, e)
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
144 // abs (this[i] - h[i]) <= e * abs (this[i])
145 //-----------------------------------------------------------------------
147 bool equalWithAbsError (const Shear6<T> &h, T e) const;
148 bool equalWithRelError (const Shear6<T> &h, T e) const;
151 //------------------------
152 // Component-wise addition
153 //------------------------
155 const Shear6 & operator += (const Shear6 &h);
156 Shear6 operator + (const Shear6 &h) const;
159 //---------------------------
160 // Component-wise subtraction
161 //---------------------------
163 const Shear6 & operator -= (const Shear6 &h);
164 Shear6 operator - (const Shear6 &h) const;
167 //------------------------------------
168 // Component-wise multiplication by -1
169 //------------------------------------
171 Shear6 operator - () const;
172 const Shear6 & negate ();
175 //------------------------------
176 // Component-wise multiplication
177 //------------------------------
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;
185 //------------------------
186 // Component-wise division
187 //------------------------
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;
195 //----------------------------------------------------------
196 // Number of dimensions, i.e. number of elements in a Shear6
197 //----------------------------------------------------------
199 static unsigned int dimensions() {return 6;}
202 //-------------------------------------------------
203 // Limitations of type T (see also class limits<T>)
204 //-------------------------------------------------
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();}
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
216 //--------------------------------------------------------------
227 std::ostream & operator << (std::ostream &s, const Shear6<T> &h);
230 //----------------------------------------------------
231 // Reverse multiplication: scalar * Shear6<T>
232 //----------------------------------------------------
234 template <class S, class T> Shear6<T> operator * (S a, const Shear6<T> &h);
237 //-------------------------
238 // Typedefs for convenience
239 //-------------------------
241 typedef Vec3 <float> Shear3f;
242 typedef Vec3 <double> Shear3d;
243 typedef Shear6 <float> Shear6f;
244 typedef Shear6 <double> Shear6d;
249 //-----------------------
250 // Implementation of Shear6
251 //-----------------------
255 Shear6<T>::operator [] (int i)
262 Shear6<T>::operator [] (int i) const
271 xy = xz = yz = yx = zx = zy = 0;
276 Shear6<T>::Shear6 (T XY, T XZ, T YZ)
288 Shear6<T>::Shear6 (const Vec3<T> &v)
301 Shear6<T>::Shear6 (const Vec3<S> &v)
313 Shear6<T>::Shear6 (T XY, T XZ, T YZ, T YX, T ZX, T ZY)
325 Shear6<T>::Shear6 (const Shear6 &h)
338 Shear6<T>::Shear6 (const Shear6<S> &h)
349 inline const Shear6<T> &
350 Shear6<T>::operator = (const Shear6 &h)
363 inline const Shear6<T> &
364 Shear6<T>::operator = (const Vec3<S> &v)
378 Shear6<T>::setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY)
391 Shear6<T>::setValue (const Shear6<S> &h)
404 Shear6<T>::getValue (S &XY, S &XZ, S &YZ, S &YX, S &ZX, S &ZY) const
417 Shear6<T>::getValue (Shear6<S> &h) const
429 Shear6<T>::getValue()
436 Shear6<T>::getValue() const
438 return (const T *) &xy;
444 Shear6<T>::operator == (const Shear6<S> &h) const
446 return xy == h.xy && xz == h.xz && yz == h.yz &&
447 yx == h.yx && zx == h.zx && zy == h.zy;
453 Shear6<T>::operator != (const Shear6<S> &h) const
455 return xy != h.xy || xz != h.xz || yz != h.yz ||
456 yx != h.yx || zx != h.zx || zy != h.zy;
461 Shear6<T>::equalWithAbsError (const Shear6<T> &h, T e) const
463 for (int i = 0; i < 6; i++)
464 if (!Imath::equalWithAbsError ((*this)[i], h[i], e))
472 Shear6<T>::equalWithRelError (const Shear6<T> &h, T e) const
474 for (int i = 0; i < 6; i++)
475 if (!Imath::equalWithRelError ((*this)[i], h[i], e))
483 inline const Shear6<T> &
484 Shear6<T>::operator += (const Shear6 &h)
497 Shear6<T>::operator + (const Shear6 &h) const
499 return Shear6 (xy + h.xy, xz + h.xz, yz + h.yz,
500 yx + h.yx, zx + h.zx, zy + h.zy);
504 inline const Shear6<T> &
505 Shear6<T>::operator -= (const Shear6 &h)
518 Shear6<T>::operator - (const Shear6 &h) const
520 return Shear6 (xy - h.xy, xz - h.xz, yz - h.yz,
521 yx - h.yx, zx - h.zx, zy - h.zy);
526 Shear6<T>::operator - () const
528 return Shear6 (-xy, -xz, -yz, -yx, -zx, -zy);
532 inline const Shear6<T> &
545 inline const Shear6<T> &
546 Shear6<T>::operator *= (const Shear6 &h)
558 inline const Shear6<T> &
559 Shear6<T>::operator *= (T a)
572 Shear6<T>::operator * (const Shear6 &h) const
574 return Shear6 (xy * h.xy, xz * h.xz, yz * h.yz,
575 yx * h.yx, zx * h.zx, zy * h.zy);
580 Shear6<T>::operator * (T a) const
582 return Shear6 (xy * a, xz * a, yz * a,
583 yx * a, zx * a, zy * a);
587 inline const Shear6<T> &
588 Shear6<T>::operator /= (const Shear6 &h)
600 inline const Shear6<T> &
601 Shear6<T>::operator /= (T a)
614 Shear6<T>::operator / (const Shear6 &h) const
616 return Shear6 (xy / h.xy, xz / h.xz, yz / h.yz,
617 yx / h.yx, zx / h.zx, zy / h.zy);
622 Shear6<T>::operator / (T a) const
624 return Shear6 (xy / a, xz / a, yz / a,
625 yx / a, zx / a, zy / a);
629 //-----------------------------
630 // Stream output implementation
631 //-----------------------------
635 operator << (std::ostream &s, const Shear6<T> &h)
638 << h.xy << ' ' << h.xz << ' ' << h.yz
639 << h.yx << ' ' << h.zx << ' ' << h.zy
644 //-----------------------------------------
645 // Implementation of reverse multiplication
646 //-----------------------------------------
648 template <class S, class T>
650 operator * (S a, const Shear6<T> &h)
652 return Shear6<T> (a * h.xy, a * h.xz, a * h.yz,
653 a * h.yx, a * h.zx, a * h.zy);