A.I.M.S


structuring_element.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_CONNECTIVITY_STRUCTURING_ELEMENT_2_H
35 #define AIMS_CONNECTIVITY_STRUCTURING_ELEMENT_2_H
36 
37 //--- aims -------------------------------------------------------------------
38 #include <aims/vector/vector.h> // Point3d
39 //--- carto ------------------------------------------------------------------
40 #include <cartobase/smart/rcptr.h> // smart pointers
41 #include <cartobase/smart/refwrapper.h> // wrapper to references
42 //--- std --------------------------------------------------------------------
43 #include <string>
44 #include <vector>
45 #include <set>
46 #include <map>
47 //--- forward declarations ---------------------------------------------------
48 namespace carto {
49  template <typename T> class Volume;
50 }
51 //----------------------------------------------------------------------------
52 
53 namespace aims {
54 
55  //==========================================================================
56  // STRUCTURING ELEMENT: BASE CLASS
57  //==========================================================================
90  public:
91  //----------------------------------------------------------------------
92  // TYPEDEF
93  //----------------------------------------------------------------------
95  typedef std::vector<Point3d>::iterator iterator;
98  typedef std::vector<Point3d>::const_iterator const_iterator;
99 
100  //----------------------------------------------------------------------
101  // CONSTRUCTOR
102  //----------------------------------------------------------------------
104  StructuringElement( const std::vector<Point3d> & vector ):
105  _vector(vector) {}
106  virtual ~StructuringElement() {}
107  // Use default compiler-supplied copy constructor and assignment operator
108  // StructuringElement( const StructuringElement & se );
109  // StructuringElement & operator= ( const StructuringElement & se );
110 
111  //----------------------------------------------------------------------
112  // ITERATORS
113  //----------------------------------------------------------------------
115  iterator begin() { return _vector.begin(); }
117  iterator end() { return _vector.end(); }
121  const_iterator begin() const { return _vector.begin(); }
125  const_iterator end() const { return _vector.end(); }
126 
127 
128  //----------------------------------------------------------------------
129  // ACCESSOR
130  //----------------------------------------------------------------------
131  size_t size() const { return _vector.size(); }
135  const std::vector<Point3d> & getVector() const { return _vector; }
146  std::vector<int> getAmplitude( const Point3d & origin = Point3d(0,0,0) ) const;
147 
149  virtual StructuringElement* clone() const { return new StructuringElement(*this); }
150  protected:
151  std::vector<Point3d> _vector;
152  };
153 
154  //==========================================================================
155  // STRUCTURED VOLUME
156  //==========================================================================
159  template <typename T>
161  {
162  public:
163  typedef typename std::vector<carto::reference_wrapper<T> >::iterator iterator;
164  typedef typename std::vector<carto::reference_wrapper<T> >::const_iterator const_iterator;
166  const StructuringElement & se ):
167  _vector()
168  {
170  for( i=se.begin(); i!=e; ++i )
171  _vector.push_back( carto::wrap::ref( vol( (*i)[0], (*i)[1], (*i)[2] ) ) );
172  }
174  typename StructuredVolume<T>::iterator begin() { return _vector.begin(); }
175  typename StructuredVolume<T>::iterator end() { return _vector.end(); }
176  typename StructuredVolume<T>::const_iterator begin() const { return _vector.begin(); }
177  typename StructuredVolume<T>::const_iterator end() const { return _vector.end(); }
178  protected:
179  std::vector<carto::reference_wrapper<T> > _vector;
180  };
181 
183  template <typename T>
185  {
186  public:
187  typedef typename std::vector<carto::reference_wrapper<const T> >::iterator iterator;
188  typedef typename std::vector<carto::reference_wrapper<const T> >::const_iterator const_iterator;
190  const StructuringElement & se ):
191  _vector()
192  {
194  for( i=se.begin(); i!=e; ++i )
195  _vector.push_back( carto::wrap::cref( vol( (*i)[0], (*i)[1], (*i)[2] ) ) );
196  }
198  typename StructuredConstVolume<T>::iterator begin() { return _vector.begin(); }
199  typename StructuredConstVolume<T>::iterator end() { return _vector.end(); }
200  typename StructuredConstVolume<T>::const_iterator begin() const { return _vector.begin(); }
201  typename StructuredConstVolume<T>::const_iterator end() const { return _vector.end(); }
202  protected:
203  std::vector<carto::reference_wrapper<const T> > _vector;
204  };
205 
206  //==========================================================================
207  // STRUCTURING ELEMENT: REFERENCE
208  //==========================================================================
214  class StructuringElementRef: public carto::rc_ptr<StructuringElement>
215  {
216  public:
219 
225  carto::rc_ptr<StructuringElement>() {}
231  carto::rc_ptr<StructuringElement>(se) {}
237  carto::rc_ptr<StructuringElement>(se) {}
238 
239  iterator begin() { return (*this)->begin(); }
240  iterator end() { return (*this)->end(); }
241  const_iterator begin() const { return (*this)->begin(); }
242  const_iterator end() const { return (*this)->end(); }
243 
244  size_t size() const { return (*this)->size(); }
245  const std::vector<Point3d> & getVector() const
246  { return (*this)->getVector(); }
247  std::vector<int>
248  getAmplitude( const Point3d & origin = Point3d(0,0,0) ) const
249  { return (*this)->getAmplitude(origin); }
250  };
251 
252  namespace strel {
254  extern StructuringElementRef none();
255 
256  //========================================================================
257  // STRUCTURING ELEMENT: SHAPE INTERFACE
258  //========================================================================
269  class Shape: public StructuringElement
270  {
271  friend class ShapeFactory;
272  public:
275  virtual ~Shape() {};
276  virtual Shape* clone() const = 0;
277  protected:
278  // Defined methods
279  void setParameters( const double amplitude = 1.,
280  const bool usecenter = false );
281  void setParameters( const Point3d & origin,
282  const double amplitude = 1.,
283  const bool usecenter = false );
284  void setParameters( const std::vector<double> & amplitude,
285  const bool usecenter = false );
286  // Pure virtual methods:
287  virtual void setParameters( const Point3d & origin,
288  const std::vector<double> & amplitude,
289  const bool usecenter = false ) = 0;
290  };
291 
292  //========================================================================
293  // STRUCTURING ELEMENT: SHAPE FACTORY
294  //========================================================================
317  class ShapeFactory {
318  public:
319  static Shape* create( const std::string & type,
320  const double amplitude = 1.,
321  const bool usecenter = false );
322  static Shape* create( const std::string & type,
323  const Point3d & origin,
324  const double amplitude = 1.,
325  const bool usecenter = false );
326  static Shape* create( const std::string & type,
327  const std::vector<double> & amplitude,
328  const bool usecenter = false );
329  static Shape* create( const std::string & type,
330  const Point3d & origin,
331  const std::vector<double> & amplitude,
332  const bool usecenter = false );
333  static void registerShape( const std::string & type,
334  const Shape & strel );
335  static std::set<std::string> shapes();
336  protected:
337  static void init();
338  static std::map<std::string,carto::rc_ptr<Shape> > & _map();
339  };
340 
341  //========================================================================
342  // STRUCTURING ELEMENT: CONNECTIVITY INTERFACE
343  //========================================================================
353  {
354  friend class ConnectivityFactory;
355  public:
358  virtual ~Connectivity() {};
359  virtual Connectivity* clone() const = 0;
360  //--------------------------------------------------------------------
361  // TYPEDEF
362  //--------------------------------------------------------------------
365  typedef const bool (&Matrix3x3x3Const)[3][3][3];
366  //--------------------------------------------------------------------
367  // METHODS
368  //--------------------------------------------------------------------
375  virtual Matrix3x3x3Const getMatrix() const = 0;
376  protected:
377  void setVectorFromMatrix( const Point3d & origin = Point3d(0,0,0),
378  const bool usecenter = false );
379  };
380 
381  //========================================================================
382  // STRUCTURING ELEMENT: CONNECTIVITY FACTORY
383  //========================================================================
422  {
423  public:
424  static Connectivity* create( const std::string & type,
425  const bool usecenter = false );
426  static Connectivity* create( const std::string & type,
427  const Point3d & origin,
428  const bool usecenter = false );
429  static void registerConnectivity( const std::string & type,
430  const Connectivity & strel );
431  static std::set<std::string> connectivities();
432  protected:
433  static void init();
434  static std::map<std::string,carto::rc_ptr<Connectivity> > & _map();
435  };
436 
437  } // namespace strel
438 } // namespace aims
439 
440 
441 //============================================================================
442 // STRUCTURING ELEMENT: SHAPE DERIVED CLASSES
443 //============================================================================
444 #define AIMS_DECLARE_STREL_SHAPE( NAME ) \
445  class NAME: public Shape \
446  { \
447  public: \
448  typedef StructuringElement::iterator iterator; \
449  typedef StructuringElement::const_iterator const_iterator; \
450  NAME( const double amplitude = 1., \
451  const bool usecenter = false ): Shape() \
452  { Shape::setParameters( amplitude, usecenter ); } \
453  NAME( const Point3d & origin, \
454  const double amplitude = 1., \
455  const bool usecenter = false ): Shape() \
456  { Shape::setParameters( origin, amplitude, usecenter ); } \
457  NAME( const std::vector<double> & amplitude, \
458  const bool usecenter = false ): Shape() \
459  { Shape::setParameters( amplitude, usecenter ); } \
460  NAME( const Point3d & origin, \
461  const std::vector<double> & amplitude, \
462  const bool usecenter = false ): Shape() \
463  { setParameters( origin, amplitude, usecenter ); } \
464  virtual ~NAME() {} \
465  protected: \
466  virtual void setParameters( const Point3d & origin, \
467  const std::vector<double> & amplitude, \
468  const bool usecenter = false ); \
469  virtual NAME* clone() const { return new NAME(*this); } \
470  }
471 
472 namespace aims {
473  namespace strel {
474  AIMS_DECLARE_STREL_SHAPE( Cube );
475  AIMS_DECLARE_STREL_SHAPE( SquareXY );
476  AIMS_DECLARE_STREL_SHAPE( SquareXZ );
477  AIMS_DECLARE_STREL_SHAPE( SquareYZ );
478  AIMS_DECLARE_STREL_SHAPE( Sphere );
479  AIMS_DECLARE_STREL_SHAPE( DiskXY );
480  AIMS_DECLARE_STREL_SHAPE( DiskXZ );
481  AIMS_DECLARE_STREL_SHAPE( DiskYZ );
482  AIMS_DECLARE_STREL_SHAPE( Cross );
483  AIMS_DECLARE_STREL_SHAPE( CrossXY );
484  AIMS_DECLARE_STREL_SHAPE( CrossXZ );
485  AIMS_DECLARE_STREL_SHAPE( CrossYZ );
486  AIMS_DECLARE_STREL_SHAPE( DiagonalCross );
487  AIMS_DECLARE_STREL_SHAPE( DiagonalCrossXY );
488  AIMS_DECLARE_STREL_SHAPE( DiagonalCrossXZ );
489  AIMS_DECLARE_STREL_SHAPE( DiagonalCrossYZ );
490  } // namespace strel
491 } // namespace aims
492 
493 //============================================================================
494 // STRUCTURING ELEMENT: CONNECTIVITY DERIVED CLASSES
495 //============================================================================
496 #define AIMS_DECLARE_STREL_CONNECTIVITY( NAME ) \
497  class NAME: public Connectivity \
498  { \
499  public: \
500  typedef StructuringElement::iterator iterator; \
501  typedef StructuringElement::const_iterator const_iterator; \
502  NAME(): Connectivity() { setVectorFromMatrix(); } \
503  virtual ~NAME() {} \
504  Connectivity::Matrix3x3x3Const getMatrix() const; \
505  protected: \
506  virtual NAME* clone() const \
507  { return new NAME(*this); } \
508  }
509 
510 namespace aims {
511  namespace strel {
512  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4XY );
513  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4XZ );
514  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4YZ );
515  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4XYDiag );
516  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4XZDiag );
517  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity4YZDiag );
518  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity6XYZ );
519  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity8XY );
520  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity8XZ );
521  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity8YZ );
522  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity8XYZ );
523  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity18XYZ );
524  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity26XYZ );
525  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9XYZMinus );
526  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9XYZPlus );
527  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9XZYMinus );
528  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9XZYPlus );
529  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9YZXMinus );
530  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity9YZXPlus );
531  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XMinusY );
532  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XPlusY );
533  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XYMinus );
534  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XYPlus );
535  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XMinusZ );
536  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XPlusZ );
537  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XZMinus );
538  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5XZPlus );
539  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5YMinusZ );
540  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5YPlusZ );
541  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5YZMinus );
542  AIMS_DECLARE_STREL_CONNECTIVITY( Connectivity5YZPlus );
543  } // namespace strel
544 } // namespace aims
545 
546 #endif
static std::set< std::string > connectivities()
const std::vector< Point3d > & getVector() const
Return std::vector
const std::vector< Point3d > & getVector() const
iterator end()
Return iterator to end.
StructuredVolume(carto::Volume< T > &vol, const StructuringElement &se)
virtual Matrix3x3x3Const getMatrix() const =0
return the Matrix3x3x3Const representing a structuring element.
StructuredConstVolume< T >::const_iterator begin() const
StructuringElementRef none()
Pointer to an empty aims::StructuringElement.
AIMS_DECLARE_STREL_SHAPE(Cube)
AIMS_DECLARE_STREL_CONNECTIVITY(Connectivity4XY)
aims::strel::Connectivity pure abstract class representing a "connectivity" 3D structuring element...
reference_wrapper< const T > cref(const T &ref)
static std::map< std::string, carto::rc_ptr< Connectivity > > & _map()
virtual StructuringElement * clone() const
Clone the StructuringElement.
aims::strel::Connectivity generator
Class giving access to an iterator over the voxels of a volume inside of a structuring element...
aims::strel::Shape generator
StructuringElement::iterator iterator
void setVectorFromMatrix(const Point3d &origin=Point3d(0, 0, 0), const bool usecenter=false)
static void registerConnectivity(const std::string &type, const Connectivity &strel)
StructuredVolume< T >::iterator begin()
virtual Shape * clone() const =0
Clone the StructuringElement.
std::vector< carto::reference_wrapper< const T > > _vector
StructuringElement::const_iterator const_iterator
StructuringElement::const_iterator const_iterator
aims::StructuringElement class representing a 3D structuring element.
StructuringElement::const_iterator const_iterator
StructuringElementRef(StructuringElement *se)
New object constructor.
std::vector< int > getAmplitude(const Point3d &origin=Point3d(0, 0, 0)) const
StructuredVolume< T >::const_iterator begin() const
const_iterator end() const
Convenient handle for a StructuringElement.
StructuredVolume< T >::const_iterator end() const
virtual Connectivity * clone() const =0
Clone the StructuringElement.
const_iterator begin() const
Return const_iterator to beginning.
StructuringElementRef()
Default constructor.
StructuringElement(const std::vector< Point3d > &vector)
std::vector< Point3d >::const_iterator const_iterator
StructuredConstVolume< T >::const_iterator end() const
StructuredConstVolume< T >::iterator end()
StructuredConstVolume(const carto::Volume< T > &vol, const StructuringElement &se)
StructuringElement::iterator iterator
std::vector< carto::reference_wrapper< const T > >::const_iterator const_iterator
std::vector< carto::reference_wrapper< T > >::const_iterator const_iterator
static void registerShape(const std::string &type, const Shape &strel)
StructuringElementRef(const carto::rc_ptr< StructuringElement > &se)
Copy constructor.
StructuredConstVolume< T >::iterator begin()
StructuredVolume< T >::iterator end()
AimsVector< int16_t, 3 > Point3d
Definition: vector.h:212
iterator begin()
Return iterator to beginning.
const_iterator begin() const
std::vector< int > getAmplitude(const Point3d &origin=Point3d(0, 0, 0)) const
Maximum distance to origin in each direction.
std::vector< Point3d > _vector
const bool(& Matrix3x3x3Const)[3][3][3]
static std::set< std::string > shapes()
std::vector< carto::reference_wrapper< T > >::iterator iterator
static Connectivity * create(const std::string &type, const bool usecenter=false)
std::vector< Point3d >::iterator iterator
void setParameters(const double amplitude=1., const bool usecenter=false)
aims::strel::Shape pure abstract class representing a "shape" 3D structuring element.
std::vector< carto::reference_wrapper< const T > >::iterator iterator
static std::map< std::string, carto::rc_ptr< Shape > > & _map()
static Shape * create(const std::string &type, const double amplitude=1., const bool usecenter=false)
StructuringElement::iterator iterator
std::vector< carto::reference_wrapper< T > > _vector
const_iterator end() const
Return const_iterator to end.
reference_wrapper< T > ref(T &ref)