diff --git a/Intersections_2/include/CGAL/Triangle_2_Iso_rectangle_2_intersection.h b/Intersections_2/include/CGAL/Triangle_2_Iso_rectangle_2_intersection.h index 4cc9b6a4ba0..4d7221fa329 100644 --- a/Intersections_2/include/CGAL/Triangle_2_Iso_rectangle_2_intersection.h +++ b/Intersections_2/include/CGAL/Triangle_2_Iso_rectangle_2_intersection.h @@ -30,14 +30,15 @@ #include #include -namespace CGAL{ - template +namespace CGAL{ namespace internal{ + + template Object - intersection(const Triangle_2 &t, const Iso_rectangle_2 &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 Segment; - typedef Point_2 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::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(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 + 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 + 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 + 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 + Object + inline intersection(const Iso_rectangle_2 &r, const Triangle_2 &t) + { + return typename K::Intersect_2()(r,t); + } + + template + Object + inline intersection(const Triangle_2 &t, const Iso_rectangle_2 &r) + { + return typename K::Intersect_2()(t,r); + } + + template + inline bool + do_intersect(const Iso_rectangle_2 & iso, + const Triangle_2 &tr) + { + typedef typename K::Do_intersect_2 Do_intersect; + return Do_intersect()(iso, tr); + } + + template + inline bool + do_intersect(const Triangle_2 &tr, const Iso_rectangle_2 &iso) + { + typedef typename K::Do_intersect_2 Do_intersect; + return Do_intersect()(tr, iso); + } + }//end namespace #endif diff --git a/Intersections_2/include/CGAL/intersection_2_2.h b/Intersections_2/include/CGAL/intersection_2_2.h index 470c966d748..d5b900b7b5b 100644 --- a/Intersections_2/include/CGAL/intersection_2_2.h +++ b/Intersections_2/include/CGAL/intersection_2_2.h @@ -36,5 +36,6 @@ #include #include #include +#include #endif diff --git a/Intersections_2/test/Intersections_2/test_intersections_2.cpp b/Intersections_2/test/Intersections_2/test_intersections_2.cpp index 606524f35b0..185da603d2e 100644 --- a/Intersections_2/test/Intersections_2/test_intersections_2.cpp +++ b/Intersections_2/test/Intersections_2/test_intersections_2.cpp @@ -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::Kernel::Do_intersect_2 do_2; + typename CGAL::Kernel_traits::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(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p( -1, 2), p(2, 2))); + check_intersection(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p(2, 2), p( -1, 2))); + check_intersection(Rec(p( 0, 0), p(1, 1)), T(p( -1, -2), p( -1, 2), p(5, 2))); + check_intersection(Rec(p( 0, 0), p(2, 2)), T(p( 0, 0), p( 1, 0), p(0, 1))); + check_intersection(Rec(p( 0, 0), p(3, 3)), T(p( 1, 1), p( 2, 1), p(1, 2))); + check_intersection

(Rec(p( 0, 0), p(1, 1)), T(p( -1, 0), p( 0, 0), p(0, -1))); + check_intersection

(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(); } };