correct.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // This file was modified by Oracle on 2017.
  7. // Modifications copyright (c) 2017 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #include <sstream>
  15. #include <algorithms/test_correct.hpp>
  16. #include <boost/geometry/strategies/strategies.hpp>
  17. #include <boost/geometry/io/dsv/write.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. // Note: 3D/box test cannot be done using WKT because:
  23. // -> wkt-box does not exist
  24. // -> so it is converted to a ring
  25. // -> ring representation of 3d-box is not supported, nor feasible
  26. // -> so it uses DSV which can represent a box
  27. template <typename Geometry>
  28. void test_geometry_dsv(std::string const& wkt, std::string const& expected)
  29. {
  30. Geometry geometry;
  31. bg::read_wkt(wkt, geometry);
  32. bg::correct(geometry);
  33. std::ostringstream out;
  34. out << bg::dsv(geometry);
  35. BOOST_CHECK_EQUAL(out.str(), expected);
  36. }
  37. template <typename P>
  38. void test_ring_polygon()
  39. {
  40. // Define clockwise and counter clockwise polygon
  41. std::string cw_ring = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
  42. std::string ccw_ring = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
  43. std::string cw_open_ring = "POLYGON((0 0,0 1,1 1,1 0))";
  44. std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";
  45. // already cw_ring
  46. test_geometry<bg::model::ring<P> >(cw_ring, cw_ring);
  47. // wrong order
  48. test_geometry<bg::model::ring<P> >(ccw_ring, cw_ring);
  49. // ccw-ring, input ccw-ring, already correct
  50. test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
  51. // ccw-ring, input cw-ring, corrected
  52. test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
  53. // open-ring, input ccw-ring, already correct
  54. test_geometry<bg::model::ring<P, true, false> >(cw_open_ring, cw_open_ring);
  55. // ccw-ring, input cw-ring, corrected
  56. test_geometry<bg::model::ring<P, true, false> >(ccw_open_ring, "POLYGON((0 1,1 1,1 0,0 0))");
  57. // not closed
  58. test_geometry<bg::model::ring<P> >(
  59. ccw_open_ring,
  60. cw_ring);
  61. // counter clockwise, cw_ring
  62. test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
  63. test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
  64. // polygon: cw_ring
  65. test_geometry<bg::model::polygon<P> >(cw_ring, cw_ring);
  66. // wrong order
  67. test_geometry<bg::model::polygon<P> >(
  68. "POLYGON((0 0,1 0,1 1,0 1,0 0))",
  69. cw_ring);
  70. // wrong order & not closed
  71. test_geometry<bg::model::polygon<P> >(
  72. ccw_open_ring,
  73. cw_ring);
  74. std::string cw_holey_polygon =
  75. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
  76. std::string ccw_holey_polygon =
  77. "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,1 2,2 2,2 1,1 1))";
  78. // with holes: cw_ring
  79. test_geometry<bg::model::polygon<P> >(
  80. cw_holey_polygon,
  81. cw_holey_polygon);
  82. // wrong order of main
  83. test_geometry<bg::model::polygon<P> >(
  84. "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
  85. cw_holey_polygon);
  86. // wrong order of hole
  87. test_geometry<bg::model::polygon<P> >(
  88. "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1))",
  89. cw_holey_polygon);
  90. // wrong order of main and hole
  91. test_geometry<bg::model::polygon<P> >(ccw_holey_polygon, cw_holey_polygon);
  92. // test the counter-clockwise
  93. test_geometry<bg::model::polygon<P, false> >(
  94. ccw_holey_polygon, ccw_holey_polygon);
  95. }
  96. template <typename P>
  97. void test_box()
  98. {
  99. // Boxes. Reference is an open box (because in this test WKT is not
  100. // explicitly closed)
  101. std::string proper_box = "POLYGON((0 0,0 2,2 2,2 0))";
  102. test_geometry<bg::model::box<P> >(proper_box, proper_box);
  103. test_geometry<bg::model::box<P> >("BOX(0 0,2 2)", proper_box);
  104. test_geometry<bg::model::box<P> >("BOX(2 2,0 0)", proper_box);
  105. test_geometry<bg::model::box<P> >("BOX(0 2,2 0)", proper_box);
  106. // Cubes
  107. typedef bg::model::box<bg::model::point<double, 3, bg::cs::cartesian> > box3d;
  108. std::string proper_3d_dsv_box = "((0, 0, 0), (2, 2, 2))";
  109. test_geometry_dsv<box3d>("BOX(0 0 0,2 2 2)", proper_3d_dsv_box);
  110. test_geometry_dsv<box3d>("BOX(2 2 2,0 0 0)", proper_3d_dsv_box);
  111. test_geometry_dsv<box3d>("BOX(0 2 2,2 0 0)", proper_3d_dsv_box);
  112. test_geometry_dsv<box3d>("BOX(2 0 2,0 2 0)", proper_3d_dsv_box);
  113. test_geometry_dsv<box3d>("BOX(0 0 2,2 2 0)", proper_3d_dsv_box);
  114. }
  115. template <typename P>
  116. void test_all()
  117. {
  118. test_ring_polygon<P>();
  119. test_box<P>();
  120. }
  121. int test_main(int, char* [])
  122. {
  123. //test_all<int[2]>();
  124. //test_all<float[2]>(); not yet because cannot be copied, for polygon
  125. //test_all<double[2]>();
  126. test_all<bg::model::d2::point_xy<int> >();
  127. test_all<bg::model::d2::point_xy<float> >();
  128. test_all<bg::model::d2::point_xy<double> >();
  129. test_ring_polygon<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
  130. test_ring_polygon<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
  131. #if defined(HAVE_TTMATH)
  132. test_all<bg::model::d2::point_xy<ttmath_big> >();
  133. #endif
  134. return 0;
  135. }