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/accu/shape/bbox.hh> 00031 #include <mln/core/alias/box2d.hh> 00032 #include <mln/core/alias/point2d.hh> 00033 00034 #include <mln/morpho/dilation.hh> 00035 #include <mln/morpho/erosion.hh> 00036 00038 #include <mln/core/image/vertex_image.hh> 00039 #include <mln/fun/i2v/array.hh> 00040 #include <mln/util/graph.hh> 00041 #include <mln/make/vertex_image.hh> 00042 00043 #include <mln/debug/draw_graph.hh> 00044 #include <mln/debug/iota.hh> 00045 #include <mln/debug/println.hh> 00046 00047 00048 int main() 00049 { 00050 using namespace mln; 00051 00052 /*--------. 00053 | Graph. | 00054 `--------*/ 00055 00056 /* The graph is as follows: 00057 00058 0 1 2 3 4 00059 .----------- 00060 | 00061 0 | 0 2 00062 1 | \ / | 00063 2 | 1 | 00064 3 | \ | 00065 4 | 3-4 00066 00067 */ 00068 00069 // Points associated to vertices. 00070 typedef fun::i2v::array<point2d> fsite_t; 00071 fsite_t sites(5); 00072 sites(0) = point2d(0,0); // Point associated to vertex 0. 00073 sites(1) = point2d(2,2); // Point associated to vertex 1. 00074 sites(2) = point2d(0,4); // Point associated to vertex 2. 00075 sites(3) = point2d(4,3); // Point associated to vertex 3. 00076 sites(4) = point2d(4,4); // Point associated to vertex 4. 00077 00078 // Graph. 00079 util::graph g; 00080 00081 // Populate the graph with vertices. 00082 g.add_vertices(sites.size()); 00083 00084 // Populate the graph with edges. 00085 g.add_edge(0, 1); 00086 g.add_edge(1, 2); 00087 g.add_edge(1, 3); 00088 g.add_edge(3, 4); 00089 g.add_edge(4, 2); 00090 00091 00092 /*--------------. 00093 | Graph image. | 00094 `--------------*/ 00095 00096 // Graph values. 00097 typedef fun::i2v::array<unsigned> viota_t; 00098 viota_t iota(g.v_nmax()); 00099 for (unsigned i = 0; i < iota.size(); ++i) 00100 iota(i) = 10 + i; 00101 00102 typedef vertex_image<point2d, unsigned, util::graph> ima_t; 00103 ima_t ima = make::vertex_image(g, sites, iota); 00104 00105 /*-------------------------------------. 00106 | Image representation/visualization. | 00107 `-------------------------------------*/ 00108 00109 // Compute the bounding box of IMA. 00110 /* FIXME: mln::graph_image should automatically feature a bbox when 00111 its parameter P is akin to a point. */ 00112 accu::shape::bbox<point2d> a; 00113 for (unsigned i = 0; i < sites.size(); ++i) 00114 a.take(sites(i)); 00115 box2d bbox = a.to_result(); 00116 // Print the image. 00117 /* FIXME: Unfortunately, displaying graph images is not easy right 00118 now (2008-02-05). We could use 00119 00120 debug::println(ima); 00121 00122 but there's not specialization working for graph_image; the one 00123 selected by the compiler is based on a 2-D bbox, and expects the 00124 interface of graph_image to work with points (not psites). 00125 Moreover, this implementation only shows *values*, not the graph 00126 itslef. 00127 00128 An alternative is to use draw::graph (which, again, is misnamed), 00129 but it doesn't show the values, only the vertices and edges of the 00130 graph. 00131 00132 The current solution is a mix between draw::graph and hand-made 00133 iterations. */ 00134 image2d<int> ima_rep(bbox); 00135 00136 /*--------------------------. 00137 | Processing graph images. | 00138 `--------------------------*/ 00139 00140 00141 ima_t::win_t win; 00142 00143 ima_t ima_dil = morpho::dilation(ima, win); 00144 debug::draw_graph(ima_rep, ima_dil.domain(), pw::cst(9), pw::cst(2)); 00145 debug::println(ima_rep); 00146 00147 ima_t ima_ero = morpho::erosion(ima, win); 00148 debug::draw_graph(ima_rep, ima_ero.domain(), pw::cst(9), pw::cst(2)); 00149 debug::println(ima_rep); 00150 }