Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 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 00026 #include <vector> 00027 00028 #include <mln/core/site_set/p_vertices.hh> 00029 #include <mln/core/image/graph_elt_window.hh> 00030 #include <mln/core/image/graph_elt_neighborhood.hh> 00031 #include <mln/core/concept/function.hh> 00032 #include <mln/core/neighb.hh> 00033 #include <mln/core/var.hh> 00034 #include <mln/accu/shape/bbox.hh> 00035 #include <mln/fun/i2v/array.hh> 00036 #include <mln/util/graph.hh> 00037 #include <mln/debug/draw_graph.hh> 00038 #include <mln/debug/println.hh> 00039 00040 00041 /* The graph is as follows: 00042 00043 0 1 2 3 4 00044 .----------- 00045 | 00046 0 | 0 2 00047 1 | \ / | 00048 2 | 1 | 00049 3 | \ | 00050 4 | 3-4 00051 00052 */ 00053 00054 00055 static const unsigned X = mln_max(unsigned); // Invalid id. 00056 00057 00058 // Expected neighbors for forward and backward iteration. 00059 // X is an invalid id. 00060 static unsigned expected_fwd_nb[5][3] = { { 1, X, X }, 00061 { 0, 2, 3 }, 00062 { 1, 4, X }, 00063 { 1, 4, X }, 00064 { 3, 2, X } }; 00065 00066 static unsigned expected_bkd_nb[5][3] = { { 1, X, X }, 00067 { 3, 2, 0 }, 00068 { 4, 1, X }, 00069 { 4, 1, X }, 00070 { 2, 3, X } }; 00071 00072 00073 int main() 00074 { 00075 using namespace mln; 00076 00077 /*--------. 00078 | Graph. | 00079 `--------*/ 00080 00081 // Points associated to vertices. 00082 typedef fun::i2v::array<point2d> fsite_t; 00083 fsite_t sites(5); 00084 sites(0) = point2d(0,0); // Point associated to vertex 0. 00085 sites(1) = point2d(2,2); // Point associated to vertex 1. 00086 sites(2) = point2d(0,4); // Point associated to vertex 2. 00087 sites(3) = point2d(4,3); // Point associated to vertex 3. 00088 sites(4) = point2d(4,4); // Point associated to vertex 4. 00089 00090 // Edges. 00091 util::graph g; 00092 00093 // Populate the graph with vertices. 00094 g.add_vertices(sites.size()); 00095 00096 // Populate the graph with edges. 00097 g.add_edge(0, 1); 00098 g.add_edge(1, 2); 00099 g.add_edge(1, 3); 00100 g.add_edge(3, 4); 00101 g.add_edge(4, 2); 00102 00103 //g.print_debug(std::cout); 00104 00105 /*----------------------. 00106 | Graph image support. | 00107 `----------------------*/ 00108 00109 typedef p_vertices<util::graph, fsite_t> S; 00110 S pv(g, sites); 00111 00112 /*-------------. 00113 | Graph image. | 00114 `-------------*/ 00115 00116 // Graph values. 00117 typedef fun::i2v::array<unsigned> viota_t; 00118 viota_t iota(pv.nsites()); 00119 for (unsigned i = 0; i < iota.size(); ++i) 00120 iota(i) = 10 + i; 00121 00122 // Create graph image. 00123 mln_const_VAR(ima, (iota | pv)); 00124 00125 { 00126 // FIXME: Move this part to a special test case. 00127 00128 // Compute the bounding box of 'ima'. 00129 accu::shape::bbox<point2d> a; 00130 mln_piter_(ima_t) p(ima.domain()); 00131 for_all(p) 00132 a.take(p); 00133 box2d bbox = a.to_result(); 00134 mln_assertion(bbox == make::box2d(5, 5)); 00135 00136 // Print the image. 00137 /* FIXME: Unfortunately, displaying graph images is not easy right 00138 now (2008-02-05). We could use 00139 00140 debug::println(ima); 00141 00142 but there's not specialization working for graph_image; the one 00143 selected by the compiler is based on a 2-D bbox, and expects 00144 the interface of graph_image to work with points (not psites). 00145 00146 An alternative is to use debug::draw_graph, but it doesn't show 00147 the values, only the vertices and edges of the graph. 00148 00149 The current solution is a mix between debug::draw_graph and 00150 hand-made iterations. */ 00151 image2d<int> ima_rep(bbox); 00152 debug::draw_graph(ima_rep, pv, 1, 9); 00153 debug::println(ima_rep); 00154 } 00155 00156 /*------------. 00157 | Iterators. | 00158 `------------*/ 00159 00160 // iteration over the domain of IMA. 00161 mln_piter_(ima_t) p(ima.domain()); 00162 unsigned i = 10; 00163 for_all(p) 00164 mln_assertion(ima(p) == i++); 00165 00166 typedef graph_elt_window<util::graph,S> win_t; 00167 win_t win; 00168 00169 { 00170 // Window - Forward iteration 00171 mln_qiter_(win_t) q(win, p); 00172 for_all(p) 00173 { 00174 i = 0; 00175 for_all(q) 00176 { 00177 mln_assertion(expected_fwd_nb[p.id()][i] == q.id()); 00178 ++i; 00179 } 00180 } 00181 } 00182 00183 { 00184 // Window - Backward iteration 00185 mln_bkd_qiter_(win_t) q(win, p); 00186 for_all(p) 00187 { 00188 i = 0; 00189 for_all(q) 00190 { 00191 mln_assertion(expected_bkd_nb[p.id()][i] == q.id()); 00192 ++i; 00193 } 00194 } 00195 } 00196 00197 typedef graph_elt_neighborhood<util::graph,S> neighb_t; 00198 neighb_t neigh; 00199 { 00200 // Neighborhood - Forward iteration 00201 mln_niter_(neighb_t) n(neigh, p); 00202 00203 for_all(p) 00204 { 00205 i = 0; 00206 for_all(n) 00207 { 00208 mln_assertion(expected_fwd_nb[p.id()][i] == n.id()); 00209 ++i; 00210 } 00211 } 00212 } 00213 00214 { 00215 // Neighborhood - Backward iteration 00216 mln_bkd_niter_(neighb_t) n(neigh, p); 00217 for_all(p) 00218 { 00219 i = 0; 00220 for_all(n) 00221 { 00222 mln_assertion(expected_bkd_nb[p.id()][i] == n.id()); 00223 ++i; 00224 } 00225 } 00226 } 00227 }