From cdfcce94729d5e02f71b1c47e19320b25b30c4af Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Fri, 24 Jan 2014 00:25:20 +0100 Subject: [PATCH 01/27] 24-01-2014 Appropriate interface for new Kd_tree created --- .../include/CGAL/Snap_rounding_kd_2.h | 150 ++++++++++++++++-- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 10008562f5e..bd00e794d25 100644 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -31,29 +31,149 @@ #include +//waqar +#include +#include + + +//define search trait class here. +// +// +//in namespace internal. + + + namespace CGAL { + +////////////////////// +////////////////////// +//My_point +////////////////////// + template class My_point : public Traits::Point_2 { private: - typedef typename Traits::Point_2 Point_2; - typedef typename Traits::FT NT; - + typedef typename Traits::Point_2 Point_2; + typedef typename Traits::FT NT; + public: + typedef typename Traits::Point_2 Point_d; + typedef typename Traits::FT FT; + + typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; + typedef typename Traits::Circle_2 Circle_2; + typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_2; + typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; + + typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename Traits::Construct_center_2 Construct_center_2; + typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + + typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; + + Point_2 orig; SAVED_OBJECT object; - My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : - Point_2(p), orig(inp_orig), object(obj) {} + My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} My_point(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} My_point() : Point_2(),orig() {} My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} }; +////////////////////// +////////////////////// +//Kd_tree_interface_new_2d +////////////////////// + +template +class Kdtree_new_interface_2d +{ +public: + typedef PT Point; + typedef typename PT::Point_d Point_2; + typedef typename PT::FT FT; + + typedef typename PT::Iso_rectangle_2 Iso_rectangle_2; + typedef typename PT::Circle_2 Circle_2; + typedef typename PT::Cartesian_const_iterator_2 Cartesian_const_iterator_2; + typedef typename PT::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; + + typedef typename PT::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename PT::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename PT::Construct_center_2 Construct_center_2; + typedef typename PT::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + + typedef typename PT::Compute_squared_radius_2 Compute_squared_radius_2; + + static int dimension( const PT & pnt ) + { + return pnt.dimension(); // return pnt.dimensions(); + } + + static int compare( int d, const PT & a, const PT & b ) + { + if ( a[ d ] < b[ d ] ) + return -1; + if ( a[ d ] > b[ d ] ) + return 1; + + return 0; + } + static void copy_coord( int d, PT & a, const PT & b ) + { + if ( d == 0 ) + a = PT( b[ 0 ], a[1] ); + else + if ( d == 1 ) + a = PT( a[ 0 ], b[1] ); + else { + CGAL_error(); + } + } +}; + + + +// ////////////////////// +// ////////////////////// +// //Search_traits_new_2 +// ////////////////////// + +// template +// class Search_traits_new_2 { + +// public: +// typedef typename K::Point_2 Point_d; +// typedef typename K::Iso_rectangle_2 Iso_box_d; +// typedef typename K::Circle_2 Sphere_d; +// typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; +// typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; + +// typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; +// typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; +// typedef typename K::Construct_center_2 Construct_center_d; +// typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; + +// typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; +// typedef typename K::FT FT; + +// Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { +// return Construct_cartesian_const_iterator_d(); +// } +// }; + +////////////////////// +////////////////////// +//Multiple Kd_trees +////////////////////// + template class Multiple_kd_tree { CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); private: - typedef Traits_ Traits; + typedef Traits_ Traits; //Snap_rounding_traits< > > typedef typename Traits::FT NT; typedef typename Traits::Segment_2 Segment_2; typedef typename Traits::Point_2 Point_2; @@ -62,10 +182,14 @@ private: typedef typename Traits::Direction_2 Direction_2; typedef typename Traits::Line_2 Line_2; typedef typename Traits::Aff_transformation_2 Transformation_2; - typedef My_point My_point_saved; - typedef CGAL::Kdtree_interface_2d Kd_interface; + + //CREATION OF TREE + typedef My_point My_point_saved; //Traits= snap_rounding_traits, SAVED_OBJECT= HOT_PIXEL * pointer. + //typedef CGAL::Kdtree_interface_2d Kd_interfacea; + typedef Kdtree_new_interface_2d Kd_interface; typedef CGAL::Kdtree_d Kd_tree; typedef typename Kd_tree::Box Box; + typedef std::list Points_List; typedef std::pair Direction_nt_pair; typedef std::pair Kd_triple; @@ -88,6 +212,10 @@ private: typedef std::list Direction_list; typedef typename Direction_list::const_iterator Direction_const_iter; + //WAQAR:: CREATION OF NEW KD_TREE + typedef CGAL::Search_traits_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree_new; + private: Traits m_gt; const double pi, half_pi; @@ -155,10 +283,12 @@ private: inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) - {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), min BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),min BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} + {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + min BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),min BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) - {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), max BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),max BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} + {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + max BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),max BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} /*! */ Direction_2 get_direction(Segment_2 seg) From 3744c7b3231a2408025bdc438880b4bbc62bccd0 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Mon, 27 Jan 2014 01:05:41 +0100 Subject: [PATCH 02/27] 27-01-2014 new search traits now work with New Kd_trees. --- .../include/CGAL/Snap_rounding_kd_2.h | 75 ++++++++++++++----- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index bd00e794d25..610ac7dbf0f 100644 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -22,10 +22,10 @@ #include #include -#include +//#include #include #include -#include +//#include #include #include @@ -34,6 +34,8 @@ //waqar #include #include +#include + //define search trait class here. @@ -164,6 +166,8 @@ public: // } // }; + + ////////////////////// ////////////////////// //Multiple Kd_trees @@ -185,10 +189,13 @@ private: //CREATION OF TREE typedef My_point My_point_saved; //Traits= snap_rounding_traits, SAVED_OBJECT= HOT_PIXEL * pointer. - //typedef CGAL::Kdtree_interface_2d Kd_interfacea; typedef Kdtree_new_interface_2d Kd_interface; - typedef CGAL::Kdtree_d Kd_tree; - typedef typename Kd_tree::Box Box; + //typedef CGAL::Kdtree_d Kd_tree; + //typedef typename Kd_tree::Box Box; + typedef CGAL::Search_traits_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree; + typedef CGAL::Fuzzy_iso_box Box; + typedef std::list Points_List; typedef std::pair Direction_nt_pair; @@ -213,8 +220,12 @@ private: typedef typename Direction_list::const_iterator Direction_const_iter; //WAQAR:: CREATION OF NEW KD_TREE - typedef CGAL::Search_traits_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree_new; + // typedef CGAL::Search_traits_2 Search_traits; + // typedef CGAL::Kd_tree Kd_tree_new; + + // typedef std::pair Kd_triple_new; + // typedef std::pair Kd_direction_nt_pair_new; + // typedef std::list Kd_triple_list_new; private: Traits m_gt; @@ -241,25 +252,41 @@ private: /*! */ + //WAQAR: CREATE_KD_TREE IS FINISHED FOR NEW KD_TREE. Kd_triple create_kd_tree(NT angle) { Points_List l; Kd_tree *tree = new Kd_tree(2); + //WAQAR INITIALIZATION OF THE NEW KD_TREE + //Kd_tree_new *new_tree = new Kd_tree_new(); + for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) { Point_2 p(iter->first); rotate(p,angle); My_point_saved rotated_point(p,iter->first,iter->second); - l.push_back(rotated_point); + //WAQAR: Comenting this as this is only needed by the old kd_ttree + //l.push_back(rotated_point); + + //WAQAR: INSERT POINTS INTO NEW KD_TREE + tree->insert(rotated_point); } - tree->build(l); + //WAQAR: new kd_tree doesn not have this constructor. + //tree->build(l); + tree->build(); + + //WAQAR: BUILD THE NEW KD_TREE WITH THE ROTATED POINTS. + //new_tree->build(); //checking validity - if (!tree->is_valid()) tree->dump(); - CGAL_assertion(tree->is_valid()); + //WAQAR: Commenting these two lines as they are not compatible with new kd_trees i.e. is_valid() and dump(). + //if (!tree->is_valid()) tree->dump(); + //CGAL_assertion(tree->is_valid()); + + //WAQAR: NO ALTERNATIVE FOR CHECKING IF TREE IS VALID IN THE NEW KD_TREE. SHOULD I ADD ONE? typename Traits::To_double to_dbl; double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); @@ -273,6 +300,8 @@ private: Direction_nt_pair kp(d, angle); Kd_triple kt(tree,kp); + //WAQAR: + //Kd_triple_new kt_new(new_tree, kp); return(kt); } @@ -461,6 +490,9 @@ public: number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) { Kd_triple kd; + //WAQAR: + //Kd_triple_new kd_new; + // check that there are at least two trees CGAL_precondition_msg(number_of_trees >= 1, "There must be at least one kd-tree" ); @@ -621,17 +653,20 @@ public: My_point_saved point1(p1); My_point_saved point2(p2); - Box b(point1, point2, 2); + //Box b(point1, point2, 2); + //WAQAR: We use new Fuzzy box for new kd_trees. + //Box b(point1, point2); + //CGAL::My_class obj(2); - // the kd-tree query - My_point_saved_list res; - iter->first->search(std::back_inserter(res), b); + // // the kd-tree query + // My_point_saved_list res; + // iter->first->search(std::back_inserter(res), b); - // create result - result_list.empty(); - for (My_point_saved_iter my_point_iter = res.begin(); - my_point_iter != res.end(); ++my_point_iter) - result_list.push_back(my_point_iter->object); + // // create result + // result_list.empty(); + // for (My_point_saved_iter my_point_iter = res.begin(); + // my_point_iter != res.end(); ++my_point_iter) + // result_list.push_back(my_point_iter->object); } }; From f57279f804bf3a840ab2779a0851f80b37a8435d Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Mon, 27 Jan 2014 23:56:04 +0100 Subject: [PATCH 03/27] 27-Jan-2014 Progress made on kd_tree migration --- .../include/CGAL/Snap_rounding_kd_2.h | 263 ++++--- .../include/CGAL/Snap_rounding_kd_new_2.h | 686 ++++++++++++++++++ 2 files changed, 834 insertions(+), 115 deletions(-) create mode 100644 Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 610ac7dbf0f..833f52bbc59 100644 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -22,10 +22,10 @@ #include #include -//#include +#include #include #include -//#include +#include #include #include @@ -33,21 +33,12 @@ //waqar #include +//#include "../../../Spatial_searching/include/CGAL/Kd_tree.h" #include #include - - -//define search trait class here. -// -// -//in namespace internal. - - - namespace CGAL { - ////////////////////// ////////////////////// //My_point @@ -75,12 +66,16 @@ public: typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; - + //waqar: the data is stored in below mentioned variables. check if the data is parsed correctly by the kd_tree Point_2 orig; SAVED_OBJECT object; + My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} + My_point(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} + My_point() : Point_2(),orig() {} + My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} }; @@ -136,48 +131,11 @@ public: } }; - - -// ////////////////////// -// ////////////////////// -// //Search_traits_new_2 -// ////////////////////// - -// template -// class Search_traits_new_2 { - -// public: -// typedef typename K::Point_2 Point_d; -// typedef typename K::Iso_rectangle_2 Iso_box_d; -// typedef typename K::Circle_2 Sphere_d; -// typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; -// typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; - -// typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; -// typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; -// typedef typename K::Construct_center_2 Construct_center_d; -// typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; - -// typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; -// typedef typename K::FT FT; - -// Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { -// return Construct_cartesian_const_iterator_d(); -// } -// }; - - - -////////////////////// -////////////////////// -//Multiple Kd_trees -////////////////////// - template class Multiple_kd_tree { CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); private: - typedef Traits_ Traits; //Snap_rounding_traits< > > + typedef Traits_ Traits; typedef typename Traits::FT NT; typedef typename Traits::Segment_2 Segment_2; typedef typename Traits::Point_2 Point_2; @@ -187,15 +145,10 @@ private: typedef typename Traits::Line_2 Line_2; typedef typename Traits::Aff_transformation_2 Transformation_2; - //CREATION OF TREE - typedef My_point My_point_saved; //Traits= snap_rounding_traits, SAVED_OBJECT= HOT_PIXEL * pointer. + typedef My_point My_point_saved; typedef Kdtree_new_interface_2d Kd_interface; - //typedef CGAL::Kdtree_d Kd_tree; - //typedef typename Kd_tree::Box Box; - typedef CGAL::Search_traits_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree; - typedef CGAL::Fuzzy_iso_box Box; - + typedef CGAL::Kdtree_d Kd_tree; + typedef typename Kd_tree::Box Box; typedef std::list Points_List; typedef std::pair Direction_nt_pair; @@ -219,19 +172,24 @@ private: typedef std::list Direction_list; typedef typename Direction_list::const_iterator Direction_const_iter; - //WAQAR:: CREATION OF NEW KD_TREE - // typedef CGAL::Search_traits_2 Search_traits; - // typedef CGAL::Kd_tree Kd_tree_new; + //WAQAR:: CREATION OF NEW KD_TREE + typedef CGAL::Search_traits_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree_new; + typedef CGAL::Fuzzy_iso_box Box_new; - // typedef std::pair Kd_triple_new; - // typedef std::pair Kd_direction_nt_pair_new; - // typedef std::list Kd_triple_list_new; + typedef std::pair Kd_triple_new; + typedef std::pair Kd_direction_nt_pair_new; + typedef std::list Kd_triple_list_new; private: Traits m_gt; const double pi, half_pi; int number_of_trees; - Kd_triple_list kd_trees_list; + + Kd_triple_list kd_trees_list; + //WAQAR + Kd_triple_list_new kd_trees_new_list; + Point_saved_pair_list input_points_list; std::map angle_to_sines_appr; // was const int @@ -252,14 +210,14 @@ private: /*! */ - //WAQAR: CREATE_KD_TREE IS FINISHED FOR NEW KD_TREE. - Kd_triple create_kd_tree(NT angle) + Kd_triple create_kd_tree(NT angle, Kd_triple_new &kd_new) { Points_List l; + Kd_tree *tree = new Kd_tree(2); + //waqar + Kd_tree_new *tree_new = new Kd_tree_new(); - //WAQAR INITIALIZATION OF THE NEW KD_TREE - //Kd_tree_new *new_tree = new Kd_tree_new(); for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) @@ -267,41 +225,39 @@ private: Point_2 p(iter->first); rotate(p,angle); My_point_saved rotated_point(p,iter->first,iter->second); - //WAQAR: Comenting this as this is only needed by the old kd_ttree - //l.push_back(rotated_point); - - //WAQAR: INSERT POINTS INTO NEW KD_TREE - tree->insert(rotated_point); + + l.push_back(rotated_point); + //waqar + tree_new->insert(rotated_point); } - //WAQAR: new kd_tree doesn not have this constructor. - //tree->build(l); - tree->build(); - - //WAQAR: BUILD THE NEW KD_TREE WITH THE ROTATED POINTS. - //new_tree->build(); + tree->build(l); + //waqar + tree_new->build(); //checking validity - //WAQAR: Commenting these two lines as they are not compatible with new kd_trees i.e. is_valid() and dump(). - //if (!tree->is_valid()) tree->dump(); - //CGAL_assertion(tree->is_valid()); - - //WAQAR: NO ALTERNATIVE FOR CHECKING IF TREE IS VALID IN THE NEW KD_TREE. SHOULD I ADD ONE? + if (!tree->is_valid()) tree->dump(); + CGAL_assertion(tree->is_valid()); typename Traits::To_double to_dbl; double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); - if (buffer_angle < 0) buffer_angle = 0; + if (buffer_angle < 0) + buffer_angle = 0; + Line_2 li(std::tan(buffer_angle), -1, 0); Direction_2 d(li); + // rotate_by 180 degrees Transformation_2 t(ROTATION, 0, -1); d = d.transform(t); Direction_nt_pair kp(d, angle); + Kd_triple kt(tree,kp); + //waqar + Kd_triple_new kt_new(tree_new, kp); + kd_new = kt_new; - //WAQAR: - //Kd_triple_new kt_new(new_tree, kp); return(kt); } @@ -490,9 +446,9 @@ public: number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) { Kd_triple kd; - //WAQAR: - //Kd_triple_new kd_new; + //waqar + Kd_triple_new kd_new; // check that there are at least two trees CGAL_precondition_msg(number_of_trees >= 1, "There must be at least one kd-tree" ); @@ -515,9 +471,13 @@ public: angle += half_pi / number_of_trees,++i) { buffer_angle = angle - half_pi / (2 * number_of_trees); - if (buffer_angle < 0) buffer_angle = 0; + + if (buffer_angle < 0) + buffer_angle = 0; + li = Line_2(std::tan(buffer_angle), -1, 0); d = Direction_2(li); + // rotate_by 180 degrees Transformation_2 t(ROTATION, 0, -1); d = d.transform(t); @@ -538,9 +498,12 @@ public: if (kd_counter[ind] >= (double)number_of_segments / (double)number_of_trees / 2.0) { - kd = create_kd_tree(angle); + kd = create_kd_tree(angle, kd_new); kd_trees_list.push_back(kd); + //waqar new kd + kd_trees_new_list.push_back(kd_new); + #ifdef CGAL_SR_DEBUG ++number_of_actual_kd_trees; #endif @@ -550,6 +513,13 @@ public: ++ind; } + //debugging + std::cout<< "*&^*&^*&^ Numer of old Kd_trees: " << kd_trees_list.size() << " , Number of new Kd_trees: " << kd_trees_new_list.size() << std::endl; + // typename Kd_triple_list_new::const_iterator iter_new ; + // for(iter_new = kd_trees_new_list.begin(); iter_new!=kd_trees_new_list.end(); ++iter_new) + // iter_new->first->statistics(std::cout); + //debugging end + delete[] kd_counter; #ifdef CGAL_SR_DEBUG @@ -565,6 +535,12 @@ public: delete (it->first); } + //waqar new kd + for(typename Kd_triple_list_new::iterator it = kd_trees_new_list.begin(); + it != kd_trees_new_list.end(); ++it) { + delete (it->first); + } + for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); it != input_points_list.end(); ++it) { delete (it->second); @@ -606,6 +582,8 @@ public: { // determine right kd-tree to work on, depending on the segment's slope Direction_2 d = get_direction(s); + + //old kd_tree block starts int i = 0; int n = kd_trees_list.size(); bool found = false; @@ -620,15 +598,59 @@ public: if (!found) iter = kd_trees_list.begin(); else --iter; + //////////// + ///code for new kd_tree i.e. block above + ///////////// + + int i_new = 0; + int n_new = kd_trees_new_list.size(); + bool found_new = false; + typename Kd_triple_list_new::const_iterator iter_new = kd_trees_new_list.begin(); + + while(i_new < n_new && !found_new) + { + + if (iter_new->second.first > d) + found_new = true; + + ++i_new; + ++iter_new; + } + + if (!found_new) + { + iter_new = kd_trees_new_list.begin(); + //std::cout << "found_new is false. kd_tree: 0 activated " << std::endl; + } + else + { + //std::cout << "Found kd_tree: " << --i_new << " activated" << std::endl; + --iter_new; + } + + //std::cout << "Debug:: i_new: " << i_new << std::endl; + + /////// + /// new block ends + //////// + + + + + + Point_list points_list; m_gt.minkowski_sum_with_pixel_2_object()(points_list, s, unit_square); Point_iter points_iter; - for (points_iter = points_list.begin(); points_iter != points_list.end(); - ++points_iter) - rotate(*points_iter, iter->second.second); - + for (points_iter = points_list.begin(); points_iter != points_list.end(); ++points_iter) + { + //the point has to be rotated only once so I am using the new kd_tree as reference. does not change the result. + //rotate(*points_iter, iter->second.second); + //waqar new kd + rotate(*points_iter, iter_new->second.second); + } // query points_iter = points_list.begin(); Point_2 point_left,point_right,point_bot,point_top; @@ -640,12 +662,11 @@ public: point_top = big_y_point(point_top,*points_iter); } - typedef typename Traits::Construct_iso_rectangle_2 - Construct_iso_rectangle_2; - Construct_iso_rectangle_2 construct_rec = - m_gt.construct_iso_rectangle_2_object(); - Iso_rectangle_2 rec = - construct_rec(point_left, point_right, point_bot, point_top); + typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + + Construct_iso_rectangle_2 construct_rec = m_gt.construct_iso_rectangle_2_object(); + + Iso_rectangle_2 rec = construct_rec(point_left, point_right, point_bot, point_top); Point_2 p1 = rec.vertex(0); Point_2 p2 = rec.vertex(2); @@ -653,20 +674,32 @@ public: My_point_saved point1(p1); My_point_saved point2(p2); - //Box b(point1, point2, 2); - //WAQAR: We use new Fuzzy box for new kd_trees. - //Box b(point1, point2); - //CGAL::My_class obj(2); + Box b(point1, point2, 2); + //waqar kd new + Box_new b_new(point1, point2); - // // the kd-tree query - // My_point_saved_list res; - // iter->first->search(std::back_inserter(res), b); + // the kd-tree query + My_point_saved_list res; + //waqar kd new + My_point_saved_list res_new; + + iter->first->search(std::back_inserter(res), b); + //waqar kd new + //i think the res_new might be different then old res (yes it is confirmed) either the box is faulty or the back inserter + iter_new->first->search(std::back_inserter(res_new), b_new); - // // create result - // result_list.empty(); - // for (My_point_saved_iter my_point_iter = res.begin(); - // my_point_iter != res.end(); ++my_point_iter) - // result_list.push_back(my_point_iter->object); + std::cout<< "******* Size of res: " << res.size()<< std::endl; + for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) + std::cout<< my_point_iter->orig << std::endl; + + std::cout<< "******* Size of res_new: " << res_new.size()<< std::endl; + for (My_point_saved_iter my_point_iter = res_new.begin(); my_point_iter != res_new.end(); ++my_point_iter) + std::cout<< my_point_iter->orig << std::endl<< std::endl; + + // create result + result_list.empty(); + for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) + result_list.push_back(my_point_iter->object); } }; diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h new file mode 100644 index 00000000000..2f21f693adb --- /dev/null +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h @@ -0,0 +1,686 @@ +// Copyright (c) 2001 Tel-Aviv University (Israel). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// 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 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// +// +// author(s) : Eli Packer +#ifndef CGAL_SNAP_ROUNDING_KD_2_H +#define CGAL_SNAP_ROUNDING_KD_2_H + +#include +#include +//#include +#include +#include +//#include +#include +#include + +#include + +//waqar +#include +#include +#include + + + + +//define search trait class here. +// +// +//in namespace internal. + + + +namespace CGAL { + + +////////////////////// +////////////////////// +//My_point +////////////////////// + +template +class My_point : public Traits::Point_2 { +private: + typedef typename Traits::Point_2 Point_2; + typedef typename Traits::FT NT; + +public: + typedef typename Traits::Point_2 Point_d; + typedef typename Traits::FT FT; + + typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; + typedef typename Traits::Circle_2 Circle_2; + typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_2; + typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; + + typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename Traits::Construct_center_2 Construct_center_2; + typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + + typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; + + //waqar: the data is stored in below mentioned variables. check if the data is parsed correctly by the kd_tree + Point_2 orig; + SAVED_OBJECT object; + + My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} + + My_point(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} + + My_point() : Point_2(),orig() {} + + My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} +}; + +////////////////////// +////////////////////// +//Kd_tree_interface_new_2d +////////////////////// + +template +class Kdtree_new_interface_2d +{ +public: + typedef PT Point; + typedef typename PT::Point_d Point_2; + typedef typename PT::FT FT; + + typedef typename PT::Iso_rectangle_2 Iso_rectangle_2; + typedef typename PT::Circle_2 Circle_2; + typedef typename PT::Cartesian_const_iterator_2 Cartesian_const_iterator_2; + typedef typename PT::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; + + typedef typename PT::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename PT::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename PT::Construct_center_2 Construct_center_2; + typedef typename PT::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + + typedef typename PT::Compute_squared_radius_2 Compute_squared_radius_2; + + static int dimension( const PT & pnt ) + { + return pnt.dimension(); // return pnt.dimensions(); + } + + static int compare( int d, const PT & a, const PT & b ) + { + if ( a[ d ] < b[ d ] ) + return -1; + if ( a[ d ] > b[ d ] ) + return 1; + + return 0; + } + static void copy_coord( int d, PT & a, const PT & b ) + { + if ( d == 0 ) + a = PT( b[ 0 ], a[1] ); + else + if ( d == 1 ) + a = PT( a[ 0 ], b[1] ); + else { + CGAL_error(); + } + } +}; + + + +// ////////////////////// +// ////////////////////// +// //Search_traits_new_2 +// ////////////////////// + +// template +// class Search_traits_new_2 { + +// public: +// typedef typename K::Point_2 Point_d; +// typedef typename K::Iso_rectangle_2 Iso_box_d; +// typedef typename K::Circle_2 Sphere_d; +// typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; +// typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; + +// typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; +// typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; +// typedef typename K::Construct_center_2 Construct_center_d; +// typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; + +// typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; +// typedef typename K::FT FT; + +// Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { +// return Construct_cartesian_const_iterator_d(); +// } +// }; + + + +////////////////////// +////////////////////// +//Multiple Kd_trees +////////////////////// + +template +class Multiple_kd_tree { + CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); +private: + typedef Traits_ Traits; //Snap_rounding_traits< > > + typedef typename Traits::FT NT; + typedef typename Traits::Segment_2 Segment_2; + typedef typename Traits::Point_2 Point_2; + typedef typename Traits::Vector_2 Vector_2; + typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; + typedef typename Traits::Direction_2 Direction_2; + typedef typename Traits::Line_2 Line_2; + typedef typename Traits::Aff_transformation_2 Transformation_2; + + //CREATION OF TREE + typedef My_point My_point_saved; //Traits= snap_rounding_traits, SAVED_OBJECT= HOT_PIXEL * pointer. + typedef Kdtree_new_interface_2d Kd_interface; + //typedef CGAL::Kdtree_d Kd_tree; + //typedef typename Kd_tree::Box Box; + typedef CGAL::Search_traits_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree; + typedef CGAL::Fuzzy_iso_box Box; + + + typedef std::list Points_List; + typedef std::pair Direction_nt_pair; + typedef std::pair Kd_triple; + typedef std::pair Kd_direction_nt_pair; + typedef std::list Kd_triple_list; + + typedef std::pair Point_saved_pair; + typedef std::list Point_saved_pair_list; + typedef typename Point_saved_pair_list::iterator Point_saved_pair_iter; + + typedef typename std::list My_point_saved_list; + typedef typename My_point_saved_list::iterator My_point_saved_iter; + + typedef std::list Point_list; + typedef typename Point_list::iterator Point_iter; + + typedef std::list Segment_list; + typedef typename Segment_list::const_iterator Segment_const_iter; + + typedef std::list Direction_list; + typedef typename Direction_list::const_iterator Direction_const_iter; + + //WAQAR:: CREATION OF NEW KD_TREE + // typedef CGAL::Search_traits_2 Search_traits; + // typedef CGAL::Kd_tree Kd_tree_new; + + // typedef std::pair Kd_triple_new; + // typedef std::pair Kd_direction_nt_pair_new; + // typedef std::list Kd_triple_list_new; + +private: + Traits m_gt; + const double pi, half_pi; + int number_of_trees; + Kd_triple_list kd_trees_list; + Point_saved_pair_list input_points_list; + std::map angle_to_sines_appr; // was const int + + /*! */ + void rotate(Point_2& p, NT angle) + { + static const double rad_to_deg = 57.297; + + typename Traits::To_double to_dbl; + int tranc_angle = int(to_dbl(angle) * rad_to_deg); + + NT cosine_val = angle_to_sines_appr[90 - tranc_angle], + sine_val = angle_to_sines_appr[tranc_angle]; + + Transformation_2 rotate(ROTATION, sine_val, cosine_val); + p = rotate(p); + } + + + /*! */ + //WAQAR: CREATE_KD_TREE IS FINISHED FOR NEW KD_TREE. + Kd_triple create_kd_tree(NT angle) + { + //Points_List l; + // Kd_tree *tree = new Kd_tree(2); + + //WAQAR INITIALIZATION OF THE NEW KD_TREE + Kd_tree *tree = new Kd_tree(); + + for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) + { + Point_2 p(iter->first); + rotate(p,angle); + My_point_saved rotated_point(p,iter->first,iter->second); + //WAQAR: Comenting this as this is only needed by the old kd_ttree + //l.push_back(rotated_point); + + //WAQAR: INSERT POINTS INTO NEW KD_TREE + tree->insert(rotated_point); + } + + //WAQAR: new kd_tree doesn not have this constructor. + //tree->build(l); + tree->build(); + //tree->statistics(std::cout); + //WAQAR: BUILD THE NEW KD_TREE WITH THE ROTATED POINTS. + //new_tree->build(); + + //checking validity + //WAQAR: Commenting these two lines as they are not compatible with new kd_trees i.e. is_valid() and dump(). + //if (!tree->is_valid()) tree->dump(); + //CGAL_assertion(tree->is_valid()); + + //WAQAR: NO ALTERNATIVE FOR CHECKING IF TREE IS VALID IN THE NEW KD_TREE. SHOULD I ADD ONE? + + typename Traits::To_double to_dbl; + double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); + + if (buffer_angle < 0) buffer_angle = 0; + Line_2 li(std::tan(buffer_angle), -1, 0); + Direction_2 d(li); + // rotate_by 180 degrees + Transformation_2 t(ROTATION, 0, -1); + d = d.transform(t); + Direction_nt_pair kp(d, angle); + Kd_triple kt(tree,kp); + + //WAQAR: + //Kd_triple_new kt_new(new_tree, kp); + return(kt); + } + + inline NT square(NT x) {return(x * x);} + + inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x, NT y) {return((x < y) ? x : y);} + inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x, NT y) {return((x < y) ? y : x);} + + inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, + NT x6) + {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + min BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),min BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} + + inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) + {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + max BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),max BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} + + /*! */ + Direction_2 get_direction(Segment_2 seg) + { + typedef typename Traits_::Construct_vertex_2 Construct_vertex_2; + Construct_vertex_2 construct_vertex = m_gt.construct_vertex_2_object(); + + // force the segment slope to [0-180) + Point_2 s = construct_vertex(seg, 0); + Point_2 t = construct_vertex(seg, 1); + Comparison_result cx = m_gt.compare_x_2_object()(s, t); + Comparison_result cy = m_gt.compare_y_2_object()(s, t); + if (cy == LARGER || (cy == EQUAL && cx == LARGER)) { + typedef typename Traits::Construct_segment_2 Construct_segment_2; + Construct_segment_2 construct_seg = m_gt.construct_segment_2_object(); + seg = construct_seg(t, s); + } + + // force the vector to [0-90) + Vector_2 v(construct_vertex(seg, 0), construct_vertex(seg, 1)); + if (cx == EQUAL || (cx == LARGER && cy == SMALLER) || + (cx == SMALLER && cy == LARGER)) + v = v.perpendicular(RIGHT_TURN); + + Direction_2 d(v.direction()); + + return(d); + } + + /*! */ + int get_kd_num(Segment_2 seg, int n, Direction_list & directions) + { + Direction_2 d = get_direction(seg); + int i = 0; + bool found = false; + Direction_const_iter iter = directions.begin(); + + while (i < n && !found) { + if (*iter > d) found = true; + ++i; + ++iter; + } + + if (found) --i; + else i = 0; + + return(i); + } + + /*! */ + void check_kd(int *kd_counter,int number_of_trees, + const Segment_list & seg_list, + Direction_list & directions) + { + for (int i = 0;i < number_of_trees;++i) + kd_counter[i] = 0; + + int kd_num; + for (Segment_const_iter iter = seg_list.begin(); iter != seg_list.end(); + ++iter) + { + kd_num = get_kd_num(*iter, number_of_trees, directions); + kd_counter[kd_num]++; + } + } + + /*! */ + void init_angle_to_sines_table() + { + angle_to_sines_appr[0] = NT(0); + angle_to_sines_appr[1] = NT(115) / NT(6613); + angle_to_sines_appr[2] = NT(57) / NT(1625); + angle_to_sines_appr[3] = NT(39) / NT(761); + angle_to_sines_appr[4] = NT(29) / NT(421); + angle_to_sines_appr[5] = NT(23) / NT(265); + angle_to_sines_appr[6] = NT(19) / NT(181); + angle_to_sines_appr[7] = NT(32) / NT(257); + angle_to_sines_appr[8] = NT(129) / NT(929); + angle_to_sines_appr[9] = NT(100) / NT(629); + angle_to_sines_appr[10] = NT(92) / NT(533); + angle_to_sines_appr[11] = NT(93) / NT(485); + angle_to_sines_appr[12] = NT(76) / NT(365); + angle_to_sines_appr[13] = NT(156) / NT(685); + angle_to_sines_appr[14] = NT(205) / NT(853); + angle_to_sines_appr[15] = NT(69) / NT(269); + angle_to_sines_appr[16] = NT(7) / NT(25); + angle_to_sines_appr[17] = NT(120) / NT(409); + angle_to_sines_appr[18] = NT(57) / NT(185); + angle_to_sines_appr[19] = NT(12) / NT(37); + angle_to_sines_appr[20] = NT(51) / NT(149); + angle_to_sines_appr[21] = NT(135) / NT(377); + angle_to_sines_appr[22] = NT(372) / NT(997); + angle_to_sines_appr[23] = NT(348) / NT(877); + angle_to_sines_appr[24] = NT(231) / NT(569); + angle_to_sines_appr[25] = NT(36) / NT(85); + angle_to_sines_appr[26] = NT(39) / NT(89); + angle_to_sines_appr[27] = NT(300) / NT(661); + angle_to_sines_appr[28] = NT(8) / NT(17); + angle_to_sines_appr[29] = NT(189) / NT(389); + angle_to_sines_appr[30] = NT(451) / NT(901); + angle_to_sines_appr[31] = NT(180) / NT(349); + angle_to_sines_appr[32] = NT(28) / NT(53); + angle_to_sines_appr[33] = NT(432) / NT(793); + angle_to_sines_appr[34] = NT(161) / NT(289); + angle_to_sines_appr[35] = NT(228) / NT(397); + angle_to_sines_appr[36] = NT(504) / NT(865); + angle_to_sines_appr[37] = NT(3) / NT(5); + angle_to_sines_appr[38] = NT(580) / NT(941); + angle_to_sines_appr[39] = NT(341) / NT(541); + angle_to_sines_appr[40] = NT(88) / NT(137); + angle_to_sines_appr[41] = NT(48) / NT(73); + angle_to_sines_appr[42] = NT(65) / NT(97); + angle_to_sines_appr[43] = NT(429) / NT(629); + angle_to_sines_appr[44] = NT(555) / NT(797); + angle_to_sines_appr[45] = NT(697) / NT(985); + angle_to_sines_appr[46] = NT(572) / NT(797); + angle_to_sines_appr[47] = NT(460) / NT(629); + angle_to_sines_appr[48] = NT(72) / NT(97); + angle_to_sines_appr[49] = NT(55) / NT(73); + angle_to_sines_appr[50] = NT(105) / NT(137); + angle_to_sines_appr[51] = NT(420) / NT(541); + angle_to_sines_appr[52] = NT(741) / NT(941); + angle_to_sines_appr[53] = NT(4) / NT(5); + angle_to_sines_appr[54] = NT(703) / NT(865); + angle_to_sines_appr[55] = NT(325) / NT(397); + angle_to_sines_appr[56] = NT(240) / NT(289); + angle_to_sines_appr[57] = NT(665) / NT(793); + angle_to_sines_appr[58] = NT(45) / NT(53); + angle_to_sines_appr[59] = NT(299) / NT(349); + angle_to_sines_appr[60] = NT(780) / NT(901); + angle_to_sines_appr[61] = NT(340) / NT(389); + angle_to_sines_appr[62] = NT(15) / NT(17); + angle_to_sines_appr[63] = NT(589) / NT(661); + angle_to_sines_appr[64] = NT(80) / NT(89); + angle_to_sines_appr[65] = NT(77) / NT(85); + angle_to_sines_appr[66] = NT(520) / NT(569); + angle_to_sines_appr[67] = NT(805) / NT(877); + angle_to_sines_appr[68] = NT(925) / NT(997); + angle_to_sines_appr[69] = NT(352) / NT(377); + angle_to_sines_appr[70] = NT(140) / NT(149); + angle_to_sines_appr[71] = NT(35) / NT(37); + angle_to_sines_appr[72] = NT(176) / NT(185); + angle_to_sines_appr[73] = NT(391) / NT(409); + angle_to_sines_appr[74] = NT(24) / NT(25); + angle_to_sines_appr[75] = NT(260) / NT(269); + angle_to_sines_appr[76] = NT(828) / NT(853); + angle_to_sines_appr[77] = NT(667) / NT(685); + angle_to_sines_appr[78] = NT(357) / NT(365); + angle_to_sines_appr[79] = NT(476) / NT(485); + angle_to_sines_appr[80] = NT(525) / NT(533); + angle_to_sines_appr[81] = NT(621) / NT(629); + angle_to_sines_appr[82] = NT(920) / NT(929); + angle_to_sines_appr[83] = NT(255) / NT(257); + angle_to_sines_appr[84] = NT(180) / NT(181); + angle_to_sines_appr[85] = NT(264) / NT(265); + angle_to_sines_appr[86] = NT(420) / NT(421); + angle_to_sines_appr[87] = NT(760) / NT(761); + angle_to_sines_appr[88] = NT(1624) / NT(1625); + angle_to_sines_appr[89] = NT(6612) / NT(6613); + angle_to_sines_appr[90] = NT(1); + } + +public: + + /*! */ + Multiple_kd_tree(const Point_saved_pair_list & inp_points_list, + int inp_number_of_trees, + const Segment_list & seg_list) : + pi(3.1415), half_pi(1.57075), + number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) + { + Kd_triple kd; + //WAQAR: + //Kd_triple_new kd_new; + + + // check that there are at least two trees + CGAL_precondition_msg(number_of_trees >= 1, "There must be at least one kd-tree" ); + + init_angle_to_sines_table(); + + // find the kd trees that have enough candidates (segments with a close + // slope) + int * kd_counter = new int[number_of_trees]; + int number_of_segments = seg_list.size(); + + // auxilary directions + Direction_list directions; + double buffer_angle; + Line_2 li; + Direction_2 d; + + int i = 0; + for (double angle = 0; i < number_of_trees; + angle += half_pi / number_of_trees,++i) + { + buffer_angle = angle - half_pi / (2 * number_of_trees); + if (buffer_angle < 0) buffer_angle = 0; + li = Line_2(std::tan(buffer_angle), -1, 0); + d = Direction_2(li); + // rotate_by 180 degrees + Transformation_2 t(ROTATION, 0, -1); + d = d.transform(t); + + directions.push_back(d); + } + + check_kd(kd_counter, number_of_trees, seg_list,directions); + int ind = 0; + +#ifdef CGAL_SR_DEBUG + int number_of_actual_kd_trees = 0; +#endif + i = 0; + for (NT angle = 0; i < number_of_trees; + angle += NT(half_pi / number_of_trees),++i) + { + if (kd_counter[ind] >= + (double)number_of_segments / (double)number_of_trees / 2.0) + { + kd = create_kd_tree(angle); + kd_trees_list.push_back(kd); + +#ifdef CGAL_SR_DEBUG + ++number_of_actual_kd_trees; +#endif + + } + + ++ind; + } + + delete[] kd_counter; + +#ifdef CGAL_SR_DEBUG + std::cout << "Actual number of kd-trees created : " << + number_of_actual_kd_trees << std::endl; +#endif + + } + + ~Multiple_kd_tree() { + for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); + it != kd_trees_list.end(); ++it) { + delete (it->first); + } + + for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); + it != input_points_list.end(); ++it) { + delete (it->second); + } + + } + + /*! */ + Point_2 small_x_point(const Point_2 & p1, const Point_2 & p2) + { + Comparison_result c = m_gt.compare_x_2_object()(p1, p2); + return (c == SMALLER) ? p1 : p2; + } + + /*! */ + Point_2 big_x_point(const Point_2 & p1, const Point_2 & p2) + { + Comparison_result c = m_gt.compare_x_2_object()(p1, p2); + return (c == SMALLER) ? p2 : p1; + } + + /*! */ + Point_2 small_y_point(const Point_2 & p1, const Point_2 & p2) + { + Comparison_result c = m_gt.compare_y_2_object()(p1, p2); + return (c == SMALLER) ? p1 : p2; + } + + /*! */ + Point_2 big_y_point(const Point_2 & p1, const Point_2 & p2) + { + Comparison_result c = m_gt.compare_y_2_object()(p1, p2); + return (c == SMALLER) ? p2 : p1; + } + + /*! */ + void get_intersecting_points(std::list & result_list, + Segment_2 s, NT unit_square) + { + // determine right kd-tree to work on, depending on the segment's slope + Direction_2 d = get_direction(s); + int i = 0; + int n = kd_trees_list.size(); + bool found = false; + typename Kd_triple_list::const_iterator iter = kd_trees_list.begin(); + + while(i < n && !found) { + if (iter->second.first > d) found = true; + ++i; + ++iter; + } + + if (!found) iter = kd_trees_list.begin(); + else --iter; + + Point_list points_list; + m_gt.minkowski_sum_with_pixel_2_object()(points_list, s, unit_square); + + Point_iter points_iter; + + for (points_iter = points_list.begin(); points_iter != points_list.end(); + ++points_iter) + rotate(*points_iter, iter->second.second); + + // query + points_iter = points_list.begin(); + Point_2 point_left,point_right,point_bot,point_top; + point_left = point_right = point_bot = point_top = *points_iter; + for (++points_iter; points_iter != points_list.end(); ++points_iter) { + point_left = small_x_point(point_left,*points_iter); + point_right = big_x_point(point_right,*points_iter); + point_bot = small_y_point(point_bot,*points_iter); + point_top = big_y_point(point_top,*points_iter); + } + + typedef typename Traits::Construct_iso_rectangle_2 + Construct_iso_rectangle_2; + Construct_iso_rectangle_2 construct_rec = + m_gt.construct_iso_rectangle_2_object(); + Iso_rectangle_2 rec = + construct_rec(point_left, point_right, point_bot, point_top); + + Point_2 p1 = rec.vertex(0); + Point_2 p2 = rec.vertex(2); + + My_point_saved point1(p1); + My_point_saved point2(p2); + + //Box b(point1, point2, 2); + //WAQAR: We use new Fuzzy box for new kd_trees. + Box b(point1, point2); + // CGAL::My_class obj(2); + // std::cout << "****(***: " << obj.a << std::endl; + + // the kd-tree query + My_point_saved_list res; + iter->first->search(std::back_inserter(res), b); + + std::cout<< "size of res: " << res.size() << std::endl; + //int count = 0; + // create result + result_list.empty(); + for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) + { + // std::cout<< "***: " << count << std::endl; + // count++; + result_list.push_back(my_point_iter->object); + std::cout<< "***: " << result_list.size() << std::endl; + } + } +}; + +} //namespace CGAL + +#endif From ef8e0890e5a19226582d8d46b6f9dd70d88adf04 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Wed, 29 Jan 2014 15:58:47 +0100 Subject: [PATCH 04/27] 29-Jan-2014 Just addd a few comments --- Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h | 0 Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h mode change 100644 => 100755 Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h old mode 100644 new mode 100755 diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h old mode 100644 new mode 100755 From 810adebb5bdb018ad226384586730be7a4eeaace Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Thu, 30 Jan 2014 00:58:06 +0100 Subject: [PATCH 05/27] 30-January-2014 Old kd_trees successfully replaced with new kd_trees. Thourough testing still pending. --- .../include/CGAL/Snap_rounding_kd_2.h | 211 +++++++----------- .../include/CGAL/Snap_rounding_traits_2.h | 39 ++-- 2 files changed, 98 insertions(+), 152 deletions(-) mode change 100644 => 100755 Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 833f52bbc59..7dbdf1cd8c5 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -31,9 +30,7 @@ #include -//waqar #include -//#include "../../../Spatial_searching/include/CGAL/Kd_tree.h" #include #include @@ -66,7 +63,6 @@ public: typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; - //waqar: the data is stored in below mentioned variables. check if the data is parsed correctly by the kd_tree Point_2 orig; SAVED_OBJECT object; @@ -79,13 +75,14 @@ public: My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} }; -////////////////////// -////////////////////// + +////////////////////////// +////////////////////////// //Kd_tree_interface_new_2d -////////////////////// +////////////////////////// template -class Kdtree_new_interface_2d +class My_point_interface_2d { public: typedef PT Point; @@ -131,6 +128,36 @@ public: } }; +////////////////////// +////////////////////// +//Search_traits_new_2 +// +//(Search traits modified to be used by the Spacial Searching kd_trees for Snap rounding) +////////////////////// + +template +class Search_traits_kd_tree_2 { + +public: + typedef typename K::Point Point_d; + typedef typename K::Iso_rectangle_2 Iso_box_d; + typedef typename K::Circle_2 Sphere_d; + typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; + typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; + + typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; + typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; + typedef typename K::Construct_center_2 Construct_center_d; + typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; + + typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; + typedef typename K::FT FT; + + Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { + return Construct_cartesian_const_iterator_d(); + } +}; + template class Multiple_kd_tree { CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); @@ -145,10 +172,11 @@ private: typedef typename Traits::Line_2 Line_2; typedef typename Traits::Aff_transformation_2 Transformation_2; - typedef My_point My_point_saved; - typedef Kdtree_new_interface_2d Kd_interface; - typedef CGAL::Kdtree_d Kd_tree; - typedef typename Kd_tree::Box Box; + typedef My_point My_point_saved; + typedef My_point_interface_2d My_point_interface; + typedef CGAL::Search_traits_kd_tree_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree; + typedef CGAL::Fuzzy_iso_box Box; typedef std::list Points_List; typedef std::pair Direction_nt_pair; @@ -172,23 +200,13 @@ private: typedef std::list Direction_list; typedef typename Direction_list::const_iterator Direction_const_iter; - //WAQAR:: CREATION OF NEW KD_TREE - typedef CGAL::Search_traits_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree_new; - typedef CGAL::Fuzzy_iso_box Box_new; - - typedef std::pair Kd_triple_new; - typedef std::pair Kd_direction_nt_pair_new; - typedef std::list Kd_triple_list_new; private: Traits m_gt; const double pi, half_pi; int number_of_trees; - - Kd_triple_list kd_trees_list; - //WAQAR - Kd_triple_list_new kd_trees_new_list; + + Kd_triple_list kd_trees_list; Point_saved_pair_list input_points_list; std::map angle_to_sines_appr; // was const int @@ -210,34 +228,27 @@ private: /*! */ - Kd_triple create_kd_tree(NT angle, Kd_triple_new &kd_new) + Kd_triple create_kd_tree(NT angle) { - Points_List l; - - Kd_tree *tree = new Kd_tree(2); - //waqar - Kd_tree_new *tree_new = new Kd_tree_new(); + Kd_tree *tree = new Kd_tree(); - for (Point_saved_pair_iter iter = input_points_list.begin(); - iter != input_points_list.end(); ++iter) + for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) { Point_2 p(iter->first); rotate(p,angle); My_point_saved rotated_point(p,iter->first,iter->second); - l.push_back(rotated_point); - //waqar - tree_new->insert(rotated_point); + tree->insert(rotated_point); } - tree->build(l); - //waqar - tree_new->build(); + tree->build(); + //Code used by the old kd_trees. + //TODO: check if it is required by the new kd_trees. //checking validity - if (!tree->is_valid()) tree->dump(); - CGAL_assertion(tree->is_valid()); + //if (!tree->is_valid()) tree->dump(); + //CGAL_assertion(tree->is_valid()); typename Traits::To_double to_dbl; double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); @@ -252,11 +263,8 @@ private: Transformation_2 t(ROTATION, 0, -1); d = d.transform(t); Direction_nt_pair kp(d, angle); - - Kd_triple kt(tree,kp); - //waqar - Kd_triple_new kt_new(tree_new, kp); - kd_new = kt_new; + + Kd_triple kt(tree, kp); return(kt); } @@ -445,11 +453,9 @@ public: pi(3.1415), half_pi(1.57075), number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) { + Kd_triple kd; - //waqar - Kd_triple_new kd_new; - // check that there are at least two trees CGAL_precondition_msg(number_of_trees >= 1, "There must be at least one kd-tree" ); @@ -498,12 +504,10 @@ public: if (kd_counter[ind] >= (double)number_of_segments / (double)number_of_trees / 2.0) { - kd = create_kd_tree(angle, kd_new); + kd = create_kd_tree(angle); + kd_trees_list.push_back(kd); - //waqar new kd - kd_trees_new_list.push_back(kd_new); - #ifdef CGAL_SR_DEBUG ++number_of_actual_kd_trees; #endif @@ -514,7 +518,7 @@ public: } //debugging - std::cout<< "*&^*&^*&^ Numer of old Kd_trees: " << kd_trees_list.size() << " , Number of new Kd_trees: " << kd_trees_new_list.size() << std::endl; + //std::cout<< "*&^*&^*&^ Numer of old Kd_trees: " << kd_trees_list.size() << " , Number of new Kd_trees: " << kd_trees_new_list.size() << std::endl; // typename Kd_triple_list_new::const_iterator iter_new ; // for(iter_new = kd_trees_new_list.begin(); iter_new!=kd_trees_new_list.end(); ++iter_new) // iter_new->first->statistics(std::cout); @@ -530,17 +534,11 @@ public: } ~Multiple_kd_tree() { - for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); - it != kd_trees_list.end(); ++it) { + //delete all the kd_trees. + for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); it != kd_trees_list.end(); ++it) delete (it->first); - } - - //waqar new kd - for(typename Kd_triple_list_new::iterator it = kd_trees_new_list.begin(); - it != kd_trees_new_list.end(); ++it) { - delete (it->first); - } - + + //delete all the points. for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); it != input_points_list.end(); ++it) { delete (it->second); @@ -583,74 +581,35 @@ public: // determine right kd-tree to work on, depending on the segment's slope Direction_2 d = get_direction(s); - //old kd_tree block starts int i = 0; int n = kd_trees_list.size(); bool found = false; typename Kd_triple_list::const_iterator iter = kd_trees_list.begin(); - while(i < n && !found) { - if (iter->second.first > d) found = true; + while(i < n && !found) + { + + if (iter->second.first > d) + found = true; + ++i; ++iter; } - if (!found) iter = kd_trees_list.begin(); - else --iter; + if (!found) + iter = kd_trees_list.begin(); - //////////// - ///code for new kd_tree i.e. block above - ///////////// - - int i_new = 0; - int n_new = kd_trees_new_list.size(); - bool found_new = false; - typename Kd_triple_list_new::const_iterator iter_new = kd_trees_new_list.begin(); - - while(i_new < n_new && !found_new) - { - - if (iter_new->second.first > d) - found_new = true; - - ++i_new; - ++iter_new; - } - - if (!found_new) - { - iter_new = kd_trees_new_list.begin(); - //std::cout << "found_new is false. kd_tree: 0 activated " << std::endl; - } else - { - //std::cout << "Found kd_tree: " << --i_new << " activated" << std::endl; - --iter_new; - } - - //std::cout << "Debug:: i_new: " << i_new << std::endl; - - /////// - /// new block ends - //////// - - - - - + --iter; Point_list points_list; m_gt.minkowski_sum_with_pixel_2_object()(points_list, s, unit_square); Point_iter points_iter; - for (points_iter = points_list.begin(); points_iter != points_list.end(); ++points_iter) - { - //the point has to be rotated only once so I am using the new kd_tree as reference. does not change the result. - //rotate(*points_iter, iter->second.second); - //waqar new kd - rotate(*points_iter, iter_new->second.second); - } + for (points_iter = points_list.begin(); points_iter != points_list.end(); ++points_iter) + rotate(*points_iter, iter->second.second); + // query points_iter = points_list.begin(); Point_2 point_left,point_right,point_bot,point_top; @@ -674,31 +633,17 @@ public: My_point_saved point1(p1); My_point_saved point2(p2); - Box b(point1, point2, 2); - //waqar kd new - Box_new b_new(point1, point2); + Box b(point1, point2); // the kd-tree query - My_point_saved_list res; - //waqar kd new - My_point_saved_list res_new; + My_point_saved_list result; - iter->first->search(std::back_inserter(res), b); - //waqar kd new - //i think the res_new might be different then old res (yes it is confirmed) either the box is faulty or the back inserter - iter_new->first->search(std::back_inserter(res_new), b_new); - - std::cout<< "******* Size of res: " << res.size()<< std::endl; - for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) - std::cout<< my_point_iter->orig << std::endl; - - std::cout<< "******* Size of res_new: " << res_new.size()<< std::endl; - for (My_point_saved_iter my_point_iter = res_new.begin(); my_point_iter != res_new.end(); ++my_point_iter) - std::cout<< my_point_iter->orig << std::endl<< std::endl; + iter->first->search(std::back_inserter(result), b); // create result result_list.empty(); - for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) + + for( My_point_saved_iter my_point_iter = result.begin(); my_point_iter != result.end(); ++my_point_iter ) result_list.push_back(my_point_iter->object); } }; diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h old mode 100644 new mode 100755 index 2c252e55afa..bc4d419e17e --- a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h @@ -33,27 +33,28 @@ class Snap_rounding_traits_2 : public CGAL::Arr_segment_traits_2 { public: // otherwise Segment_data cannot access the types - typedef typename Base_kernel::FT NT; - typedef typename Base_kernel::FT FT; - typedef typename Base_kernel::Point_2 Point_2; - typedef typename Base_kernel::Segment_2 Segment_2; - typedef typename Base_kernel::Iso_rectangle_2 Iso_rectangle_2; - typedef typename Base_kernel::Vector_2 Vector_2; - typedef typename Base_kernel::Line_2 Line_2; - typedef typename Base_kernel::Aff_transformation_2 Aff_transformation_2; - typedef typename Base_kernel::Direction_2 Direction_2; - typedef typename Base_kernel::Construct_vertex_2 Construct_vertex_2 ; - typedef typename Base_kernel::Construct_segment_2 Construct_segment_2 ; - typedef typename Base_kernel::Construct_iso_rectangle_2 - Construct_iso_rectangle_2; - typedef typename Base_kernel::Compare_y_2 Compare_y_2; + typedef typename Base_kernel::FT NT; + typedef typename Base_kernel::FT FT; + typedef typename Base_kernel::Point_2 Point_2; + typedef typename Base_kernel::Segment_2 Segment_2; + typedef typename Base_kernel::Iso_rectangle_2 Iso_rectangle_2; + typedef typename Base_kernel::Vector_2 Vector_2; + typedef typename Base_kernel::Line_2 Line_2; + typedef typename Base_kernel::Aff_transformation_2 Aff_transformation_2; + typedef typename Base_kernel::Direction_2 Direction_2; + typedef typename Base_kernel::Construct_vertex_2 Construct_vertex_2 ; + typedef typename Base_kernel::Construct_segment_2 Construct_segment_2 ; + typedef typename Base_kernel::Construct_iso_rectangle_2 Construct_iso_rectangle_2; + typedef typename Base_kernel::Compare_y_2 Compare_y_2; - typedef CGAL::Arr_segment_traits_2 Base_traits; + typedef typename Base_kernel::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename Base_kernel::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename Base_kernel::Cartesian_const_iterator_2 Cartesian_const_iterator_2; + typedef typename Base_kernel::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; - typedef typename Base_traits::Compare_x_2 Compare_x_2; - - - typedef CGAL::To_double To_double; + typedef CGAL::Arr_segment_traits_2 Base_traits; + typedef typename Base_traits::Compare_x_2 Compare_x_2; + typedef CGAL::To_double To_double; public: /*! Functor */ From 6c89d63be3dea07082eefbaa890daff38498e609 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Thu, 30 Jan 2014 01:05:30 +0100 Subject: [PATCH 06/27] 30-January-2014 Old kd_trees successfully replaced with new kd_trees (comments modified). Thourough testing still pending. --- Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 7dbdf1cd8c5..d695f373445 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -158,6 +158,11 @@ public: } }; +///////////////////// +///////////////////// +//Multiple_kd_tree +///////////////////// + template class Multiple_kd_tree { CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); From 980351403ce698cf6f095b2ebe4d2bf73c692318 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Thu, 30 Jan 2014 14:20:56 +0100 Subject: [PATCH 07/27] 30-January-2014 Snap rounding kd_tree replacement Completed. Thorough tests pending. --- .../include/CGAL/Snap_rounding_kd_2.h | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index d695f373445..59028dfd483 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -78,7 +78,7 @@ public: ////////////////////////// ////////////////////////// -//Kd_tree_interface_new_2d +//My_point_interface_2d ////////////////////////// template @@ -130,7 +130,7 @@ public: ////////////////////// ////////////////////// -//Search_traits_new_2 +//Search_traits_kd_tree_2 // //(Search traits modified to be used by the Spacial Searching kd_trees for Snap rounding) ////////////////////// @@ -502,7 +502,9 @@ public: #ifdef CGAL_SR_DEBUG int number_of_actual_kd_trees = 0; #endif + i = 0; + for (NT angle = 0; i < number_of_trees; angle += NT(half_pi / number_of_trees),++i) { @@ -522,13 +524,6 @@ public: ++ind; } - //debugging - //std::cout<< "*&^*&^*&^ Numer of old Kd_trees: " << kd_trees_list.size() << " , Number of new Kd_trees: " << kd_trees_new_list.size() << std::endl; - // typename Kd_triple_list_new::const_iterator iter_new ; - // for(iter_new = kd_trees_new_list.begin(); iter_new!=kd_trees_new_list.end(); ++iter_new) - // iter_new->first->statistics(std::cout); - //debugging end - delete[] kd_counter; #ifdef CGAL_SR_DEBUG @@ -538,7 +533,8 @@ public: } - ~Multiple_kd_tree() { + ~Multiple_kd_tree() + { //delete all the kd_trees. for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); it != kd_trees_list.end(); ++it) delete (it->first); @@ -593,7 +589,6 @@ public: while(i < n && !found) { - if (iter->second.first > d) found = true; @@ -617,9 +612,11 @@ public: // query points_iter = points_list.begin(); - Point_2 point_left,point_right,point_bot,point_top; + Point_2 point_left, point_right, point_bot, point_top; point_left = point_right = point_bot = point_top = *points_iter; - for (++points_iter; points_iter != points_list.end(); ++points_iter) { + + for (++points_iter; points_iter != points_list.end(); ++points_iter) + { point_left = small_x_point(point_left,*points_iter); point_right = big_x_point(point_right,*points_iter); point_bot = small_y_point(point_bot,*points_iter); From 287669a50066c504669d1050c24f72d596d59402 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Thu, 6 Feb 2014 16:31:19 +0100 Subject: [PATCH 08/27] code clean up --- .../include/CGAL/Snap_rounding_kd_2.h | 115 ++++-------------- 1 file changed, 26 insertions(+), 89 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 59028dfd483..a38de24275d 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -38,11 +38,11 @@ namespace CGAL { ////////////////////// ////////////////////// -//My_point +//Point_with_hot_pixel_history ////////////////////// template -class My_point : public Traits::Point_2 { +class Point_with_hot_pixel_history : public Traits::Point_2 { private: typedef typename Traits::Point_2 Point_2; typedef typename Traits::FT NT; @@ -52,82 +52,26 @@ public: typedef typename Traits::FT FT; typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; - typedef typename Traits::Circle_2 Circle_2; typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_2; typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_2; typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename Traits::Construct_center_2 Construct_center_2; typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; - typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; - Point_2 orig; SAVED_OBJECT object; - My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} + Point_with_hot_pixel_history(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} - My_point(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} + Point_with_hot_pixel_history(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} - My_point() : Point_2(),orig() {} + Point_with_hot_pixel_history() : Point_2(),orig() {} - My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} + Point_with_hot_pixel_history(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} }; -////////////////////////// -////////////////////////// -//My_point_interface_2d -////////////////////////// - -template -class My_point_interface_2d -{ -public: - typedef PT Point; - typedef typename PT::Point_d Point_2; - typedef typename PT::FT FT; - - typedef typename PT::Iso_rectangle_2 Iso_rectangle_2; - typedef typename PT::Circle_2 Circle_2; - typedef typename PT::Cartesian_const_iterator_2 Cartesian_const_iterator_2; - typedef typename PT::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; - - typedef typename PT::Construct_min_vertex_2 Construct_min_vertex_2; - typedef typename PT::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename PT::Construct_center_2 Construct_center_2; - typedef typename PT::Construct_iso_rectangle_2 Construct_iso_rectangle_2; - - typedef typename PT::Compute_squared_radius_2 Compute_squared_radius_2; - - static int dimension( const PT & pnt ) - { - return pnt.dimension(); // return pnt.dimensions(); - } - - static int compare( int d, const PT & a, const PT & b ) - { - if ( a[ d ] < b[ d ] ) - return -1; - if ( a[ d ] > b[ d ] ) - return 1; - - return 0; - } - static void copy_coord( int d, PT & a, const PT & b ) - { - if ( d == 0 ) - a = PT( b[ 0 ], a[1] ); - else - if ( d == 1 ) - a = PT( a[ 0 ], b[1] ); - else { - CGAL_error(); - } - } -}; - ////////////////////// ////////////////////// //Search_traits_kd_tree_2 @@ -139,23 +83,22 @@ template class Search_traits_kd_tree_2 { public: - typedef typename K::Point Point_d; + typedef K Point_d; typedef typename K::Iso_rectangle_2 Iso_box_d; - typedef typename K::Circle_2 Sphere_d; typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; - typedef typename K::Construct_center_2 Construct_center_d; - typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; typedef typename K::FT FT; - Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { + Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const + { return Construct_cartesian_const_iterator_d(); } + }; ///////////////////// @@ -177,24 +120,23 @@ private: typedef typename Traits::Line_2 Line_2; typedef typename Traits::Aff_transformation_2 Transformation_2; - typedef My_point My_point_saved; - typedef My_point_interface_2d My_point_interface; - typedef CGAL::Search_traits_kd_tree_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree; - typedef CGAL::Fuzzy_iso_box Box; + typedef Point_with_hot_pixel_history Point_with_hot_pixel_history_saved; + typedef CGAL::Search_traits_kd_tree_2 Search_traits; + typedef CGAL::Kd_tree Kd_tree; + typedef CGAL::Fuzzy_iso_box Box; - typedef std::list Points_List; - typedef std::pair Direction_nt_pair; - typedef std::pair Kd_triple; - typedef std::pair Kd_direction_nt_pair; - typedef std::list Kd_triple_list; + typedef std::list Points_List; + typedef std::pair Direction_nt_pair; + typedef std::pair Kd_triple; + typedef std::pair Kd_direction_nt_pair; + typedef std::list Kd_triple_list; typedef std::pair Point_saved_pair; typedef std::list Point_saved_pair_list; typedef typename Point_saved_pair_list::iterator Point_saved_pair_iter; - typedef typename std::list My_point_saved_list; - typedef typename My_point_saved_list::iterator My_point_saved_iter; + typedef typename std::list Point_with_hot_pixel_history_saved_list; + typedef typename Point_with_hot_pixel_history_saved_list::iterator Point_with_hot_pixel_history_saved_iter; typedef std::list Point_list; typedef typename Point_list::iterator Point_iter; @@ -242,18 +184,13 @@ private: { Point_2 p(iter->first); rotate(p,angle); - My_point_saved rotated_point(p,iter->first,iter->second); + Point_with_hot_pixel_history_saved rotated_point(p,iter->first,iter->second); tree->insert(rotated_point); } tree->build(); - //Code used by the old kd_trees. - //TODO: check if it is required by the new kd_trees. - //checking validity - //if (!tree->is_valid()) tree->dump(); - //CGAL_assertion(tree->is_valid()); typename Traits::To_double to_dbl; double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); @@ -632,20 +569,20 @@ public: Point_2 p1 = rec.vertex(0); Point_2 p2 = rec.vertex(2); - My_point_saved point1(p1); - My_point_saved point2(p2); + Point_with_hot_pixel_history_saved point1(p1); + Point_with_hot_pixel_history_saved point2(p2); Box b(point1, point2); // the kd-tree query - My_point_saved_list result; + Point_with_hot_pixel_history_saved_list result; iter->first->search(std::back_inserter(result), b); // create result result_list.empty(); - for( My_point_saved_iter my_point_iter = result.begin(); my_point_iter != result.end(); ++my_point_iter ) + for( Point_with_hot_pixel_history_saved_iter my_point_iter = result.begin(); my_point_iter != result.end(); ++my_point_iter ) result_list.push_back(my_point_iter->object); } }; From b46b37e7b260f1363430b814a2356ba47c4ddf72 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Mon, 10 Feb 2014 03:30:56 +0100 Subject: [PATCH 09/27] reserve_to_capacity() method for Kd_tree.h that pre-allocated memory for points added. New Snap_rounding example added where the input data is taken from a file. Snap_rounding_kd_2.h modified to use the reserve_to_capacity method before creating the kd_tree. This improves the running time. --- .../Snap_rounding_2/snap_rounding_data.cpp | 82 +++++++++++++++++++ .../include/CGAL/Snap_rounding_kd_2.h | 2 + Spatial_searching/include/CGAL/Kd_tree.h | 5 ++ 3 files changed, 89 insertions(+) create mode 100644 Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp new file mode 100644 index 00000000000..6f5268e056f --- /dev/null +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef CGAL::Quotient Number_type; +typedef CGAL::Cartesian Kernel; +typedef CGAL::Snap_rounding_traits_2 Traits; +typedef Kernel::Segment_2 Segment_2; +typedef Kernel::Point_2 Point_2; +typedef std::list Segment_list_2; +typedef std::list Polyline_2; +typedef std::list Polyline_list_2; + +int main(int argc, char* argv[]) +{ + if(argc != 2) + { + std::cout<< "Incorrect input. please provide the file path of the data file only." << std::endl; + return -1; + } + + Segment_list_2 seg_list; + Polyline_list_2 output_list; + + std::ifstream my_read_file; + my_read_file.open(argv[1]); + + if(!my_read_file.is_open()) + { + std::cout<< "Error opening the file"<< std::endl; + return -1; + } + + CGAL::Timer segment_creation_time, snap_rounding_time; + + unsigned int number_of_lines = 0; + my_read_file >> number_of_lines; + + segment_creation_time.start(); + + for(int i=0; i> point_start_x; + my_read_file >> point_start_y; + my_read_file >> point_end_x; + my_read_file >> point_end_y; + + seg_list.push_back(Segment_2(Point_2(point_start_x, point_start_y), Point_2(point_end_x, point_end_y))); + } + std::cout << "Segment list size: "< + (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); + + snap_rounding_time.stop(); + + int counter = 0; + Polyline_list_2::const_iterator iter1; + for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) + { + std::cout << "Polyline number " << ++counter << ":\n"; + Polyline_2::const_iterator iter2; + + for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) + std::cout << " (" << iter2->x() << ":" << iter2->y() << ")\n"; + } + + std::cerr << "\n\nSegment creation of " << number_of_lines << " took: " << segment_creation_time.time() << " sec" <reserve_to_capacity(input_points_list.size()); for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) { diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index 23fec0af12d..8c8f22ce683 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -267,6 +267,11 @@ public: pts.insert(pts.end(),first, beyond); } + void reserve_to_capacity(size_t size) + { + pts.reserve(size); + } + template OutputIterator From 558bbf3bc4d14800f19fa8813456c49923a79774 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Mon, 10 Feb 2014 03:36:05 +0100 Subject: [PATCH 10/27] Some unnecessary comments removed --- Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 1af4747edac..0b2f27d820d 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -179,7 +179,7 @@ private: { Kd_tree *tree = new Kd_tree(); - //test function that reserves the size of the vector first. + tree->reserve_to_capacity(input_points_list.size()); for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) From 7a6b69cabc415b8d603c6e46a93ba6659c6fa575 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Mon, 10 Feb 2014 03:39:31 +0100 Subject: [PATCH 11/27] Debug messages removed --- Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index 6f5268e056f..e09823316e3 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -52,7 +52,7 @@ int main(int argc, char* argv[]) seg_list.push_back(Segment_2(Point_2(point_start_x, point_start_y), Point_2(point_end_x, point_end_y))); } - std::cout << "Segment list size: "< Date: Tue, 18 Feb 2014 01:51:07 +0100 Subject: [PATCH 12/27] 18-Feb-2014 Variable names modified appropriately in Snap_rounding_kd_2.h. Also snap_rounding_data.cpp modified to take output file as argument aswell --- .../Snap_rounding_2/snap_rounding_data.cpp | 58 +++++++++++++++---- .../include/CGAL/Snap_rounding_kd_2.h | 18 +++--- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index e09823316e3..0c8767f9907 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -17,9 +17,9 @@ typedef std::list Polyline_list_2; int main(int argc, char* argv[]) { - if(argc != 2) + if(argc > 3 || argc < 2) { - std::cout<< "Incorrect input. please provide the file path of the data file only." << std::endl; + std::cout<< "Incorrect input. please provide the file path of the data file only. (optionally) enter the output file name" << std::endl; return -1; } @@ -27,14 +27,29 @@ int main(int argc, char* argv[]) Polyline_list_2 output_list; std::ifstream my_read_file; + std::ofstream my_write_file; + my_read_file.open(argv[1]); if(!my_read_file.is_open()) { - std::cout<< "Error opening the file"<< std::endl; + std::cout<< "Error opening the input file"<< std::endl; return -1; } + if(argc==3) + { + my_write_file.open(argv[2]); + + if(!my_read_file.is_open()) + { + std::cout<< "Error opening the output file"<< std::endl; + return -1; + } + } + + + CGAL::Timer segment_creation_time, snap_rounding_time; unsigned int number_of_lines = 0; @@ -60,23 +75,44 @@ int main(int argc, char* argv[]) // Generate an iterated snap-rounding representation, where the centers of // the hot pixels bear their original coordinates, using 1 kd trees: CGAL::snap_rounding_2 - (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); + (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); snap_rounding_time.stop(); int counter = 0; Polyline_list_2::const_iterator iter1; - for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) + + if(argc == 3) //output to the file { - std::cout << "Polyline number " << ++counter << ":\n"; - Polyline_2::const_iterator iter2; - - for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) - std::cout << " (" << iter2->x() << ":" << iter2->y() << ")\n"; + for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) + { + my_write_file << "Polyline number " << ++counter << ":\n"; + Polyline_2::const_iterator iter2; + + for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) + my_write_file << " (" << iter2->x() << ":" << iter2->y() << ")\n"; + } + + my_write_file << "\n\nSegment creation of " << number_of_lines << " took: " << segment_creation_time.time() << " sec" <begin(); iter2 != iter1->end(); ++iter2) + std::cout << " (" << iter2->x() << ":" << iter2->y() << ")\n"; + } + std::cerr << "\n\nSegment creation of " << number_of_lines << " took: " << segment_creation_time.time() << " sec" < +template class Search_traits_kd_tree_2 { public: - typedef K Point_d; - typedef typename K::Iso_rectangle_2 Iso_box_d; - typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; - typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; + typedef Traits Point_d; + typedef typename Traits::Iso_rectangle_2 Iso_box_d; + typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_d; + typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; - typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; - typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; + typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_d; + typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_d; - typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; - typedef typename K::FT FT; + typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_box_d; + typedef typename Traits::FT FT; Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { From e8ca703f028e1de816cc7359dd8cf4598f181e18 Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Tue, 18 Feb 2014 14:58:09 +0100 Subject: [PATCH 13/27] refactored some classes * simplified Point_with_history * added second parameter (Traits) to Search_traits * made classes internal --- .../include/CGAL/Snap_rounding_kd_2.h | 131 +++++++++--------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 0698c096f86..020966ac409 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -36,6 +36,8 @@ namespace CGAL { +namespace internal { + ////////////////////// ////////////////////// //Point_with_hot_pixel_history @@ -43,47 +45,43 @@ namespace CGAL { template class Point_with_hot_pixel_history : public Traits::Point_2 { + private: + + typedef typename Traits::Point_2 Base; typedef typename Traits::Point_2 Point_2; typedef typename Traits::FT NT; - -public: - typedef typename Traits::Point_2 Point_d; - typedef typename Traits::FT FT; - - typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; - typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_2; - typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; - typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_2; - typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; +public: Point_2 orig; SAVED_OBJECT object; - - Point_with_hot_pixel_history(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} - - Point_with_hot_pixel_history(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} - - Point_with_hot_pixel_history() : Point_2(),orig() {} - - Point_with_hot_pixel_history(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} -}; + + Point_with_hot_pixel_history(const Base& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Base(p), orig(inp_orig), object(obj) {} + + Point_with_hot_pixel_history(const Base& p) : Base(p), orig(Point_2(0, 0)) {} + + Point_with_hot_pixel_history() : Base(), orig() {} + + Point_with_hot_pixel_history(NT x, NT y) : Base(x, y), orig(Point_2(0, 0)) {} + +}; // Point_with_hot_pixel_history ////////////////////// ////////////////////// //Search_traits_kd_tree_2 -// +// //(Search traits modified to be used by the Spacial Searching kd_trees for Snap rounding) ////////////////////// -template +template < class Traits_, class Point_ = typename Traits_::Point_2 > class Search_traits_kd_tree_2 { public: - typedef Traits Point_d; + typedef Traits_ Traits; + typedef Point_ Point_d; + typedef typename Traits::Iso_rectangle_2 Iso_box_d; typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_d; typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; @@ -94,12 +92,14 @@ public: typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_box_d; typedef typename Traits::FT FT; - Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const + Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { return Construct_cartesian_const_iterator_d(); - } + } -}; +}; // Search_traits_kd_tree_2 + +} // namespace internal ///////////////////// ///////////////////// @@ -119,12 +119,13 @@ private: typedef typename Traits::Direction_2 Direction_2; typedef typename Traits::Line_2 Line_2; typedef typename Traits::Aff_transformation_2 Transformation_2; - - typedef Point_with_hot_pixel_history Point_with_hot_pixel_history_saved; - typedef CGAL::Search_traits_kd_tree_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree; - typedef CGAL::Fuzzy_iso_box Box; - + + typedef CGAL::internal::Point_with_hot_pixel_history Point_with_hot_pixel_history_saved; + typedef CGAL::internal::Search_traits_kd_tree_2 + Search_traits; + typedef CGAL::Kd_tree Kd_tree; + typedef CGAL::Fuzzy_iso_box Box; + typedef std::list Points_List; typedef std::pair Direction_nt_pair; typedef std::pair Kd_triple; @@ -153,8 +154,8 @@ private: const double pi, half_pi; int number_of_trees; - Kd_triple_list kd_trees_list; - + Kd_triple_list kd_trees_list; + Point_saved_pair_list input_points_list; std::map angle_to_sines_appr; // was const int @@ -179,7 +180,7 @@ private: { Kd_tree *tree = new Kd_tree(); - + tree->reserve_to_capacity(input_points_list.size()); for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) @@ -187,7 +188,7 @@ private: Point_2 p(iter->first); rotate(p,angle); Point_with_hot_pixel_history_saved rotated_point(p,iter->first,iter->second); - + tree->insert(rotated_point); } @@ -197,12 +198,12 @@ private: typename Traits::To_double to_dbl; double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); - if (buffer_angle < 0) + if (buffer_angle < 0) buffer_angle = 0; - + Line_2 li(std::tan(buffer_angle), -1, 0); Direction_2 d(li); - + // rotate_by 180 degrees Transformation_2 t(ROTATION, 0, -1); d = d.transform(t); @@ -220,11 +221,11 @@ private: inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) - {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), min BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),min BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) - {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), + {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), max BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),max BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} /*! */ @@ -397,7 +398,7 @@ public: pi(3.1415), half_pi(1.57075), number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) { - + Kd_triple kd; // check that there are at least two trees @@ -421,13 +422,13 @@ public: angle += half_pi / number_of_trees,++i) { buffer_angle = angle - half_pi / (2 * number_of_trees); - - if (buffer_angle < 0) + + if (buffer_angle < 0) buffer_angle = 0; - + li = Line_2(std::tan(buffer_angle), -1, 0); d = Direction_2(li); - + // rotate_by 180 degrees Transformation_2 t(ROTATION, 0, -1); d = d.transform(t); @@ -441,9 +442,9 @@ public: #ifdef CGAL_SR_DEBUG int number_of_actual_kd_trees = 0; #endif - + i = 0; - + for (NT angle = 0; i < number_of_trees; angle += NT(half_pi / number_of_trees),++i) { @@ -451,7 +452,7 @@ public: (double)number_of_segments / (double)number_of_trees / 2.0) { kd = create_kd_tree(angle); - + kd_trees_list.push_back(kd); #ifdef CGAL_SR_DEBUG @@ -472,15 +473,15 @@ public: } - ~Multiple_kd_tree() + ~Multiple_kd_tree() { //delete all the kd_trees. - for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); it != kd_trees_list.end(); ++it) + for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); it != kd_trees_list.end(); ++it) delete (it->first); - + //delete all the points. - for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); - it != input_points_list.end(); ++it) { + for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); + it != input_points_list.end(); ++it) { delete (it->second); } @@ -526,11 +527,11 @@ public: bool found = false; typename Kd_triple_list::const_iterator iter = kd_trees_list.begin(); - while(i < n && !found) + while(i < n && !found) { - if (iter->second.first > d) + if (iter->second.first > d) found = true; - + ++i; ++iter; } @@ -538,7 +539,7 @@ public: if (!found) iter = kd_trees_list.begin(); - else + else --iter; Point_list points_list; @@ -548,13 +549,13 @@ public: for (points_iter = points_list.begin(); points_iter != points_list.end(); ++points_iter) rotate(*points_iter, iter->second.second); - + // query points_iter = points_list.begin(); Point_2 point_left, point_right, point_bot, point_top; point_left = point_right = point_bot = point_top = *points_iter; - - for (++points_iter; points_iter != points_list.end(); ++points_iter) + + for (++points_iter; points_iter != points_list.end(); ++points_iter) { point_left = small_x_point(point_left,*points_iter); point_right = big_x_point(point_right,*points_iter); @@ -563,9 +564,9 @@ public: } typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; - + Construct_iso_rectangle_2 construct_rec = m_gt.construct_iso_rectangle_2_object(); - + Iso_rectangle_2 rec = construct_rec(point_left, point_right, point_bot, point_top); Point_2 p1 = rec.vertex(0); @@ -578,13 +579,13 @@ public: // the kd-tree query Point_with_hot_pixel_history_saved_list result; - + iter->first->search(std::back_inserter(result), b); // create result result_list.empty(); - for( Point_with_hot_pixel_history_saved_iter my_point_iter = result.begin(); my_point_iter != result.end(); ++my_point_iter ) + for( Point_with_hot_pixel_history_saved_iter my_point_iter = result.begin(); my_point_iter != result.end(); ++my_point_iter ) result_list.push_back(my_point_iter->object); } }; From 0e14493632487d1f07297b890290cf2967bd1f54 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Sat, 1 Mar 2014 18:00:11 +0100 Subject: [PATCH 14/27] 1. "snap_rounding_data.cpp" Added Example usage in-code documentation. Removed the timer that recorded the amount of time taken by the snap_rounding. The timer was initally added to test the snap rounding running time difference using new Kd_trees 2. "Snap_rounding_kd_2.h" "Snap_rounding_traits_2.h" Added documentation and changed Copyright information and authors appropriately. 3. "kd_tree.h" Renamed the reserve_to_capacity() to reserve(). 4. Snap_rounding_kd_new.h deleted --- .../Snap_rounding_2/snap_rounding_data.cpp | 59 +- .../include/CGAL/Snap_rounding_kd_2.h | 24 +- .../include/CGAL/Snap_rounding_kd_new_2.h | 686 ------------------ .../include/CGAL/Snap_rounding_traits_2.h | 24 +- Spatial_searching/include/CGAL/Kd_tree.h | 25 +- 5 files changed, 77 insertions(+), 741 deletions(-) delete mode 100755 Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index 0c8767f9907..52a27bd492d 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -1,10 +1,44 @@ +// Copyright (c) 1999 +// Utrecht University (The Netherlands), +// ETH Zurich (Switzerland), +// INRIA Sophia-Antipolis (France), +// Max-Planck-Institute Saarbruecken (Germany), +// and Tel-Aviv University (Israel). All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// +// author(s) : Waqar Khan + + +/* Usage +* +* This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE. +* (Mandatory) path to the input file containing the arrangment information. +* (Optional) path to the output file where the results of snap rounding will be stored. +* Not providing this argument will print the result on standard output. +* +* Input file format +* Line # 1: Number of line-segments present in the file. +* Line # 2 to N+1: segment_start_point_x segment_start_point_y segment_end_point_x segment_end_point_y +* +* Each line should contain information about just one segment. +*/ + #include #include #include #include #include #include -#include typedef CGAL::Quotient Number_type; typedef CGAL::Cartesian Kernel; @@ -19,7 +53,7 @@ int main(int argc, char* argv[]) { if(argc > 3 || argc < 2) { - std::cout<< "Incorrect input. please provide the file path of the data file only. (optionally) enter the output file name" << std::endl; + std::cout<< "Incorrect input. path to the INPUT file. (optional) path to the OUTPUT file" << std::endl; return -1; } @@ -48,15 +82,9 @@ int main(int argc, char* argv[]) } } - - - CGAL::Timer segment_creation_time, snap_rounding_time; - unsigned int number_of_lines = 0; my_read_file >> number_of_lines; - segment_creation_time.start(); - for(int i=0; i - (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); - - snap_rounding_time.stop(); + (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); int counter = 0; Polyline_list_2::const_iterator iter1; @@ -92,9 +114,7 @@ int main(int argc, char* argv[]) for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) my_write_file << " (" << iter2->x() << ":" << iter2->y() << ")\n"; } - - my_write_file << "\n\nSegment creation of " << number_of_lines << " took: " << segment_creation_time.time() << " sec" <begin(); iter2 != iter1->end(); ++iter2) std::cout << " (" << iter2->x() << ":" << iter2->y() << ")\n"; } - - std::cerr << "\n\nSegment creation of " << number_of_lines << " took: " << segment_creation_time.time() << " sec" < +// author(s) : Eli Packer , Waqar Khan + #ifndef CGAL_SNAP_ROUNDING_KD_2_H #define CGAL_SNAP_ROUNDING_KD_2_H @@ -181,7 +183,7 @@ private: Kd_tree *tree = new Kd_tree(); - tree->reserve_to_capacity(input_points_list.size()); + tree->reserve(input_points_list.size()); for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) { diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h deleted file mode 100755 index 2f21f693adb..00000000000 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_new_2.h +++ /dev/null @@ -1,686 +0,0 @@ -// Copyright (c) 2001 Tel-Aviv University (Israel). -// All rights reserved. -// -// This file is part of CGAL (www.cgal.org). -// 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 3 of the License, or (at your option) any later version. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. -// -// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -// -// $URL$ -// $Id$ -// -// -// author(s) : Eli Packer -#ifndef CGAL_SNAP_ROUNDING_KD_2_H -#define CGAL_SNAP_ROUNDING_KD_2_H - -#include -#include -//#include -#include -#include -//#include -#include -#include - -#include - -//waqar -#include -#include -#include - - - - -//define search trait class here. -// -// -//in namespace internal. - - - -namespace CGAL { - - -////////////////////// -////////////////////// -//My_point -////////////////////// - -template -class My_point : public Traits::Point_2 { -private: - typedef typename Traits::Point_2 Point_2; - typedef typename Traits::FT NT; - -public: - typedef typename Traits::Point_2 Point_d; - typedef typename Traits::FT FT; - - typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; - typedef typename Traits::Circle_2 Circle_2; - typedef typename Traits::Cartesian_const_iterator_2 Cartesian_const_iterator_2; - typedef typename Traits::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; - - typedef typename Traits::Construct_min_vertex_2 Construct_min_vertex_2; - typedef typename Traits::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename Traits::Construct_center_2 Construct_center_2; - typedef typename Traits::Construct_iso_rectangle_2 Construct_iso_rectangle_2; - - typedef typename Traits::Compute_squared_radius_2 Compute_squared_radius_2; - - //waqar: the data is stored in below mentioned variables. check if the data is parsed correctly by the kd_tree - Point_2 orig; - SAVED_OBJECT object; - - My_point(const Point_2& p, const Point_2& inp_orig, SAVED_OBJECT obj) : Point_2(p), orig(inp_orig), object(obj) {} - - My_point(const Point_2& p) : Point_2(p), orig(Point_2(0, 0)) {} - - My_point() : Point_2(),orig() {} - - My_point(NT x, NT y) : Point_2(x, y), orig(Point_2(0, 0)) {} -}; - -////////////////////// -////////////////////// -//Kd_tree_interface_new_2d -////////////////////// - -template -class Kdtree_new_interface_2d -{ -public: - typedef PT Point; - typedef typename PT::Point_d Point_2; - typedef typename PT::FT FT; - - typedef typename PT::Iso_rectangle_2 Iso_rectangle_2; - typedef typename PT::Circle_2 Circle_2; - typedef typename PT::Cartesian_const_iterator_2 Cartesian_const_iterator_2; - typedef typename PT::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_2; - - typedef typename PT::Construct_min_vertex_2 Construct_min_vertex_2; - typedef typename PT::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename PT::Construct_center_2 Construct_center_2; - typedef typename PT::Construct_iso_rectangle_2 Construct_iso_rectangle_2; - - typedef typename PT::Compute_squared_radius_2 Compute_squared_radius_2; - - static int dimension( const PT & pnt ) - { - return pnt.dimension(); // return pnt.dimensions(); - } - - static int compare( int d, const PT & a, const PT & b ) - { - if ( a[ d ] < b[ d ] ) - return -1; - if ( a[ d ] > b[ d ] ) - return 1; - - return 0; - } - static void copy_coord( int d, PT & a, const PT & b ) - { - if ( d == 0 ) - a = PT( b[ 0 ], a[1] ); - else - if ( d == 1 ) - a = PT( a[ 0 ], b[1] ); - else { - CGAL_error(); - } - } -}; - - - -// ////////////////////// -// ////////////////////// -// //Search_traits_new_2 -// ////////////////////// - -// template -// class Search_traits_new_2 { - -// public: -// typedef typename K::Point_2 Point_d; -// typedef typename K::Iso_rectangle_2 Iso_box_d; -// typedef typename K::Circle_2 Sphere_d; -// typedef typename K::Cartesian_const_iterator_2 Cartesian_const_iterator_d; -// typedef typename K::Construct_cartesian_const_iterator_2 Construct_cartesian_const_iterator_d; - -// typedef typename K::Construct_min_vertex_2 Construct_min_vertex_d; -// typedef typename K::Construct_max_vertex_2 Construct_max_vertex_d; -// typedef typename K::Construct_center_2 Construct_center_d; -// typedef typename K::Compute_squared_radius_2 Compute_squared_radius_d; - -// typedef typename K::Construct_iso_rectangle_2 Construct_iso_box_d; -// typedef typename K::FT FT; - -// Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { -// return Construct_cartesian_const_iterator_d(); -// } -// }; - - - -////////////////////// -////////////////////// -//Multiple Kd_trees -////////////////////// - -template -class Multiple_kd_tree { - CGAL_static_assertion_msg((boost::is_pointer::value), "SAVED_OBJECT is not a pointer."); -private: - typedef Traits_ Traits; //Snap_rounding_traits< > > - typedef typename Traits::FT NT; - typedef typename Traits::Segment_2 Segment_2; - typedef typename Traits::Point_2 Point_2; - typedef typename Traits::Vector_2 Vector_2; - typedef typename Traits::Iso_rectangle_2 Iso_rectangle_2; - typedef typename Traits::Direction_2 Direction_2; - typedef typename Traits::Line_2 Line_2; - typedef typename Traits::Aff_transformation_2 Transformation_2; - - //CREATION OF TREE - typedef My_point My_point_saved; //Traits= snap_rounding_traits, SAVED_OBJECT= HOT_PIXEL * pointer. - typedef Kdtree_new_interface_2d Kd_interface; - //typedef CGAL::Kdtree_d Kd_tree; - //typedef typename Kd_tree::Box Box; - typedef CGAL::Search_traits_2 Search_traits; - typedef CGAL::Kd_tree Kd_tree; - typedef CGAL::Fuzzy_iso_box Box; - - - typedef std::list Points_List; - typedef std::pair Direction_nt_pair; - typedef std::pair Kd_triple; - typedef std::pair Kd_direction_nt_pair; - typedef std::list Kd_triple_list; - - typedef std::pair Point_saved_pair; - typedef std::list Point_saved_pair_list; - typedef typename Point_saved_pair_list::iterator Point_saved_pair_iter; - - typedef typename std::list My_point_saved_list; - typedef typename My_point_saved_list::iterator My_point_saved_iter; - - typedef std::list Point_list; - typedef typename Point_list::iterator Point_iter; - - typedef std::list Segment_list; - typedef typename Segment_list::const_iterator Segment_const_iter; - - typedef std::list Direction_list; - typedef typename Direction_list::const_iterator Direction_const_iter; - - //WAQAR:: CREATION OF NEW KD_TREE - // typedef CGAL::Search_traits_2 Search_traits; - // typedef CGAL::Kd_tree Kd_tree_new; - - // typedef std::pair Kd_triple_new; - // typedef std::pair Kd_direction_nt_pair_new; - // typedef std::list Kd_triple_list_new; - -private: - Traits m_gt; - const double pi, half_pi; - int number_of_trees; - Kd_triple_list kd_trees_list; - Point_saved_pair_list input_points_list; - std::map angle_to_sines_appr; // was const int - - /*! */ - void rotate(Point_2& p, NT angle) - { - static const double rad_to_deg = 57.297; - - typename Traits::To_double to_dbl; - int tranc_angle = int(to_dbl(angle) * rad_to_deg); - - NT cosine_val = angle_to_sines_appr[90 - tranc_angle], - sine_val = angle_to_sines_appr[tranc_angle]; - - Transformation_2 rotate(ROTATION, sine_val, cosine_val); - p = rotate(p); - } - - - /*! */ - //WAQAR: CREATE_KD_TREE IS FINISHED FOR NEW KD_TREE. - Kd_triple create_kd_tree(NT angle) - { - //Points_List l; - // Kd_tree *tree = new Kd_tree(2); - - //WAQAR INITIALIZATION OF THE NEW KD_TREE - Kd_tree *tree = new Kd_tree(); - - for (Point_saved_pair_iter iter = input_points_list.begin(); iter != input_points_list.end(); ++iter) - { - Point_2 p(iter->first); - rotate(p,angle); - My_point_saved rotated_point(p,iter->first,iter->second); - //WAQAR: Comenting this as this is only needed by the old kd_ttree - //l.push_back(rotated_point); - - //WAQAR: INSERT POINTS INTO NEW KD_TREE - tree->insert(rotated_point); - } - - //WAQAR: new kd_tree doesn not have this constructor. - //tree->build(l); - tree->build(); - //tree->statistics(std::cout); - //WAQAR: BUILD THE NEW KD_TREE WITH THE ROTATED POINTS. - //new_tree->build(); - - //checking validity - //WAQAR: Commenting these two lines as they are not compatible with new kd_trees i.e. is_valid() and dump(). - //if (!tree->is_valid()) tree->dump(); - //CGAL_assertion(tree->is_valid()); - - //WAQAR: NO ALTERNATIVE FOR CHECKING IF TREE IS VALID IN THE NEW KD_TREE. SHOULD I ADD ONE? - - typename Traits::To_double to_dbl; - double buffer_angle(to_dbl(angle) - half_pi / (2 * number_of_trees)); - - if (buffer_angle < 0) buffer_angle = 0; - Line_2 li(std::tan(buffer_angle), -1, 0); - Direction_2 d(li); - // rotate_by 180 degrees - Transformation_2 t(ROTATION, 0, -1); - d = d.transform(t); - Direction_nt_pair kp(d, angle); - Kd_triple kt(tree,kp); - - //WAQAR: - //Kd_triple_new kt_new(new_tree, kp); - return(kt); - } - - inline NT square(NT x) {return(x * x);} - - inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x, NT y) {return((x < y) ? x : y);} - inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x, NT y) {return((x < y) ? y : x);} - - inline NT min BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, - NT x6) - {return(min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (min BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), - min BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),min BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} - - inline NT max BOOST_PREVENT_MACRO_SUBSTITUTION (NT x1, NT x2, NT x3, NT x4, NT x5, NT x6) - {return(max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (max BOOST_PREVENT_MACRO_SUBSTITUTION (x1, x2), - max BOOST_PREVENT_MACRO_SUBSTITUTION (x3, x4)),max BOOST_PREVENT_MACRO_SUBSTITUTION (x5, x6)));} - - /*! */ - Direction_2 get_direction(Segment_2 seg) - { - typedef typename Traits_::Construct_vertex_2 Construct_vertex_2; - Construct_vertex_2 construct_vertex = m_gt.construct_vertex_2_object(); - - // force the segment slope to [0-180) - Point_2 s = construct_vertex(seg, 0); - Point_2 t = construct_vertex(seg, 1); - Comparison_result cx = m_gt.compare_x_2_object()(s, t); - Comparison_result cy = m_gt.compare_y_2_object()(s, t); - if (cy == LARGER || (cy == EQUAL && cx == LARGER)) { - typedef typename Traits::Construct_segment_2 Construct_segment_2; - Construct_segment_2 construct_seg = m_gt.construct_segment_2_object(); - seg = construct_seg(t, s); - } - - // force the vector to [0-90) - Vector_2 v(construct_vertex(seg, 0), construct_vertex(seg, 1)); - if (cx == EQUAL || (cx == LARGER && cy == SMALLER) || - (cx == SMALLER && cy == LARGER)) - v = v.perpendicular(RIGHT_TURN); - - Direction_2 d(v.direction()); - - return(d); - } - - /*! */ - int get_kd_num(Segment_2 seg, int n, Direction_list & directions) - { - Direction_2 d = get_direction(seg); - int i = 0; - bool found = false; - Direction_const_iter iter = directions.begin(); - - while (i < n && !found) { - if (*iter > d) found = true; - ++i; - ++iter; - } - - if (found) --i; - else i = 0; - - return(i); - } - - /*! */ - void check_kd(int *kd_counter,int number_of_trees, - const Segment_list & seg_list, - Direction_list & directions) - { - for (int i = 0;i < number_of_trees;++i) - kd_counter[i] = 0; - - int kd_num; - for (Segment_const_iter iter = seg_list.begin(); iter != seg_list.end(); - ++iter) - { - kd_num = get_kd_num(*iter, number_of_trees, directions); - kd_counter[kd_num]++; - } - } - - /*! */ - void init_angle_to_sines_table() - { - angle_to_sines_appr[0] = NT(0); - angle_to_sines_appr[1] = NT(115) / NT(6613); - angle_to_sines_appr[2] = NT(57) / NT(1625); - angle_to_sines_appr[3] = NT(39) / NT(761); - angle_to_sines_appr[4] = NT(29) / NT(421); - angle_to_sines_appr[5] = NT(23) / NT(265); - angle_to_sines_appr[6] = NT(19) / NT(181); - angle_to_sines_appr[7] = NT(32) / NT(257); - angle_to_sines_appr[8] = NT(129) / NT(929); - angle_to_sines_appr[9] = NT(100) / NT(629); - angle_to_sines_appr[10] = NT(92) / NT(533); - angle_to_sines_appr[11] = NT(93) / NT(485); - angle_to_sines_appr[12] = NT(76) / NT(365); - angle_to_sines_appr[13] = NT(156) / NT(685); - angle_to_sines_appr[14] = NT(205) / NT(853); - angle_to_sines_appr[15] = NT(69) / NT(269); - angle_to_sines_appr[16] = NT(7) / NT(25); - angle_to_sines_appr[17] = NT(120) / NT(409); - angle_to_sines_appr[18] = NT(57) / NT(185); - angle_to_sines_appr[19] = NT(12) / NT(37); - angle_to_sines_appr[20] = NT(51) / NT(149); - angle_to_sines_appr[21] = NT(135) / NT(377); - angle_to_sines_appr[22] = NT(372) / NT(997); - angle_to_sines_appr[23] = NT(348) / NT(877); - angle_to_sines_appr[24] = NT(231) / NT(569); - angle_to_sines_appr[25] = NT(36) / NT(85); - angle_to_sines_appr[26] = NT(39) / NT(89); - angle_to_sines_appr[27] = NT(300) / NT(661); - angle_to_sines_appr[28] = NT(8) / NT(17); - angle_to_sines_appr[29] = NT(189) / NT(389); - angle_to_sines_appr[30] = NT(451) / NT(901); - angle_to_sines_appr[31] = NT(180) / NT(349); - angle_to_sines_appr[32] = NT(28) / NT(53); - angle_to_sines_appr[33] = NT(432) / NT(793); - angle_to_sines_appr[34] = NT(161) / NT(289); - angle_to_sines_appr[35] = NT(228) / NT(397); - angle_to_sines_appr[36] = NT(504) / NT(865); - angle_to_sines_appr[37] = NT(3) / NT(5); - angle_to_sines_appr[38] = NT(580) / NT(941); - angle_to_sines_appr[39] = NT(341) / NT(541); - angle_to_sines_appr[40] = NT(88) / NT(137); - angle_to_sines_appr[41] = NT(48) / NT(73); - angle_to_sines_appr[42] = NT(65) / NT(97); - angle_to_sines_appr[43] = NT(429) / NT(629); - angle_to_sines_appr[44] = NT(555) / NT(797); - angle_to_sines_appr[45] = NT(697) / NT(985); - angle_to_sines_appr[46] = NT(572) / NT(797); - angle_to_sines_appr[47] = NT(460) / NT(629); - angle_to_sines_appr[48] = NT(72) / NT(97); - angle_to_sines_appr[49] = NT(55) / NT(73); - angle_to_sines_appr[50] = NT(105) / NT(137); - angle_to_sines_appr[51] = NT(420) / NT(541); - angle_to_sines_appr[52] = NT(741) / NT(941); - angle_to_sines_appr[53] = NT(4) / NT(5); - angle_to_sines_appr[54] = NT(703) / NT(865); - angle_to_sines_appr[55] = NT(325) / NT(397); - angle_to_sines_appr[56] = NT(240) / NT(289); - angle_to_sines_appr[57] = NT(665) / NT(793); - angle_to_sines_appr[58] = NT(45) / NT(53); - angle_to_sines_appr[59] = NT(299) / NT(349); - angle_to_sines_appr[60] = NT(780) / NT(901); - angle_to_sines_appr[61] = NT(340) / NT(389); - angle_to_sines_appr[62] = NT(15) / NT(17); - angle_to_sines_appr[63] = NT(589) / NT(661); - angle_to_sines_appr[64] = NT(80) / NT(89); - angle_to_sines_appr[65] = NT(77) / NT(85); - angle_to_sines_appr[66] = NT(520) / NT(569); - angle_to_sines_appr[67] = NT(805) / NT(877); - angle_to_sines_appr[68] = NT(925) / NT(997); - angle_to_sines_appr[69] = NT(352) / NT(377); - angle_to_sines_appr[70] = NT(140) / NT(149); - angle_to_sines_appr[71] = NT(35) / NT(37); - angle_to_sines_appr[72] = NT(176) / NT(185); - angle_to_sines_appr[73] = NT(391) / NT(409); - angle_to_sines_appr[74] = NT(24) / NT(25); - angle_to_sines_appr[75] = NT(260) / NT(269); - angle_to_sines_appr[76] = NT(828) / NT(853); - angle_to_sines_appr[77] = NT(667) / NT(685); - angle_to_sines_appr[78] = NT(357) / NT(365); - angle_to_sines_appr[79] = NT(476) / NT(485); - angle_to_sines_appr[80] = NT(525) / NT(533); - angle_to_sines_appr[81] = NT(621) / NT(629); - angle_to_sines_appr[82] = NT(920) / NT(929); - angle_to_sines_appr[83] = NT(255) / NT(257); - angle_to_sines_appr[84] = NT(180) / NT(181); - angle_to_sines_appr[85] = NT(264) / NT(265); - angle_to_sines_appr[86] = NT(420) / NT(421); - angle_to_sines_appr[87] = NT(760) / NT(761); - angle_to_sines_appr[88] = NT(1624) / NT(1625); - angle_to_sines_appr[89] = NT(6612) / NT(6613); - angle_to_sines_appr[90] = NT(1); - } - -public: - - /*! */ - Multiple_kd_tree(const Point_saved_pair_list & inp_points_list, - int inp_number_of_trees, - const Segment_list & seg_list) : - pi(3.1415), half_pi(1.57075), - number_of_trees(inp_number_of_trees), input_points_list(inp_points_list) - { - Kd_triple kd; - //WAQAR: - //Kd_triple_new kd_new; - - - // check that there are at least two trees - CGAL_precondition_msg(number_of_trees >= 1, "There must be at least one kd-tree" ); - - init_angle_to_sines_table(); - - // find the kd trees that have enough candidates (segments with a close - // slope) - int * kd_counter = new int[number_of_trees]; - int number_of_segments = seg_list.size(); - - // auxilary directions - Direction_list directions; - double buffer_angle; - Line_2 li; - Direction_2 d; - - int i = 0; - for (double angle = 0; i < number_of_trees; - angle += half_pi / number_of_trees,++i) - { - buffer_angle = angle - half_pi / (2 * number_of_trees); - if (buffer_angle < 0) buffer_angle = 0; - li = Line_2(std::tan(buffer_angle), -1, 0); - d = Direction_2(li); - // rotate_by 180 degrees - Transformation_2 t(ROTATION, 0, -1); - d = d.transform(t); - - directions.push_back(d); - } - - check_kd(kd_counter, number_of_trees, seg_list,directions); - int ind = 0; - -#ifdef CGAL_SR_DEBUG - int number_of_actual_kd_trees = 0; -#endif - i = 0; - for (NT angle = 0; i < number_of_trees; - angle += NT(half_pi / number_of_trees),++i) - { - if (kd_counter[ind] >= - (double)number_of_segments / (double)number_of_trees / 2.0) - { - kd = create_kd_tree(angle); - kd_trees_list.push_back(kd); - -#ifdef CGAL_SR_DEBUG - ++number_of_actual_kd_trees; -#endif - - } - - ++ind; - } - - delete[] kd_counter; - -#ifdef CGAL_SR_DEBUG - std::cout << "Actual number of kd-trees created : " << - number_of_actual_kd_trees << std::endl; -#endif - - } - - ~Multiple_kd_tree() { - for(typename Kd_triple_list::iterator it = kd_trees_list.begin(); - it != kd_trees_list.end(); ++it) { - delete (it->first); - } - - for(typename Point_saved_pair_list::iterator it = input_points_list.begin(); - it != input_points_list.end(); ++it) { - delete (it->second); - } - - } - - /*! */ - Point_2 small_x_point(const Point_2 & p1, const Point_2 & p2) - { - Comparison_result c = m_gt.compare_x_2_object()(p1, p2); - return (c == SMALLER) ? p1 : p2; - } - - /*! */ - Point_2 big_x_point(const Point_2 & p1, const Point_2 & p2) - { - Comparison_result c = m_gt.compare_x_2_object()(p1, p2); - return (c == SMALLER) ? p2 : p1; - } - - /*! */ - Point_2 small_y_point(const Point_2 & p1, const Point_2 & p2) - { - Comparison_result c = m_gt.compare_y_2_object()(p1, p2); - return (c == SMALLER) ? p1 : p2; - } - - /*! */ - Point_2 big_y_point(const Point_2 & p1, const Point_2 & p2) - { - Comparison_result c = m_gt.compare_y_2_object()(p1, p2); - return (c == SMALLER) ? p2 : p1; - } - - /*! */ - void get_intersecting_points(std::list & result_list, - Segment_2 s, NT unit_square) - { - // determine right kd-tree to work on, depending on the segment's slope - Direction_2 d = get_direction(s); - int i = 0; - int n = kd_trees_list.size(); - bool found = false; - typename Kd_triple_list::const_iterator iter = kd_trees_list.begin(); - - while(i < n && !found) { - if (iter->second.first > d) found = true; - ++i; - ++iter; - } - - if (!found) iter = kd_trees_list.begin(); - else --iter; - - Point_list points_list; - m_gt.minkowski_sum_with_pixel_2_object()(points_list, s, unit_square); - - Point_iter points_iter; - - for (points_iter = points_list.begin(); points_iter != points_list.end(); - ++points_iter) - rotate(*points_iter, iter->second.second); - - // query - points_iter = points_list.begin(); - Point_2 point_left,point_right,point_bot,point_top; - point_left = point_right = point_bot = point_top = *points_iter; - for (++points_iter; points_iter != points_list.end(); ++points_iter) { - point_left = small_x_point(point_left,*points_iter); - point_right = big_x_point(point_right,*points_iter); - point_bot = small_y_point(point_bot,*points_iter); - point_top = big_y_point(point_top,*points_iter); - } - - typedef typename Traits::Construct_iso_rectangle_2 - Construct_iso_rectangle_2; - Construct_iso_rectangle_2 construct_rec = - m_gt.construct_iso_rectangle_2_object(); - Iso_rectangle_2 rec = - construct_rec(point_left, point_right, point_bot, point_top); - - Point_2 p1 = rec.vertex(0); - Point_2 p2 = rec.vertex(2); - - My_point_saved point1(p1); - My_point_saved point2(p2); - - //Box b(point1, point2, 2); - //WAQAR: We use new Fuzzy box for new kd_trees. - Box b(point1, point2); - // CGAL::My_class obj(2); - // std::cout << "****(***: " << obj.a << std::endl; - - // the kd-tree query - My_point_saved_list res; - iter->first->search(std::back_inserter(res), b); - - std::cout<< "size of res: " << res.size() << std::endl; - //int count = 0; - // create result - result_list.empty(); - for (My_point_saved_iter my_point_iter = res.begin(); my_point_iter != res.end(); ++my_point_iter) - { - // std::cout<< "***: " << count << std::endl; - // count++; - result_list.push_back(my_point_iter->object); - std::cout<< "***: " << result_list.size() << std::endl; - } - } -}; - -} //namespace CGAL - -#endif diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h index bc4d419e17e..9ac8530e9af 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h @@ -1,22 +1,24 @@ -// Copyright (c) 2001 Tel-Aviv University (Israel). -// All rights reserved. +// Copyright (c) 1999 +// Utrecht University (The Netherlands), +// ETH Zurich (Switzerland), +// INRIA Sophia-Antipolis (France), +// Max-Planck-Institute Saarbruecken (Germany), +// and Tel-Aviv University (Israel). All rights reserved. // -// This file is part of CGAL (www.cgal.org). -// 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 3 of the License, or (at your option) any later version. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ -// // -// author(s) : Eli Packer +// +// author(s) : Eli Packer , Waqar Khan + #ifndef CGAL_SNAP_ROUNDING_2_TRAITS_H #define CGAL_SNAP_ROUNDING_2_TRAITS_H diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index 8c8f22ce683..a8549b9013a 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -1,22 +1,22 @@ -// Copyright (c) 2002,2011 Utrecht University (The Netherlands). -// All rights reserved. +// Copyright (c) 1999 +// Utrecht University (The Netherlands), +// ETH Zurich (Switzerland), +// INRIA Sophia-Antipolis (France), +// Max-Planck-Institute Saarbruecken (Germany), +// and Tel-Aviv University (Israel). All rights reserved. // -// This file is part of CGAL (www.cgal.org). -// 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 3 of the License, or (at your option) any later version. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ -// // -// Author(s) : Hans Tangelder () +// Author(s) : Hans Tangelder (), Waqar Khan #ifndef CGAL_KD_TREE_H #define CGAL_KD_TREE_H @@ -267,7 +267,8 @@ public: pts.insert(pts.end(),first, beyond); } - void reserve_to_capacity(size_t size) + //For efficiency; reserve the size of the points vectors in advance (if the number of points is already known). + void reserve(size_t size) { pts.reserve(size); } From 1c355ef42afd4a94571940cd5d85c797b9628d7b Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Fri, 7 Mar 2014 16:13:44 +0100 Subject: [PATCH 15/27] 1. "Snap_rounding_traits_2.h" "Snap_rounding_kd_2.h" "Kd_tree.h" "snap_rounding_data.cpp", Changed the File header signature to match the templated signatures. 2. Made the required changes in the concepts i.e. in "Snap_rounding_2/doc/Snap_rounding_2/Concepts/SnapRoundingTraits_2.h" 3. Added documentation for the reserve() method of Kd_tree 4. Deleted the un maintained olk kd_tree package --- .../Concepts/SnapRoundingTraits_2.h | 20 + .../Snap_rounding_2/snap_rounding_data.cpp | 22 +- .../include/CGAL/Snap_rounding_kd_2.h | 23 +- .../include/CGAL/Snap_rounding_traits_2.h | 26 +- .../doc/Spatial_searching/CGAL/Kd_tree.h | 6 + Spatial_searching/include/CGAL/Kd_tree.h | 25 +- kdtree/dont_submit | 1 - kdtree/examples/kdtrees/example1.cin | 5 - kdtree/examples/kdtrees/example1.cpp | 80 -- kdtree/examples/kdtrees/example2.cpp | 93 -- kdtree/examples/kdtrees/example3.cpp | 155 --- kdtree/examples/kdtrees/example4.cpp | 86 -- kdtree/include/CGAL/kdtree_d.h | 1060 ----------------- kdtree/package_info/kdtree/copyright | 1 - kdtree/package_info/kdtree/description.txt | 1 - kdtree/package_info/kdtree/license.txt | 1 - .../package_info/kdtree/long_description.txt | 11 - kdtree/package_info/kdtree/maintainer | 1 - 18 files changed, 75 insertions(+), 1542 deletions(-) delete mode 100644 kdtree/dont_submit delete mode 100644 kdtree/examples/kdtrees/example1.cin delete mode 100644 kdtree/examples/kdtrees/example1.cpp delete mode 100644 kdtree/examples/kdtrees/example2.cpp delete mode 100644 kdtree/examples/kdtrees/example3.cpp delete mode 100644 kdtree/examples/kdtrees/example4.cpp delete mode 100644 kdtree/include/CGAL/kdtree_d.h delete mode 100644 kdtree/package_info/kdtree/copyright delete mode 100644 kdtree/package_info/kdtree/description.txt delete mode 100644 kdtree/package_info/kdtree/license.txt delete mode 100644 kdtree/package_info/kdtree/long_description.txt delete mode 100644 kdtree/package_info/kdtree/maintainer diff --git a/Snap_rounding_2/doc/Snap_rounding_2/Concepts/SnapRoundingTraits_2.h b/Snap_rounding_2/doc/Snap_rounding_2/Concepts/SnapRoundingTraits_2.h index a4f44c9912c..ef9cbdd51d1 100644 --- a/Snap_rounding_2/doc/Snap_rounding_2/Concepts/SnapRoundingTraits_2.h +++ b/Snap_rounding_2/doc/Snap_rounding_2/Concepts/SnapRoundingTraits_2.h @@ -41,6 +41,11 @@ Models the concept `SRTraits_2::IsoRectangle_2` */ typedef unspecified_type Iso_rectangle_2; +/*! +Models the concept `SearchTraits::Cartesian_const_iterator_2` +*/ +typedef unspecified_type Cartesian_const_iterator_2; + /// @} /// \name Functor Types @@ -93,6 +98,21 @@ Models the concept `SRTraits_2::MinkowskiSumWithPixel_2`. */ typedef unspecified_type Minkowski_sum_with_pixel_2; +/*! +Models the concept `ArrTraits::ConstructMinVertex_2`. +*/ +typedef unspecified_type Construct_min_vertex_2; + +/*! +Models the concept `ArrTraits::ConstructMaxVertex_2`. +*/ +typedef unspecified_type Construct_max_vertex_2; + +/*! +Models the concept `SearchTraits::Construct_cartesian_const_iterator_2`. +*/ +typedef unspecified_type Construct_cartesian_const_iterator_2; + /// @} /// \name Accessing Functor Objects diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index 52a27bd492d..73354ee89b7 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -1,21 +1,21 @@ -// Copyright (c) 1999 -// Utrecht University (The Netherlands), -// ETH Zurich (Switzerland), -// INRIA Sophia-Antipolis (France), -// Max-Planck-Institute Saarbruecken (Germany), -// and Tel-Aviv University (Israel). All rights reserved. +// Copyright 2009,2014 Max-Planck-Institute Saarbruecken (Germany). +// All rights reserved. // -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. +// This file is part of CGAL (www.cgal.org). +// 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 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ -// +// +// // author(s) : Waqar Khan diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index 9c43b6eb466..feaa2636430 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -1,14 +1,14 @@ -// Copyright (c) 1999 -// Utrecht University (The Netherlands), -// ETH Zurich (Switzerland), -// INRIA Sophia-Antipolis (France), -// Max-Planck-Institute Saarbruecken (Germany), -// and Tel-Aviv University (Israel). All rights reserved. +// Copyright (c) 2001 Tel-Aviv University (Israel). +// 2009,2014 Max-Planck-Institute Saarbruecken (Germany). +// All rights reserved. // -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. +// This file is part of CGAL (www.cgal.org). +// 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 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. @@ -17,7 +17,8 @@ // $Id$ // // -// author(s) : Eli Packer , Waqar Khan +// author(s) : Eli Packer , +// Waqar Khan #ifndef CGAL_SNAP_ROUNDING_KD_2_H #define CGAL_SNAP_ROUNDING_KD_2_H diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h index 9ac8530e9af..b475ad5dedf 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h @@ -1,23 +1,24 @@ -// Copyright (c) 1999 -// Utrecht University (The Netherlands), -// ETH Zurich (Switzerland), -// INRIA Sophia-Antipolis (France), -// Max-Planck-Institute Saarbruecken (Germany), -// and Tel-Aviv University (Israel). All rights reserved. +// Copyright (c) 2001 Tel-Aviv University (Israel). +// 2009,2014 Max-Planck-Institute Saarbruecken (Germany). +// All rights reserved. // -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. +// This file is part of CGAL (www.cgal.org). +// 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 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ +// // -// -// author(s) : Eli Packer , Waqar Khan +// author(s) : Eli Packer , +// Waqar Khan #ifndef CGAL_SNAP_ROUNDING_2_TRAITS_H #define CGAL_SNAP_ROUNDING_2_TRAITS_H @@ -201,7 +202,6 @@ public: return k.construct_iso_rectangle_2_object(); } - }; } //namespace CGAL diff --git a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h index 7303ff40aa0..e182425602b 100644 --- a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h +++ b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h @@ -105,6 +105,12 @@ The value type of the `InputIterator` must be `Point_d`. */ template void insert(InputIterator first, InputIterator beyond); + +/* +Reserve the memory for the 'Points' vector to the 'size' +*/ +void reserve(size_t size); + /*! Reports the points that are approximately contained by `q`. The types `FuzzyQueryItem::Point_d` and `Point_d` must be equivalent. diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index a8549b9013a..bbd62f1f310 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -1,22 +1,23 @@ -// Copyright (c) 1999 -// Utrecht University (The Netherlands), -// ETH Zurich (Switzerland), -// INRIA Sophia-Antipolis (France), -// Max-Planck-Institute Saarbruecken (Germany), -// and Tel-Aviv University (Israel). All rights reserved. +// Copyright (c) 2002,2011 Utrecht University (The Netherlands). +// 2009,2014 Max-Planck-Institute Saarbruecken (Germany) +// All rights reserved. // -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. +// This file is part of CGAL (www.cgal.org). +// 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 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ -// -// Author(s) : Hans Tangelder (), Waqar Khan +// +// Author(s) : Hans Tangelder (), +// : Waqar Khan #ifndef CGAL_KD_TREE_H #define CGAL_KD_TREE_H diff --git a/kdtree/dont_submit b/kdtree/dont_submit deleted file mode 100644 index 1e107f52e47..00000000000 --- a/kdtree/dont_submit +++ /dev/null @@ -1 +0,0 @@ -examples diff --git a/kdtree/examples/kdtrees/example1.cin b/kdtree/examples/kdtrees/example1.cin deleted file mode 100644 index ba884a49aa2..00000000000 --- a/kdtree/examples/kdtrees/example1.cin +++ /dev/null @@ -1,5 +0,0 @@ -3 -3 -7 -7 - diff --git a/kdtree/examples/kdtrees/example1.cpp b/kdtree/examples/kdtrees/example1.cpp deleted file mode 100644 index af45b7f6b66..00000000000 --- a/kdtree/examples/kdtrees/example1.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 1997 Tel-Aviv University (Israel). -// All rights reserved. -// -// This file is part of an example program for CGAL. This example -// program may be used, distributed and modified without limitation. -// - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* - * example1.C - - * Simple example the CGAL KD-tree module. - * - * Written by Sariel Har-Peled - * Iddo Hanniel -\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -#include - -#include -#include -#include -#include -#include -#include - -#include - -typedef CGAL::Cartesian K; -typedef K::Point_2 point; -typedef CGAL::Kdtree_interface_2d kd_interface; -typedef CGAL::Kdtree_d kd_tree; -typedef kd_tree::Box box; -typedef std::list points_list; - -int main() -{ - CGAL::Kdtree_d tree(2); - points_list l, res; - - std::srand( (unsigned)time(NULL) ); - - std::cout << "Insering evenly 81 points in the square (0,0)-(10,10) ...\n\n"; - for (int i=1; i<10; i++) - for (int j=1; j<10; j++) - { - point p(i,j); - l.push_front(p); - } - - // building the tree - tree.build( l ); - - // checking validity - if ( ! tree.is_valid() ) - tree.dump(); - assert( tree.is_valid() ); - - // Defining and searching the box r - double lx,ly,rx,ry; - std::cout << "Define your query square.\nEnter left x coordinate: " ; - std::cin >> lx ; - std::cout << "Enter left y coordinate: "; - std::cin >> ly; - std::cout << "Enter right x coordinate: " ; - std::cin >> rx ; - std::cout << "Enter right y coordinate: "; - std::cin >> ry; - std::cout << std::endl; - - box r(point(lx,ly), point(rx,ry) ,2); - - tree.search( std::back_inserter( res ), r ); - - std::cout << "Listing of the points in the square: \n" ; - std::copy (res.begin(),res.end(),std::ostream_iterator(std::cout," \n") ); - std::cout << std::endl; - - tree.delete_all(); - - return 0; -} diff --git a/kdtree/examples/kdtrees/example2.cpp b/kdtree/examples/kdtrees/example2.cpp deleted file mode 100644 index 8584c14eb3a..00000000000 --- a/kdtree/examples/kdtrees/example2.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) 1997 Tel-Aviv University (Israel). -// All rights reserved. -// -// This file is part of an example program for CGAL. This example -// program may be used, distributed and modified without limitation. -// - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* - * example2.C - - * Simple example the CGAL KD-tree module. - * - * Written by Sariel Har-Peled - * Iddo Hanniel -\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -#include - -#include -#include -#include -#include -#include -#include - -#include - -typedef CGAL::Cartesian K; -typedef K::Point_3 point; -typedef CGAL::Kdtree_interface_3d kd_interface; -typedef CGAL::Kdtree_d kd_tree; -typedef kd_tree::Box box; -typedef std::list points_list; - -//RANDOM FUNCTIONS -// dblRand - a random number between 0..1 -#ifndef RAND_MAX -#define RAND_MAX 0x7fffffff -#endif - -inline double dblRand( void ) -{ - return (double)std::rand() / (double)RAND_MAX; -} - -void random_points( int num, points_list &l ) -{ - double x,y,z; - - for (int j = 0; j < num; j++) - { - x = dblRand()*10 ; - y = dblRand()*10 ; - z = dblRand()*10 ; - point p(x,y,z); - l.push_front(p); - } -} - -int main() -{ - CGAL::Kdtree_d tree(3); - - std::srand( (unsigned)time(NULL) ); - - std::cout << "Choosing randomly 30 points in the cube (0,0,0)-(10,10,10)\n" ; - - points_list l , res; - random_points( 30, l); - - std::cout << "Listing of random points:\n" ; - std::copy (l.begin(),l.end(),std::ostream_iterator(std::cout,"\n") ); - std::cout << std::endl; - - // Building the tree for the random points - tree.build( l ); - - // Checking validity - if ( ! tree.is_valid() ) - tree.dump(); - assert( tree.is_valid() ); - - // Searching the box r - box r(point(2,2,2), point(7,7,7) ,3); - tree.search( std::back_inserter( res ), r ); - - std::cout << "Listing of the points in the box (2,2,2)-(7,7,7) : \n" ; - std::copy (res.begin(),res.end(),std::ostream_iterator(std::cout,"\n") ); - std::cout << std::endl; - - tree.delete_all(); - - return 0; -} diff --git a/kdtree/examples/kdtrees/example3.cpp b/kdtree/examples/kdtrees/example3.cpp deleted file mode 100644 index 13b2f6509e3..00000000000 --- a/kdtree/examples/kdtrees/example3.cpp +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright (c) 1997 Tel-Aviv University (Israel). -// All rights reserved. -// -// This file is part of an example program for CGAL. This example -// program may be used, distributed and modified without limitation. -// - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* - * example3.C - - * Simple example the CGAL KD-tree module. - * Example with user defined point_d. - * - * Written by Sariel Har-Peled - * Iddo Hanniel -\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -#include - -#include -#include -#include -#include -#include -#include - -#include - -template -class Point_float_d -{ -private: - double vec[ DIM ]; - -public: - Point_float_d() - { - for ( int ind = 0; ind < DIM; ind++ ) - vec[ ind ] = 0; - } - - int dimension() const - { - return DIM; - } - -//not essential by specification but needed for initializing a general d-point - void set_coord(int k, double x) - { - assert( 0 <= k && k < DIM ); - vec[ k ] = x; - } - - double & operator[](int k) - { - assert( 0 <= k && k < DIM ); - return vec[ k ]; - } - - double operator[](int k) const - { - assert( 0 <= k && k < DIM ); - return vec[ k ]; - } -}; - -// not essential by specification but nice to have -template -std::ostream &operator<<(std::ostream &os, const Point_float_d &p) -{ - std::cout << "("; - for(int i = 0; i < DIM; i++) - { - std::cout << p[i] ; - if (i < p.dimension() - 1) std::cout << ", "; - } - std::cout << ")"; - return os; -} - -typedef Point_float_d<4> point; -typedef CGAL::Kdtree_interface kd_interface; -typedef CGAL::Kdtree_d kd_tree; -typedef kd_tree::Box box; -typedef std::list points_list; - -//RANDOM FUNCTIONS -// dblRand - a random number between 0..1 -#ifndef RAND_MAX -#define RAND_MAX 0x7fffffff -#endif - -inline double dblRand( void ) -{ - return (double)std::rand() / (double)RAND_MAX; -} - -void random_points( int num, points_list &l, int DIM) -{ - double x; - - for (int j = 0; j < num; j++) - { - point p; - for (int i=0; i tree(3); - - std::srand( (unsigned)time(NULL) ); - - std::cout << "Choosing randomly 30 points in the cube (0,0,0)-(10,10,10)\n" ; - - points_list l , res; - random_points( 30, l , 4); - - std::cout << "Listing of random points:\n" ; - std::copy (l.begin(),l.end(),std::ostream_iterator(std::cout,"\n") ); - std::cout << std::endl; - - // Building the tree for the random points - tree.build( l ); - - // Checking validity - if ( ! tree.is_valid() ) - tree.dump(); - assert( tree.is_valid() ); - - // Searching the box r - point p,q; - for (int k=0;k<4;k++) - { - p.set_coord(k,2); - q.set_coord(k,8); - } - - box r(p, q, 4); - tree.search( std::back_inserter( res ), r ); - - std::cout << "Listing of the points in the box (2,2,2,2)-(8,8,8,8) : \n" ; - std::copy (res.begin(),res.end(), - std::ostream_iterator(std::cout,"\n") ); - std::cout << std::endl; - - tree.delete_all(); - - return 0; -} diff --git a/kdtree/examples/kdtrees/example4.cpp b/kdtree/examples/kdtrees/example4.cpp deleted file mode 100644 index bc2fc9b0fea..00000000000 --- a/kdtree/examples/kdtrees/example4.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) 1997 Tel-Aviv University (Israel). -// All rights reserved. -// -// This file is part of an example program for CGAL. This example -// program may be used, distributed and modified without limitation. -// - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* - * example2.C - bench mark - * Simple example the CGAL KD-tree module. - * - * Written by Sariel Har-Peled - * Iddo Hanniel -\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -typedef CGAL::Cartesian K; -typedef K::Point_3 Point; -typedef CGAL::Kdtree_interface_3d kd_interface; -typedef CGAL::Kdtree_d kd_tree; -typedef kd_tree::Box box; -typedef std::list points_list; - -int main() -{ - CGAL::Kdtree_d tree(3); - CGAL::Timer t; - const int dim=3; - - // const int data_point_number=1000000; - const int data_point_number=10000; - - - - - - - typedef std::list point_list; - point_list data_points,res; - - // get data points - - // add random points of dimension dim to data_points - CGAL::Random Rnd; - // std::cout << "started tstrandom()" << std::endl; - for (int i1=0; i1 -#include -#include -#include -using std::list; // to avoid compiler crash on MSVC++ - -namespace CGAL { - -/*======================================================================= - * Kdtree_interface - - * This is the default interface of point. It assume that PT (the - * point type have the following properties: - * default constructor - * int dimension() - * const coord_type & operator[]( int ) const - * ... operator=( const Pt & ) - copy operator -\*=======================================================================*/ - -template -class Kdtree_interface -{ -public: - typedef PT Point; - - static int dimension( const PT & pnt ) - { - // return pnt.dimensions(); - return pnt.dimension(); - } - - static int compare( int d, const PT & a, const PT & b ) - { - if ( a[ d ] < b[ d ] ) - return -1; - if ( a[ d ] > b[ d ] ) - return 1; - - return 0; - } - static void copy_coord( int d, PT & a, const PT & b ) - { - a[ d ] = b[ d ]; - } -}; - - - -template -class Kdtree_interface_2d -{ -public: - typedef PT Point; - - static int dimension( const PT & pnt ) - { - // return pnt.dimensions(); - return pnt.dimension(); - } - - static int compare( int d, const PT & a, const PT & b ) - { - if ( a[ d ] < b[ d ] ) - return -1; - if ( a[ d ] > b[ d ] ) - return 1; - - return 0; - } - static void copy_coord( int d, PT & a, const PT & b ) - { - if ( d == 0 ) - a = PT( b[ 0 ], a[1] ); - else - if ( d == 1 ) - a = PT( a[ 0 ], b[1] ); - else { - CGAL_error(); - } - } -}; - - -template -class Kdtree_interface_3d -{ -public: - typedef PT Point; - - static int dimension( const PT & pnt ) - { - // return pnt.dimensions(); - return pnt.dimension(); - } - - static int compare( int d, const PT & a, const PT & b ) - { - if ( a[ d ] < b[ d ] ) - return -1; - if ( a[ d ] > b[ d ] ) - return 1; - - return 0; - } - static void copy_coord( int d, PT & a, const PT & b ) - { - if ( d == 0 ) - a = PT( b[ 0 ], a[1], a[ 2 ] ); - else - if ( d == 1 ) - a = PT( a[ 0 ], b[1], a[ 2 ] ); - else - if ( d == 2 ) - a = PT( a[ 0 ], a[1], b[ 2 ] ); - else { - CGAL_error(); - } - } -}; - - -/*========================================================================= - * kdtree_d - - * A the kdtree class. - * - * Remark: The kd-trees allocates all the memory it needs in advance, - * This results in a rather efficient memory management, and a fast - * iplementation. -\*=========================================================================*/ -template -class Kdtree_d -{ - class Plane; - -public: - typedef typename Traits::Point Point; - typedef list List_points; - - - //------------------------------------------------------------------------- - // Extended_Point_d - - // A class for representing an extended d-dimal point: with - // ability to support +/- infinity. Templated by the kd-treee interface - // type. - //------------------------------------------------------------------------- - class ExtPoint - { - private: - class coordinate_type - { - public: - const Point * p_pnt; - int type; - //signed char type; - }; - - coordinate_type * p_arr; - int dim; - Point def_pnt; - - void init( int _dim ) - { - CGAL_precondition( _dim > 0 ); - dim = _dim; - - p_arr = (coordinate_type *)std::malloc( sizeof( coordinate_type ) - * dim ); - //printf( "p_arr(new): %p\n", (void *)p_arr ); - CGAL_assertion( p_arr != NULL ); - - std::memset( p_arr, 0, sizeof( coordinate_type ) * dim ); - } - - public: - enum { MINUS_INFINITY = -1, FINITE = 0, PLUS_INFINITY = 1 }; - - ExtPoint( int _type, int _dim ) - { - CGAL_precondition( _type == MINUS_INFINITY || _type == PLUS_INFINITY ); - init( _dim ); - - for ( int ind = 0; ind < dim; ind++ ) - p_arr[ ind ].type = _type; - } - - ExtPoint() - { - p_arr = NULL; - dim = -1; - } - - ExtPoint( const ExtPoint & p ) - { - init( p.dim ); - - def_pnt = p.def_pnt; - for ( int ind = 0; ind < dim; ind++ ) { - p_arr[ ind ] = p.p_arr[ ind ]; - if ( p.p_arr[ ind ].p_pnt == &p.def_pnt ) - p_arr[ ind ].p_pnt = &def_pnt; - } - } - - ExtPoint & operator=( const ExtPoint & p ) - { - term(); - - init( p.dim ); - - def_pnt = p.def_pnt; - for ( int ind = 0; ind < dim; ind++ ) { - p_arr[ ind ] = p.p_arr[ ind ]; - if ( p.p_arr[ ind ].p_pnt == &p.def_pnt ) - p_arr[ ind ].p_pnt = &def_pnt; - } - - return *this; - } - - ExtPoint( const Point & point, int _dim ) - { - init( _dim ); - - def_pnt = point; - for ( int ind = 0; ind < _dim; ind++ ) { - p_arr[ ind ].p_pnt = &def_pnt; - p_arr[ ind ].type = FINITE; - } - } - - void term() - { - if ( p_arr != NULL ) { - //printf( "a: %p\n", p_arr ); - std::free( p_arr ); - //printf( "_a\n" ); - p_arr = NULL; - } - dim = 0; - } - - ~ExtPoint() - { - term(); - } - - void set_coord( int k, Point & point ) - { - CGAL_precondition( 0 <= k && k < dim ); - - p_arr[ k ].type = FINITE; - //p_arr[ k ].p_pnt = &point; - - Traits::copy_coord( k, def_pnt, point ); - p_arr[ k ].p_pnt = &def_pnt; - } - - void set_coord( int k, const ExtPoint & point ) - { - CGAL_precondition( 0 <= k && k < dim ); - CGAL_precondition( 0 <= k && k < point.dim ); - - p_arr[ k ] = point.p_arr[ k ]; - if ( p_arr[ k ].type == FINITE ) { - Traits::copy_coord( k, def_pnt, *(p_arr[ k ].p_pnt) ); - p_arr[ k ].p_pnt = &def_pnt; - } - } - - int compare( int k, const ExtPoint & point ) const - { - CGAL_precondition( 0 <= k && k < dim ); - // the following does not compile on msvc++... - // coordinate_type & a( p_arr[ k ] ), - // & b( point.p_arr[ k ] ); - coordinate_type & a = p_arr[ k ]; - coordinate_type & b = point.p_arr[ k ]; - - if ( a.type != FINITE ) { - if ( b.type != FINITE ) { - return a.type - b.type; - } else { - return a.type; - } - } else { - if ( b.type != FINITE ) - return -b.type; - else - return Traits::compare( k, *a.p_pnt, *b.p_pnt ); - } - } - - - int compare_vector( const ExtPoint & point ) const - { - int ind, res; - - for ( ind = 0; ind < dim; ind++ ) { - res = compare( ind, point ); - if ( res != 0 ) - return res; - } - - return 0; - } - - int compare( int k, const Point & point ) const - { - CGAL_precondition( 0 <= k && k < dim ); - - // coordinate_type & a( p_arr[ k ] ); - coordinate_type & a = p_arr[ k ]; - - if ( a.type != FINITE ) - return a.type; - else - return Traits::compare( k, *a.p_pnt, point ); - } - - int dimension() const - { - return dim; - } - - int get_coord_status( int d ) const - { - CGAL_precondition( 0 <= d && d < dim ); - - return p_arr[ d ].type; - } - - - const Point * get_coord_point( int d ) const - { - CGAL_precondition( 0 <= d && d < dim ); - - return p_arr[ d ].p_pnt; - } - }; - - - // Box - represents an axis parallel box. - class Box - { - public: - int dim; - ExtPoint left, right; - - private: - friend class Plane; - - ExtPoint & get_vertex( bool f_left ) - { - return f_left? left : right; - } - - public: - // constructors - - //CHECK - Box() - { - } - - Box( const Box & box ) - { - dim = box.dim; - left = box.left; - right = box.right; - } - - - Box( const Point &l, const Point &r, int _dim ) - { - dim = _dim; - left = ExtPoint( l, dim ); - right = ExtPoint( r, dim ); - } - - Box( int _dim ) : left( ExtPoint::MINUS_INFINITY, _dim ), - right( ExtPoint::PLUS_INFINITY, _dim ) - { - dim = _dim; - } - - - // data access - void set_left( Point &l) - { - left = ExtPoint( l, dim ); - }; - - void set_right( Point &r) - { - right = ExtPoint( r, dim ); - } - - const ExtPoint &get_left() const - { - return left; - } - - const ExtPoint &get_right() const - { - return right; - } - - - void set_coord_left( int k, Point & p ) - { - left.set_coord( k, p ); - } - - - void set_coord_right( int k, Point & p ) - { - right.set_coord( k, p ); - } - - - // operations - bool is_in( const Box &o) const - // checks if o is completely inside - { - int dim = left.dimension(); - - for (int i = 0; i < dim; i++) - { - if ( (left.compare(i, o.get_left()) > 0 ) - || (right.compare(i, o.get_right()) < 0 ) ) - return false; - } - - return true; - } - - - bool is_in( const Point & o ) const - // checks if o is completely inside - { - int _dim = left.dimension(); - for (int i = 0; i < _dim; i++) - { - if ( (left.compare( i, o ) > 0 ) || - (right.compare( i, o ) <= 0 ) ) - return false; - } - return true; - } - - - bool is_coord_in_range( int k, const Point & o ) const - // checks if o is completely inside - { - return ( ! ( (left.compare( k, o ) > 0 ) - || (right.compare( k, o ) <= 0 ) ) ); - } - - - bool is_intersect( const Box &o) const - // checks if there is an intersection between o and this - { - int dim = left.dimension(); - for (int i = 0; i < dim; i++) - { - if ( (left.compare(i, o.get_right()) >= 0) || - (right.compare(i, o.get_left()) <= 0) ) - return false; - } - return true; - } - - - // checks if there is an intersection between o and this box - // only in a specific coordinate... - bool is_intersect_in_dim( int d, const Box & o ) const - { - return (! ( (left.compare( d, o.get_right() ) >= 0 ) - || (right.compare( d, o.get_left() ) <= 0) )); - } - - - // checks if there is an intersection between o and this box - // only in a specific coordinate... - bool is_intersect_in_dim_closed( int d, const Box & o ) const - { - return (! ( (left.compare( d, o.get_right() ) > 0 ) - || (right.compare( d, o.get_left() ) < 0) )); - } - - - bool intersect(Box &o) - // intersects this with o. the intersection will be in this - // returns false if intersection is empty - { - int dim = left.dimension(); - for (int i = 0; i < dim; i++) - { - // left is the maximal of the lefts - if (left.compare(i, o.get_left()) == -1) - left.set_coord(i, o.get_left()); - - // right is the minimal of the rights - if (right.compare(i, o.get_right()) == 1) - right.set_coord(i, o.get_right()); - } - return !(is_empty()); - } - - bool is_empty() const - // return true if this is not an interval (left[k] > right[k]) - { - int dim = left.dimension(); - for (int i = 0; i < dim; i++) - { - if (left.compare(i, right) == 1) - return true; - } - return false; - } - - bool is_empty_open() const - // return true if this is not an interval (left[k] > right[k]) - { - int dim = left.dimension(); - for (int i = 0; i < dim; i++) - { - if ( left.compare(i, right) >= 0 ) - return true; - } - return false; - } - - int comp( const Box & o ) const - { - int res; - - res = left.compare_vector( o.left ); - if ( res != 0 ) - return res; - - return right.compare_vector( o.right ); - } - // destructor - needed in ...recursive ... DVP - - ~Box() - { - left.term(); - right.term(); - dim = 0; - } - - }; - - -private: - class Plane - { - private: - int coord; - Point * normal; - bool f_plus; // orientation of half space - // is (0, 0, ... , +inifinity, 0, ..., 0) inside plane - - public: - Plane() - { - normal = NULL; - coord = 0; - } - - Plane( int k, Point & p ) - { - normal = &p; - coord = k; - } - - Plane( const Plane & p ) - { - coord = p.coord; - normal = p.normal; - } - - void dump( void ) - { - std::cout << "(" << coord << ": " << *normal << ")"; - } - - bool is_in( const Point & p ) const - { - int cmp; - - cmp = Traits::compare( coord, p, *normal ); - - if ( ! f_plus ) - cmp = -cmp; - - return cmp >= 0; - } - - void set_plane(int k, Point &p) - { - coord = k; - normal = &p; - //normal->copy( coord, p ); - } - - void split( Box & region, bool f_neg ) - { - ExtPoint * p_p = &(region.get_vertex( ! f_neg )); - - if ( f_neg ) { - if ( p_p->compare( coord, *normal ) > 0 ) - p_p->set_coord( coord, *normal ); - } else - if ( p_p->compare( coord, *normal ) < 0 ) - p_p->set_coord( coord, *normal ); - } - - void orient_half_space( bool f_neg_side ) - { - f_plus = ! f_neg_side; - } - - int get_coord() const - { - return coord; - } - }; - -private: - class Node - { - public: - Plane plane; - Point * pnt; - - Node * left, * right; - - enum { LEFT, RIGHT }; - - const Plane & get_hs( int side ) const - { - - ((Plane *)&plane)->orient_half_space( side == LEFT ); - - return plane; - } - - - bool is_points_in_hs( const Plane & pl ) const - { - if ( is_point() ) - return pl.is_in( *pnt ); - - if ( left != NULL && ( ! left->is_points_in_hs( pl ) ) ) - return false; - if ( right != NULL && ( ! right->is_points_in_hs( pl ) ) ) - return false; - - return true; - } - - - bool is_valid() const - { - if ( is_point() ) - return true; - - if ( left != NULL ) - if ( ! left->is_points_in_hs( get_hs( LEFT ) ) ) - return false; - if ( right != NULL ) - if ( ! right->is_points_in_hs( get_hs( RIGHT ) ) ) - return false; - - return true; - } - - - void dump( int depth ) - { - int ind; - - for ( ind = 0; ind < depth; ind++ ) - std::cout << " "; - - if ( is_point() ) { - std::cout << *pnt << "\n"; - return; - } - - plane.dump(); - std::cout << "\n"; - left->dump( depth + 1 ); - for ( ind = 0; ind < depth; ind++ ) - std::cout << " "; - - std::cout << "!!!!!!!!!!!!\n"; - right->dump( depth + 1 ); - } - - bool is_point() const - { - return ((left == NULL) && (right == NULL)); - } - - typedef std::back_insert_iterator back_iter; - - Node() : plane() - { - left = right = NULL; - } - - void copy_subtree_points( back_iter & result, - const Box & rect ) - { - if ( is_point() ) { - if ( rect.is_in( *pnt ) ) - (*result++) = *pnt; - return; - } - if ( left != NULL ) - left->copy_subtree_points( result, rect ); - if ( right != NULL ) - right->copy_subtree_points( result, rect ); - } - - static void search_recursive( back_iter & result, - Node * node, - const Box & rect, - Box & _region, - Plane & plane, - bool f_split_plus ) - { - //printf( "search_recusrive\n" ); - Box * p_r = new Box( _region ); - - //printf( "z" ); - //fflush( stdout ); - - plane.split( *p_r, f_split_plus ); - - //printf( "c" ); - //fflush( stdout ); - CGAL_precondition( node != NULL ); - - //printf( "b" ); - //fflush( stdout ); - if ( rect.is_in( *p_r ) ) - { - //printf( "5" ); - //fflush( stdout ); - node->copy_subtree_points( result, rect ); - //printf( "\tsearch_recursive done...\n" ); - //printf( "6" ); - //fflush( stdout ); - delete p_r; - return; - } - - //printf( "v" ); - //fflush( stdout ); - - if ( rect.is_intersect_in_dim_closed( plane.get_coord(), *p_r ) ) - node->search( result, rect, *p_r ); - //printf( "x" ); - //fflush( stdout ); - delete p_r; - } - - void search( std::back_insert_iterator result, - const Box &rect, Box ®ion ) - { - if (is_point()) { - if ( rect.is_in( *pnt ) ) - (*result++) = *pnt; - return; - } - - //this is not a point so it is a hypeplane - if ( left != NULL ) - search_recursive( result, left, rect, - region, plane, true ); - if ( right != NULL ) - search_recursive( result, right, rect, - region, plane, false ); - } - }; - - typedef Point * Point_ptr; - - int size; - Point * p_arr_pt; - Node *root; - int dim; - - Node * p_node_arr; - int node_count; - - Node * malloc_node( void ) - { - Node * p_ret; - - p_ret = &(p_node_arr[ node_count ]); - node_count++; - - CGAL_assertion( node_count <= ( 2 * size )); - - *p_ret = Node(); - - return p_ret; - } - - static int comp( const Point & a, const Point & b, int dim ) - { - return Traits::compare( dim, a, b ); - } - - static int partition( Point_ptr * arr, int left, int right, - Point * p_pivot, int dim ) - { - int i, j; - Point_ptr tmp; - - if ( left >= right ) - return left; - - i = left; - j = right; - - while ( i < j ) { - if ( comp( *(arr[ i ]), *(arr[ j ]), dim ) > 0 ) { - tmp = arr[ i ]; - arr[ i ] = arr[ j ]; - arr[ j ] = tmp; - } - if ( comp( *(arr[ i ]), *p_pivot, dim ) < 0 ) { - i++; - } else - if ( comp( *p_pivot, *(arr[ j ]), dim ) <= 0 ) - j--; - } - - return (i > left)? i - 1 : left; - } - - - /* split the array into two sub-arrays, such that all the elements - * from left to pos_mid are smaller than the elements from pos+1 to - * right. - */ - static void split_arr( Point_ptr * arr, - int left, - int right, - int pos_mid, - int dim ) - { - int pos; - - if ( left >= right ) - return; - - pos = partition( arr, left, right, arr[ (left + right ) / 2 ], - dim ); - if ( pos == pos_mid ) - return; - - if ( pos < pos_mid ) - split_arr( arr, pos+1, right, pos_mid, dim ); - else - split_arr( arr, left, pos, pos_mid, dim ); - } - - - static Point_ptr get_max_element( Point_ptr * arr, - int left, int right, int d ) - { - int max_pos = left; - Point mx = *(arr[ max_pos ]); - - for ( int ind = left + 1; ind <= right; ind++ ) - if ( comp( mx, *(arr[ ind ]), d ) < 0 ) { - mx = *(arr[ ind ]); - max_pos = ind; - } - - return arr[ max_pos ]; - } - - Node *build_r( Point_ptr * arr, int left, int right, - int d ) - { - int num, pos, next_d; - Node * n; - - num = right - left + 1; - - if ( num < 1) - return NULL; - - // if the list contains only one point, - // construct a leaf for this node - if ( num == 1) { - //n = new node; - n = malloc_node(); - n->pnt = arr[ left ]; - - return n; - } - - // else divide space into two regions in - // dim-dim and cotinue recursively - pos = (left + right) / 2; - split_arr( arr, left, right, pos, d ); - - Point * p_median = get_max_element( arr, left, pos, d ); - - // create division plane; - Plane plane( d, *p_median ); - //n = new node; - // CGAL_assertion( n != NULL ); - n = malloc_node(); - - n->plane = plane; - - next_d = d + 1; - if ( next_d >= dim ) - next_d = 0; - - // build left sub-tree - n->left = build_r( arr, left, pos, next_d ); - - // build right sub-tree - n->right = build_r( arr, pos + 1, right, next_d ); - - return n; - } - -public: - typedef list list_points; - - Kdtree_d(int k = 2) - { - dim = k; - root = NULL; - p_arr_pt = NULL; - p_node_arr = NULL; - } - - ~Kdtree_d() - { - delete_all(); - } - - - bool is_valid( bool verbose = false, int level = 0 ) const - { - (void)verbose; - (void)level; - - if ( root == NULL ) - return true; - - return root->is_valid(); - } - - - void dump() - { - root->dump( 0); - } - - void delete_all() - { - root = NULL; - - if ( p_arr_pt != NULL ) - delete[] p_arr_pt; - p_arr_pt = NULL; - if ( p_node_arr != NULL ) - delete[] p_node_arr; - p_node_arr = NULL; - } - - void search( std::back_insert_iterator result, - Box & rect ) - { - if (root == NULL) - return; // it is an empty tree - nothing to search in - - Box region = Box( dim ); - - root->search( result, rect, region ); - } - - void build(list &l) - { - int i; - Point_ptr * p_arr; - - size = l.size(); - p_arr_pt = new Point[ size ]; - CGAL_assertion( p_arr_pt != NULL ); - - p_arr = new Point_ptr[ size ]; - CGAL_assertion( p_arr != NULL ); - - p_node_arr = new Node[ 2 * size ]; - CGAL_assertion( p_node_arr != NULL ); - - node_count = 0; - - /* fill the array */ - i = 0; - for ( typename std::list::iterator j = l.begin(); - j != l.end(); - j++ ) - { - p_arr_pt[ i ] = (*j); - p_arr[ i ] = p_arr_pt + i; - i++; - } - - // recursively build the tree from the sorted list - // starting to divide it in coordinate 0 - root = build_r( p_arr, 0, size - 1, 0 ); - - //printf( "b\n" ); - delete[] p_arr; - } -}; - - -} //namespace CGAL - -#endif /* CGAL_KDTREE_D_H */ diff --git a/kdtree/package_info/kdtree/copyright b/kdtree/package_info/kdtree/copyright deleted file mode 100644 index 930771b9d2c..00000000000 --- a/kdtree/package_info/kdtree/copyright +++ /dev/null @@ -1 +0,0 @@ -Tel-Aviv University (Israel). diff --git a/kdtree/package_info/kdtree/description.txt b/kdtree/package_info/kdtree/description.txt deleted file mode 100644 index 12bb7366575..00000000000 --- a/kdtree/package_info/kdtree/description.txt +++ /dev/null @@ -1 +0,0 @@ -The KD-tree implementation . diff --git a/kdtree/package_info/kdtree/license.txt b/kdtree/package_info/kdtree/license.txt deleted file mode 100644 index 8bb8efcb72b..00000000000 --- a/kdtree/package_info/kdtree/license.txt +++ /dev/null @@ -1 +0,0 @@ -GPL (v3 or later) diff --git a/kdtree/package_info/kdtree/long_description.txt b/kdtree/package_info/kdtree/long_description.txt deleted file mode 100644 index efdd3de5314..00000000000 --- a/kdtree/package_info/kdtree/long_description.txt +++ /dev/null @@ -1,11 +0,0 @@ -CGAL KD-tree impelementation ----------------------------- - - The following contains the CGAL KD-tree implementation. The examples/kdtrees/ -directory contains three simple examples for the use of kd-trees in 2D, 3D -(using points from the Cgal kernel) and 4D (using user-defined points). - - The source is under include/CGAL/kdtree_d.h, and it can be used -independently from the rest of CGAL. - ------------------------------------------------------------------------ diff --git a/kdtree/package_info/kdtree/maintainer b/kdtree/package_info/kdtree/maintainer deleted file mode 100644 index 7e028c78627..00000000000 --- a/kdtree/package_info/kdtree/maintainer +++ /dev/null @@ -1 +0,0 @@ -cgal-develop From 85894658ef3687d873a83d359bacfb49dabe23bb Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Mon, 10 Mar 2014 00:03:24 +0100 Subject: [PATCH 16/27] fixed copyright headers --- .../include/CGAL/Snap_rounding_traits_2.h | 9 +- Spatial_searching/include/CGAL/Kd_tree.h | 105 +++++++++--------- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h index b475ad5dedf..bd5e23f0bae 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_traits_2.h @@ -1,5 +1,4 @@ -// Copyright (c) 2001 Tel-Aviv University (Israel). -// 2009,2014 Max-Planck-Institute Saarbruecken (Germany). +// Copyright (c) 2001,2009,2014 Tel-Aviv University (Israel), Max-Planck-Institute Saarbruecken (Germany). // All rights reserved. // // This file is part of CGAL (www.cgal.org). @@ -15,9 +14,9 @@ // // $URL$ // $Id$ -// // -// author(s) : Eli Packer , +// +// author(s) : Eli Packer , // Waqar Khan #ifndef CGAL_SNAP_ROUNDING_2_TRAITS_H @@ -58,7 +57,7 @@ public: // otherwise Segment_data cannot access the types typedef CGAL::Arr_segment_traits_2 Base_traits; typedef typename Base_traits::Compare_x_2 Compare_x_2; typedef CGAL::To_double To_double; - + public: /*! Functor */ class Snap_2 { diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index bbd62f1f310..30b92cdf2b6 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -1,5 +1,4 @@ -// Copyright (c) 2002,2011 Utrecht University (The Netherlands). -// 2009,2014 Max-Planck-Institute Saarbruecken (Germany) +// Copyright (c) 2002,2011,2014 Utrecht University (The Netherlands), Max-Planck-Institute Saarbruecken (Germany). // All rights reserved. // // This file is part of CGAL (www.cgal.org). @@ -15,8 +14,8 @@ // // $URL$ // $Id$ -// -// Author(s) : Hans Tangelder (), +// +// Author(s) : Hans Tangelder (), // : Waqar Khan #ifndef CGAL_KD_TREE_H @@ -46,7 +45,7 @@ public: typedef Splitter_ Splitter; typedef typename SearchTraits::Point_d Point_d; typedef typename Splitter::Container Point_container; - + typedef typename SearchTraits::FT FT; typedef Kd_tree_node Node; typedef Kd_tree Tree; @@ -59,7 +58,7 @@ public: typedef typename Splitter::Separator Separator; typedef typename std::vector::const_iterator iterator; typedef typename std::vector::const_iterator const_iterator; - + typedef typename std::vector::size_type size_type; private: @@ -75,7 +74,7 @@ private: // Instead of storing the points in arrays in the Kd_tree_node // we put all the data in a vector in the Kd_tree. // and we only store an iterator range in the Kd_tree_node. - // + // std::vector data; @@ -95,7 +94,7 @@ private: // the allocation of the nodes. // The leaf node - Node_handle + Node_handle create_leaf_node(Point_container& c) { Node_handle nh = nodes.emplace(static_cast(c.size()), Node::LEAF); @@ -104,40 +103,40 @@ private: return nh; } - + // The internal node - Node_handle + Node_handle create_internal_node(Point_container& c, const Tag_true&) { return create_internal_node_use_extension(c); } - Node_handle + Node_handle create_internal_node(Point_container& c, const Tag_false&) { return create_internal_node(c); } - - + + // TODO: Similiar to the leaf_init function above, a part of the code should be // moved to a the class Kd_tree_node. // It is not proper yet, but the goal was to see if there is // a potential performance gain through the Compact_container - Node_handle + Node_handle create_internal_node_use_extension(Point_container& c) { Node_handle nh = nodes.emplace(Node::EXTENDED_INTERNAL); - + Point_container c_low(c.dimension(),traits_); split(nh->separator(), c, c_low); - + int cd = nh->separator().cutting_dimension(); - + nh->low_val = c_low.bounding_box().min_coord(cd); nh->high_val = c.bounding_box().max_coord(cd); - + CGAL_assertion(nh->separator().cutting_value() >= nh->low_val); CGAL_assertion(nh->separator().cutting_value() <= nh->high_val); @@ -151,21 +150,21 @@ private: }else{ nh->upper_ch = create_leaf_node(c); } - + return nh; } - + // Note also that I duplicated the code to get rid if the if's for // the boolean use_extension which was constant over the construction - Node_handle + Node_handle create_internal_node(Point_container& c) { Node_handle nh = nodes.emplace(Node::INTERNAL); - + Point_container c_low(c.dimension(),traits_); split(nh->separator(), c, c_low); - + if (c_low.size() > split.bucket_size()){ nh->lower_ch = create_internal_node(c_low); }else{ @@ -186,11 +185,11 @@ public: Kd_tree(Splitter s = Splitter(),const SearchTraits traits=SearchTraits()) : traits_(traits),split(s), built_(false) {} - + template Kd_tree(InputIterator first, InputIterator beyond, - Splitter s = Splitter(),const SearchTraits traits=SearchTraits()) - : traits_(traits),split(s), built_(false) + Splitter s = Splitter(),const SearchTraits traits=SearchTraits()) + : traits_(traits),split(s), built_(false) { pts.insert(pts.end(), first, beyond); } @@ -198,13 +197,13 @@ public: bool empty() const { return pts.empty(); } - - void + + void build() { const Point_d& p = *pts.begin(); typename SearchTraits::Construct_cartesian_const_iterator_d ccci=traits_.construct_cartesian_const_iterator_d_object(); - int dim = static_cast(std::distance(ccci(p), ccci(p,0))); + int dim = static_cast(std::distance(ccci(p), ccci(p,0))); data.reserve(pts.size()); for(unsigned int i = 0; i < pts.size(); i++){ @@ -215,12 +214,12 @@ public: if (c.size() <= split.bucket_size()){ tree_root = create_leaf_node(c); }else { - tree_root = create_internal_node(c, UseExtendedNode()); + tree_root = create_internal_node(c, UseExtendedNode()); } built_ = true; } -private: +private: //any call to this function is for the moment not threadsafe void const_build() const { #ifdef CGAL_HAS_THREADS @@ -231,7 +230,7 @@ private: const_cast(this)->build(); //THIS IS NOT THREADSAFE } public: - + bool is_built() const { return built_; @@ -246,22 +245,22 @@ public: built_ = false; } } - + void clear() { invalidate_built(); pts.clear(); } - + void insert(const Point_d& p) { invalidate_built(); pts.push_back(p); - } - + } + template - void + void insert(InputIterator first, InputIterator beyond) { invalidate_built(); @@ -276,11 +275,11 @@ public: template - OutputIterator + OutputIterator search(OutputIterator it, const FuzzyQueryItem& q) const { if(! pts.empty()){ - + if(! is_built()){ const_build(); } @@ -298,27 +297,27 @@ public: const SearchTraits& - traits() const + traits() const { return traits_; } - Node_const_handle - root() const - { + Node_const_handle + root() const + { if(! is_built()){ const_build(); } - return tree_root; + return tree_root; } - Node_handle + Node_handle root() { if(! is_built()){ build(); } - return tree_root; + return tree_root; } void @@ -331,12 +330,12 @@ public: } const Kd_tree_rectangle& - bounding_box() const + bounding_box() const { if(! is_built()){ const_build(); } - return *bbox; + return *bbox; } const_iterator @@ -351,23 +350,23 @@ public: return pts.end(); } - size_type - size() const + size_type + size() const { return pts.size(); } // Print statistics of the tree. - std::ostream& + std::ostream& statistics(std::ostream& s) const { if(! is_built()){ const_build(); } s << "Tree statistics:" << std::endl; - s << "Number of items stored: " + s << "Number of items stored: " << root()->num_items() << std::endl; - s << "Number of nodes: " + s << "Number of nodes: " << root()->num_nodes() << std::endl; s << " Tree depth: " << root()->depth() << std::endl; return s; From 398613ec71234c9e5ccce904265c65a624e492fc Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Mon, 10 Mar 2014 12:12:59 +0100 Subject: [PATCH 17/27] rephrased documentation of "reserve" to proposal of Sebastien --- .../doc/Spatial_searching/CGAL/Kd_tree.h | 178 +++++++++--------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h index e182425602b..daf2fcebbd6 100644 --- a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h +++ b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h @@ -3,168 +3,168 @@ namespace CGAL { /*! \ingroup SearchClasses -The class `Kd_tree` defines a `k-d` tree. +The class `Kd_tree` defines a `k-d` tree. \cgalHeading{Parameters} -Expects for the first template argument a model of the concept -`SearchTraits`, for example `Search_traits_2 >`. +Expects for the first template argument a model of the concept +`SearchTraits`, for example `Search_traits_2 >`. -Expects for the second template argument a model for the concept `Splitter`. -It defaults to `Sliding_midpoint`. +Expects for the second template argument a model for the concept `Splitter`. +It defaults to `Sliding_midpoint`. -Expects for the third template argument `Tag_true`, if the -tree shall be built with extended nodes, and `Tag_false` otherwise. +Expects for the third template argument `Tag_true`, if the +tree shall be built with extended nodes, and `Tag_false` otherwise. -\sa `CGAL::Kd_tree_node` -\sa `CGAL::Search_traits_2` -\sa `CGAL::Search_traits_3` -\sa `CGAL::Search_traits` +\sa `CGAL::Kd_tree_node` +\sa `CGAL::Search_traits_2` +\sa `CGAL::Search_traits_3` +\sa `CGAL::Search_traits` */ template< typename Traits, typename Splitter, typename UseExtendedNode > class Kd_tree { public: -/// \name Types +/// \name Types /// @{ /*! -Point class. -*/ -typedef Traits::Point_d Point_d; +Point class. +*/ +typedef Traits::Point_d Point_d; /*! -Number type. -*/ -typedef Traits::FT FT; +Number type. +*/ +typedef Traits::FT FT; /*! -Splitter type. -*/ -typedef unspecified_type Splitter; +Splitter type. +*/ +typedef unspecified_type Splitter; /*! -Bidirectional const iterator with value type `Point_d` that allows -to enumerate all points in the tree. -*/ -typedef unspecified_type iterator; +Bidirectional const iterator with value type `Point_d` that allows +to enumerate all points in the tree. +*/ +typedef unspecified_type iterator; /*! -A handle with value type `Kd_tree_node`. -*/ -typedef unspecified_type Node_handle; +A handle with value type `Kd_tree_node`. +*/ +typedef unspecified_type Node_handle; /*! -A const handle with value type `Kd_tree_node`. -*/ -typedef unspecified_type Node_const_handle; +A const handle with value type `Kd_tree_node`. +*/ +typedef unspecified_type Node_const_handle; /*! -Random access const iterator with value type `const Point_d*`. -*/ -typedef unspecified_type Point_d_iterator; +Random access const iterator with value type `const Point_d*`. +*/ +typedef unspecified_type Point_d_iterator; /*! -A type that counts the number of elements in a `k-d` tree. -*/ -typedef unspecified_type size_type; +A type that counts the number of elements in a `k-d` tree. +*/ +typedef unspecified_type size_type; -/// @} +/// @} -/// \name Creation +/// \name Creation /// @{ /*! -Constructs an empty `k-d` tree. -*/ -Kd_tree(Splitter s=Splitter(),Traits t=Traits()); +Constructs an empty `k-d` tree. +*/ +Kd_tree(Splitter s=Splitter(),Traits t=Traits()); /*! -Constructs a `k-d` tree on the elements from the sequence -`[first, beyond)` using the splitting rule implemented by `s`. -The value type of the `InputIterator` must be `Point_d`. +Constructs a `k-d` tree on the elements from the sequence +`[first, beyond)` using the splitting rule implemented by `s`. +The value type of the `InputIterator` must be `Point_d`. -*/ -template Kd_tree(InputIterator first, InputIterator beyond, Splitter s=Splitter(),Traits t=Traits()); +*/ +template Kd_tree(InputIterator first, InputIterator beyond, Splitter s=Splitter(),Traits t=Traits()); -/// @} +/// @} -/// \name Operations +/// \name Operations /// @{ /*! -Inserts the point `p` in the `k-d` tree. -*/ -void insert(Point_d p); +Inserts the point `p` in the `k-d` tree. +*/ +void insert(Point_d p); /*! -Inserts the elements from the sequence `[first, beyond)` in the `k-d` tree. -The value type of the `InputIterator` must be `Point_d`. -*/ -template void insert(InputIterator first, InputIterator beyond); +Inserts the elements from the sequence `[first, beyond)` in the `k-d` tree. +The value type of the `InputIterator` must be `Point_d`. +*/ +template void insert(InputIterator first, InputIterator beyond); /* -Reserve the memory for the 'Points' vector to the 'size' +Pre-allocates memory in order to store at least 'size' points. */ void reserve(size_t size); /*! -Reports the points that are approximately contained by `q`. -The types `FuzzyQueryItem::Point_d` and `Point_d` must be equivalent. -To use this function `Traits` must be a model of the concept `RangeSearchTraits`. +Reports the points that are approximately contained by `q`. +The types `FuzzyQueryItem::Point_d` and `Point_d` must be equivalent. +To use this function `Traits` must be a model of the concept `RangeSearchTraits`. -*/ -template -OutputIterator search(OutputIterator it, FuzzyQueryItem q) const; +*/ +template +OutputIterator search(OutputIterator it, FuzzyQueryItem q) const; /*! -Returns a const iterator to the first point in the tree. -*/ -iterator begin() const; +Returns a const iterator to the first point in the tree. +*/ +iterator begin() const; /*! -Returns the appropriate past-the-end const iterator. -*/ -iterator end() const; +Returns the appropriate past-the-end const iterator. +*/ +iterator end() const; /*! -Removes all points from the `k-d` tree. -*/ -void clear(); +Removes all points from the `k-d` tree. +*/ +void clear(); /*! -Returns the number of points that are stored in the tree. -*/ -size_type size() const; +Returns the number of points that are stored in the tree. +*/ +size_type size() const; /*! -return the instance of the traits used to construct the tree. -*/ -Traits traits() const; +return the instance of the traits used to construct the tree. +*/ +Traits traits() const; /*! -Returns a handle to the root node of the tree. -*/ -Node_handle root(); +Returns a handle to the root node of the tree. +*/ +Node_handle root(); /*! -Returns a const handle to the root node of the tree. -*/ -Node_const_handle root() const; +Returns a const handle to the root node of the tree. +*/ +Node_const_handle root() const; /*! Returns a const reference to the bounding box of the root node of the tree. -*/ -const Kd_tree_rectangle& bounding_box() const; +*/ +const Kd_tree_rectangle& bounding_box() const; /*! -Inserts statistics of the tree into the output stream `s`. -*/ -std::ostream& statistics(std::ostream& s) const; +Inserts statistics of the tree into the output stream `s`. +*/ +std::ostream& statistics(std::ostream& s) const; /// @} From ae14939d26c9640b309c0d6da485126a2c2d13a5 Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Tue, 11 Mar 2014 14:09:48 +0100 Subject: [PATCH 18/27] fixed copyright header --- Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h index feaa2636430..6c0d1e459e1 100755 --- a/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h +++ b/Snap_rounding_2/include/CGAL/Snap_rounding_kd_2.h @@ -1,5 +1,4 @@ -// Copyright (c) 2001 Tel-Aviv University (Israel). -// 2009,2014 Max-Planck-Institute Saarbruecken (Germany). +// Copyright (c) 2001, 2009, 2014 Tel-Aviv University (Israel), Max-Planck-Institute Saarbruecken (Germany). // All rights reserved. // // This file is part of CGAL (www.cgal.org). @@ -17,7 +16,7 @@ // $Id$ // // -// author(s) : Eli Packer , +// author(s) : Eli Packer , // Waqar Khan #ifndef CGAL_SNAP_ROUNDING_KD_2_H From a40391353904675cd02cd35b2316d47e8b05d916 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Tue, 25 Mar 2014 15:01:37 +0100 Subject: [PATCH 19/27] Documentation reflecting the branch changes. --- Installation/changes.html | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Installation/changes.html b/Installation/changes.html index 0d57b638846..ea1b8cb15b2 100644 --- a/Installation/changes.html +++ b/Installation/changes.html @@ -108,6 +108,24 @@ and src/ directories).
+ +

Release 4.5

+
+ +

Snap_rounding_2

+
    +
  • Replaced use of private kd_tree with CGAL's official Kd_tree from Spatial_searching package; + results in a small performance gain. Removed the private kd_tree package. +
  • +
+

Spatial_searching

+
    +
  • Added method reserve(size_t size) to class Kd_tree to allocate + memory to store size points. +
  • +
+
+

Release 4.4

From b08dd99dcc808072a1b8689caf6c792c17886369 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Wed, 26 Mar 2014 12:22:01 +0100 Subject: [PATCH 20/27] * In order for the examples to run sucessfully without any input arguments (which were required previously), the snap_rounding_data.cpp will run for the default input data file i.e. data/snap_rounding_data. In case of data file as input arguments, the user specified data file will be chosen --- .../Snap_rounding_2/data/snap_rounding_data | 5 +++ .../Snap_rounding_2/snap_rounding_data.cpp | 32 +++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 Snap_rounding_2/examples/Snap_rounding_2/data/snap_rounding_data diff --git a/Snap_rounding_2/examples/Snap_rounding_2/data/snap_rounding_data b/Snap_rounding_2/examples/Snap_rounding_2/data/snap_rounding_data new file mode 100644 index 00000000000..246dc1873d4 --- /dev/null +++ b/Snap_rounding_2/examples/Snap_rounding_2/data/snap_rounding_data @@ -0,0 +1,5 @@ +4 +0 0 10 10 +0 10 10 0 +3 0 3 10 +7 0 7 10 \ No newline at end of file diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index 73354ee89b7..84f8fd3554f 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -20,17 +20,17 @@ /* Usage -* -* This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE. -* (Mandatory) path to the input file containing the arrangment information. -* (Optional) path to the output file where the results of snap rounding will be stored. -* Not providing this argument will print the result on standard output. -* -* Input file format -* Line # 1: Number of line-segments present in the file. -* Line # 2 to N+1: segment_start_point_x segment_start_point_y segment_end_point_x segment_end_point_y -* -* Each line should contain information about just one segment. + * + * This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE. + * (Mandatory) path to the input file containing the arrangment information. + * (Optional) path to the output file where the results of snap rounding will be stored. + * Not providing this argument will print the result on standard output. + * + * Input file format + * Line # 1: Number of line-segments present in the file. + * Line # 2 to N+1: segment_start_point_x segment_start_point_y segment_end_point_x segment_end_point_y + * + * Each line should contain information about just one segment. */ #include @@ -51,9 +51,10 @@ typedef std::list Polyline_list_2; int main(int argc, char* argv[]) { - if(argc > 3 || argc < 2) + //if(argc > 3 || argc < 2) + if(argc > 3) { - std::cout<< "Incorrect input. path to the INPUT file. (optional) path to the OUTPUT file" << std::endl; + std::cout<< "Incorrect input. path to the INPUT file. (optional) path to the OUTPUT file. No arguments to choose the default data file" << std::endl; return -1; } @@ -63,7 +64,10 @@ int main(int argc, char* argv[]) std::ifstream my_read_file; std::ofstream my_write_file; - my_read_file.open(argv[1]); + if(argc > 1) + my_read_file.open(argv[1]); + else + my_read_file.open("data/snap_rounding_data"); if(!my_read_file.is_open()) { From 98e6007dd3c43885dde928ed310c0c5166da233b Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Wed, 26 Mar 2014 18:02:04 +0100 Subject: [PATCH 21/27] * Added the capacity() function to the Kd_tree.h and also added its documentation * Added a test that checks the reserve() and capacity() function for Kd_tree --- .../doc/Spatial_searching/CGAL/Kd_tree.h | 5 ++ Spatial_searching/include/CGAL/Kd_tree.h | 6 ++ .../iso_rectangle_2_query_2.cpp | 69 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp diff --git a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h index daf2fcebbd6..bf5b9dfd107 100644 --- a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h +++ b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h @@ -111,6 +111,11 @@ Pre-allocates memory in order to store at least 'size' points. */ void reserve(size_t size); +/* + * Returns the size of the allocated memory for the points. + */ +size_t capacity(); + /*! Reports the points that are approximately contained by `q`. The types `FuzzyQueryItem::Point_d` and `Point_d` must be equivalent. diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index 30b92cdf2b6..72e60da1821 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -273,6 +273,12 @@ public: pts.reserve(size); } + //Get the size of the aloocated memory for the points vector. + size_t capacity() + { + return pts.capacity(); + } + template OutputIterator diff --git a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp new file mode 100644 index 00000000000..5722f5ef97d --- /dev/null +++ b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + +#include + +typedef CGAL::Simple_cartesian K; +typedef K::Point_2 Point_d; +typedef CGAL::Random_points_in_square_2 Random_points_iterator; +typedef CGAL::Counting_iterator N_Random_points_iterator; +typedef CGAL::Search_traits_2 Traits; +typedef CGAL::Kd_tree Tree; +typedef CGAL::Fuzzy_iso_box Fuzzy_iso_box; + +int +main() { + const int N = 1000; + + std::list points; + points.push_back(Point_d(0,0)); + + Tree tree; + Random_points_iterator rpg; + + tree.reserve(N); + + //to test wether the tree.capacity() function works properly. + if( tree.capacity() != N) + { + std::cout << "Something is wrong with allocating points memory." << std::endl; + return -1; + } + + for(int i = 0; i < N; i++) + { + tree.insert(*rpg++); + } + + std::list result; + + // define range query + Point_d p(0.2, 0.2); + Point_d q(0.7, 0.7); + + // Searching an exact range + // using default value 0.0 for epsilon fuzziness paramater + // Fuzzy_box exact_range(r); replaced by + Fuzzy_iso_box exact_range(p,q); + std::cout << "tree.search(..)" << std::endl; + //tree.report_all_points(std::ostream_iterator(std::cout,"\n")); + tree.search( std::back_inserter( result ), exact_range); + + std::cout << "The points in the box [0.2,0.7]x[0.2,0.7] are: " << std::endl; + std::copy (result.begin(), result.end(), std::ostream_iterator(std::cout,"\n") ); + std::cout << std::endl; + + result.clear(); + // Searching a fuzzy range + // using value 0.1 for fuzziness paramater + Fuzzy_iso_box approximate_range(p, q, 0.1); + tree.search(std::back_inserter( result ), approximate_range); + std::cout << "The points in the fuzzy box [<0.1-0.3>,<0.6-0.9>]x[<0.1-0.3>,<0.6-0.9>] are: " + << std::endl; + std::copy (result.begin(), result.end(), std::ostream_iterator(std::cout,"\n") ); + std::cout << std::endl; + return 0; +} From 7b06c4b7c805448d340457966cecd96456ef907c Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Fri, 28 Mar 2014 14:46:12 +0100 Subject: [PATCH 22/27] Cout changed to cerr in the error message. --- .../test/Spatial_searching/iso_rectangle_2_query_2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp index 5722f5ef97d..44f3276f581 100644 --- a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp +++ b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp @@ -29,7 +29,7 @@ main() { //to test wether the tree.capacity() function works properly. if( tree.capacity() != N) { - std::cout << "Something is wrong with allocating points memory." << std::endl; + std::cerr << "Something is wrong with allocating points memory." << std::endl; return -1; } From 9d8a02aa4638b72bcd3869261ca18ff2d5f8b0d1 Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Fri, 28 Mar 2014 14:49:52 +0100 Subject: [PATCH 23/27] rephrased documentation for capacity in Kd_tree --- Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h | 2 +- Spatial_searching/include/CGAL/Kd_tree.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h index bf5b9dfd107..05750c7f0be 100644 --- a/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h +++ b/Spatial_searching/doc/Spatial_searching/CGAL/Kd_tree.h @@ -112,7 +112,7 @@ Pre-allocates memory in order to store at least 'size' points. void reserve(size_t size); /* - * Returns the size of the allocated memory for the points. +Returns the number of points for which memory has been pre-allocated. */ size_t capacity(); diff --git a/Spatial_searching/include/CGAL/Kd_tree.h b/Spatial_searching/include/CGAL/Kd_tree.h index 72e60da1821..2d541378128 100644 --- a/Spatial_searching/include/CGAL/Kd_tree.h +++ b/Spatial_searching/include/CGAL/Kd_tree.h @@ -273,7 +273,7 @@ public: pts.reserve(size); } - //Get the size of the aloocated memory for the points vector. + //Get the capacity of the underlying points vector. size_t capacity() { return pts.capacity(); From 3bec7b2823512d79b043f93ba10e5958d6621bc6 Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Fri, 28 Mar 2014 14:56:03 +0100 Subject: [PATCH 24/27] added comment and allow that capacity might be larger than demanded --- .../test/Spatial_searching/iso_rectangle_2_query_2.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp index 44f3276f581..3e35a7589b4 100644 --- a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp +++ b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp @@ -24,12 +24,14 @@ main() { Tree tree; Random_points_iterator rpg; + //inserting N points one-by-one, thus the use of "reserve" is recommended, and thus we use it + tree.reserve(N); //to test wether the tree.capacity() function works properly. - if( tree.capacity() != N) + if( tree.capacity() < N) { - std::cerr << "Something is wrong with allocating points memory." << std::endl; + std::cerr << "ERROR: Something is wrong with allocating points memory." << std::endl; return -1; } From 6e1160ba0dd4cd534c9d842ac3d0ad13c3cc7cb7 Mon Sep 17 00:00:00 2001 From: Waqar Khan Date: Fri, 28 Mar 2014 15:01:06 +0100 Subject: [PATCH 25/27] Added capacity for Kd_tree. --- Installation/changes.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Installation/changes.html b/Installation/changes.html index ea1b8cb15b2..ae8fc350f10 100644 --- a/Installation/changes.html +++ b/Installation/changes.html @@ -120,8 +120,8 @@ and src/ directories).

Spatial_searching

    -
  • Added method reserve(size_t size) to class Kd_tree to allocate - memory to store size points. +
  • Added methods reserve(size_t size) and size_t capacity() to class Kd_tree to allocate + memory to store size points and to report that number (STL compliance).
From 1d2410682990882a078e7c6ecd48ce0dc035989a Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Sat, 29 Mar 2014 12:07:34 +0100 Subject: [PATCH 26/27] removed waarning (comparison signed/unsigned) shown in testsuite --- .../Snap_rounding_2/snap_rounding_data.cpp | 34 +++++++++---------- .../iso_rectangle_2_query_2.cpp | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp index 84f8fd3554f..e530e4f1238 100644 --- a/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp +++ b/Snap_rounding_2/examples/Snap_rounding_2/snap_rounding_data.cpp @@ -21,16 +21,16 @@ /* Usage * - * This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE. + * This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE. * (Mandatory) path to the input file containing the arrangment information. - * (Optional) path to the output file where the results of snap rounding will be stored. + * (Optional) path to the output file where the results of snap rounding will be stored. * Not providing this argument will print the result on standard output. * * Input file format * Line # 1: Number of line-segments present in the file. * Line # 2 to N+1: segment_start_point_x segment_start_point_y segment_end_point_x segment_end_point_y - * - * Each line should contain information about just one segment. + * + * Each line should contain information about just one segment. */ #include @@ -66,7 +66,7 @@ int main(int argc, char* argv[]) if(argc > 1) my_read_file.open(argv[1]); - else + else my_read_file.open("data/snap_rounding_data"); if(!my_read_file.is_open()) @@ -88,33 +88,33 @@ int main(int argc, char* argv[]) unsigned int number_of_lines = 0; my_read_file >> number_of_lines; - - for(int i=0; i> point_start_x; my_read_file >> point_start_y; my_read_file >> point_end_x; my_read_file >> point_end_y; - + seg_list.push_back(Segment_2(Point_2(point_start_x, point_start_y), Point_2(point_end_x, point_end_y))); } - + // Generate an iterated snap-rounding representation, where the centers of // the hot pixels bear their original coordinates, using 1 kd trees: CGAL::snap_rounding_2 - (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); + (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1); int counter = 0; Polyline_list_2::const_iterator iter1; - + if(argc == 3) //output to the file { - for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) + for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) { my_write_file << "Polyline number " << ++counter << ":\n"; Polyline_2::const_iterator iter2; - + for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) my_write_file << " (" << iter2->x() << ":" << iter2->y() << ")\n"; } @@ -123,17 +123,17 @@ int main(int argc, char* argv[]) } else //output to std output { - for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) + for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1) { std::cout << "Polyline number " << ++counter << ":\n"; Polyline_2::const_iterator iter2; - + for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2) std::cout << " (" << iter2->x() << ":" << iter2->y() << ")\n"; } } - + my_read_file.close(); return(0); -} \ No newline at end of file +} diff --git a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp index 3e35a7589b4..7cff9349315 100644 --- a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp +++ b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp @@ -16,7 +16,7 @@ typedef CGAL::Fuzzy_iso_box Fuzzy_iso_box; int main() { - const int N = 1000; + const size_t N = 1000; std::list points; points.push_back(Point_d(0,0)); From 3723b74b61ab036b1d9b0b1dda784b057644b529 Mon Sep 17 00:00:00 2001 From: Eric Berberich Date: Thu, 3 Apr 2014 11:27:47 +0200 Subject: [PATCH 27/27] removed warning (comparison signed/unsigned) --- .../test/Spatial_searching/iso_rectangle_2_query_2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp index 7cff9349315..17193f40023 100644 --- a/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp +++ b/Spatial_searching/test/Spatial_searching/iso_rectangle_2_query_2.cpp @@ -35,7 +35,7 @@ main() { return -1; } - for(int i = 0; i < N; i++) + for(size_t i = 0; i < N; i++) { tree.insert(*rpg++); }