Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
draw_graph.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 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 #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 
54  /*
55  * \param[in,out] ima The image to be drawn.
56  * \param[in] pv The p_vertices which contains vertices positions.
57  * \param[in] vcolor The value to assign to pixels which contains
58  * vertices.
59  * \param[in] ecolor The value to assign to pixels which contains
60  * edges.
61  */
62  template <typename I, typename G, typename F>
63  void
64  draw_graph(Image<I>& ima, const p_vertices<G, F>& pv,
65  mln_value(I) vcolor, mln_value(I) ecolor);
66 
67 
71  /*
72  * \param[in,out] ima The image to be drawn.
73  * \param[in] pv The p_vertices which contains vertices positions.
74  * \param[in] vcolor_f_ A function returning a color value for vertices.
75  * \param[in] ecolor_f_ A function returning a color value for edges.
76  *
77  */
78  template <typename I, typename G, typename F, typename V, typename E>
79  void
80  draw_graph(Image<I>& ima,
81  const p_vertices<G, F>& pv,
82  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_);
83 
87  /*
88  * \param[in,out] ima The image to be drawn.
89  * \param[in] pv The p_vertices which contains vertices positions.
90  * \param[in] vcolor_f_ A function returning a color value for vertices.
91  * \param[in] ecolor_f_ A function returning a color value for edges.
92  *
93  */
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<util::line_graph<G>, F>& pv,
99  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_);
100 
101 
102 # ifndef MLN_INCLUDE_ONLY
103 
104 
105 
106  // FIXME: Add assertions on the size of the image: it must be large
107  // enough to hold the representation of the graph/graph_image.
108 
109  template <typename I, typename G, typename F>
110  inline
111  void
112  draw_graph(Image<I>& ima_,
113  const p_edges<G, F>& pe,
114  mln_value(I) vcolor,
115  mln_value(I) ecolor)
116  {
117  trace::entering("debug::draw_graph");
118 
119  I& ima = exact(ima_);
120  mln_precondition(ima.is_valid());
121 
122  // Draw edges.
123  typedef p_edges<G, F> pe_t;
124  mln_piter(pe_t) p(pe);
125  for_all(p)
126  {
127  if (ima.has(p.first()) && ima.has(p.second()))
128  draw::line(ima, p.first(), p.second(), ecolor);
129  if (ima.has(p.first()))
130  ima(p.first()) = vcolor;
131  if (ima.has(p.second()))
132  ima(p.second()) = vcolor;
133  }
134 
135  trace::exiting("debug::draw_graph");
136  }
137 
138 
139  template <typename I, typename G, typename F>
140  inline
141  void
143  const p_vertices<G, F>& pv,
144  mln_value(I) vcolor,
145  mln_value(I) ecolor)
146  {
147  trace::entering("debug::draw_graph");
148 
149  I& ima = exact(ima_);
150  mln_precondition(ima.is_valid());
151 
152  // Draw edges.
153  const G& g = pv.graph();
154  typedef p_vertices<G, F> pv_t;
155  mln_edge_iter(G) ei(g);
156  for_all(ei)
157  draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor);
158 
159  // Draw vertices.
160  mln_piter(pv_t) p(pv);
161  for_all(p)
162  if (ima.has(p))
163  ima(p) = vcolor;
164 
165  trace::exiting("debug::draw_graph");
166  }
167 
168 
169  // FIXME: Refactor + be more restrictive on the function type.
170  template <typename I, typename G, typename F, typename V, typename E>
171  inline
172  void
174  const p_vertices<G, F>& pv,
175  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_)
176  {
177  trace::entering("debug::draw_graph");
178 
179  I& ima = exact(ima_);
180  const V& vcolor_f = exact(vcolor_f_);
181  const E& ecolor_f = exact(ecolor_f_);
182 
183  mln_precondition(ima.is_valid());
184 
185  // Draw edges.
186  const G& g = pv.graph();
187  typedef p_vertices<G, F> pv_t;
188  mln_edge_iter(G) ei(g);
189  for_all(ei)
190  if (ei.v1() != 0 && ei.v2() != 0)
191  draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor_f(ei.id()));
192 
193  // Draw vertices.
194  mln_piter(pv_t) p(pv);
195  for_all(p)
196  if (ima.has(p) && p.id() != 0u)
197  {
198  box2d box(p + dpoint2d(-5, -5), p + dpoint2d(+5, +5));
199  box.crop_wrt(ima.domain());
200  draw::box_plain(ima, box, vcolor_f(p.id()));
201  }
202 
203  trace::exiting("debug::draw_graph");
204  }
205 
206 
207  // FIXME: Refactor + be more restrictive on the function type.
208  template <typename I, typename G, typename F, typename V, typename E>
209  inline
210  void
212  const p_vertices<util::line_graph<G>, F>& pv,
213  const Function<V>& vcolor_f_, const Function<E>& ecolor_f_)
214  {
215  trace::entering("debug::draw_graph");
216 
217  I& ima = exact(ima_);
218  const V& vcolor_f = exact(vcolor_f_);
219  const E& ecolor_f = exact(ecolor_f_);
220 
221  mln_precondition(ima.is_valid());
222 
223  typedef util::line_graph<G> LG;
224 
225  const LG& lg = pv.graph();
226  const G& g = lg.graph();
227  typedef p_vertices<LG, F> pv_t;
228  mln_vertex_iter(LG) vi(lg);
229  for_all(vi)
230  {
231  p_line2d l = pv(vi.id());
232  // Draw edges (Line graph vertices).
233  draw::line(ima, l.begin(), l.end(), ecolor_f(vi.id()));
234 
235  // Draw vertices (graph vertices).
236  if (ima.has(l.begin()))
237  ima(l.begin()) = vcolor_f(g.edge(vi).v1());
238  if (ima.has(l.end()))
239  ima(l.end()) = vcolor_f(g.edge(vi).v2());
240  }
241 
242  trace::exiting("debug::draw_graph");
243  }
244 
245 # endif // ! MLN_INCLUDE_ONLY
246 
247  } // end of namespace mln::debug
248 
249 } // end of namespace mln
250 
251 #endif // ! MLN_DEBUG_DRAW_GRAPH_HH