Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
line_graph_image_morpho.cc
1 // Copyright (C) 2007, 2008, 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 
29 
30 #include <mln/core/alias/point2d.hh>
31 
33 #include <mln/core/image/vertex_image.hh>
34 #include <mln/fun/i2v/array.hh>
35 #include <mln/util/line_graph.hh>
36 #include <mln/util/graph.hh>
37 #include <mln/util/site_pair.hh>
38 #include <mln/make/vertex_image.hh>
39 
40 #include <mln/morpho/erosion.hh>
41 #include <mln/morpho/dilation.hh>
42 
43 static const unsigned dil_ref[] = { 12, 14, 13, 14, 13 };
44 static const unsigned ero_ref[] = { 11, 10, 10, 12, 11 };
45 
46 int main()
47 {
48  using namespace mln;
49 
50  /*--------.
51  | Graph. |
52  `--------*/
53 
54  /* The graph and its corresponding line graph are as follows:
55 
56  0 1 2 3 4 0 1 2 3 4
57  .----------- .-----------
58  | |
59  0 | 0 2 0 | * *
60  1 | \ / | 1 | 0 1 |
61  2 | 1 | 2 | * 4
62  3 | \ | 3 | 2 |
63  4 | 3-4 4 | *3*
64 
65  */
66 
67  // Graph.
68  util::graph g;
69 
70  // Populate the graph with vertices.
71  g.add_vertices(5);
72 
73  // Populate the graph with edges.
74  g.add_edge(0, 1);
75  g.add_edge(1, 2);
76  g.add_edge(1, 3);
77  g.add_edge(3, 4);
78  g.add_edge(4, 2);
79 
80  // Create the corresponding line graph.
81  // Edges are now considered as vertices.
83 
84  // Points associated to the line graph vertices (edges in the graph).
85  typedef util::site_pair<point2d> P;
86  typedef fun::i2v::array<P> fsite_t;
87  fsite_t sites(5);
88  sites(0) = P(point2d(0,0), point2d(2,2)); // Site associated to vertex 0.
89  sites(1) = P(point2d(2,2), point2d(0,4)); // Site associated to vertex 1.
90  sites(2) = P(point2d(2,2), point2d(4,3)); // Site associated to vertex 2.
91  sites(3) = P(point2d(4,3), point2d(4,4)); // Site associated to vertex 3.
92  sites(4) = P(point2d(0,4), point2d(4,4)); // Site associated to vertex 4.
93 
94  /*-------------------.
95  | Line graph image. |
96  `-------------------*/
97 
98  // Line graph values.
99  typedef fun::i2v::array<unsigned> viota_t;
100  viota_t iota(lg.v_nmax());
101  for (unsigned i = 0; i < iota.size(); ++i)
102  iota(i) = 10 + i;
103 
105  ima_t ima = make::vertex_image(lg, sites, iota);
106 
107 
108  /*-------------------------------.
109  | Processing line graph images. |
110  `-------------------------------*/
111 
112  ima_t::win_t win;
113  unsigned i = 0;
114 
115  ima_t ima_dil = morpho::dilation(ima, win);
116  // Manual iteration over the domain of IMA_DIL.
117  mln_piter_(ima_t) p_dil(ima_dil.domain());
118  for_all (p_dil)
119  mln_assertion(dil_ref[i++] == ima_dil(p_dil));
120 
121  ima_t ima_ero = morpho::erosion(ima, win);
122  // Manual iteration over the domain of IMA_ERO.
123  mln_piter_(ima_t) p_ero(ima_ero.domain());
124  i = 0;
125  for_all (p_ero)
126  mln_assertion(ero_ref[i++] == ima_ero(p_ero));
127 }