test_within.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
  5. // This file was modified by Oracle on 2014, 2015, 2017, 2019.
  6. // Modifications copyright (c) 2014-2019 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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. #ifndef BOOST_GEOMETRY_TEST_WITHIN_HPP
  12. #define BOOST_GEOMETRY_TEST_WITHIN_HPP
  13. #include <boost/variant/variant.hpp>
  14. #include <geometry_test_common.hpp>
  15. #include <boost/geometry/algorithms/covered_by.hpp>
  16. #include <boost/geometry/algorithms/within.hpp>
  17. #include <boost/geometry/core/ring_type.hpp>
  18. #include <boost/geometry/geometries/ring.hpp>
  19. #include <boost/geometry/geometries/polygon.hpp>
  20. #include <boost/geometry/geometries/multi_linestring.hpp>
  21. #include <boost/geometry/io/wkt/read.hpp>
  22. #include <boost/geometry/strategies/strategies.hpp>
  23. struct no_strategy {};
  24. template <typename Geometry1, typename Geometry2, typename Strategy>
  25. bool call_within(Geometry1 const& geometry1,
  26. Geometry2 const& geometry2,
  27. Strategy const& strategy)
  28. {
  29. return bg::within(geometry1, geometry2, strategy);
  30. }
  31. template <typename Geometry1, typename Geometry2>
  32. bool call_within(Geometry1 const& geometry1,
  33. Geometry2 const& geometry2,
  34. no_strategy)
  35. {
  36. return bg::within(geometry1, geometry2);
  37. }
  38. template <typename Geometry1, typename Geometry2, typename Strategy>
  39. void check_geometry(Geometry1 const& geometry1,
  40. Geometry2 const& geometry2,
  41. std::string const& wkt1,
  42. std::string const& wkt2,
  43. bool expected,
  44. Strategy const& strategy)
  45. {
  46. bool detected = call_within(geometry1, geometry2, strategy);
  47. BOOST_CHECK_MESSAGE(detected == expected,
  48. "within: " << wkt1
  49. << " in " << wkt2
  50. << " -> Expected: " << expected
  51. << " detected: " << detected);
  52. }
  53. template <typename Geometry1, typename Geometry2>
  54. void test_geometry(std::string const& wkt1,
  55. std::string const& wkt2, bool expected)
  56. {
  57. Geometry1 geometry1;
  58. Geometry2 geometry2;
  59. bg::read_wkt(wkt1, geometry1);
  60. bg::read_wkt(wkt2, geometry2);
  61. boost::variant<Geometry1> v1(geometry1);
  62. boost::variant<Geometry2> v2(geometry2);
  63. typedef typename bg::strategy::within::services::default_strategy
  64. <
  65. Geometry1, Geometry2
  66. >::type strategy_type;
  67. check_geometry(geometry1, geometry2, wkt1, wkt2, expected, strategy_type());
  68. check_geometry(geometry1, geometry2, wkt1, wkt2, expected, no_strategy());
  69. check_geometry(v1, geometry2, wkt1, wkt2, expected, no_strategy());
  70. check_geometry(geometry1, v2, wkt1, wkt2, expected, no_strategy());
  71. check_geometry(v1, v2, wkt1, wkt2, expected, no_strategy());
  72. }
  73. template <typename Point, bool Clockwise, bool Closed>
  74. void test_ordered_ring(std::string const& wkt_point,
  75. std::string const& wkt_geometry, bool expected, bool on_border)
  76. {
  77. typedef bg::model::ring<Point, Clockwise, Closed> ring_type;
  78. ring_type ring;
  79. Point point;
  80. bg::read_wkt(wkt_geometry, ring);
  81. if ( BOOST_GEOMETRY_CONDITION(! Clockwise) )
  82. {
  83. std::reverse(boost::begin(ring), boost::end(ring));
  84. }
  85. bg::read_wkt(wkt_point, point);
  86. bool detected = bg::within(point, ring);
  87. BOOST_CHECK_MESSAGE(detected == expected,
  88. "within: " << wkt_point
  89. << " in " << wkt_geometry
  90. << " -> Expected: " << expected
  91. << " detected: " << detected
  92. << " clockwise: " << int(Clockwise)
  93. << " closed: " << int(Closed)
  94. );
  95. // other strategy (note that this one cannot detect OnBorder
  96. // (without modifications)
  97. bg::strategy::within::franklin<Point> franklin;
  98. detected = bg::within(point, ring, franklin);
  99. if (! on_border)
  100. {
  101. BOOST_CHECK_MESSAGE(detected == expected,
  102. "within: " << wkt_point
  103. << " in " << wkt_geometry
  104. << " -> Expected: " << expected
  105. << " detected: " << detected
  106. << " clockwise: " << int(Clockwise)
  107. << " closed: " << int(Closed)
  108. );
  109. }
  110. bg::strategy::within::crossings_multiply<Point> cm;
  111. detected = bg::within(point, ring, cm);
  112. if (! on_border)
  113. {
  114. BOOST_CHECK_MESSAGE(detected == expected,
  115. "within: " << wkt_point
  116. << " in " << wkt_geometry
  117. << " -> Expected: " << expected
  118. << " detected: " << detected
  119. << " clockwise: " << int(Clockwise)
  120. << " closed: " << int(Closed)
  121. );
  122. }
  123. }
  124. template <typename Point>
  125. void test_ring(std::string const& wkt_point,
  126. std::string const& wkt_geometry,
  127. bool expected, bool on_border)
  128. {
  129. test_ordered_ring<Point, true, true>(wkt_point, wkt_geometry, expected, on_border);
  130. test_ordered_ring<Point, false, true>(wkt_point, wkt_geometry, expected, on_border);
  131. test_ordered_ring<Point, true, false>(wkt_point, wkt_geometry, expected, on_border);
  132. test_ordered_ring<Point, false, false>(wkt_point, wkt_geometry, expected, on_border);
  133. test_geometry<Point, bg::model::polygon<Point> >(wkt_point, wkt_geometry, expected);
  134. }
  135. #endif