Path: blob/master/3rdparty/openexr/Imath/ImathLine.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHLINE_H37#define INCLUDED_IMATHLINE_H3839//-------------------------------------40//41// A 3D line class template42//43//-------------------------------------4445#include "ImathVec.h"46#include "ImathLimits.h"47#include "ImathMatrix.h"4849namespace Imath {505152template <class T>53class Line354{55public:5657Vec3<T> pos;58Vec3<T> dir;5960//-------------------------------------------------------------61// Constructors - default is normalized units along direction62//-------------------------------------------------------------6364Line3() {}65Line3(const Vec3<T>& point1, const Vec3<T>& point2);6667//------------------68// State Query/Set69//------------------7071void set(const Vec3<T>& point1,72const Vec3<T>& point2);7374//-------75// F(t)76//-------7778Vec3<T> operator() (T parameter) const;7980//---------81// Query82//---------8384T distanceTo(const Vec3<T>& point) const;85T distanceTo(const Line3<T>& line) const;86Vec3<T> closestPointTo(const Vec3<T>& point) const;87Vec3<T> closestPointTo(const Line3<T>& line) const;88};899091//--------------------92// Convenient typedefs93//--------------------9495typedef Line3<float> Line3f;96typedef Line3<double> Line3d;979899//---------------100// Implementation101//---------------102103template <class T>104inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)105{106set(p0,p1);107}108109template <class T>110inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)111{112pos = p0; dir = p1-p0;113dir.normalize();114}115116template <class T>117inline Vec3<T> Line3<T>::operator()(T parameter) const118{119return pos + dir * parameter;120}121122template <class T>123inline T Line3<T>::distanceTo(const Vec3<T>& point) const124{125return (closestPointTo(point)-point).length();126}127128template <class T>129inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const130{131return ((point - pos) ^ dir) * dir + pos;132}133134template <class T>135inline T Line3<T>::distanceTo(const Line3<T>& line) const136{137T d = (dir % line.dir) ^ (line.pos - pos);138return (d >= 0)? d: -d;139}140141template <class T>142inline Vec3<T>143Line3<T>::closestPointTo(const Line3<T>& line) const144{145// Assumes the lines are normalized146147Vec3<T> posLpos = pos - line.pos ;148T c = dir ^ posLpos;149T a = line.dir ^ dir;150T f = line.dir ^ posLpos ;151T num = c - a * f;152153T denom = a*a - 1;154155T absDenom = ((denom >= 0)? denom: -denom);156157if (absDenom < 1)158{159T absNum = ((num >= 0)? num: -num);160161if (absNum >= absDenom * limits<T>::max())162return pos;163}164165return pos + dir * (num / denom);166}167168template<class T>169std::ostream& operator<< (std::ostream &o, const Line3<T> &line)170{171return o << "(" << line.pos << ", " << line.dir << ")";172}173174template<class S, class T>175inline Line3<S> operator * (const Line3<S> &line, const Matrix44<T> &M)176{177return Line3<S>( line.pos * M, (line.pos + line.dir) * M );178}179180181} // namespace Imath182183#endif184185186