traversal.hpp 40 KB

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