follow_helpers.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013-2022.
  4. // Modifications copyright (c) 2013-2022 Oracle and/or its affiliates.
  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_RELATE_FOLLOW_HELPERS_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_FOLLOW_HELPERS_HPP
  11. #include <vector>
  12. #include <boost/core/ignore_unused.hpp>
  13. #include <boost/range/size.hpp>
  14. #include <boost/geometry/algorithms/detail/overlay/get_turn_info_helpers.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  16. #include <boost/geometry/algorithms/detail/overlay/segment_identifier.hpp>
  17. #include <boost/geometry/algorithms/detail/relate/boundary_checker.hpp>
  18. #include <boost/geometry/algorithms/not_implemented.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/util/condition.hpp>
  21. #include <boost/geometry/util/range.hpp>
  22. #include <boost/geometry/util/type_traits.hpp>
  23. #include <type_traits>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace relate {
  28. // NOTE: This iterates through single geometries for which turns were not generated.
  29. // It doesn't mean that the geometry is disjoint, only that no turns were detected.
  30. template
  31. <
  32. std::size_t OpId,
  33. typename Geometry,
  34. typename Tag = typename geometry::tag<Geometry>::type,
  35. bool IsMulti = util::is_multi<Geometry>::value
  36. >
  37. struct for_each_disjoint_geometry_if
  38. : public not_implemented<Tag>
  39. {};
  40. template <std::size_t OpId, typename Geometry, typename Tag>
  41. struct for_each_disjoint_geometry_if<OpId, Geometry, Tag, false>
  42. {
  43. template <typename TurnIt, typename Pred>
  44. static void apply(TurnIt first, TurnIt last, Geometry const& geometry, Pred & pred)
  45. {
  46. if (first == last)
  47. {
  48. pred(geometry);
  49. }
  50. }
  51. };
  52. template <std::size_t OpId, typename Geometry, typename Tag>
  53. struct for_each_disjoint_geometry_if<OpId, Geometry, Tag, true>
  54. {
  55. template <typename TurnIt, typename Pred>
  56. static void apply(TurnIt first, TurnIt last, Geometry const& geometry, Pred & pred)
  57. {
  58. if (first == last)
  59. {
  60. for_empty(geometry, pred);
  61. }
  62. else
  63. {
  64. for_turns(first, last, geometry, pred);
  65. }
  66. }
  67. template <typename Pred>
  68. static void for_empty(Geometry const& geometry, Pred & pred)
  69. {
  70. // O(N)
  71. // check predicate for each contained geometry without generated turn
  72. for (auto it = boost::begin(geometry); it != boost::end(geometry) ; ++it)
  73. {
  74. if (! pred(*it))
  75. {
  76. break;
  77. }
  78. }
  79. }
  80. template <typename TurnIt, typename Pred>
  81. static void for_turns(TurnIt first, TurnIt last, Geometry const& geometry, Pred & pred)
  82. {
  83. BOOST_GEOMETRY_ASSERT(first != last);
  84. std::size_t const count = boost::size(geometry);
  85. // O(I)
  86. // gather info about turns generated for contained geometries
  87. std::vector<bool> detected_intersections(count, false);
  88. for (TurnIt it = first; it != last; ++it)
  89. {
  90. signed_size_type multi_index = it->operations[OpId].seg_id.multi_index;
  91. BOOST_GEOMETRY_ASSERT(multi_index >= 0);
  92. std::size_t const index = static_cast<std::size_t>(multi_index);
  93. BOOST_GEOMETRY_ASSERT(index < count);
  94. detected_intersections[index] = true;
  95. }
  96. // O(N)
  97. // check predicate for each contained geometry without generated turn
  98. for (std::size_t index = 0; index < detected_intersections.size(); ++index)
  99. {
  100. // if there were no intersections for this multi_index
  101. if (detected_intersections[index] == false)
  102. {
  103. if (! pred(range::at(geometry, index)))
  104. {
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. };
  111. // WARNING! This class stores pointers!
  112. // Passing a reference to local variable will result in undefined behavior!
  113. template <typename Point>
  114. class point_info
  115. {
  116. public:
  117. point_info() : sid_ptr(NULL), pt_ptr(NULL) {}
  118. point_info(Point const& pt, segment_identifier const& sid)
  119. : sid_ptr(boost::addressof(sid))
  120. , pt_ptr(boost::addressof(pt))
  121. {}
  122. segment_identifier const& seg_id() const
  123. {
  124. BOOST_GEOMETRY_ASSERT(sid_ptr);
  125. return *sid_ptr;
  126. }
  127. Point const& point() const
  128. {
  129. BOOST_GEOMETRY_ASSERT(pt_ptr);
  130. return *pt_ptr;
  131. }
  132. //friend bool operator==(point_identifier const& l, point_identifier const& r)
  133. //{
  134. // return l.seg_id() == r.seg_id()
  135. // && detail::equals::equals_point_point(l.point(), r.point());
  136. //}
  137. private:
  138. const segment_identifier * sid_ptr;
  139. const Point * pt_ptr;
  140. };
  141. // WARNING! This class stores pointers!
  142. // Passing a reference to local variable will result in undefined behavior!
  143. class same_single
  144. {
  145. public:
  146. same_single(segment_identifier const& sid)
  147. : sid_ptr(boost::addressof(sid))
  148. {}
  149. bool operator()(segment_identifier const& sid) const
  150. {
  151. return sid.multi_index == sid_ptr->multi_index;
  152. }
  153. template <typename Point>
  154. bool operator()(point_info<Point> const& pid) const
  155. {
  156. return operator()(pid.seg_id());
  157. }
  158. private:
  159. const segment_identifier * sid_ptr;
  160. };
  161. class same_ring
  162. {
  163. public:
  164. same_ring(segment_identifier const& sid)
  165. : sid_ptr(boost::addressof(sid))
  166. {}
  167. bool operator()(segment_identifier const& sid) const
  168. {
  169. return sid.multi_index == sid_ptr->multi_index
  170. && sid.ring_index == sid_ptr->ring_index;
  171. }
  172. private:
  173. const segment_identifier * sid_ptr;
  174. };
  175. // WARNING! This class stores pointers!
  176. // Passing a reference to local variable will result in undefined behavior!
  177. template <typename SameRange = same_single>
  178. class segment_watcher
  179. {
  180. public:
  181. segment_watcher()
  182. : m_seg_id_ptr(NULL)
  183. {}
  184. bool update(segment_identifier const& seg_id)
  185. {
  186. bool result = m_seg_id_ptr == 0 || !SameRange(*m_seg_id_ptr)(seg_id);
  187. m_seg_id_ptr = boost::addressof(seg_id);
  188. return result;
  189. }
  190. private:
  191. const segment_identifier * m_seg_id_ptr;
  192. };
  193. // WARNING! This class stores pointers!
  194. // Passing a reference to local variable will result in undefined behavior!
  195. template <typename TurnInfo, std::size_t OpId>
  196. class exit_watcher
  197. {
  198. static std::size_t const op_id = OpId;
  199. static std::size_t const other_op_id = (OpId + 1) % 2;
  200. typedef typename TurnInfo::point_type point_type;
  201. typedef detail::relate::point_info<point_type> point_info;
  202. public:
  203. exit_watcher()
  204. : m_exit_operation(overlay::operation_none)
  205. , m_exit_turn_ptr(NULL)
  206. {}
  207. void enter(TurnInfo const& turn)
  208. {
  209. m_other_entry_points.push_back(
  210. point_info(turn.point, turn.operations[other_op_id].seg_id) );
  211. }
  212. // TODO: exit_per_geometry parameter looks not very safe
  213. // wrong value may be easily passed
  214. void exit(TurnInfo const& turn, bool exit_per_geometry = true)
  215. {
  216. //segment_identifier const& seg_id = turn.operations[op_id].seg_id;
  217. segment_identifier const& other_id = turn.operations[other_op_id].seg_id;
  218. overlay::operation_type exit_op = turn.operations[op_id].operation;
  219. // search for the entry point in the same range of other geometry
  220. auto entry_it = std::find_if(m_other_entry_points.begin(),
  221. m_other_entry_points.end(),
  222. same_single(other_id));
  223. // this end point has corresponding entry point
  224. if ( entry_it != m_other_entry_points.end() )
  225. {
  226. // erase the corresponding entry point
  227. m_other_entry_points.erase(entry_it);
  228. if ( exit_per_geometry || m_other_entry_points.empty() )
  229. {
  230. // here we know that we possibly left LS
  231. // we must still check if we didn't get back on the same point
  232. m_exit_operation = exit_op;
  233. m_exit_turn_ptr = boost::addressof(turn);
  234. }
  235. }
  236. }
  237. bool is_outside() const
  238. {
  239. // if we didn't entered anything in the past, we're outside
  240. return m_other_entry_points.empty();
  241. }
  242. bool is_outside(TurnInfo const& turn) const
  243. {
  244. return m_other_entry_points.empty()
  245. || std::none_of(m_other_entry_points.begin(),
  246. m_other_entry_points.end(),
  247. same_single(
  248. turn.operations[other_op_id].seg_id));
  249. }
  250. overlay::operation_type get_exit_operation() const
  251. {
  252. return m_exit_operation;
  253. }
  254. point_type const& get_exit_point() const
  255. {
  256. BOOST_GEOMETRY_ASSERT(m_exit_operation != overlay::operation_none);
  257. BOOST_GEOMETRY_ASSERT(m_exit_turn_ptr);
  258. return m_exit_turn_ptr->point;
  259. }
  260. TurnInfo const& get_exit_turn() const
  261. {
  262. BOOST_GEOMETRY_ASSERT(m_exit_operation != overlay::operation_none);
  263. BOOST_GEOMETRY_ASSERT(m_exit_turn_ptr);
  264. return *m_exit_turn_ptr;
  265. }
  266. void reset_detected_exit()
  267. {
  268. m_exit_operation = overlay::operation_none;
  269. }
  270. void reset()
  271. {
  272. m_exit_operation = overlay::operation_none;
  273. m_other_entry_points.clear();
  274. }
  275. private:
  276. overlay::operation_type m_exit_operation;
  277. const TurnInfo * m_exit_turn_ptr;
  278. std::vector<point_info> m_other_entry_points; // TODO: use map here or sorted vector?
  279. };
  280. template <std::size_t OpId, typename Turn, typename Strategy>
  281. inline bool turn_on_the_same_ip(Turn const& prev_turn, Turn const& curr_turn,
  282. Strategy const& strategy)
  283. {
  284. segment_identifier const& prev_seg_id = prev_turn.operations[OpId].seg_id;
  285. segment_identifier const& curr_seg_id = curr_turn.operations[OpId].seg_id;
  286. if ( prev_seg_id.multi_index != curr_seg_id.multi_index
  287. || prev_seg_id.ring_index != curr_seg_id.ring_index )
  288. {
  289. return false;
  290. }
  291. // TODO: will this work if between segments there will be some number of degenerated ones?
  292. if ( prev_seg_id.segment_index != curr_seg_id.segment_index
  293. && ( ! curr_turn.operations[OpId].fraction.is_zero()
  294. || prev_seg_id.segment_index + 1 != curr_seg_id.segment_index ) )
  295. {
  296. return false;
  297. }
  298. return detail::equals::equals_point_point(prev_turn.point, curr_turn.point, strategy);
  299. }
  300. template <typename IntersectionPoint, typename OperationInfo, typename BoundaryChecker>
  301. static inline bool is_ip_on_boundary(IntersectionPoint const& ip,
  302. OperationInfo const& operation_info,
  303. BoundaryChecker const& boundary_checker)
  304. {
  305. // IP on the first or the last point of the linestring
  306. return (operation_info.position == overlay::position_back
  307. || operation_info.position == overlay::position_front)
  308. // check if this point is a boundary
  309. ? boundary_checker.is_endpoint_boundary(ip)
  310. : false;
  311. }
  312. }} // namespace detail::relate
  313. #endif // DOXYGEN_NO_DETAIL
  314. }} // namespace boost::geometry
  315. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_FOLLOW_HELPERS_HPP