/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------- // // Specializations of the Vec2 and Vec3 templates. // //---------------------------------------------------------------------------- #include "ImathVec.h" #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER // suppress exception specification warnings #pragma warning(disable:4290) #endif namespace Imath { namespace { template bool normalizeOrThrow(Vec2 &v) { int axis = -1; for (int i = 0; i < 2; i ++) { if (v[i] != 0) { if (axis != -1) { throw IntVecNormalizeExc ("Cannot normalize an integer " "vector unless it is parallel " "to a principal axis"); } axis = i; } } v[axis] = (v[axis] > 0) ? 1 : -1; return true; } template bool normalizeOrThrow(Vec3 &v) { int axis = -1; for (int i = 0; i < 3; i ++) { if (v[i] != 0) { if (axis != -1) { throw IntVecNormalizeExc ("Cannot normalize an integer " "vector unless it is parallel " "to a principal axis"); } axis = i; } } v[axis] = (v[axis] > 0) ? 1 : -1; return true; } template bool normalizeOrThrow(Vec4 &v) { int axis = -1; for (int i = 0; i < 4; i ++) { if (v[i] != 0) { if (axis != -1) { throw IntVecNormalizeExc ("Cannot normalize an integer " "vector unless it is parallel " "to a principal axis"); } axis = i; } } v[axis] = (v[axis] > 0) ? 1 : -1; return true; } } // Vec2 template <> short Vec2::length () const { float lenF = Math::sqrt ((float)dot (*this)); short lenS = (short) (lenF + 0.5f); return lenS; } template <> const Vec2 & Vec2::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec2 & Vec2::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec2 & Vec2::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec2 Vec2::normalized () const { Vec2 v(*this); normalizeOrThrow(v); return v; } template <> Vec2 Vec2::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec2 v(*this); normalizeOrThrow(v); return v; } template <> Vec2 Vec2::normalizedNonNull () const { Vec2 v(*this); normalizeOrThrow(v); return v; } // Vec2 template <> int Vec2::length () const { float lenF = Math::sqrt ((float)dot (*this)); int lenI = (int) (lenF + 0.5f); return lenI; } template <> const Vec2 & Vec2::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec2 & Vec2::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec2 & Vec2::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec2 Vec2::normalized () const { Vec2 v(*this); normalizeOrThrow(v); return v; } template <> Vec2 Vec2::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec2 v(*this); normalizeOrThrow(v); return v; } template <> Vec2 Vec2::normalizedNonNull () const { Vec2 v(*this); normalizeOrThrow(v); return v; } // Vec3 template <> short Vec3::length () const { float lenF = Math::sqrt ((float)dot (*this)); short lenS = (short) (lenF + 0.5f); return lenS; } template <> const Vec3 & Vec3::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec3 & Vec3::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec3 & Vec3::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec3 Vec3::normalized () const { Vec3 v(*this); normalizeOrThrow(v); return v; } template <> Vec3 Vec3::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec3 v(*this); normalizeOrThrow(v); return v; } template <> Vec3 Vec3::normalizedNonNull () const { Vec3 v(*this); normalizeOrThrow(v); return v; } // Vec3 template <> int Vec3::length () const { float lenF = Math::sqrt ((float)dot (*this)); int lenI = (int) (lenF + 0.5f); return lenI; } template <> const Vec3 & Vec3::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec3 & Vec3::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec3 & Vec3::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec3 Vec3::normalized () const { Vec3 v(*this); normalizeOrThrow(v); return v; } template <> Vec3 Vec3::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec3 v(*this); normalizeOrThrow(v); return v; } template <> Vec3 Vec3::normalizedNonNull () const { Vec3 v(*this); normalizeOrThrow(v); return v; } // Vec4 template <> short Vec4::length () const { float lenF = Math::sqrt ((float)dot (*this)); short lenS = (short) (lenF + 0.5f); return lenS; } template <> const Vec4 & Vec4::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec4 & Vec4::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0) && (w == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec4 & Vec4::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec4 Vec4::normalized () const { Vec4 v(*this); normalizeOrThrow(v); return v; } template <> Vec4 Vec4::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0) && (w == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec4 v(*this); normalizeOrThrow(v); return v; } template <> Vec4 Vec4::normalizedNonNull () const { Vec4 v(*this); normalizeOrThrow(v); return v; } // Vec4 template <> int Vec4::length () const { float lenF = Math::sqrt ((float)dot (*this)); int lenI = (int) (lenF + 0.5f); return lenI; } template <> const Vec4 & Vec4::normalize () { normalizeOrThrow(*this); return *this; } template <> const Vec4 & Vec4::normalizeExc () throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0) && (w == 0)) throw NullVecExc ("Cannot normalize null vector."); normalizeOrThrow(*this); return *this; } template <> const Vec4 & Vec4::normalizeNonNull () { normalizeOrThrow(*this); return *this; } template <> Vec4 Vec4::normalized () const { Vec4 v(*this); normalizeOrThrow(v); return v; } template <> Vec4 Vec4::normalizedExc () const throw (Iex::MathExc) { if ((x == 0) && (y == 0) && (z == 0) && (w == 0)) throw NullVecExc ("Cannot normalize null vector."); Vec4 v(*this); normalizeOrThrow(v); return v; } template <> Vec4 Vec4::normalizedNonNull () const { Vec4 v(*this); normalizeOrThrow(v); return v; } } // namespace Imath