*active intersection(Triangle_2,Iso_rectangle_2) already implemented

(with a bug-fix in the implementation)
*add do_intersect_2(Triangle_2,Iso_rectangle_2)
*add test
This commit is contained in:
Sébastien Loriot 2012-10-15 09:56:22 +00:00
parent b2732125e5
commit c4b1beb81f
3 changed files with 103 additions and 8 deletions

View File

@ -30,14 +30,15 @@
#include <vector>
#include <list>
namespace CGAL{
template <class R>
namespace CGAL{ namespace internal{
template <class K>
Object
intersection(const Triangle_2<R> &t, const Iso_rectangle_2<R> &r)
intersection(const typename K::Triangle_2 &t, const typename K::Iso_rectangle_2 &r, const K&)
{
typedef typename R::FT FT;
typedef Segment_2<R> Segment;
typedef Point_2<R> Point;
typedef typename K::FT FT;
typedef typename K::Segment_2 Segment;
typedef typename K::Point_2 Point;
FT xr1, yr1, xr2, yr2;
bool position[3][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
@ -252,7 +253,10 @@ namespace CGAL{
result.push_back(s[k].source());
}
}
//remove duplicated consecutive points
typename std::vector<Point>::iterator last = std::unique(result.begin(),result.end());
result.erase(last,result.end());
switch(result.size()){
case 0:
return Object();
@ -261,7 +265,7 @@ namespace CGAL{
case 2:
return make_object(Segment(result[0], result[1]));
case 3:
return make_object(Triangle_2<R>(result[0], result[1], result[2]));
return make_object(typename K::Triangle_2(result[0], result[1], result[2]));
default:
return make_object(result);
}
@ -269,6 +273,74 @@ namespace CGAL{
}//end if(intersection)
return Object();
}//end intersection
template <class K>
Object
inline intersection(const typename K::Iso_rectangle_2 &r, const typename K::Triangle_2 &t, const K& k)
{
return intersection(t,r,k);
}
template <class K>
bool do_intersect(
const typename K::Triangle_2 &tr,
const typename K::Iso_rectangle_2 &ir,
const K& k)
{
//1) check if at least one vertex of tr is not outside ir
//2) if not, check if at least on vertex of tr is not outside tr
typename K::Has_on_unbounded_side_2 unbounded_side=k.has_on_unbounded_side_2_object();
typename K::Construct_vertex_2 vertex=k.construct_vertex_2_object();
for (int i=0;i<3;++i)
if ( !unbounded_side( ir,vertex(tr,i) ) ) return true;
for (int i=0;i<4;++i)
if ( !unbounded_side( tr,vertex(ir,i) ) ) return true;
return false;
}
template <class K>
inline bool do_intersect(
const typename K::Iso_rectangle_2 &ir,
const typename K::Triangle_2 &tr,
const K& k)
{
return do_intersect(tr,ir,k);
}
} //namespace internal
template <class K>
Object
inline intersection(const Iso_rectangle_2<K> &r, const Triangle_2<K> &t)
{
return typename K::Intersect_2()(r,t);
}
template <class K>
Object
inline intersection(const Triangle_2<K> &t, const Iso_rectangle_2<K> &r)
{
return typename K::Intersect_2()(t,r);
}
template <class K>
inline bool
do_intersect(const Iso_rectangle_2<K> & iso,
const Triangle_2<K> &tr)
{
typedef typename K::Do_intersect_2 Do_intersect;
return Do_intersect()(iso, tr);
}
template <class K>
inline bool
do_intersect(const Triangle_2<K> &tr, const Iso_rectangle_2<K> &iso)
{
typedef typename K::Do_intersect_2 Do_intersect;
return Do_intersect()(tr, iso);
}
}//end namespace
#endif

View File

@ -36,5 +36,6 @@
#include <CGAL/Segment_2_Iso_rectangle_2_intersection.h>
#include <CGAL/Point_2_Iso_rectangle_2_intersection.h>
#include <CGAL/Iso_rectangle_2_Iso_rectangle_2_intersection.h>
#include <CGAL/Triangle_2_Iso_rectangle_2_intersection.h>
#endif

View File

@ -111,6 +111,14 @@ struct Test {
assert(CGAL::intersection(o1, o2).empty());
assert(!CGAL::do_intersect(o2, o1));
assert(CGAL::intersection(o2, o1).empty());
//check with the functors
typename CGAL::Kernel_traits<O1>::Kernel::Do_intersect_2 do_2;
typename CGAL::Kernel_traits<O1>::Kernel::Intersect_2 i_2;
assert(!do_2(o1, o2));
assert(i_2(o1, o2).empty());
assert(!do_2(o2, o1));
assert(i_2(o2, o1).empty());
}
template < typename Res, typename O1, typename O2 >
@ -304,6 +312,19 @@ struct Test {
check_intersection (Rec(p( 10, 12), p(30, 40)), Rec(p( 25, 40), p( 26, 103)), Rec(P(25, 40), P(26, 40)));
}
void T_Rec()
{
std::cout << "Triangle Iso_rectangle\n";
check_no_intersection (Rec(p( 10, 12), p(30, 40)), T(p( 4, 0), p( 12, 4), p(-4, 8)));
check_intersection<T>(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p( -1, 2), p(2, 2)));
check_intersection<T>(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p(2, 2), p( -1, 2)));
check_intersection<Pol>(Rec(p( 0, 0), p(1, 1)), T(p( -1, -2), p( -1, 2), p(5, 2)));
check_intersection<T>(Rec(p( 0, 0), p(2, 2)), T(p( 0, 0), p( 1, 0), p(0, 1)));
check_intersection<T>(Rec(p( 0, 0), p(3, 3)), T(p( 1, 1), p( 2, 1), p(1, 2)));
check_intersection<P>(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p( 0, 0), p(0, -1)));
check_intersection<P>(Rec(p( 0, 0), p(1, 1)), T(p( 0, 0), p( -1, 0), p(0, -1)));
}
void run()
{
std::cout << "2D Intersection tests\n";
@ -322,6 +343,7 @@ struct Test {
R_Rec();
S_Rec();
Rec_Rec();
T_Rec();
}
};