1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHVECALGO_H
38 #define INCLUDED_IMATHVECALGO_H
40 //-------------------------------------------------------------------------
42 // This file contains algorithms applied to or in conjunction
43 // with points (Imath::Vec2 and Imath::Vec3).
44 // The assumption made is that these functions are called much
45 // less often than the basic point functions or these functions
46 // require more support classes.
48 //-------------------------------------------------------------------------
51 #include "ImathLimits.h"
56 //--------------------------------------------------------------
57 // Find the projection of vector t onto vector s (Vec2 and Vec3)
58 //--------------------------------------------------------------
60 template <class Vec> Vec project (const Vec &s, const Vec &t);
63 //----------------------------------------------
64 // Find a vector which is perpendicular to s and
65 // in the same plane as s and t (Vec2 and Vec3)
66 //----------------------------------------------
68 template <class Vec> Vec orthogonal (const Vec &s, const Vec &t);
71 //-----------------------------------------------
72 // Find the direction of a ray s after reflection
73 // off a plane with normal t (Vec2 and Vec3)
74 //-----------------------------------------------
76 template <class Vec> Vec reflect (const Vec &s, const Vec &t);
79 //----------------------------------------------------------------------
80 // Find the vertex of triangle (v0, v1, v2), which is closest to point p
82 //----------------------------------------------------------------------
84 template <class Vec> Vec closestVertex (const Vec &v0,
95 project (const Vec &s, const Vec &t)
97 Vec sNormalized = s.normalized();
98 return sNormalized * (sNormalized ^ t);
103 orthogonal (const Vec &s, const Vec &t)
105 return t - project (s, t);
110 reflect (const Vec &s, const Vec &t)
112 return s - typename Vec::BaseType(2) * (s - project(t, s));
117 closestVertex(const Vec &v0,
123 typename Vec::BaseType neardot = (v0 - p).length2();
124 typename Vec::BaseType tmp = (v1 - p).length2();
132 tmp = (v2 - p).length2();