olena: olena-2.0-815-g95c9f69 Milena: Add support for unary minus on images.

* mln/arith/minus.hh (mln::trait::set_unary_<op::uminus, Image, I>): New. (mln::operator-(const Image<I>&)) (mln::arith::uminus(const Image<I>&)) (mln::arith::impl::uminus_(trait::image::speed::any, const I&, O&)) (mln::arith::impl::uminus_(trait::image::speed::fastest, const I&, O&)): New. * tests/arith/minus.cc: Exercise unary minus. --- milena/ChangeLog | 13 ++++++++ milena/mln/arith/minus.hh | 69 +++++++++++++++++++++++++++++++++++++++++++ milena/tests/arith/minus.cc | 29 ++++++++++++------ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/milena/ChangeLog b/milena/ChangeLog index 1db0bf1..e7d1f39 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,18 @@ 2013-10-09 Roland Levillain <roland@lrde.epita.fr> + Milena: Add support for unary minus on images. + + * mln/arith/minus.hh + (mln::trait::set_unary_<op::uminus, Image, I>): New. + (mln::operator-(const Image<I>&)) + (mln::arith::uminus(const Image<I>&)) + (mln::arith::impl::uminus_(trait::image::speed::any, const I&, O&)) + (mln::arith::impl::uminus_(trait::image::speed::fastest, const I&, O&)): + New. + * tests/arith/minus.cc: Exercise unary minus. + +2013-10-09 Roland Levillain <roland@lrde.epita.fr> + Milena: Exercise the unary plus operator on images. * tests/arith/plus.cc: Here. diff --git a/milena/mln/arith/minus.hh b/milena/mln/arith/minus.hh index 334e09d..4e4b706 100644 --- a/milena/mln/arith/minus.hh +++ b/milena/mln/arith/minus.hh @@ -42,6 +42,13 @@ namespace mln namespace trait { + template <typename I> + struct set_unary_< op::uminus, Image, I > + { + typedef mln_trait_op_uminus(mln_value(I)) value; + typedef mln_ch_value(I, value) ret; + }; + template <typename L, typename R> struct set_binary_< op::minus, Image, L, Image, R > { @@ -60,6 +67,11 @@ namespace mln + template <typename I> + mln_trait_op_uminus(I) + operator-(const Image<I>& ima); + + template <typename L, typename R> mln_trait_op_minus(L,R) operator-(const Image<L>& lhs, const Image<R>& rhs); @@ -84,6 +96,16 @@ namespace mln namespace arith { + /// Negation (unary minus operation) of image \p ima. + /*! + * \param[in] ima Sole operand image. + * \result The result image. + */ + template <typename I> + mln_trait_op_uminus(I) + uminus(const Image<I>& ima); + + /// Point-wise subtraction of images \p lhs and \p rhs. /*! * \param[in] lhs First operand image. @@ -191,6 +213,18 @@ namespace mln # ifndef MLN_INCLUDE_ONLY + template <typename I> + inline + mln_trait_op_uminus(I) + operator-(const Image<I>& ima) + { + mln_trace("operator::uminus"); + + mln_trait_op_uminus(I) output = arith::uminus(ima); + + return output; + } + template <typename L, typename R> inline mln_trait_op_minus(L,R) @@ -252,6 +286,26 @@ namespace mln namespace impl { + template <typename I, typename O> + inline + void uminus_(trait::image::speed::any, const I& ima, O& output) + { + mln_piter(I) p(ima.domain()); + for_all(p) + output(p) = -ima(p); + } + + template <typename I, typename O> + inline + void uminus_(trait::image::speed::fastest, const I& ima, O& output) + { + mln_pixter(const I) ip(ima); + mln_pixter(O) op(output); + for_all_2(ip, op) + op.val() = -ip.val(); + } + + template <typename L, typename R, typename O> inline void minus_(trait::image::speed::any, const L& lhs, @@ -329,6 +383,21 @@ namespace mln // Facades. + template <typename I> + inline + mln_trait_op_uminus(I) + uminus(const Image<I>& ima) + { + mln_trace("arith::uminus"); + + mln_trait_op_uminus(I) output; + initialize(output, ima); + impl::uminus_(mln_trait_image_speed(I)(), exact(ima), output); + + return output; + } + + template <typename L, typename R> inline mln_trait_op_minus(L, R) diff --git a/milena/tests/arith/minus.cc b/milena/tests/arith/minus.cc index d04ff70..2039631 100644 --- a/milena/tests/arith/minus.cc +++ b/milena/tests/arith/minus.cc @@ -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. // @@ -24,29 +25,39 @@ // executable file might be covered by the GNU General Public License. #include <mln/core/image/image2d.hh> -#include <mln/debug/iota.hh> + #include <mln/arith/minus.hh> + +#include <mln/debug/iota.hh> #include <mln/data/compare.hh> + int main() { using namespace mln; - // debug::trace::quiet = false; + image2d<int> ima(3,3); + debug::iota(ima); + // Exercise unary minus. { - image2d<int> ima(3,3); - debug::iota(ima); + int vs[3][3] = { + {-1, -2, -3}, + {-4, -5, -6}, + {-7, -8, -9} + }; + image2d<int> ref(make::image(vs)); + mln_assertion(-ima == ref); + } + // Exercise binary minus. + { int vs[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} }; - image2d<int> ref(make::image(vs)); - - mln_assertion (ima - 1 == ref); + mln_assertion(ima - 1 == ref); } - } -- 1.7.10.4
participants (1)
-
Roland Levillain