$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
draw_graph.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 2012 EPITA Research and
2 // Development 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 #ifndef MLN_DEBUG_DRAW_GRAPH_HH
28 # define MLN_DEBUG_DRAW_GRAPH_HH
29 
35 
36 # include <mln/core/site_set/p_vertices.hh>
37 # include <mln/core/site_set/p_edges.hh>
38 # include <mln/util/line_graph.hh>
39 # include <mln/util/site_pair.hh>
40 # include <mln/draw/line.hh>
41 # include <mln/draw/box_plain.hh>
42 # include <mln/data/fill.hh>
43 # include <mln/metal/equal.hh>
44 
45 namespace mln
46 {
47 
48  namespace debug
49  {
50 
65  template <typename I, typename G, typename F>
66  void
67  draw_graph(Image<I>& ima, const p_vertices<G, F>& pv,
68  mln_value(I) vcolor, mln_value(I) ecolor);
69 
70 
85  template <typename I, typename G, typename F, typename V, typename E>
86  void
87  draw_graph(Image<I>& ima,
88  const p_vertices<G, F>& pv,
89  const Function<V>& vcolor_f, const Function<E>& ecolor_f,
90  unsigned vwidth);
91 
92 
94  template <typename I, typename G, typename F, typename V, typename E>
95  inline
96  void
97  draw_graph(Image<I>& ima_,
98  const p_vertices<G, F>& pv,
99  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_);
100 
101 
114  template <typename I, typename G, typename F, typename V, typename E>
115  inline
116  void
117  draw_graph(Image<I>& ima,
118  const p_vertices<util::line_graph<G>, F>& pv,
119  const Function<V>& vcolor_f, const Function<E>& ecolor_f);
120 
121 
122 # ifndef MLN_INCLUDE_ONLY
123 
124 
125 
126  // FIXME: Add assertions on the size of the image: it must be large
127  // enough to hold the representation of the graph/graph_image.
128 
129  template <typename I, typename G, typename F>
130  inline
131  void
132  draw_graph(Image<I>& ima_,
133  const p_edges<G, F>& pe,
134  mln_value(I) vcolor,
135  mln_value(I) ecolor)
136  {
137  mln_trace("debug::draw_graph");
138 
139  I& ima = exact(ima_);
140  mln_precondition(ima.is_valid());
141 
142  // Draw edges.
143  typedef p_edges<G, F> pe_t;
144  mln_piter(pe_t) p(pe);
145  for_all(p)
146  {
147  if (ima.has(p.first()) && ima.has(p.second()))
148  draw::line(ima, p.first(), p.second(), ecolor);
149  if (ima.has(p.first()))
150  ima(p.first()) = vcolor;
151  if (ima.has(p.second()))
152  ima(p.second()) = vcolor;
153  }
154 
155  }
156 
157 
158  template <typename I, typename G, typename F>
159  inline
160  void
161  draw_graph(Image<I>& ima_,
162  const p_vertices<G, F>& pv,
163  mln_value(I) vcolor,
164  mln_value(I) ecolor)
165  {
166  mln_trace("debug::draw_graph");
167 
168  I& ima = exact(ima_);
169  mln_precondition(ima.is_valid());
170 
171  // Draw edges.
172  const G& g = pv.graph();
173  typedef p_vertices<G, F> pv_t;
174  mln_edge_iter(G) ei(g);
175  for_all(ei)
176  draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor);
177 
178  // Draw vertices.
179  mln_piter(pv_t) p(pv);
180  for_all(p)
181  if (ima.has(p))
182  ima(p) = vcolor;
183 
184  }
185 
186 
187  // FIXME: Refactor + be more restrictive on the function type.
188  template <typename I, typename G, typename F, typename V, typename E>
189  inline
190  void
191  draw_graph(Image<I>& ima_,
192  const p_vertices<G, F>& pv,
193  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_,
194  unsigned vwidth)
195  {
196  mln_trace("debug::draw_graph");
197 
198  I& ima = exact(ima_);
199  const V& vcolor_f = exact(vcolor_f_);
200  const E& ecolor_f = exact(ecolor_f_);
201 
202  mln_precondition(ima.is_valid());
203 
204  // Draw edges.
205  const G& g = pv.graph();
206  typedef p_vertices<G, F> pv_t;
207  mln_edge_iter(G) ei(g);
208  for_all(ei)
209  if (ei.v1() != 0 && ei.v2() != 0)
210  draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor_f(ei.id()));
211 
212  // Draw vertices.
213  unsigned vhalf_width = vwidth / 2;
214  mln_piter(pv_t) p(pv);
215  for_all(p)
216  if (ima.has(p) && p.id() != 0u)
217  {
218  box2d box(p + dpoint2d(-vhalf_width, -vhalf_width),
219  p + dpoint2d(+vhalf_width, +vhalf_width));
220  box.crop_wrt(ima.domain());
221  draw::box_plain(ima, box, vcolor_f(p.id()));
222  }
223 
224  }
225 
226 
227  template <typename I, typename G, typename F, typename V, typename E>
228  inline
229  void
230  draw_graph(Image<I>& ima,
231  const p_vertices<G, F>& pv,
232  const Function<V>& vcolor_f, const Function<E>& ecolor_f)
233  {
234  draw_graph(ima, pv, vcolor_f, ecolor_f, 5);
235  }
236 
237 
238  // FIXME: Refactor + be more restrictive on the function type.
239  template <typename I, typename G, typename F, typename V, typename E>
240  inline
241  void
242  draw_graph(Image<I>& ima_,
243  const p_vertices<util::line_graph<G>, F>& pv,
244  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_)
245  {
246  mln_trace("debug::draw_graph");
247 
248  I& ima = exact(ima_);
249  const V& vcolor_f = exact(vcolor_f_);
250  const E& ecolor_f = exact(ecolor_f_);
251 
252  mln_precondition(ima.is_valid());
253 
254  typedef util::line_graph<G> LG;
255 
256  const LG& lg = pv.graph();
257  const G& g = lg.graph();
258  typedef p_vertices<LG, F> pv_t;
259  mln_vertex_iter(LG) vi(lg);
260  for_all(vi)
261  {
262  p_line2d l = pv(vi.id());
263  // Draw edges (Line graph vertices).
264  draw::line(ima, l.begin(), l.end(), ecolor_f(vi.id()));
265 
266  // Draw vertices (graph vertices).
267  if (ima.has(l.begin()))
268  ima(l.begin()) = vcolor_f(g.edge(vi).v1());
269  if (ima.has(l.end()))
270  ima(l.end()) = vcolor_f(g.edge(vi).v2());
271  }
272 
273  }
274 
275 # endif // ! MLN_INCLUDE_ONLY
276 
277  } // end of namespace mln::debug
278 
279 } // end of namespace mln
280 
281 #endif // ! MLN_DEBUG_DRAW_GRAPH_HH