aimsdata  4.7.0
Neuroimaging data handling
roiIterator.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_ROIITERATOR_H
36 #define AIMS_ROI_ROIITERATOR_H
37 
38 #include <aims/roi/maskIterator.h>
39 #include <cartobase/smart/rcptr.h>
40 #include <aims/io/reader.h>
41 #include <set>
42 #include <string>
43 
44 namespace aims
45 {
46 
47  //---------------------------------------------------------------------------
49  {
50  public:
51 
52  virtual ~ROILabelMap() {}
53 
54  virtual int32_t at( const Point3df & ) const = 0;
55  std::vector< std::string > roiNames;
56  };
57 
58 
59  //---------------------------------------------------------------------------
61  {
62  public:
63 
65  virtual ~VolumeROILabelMap() {}
66 
67  virtual int32_t at( const Point3df & ) const;
68 
69  private:
70  AimsData< int32_t > _volume;
71  };
72 
73 
74  //---------------------------------------------------------------------------
76  {
77  public:
78 
81 
82  virtual int32_t at( const Point3df & ) const;
83 
84  private:
85 
86  const Motion _motion;
87  };
88 
89 
90  //---------------------------------------------------------------------------
92  {
93  public:
94  virtual ~RoiIterator() {}
95 
96  virtual bool isValid() const = 0;
97  virtual void restart() = 0;
98  virtual size_t count() const = 0;
99 
100  virtual void next() = 0;
101  virtual carto::rc_ptr< MaskIterator > maskIterator() const = 0;
102  inline const Point3d volumeDimension() const;
103  inline const Point3df voxelSize() const;
104  inline float voxelVolume() const;
105  virtual std::string regionName() const = 0;
113  virtual void setRegionNameAttributes( const std::vector<std::string>& ) {}
117  virtual void setRegionNameAttributes( const std::string & );
118 
119 
120  virtual carto::rc_ptr< ROILabelMap > createLabelMap();
121  };
122 
123 
124  //---------------------------------------------------------------------------
126  {
127  public:
129  const Motion & );
130  virtual ~MotionedRoiIterator();
131 
132  virtual bool isValid() const;
133  virtual void restart();
134  virtual size_t count() const;
135 
136  virtual void next();
137  virtual carto::rc_ptr< MaskIterator > maskIterator() const;
138  inline const Point3d volumeDimension() const;
139  inline const Point3df voxelSize() const;
140  inline float voxelVolume() const;
141  virtual std::string regionName() const;
142  virtual void setRegionNameAttributes( const std::vector<std::string>& );
143 
144  virtual carto::rc_ptr< ROILabelMap > createLabelMap();
145 
146  protected:
149  };
150 
151 
152  //---------------------------------------------------------------------------
154  {
155  return maskIterator()->volumeDimension();
156  }
157 
158  //---------------------------------------------------------------------------
159  inline const Point3df RoiIterator::voxelSize() const
160  {
161  return maskIterator()->voxelSize();
162  }
163 
164  //---------------------------------------------------------------------------
165  inline float RoiIterator::voxelVolume() const
166  {
167  return maskIterator()->voxelVolume();
168  }
169 
170  //---------------------------------------------------------------------------
171  template <class T>
172  class RoiIteratorOf : public RoiIterator
173  {
174  };
175 
176 
177  //---------------------------------------------------------------------------
178  template <>
179  class RoiIteratorOf< Graph > : public RoiIterator
180  {
181  const Graph *_roi;
182  carto::rc_ptr< Graph > _roiLife;
183  std::set< std::string > _names;
184  std::set< std::string >::const_iterator _itNames;
185  carto::rc_ptr< VoxelSampler > _voxelSampler;
186  std::vector<std::string> _nameAttributes;
187 
188  public:
189 
190  RoiIteratorOf( const Graph &roi,
191  carto::rc_ptr< VoxelSampler > voxelSampler =
194  carto::rc_ptr< VoxelSampler > voxelSampler =
196  RoiIteratorOf( const std::string &fileName,
197  carto::rc_ptr< VoxelSampler > voxelSampler =
199  virtual ~RoiIteratorOf();
200 
201  virtual bool isValid() const;
202  virtual void restart();
203  virtual size_t count() const;
204 
212  virtual void setRegionNameAttributes( const std::vector<std::string>
213  & attributes );
214  virtual std::string regionName() const;
215 
216  virtual carto::rc_ptr< MaskIterator > maskIterator() const;
217  virtual void next();
218  };
219 
220 
221 
222  //---------------------------------------------------------------------------
223  template <class T>
224  class RoiIteratorOf< AimsData<T> > : public RoiIterator
225  {
226  mutable AimsData<T> *_data;
227  bool _freeData;
228  Point3d _current;
229  typename std::map< T, specifiedLabels > _labels;
230  typename std::map< T, specifiedLabels >::const_iterator _label;
231  carto::rc_ptr< VoxelSampler > _voxelSampler;
232 
233  void buildLabels();
234 
235  public:
236 
237  RoiIteratorOf( const AimsData<T> &data,
238  carto::rc_ptr< VoxelSampler > voxelSampler =
240  RoiIteratorOf( const std::string &fileName,
241  carto::rc_ptr< VoxelSampler > voxelSampler );
242  virtual ~RoiIteratorOf();
243 
244  virtual bool isValid() const;
245  virtual void restart();
246  virtual size_t count() const;
247 
248  virtual std::string regionName() const;
249 
250  virtual carto::rc_ptr< MaskIterator > maskIterator() const;
251  virtual void next();
252  };
253 
254 
255 
256 
257  //---------------------------------------------------------------------------
258  template <class T>
260  carto::rc_ptr< VoxelSampler > voxelSampler ) :
261  _data( const_cast< AimsData<T> *>( &data ) ),
262  _freeData( false ),
263  _voxelSampler( voxelSampler )
264  {
265  buildLabels();
266  restart();
267  }
268 
269  //---------------------------------------------------------------------------
270  template <class T>
271  RoiIteratorOf< AimsData<T> >::RoiIteratorOf( const std::string &fileName,
272  carto::rc_ptr< VoxelSampler > voxelSampler ) :
273  _data( new AimsData<T> ),
274  _freeData( true ),
275  _voxelSampler( voxelSampler )
276  {
277  Reader< AimsData<T> > reader( fileName );
278  reader.read( *_data );
279  buildLabels();
280  restart();
281  }
282 
283  //---------------------------------------------------------------------------
284  template <class T>
285  void RoiIteratorOf< AimsData<T> >::buildLabels()
286  {
287  Point3d p;
288  typename std::map< T, specifiedLabels >::iterator it;
289 
290  for( p[2] = 0; p[2] < _data->dimZ(); ++p[2] ) {
291  for( p[1] = 0; p[1] < _data->dimY(); ++p[1] ) {
292  for( p[0] = 0; p[0] < _data->dimX(); ++p[0] ) {
293  T v = (*_data)( p );
294  // NaN values are considered background like 0 (SPM does so)
295  if ( v && !std::isnan( v ) ) {
296  it = _labels.find( v );
297  _labels[ v ].lastPoint = p;
298 
299  if ( it == _labels.end() ) {
300  _labels[ v ].firstPoint = p;
301  }
302  }
303  }
304  }
305  }
306  }
307 
308 
309  //---------------------------------------------------------------------------
310  template <class T>
311  RoiIteratorOf< AimsData<T> >::~RoiIteratorOf()
312  {
313  if ( _freeData ) delete _data;
314  }
315 
316 
317  //---------------------------------------------------------------------------
318  template <class T>
321  {
323  ( new MaskIteratorOf< AimsData<T> >( *_data, _label->second,
324  _voxelSampler ) );
325  }
326 
327  //---------------------------------------------------------------------------
328  template <class T>
330  {
331  ++_label;
332  }
333 
334 
335  //---------------------------------------------------------------------------
336  template <class T>
338  {
339  return _label != _labels.end();
340  }
341 
342  //---------------------------------------------------------------------------
343  template <class T>
345  {
346  _label = _labels.begin();
347  }
348 
349  //---------------------------------------------------------------------------
350  template <class T>
352  {
353  return _labels.size();
354  }
355 
356  //---------------------------------------------------------------------------
357  template <class T>
359  {
360  return carto::toString( _label->first );
361  }
362 
363  //---------------------------------------------------------------------------
364  // roiIterator factories (to be continued)
365 
366  //---------------------------------------------------------------------------
368  getRoiIterator( const std::string &fileName,
369  carto::rc_ptr< VoxelSampler > voxelSampler =
371 
372  //---------------------------------------------------------------------------
373  template <class T>
376  carto::rc_ptr< VoxelSampler > voxelSampler =
378  {
380  ( new RoiIteratorOf< AimsData<T> >( data, voxelSampler ) );
381  }
382 
383  //---------------------------------------------------------------------------
385  getRoiIterator( const Graph &data,
386  carto::rc_ptr< VoxelSampler > voxelSampler =
388 
389  //---------------------------------------------------------------------------
390  carto::rc_ptr< RoiIterator > getRoiIterator( const std::string &fileName,
391  const Motion & );
392 
393  //---------------------------------------------------------------------------
395  getRoiIterator( const std::string &fileName,
396  carto::rc_ptr< VoxelSampler > voxelSampler,
397  const Motion & );
398 
399  //---------------------------------------------------------------------------
400  template <class T>
402  const Motion &motion )
403  {
404  return carto::
405  rc_ptr< RoiIterator >( new MotionedRoiIterator( getRoiIterator( data ),
406  motion ) );
407  }
408 
409  //---------------------------------------------------------------------------
411  const Motion &motion ) ;
412 
413  //---------------------------------------------------------------------------
414  template <class T>
417  carto::rc_ptr< VoxelSampler > voxelSampler,
418  const Motion &motion )
419  {
420  return carto::
421  rc_ptr< RoiIterator >
422  ( new MotionedRoiIterator( getRoiIterator( data, voxelSampler ),
423  motion ) );
424  }
425 
426  //---------------------------------------------------------------------------
428  getRoiIterator( const Graph &data,
429  carto::rc_ptr< VoxelSampler > voxelSampler,
430  const Motion &motion ) ;
431 
432  //---------------------------------------------------------------------------
433  extern template class RoiIteratorOf<AimsData<int16_t> >;
434  extern template class RoiIteratorOf<Graph>;
435 
436 } // namespace aims
437 
438 #endif
virtual void restart()=0
virtual int32_t at(const Point3df &) const =0
virtual bool isValid() const =0
virtual ~RoiIterator()
Definition: roiIterator.h:94
carto::rc_ptr< RoiIterator > getRoiIterator(const std::string &fileName, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual ~VolumeROILabelMap()
Definition: roiIterator.h:65
The class for EcatSino data write operation.
Definition: border.h:42
const Point3df voxelSize() const
Definition: roiIterator.h:159
virtual void setRegionNameAttributes(const std::vector< std::string > &)
Set region name attribute in graph.
Definition: roiIterator.h:113
float voxelVolume() const
Definition: roiIterator.h:165
virtual ~ROILabelMap()
Definition: roiIterator.h:52
virtual std::string regionName() const =0
std::vector< std::string > roiNames
Definition: roiIterator.h:55
const carto::rc_ptr< RoiIterator > _roiIterator
Definition: roiIterator.h:147
Generic reader for every format of Aims object.
Definition: reader.h:69
virtual void next()=0
virtual carto::rc_ptr< MaskIterator > maskIterator() const =0
virtual size_t count() const =0
std::string toString(const T &object)
const Point3d volumeDimension() const
Definition: roiIterator.h:153
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
Affine 3D transformation.