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, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, 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 
00263 
00276   inline neighborhood3d
00277   mk_neighb_ellipsoid(float zradius, float yradius, float xradius)
00278   {
00279     precondition(zradius > 0);
00280     precondition(yradius > 0);
00281     precondition(xradius > 0);
00282 
00283     neighborhood3d neighb;
00284     coord zmax = (coord)roundf(zradius);
00285     float zr2 = zradius * zradius;
00286     float yr2 = yradius * yradius;
00287     for (coord z = -zmax; z <= zmax; ++z)
00288       {
00289         /*
00290               x^2         y^2         z^2
00291            --------- + --------- + --------- = 1
00292            xradius^2   yradius^2   zradius^2
00293         */
00294 
00295         /* Set x to 0 in the above formula to find ymax.  */
00296         float v = 1 - z * z / zr2;
00297         if (v < 0) v = 0;       // Can happen because zmax has been rounded.
00298         coord ymax = (coord)roundf(yradius * sqrtf(v));
00299         for (coord y = -ymax; y <= ymax; ++y)
00300           {
00301             float w = v - y * y / yr2;
00302             if (w < 0) w = 0;   // Can happen because ymax has been rounded.
00303             coord xmax = (coord)roundf(xradius * sqrtf(w));
00304             for (coord x = z > 0 ? 0 : 1; x <= xmax; ++x)
00305               neighb.add(z, y, x);
00306           }
00307       }
00308     return neighb;
00309   }
00310 
00311 
00317   inline neighborhood3d
00318   mk_neighb_cube(unsigned width)
00319   {
00320     return mk_neighb_block(width, width, width);
00321   }
00322 
00328   inline window3d
00329   mk_win_from_neighb(const neighborhood3d& n)
00330   {
00331     window3d win(n.card());
00332     for (unsigned i = 0; i < n.card(); ++i)
00333       win.add(n.dp(i));
00334     return win;
00335   }
00336 
00342   inline neighborhood3d
00343   mk_neighb_ball(float radius)
00344   {
00345     return mk_neighb_ellipsoid(radius, radius, radius);
00346   }
00347 } // end of oln
00348 
00349 #endif // OLENA_CORE_NEIGHBORHOOD3D_HH

Generated on Tue Feb 20 20:20:12 2007 for Olena by  doxygen 1.5.1