A.I.M.S


maskIterator.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 
35 #ifndef AIMS_ROI_MASKITERATOR_H
36 #define AIMS_ROI_MASKITERATOR_H
37 
38 #include <aims/data/data.h>
39 #include <aims/io/reader.h>
40 #include <aims/roi/roi.h>
41 #include <aims/roi/voxelSampling.h>
42 #include <cartobase/smart/rcptr.h>
44 #include <aims/resampling/motion.h>
45 #include <memory>
46 #include <string>
47 
48 namespace aims
49 {
50 
52  {
55  };
56 
57  //---------------------------------------------------------------------------
59  {
60  public:
61  virtual inline ~MaskIterator() {}
62 
63  // Returns the current point in voxel referential
64  virtual const Point3d &value() const = 0;
65  // Returns the current point in image or graph referential
66  virtual const Point3df valueMillimeters() const = 0;
67  virtual void next() = 0;
68  virtual bool isValid() const = 0;
69  virtual void restart() = 0;
70 
71  // Get voxel size
72  virtual const Point3df voxelSize() const = 0;
73  // Get voxel volume.
74  // - In cases with a motion, returns the volume of a transform voxel
75  // (which is a parallelepiped in the most general case)
76  // - In other cases, returns vs[0]*vs[1]*vs[2]
77  virtual float voxelVolume() const = 0;
78  // Check if a point (in voxel referential) belongs to the mask
79  virtual bool contains( const Point3d & ) const = 0;
80  // Check if a point (in millimeter graph/image) belongs to the mask
81  virtual bool contains( const Point3df & ) const = 0;
82  // Get maximum coordinate
83  virtual const Point3d volumeDimension() const = 0;
84  virtual std::string regionName() const = 0;
85  };
86 
87 
88  //---------------------------------------------------------------------------
90  {
91  public:
93  const Motion & );
94  virtual ~MotionedMaskIterator();
95 
96  virtual const Point3d &value() const;
97  virtual const Point3df valueMillimeters() const;
98  virtual void next();
99  virtual bool isValid() const;
100  virtual void restart();
101 
102  virtual const Point3df voxelSize() const;
103  virtual float voxelVolume() const;
104  virtual bool contains( const Point3d & ) const;
105  virtual bool contains( const Point3df & ) const;
106  virtual const Point3d volumeDimension() const;
107  virtual std::string regionName() const;
108 
109  protected:
113  };
114 
115  //---------------------------------------------------------------------------
116  template <class T>
118  {
119  };
120 
121 
122  //---------------------------------------------------------------------------
124  {
125  public:
126 
127  inline virtual ~NodeFilter() {}
128 
129  virtual bool filter( const carto::AttributedObject & ) const;
130  };
131 
132 
133  //---------------------------------------------------------------------------
135  {
136  int _label;
137 
138  public:
139 
140  LabelNodeFilter( int label ) : _label( label ) {}
141  inline virtual ~LabelNodeFilter() {}
142 
143  virtual bool filter( const carto::AttributedObject & ) const;
144  };
145 
146 
147  //---------------------------------------------------------------------------
148  class NameNodeFilter : public NodeFilter
149  {
150  std::string _name;
151 
152  public:
153 
154  NameNodeFilter( const std::string &name ) : _name( name ) {}
155  inline virtual ~NameNodeFilter() {}
156 
157  virtual bool filter( const carto::AttributedObject & ) const;
158  };
159 
160 
161  //---------------------------------------------------------------------------
162  template <>
164  {
165  const Graph *_roi;
166  carto::rc_ptr<Graph> _roiLife;
167  Graph::const_iterator _itRoi;
168  std::list<std::string> _attributeNames;
169  std::list<std::string>::const_iterator _itAttributeNames;
170  carto::rc_ptr< BucketMap<Void> > _bucketMap;
171  const BucketMap<Void>::Bucket *_bucket;
173  std::auto_ptr< NodeFilter > _nodeFilter;
174  carto::rc_ptr< VoxelSampler > _voxelSampler;
175 
176  static void
177  _findBucketAttributeNames( const Graph &graph,
178  const Vertex &node,
179  std::list<std::string> &attributeNames );
180 
181  Point3df _voxelSize;
182  void _getRequiredAttributes();
183 
184  public:
185 
186  MaskIteratorOf( const Graph &roi,
187  carto::rc_ptr< VoxelSampler > voxelSampler =
189  MaskIteratorOf( const Graph &roi, int label,
190  carto::rc_ptr< VoxelSampler > voxelSampler =
192  MaskIteratorOf( const Graph &roi, const std::string &label,
193  carto::rc_ptr< VoxelSampler > voxelSampler =
196  carto::rc_ptr< VoxelSampler > voxelSampler =
198  MaskIteratorOf( const carto::rc_ptr<Graph> &roi, int label,
199  carto::rc_ptr< VoxelSampler > voxelSampler =
202  const std::string &label,
203  carto::rc_ptr< VoxelSampler > voxelSampler =
205  MaskIteratorOf( const std::string &fileName,
206  carto::rc_ptr< VoxelSampler > voxelSampler =
208  MaskIteratorOf( const std::string &fileName, int label,
209  carto::rc_ptr< VoxelSampler > voxelSampler =
211  MaskIteratorOf( const std::string &fileName, const std::string &label,
212  carto::rc_ptr< VoxelSampler > voxelSampler =
214  virtual ~MaskIteratorOf();
215 
216  virtual const Point3df voxelSize() const { return _voxelSize; }
217  virtual float voxelVolume() const { return _voxelSize[0]*_voxelSize[1]*_voxelSize[2]; }
218  virtual const Point3d &value() const;
219  virtual const Point3df valueMillimeters() const;
220  virtual void next();
221  virtual bool isValid() const;
222  virtual void restart();
223 
224  virtual bool contains( const Point3d & ) const;
225  virtual bool contains( const Point3df & ) const;
226  virtual const Point3d volumeDimension() const;
227  virtual std::string regionName() const;
228  };
229 
230 
231 
232  //---------------------------------------------------------------------------
233  template <class T>
234  class MaskIteratorOf< AimsData<T> > : public MaskIterator
235  {
236  mutable AimsData<T> *_data;
237  bool _freeData;
238  Point3d _current;
239  T _label;
240  Point3d _lastPoint;
241  bool _useLabel;
242  carto::rc_ptr< VoxelSampler > _voxelSampler;
243 
244  public:
245 
246  MaskIteratorOf( const AimsData<T> &data,
247  carto::rc_ptr< VoxelSampler > voxelSampler =
249  MaskIteratorOf( const AimsData<T> &data, const T &label,
250  carto::rc_ptr< VoxelSampler > voxelSampler =
252  MaskIteratorOf( const AimsData<T> &data, const specifiedLabels &labels,
253  carto::rc_ptr< VoxelSampler > voxelSampler =
255  MaskIteratorOf( const std::string &fileName,
256  carto::rc_ptr< VoxelSampler > voxelSampler =
258  MaskIteratorOf( const std::string &fileName, const T &label,
259  carto::rc_ptr< VoxelSampler > voxelSampler =
261  MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
262  carto::rc_ptr< VoxelSampler > voxelSampler =
264  virtual ~MaskIteratorOf();
265 
266  virtual const Point3df voxelSize() const
267  {
268  return Point3df( _data->sizeX(), _data->sizeY(), _data->sizeZ() );
269  }
270  virtual float voxelVolume() const
271  {
272  return _data->sizeX() * _data->sizeY() * _data->sizeZ();
273  }
274  virtual const Point3d &value() const;
275  virtual const Point3df valueMillimeters() const;
276  virtual void next();
277  virtual bool isValid() const;
278  virtual void restart();
279 
280  virtual bool contains( const Point3d & ) const;
281  virtual bool contains( const Point3df & ) const;
282  virtual const Point3d volumeDimension() const;
283  virtual std::string regionName() const;
284 
285  protected:
286 
287  virtual void restart( const T &label );
288  virtual void restart( const specifiedLabels &labels );
289 
290  };
291 
292 
293 
294 
295  //---------------------------------------------------------------------------
296  template <class T>
297  MaskIteratorOf< AimsData<T> >::
298  MaskIteratorOf( const AimsData<T> &data,
299  carto::rc_ptr< VoxelSampler > voxelSampler ) :
300  _data( const_cast< AimsData<T> *>( &data ) ),
301  _freeData( false ),
302  _voxelSampler( voxelSampler )
303  {
304  _useLabel = false;
305  restart();
306  }
307 
308  //---------------------------------------------------------------------------
309  template <class T>
311  MaskIteratorOf( const AimsData<T> &data, const T &label,
312  carto::rc_ptr< VoxelSampler > voxelSampler ) :
313  _data( const_cast< AimsData<T> *>( &data ) ),
314  _freeData( false ),
315  _voxelSampler( voxelSampler )
316  {
317  _useLabel = true;
318  restart( label );
319  }
320 
321  //---------------------------------------------------------------------------
322  template <class T>
324  MaskIteratorOf( const AimsData<T> &data, const specifiedLabels &labels,
325  carto::rc_ptr< VoxelSampler > voxelSampler ) :
326  _data( const_cast< AimsData<T> *>( &data ) ),
327  _freeData( false ),
328  _voxelSampler( voxelSampler )
329  {
330  _useLabel = true;
331  restart( labels );
332  }
333 
334  //---------------------------------------------------------------------------
335  template <class T>
337  MaskIteratorOf( const std::string &fileName,
338  carto::rc_ptr< VoxelSampler > voxelSampler ) :
339  _data( new AimsData<T> ),
340  _freeData( true ),
341  _voxelSampler( voxelSampler )
342  {
343  Reader< AimsData<T> > reader( fileName );
344  reader.read( *_data );
345  _useLabel = false;
346  restart();
347  }
348 
349  //---------------------------------------------------------------------------
350  template <class T>
352  MaskIteratorOf( const std::string &fileName, const T &label,
353  carto::rc_ptr< VoxelSampler > voxelSampler ) :
354  _data( new AimsData<T> ),
355  _freeData( true ),
356  _voxelSampler( voxelSampler )
357  {
358  Reader< AimsData<T> > reader( fileName );
359  reader.read( *_data );
360  _useLabel = true;
361  restart( label );
362  }
363 
364 
365  //---------------------------------------------------------------------------
366  template <class T>
368  MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
369  carto::rc_ptr< VoxelSampler > voxelSampler ) :
370  _data( new AimsData<T> ),
371  _freeData( true ),
372  _voxelSampler( voxelSampler )
373  {
374  Reader< AimsData<T> > reader( fileName );
375  reader.read( *_data );
376  _useLabel = true;
377  restart( labels );
378  }
379 
380  //---------------------------------------------------------------------------
381  template <class T>
382  MaskIteratorOf< AimsData<T> >::~MaskIteratorOf()
383  {
384  if ( _freeData ) delete _data;
385  }
386 
387 
388  //---------------------------------------------------------------------------
389  template <class T>
390  const Point3d &MaskIteratorOf< AimsData<T> >::value() const
391  {
392  return _current;
393  }
394 
395  //---------------------------------------------------------------------------
396  template <class T>
397  const Point3df MaskIteratorOf< AimsData<T> >::valueMillimeters() const
398  {
399  if ( _voxelSampler.isNull() ) {
400  return Point3df( _current[0] * _data->sizeX(),
401  _current[1] * _data->sizeY(),
402  _current[2] * _data->sizeZ() );
403  } else {
404  const Point3df &voxelSample = _voxelSampler->value();
405  return Point3df( ( _current[0] + voxelSample[ 0 ] ) * _data->sizeX(),
406  ( _current[1] + voxelSample[ 1 ] ) * _data->sizeY(),
407  ( _current[2] + voxelSample[ 2 ] ) * _data->sizeZ() );
408  }
409  }
410 
411  //---------------------------------------------------------------------------
412  template <class T>
414  {
415  if ( (_useLabel && (*_data)( _current ) == (T) _label) ||
416  (!_useLabel && (*_data)( _current ) ) )
417  {
418  // Next sample in voxel
419  if ( ! _voxelSampler.isNull() )
420  {
421  _voxelSampler->next();
422  if ( _voxelSampler->isValid() )
423  return;
424  }
425  }
426 
427  if ( ! _voxelSampler.isNull() )
428  {
429  _voxelSampler->restart();
430  }
431 
432  if ( _current == _lastPoint ) // (to make it not valid )
433  {
434  _current[ 0 ] = _data->dimX();
435  _current[ 1 ] = _data->dimY();
436  _current[ 2 ] = _data->dimZ();
437  }
438  // Next point
439  do {
440  ++_current[ 0 ];
441  if ( _current[ 0 ] == _data->dimX() ) {
442  _current[ 0 ] = 0;
443  ++_current[ 1 ];
444  if ( _current[ 1 ] == _data->dimY() ) {
445  _current[ 1 ] = 0;
446  ++_current[ 2 ];
447  }
448  }
449  } while( isValid() && ( _useLabel ?
450  (*_data)( _current ) != (T) _label :
451  ! (*_data)( _current ) ) );
452  }
453 
454 
455  //---------------------------------------------------------------------------
456  template <class T>
457  bool MaskIteratorOf< AimsData<T> >::isValid() const
458  {
459  return _current[ 2 ] < _data->dimZ();
460  }
461 
462  //---------------------------------------------------------------------------
463  template <class T>
464  void MaskIteratorOf< AimsData<T> >::restart()
465  {
466  if ( ! _voxelSampler.isNull() )
467  {
468  _voxelSampler->restart();
469  }
470 
471  _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
472  _lastPoint[ 0 ] = _data->dimX()-1;
473  _lastPoint[ 1 ] = _data->dimY()-1;
474  _lastPoint[ 2 ] = _data->dimZ()-1;
475 
476  if ( isValid() && ( _useLabel ?
477  (*_data)( _current ) != (T) _label :
478  ! (*_data)( _current ) ) ) next();
479  }
480 
481  //---------------------------------------------------------------------------
482  template <class T>
483  void MaskIteratorOf< AimsData<T> >::restart( const T &label )
484  {
485  if ( ! _voxelSampler.isNull() )
486  {
487  _voxelSampler->restart();
488  }
489 
490  _label = label;
491  _useLabel = true;
492  _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
493  _lastPoint[ 0 ] = _data->dimX()-1;
494  _lastPoint[ 1 ] = _data->dimY()-1;
495  _lastPoint[ 2 ] = _data->dimZ()-1;
496 
497  if ( isValid() && ( (*_data)( _current ) != _label ) )
498  {
499  next();
500  }
501  }
502 
503  //---------------------------------------------------------------------------
504  template <class T>
505  void MaskIteratorOf< AimsData<T> >::restart( const specifiedLabels &labels )
506  {
507  if ( ! _voxelSampler.isNull() )
508  {
509  _voxelSampler->restart();
510  }
511 
512  _label = (*_data)( labels.firstPoint );
513  _current = labels.firstPoint;
514  _lastPoint = labels.lastPoint;
515  }
516 
517 
518  //---------------------------------------------------------------------------
519  template <class T>
520  bool MaskIteratorOf< AimsData<T> >::contains( const Point3d &p ) const
521  {
522  if ( p[ 0 ] >= 0 && p[ 0 ] < _data->dimX() &&
523  p[ 1 ] >= 0 && p[ 1 ] < _data->dimY() &&
524  p[ 2 ] >= 0 && p[ 2 ] < _data->dimZ() ) {
525  if ( _useLabel ) {
526  return (*_data)( p ) == _label;
527  } else {
528  return (*_data)( p );
529  }
530  }
531  return false;
532  }
533 
534  //---------------------------------------------------------------------------
535  template <class T>
536  bool MaskIteratorOf< AimsData<T> >::contains( const Point3df &p ) const
537  {
538  const Point3d pixel( (short) rint( p[ 0 ] / _data->sizeX() ),
539  (short) rint( p[ 1 ] / _data->sizeY() ),
540  (short) rint( p[ 2 ] / _data->sizeZ() ) );
541  return contains( pixel );
542  }
543 
544 
545  //---------------------------------------------------------------------------
546  template <class T>
547  const Point3d MaskIteratorOf< AimsData<T> >::volumeDimension() const
548  {
549  return Point3d( _data->dimX(), _data->dimY(), _data->dimZ() );
550  }
551 
552 
553  //---------------------------------------------------------------------------
554  template <class T>
555  std::string MaskIteratorOf< AimsData<T> >::regionName() const
556  {
557  if ( _useLabel ) return carto::toString( _label );
558  return "unknown";
559  }
560 
561 
562  //---------------------------------------------------------------------------
563  // maskIteratorFactories (to be continued)
564 
565  //---------------------------------------------------------------------------
566  carto::rc_ptr< MaskIterator > getMaskIterator( const std::string &fileName );
567 
568  //---------------------------------------------------------------------------
570  getMaskIterator( const std::string &fileName,
571  carto::rc_ptr< VoxelSampler > voxelSampler );
572 
573  //---------------------------------------------------------------------------
574  template <class T>
576  {
578  ( new MaskIteratorOf< AimsData<T> >( data ) );
579  }
580 
581  //---------------------------------------------------------------------------
582  template <class T>
585  carto::rc_ptr< VoxelSampler > voxelSampler )
586  {
588  ( new MaskIteratorOf< AimsData<T> >( data, voxelSampler ) );
589  }
590 
591  //---------------------------------------------------------------------------
592  carto::rc_ptr< MaskIterator > getMaskIterator( const std::string &fileName,
593  const Motion &motion );
594 
595  //---------------------------------------------------------------------------
597  getMaskIterator( const std::string &fileName,
598  carto::rc_ptr< VoxelSampler > voxelSampler,
599  const Motion &motion );
600 
601  //---------------------------------------------------------------------------
602  template <class T>
604  const Motion &motion )
605  {
607  ( new MotionedMaskIterator( getMaskIterator( data ), motion ) );
608  }
609 
610  //---------------------------------------------------------------------------
611  template <class T>
614  carto::rc_ptr< VoxelSampler > voxelSampler,
615  const Motion &motion )
616  {
618  ( new MotionedMaskIterator( getMaskIterator( data, voxelSampler ),
619  motion ) );
620  }
621 
622 
623 } // namespace aims
624 
625 #endif
virtual bool filter(const carto::AttributedObject &) const
virtual float voxelVolume() const
virtual float voxelVolume() const
Definition: maskIterator.h:270
virtual bool filter(const carto::AttributedObject &) const
virtual const Point3df voxelSize() const =0
virtual ~MaskIterator()
Definition: maskIterator.h:61
carto::rc_ptr< MaskIterator > getMaskIterator(const std::string &fileName)
virtual bool contains(const Point3d &) const
virtual bool isValid() const =0
virtual const Point3df voxelSize() const
Definition: maskIterator.h:216
virtual float voxelVolume() const =0
virtual const Point3d & value() const
virtual const Point3d volumeDimension() const
virtual const Point3d & value() const =0
virtual std::string regionName() const
virtual bool isValid() const
virtual ~LabelNodeFilter()
Definition: maskIterator.h:141
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
MotionedMaskIterator(const carto::rc_ptr< MaskIterator > &, const Motion &)
virtual void next()=0
virtual const Point3df valueMillimeters() const
virtual ~NodeFilter()
Definition: maskIterator.h:127
AimsVector< float, 3 > Point3df
Definition: vector.h:237
VSet::const_iterator const_iterator
carto::rc_ptr< MaskIterator > _maskIterator
Definition: maskIterator.h:110
Generic reader for every format of Aims object.
Definition: reader.h:69
AimsVector< int16_t, 3 > Point3d
Definition: vector.h:212
virtual const Point3df voxelSize() const
Definition: maskIterator.h:266
virtual bool filter(const carto::AttributedObject &) const
virtual bool contains(const Point3d &) const =0
virtual std::string regionName() const =0
LabelNodeFilter(int label)
Definition: maskIterator.h:140
virtual ~NameNodeFilter()
Definition: maskIterator.h:155
std::string toString(const T &object)
virtual bool read(T &obj, int border=0, const std::string *format=0, int frame=-1)
Finds the correct format and reads the object. if format is specified, this format is tried first...
Definition: reader_d.h:137
virtual const Point3df voxelSize() const
virtual const Point3df valueMillimeters() const =0
virtual void restart()=0
virtual float voxelVolume() const
Definition: maskIterator.h:217
Affine 3D transformation.
virtual const Point3d volumeDimension() const =0
NameNodeFilter(const std::string &name)
Definition: maskIterator.h:154