correct_closure.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2017 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 <sstream>
  8. #include <geometry_test_common.hpp>
  9. #include <boost/geometry/strategies/strategies.hpp>
  10. #include <boost/geometry/geometries/point_xy.hpp>
  11. #include <boost/geometry/geometries/ring.hpp>
  12. #include <boost/geometry/geometries/polygon.hpp>
  13. #include <boost/geometry/algorithms/correct_closure.hpp>
  14. #include <boost/geometry/io/wkt/read.hpp>
  15. #include <boost/geometry/io/wkt/write.hpp>
  16. #include <boost/variant/variant.hpp>
  17. template <typename BaseGeometry, typename Geometry>
  18. void check_geometry(Geometry const& geometry, std::string const& expected)
  19. {
  20. std::ostringstream out;
  21. out << bg::wkt_manipulator<Geometry>(geometry, false);
  22. BOOST_CHECK_EQUAL(out.str(), expected);
  23. }
  24. template <typename Geometry>
  25. void test_geometry(std::string const& wkt, std::string const& expected)
  26. {
  27. Geometry geometry;
  28. bg::read_wkt(wkt, geometry);
  29. // Test tye type
  30. bg::correct_closure(geometry);
  31. check_geometry<Geometry>(geometry, expected);
  32. // Test varianted type
  33. boost::variant<Geometry> v(geometry);
  34. bg::correct_closure(v);
  35. check_geometry<Geometry>(v, expected);
  36. }
  37. template <typename P>
  38. void test_all()
  39. {
  40. typedef bg::model::ring<P, true, true> cw_closed_ring_type;
  41. typedef bg::model::ring<P, true, false> cw_open_ring_type;
  42. typedef bg::model::ring<P, false, true> ccw_closed_ring_type;
  43. typedef bg::model::ring<P, false, false> ccw_open_ring_type;
  44. // Define clockwise and counter clockwise polygon
  45. std::string cw_ring = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
  46. std::string cw_open_ring = "POLYGON((0 0,0 1,1 1,1 0))";
  47. std::string ccw_ring = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
  48. std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";
  49. // Cases which should be closed or opened
  50. test_geometry<cw_closed_ring_type>(cw_open_ring, cw_ring);
  51. test_geometry<cw_open_ring_type>(cw_ring, cw_open_ring);
  52. test_geometry<ccw_closed_ring_type>(ccw_open_ring, ccw_ring);
  53. test_geometry<ccw_open_ring_type>(ccw_ring, ccw_open_ring);
  54. // Cases which are incorrect but should still be closed or opened
  55. test_geometry<cw_closed_ring_type>(ccw_open_ring, ccw_ring);
  56. test_geometry<ccw_open_ring_type>(cw_ring, cw_open_ring);
  57. // Cases where no action is necessary (even if order is incorrect)
  58. test_geometry<cw_closed_ring_type>(cw_ring, cw_ring);
  59. test_geometry<cw_closed_ring_type>(ccw_ring, ccw_ring);
  60. test_geometry<cw_open_ring_type>(cw_open_ring, cw_open_ring);
  61. test_geometry<cw_open_ring_type>(ccw_open_ring, ccw_open_ring);
  62. test_geometry<ccw_closed_ring_type>(cw_ring, cw_ring);
  63. test_geometry<ccw_closed_ring_type>(ccw_ring, ccw_ring);
  64. test_geometry<ccw_open_ring_type>(cw_open_ring, cw_open_ring);
  65. test_geometry<ccw_open_ring_type>(ccw_open_ring, ccw_open_ring);
  66. // Polygon cases
  67. std::string cw_polygon =
  68. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
  69. std::string cw_open_polygon =
  70. "POLYGON((0 0,0 4,4 4,4 0),(1 1,2 1,2 2,1 2))";
  71. typedef bg::model::polygon<P, true, true> cw_closed_polygon_type;
  72. typedef bg::model::polygon<P, true, false> cw_open_polygon_type;
  73. test_geometry<cw_closed_polygon_type>(cw_open_polygon, cw_polygon);
  74. test_geometry<cw_open_polygon_type>(cw_polygon, cw_open_polygon);
  75. test_geometry<cw_closed_polygon_type>(cw_polygon, cw_polygon);
  76. test_geometry<cw_open_polygon_type>(cw_open_polygon, cw_open_polygon);
  77. }
  78. int test_main(int, char* [])
  79. {
  80. test_all<bg::model::d2::point_xy<int> >();
  81. test_all<bg::model::d2::point_xy<float> >();
  82. test_all<bg::model::d2::point_xy<double> >();
  83. test_all<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  84. test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
  85. return 0;
  86. }