follow_helpers.hpp 12 KB

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