Path: blob/master/3rdparty/openexr/Imath/ImathShear.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHSHEAR_H37#define INCLUDED_IMATHSHEAR_H3839//----------------------------------------------------40//41// Shear6 class template.42//43//----------------------------------------------------4445#include "ImathExc.h"46#include "ImathLimits.h"47#include "ImathMath.h"48#include "ImathVec.h"4950#include <iostream>515253namespace Imath {5455565758template <class T> class Shear659{60public:6162//-------------------63// Access to elements64//-------------------6566T xy, xz, yz, yx, zx, zy;6768T & operator [] (int i);69const T & operator [] (int i) const;707172//-------------73// Constructors74//-------------7576Shear6 (); // (0 0 0 0 0 0)77Shear6 (T XY, T XZ, T YZ); // (XY XZ YZ 0 0 0)78Shear6 (const Vec3<T> &v); // (v.x v.y v.z 0 0 0)79template <class S> // (v.x v.y v.z 0 0 0)80Shear6 (const Vec3<S> &v);81Shear6 (T XY, T XZ, T YZ, // (XY XZ YZ YX ZX ZY)82T YX, T ZX, T ZY);838485//---------------------------------86// Copy constructors and assignment87//---------------------------------8889Shear6 (const Shear6 &h);90template <class S> Shear6 (const Shear6<S> &h);9192const Shear6 & operator = (const Shear6 &h);93template <class S>94const Shear6 & operator = (const Vec3<S> &v);959697//----------------------98// Compatibility with Sb99//----------------------100101template <class S>102void setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY);103104template <class S>105void setValue (const Shear6<S> &h);106107template <class S>108void getValue (S &XY, S &XZ, S &YZ,109S &YX, S &ZX, S &ZY) const;110111template <class S>112void getValue (Shear6<S> &h) const;113114T * getValue();115const T * getValue() const;116117118//---------119// Equality120//---------121122template <class S>123bool operator == (const Shear6<S> &h) const;124125template <class S>126bool operator != (const Shear6<S> &h) const;127128//-----------------------------------------------------------------------129// Compare two shears and test if they are "approximately equal":130//131// equalWithAbsError (h, e)132//133// Returns true if the coefficients of this and h are the same with134// an absolute error of no more than e, i.e., for all i135//136// abs (this[i] - h[i]) <= e137//138// equalWithRelError (h, e)139//140// Returns true if the coefficients of this and h are the same with141// a relative error of no more than e, i.e., for all i142//143// abs (this[i] - h[i]) <= e * abs (this[i])144//-----------------------------------------------------------------------145146bool equalWithAbsError (const Shear6<T> &h, T e) const;147bool equalWithRelError (const Shear6<T> &h, T e) const;148149150//------------------------151// Component-wise addition152//------------------------153154const Shear6 & operator += (const Shear6 &h);155Shear6 operator + (const Shear6 &h) const;156157158//---------------------------159// Component-wise subtraction160//---------------------------161162const Shear6 & operator -= (const Shear6 &h);163Shear6 operator - (const Shear6 &h) const;164165166//------------------------------------167// Component-wise multiplication by -1168//------------------------------------169170Shear6 operator - () const;171const Shear6 & negate ();172173174//------------------------------175// Component-wise multiplication176//------------------------------177178const Shear6 & operator *= (const Shear6 &h);179const Shear6 & operator *= (T a);180Shear6 operator * (const Shear6 &h) const;181Shear6 operator * (T a) const;182183184//------------------------185// Component-wise division186//------------------------187188const Shear6 & operator /= (const Shear6 &h);189const Shear6 & operator /= (T a);190Shear6 operator / (const Shear6 &h) const;191Shear6 operator / (T a) const;192193194//----------------------------------------------------------195// Number of dimensions, i.e. number of elements in a Shear6196//----------------------------------------------------------197198static unsigned int dimensions() {return 6;}199200201//-------------------------------------------------202// Limitations of type T (see also class limits<T>)203//-------------------------------------------------204205static T baseTypeMin() {return limits<T>::min();}206static T baseTypeMax() {return limits<T>::max();}207static T baseTypeSmallest() {return limits<T>::smallest();}208static T baseTypeEpsilon() {return limits<T>::epsilon();}209210211//--------------------------------------------------------------212// Base type -- in templates, which accept a parameter, V, which213// could be either a Vec2<T> or a Shear6<T>, you can refer to T as214// V::BaseType215//--------------------------------------------------------------216217typedef T BaseType;218};219220221//--------------222// Stream output223//--------------224225template <class T>226std::ostream & operator << (std::ostream &s, const Shear6<T> &h);227228229//----------------------------------------------------230// Reverse multiplication: scalar * Shear6<T>231//----------------------------------------------------232233template <class S, class T> Shear6<T> operator * (S a, const Shear6<T> &h);234235236//-------------------------237// Typedefs for convenience238//-------------------------239240typedef Vec3 <float> Shear3f;241typedef Vec3 <double> Shear3d;242typedef Shear6 <float> Shear6f;243typedef Shear6 <double> Shear6d;244245246247248//-----------------------249// Implementation of Shear6250//-----------------------251252template <class T>253inline T &254Shear6<T>::operator [] (int i)255{256return (&xy)[i];257}258259template <class T>260inline const T &261Shear6<T>::operator [] (int i) const262{263return (&xy)[i];264}265266template <class T>267inline268Shear6<T>::Shear6 ()269{270xy = xz = yz = yx = zx = zy = 0;271}272273template <class T>274inline275Shear6<T>::Shear6 (T XY, T XZ, T YZ)276{277xy = XY;278xz = XZ;279yz = YZ;280yx = 0;281zx = 0;282zy = 0;283}284285template <class T>286inline287Shear6<T>::Shear6 (const Vec3<T> &v)288{289xy = v.x;290xz = v.y;291yz = v.z;292yx = 0;293zx = 0;294zy = 0;295}296297template <class T>298template <class S>299inline300Shear6<T>::Shear6 (const Vec3<S> &v)301{302xy = T (v.x);303xz = T (v.y);304yz = T (v.z);305yx = 0;306zx = 0;307zy = 0;308}309310template <class T>311inline312Shear6<T>::Shear6 (T XY, T XZ, T YZ, T YX, T ZX, T ZY)313{314xy = XY;315xz = XZ;316yz = YZ;317yx = YX;318zx = ZX;319zy = ZY;320}321322template <class T>323inline324Shear6<T>::Shear6 (const Shear6 &h)325{326xy = h.xy;327xz = h.xz;328yz = h.yz;329yx = h.yx;330zx = h.zx;331zy = h.zy;332}333334template <class T>335template <class S>336inline337Shear6<T>::Shear6 (const Shear6<S> &h)338{339xy = T (h.xy);340xz = T (h.xz);341yz = T (h.yz);342yx = T (h.yx);343zx = T (h.zx);344zy = T (h.zy);345}346347template <class T>348inline const Shear6<T> &349Shear6<T>::operator = (const Shear6 &h)350{351xy = h.xy;352xz = h.xz;353yz = h.yz;354yx = h.yx;355zx = h.zx;356zy = h.zy;357return *this;358}359360template <class T>361template <class S>362inline const Shear6<T> &363Shear6<T>::operator = (const Vec3<S> &v)364{365xy = T (v.x);366xz = T (v.y);367yz = T (v.z);368yx = 0;369zx = 0;370zy = 0;371return *this;372}373374template <class T>375template <class S>376inline void377Shear6<T>::setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY)378{379xy = T (XY);380xz = T (XZ);381yz = T (YZ);382yx = T (YX);383zx = T (ZX);384zy = T (ZY);385}386387template <class T>388template <class S>389inline void390Shear6<T>::setValue (const Shear6<S> &h)391{392xy = T (h.xy);393xz = T (h.xz);394yz = T (h.yz);395yx = T (h.yx);396zx = T (h.zx);397zy = T (h.zy);398}399400template <class T>401template <class S>402inline void403Shear6<T>::getValue (S &XY, S &XZ, S &YZ, S &YX, S &ZX, S &ZY) const404{405XY = S (xy);406XZ = S (xz);407YZ = S (yz);408YX = S (yx);409ZX = S (zx);410ZY = S (zy);411}412413template <class T>414template <class S>415inline void416Shear6<T>::getValue (Shear6<S> &h) const417{418h.xy = S (xy);419h.xz = S (xz);420h.yz = S (yz);421h.yx = S (yx);422h.zx = S (zx);423h.zy = S (zy);424}425426template <class T>427inline T *428Shear6<T>::getValue()429{430return (T *) &xy;431}432433template <class T>434inline const T *435Shear6<T>::getValue() const436{437return (const T *) &xy;438}439440template <class T>441template <class S>442inline bool443Shear6<T>::operator == (const Shear6<S> &h) const444{445return xy == h.xy && xz == h.xz && yz == h.yz &&446yx == h.yx && zx == h.zx && zy == h.zy;447}448449template <class T>450template <class S>451inline bool452Shear6<T>::operator != (const Shear6<S> &h) const453{454return xy != h.xy || xz != h.xz || yz != h.yz ||455yx != h.yx || zx != h.zx || zy != h.zy;456}457458template <class T>459bool460Shear6<T>::equalWithAbsError (const Shear6<T> &h, T e) const461{462for (int i = 0; i < 6; i++)463if (!Imath::equalWithAbsError ((*this)[i], h[i], e))464return false;465466return true;467}468469template <class T>470bool471Shear6<T>::equalWithRelError (const Shear6<T> &h, T e) const472{473for (int i = 0; i < 6; i++)474if (!Imath::equalWithRelError ((*this)[i], h[i], e))475return false;476477return true;478}479480481template <class T>482inline const Shear6<T> &483Shear6<T>::operator += (const Shear6 &h)484{485xy += h.xy;486xz += h.xz;487yz += h.yz;488yx += h.yx;489zx += h.zx;490zy += h.zy;491return *this;492}493494template <class T>495inline Shear6<T>496Shear6<T>::operator + (const Shear6 &h) const497{498return Shear6 (xy + h.xy, xz + h.xz, yz + h.yz,499yx + h.yx, zx + h.zx, zy + h.zy);500}501502template <class T>503inline const Shear6<T> &504Shear6<T>::operator -= (const Shear6 &h)505{506xy -= h.xy;507xz -= h.xz;508yz -= h.yz;509yx -= h.yx;510zx -= h.zx;511zy -= h.zy;512return *this;513}514515template <class T>516inline Shear6<T>517Shear6<T>::operator - (const Shear6 &h) const518{519return Shear6 (xy - h.xy, xz - h.xz, yz - h.yz,520yx - h.yx, zx - h.zx, zy - h.zy);521}522523template <class T>524inline Shear6<T>525Shear6<T>::operator - () const526{527return Shear6 (-xy, -xz, -yz, -yx, -zx, -zy);528}529530template <class T>531inline const Shear6<T> &532Shear6<T>::negate ()533{534xy = -xy;535xz = -xz;536yz = -yz;537yx = -yx;538zx = -zx;539zy = -zy;540return *this;541}542543template <class T>544inline const Shear6<T> &545Shear6<T>::operator *= (const Shear6 &h)546{547xy *= h.xy;548xz *= h.xz;549yz *= h.yz;550yx *= h.yx;551zx *= h.zx;552zy *= h.zy;553return *this;554}555556template <class T>557inline const Shear6<T> &558Shear6<T>::operator *= (T a)559{560xy *= a;561xz *= a;562yz *= a;563yx *= a;564zx *= a;565zy *= a;566return *this;567}568569template <class T>570inline Shear6<T>571Shear6<T>::operator * (const Shear6 &h) const572{573return Shear6 (xy * h.xy, xz * h.xz, yz * h.yz,574yx * h.yx, zx * h.zx, zy * h.zy);575}576577template <class T>578inline Shear6<T>579Shear6<T>::operator * (T a) const580{581return Shear6 (xy * a, xz * a, yz * a,582yx * a, zx * a, zy * a);583}584585template <class T>586inline const Shear6<T> &587Shear6<T>::operator /= (const Shear6 &h)588{589xy /= h.xy;590xz /= h.xz;591yz /= h.yz;592yx /= h.yx;593zx /= h.zx;594zy /= h.zy;595return *this;596}597598template <class T>599inline const Shear6<T> &600Shear6<T>::operator /= (T a)601{602xy /= a;603xz /= a;604yz /= a;605yx /= a;606zx /= a;607zy /= a;608return *this;609}610611template <class T>612inline Shear6<T>613Shear6<T>::operator / (const Shear6 &h) const614{615return Shear6 (xy / h.xy, xz / h.xz, yz / h.yz,616yx / h.yx, zx / h.zx, zy / h.zy);617}618619template <class T>620inline Shear6<T>621Shear6<T>::operator / (T a) const622{623return Shear6 (xy / a, xz / a, yz / a,624yx / a, zx / a, zy / a);625}626627628//-----------------------------629// Stream output implementation630//-----------------------------631632template <class T>633std::ostream &634operator << (std::ostream &s, const Shear6<T> &h)635{636return s << '('637<< h.xy << ' ' << h.xz << ' ' << h.yz638<< h.yx << ' ' << h.zx << ' ' << h.zy639<< ')';640}641642643//-----------------------------------------644// Implementation of reverse multiplication645//-----------------------------------------646647template <class S, class T>648inline Shear6<T>649operator * (S a, const Shear6<T> &h)650{651return Shear6<T> (a * h.xy, a * h.xz, a * h.yz,652a * h.yx, a * h.zx, a * h.zy);653}654655656} // namespace Imath657658#endif659660661