chrobak_payne_drawing.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //=======================================================================
  2. // Copyright (c) Aaron Windsor 2007
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #ifndef __CHROBAK_PAYNE_DRAWING_HPP__
  9. #define __CHROBAK_PAYNE_DRAWING_HPP__
  10. #include <vector>
  11. #include <list>
  12. #include <stack>
  13. #include <boost/config.hpp>
  14. #include <boost/graph/graph_traits.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. namespace boost
  17. {
  18. namespace graph
  19. {
  20. namespace detail
  21. {
  22. template < typename Graph, typename VertexToVertexMap,
  23. typename VertexTo1DCoordMap >
  24. void accumulate_offsets(
  25. typename graph_traits< Graph >::vertex_descriptor v,
  26. std::size_t offset, const Graph& g, VertexTo1DCoordMap x,
  27. VertexTo1DCoordMap delta_x, VertexToVertexMap left,
  28. VertexToVertexMap right)
  29. {
  30. typedef typename graph_traits< Graph >::vertex_descriptor
  31. vertex_descriptor;
  32. // Suggestion of explicit stack from Aaron Windsor to avoid system
  33. // stack overflows.
  34. typedef std::pair< vertex_descriptor, std::size_t > stack_entry;
  35. std::stack< stack_entry > st;
  36. st.push(stack_entry(v, offset));
  37. while (!st.empty())
  38. {
  39. vertex_descriptor v = st.top().first;
  40. std::size_t offset = st.top().second;
  41. st.pop();
  42. if (v != graph_traits< Graph >::null_vertex())
  43. {
  44. x[v] += delta_x[v] + offset;
  45. st.push(stack_entry(left[v], x[v]));
  46. st.push(stack_entry(right[v], x[v]));
  47. }
  48. }
  49. }
  50. } /*namespace detail*/
  51. } /*namespace graph*/
  52. template < typename Graph, typename PlanarEmbedding, typename ForwardIterator,
  53. typename GridPositionMap, typename VertexIndexMap >
  54. void chrobak_payne_straight_line_drawing(const Graph& g,
  55. PlanarEmbedding embedding, ForwardIterator ordering_begin,
  56. ForwardIterator ordering_end, GridPositionMap drawing, VertexIndexMap vm)
  57. {
  58. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  59. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator_t;
  60. typedef typename PlanarEmbedding::value_type::const_iterator
  61. edge_permutation_iterator_t;
  62. typedef typename graph_traits< Graph >::vertices_size_type v_size_t;
  63. typedef std::vector< vertex_t > vertex_vector_t;
  64. typedef std::vector< v_size_t > vsize_vector_t;
  65. typedef std::vector< bool > bool_vector_t;
  66. typedef boost::iterator_property_map< typename vertex_vector_t::iterator,
  67. VertexIndexMap >
  68. vertex_to_vertex_map_t;
  69. typedef boost::iterator_property_map< typename vsize_vector_t::iterator,
  70. VertexIndexMap >
  71. vertex_to_vsize_map_t;
  72. typedef boost::iterator_property_map< typename bool_vector_t::iterator,
  73. VertexIndexMap >
  74. vertex_to_bool_map_t;
  75. vertex_vector_t left_vector(
  76. num_vertices(g), graph_traits< Graph >::null_vertex());
  77. vertex_vector_t right_vector(
  78. num_vertices(g), graph_traits< Graph >::null_vertex());
  79. vsize_vector_t seen_as_right_vector(num_vertices(g), 0);
  80. vsize_vector_t seen_vector(num_vertices(g), 0);
  81. vsize_vector_t delta_x_vector(num_vertices(g), 0);
  82. vsize_vector_t y_vector(num_vertices(g));
  83. vsize_vector_t x_vector(num_vertices(g), 0);
  84. bool_vector_t installed_vector(num_vertices(g), false);
  85. vertex_to_vertex_map_t left(left_vector.begin(), vm);
  86. vertex_to_vertex_map_t right(right_vector.begin(), vm);
  87. vertex_to_vsize_map_t seen_as_right(seen_as_right_vector.begin(), vm);
  88. vertex_to_vsize_map_t seen(seen_vector.begin(), vm);
  89. vertex_to_vsize_map_t delta_x(delta_x_vector.begin(), vm);
  90. vertex_to_vsize_map_t y(y_vector.begin(), vm);
  91. vertex_to_vsize_map_t x(x_vector.begin(), vm);
  92. vertex_to_bool_map_t installed(installed_vector.begin(), vm);
  93. v_size_t timestamp = 1;
  94. vertex_vector_t installed_neighbors;
  95. ForwardIterator itr = ordering_begin;
  96. vertex_t v1 = *itr;
  97. ++itr;
  98. vertex_t v2 = *itr;
  99. ++itr;
  100. vertex_t v3 = *itr;
  101. ++itr;
  102. delta_x[v2] = 1;
  103. delta_x[v3] = 1;
  104. y[v1] = 0;
  105. y[v2] = 0;
  106. y[v3] = 1;
  107. right[v1] = v3;
  108. right[v3] = v2;
  109. installed[v1] = installed[v2] = installed[v3] = true;
  110. for (ForwardIterator itr_end = ordering_end; itr != itr_end; ++itr)
  111. {
  112. vertex_t v = *itr;
  113. // First, find the leftmost and rightmost neighbor of v on the outer
  114. // cycle of the embedding.
  115. // Note: since we're moving clockwise through the edges adjacent to v,
  116. // we're actually moving from right to left among v's neighbors on the
  117. // outer face (since v will be installed above them all) looking for
  118. // the leftmost and rightmost installed neigbhors
  119. vertex_t leftmost = graph_traits< Graph >::null_vertex();
  120. vertex_t rightmost = graph_traits< Graph >::null_vertex();
  121. installed_neighbors.clear();
  122. vertex_t prev_vertex = graph_traits< Graph >::null_vertex();
  123. edge_permutation_iterator_t pi, pi_end;
  124. pi_end = embedding[v].end();
  125. for (pi = embedding[v].begin(); pi != pi_end; ++pi)
  126. {
  127. vertex_t curr_vertex
  128. = source(*pi, g) == v ? target(*pi, g) : source(*pi, g);
  129. // Skip any self-loops or parallel edges
  130. if (curr_vertex == v || curr_vertex == prev_vertex)
  131. continue;
  132. if (installed[curr_vertex])
  133. {
  134. seen[curr_vertex] = timestamp;
  135. if (right[curr_vertex] != graph_traits< Graph >::null_vertex())
  136. {
  137. seen_as_right[right[curr_vertex]] = timestamp;
  138. }
  139. installed_neighbors.push_back(curr_vertex);
  140. }
  141. prev_vertex = curr_vertex;
  142. }
  143. typename vertex_vector_t::iterator vi, vi_end;
  144. vi_end = installed_neighbors.end();
  145. for (vi = installed_neighbors.begin(); vi != vi_end; ++vi)
  146. {
  147. if (right[*vi] == graph_traits< Graph >::null_vertex()
  148. || seen[right[*vi]] != timestamp)
  149. rightmost = *vi;
  150. if (seen_as_right[*vi] != timestamp)
  151. leftmost = *vi;
  152. }
  153. ++timestamp;
  154. // stretch gaps
  155. ++delta_x[right[leftmost]];
  156. ++delta_x[rightmost];
  157. // adjust offsets
  158. std::size_t delta_p_q = 0;
  159. vertex_t stopping_vertex = right[rightmost];
  160. for (vertex_t temp = right[leftmost]; temp != stopping_vertex;
  161. temp = right[temp])
  162. {
  163. delta_p_q += delta_x[temp];
  164. }
  165. delta_x[v] = ((y[rightmost] + delta_p_q) - y[leftmost]) / 2;
  166. y[v] = y[leftmost] + delta_x[v];
  167. delta_x[rightmost] = delta_p_q - delta_x[v];
  168. bool leftmost_and_rightmost_adjacent = right[leftmost] == rightmost;
  169. if (!leftmost_and_rightmost_adjacent)
  170. delta_x[right[leftmost]] -= delta_x[v];
  171. // install v
  172. if (!leftmost_and_rightmost_adjacent)
  173. {
  174. left[v] = right[leftmost];
  175. vertex_t next_to_rightmost;
  176. for (vertex_t temp = leftmost; temp != rightmost;
  177. temp = right[temp])
  178. {
  179. next_to_rightmost = temp;
  180. }
  181. right[next_to_rightmost] = graph_traits< Graph >::null_vertex();
  182. }
  183. else
  184. {
  185. left[v] = graph_traits< Graph >::null_vertex();
  186. }
  187. right[leftmost] = v;
  188. right[v] = rightmost;
  189. installed[v] = true;
  190. }
  191. graph::detail::accumulate_offsets(
  192. *ordering_begin, 0, g, x, delta_x, left, right);
  193. vertex_iterator_t vi, vi_end;
  194. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  195. {
  196. vertex_t v(*vi);
  197. drawing[v].x = x[v];
  198. drawing[v].y = y[v];
  199. }
  200. }
  201. template < typename Graph, typename PlanarEmbedding, typename ForwardIterator,
  202. typename GridPositionMap >
  203. inline void chrobak_payne_straight_line_drawing(const Graph& g,
  204. PlanarEmbedding embedding, ForwardIterator ord_begin,
  205. ForwardIterator ord_end, GridPositionMap drawing)
  206. {
  207. chrobak_payne_straight_line_drawing(
  208. g, embedding, ord_begin, ord_end, drawing, get(vertex_index, g));
  209. }
  210. } // namespace boost
  211. #endif //__CHROBAK_PAYNE_DRAWING_HPP__