traversal.hpp 32 KB

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