test_convex_hull.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2014, 2015.
  5. // Modifications copyright (c) 2014-2015 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_TEST_CONVEX_HULL_HPP
  12. #define BOOST_GEOMETRY_TEST_CONVEX_HULL_HPP
  13. #include <boost/variant/variant.hpp>
  14. #include <geometry_test_common.hpp>
  15. #include <boost/geometry/algorithms/convex_hull.hpp>
  16. #include <boost/geometry/algorithms/area.hpp>
  17. #include <boost/geometry/algorithms/is_empty.hpp>
  18. #include <boost/geometry/algorithms/num_points.hpp>
  19. #include <boost/geometry/algorithms/perimeter.hpp>
  20. #include <boost/geometry/strategies/strategies.hpp>
  21. #include <boost/geometry/io/wkt/wkt.hpp>
  22. #include <boost/geometry/geometries/polygon.hpp>
  23. template <typename Geometry, typename Hull>
  24. void check_convex_hull(Geometry const& geometry, Hull const& hull,
  25. std::size_t /*size_original*/, std::size_t size_hull,
  26. double expected_area, double expected_perimeter,
  27. bool reverse)
  28. {
  29. std::size_t n = bg::num_points(hull);
  30. BOOST_CHECK_MESSAGE(n == size_hull,
  31. "convex hull: " << bg::wkt(geometry)
  32. << " -> " << bg::wkt(hull)
  33. << " type "
  34. << (typeid(typename bg::coordinate_type<Hull>::type).name())
  35. << " -> Expected: " << size_hull
  36. << " detected: " << n);
  37. // We omit this check as it is not important for the hull algorithm
  38. // BOOST_CHECK(bg::num_points(geometry) == size_original);
  39. typename bg::default_area_result<Geometry>::type ah = bg::area(hull);
  40. if (reverse)
  41. {
  42. ah = -ah;
  43. }
  44. BOOST_CHECK_CLOSE(ah, expected_area, 0.001);
  45. if ( expected_perimeter >= 0 )
  46. {
  47. typename bg::default_length_result<Geometry>::type
  48. ph = bg::perimeter(hull);
  49. BOOST_CHECK_CLOSE(ph, expected_perimeter, 0.001);
  50. }
  51. }
  52. namespace resolve_variant {
  53. struct closure_visitor : public boost::static_visitor<bg::closure_selector>
  54. {
  55. template <typename Geometry>
  56. bg::closure_selector operator()(Geometry const&) const
  57. {
  58. return bg::closure<Geometry>::value;
  59. }
  60. };
  61. template <typename Geometry>
  62. inline bg::closure_selector get_closure(Geometry const&)
  63. {
  64. return bg::closure<Geometry>::value;
  65. }
  66. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  67. inline bg::closure_selector get_closure(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& v)
  68. {
  69. return boost::apply_visitor(closure_visitor(), v);
  70. }
  71. } // namespace resolve_variant
  72. template <typename Hull, typename Strategy, typename Geometry>
  73. void test_convex_hull(Geometry const& geometry,
  74. std::size_t size_original, std::size_t size_hull_closed,
  75. double expected_area, double expected_perimeter,
  76. bool reverse)
  77. {
  78. bool const is_original_closed = resolve_variant::get_closure(geometry) != bg::open;
  79. static bool const is_hull_closed = bg::closure<Hull>::value != bg::open;
  80. // convex_hull_insert() uses the original Geometry as a source of the info about the order and closure
  81. std::size_t const size_hull_from_orig = is_original_closed ? size_hull_closed : size_hull_closed - 1;
  82. std::size_t const size_hull = is_hull_closed ? size_hull_closed : size_hull_closed - 1;
  83. Hull hull;
  84. // Test version with output iterator
  85. bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()));
  86. check_convex_hull(geometry, hull, size_original, size_hull_from_orig, expected_area, expected_perimeter, reverse);
  87. // Test version with ring as output
  88. bg::clear(hull);
  89. bg::convex_hull(geometry, hull.outer());
  90. check_convex_hull(geometry, hull, size_original, size_hull, expected_area, expected_perimeter, false);
  91. // Test version with polygon as output
  92. bg::clear(hull);
  93. bg::convex_hull(geometry, hull);
  94. check_convex_hull(geometry, hull, size_original, size_hull, expected_area, expected_perimeter, false);
  95. // Test version with strategy
  96. bg::clear(hull);
  97. bg::convex_hull(geometry, hull.outer(), Strategy());
  98. check_convex_hull(geometry, hull, size_original, size_hull, expected_area, expected_perimeter, false);
  99. // Test version with output iterator and strategy
  100. bg::clear(hull);
  101. bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()), Strategy());
  102. check_convex_hull(geometry, hull, size_original, size_hull_from_orig, expected_area, expected_perimeter, reverse);
  103. }
  104. template <typename Geometry, bool Clockwise, bool Closed>
  105. void test_geometry_order(std::string const& wkt,
  106. std::size_t size_original, std::size_t size_hull_closed,
  107. double expected_area, double expected_perimeter = -1.0)
  108. {
  109. typedef bg::model::polygon
  110. <
  111. typename bg::point_type<Geometry>::type,
  112. Clockwise,
  113. Closed
  114. > hull_type;
  115. typedef bg::strategy::convex_hull::graham_andrew
  116. <
  117. Geometry,
  118. typename bg::point_type<Geometry>::type
  119. > strategy_type;
  120. Geometry geometry;
  121. bg::read_wkt(wkt, geometry);
  122. boost::variant<Geometry> v(geometry);
  123. test_convex_hull<hull_type, strategy_type>(geometry, size_original, size_hull_closed, expected_area, expected_perimeter, !Clockwise);
  124. test_convex_hull<hull_type, strategy_type>(v, size_original, size_hull_closed, expected_area, expected_perimeter, !Clockwise);
  125. }
  126. template <typename Geometry>
  127. void test_geometry(std::string const& wkt,
  128. std::size_t size_original, std::size_t size_hull_closed,
  129. double expected_area, double expected_perimeter = -1.0)
  130. {
  131. test_geometry_order<Geometry, true, true>(wkt, size_original, size_hull_closed, expected_area, expected_perimeter);
  132. test_geometry_order<Geometry, false, true>(wkt, size_original, size_hull_closed, expected_area, expected_perimeter);
  133. test_geometry_order<Geometry, true, false>(wkt, size_original, size_hull_closed, expected_area, expected_perimeter);
  134. test_geometry_order<Geometry, false, false>(wkt, size_original, size_hull_closed, expected_area, expected_perimeter);
  135. }
  136. template <typename Geometry>
  137. void test_empty_input()
  138. {
  139. Geometry geometry;
  140. bg::model::polygon
  141. <
  142. typename bg::point_type<Geometry>::type
  143. > hull;
  144. bg::convex_hull(geometry, hull);
  145. BOOST_CHECK_MESSAGE(bg::is_empty(hull), "Output convex hull should be empty" );
  146. }
  147. #endif