Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
line_graph_image_wst.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/pw/all.hh>
35 #include <mln/fun/i2v/array.hh>
36 #include <mln/util/graph.hh>
37 #include <mln/util/line_graph.hh>
38 #include <mln/util/site_pair.hh>
39 #include <mln/make/vertex_image.hh>
40 
41 #include <mln/morpho/watershed/flooding.hh>
42 
43 static const unsigned ima_ref[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
44 static const unsigned ima_wst[] = { 2, 0, 1, 2, 2, 1, 1, 2, 0, 1 };
45 
46 int main()
47 {
48  using namespace mln;
49 
50  /*-------------.
51  | Line graph. |
52  `-------------*/
53 
54  /* Actually this graph is from Jean Cousty's PhD thesis, page 76.
55 
56  0 1 2 3 (rows)
57  ,------------------------
58  | 0 10 5
59  0 | o-----o-----o-----o
60  | | | | |
61  | 2| 4| 6| 0|
62  | | | | |
63  1 | o-----o-----o-----o
64  3 5 2
65  (cols)
66 
67  In G, vertices and egdes are numbered following in the classical
68  foward order. */
69  util::graph g;
70 
71  // Populate the graph with vertices.
72  g.add_vertices(8);
73 
74  // Populate the graph with edges.
75  g.add_edge(0, 1);
76  g.add_edge(1, 2);
77  g.add_edge(2, 3);
78 
79  g.add_edge(0, 4);
80  g.add_edge(1, 5);
81  g.add_edge(2, 6);
82  g.add_edge(3, 7);
83 
84  g.add_edge(4, 5);
85  g.add_edge(5, 6);
86  g.add_edge(6, 7);
87 
89 
90  // Sites associated to vertices.
91  typedef util::site_pair<point2d> P;
92  typedef fun::i2v::array<P> fsite_t;
93  fsite_t sites(10);
94  sites(0) = P(point2d(0,0), point2d(0,1)); // Site associated to vertex 0.
95  sites(1) = P(point2d(0,1), point2d(0,2)); // Site associated to vertex 1.
96  sites(2) = P(point2d(0,2), point2d(0,3)); // Site associated to vertex 2.
97  sites(3) = P(point2d(0,0), point2d(1,0)); // Site associated to vertex 7.
98  sites(4) = P(point2d(0,1), point2d(1,1)); // Site associated to vertex 8.
99  sites(5) = P(point2d(0,2), point2d(1,2)); // Site associated to vertex 9.
100  sites(6) = P(point2d(0,3), point2d(1,3)); // Site associated to vertex 3.
101  sites(7) = P(point2d(1,0), point2d(1,1)); // Site associated to vertex 4.
102  sites(8) = P(point2d(1,1), point2d(1,2)); // Site associated to vertex 5.
103  sites(9) = P(point2d(1,2), point2d(1,3)); // Site associated to vertex 6.
104 
105  // Edge values.
106  typedef fun::i2v::array<unsigned> edge_values_t;
107  edge_values_t edge_values(10);
108 
109  static const unsigned values[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
110  for (unsigned i = 0; i < edge_values.size(); ++i)
111  edge_values(i) = values[i];
112 
114  ima_t ima = make::vertex_image(lg, sites, edge_values);
115 
116 
117  /*------------.
118  | Iterators. |
119  `------------*/
120 
121  unsigned i = 0;
122 
123  // Manual iteration over the domain of IMA.
124  mln_piter_(ima_t) p(ima.domain());
125  for_all (p)
126  mln_assertion(ima_ref[i++] == ima(p));
127 
128  typedef ima_t::nbh_t nbh_t;
129  nbh_t nbh;
130  unsigned nbasins;
131  typedef mln_ch_value_(ima_t,unsigned) wshed_t;
132  wshed_t wshed = morpho::watershed::flooding(ima, nbh, nbasins);
133  mln_assertion(nbasins == 2);
134 
135  i = 0;
136  // Manual iteration over the domain of WSHED.
137  mln_piter_(wshed_t) pw(wshed.domain());
138  for_all (pw)
139  mln_assertion(ima_wst[i++] == wshed(pw));
140 }