/Src/Dependencies/Boost/libs/geometry/test/algorithms/correct.cpp

http://hadesmem.googlecode.com/ · C++ · 182 lines · 94 code · 50 blank · 38 comment · 0 complexity · 4ef8342bffed2451d6952c4c7dcc81f0 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. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  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. #include <sstream>
  12. #include <geometry_test_common.hpp>
  13. #include <boost/geometry/algorithms/correct.hpp>
  14. #include <boost/geometry/strategies/strategies.hpp>
  15. #include <boost/geometry/util/write_dsv.hpp>
  16. #include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
  17. #include <boost/geometry/domains/gis/io/wkt/write_wkt.hpp>
  18. #include <boost/geometry/geometries/point_xy.hpp>
  19. #include <boost/geometry/geometries/box.hpp>
  20. #include <boost/geometry/geometries/ring.hpp>
  21. #include <boost/geometry/geometries/polygon.hpp>
  22. template <typename Geometry>
  23. void test_geometry(std::string const& wkt, std::string const& expected)
  24. {
  25. Geometry geometry;
  26. bg::read_wkt(wkt, geometry);
  27. bg::correct(geometry);
  28. std::ostringstream out;
  29. out << bg::wkt(geometry);
  30. BOOST_CHECK_EQUAL(out.str(), expected);
  31. }
  32. // Note: 3D/box test cannot be done using WKT because:
  33. // -> wkt-box does not exist
  34. // -> so it is converted to a ring
  35. // -> ring representation of 3d-box is not supported, nor feasible
  36. // -> so it uses DSV which can represent a box
  37. template <typename Geometry>
  38. void test_geometry_dsv(std::string const& wkt, std::string const& expected)
  39. {
  40. Geometry geometry;
  41. bg::read_wkt(wkt, geometry);
  42. bg::correct(geometry);
  43. std::ostringstream out;
  44. out << bg::dsv(geometry);
  45. BOOST_CHECK_EQUAL(out.str(), expected);
  46. }
  47. template <typename P>
  48. void test_all()
  49. {
  50. // Define clockwise and counter clockwise polygon
  51. std::string cw_ring = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
  52. std::string ccw_ring = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
  53. std::string cw_open_ring = "POLYGON((0 0,0 1,1 1,1 0))";
  54. std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";
  55. // already cw_ring
  56. test_geometry<bg::model::ring<P> >(cw_ring, cw_ring);
  57. // wrong order
  58. test_geometry<bg::model::ring<P> >(ccw_ring, cw_ring);
  59. // ccw-ring, input ccw-ring, already correct
  60. test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
  61. // ccw-ring, input cw-ring, corrected
  62. test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
  63. // open-ring, input ccw-ring, already correct
  64. test_geometry<bg::model::ring<P, true, false> >(cw_open_ring, cw_open_ring);
  65. // ccw-ring, input cw-ring, corrected
  66. test_geometry<bg::model::ring<P, true, false> >(ccw_open_ring, "POLYGON((0 1,1 1,1 0,0 0))");
  67. // not closed
  68. test_geometry<bg::model::ring<P> >(
  69. ccw_open_ring,
  70. cw_ring);
  71. // counter clockwise, cw_ring
  72. test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
  73. test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
  74. // polygon: cw_ring
  75. test_geometry<bg::model::polygon<P> >(cw_ring, cw_ring);
  76. // wrong order
  77. test_geometry<bg::model::polygon<P> >(
  78. "POLYGON((0 0,1 0,1 1,0 1,0 0))",
  79. cw_ring);
  80. // wrong order & not closed
  81. test_geometry<bg::model::polygon<P> >(
  82. ccw_open_ring,
  83. cw_ring);
  84. std::string cw_holey_polygon =
  85. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
  86. std::string ccw_holey_polygon =
  87. "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,1 2,2 2,2 1,1 1))";
  88. // with holes: cw_ring
  89. test_geometry<bg::model::polygon<P> >(
  90. cw_holey_polygon,
  91. cw_holey_polygon);
  92. // wrong order of main
  93. test_geometry<bg::model::polygon<P> >(
  94. "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  95. cw_holey_polygon);
  96. // wrong order of hole
  97. test_geometry<bg::model::polygon<P> >(
  98. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1))",
  99. cw_holey_polygon);
  100. // wrong order of main and hole
  101. test_geometry<bg::model::polygon<P> >(ccw_holey_polygon, cw_holey_polygon);
  102. // test the counter-clockwise
  103. test_geometry<bg::model::polygon<P, false> >(
  104. ccw_holey_polygon, ccw_holey_polygon);
  105. // Boxes
  106. std::string proper_box = "POLYGON((0 0,0 2,2 2,2 0,0 0))";
  107. test_geometry<bg::model::box<P> >(proper_box, proper_box);
  108. test_geometry<bg::model::box<P> >("BOX(0 0,2 2)", proper_box);
  109. test_geometry<bg::model::box<P> >("BOX(2 2,0 0)", proper_box);
  110. test_geometry<bg::model::box<P> >("BOX(0 2,2 0)", proper_box);
  111. // Cubes
  112. typedef bg::model::box<bg::model::point<double, 3, bg::cs::cartesian> > box3d;
  113. std::string proper_3d_dsv_box = "((0, 0, 0), (2, 2, 2))";
  114. test_geometry_dsv<box3d>("BOX(0 0 0,2 2 2)", proper_3d_dsv_box);
  115. test_geometry_dsv<box3d>("BOX(2 2 2,0 0 0)", proper_3d_dsv_box);
  116. test_geometry_dsv<box3d>("BOX(0 2 2,2 0 0)", proper_3d_dsv_box);
  117. test_geometry_dsv<box3d>("BOX(2 0 2,0 2 0)", proper_3d_dsv_box);
  118. test_geometry_dsv<box3d>("BOX(0 0 2,2 2 0)", proper_3d_dsv_box);
  119. }
  120. int test_main(int, char* [])
  121. {
  122. //test_all<int[2]>();
  123. //test_all<float[2]>(); not yet because cannot be copied, for polygon
  124. //test_all<double[2]>();
  125. //test_all<point_xy<int> >();
  126. test_all<bg::model::d2::point_xy<float> >();
  127. test_all<bg::model::d2::point_xy<double> >();
  128. #if defined(HAVE_TTMATH)
  129. test_all<bg::model::d2::point_xy<ttmath_big> >();
  130. #endif
  131. return 0;
  132. }