neighborhood3d.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_NEIGHBORHOOD3D_HH
00029 # define OLENA_CORE_NEIGHBORHOOD3D_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/window3d.hh>
00035 # include <algorithm>
00036 
00037 namespace oln {
00038 
00039   class neighborhood3d; // forward declaration
00040 
00044   template<>
00045   struct struct_elt_traits<neighborhood3d>: public
00046   struct_elt_traits<abstract::neighborhoodnd<neighborhood3d> >
00047   {
00048     enum { dim = 3 }; 
00049     typedef point3d point_type; 
00050     typedef dpoint3d dpoint_type; 
00051     typedef winiter< neighborhood3d > iter_type; 
00052     typedef winneighb< neighborhood3d > neighb_type; 
00053     typedef window3d win_type; 
00054   };
00055 
00064   class neighborhood3d :
00065     public abstract::neighborhoodnd< neighborhood3d >
00066   {
00067   public:
00068 
00069     typedef abstract::neighborhoodnd< neighborhood3d > super_type;
00071     typedef neighborhood3d self_type; 
00072 
00078     typedef struct_elt_traits< self_type >::iter_type   iter_type;
00079     typedef struct_elt_traits< self_type >::neighb_type neighb_type;
00080 
00086     typedef struct_elt_traits< self_type >::dpoint_type dpoint_type;
00087 
00088     friend class abstract::window_base<abstract::neighborhood<neighborhood3d>, neighborhood3d>;
00089 
00097     neighborhood3d&
00098     add(const dpoint_type& dp)
00099     {
00100       this->exact().add_(dp);
00101       return this->exact().add_(-dp);
00102     }
00103 
00113     neighborhood3d&
00114     add(coord slice, coord row, coord col)
00115     {
00116       return this->add(dpoint3d(slice, row, col));
00117     }
00118 
00122     neighborhood3d() : super_type()
00123     {}
00124 
00129     neighborhood3d(unsigned size) : super_type(size)
00130     {}
00131 
00137     neighborhood3d(unsigned n, const coord crd[]) : super_type()
00138     {
00139       for (unsigned i = 0; i < 3 * n; i += 3)
00140         add(dpoint_type(crd[i], crd[i+1], crd[i+2]));
00141     }
00142 
00144     static std::string
00145     name()
00146     {
00147       return std::string("neighborhood3d");
00148     }
00149 
00150   protected:
00151 
00160     coord
00161     delta_update_(const dpoint_type& dp)
00162     {
00163       delta_(abs(dp.slice()));
00164       delta_(abs(dp.row()));
00165       delta_(abs(dp.col()));
00166       return delta_;
00167     }
00168 
00169   };
00170 
00171   // std neighb
00172 
00177   inline const neighborhood3d&
00178   neighb_c6()
00179   {
00180     static const coord crd[] = { 0,  0,  1,
00181                                  0,  1,  0,
00182                                  1,  0,  0};
00183     static const neighborhood3d neighb(3, crd);
00184     return neighb;
00185   }
00186 
00191   inline const neighborhood3d&
00192   neighb_c18()
00193   {
00194     static const coord crd[] = { 0,  0,  1,
00195                                  0,  1, -1,
00196                                  0,  1,  0,
00197                                  0,  1,  1,
00198                                  1, -1,  0,
00199                                  1,  0, -1,
00200                                  1,  0,  0,
00201                                  1,  0,  1,
00202                                  1,  1,  0 };
00203     static neighborhood3d neighb(9, crd);
00204     return neighb;
00205   }
00206 
00211   inline const neighborhood3d&
00212   neighb_c26()
00213   {
00214     static const coord crd[] = { 0,  0,  1,
00215                                  0,  1, -1,
00216                                  0,  1,  0,
00217                                  0,  1,  1,
00218                                  1, -1, -1,
00219                                  1, -1,  0,
00220                                  1, -1,  1,
00221                                  1,  0, -1,
00222                                  1,  0,  0,
00223                                  1,  0,  1,
00224                                  1,  1, -1,
00225                                  1,  1,  0,
00226                                  1,  1,  1 };
00227     static neighborhood3d neighb(13, crd);
00228     return neighb;
00229   }
00230 
00231   // mk_neighb's
00232 
00246   inline neighborhood3d
00247   mk_neighb_block(unsigned nslices, unsigned nrows, unsigned ncols)
00248   {
00249     precondition(nslices >= 3 && (nslices % 2) == 1);
00250     precondition(nrows >= 3 && (nrows % 2) == 1);
00251     precondition(ncols >= 3 && (ncols % 2) == 1);
00252     neighborhood3d neighb(nrows * ncols);
00253     int half_nslices = nslices / 2;
00254     int half_nrows = nrows / 2;
00255     int half_ncols = ncols / 2;
00256     for (coord slice = - half_nslices; slice <= half_nslices; ++slice)
00257       for (coord row = - half_nrows; row <= half_nrows; ++row)
00258         for (coord col = (slice > 0) ? 0 : 1 ; col <= half_ncols; ++col)
00259             neighb.add(slice, row, col);
00260     return neighb;
00261   }
00262 
00268   inline neighborhood3d
00269   mk_neighb_cube(unsigned width)
00270   {
00271     return mk_neighb_block(width, width, width);
00272   }
00273 
00279   inline window3d
00280   mk_win_from_neighb(const neighborhood3d& n)
00281   {
00282     window3d win(n.card());
00283     for (unsigned i = 0; i < n.card(); ++i)
00284       win.add(n.dp(i));
00285     return win;
00286   }
00287 
00288 } // end of oln
00289 
00290 #endif // OLENA_CORE_NEIGHBORHOOD3D_HH

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