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