multi_point.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Boost.Geometry
  2. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2017-2023, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  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_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP
  11. #include <algorithm>
  12. #include <vector>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/size.hpp>
  16. #include <boost/range/value_type.hpp>
  17. #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
  18. #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
  19. #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
  20. #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
  21. #include <boost/geometry/algorithms/envelope.hpp>
  22. #include <boost/geometry/algorithms/detail/partition.hpp>
  23. #include <boost/geometry/core/tag.hpp>
  24. #include <boost/geometry/core/tag_cast.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/geometries/box.hpp>
  27. #include <boost/geometry/index/rtree.hpp>
  28. #include <boost/geometry/policies/compare.hpp>
  29. #include <boost/geometry/strategies/covered_by.hpp>
  30. #include <boost/geometry/strategies/disjoint.hpp>
  31. #include <boost/geometry/util/constexpr.hpp>
  32. #include <boost/geometry/util/type_traits.hpp>
  33. namespace boost { namespace geometry {
  34. #ifndef DOXYGEN_NO_DETAIL
  35. namespace detail { namespace within {
  36. struct multi_point_point
  37. {
  38. template <typename MultiPoint, typename Point, typename Strategy>
  39. static inline bool apply(MultiPoint const& multi_point,
  40. Point const& point,
  41. Strategy const& strategy)
  42. {
  43. auto const s = strategy.relate(multi_point, point);
  44. for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it)
  45. {
  46. if (! s.apply(*it, point))
  47. {
  48. return false;
  49. }
  50. }
  51. // all points of MultiPoint inside Point
  52. return true;
  53. }
  54. };
  55. // NOTE: currently the strategy is ignored, math::equals() is used inside geometry::less<>
  56. struct multi_point_multi_point
  57. {
  58. template <typename MultiPoint1, typename MultiPoint2, typename Strategy>
  59. static inline bool apply(MultiPoint1 const& multi_point1,
  60. MultiPoint2 const& multi_point2,
  61. Strategy const& /*strategy*/)
  62. {
  63. typedef typename boost::range_value<MultiPoint2>::type point2_type;
  64. typedef geometry::less<void, -1, Strategy> less_type;
  65. less_type const less = less_type();
  66. std::vector<point2_type> points2(boost::begin(multi_point2), boost::end(multi_point2));
  67. std::sort(points2.begin(), points2.end(), less);
  68. bool result = false;
  69. for (auto it = boost::begin(multi_point1); it != boost::end(multi_point1); ++it)
  70. {
  71. if (! std::binary_search(points2.begin(), points2.end(), *it, less))
  72. {
  73. return false;
  74. }
  75. else
  76. {
  77. result = true;
  78. }
  79. }
  80. return result;
  81. }
  82. };
  83. // TODO: the complexity could be lesser
  84. // the second geometry could be "prepared"/sorted
  85. // For Linear geometries partition could be used
  86. // For Areal geometries point_in_geometry() would have to call the winding
  87. // strategy differently, currently it linearly calls the strategy for each
  88. // segment. So the segments would have to be sorted in a way consistent with
  89. // the strategy and then the strategy called only for the segments in range.
  90. template <bool Within>
  91. struct multi_point_single_geometry
  92. {
  93. template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
  94. static inline bool apply(MultiPoint const& multi_point,
  95. LinearOrAreal const& linear_or_areal,
  96. Strategy const& strategy)
  97. {
  98. //typedef typename boost::range_value<MultiPoint>::type point1_type;
  99. typedef typename point_type<LinearOrAreal>::type point2_type;
  100. typedef model::box<point2_type> box2_type;
  101. // Create envelope of geometry
  102. box2_type box;
  103. geometry::envelope(linear_or_areal, box, strategy);
  104. geometry::detail::expand_by_epsilon(box);
  105. // Test each Point with envelope and then geometry if needed
  106. // If in the exterior, break
  107. bool result = false;
  108. for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it )
  109. {
  110. typedef decltype(strategy.covered_by(*it, box)) point_in_box_type;
  111. int in_val = 0;
  112. // exterior of box and of geometry
  113. if (! point_in_box_type::apply(*it, box)
  114. || (in_val = point_in_geometry(*it, linear_or_areal, strategy)) < 0)
  115. {
  116. result = false;
  117. break;
  118. }
  119. // interior : interior/boundary
  120. if (Within ? in_val > 0 : in_val >= 0)
  121. {
  122. result = true;
  123. }
  124. }
  125. return result;
  126. }
  127. };
  128. // TODO: same here, probably the complexity could be lesser
  129. template <bool Within>
  130. struct multi_point_multi_geometry
  131. {
  132. template <typename MultiPoint, typename LinearOrAreal, typename Strategy>
  133. static inline bool apply(MultiPoint const& multi_point,
  134. LinearOrAreal const& linear_or_areal,
  135. Strategy const& strategy)
  136. {
  137. typedef typename point_type<LinearOrAreal>::type point2_type;
  138. typedef model::box<point2_type> box2_type;
  139. static const bool is_linear = util::is_linear<LinearOrAreal>::value;
  140. // TODO: box pairs could be constructed on the fly, inside the rtree
  141. // Prepare range of envelopes and ids
  142. std::size_t count2 = boost::size(linear_or_areal);
  143. typedef std::pair<box2_type, std::size_t> box_pair_type;
  144. typedef std::vector<box_pair_type> box_pair_vector;
  145. box_pair_vector boxes(count2);
  146. for (std::size_t i = 0 ; i < count2 ; ++i)
  147. {
  148. geometry::envelope(linear_or_areal, boxes[i].first, strategy);
  149. geometry::detail::expand_by_epsilon(boxes[i].first);
  150. boxes[i].second = i;
  151. }
  152. // Create R-tree
  153. typedef index::parameters<index::rstar<4>, Strategy> index_parameters_type;
  154. index::rtree<box_pair_type, index_parameters_type>
  155. rtree(boxes.begin(), boxes.end(),
  156. index_parameters_type(index::rstar<4>(), strategy));
  157. // For each point find overlapping envelopes and test corresponding single geometries
  158. // If a point is in the exterior break
  159. bool result = false;
  160. for (auto it = boost::begin(multi_point); it != boost::end(multi_point); ++it)
  161. {
  162. // TODO: investigate the possibility of using satisfies
  163. // TODO: investigate the possibility of using iterative queries (optimization below)
  164. box_pair_vector inters_boxes;
  165. rtree.query(index::intersects(*it), std::back_inserter(inters_boxes));
  166. bool found_interior = false;
  167. bool found_boundary = false;
  168. int boundaries = 0;
  169. typedef typename box_pair_vector::const_iterator box_iterator;
  170. for (box_iterator box_it = inters_boxes.begin() ;
  171. box_it != inters_boxes.end() ; ++box_it )
  172. {
  173. int const in_val = point_in_geometry(*it,
  174. range::at(linear_or_areal, box_it->second), strategy);
  175. if (in_val > 0)
  176. {
  177. found_interior = true;
  178. }
  179. else if (in_val == 0)
  180. {
  181. ++boundaries;
  182. }
  183. // If the result was set previously (interior or
  184. // interior/boundary found) the only thing that needs to be
  185. // done for other points is to make sure they're not
  186. // overlapping the exterior no need to analyse boundaries.
  187. if (result && in_val >= 0)
  188. {
  189. break;
  190. }
  191. }
  192. if (boundaries > 0)
  193. {
  194. if BOOST_GEOMETRY_CONSTEXPR (is_linear)
  195. {
  196. if (boundaries % 2 == 0)
  197. {
  198. found_interior = true;
  199. }
  200. else
  201. {
  202. found_boundary = true;
  203. }
  204. }
  205. else
  206. {
  207. found_boundary = true;
  208. }
  209. }
  210. // exterior
  211. if (! found_interior && ! found_boundary)
  212. {
  213. result = false;
  214. break;
  215. }
  216. // interior : interior/boundary
  217. if (Within ? found_interior : (found_interior || found_boundary))
  218. {
  219. result = true;
  220. }
  221. }
  222. return result;
  223. }
  224. };
  225. }} // namespace detail::within
  226. #endif // DOXYGEN_NO_DETAIL
  227. }} // namespace boost::geometry
  228. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_WITHIN_MULTI_POINT_HPP