sym_difference_areal_areal.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2015 Oracle and/or its affiliates.
  3. // Licensed under the Boost Software License version 1.0.
  4. // http://www.boost.org/users/license.html
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. #include <iostream>
  7. #ifndef BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_sym_difference_areal_areal
  9. #endif
  10. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  11. #define BOOST_GEOMETRY_DEBUG_TURNS
  12. #define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER
  13. #endif
  14. #include <boost/test/included/unit_test.hpp>
  15. #include <boost/geometry/geometries/linestring.hpp>
  16. #include <boost/geometry/geometries/multi_linestring.hpp>
  17. #include <boost/geometry/algorithms/sym_difference.hpp>
  18. #include "../difference/test_difference.hpp"
  19. #include <from_wkt.hpp>
  20. typedef bg::model::point<double,2,bg::cs::cartesian> point_type;
  21. typedef bg::model::ring<point_type> ring_type; // ccw, closed
  22. typedef bg::model::polygon<point_type> polygon_type; // ccw, closed
  23. typedef bg::model::multi_polygon<polygon_type> multi_polygon_type;
  24. double const default_tolerance = 0.0001;
  25. template
  26. <
  27. typename Areal1, typename Areal2, typename PolygonOut
  28. >
  29. struct test_sym_difference_of_areal_geometries
  30. {
  31. static inline void apply(std::string const& case_id,
  32. Areal1 const& areal1,
  33. Areal2 const& areal2,
  34. int expected_polygon_count,
  35. int expected_point_count,
  36. double expected_area,
  37. double tolerance = default_tolerance)
  38. {
  39. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  40. bg::model::multi_polygon<PolygonOut> sdf;
  41. bg::sym_difference(areal1, areal2, sdf);
  42. std::cout << "Case ID: " << case_id << std::endl;
  43. std::cout << "Geometry #1: " << bg::wkt(areal1) << std::endl;
  44. std::cout << "Geometry #2: " << bg::wkt(areal2) << std::endl;
  45. std::cout << "Sym diff: " << bg::wkt(sdf) << std::endl;
  46. std::cout << "Polygon count: expected: "
  47. << expected_polygon_count
  48. << "; detected: " << sdf.size() << std::endl;
  49. std::cout << "Point count: expected: "
  50. << expected_point_count
  51. << "; detected: " << bg::num_points(sdf) << std::endl;
  52. std::cout << "Area: expected: "
  53. << expected_area
  54. << "; detected: " << bg::area(sdf) << std::endl;
  55. #endif
  56. ut_settings settings;
  57. settings.percentage = tolerance;
  58. test_difference
  59. <
  60. PolygonOut
  61. >(case_id, areal1, areal2,
  62. expected_polygon_count, expected_point_count, expected_area,
  63. true, settings);
  64. }
  65. };
  66. //===========================================================================
  67. //===========================================================================
  68. //===========================================================================
  69. BOOST_AUTO_TEST_CASE( test_sym_difference_ring_ring )
  70. {
  71. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  72. std::cout << std::endl << std::endl << std::endl;
  73. std::cout << "*** RING / RING SYMMETRIC DIFFERENCE ***" << std::endl;
  74. std::cout << std::endl;
  75. #endif
  76. typedef ring_type R;
  77. typedef polygon_type PG;
  78. typedef test_sym_difference_of_areal_geometries<R, R, PG> tester;
  79. tester::apply("r-r-sdf00",
  80. from_wkt<R>("POLYGON((0 0,0 10,10 10,10 0,0 0))"),
  81. from_wkt<R>("POLYGON((10 0,10 20,20 20,20 0,10 0))"),
  82. 1,
  83. 8,
  84. 300);
  85. tester::apply("r-r-sdf01",
  86. from_wkt<R>("POLYGON((0 0,0 10,10 10,10 0,0 0))"),
  87. from_wkt<R>("POLYGON((9 0,9 20,20 20,20 0,9 0))"),
  88. 2,
  89. 12,
  90. 300);
  91. }
  92. BOOST_AUTO_TEST_CASE( test_sym_difference_polygon_multipolygon )
  93. {
  94. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  95. std::cout << std::endl << std::endl << std::endl;
  96. std::cout << "*** POLYGON / MULTIPOLYGON SYMMETRIC DIFFERENCE ***"
  97. << std::endl;
  98. std::cout << std::endl;
  99. #endif
  100. typedef polygon_type PG;
  101. typedef multi_polygon_type MPG;
  102. typedef test_sym_difference_of_areal_geometries<PG, MPG, PG> tester;
  103. tester::apply
  104. ("pg-mpg-sdf00",
  105. from_wkt<PG>("POLYGON((10 0,10 10,20 10,20 0,10 0))"),
  106. from_wkt<MPG>("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)),\
  107. ((20 0,20 10,30 10,30 0,20 0)))"),
  108. 1,
  109. 6,
  110. 300);
  111. }