$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vertex_and_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/graph_elt_mixed_window.hh>
29 #include <mln/core/image/vertex_image.hh>
30 #include <mln/core/image/edge_image.hh>
31 #include <mln/fun/i2v/array.hh>
32 #include <mln/util/graph.hh>
33 
34 
35 
36 /* The graph is as follows:
37 
38  0 1 2 3 4
39  .-----------
40  |
41  0 | 0 2
42  1 | \ / |
43  2 | 1 |
44  3 | \ |
45  4 | 3-4
46 
47 */
48 
49 static const unsigned X = mln_max(unsigned); // Invalid id.
50 
51 
52 static unsigned expected_fwd_nb[5][3] = { { 0, X, X },
53  { 0, 1, 2 },
54  { 1, 4, X },
55  { 2, 3, X },
56  { 3, 4, X } };
57 
58 
59 static unsigned expected_bkd_nb[5][3] = { { 0, X, X },
60  { 2, 1, 0 },
61  { 4, 1, X },
62  { 3, 2, X },
63  { 4, 3, X } };
64 
65 
66 int main()
67 {
68  using namespace mln;
69 
70  /*--------.
71  | Graph. |
72  `--------*/
73 
74  // Points associated to vertices.
75  typedef fun::i2v::array<point2d> fsite_t;
76  fsite_t sites(5);
77  sites(0) = point2d(0,0); // Point associated to vertex 0.
78  sites(1) = point2d(2,2); // Point associated to vertex 1.
79  sites(2) = point2d(0,4); // Point associated to vertex 2.
80  sites(3) = point2d(4,3); // Point associated to vertex 3.
81  sites(4) = point2d(4,4); // Point associated to vertex 4.
82 
83  // Edges.
84  util::graph g;
85 
86  // Populate the graph with vertices.
87  g.add_vertices(sites.size());
88 
89  // Populate the graph with edges.
90  g.add_edge(0, 1);
91  g.add_edge(1, 2);
92  g.add_edge(1, 3);
93  g.add_edge(3, 4);
94  g.add_edge(4, 2);
95 
96  //g.print_debug(std::cout);
97 
98  /*-------------.
99  | Graph image. |
100  `-------------*/
101 
102  // Vertex values.
103  typedef fun::i2v::array<unsigned> viota_t;
104  viota_t iota(g.v_nmax());
105  for (unsigned i = 0; i < iota.size(); ++i)
106  iota(i) = 10 + i;
107 
108 
109  // Edge values.
110  typedef fun::i2v::array<unsigned> eiota_t;
111  eiota_t eiota(g.e_nmax());
112  for (unsigned i = 0; i < eiota.size(); ++i)
113  eiota(i) = 20 + i;
114 
115 
116  typedef vertex_image<void,unsigned> v_ima_t;
117  v_ima_t v_ima(g, iota);
118 
119  typedef edge_image<void,unsigned> e_ima_t;
120  e_ima_t e_ima(g, eiota);
121 
122 
123  // Iterator over the vertex image.
124  mln_piter_(v_ima_t) v(v_ima.domain());
125 
126 
127  typedef graph_elt_mixed_window<util::graph,
128  v_ima_t::domain_t,
129  e_ima_t::domain_t> edge_win_t;
130  edge_win_t win;
131 
132  // Forward Iteration
133  {
134  for_all(v)
135  {
136  int i = 0;
137  // Iterator over the neighbor edges in the edge image.
138  mln_qiter_(edge_win_t) e(win, e_ima.domain(), v);
139  for_all(e)
140  {
141  mln_assertion(expected_fwd_nb[v.id()][i++] == e.id());
142  mln_assertion((e.id() + 20) == e_ima(e));
143  }
144  }
145  }
146 
147  // Backward Iteration
148  {
149  for_all(v)
150  {
151  int i = 0;
152  // Iterator over the neighbor edges in the edge image.
153  mln_bkd_qiter_(edge_win_t) e(win, e_ima.domain(), v);
154  for_all(e)
155  {
156  mln_assertion(expected_bkd_nb[v.id()][i++] == e.id());
157  mln_assertion((e.id() + 20) == e_ima(e));
158  }
159  }
160  }
161 
162 
163 
164 // FIXME: add tests for graph_window_if and graph_neighborhood_if when
165 // they support this feature.
166 
167 }