Path: blob/master/3rdparty/openexr/Imath/ImathVec.cpp
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//----------------------------------------------------------------------------37//38// Specializations of the Vec2<T> and Vec3<T> templates.39//40//----------------------------------------------------------------------------4142#include "ImathVec.h"4344#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER45// suppress exception specification warnings46#pragma warning(disable:4290)47#endif484950namespace Imath {5152namespace53{5455template<class T>56bool57normalizeOrThrow(Vec2<T> &v)58{59int axis = -1;60for (int i = 0; i < 2; i ++)61{62if (v[i] != 0)63{64if (axis != -1)65{66throw IntVecNormalizeExc ("Cannot normalize an integer "67"vector unless it is parallel "68"to a principal axis");69}70axis = i;71}72}73v[axis] = (v[axis] > 0) ? 1 : -1;74return true;75}767778template<class T>79bool80normalizeOrThrow(Vec3<T> &v)81{82int axis = -1;83for (int i = 0; i < 3; i ++)84{85if (v[i] != 0)86{87if (axis != -1)88{89throw IntVecNormalizeExc ("Cannot normalize an integer "90"vector unless it is parallel "91"to a principal axis");92}93axis = i;94}95}96v[axis] = (v[axis] > 0) ? 1 : -1;97return true;98}99100101template<class T>102bool103normalizeOrThrow(Vec4<T> &v)104{105int axis = -1;106for (int i = 0; i < 4; i ++)107{108if (v[i] != 0)109{110if (axis != -1)111{112throw IntVecNormalizeExc ("Cannot normalize an integer "113"vector unless it is parallel "114"to a principal axis");115}116axis = i;117}118}119v[axis] = (v[axis] > 0) ? 1 : -1;120return true;121}122123}124125126// Vec2<short>127128template <>129short130Vec2<short>::length () const131{132float lenF = Math<float>::sqrt ((float)dot (*this));133short lenS = (short) (lenF + 0.5f);134return lenS;135}136137template <>138const Vec2<short> &139Vec2<short>::normalize ()140{141normalizeOrThrow<short>(*this);142return *this;143}144145template <>146const Vec2<short> &147Vec2<short>::normalizeExc () throw (Iex::MathExc)148{149if ((x == 0) && (y == 0))150throw NullVecExc ("Cannot normalize null vector.");151152normalizeOrThrow<short>(*this);153return *this;154}155156template <>157const Vec2<short> &158Vec2<short>::normalizeNonNull ()159{160normalizeOrThrow<short>(*this);161return *this;162}163164template <>165Vec2<short>166Vec2<short>::normalized () const167{168Vec2<short> v(*this);169normalizeOrThrow<short>(v);170return v;171}172173template <>174Vec2<short>175Vec2<short>::normalizedExc () const throw (Iex::MathExc)176{177if ((x == 0) && (y == 0))178throw NullVecExc ("Cannot normalize null vector.");179180Vec2<short> v(*this);181normalizeOrThrow<short>(v);182return v;183}184185template <>186Vec2<short>187Vec2<short>::normalizedNonNull () const188{189Vec2<short> v(*this);190normalizeOrThrow<short>(v);191return v;192}193194195// Vec2<int>196197template <>198int199Vec2<int>::length () const200{201float lenF = Math<float>::sqrt ((float)dot (*this));202int lenI = (int) (lenF + 0.5f);203return lenI;204}205206template <>207const Vec2<int> &208Vec2<int>::normalize ()209{210normalizeOrThrow<int>(*this);211return *this;212}213214template <>215const Vec2<int> &216Vec2<int>::normalizeExc () throw (Iex::MathExc)217{218if ((x == 0) && (y == 0))219throw NullVecExc ("Cannot normalize null vector.");220221normalizeOrThrow<int>(*this);222return *this;223}224225template <>226const Vec2<int> &227Vec2<int>::normalizeNonNull ()228{229normalizeOrThrow<int>(*this);230return *this;231}232233template <>234Vec2<int>235Vec2<int>::normalized () const236{237Vec2<int> v(*this);238normalizeOrThrow<int>(v);239return v;240}241242template <>243Vec2<int>244Vec2<int>::normalizedExc () const throw (Iex::MathExc)245{246if ((x == 0) && (y == 0))247throw NullVecExc ("Cannot normalize null vector.");248249Vec2<int> v(*this);250normalizeOrThrow<int>(v);251return v;252}253254template <>255Vec2<int>256Vec2<int>::normalizedNonNull () const257{258Vec2<int> v(*this);259normalizeOrThrow<int>(v);260return v;261}262263264// Vec3<short>265266template <>267short268Vec3<short>::length () const269{270float lenF = Math<float>::sqrt ((float)dot (*this));271short lenS = (short) (lenF + 0.5f);272return lenS;273}274275template <>276const Vec3<short> &277Vec3<short>::normalize ()278{279normalizeOrThrow<short>(*this);280return *this;281}282283template <>284const Vec3<short> &285Vec3<short>::normalizeExc () throw (Iex::MathExc)286{287if ((x == 0) && (y == 0) && (z == 0))288throw NullVecExc ("Cannot normalize null vector.");289290normalizeOrThrow<short>(*this);291return *this;292}293294template <>295const Vec3<short> &296Vec3<short>::normalizeNonNull ()297{298normalizeOrThrow<short>(*this);299return *this;300}301302template <>303Vec3<short>304Vec3<short>::normalized () const305{306Vec3<short> v(*this);307normalizeOrThrow<short>(v);308return v;309}310311template <>312Vec3<short>313Vec3<short>::normalizedExc () const throw (Iex::MathExc)314{315if ((x == 0) && (y == 0) && (z == 0))316throw NullVecExc ("Cannot normalize null vector.");317318Vec3<short> v(*this);319normalizeOrThrow<short>(v);320return v;321}322323template <>324Vec3<short>325Vec3<short>::normalizedNonNull () const326{327Vec3<short> v(*this);328normalizeOrThrow<short>(v);329return v;330}331332333// Vec3<int>334335template <>336int337Vec3<int>::length () const338{339float lenF = Math<float>::sqrt ((float)dot (*this));340int lenI = (int) (lenF + 0.5f);341return lenI;342}343344template <>345const Vec3<int> &346Vec3<int>::normalize ()347{348normalizeOrThrow<int>(*this);349return *this;350}351352template <>353const Vec3<int> &354Vec3<int>::normalizeExc () throw (Iex::MathExc)355{356if ((x == 0) && (y == 0) && (z == 0))357throw NullVecExc ("Cannot normalize null vector.");358359normalizeOrThrow<int>(*this);360return *this;361}362363template <>364const Vec3<int> &365Vec3<int>::normalizeNonNull ()366{367normalizeOrThrow<int>(*this);368return *this;369}370371template <>372Vec3<int>373Vec3<int>::normalized () const374{375Vec3<int> v(*this);376normalizeOrThrow<int>(v);377return v;378}379380template <>381Vec3<int>382Vec3<int>::normalizedExc () const throw (Iex::MathExc)383{384if ((x == 0) && (y == 0) && (z == 0))385throw NullVecExc ("Cannot normalize null vector.");386387Vec3<int> v(*this);388normalizeOrThrow<int>(v);389return v;390}391392template <>393Vec3<int>394Vec3<int>::normalizedNonNull () const395{396Vec3<int> v(*this);397normalizeOrThrow<int>(v);398return v;399}400401402// Vec4<short>403404template <>405short406Vec4<short>::length () const407{408float lenF = Math<float>::sqrt ((float)dot (*this));409short lenS = (short) (lenF + 0.5f);410return lenS;411}412413template <>414const Vec4<short> &415Vec4<short>::normalize ()416{417normalizeOrThrow<short>(*this);418return *this;419}420421template <>422const Vec4<short> &423Vec4<short>::normalizeExc () throw (Iex::MathExc)424{425if ((x == 0) && (y == 0) && (z == 0) && (w == 0))426throw NullVecExc ("Cannot normalize null vector.");427428normalizeOrThrow<short>(*this);429return *this;430}431432template <>433const Vec4<short> &434Vec4<short>::normalizeNonNull ()435{436normalizeOrThrow<short>(*this);437return *this;438}439440template <>441Vec4<short>442Vec4<short>::normalized () const443{444Vec4<short> v(*this);445normalizeOrThrow<short>(v);446return v;447}448449template <>450Vec4<short>451Vec4<short>::normalizedExc () const throw (Iex::MathExc)452{453if ((x == 0) && (y == 0) && (z == 0) && (w == 0))454throw NullVecExc ("Cannot normalize null vector.");455456Vec4<short> v(*this);457normalizeOrThrow<short>(v);458return v;459}460461template <>462Vec4<short>463Vec4<short>::normalizedNonNull () const464{465Vec4<short> v(*this);466normalizeOrThrow<short>(v);467return v;468}469470471// Vec4<int>472473template <>474int475Vec4<int>::length () const476{477float lenF = Math<float>::sqrt ((float)dot (*this));478int lenI = (int) (lenF + 0.5f);479return lenI;480}481482template <>483const Vec4<int> &484Vec4<int>::normalize ()485{486normalizeOrThrow<int>(*this);487return *this;488}489490template <>491const Vec4<int> &492Vec4<int>::normalizeExc () throw (Iex::MathExc)493{494if ((x == 0) && (y == 0) && (z == 0) && (w == 0))495throw NullVecExc ("Cannot normalize null vector.");496497normalizeOrThrow<int>(*this);498return *this;499}500501template <>502const Vec4<int> &503Vec4<int>::normalizeNonNull ()504{505normalizeOrThrow<int>(*this);506return *this;507}508509template <>510Vec4<int>511Vec4<int>::normalized () const512{513Vec4<int> v(*this);514normalizeOrThrow<int>(v);515return v;516}517518template <>519Vec4<int>520Vec4<int>::normalizedExc () const throw (Iex::MathExc)521{522if ((x == 0) && (y == 0) && (z == 0) && (w == 0))523throw NullVecExc ("Cannot normalize null vector.");524525Vec4<int> v(*this);526normalizeOrThrow<int>(v);527return v;528}529530template <>531Vec4<int>532Vec4<int>::normalizedNonNull () const533{534Vec4<int> v(*this);535normalizeOrThrow<int>(v);536return v;537}538539} // namespace Imath540541542