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_IMATHLINEALGO_H
38 #define INCLUDED_IMATHLINEALGO_H
40 //------------------------------------------------------------------
42 // This file contains algorithms applied to or in conjunction
43 // with lines (Imath::Line). These algorithms may require
44 // more headers to compile. The assumption made is that these
45 // functions are called much less often than the basic line
46 // functions or these functions require more support classes
50 // bool closestPoints(const Line<T>& line1,
51 // const Line<T>& line2,
55 // bool intersect( const Line3<T> &line,
60 // Vec3<T> &barycentric,
64 // closestVertex(const Vec3<T> &v0,
70 // nearestPointOnTriangle(const Vec3<T> &v0,
76 // rotatePoint(const Vec3<T> p, Line3<T> l, float angle)
78 //------------------------------------------------------------------
80 #include "ImathLine.h"
81 #include "ImathVecAlgo.h"
87 bool closestPoints(const Line3<T>& line1,
88 const Line3<T>& line2,
93 // Compute the closest points on two lines. This was originally
94 // lifted from inventor. This function assumes that the line
95 // directions are normalized. The original math has been collapsed.
98 T A = line1.dir ^ line2.dir;
100 if ( A == 1 ) return false;
104 T B = (line1.dir ^ line1.pos) - (line1.dir ^ line2.pos);
105 T C = (line2.dir ^ line1.pos) - (line2.dir ^ line2.pos);
107 point1 = line1(( B - A * C ) / denom);
108 point2 = line2(( B * A - C ) / denom);
116 bool intersect( const Line3<T> &line,
121 Vec3<T> &barycentric,
124 // Intersect the line with a triangle.
125 // 1. find plane of triangle
126 // 2. find intersection point of ray and plane
127 // 3. pick plane to project point and triangle into
128 // 4. check each edge of triangle to see if point is inside it
131 // XXX TODO - this routine is way too long
132 // - the value of EPSILON is dubious
133 // - there should be versions of this
134 // routine that do not calculate the
135 // barycentric coordinates or the
138 const float EPSILON = 1e-6;
140 T d, t, d01, d12, d20, vd0, vd1, vd2, ax, ay, az, sense;
141 Vec3<T> v01, v12, v20, c;
144 // calculate plane for polygon
148 // c is un-normalized normal
153 return false; // cant hit a triangle with no area
156 // calculate distance to plane along ray
159 if (d < EPSILON && d > -EPSILON)
160 return false; // line is parallel to plane containing triangle
162 t = (v0 - line.pos).dot(c) / d;
167 // calculate intersection point
168 pt = line.pos + t * line.dir;
170 // is point inside triangle? Project to 2d to find out
171 // use the plane that has the largest absolute value
172 // component in the normal
173 ax = c[0] < 0 ? -c[0] : c[0];
174 ay = c[1] < 0 ? -c[1] : c[1];
175 az = c[2] < 0 ? -c[2] : c[2];
177 if(ax > ay && ax > az)
179 // project on x=0 plane
183 sense = c[0] < 0 ? -1 : 1;
189 sense = c[1] < 0 ? -1 : 1;
195 sense = c[2] < 0 ? -1 : 1;
198 // distance from v0-v1 must be less than distance from v2 to v0-v1
199 d01 = sense * ((pt[axis0] - v0[axis0]) * v01[axis1]
200 - (pt[axis1] - v0[axis1]) * v01[axis0]);
202 if(d01 < 0) return false;
204 vd2 = sense * ((v2[axis0] - v0[axis0]) * v01[axis1]
205 - (v2[axis1] - v0[axis1]) * v01[axis0]);
207 if(d01 > vd2) return false;
209 // distance from v1-v2 must be less than distance from v1 to v2-v0
210 d12 = sense * ((pt[axis0] - v1[axis0]) * v12[axis1]
211 - (pt[axis1] - v1[axis1]) * v12[axis0]);
213 if(d12 < 0) return false;
215 vd0 = sense * ((v0[axis0] - v1[axis0]) * v12[axis1]
216 - (v0[axis1] - v1[axis1]) * v12[axis0]);
218 if(d12 > vd0) return false;
220 // calculate v20, and do check on final side of triangle
222 d20 = sense * ((pt[axis0] - v2[axis0]) * v20[axis1]
223 - (pt[axis1] - v2[axis1]) * v20[axis0]);
225 if(d20 < 0) return false;
227 vd1 = sense * ((v1[axis0] - v2[axis0]) * v20[axis1]
228 - (v1[axis1] - v2[axis1]) * v20[axis0]);
230 if(d20 > vd1) return false;
232 // vd0, vd1, and vd2 will always be non-zero for a triangle
233 // that has non-zero area (we return before this for
234 // zero area triangles)
235 barycentric = Vec3<T>(d12 / vd0, d20 / vd1, d01 / vd2);
236 front = line.dir.dot(c) < 0;
243 closestVertex(const Vec3<T> &v0,
248 Vec3<T> nearest = v0;
249 T neardot = (v0 - l.closestPointTo(v0)).length2();
251 T tmp = (v1 - l.closestPointTo(v1)).length2();
259 tmp = (v2 - l.closestPointTo(v2)).length2();
271 nearestPointOnTriangle(const Vec3<T> &v0,
276 Vec3<T> pt, barycentric;
279 if (intersect (l, v0, v1, v2, pt, barycentric, front))
283 // The line did not intersect the triangle, so to be picky, you should
284 // find the closest edge that it passed over/under, but chances are that
285 // 1) another triangle will be closer
286 // 2) the app does not need this much precision for a ray that does not
287 // intersect the triangle
288 // 3) the expense of the calculation is not worth it since this is the
291 // XXX TODO This is bogus -- nearestPointOnTriangle() should do
292 // what its name implies; it should return a point
293 // on an edge if some edge is closer to the line than
294 // any vertex. If the application does not want the
295 // extra calculations, it should be possible to specify
296 // that; it is not up to this nearestPointOnTriangle()
297 // to make the decision.
299 return closestVertex(v0, v1, v2, l);
304 rotatePoint(const Vec3<T> p, Line3<T> l, T angle)
307 // Rotate the point p around the line l by the given angle.
311 // Form a coordinate frame with <x,y,a>. The rotation is the in xy
315 Vec3<T> q = l.closestPointTo(p);
317 T radius = x.length();
320 Vec3<T> y = (x % l.dir).normalize();
322 T cosangle = Math<T>::cos(angle);
323 T sinangle = Math<T>::sin(angle);
325 Vec3<T> r = q + x * radius * cosangle + y * radius * sinangle;