• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include <iostream>
11 #include <boost/array.hpp>
12 #include <boost/graph/grid_graph.hpp>
13 
14 #define DIMENSIONS 3
15 using namespace boost;
16 
17 typedef grid_graph< DIMENSIONS > Graph;
18 typedef graph_traits< Graph > Traits;
19 
20 // Define a simple function to print vertices
print_vertex(Traits::vertex_descriptor vertex_to_print)21 void print_vertex(Traits::vertex_descriptor vertex_to_print)
22 {
23     std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] << ", "
24               << vertex_to_print[2] << ")" << std::endl;
25 }
26 
main(int argc,char * argv[])27 int main(int argc, char* argv[])
28 {
29 
30     // Define a 3x5x7 grid_graph where the second dimension doesn't wrap
31     boost::array< std::size_t, 3 > lengths = { { 3, 5, 7 } };
32     boost::array< bool, 3 > wrapped = { { true, false, true } };
33     Graph graph(lengths, wrapped);
34 
35     // Do a round-trip test of the vertex index functions
36     for (Traits::vertices_size_type v_index = 0; v_index < num_vertices(graph);
37          ++v_index)
38     {
39 
40         // The two indicies should always be equal
41         std::cout << "Index of vertex " << v_index << " is "
42                   << get(boost::vertex_index, graph, vertex(v_index, graph))
43                   << std::endl;
44     }
45 
46     // Do a round-trip test of the edge index functions
47     for (Traits::edges_size_type e_index = 0; e_index < num_edges(graph);
48          ++e_index)
49     {
50 
51         // The two indicies should always be equal
52         std::cout << "Index of edge " << e_index << " is "
53                   << get(boost::edge_index, graph, edge_at(e_index, graph))
54                   << std::endl;
55     }
56 
57     // Print number of dimensions
58     std::cout << graph.dimensions() << std::endl; // prints "3"
59 
60     // Print dimension lengths (same order as in the lengths array)
61     std::cout << graph.length(0) << "x" << graph.length(1) << "x"
62               << graph.length(2) << std::endl; // prints "3x5x7"
63 
64     // Print dimension wrapping (W = wrapped, U = unwrapped)
65     std::cout << (graph.wrapped(0) ? "W" : "U") << ", "
66               << (graph.wrapped(1) ? "W" : "U") << ", "
67               << (graph.wrapped(2) ? "W" : "U")
68               << std::endl; // prints "W, U, W"
69 
70     // Start with the first vertex in the graph
71     Traits::vertex_descriptor first_vertex = vertex(0, graph);
72     print_vertex(first_vertex); // prints "(0, 0, 0)"
73 
74     // Print the next vertex in dimension 0
75     print_vertex(graph.next(first_vertex, 0)); // prints "(1, 0, 0)"
76 
77     // Print the next vertex in dimension 1
78     print_vertex(graph.next(first_vertex, 1)); // prints "(0, 1, 0)"
79 
80     // Print the 5th next vertex in dimension 2
81     print_vertex(graph.next(first_vertex, 2, 5)); // prints "(0, 0, 4)"
82 
83     // Print the previous vertex in dimension 0 (wraps)
84     print_vertex(graph.previous(first_vertex, 0)); // prints "(2, 0, 0)"
85 
86     // Print the previous vertex in dimension 1 (doesn't wrap, so it's the same)
87     print_vertex(graph.previous(first_vertex, 1)); // prints "(0, 0, 0)"
88 
89     // Print the 20th previous vertex in dimension 2 (wraps around twice)
90     print_vertex(graph.previous(first_vertex, 2, 20)); // prints "(0, 0, ?)"
91 }
92