1 | /////////////////////////////////////////////////////////////////////////// |
---|
2 | // |
---|
3 | // Copyright (c) 2002, 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 | |
---|
53 | namespace Imath { |
---|
54 | |
---|
55 | |
---|
56 | //-------------------------------------------------------------- |
---|
57 | // Find the projection of vector t onto vector s (Vec2 and Vec3) |
---|
58 | //-------------------------------------------------------------- |
---|
59 | |
---|
60 | template <class Vec> Vec project (const Vec &s, const Vec &t); |
---|
61 | |
---|
62 | |
---|
63 | //---------------------------------------------- |
---|
64 | // Find a vector which is perpendicular to s and |
---|
65 | // in the same plane as s and t (Vec2 and Vec3) |
---|
66 | //---------------------------------------------- |
---|
67 | |
---|
68 | template <class Vec> Vec orthogonal (const Vec &s, const Vec &t); |
---|
69 | |
---|
70 | |
---|
71 | //----------------------------------------------- |
---|
72 | // Find the direction of a ray s after reflection |
---|
73 | // off a plane with normal t (Vec2 and Vec3) |
---|
74 | //----------------------------------------------- |
---|
75 | |
---|
76 | template <class Vec> Vec reflect (const Vec &s, const Vec &t); |
---|
77 | |
---|
78 | |
---|
79 | //---------------------------------------------------------------------- |
---|
80 | // Find the vertex of triangle (v0, v1, v2), which is closest to point p |
---|
81 | // (Vec2 and Vec3). |
---|
82 | //---------------------------------------------------------------------- |
---|
83 | |
---|
84 | template <class Vec> Vec closestVertex (const Vec &v0, |
---|
85 | const Vec &v1, |
---|
86 | const Vec &v2, |
---|
87 | const Vec &p); |
---|
88 | |
---|
89 | //--------------- |
---|
90 | // Implementation |
---|
91 | //--------------- |
---|
92 | |
---|
93 | template <class Vec> |
---|
94 | Vec |
---|
95 | project (const Vec &s, const Vec &t) |
---|
96 | { |
---|
97 | Vec sNormalized = s.normalized(); |
---|
98 | return sNormalized * (sNormalized ^ t); |
---|
99 | } |
---|
100 | |
---|
101 | template <class Vec> |
---|
102 | Vec |
---|
103 | orthogonal (const Vec &s, const Vec &t) |
---|
104 | { |
---|
105 | return t - project (s, t); |
---|
106 | } |
---|
107 | |
---|
108 | template <class Vec> |
---|
109 | Vec |
---|
110 | reflect (const Vec &s, const Vec &t) |
---|
111 | { |
---|
112 | return s - typename Vec::BaseType(2) * (s - project(t, s)); |
---|
113 | } |
---|
114 | |
---|
115 | template <class Vec> |
---|
116 | Vec |
---|
117 | closestVertex(const Vec &v0, |
---|
118 | const Vec &v1, |
---|
119 | const Vec &v2, |
---|
120 | const Vec &p) |
---|
121 | { |
---|
122 | Vec nearest = v0; |
---|
123 | typename Vec::BaseType neardot = (v0 - p).length2(); |
---|
124 | typename Vec::BaseType tmp = (v1 - p).length2(); |
---|
125 | |
---|
126 | if (tmp < neardot) |
---|
127 | { |
---|
128 | neardot = tmp; |
---|
129 | nearest = v1; |
---|
130 | } |
---|
131 | |
---|
132 | tmp = (v2 - p).length2(); |
---|
133 | |
---|
134 | if (tmp < neardot) |
---|
135 | { |
---|
136 | neardot = tmp; |
---|
137 | nearest = v2; |
---|
138 | } |
---|
139 | |
---|
140 | return nearest; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | } // namespace Imath |
---|
145 | |
---|
146 | #endif |
---|