neighborhood2d.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CORE_NEIGHBORHOOD2D_HH
00029 # define OLENA_CORE_NEIGHBORHOOD2D_HH
00030 
00031 # include <oln/core/abstract/neighborhoodnd.hh>
00032 # include <oln/core/winiter.hh>
00033 # include <oln/core/accum.hh>
00034 # include <oln/core/window2d.hh>
00035 # include <oln/io/readable.hh>
00036 # include <algorithm>
00037 
00038 namespace oln {
00039 
00040   class neighborhood2d; // forward declaration
00041 
00045   template<>
00046   struct struct_elt_traits<neighborhood2d>: public
00047   struct_elt_traits<abstract::neighborhoodnd<neighborhood2d> >
00048   {
00049     enum { dim = 2 }; 
00050     typedef point2d point_type; 
00051     typedef dpoint2d dpoint_type; 
00052     typedef winiter< neighborhood2d > iter_type; 
00053     typedef winneighb< neighborhood2d > neighb_type; 
00054     typedef window2d win_type; 
00055   };
00056 
00065   class neighborhood2d :
00066     public abstract::neighborhoodnd< neighborhood2d >
00067   {
00068   public:
00069 
00070     typedef abstract::neighborhoodnd< neighborhood2d > super_type;
00072     typedef neighborhood2d self_type; 
00073 
00079     typedef struct_elt_traits< self_type >::iter_type   iter_type;
00080 
00081     typedef struct_elt_traits< self_type >::neighb_type neighb_type;
00082 
00088     typedef struct_elt_traits< self_type >::dpoint_type dpoint_type;
00089 
00090     friend class abstract::window_base<abstract::neighborhood<neighborhood2d>, neighborhood2d>;
00091 
00099     neighborhood2d&
00100     add(const dpoint_type& dp)
00101     {
00102       this->exact().add_(dp);
00103       return this->exact().add_(-dp);
00104     }
00105 
00114     neighborhood2d&
00115     add(coord row, coord col)
00116     {
00117       return this->add(dpoint_type(row, col));
00118     }
00119 
00123     neighborhood2d() : super_type()
00124     {}
00125 
00130     neighborhood2d(unsigned size) : super_type(size)
00131     {}
00132 
00138     neighborhood2d(unsigned n, const coord crd[]) : super_type(n)
00139     {
00140       for (unsigned i = 0; i < 2 * n; i += 2)
00141         add(dpoint_type(crd[i], crd[i+1]));
00142     }
00143 
00144     // Constructor used in io functions.
00145     neighborhood2d(const io::internal::anything& r) : super_type()
00146     {
00147       r.assign(*this);
00148     }
00149 
00151     static std::string
00152     name()
00153     {
00154       return std::string("neighborhood2d");
00155     }
00156 
00157   protected:
00158 
00167     coord
00168     delta_update_(const dpoint_type& dp)
00169     {
00170       delta_(abs(dp.row()));
00171       delta_(abs(dp.col()));
00172       return delta_;
00173     }
00174 
00175   };
00176 
00177   // std neighbs
00178 
00183   inline const neighborhood2d&
00184   neighb_c4()
00185   {
00186     static const coord crd[] = { 0,1,  1,0 };
00187     static const neighborhood2d neighb(2, crd);
00188     return neighb;
00189   }
00190 
00191 
00197   inline const neighborhood2d&
00198   neighb_c8()
00199   {
00200     static const coord crd[] = { 0,1,  1,1,  1,0,  1,-1 };
00201     static const neighborhood2d neighb(4, crd);
00202     return neighb;
00203   }
00204 
00205   // mk_neighb's
00206 
00217   inline neighborhood2d
00218   mk_neighb_rectangle(unsigned nrows, unsigned ncols)
00219   {
00220     precondition(nrows >= 3 && (nrows % 2) == 1);
00221     precondition(ncols >= 3 && (ncols % 2) == 1);
00222     neighborhood2d neighb(nrows * ncols);
00223     int half_nrows = nrows / 2, half_ncols = ncols / 2;
00224     for (coord row = - half_nrows; row <= half_nrows; ++row)
00225       for (coord col = (row <= 0)? 1 : 0; col <= half_ncols; ++col)
00226           neighb.add(row, col);
00227     return neighb;
00228   }
00229 
00235   inline neighborhood2d
00236   mk_neighb_square(unsigned width)
00237   {
00238     return mk_neighb_rectangle(width, width);
00239   }
00240 
00246   inline window2d
00247   mk_win_from_neighb(const neighborhood2d& n)
00248   {
00249     window2d win(n.card());
00250     for (unsigned i = 0; i < n.card(); ++i)
00251       win.add(n.dp(i));
00252     return win;
00253   }
00254 
00255 } // end of oln
00256 
00257 #endif // OLENA_CORE_NEIGHBORHOOD2D_HH

Generated on Thu Apr 15 20:13:13 2004 for Olena by doxygen 1.3.6-20040222