
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2008-01-15 Simon Nivault <simon.nivault@lrde.epita.fr> Add util::lemmings. * mln/util/lemmings.hh: New. * tests/util/Makefile.am: Add the following test. * tests/util/lemmings.cc: New. --- mln/util/lemmings.hh | 128 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/util/Makefile.am | 2 tests/util/lemmings.cc | 74 ++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) Index: trunk/milena/tests/util/lemmings.cc =================================================================== --- trunk/milena/tests/util/lemmings.cc (revision 0) +++ trunk/milena/tests/util/lemmings.cc (revision 1664) @@ -0,0 +1,74 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// 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/util/lemmings.cc + * + * \brief test of mln::util::lemmings + * + */ + +#include <mln/util/lemmings.hh> +#include <mln/core/image2d.hh> + +int main () +{ + using namespace mln; + + typedef image2d<int> I; + + int vals[4][4] = {{2, 2, 6, 6}, + {2, 2, 6, 6}, + {3, 3, 4, 4}, + {3, 3, 4, 4}}; + + I ima = make::image2d<int>(vals); + + mln_point_(I) pt1(1, 0); + mln_point_(I) pt2(0, 2); + mln_point_(I) pt3(2, 3); + mln_point_(I) pt4(3, 1); + mln_point_(I) pt5(1, 1); + + mln_value_(I) vl1 = ima(pt1); + mln_value_(I) vl2 = ima(pt2); + mln_value_(I) vl3 = ima(pt3); + mln_value_(I) vl4 = ima(pt4); + mln_value_(I) vl5 = ima(pt5); + + mln_point_(I) ptl1 = util::lemmings(ima, ima.domain(), pt1, right, vl1); + mln_point_(I) ptl2 = util::lemmings(ima, ima.domain(), pt2, down, vl2); + mln_point_(I) ptl3 = util::lemmings(ima, ima.domain(), pt3, left, vl3); + mln_point_(I) ptl4 = util::lemmings(ima, ima.domain(), pt4, up, vl4); + mln_point_(I) ptl5 = util::lemmings(ima, ima.domain(), pt5, up, vl5); + + mln_assertion(ptl1 == point2d(1, 2)); + mln_assertion(ptl2 == point2d(2, 2)); + mln_assertion(ptl3 == point2d(2, 1)); + mln_assertion(ptl4 == point2d(1, 1)); + mln_assertion(ptl5 == point2d(-1, 1)); +} Index: trunk/milena/tests/util/Makefile.am =================================================================== --- trunk/milena/tests/util/Makefile.am (revision 1663) +++ trunk/milena/tests/util/Makefile.am (revision 1664) @@ -9,6 +9,7 @@ eat \ graph \ lazy_set \ + lemmings \ ordpair \ tree \ tree_fast \ @@ -21,6 +22,7 @@ eat_SOURCES = eat.cc graph_SOURCES = graph.cc lazy_set_SOURCES = lazy_set.cc +lemmings_SOURCES = lemmings.cc ordpair_SOURCES = ordpair.cc tree_SOURCES = tree.cc tree_fast_SOURCES = tree_fast.cc Index: trunk/milena/mln/util/lemmings.hh =================================================================== --- trunk/milena/mln/util/lemmings.hh (revision 0) +++ trunk/milena/mln/util/lemmings.hh (revision 1664) @@ -0,0 +1,128 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// 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_UTIL_LEMMINGS_HH +# define MLN_UTIL_LEMMINGS_HH + +/*! \file mln/util/lemmings.hh + * + * \brief Definition of an "lemmings" object. + */ + +# include <mln/core/concept/image.hh> +# include <mln/set/is_subset_of.hh> + + +namespace mln +{ + + namespace util + { + + /*! \brief Lemmings tool. + * + */ + template <typename I> + struct lemmings_ : public Object< lemmings_<I> > + { + lemmings_(const Image<I>& ima, const mln_pset(I)& domain, + const mln_point(I)& pt, const mln_dpoint(I)& dpt, + const mln_value(I)& val); + + mln_point(I) operator()(); + + const I& ima_; + const mln_pset(I)& domain_; + mln_point(I) pt_; + const mln_dpoint(I)& dpt_; + const mln_value(I)& val_; + }; + + /*! \brief Launch a lemmings on an image. + ** + ** A lemmings is the point \p pt that you put on an image \p ima + ** . This point will move through the image using the delta-point + ** \p dpt while consider his value on the given image. + ** + ** @return The first point that is not in the domain \p domain or + ** which value on the given image is different to the value \p + ** val. + ** + ** \pre The domain \p domain must be contained in the domain of \p + ** ima. + */ + template <typename I> + mln_point(I) + lemmings(const Image<I>& ima, const mln_pset(I)& domain, + const mln_point(I)& pt, const mln_dpoint(I)& dpt, + const mln_value(I)& val); + +# ifndef MLN_INCLUDE_ONLY + + // lemmings + + template <typename I> + inline + lemmings_<I>::lemmings_(const Image<I>& ima, const mln_pset(I)& domain, + const mln_point(I)& pt, const mln_dpoint(I)& dpt, + const mln_value(I)& val) + : ima_(exact(ima)), + domain_(domain), + pt_(pt), + dpt_(dpt), + val_(val) + { + mln_precondition(set::is_subset_of(domain, ima_.domain())); + } + + template <typename I> + mln_point(I) + lemmings_<I>::operator()() + { + while (domain_.has(pt_) && ima_(pt_) == val_) + pt_ += dpt_; + return pt_; + } + + template <typename I> + mln_point(I) + lemmings(const Image<I>& ima, const mln_pset(I)& domain, + const mln_point(I)& pt, const mln_dpoint(I)& dpt, + const mln_value(I)& val) + { + lemmings_<I> lm(ima, domain, pt, dpt, val); + return lm(); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::util + +} // end of namespace mln + + +#endif // ! MLN_UTIL_LEMMINGS_HH