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_IMATHLINE_H
38 #define INCLUDED_IMATHLINE_H
39 
40 //-------------------------------------
41 //
42 //	A 3D line class template
43 //
44 //-------------------------------------
45 
46 #include "ImathVec.h"
47 #include "ImathLimits.h"
48 #include "ImathMatrix.h"
49 #include "ImathNamespace.h"
50 
51 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
52 
53 
54 template <class T>
55 class Line3
56 {
57   public:
58 
59     Vec3<T>			pos;
60     Vec3<T>			dir;
61 
62     //-------------------------------------------------------------
63     //	Constructors - default is normalized units along direction
64     //-------------------------------------------------------------
65 
Line3()66     Line3() {}
67     Line3(const Vec3<T>& point1, const Vec3<T>& point2);
68 
69     //------------------
70     //	State Query/Set
71     //------------------
72 
73     void			set(const Vec3<T>& point1,
74 				    const Vec3<T>& point2);
75 
76     //-------
77     //	F(t)
78     //-------
79 
80     Vec3<T>			operator() (T parameter) const;
81 
82     //---------
83     //	Query
84     //---------
85 
86     T				distanceTo(const Vec3<T>& point) const;
87     T				distanceTo(const Line3<T>& line) const;
88     Vec3<T>			closestPointTo(const Vec3<T>& point) const;
89     Vec3<T>			closestPointTo(const Line3<T>& line) const;
90 };
91 
92 
93 //--------------------
94 // Convenient typedefs
95 //--------------------
96 
97 typedef Line3<float> Line3f;
98 typedef Line3<double> Line3d;
99 
100 
101 //---------------
102 // Implementation
103 //---------------
104 
105 template <class T>
Line3(const Vec3<T> & p0,const Vec3<T> & p1)106 inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)
107 {
108     set(p0,p1);
109 }
110 
111 template <class T>
set(const Vec3<T> & p0,const Vec3<T> & p1)112 inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)
113 {
114     pos = p0; dir = p1-p0;
115     dir.normalize();
116 }
117 
118 template <class T>
operator()119 inline Vec3<T> Line3<T>::operator()(T parameter) const
120 {
121     return pos + dir * parameter;
122 }
123 
124 template <class T>
distanceTo(const Vec3<T> & point)125 inline T Line3<T>::distanceTo(const Vec3<T>& point) const
126 {
127     return (closestPointTo(point)-point).length();
128 }
129 
130 template <class T>
closestPointTo(const Vec3<T> & point)131 inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const
132 {
133     return ((point - pos) ^ dir) * dir + pos;
134 }
135 
136 template <class T>
distanceTo(const Line3<T> & line)137 inline T Line3<T>::distanceTo(const Line3<T>& line) const
138 {
139     T d = (dir % line.dir) ^ (line.pos - pos);
140     return (d >= 0)? d: -d;
141 }
142 
143 template <class T>
144 inline Vec3<T>
closestPointTo(const Line3<T> & line)145 Line3<T>::closestPointTo(const Line3<T>& line) const
146 {
147     // Assumes the lines are normalized
148 
149     Vec3<T> posLpos = pos - line.pos ;
150     T c = dir ^ posLpos;
151     T a = line.dir ^ dir;
152     T f = line.dir ^ posLpos ;
153     T num = c - a * f;
154 
155     T denom = a*a - 1;
156 
157     T absDenom = ((denom >= 0)? denom: -denom);
158 
159     if (absDenom < 1)
160     {
161 	T absNum = ((num >= 0)? num: -num);
162 
163 	if (absNum >= absDenom * limits<T>::max())
164 	    return pos;
165     }
166 
167     return pos + dir * (num / denom);
168 }
169 
170 template<class T>
171 std::ostream& operator<< (std::ostream &o, const Line3<T> &line)
172 {
173     return o << "(" << line.pos << ", " << line.dir << ")";
174 }
175 
176 template<class S, class T>
177 inline Line3<S> operator * (const Line3<S> &line, const Matrix44<T> &M)
178 {
179     return Line3<S>( line.pos * M, (line.pos + line.dir) * M );
180 }
181 
182 
183 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
184 
185 #endif // INCLUDED_IMATHLINE_H
186