source: NonGTP/OpenEXR/include/Imath/ImathLine.h @ 855

Revision 855, 4.6 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[855]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_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
50namespace Imath {
51
52
53template <class T>
54class Line3
55{
56  public:
57
58    Vec3<T>                     pos;
59    Vec3<T>                     dir;
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
96typedef Line3<float> Line3f;
97typedef Line3<double> Line3d;
98
99
100//---------------
101// Implementation
102//---------------
103
104template <class T>
105inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)
106{
107    set(p0,p1);
108}
109
110template <class T>
111inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)
112{
113    pos = p0; dir = p1-p0;
114    dir.normalize();
115}
116
117template <class T>
118inline Vec3<T> Line3<T>::operator()(T parameter) const
119{
120    return pos + dir * parameter;
121}
122
123template <class T>
124inline T Line3<T>::distanceTo(const Vec3<T>& point) const
125{
126    return (closestPointTo(point)-point).length();
127}
128
129template <class T>
130inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const
131{
132    return ((point - pos) ^ dir) * dir + pos;
133}
134
135template <class T>
136inline 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
142template <class T>
143inline Vec3<T>
144Line3<T>::closestPointTo(const Line3<T>& line) const
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
169template<class T>
170std::ostream& operator<< (std::ostream &o, const Line3<T> &line)
171{
172    return o << "(" << line.pos << ", " << line.dir << ")";
173}
174
175template<class S, class T>
176inline 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
Note: See TracBrowser for help on using the repository browser.