Milena (Olena)
User documentation 2.0a Id
|
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 00026 #include <vector> 00027 #include <utility> 00028 00029 #include <mln/core/image/image2d.hh> 00030 #include <mln/core/alias/point2d.hh> 00031 #include <mln/debug/println.hh> 00032 #include <mln/util/graph.hh> 00033 #include <mln/core/site_set/p_vertices.hh> 00034 #include <mln/core/site_set/p_vertices_psite.hh> 00035 #include <mln/debug/draw_graph.hh> 00036 #include <mln/data/compare.hh> 00037 00038 00040 typedef std::vector< mln::point2d > points_type; 00042 typedef std::vector< std::pair<int,int> > edges_type; 00043 00044 using namespace mln; 00045 00046 // FIXME: We might want to extract NROWS and NCOLS from REF instead of 00047 // getting them from the caller. 00048 void do_test(points_type& points, const edges_type& edges, 00049 unsigned nrows, unsigned ncols, const mln::image2d<int>& ref) 00050 { 00051 // Graph. 00052 typedef util::graph G; 00053 G g; 00054 // Populate the graph with nodes. 00055 g.add_vertices(points.size()); 00056 // Populate the graph with edges. 00057 for (edges_type::const_iterator i = edges.begin(); i != edges.end(); ++i) 00058 g.add_edge(i->first, i->second); 00059 00060 // Associate vertices to sites. 00061 typedef fun::i2v::array<mln::point2d> F; 00062 F fpoints(points); 00063 00064 mln::p_vertices<G, F> pg(g, fpoints); 00065 00066 image2d<int> ima(nrows, ncols); 00067 data::fill(ima, 0); 00068 debug::draw_graph(ima, pg, 2, 1); 00069 mln_assertion(ima == ref); 00070 } 00071 00072 int 00073 main() 00074 { 00075 /*---------. 00076 | Test 1. | 00077 `---------*/ 00078 00079 { 00080 // Reference image. 00081 int vs[3][3] = { 00082 {2, 0, 0}, 00083 {0, 1, 0}, 00084 {0, 0, 2} 00085 }; 00086 image2d<int> ref(make::image(vs)); 00087 00088 // Points associated to nodes. 00089 points_type points; 00090 points.push_back(point2d(0,0)); // Point associated to node 0. 00091 points.push_back(point2d(2,2)); // Point associated to node 1. 00092 00093 // Edges. 00094 edges_type edges; 00095 edges.push_back(std::make_pair(0, 1)); 00096 00097 do_test(points, edges, 3, 3, ref); 00098 } 00099 00100 00101 /*---------. 00102 | Test 2. | 00103 `---------*/ 00104 00105 { 00106 int vs[5][5] = { 00107 {2, 0, 0, 0, 2}, 00108 {0, 1, 0, 1, 1}, 00109 {0, 0, 2, 0, 1}, 00110 {0, 0, 0, 1, 1}, 00111 {0, 0, 0, 2, 2}, 00112 }; 00113 image2d<int> ref(make::image(vs)); 00114 00115 // Points associated to nodes. 00116 points_type points; 00117 points.push_back(point2d(0,0)); // Point associated to node 0. 00118 points.push_back(point2d(2,2)); // Point associated to node 1. 00119 points.push_back(point2d(0,4)); // Point associated to node 2. 00120 points.push_back(point2d(4,3)); // Point associated to node 3. 00121 points.push_back(point2d(4,4)); // Point associated to node 4. 00122 00123 // Edges. 00124 edges_type edges; 00125 edges.push_back(std::make_pair(0, 1)); 00126 edges.push_back(std::make_pair(1, 2)); 00127 edges.push_back(std::make_pair(1, 3)); 00128 edges.push_back(std::make_pair(3, 4)); 00129 edges.push_back(std::make_pair(4, 2)); 00130 00131 do_test(points, edges, 5, 5, ref); 00132 } 00133 }