1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
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
16 // distribution.
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.
20 //
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.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHVECALGO_H
38 #define INCLUDED_IMATHVECALGO_H
39 
40 //-------------------------------------------------------------------------
41 //
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.
47 //
48 //-------------------------------------------------------------------------
49 
50 #include "ImathVec.h"
51 #include "ImathLimits.h"
52 #include "ImathNamespace.h"
53 
54 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
55 
56 
57 //-----------------------------------------------------------------
58 // Find the projection of vector t onto vector s (Vec2, Vec3, Vec4)
59 //-----------------------------------------------------------------
60 
61 template <class Vec> Vec        project (const Vec &s, const Vec &t);
62 
63 
64 //------------------------------------------------
65 // Find a vector that is perpendicular to s and
66 // in the same plane as s and t (Vec2, Vec3, Vec4)
67 //------------------------------------------------
68 
69 template <class Vec> Vec        orthogonal (const Vec &s, const Vec &t);
70 
71 
72 //-----------------------------------------------
73 // Find the direction of a ray s after reflection
74 // off a plane with normal t (Vec2, Vec3, Vec4)
75 //-----------------------------------------------
76 
77 template <class Vec> Vec        reflect (const Vec &s, const Vec &t);
78 
79 
80 //--------------------------------------------------------------------
81 // Find the vertex of triangle (v0, v1, v2) that is closest to point p
82 // (Vec2, Vec3, Vec4)
83 //--------------------------------------------------------------------
84 
85 template <class Vec> Vec        closestVertex (const Vec &v0,
86                                                const Vec &v1,
87                                                const Vec &v2,
88                                                const Vec &p);
89 
90 //---------------
91 // Implementation
92 //---------------
93 
94 template <class Vec>
95 Vec
project(const Vec & s,const Vec & t)96 project (const Vec &s, const Vec &t)
97 {
98     Vec sNormalized = s.normalized();
99     return sNormalized * (sNormalized ^ t);
100 }
101 
102 template <class Vec>
103 Vec
orthogonal(const Vec & s,const Vec & t)104 orthogonal (const Vec &s, const Vec &t)
105 {
106     return t - project (s, t);
107 }
108 
109 template <class Vec>
110 Vec
reflect(const Vec & s,const Vec & t)111 reflect (const Vec &s, const Vec &t)
112 {
113     return s - typename Vec::BaseType(2) * (s - project(t, s));
114 }
115 
116 template <class Vec>
117 Vec
closestVertex(const Vec & v0,const Vec & v1,const Vec & v2,const Vec & p)118 closestVertex(const Vec &v0,
119               const Vec &v1,
120               const Vec &v2,
121               const Vec &p)
122 {
123     Vec nearest = v0;
124     typename Vec::BaseType neardot = (v0 - p).length2();
125     typename Vec::BaseType tmp     = (v1 - p).length2();
126 
127     if (tmp < neardot)
128     {
129         neardot = tmp;
130         nearest = v1;
131     }
132 
133     tmp = (v2 - p).length2();
134 
135     if (tmp < neardot)
136     {
137         neardot = tmp;
138         nearest = v2;
139     }
140 
141     return nearest;
142 }
143 
144 
145 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
146 
147 #endif // INCLUDED_IMATHVECALGO_H
148