milena r1286: Improve hexagonal images

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-10-09 Matthieu Garrigues <garrigues@lrde.epita.fr> Improve hexagonal images. Fixme : doesn't work properly now. * mln/debug/println.hh: Println for hexa images. * mln/core/box2d_h.hh: Box with hexa points. * mln/core/hexa.hh: Hexa image class declaration. * tests/hexa.cc: Hexa test * tests/image2d_h.cc: hexagonal image2d test. * mln/core/hexa_piter.hh: Hexagonal iterator. * mln/core/image2d_h.hh: Shortcut to hexa< image2d<V> > --- mln/core/box2d_h.hh | 11 +-- mln/core/hexa.hh | 55 ++++++++++++------ mln/core/hexa_piter.hh | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ mln/core/image2d_h.hh | 86 ++++++++++++++++++++++++++++ mln/debug/println.hh | 41 +++++++++++++ tests/hexa.cc | 8 +- tests/image2d_h.cc | 65 +++++++++++++++++++++ 7 files changed, 386 insertions(+), 27 deletions(-) Index: trunk/milena/tests/image2d_h.cc =================================================================== --- trunk/milena/tests/image2d_h.cc (revision 0) +++ trunk/milena/tests/image2d_h.cc (revision 1286) @@ -0,0 +1,65 @@ +// 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/hexa.cc + * + * \brief Tests on mln::hexa + */ + +#include <mln/core/image2d_h.hh> + +#include <mln/value/int_u16.hh> +#include <mln/trait/image/print.hh> +#include <mln/debug/iota.hh> +#include <mln/debug/println.hh> + + +int main() +{ + using namespace mln; + using value::int_u16; + + image2d_h<int_u16> h(2,7); + + debug::iota(h); + + debug::println(h); + +// image2d<int_u16> h(2,7); +// debug::println(h); + + trait::image::print(h, std::cout); + + + // FIXME : to put into debug::println + image2d_h<int_u16>::fwd_piter p(h.domain()); + +// for_all(p) +// { +// std::cout << p << "->" << std::endl; +// } +} Index: trunk/milena/tests/hexa.cc =================================================================== --- trunk/milena/tests/hexa.cc (revision 1285) +++ trunk/milena/tests/hexa.cc (revision 1286) @@ -35,6 +35,7 @@ #include <mln/value/int_u8.hh> #include <mln/trait/image/print.hh> +#include <mln/debug/iota.hh> int main() @@ -46,14 +47,15 @@ I ima(3,3); hexa<I> h(ima); - + debug::iota(ima); trait::image::print(h, std::cout); // FIXME : to put into debug::println - I::fwd_piter p(ima.domain()); + hexa<I>::fwd_piter p(h.domain()); + for_all(p) { - ; + std::cout << p << "->" << h(p) << std::endl; } } Index: trunk/milena/mln/debug/println.hh =================================================================== --- trunk/milena/mln/debug/println.hh (revision 1285) +++ trunk/milena/mln/debug/println.hh (revision 1286) @@ -56,8 +56,11 @@ // generic version template <typename S, typename I> - void println(const S&, const Image<I>& input_) + void println(const S& b, const Image<I>& input_) { + void* tes = exact(input_); + void* a = b; + const I& input = exact(input_); mln_piter(I) p(input.domain()); for_all(p) @@ -92,6 +95,42 @@ # endif // MLN_CORE_BOX2D_HH +# ifdef MLN_CORE_IMAGE2D_H_HH + + // Hexa version + template <typename I> + void println(const box2d_h& b, const hexa<I>& input) + { + typename hexa<I>::fwd_piter p(input.domain()); + + int c = 1; + int r = 1; + int row_len = (b.max_col() - b.min_col()) / 2; + + for_all(p) + { + //if (input.has(p)) + //std::cout << p << " "; + std::cout << format(input(p)) << " "; + //else + // std::cout << " "; + + if (c >= row_len) + { + std::cout << std::endl; + if (r % 2) + std::cout << " "; + c = 0; + r++; + } + c++; + } + std::cout << std::endl; + } + +# endif // MLN_CORE_IMAGE2D_H_HH + + # ifdef MLN_CORE_BOX3D_HH template <typename I> Index: trunk/milena/mln/core/hexa.hh =================================================================== --- trunk/milena/mln/core/hexa.hh (revision 1285) +++ trunk/milena/mln/core/hexa.hh (revision 1286) @@ -37,6 +37,8 @@ # include <mln/core/internal/image_domain_morpher.hh> # include <mln/core/point2d_h.hh> +# include <mln/core/box2d_h.hh> +# include <mln/core/hexa_piter.hh> namespace mln @@ -51,9 +53,10 @@ template <typename I> struct data_< hexa<I> > { - data_(I& ima); + data_(I& ima, box2d_h b); I ima_; + mln::box2d_h b_; }; } // end of namespace mln::internal @@ -92,22 +95,24 @@ * which handles hexagonal grid. * * Ex : + * 1 3 5 7 9 11 + * 0 2 4 6 8 10 * ------------------- - * XX| | | | | | |XX + * 0 XX| | | | | | |XX * --------------------- - * XX| | | | | | |XX + * 2 XX| | | | | | |XX * --------------------- - * XX| | | | | | |XX + * 4 XX| | | | | | |XX * --------------------- - * XX| | | | | | |XX + * 6 XX| | | | | | |XX * --------------------- - * XX| | | | | | |XX + * 8 XX| | | | | | |XX * ------------------- * * */ template <typename I> - struct hexa : public internal::image_domain_morpher_< I, mln_pset(I), hexa<I> > + struct hexa : public internal::image_domain_morpher_< I, box2d_h, hexa<I> > { /// Skeleton. typedef hexa< tag::image_<I> > skeleton; @@ -116,7 +121,7 @@ typedef mln_value(I) value; /// Lvalue associated type. - typedef mln_value(I) lvalue; + typedef mln_lvalue(I) lvalue; /// Return type of read-only access. typedef mln_rvalue(I) rvalue; @@ -124,6 +129,15 @@ /// Point site type typedef point2d_h psite; + /// FIXME : should it be in box2d_h? + /// Forward Point_Iterator associated type. + typedef hexa_fwd_piter_<box2d> fwd_piter; + + /// FIXME : should it be in box2d_h? + /// Backward Point_Iterator associated type. + /// typedef hexa_fwd_piter_<box2d> bkd_piter; + + /// Constructor without argument. hexa(); @@ -134,7 +148,7 @@ void init_(I& ima); /// Give the definition domain. - const mln_pset(I)& domain() const; + const box2d_h& domain() const; /// Test if \p p belongs to the image domain. bool has(const psite& p) const; @@ -144,7 +158,7 @@ rvalue operator()(const point2d_h& p) const; /// Read-write access of pixel value at hexa point site \p p. - value operator()(const point2d_h& p); + lvalue operator()(const point2d_h& p); }; template <typename I, typename J> @@ -171,8 +185,9 @@ { template <typename I> - data_< hexa<I> >::data_(I& ima) - : ima_(ima) + data_< hexa<I> >::data_(I& ima, box2d_h b) + : ima_(ima), + b_(b) { } @@ -184,11 +199,13 @@ hexa<I>::init_(I& ima) { mln_precondition(! this->has_data()); - this->data_ = new internal::data_< hexa<I> >(ima); + box2d b_in = ima.bbox(); + box2d_h b = make::box2d_h(b_in.pmin()[0], b_in.pmin()[1] * 2 - 1, + b_in.pmax()[0], b_in.pmax()[1] * 2 - 1); + this->data_ = new internal::data_< hexa<I> >(ima, b); } - template <typename I> hexa<I>::hexa() { @@ -205,27 +222,31 @@ typename hexa<I>::rvalue hexa<I>::operator()(const point2d_h& p) const { + mln_precondition(this->has_data()); return this->data_->ima_(make::point2d(p[0] / 2, p[1] / 2)); } template <typename I> - typename hexa<I>::value + typename hexa<I>::lvalue hexa<I>::operator()(const point2d_h& p) { + mln_precondition(this->has_data()); return this->data_->ima_(make::point2d(p[0] / 2, p[1] / 2)); } template <typename I> - const mln_pset(I)& + const box2d_h& hexa<I>::domain() const { - return this->data_->ima_.domain(); + mln_precondition(this->has_data()); + return this->data_->b_; } template <typename I> bool hexa<I>::has(const psite& p) const { + mln_precondition(this->has_data()); return this->data_->ima_.has(make::point2d(p[0] / 2, p[1] / 2)); } Index: trunk/milena/mln/core/hexa_piter.hh =================================================================== --- trunk/milena/mln/core/hexa_piter.hh (revision 0) +++ trunk/milena/mln/core/hexa_piter.hh (revision 1286) @@ -0,0 +1,147 @@ +// 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_CORE_HEXA_PITER_HH +# define MLN_CORE_HEXA_PITER_HH + +/*! \file mln/core/hexa_piter.hh + * + * \brief Definition of iterators on points of pset_ifes. + */ + +# include <mln/core/internal/piter_adaptor.hh> + +# include <mln/core/box2d.hh> +# include <mln/core/inplace.hh> + +namespace mln +{ + + /*! \brief A generic forward iterator on points of subsets. + * + * Parameter \c S is a point set type; parameter F is a function + * from point to Boolean. + * + * \see mln::hexa + */ + template <typename S> + class hexa_fwd_piter_ + : public internal::piter_adaptor_< mln_fwd_piter(S), + hexa_fwd_piter_<S> > + { + typedef mln_fwd_piter(S) adaptee_; + typedef hexa_fwd_piter_<S> self_; + typedef internal::piter_adaptor_<adaptee_, self_> super_; + + public: + + /// Constructor from a subset of points. + hexa_fwd_piter_(const box2d& subset); + hexa_fwd_piter_(const box2d_h& subset); + + /// Dtor + ~hexa_fwd_piter_(); + + /// Start an iteration. + void start(); + + /// Go to the next point. + void next_(); + + operator point2d_h() const; + private: + + point2d_h p_; + }; + + + // FIXME: + template <typename S> + class hexa_bkd_piter_ + : + public mln::internal::fixme + {}; + + + +# ifndef MLN_INCLUDE_ONLY + + + // hexa_fwd_piter_<I> + + template <typename S> + hexa_fwd_piter_<S>::hexa_fwd_piter_(const box2d& b) + : super_(adaptee_(b)) + { + } + + template <typename S> + hexa_fwd_piter_<S>::hexa_fwd_piter_(const box2d_h& b) + : super_(adaptee_(*new box2d(make::box2d(b.pmin()[0] / 2, b.pmin()[1] / 2, + b.pmax()[0] / 2 + 1, b.pmax()[1] / 2 + 1)))) + { + } + + template <typename S> + hexa_fwd_piter_<S>::~hexa_fwd_piter_() + { + } + + template <typename S> + void + hexa_fwd_piter_<S>::start() + { + this->piter_.start(); + p_[0] = this->piter_[0]; + p_[1] = this->piter_[1]; + } + + template <typename S> + void + hexa_fwd_piter_<S>::next_() + { + this->piter_.next(); + p_[0] = this->piter_[0] * 2; + p_[1] = this->piter_[1] * 2 + this->piter_[0] % 2; + std::cout << "next :" << p_ << std::endl; + } + + template <typename S> + hexa_fwd_piter_<S>::operator point2d_h() const + { + return p_; + } + + // FIXME: hexa_bkd_piter_<S> + + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + +#endif // ! MLN_CORE_HEXA_PITER_HH Index: trunk/milena/mln/core/box2d_h.hh =================================================================== --- trunk/milena/mln/core/box2d_h.hh (revision 1285) +++ trunk/milena/mln/core/box2d_h.hh (revision 1286) @@ -25,8 +25,8 @@ // reasons why the executable file might be covered by the GNU General // Public License. -#ifndef MLN_CORE_BOX2D_HH -# define MLN_CORE_BOX2D_HH +#ifndef MLN_CORE_BOX2D_H_HH +# define MLN_CORE_BOX2D_H_HH /*! \file mln/core/box2d.hh * @@ -35,19 +35,18 @@ */ # include <mln/core/box.hh> +# include <mln/core/box2d.hh> # include <mln/core/point2d_h.hh> namespace mln { - /*! \brief Type alias for a box defined on the 2D square grid with - * integer coordinates. + /*! \brief FIXME * - * \see mln::win::rectangle2d. */ - typedef box_<point2d_h> box2d_h; + typedef box_<point2d_h> box2d_h; } // end of namespace mln Index: trunk/milena/mln/core/image2d_h.hh =================================================================== --- trunk/milena/mln/core/image2d_h.hh (revision 0) +++ trunk/milena/mln/core/image2d_h.hh (revision 1286) @@ -0,0 +1,86 @@ +// 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_CORE_IMAGE2D_H_HH +# define MLN_CORE_IMAGE2D_H_HH + +/*! \file mln/core/hexa_piter.hh + * + * \brief Definition of iterators on points of pset_ifes. + */ + +# include <mln/core/image2d.hh> +# include <mln/core/hexa.hh> + +# include <mln/border/thickness.hh> + + +# include <mln/debug/println.hh> + +namespace mln +{ + template <typename V> + struct image2d_h + : public hexa< image2d<V> > + { + //typedef hexa< image2d<V> > super_; + + /// Point site type + typedef point2d_h psite; + + /// Constructor with the numbers of rows and columns + /// border thickness. + image2d_h(int nrows, int ncols, unsigned bdr = border::thickness); + + //using super_::init_; + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename V> + image2d_h<V>::image2d_h(int nrows, int ncols, unsigned bdr) + { + std::cout << "Image2d_h Ctor " << nrows << " " + << ncols / 2 + 1 << std::endl; + + mln_assertion(ncols % 2 == 1); + image2d<V> ima(nrows, + ncols / 2 + 1, + bdr); + + debug::println(ima); + + this->init_(ima); + } + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + +#endif // ! MLN_CORE_IMAGE2D_H_HH
participants (1)
-
Matthieu Garrigues