Blame Imath/ImathLineAlgo.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_IMATHLINEALGO_H
Packit 8dc392
#define INCLUDED_IMATHLINEALGO_H
Packit 8dc392
Packit 8dc392
//------------------------------------------------------------------
Packit 8dc392
//
Packit 8dc392
//	This file contains algorithms applied to or in conjunction
Packit 8dc392
//	with lines (Imath::Line). These algorithms may require
Packit 8dc392
//	more headers to compile. The assumption made is that these
Packit 8dc392
//	functions are called much less often than the basic line
Packit 8dc392
//	functions or these functions require more support classes
Packit 8dc392
//
Packit 8dc392
//	Contains:
Packit 8dc392
//
Packit 8dc392
//	bool closestPoints(const Line<T>& line1,
Packit 8dc392
//			   const Line<T>& line2,
Packit 8dc392
//			   Vec3<T>& point1,
Packit 8dc392
//			   Vec3<T>& point2)
Packit 8dc392
//
Packit 8dc392
//	bool intersect( const Line3<T> &line,
Packit 8dc392
//			const Vec3<T> &v0,
Packit 8dc392
//			const Vec3<T> &v1,
Packit 8dc392
//			const Vec3<T> &v2,
Packit 8dc392
//			Vec3<T> &pt,
Packit 8dc392
//			Vec3<T> &barycentric,
Packit 8dc392
//			bool &front)
Packit 8dc392
//
Packit 8dc392
//      V3f
Packit 8dc392
//      closestVertex(const Vec3<T> &v0,
Packit 8dc392
//                    const Vec3<T> &v1,
Packit 8dc392
//                    const Vec3<T> &v2,
Packit 8dc392
//                    const Line3<T> &l)
Packit 8dc392
//
Packit 8dc392
//	V3f
Packit 8dc392
//	rotatePoint(const Vec3<T> p, Line3<T> l, float angle)
Packit 8dc392
//
Packit 8dc392
//------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
#include "ImathLine.h"
Packit 8dc392
#include "ImathVecAlgo.h"
Packit 8dc392
#include "ImathFun.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
bool
Packit 8dc392
closestPoints
Packit 8dc392
    (const Line3<T>& line1,
Packit 8dc392
     const Line3<T>& line2,
Packit 8dc392
     Vec3<T>& point1,
Packit 8dc392
     Vec3<T>& point2)
Packit 8dc392
{
Packit 8dc392
    //
Packit 8dc392
    // Compute point1 and point2 such that point1 is on line1, point2
Packit 8dc392
    // is on line2 and the distance between point1 and point2 is minimal.
Packit 8dc392
    // This function returns true if point1 and point2 can be computed,
Packit 8dc392
    // or false if line1 and line2 are parallel or nearly parallel.
Packit 8dc392
    // This function assumes that line1.dir and line2.dir are normalized.
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    Vec3<T> w = line1.pos - line2.pos;
Packit 8dc392
    T d1w = line1.dir ^ w;
Packit 8dc392
    T d2w = line2.dir ^ w;
Packit 8dc392
    T d1d2 = line1.dir ^ line2.dir;
Packit 8dc392
    T n1 = d1d2 * d2w - d1w;
Packit 8dc392
    T n2 = d2w - d1d2 * d1w;
Packit 8dc392
    T d = 1 - d1d2 * d1d2;
Packit 8dc392
    T absD = abs (d);
Packit 8dc392
Packit 8dc392
    if ((absD > 1) ||
Packit 8dc392
	(abs (n1) < limits<T>::max() * absD &&
Packit 8dc392
	 abs (n2) < limits<T>::max() * absD))
Packit 8dc392
    {
Packit 8dc392
	point1 = line1 (n1 / d);
Packit 8dc392
	point2 = line2 (n2 / d);
Packit 8dc392
	return true;
Packit 8dc392
    }
Packit 8dc392
    else
Packit 8dc392
    {
Packit 8dc392
	return false;
Packit 8dc392
    }
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
bool
Packit 8dc392
intersect
Packit 8dc392
    (const Line3<T> &line,
Packit 8dc392
     const Vec3<T> &v0,
Packit 8dc392
     const Vec3<T> &v1,
Packit 8dc392
     const Vec3<T> &v2,
Packit 8dc392
     Vec3<T> &pt,
Packit 8dc392
     Vec3<T> &barycentric,
Packit 8dc392
     bool &front)
Packit 8dc392
{
Packit 8dc392
    //
Packit 8dc392
    // Given a line and a triangle (v0, v1, v2), the intersect() function
Packit 8dc392
    // finds the intersection of the line and the plane that contains the
Packit 8dc392
    // triangle.
Packit 8dc392
    //
Packit 8dc392
    // If the intersection point cannot be computed, either because the
Packit 8dc392
    // line and the triangle's plane are nearly parallel or because the
Packit 8dc392
    // triangle's area is very small, intersect() returns false.
Packit 8dc392
    //
Packit 8dc392
    // If the intersection point is outside the triangle, intersect
Packit 8dc392
    // returns false.
Packit 8dc392
    //
Packit 8dc392
    // If the intersection point, pt, is inside the triangle, intersect()
Packit 8dc392
    // computes a front-facing flag and the barycentric coordinates of
Packit 8dc392
    // the intersection point, and returns true.
Packit 8dc392
    //
Packit 8dc392
    // The front-facing flag is true if the dot product of the triangle's
Packit 8dc392
    // normal, (v2-v1)%(v1-v0), and the line's direction is negative.
Packit 8dc392
    //
Packit 8dc392
    // The barycentric coordinates have the following property:
Packit 8dc392
    //
Packit 8dc392
    //     pt = v0 * barycentric.x + v1 * barycentric.y + v2 * barycentric.z
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    Vec3<T> edge0 = v1 - v0;
Packit 8dc392
    Vec3<T> edge1 = v2 - v1;
Packit 8dc392
    Vec3<T> normal = edge1 % edge0;
Packit 8dc392
Packit 8dc392
    T l = normal.length();
Packit 8dc392
Packit 8dc392
    if (l != 0)
Packit 8dc392
	normal /= l;
Packit 8dc392
    else
Packit 8dc392
	return false;	// zero-area triangle
Packit 8dc392
Packit 8dc392
    //
Packit 8dc392
    // d is the distance of line.pos from the plane that contains the triangle.
Packit 8dc392
    // The intersection point is at line.pos + (d/nd) * line.dir.
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    T d = normal ^ (v0 - line.pos);
Packit 8dc392
    T nd = normal ^ line.dir;
Packit 8dc392
Packit 8dc392
    if (abs (nd) > 1 || abs (d) < limits<T>::max() * abs (nd))
Packit 8dc392
	pt = line (d / nd);
Packit 8dc392
    else
Packit 8dc392
	return false;  // line and plane are nearly parallel
Packit 8dc392
Packit 8dc392
    //
Packit 8dc392
    // Compute the barycentric coordinates of the intersection point.
Packit 8dc392
    // The intersection is inside the triangle if all three barycentric
Packit 8dc392
    // coordinates are between zero and one.
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    {
Packit 8dc392
	Vec3<T> en = edge0.normalized();
Packit 8dc392
	Vec3<T> a = pt - v0;
Packit 8dc392
	Vec3<T> b = v2 - v0;
Packit 8dc392
	Vec3<T> c = (a - en * (en ^ a));
Packit 8dc392
	Vec3<T> d = (b - en * (en ^ b));
Packit 8dc392
	T e = c ^ d;
Packit 8dc392
	T f = d ^ d;
Packit 8dc392
Packit 8dc392
	if (e >= 0 && e <= f)
Packit 8dc392
	    barycentric.z = e / f;
Packit 8dc392
	else
Packit 8dc392
	    return false; // outside
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    {
Packit 8dc392
	Vec3<T> en = edge1.normalized();
Packit 8dc392
	Vec3<T> a = pt - v1;
Packit 8dc392
	Vec3<T> b = v0 - v1;
Packit 8dc392
	Vec3<T> c = (a - en * (en ^ a));
Packit 8dc392
	Vec3<T> d = (b - en * (en ^ b));
Packit 8dc392
	T e = c ^ d;
Packit 8dc392
	T f = d ^ d;
Packit 8dc392
Packit 8dc392
	if (e >= 0 && e <= f)
Packit 8dc392
	    barycentric.x = e / f;
Packit 8dc392
	else
Packit 8dc392
	    return false; // outside
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    barycentric.y = 1 - barycentric.x - barycentric.z;
Packit 8dc392
Packit 8dc392
    if (barycentric.y < 0)
Packit 8dc392
	return false; // outside
Packit 8dc392
Packit 8dc392
    front = ((line.dir ^ normal) < 0);
Packit 8dc392
    return true;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
Vec3<T>
Packit 8dc392
closestVertex
Packit 8dc392
    (const Vec3<T> &v0,
Packit 8dc392
     const Vec3<T> &v1,
Packit 8dc392
     const Vec3<T> &v2,
Packit 8dc392
     const Line3<T> &l)
Packit 8dc392
{
Packit 8dc392
    Vec3<T> nearest = v0;
Packit 8dc392
    T neardot       = (v0 - l.closestPointTo(v0)).length2();
Packit 8dc392
    
Packit 8dc392
    T tmp           = (v1 - l.closestPointTo(v1)).length2();
Packit 8dc392
Packit 8dc392
    if (tmp < neardot)
Packit 8dc392
    {
Packit 8dc392
        neardot = tmp;
Packit 8dc392
        nearest = v1;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    tmp = (v2 - l.closestPointTo(v2)).length2();
Packit 8dc392
    if (tmp < neardot)
Packit 8dc392
    {
Packit 8dc392
        neardot = tmp;
Packit 8dc392
        nearest = v2;
Packit 8dc392
    }
Packit 8dc392
Packit 8dc392
    return nearest;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
template <class T>
Packit 8dc392
Vec3<T>
Packit 8dc392
rotatePoint (const Vec3<T> p, Line3<T> l, T angle)
Packit 8dc392
{
Packit 8dc392
    //
Packit 8dc392
    // Rotate the point p around the line l by the given angle.
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    //
Packit 8dc392
    // Form a coordinate frame with <x,y,a>. The rotation is the in xy
Packit 8dc392
    // plane.
Packit 8dc392
    //
Packit 8dc392
Packit 8dc392
    Vec3<T> q = l.closestPointTo(p);
Packit 8dc392
    Vec3<T> x = p - q;
Packit 8dc392
    T radius = x.length();
Packit 8dc392
Packit 8dc392
    x.normalize();
Packit 8dc392
    Vec3<T> y = (x % l.dir).normalize();
Packit 8dc392
Packit 8dc392
    T cosangle = Math<T>::cos(angle);
Packit 8dc392
    T sinangle = Math<T>::sin(angle);
Packit 8dc392
Packit 8dc392
    Vec3<T> r = q + x * radius * cosangle + y * radius * sinangle; 
Packit 8dc392
Packit 8dc392
    return r;
Packit 8dc392
}
Packit 8dc392
Packit 8dc392
Packit 8dc392
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 8dc392
Packit 8dc392
#endif // INCLUDED_IMATHLINEALGO_H