Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
tests/draw/graph.cc
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <vector>
27 #include <utility>
28 
29 #include <mln/core/image/image2d.hh>
30 #include <mln/core/alias/point2d.hh>
31 #include <mln/debug/println.hh>
32 #include <mln/util/graph.hh>
33 #include <mln/core/site_set/p_vertices.hh>
34 #include <mln/core/site_set/p_vertices_psite.hh>
35 #include <mln/debug/draw_graph.hh>
36 #include <mln/data/compare.hh>
37 
38 
40 typedef std::vector< mln::point2d > points_type;
42 typedef std::vector< std::pair<int,int> > edges_type;
43 
44 using namespace mln;
45 
46 // FIXME: We might want to extract NROWS and NCOLS from REF instead of
47 // getting them from the caller.
48 void do_test(points_type& points, const edges_type& edges,
49  unsigned nrows, unsigned ncols, const mln::image2d<int>& ref)
50 {
51  // Graph.
52  typedef util::graph G;
53  G g;
54  // Populate the graph with nodes.
55  g.add_vertices(points.size());
56  // Populate the graph with edges.
57  for (edges_type::const_iterator i = edges.begin(); i != edges.end(); ++i)
58  g.add_edge(i->first, i->second);
59 
60  // Associate vertices to sites.
61  typedef fun::i2v::array<mln::point2d> F;
62  F fpoints(points);
63 
64  mln::p_vertices<G, F> pg(g, fpoints);
65 
66  image2d<int> ima(nrows, ncols);
67  data::fill(ima, 0);
68  debug::draw_graph(ima, pg, 2, 1);
69  mln_assertion(ima == ref);
70 }
71 
72 int
73 main()
74 {
75  /*---------.
76  | Test 1. |
77  `---------*/
78 
79  {
80  // Reference image.
81  int vs[3][3] = {
82  {2, 0, 0},
83  {0, 1, 0},
84  {0, 0, 2}
85  };
86  image2d<int> ref(make::image(vs));
87 
88  // Points associated to nodes.
89  points_type points;
90  points.push_back(point2d(0,0)); // Point associated to node 0.
91  points.push_back(point2d(2,2)); // Point associated to node 1.
92 
93  // Edges.
94  edges_type edges;
95  edges.push_back(std::make_pair(0, 1));
96 
97  do_test(points, edges, 3, 3, ref);
98  }
99 
100 
101  /*---------.
102  | Test 2. |
103  `---------*/
104 
105  {
106  int vs[5][5] = {
107  {2, 0, 0, 0, 2},
108  {0, 1, 0, 1, 1},
109  {0, 0, 2, 0, 1},
110  {0, 0, 0, 1, 1},
111  {0, 0, 0, 2, 2},
112  };
113  image2d<int> ref(make::image(vs));
114 
115  // Points associated to nodes.
116  points_type points;
117  points.push_back(point2d(0,0)); // Point associated to node 0.
118  points.push_back(point2d(2,2)); // Point associated to node 1.
119  points.push_back(point2d(0,4)); // Point associated to node 2.
120  points.push_back(point2d(4,3)); // Point associated to node 3.
121  points.push_back(point2d(4,4)); // Point associated to node 4.
122 
123  // Edges.
124  edges_type edges;
125  edges.push_back(std::make_pair(0, 1));
126  edges.push_back(std::make_pair(1, 2));
127  edges.push_back(std::make_pair(1, 3));
128  edges.push_back(std::make_pair(3, 4));
129  edges.push_back(std::make_pair(4, 2));
130 
131  do_test(points, edges, 5, 5, ref);
132  }
133 }