A.I.M.S


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;
106 
108  };
109 
110 
111  //---------------------------------------------------------------------------
113  {
114  public:
116  const Motion & );
117  virtual ~MotionedRoiIterator();
118 
119  virtual bool isValid() const;
120  virtual void restart();
121  virtual size_t count() const;
122 
123  virtual void next();
125  inline const Point3d volumeDimension() const;
126  inline const Point3df voxelSize() const;
127  inline float voxelVolume() const;
128  virtual std::string regionName() const;
129 
131 
132  protected:
135  };
136 
137 
138  //---------------------------------------------------------------------------
140  {
141  return maskIterator()->volumeDimension();
142  }
143 
144  //---------------------------------------------------------------------------
145  inline const Point3df RoiIterator::voxelSize() const
146  {
147  return maskIterator()->voxelSize();
148  }
149 
150  //---------------------------------------------------------------------------
151  inline float RoiIterator::voxelVolume() const
152  {
153  return maskIterator()->voxelVolume();
154  }
155 
156  //---------------------------------------------------------------------------
157  template <class T>
158  class RoiIteratorOf : public RoiIterator
159  {
160  };
161 
162 
163  //---------------------------------------------------------------------------
164  template <>
165  class RoiIteratorOf< Graph > : public RoiIterator
166  {
167  const Graph *_roi;
168  carto::rc_ptr< Graph > _roiLife;
169  std::set< std::string > _names;
170  std::set< std::string >::const_iterator _itNames;
171  carto::rc_ptr< VoxelSampler > _voxelSampler;
172 
173  public:
174 
175  RoiIteratorOf( const Graph &roi,
176  carto::rc_ptr< VoxelSampler > voxelSampler =
179  carto::rc_ptr< VoxelSampler > voxelSampler =
181  RoiIteratorOf( const std::string &fileName,
182  carto::rc_ptr< VoxelSampler > voxelSampler =
184  virtual ~RoiIteratorOf();
185 
186  virtual bool isValid() const;
187  virtual void restart();
188  virtual size_t count() const;
189 
190  virtual std::string regionName() const;
191 
193  virtual void next();
194  };
195 
196 
197 
198  //---------------------------------------------------------------------------
199  template <class T>
200  class RoiIteratorOf< AimsData<T> > : public RoiIterator
201  {
202  mutable AimsData<T> *_data;
203  bool _freeData;
204  Point3d _current;
205  typename std::map< T, specifiedLabels > _labels;
206  typename std::map< T, specifiedLabels >::const_iterator _label;
207  carto::rc_ptr< VoxelSampler > _voxelSampler;
208 
209  void buildLabels();
210 
211  public:
212 
213  RoiIteratorOf( const AimsData<T> &data,
214  carto::rc_ptr< VoxelSampler > voxelSampler =
216  RoiIteratorOf( const std::string &fileName,
217  carto::rc_ptr< VoxelSampler > voxelSampler );
218  virtual ~RoiIteratorOf();
219 
220  virtual bool isValid() const;
221  virtual void restart();
222  virtual size_t count() const;
223 
224  virtual std::string regionName() const;
225 
227  virtual void next();
228  };
229 
230 
231 
232 
233  //---------------------------------------------------------------------------
234  template <class T>
236  carto::rc_ptr< VoxelSampler > voxelSampler ) :
237  _data( const_cast< AimsData<T> *>( &data ) ),
238  _freeData( false ),
239  _voxelSampler( voxelSampler )
240  {
241  buildLabels();
242  restart();
243  }
244 
245  //---------------------------------------------------------------------------
246  template <class T>
247  RoiIteratorOf< AimsData<T> >::RoiIteratorOf( const std::string &fileName,
248  carto::rc_ptr< VoxelSampler > voxelSampler ) :
249  _data( new AimsData<T> ),
250  _freeData( true ),
251  _voxelSampler( voxelSampler )
252  {
253  Reader< AimsData<T> > reader( fileName );
254  reader.read( *_data );
255  buildLabels();
256  restart();
257  }
258 
259  //---------------------------------------------------------------------------
260  template <class T>
261  void RoiIteratorOf< AimsData<T> >::buildLabels()
262  {
263  Point3d p;
264  typename std::map< T, specifiedLabels >::iterator it;
265 
266  for( p[2] = 0; p[2] < _data->dimZ(); ++p[2] ) {
267  for( p[1] = 0; p[1] < _data->dimY(); ++p[1] ) {
268  for( p[0] = 0; p[0] < _data->dimX(); ++p[0] ) {
269  T v = (*_data)( p );
270  // NaN values are considered background like 0 (SPM does so)
271  if ( v && !isnan( v ) ) {
272  it = _labels.find( v );
273  _labels[ v ].lastPoint = p;
274 
275  if ( it == _labels.end() ) {
276  _labels[ v ].firstPoint = p;
277  }
278  }
279  }
280  }
281  }
282  }
283 
284 
285  //---------------------------------------------------------------------------
286  template <class T>
287  RoiIteratorOf< AimsData<T> >::~RoiIteratorOf()
288  {
289  if ( _freeData ) delete _data;
290  }
291 
292 
293  //---------------------------------------------------------------------------
294  template <class T>
296  maskIterator() const
297  {
299  ( new MaskIteratorOf< AimsData<T> >( *_data, _label->second,
300  _voxelSampler ) );
301  }
302 
303  //---------------------------------------------------------------------------
304  template <class T>
306  {
307  ++_label;
308  }
309 
310 
311  //---------------------------------------------------------------------------
312  template <class T>
313  bool RoiIteratorOf< AimsData<T> >::isValid() const
314  {
315  return _label != _labels.end();
316  }
317 
318  //---------------------------------------------------------------------------
319  template <class T>
320  void RoiIteratorOf< AimsData<T> >::restart()
321  {
322  _label = _labels.begin();
323  }
324 
325  //---------------------------------------------------------------------------
326  template <class T>
327  size_t RoiIteratorOf< AimsData<T> >::count() const
328  {
329  return _labels.size();
330  }
331 
332  //---------------------------------------------------------------------------
333  template <class T>
334  std::string RoiIteratorOf< AimsData<T> >::regionName() const
335  {
336  return carto::toString( _label->first );
337  }
338 
339  //---------------------------------------------------------------------------
340  // roiIterator factories (to be continued)
341 
342  //---------------------------------------------------------------------------
344  getRoiIterator( const std::string &fileName,
345  carto::rc_ptr< VoxelSampler > voxelSampler =
347 
348  //---------------------------------------------------------------------------
349  template <class T>
352  carto::rc_ptr< VoxelSampler > voxelSampler =
354  {
356  ( new RoiIteratorOf< AimsData<T> >( data, voxelSampler ) );
357  }
358 
359  //---------------------------------------------------------------------------
361  getRoiIterator( const Graph &data,
362  carto::rc_ptr< VoxelSampler > voxelSampler =
364 
365  //---------------------------------------------------------------------------
366  carto::rc_ptr< RoiIterator > getRoiIterator( const std::string &fileName,
367  const Motion & );
368 
369  //---------------------------------------------------------------------------
371  getRoiIterator( const std::string &fileName,
372  carto::rc_ptr< VoxelSampler > voxelSampler,
373  const Motion & );
374 
375  //---------------------------------------------------------------------------
376  template <class T>
378  const Motion &motion )
379  {
380  return carto::
382  motion ) );
383  }
384 
385  //---------------------------------------------------------------------------
387  const Motion &motion ) ;
388 
389  //---------------------------------------------------------------------------
390  template <class T>
393  carto::rc_ptr< VoxelSampler > voxelSampler,
394  const Motion &motion )
395  {
396  return carto::
398  ( new MotionedRoiIterator( getRoiIterator( data, voxelSampler ),
399  motion ) );
400  }
401 
402  //---------------------------------------------------------------------------
404  getRoiIterator( const Graph &data,
405  carto::rc_ptr< VoxelSampler > voxelSampler,
406  const Motion &motion ) ;
407 
408 } // namespace aims
409 
410 #endif
virtual int32_t at(const Point3df &) const
float voxelVolume() const
Definition: roiIterator.h:151
virtual void restart()=0
MotionedRoiIterator(const carto::rc_ptr< RoiIterator > &, const Motion &)
virtual int32_t at(const Point3df &) const =0
virtual bool isValid() const =0
float voxelVolume() const
const Point3df voxelSize() const
Definition: roiIterator.h:145
virtual ~RoiIterator()
Definition: roiIterator.h:94
carto::rc_ptr< RoiIterator > getRoiIterator(const std::string &fileName, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
const Point3d volumeDimension() const
Definition: roiIterator.h:139
virtual ~VolumeROILabelMap()
Definition: roiIterator.h:65
virtual int32_t at(const Point3df &) const
virtual carto::rc_ptr< MaskIterator > maskIterator() const
virtual bool isValid() const
virtual ~ROILabelMap()
Definition: roiIterator.h:52
virtual std::string regionName() const
virtual std::string regionName() const =0
std::vector< std::string > roiNames
Definition: roiIterator.h:55
virtual size_t count() const
const carto::rc_ptr< RoiIterator > _roiIterator
Definition: roiIterator.h:133
Generic reader for every format of Aims object.
Definition: reader.h:69
const Point3d volumeDimension() const
virtual carto::rc_ptr< ROILabelMap > createLabelMap()
virtual void next()=0
virtual carto::rc_ptr< MaskIterator > maskIterator() const =0
virtual size_t count() const =0
MotionedVolumeROILabelMap(AimsData< int32_t > &, const Motion &)
VolumeROILabelMap(AimsData< int32_t > &)
std::string toString(const T &object)
virtual carto::rc_ptr< ROILabelMap > createLabelMap()
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
const Point3df voxelSize() const
Affine 3D transformation.