check_enrich.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 2018.
  4. // Modifications copyright (c) 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_CHECK_ENRICH_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CHECK_ENRICH_HPP
  11. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  12. #include <iostream>
  13. #endif // BOOST_GEOMETRY_DEBUG_ENRICH
  14. #include <cstddef>
  15. #include <vector>
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail { namespace overlay
  24. {
  25. template<typename Turn>
  26. struct meta_turn
  27. {
  28. int index;
  29. Turn const* turn;
  30. bool handled[2];
  31. inline meta_turn(int i, Turn const& t)
  32. : index(i), turn(&t)
  33. {
  34. handled[0] = false;
  35. handled[1] = false;
  36. }
  37. };
  38. template <typename MetaTurn>
  39. inline void display(MetaTurn const& meta_turn, const char* reason = "")
  40. {
  41. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  42. std::cout << meta_turn.index
  43. << "\tMethods: " << method_char(meta_turn.turn->method)
  44. << " operations: " << operation_char(meta_turn.turn->operations[0].operation)
  45. << operation_char(meta_turn.turn->operations[1].operation)
  46. << " travels to " << meta_turn.turn->operations[0].enriched.travels_to_ip_index
  47. << " and " << meta_turn.turn->operations[1].enriched.travels_to_ip_index
  48. //<< " -> " << op_index
  49. << " " << reason
  50. << std::endl;
  51. #endif
  52. }
  53. template <typename MetaTurns, typename MetaTurn>
  54. inline void check_detailed(MetaTurns& meta_turns, MetaTurn const& meta_turn,
  55. int op_index, int cycle, int start, operation_type for_operation,
  56. bool& error)
  57. {
  58. display(meta_turn);
  59. int const ip_index = meta_turn.turn->operations[op_index].enriched.travels_to_ip_index;
  60. if (ip_index >= 0)
  61. {
  62. bool found = false;
  63. if (ip_index == start)
  64. {
  65. display(meta_turns[ip_index], " FINISH");
  66. return;
  67. }
  68. // check on continuing, or on same-operation-on-same-geometry
  69. if (! meta_turns[ip_index].handled[op_index]
  70. && (meta_turns[ip_index].turn->operations[op_index].operation == operation_continue
  71. || meta_turns[ip_index].turn->operations[op_index].operation == for_operation)
  72. )
  73. {
  74. meta_turns[ip_index].handled[op_index] = true;
  75. check_detailed(meta_turns, meta_turns[ip_index], op_index, cycle, start, for_operation, error);
  76. found = true;
  77. }
  78. // check on other geometry
  79. if (! found)
  80. {
  81. int const other_index = 1 - op_index;
  82. if (! meta_turns[ip_index].handled[other_index]
  83. && meta_turns[ip_index].turn->operations[other_index].operation == for_operation)
  84. {
  85. meta_turns[ip_index].handled[other_index] = true;
  86. check_detailed(meta_turns, meta_turns[ip_index], other_index, cycle, start, for_operation, error);
  87. found = true;
  88. }
  89. }
  90. if (! found)
  91. {
  92. display(meta_turns[ip_index], " STOP");
  93. error = true;
  94. #ifndef BOOST_GEOMETRY_DEBUG_ENRICH
  95. //std::cout << " STOP";
  96. #endif
  97. }
  98. }
  99. }
  100. template <typename TurnPoints>
  101. inline bool check_graph(TurnPoints& turn_points, operation_type for_operation)
  102. {
  103. typedef typename boost::range_value<TurnPoints>::type turn_point_type;
  104. bool error = false;
  105. std::vector<meta_turn<turn_point_type> > meta_turns;
  106. for_each_with_index(turn_points, [&](std::size_t index, auto const& point)
  107. {
  108. meta_turns.push_back(meta_turn<turn_point_type>(index, point));
  109. });
  110. int cycle = 0;
  111. for (auto& meta_turn : meta_turns)
  112. {
  113. if (! (meta_turn.turn->blocked() || meta_turn.turn->discarded))
  114. {
  115. for (int i = 0 ; i < 2; i++)
  116. {
  117. if (! meta_turn.handled[i]
  118. && meta_turn.turn->operations[i].operation == for_operation)
  119. {
  120. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  121. std::cout << "CYCLE " << cycle << std::endl;
  122. #endif
  123. meta_turn.handled[i] = true;
  124. check_detailed(meta_turns, meta_turn, i, cycle++, meta_turn.index, for_operation, error);
  125. #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
  126. std::cout <<" END CYCLE " << meta_turn.index << std::endl;
  127. #endif
  128. }
  129. }
  130. }
  131. }
  132. return error;
  133. }
  134. }} // namespace detail::overlay
  135. #endif //DOXYGEN_NO_DETAIL
  136. }} // namespace boost::geometry
  137. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_CHECK_ENRICH_HPP