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