2004-04-21 16:22:27 +02:00
|
|
|
/****************************************************************************
|
2016-06-13 07:29:25 +02:00
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2004-04-21 16:22:27 +02:00
|
|
|
|
|
|
|
#ifndef __VCGLIB_INTERSECTION_3
|
|
|
|
#define __VCGLIB_INTERSECTION_3
|
|
|
|
|
2007-06-07 17:16:39 +02:00
|
|
|
#include <vcg/math/base.h>
|
2004-04-21 16:22:27 +02:00
|
|
|
#include <vcg/space/point3.h>
|
2004-04-26 14:34:50 +02:00
|
|
|
#include <vcg/space/line3.h>
|
2005-09-28 21:40:55 +02:00
|
|
|
#include <vcg/space/ray3.h>
|
2004-04-26 14:34:50 +02:00
|
|
|
#include <vcg/space/plane3.h>
|
|
|
|
#include <vcg/space/segment3.h>
|
|
|
|
#include <vcg/space/sphere3.h>
|
|
|
|
#include <vcg/space/triangle3.h>
|
|
|
|
#include <vcg/space/intersection/triangle_triangle3.h>
|
2004-04-21 16:22:27 +02:00
|
|
|
|
|
|
|
|
2004-04-26 14:34:50 +02:00
|
|
|
|
2004-10-13 14:45:51 +02:00
|
|
|
|
|
|
|
namespace vcg {
|
2004-04-26 14:34:50 +02:00
|
|
|
/** \addtogroup space */
|
|
|
|
/*@{*/
|
2014-04-07 16:48:16 +02:00
|
|
|
/**
|
2004-05-14 01:43:54 +02:00
|
|
|
Function computing the intersection between couple of geometric primitives in
|
|
|
|
3 dimension
|
|
|
|
*/
|
|
|
|
/// interseciton between sphere and line
|
|
|
|
template<class T>
|
2005-06-29 17:28:31 +02:00
|
|
|
inline bool IntersectionLineSphere( const Sphere3<T> & sp, const Line3<T> & li, Point3<T> & p0,Point3<T> & p1 ){
|
2004-05-14 01:43:54 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
// Per prima cosa si sposta il sistema di riferimento
|
2004-05-14 01:43:54 +02:00
|
|
|
// fino a portare il centro della sfera nell'origine
|
|
|
|
Point3<T> neworig=li.Origin()-sp.Center();
|
|
|
|
// poi si risolve il sistema di secondo grado (con maple...)
|
2004-05-14 05:14:29 +02:00
|
|
|
T t1 = li.Direction().X()*li.Direction().X();
|
|
|
|
T t2 = li.Direction().Y()*li.Direction().Y();
|
|
|
|
T t3 = li.Direction().Z()*li.Direction().Z();
|
|
|
|
T t6 = neworig.Y()*li.Direction().Y();
|
|
|
|
T t7 = neworig.X()*li.Direction().X();
|
|
|
|
T t8 = neworig.Z()*li.Direction().Z();
|
2004-05-14 01:43:54 +02:00
|
|
|
T t15 = sp.Radius()*sp.Radius();
|
2004-05-14 05:14:29 +02:00
|
|
|
T t17 = neworig.Z()*neworig.Z();
|
|
|
|
T t19 = neworig.Y()*neworig.Y();
|
|
|
|
T t21 = neworig.X()*neworig.X();
|
2004-07-12 00:08:04 +02:00
|
|
|
T t28 = T(2.0*t7*t6+2.0*t6*t8+2.0*t7*t8+t1*t15-t1*t17-t1*t19-t2*t21+t2*t15-t2*t17-t3*t21+t3*t15-t3*t19);
|
2004-05-14 01:43:54 +02:00
|
|
|
if(t28<0) return false;
|
2014-04-07 16:48:16 +02:00
|
|
|
T t29 = sqrt(t28);
|
|
|
|
T val0 = 1/(t1+t2+t3)*(-t6-t7-t8+t29);
|
2004-05-14 01:43:54 +02:00
|
|
|
T val1 = 1/(t1+t2+t3)*(-t6-t7-t8-t29);
|
|
|
|
|
|
|
|
p0=li.P(val0);
|
|
|
|
p1=li.P(val1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
/*
|
|
|
|
* Function computing the intersection between a sphere and a segment.
|
|
|
|
* @param[in] sphere the sphere
|
|
|
|
* @param[in] segment the segment
|
|
|
|
* @param[out] intersection the intersection point, meaningful only if the segment intersects the sphere
|
|
|
|
* \return (0, 1 or 2) the number of intersections between the segment and the sphere.
|
|
|
|
* t1 is a valid intersection only if the returned value is at least 1;
|
|
|
|
* similarly t2 is valid iff the returned value is 2.
|
|
|
|
*/
|
|
|
|
template < class SCALAR_TYPE >
|
|
|
|
inline int IntersectionSegmentSphere(const Sphere3<SCALAR_TYPE>& sphere, const Segment3<SCALAR_TYPE>& segment, Point3<SCALAR_TYPE> & t0, Point3<SCALAR_TYPE> & t1)
|
|
|
|
{
|
|
|
|
typedef SCALAR_TYPE ScalarType;
|
|
|
|
typedef typename vcg::Point3< ScalarType > Point3t;
|
|
|
|
|
|
|
|
Point3t s = segment.P0() - sphere.Center();
|
|
|
|
Point3t r = segment.P1() - segment.P0();
|
|
|
|
|
|
|
|
ScalarType rho2 = sphere.Radius()*sphere.Radius();
|
|
|
|
|
|
|
|
ScalarType sr = s*r;
|
|
|
|
ScalarType r_squared_norm = r.SquaredNorm();
|
|
|
|
ScalarType s_squared_norm = s.SquaredNorm();
|
|
|
|
ScalarType sigma = sr*sr - r_squared_norm*(s_squared_norm-rho2);
|
|
|
|
|
|
|
|
if (sigma<ScalarType(0.0)) // the line containing the edge doesn't intersect the sphere
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ScalarType sqrt_sigma = ScalarType(sqrt( ScalarType(sigma) ));
|
|
|
|
ScalarType lambda1 = (-sr - sqrt_sigma)/r_squared_norm;
|
|
|
|
ScalarType lambda2 = (-sr + sqrt_sigma)/r_squared_norm;
|
|
|
|
|
|
|
|
int solution_count = 0;
|
|
|
|
if (ScalarType(0.0)<=lambda1 && lambda1<=ScalarType(1.0))
|
|
|
|
{
|
2010-09-22 00:09:13 +02:00
|
|
|
ScalarType t_enter = std::max< ScalarType >(lambda1, ScalarType(0.0));
|
2014-04-07 16:48:16 +02:00
|
|
|
t0 = segment.P0() + r*t_enter;
|
|
|
|
solution_count++;
|
|
|
|
}
|
2007-05-29 16:33:29 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
if (ScalarType(0.0)<=lambda2 && lambda2<=ScalarType(1.0))
|
|
|
|
{
|
|
|
|
Point3t *pt = (solution_count>0) ? &t1 : &t0;
|
2010-09-22 00:09:13 +02:00
|
|
|
ScalarType t_exit = std::min< ScalarType >(lambda2, ScalarType(1.0));
|
2014-04-07 16:48:16 +02:00
|
|
|
*pt = segment.P0() + r*t_exit;
|
|
|
|
solution_count++;
|
|
|
|
}
|
|
|
|
return solution_count;
|
|
|
|
}; // end of IntersectionSegmentSphere
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Compute the intersection between a sphere and a triangle.
|
|
|
|
* \param[in] sphere the input sphere
|
|
|
|
* \param[in] triangle the input triangle
|
|
|
|
* \param[out] witness it is the point on the triangle nearest to the center of the sphere (even when there isn't intersection)
|
|
|
|
* \param[out] res if not null, in the first item is stored the minimum distance between the triangle and the sphere,
|
|
|
|
* while in the second item is stored the penetration depth
|
|
|
|
* \return true iff there is an intersection between the sphere and the triangle
|
|
|
|
*/
|
|
|
|
template < class SCALAR_TYPE, class TRIANGLETYPE >
|
|
|
|
bool IntersectionSphereTriangle(const vcg::Sphere3 < SCALAR_TYPE > & sphere ,
|
|
|
|
TRIANGLETYPE triangle,
|
|
|
|
vcg::Point3 < SCALAR_TYPE > & witness ,
|
|
|
|
std::pair< SCALAR_TYPE, SCALAR_TYPE > * res=NULL)
|
|
|
|
{
|
|
|
|
typedef SCALAR_TYPE ScalarType;
|
|
|
|
typedef typename vcg::Point3< ScalarType > Point3t;
|
|
|
|
|
|
|
|
bool penetration_detected = false;
|
|
|
|
|
|
|
|
ScalarType radius = sphere.Radius();
|
|
|
|
Point3t center = sphere.Center();
|
|
|
|
Point3t p0 = triangle.P(0)-center;
|
|
|
|
Point3t p1 = triangle.P(1)-center;
|
|
|
|
Point3t p2 = triangle.P(2)-center;
|
|
|
|
|
|
|
|
Point3t p10 = p1-p0;
|
|
|
|
Point3t p21 = p2-p1;
|
|
|
|
Point3t p20 = p2-p0;
|
|
|
|
|
|
|
|
ScalarType delta0_p01 = p10.dot(p1);
|
|
|
|
ScalarType delta1_p01 = -p10.dot(p0);
|
|
|
|
ScalarType delta0_p02 = p20.dot(p2);
|
|
|
|
ScalarType delta2_p02 = -p20.dot(p0);
|
|
|
|
ScalarType delta1_p12 = p21.dot(p2);
|
|
|
|
ScalarType delta2_p12 = -p21.dot(p1);
|
|
|
|
|
|
|
|
// the closest point can be one of the vertices of the triangle
|
|
|
|
if (delta1_p01<=ScalarType(0.0) && delta2_p02<=ScalarType(0.0)) { witness = p0; }
|
|
|
|
else if (delta0_p01<=ScalarType(0.0) && delta2_p12<=ScalarType(0.0)) { witness = p1; }
|
|
|
|
else if (delta0_p02<=ScalarType(0.0) && delta1_p12<=ScalarType(0.0)) { witness = p2; }
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ScalarType temp = p10.dot(p2);
|
|
|
|
ScalarType delta0_p012 = delta0_p01*delta1_p12 + delta2_p12*temp;
|
|
|
|
ScalarType delta1_p012 = delta1_p01*delta0_p02 - delta2_p02*temp;
|
|
|
|
ScalarType delta2_p012 = delta2_p02*delta0_p01 - delta1_p01*(p20.dot(p1));
|
|
|
|
|
|
|
|
// otherwise, can be a point lying on same edge of the triangle
|
|
|
|
if (delta0_p012<=ScalarType(0.0))
|
|
|
|
{
|
|
|
|
ScalarType denominator = delta1_p12+delta2_p12;
|
|
|
|
ScalarType mu1 = delta1_p12/denominator;
|
|
|
|
ScalarType mu2 = delta2_p12/denominator;
|
|
|
|
witness = (p1*mu1 + p2*mu2);
|
|
|
|
}
|
|
|
|
else if (delta1_p012<=ScalarType(0.0))
|
|
|
|
{
|
|
|
|
ScalarType denominator = delta0_p02+delta2_p02;
|
|
|
|
ScalarType mu0 = delta0_p02/denominator;
|
|
|
|
ScalarType mu2 = delta2_p02/denominator;
|
|
|
|
witness = (p0*mu0 + p2*mu2);
|
|
|
|
}
|
|
|
|
else if (delta2_p012<=ScalarType(0.0))
|
|
|
|
{
|
|
|
|
ScalarType denominator = delta0_p01+delta1_p01;
|
|
|
|
ScalarType mu0 = delta0_p01/denominator;
|
|
|
|
ScalarType mu1 = delta1_p01/denominator;
|
|
|
|
witness = (p0*mu0 + p1*mu1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// or else can be an point internal to the triangle
|
|
|
|
ScalarType denominator = delta0_p012 + delta1_p012 + delta2_p012;
|
|
|
|
ScalarType lambda0 = delta0_p012/denominator;
|
|
|
|
ScalarType lambda1 = delta1_p012/denominator;
|
|
|
|
ScalarType lambda2 = delta2_p012/denominator;
|
|
|
|
witness = p0*lambda0 + p1*lambda1 + p2*lambda2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res!=NULL)
|
|
|
|
{
|
|
|
|
ScalarType witness_norm = witness.Norm();
|
2010-09-22 00:09:13 +02:00
|
|
|
res->first = std::max< ScalarType >( witness_norm-radius, ScalarType(0.0) );
|
|
|
|
res->second = std::max< ScalarType >( radius-witness_norm, ScalarType(0.0) );
|
2014-04-07 16:48:16 +02:00
|
|
|
}
|
|
|
|
penetration_detected = (witness.SquaredNorm() <= (radius*radius));
|
|
|
|
witness += center;
|
|
|
|
return penetration_detected;
|
|
|
|
}; //end of IntersectionSphereTriangle
|
2007-06-07 17:16:39 +02:00
|
|
|
|
2004-05-14 01:43:54 +02:00
|
|
|
/// intersection between line and plane
|
|
|
|
template<class T>
|
2012-01-18 17:41:34 +01:00
|
|
|
inline bool IntersectionPlaneLine( const Plane3<T> & pl, const Line3<T> & li, Point3<T> & po){
|
2004-05-14 01:43:54 +02:00
|
|
|
const T epsilon = T(1e-8);
|
|
|
|
|
2008-10-27 20:35:17 +01:00
|
|
|
T k = pl.Direction().dot(li.Direction()); // Compute 'k' factor
|
2004-05-14 01:43:54 +02:00
|
|
|
if( (k > -epsilon) && (k < epsilon))
|
|
|
|
return false;
|
2008-10-27 20:35:17 +01:00
|
|
|
T r = (pl.Offset() - pl.Direction().dot(li.Origin()))/k; // Compute ray distance
|
2004-05-14 01:43:54 +02:00
|
|
|
po = li.Origin() + li.Direction()*r;
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-18 17:41:34 +01:00
|
|
|
/// intersection between line and plane
|
|
|
|
template<class T>
|
|
|
|
inline bool IntersectionLinePlane(const Line3<T> & li, const Plane3<T> & pl, Point3<T> & po){
|
|
|
|
|
|
|
|
return IntersectionPlaneLine(pl,li,po);
|
|
|
|
}
|
|
|
|
|
2006-03-20 15:42:49 +01:00
|
|
|
/// intersection between segment and plane
|
|
|
|
template<class T>
|
2010-04-20 02:41:45 +02:00
|
|
|
inline bool IntersectionPlaneSegment( const Plane3<T> & pl, const Segment3<T> & s, Point3<T> & p0){
|
2014-04-07 16:48:16 +02:00
|
|
|
T p1_proj = s.P1()*pl.Direction()-pl.Offset();
|
|
|
|
T p0_proj = s.P0()*pl.Direction()-pl.Offset();
|
2011-05-11 11:56:23 +02:00
|
|
|
if ( (p1_proj>0)-(p0_proj<0)) return false;
|
|
|
|
|
|
|
|
if(p0_proj == p1_proj) return false;
|
|
|
|
|
|
|
|
// check that we perform the computation in a way that is independent with v0 v1 swaps
|
|
|
|
if(p0_proj < p1_proj)
|
|
|
|
p0 = s.P0() + (s.P1()-s.P0()) * fabs(p0_proj/(p1_proj-p0_proj));
|
|
|
|
if(p0_proj > p1_proj)
|
|
|
|
p0 = s.P1() + (s.P0()-s.P1()) * fabs(p1_proj/(p0_proj-p1_proj));
|
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
return true;
|
2006-03-20 15:42:49 +01:00
|
|
|
}
|
2004-05-14 01:43:54 +02:00
|
|
|
|
|
|
|
/// intersection between segment and plane
|
2010-04-20 02:41:45 +02:00
|
|
|
template<class ScalarType>
|
|
|
|
inline bool IntersectionPlaneSegmentEpsilon(const Plane3<ScalarType> & pl,
|
|
|
|
const Segment3<ScalarType> & sg,
|
|
|
|
Point3<ScalarType> & po,
|
|
|
|
const ScalarType epsilon = ScalarType(1e-8)){
|
2004-05-14 01:43:54 +02:00
|
|
|
|
2010-04-20 02:41:45 +02:00
|
|
|
typedef ScalarType T;
|
make point2 derived Eigen's Matrix, and a set of minimal fixes to make meshlab compile
with both old and new version. The fixes include:
- dot product: vec0 * vec1 => vec0.dot(vec1) (I added .dot() to the old Point classes too)
- Transpose: Transpose is an Eigen type, so we cannot keep it if Eigen is used. Therefore
I added a .tranpose() to old matrix classes, and modified most of the Transpose() to transpose()
both in vcg and meshlab. In fact, transpose() are free with Eigen, it simply returns a transpose
expression without copies. On the other be carefull: m = m.transpose() won't work as expected,
here me must evaluate to a temporary: m = m.transpose().eval(); However, this operation in very
rarely needed: you transpose at the same sime you set m, or you use m.transpose() directly.
- the last issue is Normalize which both modifies *this and return a ref to it. This behavior
don't make sense anymore when using expression template, e.g., in (a+b).Normalize(), the type
of a+b if not a Point (or whatever Vector types), it an expression of the addition of 2 points,
so we cannot modify the value of *this, since there is no value. Therefore I've already changed
all those .Normalize() of expressions to the Eigen's version .normalized().
- Finally I've changed the Zero to SetZero in the old Point classes too.
2008-10-28 01:59:46 +01:00
|
|
|
T k = pl.Direction().dot((sg.P1()-sg.P0()));
|
2004-05-14 01:43:54 +02:00
|
|
|
if( (k > -epsilon) && (k < epsilon))
|
|
|
|
return false;
|
make point2 derived Eigen's Matrix, and a set of minimal fixes to make meshlab compile
with both old and new version. The fixes include:
- dot product: vec0 * vec1 => vec0.dot(vec1) (I added .dot() to the old Point classes too)
- Transpose: Transpose is an Eigen type, so we cannot keep it if Eigen is used. Therefore
I added a .tranpose() to old matrix classes, and modified most of the Transpose() to transpose()
both in vcg and meshlab. In fact, transpose() are free with Eigen, it simply returns a transpose
expression without copies. On the other be carefull: m = m.transpose() won't work as expected,
here me must evaluate to a temporary: m = m.transpose().eval(); However, this operation in very
rarely needed: you transpose at the same sime you set m, or you use m.transpose() directly.
- the last issue is Normalize which both modifies *this and return a ref to it. This behavior
don't make sense anymore when using expression template, e.g., in (a+b).Normalize(), the type
of a+b if not a Point (or whatever Vector types), it an expression of the addition of 2 points,
so we cannot modify the value of *this, since there is no value. Therefore I've already changed
all those .Normalize() of expressions to the Eigen's version .normalized().
- Finally I've changed the Zero to SetZero in the old Point classes too.
2008-10-28 01:59:46 +01:00
|
|
|
T r = (pl.Offset() - pl.Direction().dot(sg.P0()))/k; // Compute ray distance
|
2004-05-14 01:43:54 +02:00
|
|
|
if( (r<0) || (r > 1.0))
|
|
|
|
return false;
|
|
|
|
po = sg.P0()*(1-r)+sg.P1() * r;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
/// intersection between plane and triangle
|
2004-05-14 01:43:54 +02:00
|
|
|
// not optimal: uses plane-segment intersection (and the fact the two or none edges can be intersected)
|
2011-11-21 09:54:35 +01:00
|
|
|
// its use is rather dangerous because it can return inconsistent stuff on degenerate cases.
|
|
|
|
// added assert to underline this danger.
|
2014-04-07 16:48:16 +02:00
|
|
|
template<typename TRIANGLETYPE>
|
|
|
|
inline bool IntersectionPlaneTriangle( const Plane3<typename TRIANGLETYPE::ScalarType> & pl,
|
|
|
|
const TRIANGLETYPE & tr,
|
2010-04-20 02:41:45 +02:00
|
|
|
Segment3<typename TRIANGLETYPE::ScalarType> & sg)
|
|
|
|
{
|
|
|
|
typedef typename TRIANGLETYPE::ScalarType T;
|
2012-11-15 20:11:28 +01:00
|
|
|
if(IntersectionPlaneSegment(pl,Segment3<T>(tr.cP(0),tr.cP(1)),sg.P0()))
|
|
|
|
{
|
|
|
|
if(IntersectionPlaneSegment(pl,Segment3<T>(tr.cP(0),tr.cP(2)),sg.P1()))
|
2010-04-20 02:41:45 +02:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
2012-11-15 20:11:28 +01:00
|
|
|
if(IntersectionPlaneSegment(pl,Segment3<T>(tr.cP(1),tr.cP(2)),sg.P1()))
|
2011-11-21 09:54:35 +01:00
|
|
|
return true;
|
|
|
|
else assert(0);
|
|
|
|
return true;
|
2010-04-20 02:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-14 01:43:54 +02:00
|
|
|
else
|
|
|
|
{
|
2012-11-15 20:11:28 +01:00
|
|
|
if(IntersectionPlaneSegment(pl,Segment3<T>(tr.cP(1),tr.cP(2)),sg.P0()))
|
2010-04-20 02:41:45 +02:00
|
|
|
{
|
2012-11-15 20:11:28 +01:00
|
|
|
if(IntersectionPlaneSegment(pl,Segment3<T>(tr.cP(0),tr.cP(2)),sg.P1()))return true;
|
2011-11-21 09:54:35 +01:00
|
|
|
assert(0);
|
2010-04-20 02:41:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2004-05-14 01:43:54 +02:00
|
|
|
}
|
2010-04-20 02:41:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-05-14 01:43:54 +02:00
|
|
|
|
2012-11-15 20:11:28 +01:00
|
|
|
/// intersection between two triangles
|
|
|
|
template<typename TRIANGLETYPE>
|
2010-04-20 02:41:45 +02:00
|
|
|
inline bool IntersectionTriangleTriangle(const TRIANGLETYPE & t0,const TRIANGLETYPE & t1){
|
2012-11-15 20:11:28 +01:00
|
|
|
return NoDivTriTriIsect(t0.cP(0),t0.cP(1),t0.cP(2),
|
|
|
|
t1.cP(0),t1.cP(1),t1.cP(2));
|
|
|
|
}
|
2008-10-28 12:47:37 +01:00
|
|
|
|
2004-05-14 01:43:54 +02:00
|
|
|
template<class T>
|
2010-04-20 02:41:45 +02:00
|
|
|
inline bool IntersectionTriangleTriangle(Point3<T> V0,Point3<T> V1,Point3<T> V2,
|
2014-04-07 16:48:16 +02:00
|
|
|
Point3<T> U0,Point3<T> U1,Point3<T> U2){
|
2004-05-14 01:43:54 +02:00
|
|
|
return NoDivTriTriIsect(V0,V1,V2,U0,U1,U2);
|
|
|
|
}
|
2008-10-28 12:47:37 +01:00
|
|
|
#if 0
|
2004-05-14 01:43:54 +02:00
|
|
|
template<class T>
|
|
|
|
inline bool Intersection(Point3<T> V0,Point3<T> V1,Point3<T> V2,
|
2014-04-07 16:48:16 +02:00
|
|
|
Point3<T> U0,Point3<T> U1,Point3<T> U2,int *coplanar,
|
|
|
|
Point3<T> &isectpt1,Point3<T> &isectpt2){
|
2004-05-14 01:43:54 +02:00
|
|
|
|
|
|
|
return tri_tri_intersect_with_isectline(V0,V1,V2,U0,U1,U2,
|
2014-04-07 16:48:16 +02:00
|
|
|
coplanar,isectpt1,isectpt2);
|
2004-05-14 01:43:54 +02:00
|
|
|
}
|
2004-08-04 22:55:02 +02:00
|
|
|
|
2004-05-14 01:43:54 +02:00
|
|
|
template<typename TRIANGLETYPE,typename SEGMENTTYPE >
|
|
|
|
inline bool Intersection(const TRIANGLETYPE & t0,const TRIANGLETYPE & t1,bool &coplanar,
|
2014-04-07 16:48:16 +02:00
|
|
|
SEGMENTTYPE & sg){
|
|
|
|
Point3<typename SEGMENTTYPE::PointType> ip0,ip1;
|
2004-05-14 01:43:54 +02:00
|
|
|
return tri_tri_intersect_with_isectline(t0.P0(0),t0.P0(1),t0.P0(2),
|
2014-04-07 16:48:16 +02:00
|
|
|
t1.P0(0),t1.P0(1),t1.P0(2),
|
|
|
|
coplanar,sg.P0(),sg.P1()
|
|
|
|
);
|
2004-05-14 01:43:54 +02:00
|
|
|
}
|
2004-04-21 16:22:27 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function computing the intersection between a line and a triangle.
|
|
|
|
* from:
|
|
|
|
* Tomas Moller and Ben Trumbore,
|
|
|
|
* ``Fast, Minimum Storage Ray-Triangle Intersection'',
|
|
|
|
* journal of graphics tools, vol. 2, no. 1, pp. 21-28, 1997
|
|
|
|
* @param[in] line
|
|
|
|
* @param[in] triangle vertices
|
|
|
|
* @param[out]=(t,u,v) the intersection point, meaningful only if the line intersects the triangle
|
|
|
|
* t is the line parameter and
|
|
|
|
* (u,v) are the baricentric coords of the intersection point
|
|
|
|
*
|
|
|
|
* Line.Orig + t * Line.Dir = (1-u-v) * Vert0 + u * Vert1 +v * Vert2
|
|
|
|
*
|
|
|
|
*/
|
2005-09-28 21:40:55 +02:00
|
|
|
|
2004-08-04 22:55:02 +02:00
|
|
|
template<class T>
|
2014-04-07 16:48:16 +02:00
|
|
|
bool IntersectionLineTriangle( const Line3<T> & line, const Point3<T> & vert0,
|
|
|
|
const Point3<T> & vert1, const Point3<T> & vert2,
|
|
|
|
T & t ,T & u, T & v)
|
2009-03-17 19:59:20 +01:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
#define EPSIL 0.000001
|
2009-03-17 19:59:20 +01:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
vcg::Point3<T> edge1, edge2, tvec, pvec, qvec;
|
|
|
|
T det,inv_det;
|
2009-03-17 19:59:20 +01:00
|
|
|
|
|
|
|
/* find vectors for two edges sharing vert0 */
|
|
|
|
edge1 = vert1 - vert0;
|
|
|
|
edge2 = vert2 - vert0;
|
|
|
|
|
|
|
|
/* begin calculating determinant - also used to calculate U parameter */
|
|
|
|
pvec = line.Direction() ^ edge2;
|
|
|
|
|
|
|
|
/* if determinant is near zero, line lies in plane of triangle */
|
|
|
|
det = edge1 * pvec;
|
|
|
|
|
|
|
|
/* calculate distance from vert0 to line origin */
|
|
|
|
tvec = line.Origin() - vert0;
|
|
|
|
inv_det = 1.0 / det;
|
2014-04-07 16:48:16 +02:00
|
|
|
|
2009-03-17 19:59:20 +01:00
|
|
|
qvec = tvec ^ edge1;
|
2014-04-07 16:48:16 +02:00
|
|
|
|
2009-03-17 19:59:20 +01:00
|
|
|
if (det > EPSIL)
|
|
|
|
{
|
|
|
|
u = tvec * pvec ;
|
|
|
|
if ( u < 0.0 || u > det)
|
2014-04-07 16:48:16 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-03-17 19:59:20 +01:00
|
|
|
/* calculate V parameter and test bounds */
|
|
|
|
v = line.Direction() * qvec;
|
|
|
|
if ( v < 0.0 || u + v > det)
|
2014-04-07 16:48:16 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-03-17 19:59:20 +01:00
|
|
|
}
|
|
|
|
else if(det < -EPSIL)
|
|
|
|
{
|
|
|
|
/* calculate U parameter and test bounds */
|
|
|
|
u = tvec * pvec ;
|
|
|
|
if ( u > 0.0 || u < det)
|
2014-04-07 16:48:16 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-03-17 19:59:20 +01:00
|
|
|
/* calculate V parameter and test bounds */
|
|
|
|
v = line.Direction() * qvec ;
|
|
|
|
if ( v > 0.0 || u + v < det)
|
2014-04-07 16:48:16 +02:00
|
|
|
return 0;
|
2009-03-17 19:59:20 +01:00
|
|
|
}
|
|
|
|
else return 0; /* line is parallell to the plane of the triangle */
|
|
|
|
|
|
|
|
t = edge2 * qvec * inv_det;
|
|
|
|
( u) *= inv_det;
|
|
|
|
( v) *= inv_det;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-04 22:55:02 +02:00
|
|
|
|
2009-01-23 15:33:13 +01:00
|
|
|
template<class T>
|
2014-04-07 16:48:16 +02:00
|
|
|
bool IntersectionRayTriangle( const Ray3<T> & ray, const Point3<T> & vert0,
|
|
|
|
const Point3<T> & vert1, const Point3<T> & vert2,
|
|
|
|
T & t ,T & u, T & v)
|
2009-01-23 15:33:13 +01:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
Line3<T> line(ray.Origin(), ray.Direction());
|
|
|
|
if (IntersectionLineTriangle(line, vert0, vert1, vert2, t, u, v))
|
|
|
|
{
|
|
|
|
if (t < 0) return 0;
|
|
|
|
else return 1;
|
|
|
|
}else return 0;
|
2009-01-23 15:33:13 +01:00
|
|
|
}
|
2004-08-04 22:55:02 +02:00
|
|
|
|
2005-10-03 18:07:50 +02:00
|
|
|
// line-box
|
2005-01-26 11:03:08 +01:00
|
|
|
template<class T>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionLineBox( const Box3<T> & box, const Line3<T> & r, Point3<T> & coord )
|
2005-01-26 11:03:08 +01:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
const int NUMDIM = 3;
|
|
|
|
const int RIGHT = 0;
|
|
|
|
const int LEFT = 1;
|
|
|
|
const int MIDDLE = 2;
|
2005-01-26 11:03:08 +01:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
int inside = 1;
|
|
|
|
char quadrant[NUMDIM];
|
2005-01-26 11:03:08 +01:00
|
|
|
int i;
|
|
|
|
int whichPlane;
|
|
|
|
Point3<T> maxT,candidatePlane;
|
2014-04-07 16:48:16 +02:00
|
|
|
|
|
|
|
// Find candidate planes; this loop can be avoided if
|
|
|
|
// rays cast all from the eye(assume perpsective view)
|
2005-01-26 11:03:08 +01:00
|
|
|
for (i=0; i<NUMDIM; i++)
|
|
|
|
{
|
|
|
|
if(r.Origin()[i] < box.min[i])
|
2014-04-07 16:48:16 +02:00
|
|
|
{
|
|
|
|
quadrant[i] = LEFT;
|
|
|
|
candidatePlane[i] = box.min[i];
|
|
|
|
inside = 0;
|
|
|
|
}
|
|
|
|
else if (r.Origin()[i] > box.max[i])
|
|
|
|
{
|
|
|
|
quadrant[i] = RIGHT;
|
|
|
|
candidatePlane[i] = box.max[i];
|
|
|
|
inside = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quadrant[i] = MIDDLE;
|
|
|
|
}
|
2005-01-26 11:03:08 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
// Ray origin inside bounding box
|
|
|
|
if(inside){
|
|
|
|
coord = r.Origin();
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-26 11:03:08 +01:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
// Calculate T distances to candidate planes
|
2005-01-26 11:03:08 +01:00
|
|
|
for (i = 0; i < NUMDIM; i++)
|
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
if (quadrant[i] != MIDDLE && r.Direction()[i] !=0.)
|
|
|
|
maxT[i] = (candidatePlane[i]-r.Origin()[i]) / r.Direction()[i];
|
|
|
|
else
|
|
|
|
maxT[i] = -1.;
|
2005-01-26 11:03:08 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
// Get largest of the maxT's for final choice of intersection
|
2005-01-26 11:03:08 +01:00
|
|
|
whichPlane = 0;
|
|
|
|
for (i = 1; i < NUMDIM; i++)
|
2014-04-07 16:48:16 +02:00
|
|
|
if (maxT[whichPlane] < maxT[i])
|
|
|
|
whichPlane = i;
|
2005-01-26 11:03:08 +01:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
// Check final candidate actually inside box
|
2005-01-26 11:03:08 +01:00
|
|
|
if (maxT[whichPlane] < 0.) return false;
|
|
|
|
for (i = 0; i < NUMDIM; i++)
|
2014-04-07 16:48:16 +02:00
|
|
|
if (whichPlane != i)
|
|
|
|
{
|
|
|
|
coord[i] = r.Origin()[i] + maxT[whichPlane] *r.Direction()[i];
|
|
|
|
if (coord[i] < box.min[i] || coord[i] > box.max[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
coord[i] = candidatePlane[i];
|
|
|
|
}
|
2005-01-26 11:03:08 +01:00
|
|
|
return true; // ray hits box
|
2014-04-07 16:48:16 +02:00
|
|
|
}
|
2005-01-26 11:03:08 +01:00
|
|
|
|
2005-10-03 18:07:50 +02:00
|
|
|
// ray-box
|
|
|
|
template<class T>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionRayBox( const Box3<T> & box, const Ray3<T> & r, Point3<T> & coord )
|
2005-10-03 18:07:50 +02:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
Line3<T> l;
|
|
|
|
l.SetOrigin(r.Origin());
|
|
|
|
l.SetDirection(r.Direction());
|
2010-04-20 02:41:45 +02:00
|
|
|
return(IntersectionLineBox<T>(box,l,coord));
|
2014-04-07 16:48:16 +02:00
|
|
|
}
|
2005-10-03 18:07:50 +02:00
|
|
|
|
2006-03-20 15:42:49 +01:00
|
|
|
// segment-box return fist intersection found from p0 to p1
|
2006-06-01 10:38:02 +02:00
|
|
|
template<class ScalarType>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionSegmentBox( const Box3<ScalarType> & box,
|
2014-04-07 16:48:16 +02:00
|
|
|
const Segment3<ScalarType> & s,
|
|
|
|
Point3<ScalarType> & coord )
|
2006-01-20 17:35:51 +01:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
//as first perform box-box intersection
|
|
|
|
Box3<ScalarType> test;
|
|
|
|
test.Add(s.P0());
|
|
|
|
test.Add(s.P1());
|
|
|
|
if (!test.Collide(box))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Line3<ScalarType> l;
|
|
|
|
Point3<ScalarType> dir=s.P1()-s.P0();
|
|
|
|
dir.Normalize();
|
|
|
|
l.SetOrigin(s.P0());
|
|
|
|
l.SetDirection(dir);
|
2010-04-20 02:41:45 +02:00
|
|
|
if(IntersectionLineBox<ScalarType>(box,l,coord))
|
2014-04-07 16:48:16 +02:00
|
|
|
return (test.IsIn(coord));
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-01 10:38:02 +02:00
|
|
|
}
|
2006-01-20 17:35:51 +01:00
|
|
|
|
2006-03-20 15:42:49 +01:00
|
|
|
// segment-box intersection , return number of intersections and intersection points
|
2006-06-01 10:38:02 +02:00
|
|
|
template<class ScalarType>
|
2010-04-20 02:41:45 +02:00
|
|
|
int IntersectionSegmentBox( const Box3<ScalarType> & box,
|
2014-04-07 16:48:16 +02:00
|
|
|
const Segment3<ScalarType> & s,
|
|
|
|
Point3<ScalarType> & coord0,
|
|
|
|
Point3<ScalarType> & coord1 )
|
2006-03-20 15:42:49 +01:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
int num=0;
|
|
|
|
Segment3<ScalarType> test= s;
|
2010-04-20 02:41:45 +02:00
|
|
|
if (IntersectionSegmentBox(box,test,coord0 ))
|
2014-04-07 16:48:16 +02:00
|
|
|
{
|
|
|
|
num++;
|
|
|
|
Point3<ScalarType> swap=test.P0();
|
|
|
|
test.P0()=test.P1();
|
|
|
|
test.P1()=swap;
|
2010-04-20 02:41:45 +02:00
|
|
|
if (IntersectionSegmentBox(box,test,coord1 ))
|
2014-04-07 16:48:16 +02:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
2005-10-03 18:07:50 +02:00
|
|
|
|
2010-04-20 02:41:45 +02:00
|
|
|
/**
|
|
|
|
* Compute the intersection between a segment and a triangle.
|
|
|
|
* It relies on the lineTriangle Intersection
|
|
|
|
* @param[in] segment
|
|
|
|
* @param[in] triangle vertices
|
|
|
|
* @param[out]=(t,u,v) the intersection point, meaningful only if the line of segment intersects the triangle
|
|
|
|
* t is the baricentric coord of the point on the segment
|
|
|
|
* (u,v) are the baricentric coords of the intersection point in the segment
|
|
|
|
*
|
|
|
|
*/
|
2006-06-01 10:38:02 +02:00
|
|
|
template<class ScalarType>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionSegmentTriangle( const vcg::Segment3<ScalarType> & seg,
|
2014-04-07 16:48:16 +02:00
|
|
|
const Point3<ScalarType> & vert0,
|
|
|
|
const Point3<ScalarType> & vert1, const
|
|
|
|
Point3<ScalarType> & vert2,
|
|
|
|
ScalarType & a ,ScalarType & b)
|
2006-06-01 10:38:02 +02:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
//control intersection of bounding boxes
|
|
|
|
vcg::Box3<ScalarType> bb0,bb1;
|
|
|
|
bb0.Add(seg.P0());
|
|
|
|
bb0.Add(seg.P1());
|
|
|
|
bb1.Add(vert0);
|
|
|
|
bb1.Add(vert1);
|
|
|
|
bb1.Add(vert2);
|
|
|
|
Point3<ScalarType> inter;
|
|
|
|
if (!bb0.Collide(bb1))
|
|
|
|
return false;
|
2010-04-20 02:41:45 +02:00
|
|
|
if (!vcg::IntersectionSegmentBox(bb1,seg,inter))
|
2014-04-07 16:48:16 +02:00
|
|
|
return false;
|
2006-10-25 18:04:32 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
//first set both directions of ray
|
2010-04-20 02:41:45 +02:00
|
|
|
vcg::Line3<ScalarType> line;
|
2014-04-07 16:48:16 +02:00
|
|
|
vcg::Point3<ScalarType> dir;
|
|
|
|
ScalarType length=seg.Length();
|
|
|
|
dir=(seg.P1()-seg.P0());
|
|
|
|
dir.Normalize();
|
2010-04-20 02:41:45 +02:00
|
|
|
line.Set(seg.P0(),dir);
|
2014-04-07 16:48:16 +02:00
|
|
|
ScalarType orig_dist;
|
2010-10-15 17:15:27 +02:00
|
|
|
if(IntersectionLineTriangle<ScalarType>(line,vert0,vert1,vert2,orig_dist,a,b))
|
2012-10-15 11:42:05 +02:00
|
|
|
return (orig_dist>=0 && orig_dist<=length);
|
2010-10-15 17:15:27 +02:00
|
|
|
return false;
|
2010-04-20 02:41:45 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Compute the intersection between a segment and a triangle.
|
|
|
|
* Wrapper of the above function
|
|
|
|
*/
|
|
|
|
template<class TriangleType>
|
|
|
|
bool IntersectionSegmentTriangle( const vcg::Segment3<typename TriangleType::ScalarType> & seg,
|
|
|
|
const TriangleType &t,
|
2010-10-15 17:15:27 +02:00
|
|
|
typename TriangleType::ScalarType & a ,typename TriangleType::ScalarType & b)
|
2010-04-20 02:41:45 +02:00
|
|
|
{
|
2012-11-15 20:11:28 +01:00
|
|
|
return IntersectionSegmentTriangle(seg,t.cP(0),t.cP(1),t.cP(2),a,b);
|
2006-06-01 10:38:02 +02:00
|
|
|
}
|
|
|
|
|
2010-04-20 02:41:45 +02:00
|
|
|
template<class ScalarType>
|
|
|
|
bool IntersectionPlaneBox(const vcg::Plane3<ScalarType> &pl,
|
2014-04-07 16:48:16 +02:00
|
|
|
vcg::Box3<ScalarType> &bbox)
|
2006-06-01 10:38:02 +02:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
ScalarType dist,dist1;
|
|
|
|
if(bbox.IsNull()) return false; // intersection with a null bbox is empty
|
|
|
|
dist = SignedDistancePlanePoint(pl,bbox.P(0)) ;
|
|
|
|
for (int i=1;i<8;i++) if( SignedDistancePlanePoint(pl,bbox.P(i))*dist<0) return true;
|
|
|
|
return true;
|
2006-06-01 10:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class ScalarType>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionTriangleBox(const vcg::Box3<ScalarType> &bbox,
|
2014-04-07 16:48:16 +02:00
|
|
|
const vcg::Point3<ScalarType> &p0,
|
|
|
|
const vcg::Point3<ScalarType> &p1,
|
|
|
|
const vcg::Point3<ScalarType> &p2)
|
2006-06-01 10:38:02 +02:00
|
|
|
{
|
2014-04-07 16:48:16 +02:00
|
|
|
typedef typename vcg::Point3<ScalarType> CoordType;
|
|
|
|
CoordType intersection;
|
|
|
|
/// control bounding box collision
|
|
|
|
vcg::Box3<ScalarType> test;
|
|
|
|
test.SetNull();
|
|
|
|
test.Add(p0);
|
|
|
|
test.Add(p1);
|
|
|
|
test.Add(p2);
|
|
|
|
if (!test.Collide(bbox))
|
|
|
|
return false;
|
|
|
|
/// control if each point is inside the bouding box
|
|
|
|
if ((bbox.IsIn(p0))||(bbox.IsIn(p1))||(bbox.IsIn(p2)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/////control plane of the triangle with bbox
|
|
|
|
//vcg::Plane3<ScalarType> plTri=vcg::Plane3<ScalarType>();
|
|
|
|
//plTri.Init(p0,p1,p2);
|
2010-04-20 02:41:45 +02:00
|
|
|
//if (!IntersectionPlaneBox<ScalarType>(plTri,bbox))
|
2014-04-07 16:48:16 +02:00
|
|
|
// return false;
|
2006-06-01 10:38:02 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
///then control intersection of segments with box
|
2010-04-20 02:41:45 +02:00
|
|
|
if (IntersectionSegmentBox<ScalarType>(bbox,vcg::Segment3<ScalarType>(p0,p1),intersection)||
|
|
|
|
IntersectionSegmentBox<ScalarType>(bbox,vcg::Segment3<ScalarType>(p1,p2),intersection)||
|
|
|
|
IntersectionSegmentBox<ScalarType>(bbox,vcg::Segment3<ScalarType>(p2,p0),intersection))
|
2014-04-07 16:48:16 +02:00
|
|
|
return true;
|
|
|
|
///control intersection of diagonal of the cube with triangle
|
|
|
|
|
|
|
|
Segment3<ScalarType> diag[4];
|
|
|
|
|
|
|
|
diag[0]=Segment3<ScalarType>(bbox.P(0),bbox.P(7));
|
|
|
|
diag[1]=Segment3<ScalarType>(bbox.P(1),bbox.P(6));
|
|
|
|
diag[2]=Segment3<ScalarType>(bbox.P(2),bbox.P(5));
|
|
|
|
diag[3]=Segment3<ScalarType>(bbox.P(3),bbox.P(4));
|
2014-07-01 12:08:00 +02:00
|
|
|
ScalarType a,b;
|
2014-04-07 16:48:16 +02:00
|
|
|
for (int i=0;i<3;i++)
|
|
|
|
if (IntersectionSegmentTriangle<ScalarType>(diag[i],p0,p1,p2,a,b))
|
|
|
|
return true;
|
2006-06-01 10:38:02 +02:00
|
|
|
|
2014-04-07 16:48:16 +02:00
|
|
|
return false;
|
2006-06-01 10:38:02 +02:00
|
|
|
}
|
|
|
|
|
2006-09-14 10:39:07 +02:00
|
|
|
template <class SphereType>
|
2010-04-20 02:41:45 +02:00
|
|
|
bool IntersectionSphereSphere( const SphereType & s0,const SphereType & s1){
|
2014-04-07 16:48:16 +02:00
|
|
|
return (s0.Center()-s1.Center()).SquaredNorm() < (s0.Radius()+s1.Radius())*(s0.Radius()+s1.Radius());
|
2006-09-14 10:39:07 +02:00
|
|
|
}
|
|
|
|
|
2005-03-15 12:22:39 +01:00
|
|
|
template<class T>
|
2007-04-11 00:26:47 +02:00
|
|
|
bool IntersectionPlanePlane (const Plane3<T> & plane0, const Plane3<T> & plane1,
|
|
|
|
Line3<T> & line)
|
2005-03-15 12:22:39 +01:00
|
|
|
{
|
|
|
|
// If Cross(N0,N1) is zero, then either planes are parallel and separated
|
|
|
|
// or the same plane. In both cases, 'false' is returned. Otherwise,
|
|
|
|
// the intersection line is
|
|
|
|
//
|
|
|
|
// L(t) = t*Cross(N0,N1) + c0*N0 + c1*N1
|
|
|
|
//
|
|
|
|
// for some coefficients c0 and c1 and for t any real number (the line
|
|
|
|
// parameter). Taking dot products with the normals,
|
|
|
|
//
|
|
|
|
// d0 = Dot(N0,L) = c0*Dot(N0,N0) + c1*Dot(N0,N1)
|
|
|
|
// d1 = Dot(N1,L) = c0*Dot(N0,N1) + c1*Dot(N1,N1)
|
|
|
|
//
|
|
|
|
// which are two equations in two unknowns. The solution is
|
|
|
|
//
|
|
|
|
// c0 = (Dot(N1,N1)*d0 - Dot(N0,N1)*d1)/det
|
|
|
|
// c1 = (Dot(N0,N0)*d1 - Dot(N0,N1)*d0)/det
|
|
|
|
//
|
|
|
|
// where det = Dot(N0,N0)*Dot(N1,N1)-Dot(N0,N1)^2.
|
|
|
|
|
|
|
|
T n00 = plane0.Direction()*plane0.Direction();
|
|
|
|
T n01 = plane0.Direction()*plane1.Direction();
|
|
|
|
T n11 = plane1.Direction()*plane1.Direction();
|
|
|
|
T det = n00*n11-n01*n01;
|
|
|
|
|
|
|
|
const T tolerance = (T)(1e-06f);
|
2014-04-07 16:48:16 +02:00
|
|
|
if ( math::Abs(det) < tolerance )
|
2005-03-15 12:22:39 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
T invDet = 1.0f/det;
|
|
|
|
T c0 = (n11*plane0.Offset() - n01*plane1.Offset())*invDet;
|
|
|
|
T c1 = (n00*plane1.Offset() - n01*plane0.Offset())*invDet;
|
|
|
|
|
|
|
|
line.SetDirection(plane0.Direction()^plane1.Direction());
|
|
|
|
line.SetOrigin(plane0.Direction()*c0+ plane1.Direction()*c1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-26 11:03:08 +01:00
|
|
|
|
|
|
|
|
2005-09-29 13:48:00 +02:00
|
|
|
// Ray-Triangle Functor
|
|
|
|
template <bool BACKFACETEST = true>
|
|
|
|
class RayTriangleIntersectionFunctor {
|
|
|
|
public:
|
2014-04-07 16:48:16 +02:00
|
|
|
template <class TRIANGLETYPE, class SCALARTYPE>
|
|
|
|
inline bool operator () (const TRIANGLETYPE & f, const Ray3<SCALARTYPE> & ray, SCALARTYPE & t) {
|
|
|
|
typedef SCALARTYPE ScalarType;
|
|
|
|
ScalarType u;
|
|
|
|
ScalarType v;
|
|
|
|
|
|
|
|
bool bret = IntersectionRayTriangle(ray, Point3<SCALARTYPE>::Construct(f.cP(0)), Point3<SCALARTYPE>::Construct(f.cP(1)), Point3<SCALARTYPE>::Construct(f.cP(2)), t, u, v);
|
|
|
|
if (BACKFACETEST) {
|
|
|
|
if (!bret) {
|
|
|
|
bret = IntersectionRayTriangle(ray, Point3<SCALARTYPE>::Construct(f.cP(0)), Point3<SCALARTYPE>::Construct(f.cP(2)), Point3<SCALARTYPE>::Construct(f.cP(1)), t, u, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (bret);
|
|
|
|
}
|
2005-09-29 13:48:00 +02:00
|
|
|
};
|
2014-04-07 16:48:16 +02:00
|
|
|
|
2005-01-26 11:03:08 +01:00
|
|
|
|
2004-10-13 14:45:51 +02:00
|
|
|
/*@}*/
|
2004-04-29 12:48:44 +02:00
|
|
|
|
2009-01-22 15:34:27 +01:00
|
|
|
|
2004-04-21 16:22:27 +02:00
|
|
|
} // end namespace
|
2004-05-14 01:43:54 +02:00
|
|
|
#endif
|