$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
voronoi.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_MAKE_VORONOI_HH
27 # define MLN_MAKE_VORONOI_HH
28 
32 
33 # include <vector>
34 # include <map>
35 
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/core/site_set/p_vertices.hh>
38 # include <mln/accu/stat/mean.hh>
39 # include <mln/estim/min_max.hh>
40 # include <mln/util/graph.hh>
41 
42 namespace mln
43 {
44 
45  namespace make
46  {
47 
50 
57  template <typename I, typename N>
58  p_vertices<util::graph, fun::i2v::array<mln_site(I)> >
59  voronoi (Image<I>& ima_,
60  Image<I>& orig_,
61  const Neighborhood<N>& nbh);
62 
63 # ifndef MLN_INCLUDE_ONLY
64 
65  template <typename I, typename N>
66  inline
67  p_vertices<util::graph, fun::i2v::array<mln_site(I)> >
68  voronoi (Image<I>& ima_,
69  Image<I>& orig_,
70  const Neighborhood<N>& nbh)
71  {
72  typedef algebra::vec<2,float> X;
73  typedef mln_value(I) V;
74  typedef mln_psite(I) P;
75 
76  I& ima = exact(ima_);
77  I& orig = exact(orig_);
78  util::graph gr;
79  V min, max;
80  estim::min_max (ima, min, max);
81  unsigned nb = max - min + 1;
82  fun::i2v::array<P> v(nb);
83  std::vector< accu::stat::mean< X > > tab_mean (nb);
84  std::map<std::pair<V, V>, bool> m;
85 
87  {
88  mln_piter(I) p(orig.domain());
89 
90  for_all(p)
91  {
92  if (orig(p) != 0)
93  {
94  X x = mln_psite(I)(p);
95  tab_mean[orig(p) - min].take(x);
96  }
97  }
98  }
99 
101  {
102  mln_piter(I) p(ima.domain());
103  mln_niter(N) n(nbh, p);
104 
105  for_all(p)
106  {
107  for_all (n) if (ima.has(n))
108  if (ima(p) != ima(n))
109  m[std::pair<V, V>(ima(p) - min, ima(n) - min)] = true;
110  }
111  }
112 
114  for (unsigned i = 0; i < nb; ++i)
115  {
116  gr.add_vertex();
117 
119  v[i] = point2d ((unsigned)tab_mean[i].to_result ()[0],
120  (unsigned)tab_mean[i].to_result ()[1]);
121  }
122 
123  typename std::map<std::pair<V, V>, bool>::const_iterator it = m.begin ();
124  for (; it != m.end (); ++it)
125  gr.add_edge((*it).first.first, (*it).first.second);
126 
127  p_vertices<util::graph, fun::i2v::array<P> > res(gr, v);
128  return res;
129  }
130 
131 # endif // ! MLN_INCLUDE_ONLY
132 
133  } // end of namespace mln::make
134 
135 } // end of namespace mln
136 
137 
138 #endif // ! MLN_MAKE_VORONOI_HH