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/image/graph_elt_mixed_window.hh> 00029 #include <mln/core/image/vertex_image.hh> 00030 #include <mln/core/image/edge_image.hh> 00031 #include <mln/fun/i2v/array.hh> 00032 #include <mln/util/graph.hh> 00033 00034 00035 00036 /* The graph is as follows: 00037 00038 0 1 2 3 4 00039 .----------- 00040 | 00041 0 | 0 2 00042 1 | \ / | 00043 2 | 1 | 00044 3 | \ | 00045 4 | 3-4 00046 00047 */ 00048 00049 static const unsigned X = mln_max(unsigned); // Invalid id. 00050 00051 00052 static unsigned expected_fwd_nb[5][3] = { { 0, X, X }, 00053 { 0, 1, 2 }, 00054 { 1, 4, X }, 00055 { 2, 3, X }, 00056 { 3, 4, X } }; 00057 00058 00059 static unsigned expected_bkd_nb[5][3] = { { 0, X, X }, 00060 { 2, 1, 0 }, 00061 { 4, 1, X }, 00062 { 3, 2, X }, 00063 { 4, 3, X } }; 00064 00065 00066 int main() 00067 { 00068 using namespace mln; 00069 00070 /*--------. 00071 | Graph. | 00072 `--------*/ 00073 00074 // Points associated to vertices. 00075 typedef fun::i2v::array<point2d> fsite_t; 00076 fsite_t sites(5); 00077 sites(0) = point2d(0,0); // Point associated to vertex 0. 00078 sites(1) = point2d(2,2); // Point associated to vertex 1. 00079 sites(2) = point2d(0,4); // Point associated to vertex 2. 00080 sites(3) = point2d(4,3); // Point associated to vertex 3. 00081 sites(4) = point2d(4,4); // Point associated to vertex 4. 00082 00083 // Edges. 00084 util::graph g; 00085 00086 // Populate the graph with vertices. 00087 g.add_vertices(sites.size()); 00088 00089 // Populate the graph with edges. 00090 g.add_edge(0, 1); 00091 g.add_edge(1, 2); 00092 g.add_edge(1, 3); 00093 g.add_edge(3, 4); 00094 g.add_edge(4, 2); 00095 00096 //g.print_debug(std::cout); 00097 00098 /*-------------. 00099 | Graph image. | 00100 `-------------*/ 00101 00102 // Vertex values. 00103 typedef fun::i2v::array<unsigned> viota_t; 00104 viota_t iota(g.v_nmax()); 00105 for (unsigned i = 0; i < iota.size(); ++i) 00106 iota(i) = 10 + i; 00107 00108 00109 // Edge values. 00110 typedef fun::i2v::array<unsigned> eiota_t; 00111 eiota_t eiota(g.e_nmax()); 00112 for (unsigned i = 0; i < eiota.size(); ++i) 00113 eiota(i) = 20 + i; 00114 00115 00116 typedef vertex_image<void,unsigned> v_ima_t; 00117 v_ima_t v_ima(g, iota); 00118 00119 typedef edge_image<void,unsigned> e_ima_t; 00120 e_ima_t e_ima(g, eiota); 00121 00122 00123 // Iterator over the vertex image. 00124 mln_piter_(v_ima_t) v(v_ima.domain()); 00125 00126 00127 typedef graph_elt_mixed_window<util::graph, 00128 v_ima_t::domain_t, 00129 e_ima_t::domain_t> edge_win_t; 00130 edge_win_t win; 00131 00132 // Forward Iteration 00133 { 00134 for_all(v) 00135 { 00136 int i = 0; 00137 // Iterator over the neighbor edges in the edge image. 00138 mln_qiter_(edge_win_t) e(win, e_ima.domain(), v); 00139 for_all(e) 00140 { 00141 mln_assertion(expected_fwd_nb[v.id()][i++] == e.id()); 00142 mln_assertion((e.id() + 20) == e_ima(e)); 00143 } 00144 } 00145 } 00146 00147 // Backward Iteration 00148 { 00149 for_all(v) 00150 { 00151 int i = 0; 00152 // Iterator over the neighbor edges in the edge image. 00153 mln_bkd_qiter_(edge_win_t) e(win, e_ima.domain(), v); 00154 for_all(e) 00155 { 00156 mln_assertion(expected_bkd_nb[v.id()][i++] == e.id()); 00157 mln_assertion((e.id() + 20) == e_ima(e)); 00158 } 00159 } 00160 } 00161 00162 00163 00164 // FIXME: add tests for graph_window_if and graph_neighborhood_if when 00165 // they support this feature. 00166 00167 }