multi_point_growth.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Performance Test
  3. // Copyright (c) 2012-2015 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 <boost/geometry.hpp>
  8. #include <boost/geometry/geometries/geometries.hpp>
  9. #include <boost/foreach.hpp>
  10. #include <boost/timer.hpp>
  11. #include <fstream>
  12. namespace bg = boost::geometry;
  13. template
  14. <
  15. typename GeometryOut,
  16. typename Geometry
  17. >
  18. double test_growth(Geometry const& geometry, int n, int d, double distance)
  19. {
  20. typedef typename bg::coordinate_type<Geometry>::type coordinate_type;
  21. typedef typename bg::point_type<Geometry>::type point_type;
  22. // extern int point_buffer_count;
  23. std::ostringstream complete;
  24. complete
  25. << "point_growth"
  26. << "_" << "r"
  27. << "_" << n
  28. << "_" << d
  29. // << "_" << point_buffer_count
  30. ;
  31. //std::cout << complete.str() << std::endl;
  32. std::ostringstream filename;
  33. filename << "buffer_" << complete.str() << ".svg";
  34. std::ofstream svg(filename.str().c_str());
  35. #ifdef BOOST_GEOMETRY_DEBUG_WITH_MAPPER
  36. bg::svg_mapper<point_type> mapper(svg, 500, 500);
  37. {
  38. bg::model::box<point_type> box;
  39. bg::envelope(geometry, box);
  40. bg::buffer(box, box, distance * 1.01);
  41. mapper.add(box);
  42. }
  43. #endif
  44. bg::strategy::buffer::join_round join_strategy(100);
  45. bg::strategy::buffer::end_flat end_strategy;
  46. bg::strategy::buffer::point_circle point_strategy;
  47. bg::strategy::buffer::side_straight side_strategy;
  48. typedef bg::strategy::buffer::distance_symmetric<coordinate_type> distance_strategy_type;
  49. distance_strategy_type distance_strategy(distance);
  50. std::vector<GeometryOut> buffered;
  51. bg::buffer(geometry, buffered,
  52. distance_strategy, side_strategy,
  53. join_strategy, end_strategy, point_strategy);
  54. typename bg::default_area_result<GeometryOut>::type area = 0;
  55. BOOST_FOREACH(GeometryOut const& polygon, buffered)
  56. {
  57. area += bg::area(polygon);
  58. }
  59. #ifdef BOOST_GEOMETRY_DEBUG_WITH_MAPPER
  60. // Map input geometry in green
  61. mapper.map(geometry, "opacity:0.5;fill:rgb(0,128,0);stroke:rgb(0,128,0);stroke-width:10");
  62. BOOST_FOREACH(GeometryOut const& polygon, buffered)
  63. {
  64. mapper.map(polygon, "opacity:0.4;fill:rgb(255,255,128);stroke:rgb(0,0,0);stroke-width:3");
  65. }
  66. #endif
  67. return area;
  68. }
  69. template <typename P>
  70. void test_growth(int n, int distance_count)
  71. {
  72. srand(int(time(NULL)));
  73. //std::cout << typeid(bg::coordinate_type<P>::type).name() << std::endl;
  74. boost::timer t;
  75. namespace buf = bg::strategy::buffer;
  76. typedef bg::model::polygon<P> polygon;
  77. typedef bg::model::multi_point<P> multi_point_type;
  78. multi_point_type multi_point;
  79. for (int i = 0; i < n; i++)
  80. {
  81. P point(rand() % 100, rand() % 100);
  82. multi_point.push_back(point);
  83. }
  84. //std::cout << bg::wkt(multi_point) << std::endl;
  85. double previous_area = 0;
  86. double epsilon = 0.1;
  87. double distance = 15.0;
  88. for (int d = 0; d < distance_count; d++, distance += epsilon)
  89. {
  90. double area = test_growth<polygon>(multi_point, n, d, distance);
  91. if (area < previous_area)
  92. {
  93. std::cout << "Error: " << area << " < " << previous_area << std::endl
  94. << " n=" << n << " distance=" << distance
  95. << bg::wkt(multi_point) << std::endl;
  96. }
  97. previous_area = area;
  98. }
  99. std::cout << "n=" << n << " time=" << t.elapsed() << std::endl;
  100. }
  101. int main(int, char* [])
  102. {
  103. for (int i = 5; i <= 50; i++)
  104. {
  105. test_growth<bg::model::point<double, 2, bg::cs::cartesian> >(i, 20);
  106. }
  107. return 0;
  108. }