cleanup-2008 2157: Make the 'plain' and 'safe' morphers work.

https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Make the 'plain' and 'safe' morphers work. * doc/tutorial/examples/image_plain.cc: New. * doc/tutorial/examples/image_safe.cc: New. * mln/trait/image/status.txt: Update. * mln/core/safe.hh (data_): Update, i.e., rename as... (data): ...this. (trait::image_): New. * mln/core/plain.hh: Likewise. (operator=): New overload for 'plain' arg. (plain): New ctor overload for 'plain' arg. * mln/core/concept/site_set.hh (operator==, operator<=): Fix warning. doc/tutorial/examples/image_plain.cc | 41 ++++++++++++ doc/tutorial/examples/image_safe.cc | 24 +++++++ mln/core/concept/site_set.hh | 4 - mln/core/plain.hh | 118 ++++++++++++++++++++++++----------- mln/core/safe.hh | 56 ++++++++++++---- mln/trait/image/status.txt | 4 - 6 files changed, 193 insertions(+), 54 deletions(-) Index: doc/tutorial/examples/image_plain.cc --- doc/tutorial/examples/image_plain.cc (revision 0) +++ doc/tutorial/examples/image_plain.cc (revision 0) @@ -0,0 +1,41 @@ +# include <mln/core/image2d.hh> +# include <mln/core/plain.hh> +# include <mln/debug/println.hh> +# include <mln/debug/iota.hh> +# include <mln/level/fill_with_value.hh> + + +template <typename I> +void picture(const I& ima) +{ + using namespace mln; + const unsigned + nr = ima.at(0,0).domain().nrows(), + nc = ima.at(0,0).domain().ncols(); + for (unsigned row = 0; row < ima.nrows(); ++row) + for (unsigned r = 0; r < nr; ++r) + { + for (unsigned col = 0; col < ima.ncols(); ++col) + for (unsigned c = 0; c < nc; ++c) + std::cout << ima.at(row, col)(make::point2d(r,c)) << ' '; + std::cout << std::endl; + } +} + + +int main() +{ + using namespace mln; + + typedef image2d<int> I; + typedef plain<I> I_; + + I ima(3, 3); + debug::iota(ima); + debug::println(ima); + + image2d<I_> mos(2, 2); + level::fill_with_value(mos, ima); + level::fill_with_value(mos.at(1,1), 0); + picture(mos); +} Index: doc/tutorial/examples/image_safe.cc --- doc/tutorial/examples/image_safe.cc (revision 0) +++ doc/tutorial/examples/image_safe.cc (revision 0) @@ -0,0 +1,24 @@ +# include <mln/core/image2d.hh> +# include <mln/core/line2d.hh> +# include <mln/core/safe.hh> +# include <mln/debug/println.hh> +# include <mln/level/fill.hh> +# include <mln/level/paste.hh> +# include <mln/pw/all.hh> + + + +int main() +{ + using namespace mln; + + typedef image2d<int> I; + I ima(3, 3); + level::fill(ima, 0); + + safe_image<I> ima_(ima); + level::paste(pw::cst(8) | p_line2d(make::point2d(-1,-1), + make::point2d( 3, 3)), + ima_); + debug::println(ima); +} Index: mln/trait/image/status.txt --- mln/trait/image/status.txt (revision 2156) +++ mln/trait/image/status.txt (working copy) @@ -57,11 +57,11 @@ ** identity morpher new instant +ok plain +ok safe KO decorated_image KO interpolated -KO plain -KO safe Index: mln/core/safe.hh --- mln/core/safe.hh (revision 2156) +++ mln/core/safe.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// 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 @@ -29,14 +29,14 @@ # define MLN_CORE_SAFE_HH /*! - * \file safe.hh + * \file mln/core/safe.hh * * \brief Definition of a morpher that makes image become accessible * at undefined location. * + * \todo Use 'instant' as the routine safe returns. */ - # include <mln/core/internal/image_identity.hh> @@ -44,17 +44,18 @@ namespace mln { - // Fwd decl. + // Forward declaration. template <typename I> struct safe_image; + namespace internal { /// \internal Data structure for \c mln::safe_image<I>. template <typename I> - struct data_< safe_image<I> > + struct data< safe_image<I> > { - data_(I& ima, const mln_value(I)& default_value); + data(I& ima, const mln_value(I)& default_value); I ima_; mln_value(I) default_value_; @@ -63,6 +64,20 @@ } // end of namespace mln::internal + namespace trait + { + + template <typename I> + struct image_< safe_image<I> > : image_< I > // Same as I except... + { + // ...this change. + typedef trait::image::category::identity_morpher category; + }; + + } // end of namespace mln::trait + + + // FIXME: Doc! template <typename I> @@ -74,8 +89,9 @@ /// Skeleton. typedef safe_image< tag::image_<I> > skeleton; - safe_image(I& ima, const mln_value(I)& default_value); safe_image(); + safe_image(I& ima); + safe_image(I& ima, const mln_value(I)& default_value); mln_rvalue(I) operator()(const mln_psite(I)& p) const; @@ -100,14 +116,14 @@ # ifndef MLN_INCLUDE_ONLY + // internal::data< safe_image<I,S> > + namespace internal { - // internal::data_< safe_image<I,S> > - template <typename I> inline - data_< safe_image<I> >::data_(I& ima, const mln_value(I)& default_value) + data< safe_image<I> >::data(I& ima, const mln_value(I)& default_value) : ima_(ima), default_value_(default_value) { @@ -119,15 +135,22 @@ template <typename I> inline + safe_image<I>::safe_image() + { + } + + template <typename I> + inline safe_image<I>::safe_image(I& ima, const mln_value(I)& default_value) { - this->data_ = new internal::data_< safe_image<I> >(ima, default_value); + this->data_ = new internal::data< safe_image<I> >(ima, default_value); } template <typename I> inline - safe_image<I>::safe_image() + safe_image<I>::safe_image(I& ima) { + this->data_ = new internal::data< safe_image<I> >(ima, mln_value(I)()); } template <typename I> @@ -135,6 +158,7 @@ mln_rvalue(I) safe_image<I>::operator()(const mln_psite(I)& p) const { + mln_precondition(this->has_data()); if (! this->has(p)) return this->data_->default_value_; return this->data_->ima_(p); @@ -145,11 +169,13 @@ typename safe_image<I>::lvalue safe_image<I>::operator()(const mln_psite(I)& p) { + mln_precondition(this->has_data()); static mln_value(I) forget_it_; - if (! this->has(p)) - // so data_->default_value_ is returned but cannot be modified - return forget_it_ = this->data_->default_value_; + if (this->has(p)) return this->data_->ima_(p); + else + // A copy of data_->default_value_ is returned. + return forget_it_ = this->data_->default_value_; } template <typename I> Index: mln/core/concept/site_set.hh --- mln/core/concept/site_set.hh (revision 2156) +++ mln/core/concept/site_set.hh (working copy) @@ -316,7 +316,7 @@ template <typename Sl, typename Sr> inline - util::yes operator==(const Site_Set<Sl>& lhs_, const Site_Set<Sr>& rhs_) + util::yes operator==(const Site_Set<Sl>&, const Site_Set<Sr>&) { // // FIXME: Same grid! // const Sl& lhs = exact(lhs_); @@ -340,7 +340,7 @@ template <typename Sl, typename Sr> inline - util::yes operator<=(const Site_Set<Sl>& lhs_, const Site_Set<Sr>& rhs_) + util::yes operator<=(const Site_Set<Sl>&, const Site_Set<Sr>&) { // // FIXME: Same grid! // const Sl& lhs = exact(lhs_); Index: mln/core/plain.hh --- mln/core/plain.hh (revision 2156) +++ mln/core/plain.hh (working copy) @@ -30,73 +30,86 @@ /*! \file mln/core/plain.hh * - * \brief Definition of a morpher that prevent an image from sharing + * \brief Definition of a morpher that prevents an image from sharing * his data. */ -# include <cmath> # include <mln/core/internal/image_identity.hh> # include <mln/core/clone.hh> +# include <mln/metal/is_not_const.hh> namespace mln { - // Fwd decl. + // Forward declaration. template <typename I> struct plain; + namespace internal { /// \internal Data structure for \c mln::plain<I>. template <typename I> - struct data_< plain<I> > + struct data< plain<I> > { - data_(const I& ima); - + data(const I& ima); I ima_; }; } // end of namespace mln::internal - /*! \brief FIXME - * - */ + + namespace trait + { + template <typename I> - struct plain : public mln::internal::image_identity< I, mln_pset(I), plain<I> > + struct image_< plain<I> > : image_< I > // Same as I except... { + // ...this change. + typedef trait::image::category::identity_morpher category; + }; - typedef mln::internal::image_identity< I, mln_pset(I), plain<I> > super_; + } // end of namespace mln::trait - /// Point_Site associated type. - typedef mln_psite(I) psite; - /// Value associated type. - typedef mln_value(I) value; - /// Return type of read-write access. - typedef mln_lvalue(I) lvalue; // FIXME: Depends on lvalue presence in I. + /*! \brief FIXME + * + */ + template <typename I> + class plain - /// Return type of read-only access. - typedef mln_rvalue(I) rvalue; + : public mln::internal::image_identity< I, mln_pset(I), plain<I> >, + private mlc_is_not_const(I)::check_t + { + typedef plain<I> self_; + typedef mln::internal::image_identity<I, mln_pset(I), self_> super_; + + public: /// Skeleton. typedef plain< tag::image_<I> > skeleton; - - /// Constructors. - plain(const I& ima); + /// Constructor without argument. plain(); - /// Read-only access of pixel value at point site \p p. - /// Mutable access is only OK for reading (not writing). - //using super_::operator(); + /// Copy constructor. + plain(const plain<I>& rhs); + + /// Copy constructor from an image \p ima. + plain(const I& ima); /// Assignment operator. - plain& operator=(const I& rhs); + plain<I>& operator=(const plain<I>& rhs); + /// Assignment operator from an image \p ima. + plain<I>& operator=(const I& ima); - /// Conversion into an I image + /// Initialization routine. + void init(const I& ima); + + /// Conversion into an image with type \c I. operator I () const; }; @@ -104,43 +117,77 @@ # ifndef MLN_INCLUDE_ONLY + + // internal::data< plain<I> > + namespace internal { - // internal::data_< plain<I> > - template <typename I> inline - data_< plain<I> >::data_(const I& ima) + data< plain<I> >::data(const I& ima) : ima_(clone(ima)) { } } // end of namespace mln::internal + + // plain<I> + + template <typename I> + inline + plain<I>::plain() + { + } + + template <typename I> + inline + plain<I>::plain(const plain<I>& rhs) + : super_() + { + mln_precondition(rhs.has_data()); + init(rhs.data_->ima_); + } + template <typename I> inline plain<I>::plain(const I& ima) { mln_precondition(ima.has_data()); - this->data_ = new internal::data_< plain<I> >(ima); + init(ima); } - template <typename I> inline - plain<I>::plain() + void + plain<I>::init(const I& ima) { + mln_precondition(ima.has_data()); + this->data_ = new internal::data< plain<I> >(ima); } template <typename I> inline plain<I>& - plain<I>::operator=(const I& rhs) + plain<I>::operator=(const plain<I>& rhs) { mln_precondition(rhs.has_data()); + if (&rhs == this) + return *this; + this->destroy(); + init(rhs.data_->ima_); + return *this; + } + + template <typename I> + inline + plain<I>& + plain<I>::operator=(const I& ima) + { + mln_precondition(ima.has_data()); this->destroy(); - this->data_ = new internal::data_< plain<I> >(rhs); + init(ima); return *this; } @@ -148,6 +195,7 @@ inline plain<I>::operator I () const { + mln_precondition(this->has_data()); return clone(this->data_->ima_); }
participants (1)
-
Thierry Geraud