Added TriangleTriangleIntersect2D function
This commit is contained in:
parent
cf619d282b
commit
ed6221e993
|
@ -1,27 +1,27 @@
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* VCGLib o o *
|
* VCGLib o o *
|
||||||
* Visual and Computer Graphics Library o o *
|
* Visual and Computer Graphics Library o o *
|
||||||
* _ O _ *
|
* _ O _ *
|
||||||
* Copyright(C) 2004 \/)\/ *
|
* Copyright(C) 2004 \/)\/ *
|
||||||
* Visual Computing Lab /\/| *
|
* Visual Computing Lab /\/| *
|
||||||
* ISTI - Italian National Research Council | *
|
* ISTI - Italian National Research Council | *
|
||||||
* \ *
|
* \ *
|
||||||
* All rights reserved. *
|
* All rights reserved. *
|
||||||
* *
|
* *
|
||||||
* This program is free software; you can redistribute it and/or modify *
|
* 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 *
|
* it under the terms of the GNU General Public License as published by *
|
||||||
* the Free Software Foundation; either version 2 of the License, or *
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
* (at your option) any later version. *
|
* (at your option) any later version. *
|
||||||
* *
|
* *
|
||||||
* This program is distributed in the hope that it will be useful, *
|
* This program is distributed in the hope that it will be useful, *
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||||
* for more details. *
|
* for more details. *
|
||||||
* *
|
* *
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
History
|
History
|
||||||
|
|
||||||
$Log: not supported by cvs2svn $
|
$Log: not supported by cvs2svn $
|
||||||
Revision 1.6 2007/05/08 12:11:58 pietroni
|
Revision 1.6 2007/05/08 12:11:58 pietroni
|
||||||
|
@ -45,28 +45,28 @@ added circle-line intersection
|
||||||
|
|
||||||
|
|
||||||
namespace vcg {
|
namespace vcg {
|
||||||
/** \addtogroup space */
|
/** \addtogroup space */
|
||||||
/*@{*/
|
/*@{*/
|
||||||
/**
|
/**
|
||||||
Function computing the intersection between couple of geometric primitives in
|
Function computing the intersection between couple of geometric primitives in
|
||||||
2 dimension
|
2 dimension
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// return true if the algle is convex (right rotation)
|
/// return true if the algle is convex (right rotation)
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool Convex(const Point2<SCALAR_TYPE> & p0,const Point2<SCALAR_TYPE> & p1,const Point2<SCALAR_TYPE> & p2)
|
inline bool Convex(const Point2<SCALAR_TYPE> & p0,const Point2<SCALAR_TYPE> & p1,const Point2<SCALAR_TYPE> & p2)
|
||||||
{
|
{
|
||||||
const SCALAR_TYPE EPS= SCALAR_TYPE(1e-8);
|
const SCALAR_TYPE EPS= SCALAR_TYPE(1e-8);
|
||||||
return (((p0-p1)^(p2-p1))<=EPS);
|
return (((p0-p1)^(p2-p1))<=EPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
///return if exist the intersection point
|
///return if exist the intersection point
|
||||||
///between 2 lines in a 2d plane
|
///between 2 lines in a 2d plane
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool LineLineIntersection(const vcg::Line2<SCALAR_TYPE> & l0,
|
inline bool LineLineIntersection(const vcg::Line2<SCALAR_TYPE> & l0,
|
||||||
const vcg::Line2<SCALAR_TYPE> & l1,
|
const vcg::Line2<SCALAR_TYPE> & l1,
|
||||||
Point2<SCALAR_TYPE> &p)
|
Point2<SCALAR_TYPE> &p)
|
||||||
{
|
{
|
||||||
const SCALAR_TYPE Eps= SCALAR_TYPE(1e-8);
|
const SCALAR_TYPE Eps= SCALAR_TYPE(1e-8);
|
||||||
///first line
|
///first line
|
||||||
SCALAR_TYPE x1=l0.Origin().X();
|
SCALAR_TYPE x1=l0.Origin().X();
|
||||||
|
@ -95,15 +95,15 @@ inline bool LineLineIntersection(const vcg::Line2<SCALAR_TYPE> & l0,
|
||||||
p.X()=numx/den;
|
p.X()=numx/den;
|
||||||
p.Y()=numy/den;
|
p.Y()=numy/den;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
///return if exist the intersection point
|
///return if exist the intersection point
|
||||||
///between 2 lines in a 2d plane
|
///between 2 lines in a 2d plane
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool RayLineIntersection(const vcg::Line2<SCALAR_TYPE> & l,
|
inline bool RayLineIntersection(const vcg::Line2<SCALAR_TYPE> & l,
|
||||||
const vcg::Ray2<SCALAR_TYPE> & r,
|
const vcg::Ray2<SCALAR_TYPE> & r,
|
||||||
Point2<SCALAR_TYPE> &p)
|
Point2<SCALAR_TYPE> &p)
|
||||||
{
|
{
|
||||||
///construct line from ray
|
///construct line from ray
|
||||||
vcg::Line2<SCALAR_TYPE> l_test;
|
vcg::Line2<SCALAR_TYPE> l_test;
|
||||||
l_test.Set(r.Origin(),r.Direction());
|
l_test.Set(r.Origin(),r.Direction());
|
||||||
|
@ -112,15 +112,15 @@ inline bool RayLineIntersection(const vcg::Line2<SCALAR_TYPE> & l,
|
||||||
Point2<SCALAR_TYPE> dir=p-r.Origin();
|
Point2<SCALAR_TYPE> dir=p-r.Origin();
|
||||||
dir.Normalize();
|
dir.Normalize();
|
||||||
return (dir*r.Direction()>0);
|
return (dir*r.Direction()>0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// interseciton between point and triangle
|
/// interseciton between point and triangle
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool RaySegmentIntersection(const vcg::Ray2<SCALAR_TYPE> & r,
|
inline bool RaySegmentIntersection(const vcg::Ray2<SCALAR_TYPE> & r,
|
||||||
const vcg::Segment2<SCALAR_TYPE> &seg,
|
const vcg::Segment2<SCALAR_TYPE> &seg,
|
||||||
Point2<SCALAR_TYPE> &p_inters)
|
Point2<SCALAR_TYPE> &p_inters)
|
||||||
{
|
{
|
||||||
///first compute intersection between lines
|
///first compute intersection between lines
|
||||||
vcg::Line2<SCALAR_TYPE> line2;
|
vcg::Line2<SCALAR_TYPE> line2;
|
||||||
line2.SetOrigin(seg.P0());
|
line2.SetOrigin(seg.P0());
|
||||||
|
@ -135,14 +135,14 @@ inline bool RaySegmentIntersection(const vcg::Ray2<SCALAR_TYPE> & r,
|
||||||
SCALAR_TYPE d1=(seg.P0()-p_inters).Norm();
|
SCALAR_TYPE d1=(seg.P0()-p_inters).Norm();
|
||||||
SCALAR_TYPE lenght=(seg.P0()-seg.P1()).Norm();
|
SCALAR_TYPE lenght=(seg.P0()-seg.P1()).Norm();
|
||||||
return ((d0<lenght)&&(d1<lenght));
|
return ((d0<lenght)&&(d1<lenght));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// interseciton between point and triangle
|
/// interseciton between point and triangle
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool LineSegmentIntersection(const vcg::Line2<SCALAR_TYPE> & line,
|
inline bool LineSegmentIntersection(const vcg::Line2<SCALAR_TYPE> & line,
|
||||||
const vcg::Segment2<SCALAR_TYPE> &seg,
|
const vcg::Segment2<SCALAR_TYPE> &seg,
|
||||||
Point2<SCALAR_TYPE> &p_inters)
|
Point2<SCALAR_TYPE> &p_inters)
|
||||||
{
|
{
|
||||||
///first compute intersection between lines
|
///first compute intersection between lines
|
||||||
vcg::Line2<SCALAR_TYPE> line2;
|
vcg::Line2<SCALAR_TYPE> line2;
|
||||||
line2.SetOrigin(seg.P0());
|
line2.SetOrigin(seg.P0());
|
||||||
|
@ -157,14 +157,14 @@ inline bool LineSegmentIntersection(const vcg::Line2<SCALAR_TYPE> & line,
|
||||||
SCALAR_TYPE d1=(seg.P0()-p_inters).Norm();
|
SCALAR_TYPE d1=(seg.P0()-p_inters).Norm();
|
||||||
SCALAR_TYPE lenght=(seg.P0()-seg.P1()).Norm();
|
SCALAR_TYPE lenght=(seg.P0()-seg.P1()).Norm();
|
||||||
return ((d0<lenght)&&(d1<lenght));
|
return ((d0<lenght)&&(d1<lenght));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// interseciton between point and triangle
|
/// interseciton between point and triangle
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool SegmentSegmentIntersection(const vcg::Segment2<SCALAR_TYPE> &seg0,
|
inline bool SegmentSegmentIntersection(const vcg::Segment2<SCALAR_TYPE> &seg0,
|
||||||
const vcg::Segment2<SCALAR_TYPE> &seg1,
|
const vcg::Segment2<SCALAR_TYPE> &seg1,
|
||||||
Point2<SCALAR_TYPE> &p_inters)
|
Point2<SCALAR_TYPE> &p_inters)
|
||||||
{
|
{
|
||||||
vcg::Line2<SCALAR_TYPE> l0,l1;
|
vcg::Line2<SCALAR_TYPE> l0,l1;
|
||||||
|
|
||||||
l0.SetOrigin(seg0.P0());
|
l0.SetOrigin(seg0.P0());
|
||||||
|
@ -192,11 +192,11 @@ inline bool SegmentSegmentIntersection(const vcg::Segment2<SCALAR_TYPE> &seg0,
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
/// interseciton between point and triangle
|
/// interseciton between point and triangle
|
||||||
template<class SCALAR_TYPE>
|
template<class SCALAR_TYPE>
|
||||||
inline bool IsInsideTrianglePoint( const Triangle2<SCALAR_TYPE> & t,const Point2<SCALAR_TYPE> & p)
|
inline bool IsInsideTrianglePoint( const Triangle2<SCALAR_TYPE> & t,const Point2<SCALAR_TYPE> & p)
|
||||||
{
|
{
|
||||||
Point2<SCALAR_TYPE> p0=t.P0(0);
|
Point2<SCALAR_TYPE> p0=t.P0(0);
|
||||||
Point2<SCALAR_TYPE> p1=t.P0(1);
|
Point2<SCALAR_TYPE> p1=t.P0(1);
|
||||||
Point2<SCALAR_TYPE> p2=t.P0(2);
|
Point2<SCALAR_TYPE> p2=t.P0(2);
|
||||||
|
@ -214,10 +214,48 @@ template<class SCALAR_TYPE>
|
||||||
std::swap<Point2<SCALAR_TYPE> >(p1,p2);
|
std::swap<Point2<SCALAR_TYPE> >(p1,p2);
|
||||||
return((Convex(p,p0,p1))&&(Convex(p,p1,p2))&&(Convex(p,p2,p0)));
|
return((Convex(p,p0,p1))&&(Convex(p,p1,p2))&&(Convex(p,p2,p0)));
|
||||||
//return((Convex(p,p0,p1))&&(Convex(p,p1,p2))&&(Convex(p,p2,p0)));
|
//return((Convex(p,p0,p1))&&(Convex(p,p1,p2))&&(Convex(p,p2,p0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
//intersection between a circle and a line
|
template<class ScalarType>
|
||||||
template<class ScalarType>
|
bool TriangleTriangleIntersect2D(const vcg::Triangle2<ScalarType> &tr0,
|
||||||
|
const vcg::Triangle2<ScalarType> &tr1)
|
||||||
|
{
|
||||||
|
///test BBox Intersection
|
||||||
|
vcg::Box2<ScalarType> bbtr0;
|
||||||
|
bbtr0.Add(tr0.P(0));
|
||||||
|
bbtr0.Add(tr0.P(1));
|
||||||
|
bbtr0.Add(tr0.P(2));
|
||||||
|
vcg::Box2<ScalarType> bbtr1;
|
||||||
|
bbtr1.Add(tr1.P(0));
|
||||||
|
bbtr1.Add(tr1.P(1));
|
||||||
|
bbtr1.Add(tr1.P(2));
|
||||||
|
if (!bbtr0.Collide(bbtr1)) return false;
|
||||||
|
///test vertex in face
|
||||||
|
for (int i=0;i<3;i++)
|
||||||
|
{
|
||||||
|
bool inside0=vcg::IsInsideTrianglePoint(tr0,tr1.P(i));
|
||||||
|
bool inside1=vcg::IsInsideTrianglePoint(tr1,tr0.P(i));
|
||||||
|
if (inside0 || inside1) return true;
|
||||||
|
}
|
||||||
|
///test segment
|
||||||
|
///to segment intersection
|
||||||
|
for (int i=0;i<3;i++)
|
||||||
|
{
|
||||||
|
for (int j=0;j<3;j++)
|
||||||
|
{
|
||||||
|
if (i>j) continue;
|
||||||
|
vcg::Segment2<ScalarType> seg0=vcg::Segment2<ScalarType>(tr0.P(i),tr0.P((i+1)%3));
|
||||||
|
vcg::Segment2<ScalarType> seg1=vcg::Segment2<ScalarType>(tr1.P(j),tr1.P((j+1)%3));
|
||||||
|
vcg::Point2<ScalarType> p_inters;
|
||||||
|
bool intersect=SegmentSegmentIntersection(seg0,seg1,p_inters);
|
||||||
|
if (intersect) return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//intersection between a circle and a line
|
||||||
|
template<class ScalarType>
|
||||||
inline bool CircleLineIntersection(const vcg::Line2<ScalarType> & line,
|
inline bool CircleLineIntersection(const vcg::Line2<ScalarType> & line,
|
||||||
const vcg::Point2<ScalarType> ¢er,
|
const vcg::Point2<ScalarType> ¢er,
|
||||||
const ScalarType &radius,
|
const ScalarType &radius,
|
||||||
|
@ -255,6 +293,6 @@ template<class ScalarType>
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*@}*/
|
/*@}*/
|
||||||
} // end namespace
|
} // end namespace
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue