File: grid_graph_example.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (91 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//=======================================================================
// Copyright 2009 Trustees of Indiana University.
// Authors: Michael Hansen, Andrew Lumsdaine
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <iostream>
#include <boost/array.hpp>
#include <boost/graph/grid_graph.hpp>

#define DIMENSIONS 3
using namespace boost;

typedef grid_graph< DIMENSIONS > Graph;
typedef graph_traits< Graph > Traits;

// Define a simple function to print vertices
void print_vertex(Traits::vertex_descriptor vertex_to_print)
{
    std::cout << "(" << vertex_to_print[0] << ", " << vertex_to_print[1] << ", "
              << vertex_to_print[2] << ")" << std::endl;
}

int main(int argc, char* argv[])
{

    // Define a 3x5x7 grid_graph where the second dimension doesn't wrap
    boost::array< std::size_t, 3 > lengths = { { 3, 5, 7 } };
    boost::array< bool, 3 > wrapped = { { true, false, true } };
    Graph graph(lengths, wrapped);

    // Do a round-trip test of the vertex index functions
    for (Traits::vertices_size_type v_index = 0; v_index < num_vertices(graph);
         ++v_index)
    {

        // The two indicies should always be equal
        std::cout << "Index of vertex " << v_index << " is "
                  << get(boost::vertex_index, graph, vertex(v_index, graph))
                  << std::endl;
    }

    // Do a round-trip test of the edge index functions
    for (Traits::edges_size_type e_index = 0; e_index < num_edges(graph);
         ++e_index)
    {

        // The two indicies should always be equal
        std::cout << "Index of edge " << e_index << " is "
                  << get(boost::edge_index, graph, edge_at(e_index, graph))
                  << std::endl;
    }

    // Print number of dimensions
    std::cout << graph.dimensions() << std::endl; // prints "3"

    // Print dimension lengths (same order as in the lengths array)
    std::cout << graph.length(0) << "x" << graph.length(1) << "x"
              << graph.length(2) << std::endl; // prints "3x5x7"

    // Print dimension wrapping (W = wrapped, U = unwrapped)
    std::cout << (graph.wrapped(0) ? "W" : "U") << ", "
              << (graph.wrapped(1) ? "W" : "U") << ", "
              << (graph.wrapped(2) ? "W" : "U")
              << std::endl; // prints "W, U, W"

    // Start with the first vertex in the graph
    Traits::vertex_descriptor first_vertex = vertex(0, graph);
    print_vertex(first_vertex); // prints "(0, 0, 0)"

    // Print the next vertex in dimension 0
    print_vertex(graph.next(first_vertex, 0)); // prints "(1, 0, 0)"

    // Print the next vertex in dimension 1
    print_vertex(graph.next(first_vertex, 1)); // prints "(0, 1, 0)"

    // Print the 5th next vertex in dimension 2
    print_vertex(graph.next(first_vertex, 2, 5)); // prints "(0, 0, 4)"

    // Print the previous vertex in dimension 0 (wraps)
    print_vertex(graph.previous(first_vertex, 0)); // prints "(2, 0, 0)"

    // Print the previous vertex in dimension 1 (doesn't wrap, so it's the same)
    print_vertex(graph.previous(first_vertex, 1)); // prints "(0, 0, 0)"

    // Print the 20th previous vertex in dimension 2 (wraps around twice)
    print_vertex(graph.previous(first_vertex, 2, 20)); // prints "(0, 0, ?)"
}