$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vertex_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/image/vertex_image.hh>
29 #include <mln/make/vertex_image.hh>
30 #include <mln/core/image/image2d.hh>
31 #include <mln/accu/shape/bbox.hh>
32 #include <mln/fun/i2v/array.hh>
33 #include <mln/util/graph.hh>
34 #include <mln/debug/draw_graph.hh>
35 
36 
37 
38 /* The graph is as follows:
39 
40  0 1 2 3 4
41  .-----------
42  |
43  0 | 0 2
44  1 | \ / |
45  2 | 1 |
46  3 | \ |
47  4 | 3-4
48 
49 */
50 
51 
52 static const unsigned X = mln_max(unsigned); // Invalid id.
53 
54 
55 // Expected neighbors for forward and backward iteration.
56 // X is an invalid id.
57 static unsigned expected_fwd_nb[5][3] = { { 1, X, X },
58  { 0, 2, 3 },
59  { 1, 4, X },
60  { 1, 4, X },
61  { 3, 2, X } };
62 
63 static unsigned expected_bkd_nb[5][3] = { { 1, X, X },
64  { 3, 2, 0 },
65  { 4, 1, X },
66  { 4, 1, X },
67  { 2, 3, X } };
68 
69 
70 int main()
71 {
72  using namespace mln;
73 
74  /*--------.
75  | Graph. |
76  `--------*/
77 
78  // Points associated to vertices.
79  typedef fun::i2v::array<point2d> fsite_t;
80  fsite_t sites(5);
81  sites(0) = point2d(0,0); // Point associated to vertex 0.
82  sites(1) = point2d(2,2); // Point associated to vertex 1.
83  sites(2) = point2d(0,4); // Point associated to vertex 2.
84  sites(3) = point2d(4,3); // Point associated to vertex 3.
85  sites(4) = point2d(4,4); // Point associated to vertex 4.
86 
87  // Edges.
88  util::graph g;
89 
90  // Populate the graph with vertices.
91  g.add_vertices(sites.size());
92 
93  // Populate the graph with edges.
94  g.add_edge(0, 1);
95  g.add_edge(1, 2);
96  g.add_edge(1, 3);
97  g.add_edge(3, 4);
98  g.add_edge(4, 2);
99 
100  //g.print_debug(std::cout);
101 
102  /*-------------.
103  | Graph image. |
104  `-------------*/
105 
106  // Graph values.
107  typedef fun::i2v::array<unsigned> viota_t;
108  viota_t iota(g.v_nmax());
109  for (unsigned i = 0; i < iota.size(); ++i)
110  iota(i) = 10 + i;
111 
112 
113 
115 
116  {
117  typedef vertex_image<point2d,unsigned> ima_t;
118  ima_t ima = make::vertex_image(g, sites, iota);
119 
120  {
121  // FIXME: Move this part to a special test case.
122 
123  // Compute the bounding box of 'ima'.
125  mln_piter_(ima_t) p(ima.domain());
126  for_all(p)
127  a.take(p);
128  box2d bbox = a.to_result();
129  mln_assertion(bbox == make::box2d(5, 5));
130 
131  // Print the image.
132  /* FIXME: Unfortunately, displaying graph images is not easy right
133  now(2008-02-05). We could use
134 
135  debug::println(ima);
136 
137  but there's not specialization working for graph_image; the one
138  selected by the compiler is based on a 2-D bbox, and expects the
139  interface of graph_image to work with points(not psites).
140  Moreover, this implementation only shows *values*, not the graph
141  itslef.
142 
143  An alternative is to use debug::graph,
144  but it doesn't show the values, only the vertices and edges of the
145  graph.
146 
147  The current solution is a mix between debug::graph and hand-made
148  iterations. */
149  image2d<int> ima_rep(bbox);
150 
151  // We use the value 9 in debug::graph to represent edges to distinguish it
152  // from vertices holding a value of 1.
153  debug::draw_graph(ima_rep, ima.domain(), 1, 9);
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 ima_t::win_t 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 ima_t::nbh_t nbh_t;
198  nbh_t neigh;
199  {
200  // Neighborhood - Forward iteration
201  mln_niter_(nbh_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_(nbh_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  }
228 
229 
230 
233 
234  {
235  typedef vertex_image<void,unsigned> ima_void_t;
236  ima_void_t ima_void(g, iota);
237 
238  // iteration over the domain of IMA.
239  mln_piter_(ima_void_t) p(ima_void.domain());
240  unsigned i = 10;
241  for_all(p)
242  mln_assertion(ima_void(p) == i++);
243 
244  typedef ima_void_t::win_t win_t;
245  win_t win;
246 
247  {
248  // Window - Forward iteration
249  mln_qiter_(win_t) q(win, p);
250  for_all(p)
251  {
252  i = 0;
253  for_all(q)
254  {
255  mln_assertion(expected_fwd_nb[p.id()][i] == q.id());
256  ++i;
257  }
258  }
259  }
260 
261  {
262  // Window - Backward iteration
263  mln_bkd_qiter_(win_t) q(win, p);
264  for_all(p)
265  {
266  i = 0;
267  for_all(q)
268  {
269  mln_assertion(expected_bkd_nb[p.id()][i] == q.id());
270  ++i;
271  }
272  }
273  }
274 
275  typedef ima_void_t::nbh_t nbh_t;
276  nbh_t neigh;
277  {
278  // Neighborhood - Forward iteration
279  mln_niter_(nbh_t) n(neigh, p);
280 
281  for_all(p)
282  {
283  i = 0;
284  for_all(n)
285  {
286  //FIXME: Ambiguities with n.id()!!!!
287  mln_assertion(expected_fwd_nb[p.id()][i] == n.element().id());
288  ++i;
289  }
290  }
291  }
292 
293  {
294  // Neighborhood - Backward iteration
295  mln_bkd_niter_(nbh_t) n(neigh, p);
296  for_all(p)
297  {
298  i = 0;
299  for_all(n)
300  {
301  //FIXME: Ambiguities with n.id()!!!!
302  mln_assertion(expected_bkd_nb[p.id()][i] == n.element().id());
303  ++i;
304  }
305  }
306  }
307  }
308 }