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