milena r2463: Add a generic snake browsing with some examples

URL: https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena ChangeLog: 2008-10-01 Matthieu Garrigues <garrigues@lrde.epita.fr> Add a generic snake browsing with some examples. * mln/canvas/browsing/snake_generic.hh: New, generic snake. Works on any dimension and follows the dimensions in any order. * tests/canvas/browsing/snake_generic_2d_hori.cc: New, * tests/canvas/browsing/snake_generic_2d_vert.cc: New, * tests/canvas/browsing/snake_generic_3d_hori.cc: New, * tests/canvas/browsing/snake_generic_3d_vert.cc: New, examples * mln/canvas/browsing/directional.hh: Documentation. --- mln/canvas/browsing/directional.hh | 15 ++ mln/canvas/browsing/snake_generic.hh | 163 +++++++++++++++++++++++++ tests/canvas/browsing/snake_generic_2d_hori.cc | 106 ++++++++++++++++ tests/canvas/browsing/snake_generic_2d_vert.cc | 106 ++++++++++++++++ tests/canvas/browsing/snake_generic_3d_hori.cc | 114 +++++++++++++++++ tests/canvas/browsing/snake_generic_3d_vert.cc | 114 +++++++++++++++++ 6 files changed, 618 insertions(+) Index: branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_vert.cc =================================================================== --- branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_vert.cc (revision 0) +++ branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_vert.cc (revision 2463) @@ -0,0 +1,106 @@ +// Copyright (C) 2007, 2008 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/canvas/browsing/snake_generic_2d_vert.cc + * + * \brief Tests on mln::canvas::browsing::snake_generic. + */ + +#include <mln/core/image/image2d.hh> +#include <mln/canvas/browsing/snake_generic.hh> +#include <mln/fun/p2v/iota.hh> +#include <mln/debug/println.hh> + +template <typename I, typename F> +struct assign_browsing_functor +{ + enum { dim = I::site::dim }; + + typedef assign_browsing_functor<I, F> self; + typedef mln_deduce(I, psite, delta) dpsite; + typedef void (assign_browsing_functor<I,F>::*move_fun)(); + + I input; + F f; + std::vector<move_fun> moves; + std::vector<dpsite> dps; + + assign_browsing_functor(I& input, F f = F()) + : input(input), + f(f), + moves(3), + dps(3) + { + dps[0] = dpsite(0, 1); + dps[1] = dpsite(1, 0); + dps[2] = dpsite(-1, 0); + moves[0] = &self::fwd; + moves[1] = &self::down; + moves[2] = &self::up; + } + + mln_psite(I) p; + + void init() {} + void final() {} + void next() + { + input(p) = f(p); + } + void fwd() { std::cout << "fwd" << std::endl; next(); } + void up() { std::cout << "up" << std::endl; next(); } + void down() { std::cout << "down" << std::endl; next(); } +}; + +namespace mln +{ + + template <typename I, typename F, typename B> + void my_test(Image<I>& ima_, + const Function_p2v<F>& f_, + const Browsing<B>& browse_) + { + I& ima = exact(ima_); + const F& f = exact(f_); + const B& browse = exact(browse_); + + assign_browsing_functor<I, F> fun(ima, f); + browse(fun); + } + +} + + +int main() +{ + using namespace mln; + image2d<unsigned> ima2(3, 3); + + std::cout << ima2.bbox() << std::endl; + my_test(ima2, fun::p2v::iota, canvas::browsing::snake_generic); + debug::println(ima2); +} Index: branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_hori.cc =================================================================== --- branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_hori.cc (revision 0) +++ branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_2d_hori.cc (revision 2463) @@ -0,0 +1,106 @@ +// Copyright (C) 2007, 2008 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/canvas/browsing/snake_generic_3d_hori.cc + * + * \brief Tests on mln::canvas::browsing::snake_generic. + */ + +#include <mln/core/image/image2d.hh> +#include <mln/canvas/browsing/snake_generic.hh> +#include <mln/fun/p2v/iota.hh> +#include <mln/debug/println.hh> + +template <typename I, typename F> +struct assign_browsing_functor +{ + enum { dim = I::site::dim }; + + typedef assign_browsing_functor<I, F> self; + typedef mln_deduce(I, psite, delta) dpsite; + typedef void (assign_browsing_functor<I,F>::*move_fun)(); + + I input; + F f; + std::vector<move_fun> moves; + std::vector<dpsite> dps; + + assign_browsing_functor(I& input, F f = F()) + : input(input), + f(f), + moves(3), + dps(3) + { + dps[0] = dpsite(1, 0); + dps[1] = dpsite(0, 1); + dps[2] = dpsite(0, -1); + moves[0] = &self::down; + moves[1] = &self::fwd; + moves[2] = &self::bkd; + } + + mln_psite(I) p; + + void init() {} + void final() {} + void next() + { + input(p) = f(p); + } + void fwd() { std::cout << "fwd" << std::endl; next(); } + void bkd() { std::cout << "bkd" << std::endl; next(); } + void down() { std::cout << "down" << std::endl; next(); } +}; + +namespace mln +{ + + template <typename I, typename F, typename B> + void my_test(Image<I>& ima_, + const Function_p2v<F>& f_, + const Browsing<B>& browse_) + { + I& ima = exact(ima_); + const F& f = exact(f_); + const B& browse = exact(browse_); + + assign_browsing_functor<I, F> fun(ima, f); + browse(fun); + } + +} + + +int main() +{ + using namespace mln; + image2d<unsigned> ima2(3, 3); + + std::cout << ima2.bbox() << std::endl; + my_test(ima2, fun::p2v::iota, canvas::browsing::snake_generic); + debug::println(ima2); +} Index: branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_vert.cc =================================================================== --- branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_vert.cc (revision 0) +++ branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_vert.cc (revision 2463) @@ -0,0 +1,114 @@ +// Copyright (C) 2007, 2008 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/canvas/browsing/snake_generic_3d_vert.cc + * + * \brief Tests on mln::canvas::browsing::snake_generic. + */ + +#include <mln/core/image/image3d.hh> +#include <mln/canvas/browsing/snake_generic.hh> +#include <mln/fun/p2v/iota.hh> +#include <mln/debug/println.hh> + +template <typename I, typename F> +struct assign_browsing_functor +{ + enum { dim = I::site::dim }; + + typedef assign_browsing_functor<I, F> self; + typedef mln_deduce(I, psite, delta) dpsite; + typedef void (assign_browsing_functor<I,F>::*move_fun)(); + + I input; + F f; + std::vector<move_fun> moves; + std::vector<dpsite> dps; + + assign_browsing_functor(I& input, F f = F()) + : input(input), + f(f), + moves(5), + dps(5) + { + dps[0] = dpsite(0, 0, 1); + dps[1] = dpsite(0, 1, 0); + dps[2] = dpsite(0, -1, 0); + dps[3] = dpsite(1, 0, 0); + dps[4] = dpsite(-1, 0, 0); + + moves[0] = &self::thr; + moves[1] = &self::down; + moves[2] = &self::up; + moves[3] = &self::fwd; + moves[4] = &self::bkd; + } + + mln_psite(I) p; + + void init() {} + void final() {} + void next() + { + input(p) = f(p); + } + void fwd() { std::cout << "fwd" << std::endl; next(); } + void bkd() { std::cout << "bkd" << std::endl; next(); } + void down() { std::cout << "down" << std::endl; next(); } + void up() { std::cout << "up" << std::endl; next(); } + void thr() { std::cout << "thr" << std::endl; next(); } +}; + +namespace mln +{ + + template <typename I, typename F, typename B> + void my_test(Image<I>& ima_, + const Function_p2v<F>& f_, + const Browsing<B>& browse_) + { + I& ima = exact(ima_); + const F& f = exact(f_); + const B& browse = exact(browse_); + + assign_browsing_functor<I, F> fun(ima, f); + browse(fun); + } + +} + + +int main() +{ + using namespace mln; + image3d<unsigned> ima(3, 3, 3); + + ima(point3d(0,0,0)) = 42; + std::cout << ima.bbox() << std::endl; + my_test(ima, fun::p2v::iota, canvas::browsing::snake_generic); + debug::println(ima); +} Index: branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_hori.cc =================================================================== --- branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_hori.cc (revision 0) +++ branches/cleanup-2008/milena/tests/canvas/browsing/snake_generic_3d_hori.cc (revision 2463) @@ -0,0 +1,114 @@ +// Copyright (C) 2007, 2008 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/canvas/browsing/snake_generic_3d_hori.cc + * + * \brief Tests on mln::canvas::browsing::snake_generic. + */ + +#include <mln/core/image/image3d.hh> +#include <mln/canvas/browsing/snake_generic.hh> +#include <mln/fun/p2v/iota.hh> +#include <mln/debug/println.hh> + +template <typename I, typename F> +struct assign_browsing_functor +{ + enum { dim = I::site::dim }; + + typedef assign_browsing_functor<I, F> self; + typedef mln_deduce(I, psite, delta) dpsite; + typedef void (assign_browsing_functor<I,F>::*move_fun)(); + + I input; + F f; + std::vector<move_fun> moves; + std::vector<dpsite> dps; + + assign_browsing_functor(I& input, F f = F()) + : input(input), + f(f), + moves(5), + dps(5) + { + dps[0] = dpsite(1, 0, 0); + dps[1] = dpsite(0, 1, 0); + dps[2] = dpsite(0, -1, 0); + dps[3] = dpsite(0, 0, 1); + dps[4] = dpsite(0, 0, -1); + + moves[0] = &self::thr; + moves[1] = &self::down; + moves[2] = &self::up; + moves[3] = &self::fwd; + moves[4] = &self::bkd; + } + + mln_psite(I) p; + + void init() {} + void final() {} + void next() + { + input(p) = f(p); + } + void fwd() { std::cout << "fwd" << std::endl; next(); } + void bkd() { std::cout << "bkd" << std::endl; next(); } + void down() { std::cout << "down" << std::endl; next(); } + void up() { std::cout << "up" << std::endl; next(); } + void thr() { std::cout << "thr" << std::endl; next(); } +}; + +namespace mln +{ + + template <typename I, typename F, typename B> + void my_test(Image<I>& ima_, + const Function_p2v<F>& f_, + const Browsing<B>& browse_) + { + I& ima = exact(ima_); + const F& f = exact(f_); + const B& browse = exact(browse_); + + assign_browsing_functor<I, F> fun(ima, f); + browse(fun); + } + +} + + +int main() +{ + using namespace mln; + image3d<unsigned> ima(3, 3, 3); + + ima(point3d(0,0,0)) = 42; + std::cout << ima.bbox() << std::endl; + my_test(ima, fun::p2v::iota, canvas::browsing::snake_generic); + debug::println(ima); +} Index: branches/cleanup-2008/milena/mln/canvas/browsing/directional.hh =================================================================== --- branches/cleanup-2008/milena/mln/canvas/browsing/directional.hh (revision 2462) +++ branches/cleanup-2008/milena/mln/canvas/browsing/directional.hh (revision 2463) @@ -74,6 +74,21 @@ * void final(); \n * } \n * + * Example : \n + * + * 1 0 0 + * 2 0 0 + * 3 0 0 + * + * 4 0 0 + * 5 0 0 + * 6 0 0 + * + * 7 0 0 + * 8 0 0 + * 9 0 0 + * + * */ struct directional_t : public Browsing< directional_t > { Index: branches/cleanup-2008/milena/mln/canvas/browsing/snake_generic.hh =================================================================== --- branches/cleanup-2008/milena/mln/canvas/browsing/snake_generic.hh (revision 0) +++ branches/cleanup-2008/milena/mln/canvas/browsing/snake_generic.hh (revision 2463) @@ -0,0 +1,163 @@ +// 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_CANVAS_BROWSING_SNAKE_GENERIC_HH +# define MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH + +/*! \file mln/canvas/browsing/snake_generic.hh + * + * \brief Browsing in a snake-way, forward. + */ + +# include <stack> +# include <mln/core/concept/browsing.hh> + +namespace mln +{ + + namespace canvas + { + + namespace browsing + { + + /*! + * \brief Multidimentional Browsing in a given-way. + * + * F shall feature: \n + * { \n + * --- as attributes: \n + * input; \n + * p; \n + * --- as methods: \n + * void init(); \n + * void *() moves[]; \n + * dpsite dps[]; \n + * } \n + * + * init is called before browsing + * + * The snake follow dimension using the delta point site of dps. + * dps[0] = delta psite following the global dimension (forward) + * dps[1] = delta psite following the 2nd dimension to follow (forward). + * dps[2] = delta psite following the 2nd dimension to follow (backward). + * dps[3] = delta psite following the 3nd dimension to follow (forward). + * dps[3] = delta psite following the 3nd dimension to follow (backward). + * + * moves contains pointer to f's members. These merbers will be call in each time + * the snake progress in the correct dimension : + * + * moves[i] is called at each move following the delta psite dps[i] + * + */ + + struct snake_generic_t : public Browsing< snake_generic_t > + { + + template <typename F> + void operator()(F& f) const; + } + + snake_generic; + + + +# ifndef MLN_INCLUDE_ONLY + + template <typename F> + inline + void + snake_generic_t::operator()(F& f) const + { + trace::entering("canvas::browsing::snake_generic"); + mln_precondition(f.input.has_data()); + + // p init + f.p = f.input.bbox().pmin();// - f.dps[0]; + + std::vector< int > directions(f.moves.size(), 0); + unsigned deph = 0; + unsigned total_deph = f.moves.size() / 2 + 1; + + // initialization + trace::entering("canvas::browsing::snake_generic::init"); + f.init(); + trace::exiting("canvas::browsing::snake_generic::init"); + + bool first = true; + directions[deph] = 1; + deph = total_deph - 1; + + // Call the move function (for the first point) + (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) (); + while (deph > 0) // If direction is empty, break + { + mln_assertion(deph <= total_deph); + mln_assertion(deph > 0); + // If f.p is near the border (we ended a direction) -> next child + if (!f.input.domain().has(f.p + + f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]])) + { + // Go up the tree + deph--; + if (deph >= 1) + // Change directions + directions[deph] = directions[deph] == 1 ? 0 : 1; + continue; + } + + if (!first) + { + // Move f.p + f.p += f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]]; + // Call the move function + (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) (); + } + else + first = false; + + if (deph != total_deph) + { + // Go down the tree + deph++; + first = true; + } + } + + trace::exiting("canvas::browsing::snake_generic"); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::canvas::browsing + + } // end of namespace mln::canvas + +} // end of namespace mln + + +#endif // ! MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
participants (1)
-
Matthieu Garrigues