traversal.hpp 39 KB

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