aimsdata  5.0.5
Neuroimaging data handling
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 
130  const std::vector<std::string> & ) {}
131  virtual bool filter( const carto::AttributedObject & ) const;
132  };
133 
134 
135  //---------------------------------------------------------------------------
137  {
138  int _label;
139 
140  public:
141 
142  LabelNodeFilter( int label ) : _label( label ) {}
143  inline virtual ~LabelNodeFilter() {}
144 
145  virtual bool filter( const carto::AttributedObject & ) const;
146  };
147 
148 
149  //---------------------------------------------------------------------------
150  class NameNodeFilter : public NodeFilter
151  {
152  std::string _name;
153  std::vector<std::string> _nameAttributes;
154 
155  public:
156 
157  NameNodeFilter( const std::string &name,
158  const std::vector<std::string> & name_attributes
159  = std::vector<std::string>() );
160  inline virtual ~NameNodeFilter() {}
161 
162  virtual void setRegionNameAttributes(
163  const std::vector<std::string> & attributes );
164  virtual bool filter( const carto::AttributedObject & ) const;
165  };
166 
167 
168  //---------------------------------------------------------------------------
169  template <>
171  {
172  const Graph *_roi;
173  carto::rc_ptr<Graph> _roiLife;
174  Graph::const_iterator _itRoi;
175  std::list<std::string> _attributeNames;
176  std::list<std::string>::const_iterator _itAttributeNames;
177  carto::rc_ptr< BucketMap<Void> > _bucketMap;
178  const BucketMap<Void>::Bucket *_bucket;
180  std::unique_ptr< NodeFilter > _nodeFilter;
181  carto::rc_ptr< VoxelSampler > _voxelSampler;
182  std::vector<std::string> _nameAttributes;
183 
184  static void
185  _findBucketAttributeNames( const Graph &graph,
186  const Vertex &node,
187  std::list<std::string> &attributeNames );
188 
189  Point3df _voxelSize;
190  void _getRequiredAttributes();
191 
192  public:
193 
194  MaskIteratorOf( const Graph &roi,
195  carto::rc_ptr< VoxelSampler > voxelSampler =
197  MaskIteratorOf( const Graph &roi, int label,
198  carto::rc_ptr< VoxelSampler > voxelSampler =
200  MaskIteratorOf( const Graph &roi, const std::string &label,
201  carto::rc_ptr< VoxelSampler > voxelSampler =
203  const std::vector<std::string> & regionNameAttributes =
204  std::vector<std::string>() );
206  carto::rc_ptr< VoxelSampler > voxelSampler =
208  MaskIteratorOf( const carto::rc_ptr<Graph> &roi, int label,
209  carto::rc_ptr< VoxelSampler > voxelSampler =
212  const std::string &label,
213  carto::rc_ptr< VoxelSampler > voxelSampler =
215  const std::vector<std::string> & regionNameAttributes =
216  std::vector<std::string>() );
217  MaskIteratorOf( const std::string &fileName,
218  carto::rc_ptr< VoxelSampler > voxelSampler =
220  MaskIteratorOf( const std::string &fileName, int label,
221  carto::rc_ptr< VoxelSampler > voxelSampler =
223  MaskIteratorOf( const std::string &fileName, const std::string &label,
224  carto::rc_ptr< VoxelSampler > voxelSampler =
226  const std::vector<std::string> & regionNameAttributes =
227  std::vector<std::string>() );
228  virtual ~MaskIteratorOf();
229 
230  virtual const Point3df voxelSize() const { return _voxelSize; }
231  virtual float voxelVolume() const { return _voxelSize[0]*_voxelSize[1]*_voxelSize[2]; }
232  virtual const Point3d &value() const;
233  virtual const Point3df valueMillimeters() const;
234  virtual void next();
235  virtual bool isValid() const;
236  virtual void restart();
237 
238  virtual bool contains( const Point3d & ) const;
239  virtual bool contains( const Point3df & ) const;
240  virtual const Point3d volumeDimension() const;
241  virtual std::string regionName() const;
249  virtual void setRegionNameAttributes( const std::vector<std::string>
250  & attributes );
251  };
252 
253 
254 
255  //---------------------------------------------------------------------------
256  template <class T>
257  class MaskIteratorOf< AimsData<T> > : public MaskIterator
258  {
259  mutable AimsData<T> *_data;
260  bool _freeData;
261  Point3d _current;
262  T _label;
263  Point3d _lastPoint;
264  bool _useLabel;
265  carto::rc_ptr< VoxelSampler > _voxelSampler;
266 
267  public:
268 
269  MaskIteratorOf( const AimsData<T> &data,
270  carto::rc_ptr< VoxelSampler > voxelSampler =
272  MaskIteratorOf( const AimsData<T> &data, const T &label,
273  carto::rc_ptr< VoxelSampler > voxelSampler =
275  MaskIteratorOf( const AimsData<T> &data, const specifiedLabels &labels,
276  carto::rc_ptr< VoxelSampler > voxelSampler =
278  MaskIteratorOf( const std::string &fileName,
279  carto::rc_ptr< VoxelSampler > voxelSampler =
281  MaskIteratorOf( const std::string &fileName, const T &label,
282  carto::rc_ptr< VoxelSampler > voxelSampler =
284  MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
285  carto::rc_ptr< VoxelSampler > voxelSampler =
287  virtual ~MaskIteratorOf();
288 
289  virtual const Point3df voxelSize() const
290  {
291  return Point3df( _data->sizeX(), _data->sizeY(), _data->sizeZ() );
292  }
293  virtual float voxelVolume() const
294  {
295  return _data->sizeX() * _data->sizeY() * _data->sizeZ();
296  }
297  virtual const Point3d &value() const;
298  virtual const Point3df valueMillimeters() const;
299  virtual void next();
300  virtual bool isValid() const;
301  virtual void restart();
302 
303  virtual bool contains( const Point3d & ) const;
304  virtual bool contains( const Point3df & ) const;
305  virtual const Point3d volumeDimension() const;
306  virtual std::string regionName() const;
307 
308  protected:
309 
310  virtual void restart( const T &label );
311  virtual void restart( const specifiedLabels &labels );
312 
313  };
314 
315 
316 
317 
318  //---------------------------------------------------------------------------
319  template <class T>
321  MaskIteratorOf( const AimsData<T> &data,
322  carto::rc_ptr< VoxelSampler > voxelSampler ) :
323  _data( const_cast< AimsData<T> *>( &data ) ),
324  _freeData( false ),
325  _voxelSampler( voxelSampler )
326  {
327  _useLabel = false;
328  restart();
329  }
330 
331  //---------------------------------------------------------------------------
332  template <class T>
334  MaskIteratorOf( const AimsData<T> &data, const T &label,
335  carto::rc_ptr< VoxelSampler > voxelSampler ) :
336  _data( const_cast< AimsData<T> *>( &data ) ),
337  _freeData( false ),
338  _voxelSampler( voxelSampler )
339  {
340  _useLabel = true;
341  restart( label );
342  }
343 
344  //---------------------------------------------------------------------------
345  template <class T>
347  MaskIteratorOf( const AimsData<T> &data, const specifiedLabels &labels,
348  carto::rc_ptr< VoxelSampler > voxelSampler ) :
349  _data( const_cast< AimsData<T> *>( &data ) ),
350  _freeData( false ),
351  _voxelSampler( voxelSampler )
352  {
353  _useLabel = true;
354  restart( labels );
355  }
356 
357  //---------------------------------------------------------------------------
358  template <class T>
360  MaskIteratorOf( const std::string &fileName,
361  carto::rc_ptr< VoxelSampler > voxelSampler ) :
362  _data( new AimsData<T> ),
363  _freeData( true ),
364  _voxelSampler( voxelSampler )
365  {
366  Reader< AimsData<T> > reader( fileName );
367  reader.read( *_data );
368  _useLabel = false;
369  restart();
370  }
371 
372  //---------------------------------------------------------------------------
373  template <class T>
375  MaskIteratorOf( const std::string &fileName, const T &label,
376  carto::rc_ptr< VoxelSampler > voxelSampler ) :
377  _data( new AimsData<T> ),
378  _freeData( true ),
379  _voxelSampler( voxelSampler )
380  {
381  Reader< AimsData<T> > reader( fileName );
382  reader.read( *_data );
383  _useLabel = true;
384  restart( label );
385  }
386 
387 
388  //---------------------------------------------------------------------------
389  template <class T>
391  MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
392  carto::rc_ptr< VoxelSampler > voxelSampler ) :
393  _data( new AimsData<T> ),
394  _freeData( true ),
395  _voxelSampler( voxelSampler )
396  {
397  Reader< AimsData<T> > reader( fileName );
398  reader.read( *_data );
399  _useLabel = true;
400  restart( labels );
401  }
402 
403  //---------------------------------------------------------------------------
404  template <class T>
405  MaskIteratorOf< AimsData<T> >::~MaskIteratorOf()
406  {
407  if ( _freeData ) delete _data;
408  }
409 
410 
411  //---------------------------------------------------------------------------
412  template <class T>
414  {
415  return _current;
416  }
417 
418  //---------------------------------------------------------------------------
419  template <class T>
421  {
422  if ( _voxelSampler.isNull() ) {
423  return Point3df( _current[0] * _data->sizeX(),
424  _current[1] * _data->sizeY(),
425  _current[2] * _data->sizeZ() );
426  } else {
427  const Point3df &voxelSample = _voxelSampler->value();
428  return Point3df( ( _current[0] + voxelSample[ 0 ] ) * _data->sizeX(),
429  ( _current[1] + voxelSample[ 1 ] ) * _data->sizeY(),
430  ( _current[2] + voxelSample[ 2 ] ) * _data->sizeZ() );
431  }
432  }
433 
434  //---------------------------------------------------------------------------
435  template <class T>
437  {
438  if ( (_useLabel && (*_data)( _current ) == (T) _label) ||
439  (!_useLabel && (*_data)( _current ) ) )
440  {
441  // Next sample in voxel
442  if ( ! _voxelSampler.isNull() )
443  {
444  _voxelSampler->next();
445  if ( _voxelSampler->isValid() )
446  return;
447  }
448  }
449 
450  if ( ! _voxelSampler.isNull() )
451  {
452  _voxelSampler->restart();
453  }
454 
455  if ( _current == _lastPoint ) // (to make it not valid )
456  {
457  _current[ 0 ] = _data->dimX();
458  _current[ 1 ] = _data->dimY();
459  _current[ 2 ] = _data->dimZ();
460  }
461  // Next point
462  do {
463  ++_current[ 0 ];
464  if ( _current[ 0 ] == _data->dimX() ) {
465  _current[ 0 ] = 0;
466  ++_current[ 1 ];
467  if ( _current[ 1 ] == _data->dimY() ) {
468  _current[ 1 ] = 0;
469  ++_current[ 2 ];
470  }
471  }
472  } while( isValid() && ( _useLabel ?
473  (*_data)( _current ) != (T) _label :
474  ! (*_data)( _current ) ) );
475  }
476 
477 
478  //---------------------------------------------------------------------------
479  template <class T>
481  {
482  return _current[ 2 ] < _data->dimZ();
483  }
484 
485  //---------------------------------------------------------------------------
486  template <class T>
488  {
489  if ( ! _voxelSampler.isNull() )
490  {
491  _voxelSampler->restart();
492  }
493 
494  _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
495  _lastPoint[ 0 ] = _data->dimX()-1;
496  _lastPoint[ 1 ] = _data->dimY()-1;
497  _lastPoint[ 2 ] = _data->dimZ()-1;
498 
499  if ( isValid() && ( _useLabel ?
500  (*_data)( _current ) != (T) _label :
501  ! (*_data)( _current ) ) ) next();
502  }
503 
504  //---------------------------------------------------------------------------
505  template <class T>
506  void MaskIteratorOf< AimsData<T> >::restart( const T &label )
507  {
508  if ( ! _voxelSampler.isNull() )
509  {
510  _voxelSampler->restart();
511  }
512 
513  _label = label;
514  _useLabel = true;
515  _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
516  _lastPoint[ 0 ] = _data->dimX()-1;
517  _lastPoint[ 1 ] = _data->dimY()-1;
518  _lastPoint[ 2 ] = _data->dimZ()-1;
519 
520  if ( isValid() && ( (*_data)( _current ) != _label ) )
521  {
522  next();
523  }
524  }
525 
526  //---------------------------------------------------------------------------
527  template <class T>
529  {
530  if ( ! _voxelSampler.isNull() )
531  {
532  _voxelSampler->restart();
533  }
534 
535  _label = (*_data)( labels.firstPoint );
536  _current = labels.firstPoint;
537  _lastPoint = labels.lastPoint;
538  }
539 
540 
541  //---------------------------------------------------------------------------
542  template <class T>
544  {
545  if ( p[ 0 ] >= 0 && p[ 0 ] < _data->dimX() &&
546  p[ 1 ] >= 0 && p[ 1 ] < _data->dimY() &&
547  p[ 2 ] >= 0 && p[ 2 ] < _data->dimZ() ) {
548  if ( _useLabel ) {
549  return (*_data)( p ) == _label;
550  } else {
551  return (*_data)( p );
552  }
553  }
554  return false;
555  }
556 
557  //---------------------------------------------------------------------------
558  template <class T>
560  {
561  const Point3d pixel( (short) rint( p[ 0 ] / _data->sizeX() ),
562  (short) rint( p[ 1 ] / _data->sizeY() ),
563  (short) rint( p[ 2 ] / _data->sizeZ() ) );
564  return contains( pixel );
565  }
566 
567 
568  //---------------------------------------------------------------------------
569  template <class T>
571  {
572  return Point3d( _data->dimX(), _data->dimY(), _data->dimZ() );
573  }
574 
575 
576  //---------------------------------------------------------------------------
577  template <class T>
579  {
580  if ( _useLabel ) return carto::toString( _label );
581  return "unknown";
582  }
583 
584 
585  //---------------------------------------------------------------------------
586  // maskIteratorFactories (to be continued)
587 
588  //---------------------------------------------------------------------------
589  carto::rc_ptr< MaskIterator > getMaskIterator( const std::string &fileName );
590 
591  //---------------------------------------------------------------------------
593  getMaskIterator( const std::string &fileName,
594  carto::rc_ptr< VoxelSampler > voxelSampler );
595 
596  //---------------------------------------------------------------------------
597  template <class T>
599  {
601  ( new MaskIteratorOf< AimsData<T> >( data ) );
602  }
603 
604  //---------------------------------------------------------------------------
605  template <class T>
608  carto::rc_ptr< VoxelSampler > voxelSampler )
609  {
611  ( new MaskIteratorOf< AimsData<T> >( data, voxelSampler ) );
612  }
613 
614  //---------------------------------------------------------------------------
615  carto::rc_ptr< MaskIterator > getMaskIterator( const std::string &fileName,
616  const Motion &motion );
617 
618  //---------------------------------------------------------------------------
620  getMaskIterator( const std::string &fileName,
621  carto::rc_ptr< VoxelSampler > voxelSampler,
622  const Motion &motion );
623 
624  //---------------------------------------------------------------------------
625  template <class T>
627  const Motion &motion )
628  {
630  ( new MotionedMaskIterator( getMaskIterator( data ), motion ) );
631  }
632 
633  //---------------------------------------------------------------------------
634  template <class T>
637  carto::rc_ptr< VoxelSampler > voxelSampler,
638  const Motion &motion )
639  {
641  ( new MotionedMaskIterator( getMaskIterator( data, voxelSampler ),
642  motion ) );
643  }
644 
645 
646 } // namespace aims
647 
648 #endif
virtual ~MaskIterator()
Definition: maskIterator.h:61
carto::rc_ptr< MaskIterator > getMaskIterator(const std::string &fileName)
virtual const Point3df voxelSize() const
Definition: maskIterator.h:230
The class for EcatSino data write operation.
Definition: border.h:44
virtual bool isValid() const =0
virtual const Point3d & value() const =0
virtual void setRegionNameAttributes(const std::vector< std::string > &)
Definition: maskIterator.h:129
virtual ~LabelNodeFilter()
Definition: maskIterator.h:143
virtual const Point3df voxelSize() const
Definition: maskIterator.h:289
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
virtual void next()=0
virtual float voxelVolume() const
Definition: maskIterator.h:231
virtual ~NodeFilter()
Definition: maskIterator.h:127
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
virtual bool contains(const Point3d &) const =0
virtual std::string regionName() const =0
LabelNodeFilter(int label)
Definition: maskIterator.h:142
virtual ~NameNodeFilter()
Definition: maskIterator.h:160
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:142
virtual const Point3df valueMillimeters() const =0
virtual void restart()=0
Affine 3D transformation.
virtual const Point3d volumeDimension() const =0