traversal.hpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 2017-2020.
  4. // Modifications copyright (c) 2017-2020 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_OVERLAY_TRAVERSAL_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_HPP
  11. #include <cstddef>
  12. #include <set>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/value_type.hpp>
  16. #include <boost/geometry/algorithms/detail/overlay/cluster_info.hpp>
  17. #include <boost/geometry/algorithms/detail/overlay/cluster_exits.hpp>
  18. #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/sort_by_side.hpp>
  20. #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
  21. #include <boost/geometry/core/assert.hpp>
  22. #include <boost/geometry/util/condition.hpp>
  23. #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION) \
  24. || defined(BOOST_GEOMETRY_OVERLAY_REPORT_WKT) \
  25. || defined(BOOST_GEOMETRY_DEBUG_TRAVERSE)
  26. # include <string>
  27. # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  28. # include <boost/geometry/io/wkt/wkt.hpp>
  29. #endif
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace overlay
  34. {
  35. template <typename Turn, typename Operation>
  36. #ifdef BOOST_GEOMETRY_DEBUG_TRAVERSE
  37. inline void debug_traverse(Turn const& turn, Operation op,
  38. std::string const& header, bool condition = true)
  39. {
  40. if (! condition)
  41. {
  42. return;
  43. }
  44. std::cout << " " << header
  45. << " at " << op.seg_id
  46. << " meth: " << method_char(turn.method)
  47. << " op: " << operation_char(op.operation)
  48. << " vis: " << visited_char(op.visited)
  49. << " of: " << operation_char(turn.operations[0].operation)
  50. << operation_char(turn.operations[1].operation)
  51. << " " << geometry::wkt(turn.point)
  52. << std::endl;
  53. if (boost::contains(header, "Finished"))
  54. {
  55. std::cout << std::endl;
  56. }
  57. }
  58. #else
  59. inline void debug_traverse(Turn const& , Operation, const char*, bool = true)
  60. {
  61. }
  62. #endif
  63. template
  64. <
  65. bool Reverse1,
  66. bool Reverse2,
  67. overlay_type OverlayType,
  68. typename Geometry1,
  69. typename Geometry2,
  70. typename Turns,
  71. typename Clusters,
  72. typename RobustPolicy,
  73. typename SideStrategy,
  74. typename Visitor
  75. >
  76. struct traversal
  77. {
  78. private :
  79. static const operation_type target_operation = operation_from_overlay<OverlayType>::value;
  80. typedef typename sort_by_side::side_compare<target_operation>::type side_compare_type;
  81. typedef typename boost::range_value<Turns>::type turn_type;
  82. typedef typename turn_type::turn_operation_type turn_operation_type;
  83. typedef typename geometry::point_type<Geometry1>::type point_type;
  84. typedef sort_by_side::side_sorter
  85. <
  86. Reverse1, Reverse2, OverlayType,
  87. point_type, SideStrategy, side_compare_type
  88. > sbs_type;
  89. public :
  90. inline traversal(Geometry1 const& geometry1, Geometry2 const& geometry2,
  91. Turns& turns, Clusters const& clusters,
  92. RobustPolicy const& robust_policy, SideStrategy const& strategy,
  93. Visitor& visitor)
  94. : m_geometry1(geometry1)
  95. , m_geometry2(geometry2)
  96. , m_turns(turns)
  97. , m_clusters(clusters)
  98. , m_robust_policy(robust_policy)
  99. , m_strategy(strategy)
  100. , m_visitor(visitor)
  101. {
  102. }
  103. template <typename TurnInfoMap>
  104. inline void finalize_visit_info(TurnInfoMap& turn_info_map)
  105. {
  106. for (auto& turn : m_turns)
  107. {
  108. for (int i = 0; i < 2; i++)
  109. {
  110. turn_operation_type& op = turn.operations[i];
  111. if (op.visited.visited()
  112. || op.visited.started()
  113. || op.visited.finished() )
  114. {
  115. ring_identifier const ring_id = ring_id_by_seg_id(op.seg_id);
  116. turn_info_map[ring_id].has_traversed_turn = true;
  117. if (op.operation == operation_continue)
  118. {
  119. // Continue operations should mark the other operation
  120. // as traversed too
  121. turn_operation_type& other_op = turn.operations[1 - i];
  122. ring_identifier const other_ring_id
  123. = ring_id_by_seg_id(other_op.seg_id);
  124. turn_info_map[other_ring_id].has_traversed_turn = true;
  125. }
  126. }
  127. op.visited.finalize();
  128. }
  129. }
  130. }
  131. //! Sets visited for ALL turns traveling to the same turn
  132. inline void set_visited_in_cluster(signed_size_type cluster_id,
  133. signed_size_type rank)
  134. {
  135. auto mit = m_clusters.find(cluster_id);
  136. BOOST_ASSERT(mit != m_clusters.end());
  137. cluster_info const& cinfo = mit->second;
  138. for (auto turn_index : cinfo.turn_indices)
  139. {
  140. turn_type& turn = m_turns[turn_index];
  141. for (auto& op : turn.operations)
  142. {
  143. if (op.visited.none() && op.enriched.rank == rank)
  144. {
  145. op.visited.set_visited();
  146. }
  147. }
  148. }
  149. }
  150. inline void set_visited(turn_type& turn, turn_operation_type& op)
  151. {
  152. if (op.operation == detail::overlay::operation_continue)
  153. {
  154. // On "continue", all go in same direction so set "visited" for ALL
  155. for (int i = 0; i < 2; i++)
  156. {
  157. turn_operation_type& turn_op = turn.operations[i];
  158. if (turn_op.visited.none())
  159. {
  160. turn_op.visited.set_visited();
  161. }
  162. }
  163. }
  164. else
  165. {
  166. op.visited.set_visited();
  167. }
  168. if (turn.is_clustered())
  169. {
  170. set_visited_in_cluster(turn.cluster_id, op.enriched.rank);
  171. }
  172. }
  173. inline bool is_visited(turn_type const& , turn_operation_type const& op,
  174. signed_size_type , int) const
  175. {
  176. return op.visited.visited();
  177. }
  178. template <signed_size_type segment_identifier::*Member>
  179. inline bool select_source_generic(turn_type const& turn,
  180. segment_identifier const& current,
  181. segment_identifier const& previous) const
  182. {
  183. turn_operation_type const& op0 = turn.operations[0];
  184. turn_operation_type const& op1 = turn.operations[1];
  185. bool const switch_source = op0.enriched.region_id != -1
  186. && op0.enriched.region_id == op1.enriched.region_id;
  187. #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
  188. if (switch_source)
  189. {
  190. std::cout << "Switch source at " << &turn << std::endl;
  191. }
  192. else
  193. {
  194. std::cout << "DON'T SWITCH SOURCES at " << &turn << std::endl;
  195. }
  196. #endif
  197. return switch_source
  198. ? current.*Member != previous.*Member
  199. : current.*Member == previous.*Member;
  200. }
  201. inline bool select_source(turn_type const& turn,
  202. segment_identifier const& candidate_seg_id,
  203. segment_identifier const& previous_seg_id) const
  204. {
  205. // For uu/ii, only switch sources if indicated
  206. if (BOOST_GEOMETRY_CONDITION(OverlayType == overlay_buffer))
  207. {
  208. // Buffer does not use source_index (always 0).
  209. return select_source_generic<&segment_identifier::multi_index>(
  210. turn, candidate_seg_id, previous_seg_id);
  211. }
  212. if (is_self_turn<OverlayType>(turn))
  213. {
  214. // Also, if it is a self-turn, stay on same ring (multi/ring)
  215. return select_source_generic<&segment_identifier::multi_index>(
  216. turn, candidate_seg_id, previous_seg_id);
  217. }
  218. // Use source_index
  219. return select_source_generic<&segment_identifier::source_index>(
  220. turn, candidate_seg_id, previous_seg_id);
  221. }
  222. inline bool traverse_possible(signed_size_type turn_index) const
  223. {
  224. if (turn_index == -1)
  225. {
  226. return false;
  227. }
  228. turn_type const& turn = m_turns[turn_index];
  229. // It is not a dead end if there is an operation to continue, or of
  230. // there is a cluster (assuming for now we can get out of the cluster)
  231. return turn.is_clustered()
  232. || turn.has(target_operation)
  233. || turn.has(operation_continue);
  234. }
  235. inline std::size_t get_shortcut_level(turn_operation_type const& op,
  236. signed_size_type start_turn_index,
  237. signed_size_type origin_turn_index,
  238. std::size_t level = 1) const
  239. {
  240. signed_size_type next_turn_index = op.enriched.get_next_turn_index();
  241. if (next_turn_index == -1)
  242. {
  243. return 0;
  244. }
  245. if (next_turn_index == start_turn_index)
  246. {
  247. // This operation finishes the ring
  248. return 0;
  249. }
  250. if (next_turn_index == origin_turn_index)
  251. {
  252. // This operation travels to itself
  253. return level;
  254. }
  255. if (level > 10)
  256. {
  257. // Avoid infinite recursion
  258. return 0;
  259. }
  260. turn_type const& next_turn = m_turns[next_turn_index];
  261. for (int i = 0; i < 2; i++)
  262. {
  263. turn_operation_type const& next_op = next_turn.operations[i];
  264. if (next_op.operation == target_operation
  265. && ! next_op.visited.finished()
  266. && ! next_op.visited.visited())
  267. {
  268. // Recursively continue verifying
  269. if (get_shortcut_level(next_op, start_turn_index,
  270. origin_turn_index, level + 1))
  271. {
  272. return level + 1;
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. inline
  279. bool select_cc_operation(turn_type const& turn,
  280. signed_size_type start_turn_index,
  281. int& selected_op_index) const
  282. {
  283. // For "cc", take either one, but if there is a starting one,
  284. // take that one. If next is dead end, skip that one.
  285. // If both are valid candidates, take the one with minimal remaining
  286. // distance (important for #mysql_23023665 in buffer).
  287. signed_size_type next[2] = {0};
  288. bool possible[2] = {0};
  289. bool close[2] = {0};
  290. for (int i = 0; i < 2; i++)
  291. {
  292. next[i] = turn.operations[i].enriched.get_next_turn_index();
  293. possible[i] = traverse_possible(next[i]);
  294. close[i] = possible[i] && next[i] == start_turn_index;
  295. }
  296. if (close[0] != close[1])
  297. {
  298. // One of the operations will finish the ring. Take that one.
  299. selected_op_index = close[0] ? 0 : 1;
  300. debug_traverse(turn, turn.operations[selected_op_index], "Candidate cc closing");
  301. return true;
  302. }
  303. if (BOOST_GEOMETRY_CONDITION(OverlayType == overlay_buffer)
  304. && possible[0] && possible[1])
  305. {
  306. // Buffers sometimes have multiple overlapping pieces, where remaining
  307. // distance could lead to the wrong choice. Take the matching operation.
  308. bool is_target[2] = {0};
  309. for (int i = 0; i < 2; i++)
  310. {
  311. turn_operation_type const& next_op = m_turns[next[i]].operations[i];
  312. is_target[i] = next_op.operation == target_operation;
  313. }
  314. if (is_target[0] != is_target[1])
  315. {
  316. // Take the matching operation
  317. selected_op_index = is_target[0] ? 0 : 1;
  318. debug_traverse(turn, turn.operations[selected_op_index], "Candidate cc target");
  319. return true;
  320. }
  321. }
  322. static bool const is_union = target_operation == operation_union;
  323. typename turn_operation_type::comparable_distance_type
  324. best_remaining_distance = 0;
  325. bool result = false;
  326. for (int i = 0; i < 2; i++)
  327. {
  328. if (!possible[i])
  329. {
  330. continue;
  331. }
  332. turn_operation_type const& op = turn.operations[i];
  333. if (! result
  334. || (is_union && op.remaining_distance > best_remaining_distance)
  335. || (!is_union && op.remaining_distance < best_remaining_distance))
  336. {
  337. debug_traverse(turn, op, "First candidate cc", ! result);
  338. debug_traverse(turn, op, "Candidate cc override (remaining)",
  339. result && op.remaining_distance < best_remaining_distance);
  340. selected_op_index = i;
  341. best_remaining_distance = op.remaining_distance;
  342. result = true;
  343. }
  344. }
  345. return result;
  346. }
  347. inline
  348. bool select_noncc_operation(turn_type const& turn,
  349. segment_identifier const& previous_seg_id,
  350. int& selected_op_index) const
  351. {
  352. bool result = false;
  353. for (int i = 0; i < 2; i++)
  354. {
  355. turn_operation_type const& op = turn.operations[i];
  356. if (op.operation == target_operation
  357. && ! op.visited.finished()
  358. && ! op.visited.visited()
  359. && (! result || select_source(turn, op.seg_id, previous_seg_id)))
  360. {
  361. selected_op_index = i;
  362. debug_traverse(turn, op, "Candidate");
  363. result = true;
  364. }
  365. }
  366. return result;
  367. }
  368. inline
  369. bool select_preferred_operation(turn_type const& turn,
  370. signed_size_type turn_index,
  371. signed_size_type start_turn_index,
  372. int& selected_op_index) const
  373. {
  374. bool option[2] = {0};
  375. bool finishing[2] = {0};
  376. bool preferred[2] = {0};
  377. std::size_t shortcut_level[2] = {0};
  378. for (int i = 0; i < 2; i++)
  379. {
  380. turn_operation_type const& op = turn.operations[i];
  381. if (op.operation == target_operation
  382. && ! op.visited.finished()
  383. && ! op.visited.visited())
  384. {
  385. option[i] = true;
  386. if (op.enriched.get_next_turn_index() == start_turn_index)
  387. {
  388. finishing[i] = true;
  389. }
  390. else
  391. {
  392. shortcut_level[i] = get_shortcut_level(op, start_turn_index,
  393. turn_index);
  394. }
  395. if (op.enriched.prefer_start)
  396. {
  397. preferred[i] = true;
  398. }
  399. }
  400. }
  401. if (option[0] != option[1])
  402. {
  403. // Only one operation is acceptable, take that one
  404. selected_op_index = option[0] ? 0 : 1;
  405. return true;
  406. }
  407. if (option[0] && option[1])
  408. {
  409. // Both operations are acceptable
  410. if (finishing[0] != finishing[1])
  411. {
  412. // Prefer operation finishing the ring
  413. selected_op_index = finishing[0] ? 0 : 1;
  414. return true;
  415. }
  416. if (shortcut_level[0] != shortcut_level[1])
  417. {
  418. // If a turn can travel to itself again (without closing the
  419. // ring), take the shortest one
  420. selected_op_index = shortcut_level[0] < shortcut_level[1] ? 0 : 1;
  421. return true;
  422. }
  423. if (preferred[0] != preferred[1])
  424. {
  425. // Only one operation is preferred (== was not intersection)
  426. selected_op_index = preferred[0] ? 0 : 1;
  427. return true;
  428. }
  429. }
  430. for (int i = 0; i < 2; i++)
  431. {
  432. if (option[i])
  433. {
  434. selected_op_index = 0;
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. inline
  441. bool select_operation(turn_type const& turn,
  442. signed_size_type turn_index,
  443. signed_size_type start_turn_index,
  444. segment_identifier const& previous_seg_id,
  445. int& selected_op_index) const
  446. {
  447. bool result = false;
  448. selected_op_index = -1;
  449. if (turn.both(operation_continue))
  450. {
  451. result = select_cc_operation(turn, start_turn_index,
  452. selected_op_index);
  453. }
  454. else if (BOOST_GEOMETRY_CONDITION(OverlayType == overlay_dissolve))
  455. {
  456. result = select_preferred_operation(turn, turn_index,
  457. start_turn_index, selected_op_index);
  458. }
  459. else
  460. {
  461. result = select_noncc_operation(turn, previous_seg_id,
  462. selected_op_index);
  463. }
  464. if (result)
  465. {
  466. debug_traverse(turn, turn.operations[selected_op_index], "Accepted");
  467. }
  468. return result;
  469. }
  470. inline int starting_operation_index(turn_type const& turn) const
  471. {
  472. for (int i = 0; i < 2; i++)
  473. {
  474. if (turn.operations[i].visited.started())
  475. {
  476. return i;
  477. }
  478. }
  479. return -1;
  480. }
  481. inline bool both_finished(turn_type const& turn) const
  482. {
  483. for (int i = 0; i < 2; i++)
  484. {
  485. if (! turn.operations[i].visited.finished())
  486. {
  487. return false;
  488. }
  489. }
  490. return true;
  491. }
  492. inline int priority_of_turn_in_cluster_union(sort_by_side::rank_type selected_rank,
  493. typename sbs_type::rp const& ranked_point,
  494. std::set<signed_size_type> const& cluster_indices,
  495. signed_size_type start_turn_index, int start_op_index) const
  496. {
  497. // Returns 0: not OK
  498. // Returns 1: OK but next turn is in same cluster
  499. // Returns 2: OK
  500. // Returns 3: OK and start turn matches
  501. // Returns 4: OK and start turn and start op both match
  502. if (ranked_point.rank != selected_rank
  503. || ranked_point.direction != sort_by_side::dir_to)
  504. {
  505. return 0;
  506. }
  507. auto const& turn = m_turns[ranked_point.turn_index];
  508. auto const& op = turn.operations[ranked_point.operation_index];
  509. // Check finalized: TODO: this should be finetuned, it is not necessary
  510. if (op.visited.finalized())
  511. {
  512. return 0;
  513. }
  514. if (BOOST_GEOMETRY_CONDITION(OverlayType != overlay_dissolve)
  515. && (op.enriched.count_left != 0 || op.enriched.count_right == 0))
  516. {
  517. // Check counts: in some cases interior rings might be generated with
  518. // polygons on both sides. For dissolve it can be anything.
  519. return 0;
  520. }
  521. bool const to_start = ranked_point.turn_index == start_turn_index;
  522. bool const to_start_index = ranked_point.operation_index == start_op_index;
  523. bool const next_in_same_cluster
  524. = cluster_indices.count(op.enriched.get_next_turn_index()) > 0;
  525. return to_start && to_start_index ? 4
  526. : to_start ? 3
  527. : next_in_same_cluster ? 1
  528. : 2
  529. ;
  530. }
  531. template <typename RankedPoint>
  532. inline turn_operation_type const& operation_from_rank(RankedPoint const& rp) const
  533. {
  534. return m_turns[rp.turn_index].operations[rp.operation_index];
  535. }
  536. inline sort_by_side::rank_type select_rank(sbs_type const& sbs) const
  537. {
  538. static bool const is_intersection
  539. = target_operation == operation_intersection;
  540. // Take the first outgoing rank corresponding to incoming region,
  541. // or take another region if it is not isolated
  542. auto const& in_op = operation_from_rank(sbs.m_ranked_points.front());
  543. for (std::size_t i = 0; i < sbs.m_ranked_points.size(); i++)
  544. {
  545. auto const& rp = sbs.m_ranked_points[i];
  546. if (rp.rank == 0 || rp.direction == sort_by_side::dir_from)
  547. {
  548. continue;
  549. }
  550. auto const& out_op = operation_from_rank(rp);
  551. if (out_op.operation != target_operation
  552. && out_op.operation != operation_continue)
  553. {
  554. continue;
  555. }
  556. if (in_op.enriched.region_id == out_op.enriched.region_id
  557. || (is_intersection && ! out_op.enriched.isolated))
  558. {
  559. // Region corresponds to incoming region, or (for intersection)
  560. // there is a non-isolated other region which should be taken
  561. return rp.rank;
  562. }
  563. }
  564. return -1;
  565. }
  566. inline bool select_from_cluster_union(signed_size_type& turn_index,
  567. std::set<signed_size_type> const& cluster_indices,
  568. int& op_index, sbs_type const& sbs,
  569. signed_size_type start_turn_index, int start_op_index) const
  570. {
  571. sort_by_side::rank_type const selected_rank = select_rank(sbs);
  572. int current_priority = 0;
  573. for (std::size_t i = 1; i < sbs.m_ranked_points.size(); i++)
  574. {
  575. typename sbs_type::rp const& ranked_point = sbs.m_ranked_points[i];
  576. if (ranked_point.rank > selected_rank)
  577. {
  578. break;
  579. }
  580. int const priority = priority_of_turn_in_cluster_union(selected_rank,
  581. ranked_point, cluster_indices, start_turn_index, start_op_index);
  582. if (priority > current_priority)
  583. {
  584. current_priority = priority;
  585. turn_index = ranked_point.turn_index;
  586. op_index = ranked_point.operation_index;
  587. }
  588. }
  589. return current_priority > 0;
  590. }
  591. inline bool analyze_cluster_intersection(signed_size_type& turn_index,
  592. int& op_index, sbs_type const& sbs) const
  593. {
  594. // Select the rank based on regions and isolation
  595. sort_by_side::rank_type const selected_rank = select_rank(sbs);
  596. if (selected_rank <= 0)
  597. {
  598. return false;
  599. }
  600. // From these ranks, select the index: the first, or the one with
  601. // the smallest remaining distance
  602. typename turn_operation_type::comparable_distance_type
  603. min_remaining_distance = 0;
  604. std::size_t selected_index = sbs.m_ranked_points.size();
  605. for (std::size_t i = 0; i < sbs.m_ranked_points.size(); i++)
  606. {
  607. auto const& ranked_point = sbs.m_ranked_points[i];
  608. if (ranked_point.rank > selected_rank)
  609. {
  610. break;
  611. }
  612. else if (ranked_point.rank == selected_rank)
  613. {
  614. auto const& op = operation_from_rank(ranked_point);
  615. if (op.visited.finalized())
  616. {
  617. // This direction is already traveled,
  618. // it cannot be traveled again
  619. continue;
  620. }
  621. if (selected_index == sbs.m_ranked_points.size()
  622. || op.remaining_distance < min_remaining_distance)
  623. {
  624. // It was unassigned or it is better
  625. selected_index = i;
  626. min_remaining_distance = op.remaining_distance;
  627. }
  628. }
  629. }
  630. if (selected_index == sbs.m_ranked_points.size())
  631. {
  632. // Should not happen, there must be points with the selected rank
  633. return false;
  634. }
  635. auto const& ranked_point = sbs.m_ranked_points[selected_index];
  636. turn_index = ranked_point.turn_index;
  637. op_index = ranked_point.operation_index;
  638. return true;
  639. }
  640. inline bool fill_sbs(sbs_type& sbs,
  641. signed_size_type turn_index,
  642. std::set<signed_size_type> const& cluster_indices,
  643. segment_identifier const& previous_seg_id) const
  644. {
  645. for (auto cluster_turn_index : cluster_indices)
  646. {
  647. turn_type const& cluster_turn = m_turns[cluster_turn_index];
  648. if (cluster_turn.discarded)
  649. {
  650. // Defensive check, discarded turns should not be in cluster
  651. continue;
  652. }
  653. for (int i = 0; i < 2; i++)
  654. {
  655. sbs.add(cluster_turn,
  656. cluster_turn.operations[i],
  657. cluster_turn_index, i, previous_seg_id,
  658. m_geometry1, m_geometry2,
  659. cluster_turn_index == turn_index);
  660. }
  661. }
  662. if (! sbs.has_origin())
  663. {
  664. return false;
  665. }
  666. turn_type const& turn = m_turns[turn_index];
  667. sbs.apply(turn.point);
  668. return true;
  669. }
  670. inline bool select_turn_from_cluster(signed_size_type& turn_index,
  671. int& op_index,
  672. signed_size_type start_turn_index, int start_op_index,
  673. segment_identifier const& previous_seg_id) const
  674. {
  675. bool const is_union = target_operation == operation_union;
  676. turn_type const& turn = m_turns[turn_index];
  677. BOOST_ASSERT(turn.is_clustered());
  678. auto mit = m_clusters.find(turn.cluster_id);
  679. BOOST_ASSERT(mit != m_clusters.end());
  680. cluster_info const& cinfo = mit->second;
  681. sbs_type sbs(m_strategy);
  682. if (! fill_sbs(sbs, turn_index, cinfo.turn_indices, previous_seg_id))
  683. {
  684. return false;
  685. }
  686. cluster_exits<OverlayType, Turns, sbs_type> exits(m_turns, cinfo.turn_indices, sbs);
  687. if (exits.apply(turn_index, op_index))
  688. {
  689. return true;
  690. }
  691. bool result = false;
  692. if (is_union)
  693. {
  694. result = select_from_cluster_union(turn_index, cinfo.turn_indices,
  695. op_index, sbs,
  696. start_turn_index, start_op_index);
  697. if (! result)
  698. {
  699. // There no way out found, try second pass in collected cluster exits
  700. result = exits.apply(turn_index, op_index, false);
  701. }
  702. }
  703. else
  704. {
  705. result = analyze_cluster_intersection(turn_index, op_index, sbs);
  706. }
  707. return result;
  708. }
  709. // Analyzes a non-clustered "ii" intersection, as if it is clustered.
  710. inline bool analyze_ii_intersection(signed_size_type& turn_index, int& op_index,
  711. turn_type const& current_turn,
  712. segment_identifier const& previous_seg_id)
  713. {
  714. sbs_type sbs(m_strategy);
  715. // Add this turn to the sort-by-side sorter
  716. for (int i = 0; i < 2; i++)
  717. {
  718. sbs.add(current_turn,
  719. current_turn.operations[i],
  720. turn_index, i, previous_seg_id,
  721. m_geometry1, m_geometry2,
  722. true);
  723. }
  724. if (! sbs.has_origin())
  725. {
  726. return false;
  727. }
  728. sbs.apply(current_turn.point);
  729. bool result = analyze_cluster_intersection(turn_index, op_index, sbs);
  730. return result;
  731. }
  732. inline void change_index_for_self_turn(signed_size_type& to_vertex_index,
  733. turn_type const& start_turn,
  734. turn_operation_type const& start_op,
  735. int start_op_index) const
  736. {
  737. if (BOOST_GEOMETRY_CONDITION(OverlayType != overlay_buffer
  738. && OverlayType != overlay_dissolve))
  739. {
  740. return;
  741. }
  742. const bool allow_uu = OverlayType != overlay_buffer;
  743. // It travels to itself, can happen. If this is a buffer, it can
  744. // sometimes travel to itself in the following configuration:
  745. //
  746. // +---->--+
  747. // | |
  748. // | +---*----+ *: one turn, with segment index 2/7
  749. // | | | |
  750. // | +---C | C: closing point (start/end)
  751. // | |
  752. // +------------+
  753. //
  754. // If it starts on segment 2 and travels to itself on segment 2, that
  755. // should be corrected to 7 because that is the shortest path
  756. //
  757. // Also a uu turn (touching with another buffered ring) might have this
  758. // apparent configuration, but there it should
  759. // always travel the whole ring
  760. turn_operation_type const& other_op
  761. = start_turn.operations[1 - start_op_index];
  762. bool const correct
  763. = (allow_uu || ! start_turn.both(operation_union))
  764. && start_op.seg_id.source_index == other_op.seg_id.source_index
  765. && start_op.seg_id.multi_index == other_op.seg_id.multi_index
  766. && start_op.seg_id.ring_index == other_op.seg_id.ring_index
  767. && start_op.seg_id.segment_index == to_vertex_index;
  768. #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSE)
  769. std::cout << " WARNING: self-buffer "
  770. << " correct=" << correct
  771. << " turn=" << operation_char(start_turn.operations[0].operation)
  772. << operation_char(start_turn.operations[1].operation)
  773. << " start=" << start_op.seg_id.segment_index
  774. << " from=" << to_vertex_index
  775. << " to=" << other_op.enriched.travels_to_vertex_index
  776. << std::endl;
  777. #endif
  778. if (correct)
  779. {
  780. to_vertex_index = other_op.enriched.travels_to_vertex_index;
  781. }
  782. }
  783. bool select_turn_from_enriched(signed_size_type& turn_index,
  784. segment_identifier& previous_seg_id,
  785. signed_size_type& to_vertex_index,
  786. signed_size_type start_turn_index,
  787. int start_op_index,
  788. turn_type const& previous_turn,
  789. turn_operation_type const& previous_op,
  790. bool is_start) const
  791. {
  792. to_vertex_index = -1;
  793. if (previous_op.enriched.next_ip_index < 0)
  794. {
  795. // There is no next IP on this segment
  796. if (previous_op.enriched.travels_to_vertex_index < 0
  797. || previous_op.enriched.travels_to_ip_index < 0)
  798. {
  799. return false;
  800. }
  801. to_vertex_index = previous_op.enriched.travels_to_vertex_index;
  802. if (is_start &&
  803. previous_op.enriched.travels_to_ip_index == start_turn_index)
  804. {
  805. change_index_for_self_turn(to_vertex_index, previous_turn,
  806. previous_op, start_op_index);
  807. }
  808. turn_index = previous_op.enriched.travels_to_ip_index;
  809. previous_seg_id = previous_op.seg_id;
  810. }
  811. else
  812. {
  813. // Take the next IP on this segment
  814. turn_index = previous_op.enriched.next_ip_index;
  815. previous_seg_id = previous_op.seg_id;
  816. }
  817. return true;
  818. }
  819. bool select_turn(signed_size_type start_turn_index, int start_op_index,
  820. signed_size_type& turn_index,
  821. int& op_index,
  822. int previous_op_index,
  823. signed_size_type previous_turn_index,
  824. segment_identifier const& previous_seg_id,
  825. bool is_start, bool has_points)
  826. {
  827. turn_type const& current_turn = m_turns[turn_index];
  828. bool const back_at_start_cluster
  829. = has_points
  830. && current_turn.is_clustered()
  831. && m_turns[start_turn_index].cluster_id == current_turn.cluster_id;
  832. if (BOOST_GEOMETRY_CONDITION(target_operation == operation_intersection))
  833. {
  834. // Intersection or difference
  835. if (has_points && (turn_index == start_turn_index || back_at_start_cluster))
  836. {
  837. // Intersection can always be finished if returning
  838. turn_index = start_turn_index;
  839. op_index = start_op_index;
  840. return true;
  841. }
  842. if (! current_turn.is_clustered()
  843. && current_turn.both(operation_intersection)
  844. && analyze_ii_intersection(turn_index, op_index,
  845. current_turn, previous_seg_id))
  846. {
  847. return true;
  848. }
  849. }
  850. else if (turn_index == start_turn_index || back_at_start_cluster)
  851. {
  852. // Union or buffer: cannot return immediately to starting turn, because it then
  853. // might miss a formed multi polygon with a touching point.
  854. auto const& current_op = current_turn.operations[op_index];
  855. signed_size_type const next_turn_index = current_op.enriched.get_next_turn_index();
  856. bool const to_other_turn = next_turn_index >= 0 && m_turns[next_turn_index].cluster_id != current_turn.cluster_id;
  857. if (! to_other_turn)
  858. {
  859. // Return to starting point
  860. turn_index = start_turn_index;
  861. op_index = start_op_index;
  862. return true;
  863. }
  864. }
  865. if (current_turn.is_clustered())
  866. {
  867. if (! select_turn_from_cluster(turn_index, op_index,
  868. start_turn_index, start_op_index, previous_seg_id))
  869. {
  870. return false;
  871. }
  872. if (is_start && turn_index == previous_turn_index)
  873. {
  874. op_index = previous_op_index;
  875. }
  876. }
  877. else
  878. {
  879. op_index = starting_operation_index(current_turn);
  880. if (op_index == -1)
  881. {
  882. if (both_finished(current_turn))
  883. {
  884. return false;
  885. }
  886. if (! select_operation(current_turn, turn_index,
  887. start_turn_index,
  888. previous_seg_id,
  889. op_index))
  890. {
  891. return false;
  892. }
  893. }
  894. }
  895. return true;
  896. }
  897. private :
  898. Geometry1 const& m_geometry1;
  899. Geometry2 const& m_geometry2;
  900. Turns& m_turns;
  901. Clusters const& m_clusters;
  902. RobustPolicy const& m_robust_policy;
  903. SideStrategy m_strategy;
  904. Visitor& m_visitor;
  905. };
  906. }} // namespace detail::overlay
  907. #endif // DOXYGEN_NO_DETAIL
  908. }} // namespace boost::geometry
  909. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_HPP