milena r1462: Add dpoints_bkd_piter

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-11-12 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> Add dpoints_bkd_piter. * mln/core/clock_neighb.hh: Add typo and fix new header. * mln/core/clock_neighb2d.hh: Fix warnings (int -> unsigned). * mln/core/dpoints_piter.hh: Add bkf_iterator. * tests/clock_neighb2d.cc: New test for this. --- mln/core/clock_neighb.hh | 4 - mln/core/clock_neighb2d.hh | 8 +- mln/core/dpoints_piter.hh | 150 +++++++++++++++++++++++++++++++++++++++++++-- tests/clock_neighb2d.cc | 29 +++++++- 4 files changed, 179 insertions(+), 12 deletions(-) Index: trunk/milena/tests/clock_neighb2d.cc =================================================================== --- trunk/milena/tests/clock_neighb2d.cc (revision 1461) +++ trunk/milena/tests/clock_neighb2d.cc (revision 1462) @@ -32,7 +32,7 @@ namespace mln { template <typename I, typename N> - void test(Image<I>& input_, + void test_fwd(Image<I>& input_, const Neighborhood<N>& nbh) { I& input = exact (input_); @@ -40,9 +40,26 @@ int v = 1; mln_fwd_niter(N) n (nbh, p); + for_all (n) + { + std::cout << n << std::endl; + input(n) = v++; + } + } + + template <typename I, typename N> + void test_bkd(Image<I>& input_, + const Neighborhood<N>& nbh) + { + I& input = exact (input_); + point2d p (1,1); + int v = 1; + + mln_bkd_niter(N) n (nbh, p); for_all (n) { + std::cout << n << std::endl; input(n) = v++; } @@ -59,11 +76,17 @@ image2d<int> ima(3,3); dpoint2d dp (1,0); - test(ima, cc8(dp)); + test_fwd(ima, cc4(dp)); debug::println(ima); + test_bkd(ima, cc4(dp)); + debug::println(ima); + dpoint2d dp2 (-1,-1); - test(ima, cc8(dp2)); + test_fwd(ima, cc8(dp2)); debug::println(ima); + test_bkd(ima, cc8(dp2)); + debug::println(ima); + } Index: trunk/milena/mln/core/dpoints_piter.hh =================================================================== --- trunk/milena/mln/core/dpoints_piter.hh (revision 1461) +++ trunk/milena/mln/core/dpoints_piter.hh (revision 1462) @@ -99,10 +99,63 @@ }; - // FIXME: - template <typename D> - class dpoints_bkd_piter : public mln::internal::fixme - {}; + /*! \brief A generic backward iterator on points of windows and of + * neighborhoods. + * + * The parameter \c D is the type of delta-points. + */ + template <typename D> + class dpoints_bkd_piter : public internal::point_iterator_base_< mln_point(D), dpoints_bkd_piter<D> > + { + public: + + /*! \brief Constructor. + * + * \param[in] dps Object that can provide an array of delta-points. + * \param[in] p_ref Center point to iterate around. + */ + template <typename Dps, typename Pref> + dpoints_bkd_piter(const Dps& dps, // FIXME: explicitly set_of_<D>? + const Point_Site<Pref>& p_ref); + + /// Convertion to point. + operator mln_point(D) () const; + + /// Reference to the corresponding point. + const mln_point(D)& to_point() const; + + /// Test the iterator validity. + bool is_valid() const; + + /// Invalidate the iterator. + void invalidate(); + + /// Start an iteration. + void start(); + + /// Go to the next point. + void next_(); + + /// Give the i-th coordinate. + mln_coord(D) operator[](unsigned i) const; + + /// The point around which this iterator moves. + const mln_point(D)& center_point() const; + + /// Force this iterator to update its location to take into + /// account that its center point may have moved. + void update(); + + protected: + + const std::vector<D>& dps_; + const mln_point(D)& p_ref_; // reference point (or "center point") + + int i_; + mln_point(D) p_; // location of this iterator; p_ makes this iterator be + // itself a potential center point. + }; + # ifndef MLN_INCLUDE_ONLY @@ -191,6 +244,95 @@ return p_[i]; } + + + template <typename D> + template <typename Dps, typename Pref> + dpoints_bkd_piter<D>::dpoints_bkd_piter(const Dps& dps, + const Point_Site<Pref>& p_ref) + : dps_(exact(dps).vect()), + p_ref_(exact(p_ref).to_point()) + { + invalidate(); + } + + template <typename D> + dpoints_bkd_piter<D>::operator mln_point(D) () const + { + mln_precondition(is_valid()); + return p_; + } + + template <typename D> + const mln_point(D)& + dpoints_bkd_piter<D>::to_point() const + { + return p_; + } + + template <typename D> + bool + dpoints_bkd_piter<D>::is_valid() const + { + unsigned i = i_; + + return i < dps_.size(); + } + + template <typename D> + void + dpoints_bkd_piter<D>::invalidate() + { + i_ = -1; + } + + template <typename D> + void + dpoints_bkd_piter<D>::start() + { + i_ = dps_.size() - 1; + update(); + } + + template <typename D> + void + dpoints_bkd_piter<D>::next_() + { + --i_; + update(); + } + + template <typename D> + const mln_point(D)& + dpoints_bkd_piter<D>::center_point() const + { + return p_ref_; + } + + template <typename D> + void + dpoints_bkd_piter<D>::update() + { + if (is_valid()) + p_ = p_ref_ + dps_[i_]; + } + + template <typename D> + mln_coord(D) + dpoints_bkd_piter<D>::operator[](unsigned i) const + { + mln_precondition(is_valid()); + + // below we test that no update is required + // meaning that p_ref_ has not moved or that + // the user has explicitly called update() + mln_precondition(p_ref_ + dps_[i_] == p_); + // FIXME: Explain this issue in the class documentation... + + return p_[i]; + } + + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: trunk/milena/mln/core/clock_neighb.hh =================================================================== --- trunk/milena/mln/core/clock_neighb.hh (revision 1461) +++ trunk/milena/mln/core/clock_neighb.hh (revision 1462) @@ -36,7 +36,7 @@ # include <mln/core/concept/neighborhood.hh> # include <mln/core/internal/dpoints_base.hh> # include <mln/core/dpoint.hh> -# include <mln/core/vec_p.hh> +# include <mln/core/p_array.hh> namespace mln { @@ -67,6 +67,8 @@ /*! \brief Point_Iterator type to browse the points of a generic * neighborhood w.r.t. the reverse ordering of delta-points. + * + * !!! Be careful the start delta point become the last now. */ typedef dpoints_bkd_piter<D> bkd_niter; Index: trunk/milena/mln/core/clock_neighb2d.hh =================================================================== --- trunk/milena/mln/core/clock_neighb2d.hh (revision 1461) +++ trunk/milena/mln/core/clock_neighb2d.hh (revision 1462) @@ -112,9 +112,9 @@ for (; (ite != v.end ()) && (dp != *ite); ++ite, ++begin) ; mln_assertion (ite != v.end ()); - for (int i = begin; i < v.size(); ++i) + for (unsigned i = begin; i < v.size(); ++i) it.append(v[i]); - for (int i = 0; i < begin; ++i) + for (unsigned i = 0; i < begin; ++i) it.append(v[i]); flower = false; @@ -144,9 +144,9 @@ for (; (ite != v.end ()) && (dp != *ite); ++ite, ++begin) ; // mln_assertion (*ite != v.end ()); - for (int i = begin; i < v.size(); ++i) + for (unsigned i = begin; i < v.size(); ++i) it.append(v[i]); - for (int i = 0; i < begin; ++i) + for (unsigned i = 0; i < begin; ++i) it.append(v[i]); // flower = false;
participants (1)
-
Guillaume Duhamel