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
54IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
55
56template <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
196 static unsigned int dimensions() {return 6;}
197
198
199 //-------------------------------------------------
200 // Limitations of type T (see also class limits<T>)
201 //-------------------------------------------------
202
203 static T baseTypeMin() {return limits<T>::min();}
204 static T baseTypeMax() {return limits<T>::max();}
205 static T baseTypeSmallest() {return limits<T>::smallest();}
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
223template <class T>
224std::ostream & operator << (std::ostream &s, const Shear6<T> &h);
225
226
227//----------------------------------------------------
228// Reverse multiplication: scalar * Shear6<T>
229//----------------------------------------------------
230
231template <class S, class T> Shear6<T> operator * (S a, const Shear6<T> &h);
232
233
234//-------------------------
235// Typedefs for convenience
236//-------------------------
237
238typedef Vec3 <float> Shear3f;
239typedef Vec3 <double> Shear3d;
240typedef Shear6 <float> Shear6f;
241typedef Shear6 <double> Shear6d;
242
243
244
245
246//-----------------------
247// Implementation of Shear6
248//-----------------------
249
250template <class T>
251inline T &
252Shear6<T>::operator [] (int i)
253{
254 return (&xy)[i];
255}
256
257template <class T>
258inline const T &
259Shear6<T>::operator [] (int i) const
260{
261 return (&xy)[i];
262}
263
264template <class T>
265inline
266Shear6<T>::Shear6 ()
267{
268 xy = xz = yz = yx = zx = zy = 0;
269}
270
271template <class T>
272inline
273Shear6<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
283template <class T>
284inline
285Shear6<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
295template <class T>
296template <class S>
297inline
298Shear6<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
308template <class T>
309inline
310Shear6<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
320template <class T>
321inline
322Shear6<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
332template <class T>
333template <class S>
334inline
335Shear6<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
345template <class T>
346inline const Shear6<T> &
347Shear6<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
358template <class T>
359template <class S>
360inline const Shear6<T> &
361Shear6<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
372template <class T>
373template <class S>
374inline void
375Shear6<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
385template <class T>
386template <class S>
387inline void
388Shear6<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
398template <class T>
399template <class S>
400inline void
401Shear6<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
411template <class T>
412template <class S>
413inline void
414Shear6<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
424template <class T>
425inline T *
426Shear6<T>::getValue()
427{
428 return (T *) &xy;
429}
430
431template <class T>
432inline const T *
433Shear6<T>::getValue() const
434{
435 return (const T *) &xy;
436}
437
438template <class T>
439template <class S>
440inline bool
441Shear6<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
447template <class T>
448template <class S>
449inline bool
450Shear6<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
456template <class T>
457bool
458Shear6<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
467template <class T>
468bool
469Shear6<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
479template <class T>
480inline const Shear6<T> &
481Shear6<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
492template <class T>
493inline Shear6<T>
494Shear6<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
500template <class T>
501inline const Shear6<T> &
502Shear6<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
513template <class T>
514inline Shear6<T>
515Shear6<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
521template <class T>
522inline Shear6<T>
523Shear6<T>::operator - () const
524{
525 return Shear6 (-xy, -xz, -yz, -yx, -zx, -zy);
526}
527
528template <class T>
529inline const Shear6<T> &
530Shear6<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
541template <class T>
542inline const Shear6<T> &
543Shear6<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
554template <class T>
555inline const Shear6<T> &
556Shear6<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
567template <class T>
568inline Shear6<T>
569Shear6<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
575template <class T>
576inline Shear6<T>
577Shear6<T>::operator * (T a) const
578{
579 return Shear6 (xy * a, xz * a, yz * a,
580 yx * a, zx * a, zy * a);
581}
582
583template <class T>
584inline const Shear6<T> &
585Shear6<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
596template <class T>
597inline const Shear6<T> &
598Shear6<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
609template <class T>
610inline Shear6<T>
611Shear6<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
617template <class T>
618inline Shear6<T>
619Shear6<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
630template <class T>
631std::ostream &
632operator << (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
645template <class S, class T>
646inline Shear6<T>
647operator * (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
654IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
655
656#endif // INCLUDED_IMATHSHEAR_H
657