Blame Imath/ImathVecAlgo.h

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
Packit 8dc392
Packit 8dc392
#ifndef INCLUDED_IMATHVECALGO_H
Packit 8dc392
#define INCLUDED_IMATHVECALGO_H
Packit 8dc392
Packit 8dc392
//-------------------------------------------------------------------------
Packit 8dc392
//
Packit 8dc392
//      This file contains algorithms applied to or in conjunction
Packit 8dc392
//      with points (Imath::Vec2 and Imath::Vec3).
Packit 8dc392
//      The assumption made is that these functions are called much
Packit 8dc392
//      less often than the basic point functions or these functions
Packit 8dc392
//      require more support classes.
Packit 8dc392
//
Packit 8dc392
//-------------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
#include "ImathVec.h"
Packit 8dc392
#include "ImathLimits.h"
Packit 8dc392
#include "ImathNamespace.h"
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 8dc392
Packit 8dc392
Packit 8dc392
//-----------------------------------------------------------------
Packit 8dc392
// Find the projection of vector t onto vector s (Vec2, Vec3, Vec4)
Packit 8dc392
//-----------------------------------------------------------------
Packit 8dc392
Packit 8dc392
template <class Vec> Vec        project (const Vec &s, const Vec &t);
Packit 8dc392
Packit 8dc392
Packit 8dc392
//------------------------------------------------
Packit 8dc392
// Find a vector that is perpendicular to s and
Packit 8dc392
// in the same plane as s and t (Vec2, Vec3, Vec4)
Packit 8dc392
//------------------------------------------------
Packit 8dc392
Packit 8dc392
template <class Vec> Vec        orthogonal (const Vec &s, const Vec &t);
Packit 8dc392
Packit 8dc392
Packit 8dc392
//-----------------------------------------------
Packit 8dc392
// Find the direction of a ray s after reflection
Packit 8dc392
// off a plane with normal t (Vec2, Vec3, Vec4)
Packit 8dc392
//-----------------------------------------------
Packit 8dc392
Packit 8dc392
template <class Vec> Vec        reflect (const Vec &s, const Vec &t);
Packit 8dc392
Packit 8dc392
Packit 8dc392
//--------------------------------------------------------------------
Packit 8dc392
// Find the vertex of triangle (v0, v1, v2) that is closest to point p
Packit 8dc392
// (Vec2, Vec3, Vec4)
Packit 8dc392
//--------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
template <class Vec> Vec        closestVertex (const Vec &v0,
Packit 8dc392
                                               const Vec &v1,
Packit 8dc392
                                               const Vec &v2, 
Packit 8dc392
                                               const Vec &p);
Packit 8dc392
Packit 8dc392
//---------------
Packit 8dc392
// Implementation
Packit 8dc392
//---------------
Packit 8dc392
Packit 8dc392
template <class Vec>
Packit 8dc392
Vec
Packit 8dc392
project (const Vec &s, const Vec &t)
Packit 8dc392
{
Packit 8dc392
    Vec sNormalized = s.normalized();
Packit 8dc392
    return sNormalized * (sNormalized ^ t);
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class Vec>
Packit 8dc392
Vec
Packit 8dc392
orthogonal (const Vec &s, const Vec &t)
Packit 8dc392
{
Packit 8dc392
    return t - project (s, t);
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class Vec>
Packit 8dc392
Vec
Packit 8dc392
reflect (const Vec &s, const Vec &t)
Packit 8dc392
{
Packit 8dc392
    return s - typename Vec::BaseType(2) * (s - project(t, s));
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class Vec>
Packit 8dc392
Vec
Packit 8dc392
closestVertex(const Vec &v0,
Packit 8dc392
              const Vec &v1,
Packit 8dc392
              const Vec &v2, 
Packit 8dc392
              const Vec &p)
Packit 8dc392
{
Packit 8dc392
    Vec nearest = v0;
Packit 8dc392
    typename Vec::BaseType neardot = (v0 - p).length2();
Packit 8dc392
    typename Vec::BaseType tmp     = (v1 - p).length2();
Packit 8dc392
Packit 8dc392
    if (tmp < neardot)
Packit 8dc392
    {
Packit 8dc392
        neardot = tmp;
Packit 8dc392
        nearest = v1;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    tmp = (v2 - p).length2();
Packit 8dc392
Packit 8dc392
    if (tmp < neardot)
Packit 8dc392
    {
Packit 8dc392
        neardot = tmp;
Packit 8dc392
        nearest = v2;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    return nearest;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 8dc392
Packit 8dc392
#endif // INCLUDED_IMATHVECALGO_H