grid_graph_example.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //=======================================================================
  2. // Copyright 2009 Trustees of Indiana University.
  3. // Authors: Michael Hansen, Andrew Lumsdaine
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <iostream>
  10. #include <boost/array.hpp>
  11. #include <boost/graph/grid_graph.hpp>
  12. #define DIMENSIONS 3
  13. using namespace boost;
  14. typedef grid_graph<DIMENSIONS> Graph;
  15. typedef graph_traits<Graph> Traits;
  16. // Define a simple function to print vertices
  17. void print_vertex(Traits::vertex_descriptor vertex_to_print) {
  18. std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] <<
  19. ", " << vertex_to_print[2] << ")" << std::endl;
  20. }
  21. int main(int argc, char* argv[]) {
  22. // Define a 3x5x7 grid_graph where the second dimension doesn't wrap
  23. boost::array<std::size_t, 3> lengths = { { 3, 5, 7 } };
  24. boost::array<bool, 3> wrapped = { { true, false, true } };
  25. Graph graph(lengths, wrapped);
  26. // Do a round-trip test of the vertex index functions
  27. for (Traits::vertices_size_type v_index = 0;
  28. v_index < num_vertices(graph); ++v_index) {
  29. // The two indicies should always be equal
  30. std::cout << "Index of vertex " << v_index << " is " <<
  31. get(boost::vertex_index, graph, vertex(v_index, graph)) << std::endl;
  32. }
  33. // Do a round-trip test of the edge index functions
  34. for (Traits::edges_size_type e_index = 0;
  35. e_index < num_edges(graph); ++e_index) {
  36. // The two indicies should always be equal
  37. std::cout << "Index of edge " << e_index << " is " <<
  38. get(boost::edge_index, graph, edge_at(e_index, graph)) << std::endl;
  39. }
  40. // Print number of dimensions
  41. std::cout << graph.dimensions() << std::endl; // prints "3"
  42. // Print dimension lengths (same order as in the lengths array)
  43. std::cout << graph.length(0) << "x" << graph.length(1) <<
  44. "x" << graph.length(2) << std::endl; // prints "3x5x7"
  45. // Print dimension wrapping (W = wrapped, U = unwrapped)
  46. std::cout << (graph.wrapped(0) ? "W" : "U") << ", " <<
  47. (graph.wrapped(1) ? "W" : "U") << ", " <<
  48. (graph.wrapped(2) ? "W" : "U") << std::endl; // prints "W, U, W"
  49. // Start with the first vertex in the graph
  50. Traits::vertex_descriptor first_vertex = vertex(0, graph);
  51. print_vertex(first_vertex); // prints "(0, 0, 0)"
  52. // Print the next vertex in dimension 0
  53. print_vertex(graph.next(first_vertex, 0)); // prints "(1, 0, 0)"
  54. // Print the next vertex in dimension 1
  55. print_vertex(graph.next(first_vertex, 1)); // prints "(0, 1, 0)"
  56. // Print the 5th next vertex in dimension 2
  57. print_vertex(graph.next(first_vertex, 2, 5)); // prints "(0, 0, 4)"
  58. // Print the previous vertex in dimension 0 (wraps)
  59. print_vertex(graph.previous(first_vertex, 0)); // prints "(2, 0, 0)"
  60. // Print the previous vertex in dimension 1 (doesn't wrap, so it's the same)
  61. print_vertex(graph.previous(first_vertex, 1)); // prints "(0, 0, 0)"
  62. // Print the 20th previous vertex in dimension 2 (wraps around twice)
  63. print_vertex(graph.previous(first_vertex, 2, 20)); // prints "(0, 0, ?)"
  64. }