olena: olena-2.0-816-ge037dab Milena: Introduce point-wise function bindings.

* mln/pw/bind.hh: New. * mln/pw/all.hh: Include mln/pw/bind.hh. * tests/pw/bind.cc: New. * tests/pw/Makefile.am (check_PROGRAMS): Add bind. * headers.mk, * tests/unit_test/unit-tests.mk: Regen. --- milena/ChangeLog | 12 ++ milena/headers.mk | 1 + milena/mln/pw/all.hh | 10 +- milena/mln/pw/bind.hh | 201 ++++++++++++++++++++ milena/tests/pw/Makefile.am | 5 +- .../tests/pw/bind.cc | 62 +++--- milena/tests/unit_test/unit-tests.mk | 1 + 7 files changed, 250 insertions(+), 42 deletions(-) create mode 100644 milena/mln/pw/bind.hh copy scribo/tests/primitive/extract/lines_h_discontinued.cc => milena/tests/pw/bind.cc (57%) diff --git a/milena/ChangeLog b/milena/ChangeLog index e7d1f39..c63c9e6 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,17 @@ 2013-10-09 Roland Levillain <roland@lrde.epita.fr> + Milena: Introduce point-wise function bindings. + + * mln/pw/bind.hh: New. + * mln/pw/all.hh: Include mln/pw/bind.hh. + * tests/pw/bind.cc: New. + * tests/pw/Makefile.am (check_PROGRAMS): Add bind. + * headers.mk, + * tests/unit_test/unit-tests.mk: + Regen. + +2013-10-09 Roland Levillain <roland@lrde.epita.fr> + Milena: Add support for unary minus on images. * mln/arith/minus.hh diff --git a/milena/headers.mk b/milena/headers.mk index 5321e76..7f5ba92 100644 --- a/milena/headers.mk +++ b/milena/headers.mk @@ -1013,6 +1013,7 @@ mln/opt/element.hh \ mln/opt/essential.hh \ mln/opt/value.hh \ mln/pw/all.hh \ +mln/pw/bind.hh \ mln/pw/cst.hh \ mln/pw/essential.hh \ mln/pw/image.hh \ diff --git a/milena/mln/pw/all.hh b/milena/mln/pw/all.hh index 19e559f..3355486 100644 --- a/milena/mln/pw/all.hh +++ b/milena/mln/pw/all.hh @@ -1,4 +1,5 @@ -// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2007, 2008, 2009, 2013 EPITA Research and Development +// Laboratory (LRDE). // // This file is part of Olena. // @@ -26,10 +27,8 @@ #ifndef MLN_PW_ALL_HH # define MLN_PW_ALL_HH -/*! \file - * - * \brief File that includes all "point-wise" expression tools. - */ +/// \file +/// \brief Inclusion of all "point-wise" expression tools. namespace mln @@ -41,6 +40,7 @@ namespace mln } // end of namespace mln +# include <mln/pw/bind.hh> # include <mln/pw/cst.hh> # include <mln/pw/image.hh> # include <mln/pw/value.hh> diff --git a/milena/mln/pw/bind.hh b/milena/mln/pw/bind.hh new file mode 100644 index 0000000..690b130 --- /dev/null +++ b/milena/mln/pw/bind.hh @@ -0,0 +1,201 @@ +// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE). +// +// This file is part of Olena. +// +// 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, +// 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 +// along with Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// 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. + +#ifndef MLN_PW_BIND_HH +# define MLN_PW_BIND_HH + +/// \file +/// \brief Definition of point-wise function bindings. + +# include <mln/core/concept/function.hh> +# include <mln/fun/internal/selector.hh> + + +namespace mln +{ + + namespace pw + { + + /*-------------------------. + | Unary function binding. | + `-------------------------*/ + + /// \brief Point-wise unary function binding. + /// + /// This point-wise class is actually a function object embedding + /// a Milena function (functor) and another point-wise expression. + /// Its operator() returns the application of the embedded + /// function to the embedded point-wise expression. + template <typename F, typename PW> + struct unary_bound_fun_ + : fun::internal::selector_from_result_< typename F::result, + unary_bound_fun_<F, PW> >::ret + { + typedef typename F::result result; + + unary_bound_fun_(const Function_v2v<F>& f, const Function_v2v<PW>& pw); + + template <typename P> + result operator()(const P& p) const; + + protected: + /// The function to apply. + const F f_; + /// The point-wise expression. + const PW pw_; + }; + + + /// \brief Bind the unary function \a f to the point-wise + /// expression \a pw. + template <typename F, typename PW> + unary_bound_fun_<F, PW> + bind(const Function_v2v<F>& f, const Function_v2v<PW>& pw); + + + + /*--------------------------. + | Binary function binding. | + `--------------------------*/ + + /// \brief Point-wise binary function binding. + /// + /// This point-wise class is actually a function object embedding + /// a Milena function (functor) and another point-wise expression. + /// Its operator() returns the application of the embedded + /// function to the embedded point-wise expression. + template <typename F, typename PW1, typename PW2> + struct binary_bound_fun_ + : fun::internal::selector_from_result_< typename F::result, + binary_bound_fun_<F, + PW1, + PW2> >::ret + { + typedef typename F::result result; + + binary_bound_fun_(const Function_vv2v<F>& f, + const Function_v2v<PW1>& pw1, + const Function_v2v<PW2>& pw2); + + template <typename P> + result operator()(const P& p) const; + + protected: + /// The function to apply. + const F f_; + /// The first point-wise expression. + const PW1 pw1_; + /// The second point-wise expression. + const PW2 pw2_; + }; + + + /// \brief Bind the binary function \a f to the point-wise + /// expressions \a pw1 and \a pw2. + template <typename F, typename PW1, typename PW2> + binary_bound_fun_<F, PW1, PW2> + bind(const Function_vv2v<F>& f, + const Function_v2v<PW1>& pw1, const Function_v2v<PW2>& pw2); + + + +# ifndef MLN_INCLUDE_ONLY + + // pw::unary_bound_fun_<F, PW>. + + template <typename F, typename PW> + inline + unary_bound_fun_<F, PW>::unary_bound_fun_(const Function_v2v<F>& f, + const Function_v2v<PW>& pw) + : f_(exact(f)), + pw_(exact(pw)) + { + } + + template <typename F, typename PW> + template <typename P> + inline + typename F::result + unary_bound_fun_<F, PW>::operator()(const P& p) const + { + return f_(pw_(p)); + } + + + // pw::bind(f, pw). + + template <typename F, typename PW> + inline + unary_bound_fun_<F, PW> + bind(const Function_v2v<F>& f, const Function_v2v<PW>& pw) + { + return unary_bound_fun_<F, PW>(f, pw); + } + + + + // pw::binary_bound_fun_<F, PW1, PW2>. + + template <typename F, typename PW1, typename PW2> + inline + binary_bound_fun_<F, PW1, PW2>::binary_bound_fun_(const Function_vv2v<F>& f, + const Function_v2v<PW1>& pw1, + const Function_v2v<PW2>& pw2) + : f_(exact(f)), + pw1_(exact(pw1)), + pw2_(exact(pw2)) + { + } + + template <typename F, typename PW1, typename PW2> + template <typename P> + inline + typename F::result + binary_bound_fun_<F, PW1, PW2>::operator()(const P& p) const + { + return f_(pw1_(p), pw2_(p)); + } + + + // pw::bind(f, pw1, pw2). + + template <typename F, typename PW1, typename PW2> + inline + binary_bound_fun_<F, PW1, PW2> + bind(const Function_vv2v<F>& f, + const Function_v2v<PW1>& pw1, const Function_v2v<PW2>& pw2) + { + return binary_bound_fun_<F, PW1, PW2>(f, pw1, pw2); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::pw + +} // end of namespace mln + + +#endif // ! MLN_PW_BIND_HH diff --git a/milena/tests/pw/Makefile.am b/milena/tests/pw/Makefile.am index 16bfa30..84e472b 100644 --- a/milena/tests/pw/Makefile.am +++ b/milena/tests/pw/Makefile.am @@ -1,5 +1,5 @@ -# Copyright (C) 2007, 2008, 2009, 2010 EPITA Research and Development -# Laboratory (LRDE) +# Copyright (C) 2007, 2008, 2009, 2010, 2013 EPITA Research and +# Development Laboratory (LRDE). # # This file is part of Olena. # @@ -18,6 +18,7 @@ include $(top_srcdir)/milena/tests/tests.mk check_PROGRAMS = \ + bind \ image \ value diff --git a/scribo/tests/primitive/extract/lines_h_discontinued.cc b/milena/tests/pw/bind.cc similarity index 57% copy from scribo/tests/primitive/extract/lines_h_discontinued.cc copy to milena/tests/pw/bind.cc index 73d457d..9095209 100644 --- a/scribo/tests/primitive/extract/lines_h_discontinued.cc +++ b/milena/tests/pw/bind.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE). // // This file is part of Olena. // @@ -23,47 +23,39 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -// \file +/// \file +/// \brief Exercise mln::pw::bind with unary and binary functions +/// bound to point-wise expressions. #include <mln/core/image/image2d.hh> -#include <mln/core/alias/neighb2d.hh> -#include <mln/win/hline2d.hh> -#include <mln/io/pbm/load.hh> -#include <mln/value/label_8.hh> -#include <mln/make/box2d.hh> -#include <scribo/primitive/extract/lines_h_discontinued.hh> -#include "tests/data.hh" +#include <mln/pw/all.hh> +#include <mln/fun/v2v/abs.hh> +#include <mln/fun/vv2v/max.hh> + +#include <mln/arith/minus.hh> +#include <mln/debug/iota.hh> +#include <mln/test/predicate.hh> int main() { using namespace mln; - using namespace scribo; - - image2d<bool> input; - io::pbm::load(input, SCRIBO_IMG_DIR "/lines_discontinued.pbm"); - - typedef value::label_8 V; - typedef image2d<V> I; - - // Horizontal - { - V nlines; - component_set<I> - comps = primitive::extract::lines_h_discontinued(input, c4(), nlines, - 51, 3); - - mln_assertion(comps.nelements() == 2); - - mln_assertion(comps(1).bbox() == make::box2d(21, 37, 24, 176)); - mln_assertion(comps(1).mass_center() == point2d(23, 107)); - mln_assertion(comps(1).card() == 560); - - mln_assertion(comps(2).bbox() == make::box2d(94, 32, 97, 91)); - mln_assertion(comps(2).mass_center() == point2d(96, 62)); - mln_assertion(comps(2).card() == 240); - } - + image2d<int> pos_ints(3, 3); + debug::iota(pos_ints); + image2d<int> neg_ints = -pos_ints; + + // Exercise unary point-wise function binding. + mln_assertion(test::predicate(pos_ints.domain(), + pw::bind(fun::v2v::abs<int>(), + pw::value(neg_ints)) + == pw::value(pos_ints))); + + // Exercise binary point-wise function binding. + mln_assertion(test::predicate(pos_ints.domain(), + pw::bind(fun::vv2v::max<int>(), + pw::value(pos_ints), + pw::value(neg_ints)) + == pw::value(pos_ints))); } diff --git a/milena/tests/unit_test/unit-tests.mk b/milena/tests/unit_test/unit-tests.mk index c95e225..6b9fe56 100644 --- a/milena/tests/unit_test/unit-tests.mk +++ b/milena/tests/unit_test/unit-tests.mk @@ -1065,6 +1065,7 @@ mln_opt_element \ mln_opt_essential \ mln_opt_value \ mln_pw_all \ +mln_pw_bind \ mln_pw_cst \ mln_pw_essential \ mln_pw_image \ -- 1.7.10.4
participants (1)
-
Roland Levillain