test_simplify.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-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. #ifndef BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
  8. #define BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
  9. // Test-functionality, shared between single and multi tests
  10. #include <iomanip>
  11. #include <sstream>
  12. #include <geometry_test_common.hpp>
  13. #include <boost/geometry/algorithms/correct_closure.hpp>
  14. #include <boost/geometry/algorithms/equals.hpp>
  15. #include <boost/geometry/algorithms/simplify.hpp>
  16. #include <boost/geometry/algorithms/distance.hpp>
  17. #include <boost/geometry/strategies/strategies.hpp>
  18. #include <boost/geometry/io/wkt/wkt.hpp>
  19. #include <boost/variant/variant.hpp>
  20. template
  21. <
  22. typename GeometryForTag,
  23. typename Tag = typename bg::tag<GeometryForTag>::type
  24. >
  25. struct test_equality
  26. {
  27. template <typename Geometry, typename Expected>
  28. static void apply(Geometry const& geometry, Expected const& expected)
  29. {
  30. // Verify both spatially equal AND number of points, because several
  31. // of the tests only check explicitly on collinear points being
  32. // simplified away
  33. bool const result
  34. = bg::equals(geometry, expected)
  35. && bg::num_points(geometry) == bg::num_points(expected);
  36. BOOST_CHECK_MESSAGE(result,
  37. " result: " << bg::wkt(geometry) << " " << bg::area(geometry)
  38. << " expected: " << bg::wkt(expected) << " " << bg::area(expected));
  39. }
  40. };
  41. // Linestring does NOT yet have "geometry::equals" implemented
  42. // Until then, WKT's are compared (which is acceptable for linestrings, but not
  43. // for polygons, because simplify might rotate them)
  44. template <typename GeometryForTag>
  45. struct test_equality<GeometryForTag, bg::linestring_tag>
  46. {
  47. template <typename Geometry, typename Expected>
  48. static void apply(Geometry const& geometry, Expected const& expected)
  49. {
  50. std::ostringstream out1, out2;
  51. out1 << bg::wkt(geometry);
  52. out2 << bg::wkt(expected);
  53. BOOST_CHECK_EQUAL(out1.str(), out2.str());
  54. }
  55. };
  56. template <typename Tag>
  57. struct test_inserter
  58. {
  59. template <typename Geometry, typename Expected>
  60. static void apply(Geometry& , Expected const& , double )
  61. {}
  62. };
  63. template <>
  64. struct test_inserter<bg::linestring_tag>
  65. {
  66. template <typename Geometry, typename Expected, typename DistanceMeasure>
  67. static void apply(Geometry& geometry,
  68. Expected const& expected,
  69. DistanceMeasure const& distance)
  70. {
  71. {
  72. Geometry simplified;
  73. bg::detail::simplify::simplify_insert(geometry,
  74. std::back_inserter(simplified), distance);
  75. test_equality<Geometry>::apply(simplified, expected);
  76. }
  77. #ifdef TEST_PULL89
  78. {
  79. typedef typename bg::point_type<Geometry>::type point_type;
  80. typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
  81. typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
  82. distance_type max_distance(distance);
  83. less_comparator less(max_distance);
  84. bg::strategy::simplify::detail::douglas_peucker
  85. <
  86. point_type,
  87. bg::strategy::distance::detail::projected_point_ax<>,
  88. less_comparator
  89. > strategy(less);
  90. Geometry simplified;
  91. bg::detail::simplify::simplify_insert(geometry,
  92. std::back_inserter(simplified), max_distance, strategy);
  93. test_equality<Geometry>::apply(simplified, expected);
  94. }
  95. #endif
  96. }
  97. };
  98. template <typename Geometry, typename Expected, typename DistanceMeasure>
  99. void check_geometry(Geometry const& geometry,
  100. Expected const& expected,
  101. DistanceMeasure const& distance)
  102. {
  103. Geometry simplified;
  104. bg::simplify(geometry, simplified, distance);
  105. test_equality<Expected>::apply(simplified, expected);
  106. }
  107. template <typename Geometry, typename Expected, typename Strategy, typename DistanceMeasure>
  108. void check_geometry(Geometry const& geometry,
  109. Expected const& expected,
  110. DistanceMeasure const& distance,
  111. Strategy const& strategy)
  112. {
  113. Geometry simplified;
  114. bg::simplify(geometry, simplified, distance, strategy);
  115. test_equality<Expected>::apply(simplified, expected);
  116. }
  117. template <typename Geometry, typename DistanceMeasure>
  118. void check_geometry_with_area(Geometry const& geometry,
  119. double expected_area,
  120. DistanceMeasure const& distance)
  121. {
  122. Geometry simplified;
  123. bg::simplify(geometry, simplified, distance);
  124. BOOST_CHECK_CLOSE(bg::area(simplified), expected_area, 0.01);
  125. }
  126. template <typename Geometry, typename DistanceMeasure>
  127. void test_geometry(std::string const& wkt,
  128. std::string const& expected_wkt,
  129. DistanceMeasure distance)
  130. {
  131. typedef typename bg::point_type<Geometry>::type point_type;
  132. Geometry geometry, expected;
  133. bg::read_wkt(wkt, geometry);
  134. bg::read_wkt(expected_wkt, expected);
  135. boost::variant<Geometry> v(geometry);
  136. // Define default strategy for testing
  137. typedef bg::strategy::simplify::douglas_peucker
  138. <
  139. typename bg::point_type<Geometry>::type,
  140. bg::strategy::distance::projected_point<double>
  141. > dp;
  142. check_geometry(geometry, expected, distance);
  143. check_geometry(v, expected, distance);
  144. BOOST_CONCEPT_ASSERT( (bg::concepts::SimplifyStrategy<dp, point_type>) );
  145. check_geometry(geometry, expected, distance, dp());
  146. check_geometry(v, expected, distance, dp());
  147. // Check inserter (if applicable)
  148. test_inserter
  149. <
  150. typename bg::tag<Geometry>::type
  151. >::apply(geometry, expected, distance);
  152. #ifdef TEST_PULL89
  153. // Check using non-default less comparator in douglass_peucker
  154. typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
  155. typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
  156. distance_type const max_distance(distance);
  157. less_comparator const less(max_distance);
  158. typedef bg::strategy::simplify::detail::douglas_peucker
  159. <
  160. point_type,
  161. bg::strategy::distance::detail::projected_point_ax<>,
  162. less_comparator
  163. > douglass_peucker_with_less;
  164. BOOST_CONCEPT_ASSERT( (bg::concepts::SimplifyStrategy<douglass_peucker_with_less, point_type>) );
  165. check_geometry(geometry, expected, distance, douglass_peucker_with_less(less));
  166. check_geometry(v, expected, distance, douglass_peucker_with_less(less));
  167. #endif
  168. }
  169. template <typename Geometry, typename Strategy, typename DistanceMeasure>
  170. void test_geometry(std::string const& wkt,
  171. std::string const& expected_wkt,
  172. DistanceMeasure const& distance,
  173. Strategy const& strategy)
  174. {
  175. Geometry geometry, expected;
  176. bg::read_wkt(wkt, geometry);
  177. bg::read_wkt(expected_wkt, expected);
  178. bg::correct_closure(geometry);
  179. bg::correct_closure(expected);
  180. boost::variant<Geometry> v(geometry);
  181. BOOST_CONCEPT_ASSERT( (bg::concepts::SimplifyStrategy<Strategy,
  182. typename bg::point_type<Geometry>::type>) );
  183. check_geometry(geometry, expected, distance, strategy);
  184. check_geometry(v, expected, distance, strategy);
  185. }
  186. template <typename Geometry, typename DistanceMeasure>
  187. void test_geometry(std::string const& wkt,
  188. double expected_area,
  189. DistanceMeasure const& distance)
  190. {
  191. Geometry geometry;
  192. bg::read_wkt(wkt, geometry);
  193. bg::correct_closure(geometry);
  194. check_geometry_with_area(geometry, expected_area, distance);
  195. }
  196. #endif