3060: Add an opt::at function.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr> Add an opt::at function. * mln/core/image/cast_image.hh: Fix properties. * mln/opt: New directory. * mln/opt/at.hh: New function opt::at. * mln/level/paste.spe.hh: Fix a bug. * tests/opt, * tests/opt/at.cc: New tests. mln/core/image/cast_image.hh | 1 mln/level/paste.spe.hh | 2 mln/opt/at.hh | 337 +++++++++++++++++++++++++++++++++++++++++++ tests/opt/at.cc | 157 ++++++++++++++++++++ 4 files changed, 496 insertions(+), 1 deletion(-) Index: mln/core/image/cast_image.hh --- mln/core/image/cast_image.hh (revision 3059) +++ mln/core/image/cast_image.hh (working copy) @@ -99,6 +99,7 @@ trait::image::quant::high, trait::image::quant::low) quant; + typedef trait::image::category::value_morpher category; typedef trait::image::value_io::read_only value_io; typedef trait::image::pw_io::read pw_io; typedef trait::image::value_access::indirect value_access; Index: mln/opt/at.hh --- mln/opt/at.hh (revision 0) +++ mln/opt/at.hh (revision 0) @@ -0,0 +1,337 @@ +// Copyright (C) 2008 EPITA Research and Development Laboratory +// (LRDE) +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library 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 this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library 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_OPT_AT_HH +# define MLN_OPT_AT_HH + +/// \file mln/opt/at.hh +/// +/// FIXME + +# include <mln/core/concept/image.hh> +# include <mln/trait/images.hh> +# include <mln/trace/all.hh> + +# include <mln/core/alias/point1d.hh> +# include <mln/core/alias/point2d.hh> +# include <mln/core/alias/point3d.hh> + +namespace mln +{ + + namespace opt + { + + /// One dimension + template <typename I> + mln_rvalue(I) at(const Image<I>& ima, int ind); + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int ind); + + + /// Two dimensions + template <typename I> + mln_rvalue(I) at(const Image<I>& ima, int row, int col); + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int row, int col); + + /// Three dimensions + template <typename I> + mln_rvalue(I) at(const Image<I>& ima, int sli, int row, int col); + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int sli, int row, int col); + + +# ifndef MLN_INCLUDE_ONLY + + + /// One dimension + namespace impl + { + + template <typename I> + inline + mln_rvalue(I) at_1d_impl(trait::image::category::domain_morpher, + const Image<I>& ima, int ind) + { + point1d p(ind); + return exact(ima)(p); + } + + template <typename I> + inline + mln_rvalue(I) at_1d_impl(trait::image::category::morpher, + const Image<I>& ima, int ind) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), ind); + } + + template <typename I> + inline + mln_rvalue(I) at_1d_impl(trait::image::category::primary, + const Image<I>& ima, int ind) + { + return exact(ima).at(ind); + } + + + template <typename I> + inline + mln_lvalue(I) at_1d_impl(trait::image::category::domain_morpher, + Image<I>& ima, int ind) + { + mlc_is(mln_trait_image_pw_io(I), + trait::image::pw_io::read_write)::check(); + + point1d p(ind); + return exact(ima)(p); + } + + template <typename I> + inline + mln_lvalue(I) at_1d_impl(trait::image::category::morpher, + Image<I>& ima, int ind) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), ind); + } + + template <typename I> + inline + mln_lvalue(I) at_1d_impl(trait::image::category::primary, + Image<I>& ima, int ind) + { + return exact(ima).at(ind); + } + + } // end of namespace mln::opt::impl + + template <typename I> + inline + mln_rvalue(I) at(const Image<I>& ima, int ind) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::one_d)::check(); + + return impl::at_1d_impl(mln_trait_image_category(I)(), ima, ind); + } + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int ind) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::one_d)::check(); + + return impl::at_1d_impl(mln_trait_image_category(I)(), ima, ind); + } + + + + /// Two dimensions + namespace impl + { + + template <typename I> + inline + mln_rvalue(I) at_2d_impl(trait::image::category::domain_morpher, + const Image<I>& ima, int row, int col) + { + point2d p(row, col); + return exact(ima)(p); + } + + template <typename I> + inline + mln_rvalue(I) at_2d_impl(trait::image::category::morpher, + const Image<I>& ima, int row, int col) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), row, col); + } + + template <typename I> + inline + mln_rvalue(I) at_2d_impl(trait::image::category::primary, + const Image<I>& ima, int row, int col) + { + return exact(ima).at(row, col); + } + + + template <typename I> + inline + mln_lvalue(I) at_2d_impl(trait::image::category::domain_morpher, + Image<I>& ima, int row, int col) + { + mlc_is(mln_trait_image_pw_io(I), + trait::image::pw_io::read_write)::check(); + + point2d p(row, col); + return exact(ima)(p); + } + + template <typename I> + inline + mln_lvalue(I) at_2d_impl(trait::image::category::morpher, + Image<I>& ima, int row, int col) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), row, col); + } + + template <typename I> + inline + mln_lvalue(I) at_2d_impl(trait::image::category::primary, + Image<I>& ima, int row, int col) + { + return exact(ima).at(row, col); + } + + } // end of namespace mln::opt::impl + + template <typename I> + inline + mln_rvalue(I) at(const Image<I>& ima, int row, int col) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::two_d)::check(); + + return impl::at_2d_impl(mln_trait_image_category(I)(), ima, row, col); + } + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int row, int col) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::two_d)::check(); + + return impl::at_2d_impl(mln_trait_image_category(I)(), ima, row, col); + } + + + /// Three dimensions + namespace impl + { + + template <typename I> + inline + mln_rvalue(I) at_3d_impl(trait::image::category::domain_morpher, + const Image<I>& ima, int sli, int row, int col) + { + point3d p(sli, row, col); + return exact(ima)(p); + } + + template <typename I> + inline + mln_rvalue(I) at_3d_impl(trait::image::category::morpher, + const Image<I>& ima, int sli, int row, int col) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), sli, row, col); + } + + template <typename I> + inline + mln_rvalue(I) at_3d_impl(trait::image::category::primary, + const Image<I>& ima, int sli, int row, int col) + { + return exact(ima).at(sli, row, col); + } + + + template <typename I> + inline + mln_lvalue(I) at_3d_impl(trait::image::category::domain_morpher, + Image<I>& ima, int sli, int row, int col) + { + mlc_is(mln_trait_image_pw_io(I), + trait::image::pw_io::read_write)::check(); + + point3d p(sli, row, col); + return exact(ima)(p); + } + + template <typename I> + inline + mln_lvalue(I) at_3d_impl(trait::image::category::morpher, + Image<I>& ima, int sli, int row, int col) + { + // FIXME: what about morpher that modify the image value? + // (through a function for instance) + return at(*exact(ima).delegatee_(), sli, row, col); + } + + template <typename I> + inline + mln_lvalue(I) at_3d_impl(trait::image::category::primary, + Image<I>& ima, int sli, int row, int col) + { + return exact(ima).at(sli, row, col); + } + + } // end of namespace mln::opt::impl + + template <typename I> + inline + mln_rvalue(I) at(const Image<I>& ima, int sli, int row, int col) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::three_d)::check(); + + return impl::at_3d_impl(mln_trait_image_category(I)(), + ima, sli, row, col); + } + + template <typename I> + mln_lvalue(I) at(Image<I>& ima, int sli, int row, int col) + { + mlc_is(mln_trait_image_dimension(I), + trait::image::dimension::three_d)::check(); + + return impl::at_3d_impl(mln_trait_image_category(I)(), + ima, sli, row, col); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::opt + +} // end of namespace mln + + +#endif // ! MLN_OPT_AT_HH Index: mln/level/paste.spe.hh --- mln/level/paste.spe.hh (revision 3059) +++ mln/level/paste.spe.hh (working copy) @@ -143,7 +143,7 @@ const I& input = exact(input_); - level::fill_with_value(output_, input.val()); + level::fill_with_value((output_ | input.domain()).rw(), input.val()); trace::exiting("level::impl::paste_singleton"); } Index: tests/opt/at.cc --- tests/opt/at.cc (revision 0) +++ tests/opt/at.cc (revision 0) @@ -0,0 +1,157 @@ +// Copyright (C) 2008 EPITA Research and Development Laboratory +// (LRDE) +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library 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 this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library 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. + +/// \file tests/opt/at.cc +/// +/// Tests on mln::opt::at. + +#include <mln/core/image/image1d.hh> +#include <mln/core/image/sub_image.hh> +#include <mln/core/image/cast_image.hh> + +/*#include <mln/core/image/image2d.hh> +#include <mln/core/image/image3d.hh> +#include <mln/pw/image.hh> +#include <mln/core/image/flat_image.hh> +#include <mln/core/image/image_if.hh> +#include <mln/core/image/extension_val.hh>*/ + +#include <mln/level/fill.hh> +#include <mln/level/paste.hh> +#include <mln/level/compare.hh> + +#include <mln/opt/at.hh> + +#include <mln/debug/iota.hh> +#include <mln/debug/println.hh> +#include <mln/trace/all.hh> + + +int main() +{ + using namespace mln; + const unsigned size = 50; + + /// One dimensions tests... + { + image1d<short> ima(size); + debug::iota(ima); + const image1d<short> cima = ima; + + point1d p(5); + mln_assertion(cima(p) == opt::at(cima, 5)); + + opt::at(ima, 5) = 12; + mln_assertion(cima(p) == 12); + } + { + typedef image1d<short> I; + typedef sub_image< image1d<short>, box1d > II; + + I ima(size); + II sub_ima(ima, make::box1d(4, 10)); + const II csub_ima(ima, make::box1d(4, 10)); + point1d p(5); + + level::fill(ima, 51); + mln_assertion(csub_ima(p) == opt::at(csub_ima, 5)); + opt::at(sub_ima, 5) = 12; + mln_assertion(sub_ima(p) == 12); + } + { + typedef image1d<unsigned short> I; + typedef cast_image_<int, I> II; + + I in(size, size); + II cast(in); + const II ccast(in); + point1d p(5); + + level::fill(in, 51); + mln_assertion(ccast(p) == opt::at(ccast, 5)); + // FIXME + //opt::at(cast, 5) = 12; + //mln_assertion(cast(p) == 12); + } + + /// Two dimensions tests... + { + image2d<short> ima(size, size); + debug::iota(ima); + const image2d<short> cima = ima; + + point2d p(5, 5); + mln_assertion(cima(p) == opt::at(cima, 5, 5)); + + opt::at(ima, 5, 5) = 12; + mln_assertion(cima(p) == 12); + } + { + typedef image2d<short> I; + typedef sub_image< image2d<short>, box2d > II; + + I ima(size, size); + II sub_ima(ima, make::box2d(4,4, 10, 10)); + const II csub_ima(ima, make::box2d(4, 4, 10, 10)); + point2d p(5, 5); + + level::fill(ima, 51); + mln_assertion(csub_ima(p) == opt::at(csub_ima, 5, 5)); + opt::at(sub_ima, 5, 5) = 12; + mln_assertion(sub_ima(p) == 12); + } + { + typedef image2d<unsigned short> I; + typedef cast_image_<int, I> II; + + I in(size, size); + II cast(in); + const II ccast(in); + point2d p(5,5); + + level::fill(in, 51); + mln_assertion(ccast(p) == opt::at(ccast, 5, 5)); + // FIXME + //opt::at(cast, 5) = 12; + //mln_assertion(cast(p) == 12); + } + + + /// Three dimensions tests... + { + image3d<short> ima(size, size, size); + debug::iota(ima); + const image3d<short> cima = ima; + + point3d p(5, 5, 5); + mln_assertion(cima(p) == opt::at(cima, 5, 5, 5)); + + opt::at(ima, 5, 5, 5) = 12; + mln_assertion(cima(p) == 12); + } +}
participants (1)
-
Nicolas Ballas