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

vertex_image.cc

00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #include <vector>
00027 
00028 #include <mln/core/image/vertex_image.hh>
00029 #include <mln/make/vertex_image.hh>
00030 #include <mln/core/image/image2d.hh>
00031 #include <mln/accu/shape/bbox.hh>
00032 #include <mln/fun/i2v/array.hh>
00033 #include <mln/util/graph.hh>
00034 #include <mln/debug/draw_graph.hh>
00035 
00036 
00037 
00038 /* The graph is as follows:
00039 
00040             0 1 2 3 4
00041          .-----------
00042          |
00043        0 |  0       2
00044        1 |    \   / |
00045        2 |      1   |
00046        3 |       \  |
00047        4 |        3-4
00048 
00049 */
00050 
00051 
00052 static const unsigned X = mln_max(unsigned); // Invalid id.
00053 
00054 
00055 // Expected neighbors for forward and backward iteration.
00056 // X is an invalid id.
00057 static unsigned expected_fwd_nb[5][3] = { { 1, X, X },
00058                                           { 0, 2, 3 },
00059                                           { 1, 4, X },
00060                                           { 1, 4, X },
00061                                           { 3, 2, X } };
00062 
00063 static unsigned expected_bkd_nb[5][3] = { { 1, X, X },
00064                                           { 3, 2, 0 },
00065                                           { 4, 1, X },
00066                                           { 4, 1, X },
00067                                           { 2, 3, X } };
00068 
00069 
00070 int main()
00071 {
00072   using namespace mln;
00073 
00074   /*--------.
00075   | Graph.  |
00076   `--------*/
00077 
00078   // Points associated to vertices.
00079   typedef fun::i2v::array<point2d> fsite_t;
00080   fsite_t sites(5);
00081   sites(0) = point2d(0,0); // Point associated to vertex 0.
00082   sites(1) = point2d(2,2); // Point associated to vertex 1.
00083   sites(2) = point2d(0,4); // Point associated to vertex 2.
00084   sites(3) = point2d(4,3); // Point associated to vertex 3.
00085   sites(4) = point2d(4,4); // Point associated to vertex 4.
00086 
00087   // Edges.
00088   util::graph g;
00089 
00090   // Populate the graph with vertices.
00091   g.add_vertices(sites.size());
00092 
00093   // Populate the graph with edges.
00094   g.add_edge(0, 1);
00095   g.add_edge(1, 2);
00096   g.add_edge(1, 3);
00097   g.add_edge(3, 4);
00098   g.add_edge(4, 2);
00099 
00100   //g.print_debug(std::cout);
00101 
00102   /*-------------.
00103   | Graph image.  |
00104   `-------------*/
00105 
00106   // Graph values.
00107   typedef fun::i2v::array<unsigned> viota_t;
00108   viota_t iota(g.v_nmax());
00109   for (unsigned i = 0; i < iota.size(); ++i)
00110     iota(i) = 10 + i;
00111 
00112 
00113 
00115 
00116   {
00117     typedef vertex_image<point2d,unsigned> ima_t;
00118     ima_t ima = make::vertex_image(g, sites, iota);
00119 
00120     {
00121       // FIXME: Move this part to a special test case.
00122 
00123       // Compute the bounding box of 'ima'.
00124       accu::shape::bbox<point2d> a;
00125       mln_piter_(ima_t) p(ima.domain());
00126       for_all(p)
00127         a.take(p);
00128       box2d bbox = a.to_result();
00129       mln_assertion(bbox == make::box2d(5, 5));
00130 
00131       // Print the image.
00132       /* FIXME: Unfortunately, displaying graph images is not easy right
00133          now(2008-02-05).  We could use
00134 
00135          debug::println(ima);
00136 
00137          but there's not specialization working for graph_image; the one
00138          selected by the compiler is based on a 2-D bbox, and expects the
00139          interface of graph_image to work with points(not psites).
00140          Moreover, this implementation only shows *values*, not the graph
00141          itslef.
00142 
00143          An alternative is to use debug::graph,
00144          but it doesn't show the values, only the vertices and edges of the
00145          graph.
00146 
00147          The current solution is a mix between debug::graph and hand-made
00148          iterations.  */
00149       image2d<int> ima_rep(bbox);
00150 
00151       // We use the value 9 in debug::graph to represent edges to distinguish it
00152       // from vertices holding a value of 1.
00153       debug::draw_graph(ima_rep, ima.domain(), 1, 9);
00154     }
00155 
00156     /*------------.
00157       | Iterators.  |
00158       `------------*/
00159 
00160     // iteration over the domain of IMA.
00161     mln_piter_(ima_t) p(ima.domain());
00162     unsigned i = 10;
00163     for_all(p)
00164       mln_assertion(ima(p) == i++);
00165 
00166     typedef ima_t::win_t win_t;
00167     win_t win;
00168 
00169     {
00170       // Window - Forward iteration
00171       mln_qiter_(win_t) q(win, p);
00172       for_all(p)
00173       {
00174         i = 0;
00175         for_all(q)
00176         {
00177           mln_assertion(expected_fwd_nb[p.id()][i] == q.id());
00178           ++i;
00179         }
00180       }
00181     }
00182 
00183     {
00184       // Window - Backward iteration
00185       mln_bkd_qiter_(win_t) q(win, p);
00186       for_all(p)
00187       {
00188         i = 0;
00189         for_all(q)
00190         {
00191           mln_assertion(expected_bkd_nb[p.id()][i] == q.id());
00192           ++i;
00193         }
00194       }
00195     }
00196 
00197     typedef ima_t::nbh_t nbh_t;
00198     nbh_t neigh;
00199     {
00200       // Neighborhood - Forward iteration
00201       mln_niter_(nbh_t) n(neigh, p);
00202 
00203       for_all(p)
00204       {
00205         i = 0;
00206         for_all(n)
00207         {
00208           mln_assertion(expected_fwd_nb[p.id()][i] == n.id());
00209           ++i;
00210         }
00211       }
00212     }
00213 
00214     {
00215       // Neighborhood - Backward iteration
00216       mln_bkd_niter_(nbh_t) n(neigh, p);
00217       for_all(p)
00218       {
00219         i = 0;
00220         for_all(n)
00221         {
00222           mln_assertion(expected_bkd_nb[p.id()][i] == n.id());
00223           ++i;
00224         }
00225       }
00226     }
00227   }
00228 
00229 
00230 
00233 
00234   {
00235     typedef vertex_image<void,unsigned> ima_void_t;
00236     ima_void_t ima_void(g, iota);
00237 
00238     // iteration over the domain of IMA.
00239     mln_piter_(ima_void_t) p(ima_void.domain());
00240     unsigned i = 10;
00241     for_all(p)
00242       mln_assertion(ima_void(p) == i++);
00243 
00244     typedef ima_void_t::win_t win_t;
00245     win_t win;
00246 
00247     {
00248       // Window - Forward iteration
00249       mln_qiter_(win_t) q(win, p);
00250       for_all(p)
00251       {
00252         i = 0;
00253         for_all(q)
00254         {
00255           mln_assertion(expected_fwd_nb[p.id()][i] == q.id());
00256           ++i;
00257         }
00258       }
00259     }
00260 
00261     {
00262       // Window - Backward iteration
00263       mln_bkd_qiter_(win_t) q(win, p);
00264       for_all(p)
00265       {
00266         i = 0;
00267         for_all(q)
00268         {
00269           mln_assertion(expected_bkd_nb[p.id()][i] == q.id());
00270           ++i;
00271         }
00272       }
00273     }
00274 
00275     typedef ima_void_t::nbh_t nbh_t;
00276     nbh_t neigh;
00277     {
00278       // Neighborhood - Forward iteration
00279       mln_niter_(nbh_t) n(neigh, p);
00280 
00281       for_all(p)
00282       {
00283         i = 0;
00284         for_all(n)
00285         {
00286           //FIXME: Ambiguities with n.id()!!!!
00287           mln_assertion(expected_fwd_nb[p.id()][i] == n.element().id());
00288           ++i;
00289         }
00290       }
00291     }
00292 
00293     {
00294       // Neighborhood - Backward iteration
00295       mln_bkd_niter_(nbh_t) n(neigh, p);
00296       for_all(p)
00297       {
00298         i = 0;
00299         for_all(n)
00300         {
00301           //FIXME: Ambiguities with n.id()!!!!
00302           mln_assertion(expected_bkd_nb[p.id()][i] == n.element().id());
00303           ++i;
00304         }
00305       }
00306     }
00307   }
00308 }

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