$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph-data.cc
1 #include <mln/core/site_set/p_vertices.hh>
2 #include <mln/core/alias/point2d.hh>
3 #include <mln/core/concept/function.hh>
4 #include <mln/core/var.hh>
5 
6 #include <mln/fun/i2v/array.hh>
7 #include <mln/util/graph.hh>
8 #include <mln/pw/all.hh>
9 
10 #include <vector>
11 
12 
13 // \{
14 template <typename S>
15 struct viota_t : public mln::Function_v2v< viota_t<S> >
16 {
17  typedef unsigned result;
18 
19  viota_t(unsigned size)
20  {
21  v_.resize(size);
22  for(unsigned i = 0; i < size; ++i)
23  v_[i] = 10 + i;
24  }
25 
26  unsigned
27  operator()(const mln_psite(S)& p) const
28  {
29  return v_[p.v().id()];
30  }
31 
32  protected:
33  std::vector<result> v_;
34 };
35 // \}
36 
37 int main()
38 {
39  using namespace mln;
40 
41  // \{
42  util::graph g;
43 
44  for (unsigned i = 0; i < 5; ++i)
45  g.add_vertex(); // Add vertex 'i';
46  // \}
47 
48  // \{
49  g.add_edge(0, 1); // Associated to edge 0.
50  g.add_edge(1, 2); // Associated to edge 1.
51  g.add_edge(1, 3); // Associated to edge 2.
52  g.add_edge(3, 4); // Associated to edge 3.
53  g.add_edge(4, 2); // Associated to edge 4.
54  // \}
55 
56  // \{
57  typedef fun::i2v::array<point2d> F;
58  F f(5); // We need to map 5 vertices.
59  f(0) = point2d(0, 0);
60  f(1) = point2d(2, 2);
61  f(2) = point2d(0, 4);
62  f(3) = point2d(4, 3);
63  f(4) = point2d(4, 4);
64  // \}
65 
66  // \{
67  typedef p_vertices<util::graph, F> pv_t;
68  pv_t pv(g, f);
69  // \}
70 
71  // \{
72 
73  // Constructs an image
74  viota_t<pv_t> viota(pv.nsites());
75  mln_VAR(graph_vertices_ima, viota | pv);
76 
77  //Prints each vertex and its associated data.
78  mln_piter_(graph_vertices_ima_t) p(graph_vertices_ima.domain());
79  for_all(p)
80  std::cout << "graph_vertices_ima(" << p << ") = "
81  << graph_vertices_ima(p) << std::endl;
82 
83  // \}
84 }
85