Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
line_graph_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 <mln/core/alias/point2d.hh>
27 
29 #include <mln/core/site_set/p_edges.hh>
30 #include <mln/core/image/graph_elt_window.hh>
31 #include <mln/core/image/graph_elt_neighborhood.hh>
32 #include <mln/core/image/vertex_image.hh>
33 #include <mln/core/neighb.hh>
34 #include <mln/core/var.hh>
35 #include <mln/pw/all.hh>
36 #include <mln/fun/i2v/array.hh>
37 #include <mln/util/graph.hh>
38 #include <mln/make/vertex_image.hh>
39 
40 
41 /* The graph and its corresponding line graph are as follows:
42 
43  0 1 2 3 4 0 1 2 3 4
44  .----------- .-----------
45  | |
46  0 | 0 2 0 | * *
47  1 | \ / | 1 | 0 1 |
48  2 | 1 | 2 | * 4
49  3 | \ | 3 | 2 |
50  4 | 3-4 4 | *3*
51 
52 */
53 
54 
55 static const unsigned X = mln_max(unsigned); // Invalid id.
56 
57 
58 // Expected neighbors for forward and backward iteration.
59 static unsigned expected_fwd_nbh[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_nbh[5][3] = { { 1, 0, 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  // Sites associated to edges.
81  typedef fun::i2v::array<point2d> fsite_t;
82  fsite_t sites(5);
83  sites(0) = point2d(0,0); // Point associated to edge 0.
84  sites(1) = point2d(2,2); // Point associated to edge 1.
85  sites(2) = point2d(0,4); // Point associated to edge 2.
86  sites(3) = point2d(4,3); // Point associated to edge 3.
87  sites(4) = point2d(4,4); // Point associated to edge 4.
88 
89  util::graph g;
90 
91  // Populate the graph with vertices.
92  g.add_vertices(sites.size());
93 
94  // Populate the graph with edges.
95  g.add_edge(0, 1);
96  g.add_edge(1, 2);
97  g.add_edge(1, 3);
98  g.add_edge(3, 4);
99  g.add_edge(4, 2);
100 
101  /*-------------------.
102  | Line 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  // Create line graph image.
112  typedef vertex_image<point2d,unsigned> ima_t;
113  ima_t ima = make::vertex_image(g, sites, iota);
114 
115  /*------------.
116  | Iterators. |
117  `------------*/
118 
119  mln_edge_iter_(util::graph) ei(g);
120  mln_edge_nbh_edge_iter_(util::graph) en(ei);
121 
122  // Manual iteration over the domain of IMA.
123  mln_piter_(ima_t) p(ima.domain());
124  unsigned i = 10;
125  for_all(p)
126  mln_assertion(ima(p) == i++);
127 
128  typedef ima_t::win_t win_t;
129  win_t win;
130 
131  {
132  // Window - Forward iteration
133  mln_fwd_qiter_(win_t) q(win, p);
134  for_all(p)
135  {
136  i = 0;
137  for_all(q)
138  {
139  mln_assertion(expected_fwd_nbh[p.id()][i] == q.id());
140  ++i;
141  }
142  }
143  }
144 
145  {
146  // Window - Backward iteration
147  mln_bkd_qiter_(win_t) q(win, p);
148  for_all(p)
149  {
150  i = 0;
151  for_all(q)
152  {
153  mln_assertion(expected_bkd_nbh[p.id()][i] == q.id());
154  ++i;
155  }
156  }
157  }
158 
159 
160  typedef ima_t::nbh_t neighb_t;
161  neighb_t neigh;
162 
163  {
164  // Neighborhood - Forward iteration
165  mln_fwd_niter_(neighb_t) n(neigh, p);
166  for_all(p)
167  {
168  i = 0;
169  for_all(n)
170  {
171  mln_assertion(expected_fwd_nbh[p.id()][i] == n.id());
172  ++i;
173  }
174  }
175  }
176 
177  {
178  // Neighborhood - Backward iteration
179  mln_bkd_niter_(neighb_t) n(neigh, p);
180  for_all(p)
181  {
182  i = 0;
183  for_all(n)
184  {
185  mln_assertion(expected_bkd_nbh[p.id()][i] == n.id());
186  ++i;
187  }
188  }
189  }
190 
191 }