864: Add type of 2d images with border.

https://svn.lrde.epita.fr/svn/oln/trunk/olena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Add type of 2d images with border. * tests/core/Makefile.am (check_PROGRAMS): Add neighb2d. * oln/debug/print.hh: Update. * oln/core/2d/array2d.hh (operator[], blen_): New. * oln/core/2d/image2d.hh (npoints): Fix; rename as... (impl_npoints): ...this. (operator[]): Update. * oln/core/2d/image2d_b.hh: Update. * oln/core/2d/grid2d.hh (OLN_ENV_2D): New. * oln/core/equipment.hh (oln_coord): New. * oln/core/gen/dpoints_piter.hh (dps_): Change type. * oln/core/internal/image_base.hh (impl_npoints): New; default impl. * oln/core/internal/point_set_std_based.hh: Fix. oln/core/2d/array2d.hh | 27 +++ oln/core/2d/grid2d.hh | 4 oln/core/2d/image2d.hh | 9 - oln/core/2d/image2d_b.hh | 213 ++++++++++++++++++++----------- oln/core/equipment.hh | 2 oln/core/gen/dpoints_piter.hh | 9 - oln/core/internal/image_base.hh | 9 + oln/core/internal/point_set_std_based.hh | 2 oln/debug/print.hh | 55 +++++--- tests/core/Makefile.am | 2 10 files changed, 231 insertions(+), 101 deletions(-) Index: tests/core/Makefile.am --- tests/core/Makefile.am (revision 863) +++ tests/core/Makefile.am (working copy) @@ -23,6 +23,7 @@ point2d \ grid \ image2d \ + neighb2d \ npoints \ neighb2d \ window2d \ @@ -33,6 +34,7 @@ point2d_SOURCES = point2d.cc grid_SOURCES = grid.cc image2d_SOURCES = image2d.cc +neighb2d_SOURCES = neighb2d.cc npoints_SOURCES = npoints.cc window2d_SOURCES = window2d.cc neighb2d_SOURCES = neighb2d.cc Index: oln/debug/print.hh --- oln/debug/print.hh (revision 863) +++ oln/debug/print.hh (working copy) @@ -29,11 +29,7 @@ # define OLN_DEBUG_PRINT_HH # include <iostream> -# include <oln/core/abstract/image.hh> -# include <oln/core/abstract/image/hybrid/classical.hh> -# include <oln/core/abstract/iterator.hh> -# include <oln/core/spe/row.hh> -# include <oln/core/spe/col.hh> +# include <oln/core/concept/image.hh> # ifdef OLN_ENV_2D # include <oln/core/2d/point2d.hh> @@ -49,7 +45,7 @@ /// Fwd decl. template <typename I> - void print(const abstract::image<I>& input, std::ostream& ostr = std::cout); + void print(const Image<I>& input, std::ostream& ostr = std::cout); # ifndef OLN_INCLUDE_ONLY @@ -70,30 +66,47 @@ /// Generic version. + template <typename I> - void print(const abstract::image<I>& input, std::ostream& ostr) + void print_Gen(const Image<I>& input, std::ostream& ostr) { - oln_vtype(I, fwd_piter) p(input.topo()); + oln_fwd_piter(I) p(input.points()); for_all(p) ostr << p.to_point() << ':' << format(input(p)) << ' '; } + template <typename I> + void print(const Image<I>& input, std::ostream& ostr) + { + print_Gen(input, ostr); + } + + # ifdef OLN_ENV_2D + /// Default version. + + template <typename I> + void print_2D(const Image<I>&, const I& input, std::ostream& ostr) + { + print_Gen(input, ostr); + } + /// Version for classical 2D images. + template <typename I> - void print(const abstract::classical_2d_image<I>& input, + void print_2D(const Point_Wise_Accessible_Image<I>&, const I& input, std::ostream& ostr) { const oln_coord(I) - min_row = oln::min_row(input), - max_row = oln::max_row(input); + min_row = input.bbox().pmin().row(), + max_row = input.bbox().pmax().row(); + const oln_coord(I) + min_col = input.bbox().pmin().col(), + max_col = input.bbox().pmax().col(); for (oln_coord(I) row = min_row; row <= max_row; ++row) { - const oln_coord(I) - min_col = oln::min_col(input), - max_col = oln::max_col(input); for (oln_coord(I) col = min_col; col <= max_col; ++col) { point2d p(row, col); @@ -107,7 +120,13 @@ } } -# endif + template <typename I> + void print(const Image_2D<I>& input, std::ostream& ostr) + { + impl::print_2D(exact(input), exact(input), ostr); + } + +# endif // OLN_ENV_2D } // end of namespace oln::debug::impl @@ -115,13 +134,15 @@ /// Facade. template <typename I> - void print(const abstract::image<I>& input, std::ostream& ostr) + void print(const Image<I>& input, std::ostream& ostr) { - impl::print(input.exact(), ostr); + impl::print(exact(input), ostr); } + # endif // ! OLN_INCLUDE_ONLY + } // end of namespace oln::debug } // end of namespace oln Index: oln/core/2d/array2d.hh --- oln/core/2d/array2d.hh (revision 863) +++ oln/core/2d/array2d.hh (working copy) @@ -55,6 +55,9 @@ const T& operator()(C i, C j) const; T& operator()(C i, C j); + const T& operator[](std::size_t ind) const; + T& operator[](std::size_t ind); + bool has(C i, C j) const; std::size_t memsize() const; @@ -72,6 +75,7 @@ C imin_, jmin_, imax_, jmax_; C ilen_, jlen_; + std::size_t blen_; T* buffer_; T** array_; @@ -132,6 +136,22 @@ } template <typename T, typename C> + const T& array2d_<T, C>::operator[](std::size_t ind) const + { + precondition(buffer_ != 0); + precondition(ind < blen_); + return buffer_[ind]; + } + + template <typename T, typename C> + T& array2d_<T, C>::operator[](std::size_t ind) + { + precondition(buffer_ != 0); + precondition(ind < blen_); + return buffer_[ind]; + } + + template <typename T, typename C> bool array2d_<T, C>::has(C i, C j) const { return @@ -179,7 +199,7 @@ template <typename T, typename C> std::size_t array2d_<T, C>::ncells() const { - return std::size_t(ilen_) * std::size_t(jlen_); + return blen_; } template <typename T, typename C> @@ -187,7 +207,7 @@ { return // buffer_ - std::size_t(ilen_) * std::size_t(jlen_) * sizeof(T) + blen_ * sizeof(T) + // array_ std::size_t(ilen_) * sizeof(T*); @@ -196,7 +216,8 @@ template <typename T, typename C> void array2d_<T, C>::allocate_() { - buffer_ = new T[std::size_t(ilen_) * std::size_t(jlen_)]; + blen_ = std::size_t(ilen_) * std::size_t(jlen_); + buffer_ = new T[blen_]; array_ = new T*[std::size_t(ilen_)]; T* buf = buffer_ - jmin_; for (C i = 0; i < ilen_; ++i) Index: oln/core/2d/image2d.hh --- oln/core/2d/image2d.hh (revision 863) +++ oln/core/2d/image2d.hh (working copy) @@ -95,7 +95,7 @@ T& impl_index_read_write(unsigned i); T& impl_at(int row, int col); - std::size_t npoints() const; + std::size_t impl_npoints() const; box2d impl_bbox() const; box2d impl_points() const; @@ -157,7 +157,7 @@ { assert(this->has_data()); assert(i < this->npoints()); - return this->data_->buffer()[i]; + return this->data_->operator[](i); } template <typename T> @@ -179,7 +179,7 @@ { assert(this->has_data()); assert(i < this->npoints()); - return this->data_->buffer()[i]; + return this->data_->operator[](i); } template <typename T> @@ -190,8 +190,9 @@ } template <typename T> - std::size_t image2d<T>::npoints() const + std::size_t image2d<T>::impl_npoints() const { + // faster than the default code given by primitive_image_ assert(this->has_data()); return this->data_->ncells(); } Index: oln/core/2d/image2d_b.hh --- oln/core/2d/image2d_b.hh (revision 863) +++ oln/core/2d/image2d_b.hh (working copy) @@ -29,79 +29,124 @@ #ifndef OLN_CORE_2D_IMAGE2D_B_HH # define OLN_CORE_2D_IMAGE2D_B_HH -# include <oln/core/image_entry.hh> -# include <oln/core/gen/grid.hh> -# include <oln/core/internal/tracked_ptr.hh> +# include <oln/core/internal/image_base.hh> # include <oln/core/2d/array2d.hh> -# include <oln/core/2d/point2d.hh> -# include <oln/core/2d/topo2d.hh> -// For fwd_piter and bkd_piter virtual types. -# include <oln/core/iterator_vtypes.hh> +# include <oln/core/2d/box2d.hh> namespace oln { - // Forward declaration. + + // FIXME: Move it! + + namespace internal + { + + + template <typename P, typename T> + struct f_point_value_to_array_; + + template <typename T> + struct f_point_value_to_array_< point2d, T > + { + typedef array2d_<T, int> ret; + }; + + + template <typename P, typename T> + struct array_b_ + { + typedef typename f_point_value_to_array_<P, T>::ret array_t; + typedef typename P::coord coord; + + array_b_(const P& pmin, const P& pmax, unsigned border) + : array(pmin.row() - border, pmin.col() - border, + pmax.row() + border, pmax.col() + border), + border(border), + box(pmin, pmax) + { + } + + array_t array; + unsigned border; + box_<P> box; + }; + + + } // end of namespace oln::internal + + + // end of FIXME + + + + // Fwd decl. template <typename T> class image2d_b; - /// Virtual types associated to oln::image2d_b<T>. + /// Virtual types. template <typename T> struct vtypes< image2d_b<T> > { - typedef topo2d topo_type; + typedef point2d point; + + typedef int coord; + typedef unsigned index; + + typedef T value; + typedef const T& rvalue; + typedef T& lvalue; - typedef point2d point_type; + typedef box2d pset; + typedef internal::array_b_<point2d, T> data; - typedef mlc::false_ is_computed_type; - typedef T value_type; - typedef T& lvalue_type; + // FIXME: wrong qiter!!! }; - /// Super type declaration. + /// Super type. template <typename T> - struct set_super_type< image2d_b<T> > + struct super_trait_< image2d_b<T> > { - typedef image2d_b<T> self_t; - typedef image_entry<self_t> ret; + typedef image2d_b<T> current; + typedef internal::plain_primitive_image_<current> ret; }; /// General 2D image class. + template <typename T> - class image2d_b : public image_entry< image2d_b<T> > + class image2d_b : public internal::plain_primitive_image_< image2d_b<T> > { - typedef image2d_b<T> self_t; - typedef array2d<T> array_t; - + typedef image2d_b<T> current; + typedef internal::plain_primitive_image_<current> super; public: + stc_using(data); - /// Ctor without info. image2d_b(); - - /// Ctor using sizes. + image2d_b(const box2d& b, unsigned border = 2); image2d_b(unsigned nrows, unsigned ncols, unsigned border = 2); - /// Ctor using an existing topology. - image2d_b(const topo2d& topo); + bool impl_owns_(const point2d& p) const; - const topo2d& impl_topo() const; + bool impl_has(const point2d& p) const; + bool impl_has_at(int row, int col) const; - T impl_op_read(const point2d& p) const; - T impl_at(int row, int col) const; + const T& impl_read(const point2d& p) const; + const T& impl_index_read(unsigned i) const; + const T& impl_at(int row, int col) const; - T& impl_op_readwrite(const point2d& p); + T& impl_read_write(const point2d& p); + T& impl_index_read_write(unsigned i); T& impl_at(int row, int col); - T* adr_at(int row, int col); - const T* adr_at(int row, int col) const; + std::size_t impl_npoints() const; - private: + box2d impl_bbox() const; + box2d impl_points() const; - topo2d topo_; - internal::tracked_ptr<array_t> data_; + unsigned border() const; }; @@ -110,86 +155,108 @@ template <typename T> image2d_b<T>::image2d_b() - : topo_(), - data_() { } template <typename T> + image2d_b<T>::image2d_b(const box2d& b, unsigned border) + { + this->data_ = new data(b.pmin(), b.pmax(), border); + } + + template <typename T> image2d_b<T>::image2d_b(unsigned nrows, unsigned ncols, unsigned border) - : topo_(bbox2d(point2d(0, 0 ), - point2d(nrows - 1, ncols - 1)), - border), - data_(new array_t(0 - border, 0 - border, - nrows - 1 + border, ncols - 1 + border)) { + precondition(nrows != 0 and ncols != 0); + this->data_ = new data(point2d(0, 0), + point2d(nrows - 1, ncols - 1), + border); } template <typename T> - image2d_b<T>::image2d_b(const topo2d& topo) - : topo_(topo), - data_(new array_t(topo.bbox().pmin().row(), - topo.bbox().pmin().col(), - topo.bbox().pmax().row(), - topo.bbox().pmax().col())) + bool image2d_b<T>::impl_owns_(const point2d& p) const { + assert(this->has_data()); + return this->data_->array.has(p.row(), p.col()); } + template <typename T> + bool image2d_b<T>::impl_has(const point2d& p) const + { + assert(this->has_data()); + return this->data_->box.has(p); + } template <typename T> - const topo2d& image2d_b<T>::impl_topo() const + bool image2d_b<T>::impl_has_at(int row, int col) const { - return topo_; + assert(this->has_data()); + return this->data_->box.has(point2d(row, col)); } + template <typename T> + const T& image2d_b<T>::impl_read(const point2d& p) const + { + assert(this->has_data()); + return this->data_->array(p.row(), p.col()); + } template <typename T> - T image2d_b<T>::impl_op_read(const point2d& p) const + const T& image2d_b<T>::impl_index_read(unsigned i) const { - precondition(data_ != 0); - precondition(topo_.has_large(p)); - return data_->operator()(p.row(), p.col()); + assert(this->has_data()); + assert(i < this->npoints()); + return this->data_->array[i]; } template <typename T> - T image2d_b<T>::impl_at(int row, int col) const + const T& image2d_b<T>::impl_at(int row, int col) const { - precondition(data_ != 0); - precondition(data_->has(row, col)); - return data_->operator()(row, col); + assert(this->has_data()); + return this->data_->array(row, col); } + template <typename T> + T& image2d_b<T>::impl_read_write(const point2d& p) + { + assert(this->has_data()); + return this->data_->array(p.row(), p.col()); + } template <typename T> - T& image2d_b<T>::impl_op_readwrite(const point2d& p) + T& image2d_b<T>::impl_index_read_write(unsigned i) { - precondition(data_ != 0); - precondition(topo_.has_large(p)); - return data_->operator()(p.row(), p.col()); + assert(this->has_data()); + assert(i < this->npoints()); + return this->data_->array[i]; } template <typename T> T& image2d_b<T>::impl_at(int row, int col) { - precondition(data_->has(row, col)); - return data_->operator()(row, col); + assert(this->has_data()); + return this->data_->array(row, col); } + template <typename T> + box2d image2d_b<T>::impl_bbox() const + { + assert(this->has_data()); + return this->data_->box; + } template <typename T> - T* image2d_b<T>::adr_at(int row, int col) + box2d image2d_b<T>::impl_points() const { - precondition(data_ != 0); - precondition(data_->has(row, col)); - return &(data_->operator()(row, col)); + assert(this->has_data()); + return this->data_->box; } template <typename T> - const T* image2d_b<T>::adr_at(int row, int col) const + unsigned image2d_b<T>::border() const { - precondition(data_ != 0); - precondition(data_->has(row, col)); - return &(data_->operator()(row, col)); + assert(this->has_data()); + return this->data_->border; } # endif Index: oln/core/2d/grid2d.hh --- oln/core/2d/grid2d.hh (revision 863) +++ oln/core/2d/grid2d.hh (working copy) @@ -32,6 +32,10 @@ # include <oln/core/concept/grid.hh> +# define OLN_ENV_2D + + + namespace oln { Index: oln/core/equipment.hh --- oln/core/equipment.hh (revision 863) +++ oln/core/equipment.hh (working copy) @@ -55,6 +55,8 @@ stc_decl_associated_type( coord ); stc_decl_associated_type( ch_value ); +# define oln_coord(T) oln_typename_shortcut__(T, coord) + // d stc_decl_associated_type( data ); stc_decl_associated_type( dim ); Index: oln/core/gen/dpoints_piter.hh --- oln/core/gen/dpoints_piter.hh (revision 863) +++ oln/core/gen/dpoints_piter.hh (working copy) @@ -80,6 +80,9 @@ { public: + dpoints_piter_impl_(const dpoints_piter_impl_&); + void operator=(const dpoints_piter_impl_&); + void impl_invalidate(); bool impl_is_valid() const; @@ -91,7 +94,7 @@ protected: const P* p_ref_; - const std::vector<typename P::dpoint>* dps_; + std::vector<typename P::dpoint> dps_; unsigned n_, i_; P p_; @@ -168,7 +171,7 @@ const internal::dpoints_impl_<typename P::dpoint>& data) { p_ref_ = point_adr_(ref); - dps_ = &(data.dpoints()); + dps_ = data.dpoints(); n_ = data.size(); i_ = n_; postcondition(n_ != 0); @@ -206,7 +209,7 @@ void dpoints_piter_impl_<P>::update_p_() { - p_ = *p_ref_+ (*dps_)[i_]; + p_ = *p_ref_+ dps_[i_]; } } // end of namespace oln::internal Index: oln/core/internal/image_base.hh --- oln/core/internal/image_base.hh (revision 863) +++ oln/core/internal/image_base.hh (working copy) @@ -208,6 +208,8 @@ template <typename Exact> class primitive_image_ : public image_base_<Exact> { + public: + std::size_t impl_npoints() const; protected: primitive_image_(); }; @@ -309,6 +311,13 @@ { } + template <typename Exact> + std::size_t primitive_image_<Exact>::impl_npoints() const + { + precondition(this->has_data()); + return this->pset().npoints(); + } + /// plain_primitive_image_<Exact> template <typename Exact> Index: oln/core/internal/point_set_std_based.hh --- oln/core/internal/point_set_std_based.hh (revision 863) +++ oln/core/internal/point_set_std_based.hh (working copy) @@ -153,7 +153,7 @@ const typename point_set_std_based_<Exact>::std_container& point_set_std_based_<Exact>::con() const { - this->con_; + return this->con_; } # endif
participants (1)
-
Thierry Geraud