skeleton_constrained.hh 8.04 KB
Newer Older
1
2
// Copyright (C) 2008, 2009, 2011 EPITA Research and Development
// Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
11
12
13
14
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// 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
22
23
24
25
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
26
27
28
29

#ifndef MLN_MORPHO_SKELETON_CONSTRAINED_HH
# define MLN_MORPHO_SKELETON_CONSTRAINED_HH

30
/// \file
31
32
///
/// Compute a skeleton under constraints.
33
34
///
/// \todo Add an extension handling policy for the user to set it.
35
36
37
38
39

# include <mln/core/concept/image.hh>
# include <mln/core/concept/neighborhood.hh>
# include <mln/core/site_set/p_queue_fast.hh>
# include <mln/core/site_set/p_priority.hh>
40
# include <mln/extension/adjust_duplicate.hh>
41
# include <mln/extension/adjust_fill.hh>
42
# include <mln/data/fill.hh>
43

44
# include <mln/util/timer.hh>
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

namespace mln
{

  namespace morpho
  {

    template <typename I,
	      typename N, typename F,
	      typename K, typename R>
    mln_ch_value(I, bool)
    skeleton_constrained(const Image<I>& input,
			 const Neighborhood<N>& nbh, const F& is_simple,
			 const Image<K>& constraint, const Image<R>& priority);



# ifndef MLN_INCLUDE_ONLY

64
65

    namespace impl
66
    {
67

68
69
      namespace generic
      {
70

71
72
73
74
75
76
77
78
79
80
	template <typename I,
		  typename N, typename F,
		  typename K, typename R>
	inline
	mln_ch_value(I, bool)
	  skeleton_constrained(const Image<I>& input_,
			       const Neighborhood<N>& nbh_, const F& is_simple,
			       const Image<K>& constraint_, const Image<R>& priority_)
	{
	  trace::entering("morpho::skeleton_constrained");
81

82
83
84
85
	  const I& input      = exact(input_);
	  const N& nbh        = exact(nbh_);
	  const K& constraint = exact(constraint_);
	  const R& priority   = exact(priority_);
86

87
88
89
90
	  mln_precondition(input.is_valid());
	  mln_precondition(nbh.is_valid());
	  mln_precondition(constraint.is_valid());
	  mln_precondition(priority.is_valid());
91

92
93
	  typedef mln_value(I) V;
	  mlc_is(V, bool)::check();
94

95
	  extension::adjust_duplicate(input, nbh);
96

97
	  // FIXME: Tests!
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	  typedef mln_psite(I) P;
	  typedef p_queue_fast<P> Q;
	  p_priority<mln_value(R), Q> q;

	  mln_concrete(I) output;

	  // Initialization.
	  {
	    output = duplicate(input);
	    extension::adjust_duplicate(output, nbh);

	    mln_piter(I) p(output.domain());
	    for_all(p)
	      if (output(p) == false &&
		  is_simple.check(input, p)) // <-- is_simple.check
                                      // p is a simple point of the background.
	      {
		q.push(priority(p), p);
	      }
	  }

	  // Propagation.
121
	  {
122
123
124
125
126
127
128
129
130
131
	    P p;
	    mln_niter(N) n(nbh, p);
	    while (! q.is_empty())
	    {
	      p = q.pop_front();
	      for_all(n)
		if (output.has(n) &&
		    output(n) == true &&
		    constraint(n) == false &&
		    is_simple.check(output, n)) // <-- is_simple.check
132
133
134
135
		{
		  output(n) = false; // Remove n from object.
		  q.push(priority(n), n);
		}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	    }
	  }

	  trace::exiting("morpho::skeleton_constrained");
	  return output;
	}

      } // end of namespace mln::morpho::impl::generic


	template <typename I,
		  typename N, typename F,
		  typename K, typename R>
	inline
	mln_ch_value(I, bool)
	skeleton_constrained_fast(const Image<I>& input_,
				  const Neighborhood<N>& nbh_,
				  const F& is_simple,
				  const Image<K>& constraint_,
				  const Image<R>& priority_)
	{
	  trace::entering("morpho::skeleton_constrained_fast");

	  const I& input      = exact(input_);
	  const N& nbh        = exact(nbh_);
	  const K& constraint = exact(constraint_);
	  const R& priority   = exact(priority_);

	  mln_precondition(input.is_valid());
	  mln_precondition(nbh.is_valid());
	  mln_precondition(constraint.is_valid());
	  mln_precondition(priority.is_valid());

	  typedef mln_value(I) V;
	  mlc_is(V, bool)::check();

	  // Whatever the value of the extension, results of this fast
	  // version may differ from the generic one. Indeed, we
	  // cannot check whether a site belongs to the image or not,
	  // so we try to not take border site in consideration by
	  // setting their value to false.
	  extension::adjust_fill(input, nbh, false);
	  extension::adjust_fill(constraint, nbh, false);

	  // FIXME: Tests!

	  typedef p_queue_fast<unsigned> Q;
	  p_priority<mln_value(R), Q> q;

	  mln_concrete(I) output;

	  // Initialization.
	  {
	    output = duplicate(input);
	    extension::adjust_fill(output, nbh, false);

	    mln_pixter(I) p_out(output);
	    for_all(p_out)
	      if (p_out.val() == false &&
		  is_simple.check__(input, p_out)) // <-- is_simple.check
		                                   // p is a simple point of the background.
	      {
		q.push(priority.element(p_out.offset()), p_out);
	      }
200
	  }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

	  // Propagation.
	  {
	    mln_pixter(I) p(output);
	    mln_nixter(I, N) n(p, nbh);

	    util::array<int> dp = offsets_wrt(input, nbh);
	    const unsigned n_nbhs = dp.nelements();
	    while (! q.is_empty())
	    {
	      unsigned p = q.pop_front();

	      for (unsigned i = 0; i < n_nbhs; ++i)
	      {
		unsigned n = p + dp[i];

		if (output.element(n) == true &&
		    constraint.element(n) == false &&
		    is_simple.check__(output, n)) // <-- is_simple.check
		{
		  output.element(n) = false; // Remove n from object.
		  q.push(priority.element(n), n);
		}
	      }
	    }
	  }

	  trace::exiting("morpho::skeleton_constrained_fast");
	  return output;
	}


    } // end of namespace mln::morpho::impl


    namespace internal
    {

      template <typename I,
		typename N, typename F,
		typename K, typename R>
      inline
      mln_ch_value(I, bool)
      skeleton_constrained_dispatch(
	mln::trait::image::value_access::any,
	mln::trait::image::value_storage::any,
	const Image<I>& input,
	const Neighborhood<N>& nbh,
	const F& is_simple,
	const Image<K>& constraint,
	const Image<R>& priority)
      {
	return impl::generic::skeleton_constrained(input, nbh,
						   is_simple,
						   constraint,
						   priority);
257
258
      }

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
      template <typename I,
		typename N, typename F,
		typename K, typename R>
      inline
      mln_ch_value(I, bool)
      skeleton_constrained_dispatch(
	mln::trait::image::value_access::direct,
	mln::trait::image::value_storage::one_block,
	const Image<I>& input,
	const Neighborhood<N>& nbh,
	const F& is_simple,
	const Image<K>& constraint,
	const Image<R>& priority)
      {
	return impl::skeleton_constrained_fast(input, nbh,
					       is_simple,
					       constraint,
					       priority);
      }


    } // end of namespace mln::morpho::internal


    template <typename I,
	      typename N, typename F,
	      typename K, typename R>
    inline
    mln_ch_value(I, bool)
    skeleton_constrained(const Image<I>& input,
			 const Neighborhood<N>& nbh, const F& is_simple,
			 const Image<K>& constraint, const Image<R>& priority)
    {
      trace::entering("morpho::skeleton_constrained");

      mln_ch_value(I, bool)
	output = internal::skeleton_constrained_dispatch(
	  mln_trait_image_value_access(I)(),
	  mln_trait_image_value_storage(I)(),
	  input, nbh, is_simple, constraint, priority);

300
      trace::exiting("morpho::skeleton_constrained");
301
302
303
304
305
306
307
308
309
310
311
      return output;
    }

# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::morpho

} // end of namespace mln


#endif // ! MLN_MORPHO_SKELETON_CONSTRAINED_HH