Path: blob/master/3rdparty/openexr/Imath/ImathVecAlgo.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_IMATHVECALGO_H37#define INCLUDED_IMATHVECALGO_H3839//-------------------------------------------------------------------------40//41// This file contains algorithms applied to or in conjunction42// with points (Imath::Vec2 and Imath::Vec3).43// The assumption made is that these functions are called much44// less often than the basic point functions or these functions45// require more support classes.46//47//-------------------------------------------------------------------------4849#include "ImathVec.h"50#include "ImathLimits.h"5152namespace Imath {535455//-----------------------------------------------------------------56// Find the projection of vector t onto vector s (Vec2, Vec3, Vec4)57//-----------------------------------------------------------------5859template <class Vec> Vec project (const Vec &s, const Vec &t);606162//------------------------------------------------63// Find a vector that is perpendicular to s and64// in the same plane as s and t (Vec2, Vec3, Vec4)65//------------------------------------------------6667template <class Vec> Vec orthogonal (const Vec &s, const Vec &t);686970//-----------------------------------------------71// Find the direction of a ray s after reflection72// off a plane with normal t (Vec2, Vec3, Vec4)73//-----------------------------------------------7475template <class Vec> Vec reflect (const Vec &s, const Vec &t);767778//--------------------------------------------------------------------79// Find the vertex of triangle (v0, v1, v2) that is closest to point p80// (Vec2, Vec3, Vec4)81//--------------------------------------------------------------------8283template <class Vec> Vec closestVertex (const Vec &v0,84const Vec &v1,85const Vec &v2,86const Vec &p);8788//---------------89// Implementation90//---------------9192template <class Vec>93Vec94project (const Vec &s, const Vec &t)95{96Vec sNormalized = s.normalized();97return sNormalized * (sNormalized ^ t);98}99100template <class Vec>101Vec102orthogonal (const Vec &s, const Vec &t)103{104return t - project (s, t);105}106107template <class Vec>108Vec109reflect (const Vec &s, const Vec &t)110{111return s - typename Vec::BaseType(2) * (s - project(t, s));112}113114template <class Vec>115Vec116closestVertex(const Vec &v0,117const Vec &v1,118const Vec &v2,119const Vec &p)120{121Vec nearest = v0;122typename Vec::BaseType neardot = (v0 - p).length2();123typename Vec::BaseType tmp = (v1 - p).length2();124125if (tmp < neardot)126{127neardot = tmp;128nearest = v1;129}130131tmp = (v2 - p).length2();132133if (tmp < neardot)134{135neardot = tmp;136nearest = v2;137}138139return nearest;140}141142143} // namespace Imath144145#endif146147148