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