27 #ifndef MLN_LABELING_COMPUTE_HH
28 # define MLN_LABELING_COMPUTE_HH
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>
72 template <
typename A,
typename I,
typename L>
73 util::array<mln_result(A)>
74 compute(
const Accumulator<A>& a,
75 const Image<I>& input,
76 const Image<L>& label,
77 const mln_value(L)& nlabels);
89 template <
typename A,
typename I,
typename L>
90 util::array<mln_meta_accu_result(A, mln_value(I))>
91 compute(
const Meta_Accumulator<A>& a,
92 const Image<I>& input,
93 const Image<L>& label,
94 const mln_value(L)& nlabels);
105 template <
typename A,
typename L>
106 util::array<mln_result(A)>
107 compute(
const Accumulator<A>& a,
108 const Image<L>& label,
109 const mln_value(L)& nlabels);
120 template <
typename A,
typename L>
121 util::array<mln_meta_accu_result(A, mln_psite(L))>
122 compute(
const Meta_Accumulator<A>& a,
123 const Image<L>& label,
124 const mln_value(L)& nlabels);
136 template <
typename A,
typename I,
typename L>
137 util::array<mln_result(A)>
139 const Image<I>& input,
140 const Image<L>& label,
141 const mln_value(L)& nlabels);
145 # ifndef MLN_INCLUDE_ONLY
150 template <
typename A,
typename L>
153 compute_tests(
const Accumulator<A>& a,
154 const Image<L>& label,
155 const mln_value(L)& nlabels)
157 mln_precondition(exact(label).is_valid());
165 template <
typename A,
typename I,
typename L>
168 compute_tests(
const Accumulator<A>& a,
169 const Image<I>& input,
170 const Image<L>& label,
171 const mln_value(L)& nlabels)
173 mln_precondition(exact(input).is_valid());
174 mln_precondition(exact(label).is_valid());
201 template <
typename A,
typename L>
203 util::array<mln_result(A)>
206 const mln_value(L)& nlabels)
208 trace::entering(
"labeling::impl::generic::compute");
209 internal::compute_tests(a_, label_, nlabels);
211 const A& a = exact(a_);
212 const L& label = exact(label_);
216 mln_piter(L) p(label.domain());
218 accus[label(p)].take(p);
221 convert::from_to(accus, res);
223 trace::exiting(
"labeling::impl::generic::compute");
238 template <
typename A,
typename L>
243 const mln_value(L)& nlabels)
245 trace::entering(
"labeling::impl::generic::compute");
246 internal::compute_tests(A(), label_, nlabels);
248 if (value::next(nlabels) != accus.
size())
253 accus.
resize(value::next(nlabels));
256 const L& label = exact(label_);
258 mln_piter(L) p(label.domain());
260 accus[label(p)].take(p);
263 convert::from_to(accus, res);
265 trace::exiting(
"labeling::impl::generic::compute");
280 template <
typename A,
typename I,
typename L>
286 const mln_value(L)& nlabels)
288 trace::entering(
"labeling::impl::generic::compute");
289 internal::compute_tests(a_, input_, label_, nlabels);
291 const A& a = exact(a_);
292 const I& input = exact(input_);
293 const L& label = exact(label_);
297 mln_piter(I) p(input.domain());
299 accus[label(p)].take(input(p));
302 convert::from_to(accus, res);
304 trace::exiting(
"labeling::impl::generic::compute");
318 template <
typename A,
typename I,
typename L>
324 const mln_value(L)& nlabels)
326 trace::entering(
"labeling::impl::generic::compute");
330 const I& input = exact(input_);
331 const L& label = exact(label_);
334 if (value::next(nlabels) != accus.
size())
339 accus.
resize(value::next(nlabels));
342 mln_piter(I) p(input.domain());
344 accus[label(p)].take(input(p));
347 convert::from_to(accus, res);
349 trace::exiting(
"labeling::impl::generic::compute");
370 template <
typename A,
typename I,
typename L>
376 const mln_value(L)& nlabels)
378 trace::entering(
"labeling::impl::compute_fastest");
379 internal::compute_tests(a_, input_, label_, nlabels);
381 const A& a = exact(a_);
382 const I& input = exact(input_);
383 const L& label = exact(label_);
391 typedef mln_site(I) P;
392 typedef const mln_value(I) * iptr_t;
393 typedef const mln_value(L) * lptr_t;
397 iptr_t iptr = & input(p);
398 lptr_t lptr = & label(p);
400 for (
unsigned i = 0; i < ncols; ++i)
401 accus[*lptr++].take(*iptr++);
405 convert::from_to(accus, res);
407 trace::exiting(
"labeling::impl::generic::compute_fastest");
424 template <
typename A,
typename I,
typename L>
430 const mln_value(L)& nlabels)
432 trace::entering(
"labeling::impl::generic::compute_fastest");
438 const I& input = exact(input_);
439 const L& label = exact(label_);
442 if (value::next(nlabels) != accus.
size())
447 accus.
resize(value::next(nlabels));
452 typedef mln_site(I) P;
453 typedef const mln_value(I) * iptr_t;
454 typedef const mln_value(L) * lptr_t;
458 iptr_t iptr = & input(p);
459 lptr_t lptr = & label(p);
461 for (
unsigned i = 0; i < ncols; ++i)
462 accus[*lptr++].take(*iptr++);
466 convert::from_to(accus, res);
468 trace::exiting(
"labeling::impl::generic::compute_fastest");
482 template <
typename A,
typename L>
487 const mln_value(L)& nlabels)
496 template <
typename A,
typename I,
typename L>
498 util::array<mln_result(A)>
500 mln::trait::image::value_access::any,
501 mln::trait::image::value_access::any,
502 mln::trait::image::ext_domain::any,
503 mln::trait::image::ext_domain::any,
504 const Accumulator<A>& a,
505 const Image<I>& input,
506 const Image<L>& label,
507 const mln_value(L)& nlabels)
513 template <
typename A,
typename I,
typename L>
515 util::array<mln_result(A)>
517 mln::trait::image::value_access::direct,
518 mln::trait::image::value_access::direct,
519 mln::trait::image::ext_domain::some,
520 mln::trait::image::ext_domain::some,
521 const Accumulator<A>& a,
522 const Image<I>& input,
523 const Image<L>& label,
524 const mln_value(L)& nlabels)
530 template <
typename A,
typename I,
typename L>
532 util::array<mln_result(A)>
533 compute_dispatch(mln::trait::image::value_storage::any,
534 mln::trait::image::value_storage::any,
535 const Accumulator<A>& a,
536 const Image<I>& input,
537 const Image<L>& label,
538 const mln_value(L)& nlabels)
544 template <
typename A,
typename I,
typename L>
546 util::array<mln_result(A)>
547 compute_dispatch(mln::trait::image::value_storage::one_block,
548 mln::trait::image::value_storage::one_block,
549 const Accumulator<A>& a,
550 const Image<I>& input_,
551 const Image<L>& label_,
552 const mln_value(L)& nlabels)
554 const I& input = exact(input_);
555 const L& label = exact(label_);
558 if (mlc_is(mln_trait_image_value_alignment(I),
559 trait::image::value_alignment::with_grid)::
value &&
560 mlc_is(mln_trait_image_value_alignment(L),
561 trait::image::value_alignment::with_grid)::
value)
563 return compute_dispatch(
564 mln_trait_image_value_access(I)(),
565 mln_trait_image_value_access(L)(),
566 mln_trait_image_ext_domain(I)(),
567 mln_trait_image_ext_domain(L)(),
568 a, input, label, nlabels);
575 template <
typename A,
typename I,
typename L>
577 util::array<mln_result(A)>
578 compute_dispatch(
const Accumulator<A>& a,
579 const Image<I>& input,
580 const Image<L>& label,
581 const mln_value(L)& nlabels)
583 return compute_dispatch(mln_trait_image_value_storage(I)(),
584 mln_trait_image_value_storage(L)(),
585 a, input, label, nlabels);
595 template <
typename A,
typename I,
typename L>
597 util::array<mln_result(A)>
599 mln::trait::image::value_access::any,
600 mln::trait::image::value_access::any,
601 mln::trait::image::ext_domain::any,
602 mln::trait::image::ext_domain::any,
604 const Image<I>& input,
605 const Image<L>& label,
606 const mln_value(L)& nlabels)
612 template <
typename A,
typename I,
typename L>
614 util::array<mln_result(A)>
616 mln::trait::image::value_access::direct,
617 mln::trait::image::value_access::direct,
618 mln::trait::image::ext_domain::some,
619 mln::trait::image::ext_domain::some,
621 const Image<I>& input,
622 const Image<L>& label,
623 const mln_value(L)& nlabels)
629 template <
typename A,
typename I,
typename L>
631 util::array<mln_result(A)>
632 compute_dispatch(mln::trait::image::value_storage::one_block,
633 mln::trait::image::value_storage::one_block,
635 const Image<I>& input_,
636 const Image<L>& label_,
637 const mln_value(L)& nlabels)
639 const I& input = exact(input_);
640 const L& label = exact(label_);
643 if (mlc_is(mln_trait_image_value_alignment(I),
644 trait::image::value_alignment::with_grid)::value &&
645 mlc_is(mln_trait_image_value_alignment(L),
646 trait::image::value_alignment::with_grid)::value)
648 return compute_dispatch(
649 mln_trait_image_value_access(I)(),
650 mln_trait_image_value_access(L)(),
651 mln_trait_image_ext_domain(I)(),
652 mln_trait_image_ext_domain(L)(),
653 a, input, label, nlabels);
660 template <
typename A,
typename I,
typename L>
662 util::array<mln_result(A)>
663 compute_dispatch(util::array<A>& a,
664 const Image<I>& input,
665 const Image<L>& label,
666 const mln_value(L)& nlabels)
668 return compute_dispatch(mln_trait_image_value_storage(I)(),
669 mln_trait_image_value_storage(L)(),
670 a, input, label, nlabels);
677 template <
typename A,
typename L>
679 util::array<mln_result(A)>
680 compute_dispatch(util::array<A>& accus,
681 const Image<L>& label,
682 const mln_value(L)& nlabels)
693 template <
typename A,
typename I,
typename L>
695 util::array<mln_result(A)>
699 const mln_value(L)& nlabels)
701 trace::entering(
"labeling::compute");
706 R res = internal::compute_dispatch(a, input, label, nlabels);
708 trace::exiting(
"labeling::compute");
712 template <
typename A,
typename I,
typename L>
718 const mln_value(L)& nlabels)
720 trace::entering(
"labeling::compute");
722 internal::compute_tests(a, input, label, nlabels);
725 R res = internal::compute_dispatch(a, input, label, nlabels);
727 trace::exiting(
"labeling::compute");
731 template <
typename A,
typename I,
typename L>
737 const mln_value(L)& nlabels)
739 typedef mln_accu_with(A, mln_value(I)) A_;
740 A_ a_ = accu::unmeta(exact(a), mln_value(I)());
742 return compute(a_, input, label, nlabels);
746 template <
typename A,
typename L>
751 const mln_value(L)& nlabels)
753 trace::entering(
"labeling::compute");
755 internal::compute_tests(A(), label, nlabels);
758 R res = internal::compute_dispatch(accus, label, nlabels);
760 mln_postcondition(res.nelements() == value::next(nlabels));
762 trace::exiting(
"labeling::compute");
768 template <
typename A,
typename L>
770 util::array<mln_result(A)>
773 const mln_value(L)& nlabels)
775 trace::entering(
"labeling::compute");
777 internal::compute_tests(a, label, nlabels);
780 R res = internal::compute_dispatch(a, label, nlabels);
782 mln_postcondition(res.nelements() == value::next(nlabels));
784 trace::exiting(
"labeling::compute");
789 template <
typename A,
typename L>
794 const mln_value(L)& nlabels)
796 typedef mln_accu_with(A, mln_psite(L)) A_;
797 A_ a_ = accu::unmeta(exact(a), mln_psite(L)());
799 return compute(a_, label, nlabels);
803 # endif // ! MLN_INCLUDE_ONLY
810 #endif // ! MLN_LABELING_COMPUTE_HH