pointlike_areal.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2020, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_AREAL_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_AREAL_HPP
  8. #include <vector>
  9. #include <boost/range/begin.hpp>
  10. #include <boost/range/end.hpp>
  11. #include <boost/range/size.hpp>
  12. #include <boost/range/value_type.hpp>
  13. #include <boost/geometry/algorithms/disjoint.hpp>
  14. #include <boost/geometry/algorithms/envelope.hpp>
  15. #include <boost/geometry/algorithms/expand.hpp>
  16. #include <boost/geometry/algorithms/not_implemented.hpp>
  17. #include <boost/geometry/algorithms/detail/not.hpp>
  18. #include <boost/geometry/algorithms/detail/partition.hpp>
  19. #include <boost/geometry/algorithms/detail/disjoint/point_geometry.hpp>
  20. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  21. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  22. #include <boost/geometry/algorithms/detail/overlay/pointlike_linear.hpp>
  23. #include <boost/geometry/core/tags.hpp>
  24. #include <boost/geometry/geometries/box.hpp>
  25. #include <boost/geometry/geometries/point.hpp>
  26. // TEMP
  27. #include <boost/geometry/strategies/envelope/cartesian.hpp>
  28. #include <boost/geometry/strategies/envelope/geographic.hpp>
  29. #include <boost/geometry/strategies/envelope/spherical.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace overlay
  34. {
  35. // difference/intersection of multipoint-multipolygon
  36. template
  37. <
  38. typename MultiPoint,
  39. typename MultiPolygon,
  40. typename PointOut,
  41. overlay_type OverlayType,
  42. typename Policy
  43. >
  44. class multipoint_multipolygon_point
  45. {
  46. private:
  47. template <typename Strategy>
  48. struct expand_box_point
  49. {
  50. explicit expand_box_point(Strategy const& strategy)
  51. : m_strategy(strategy)
  52. {}
  53. template <typename Box, typename Point>
  54. inline void apply(Box& total, Point const& point) const
  55. {
  56. geometry::expand(total, point, m_strategy);
  57. }
  58. Strategy const& m_strategy;
  59. };
  60. template <typename Strategy>
  61. struct expand_box_boxpair
  62. {
  63. explicit expand_box_boxpair(Strategy const& strategy)
  64. : m_strategy(strategy)
  65. {}
  66. template <typename Box1, typename Box2, typename SizeT>
  67. inline void apply(Box1& total, std::pair<Box2, SizeT> const& box_pair) const
  68. {
  69. geometry::expand(total, box_pair.first, m_strategy);
  70. }
  71. Strategy const& m_strategy;
  72. };
  73. template <typename Strategy>
  74. struct overlaps_box_point
  75. {
  76. explicit overlaps_box_point(Strategy const& strategy)
  77. : m_strategy(strategy)
  78. {}
  79. template <typename Box, typename Point>
  80. inline bool apply(Box const& box, Point const& point) const
  81. {
  82. return ! geometry::disjoint(point, box, m_strategy);
  83. }
  84. Strategy const& m_strategy;
  85. };
  86. template <typename Strategy>
  87. struct overlaps_box_boxpair
  88. {
  89. explicit overlaps_box_boxpair(Strategy const& strategy)
  90. : m_strategy(strategy)
  91. {}
  92. template <typename Box1, typename Box2, typename SizeT>
  93. inline bool apply(Box1 const& box, std::pair<Box2, SizeT> const& box_pair) const
  94. {
  95. return ! geometry::disjoint(box, box_pair.first, m_strategy);
  96. }
  97. Strategy const& m_strategy;
  98. };
  99. template <typename OutputIterator, typename Strategy>
  100. class item_visitor_type
  101. {
  102. public:
  103. item_visitor_type(MultiPolygon const& multipolygon,
  104. OutputIterator& oit,
  105. Strategy const& strategy)
  106. : m_multipolygon(multipolygon)
  107. , m_oit(oit)
  108. , m_strategy(strategy)
  109. {}
  110. template <typename Point, typename Box, typename SizeT>
  111. inline bool apply(Point const& item1, std::pair<Box, SizeT> const& item2)
  112. {
  113. action_selector_pl
  114. <
  115. PointOut, overlay_intersection
  116. >::apply(item1,
  117. Policy::apply(item1,
  118. range::at(m_multipolygon,
  119. item2.second),
  120. m_strategy),
  121. m_oit);
  122. return true;
  123. }
  124. private:
  125. MultiPolygon const& m_multipolygon;
  126. OutputIterator& m_oit;
  127. Strategy const& m_strategy;
  128. };
  129. template <typename Iterator, typename Box, typename SizeT, typename Strategy>
  130. static inline void fill_box_pairs(Iterator first, Iterator last,
  131. std::vector<std::pair<Box, SizeT> > & box_pairs,
  132. Strategy const& strategy)
  133. {
  134. SizeT index = 0;
  135. for (; first != last; ++first, ++index)
  136. {
  137. box_pairs.push_back(
  138. std::make_pair(geometry::return_envelope<Box>(*first, strategy),
  139. index));
  140. }
  141. }
  142. template <typename OutputIterator, typename Strategy>
  143. static inline OutputIterator get_common_points(MultiPoint const& multipoint,
  144. MultiPolygon const& multipolygon,
  145. OutputIterator oit,
  146. Strategy const& strategy)
  147. {
  148. item_visitor_type<OutputIterator, Strategy> item_visitor(multipolygon, oit, strategy);
  149. typedef geometry::model::point
  150. <
  151. typename geometry::coordinate_type<MultiPoint>::type,
  152. geometry::dimension<MultiPoint>::value,
  153. typename geometry::coordinate_system<MultiPoint>::type
  154. > point_type;
  155. typedef geometry::model::box<point_type> box_type;
  156. typedef std::pair<box_type, std::size_t> box_pair;
  157. std::vector<box_pair> box_pairs;
  158. box_pairs.reserve(boost::size(multipolygon));
  159. fill_box_pairs(boost::begin(multipolygon),
  160. boost::end(multipolygon),
  161. box_pairs, strategy);
  162. geometry::partition
  163. <
  164. box_type
  165. >::apply(multipoint, box_pairs, item_visitor,
  166. expand_box_point<Strategy>(strategy),
  167. overlaps_box_point<Strategy>(strategy),
  168. expand_box_boxpair<Strategy>(strategy),
  169. overlaps_box_boxpair<Strategy>(strategy));
  170. return oit;
  171. }
  172. public:
  173. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  174. static inline OutputIterator apply(MultiPoint const& multipoint,
  175. MultiPolygon const& multipolygon,
  176. RobustPolicy const& robust_policy,
  177. OutputIterator oit,
  178. Strategy const& strategy)
  179. {
  180. typedef std::vector
  181. <
  182. typename boost::range_value<MultiPoint>::type
  183. > point_vector_type;
  184. point_vector_type common_points;
  185. // compute the common points
  186. get_common_points(multipoint, multipolygon,
  187. std::back_inserter(common_points),
  188. strategy);
  189. return multipoint_multipoint_point
  190. <
  191. MultiPoint, point_vector_type, PointOut, OverlayType
  192. >::apply(multipoint, common_points, robust_policy, oit, strategy);
  193. }
  194. };
  195. }} // namespace detail::overlay
  196. #endif // DOXYGEN_NO_DISPATCH
  197. #ifndef DOXYGEN_NO_DISPATCH
  198. namespace detail_dispatch { namespace overlay
  199. {
  200. // dispatch struct for pointlike-areal difference/intersection computation
  201. template
  202. <
  203. typename PointLike,
  204. typename Areal,
  205. typename PointOut,
  206. overlay_type OverlayType,
  207. typename Tag1,
  208. typename Tag2
  209. >
  210. struct pointlike_areal_point
  211. : not_implemented<PointLike, Areal, PointOut>
  212. {};
  213. template
  214. <
  215. typename Point,
  216. typename Areal,
  217. typename PointOut,
  218. overlay_type OverlayType,
  219. typename Tag2
  220. >
  221. struct pointlike_areal_point
  222. <
  223. Point, Areal, PointOut, OverlayType, point_tag, Tag2
  224. > : detail::overlay::point_single_point
  225. <
  226. Point, Areal, PointOut, OverlayType,
  227. detail::not_<detail::disjoint::reverse_covered_by>
  228. >
  229. {};
  230. // TODO: Consider implementing Areal-specific version
  231. // calculating envelope first in order to reject Points without
  232. // calling disjoint for Rings and Polygons
  233. template
  234. <
  235. typename MultiPoint,
  236. typename Areal,
  237. typename PointOut,
  238. overlay_type OverlayType,
  239. typename Tag2
  240. >
  241. struct pointlike_areal_point
  242. <
  243. MultiPoint, Areal, PointOut, OverlayType, multi_point_tag, Tag2
  244. > : detail::overlay::multipoint_single_point
  245. <
  246. MultiPoint, Areal, PointOut, OverlayType,
  247. detail::not_<detail::disjoint::reverse_covered_by>
  248. >
  249. {};
  250. template
  251. <
  252. typename MultiPoint,
  253. typename MultiPolygon,
  254. typename PointOut,
  255. overlay_type OverlayType
  256. >
  257. struct pointlike_areal_point
  258. <
  259. MultiPoint, MultiPolygon, PointOut, OverlayType, multi_point_tag, multi_polygon_tag
  260. > : detail::overlay::multipoint_multipolygon_point
  261. <
  262. MultiPoint, MultiPolygon, PointOut, OverlayType,
  263. detail::not_<detail::disjoint::reverse_covered_by>
  264. >
  265. {};
  266. }} // namespace detail_dispatch::overlay
  267. #endif // DOXYGEN_NO_DISPATCH
  268. }} // namespace boost::geometry
  269. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_AREAL_HPP