pointlike_linear.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2015-2020, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP
  9. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP
  10. #include <iterator>
  11. #include <vector>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/value_type.hpp>
  15. #include <boost/geometry/algorithms/disjoint.hpp>
  16. #include <boost/geometry/algorithms/envelope.hpp>
  17. #include <boost/geometry/algorithms/expand.hpp>
  18. #include <boost/geometry/algorithms/not_implemented.hpp>
  19. #include <boost/geometry/algorithms/detail/not.hpp>
  20. #include <boost/geometry/algorithms/detail/partition.hpp>
  21. #include <boost/geometry/algorithms/detail/disjoint/point_geometry.hpp>
  22. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  23. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  24. #include <boost/geometry/algorithms/detail/overlay/pointlike_pointlike.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/geometries/box.hpp>
  27. #include <boost/geometry/iterators/segment_iterator.hpp>
  28. // TEMP
  29. #include <boost/geometry/strategies/envelope/cartesian.hpp>
  30. #include <boost/geometry/strategies/envelope/geographic.hpp>
  31. #include <boost/geometry/strategies/envelope/spherical.hpp>
  32. namespace boost { namespace geometry
  33. {
  34. #ifndef DOXYGEN_NO_DETAIL
  35. namespace detail { namespace overlay
  36. {
  37. // difference/intersection of point-linear
  38. template
  39. <
  40. typename Point,
  41. typename Geometry,
  42. typename PointOut,
  43. overlay_type OverlayType,
  44. typename Policy
  45. >
  46. struct point_single_point
  47. {
  48. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  49. static inline OutputIterator apply(Point const& point,
  50. Geometry const& geometry,
  51. RobustPolicy const&,
  52. OutputIterator oit,
  53. Strategy const& strategy)
  54. {
  55. action_selector_pl
  56. <
  57. PointOut, OverlayType
  58. >::apply(point, Policy::apply(point, geometry, strategy), oit);
  59. return oit;
  60. }
  61. };
  62. // difference/intersection of multipoint-segment
  63. template
  64. <
  65. typename MultiPoint,
  66. typename Geometry,
  67. typename PointOut,
  68. overlay_type OverlayType,
  69. typename Policy
  70. >
  71. struct multipoint_single_point
  72. {
  73. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  74. static inline OutputIterator apply(MultiPoint const& multipoint,
  75. Geometry const& geometry,
  76. RobustPolicy const&,
  77. OutputIterator oit,
  78. Strategy const& strategy)
  79. {
  80. for (auto it = boost::begin(multipoint); it != boost::end(multipoint); ++it)
  81. {
  82. action_selector_pl
  83. <
  84. PointOut, OverlayType
  85. >::apply(*it, Policy::apply(*it, geometry, strategy), oit);
  86. }
  87. return oit;
  88. }
  89. };
  90. // difference/intersection of multipoint-linear
  91. template
  92. <
  93. typename MultiPoint,
  94. typename Linear,
  95. typename PointOut,
  96. overlay_type OverlayType,
  97. typename Policy
  98. >
  99. class multipoint_linear_point
  100. {
  101. private:
  102. // structs for partition -- start
  103. template <typename Strategy>
  104. struct expand_box_point
  105. {
  106. expand_box_point(Strategy const& strategy)
  107. : m_strategy(strategy)
  108. {}
  109. template <typename Box, typename Point>
  110. inline void apply(Box& total, Point const& point) const
  111. {
  112. geometry::expand(total, point, m_strategy);
  113. }
  114. Strategy const& m_strategy;
  115. };
  116. template <typename Strategy>
  117. struct expand_box_segment
  118. {
  119. explicit expand_box_segment(Strategy const& strategy)
  120. : m_strategy(strategy)
  121. {}
  122. template <typename Box, typename Segment>
  123. inline void apply(Box& total, Segment const& segment) const
  124. {
  125. geometry::expand(total,
  126. geometry::return_envelope<Box>(segment, m_strategy),
  127. m_strategy);
  128. }
  129. Strategy const& m_strategy;
  130. };
  131. template <typename Strategy>
  132. struct overlaps_box_point
  133. {
  134. explicit overlaps_box_point(Strategy const& strategy)
  135. : m_strategy(strategy)
  136. {}
  137. template <typename Box, typename Point>
  138. inline bool apply(Box const& box, Point const& point) const
  139. {
  140. return ! geometry::disjoint(point, box, m_strategy);
  141. }
  142. Strategy const& m_strategy;
  143. };
  144. template <typename Strategy>
  145. struct overlaps_box_segment
  146. {
  147. explicit overlaps_box_segment(Strategy const& strategy)
  148. : m_strategy(strategy)
  149. {}
  150. template <typename Box, typename Segment>
  151. inline bool apply(Box const& box, Segment const& segment) const
  152. {
  153. return ! geometry::disjoint(segment, box, m_strategy);
  154. }
  155. Strategy const& m_strategy;
  156. };
  157. template <typename OutputIterator, typename Strategy>
  158. class item_visitor_type
  159. {
  160. public:
  161. item_visitor_type(OutputIterator& oit, Strategy const& strategy)
  162. : m_oit(oit)
  163. , m_strategy(strategy)
  164. {}
  165. template <typename Item1, typename Item2>
  166. inline bool apply(Item1 const& item1, Item2 const& item2)
  167. {
  168. action_selector_pl
  169. <
  170. PointOut, overlay_intersection
  171. >::apply(item1, Policy::apply(item1, item2, m_strategy), m_oit);
  172. return true;
  173. }
  174. private:
  175. OutputIterator& m_oit;
  176. Strategy const& m_strategy;
  177. };
  178. // structs for partition -- end
  179. class segment_range
  180. {
  181. public:
  182. typedef geometry::segment_iterator<Linear const> const_iterator;
  183. typedef const_iterator iterator;
  184. explicit segment_range(Linear const& linear)
  185. : m_linear(linear)
  186. {}
  187. const_iterator begin() const
  188. {
  189. return geometry::segments_begin(m_linear);
  190. }
  191. const_iterator end() const
  192. {
  193. return geometry::segments_end(m_linear);
  194. }
  195. private:
  196. Linear const& m_linear;
  197. };
  198. template <typename OutputIterator, typename Strategy>
  199. static inline OutputIterator get_common_points(MultiPoint const& multipoint,
  200. Linear const& linear,
  201. OutputIterator oit,
  202. Strategy const& strategy)
  203. {
  204. item_visitor_type<OutputIterator, Strategy> item_visitor(oit, strategy);
  205. // TODO: disjoint Segment/Box may be called in partition multiple times
  206. // possibly for non-cartesian segments which could be slow. We should consider
  207. // passing a range of bounding boxes of segments after calculating them once.
  208. // Alternatively instead of a range of segments a range of Segment/Envelope pairs
  209. // should be passed, where envelope would be lazily calculated when needed the first time
  210. geometry::partition
  211. <
  212. geometry::model::box
  213. <
  214. typename boost::range_value<MultiPoint>::type
  215. >
  216. >::apply(multipoint, segment_range(linear), item_visitor,
  217. expand_box_point<Strategy>(strategy),
  218. overlaps_box_point<Strategy>(strategy),
  219. expand_box_segment<Strategy>(strategy),
  220. overlaps_box_segment<Strategy>(strategy));
  221. return oit;
  222. }
  223. public:
  224. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  225. static inline OutputIterator apply(MultiPoint const& multipoint,
  226. Linear const& linear,
  227. RobustPolicy const& robust_policy,
  228. OutputIterator oit,
  229. Strategy const& strategy)
  230. {
  231. typedef std::vector
  232. <
  233. typename boost::range_value<MultiPoint>::type
  234. > point_vector_type;
  235. point_vector_type common_points;
  236. // compute the common points
  237. get_common_points(multipoint, linear,
  238. std::back_inserter(common_points),
  239. strategy);
  240. return multipoint_multipoint_point
  241. <
  242. MultiPoint, point_vector_type, PointOut, OverlayType
  243. >::apply(multipoint, common_points, robust_policy, oit, strategy);
  244. }
  245. };
  246. }} // namespace detail::overlay
  247. #endif // DOXYGEN_NO_DETAIL
  248. #ifndef DOXYGEN_NO_DISPATCH
  249. namespace detail_dispatch { namespace overlay
  250. {
  251. // dispatch struct for pointlike-linear difference/intersection computation
  252. template
  253. <
  254. typename PointLike,
  255. typename Linear,
  256. typename PointOut,
  257. overlay_type OverlayType,
  258. typename Tag1,
  259. typename Tag2
  260. >
  261. struct pointlike_linear_point
  262. : not_implemented<PointLike, Linear, PointOut>
  263. {};
  264. template
  265. <
  266. typename Point,
  267. typename Linear,
  268. typename PointOut,
  269. overlay_type OverlayType
  270. >
  271. struct pointlike_linear_point
  272. <
  273. Point, Linear, PointOut, OverlayType, point_tag, linear_tag
  274. > : detail::overlay::point_single_point
  275. <
  276. Point, Linear, PointOut, OverlayType,
  277. detail::not_<detail::disjoint::reverse_covered_by>
  278. >
  279. {};
  280. template
  281. <
  282. typename Point,
  283. typename Segment,
  284. typename PointOut,
  285. overlay_type OverlayType
  286. >
  287. struct pointlike_linear_point
  288. <
  289. Point, Segment, PointOut, OverlayType, point_tag, segment_tag
  290. > : detail::overlay::point_single_point
  291. <
  292. Point, Segment, PointOut, OverlayType,
  293. detail::not_<detail::disjoint::reverse_covered_by>
  294. >
  295. {};
  296. template
  297. <
  298. typename MultiPoint,
  299. typename Linear,
  300. typename PointOut,
  301. overlay_type OverlayType
  302. >
  303. struct pointlike_linear_point
  304. <
  305. MultiPoint, Linear, PointOut, OverlayType, multi_point_tag, linear_tag
  306. > : detail::overlay::multipoint_linear_point
  307. <
  308. MultiPoint, Linear, PointOut, OverlayType,
  309. detail::not_<detail::disjoint::reverse_covered_by>
  310. >
  311. {};
  312. template
  313. <
  314. typename MultiPoint,
  315. typename Segment,
  316. typename PointOut,
  317. overlay_type OverlayType
  318. >
  319. struct pointlike_linear_point
  320. <
  321. MultiPoint, Segment, PointOut, OverlayType, multi_point_tag, segment_tag
  322. > : detail::overlay::multipoint_single_point
  323. <
  324. MultiPoint, Segment, PointOut, OverlayType,
  325. detail::not_<detail::disjoint::reverse_covered_by>
  326. >
  327. {};
  328. }} // namespace detail_dispatch::overlay
  329. #endif // DOXYGEN_NO_DISPATCH
  330. }} // namespace boost::geometry
  331. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP