/Src/Dependencies/Boost/libs/geometry/test/algorithms/test_convex_hull.hpp

http://hadesmem.googlecode.com/ · C++ Header · 112 lines · 73 code · 26 blank · 13 comment · 3 complexity · eb075dc643d5b8cc53c1d04acb1313ba MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2011 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_CONVEX_HULL_HPP
  8. #define BOOST_GEOMETRY_TEST_CONVEX_HULL_HPP
  9. #include <geometry_test_common.hpp>
  10. #include <boost/geometry/algorithms/convex_hull.hpp>
  11. #include <boost/geometry/algorithms/area.hpp>
  12. #include <boost/geometry/algorithms/num_points.hpp>
  13. #include <boost/geometry/strategies/strategies.hpp>
  14. #include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
  15. #include <boost/geometry/domains/gis/io/wkt/write_wkt.hpp>
  16. #include <boost/geometry/geometries/polygon.hpp>
  17. template <typename Geometry, typename Hull>
  18. void test_convex_hull(Geometry const& geometry, Hull const& hull,
  19. std::size_t size_original, std::size_t size_hull,
  20. double expected_area, bool reverse)
  21. {
  22. std::size_t n = bg::num_points(hull);
  23. BOOST_CHECK_MESSAGE(n == size_hull,
  24. "convex hull: " << bg::wkt(geometry)
  25. << " -> " << bg::wkt(hull)
  26. << " type "
  27. << (typeid(typename bg::coordinate_type<Hull>::type).name())
  28. << " -> Expected: " << size_hull
  29. << " detected: " << n);
  30. BOOST_CHECK(bg::num_points(geometry) == size_original);
  31. typename bg::default_area_result<Geometry>::type ah = bg::area(hull);
  32. if (reverse)
  33. {
  34. ah = -ah;
  35. }
  36. //std::cout << "Area: " << bg::area(geometry) << std::endl;
  37. //std::cout << bg::wkt(hull) << std::endl;
  38. BOOST_CHECK_CLOSE(ah, expected_area, 0.001);
  39. }
  40. template <typename Geometry, bool Clockwise>
  41. void test_geometry_order(std::string const& wkt,
  42. std::size_t size_original, std::size_t size_hull,
  43. double expected_area)
  44. {
  45. Geometry geometry;
  46. bg::read_wkt(wkt, geometry);
  47. bg::model::polygon
  48. <
  49. typename bg::point_type<Geometry>::type,
  50. Clockwise
  51. > hull;
  52. // Test version with output iterator
  53. bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()));
  54. test_convex_hull(geometry, hull,
  55. size_original, size_hull, expected_area, ! Clockwise);
  56. // Test version with ring as output
  57. bg::clear(hull);
  58. bg::convex_hull(geometry, hull.outer());
  59. test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
  60. // Test version with polygon as output
  61. bg::clear(hull);
  62. bg::convex_hull(geometry, hull);
  63. test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
  64. // Test version with strategy
  65. bg::clear(hull);
  66. bg::strategy::convex_hull::graham_andrew
  67. <
  68. Geometry,
  69. typename bg::point_type<Geometry>::type
  70. > graham;
  71. bg::convex_hull(geometry, hull.outer(), graham);
  72. test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
  73. // Test version with output iterator and strategy
  74. bg::clear(hull);
  75. bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()), graham);
  76. test_convex_hull(geometry, hull, size_original, size_hull, expected_area, ! Clockwise);
  77. }
  78. template <typename Geometry>
  79. void test_geometry(std::string const& wkt,
  80. std::size_t size_original, std::size_t size_hull,
  81. double expected_area)
  82. {
  83. test_geometry_order<Geometry, true>(wkt, size_original, size_hull, expected_area);
  84. test_geometry_order<Geometry, false>(wkt, size_original, size_hull, expected_area);
  85. }
  86. #endif