• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

draw_graph.hh

00001 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_DEBUG_DRAW_GRAPH_HH
00028 # define MLN_DEBUG_DRAW_GRAPH_HH
00029 
00035 
00036 # include <mln/core/site_set/p_vertices.hh>
00037 # include <mln/core/site_set/p_edges.hh>
00038 # include <mln/util/line_graph.hh>
00039 # include <mln/util/site_pair.hh>
00040 # include <mln/draw/line.hh>
00041 # include <mln/draw/box_plain.hh>
00042 # include <mln/data/fill.hh>
00043 # include <mln/metal/equal.hh>
00044 
00045 namespace mln
00046 {
00047 
00048   namespace debug
00049   {
00050 
00054     /*
00055      * \param[in,out] ima      The image to be drawn.
00056      * \param[in]     pv       The p_vertices which contains vertices positions.
00057      * \param[in]     vcolor The value to assign to pixels which contains
00058      *                          vertices.
00059      * \param[in]     ecolor   The value to assign to pixels which contains
00060      *                         edges.
00061      */
00062     template <typename I, typename G, typename F>
00063     void
00064     draw_graph(Image<I>& ima, const p_vertices<G, F>& pv,
00065                mln_value(I) vcolor, mln_value(I) ecolor);
00066 
00067 
00071     /*
00072      * \param[in,out] ima       The image to be drawn.
00073      * \param[in]     pv        The p_vertices which contains vertices positions.
00074      * \param[in]     vcolor_f_ A function returning a color value for vertices.
00075      * \param[in]     ecolor_f_ A function returning a color value for edges.
00076      *
00077      */
00078     template <typename I, typename G, typename F, typename V, typename E>
00079     void
00080     draw_graph(Image<I>& ima,
00081                const p_vertices<G, F>& pv,
00082                const Function<V>& vcolor_f_, const Function<E>& ecolor_f_);
00083 
00087     /*
00088      * \param[in,out] ima       The image to be drawn.
00089      * \param[in]     pv        The p_vertices which contains vertices positions.
00090      * \param[in]     vcolor_f_ A function returning a color value for vertices.
00091      * \param[in]     ecolor_f_ A function returning a color value for edges.
00092      *
00093      */
00094     template <typename I, typename G, typename F, typename V, typename E>
00095     inline
00096     void
00097     draw_graph(Image<I>& ima,
00098                const p_vertices<util::line_graph<G>, F>& pv,
00099                const Function<V>& vcolor_f_, const Function<E>& ecolor_f_);
00100 
00101 
00102 # ifndef MLN_INCLUDE_ONLY
00103 
00104 
00105 
00106     // FIXME: Add assertions on the size of the image: it must be large
00107     // enough to hold the representation of the graph/graph_image.
00108 
00109     template <typename I, typename G, typename F>
00110     inline
00111     void
00112     draw_graph(Image<I>& ima_,
00113                const p_edges<G, F>& pe,
00114                mln_value(I) vcolor,
00115                mln_value(I) ecolor)
00116     {
00117       trace::entering("debug::draw_graph");
00118 
00119       I& ima = exact(ima_);
00120       mln_precondition(ima.is_valid());
00121 
00122       // Draw edges.
00123       typedef p_edges<G, F> pe_t;
00124       mln_piter(pe_t) p(pe);
00125       for_all(p)
00126       {
00127         if (ima.has(p.first()) && ima.has(p.second()))
00128           draw::line(ima, p.first(), p.second(), ecolor);
00129         if (ima.has(p.first()))
00130           ima(p.first()) = vcolor;
00131         if (ima.has(p.second()))
00132           ima(p.second()) = vcolor;
00133       }
00134 
00135       trace::exiting("debug::draw_graph");
00136     }
00137 
00138 
00139     template <typename I, typename G, typename F>
00140     inline
00141     void
00142     draw_graph(Image<I>& ima_,
00143                const p_vertices<G, F>& pv,
00144                mln_value(I) vcolor,
00145                mln_value(I) ecolor)
00146     {
00147       trace::entering("debug::draw_graph");
00148 
00149       I& ima = exact(ima_);
00150       mln_precondition(ima.is_valid());
00151 
00152       // Draw edges.
00153       const G& g = pv.graph();
00154       typedef p_vertices<G, F> pv_t;
00155       mln_edge_iter(G) ei(g);
00156       for_all(ei)
00157         draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor);
00158 
00159       // Draw vertices.
00160       mln_piter(pv_t) p(pv);
00161       for_all(p)
00162         if (ima.has(p))
00163           ima(p) = vcolor;
00164 
00165       trace::exiting("debug::draw_graph");
00166     }
00167 
00168 
00169     // FIXME: Refactor + be more restrictive on the function type.
00170     template <typename I, typename G, typename F, typename V, typename E>
00171     inline
00172     void
00173     draw_graph(Image<I>& ima_,
00174                const p_vertices<G, F>& pv,
00175                const Function<V>& vcolor_f_, const Function<E>& ecolor_f_)
00176     {
00177       trace::entering("debug::draw_graph");
00178 
00179       I& ima = exact(ima_);
00180       const V& vcolor_f = exact(vcolor_f_);
00181       const E& ecolor_f = exact(ecolor_f_);
00182 
00183       mln_precondition(ima.is_valid());
00184 
00185       // Draw edges.
00186       const G& g = pv.graph();
00187       typedef p_vertices<G, F> pv_t;
00188       mln_edge_iter(G) ei(g);
00189       for_all(ei)
00190         if (ei.v1() != 0 && ei.v2() != 0)
00191           draw::line(ima, pv(ei.v1()), pv(ei.v2()), ecolor_f(ei.id()));
00192 
00193       // Draw vertices.
00194       mln_piter(pv_t) p(pv);
00195       for_all(p)
00196         if (ima.has(p) && p.id() != 0u)
00197         {
00198           box2d box(p + dpoint2d(-5, -5), p + dpoint2d(+5, +5));
00199           box.crop_wrt(ima.domain());
00200           draw::box_plain(ima, box, vcolor_f(p.id()));
00201         }
00202 
00203       trace::exiting("debug::draw_graph");
00204     }
00205 
00206 
00207     // FIXME: Refactor + be more restrictive on the function type.
00208     template <typename I, typename G, typename F, typename V, typename E>
00209     inline
00210     void
00211     draw_graph(Image<I>& ima_,
00212                const p_vertices<util::line_graph<G>, F>& pv,
00213                const Function<V>& vcolor_f_, const Function<E>& ecolor_f_)
00214     {
00215       trace::entering("debug::draw_graph");
00216 
00217       I& ima = exact(ima_);
00218       const V& vcolor_f = exact(vcolor_f_);
00219       const E& ecolor_f = exact(ecolor_f_);
00220 
00221       mln_precondition(ima.is_valid());
00222 
00223       typedef util::line_graph<G> LG;
00224 
00225       const LG& lg = pv.graph();
00226       const G& g = lg.graph();
00227       typedef p_vertices<LG, F> pv_t;
00228       mln_vertex_iter(LG) vi(lg);
00229       for_all(vi)
00230       {
00231         p_line2d l = pv(vi.id());
00232         // Draw edges (Line graph vertices).
00233         draw::line(ima, l.begin(), l.end(), ecolor_f(vi.id()));
00234 
00235         // Draw vertices (graph vertices).
00236         if (ima.has(l.begin()))
00237           ima(l.begin()) = vcolor_f(g.edge(vi).v1());
00238         if (ima.has(l.end()))
00239           ima(l.end()) = vcolor_f(g.edge(vi).v2());
00240       }
00241 
00242       trace::exiting("debug::draw_graph");
00243     }
00244 
00245 # endif // ! MLN_INCLUDE_ONLY
00246 
00247    } // end of namespace mln::debug
00248 
00249 } // end of namespace mln
00250 
00251 #endif // ! MLN_DEBUG_DRAW_GRAPH_HH

Generated on Tue Oct 4 2011 15:23:42 for Milena (Olena) by  doxygen 1.7.1