$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
line_graph_image.cc
1 // Copyright (C) 2009, 2011 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 
36 
37 #include <mln/core/alias/point2d.hh>
38 
39 #include <mln/util/graph.hh>
40 #include <mln/util/line_graph.hh>
41 #include <mln/fun/i2v/array.hh>
42 #include <mln/util/site_pair.hh>
43 #include <mln/core/image/vertex_image.hh>
44 #include <mln/make/vertex_image.hh>
45 
46 
47 int main()
48 {
49  using namespace mln;
50 
51  /*--------.
52  | Graph. |
53  `--------*/
54 
55  /* The graph and its corresponding line graph are as follows:
56 
57  0 1 2 3 4 0 1 2 3 4
58  .----------- .-----------
59  | |
60  0 | 0 2 0 | * *
61  1 | \ / | 1 | 0 1 |
62  2 | 1 | 2 | * 4
63  3 | \ | 3 | 2 |
64  4 | 3-4 4 | *3*
65 
66  Numbers in the graph represent the vertices and edges identifers
67  (resp.). */
68 
69  // Graph.
70  util::graph g;
71 
72  // Populate the graph with 5 vertices.
73  g.add_vertices(5);
74 
75  // Populate the graph with edges.
76  g.add_edge(0, 1);
77  g.add_edge(1, 2);
78  g.add_edge(1, 3);
79  g.add_edge(3, 4);
80  g.add_edge(4, 2);
81 
82  // Sites (points) associated to edges.
83  /* FIXME: It would be nicer to deduce these pairs from points
84  attached to a vertex graph (image) rather than building them
85  manually. */
86  typedef util::site_pair<point2d> site_t;
87  typedef fun::i2v::array<site_t> fsite_t;
88  fsite_t sites(5);
89  sites(0) = site_t(point2d(0,0), point2d(2,2)); // Site associated to edge 0.
90  sites(1) = site_t(point2d(2,2), point2d(0,4)); // Site associated to edge 1.
91  sites(2) = site_t(point2d(2,2), point2d(4,3)); // Site associated to edge 2.
92  sites(3) = site_t(point2d(4,3), point2d(4,4)); // Site associated to edge 3.
93  sites(4) = site_t(point2d(4,4), point2d(0,4)); // Site associated to edge 4.
94 
95  // Line graph based on the graph G.
96  typedef util::line_graph<util::graph> line_graph_t;
97  line_graph_t lg(g);
98 
99  /*-------------------.
100  | Line graph image. |
101  `-------------------*/
102 
103  // Graph values.
104  typedef fun::i2v::array<unsigned> viota_t;
105  viota_t iota(g.v_nmax());
106  for (unsigned i = 0; i < iota.size(); ++i)
107  iota(i) = 10 + i;
108 
110  ima_t ima = make::vertex_image(lg, sites, iota);
111 
112  /*------------.
113  | Iterators. |
114  `------------*/
115 
116  // Invalid identifier.
117  const unsigned X = mln_max(unsigned);
118 
119  // Expected neighbors for forward and backward iterations on an
120  // elementary window.
121  const unsigned expected_fwd_nbh[5][3] = { { 1, 2, X },
122  { 0, 2, 4 },
123  { 0, 1, 3 },
124  { 2, 4, X },
125  { 1, 3, X } };
126 
127  const unsigned expected_bkd_nbh[5][3] = { { 2, 1, X },
128  { 4, 2, 0 },
129  { 3, 1, 0 },
130  { 4, 2, X },
131  { 3, 1, X } };
132 
133  // Iteration over the domain of IMA.
134  mln_piter_(ima_t) p(ima.domain());
135  unsigned i = 10;
136  for_all(p)
137  mln_assertion(ima(p) == i++);
138 
139  // Elementary window.
140  typedef ima_t::win_t win_t;
141  win_t win;
142 
143  {
144  // Window -- Forward iteration.
145  mln_fwd_qiter_(win_t) q(win, p);
146  for_all(p)
147  {
148  i = 0;
149  for_all(q)
150  {
151  mln_assertion(expected_fwd_nbh[p.id()][i] == q.id());
152  ++i;
153  }
154  }
155  }
156 
157  {
158  // Window -- Backward iteration.
159  mln_bkd_qiter_(win_t) q(win, p);
160  for_all(p)
161  {
162  i = 0;
163  for_all(q)
164  {
165  mln_assertion(expected_bkd_nbh[p.id()][i] == q.id());
166  ++i;
167  }
168  }
169  }
170 
171 
172  // Elementary neighborhood.
173  typedef ima_t::nbh_t nbh_t;
174  nbh_t nbh;
175 
176  {
177  // Neighborhood -- Forward iteration.
178  mln_fwd_niter_(nbh_t) n(nbh, p);
179  for_all(p)
180  {
181  i = 0;
182  for_all(n)
183  {
184  mln_assertion(expected_fwd_nbh[p.id()][i] == n.id());
185  ++i;
186  }
187  }
188  }
189 
190  {
191  // Neighborhood -- Backward iteration.
192  mln_bkd_niter_(nbh_t) n(nbh, p);
193  for_all(p)
194  {
195  i = 0;
196  for_all(n)
197  {
198  mln_assertion(expected_bkd_nbh[p.id()][i] == n.id());
199  ++i;
200  }
201  }
202  }
203 }