enrich_intersection_points.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017-2021.
  5. // Modifications copyright (c) 2017-2020 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
  12. #include <cstddef>
  13. #include <algorithm>
  14. #include <map>
  15. #include <set>
  16. #include <vector>
  17. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  18. # include <iostream>
  19. # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  20. # include <boost/geometry/io/wkt/wkt.hpp>
  21. # if ! defined(BOOST_GEOMETRY_DEBUG_IDENTIFIER)
  22. # define BOOST_GEOMETRY_DEBUG_IDENTIFIER
  23. #endif
  24. #endif
  25. #include <boost/range/begin.hpp>
  26. #include <boost/range/end.hpp>
  27. #include <boost/range/value_type.hpp>
  28. #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
  29. #include <boost/geometry/algorithms/detail/overlay/discard_duplicate_turns.hpp>
  30. #include <boost/geometry/algorithms/detail/overlay/handle_colocations.hpp>
  31. #include <boost/geometry/algorithms/detail/overlay/handle_self_turns.hpp>
  32. #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
  33. #include <boost/geometry/algorithms/detail/overlay/less_by_segment_ratio.hpp>
  34. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  35. #include <boost/geometry/policies/robustness/robust_type.hpp>
  36. #include <boost/geometry/util/constexpr.hpp>
  37. #include <boost/geometry/util/for_each_with_index.hpp>
  38. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  39. # include <boost/geometry/algorithms/detail/overlay/check_enrich.hpp>
  40. #endif
  41. namespace boost { namespace geometry
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace overlay
  45. {
  46. template <typename Turns>
  47. struct discarded_indexed_turn
  48. {
  49. discarded_indexed_turn(Turns const& turns)
  50. : m_turns(turns)
  51. {}
  52. template <typename IndexedTurn>
  53. inline bool operator()(IndexedTurn const& indexed) const
  54. {
  55. return m_turns[indexed.turn_index].discarded;
  56. }
  57. Turns const& m_turns;
  58. };
  59. // Sorts IP-s of this ring on segment-identifier, and if on same segment,
  60. // on distance.
  61. // Then assigns for each IP which is the next IP on this segment,
  62. // plus the vertex-index to travel to, plus the next IP
  63. // (might be on another segment)
  64. template
  65. <
  66. bool Reverse1, bool Reverse2,
  67. typename Operations,
  68. typename Turns,
  69. typename Geometry1, typename Geometry2,
  70. typename RobustPolicy,
  71. typename Strategy
  72. >
  73. inline void enrich_sort(Operations& operations,
  74. Turns const& turns,
  75. Geometry1 const& geometry1,
  76. Geometry2 const& geometry2,
  77. RobustPolicy const& robust_policy,
  78. Strategy const& strategy)
  79. {
  80. std::sort(std::begin(operations),
  81. std::end(operations),
  82. less_by_segment_ratio
  83. <
  84. Turns,
  85. typename boost::range_value<Operations>::type,
  86. Geometry1, Geometry2,
  87. RobustPolicy,
  88. Strategy,
  89. Reverse1, Reverse2
  90. >(turns, geometry1, geometry2, robust_policy, strategy));
  91. }
  92. // Assign travel-to-vertex/ip index for each turn.
  93. template <typename Operations, typename Turns>
  94. inline void enrich_assign(Operations& operations, Turns& turns,
  95. bool check_consecutive_turns)
  96. {
  97. for_each_with_index(operations, [&](std::size_t index, auto const& indexed)
  98. {
  99. auto& turn = turns[indexed.turn_index];
  100. auto& op = turn.operations[indexed.operation_index];
  101. std::size_t next_index = index + 1 < operations.size() ? index + 1 : 0;
  102. auto advance = [&operations](auto index)
  103. {
  104. std::size_t const result = index + 1;
  105. return result >= operations.size() ? 0 : result;
  106. };
  107. auto next_turn = [&operations, &turns, &next_index]()
  108. {
  109. return turns[operations[next_index].turn_index];
  110. };
  111. auto next_operation = [&operations, &turns, &next_index]()
  112. {
  113. auto const& next_turn = turns[operations[next_index].turn_index];
  114. return next_turn.operations[operations[next_index].operation_index];
  115. };
  116. if (check_consecutive_turns
  117. && indexed.turn_index == operations[next_index].turn_index
  118. && op.seg_id == next_operation().seg_id)
  119. {
  120. // If the two operations on the same turn are ordered consecutively,
  121. // and they are on the same segment, then the turn where to travel to should
  122. // be considered one further. Therefore next is increased.
  123. //
  124. // It often happens in buffer, in these configurations:
  125. // +---->--+
  126. // | |
  127. // | +->-*---->
  128. // | | |
  129. // ^ +-<-+
  130. // If the next index is not corrected, the small rectangle
  131. // will be kept in the output.
  132. // This is a normal situation and occurs, for example, in every concave bend.
  133. // In general it should always travel from turn to next turn.
  134. // Only in some circumstances traveling to the same turn is necessary, for example
  135. // if there is only one turn in the outer ring.
  136. //
  137. // (For dissolve this is not done, turn_index is often
  138. // the same for two consecutive operations - but the conditions are changed
  139. // and this should be verified again)
  140. next_index = advance(next_index);
  141. }
  142. // Cluster behaviour: next should point after cluster, unless
  143. // their seg_ids are not the same
  144. // (For dissolve, this is still to be examined - TODO)
  145. while (turn.is_clustered()
  146. && turn.cluster_id == next_turn().cluster_id
  147. && op.seg_id == next_operation().seg_id
  148. && indexed.turn_index != operations[next_index].turn_index)
  149. {
  150. next_index = advance(next_index);
  151. }
  152. op.enriched.travels_to_ip_index
  153. = static_cast<signed_size_type>(operations[next_index].turn_index);
  154. op.enriched.travels_to_vertex_index
  155. = operations[next_index].subject->seg_id.segment_index;
  156. auto const& next_op = next_operation();
  157. if (op.seg_id.segment_index == next_op.seg_id.segment_index
  158. && op.fraction < next_op.fraction)
  159. {
  160. // Next turn is located further on same segment: assign next_ip_index
  161. op.enriched.next_ip_index = static_cast<signed_size_type>(operations[next_index].turn_index);
  162. }
  163. });
  164. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  165. for (auto const& indexed_op : operations)
  166. {
  167. auto const& op = turns[indexed_op.turn_index].operations[indexed_op.operation_index];
  168. std::cout << indexed_op.turn_index
  169. << " cl=" << turns[indexed_op.turn_index].cluster_id
  170. << " meth=" << method_char(turns[indexed_op.turn_index].method)
  171. << " seg=" << op.seg_id
  172. << " dst=" << op.fraction // needs define
  173. << " op=" << operation_char(turns[indexed_op.turn_index].operations[0].operation)
  174. << operation_char(turns[indexed_op.turn_index].operations[1].operation)
  175. << " (" << operation_char(op.operation) << ")"
  176. << " nxt=" << op.enriched.next_ip_index
  177. << " / " << op.enriched.travels_to_ip_index
  178. << " [vx " << op.enriched.travels_to_vertex_index << "]"
  179. << (turns[indexed_op.turn_index].discarded ? " [discarded]" : "")
  180. << (op.enriched.startable ? "" : " [not startable]")
  181. << std::endl;
  182. }
  183. #endif
  184. }
  185. template <typename Operations, typename Turns>
  186. inline void enrich_adapt(Operations& operations, Turns& turns)
  187. {
  188. // Operations is a vector of indexed_turn_operation<>
  189. // If it is empty, or contains one or two items, it makes no sense
  190. if (operations.size() < 3)
  191. {
  192. return;
  193. }
  194. bool next_phase = false;
  195. std::size_t previous_index = operations.size() - 1;
  196. for_each_with_index(operations, [&](std::size_t index, auto const& indexed)
  197. {
  198. auto& turn = turns[indexed.turn_index];
  199. auto& op = turn.operations[indexed.operation_index];
  200. std::size_t const next_index = index + 1 < operations.size() ? index + 1 : 0;
  201. auto const& next_turn = turns[operations[next_index].turn_index];
  202. auto const& next_op = next_turn.operations[operations[next_index].operation_index];
  203. if (op.seg_id.segment_index == next_op.seg_id.segment_index)
  204. {
  205. auto const& prev_turn = turns[operations[previous_index].turn_index];
  206. auto const& prev_op = prev_turn.operations[operations[previous_index].operation_index];
  207. if (op.seg_id.segment_index == prev_op.seg_id.segment_index)
  208. {
  209. op.enriched.startable = false;
  210. next_phase = true;
  211. }
  212. }
  213. previous_index = index;
  214. });
  215. if (! next_phase)
  216. {
  217. return;
  218. }
  219. // Discard turns which are both non-startable
  220. next_phase = false;
  221. for (auto& turn : turns)
  222. {
  223. if (! turn.operations[0].enriched.startable
  224. && ! turn.operations[1].enriched.startable)
  225. {
  226. turn.discarded = true;
  227. next_phase = true;
  228. }
  229. }
  230. if (! next_phase)
  231. {
  232. return;
  233. }
  234. // Remove discarded turns from operations to avoid having them as next turn
  235. discarded_indexed_turn<Turns> const predicate(turns);
  236. operations.erase(std::remove_if(std::begin(operations),
  237. std::end(operations), predicate), std::end(operations));
  238. }
  239. struct enriched_map_default_include_policy
  240. {
  241. template <typename Operation>
  242. static inline bool include(Operation const& )
  243. {
  244. // By default include all operations
  245. return true;
  246. }
  247. };
  248. // Add all (non discarded) operations on this ring
  249. // Blocked operations or uu on clusters (for intersection)
  250. // should be included, to block potential paths in clusters
  251. template <typename Turns, typename MappedVector, typename IncludePolicy>
  252. inline void create_map(Turns const& turns, MappedVector& mapped_vector,
  253. IncludePolicy const& include_policy)
  254. {
  255. for_each_with_index(turns, [&](std::size_t index, auto const& turn)
  256. {
  257. if (! turn.discarded)
  258. {
  259. for_each_with_index(turn.operations, [&](std::size_t op_index, auto const& op)
  260. {
  261. if (include_policy.include(op.operation))
  262. {
  263. ring_identifier const ring_id
  264. (
  265. op.seg_id.source_index,
  266. op.seg_id.multi_index,
  267. op.seg_id.ring_index
  268. );
  269. mapped_vector[ring_id].emplace_back
  270. (
  271. index, op_index, op, turn.operations[1 - op_index].seg_id
  272. );
  273. }
  274. });
  275. }
  276. });
  277. }
  278. template <typename Point1, typename Point2>
  279. inline typename geometry::coordinate_type<Point1>::type
  280. distance_measure(Point1 const& a, Point2 const& b)
  281. {
  282. // TODO: use comparable distance for point-point instead - but that
  283. // causes currently cycling include problems
  284. using ctype = typename geometry::coordinate_type<Point1>::type;
  285. ctype const dx = get<0>(a) - get<0>(b);
  286. ctype const dy = get<1>(a) - get<1>(b);
  287. return dx * dx + dy * dy;
  288. }
  289. template <typename Turns>
  290. inline void calculate_remaining_distance(Turns& turns)
  291. {
  292. for (auto& turn : turns)
  293. {
  294. auto& op0 = turn.operations[0];
  295. auto& op1 = turn.operations[1];
  296. static decltype(op0.remaining_distance) const zero_distance = 0;
  297. if (op0.remaining_distance != zero_distance
  298. || op1.remaining_distance != zero_distance)
  299. {
  300. continue;
  301. }
  302. auto const to_index0 = op0.enriched.get_next_turn_index();
  303. auto const to_index1 = op1.enriched.get_next_turn_index();
  304. if (to_index0 >= 0
  305. && to_index1 >= 0
  306. && to_index0 != to_index1)
  307. {
  308. op0.remaining_distance = distance_measure(turn.point, turns[to_index0].point);
  309. op1.remaining_distance = distance_measure(turn.point, turns[to_index1].point);
  310. }
  311. }
  312. }
  313. }} // namespace detail::overlay
  314. #endif //DOXYGEN_NO_DETAIL
  315. /*!
  316. \brief All intersection points are enriched with successor information
  317. \ingroup overlay
  318. \tparam Turns type of intersection container
  319. (e.g. vector of "intersection/turn point"'s)
  320. \tparam Clusters type of cluster container
  321. \tparam Geometry1 \tparam_geometry
  322. \tparam Geometry2 \tparam_geometry
  323. \tparam PointInGeometryStrategy point in geometry strategy type
  324. \param turns container containing intersection points
  325. \param clusters container containing clusters
  326. \param geometry1 \param_geometry
  327. \param geometry2 \param_geometry
  328. \param robust_policy policy to handle robustness issues
  329. \param strategy point in geometry strategy
  330. */
  331. template
  332. <
  333. bool Reverse1, bool Reverse2,
  334. overlay_type OverlayType,
  335. typename Turns,
  336. typename Clusters,
  337. typename Geometry1, typename Geometry2,
  338. typename RobustPolicy,
  339. typename IntersectionStrategy
  340. >
  341. inline void enrich_intersection_points(Turns& turns,
  342. Clusters& clusters,
  343. Geometry1 const& geometry1, Geometry2 const& geometry2,
  344. RobustPolicy const& robust_policy,
  345. IntersectionStrategy const& strategy)
  346. {
  347. constexpr detail::overlay::operation_type target_operation
  348. = detail::overlay::operation_from_overlay<OverlayType>::value;
  349. constexpr detail::overlay::operation_type opposite_operation
  350. = target_operation == detail::overlay::operation_union
  351. ? detail::overlay::operation_intersection
  352. : detail::overlay::operation_union;
  353. constexpr bool is_dissolve = OverlayType == overlay_dissolve;
  354. constexpr bool is_buffer = OverlayType == overlay_buffer;
  355. using turn_type = typename boost::range_value<Turns>::type;
  356. using indexed_turn_operation = detail::overlay::indexed_turn_operation
  357. <
  358. typename turn_type::turn_operation_type
  359. > ;
  360. using mapped_vector_type = std::map
  361. <
  362. ring_identifier,
  363. std::vector<indexed_turn_operation>
  364. >;
  365. // Turns are often used by index (in clusters, next_index, etc)
  366. // and turns may therefore NOT be DELETED - they may only be flagged as discarded
  367. bool has_cc = false;
  368. bool has_colocations = false;
  369. if BOOST_GEOMETRY_CONSTEXPR (! is_buffer)
  370. {
  371. // Handle colocations, gathering clusters and (below) their properties.
  372. has_colocations = detail::overlay::handle_colocations
  373. <
  374. Reverse1, Reverse2, OverlayType, Geometry1, Geometry2
  375. >(turns, clusters, robust_policy);
  376. // Gather cluster properties (using even clusters with
  377. // discarded turns - for open turns)
  378. detail::overlay::gather_cluster_properties
  379. <
  380. Reverse1,
  381. Reverse2,
  382. OverlayType
  383. >(clusters, turns, target_operation,
  384. geometry1, geometry2, strategy);
  385. }
  386. else
  387. {
  388. // For buffer, this was already done before calling enrich_intersection_points.
  389. has_colocations = ! clusters.empty();
  390. }
  391. discard_duplicate_start_turns(turns, geometry1, geometry2);
  392. // Discard turns not part of target overlay
  393. for (auto& turn : turns)
  394. {
  395. if (turn.both(detail::overlay::operation_none)
  396. || turn.both(opposite_operation)
  397. || turn.both(detail::overlay::operation_blocked)
  398. || (detail::overlay::is_self_turn<OverlayType>(turn)
  399. && ! turn.is_clustered()
  400. && ! turn.both(target_operation)))
  401. {
  402. // For all operations, discard xx and none/none
  403. // For intersections, remove uu to avoid the need to travel
  404. // a union (during intersection) in uu/cc clusters (e.g. #31,#32,#33)
  405. // The ux is necessary to indicate impossible paths
  406. // (especially if rescaling is removed)
  407. // Similarly, for union, discard ii and ix
  408. // For self-turns, only keep uu / ii
  409. turn.discarded = true;
  410. turn.cluster_id = -1;
  411. continue;
  412. }
  413. if (! turn.discarded
  414. && turn.both(detail::overlay::operation_continue))
  415. {
  416. has_cc = true;
  417. }
  418. }
  419. if (! is_dissolve)
  420. {
  421. detail::overlay::discard_closed_turns
  422. <
  423. OverlayType,
  424. target_operation
  425. >::apply(turns, clusters, geometry1, geometry2,
  426. strategy);
  427. detail::overlay::discard_open_turns
  428. <
  429. OverlayType,
  430. target_operation
  431. >::apply(turns, clusters, geometry1, geometry2,
  432. strategy);
  433. }
  434. // Create a map of vectors of indexed operation-types to be able
  435. // to sort intersection points PER RING
  436. mapped_vector_type mapped_vector;
  437. detail::overlay::create_map(turns, mapped_vector,
  438. detail::overlay::enriched_map_default_include_policy());
  439. for (auto& pair : mapped_vector)
  440. {
  441. detail::overlay::enrich_sort<Reverse1, Reverse2>(
  442. pair.second, turns,
  443. geometry1, geometry2,
  444. robust_policy, strategy);
  445. }
  446. if (has_colocations)
  447. {
  448. detail::overlay::cleanup_clusters(turns, clusters);
  449. detail::overlay::colocate_clusters(clusters, turns);
  450. }
  451. // After cleaning up clusters assign the next turns
  452. for (auto& pair : mapped_vector)
  453. {
  454. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  455. std::cout << "ENRICH-assign Ring " << pair.first << std::endl;
  456. #endif
  457. if (is_dissolve)
  458. {
  459. detail::overlay::enrich_adapt(pair.second, turns);
  460. }
  461. detail::overlay::enrich_assign(pair.second, turns, ! is_dissolve);
  462. }
  463. if (has_cc)
  464. {
  465. detail::overlay::calculate_remaining_distance(turns);
  466. }
  467. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  468. //detail::overlay::check_graph(turns, for_operation);
  469. #endif
  470. }
  471. }} // namespace boost::geometry
  472. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP