Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
influence_zone_adjacency_graph.hh
1 // Copyright (C) 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_INFLUENCE_ZONE_ADJACENCY_GRAPH_HH
27 # define MLN_MAKE_INFLUENCE_ZONE_ADJACENCY_GRAPH_HH
28 
37 
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/concept/neighborhood.hh>
40 # include <mln/core/image/image2d.hh>
41 # include <mln/core/alias/box2d.hh>
42 # include <mln/extension/adjust_fill.hh>
43 # include <mln/util/graph.hh>
44 # include <mln/util/adjacency_matrix.hh>
45 
46 
47 namespace mln
48 {
49 
50  namespace make
51  {
52 
60  //
61  template <typename I, typename N>
62  util::graph
63  influence_zone_adjacency_graph(const Image<I>& iz_,
64  const Neighborhood<N>& nbh,
65  const mln_value(I)& nlabels);
66 
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71 
72  namespace internal
73  {
74 
75  template <typename I, typename N>
76  void
77  influence_zone_adjacency_graph_tests(const Image<I>& iz,
78  const Neighborhood<N>& nbh,
79  const mln_value(I)&)
80  {
81  mln_precondition(exact(iz).is_valid());
82  mln_precondition(exact(nbh).is_valid());
83  (void) iz;
84  (void) nbh;
85  }
86 
87  } // end of namespace mln::make::internal
88 
89 
90  namespace impl
91  {
92 
93  namespace generic
94  {
95 
103  //
104  template <typename I, typename N>
105  util::graph
106  influence_zone_adjacency_graph(const Image<I>& iz_,
107  const Neighborhood<N>& nbh_,
108  const mln_value(I)& nlabels)
109  {
110  trace::entering("make::impl::generic::influence_zone_adjacency_graph");
111 
112  internal::influence_zone_adjacency_graph_tests(iz_, nbh_, nlabels);
113  const I& iz = exact(iz_);
114  const N& nbh = exact(nbh_);
115 
116  util::adjacency_matrix<> adj(nlabels.next());
117  extension::adjust_fill(iz, nbh, 0u);
118 
119  typedef mln_value(I) L;
120  mln_piter(I) p(iz.domain());
121  mln_niter(N) n(nbh, p);
122  for_all(p)
123  {
124  L l1 = iz(p);
125  for_all(n)
126  {
127  if (iz.domain().has(n))
128  {
129  L l2 = iz(n);
130  if (iz(n) != iz((p)))
131  adj.add(l1, l2);
132  }
133  }
134  }
135 
136  // Construct graph.
137  util::graph g;
138  g.add_vertices(nlabels.next());
139  for (unsigned i = 0; i < nlabels.next(); ++i)
140  for (unsigned j = 0; j < i; ++j)
141  if (adj.are_adjacent(i, j))
142  g.add_edge(i, j);
143 
144  trace::exiting("make::impl::generic::influence_zone_adjacency_graph");
145  return g;
146  }
147 
148  } // end of namespace mln::make::impl::generic
149 
150  } // end of namespace mln::make::impl
151 
152 
153 
154  namespace internal
155  {
156 
157  template <typename I, typename N>
158  util::graph
159  influence_zone_adjacency_graph_dispatch(const Image<I>& iz,
160  const Neighborhood<N>& nbh,
161  const mln_value(I)& nlabels)
162  {
163  return make::impl::generic::influence_zone_adjacency_graph(iz, nbh, nlabels);
164  }
165 
166  } // end of namespace mln::make::internal
167 
168 
169 
170  // Facade
171 
172  template <typename I, typename N>
173  inline
174  util::graph
176  const Neighborhood<N>& nbh,
177  const mln_value(I)& nlabels)
178  {
179  trace::entering("make::influence_zone_adjacency_graph");
180 
181  internal::influence_zone_adjacency_graph_tests(iz, nbh, nlabels);
182 
183  util::graph g = internal::influence_zone_adjacency_graph_dispatch(iz, nbh, nlabels);
184 
185  trace::exiting("make::influence_zone_adjacency_graph");
186  return g;
187  }
188 
189 
190 # endif // ! MLN_INCLUDE_ONLY
191 
192 
193  } // end of namespace mln::make
194 
195 } // end of namespace mln
196 
197 
198 #endif // ! MLN_MAKE_INFLUENCE_ZONE_ADJACENCY_GRAPH_HH