Blame Imath/ImathLine.h

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
Packit 8dc392
Packit 8dc392
#ifndef INCLUDED_IMATHLINE_H
Packit 8dc392
#define INCLUDED_IMATHLINE_H
Packit 8dc392
Packit 8dc392
//-------------------------------------
Packit 8dc392
//
Packit 8dc392
//	A 3D line class template
Packit 8dc392
//
Packit 8dc392
//-------------------------------------
Packit 8dc392
Packit 8dc392
#include "ImathVec.h"
Packit 8dc392
#include "ImathLimits.h"
Packit 8dc392
#include "ImathMatrix.h"
Packit 8dc392
#include "ImathNamespace.h"
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
class Line3
Packit 8dc392
{
Packit 8dc392
  public:
Packit 8dc392
Packit 8dc392
    Vec3<T>			pos;
Packit 8dc392
    Vec3<T>			dir;
Packit 8dc392
    
Packit 8dc392
    //-------------------------------------------------------------
Packit 8dc392
    //	Constructors - default is normalized units along direction
Packit 8dc392
    //-------------------------------------------------------------
Packit 8dc392
Packit 8dc392
    Line3() {}
Packit 8dc392
    Line3(const Vec3<T>& point1, const Vec3<T>& point2);
Packit 8dc392
Packit 8dc392
    //------------------
Packit 8dc392
    //	State Query/Set
Packit 8dc392
    //------------------
Packit 8dc392
Packit 8dc392
    void			set(const Vec3<T>& point1, 
Packit 8dc392
				    const Vec3<T>& point2);
Packit 8dc392
Packit 8dc392
    //-------
Packit 8dc392
    //	F(t)
Packit 8dc392
    //-------
Packit 8dc392
Packit 8dc392
    Vec3<T>			operator() (T parameter) const;
Packit 8dc392
Packit 8dc392
    //---------
Packit 8dc392
    //	Query
Packit 8dc392
    //---------
Packit 8dc392
Packit 8dc392
    T				distanceTo(const Vec3<T>& point) const;
Packit 8dc392
    T				distanceTo(const Line3<T>& line) const;
Packit 8dc392
    Vec3<T>			closestPointTo(const Vec3<T>& point) const;
Packit 8dc392
    Vec3<T>			closestPointTo(const Line3<T>& line) const;
Packit 8dc392
};
Packit 8dc392
Packit 8dc392
Packit 8dc392
//--------------------
Packit 8dc392
// Convenient typedefs
Packit 8dc392
//--------------------
Packit 8dc392
Packit 8dc392
typedef Line3<float> Line3f;
Packit 8dc392
typedef Line3<double> Line3d;
Packit 8dc392
Packit 8dc392
Packit 8dc392
//---------------
Packit 8dc392
// Implementation
Packit 8dc392
//---------------
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)
Packit 8dc392
{
Packit 8dc392
    set(p0,p1);
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)
Packit 8dc392
{
Packit 8dc392
    pos = p0; dir = p1-p0;
Packit 8dc392
    dir.normalize();
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline Vec3<T> Line3<T>::operator()(T parameter) const
Packit 8dc392
{
Packit 8dc392
    return pos + dir * parameter;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline T Line3<T>::distanceTo(const Vec3<T>& point) const
Packit 8dc392
{
Packit 8dc392
    return (closestPointTo(point)-point).length();
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const
Packit 8dc392
{
Packit 8dc392
    return ((point - pos) ^ dir) * dir + pos;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline T Line3<T>::distanceTo(const Line3<T>& line) const
Packit 8dc392
{
Packit 8dc392
    T d = (dir % line.dir) ^ (line.pos - pos);
Packit 8dc392
    return (d >= 0)? d: -d;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
inline Vec3<T> 
Packit 8dc392
Line3<T>::closestPointTo(const Line3<T>& line) const
Packit 8dc392
{
Packit 8dc392
    // Assumes the lines are normalized
Packit 8dc392
Packit 8dc392
    Vec3<T> posLpos = pos - line.pos ;
Packit 8dc392
    T c = dir ^ posLpos;
Packit 8dc392
    T a = line.dir ^ dir;
Packit 8dc392
    T f = line.dir ^ posLpos ;
Packit 8dc392
    T num = c - a * f;
Packit 8dc392
Packit 8dc392
    T denom = a*a - 1;
Packit 8dc392
Packit 8dc392
    T absDenom = ((denom >= 0)? denom: -denom);
Packit 8dc392
Packit 8dc392
    if (absDenom < 1)
Packit 8dc392
    {
Packit 8dc392
	T absNum = ((num >= 0)? num: -num);
Packit 8dc392
Packit 8dc392
	if (absNum >= absDenom * limits<T>::max())
Packit 8dc392
	    return pos;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    return pos + dir * (num / denom);
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template<class T>
Packit 8dc392
std::ostream& operator<< (std::ostream &o, const Line3<T> &line)
Packit 8dc392
{
Packit 8dc392
    return o << "(" << line.pos << ", " << line.dir << ")";
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
template<class S, class T>
Packit 8dc392
inline Line3<S> operator * (const Line3<S> &line, const Matrix44<T> &M)
Packit 8dc392
{
Packit 8dc392
    return Line3<S>( line.pos * M, (line.pos + line.dir) * M );
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 8dc392
Packit 8dc392
#endif // INCLUDED_IMATHLINE_H