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_IMATHLINE_H
38 #define INCLUDED_IMATHLINE_H
40 //-------------------------------------
42 // A 3D line class template
44 //-------------------------------------
47 #include "ImathLimits.h"
48 #include "ImathMatrix.h"
61 //-------------------------------------------------------------
62 // Constructors - default is normalized units along direction
63 //-------------------------------------------------------------
66 Line3(const Vec3<T>& point1, const Vec3<T>& point2);
72 void set(const Vec3<T>& point1,
73 const Vec3<T>& point2);
79 Vec3<T> operator() (T parameter) const;
85 T distanceTo(const Vec3<T>& point) const;
86 T distanceTo(const Line3<T>& line) const;
87 Vec3<T> closestPointTo(const Vec3<T>& point) const;
88 Vec3<T> closestPointTo(const Line3<T>& line) const;
92 //--------------------
93 // Convenient typedefs
94 //--------------------
96 typedef Line3<float> Line3f;
97 typedef Line3<double> Line3d;
105 inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)
111 inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)
113 pos = p0; dir = p1-p0;
118 inline Vec3<T> Line3<T>::operator()(T parameter) const
120 return pos + dir * parameter;
124 inline T Line3<T>::distanceTo(const Vec3<T>& point) const
126 return (closestPointTo(point)-point).length();
130 inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const
132 return ((point - pos) ^ dir) * dir + pos;
136 inline T Line3<T>::distanceTo(const Line3<T>& line) const
138 T d = (dir % line.dir) ^ (line.pos - pos);
139 return (d >= 0)? d: -d;
144 Line3<T>::closestPointTo(const Line3<T>& line) const
146 // Assumes the lines are normalized
148 Vec3<T> posLpos = pos - line.pos ;
150 T a = line.dir ^ dir;
151 T f = line.dir ^ posLpos ;
156 T absDenom = ((denom >= 0)? denom: -denom);
160 T absNum = ((num >= 0)? num: -num);
162 if (absNum >= absDenom * limits<T>::max())
166 return pos + dir * (num / denom);
170 std::ostream& operator<< (std::ostream &o, const Line3<T> &line)
172 return o << "(" << line.pos << ", " << line.dir << ")";
175 template<class S, class T>
176 inline Line3<S> operator * (const Line3<S> &line, const Matrix44<T> &M)
178 return Line3<S>( line.pos * M, (line.pos + line.dir) * M );