
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Start a multiple window class. * doc/tutorial/examples/win_multiple.cc: New. * mln/core/internal/site_relative_iterator_base.hh: . * mln/win/multiple.hh: New. doc/tutorial/examples/win_multiple.cc | 59 ++++++ mln/core/internal/site_relative_iterator_base.hh | 6 mln/win/multiple.hh | 194 +++++++++++------------ 3 files changed, 162 insertions(+), 97 deletions(-) Index: doc/tutorial/examples/win_multiple.cc --- doc/tutorial/examples/win_multiple.cc (revision 0) +++ doc/tutorial/examples/win_multiple.cc (revision 0) @@ -0,0 +1,59 @@ +# include <mln/core/image/image2d.hh> +# include <mln/core/alias/window2d.hh> +# include <mln/win/multiple.hh> + +# include <mln/debug/iota.hh> +# include <mln/debug/println.hh> + +# include <mln/core/alias/neighb2d.hh> + + + +template <typename I, typename W> +void browse(const I& ima, const W& win) +{ + mln_fwd_piter(I) p(ima.domain()); + mln_fwd_qiter(W) q(win, p); + for_all(p) + { + std::cout << ima(p) << ": "; + for_all(q) + if (ima.has(q)) + std::cout << ima(q) << ' '; + std::cout << std::endl; + } +} + + +struct row_oddity +{ + unsigned operator()(const mln::point2d& p) const + { + return p.row() % 2; + } +}; + + +int main() +{ + using namespace mln; + + typedef image2d<unsigned> I; + I ima(3, 3, 0); // no border + debug::iota(ima); + debug::println(ima); + + win::multiple<2, window2d, row_oddity> w; + + bool vert[] = { 0, 1, 0, + 0, 0, 0, + 0, 1, 0 }; + w.set_window(0, make::window2d(vert)); + bool horiz[] = { 0, 0, 0, + 1, 0, 1, + 0, 0, 0 }; + w.set_window(1, make::window2d(horiz)); + + browse(ima, w); + +} Index: mln/core/internal/site_relative_iterator_base.hh --- mln/core/internal/site_relative_iterator_base.hh (revision 2247) +++ mln/core/internal/site_relative_iterator_base.hh (working copy) @@ -35,6 +35,8 @@ * * \todo Add a method to get the site set (if the center is defined) or * the site set at a given center. + * + * \todo In start_ and next_, think about the commented code... */ # include <vector> @@ -143,6 +145,8 @@ exact(this)->do_start_(); if (this->is_valid()) p_ = exact(this)->compute_p_(); +// else +// exact(this)->invalidate(); } template <typename S, typename E> @@ -153,6 +157,8 @@ exact(this)->do_next_(); if (this->is_valid()) p_ = exact(this)->compute_p_(); +// else +// exact(this)->invalidate(); } template <typename S, typename E> Index: mln/win/multiple.hh --- mln/win/multiple.hh (revision 2245) +++ mln/win/multiple.hh (working copy) @@ -1,167 +1,167 @@ -# include <mln/core/concept/neighborhood.hh> +// Copyright (C) 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. + +#ifndef MLN_WIN_MULTIPLE_HH +# define MLN_WIN_MULTIPLE_HH + +/*! \file mln/win/multiple.hh + * + * \brief Definition of a multiple window. + */ + +# include <mln/core/internal/window_base.hh> +# include <mln/core/internal/site_relative_iterator_base.hh> + namespace mln { + namespace win + { - // Fwd decl. - template <typename D, typename F> class dbl_niter_; + // Forward declaration. + template <unsigned n, typename W, typename F> class multiple_qiter; - template <typename D, typename F> - struct dbl_neighb_ : public Neighborhood< dbl_neighb_<D,F> > + template <unsigned n, typename W, typename F> + class multiple : public internal::window_base< mln_dpsite(W), multiple<n,W,F> > { - typedef D dpoint; - typedef mln_point(D) point; + public: - typedef dbl_niter_<D,F> fwd_niter; - typedef dbl_niter_<D,F> bkd_niter; - typedef dbl_niter_<D,F> niter; + typedef mln_dpsite(W) dpsite; + typedef mln_psite(W) psite; + typedef mln_site(W) site; - dbl_neighb_(F f) - : f(f) - { - } + typedef multiple_qiter<n,W,F> fwd_qiter; + typedef multiple_qiter<n,W,F> bkd_qiter; + typedef multiple_qiter<n,W,F> qiter; - dbl_neighb_() - : f() + multiple() + : f_() { } - bool is_empty() const + multiple(const F& f) + : f_(f) { - return trues.size() == 0 && falses.size() == 0; } - dbl_neighb_& insert_true(const D& dp) + bool is_empty() const { - trues.push_back(dp); - trues.push_back(-dp); - return *this; + bool res = false; + for (unsigned i = 0; i < n; ++i) + if (win_[i].is_empty()) + return true; + return res; } - dbl_neighb_& insert_false(const D& dp) + void set_window(unsigned i, const W& win) { - falses.push_back(dp); - falses.push_back(-dp); - return *this; + mln_precondition(i < n); + win_[i] = win; } - template <typename N> - dbl_neighb_& when_true(const Neighborhood<N>& nbh_) + const W& window(unsigned i) const { - const N& nbh = exact(nbh_); - for (unsigned i = 0; i < nbh.size(); ++i) - trues.push_back(nbh.dp(i)); - return *this; + mln_precondition(i < n); + return win_[i]; } - template <typename N> - dbl_neighb_& when_false(const Neighborhood<N>& nbh_) + const F& fun() const { - const N& nbh = exact(nbh_); - for (unsigned i = 0; i < nbh.size(); ++i) - falses.push_back(nbh.dp(i)); - return *this; + return f_; } - std::vector<D> trues, falses; - F f; + private: + W win_[n]; + F f_; }; - template <typename D, typename F> - class dbl_niter_ - : public internal::point_iterator_base_< mln_point(D), dbl_niter_<D,F> > + template <unsigned n, typename W, typename F> + class multiple_qiter + : public internal::site_relative_iterator_base< multiple<n,W,F>, + multiple_qiter<n,W,F> > { public: - typedef mln_point(D) point; - - template <typename Pref> - dbl_niter_(const Neighborhood< dbl_neighb_<D,F> >& nbh, - const Point_Site<Pref>& p_ref) : - dps_true(exact(nbh).trues), - dps_false(exact(nbh).falses), - p_ref_(exact(p_ref).to_point()), - f(exact(nbh).f) + multiple_qiter() { - invalidate(); } - /// Conversion to point. - operator point () const + template <typename P> + multiple_qiter(const multiple<n,W,F>& w, const P& c) { - mln_precondition(is_valid()); - return p_; - } - - /// Reference to the corresponding point. - const point& to_point() const - { - return p_; + this->change_target(w); + this->center_at(c); } /// Test the iterator validity. - bool is_valid() const + bool is_valid_() const { - return i_ != dps_true.size(); + return i_ != -1; } /// Invalidate the iterator. - void invalidate() + void invalidate_() { - i_ = dps_true.size(); + i_ = -1; } /// Start an iteration. - void start() + void do_start_() { i_ = 0; - update(); } /// Go to the next point. - void next_() + void do_next_() { ++i_; - update(); - } - - /// Give the i-th coordinate. - int operator[](unsigned i) const - { - mln_precondition(is_valid()); - return p_[i]; } - /// The point around which this iterator moves. - const point& center_point() const + /// Compute the current psite. + mln_psite(W) compute_p_() const { - return p_ref_; + unsigned w = this->s_->fun()(*this->c_); + return *this->c_ + this->s_->window(w).std_vector()[i_]; } - /// Force this iterator to update its location to take into - /// account that its center point may have moved. - void update() - { - if (is_valid()) - p_ = p_ref_ + (f(p_ref_) ? dps_true[i_] : dps_false[i_]); - } + private: + int i_; + }; - protected: - const std::vector<D>& dps_true, dps_false; - const point& p_ref_; // reference point (or "center point") - F f; - unsigned i_; - point p_; // location of this iterator; p_ makes this iterator be - // itself a potential center point. - }; + } // end of namespace mln::win +} // end of namespace mln -} // mln +#endif // ! MLN_WIN_MULTIPLE_HH