buffer_with_strategies.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2015-2019 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 "geometry_test_common.hpp"
  8. #include <boost/geometry/algorithms/buffer.hpp>
  9. #include <boost/geometry/strategies/strategies.hpp>
  10. #include <boost/geometry/geometries/geometries.hpp>
  11. // For test
  12. #include <boost/geometry/algorithms/area.hpp>
  13. #include <boost/geometry/algorithms/correct.hpp>
  14. #include <boost/geometry/algorithms/num_points.hpp>
  15. // This unit test tests boost::geometry::buffer (overload with strategies)
  16. // More detailed tests are, per geometry type, available in buffer_<TYPE>.cpp
  17. // (testing boost::geometry::buffer_inserter)
  18. // SVG's are not created (they are in detailed tests)
  19. static std::string const polygon_simplex = "POLYGON ((0 0,1 5,6 1,0 0))";
  20. static std::string const polygon_empty = "POLYGON()";
  21. static std::string const multi_polygon_empty = "MULTIPOLYGON()";
  22. static std::string const multi_polygon_simplex
  23. = "MULTIPOLYGON(((0 1,2 5,5 3,0 1)),((1 1,5 2,5 0,1 1)))";
  24. template
  25. <
  26. typename Geometry,
  27. typename GeometryOut,
  28. typename JoinStrategy,
  29. typename EndStrategy,
  30. typename SideStrategy,
  31. typename PointStrategy,
  32. typename DistanceStrategy
  33. >
  34. void test_with_strategies(std::string const& caseid,
  35. std::string const& wkt,
  36. JoinStrategy const& join_strategy,
  37. EndStrategy const& end_strategy,
  38. SideStrategy const& side_strategy,
  39. PointStrategy const& point_strategy,
  40. DistanceStrategy const& distance_strategy,
  41. double expected_area,
  42. std::size_t expected_numpoints,
  43. double tolerance = 0.01)
  44. {
  45. namespace bg = boost::geometry;
  46. Geometry g;
  47. bg::read_wkt(wkt, g);
  48. bg::correct(g);
  49. GeometryOut result;
  50. bg::buffer(g, result,
  51. distance_strategy, side_strategy,
  52. join_strategy, end_strategy, point_strategy);
  53. BOOST_CHECK_MESSAGE
  54. (
  55. bg::num_points(result) == expected_numpoints,
  56. "Buffer " << caseid
  57. << " numpoints expected: " << expected_numpoints
  58. << " detected: " << bg::num_points(result)
  59. );
  60. double const area = bg::area(result);
  61. double const difference = area - expected_area;
  62. BOOST_CHECK_MESSAGE
  63. (
  64. bg::math::abs(difference) < tolerance,
  65. "Buffer " << caseid
  66. << std::setprecision(12)
  67. << " area expected: " << expected_area
  68. << " detected: " << area
  69. );
  70. }
  71. template <bool Clockwise, typename Point>
  72. void test_all()
  73. {
  74. typedef bg::model::polygon<Point, Clockwise> polygon;
  75. typedef bg::model::multi_polygon<polygon> multi_polygon;
  76. bg::strategy::buffer::join_round join(160);
  77. bg::strategy::buffer::end_round end(160);
  78. bg::strategy::buffer::point_circle circle(160);
  79. bg::strategy::buffer::side_straight side;
  80. typedef bg::strategy::buffer::distance_symmetric
  81. <
  82. typename bg::coordinate_type<Point>::type
  83. > distance;
  84. test_with_strategies<multi_polygon, multi_polygon>(
  85. "multi_polygon_empty", multi_polygon_empty,
  86. join, end, side, circle, distance(1.0),
  87. 0.0, 0);
  88. // PostGIS: 34.2550669294223 216 (40 / qcircle)
  89. // SQL Server: 34.2550419903829 220 (default options)
  90. test_with_strategies<multi_polygon, multi_polygon>(
  91. "multi_polygon_simplex", multi_polygon_simplex,
  92. join, end, side, circle, distance(1.0),
  93. 34.2551, 219);
  94. test_with_strategies<polygon, multi_polygon>(
  95. "polygon_empty", polygon_empty,
  96. join, end, side, circle, distance(1.0),
  97. 0.0, 0);
  98. //
  99. // PostGIS: 35.2256914798762 164 (40 / qcircle)
  100. // SQL Server: 35.2252355201605 153 (default options)
  101. test_with_strategies<polygon, multi_polygon>(
  102. "polygon_simplex", polygon_simplex,
  103. join, end, side, circle, distance(1.0),
  104. 35.2257, 166);
  105. }
  106. int test_main(int, char* [])
  107. {
  108. BoostGeometryWriteTestConfiguration();
  109. test_all<true, bg::model::point<default_test_type, 2, bg::cs::cartesian> >();
  110. return 0;
  111. }