Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
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 
28 
29 #include <vector>
30 
31 #include <mln/core/alias/point2d.hh>
32 
34 #include <mln/core/image/vertex_image.hh>
35 #include <mln/core/var.hh>
36 #include <mln/fun/i2v/array.hh>
37 #include <mln/util/graph.hh>
38 #include <mln/make/vertex_image.hh>
39 
40 #include <mln/morpho/meyer_wst.hh>
41 
42 
43 int main()
44 {
45  using namespace mln;
46 
47  /*--------.
48  | Graph. |
49  `--------*/
50 
51  /* The graph is as follows:
52 
53  0 1 2 3 4
54  .-----------
55  |
56  0 | 0 2
57  1 | \ / |
58  2 | 1 |
59  3 | \ |
60  4 | 3-0
61 
62  */
63 
64  // Points associated to vertices.
65  typedef fun::i2v::array<point2d> fsite_t;
66  fsite_t sites(5);
67  sites(0) = point2d(0,0); // Point associated to vertex 0.
68  sites(1) = point2d(2,2); // Point associated to vertex 1.
69  sites(2) = point2d(0,4); // Point associated to vertex 2.
70  sites(3) = point2d(4,3); // Point associated to vertex 3.
71  sites(4) = point2d(4,4); // Point associated to vertex 4.
72 
73  // Graph.
74  util::graph g;
75 
76  // Populate the graph with vertices.
77  g.add_vertices(sites.size());
78 
79  // Populate the graph with edges.
80  g.add_edge(0, 1);
81  g.add_edge(1, 2);
82  g.add_edge(1, 3);
83  g.add_edge(3, 4);
84  g.add_edge(4, 2);
85 
86  /*-------------.
87  | Graph image. |
88  `-------------*/
89 
90  // Graph values.
91  typedef fun::i2v::array<unsigned> viota_t;
92  viota_t iota(g.v_nmax());
93  for (unsigned i = 0; i < iota.size(); ++i)
94  iota(i) = 10 + i;
95 
97  ima_t ima = make::vertex_image(g, sites, iota);
98 
99  /*------.
100  | WST. |
101  `------*/
102 
103  typedef ima_t::nbh_t nbh_t;
104  nbh_t nbh;
105 
106  unsigned nbasins;
107  ima_t wshed = morpho::meyer_wst(ima, nbh, nbasins);
108  std::cout << "nbasins = " << nbasins << std::endl;
109 
110  // Manual iteration over the domain of WSHED.
111  mln_piter_(ima_t) pw(wshed.domain());
112  for_all (pw)
113  std::cout << "wshed (" << pw << ") = " << wshed(pw) << std::endl;
114 }