test_relate.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013, 2014, 2015, 2017.
  4. // Modifications copyright (c) 2013-2017 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_TEST_RELATE_HPP
  10. #define BOOST_GEOMETRY_TEST_RELATE_HPP
  11. #include <geometry_test_common.hpp>
  12. #include <boost/variant.hpp>
  13. #include <boost/geometry/core/ring_type.hpp>
  14. #include <boost/geometry/algorithms/relate.hpp>
  15. #include <boost/geometry/algorithms/relation.hpp>
  16. #include <boost/geometry/strategies/strategies.hpp>
  17. #include <boost/geometry/geometries/geometries.hpp>
  18. #include <boost/geometry/geometries/point_xy.hpp>
  19. #include <boost/geometry/io/wkt/read.hpp>
  20. #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
  21. #include <boost/geometry/strategies/cartesian/box_in_box.hpp>
  22. #include <boost/geometry/strategies/agnostic/point_in_box_by_side.hpp>
  23. namespace bgdr = bg::detail::relate;
  24. std::string transposed(std::string matrix)
  25. {
  26. if ( !matrix.empty() )
  27. {
  28. std::swap(matrix[1], matrix[3]);
  29. std::swap(matrix[2], matrix[6]);
  30. std::swap(matrix[5], matrix[7]);
  31. }
  32. return matrix;
  33. }
  34. bool matrix_compare(std::string const& m1, std::string const& m2)
  35. {
  36. BOOST_ASSERT(m1.size() == 9 && m2.size() == 9);
  37. for ( size_t i = 0 ; i < 9 ; ++i )
  38. {
  39. if ( m1[i] == '*' || m2[i] == '*' )
  40. continue;
  41. if ( m1[i] != m2[i] )
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool matrix_compare(std::string const& m, std::string const& res1, std::string const& res2)
  47. {
  48. return matrix_compare(m, res1)
  49. || ( !res2.empty() ? matrix_compare(m, res2) : false );
  50. }
  51. std::string matrix_format(std::string const& matrix1, std::string const& matrix2)
  52. {
  53. return matrix1
  54. + ( !matrix2.empty() ? " || " : "" ) + matrix2;
  55. }
  56. template <typename M>
  57. char get_ii(M const& m)
  58. {
  59. using bg::detail::relate::interior;
  60. return m.template get<interior, interior>();
  61. }
  62. template <typename M>
  63. char get_ee(M const& m)
  64. {
  65. using bg::detail::relate::exterior;
  66. return m.template get<exterior, exterior>();
  67. }
  68. void check_mask()
  69. {
  70. bg::de9im::mask m1("");
  71. bg::de9im::mask m2("TTT");
  72. bg::de9im::mask m3("000111222");
  73. bg::de9im::mask m4("000111222FFFF");
  74. bg::de9im::mask m5(std::string(""));
  75. bg::de9im::mask m6(std::string("TTT"));
  76. bg::de9im::mask m7(std::string("000111222"));
  77. bg::de9im::mask m8(std::string("000111222FFFF"));
  78. using bg::detail::relate::interior;
  79. using bg::detail::relate::exterior;
  80. BOOST_CHECK(get_ii(m1) == '*' && get_ee(m1) == '*');
  81. BOOST_CHECK(get_ii(m2) == 'T' && get_ee(m2) == '*');
  82. BOOST_CHECK(get_ii(m3) == '0' && get_ee(m3) == '2');
  83. BOOST_CHECK(get_ii(m4) == '0' && get_ee(m4) == '2');
  84. BOOST_CHECK(get_ii(m5) == '*' && get_ee(m5) == '*');
  85. BOOST_CHECK(get_ii(m6) == 'T' && get_ee(m6) == '*');
  86. BOOST_CHECK(get_ii(m7) == '0' && get_ee(m7) == '2');
  87. BOOST_CHECK(get_ii(m8) == '0' && get_ee(m8) == '2');
  88. }
  89. template <typename Geometry1, typename Geometry2>
  90. void check_geometry(Geometry1 const& geometry1,
  91. Geometry2 const& geometry2,
  92. std::string const& wkt1,
  93. std::string const& wkt2,
  94. std::string const& expected1,
  95. std::string const& expected2 = std::string())
  96. {
  97. boost::variant<Geometry1> variant1 = geometry1;
  98. boost::variant<Geometry2> variant2 = geometry2;
  99. {
  100. std::string res_str = bg::relation(geometry1, geometry2).str();
  101. bool ok = matrix_compare(res_str, expected1, expected2);
  102. BOOST_CHECK_MESSAGE(ok,
  103. "relate: " << wkt1
  104. << " and " << wkt2
  105. << " -> Expected: " << matrix_format(expected1, expected2)
  106. << " detected: " << res_str);
  107. typedef typename bg::strategy::relate::services::default_strategy
  108. <
  109. Geometry1, Geometry2
  110. >::type strategy_type;
  111. std::string res_str0 = bg::relation(geometry1, geometry2, strategy_type()).str();
  112. BOOST_CHECK(res_str == res_str0);
  113. // test variants
  114. boost::variant<Geometry1> v1 = geometry1;
  115. boost::variant<Geometry2> v2 = geometry2;
  116. std::string res_str1 = bg::relation(geometry1, variant2).str();
  117. std::string res_str2 = bg::relation(variant1, geometry2).str();
  118. std::string res_str3 = bg::relation(variant1, variant2).str();
  119. BOOST_CHECK(res_str == res_str1);
  120. BOOST_CHECK(res_str == res_str2);
  121. BOOST_CHECK(res_str == res_str3);
  122. }
  123. // changed sequence of geometries - transposed result
  124. {
  125. std::string res_str = bg::relation(geometry2, geometry1).str();
  126. std::string expected1_tr = transposed(expected1);
  127. std::string expected2_tr = transposed(expected2);
  128. bool ok = matrix_compare(res_str, expected1_tr, expected2_tr);
  129. BOOST_CHECK_MESSAGE(ok,
  130. "relate: " << wkt2
  131. << " and " << wkt1
  132. << " -> Expected: " << matrix_format(expected1_tr, expected2_tr)
  133. << " detected: " << res_str);
  134. }
  135. if ( expected2.empty() )
  136. {
  137. {
  138. bool result = bg::relate(geometry1, geometry2, bg::de9im::mask(expected1));
  139. // TODO: SHOULD BE !interrupted - CHECK THIS!
  140. BOOST_CHECK_MESSAGE(result,
  141. "relate: " << wkt1
  142. << " and " << wkt2
  143. << " -> Expected: " << expected1);
  144. typedef typename bg::strategy::relate::services::default_strategy
  145. <
  146. Geometry1, Geometry2
  147. >::type strategy_type;
  148. bool result0 = bg::relate(geometry1, geometry2, bg::de9im::mask(expected1), strategy_type());
  149. BOOST_CHECK(result == result0);
  150. // test variants
  151. bool result1 = bg::relate(geometry1, variant2, bg::de9im::mask(expected1));
  152. bool result2 = bg::relate(variant1, geometry2, bg::de9im::mask(expected1));
  153. bool result3 = bg::relate(variant1, variant2, bg::de9im::mask(expected1));
  154. BOOST_CHECK(result == result1);
  155. BOOST_CHECK(result == result2);
  156. BOOST_CHECK(result == result3);
  157. }
  158. if ( BOOST_GEOMETRY_CONDITION((
  159. bg::detail::relate::interruption_enabled<Geometry1, Geometry2>::value )) )
  160. {
  161. // brake the expected output
  162. std::string expected_interrupt = expected1;
  163. bool changed = false;
  164. BOOST_FOREACH(char & c, expected_interrupt)
  165. {
  166. if ( c >= '0' && c <= '9' )
  167. {
  168. if ( c == '0' )
  169. c = 'F';
  170. else
  171. --c;
  172. changed = true;
  173. }
  174. }
  175. if ( changed )
  176. {
  177. bool result = bg::relate(geometry1, geometry2, bg::de9im::mask(expected_interrupt));
  178. // TODO: SHOULD BE interrupted - CHECK THIS!
  179. BOOST_CHECK_MESSAGE(!result,
  180. "relate: " << wkt1
  181. << " and " << wkt2
  182. << " -> Expected interrupt for:" << expected_interrupt);
  183. }
  184. }
  185. }
  186. }
  187. template <typename Geometry1, typename Geometry2>
  188. void test_geometry(std::string const& wkt1,
  189. std::string const& wkt2,
  190. std::string const& expected1,
  191. std::string const& expected2 = std::string())
  192. {
  193. Geometry1 geometry1;
  194. Geometry2 geometry2;
  195. bg::read_wkt(wkt1, geometry1);
  196. bg::read_wkt(wkt2, geometry2);
  197. check_geometry(geometry1, geometry2, wkt1, wkt2, expected1, expected2);
  198. }
  199. #endif // BOOST_GEOMETRY_TEST_RELATE_HPP