pointlike_linear.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2015-2019, 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.hpp>
  13. #include <boost/geometry/core/tags.hpp>
  14. #include <boost/geometry/geometries/box.hpp>
  15. #include <boost/geometry/iterators/segment_iterator.hpp>
  16. #include <boost/geometry/algorithms/disjoint.hpp>
  17. #include <boost/geometry/algorithms/envelope.hpp>
  18. #include <boost/geometry/algorithms/expand.hpp>
  19. #include <boost/geometry/algorithms/not_implemented.hpp>
  20. #include <boost/geometry/algorithms/detail/not.hpp>
  21. #include <boost/geometry/algorithms/detail/partition.hpp>
  22. #include <boost/geometry/algorithms/detail/disjoint/point_geometry.hpp>
  23. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  24. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  25. #include <boost/geometry/algorithms/detail/overlay/pointlike_pointlike.hpp>
  26. namespace boost { namespace geometry
  27. {
  28. #ifndef DOXYGEN_NO_DETAIL
  29. namespace detail { namespace overlay
  30. {
  31. // action struct for pointlike-linear difference/intersection
  32. // it works the same as its pointlike-pointlike counterpart, hence the
  33. // derivation
  34. template <typename PointOut, overlay_type OverlayType>
  35. struct action_selector_pl_l
  36. : action_selector_pl_pl<PointOut, OverlayType>
  37. {};
  38. // difference/intersection of point-linear
  39. template
  40. <
  41. typename Point,
  42. typename Linear,
  43. typename PointOut,
  44. overlay_type OverlayType,
  45. typename Policy
  46. >
  47. struct point_linear_point
  48. {
  49. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  50. static inline OutputIterator apply(Point const& point,
  51. Linear const& linear,
  52. RobustPolicy const&,
  53. OutputIterator oit,
  54. Strategy const& strategy)
  55. {
  56. action_selector_pl_l
  57. <
  58. PointOut, OverlayType
  59. >::apply(point, Policy::apply(point, linear, strategy), oit);
  60. return oit;
  61. }
  62. };
  63. // difference/intersection of multipoint-segment
  64. template
  65. <
  66. typename MultiPoint,
  67. typename Segment,
  68. typename PointOut,
  69. overlay_type OverlayType,
  70. typename Policy
  71. >
  72. struct multipoint_segment_point
  73. {
  74. template <typename RobustPolicy, typename OutputIterator, typename Strategy>
  75. static inline OutputIterator apply(MultiPoint const& multipoint,
  76. Segment const& segment,
  77. RobustPolicy const&,
  78. OutputIterator oit,
  79. Strategy const& strategy)
  80. {
  81. for (typename boost::range_iterator<MultiPoint const>::type
  82. it = boost::begin(multipoint);
  83. it != boost::end(multipoint);
  84. ++it)
  85. {
  86. action_selector_pl_l
  87. <
  88. PointOut, OverlayType
  89. >::apply(*it, Policy::apply(*it, segment, strategy), oit);
  90. }
  91. return oit;
  92. }
  93. };
  94. // difference/intersection of multipoint-linear
  95. template
  96. <
  97. typename MultiPoint,
  98. typename Linear,
  99. typename PointOut,
  100. overlay_type OverlayType,
  101. typename Policy
  102. >
  103. class multipoint_linear_point
  104. {
  105. private:
  106. // structs for partition -- start
  107. template <typename ExpandPointStrategy>
  108. struct expand_box_point
  109. {
  110. template <typename Box, typename Point>
  111. static inline void apply(Box& total, Point const& point)
  112. {
  113. geometry::expand(total, point, ExpandPointStrategy());
  114. }
  115. };
  116. template <typename EnvelopeStrategy>
  117. struct expand_box_segment
  118. {
  119. explicit expand_box_segment(EnvelopeStrategy 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.get_box_expand_strategy());
  128. }
  129. EnvelopeStrategy const& m_strategy;
  130. };
  131. template <typename DisjointPointBoxStrategy>
  132. struct overlaps_box_point
  133. {
  134. template <typename Box, typename Point>
  135. static inline bool apply(Box const& box, Point const& point)
  136. {
  137. return ! geometry::disjoint(point, box, DisjointPointBoxStrategy());
  138. }
  139. };
  140. template <typename DisjointStrategy>
  141. struct overlaps_box_segment
  142. {
  143. explicit overlaps_box_segment(DisjointStrategy const& strategy)
  144. : m_strategy(strategy)
  145. {}
  146. template <typename Box, typename Segment>
  147. inline bool apply(Box const& box, Segment const& segment) const
  148. {
  149. return ! geometry::disjoint(segment, box, m_strategy);
  150. }
  151. DisjointStrategy const& m_strategy;
  152. };
  153. template <typename OutputIterator, typename Strategy>
  154. class item_visitor_type
  155. {
  156. public:
  157. item_visitor_type(OutputIterator& oit, Strategy const& strategy)
  158. : m_oit(oit)
  159. , m_strategy(strategy)
  160. {}
  161. template <typename Item1, typename Item2>
  162. inline bool apply(Item1 const& item1, Item2 const& item2)
  163. {
  164. action_selector_pl_l
  165. <
  166. PointOut, overlay_intersection
  167. >::apply(item1, Policy::apply(item1, item2, m_strategy), m_oit);
  168. return true;
  169. }
  170. private:
  171. OutputIterator& m_oit;
  172. Strategy const& m_strategy;
  173. };
  174. // structs for partition -- end
  175. class segment_range
  176. {
  177. public:
  178. typedef geometry::segment_iterator<Linear const> const_iterator;
  179. typedef const_iterator iterator;
  180. segment_range(Linear const& linear)
  181. : m_linear(linear)
  182. {}
  183. const_iterator begin() const
  184. {
  185. return geometry::segments_begin(m_linear);
  186. }
  187. const_iterator end() const
  188. {
  189. return geometry::segments_end(m_linear);
  190. }
  191. private:
  192. Linear const& m_linear;
  193. };
  194. template <typename OutputIterator, typename Strategy>
  195. static inline OutputIterator get_common_points(MultiPoint const& multipoint,
  196. Linear const& linear,
  197. OutputIterator oit,
  198. Strategy const& strategy)
  199. {
  200. item_visitor_type<OutputIterator, Strategy> item_visitor(oit, strategy);
  201. typedef typename Strategy::envelope_strategy_type envelope_strategy_type;
  202. typedef typename Strategy::disjoint_strategy_type disjoint_strategy_type;
  203. typedef typename Strategy::disjoint_point_box_strategy_type disjoint_point_box_strategy_type;
  204. typedef typename Strategy::expand_point_strategy_type expand_point_strategy_type;
  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<expand_point_strategy_type>(),
  218. overlaps_box_point<disjoint_point_box_strategy_type>(),
  219. expand_box_segment<envelope_strategy_type>(strategy.get_envelope_strategy()),
  220. overlaps_box_segment<disjoint_strategy_type>(strategy.get_disjoint_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_linear_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_linear_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_segment_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