follow_helpers.hpp 13 KB

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