$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_image.cc
1 // Copyright (C) 2009, 2014 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/accu/shape/bbox.hh>
34 #include <mln/fun/i2v/array.hh>
35 #include <mln/util/graph.hh>
36 #include <mln/debug/draw_graph.hh>
37 #include <mln/debug/println.hh>
38 
39 
40 /* The graph is as follows:
41 
42  0 1 2 3 4
43  .-----------
44  |
45  0 | 0 2
46  1 | \ / |
47  2 | 1 |
48  3 | \ |
49  4 | 3-4
50 
51 */
52 
53 
54 static const unsigned X = mln_max(unsigned); // Invalid id.
55 
56 
57 // Expected neighbors for forward and backward iteration.
58 // X is an invalid id.
59 static unsigned expected_fwd_nb[5][3] = { { 1, X, X },
60  { 0, 2, 3 },
61  { 1, 4, X },
62  { 1, 4, X },
63  { 3, 2, X } };
64 
65 static unsigned expected_bkd_nb[5][3] = { { 1, X, X },
66  { 3, 2, 0 },
67  { 4, 1, X },
68  { 4, 1, X },
69  { 2, 3, X } };
70 
71 
72 int main()
73 {
74  using namespace mln;
75 
76  /*--------.
77  | Graph. |
78  `--------*/
79 
80  // Points associated to vertices.
81  typedef fun::i2v::array<point2d> fsite_t;
82  fsite_t sites(5);
83  sites(0) = point2d(0,0); // Point associated to vertex 0.
84  sites(1) = point2d(2,2); // Point associated to vertex 1.
85  sites(2) = point2d(0,4); // Point associated to vertex 2.
86  sites(3) = point2d(4,3); // Point associated to vertex 3.
87  sites(4) = point2d(4,4); // Point associated to vertex 4.
88 
89  // Edges.
90  util::graph g;
91 
92  // Populate the graph with vertices.
93  g.add_vertices(sites.size());
94 
95  // Populate the graph with edges.
96  g.add_edge(0, 1);
97  g.add_edge(1, 2);
98  g.add_edge(1, 3);
99  g.add_edge(3, 4);
100  g.add_edge(4, 2);
101 
102  //g.print_debug(std::cout);
103 
104  /*----------------------.
105  | Graph image support. |
106  `----------------------*/
107 
109  S pv(g, sites);
110 
111  /*-------------.
112  | Graph image. |
113  `-------------*/
114 
115  // Graph values.
116  typedef fun::i2v::array<unsigned> viota_t;
117  viota_t iota(pv.nsites());
118  for (unsigned i = 0; i < iota.size(); ++i)
119  iota(i) = 10 + i;
120 
121  // Create graph image.
124  fun::i2v::array<point2d> > > ima_t;
125  ima_t ima = iota | pv;
126 
127  {
128  // FIXME: Move this part to a special test case.
129 
130  // Compute the bounding box of 'ima'.
132  mln_piter_(ima_t) p(ima.domain());
133  for_all(p)
134  a.take(p);
135  box2d bbox = a.to_result();
136  mln_assertion(bbox == make::box2d(5, 5));
137 
138  // Print the image.
139  /* FIXME: Unfortunately, displaying graph images is not easy right
140  now (2008-02-05). We could use
141 
142  debug::println(ima);
143 
144  but there's not specialization working for graph_image; the one
145  selected by the compiler is based on a 2-D bbox, and expects
146  the interface of graph_image to work with points (not psites).
147 
148  An alternative is to use debug::draw_graph, but it doesn't show
149  the values, only the vertices and edges of the graph.
150 
151  The current solution is a mix between debug::draw_graph and
152  hand-made iterations. */
153  image2d<int> ima_rep(bbox);
154  debug::draw_graph(ima_rep, pv, 1, 9);
155  debug::println(ima_rep);
156  }
157 
158  /*------------.
159  | Iterators. |
160  `------------*/
161 
162  // iteration over the domain of IMA.
163  mln_piter_(ima_t) p(ima.domain());
164  unsigned i = 10;
165  for_all(p)
166  mln_assertion(ima(p) == i++);
167 
168  typedef graph_elt_window<util::graph,S> win_t;
169  win_t win;
170 
171  {
172  // Window - Forward iteration
173  mln_qiter_(win_t) q(win, p);
174  for_all(p)
175  {
176  i = 0;
177  for_all(q)
178  {
179  mln_assertion(expected_fwd_nb[p.id()][i] == q.id());
180  ++i;
181  }
182  }
183  }
184 
185  {
186  // Window - Backward iteration
187  mln_bkd_qiter_(win_t) q(win, p);
188  for_all(p)
189  {
190  i = 0;
191  for_all(q)
192  {
193  mln_assertion(expected_bkd_nb[p.id()][i] == q.id());
194  ++i;
195  }
196  }
197  }
198 
199  typedef graph_elt_neighborhood<util::graph,S> neighb_t;
200  neighb_t neigh;
201  {
202  // Neighborhood - Forward iteration
203  mln_niter_(neighb_t) n(neigh, p);
204 
205  for_all(p)
206  {
207  i = 0;
208  for_all(n)
209  {
210  mln_assertion(expected_fwd_nb[p.id()][i] == n.id());
211  ++i;
212  }
213  }
214  }
215 
216  {
217  // Neighborhood - Backward iteration
218  mln_bkd_niter_(neighb_t) n(neigh, p);
219  for_all(p)
220  {
221  i = 0;
222  for_all(n)
223  {
224  mln_assertion(expected_bkd_nb[p.id()][i] == n.id());
225  ++i;
226  }
227  }
228  }
229 }