Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
graph_image.cc
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <vector>
27 
28 #include <mln/core/site_set/p_vertices.hh>
29 #include <mln/core/image/graph_elt_window.hh>
30 #include <mln/core/image/graph_elt_neighborhood.hh>
31 #include <mln/core/concept/function.hh>
32 #include <mln/core/neighb.hh>
33 #include <mln/core/var.hh>
34 #include <mln/accu/shape/bbox.hh>
35 #include <mln/fun/i2v/array.hh>
36 #include <mln/util/graph.hh>
37 #include <mln/debug/draw_graph.hh>
38 #include <mln/debug/println.hh>
39 
40 
41 /* The graph is as follows:
42 
43  0 1 2 3 4
44  .-----------
45  |
46  0 | 0 2
47  1 | \ / |
48  2 | 1 |
49  3 | \ |
50  4 | 3-4
51 
52 */
53 
54 
55 static const unsigned X = mln_max(unsigned); // Invalid id.
56 
57 
58 // Expected neighbors for forward and backward iteration.
59 // X is an invalid id.
60 static unsigned expected_fwd_nb[5][3] = { { 1, X, X },
61  { 0, 2, 3 },
62  { 1, 4, X },
63  { 1, 4, X },
64  { 3, 2, X } };
65 
66 static unsigned expected_bkd_nb[5][3] = { { 1, X, X },
67  { 3, 2, 0 },
68  { 4, 1, X },
69  { 4, 1, X },
70  { 2, 3, X } };
71 
72 
73 int main()
74 {
75  using namespace mln;
76 
77  /*--------.
78  | Graph. |
79  `--------*/
80 
81  // Points associated to vertices.
82  typedef fun::i2v::array<point2d> fsite_t;
83  fsite_t sites(5);
84  sites(0) = point2d(0,0); // Point associated to vertex 0.
85  sites(1) = point2d(2,2); // Point associated to vertex 1.
86  sites(2) = point2d(0,4); // Point associated to vertex 2.
87  sites(3) = point2d(4,3); // Point associated to vertex 3.
88  sites(4) = point2d(4,4); // Point associated to vertex 4.
89 
90  // Edges.
91  util::graph g;
92 
93  // Populate the graph with vertices.
94  g.add_vertices(sites.size());
95 
96  // Populate the graph with edges.
97  g.add_edge(0, 1);
98  g.add_edge(1, 2);
99  g.add_edge(1, 3);
100  g.add_edge(3, 4);
101  g.add_edge(4, 2);
102 
103  //g.print_debug(std::cout);
104 
105  /*----------------------.
106  | Graph image support. |
107  `----------------------*/
108 
110  S pv(g, sites);
111 
112  /*-------------.
113  | Graph image. |
114  `-------------*/
115 
116  // Graph values.
117  typedef fun::i2v::array<unsigned> viota_t;
118  viota_t iota(pv.nsites());
119  for (unsigned i = 0; i < iota.size(); ++i)
120  iota(i) = 10 + i;
121 
122  // Create graph image.
123  mln_const_VAR(ima, (iota | pv));
124 
125  {
126  // FIXME: Move this part to a special test case.
127 
128  // Compute the bounding box of 'ima'.
130  mln_piter_(ima_t) p(ima.domain());
131  for_all(p)
132  a.take(p);
133  box2d bbox = a.to_result();
134  mln_assertion(bbox == make::box2d(5, 5));
135 
136  // Print the image.
137  /* FIXME: Unfortunately, displaying graph images is not easy right
138  now (2008-02-05). We could use
139 
140  debug::println(ima);
141 
142  but there's not specialization working for graph_image; the one
143  selected by the compiler is based on a 2-D bbox, and expects
144  the interface of graph_image to work with points (not psites).
145 
146  An alternative is to use debug::draw_graph, but it doesn't show
147  the values, only the vertices and edges of the graph.
148 
149  The current solution is a mix between debug::draw_graph and
150  hand-made iterations. */
151  image2d<int> ima_rep(bbox);
152  debug::draw_graph(ima_rep, pv, 1, 9);
153  debug::println(ima_rep);
154  }
155 
156  /*------------.
157  | Iterators. |
158  `------------*/
159 
160  // iteration over the domain of IMA.
161  mln_piter_(ima_t) p(ima.domain());
162  unsigned i = 10;
163  for_all(p)
164  mln_assertion(ima(p) == i++);
165 
166  typedef graph_elt_window<util::graph,S> win_t;
167  win_t win;
168 
169  {
170  // Window - Forward iteration
171  mln_qiter_(win_t) q(win, p);
172  for_all(p)
173  {
174  i = 0;
175  for_all(q)
176  {
177  mln_assertion(expected_fwd_nb[p.id()][i] == q.id());
178  ++i;
179  }
180  }
181  }
182 
183  {
184  // Window - Backward iteration
185  mln_bkd_qiter_(win_t) q(win, p);
186  for_all(p)
187  {
188  i = 0;
189  for_all(q)
190  {
191  mln_assertion(expected_bkd_nb[p.id()][i] == q.id());
192  ++i;
193  }
194  }
195  }
196 
197  typedef graph_elt_neighborhood<util::graph,S> neighb_t;
198  neighb_t neigh;
199  {
200  // Neighborhood - Forward iteration
201  mln_niter_(neighb_t) n(neigh, p);
202 
203  for_all(p)
204  {
205  i = 0;
206  for_all(n)
207  {
208  mln_assertion(expected_fwd_nb[p.id()][i] == n.id());
209  ++i;
210  }
211  }
212  }
213 
214  {
215  // Neighborhood - Backward iteration
216  mln_bkd_niter_(neighb_t) n(neigh, p);
217  for_all(p)
218  {
219  i = 0;
220  for_all(n)
221  {
222  mln_assertion(expected_bkd_nb[p.id()][i] == n.id());
223  ++i;
224  }
225  }
226  }
227 }