proto-1.0 75: Add neighborhood

Index: ChangeLog from Simon Odou <simon@lrde.epita.fr> * tests/core/tests/neighborhood2d: New. * oln/core/abstract/neighborhood.hh: New. * oln/core/2d/neighborhood2d.hh: New. * oln/core/properties.hh: Add `neighborhood' category. oln/core/2d/neighborhood2d.hh | 133 ++++++++++++++++++++++++++ oln/core/abstract/neighborhood.hh | 192 ++++++++++++++++++++++++++++++++++++++ oln/core/properties.hh | 1 tests/core/tests/neighborhood2d | 17 +++ 4 files changed, 343 insertions(+) Index: tests/core/tests/neighborhood2d --- tests/core/tests/neighborhood2d (revision 0) +++ tests/core/tests/neighborhood2d (revision 0) @@ -0,0 +1,17 @@ +#include <oln/core/2d/neighborhood2d.hh> + +#include <ntg/all.hh> + +#include <cassert> + +using namespace oln; +using namespace ntg; + +int main() +{ + neighborhood2d w1; + w1.add(dpoint2d(0, 5)); + w1.add(dpoint2d(0, 5)); + neighborhood2d w2 = neighb_c8(); + return 0; +} Index: oln/core/abstract/neighborhood.hh --- oln/core/abstract/neighborhood.hh (revision 0) +++ oln/core/abstract/neighborhood.hh (revision 0) @@ -0,0 +1,192 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 59 Temple Place - Suite 330, 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 OLENA_CORE_NEIGHBORHOOD_HH +# define OLENA_CORE_NEIGHBORHOOD_HH + +# include <mlc/types.hh> +# include <oln/core/properties.hh> +# include <oln/core/coord.hh> +# include <oln/core/abstract/dpoint.hh> +# include <vector> +# include <utility> +# include <iostream> + +namespace oln { + + // fwd decl + namespace abstract { + template<class Exact> + struct neighborhood; + } + + // category + template <typename E> + struct set_category< abstract::neighborhood<E> > + { + typedef category::neighborhood ret; + }; + + /// properties of any type in category::neighborhood + + template <typename type> + struct props_of <category::neighborhood, type> + { + typedef mlc::true_type user_defined_; + + mlc_decl_prop(category::neighborhood, dpoint_type); + mlc_decl_prop(category::neighborhood, size_type); + + static void echo(std::ostream& ostr) + { + ostr << "props_of( category::neighborhood, " + << typeid(type).name() << ") = {" + << " size_type = " << typeid(size_type).name() << "," << std::endl + << " dpoint_type = " << typeid(dpoint_type).name() << " }" << std::endl; + } + + }; + + mlc_register_prop(category::neighborhood, dpoint_type); + mlc_register_prop(category::neighborhood, size_type); + + + namespace abstract { + + /*! + ** Structuring elements (set of dpoints). + ** + ** This abstract class defines several virtual methods for its + ** subclasses. Its goal is to deal with a set of 'move' points. + */ + template<class E> + class neighborhood : public mlc::any__best_memory<E> + { + + public: + + typedef oln_type_of(E, dpoint) dpoint_type; + + typedef E exact_type; + + static std::string + name() + { + return std::string("neighborhood<") + exact_type::name() + ">"; + } + + bool + has(const abstract::dpoint<dpoint_type>& dp) const + { + return this->exact().impl_has(dp.exact()); + } + + unsigned + card() const + { + return this->exact().impl_card(); + } + + exact_type& + add(const dpoint_type& dp) + { + return this->exact().impl_add(dp); + } + + dpoint_type + dp(unsigned i) const + { + return this->exact().impl_at(i); + } + + const dpoint_type + operator[](unsigned i) const + { + return this->exact().impl_at(i); + } + + protected: + + bool + impl_has(const dpoint_type& dp) const + { + return std::find(dp_.begin(), dp_.end(), dp) != dp_.end(); + } + + exact_type& + impl_add(const dpoint_type& dp) + { + if (!(impl_has(dp))) + { + this->dp_.push_back(dp); + this->dp_.push_back(-dp); + } + return this->exact(); + } + + unsigned + impl_card() const + { + return dp_.size(); + } + + const dpoint_type + impl_at(unsigned i) const + { + precondition(i < this->card()); + return dp_[i]; + } + + neighborhood() : dp_() + {}; + + neighborhood(unsigned size) : dp_() + { + dp_.reserve(size); + }; + + std::vector<dpoint_type> dp_; + + }; + } // end of abstract + +} // end of oln + +template<class E> +std::ostream& +operator<<(std::ostream& o, const oln::abstract::neighborhood<E>& se) +{ + unsigned c = se.card(); + o << "["; + for (unsigned i = 0; i < c; ++i) + o << se.dp(i); + o << "]"; + return o; +} + + +#endif // ! OLENA_CORE_NEIGHBORHOOD_HH Index: oln/core/2d/neighborhood2d.hh --- oln/core/2d/neighborhood2d.hh (revision 0) +++ oln/core/2d/neighborhood2d.hh (revision 0) @@ -0,0 +1,133 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 59 Temple Place - Suite 330, 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 OLENA_CORE_NEIGHBORHOOD2D_HH +# define OLENA_CORE_NEIGHBORHOOD2D_HH + +# include <oln/core/2d/dpoint2d.hh> +# include <oln/core/2d/size2d.hh> +# include <oln/core/coord.hh> +# include <oln/core/abstract/struct_elt.hh> + +namespace oln { + + class neighborhood2d; // forward declaration + + // category + template <> + struct set_category< neighborhood2d > { typedef category::struct_elt ret; }; + + // super_type + template <> + struct set_super_type< neighborhood2d > + { + typedef abstract::struct_elt< neighborhood2d > ret; + }; + + template <> + struct set_props< category::struct_elt, neighborhood2d > + : public props_of<category::struct_elt> + { + typedef dpoint2d dpoint_type; + typedef size2d size_type; + }; + + class neighborhood2d : public abstract::struct_elt<neighborhood2d> + { + + public: + + typedef abstract::struct_elt<neighborhood2d> super_type; + + /*! + ** \brief Construct a neighborhood of 2 dimensions. + */ + neighborhood2d() : super_type() + {} + + /*! + ** \brief Construct a neighborhood of 2 dimensions. + ** \arg size The number of element. + */ + neighborhood2d(unsigned size) : super_type(size) + {} + + /*! + ** \brief Construct a neighborhood of 2 dimensions from several points. + ** \arg n The number of element. + ** \arg crd The coordinates of the elements + */ + neighborhood2d(unsigned n, const coord_t crd[]) : super_type(n) + { + for (unsigned i = 0; i < 2 * n; i += 2) + this->add(dpoint_type(crd[i], crd[i+1])); + } + + neighborhood2d& + add(const dpoint_type& dp) + { + return this->exact().impl_add(dp); + } + + neighborhood2d& + add(coord_t row, coord_t col) + { + dpoint_type dp(row, col); + return add(dp); + } + + + /// Return the name of the type. + static std::string + name() + { + return std::string("neighborhood2d"); + } + + protected: + + }; + + inline const neighborhood2d& + neighb_c4() + { + static const coord_t crd[] = { 0,1, 1,0 }; + static const neighborhood2d neighb(2, crd); + return neighb; + } + + inline const neighborhood2d& + neighb_c8() + { + static const coord_t crd[] = { 0,1, 1,1, 1,0, 1,-1 }; + static const neighborhood2d neighb(4, crd); + return neighb; + } + +} // end of oln + +#endif // OLENA_CORE_NEIGHBORHOOD2D_HH Index: oln/core/properties.hh --- oln/core/properties.hh (revision 74) +++ oln/core/properties.hh (working copy) @@ -42,6 +42,7 @@ struct data_storage; struct image; struct struct_elt; + struct neighborhood; struct point; struct size; struct piter;
participants (1)
-
Simon Odou