$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
labeling/compute.hh
1 // Copyright (C) 2008, 2009, 2010, 2012, 2013 EPITA Research and
2 // Development Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // 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 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_LABELING_COMPUTE_HH
28 # define MLN_LABELING_COMPUTE_HH
29 
47 
48 # include <mln/core/concept/image.hh>
49 # include <mln/core/concept/accumulator.hh>
50 # include <mln/core/concept/meta_accumulator.hh>
51 # include <mln/util/array.hh>
52 # include <mln/convert/from_to.hh>
53 # include <mln/geom/ncols.hh>
54 # include <mln/value/next.hh>
55 
56 
57 namespace mln
58 {
59 
60  namespace labeling
61  {
62 
74  template <typename A, typename I, typename L>
75  util::array<mln_result(A)>
76  compute(const Accumulator<A>& a,
77  const Image<I>& input,
78  const Image<L>& label,
79  const mln_value(L)& nlabels);
80 
81 
93  template <typename A, typename I, typename L>
94  util::array<mln_meta_accu_result(A, mln_value(I))>
95  compute(const Meta_Accumulator<A>& a,
96  const Image<I>& input,
97  const Image<L>& label,
98  const mln_value(L)& nlabels);
99 
100 
111  template <typename A, typename L>
112  util::array<mln_result(A)>
113  compute(const Accumulator<A>& a,
114  const Image<L>& label,
115  const mln_value(L)& nlabels);
116 
117 
128  template <typename A, typename L>
129  util::array<mln_meta_accu_result(A, mln_psite(L))>
130  compute(const Meta_Accumulator<A>& a,
131  const Image<L>& label,
132  const mln_value(L)& nlabels);
133 
134 
146  template <typename A, typename I, typename L>
147  util::array<mln_result(A)>
148  compute(util::array<A>& a,
149  const Image<I>& input,
150  const Image<L>& label,
151  const mln_value(L)& nlabels);
152 
153 
154 
155 # ifndef MLN_INCLUDE_ONLY
156 
157  namespace internal
158  {
159 
160  template <typename A, typename L>
161  inline
162  void
163  compute_tests(const Accumulator<A>& a,
164  const Image<L>& label,
165  const mln_value(L)& nlabels)
166  {
167  mln_precondition(exact(label).is_valid());
168  // mlc_is_a(mln_value(L), mln::value::Symbolic)::check();
169  (void) a;
170  (void) label;
171  (void) nlabels;
172  }
173 
174 
175  template <typename A, typename I, typename L>
176  inline
177  void
178  compute_tests(const Accumulator<A>& a,
179  const Image<I>& input,
180  const Image<L>& label,
181  const mln_value(L)& nlabels)
182  {
183  mln_precondition(exact(input).is_valid());
184  mln_precondition(exact(label).is_valid());
185  // mlc_is_a(mln_value(L), mln::value::Symbolic)::check();
186  (void) a;
187  (void) input;
188  (void) label;
189  (void) nlabels;
190  }
191 
192  } // end of namespace mln::labeling::internal
193 
194 
195 
196  namespace impl
197  {
198 
199  namespace generic
200  {
201 
202  template <typename A, typename L>
203  inline
204  util::array<mln_result(A)>
205  compute(const Accumulator<A>& a_,
206  const Image<L>& label_,
207  const mln_value(L)& nlabels)
208  {
209  mln_trace("labeling::impl::generic::compute");
210  internal::compute_tests(a_, label_, nlabels);
211 
212  const A& a = exact(a_);
213  const L& label = exact(label_);
214 
215  util::array<A> accus(value::next(nlabels), a);
216 
217  mln_piter(L) p(label.domain());
218  for_all(p)
219  accus[label(p)].take(p);
220 
221  util::array<mln_result(A)> res;
222  convert::from_to(accus, res);
223 
224  return res;
225  }
226 
227  template <typename A, typename L>
228  inline
229  util::array<mln_result(A)>
230  compute(util::array<A>& accus,
231  const Image<L>& label_,
232  const mln_value(L)& nlabels)
233  {
234  mln_trace("labeling::impl::generic::compute");
235  internal::compute_tests(A(), label_, nlabels);
236 
237  if (value::next(nlabels) != accus.size())
238  {
239  accus.resize(0); // Make sure all the accumulators are
240  // re-initialized when resizing on next
241  // line.
242  accus.resize(value::next(nlabels));
243  }
244 
245  const L& label = exact(label_);
246 
247  mln_piter(L) p(label.domain());
248  for_all(p)
249  accus[label(p)].take(p);
250 
251  util::array<mln_result(A)> res;
252  convert::from_to(accus, res);
253 
254  return res;
255  }
256 
257  template <typename A, typename I, typename L>
258  inline
259  util::array<mln_result(A)>
260  compute(const Accumulator<A>& a_,
261  const Image<I>& input_,
262  const Image<L>& label_,
263  const mln_value(L)& nlabels)
264  {
265  mln_trace("labeling::impl::generic::compute");
266  internal::compute_tests(a_, input_, label_, nlabels);
267 
268  const A& a = exact(a_);
269  const I& input = exact(input_);
270  const L& label = exact(label_);
271 
272  util::array<A> accus(value::next(nlabels), a);
273 
274  mln_piter(I) p(input.domain());
275  for_all(p)
276  accus[label(p)].take(input(p));
277 
278  util::array<mln_result(A)> res;
279  convert::from_to(accus, res);
280 
281  return res;
282  }
283 
284  template <typename A, typename I, typename L>
285  inline
286  util::array<mln_result(A)>
287  compute(util::array<A>& accus,
288  const Image<I>& input_,
289  const Image<L>& label_,
290  const mln_value(L)& nlabels)
291  {
292  mln_trace("labeling::impl::generic::compute");
293  //internal::compute_tests(a_, input_, label_, nlabels);
294 
295  //const A& a = exact(a_);
296  const I& input = exact(input_);
297  const L& label = exact(label_);
298  (void) nlabels;
299 
300  if (value::next(nlabels) != accus.size())
301  {
302  accus.resize(0); // Make sure all the accumulators are
303  // re-initialized when resizing on next
304  // line.
305  accus.resize(value::next(nlabels));
306  }
307 
308  mln_piter(I) p(input.domain());
309  for_all(p)
310  accus[label(p)].take(input(p));
311 
312  util::array<mln_result(A)> res;
313  convert::from_to(accus, res);
314 
315  return res;
316  }
317 
318  } // end of namespace mln::labeling::impl::generic
319 
320 
321 
322  // FIXME: is there any optimization if border::get(input) ==
323  // border::get(label)) ?
324  //
325 
326  template <typename A, typename I, typename L>
327  inline
328  util::array<mln_result(A)>
329  compute_fastest(const Accumulator<A>& a_,
330  const Image<I>& input_,
331  const Image<L>& label_,
332  const mln_value(L)& nlabels)
333  {
334  mln_trace("labeling::impl::compute_fastest");
335  internal::compute_tests(a_, input_, label_, nlabels);
336 
337  const A& a = exact(a_);
338  const I& input = exact(input_);
339  const L& label = exact(label_);
340 
341  // FIXME: check image properties + add doc.
342 
343  util::array<A> accus(value::next(nlabels), a);
344 
345  unsigned ncols = geom::ncols(label);
346 
347  typedef mln_site(I) P;
348  typedef const mln_value(I) * iptr_t;
349  typedef const mln_value(L) * lptr_t;
350  box_runstart_piter<P> p(label.domain());
351  for_all(p)
352  {
353  iptr_t iptr = & input(p);
354  lptr_t lptr = & label(p);
355 
356  for (unsigned i = 0; i < ncols; ++i)
357  accus[*lptr++].take(*iptr++);
358  }
359 
360  util::array<mln_result(A)> res;
361  convert::from_to(accus, res);
362 
363  return res;
364  }
365 
366  // FIXME: is there any optimization if border::get(input) ==
367  // border::get(label)) ?
368  //
369 
370  template <typename A, typename I, typename L>
371  inline
372  util::array<mln_result(A)>
373  compute_fastest(util::array<A>& accus,
374  const Image<I>& input_,
375  const Image<L>& label_,
376  const mln_value(L)& nlabels)
377  {
378  mln_trace("labeling::impl::generic::compute_fastest");
379  //internal::compute_tests(a_, input_, label_, nlabels);
380 
381  // FIXME: check image properties + add doc.
382 
383  //const A& a = exact(a_);
384  const I& input = exact(input_);
385  const L& label = exact(label_);
386  (void) nlabels;
387 
388  if (value::next(nlabels) != accus.size())
389  {
390  accus.resize(0); // Make sure all the accumulators are
391  // re-initialized when resizing on next
392  // line.
393  accus.resize(value::next(nlabels));
394  }
395 
396  unsigned ncols = geom::ncols(label);
397 
398  typedef mln_site(I) P;
399  typedef const mln_value(I) * iptr_t;
400  typedef const mln_value(L) * lptr_t;
401  box_runstart_piter<P> p(label.domain());
402  for_all(p)
403  {
404  iptr_t iptr = & input(p);
405  lptr_t lptr = & label(p);
406 
407  for (unsigned i = 0; i < ncols; ++i)
408  accus[*lptr++].take(*iptr++);
409  }
410 
411  util::array<mln_result(A)> res;
412  convert::from_to(accus, res);
413 
414  return res;
415  }
416 
417 
418 
419  } // end of namespace mln::labeling::impl
420 
421 
422 
423  namespace internal
424  {
425 
426 
427  template <typename A, typename L>
428  inline
429  util::array<mln_result(A)>
430  compute_dispatch(const Accumulator<A>& a,
431  const Image<L>& label,
432  const mln_value(L)& nlabels)
433  {
434  return impl::generic::compute(a, label, nlabels);
435  }
436 
437 
438 
439 
440 
441  template <typename A, typename I, typename L>
442  inline
443  util::array<mln_result(A)>
444  compute_dispatch(
445  mln::trait::image::value_access::any,
446  mln::trait::image::value_access::any,
447  mln::trait::image::ext_domain::any,
448  mln::trait::image::ext_domain::any,
449  const Accumulator<A>& a,
450  const Image<I>& input,
451  const Image<L>& label,
452  const mln_value(L)& nlabels)
453  {
454  return impl::generic::compute(a, input, label, nlabels);
455  }
456 
457 
458  template <typename A, typename I, typename L>
459  inline
460  util::array<mln_result(A)>
461  compute_dispatch(
462  mln::trait::image::value_access::direct,
463  mln::trait::image::value_access::direct,
464  mln::trait::image::ext_domain::some,
465  mln::trait::image::ext_domain::some,
466  const Accumulator<A>& a,
467  const Image<I>& input,
468  const Image<L>& label,
469  const mln_value(L)& nlabels)
470  {
471  return impl::compute_fastest(a, input, label, nlabels);
472  }
473 
474 
475  template <typename A, typename I, typename L>
476  inline
477  util::array<mln_result(A)>
478  compute_dispatch(mln::trait::image::value_storage::any,
479  mln::trait::image::value_storage::any,
480  const Accumulator<A>& a,
481  const Image<I>& input,
482  const Image<L>& label,
483  const mln_value(L)& nlabels)
484  {
485  return impl::generic::compute(a, input, label, nlabels);
486  }
487 
488 
489  template <typename A, typename I, typename L>
490  inline
491  util::array<mln_result(A)>
492  compute_dispatch(mln::trait::image::value_storage::one_block,
493  mln::trait::image::value_storage::one_block,
494  const Accumulator<A>& a,
495  const Image<I>& input_,
496  const Image<L>& label_,
497  const mln_value(L)& nlabels)
498  {
499  const I& input = exact(input_);
500  const L& label = exact(label_);
501 
503  if (mlc_is(mln_trait_image_value_alignment(I),
504  trait::image::value_alignment::with_grid)::value &&
505  mlc_is(mln_trait_image_value_alignment(L),
506  trait::image::value_alignment::with_grid)::value)
507  {
508  return compute_dispatch(
509  mln_trait_image_value_access(I)(),
510  mln_trait_image_value_access(L)(),
511  mln_trait_image_ext_domain(I)(),
512  mln_trait_image_ext_domain(L)(),
513  a, input, label, nlabels);
514  }
515  else
516  return impl::generic::compute(a, input, label, nlabels);
517  }
518 
519 
520  template <typename A, typename I, typename L>
521  inline
522  util::array<mln_result(A)>
523  compute_dispatch(const Accumulator<A>& a,
524  const Image<I>& input,
525  const Image<L>& label,
526  const mln_value(L)& nlabels)
527  {
528  return compute_dispatch(mln_trait_image_value_storage(I)(),
529  mln_trait_image_value_storage(L)(),
530  a, input, label, nlabels);
531  }
532 
533 
534 
535 
536 
537 
538 
539 
540  template <typename A, typename I, typename L>
541  inline
542  util::array<mln_result(A)>
543  compute_dispatch(
544  mln::trait::image::value_access::any,
545  mln::trait::image::value_access::any,
546  mln::trait::image::ext_domain::any,
547  mln::trait::image::ext_domain::any,
548  util::array<A>& a,
549  const Image<I>& input,
550  const Image<L>& label,
551  const mln_value(L)& nlabels)
552  {
553  return impl::generic::compute(a, input, label, nlabels);
554  }
555 
556 
557  template <typename A, typename I, typename L>
558  inline
559  util::array<mln_result(A)>
560  compute_dispatch(
561  mln::trait::image::value_access::direct,
562  mln::trait::image::value_access::direct,
563  mln::trait::image::ext_domain::some,
564  mln::trait::image::ext_domain::some,
565  util::array<A>& a,
566  const Image<I>& input,
567  const Image<L>& label,
568  const mln_value(L)& nlabels)
569  {
570  return impl::compute_fastest(a, input, label, nlabels);
571  }
572 
573 
574  template <typename A, typename I, typename L>
575  inline
576  util::array<mln_result(A)>
577  compute_dispatch(mln::trait::image::value_storage::one_block,
578  mln::trait::image::value_storage::one_block,
579  util::array<A>& a,
580  const Image<I>& input_,
581  const Image<L>& label_,
582  const mln_value(L)& nlabels)
583  {
584  const I& input = exact(input_);
585  const L& label = exact(label_);
586 
588  if (mlc_is(mln_trait_image_value_alignment(I),
589  trait::image::value_alignment::with_grid)::value &&
590  mlc_is(mln_trait_image_value_alignment(L),
591  trait::image::value_alignment::with_grid)::value)
592  {
593  return compute_dispatch(
594  mln_trait_image_value_access(I)(),
595  mln_trait_image_value_access(L)(),
596  mln_trait_image_ext_domain(I)(),
597  mln_trait_image_ext_domain(L)(),
598  a, input, label, nlabels);
599  }
600  else
601  return impl::generic::compute(a, input, label, nlabels);
602  }
603 
604 
605  template <typename A, typename I, typename L>
606  inline
607  util::array<mln_result(A)>
608  compute_dispatch(util::array<A>& a,
609  const Image<I>& input,
610  const Image<L>& label,
611  const mln_value(L)& nlabels)
612  {
613  return compute_dispatch(mln_trait_image_value_storage(I)(),
614  mln_trait_image_value_storage(L)(),
615  a, input, label, nlabels);
616  }
617 
618 
619 
620 
621 
622  template <typename A, typename L>
623  inline
624  util::array<mln_result(A)>
625  compute_dispatch(util::array<A>& accus,
626  const Image<L>& label,
627  const mln_value(L)& nlabels)
628  {
629  return impl::generic::compute(accus, label, nlabels);
630  }
631 
632  } // end of namespace mln::labeling::internal
633 
634 
635 
636  // Facades.
637 
638  template <typename A, typename I, typename L>
639  inline
640  util::array<mln_result(A)>
641  compute(util::array<A>& a,
642  const Image<I>& input,
643  const Image<L>& label,
644  const mln_value(L)& nlabels)
645  {
646  mln_trace("labeling::compute");
647 
648  //internal::compute_tests(a, input, label, nlabels);
649 
650  typedef util::array<mln_result(A)> R;
651  R res = internal::compute_dispatch(a, input, label, nlabels);
652 
653  return res;
654  }
655 
656  template <typename A, typename I, typename L>
657  inline
658  util::array<mln_result(A)>
659  compute(const Accumulator<A>& a,
660  const Image<I>& input,
661  const Image<L>& label,
662  const mln_value(L)& nlabels)
663  {
664  mln_trace("labeling::compute");
665 
666  internal::compute_tests(a, input, label, nlabels);
667 
668  typedef util::array<mln_result(A)> R;
669  R res = internal::compute_dispatch(a, input, label, nlabels);
670 
671  return res;
672  }
673 
674  template <typename A, typename I, typename L>
675  inline
676  util::array<mln_meta_accu_result(A, mln_value(I))>
677  compute(const Meta_Accumulator<A>& a,
678  const Image<I>& input,
679  const Image<L>& label,
680  const mln_value(L)& nlabels)
681  {
682  typedef mln_accu_with(A, mln_value(I)) A_;
683  A_ a_ = accu::unmeta(exact(a), mln_value(I)());
684 
685  return compute(a_, input, label, nlabels);
686  }
687 
688 
689  template <typename A, typename L>
690  inline
691  util::array<mln_result(A)>
692  compute(util::array<A>& accus,
693  const Image<L>& label,
694  const mln_value(L)& nlabels)
695  {
696  mln_trace("labeling::compute");
697 
698  internal::compute_tests(A(), label, nlabels);
699 
700  typedef util::array<mln_result(A)> R;
701  R res = internal::compute_dispatch(accus, label, nlabels);
702 
703  mln_postcondition(res.nelements() == value::next(nlabels));
704 
705  return res;
706  }
707 
708 
709 
710  template <typename A, typename L>
711  inline
712  util::array<mln_result(A)>
713  compute(const Accumulator<A>& a,
714  const Image<L>& label,
715  const mln_value(L)& nlabels)
716  {
717  mln_trace("labeling::compute");
718 
719  internal::compute_tests(a, label, nlabels);
720 
721  typedef util::array<mln_result(A)> R;
722  R res = internal::compute_dispatch(a, label, nlabels);
723 
724  mln_postcondition(res.nelements() == value::next(nlabels));
725 
726  return res;
727  }
728 
729 
730  template <typename A, typename L>
731  inline
732  util::array<mln_meta_accu_result(A, mln_psite(L))>
733  compute(const Meta_Accumulator<A>& a,
734  const Image<L>& label,
735  const mln_value(L)& nlabels)
736  {
737  typedef mln_accu_with(A, mln_psite(L)) A_;
738  A_ a_ = accu::unmeta(exact(a), mln_psite(L)());
739 
740  return compute(a_, label, nlabels);
741  }
742 
743 
744 # endif // ! MLN_INCLUDE_ONLY
745 
746  } // end of namespace mln::labeling
747 
748 } // end of namespace mln
749 
750 
751 #endif // ! MLN_LABELING_COMPUTE_HH