Path: blob/master/3rdparty/openexr/Imath/ImathEuler.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, 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_IMATHEULER_H37#define INCLUDED_IMATHEULER_H3839//----------------------------------------------------------------------40//41// template class Euler<T>42//43// This class represents euler angle orientations. The class44// inherits from Vec3 to it can be freely cast. The additional45// information is the euler priorities rep. This class is46// essentially a rip off of Ken Shoemake's GemsIV code. It has47// been modified minimally to make it more understandable, but48// hardly enough to make it easy to grok completely.49//50// There are 24 possible combonations of Euler angle51// representations of which 12 are common in CG and you will52// probably only use 6 of these which in this scheme are the53// non-relative-non-repeating types.54//55// The representations can be partitioned according to two56// criteria:57//58// 1) Are the angles measured relative to a set of fixed axis59// or relative to each other (the latter being what happens60// when rotation matrices are multiplied together and is61// almost ubiquitous in the cg community)62//63// 2) Is one of the rotations repeated (ala XYX rotation)64//65// When you construct a given representation from scratch you66// must order the angles according to their priorities. So, the67// easiest is a softimage or aerospace (yaw/pitch/roll) ordering68// of ZYX.69//70// float x_rot = 1;71// float y_rot = 2;72// float z_rot = 3;73//74// Eulerf angles(z_rot, y_rot, x_rot, Eulerf::ZYX);75// -or-76// Eulerf angles( V3f(z_rot,y_rot,z_rot), Eulerf::ZYX );77//78// If instead, the order was YXZ for instance you would have to79// do this:80//81// float x_rot = 1;82// float y_rot = 2;83// float z_rot = 3;84//85// Eulerf angles(y_rot, x_rot, z_rot, Eulerf::YXZ);86// -or-87// Eulerf angles( V3f(y_rot,x_rot,z_rot), Eulerf::YXZ );88//89// Notice how the order you put the angles into the three slots90// should correspond to the enum (YXZ) ordering. The input angle91// vector is called the "ijk" vector -- not an "xyz" vector. The92// ijk vector order is the same as the enum. If you treat the93// Euler<> as a Vec<> (which it inherts from) you will find the94// angles are ordered in the same way, i.e.:95//96// V3f v = angles;97// // v.x == y_rot, v.y == x_rot, v.z == z_rot98//99// If you just want the x, y, and z angles stored in a vector in100// that order, you can do this:101//102// V3f v = angles.toXYZVector()103// // v.x == x_rot, v.y == y_rot, v.z == z_rot104//105// If you want to set the Euler with an XYZVector use the106// optional layout argument:107//108// Eulerf angles(x_rot, y_rot, z_rot,109// Eulerf::YXZ,110// Eulerf::XYZLayout);111//112// This is the same as:113//114// Eulerf angles(y_rot, x_rot, z_rot, Eulerf::YXZ);115//116// Note that this won't do anything intelligent if you have a117// repeated axis in the euler angles (e.g. XYX)118//119// If you need to use the "relative" versions of these, you will120// need to use the "r" enums.121//122// The units of the rotation angles are assumed to be radians.123//124//----------------------------------------------------------------------125126127#include "ImathMath.h"128#include "ImathVec.h"129#include "ImathQuat.h"130#include "ImathMatrix.h"131#include "ImathLimits.h"132#include <iostream>133134namespace Imath {135136#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER137// Disable MS VC++ warnings about conversion from double to float138#pragma warning(disable:4244)139#endif140141template <class T>142class Euler : public Vec3<T>143{144public:145146using Vec3<T>::x;147using Vec3<T>::y;148using Vec3<T>::z;149150enum Order151{152//153// All 24 possible orderings154//155156XYZ = 0x0101, // "usual" orderings157XZY = 0x0001,158YZX = 0x1101,159YXZ = 0x1001,160ZXY = 0x2101,161ZYX = 0x2001,162163XZX = 0x0011, // first axis repeated164XYX = 0x0111,165YXY = 0x1011,166YZY = 0x1111,167ZYZ = 0x2011,168ZXZ = 0x2111,169170XYZr = 0x2000, // relative orderings -- not common171XZYr = 0x2100,172YZXr = 0x1000,173YXZr = 0x1100,174ZXYr = 0x0000,175ZYXr = 0x0100,176177XZXr = 0x2110, // relative first axis repeated178XYXr = 0x2010,179YXYr = 0x1110,180YZYr = 0x1010,181ZYZr = 0x0110,182ZXZr = 0x0010,183// ||||184// VVVV185// Legend: ABCD186// A -> Initial Axis (0==x, 1==y, 2==z)187// B -> Parity Even (1==true)188// C -> Initial Repeated (1==true)189// D -> Frame Static (1==true)190//191192Legal = XYZ | XZY | YZX | YXZ | ZXY | ZYX |193XZX | XYX | YXY | YZY | ZYZ | ZXZ |194XYZr| XZYr| YZXr| YXZr| ZXYr| ZYXr|195XZXr| XYXr| YXYr| YZYr| ZYZr| ZXZr,196197Min = 0x0000,198Max = 0x2111,199Default = XYZ200};201202enum Axis { X = 0, Y = 1, Z = 2 };203204enum InputLayout { XYZLayout, IJKLayout };205206//--------------------------------------------------------------------207// Constructors -- all default to ZYX non-relative ala softimage208// (where there is no argument to specify it)209//210// The Euler-from-matrix constructors assume that the matrix does211// not include shear or non-uniform scaling, but the constructors212// do not examine the matrix to verify this assumption. If necessary,213// you can adjust the matrix by calling the removeScalingAndShear()214// function, defined in ImathMatrixAlgo.h.215//--------------------------------------------------------------------216217Euler();218Euler(const Euler&);219Euler(Order p);220Euler(const Vec3<T> &v, Order o = Default, InputLayout l = IJKLayout);221Euler(T i, T j, T k, Order o = Default, InputLayout l = IJKLayout);222Euler(const Euler<T> &euler, Order newp);223Euler(const Matrix33<T> &, Order o = Default);224Euler(const Matrix44<T> &, Order o = Default);225226//---------------------------------227// Algebraic functions/ Operators228//---------------------------------229230const Euler<T>& operator= (const Euler<T>&);231const Euler<T>& operator= (const Vec3<T>&);232233//--------------------------------------------------------234// Set the euler value235// This does NOT convert the angles, but setXYZVector()236// does reorder the input vector.237//--------------------------------------------------------238239static bool legal(Order);240241void setXYZVector(const Vec3<T> &);242243Order order() const;244void setOrder(Order);245246void set(Axis initial,247bool relative,248bool parityEven,249bool firstRepeats);250251//------------------------------------------------------------252// Conversions, toXYZVector() reorders the angles so that253// the X rotation comes first, followed by the Y and Z254// in cases like XYX ordering, the repeated angle will be255// in the "z" component256//257// The Euler-from-matrix extract() functions assume that the258// matrix does not include shear or non-uniform scaling, but259// the extract() functions do not examine the matrix to verify260// this assumption. If necessary, you can adjust the matrix261// by calling the removeScalingAndShear() function, defined262// in ImathMatrixAlgo.h.263//------------------------------------------------------------264265void extract(const Matrix33<T>&);266void extract(const Matrix44<T>&);267void extract(const Quat<T>&);268269Matrix33<T> toMatrix33() const;270Matrix44<T> toMatrix44() const;271Quat<T> toQuat() const;272Vec3<T> toXYZVector() const;273274//---------------------------------------------------275// Use this function to unpack angles from ijk form276//---------------------------------------------------277278void angleOrder(int &i, int &j, int &k) const;279280//---------------------------------------------------281// Use this function to determine mapping from xyz to ijk282// - reshuffles the xyz to match the order283//---------------------------------------------------284285void angleMapping(int &i, int &j, int &k) const;286287//----------------------------------------------------------------------288//289// Utility methods for getting continuous rotations. None of these290// methods change the orientation given by its inputs (or at least291// that is the intent).292//293// angleMod() converts an angle to its equivalent in [-PI, PI]294//295// simpleXYZRotation() adjusts xyzRot so that its components differ296// from targetXyzRot by no more than +-PI297//298// nearestRotation() adjusts xyzRot so that its components differ299// from targetXyzRot by as little as possible.300// Note that xyz here really means ijk, because301// the order must be provided.302//303// makeNear() adjusts "this" Euler so that its components differ304// from target by as little as possible. This method305// might not make sense for Eulers with different order306// and it probably doesn't work for repeated axis and307// relative orderings (TODO).308//309//-----------------------------------------------------------------------310311static float angleMod (T angle);312static void simpleXYZRotation (Vec3<T> &xyzRot,313const Vec3<T> &targetXyzRot);314static void nearestRotation (Vec3<T> &xyzRot,315const Vec3<T> &targetXyzRot,316Order order = XYZ);317318void makeNear (const Euler<T> &target);319320bool frameStatic() const { return _frameStatic; }321bool initialRepeated() const { return _initialRepeated; }322bool parityEven() const { return _parityEven; }323Axis initialAxis() const { return _initialAxis; }324325protected:326327bool _frameStatic : 1; // relative or static rotations328bool _initialRepeated : 1; // init axis repeated as last329bool _parityEven : 1; // "parity of axis permutation"330#if defined _WIN32 || defined _WIN64331Axis _initialAxis ; // First axis of rotation332#else333Axis _initialAxis : 2; // First axis of rotation334#endif335};336337338//--------------------339// Convenient typedefs340//--------------------341342typedef Euler<float> Eulerf;343typedef Euler<double> Eulerd;344345346//---------------347// Implementation348//---------------349350template<class T>351inline void352Euler<T>::angleOrder(int &i, int &j, int &k) const353{354i = _initialAxis;355j = _parityEven ? (i+1)%3 : (i > 0 ? i-1 : 2);356k = _parityEven ? (i > 0 ? i-1 : 2) : (i+1)%3;357}358359template<class T>360inline void361Euler<T>::angleMapping(int &i, int &j, int &k) const362{363int m[3];364365m[_initialAxis] = 0;366m[(_initialAxis+1) % 3] = _parityEven ? 1 : 2;367m[(_initialAxis+2) % 3] = _parityEven ? 2 : 1;368i = m[0];369j = m[1];370k = m[2];371}372373template<class T>374inline void375Euler<T>::setXYZVector(const Vec3<T> &v)376{377int i,j,k;378angleMapping(i,j,k);379(*this)[i] = v.x;380(*this)[j] = v.y;381(*this)[k] = v.z;382}383384template<class T>385inline Vec3<T>386Euler<T>::toXYZVector() const387{388int i,j,k;389angleMapping(i,j,k);390return Vec3<T>((*this)[i],(*this)[j],(*this)[k]);391}392393394template<class T>395Euler<T>::Euler() :396Vec3<T>(0,0,0),397_frameStatic(true),398_initialRepeated(false),399_parityEven(true),400_initialAxis(X)401{}402403template<class T>404Euler<T>::Euler(typename Euler<T>::Order p) :405Vec3<T>(0,0,0),406_frameStatic(true),407_initialRepeated(false),408_parityEven(true),409_initialAxis(X)410{411setOrder(p);412}413414template<class T>415inline Euler<T>::Euler( const Vec3<T> &v,416typename Euler<T>::Order p,417typename Euler<T>::InputLayout l )418{419setOrder(p);420if ( l == XYZLayout ) setXYZVector(v);421else { x = v.x; y = v.y; z = v.z; }422}423424template<class T>425inline Euler<T>::Euler(const Euler<T> &euler)426{427operator=(euler);428}429430template<class T>431inline Euler<T>::Euler(const Euler<T> &euler,Order p)432{433setOrder(p);434Matrix33<T> M = euler.toMatrix33();435extract(M);436}437438template<class T>439inline Euler<T>::Euler( T xi, T yi, T zi,440typename Euler<T>::Order p,441typename Euler<T>::InputLayout l)442{443setOrder(p);444if ( l == XYZLayout ) setXYZVector(Vec3<T>(xi,yi,zi));445else { x = xi; y = yi; z = zi; }446}447448template<class T>449inline Euler<T>::Euler( const Matrix33<T> &M, typename Euler::Order p )450{451setOrder(p);452extract(M);453}454455template<class T>456inline Euler<T>::Euler( const Matrix44<T> &M, typename Euler::Order p )457{458setOrder(p);459extract(M);460}461462template<class T>463inline void Euler<T>::extract(const Quat<T> &q)464{465extract(q.toMatrix33());466}467468template<class T>469void Euler<T>::extract(const Matrix33<T> &M)470{471int i,j,k;472angleOrder(i,j,k);473474if (_initialRepeated)475{476//477// Extract the first angle, x.478//479480x = Math<T>::atan2 (M[j][i], M[k][i]);481482//483// Remove the x rotation from M, so that the remaining484// rotation, N, is only around two axes, and gimbal lock485// cannot occur.486//487488Vec3<T> r (0, 0, 0);489r[i] = (_parityEven? -x: x);490491Matrix44<T> N;492N.rotate (r);493494N = N * Matrix44<T> (M[0][0], M[0][1], M[0][2], 0,495M[1][0], M[1][1], M[1][2], 0,496M[2][0], M[2][1], M[2][2], 0,4970, 0, 0, 1);498//499// Extract the other two angles, y and z, from N.500//501502T sy = Math<T>::sqrt (N[j][i]*N[j][i] + N[k][i]*N[k][i]);503y = Math<T>::atan2 (sy, N[i][i]);504z = Math<T>::atan2 (N[j][k], N[j][j]);505}506else507{508//509// Extract the first angle, x.510//511512x = Math<T>::atan2 (M[j][k], M[k][k]);513514//515// Remove the x rotation from M, so that the remaining516// rotation, N, is only around two axes, and gimbal lock517// cannot occur.518//519520Vec3<T> r (0, 0, 0);521r[i] = (_parityEven? -x: x);522523Matrix44<T> N;524N.rotate (r);525526N = N * Matrix44<T> (M[0][0], M[0][1], M[0][2], 0,527M[1][0], M[1][1], M[1][2], 0,528M[2][0], M[2][1], M[2][2], 0,5290, 0, 0, 1);530//531// Extract the other two angles, y and z, from N.532//533534T cy = Math<T>::sqrt (N[i][i]*N[i][i] + N[i][j]*N[i][j]);535y = Math<T>::atan2 (-N[i][k], cy);536z = Math<T>::atan2 (-N[j][i], N[j][j]);537}538539if (!_parityEven)540*this *= -1;541542if (!_frameStatic)543{544T t = x;545x = z;546z = t;547}548}549550template<class T>551void Euler<T>::extract(const Matrix44<T> &M)552{553int i,j,k;554angleOrder(i,j,k);555556if (_initialRepeated)557{558//559// Extract the first angle, x.560//561562x = Math<T>::atan2 (M[j][i], M[k][i]);563564//565// Remove the x rotation from M, so that the remaining566// rotation, N, is only around two axes, and gimbal lock567// cannot occur.568//569570Vec3<T> r (0, 0, 0);571r[i] = (_parityEven? -x: x);572573Matrix44<T> N;574N.rotate (r);575N = N * M;576577//578// Extract the other two angles, y and z, from N.579//580581T sy = Math<T>::sqrt (N[j][i]*N[j][i] + N[k][i]*N[k][i]);582y = Math<T>::atan2 (sy, N[i][i]);583z = Math<T>::atan2 (N[j][k], N[j][j]);584}585else586{587//588// Extract the first angle, x.589//590591x = Math<T>::atan2 (M[j][k], M[k][k]);592593//594// Remove the x rotation from M, so that the remaining595// rotation, N, is only around two axes, and gimbal lock596// cannot occur.597//598599Vec3<T> r (0, 0, 0);600r[i] = (_parityEven? -x: x);601602Matrix44<T> N;603N.rotate (r);604N = N * M;605606//607// Extract the other two angles, y and z, from N.608//609610T cy = Math<T>::sqrt (N[i][i]*N[i][i] + N[i][j]*N[i][j]);611y = Math<T>::atan2 (-N[i][k], cy);612z = Math<T>::atan2 (-N[j][i], N[j][j]);613}614615if (!_parityEven)616*this *= -1;617618if (!_frameStatic)619{620T t = x;621x = z;622z = t;623}624}625626template<class T>627Matrix33<T> Euler<T>::toMatrix33() const628{629int i,j,k;630angleOrder(i,j,k);631632Vec3<T> angles;633634if ( _frameStatic ) angles = (*this);635else angles = Vec3<T>(z,y,x);636637if ( !_parityEven ) angles *= -1.0;638639T ci = Math<T>::cos(angles.x);640T cj = Math<T>::cos(angles.y);641T ch = Math<T>::cos(angles.z);642T si = Math<T>::sin(angles.x);643T sj = Math<T>::sin(angles.y);644T sh = Math<T>::sin(angles.z);645646T cc = ci*ch;647T cs = ci*sh;648T sc = si*ch;649T ss = si*sh;650651Matrix33<T> M;652653if ( _initialRepeated )654{655M[i][i] = cj; M[j][i] = sj*si; M[k][i] = sj*ci;656M[i][j] = sj*sh; M[j][j] = -cj*ss+cc; M[k][j] = -cj*cs-sc;657M[i][k] = -sj*ch; M[j][k] = cj*sc+cs; M[k][k] = cj*cc-ss;658}659else660{661M[i][i] = cj*ch; M[j][i] = sj*sc-cs; M[k][i] = sj*cc+ss;662M[i][j] = cj*sh; M[j][j] = sj*ss+cc; M[k][j] = sj*cs-sc;663M[i][k] = -sj; M[j][k] = cj*si; M[k][k] = cj*ci;664}665666return M;667}668669template<class T>670Matrix44<T> Euler<T>::toMatrix44() const671{672int i,j,k;673angleOrder(i,j,k);674675Vec3<T> angles;676677if ( _frameStatic ) angles = (*this);678else angles = Vec3<T>(z,y,x);679680if ( !_parityEven ) angles *= -1.0;681682T ci = Math<T>::cos(angles.x);683T cj = Math<T>::cos(angles.y);684T ch = Math<T>::cos(angles.z);685T si = Math<T>::sin(angles.x);686T sj = Math<T>::sin(angles.y);687T sh = Math<T>::sin(angles.z);688689T cc = ci*ch;690T cs = ci*sh;691T sc = si*ch;692T ss = si*sh;693694Matrix44<T> M;695696if ( _initialRepeated )697{698M[i][i] = cj; M[j][i] = sj*si; M[k][i] = sj*ci;699M[i][j] = sj*sh; M[j][j] = -cj*ss+cc; M[k][j] = -cj*cs-sc;700M[i][k] = -sj*ch; M[j][k] = cj*sc+cs; M[k][k] = cj*cc-ss;701}702else703{704M[i][i] = cj*ch; M[j][i] = sj*sc-cs; M[k][i] = sj*cc+ss;705M[i][j] = cj*sh; M[j][j] = sj*ss+cc; M[k][j] = sj*cs-sc;706M[i][k] = -sj; M[j][k] = cj*si; M[k][k] = cj*ci;707}708709return M;710}711712template<class T>713Quat<T> Euler<T>::toQuat() const714{715Vec3<T> angles;716int i,j,k;717angleOrder(i,j,k);718719if ( _frameStatic ) angles = (*this);720else angles = Vec3<T>(z,y,x);721722if ( !_parityEven ) angles.y = -angles.y;723724T ti = angles.x*0.5;725T tj = angles.y*0.5;726T th = angles.z*0.5;727T ci = Math<T>::cos(ti);728T cj = Math<T>::cos(tj);729T ch = Math<T>::cos(th);730T si = Math<T>::sin(ti);731T sj = Math<T>::sin(tj);732T sh = Math<T>::sin(th);733T cc = ci*ch;734T cs = ci*sh;735T sc = si*ch;736T ss = si*sh;737738T parity = _parityEven ? 1.0 : -1.0;739740Quat<T> q;741Vec3<T> a;742743if ( _initialRepeated )744{745a[i] = cj*(cs + sc);746a[j] = sj*(cc + ss) * parity,747a[k] = sj*(cs - sc);748q.r = cj*(cc - ss);749}750else751{752a[i] = cj*sc - sj*cs,753a[j] = (cj*ss + sj*cc) * parity,754a[k] = cj*cs - sj*sc;755q.r = cj*cc + sj*ss;756}757758q.v = a;759760return q;761}762763template<class T>764inline bool765Euler<T>::legal(typename Euler<T>::Order order)766{767return (order & ~Legal) ? false : true;768}769770template<class T>771typename Euler<T>::Order772Euler<T>::order() const773{774int foo = (_initialAxis == Z ? 0x2000 : (_initialAxis == Y ? 0x1000 : 0));775776if (_parityEven) foo |= 0x0100;777if (_initialRepeated) foo |= 0x0010;778if (_frameStatic) foo++;779780return (Order)foo;781}782783template<class T>784inline void Euler<T>::setOrder(typename Euler<T>::Order p)785{786set( p & 0x2000 ? Z : (p & 0x1000 ? Y : X), // initial axis787!(p & 0x1), // static?788!!(p & 0x100), // permutation even?789!!(p & 0x10)); // initial repeats?790}791792template<class T>793void Euler<T>::set(typename Euler<T>::Axis axis,794bool relative,795bool parityEven,796bool firstRepeats)797{798_initialAxis = axis;799_frameStatic = !relative;800_parityEven = parityEven;801_initialRepeated = firstRepeats;802}803804template<class T>805const Euler<T>& Euler<T>::operator= (const Euler<T> &euler)806{807x = euler.x;808y = euler.y;809z = euler.z;810_initialAxis = euler._initialAxis;811_frameStatic = euler._frameStatic;812_parityEven = euler._parityEven;813_initialRepeated = euler._initialRepeated;814return *this;815}816817template<class T>818const Euler<T>& Euler<T>::operator= (const Vec3<T> &v)819{820x = v.x;821y = v.y;822z = v.z;823return *this;824}825826template<class T>827std::ostream& operator << (std::ostream &o, const Euler<T> &euler)828{829char a[3] = { 'X', 'Y', 'Z' };830831const char* r = euler.frameStatic() ? "" : "r";832int i,j,k;833euler.angleOrder(i,j,k);834835if ( euler.initialRepeated() ) k = i;836837return o << "("838<< euler.x << " "839<< euler.y << " "840<< euler.z << " "841<< a[i] << a[j] << a[k] << r << ")";842}843844template <class T>845float846Euler<T>::angleMod (T angle)847{848angle = fmod(T (angle), T (2 * M_PI));849850if (angle < -M_PI) angle += 2 * M_PI;851if (angle > +M_PI) angle -= 2 * M_PI;852853return angle;854}855856template <class T>857void858Euler<T>::simpleXYZRotation (Vec3<T> &xyzRot, const Vec3<T> &targetXyzRot)859{860Vec3<T> d = xyzRot - targetXyzRot;861xyzRot[0] = targetXyzRot[0] + angleMod(d[0]);862xyzRot[1] = targetXyzRot[1] + angleMod(d[1]);863xyzRot[2] = targetXyzRot[2] + angleMod(d[2]);864}865866template <class T>867void868Euler<T>::nearestRotation (Vec3<T> &xyzRot, const Vec3<T> &targetXyzRot,869Order order)870{871int i,j,k;872Euler<T> e (0,0,0, order);873e.angleOrder(i,j,k);874875simpleXYZRotation(xyzRot, targetXyzRot);876877Vec3<T> otherXyzRot;878otherXyzRot[i] = M_PI+xyzRot[i];879otherXyzRot[j] = M_PI-xyzRot[j];880otherXyzRot[k] = M_PI+xyzRot[k];881882simpleXYZRotation(otherXyzRot, targetXyzRot);883884Vec3<T> d = xyzRot - targetXyzRot;885Vec3<T> od = otherXyzRot - targetXyzRot;886T dMag = d.dot(d);887T odMag = od.dot(od);888889if (odMag < dMag)890{891xyzRot = otherXyzRot;892}893}894895template <class T>896void897Euler<T>::makeNear (const Euler<T> &target)898{899Vec3<T> xyzRot = toXYZVector();900Vec3<T> targetXyz;901if (order() != target.order())902{903Euler<T> targetSameOrder = Euler<T>(target, order());904targetXyz = targetSameOrder.toXYZVector();905}906else907{908targetXyz = target.toXYZVector();909}910911nearestRotation(xyzRot, targetXyz, order());912913setXYZVector(xyzRot);914}915916#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER917#pragma warning(default:4244)918#endif919920} // namespace Imath921922923#endif924925926