• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

line_graph_image_wst.cc

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00029 
00030 #include <mln/core/alias/point2d.hh>
00031 
00033 #include <mln/core/image/vertex_image.hh>
00034 #include <mln/pw/all.hh>
00035 #include <mln/fun/i2v/array.hh>
00036 #include <mln/util/graph.hh>
00037 #include <mln/util/line_graph.hh>
00038 #include <mln/util/site_pair.hh>
00039 #include <mln/make/vertex_image.hh>
00040 
00041 #include <mln/morpho/watershed/flooding.hh>
00042 
00043 static const unsigned ima_ref[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
00044 static const unsigned ima_wst[] = { 2, 0, 1, 2, 2, 1, 1, 2, 0, 1 };
00045 
00046 int main()
00047 {
00048   using namespace mln;
00049 
00050   /*-------------.
00051   | Line graph.  |
00052   `-------------*/
00053 
00054   /* Actually this graph is from Jean Cousty's PhD thesis, page 76.
00055 
00056                0     1     2     3  (rows)
00057          ,------------------------
00058          |        0     10    5
00059        0 |     o-----o-----o-----o
00060          |     |     |     |     |
00061          |    2|    4|    6|    0|
00062          |     |     |     |     |
00063        1 |     o-----o-----o-----o
00064                   3     5     2
00065     (cols)
00066 
00067     In G, vertices and egdes are numbered following in the classical
00068     foward order.  */
00069   util::graph g;
00070 
00071   // Populate the graph with vertices.
00072   g.add_vertices(8);
00073 
00074   // Populate the graph with edges.
00075   g.add_edge(0, 1);
00076   g.add_edge(1, 2);
00077   g.add_edge(2, 3);
00078 
00079   g.add_edge(0, 4);
00080   g.add_edge(1, 5);
00081   g.add_edge(2, 6);
00082   g.add_edge(3, 7);
00083 
00084   g.add_edge(4, 5);
00085   g.add_edge(5, 6);
00086   g.add_edge(6, 7);
00087 
00088   util::line_graph<util::graph> lg(g);
00089 
00090   // Sites associated to vertices.
00091   typedef util::site_pair<point2d> P;
00092   typedef fun::i2v::array<P> fsite_t;
00093   fsite_t sites(10);
00094   sites(0) = P(point2d(0,0), point2d(0,1)); // Site associated to vertex 0.
00095   sites(1) = P(point2d(0,1), point2d(0,2)); // Site associated to vertex 1.
00096   sites(2) = P(point2d(0,2), point2d(0,3)); // Site associated to vertex 2.
00097   sites(3) = P(point2d(0,0), point2d(1,0)); // Site associated to vertex 7.
00098   sites(4) = P(point2d(0,1), point2d(1,1)); // Site associated to vertex 8.
00099   sites(5) = P(point2d(0,2), point2d(1,2)); // Site associated to vertex 9.
00100   sites(6) = P(point2d(0,3), point2d(1,3)); // Site associated to vertex 3.
00101   sites(7) = P(point2d(1,0), point2d(1,1)); // Site associated to vertex 4.
00102   sites(8) = P(point2d(1,1), point2d(1,2)); // Site associated to vertex 5.
00103   sites(9) = P(point2d(1,2), point2d(1,3)); // Site associated to vertex 6.
00104 
00105   // Edge values.
00106   typedef fun::i2v::array<unsigned> edge_values_t;
00107   edge_values_t edge_values(10);
00108 
00109   static const unsigned values[] = { 0, 10, 5, 2, 4, 6, 0, 3, 5, 2 };
00110   for (unsigned i = 0; i < edge_values.size(); ++i)
00111     edge_values(i) = values[i];
00112 
00113   typedef vertex_image< P, unsigned, util::line_graph<util::graph> > ima_t;
00114   ima_t ima = make::vertex_image(lg, sites, edge_values);
00115 
00116 
00117   /*------------.
00118   | Iterators.  |
00119   `------------*/
00120 
00121   unsigned i = 0;
00122 
00123   // Manual iteration over the domain of IMA.
00124   mln_piter_(ima_t) p(ima.domain());
00125   for_all (p)
00126     mln_assertion(ima_ref[i++] == ima(p));
00127 
00128   typedef ima_t::nbh_t nbh_t;
00129   nbh_t nbh;
00130   unsigned nbasins;
00131   typedef mln_ch_value_(ima_t,unsigned) wshed_t;
00132   wshed_t wshed = morpho::watershed::flooding(ima, nbh, nbasins);
00133   mln_assertion(nbasins == 2);
00134 
00135   i = 0;
00136   // Manual iteration over the domain of WSHED.
00137   mln_piter_(wshed_t) pw(wshed.domain());
00138   for_all (pw)
00139     mln_assertion(ima_wst[i++] == wshed(pw));
00140 }

Generated on Tue Oct 4 2011 15:24:02 for Milena (Olena) by  doxygen 1.7.1