range_by_section.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. #include <string>
  9. #define BOOST_GEOMETRY_UNIT_TEST_SECTIONALIZE
  10. #include <geometry_test_common.hpp>
  11. #include <boost/geometry/algorithms/detail/sections/sectionalize.hpp>
  12. #include <boost/geometry/algorithms/detail/sections/range_by_section.hpp>
  13. #include <boost/geometry/views/detail/range_type.hpp>
  14. #include <boost/geometry/geometries/geometries.hpp>
  15. #include <boost/geometry/geometries/point_xy.hpp>
  16. #include <boost/geometry/io/wkt/wkt.hpp>
  17. #include <boost/geometry/util/condition.hpp>
  18. template <int DimensionCount, bool Reverse, typename Geometry>
  19. void test_sectionalize(std::string const /*caseid*/, Geometry const& geometry, std::size_t section_count)
  20. {
  21. typedef typename bg::point_type<Geometry>::type point;
  22. typedef bg::model::box<point> box;
  23. typedef bg::sections<box, DimensionCount> sections;
  24. typedef boost::mpl::vector_c<std::size_t, 0> dim2;
  25. sections s;
  26. bg::sectionalize<Reverse, dim2>(geometry, bg::detail::no_rescale_policy(), s);
  27. BOOST_CHECK_EQUAL(s.size(), section_count);
  28. typedef typename bg::closeable_view
  29. <
  30. typename bg::detail::range_type<Geometry>::type const,
  31. bg::closure<Geometry>::value
  32. >::type cview_type;
  33. typedef typename bg::reversible_view
  34. <
  35. cview_type const,
  36. Reverse ? bg::iterate_reverse : bg::iterate_forward
  37. >::type view_type;
  38. typedef typename boost::range_iterator
  39. <
  40. view_type const
  41. >::type range_iterator;
  42. BOOST_FOREACH(typename sections::value_type const& sec, s)
  43. {
  44. cview_type cview(bg::range_by_section(geometry, sec));
  45. view_type view(cview);
  46. range_iterator it1 = boost::begin(view) + sec.begin_index;
  47. range_iterator it2 = boost::begin(view) + sec.end_index;
  48. int count = 0;
  49. for (range_iterator it = it1; it != it2; ++it)
  50. {
  51. count++;
  52. }
  53. BOOST_CHECK_EQUAL(int(sec.count), count);
  54. }
  55. }
  56. template <typename Geometry, bool Reverse>
  57. void test_sectionalize(std::string const& caseid, std::string const& wkt,
  58. std::size_t count1)
  59. {
  60. Geometry geometry;
  61. bg::read_wkt(wkt, geometry);
  62. if ( BOOST_GEOMETRY_CONDITION( bg::closure<Geometry>::value == bg::open ) )
  63. {
  64. geometry.outer().resize(geometry.outer().size() - 1);
  65. }
  66. //bg::correct(geometry);
  67. test_sectionalize<1, Reverse>(caseid + "_d1", geometry, count1);
  68. }
  69. template <typename P>
  70. void test_all()
  71. {
  72. std::string const first = "polygon((2.0 1.3, 2.4 1.7, 2.8 1.8, 3.4 1.2, 3.7 1.6,3.4 2.0, 4.1 3.0, 5.3 2.6, 5.4 1.2, 4.9 0.8, 2.9 0.7,2.0 1.3))";
  73. test_sectionalize<bg::model::polygon<P>, false>("first", first, 4);
  74. test_sectionalize<bg::model::polygon<P, false>, true>("first_reverse",
  75. first, 4);
  76. test_sectionalize<bg::model::polygon<P, false, true>, false>("first_open",
  77. first, 4);
  78. test_sectionalize<bg::model::polygon<P, true, false>, true>("first_open_reverse",
  79. first, 4);
  80. }
  81. int test_main(int, char* [])
  82. {
  83. test_all<bg::model::d2::point_xy<double> >();
  84. return 0;
  85. }