aimsdata 6.0.0
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/io/reader.h>
39#include <aims/roi/roi.h>
41#include <cartodata/volume/volume.h>
42#include <cartobase/type/string_conversion.h>
44#include <memory>
45#include <string>
46
47namespace aims
48{
49
55
56 //---------------------------------------------------------------------------
58 {
59 public:
60 virtual inline ~MaskIterator() {}
61
62 // Returns the current point in voxel referential
63 virtual const Point3d &value() const = 0;
64 // Returns the current point in image or graph referential
65 virtual const Point3df valueMillimeters() const = 0;
66 virtual void next() = 0;
67 virtual bool isValid() const = 0;
68 virtual void restart() = 0;
69
70 // Get voxel size
71 virtual const Point3df voxelSize() const = 0;
72 // Get voxel volume.
73 // - In cases with a motion, returns the volume of a transform voxel
74 // (which is a parallelepiped in the most general case)
75 // - In other cases, returns vs[0]*vs[1]*vs[2]
76 virtual float voxelVolume() const = 0;
77 // Check if a point (in voxel referential) belongs to the mask
78 virtual bool contains( const Point3d & ) const = 0;
79 // Check if a point (in millimeter graph/image) belongs to the mask
80 virtual bool contains( const Point3df & ) const = 0;
81 // Get maximum coordinate
82 virtual const Point3d volumeDimension() const = 0;
83 virtual std::string regionName() const = 0;
84 };
85
86
87 //---------------------------------------------------------------------------
89 {
90 public:
92 const AffineTransformation3d & );
94
95 virtual const Point3d &value() const;
96 virtual const Point3df valueMillimeters() const;
97 virtual void next();
98 virtual bool isValid() const;
99 virtual void restart();
100
101 virtual const Point3df voxelSize() const;
102 virtual float voxelVolume() const;
103 virtual bool contains( const Point3d & ) const;
104 virtual bool contains( const Point3df & ) const;
105 virtual const Point3d volumeDimension() const;
106 virtual std::string regionName() const;
107
108 protected:
112 };
113
114 //---------------------------------------------------------------------------
115 template <class T>
117 {
118 };
119
120
121 //---------------------------------------------------------------------------
123 {
124 public:
125
126 inline virtual ~NodeFilter() {}
127
129 const std::vector<std::string> & ) {}
130 virtual bool filter( const carto::AttributedObject & ) const;
131 };
132
133
134 //---------------------------------------------------------------------------
136 {
137 int _label;
138
139 public:
140
141 LabelNodeFilter( int label ) : _label( label ) {}
142 inline virtual ~LabelNodeFilter() {}
143
144 virtual bool filter( const carto::AttributedObject & ) const;
145 };
146
147
148 //---------------------------------------------------------------------------
150 {
151 std::string _name;
152 std::vector<std::string> _nameAttributes;
153
154 public:
155
156 NameNodeFilter( const std::string &name,
157 const std::vector<std::string> & name_attributes
158 = std::vector<std::string>() );
159 inline virtual ~NameNodeFilter() {}
160
162 const std::vector<std::string> & attributes );
163 virtual bool filter( const carto::AttributedObject & ) const;
164 };
165
166
167 //---------------------------------------------------------------------------
168 template <>
170 {
171 const Graph *_roi;
172 carto::rc_ptr<Graph> _roiLife;
174 std::list<std::string> _attributeNames;
175 std::list<std::string>::const_iterator _itAttributeNames;
177 const BucketMap<Void>::Bucket *_bucket;
178 BucketMap<Void>::Bucket::const_iterator _itPoints;
179 std::unique_ptr< NodeFilter > _nodeFilter;
180 carto::rc_ptr< VoxelSampler > _voxelSampler;
181 std::vector<std::string> _nameAttributes;
182
183 static void
184 _findBucketAttributeNames( const Graph &graph,
185 const Vertex &node,
186 std::list<std::string> &attributeNames );
187
188 Point3df _voxelSize;
189 void _getRequiredAttributes();
190
191 public:
192
194 carto::rc_ptr< VoxelSampler > voxelSampler =
196 MaskIteratorOf( const Graph &roi, int label,
197 carto::rc_ptr< VoxelSampler > voxelSampler =
199 MaskIteratorOf( const Graph &roi, const std::string &label,
200 carto::rc_ptr< VoxelSampler > voxelSampler =
202 const std::vector<std::string> & regionNameAttributes =
203 std::vector<std::string>() );
205 carto::rc_ptr< VoxelSampler > voxelSampler =
207 MaskIteratorOf( const carto::rc_ptr<Graph> &roi, int label,
208 carto::rc_ptr< VoxelSampler > voxelSampler =
211 const std::string &label,
212 carto::rc_ptr< VoxelSampler > voxelSampler =
214 const std::vector<std::string> & regionNameAttributes =
215 std::vector<std::string>() );
216 MaskIteratorOf( const std::string &fileName,
217 carto::rc_ptr< VoxelSampler > voxelSampler =
219 MaskIteratorOf( const std::string &fileName, int label,
220 carto::rc_ptr< VoxelSampler > voxelSampler =
222 MaskIteratorOf( const std::string &fileName, const std::string &label,
223 carto::rc_ptr< VoxelSampler > voxelSampler =
225 const std::vector<std::string> & regionNameAttributes =
226 std::vector<std::string>() );
228
229 virtual const Point3df voxelSize() const { return _voxelSize; }
230 virtual float voxelVolume() const { return _voxelSize[0]*_voxelSize[1]*_voxelSize[2]; }
231 virtual const Point3d &value() const;
232 virtual const Point3df valueMillimeters() const;
233 virtual void next();
234 virtual bool isValid() const;
235 virtual void restart();
236
237 virtual bool contains( const Point3d & ) const;
238 virtual bool contains( const Point3df & ) const;
239 virtual const Point3d volumeDimension() const;
240 virtual std::string regionName() const;
248 virtual void setRegionNameAttributes( const std::vector<std::string>
249 & attributes );
250 };
251
252
253
254 //---------------------------------------------------------------------------
255 template <class T>
256 class MaskIteratorOf< carto::VolumeRef<T> > : public MaskIterator
257 {
258 mutable carto::VolumeRef<T> _data;
259 Point3d _current;
260 T _label;
261 Point3d _lastPoint;
262 bool _useLabel;
263 carto::rc_ptr< VoxelSampler > _voxelSampler;
264
265 public:
266
268 carto::rc_ptr< VoxelSampler > voxelSampler =
270 MaskIteratorOf( const carto::VolumeRef<T> &data, const T &label,
271 carto::rc_ptr< VoxelSampler > voxelSampler =
274 const specifiedLabels &labels,
275 carto::rc_ptr< VoxelSampler > voxelSampler =
277 MaskIteratorOf( const std::string &fileName,
278 carto::rc_ptr< VoxelSampler > voxelSampler =
280 MaskIteratorOf( const std::string &fileName, const T &label,
281 carto::rc_ptr< VoxelSampler > voxelSampler =
283 MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
284 carto::rc_ptr< VoxelSampler > voxelSampler =
286 virtual ~MaskIteratorOf();
287
288 virtual const Point3df voxelSize() const
289 {
290 Point3df vs( 3, 1. );
291 try
292 {
293 carto::Object o = _data->header().getProperty( "voxel_size" );
294 vs[0] = o->getArrayItem( 0 )->getScalar();
295 vs[1] = o->getArrayItem( 1 )->getScalar();
296 vs[2] = o->getArrayItem( 2 )->getScalar();
297 }
298 catch( ... )
299 {
300 }
301 return vs;
302 }
303 virtual float voxelVolume() const
304 {
305 Point3df vs = voxelSize();
306 return vs[0] * vs[1] * vs[2];
307 }
308 virtual const Point3d &value() const;
309 virtual const Point3df valueMillimeters() const;
310 virtual void next();
311 virtual bool isValid() const;
312 virtual void restart();
313
314 virtual bool contains( const Point3d & ) const;
315 virtual bool contains( const Point3df & ) const;
316 virtual const Point3d volumeDimension() const;
317 virtual std::string regionName() const;
318
319 protected:
320
321 virtual void restart( const T &label );
322 virtual void restart( const specifiedLabels &labels );
323
324 };
325
326
327
328
329 //---------------------------------------------------------------------------
330 template <class T>
332 MaskIteratorOf( const carto::VolumeRef<T> &data,
333 carto::rc_ptr< VoxelSampler > voxelSampler ) :
334 _data( data ),
335 _voxelSampler( voxelSampler )
336 {
337 _useLabel = false;
338 restart();
339 }
340
341 //---------------------------------------------------------------------------
342 template <class T>
344 MaskIteratorOf( const carto::VolumeRef<T> &data, const T &label,
345 carto::rc_ptr< VoxelSampler > voxelSampler ) :
346 _data( data ),
347 _voxelSampler( voxelSampler )
348 {
349 _useLabel = true;
350 restart( label );
351 }
352
353 //---------------------------------------------------------------------------
354 template <class T>
356 MaskIteratorOf( const carto::VolumeRef<T> &data,
357 const specifiedLabels &labels,
358 carto::rc_ptr< VoxelSampler > voxelSampler ) :
359 _data( data ),
360 _voxelSampler( voxelSampler )
361 {
362 _useLabel = true;
363 restart( labels );
364 }
365
366 //---------------------------------------------------------------------------
367 template <class T>
369 MaskIteratorOf( const std::string &fileName,
370 carto::rc_ptr< VoxelSampler > voxelSampler ) :
371 _data(),
372 _voxelSampler( voxelSampler )
373 {
374 Reader< carto::Volume<T> > reader( fileName );
375 _data.reset( reader.read() );
376 _useLabel = false;
377 restart();
378 }
379
380 //---------------------------------------------------------------------------
381 template <class T>
383 MaskIteratorOf( const std::string &fileName, const T &label,
384 carto::rc_ptr< VoxelSampler > voxelSampler ) :
385 _data(),
386 _voxelSampler( voxelSampler )
387 {
388 Reader< carto::Volume<T> > reader( fileName );
389 _data.reset( reader.read() );
390 _useLabel = true;
391 restart( label );
392 }
393
394
395 //---------------------------------------------------------------------------
396 template <class T>
398 MaskIteratorOf( const std::string &fileName, const specifiedLabels &labels,
399 carto::rc_ptr< VoxelSampler > voxelSampler ) :
400 _data(),
401 _voxelSampler( voxelSampler )
402 {
403 Reader< carto::Volume<T> > reader( fileName );
404 _data.reset( reader.read() );
405 _useLabel = true;
406 restart( labels );
407 }
408
409 //---------------------------------------------------------------------------
410 template <class T>
414
415
416 //---------------------------------------------------------------------------
417 template <class T>
419 {
420 return _current;
421 }
422
423 //---------------------------------------------------------------------------
424 template <class T>
426 {
427 Point3df vs = voxelSize();
428 if ( _voxelSampler.isNull() )
429 {
430 return Point3df( _current[0] * vs[0],
431 _current[1] * vs[1],
432 _current[2] * vs[2] );
433 } else {
434 const Point3df &voxelSample = _voxelSampler->value();
435 return Point3df( ( _current[0] + voxelSample[ 0 ] ) * vs[0],
436 ( _current[1] + voxelSample[ 1 ] ) * vs[1],
437 ( _current[2] + voxelSample[ 2 ] ) * vs[2] );
438 }
439 }
440
441 //---------------------------------------------------------------------------
442 template <class T>
444 {
445 if ( (_useLabel && (*_data)( _current ) == (T) _label) ||
446 (!_useLabel && (*_data)( _current ) ) )
447 {
448 // Next sample in voxel
449 if ( ! _voxelSampler.isNull() )
450 {
451 _voxelSampler->next();
452 if ( _voxelSampler->isValid() )
453 return;
454 }
455 }
456
457 if ( ! _voxelSampler.isNull() )
458 {
459 _voxelSampler->restart();
460 }
461
462 if ( _current == _lastPoint ) // (to make it not valid )
463 {
464 _current[ 0 ] = _data->getSizeX();
465 _current[ 1 ] = _data->getSizeY();
466 _current[ 2 ] = _data->getSizeZ();
467 }
468 // Next point
469 do {
470 ++_current[ 0 ];
471 if ( _current[ 0 ] == _data->getSizeX() ) {
472 _current[ 0 ] = 0;
473 ++_current[ 1 ];
474 if ( _current[ 1 ] == _data->getSizeY() ) {
475 _current[ 1 ] = 0;
476 ++_current[ 2 ];
477 }
478 }
479 } while( isValid() && ( _useLabel ?
480 (*_data)( _current ) != (T) _label :
481 ! (*_data)( _current ) ) );
482 }
483
484
485 //---------------------------------------------------------------------------
486 template <class T>
488 {
489 return _current[ 2 ] < _data->getSizeZ();
490 }
491
492 //---------------------------------------------------------------------------
493 template <class T>
495 {
496 if ( ! _voxelSampler.isNull() )
497 {
498 _voxelSampler->restart();
499 }
500
501 _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
502 _lastPoint[ 0 ] = _data->getSizeX()-1;
503 _lastPoint[ 1 ] = _data->getSizeY()-1;
504 _lastPoint[ 2 ] = _data->getSizeZ()-1;
505
506 if ( isValid() && ( _useLabel ?
507 (*_data)( _current ) != (T) _label :
508 ! (*_data)( _current ) ) ) next();
509 }
510
511 //---------------------------------------------------------------------------
512 template <class T>
514 {
515 if ( ! _voxelSampler.isNull() )
516 {
517 _voxelSampler->restart();
518 }
519
520 _label = label;
521 _useLabel = true;
522 _current[ 0 ] = _current[ 1 ] = _current[ 2 ] = 0;
523 _lastPoint[ 0 ] = _data->getSizeX()-1;
524 _lastPoint[ 1 ] = _data->getSizeY()-1;
525 _lastPoint[ 2 ] = _data->getSizeZ()-1;
526
527 if ( isValid() && ( (*_data)( _current ) != _label ) )
528 {
529 next();
530 }
531 }
532
533 //---------------------------------------------------------------------------
534 template <class T>
536 const specifiedLabels &labels )
537 {
538 if ( ! _voxelSampler.isNull() )
539 {
540 _voxelSampler->restart();
541 }
542
543 _label = (*_data)( labels.firstPoint );
544 _current = labels.firstPoint;
545 _lastPoint = labels.lastPoint;
546 }
547
548
549 //---------------------------------------------------------------------------
550 template <class T>
552 const Point3d &p ) const
553 {
554 if ( p[ 0 ] >= 0 && p[ 0 ] < _data->getSizeX() &&
555 p[ 1 ] >= 0 && p[ 1 ] < _data->getSizeY() &&
556 p[ 2 ] >= 0 && p[ 2 ] < _data->getSizeZ() )
557 {
558 if ( _useLabel )
559 {
560 return (*_data)( p ) == _label;
561 }
562 else
563 {
564 return (*_data)( p );
565 }
566 }
567 return false;
568 }
569
570 //---------------------------------------------------------------------------
571 template <class T>
573 const Point3df &p ) const
574 {
575 Point3df vs = voxelSize();
576 const Point3d pixel( (short) rint( p[ 0 ] / vs[0] ),
577 (short) rint( p[ 1 ] / vs[1] ),
578 (short) rint( p[ 2 ] / vs[2] ) );
579 return contains( pixel );
580 }
581
582
583 //---------------------------------------------------------------------------
584 template <class T>
586 {
587 return Point3d( _data->getSizeX(), _data->getSizeY(), _data->getSizeZ() );
588 }
589
590
591 //---------------------------------------------------------------------------
592 template <class T>
594 {
595 if ( _useLabel ) return carto::toString( _label );
596 return "unknown";
597 }
598
599
600 //---------------------------------------------------------------------------
601 // maskIteratorFactories (to be continued)
602
603 //---------------------------------------------------------------------------
604 carto::rc_ptr< MaskIterator > getMaskIterator( const std::string &fileName );
605
606 //---------------------------------------------------------------------------
608 getMaskIterator( const std::string &fileName,
609 carto::rc_ptr< VoxelSampler > voxelSampler );
610
611 //---------------------------------------------------------------------------
612 template <class T>
619
620 //---------------------------------------------------------------------------
621 template <class T>
628
629 //---------------------------------------------------------------------------
630 template <class T>
633 carto::rc_ptr< VoxelSampler > voxelSampler )
634 {
636 ( new MaskIteratorOf< carto::VolumeRef<T> >( data, voxelSampler ) );
637 }
638
639 //---------------------------------------------------------------------------
640 template <class T>
643 carto::rc_ptr< VoxelSampler > voxelSampler )
644 {
646 ( new MaskIteratorOf< carto::VolumeRef<T> >( data, voxelSampler ) );
647 }
648
649 //---------------------------------------------------------------------------
651 const std::string &fileName, const AffineTransformation3d &motion );
652
653 //---------------------------------------------------------------------------
655 getMaskIterator( const std::string &fileName,
657 const AffineTransformation3d &motion );
658
659 //---------------------------------------------------------------------------
660 template <class T>
667
668 //---------------------------------------------------------------------------
669 template <class T>
677
678 //---------------------------------------------------------------------------
679 template <class T>
683 const AffineTransformation3d &motion )
684 {
686 ( new MotionedMaskIterator( getMaskIterator( data, voxelSampler ),
687 motion ) );
688 }
689
690 //---------------------------------------------------------------------------
691 template <class T>
695 const AffineTransformation3d &motion )
696 {
698 ( new MotionedMaskIterator( getMaskIterator( data, voxelSampler ),
699 motion ) );
700 }
701
702
703} // namespace aims
704
705#endif
VSet::const_iterator const_iterator
std::map< Point3d, T, BucketMapLess > Bucket
Definition bucketMap.h:102
virtual bool filter(const carto::AttributedObject &) const
MaskIteratorOf(const std::string &fileName, const std::string &label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >(), const std::vector< std::string > &regionNameAttributes=std::vector< std::string >())
MaskIteratorOf(const Graph &roi, const std::string &label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >(), const std::vector< std::string > &regionNameAttributes=std::vector< std::string >())
virtual const Point3df valueMillimeters() const
MaskIteratorOf(const carto::rc_ptr< Graph > &roi, const std::string &label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >(), const std::vector< std::string > &regionNameAttributes=std::vector< std::string >())
virtual const Point3d & value() const
MaskIteratorOf(const std::string &fileName, int label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual float voxelVolume() const
MaskIteratorOf(const std::string &fileName, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual bool contains(const Point3d &) const
MaskIteratorOf(const carto::rc_ptr< Graph > &roi, int label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
MaskIteratorOf(const carto::rc_ptr< Graph > &roi, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual const Point3d volumeDimension() const
MaskIteratorOf(const Graph &roi, int label, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual void setRegionNameAttributes(const std::vector< std::string > &attributes)
Set region name attribute in graph.
virtual const Point3df voxelSize() const
virtual bool isValid() const
MaskIteratorOf(const Graph &roi, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual std::string regionName() const
virtual bool contains(const Point3df &) const
virtual bool contains(const Point3d &) const
virtual const Point3d volumeDimension() const
MaskIteratorOf(const carto::VolumeRef< T > &data, carto::rc_ptr< VoxelSampler > voxelSampler=carto::rc_ptr< VoxelSampler >())
virtual const Point3df voxelSize() const
virtual const Point3d & value() const
virtual const Point3df valueMillimeters() const
virtual const Point3d volumeDimension() const =0
virtual bool isValid() const =0
virtual bool contains(const Point3d &) const =0
virtual const Point3d & value() const =0
virtual ~MaskIterator()
virtual std::string regionName() const =0
virtual void restart()=0
virtual bool contains(const Point3df &) const =0
virtual float voxelVolume() const =0
virtual const Point3df voxelSize() const =0
virtual void next()=0
virtual const Point3df valueMillimeters() const =0
AffineTransformation3d _motion
virtual const Point3d & value() const
virtual bool contains(const Point3d &) const
virtual bool contains(const Point3df &) const
virtual const Point3df valueMillimeters() const
MotionedMaskIterator(const carto::rc_ptr< MaskIterator > &, const AffineTransformation3d &)
virtual float voxelVolume() const
AffineTransformation3d _inverseMotion
virtual std::string regionName() const
virtual bool isValid() const
carto::rc_ptr< MaskIterator > _maskIterator
virtual const Point3df voxelSize() const
virtual const Point3d volumeDimension() const
virtual bool filter(const carto::AttributedObject &) const
virtual void setRegionNameAttributes(const std::vector< std::string > &attributes)
NameNodeFilter(const std::string &name, const std::vector< std::string > &name_attributes=std::vector< std::string >())
virtual bool filter(const carto::AttributedObject &) const
virtual ~NodeFilter()
virtual void setRegionNameAttributes(const std::vector< std::string > &)
Generic reader for every format of Aims object.
Definition reader.h:70
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
The class for EcatSino data write operation.
carto::rc_ptr< MaskIterator > getMaskIterator(const std::string &fileName)
SyntaxedObject< PropertySet > AttributedObject
std::string toString(const T &object)
AimsVector< float, 3 > Point3df
AimsVector< int16_t, 3 > Point3d