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 <mln/core/alias/point2d.hh> 00027 00029 #include <mln/core/site_set/p_edges.hh> 00030 #include <mln/core/image/graph_elt_window.hh> 00031 #include <mln/core/image/graph_elt_neighborhood.hh> 00032 #include <mln/core/image/vertex_image.hh> 00033 #include <mln/core/neighb.hh> 00034 #include <mln/core/var.hh> 00035 #include <mln/pw/all.hh> 00036 #include <mln/fun/i2v/array.hh> 00037 #include <mln/util/graph.hh> 00038 #include <mln/make/vertex_image.hh> 00039 00040 00041 /* The graph and its corresponding line graph are as follows: 00042 00043 0 1 2 3 4 0 1 2 3 4 00044 .----------- .----------- 00045 | | 00046 0 | 0 2 0 | * * 00047 1 | \ / | 1 | 0 1 | 00048 2 | 1 | 2 | * 4 00049 3 | \ | 3 | 2 | 00050 4 | 3-4 4 | *3* 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 static unsigned expected_fwd_nbh[5][3] = { { 1, X, X }, 00060 { 0, 2, 3 }, 00061 { 1, 4, X }, 00062 { 1, 4, X }, 00063 { 3, 2, X } }; 00064 00065 static unsigned expected_bkd_nbh[5][3] = { { 1, 0, X }, 00066 { 3, 2, 0 }, 00067 { 4, 1, X }, 00068 { 4, 1, X }, 00069 { 2, 3, X } }; 00070 00071 00072 int main() 00073 { 00074 using namespace mln; 00075 00076 /*--------. 00077 | Graph. | 00078 `--------*/ 00079 00080 // Sites associated to edges. 00081 typedef fun::i2v::array<point2d> fsite_t; 00082 fsite_t sites(5); 00083 sites(0) = point2d(0,0); // Point associated to edge 0. 00084 sites(1) = point2d(2,2); // Point associated to edge 1. 00085 sites(2) = point2d(0,4); // Point associated to edge 2. 00086 sites(3) = point2d(4,3); // Point associated to edge 3. 00087 sites(4) = point2d(4,4); // Point associated to edge 4. 00088 00089 util::graph g; 00090 00091 // Populate the graph with vertices. 00092 g.add_vertices(sites.size()); 00093 00094 // Populate the graph with edges. 00095 g.add_edge(0, 1); 00096 g.add_edge(1, 2); 00097 g.add_edge(1, 3); 00098 g.add_edge(3, 4); 00099 g.add_edge(4, 2); 00100 00101 /*-------------------. 00102 | Line graph image. | 00103 `-------------------*/ 00104 00105 // Graph values. 00106 typedef fun::i2v::array<unsigned> viota_t; 00107 viota_t iota(g.v_nmax()); 00108 for (unsigned i = 0; i < iota.size(); ++i) 00109 iota(i) = 10 + i; 00110 00111 // Create line graph image. 00112 typedef vertex_image<point2d,unsigned> ima_t; 00113 ima_t ima = make::vertex_image(g, sites, iota); 00114 00115 /*------------. 00116 | Iterators. | 00117 `------------*/ 00118 00119 mln_edge_iter_(util::graph) ei(g); 00120 mln_edge_nbh_edge_iter_(util::graph) en(ei); 00121 00122 // Manual iteration over the domain of IMA. 00123 mln_piter_(ima_t) p(ima.domain()); 00124 unsigned i = 10; 00125 for_all(p) 00126 mln_assertion(ima(p) == i++); 00127 00128 typedef ima_t::win_t win_t; 00129 win_t win; 00130 00131 { 00132 // Window - Forward iteration 00133 mln_fwd_qiter_(win_t) q(win, p); 00134 for_all(p) 00135 { 00136 i = 0; 00137 for_all(q) 00138 { 00139 mln_assertion(expected_fwd_nbh[p.id()][i] == q.id()); 00140 ++i; 00141 } 00142 } 00143 } 00144 00145 { 00146 // Window - Backward iteration 00147 mln_bkd_qiter_(win_t) q(win, p); 00148 for_all(p) 00149 { 00150 i = 0; 00151 for_all(q) 00152 { 00153 mln_assertion(expected_bkd_nbh[p.id()][i] == q.id()); 00154 ++i; 00155 } 00156 } 00157 } 00158 00159 00160 typedef ima_t::nbh_t neighb_t; 00161 neighb_t neigh; 00162 00163 { 00164 // Neighborhood - Forward iteration 00165 mln_fwd_niter_(neighb_t) n(neigh, p); 00166 for_all(p) 00167 { 00168 i = 0; 00169 for_all(n) 00170 { 00171 mln_assertion(expected_fwd_nbh[p.id()][i] == n.id()); 00172 ++i; 00173 } 00174 } 00175 } 00176 00177 { 00178 // Neighborhood - Backward iteration 00179 mln_bkd_niter_(neighb_t) n(neigh, p); 00180 for_all(p) 00181 { 00182 i = 0; 00183 for_all(n) 00184 { 00185 mln_assertion(expected_bkd_nbh[p.id()][i] == n.id()); 00186 ++i; 00187 } 00188 } 00189 } 00190 00191 }