aimsdata  5.1.2
Neuroimaging data handling
converter_bucket.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  * Converter between the different A.I.M.S. types
36  */
37 #ifndef AIMS_UTILITY_CONVERTER_BUCKET_H
38 #define AIMS_UTILITY_CONVERTER_BUCKET_H
39 
41 #include <aims/bucket/bucket.h>
43 
44 namespace carto
45 {
46 
49  template <class INP,class OUTP>
50  class RawConverter<aims::BucketMap<INP>, aims::BucketMap<OUTP> >
51  {
52  public :
53  void convert( const aims::BucketMap<INP> &in,
54  aims::BucketMap<OUTP> & out ) const;
55  };
56 
57 
60  template <class INP,class OUTP>
61  class RawConverter<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
62  {
63  public :
64  void convert( const aims::BucketMap<INP> &in,
65  rc_ptr<Volume<OUTP> > & out ) const;
68  rc_ptr<Volume<OUTP> > & out,
69  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
70  };
71 
72 
75  template <class INP,class OUTP>
76  class RawConverter<aims::BucketMap<INP>, VolumeRef<OUTP> >
77  : public RawConverter<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
78  {
79  public :
80  void convert( const aims::BucketMap<INP> &in,
81  VolumeRef<OUTP> & out ) const
82  {
84  in, out );
85  }
88  VolumeRef<OUTP> & out,
89  const Point3d & offset = Point3d( 0, 0, 0 ) ) const
90  {
92  ::printToVolume( in, out, offset );
93  }
94  };
95 
96 
99  template <class OUTP>
100  class RawConverter<aims::BucketMap<Void>, rc_ptr<Volume<OUTP> > >
101  {
102  public :
103  RawConverter( bool timeislabel = false, bool withConstantValue = false,
104  OUTP value = 0 )
105  : _timeIsLabel( timeislabel ), _hasValue( withConstantValue ),
106  _value( value ) {}
107  void convert( const aims::BucketMap<Void> &in,
108  rc_ptr<Volume<OUTP> > & out ) const;
110  rc_ptr<Volume<OUTP> > & out,
111  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
112  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
113  bool timeIsLabel() const { return( _timeIsLabel ); }
114 
115  private:
116  bool _timeIsLabel;
117  bool _hasValue;
118  OUTP _value;
119  };
120 
121 
124  template <class OUTP>
125  class RawConverter<aims::BucketMap<Void>, VolumeRef<OUTP> >
126  : public RawConverter<aims::BucketMap<Void>, rc_ptr<Volume<OUTP> > >
127  {
128  public :
130  VolumeRef<OUTP> & out ) const
131  {
133  in, out );
134  }
136  VolumeRef<OUTP> & out,
137  const Point3d & offset = Point3d( 0, 0, 0 ) ) const
138  {
140  ::printToVolume( in, out, offset );
141  }
142  };
143 
144 
147  template <class INP,class OUTP>
148  class RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<OUTP> >
149  {
150  public:
151  void convert( const rc_ptr<Volume<INP> > &in,
152  aims::BucketMap<OUTP> & out ) const;
153  };
154 
155 
158  template <class INP,class OUTP>
159  class RawConverter<VolumeRef<INP>, aims::BucketMap<OUTP> >
160  : public RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<OUTP> >
161  {
162  public:
163 // void convert( const VolumeRef<INP> &in,
164 // aims::BucketMap<OUTP> & out ) const;
165  };
166 
167 
170  template <class INP>
171  class RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >
172  {
173  public:
174  RawConverter( bool timeislabel = false )
175  : _timeIsLabel( timeislabel ) {}
176 
177  void convert( const rc_ptr<Volume<INP> > &in,
178  aims::BucketMap<Void> & out ) const;
179 
180  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
181  bool timeIsLabel() const { return( _timeIsLabel ); }
182 
183  private:
184  bool _timeIsLabel;
185  };
186 
187 
190  template <class INP>
191  class RawConverter<VolumeRef<INP>, aims::BucketMap<Void> >
192  : public RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >
193  {
194  public:
195  RawConverter( bool timeislabel = false )
196  : RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >(
197  timeislabel )
198  {}
199  };
200 
201 
202  template <typename INP,typename OUTP>
203  class ConverterAllocator<aims::BucketMap<INP>,aims::BucketMap<OUTP> >
204  {
205  public:
206  static aims::BucketMap<OUTP>* alloc( const aims::BucketMap<INP> &in );
207  };
208 
209 
210  template <typename INP,typename OUTP>
211  class ConverterAllocator<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
212  {
213  public:
214  static rc_ptr<Volume<OUTP> >* alloc( const aims::BucketMap<INP> &in );
215  };
216 
217 
218  template <typename INP,typename OUTP>
219  class ConverterAllocator<aims::BucketMap<INP>, VolumeRef<OUTP> >
220  : public ConverterAllocator<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
221  {
222  public:
224  {
225  return static_cast<VolumeRef<OUTP>*>(
227  rc_ptr<Volume<OUTP> > >::alloc( in ) );
228  }
229  };
230 
231 
232  // implementation
233 
234  template <class INP,class OUTP> inline
236  ( const aims::BucketMap<INP> &in, aims::BucketMap<OUTP> &out ) const
237  {
238  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
239  typename aims::BucketMap<INP>::const_iterator i, e = in.end();
241  OUTP outval;
242  RawConverter<INP,OUTP> itemconv;
243  out.setHeader( in.header() );
244  out.header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
245 
246  for( i=in.begin(); i!=e; ++i )
247  {
248  typename aims::BucketMap<OUTP>::Bucket & b = out[ i->first ];
249  for( ib=i->second.begin(), eb=i->second.end(); ib!=eb; ++ib )
250  {
251  itemconv.convert( ib->second, outval );
252  b[ ib->first ] = outval;
253  }
254  }
255  }
256 
257 
258  template <class INP,class OUTP> inline
260  carto::rc_ptr<Volume<OUTP> > >::printToVolume
261  ( const aims::BucketMap<INP> & in, carto::rc_ptr<Volume<OUTP> > & out,
262  const Point3d & offset ) const
263  {
264  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
266  unsigned i = 0;
267  int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ(),
268  dt = out->getSizeT();
269 
270  RawConverter<INP, OUTP> itemconv;
271 
272  for( it=in.begin(); it!=et; ++it, ++i )
273  {
274  if( i >= dt )
275  continue;
276  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
277  {
278  const Point3d pos = ib->first + offset;
279  if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
280  && pos[0] < dx && pos[1] < dy && pos[2] < dz )
281  itemconv.convert( ib->second,
282  out->at( pos[0], pos[1], pos[2], i ) );
283  }
284  }
285  }
286 
287 
288  namespace internal
289  {
290  template <typename OUTP, bool MODE>
292  {
293  static
295  carto::rc_ptr<Volume<OUTP> > & out,
296  const Point3d & offset, bool timelabel );
297  };
298 
299  template <typename OUTP>
300  struct VolumePrinter<OUTP, true>
301  {
302  template <typename INP>
303  static
305  carto::rc_ptr<Volume<OUTP> > & out, const Point3d & offset,
306  bool timelabel )
307  {
308  if( !timelabel )
309  {
310  VolumePrinter<OUTP, false>::printToVolume( in, out, offset, false );
311  return;
312  }
313 
314  aims::BucketMap<Void>::const_iterator it, et = in.end();
316  unsigned i = 0;
317  int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ();
318 
319  for( it=in.begin(); it!=et; ++it, ++i )
320  {
321  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
322  {
323  const Point3d pos = ib->first + offset;
324  if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
325  && pos[0] < dx && pos[1] < dy && pos[2] < dz )
326  out->at( pos ) = (OUTP) it->first;
327  }
328  }
329  }
330  };
331 
332 
333  template <typename OUTP>
334  struct VolumePrinter<OUTP, false>
335  {
336  template <typename INP>
337  static
339  carto::rc_ptr<Volume<OUTP> > & out,
340  const Point3d & offset, bool )
341  {
342  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
344  unsigned i = 0;
345  int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ(),
346  dt = out->getSizeT();
347 
348  RawConverter<INP,OUTP> itemconv;
349 
350  for( it=in.begin(); it!=et; ++it, ++i )
351  {
352  if( i >= dt )
353  continue;
354  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
355  {
356  const Point3d pos = ib->first + offset;
357  if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
358  && pos[0] < dx && pos[1] < dy && pos[2] < dz )
359  itemconv.convert( ib->second,
360  out->at( pos[0], pos[1], pos[2], i ) );
361  }
362  }
363  }
364  };
365 
366  template <typename OUTP> inline
368  carto::rc_ptr<Volume<OUTP> > & out,
369  const Point3d & offset, bool timelabel )
370  {
372  ( in, out, offset, timelabel );
373  }
374 
375  }
376 
377 
378  template <typename OUTP> inline
379  void RawConverter<aims::BucketMap<Void>,
380  carto::rc_ptr<Volume<OUTP> > >::printToVolume
381  ( const aims::BucketMap<Void> & in, carto::rc_ptr<Volume<OUTP> > & out,
382  const Point3d & offset ) const
383  {
384  if( _hasValue )
385  {
386  typename aims::BucketMap<Void>::const_iterator it, et = in.end();
388  unsigned i = 0;
389  int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ();
390  for( it=in.begin(); it!=et; ++it, ++i )
391  {
392  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
393  {
394  const Point3d & pos = ib->first + offset;
395  if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
396  && pos[0] < dx && pos[1] < dy && pos[2] < dz )
397  out->at( pos[0], pos[1], pos[2], i ) = _value;
398  }
399  }
400  }
401  else
402  internal::printToVolume_smart( in, out, offset, timeIsLabel() );
403  }
404 
405 
406  template <class INP,class OUTP> inline
407  void RawConverter<carto::rc_ptr<Volume<INP> >,
408  aims::BucketMap<OUTP> >::convert
409  ( const carto::rc_ptr<Volume<INP> > &in, aims::BucketMap<OUTP> &out ) const
410  {
411  unsigned t, nt = in->getSizeT();
412  int x, y, z, nx = in->getSizeX(), ny = in->getSizeY(), nz = in->getSizeZ();
413  INP val;
414  OUTP outval;
415  RawConverter<INP,OUTP> itemconv;
416 
417  std::vector<float> vs = in->getVoxelSize();
418  out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
419  for( t=0; t<nt; ++t )
420  {
421  //cout << "time : " << t << endl;
422  typename aims::BucketMap<OUTP>::Bucket & bck = out[t];
423  for( z=0; z<nz; ++z )
424  for( y=0; y<ny; ++y )
425  for( x=0; x<nx; ++x )
426  {
427  /*if( y == 0 && x == 0 )
428  cout << "slice : " << z << endl;*/
429  val = in->at( x, y, z, t );
430  if( val != (INP)0 )
431  {
432  itemconv.convert( val, outval );
433  bck.insert( std::pair<Point3d, OUTP>
434  ( Point3d( x, y, z ), outval ) );
435  }
436  }
437  if( bck.empty() )
438  out.erase( t );
439  }
440  }
441 
442 
443  template <class INP> inline
444  void RawConverter<carto::rc_ptr<Volume<INP> >,
445  aims::BucketMap<Void> >::convert
446  ( const carto::rc_ptr<Volume<INP> > &in, aims::BucketMap<Void> &out ) const
447  {
448  unsigned t, nt = in->getSizeT();
449  int x, y, z, nx = in->getSizeX(), ny = in->getSizeY(), nz = in->getSizeZ();
450  INP val;
451 
452  std::vector<float> vs = in->getVoxelSize();
453  out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
454  if( nt == 1 && timeIsLabel() )
455  {
456  size_t t;
457  RawConverter<INP,size_t> rc;
458  for( z=0; z<nz; ++z )
459  for( y=0; y<ny; ++y )
460  for( x=0; x<nx; ++x )
461  {
462  val = in->at( x, y, z );
463  if( val != INP(0) )
464  {
465  rc.convert( val, t );
466  out[ t ].insert( std::pair<Point3d, Void>
467  ( Point3d( x, y, z ), Void() ) );
468  }
469  }
470  }
471  else
472  for( t=0; t<nt; ++t )
473  {
474  //cout << "time : " << t << endl;
475  aims::BucketMap<Void>::Bucket & bck = out[t];
476  for( z=0; z<nz; ++z )
477  for( y=0; y<ny; ++y )
478  for( x=0; x<nx; ++x )
479  {
480  val = in->at( x, y, z, t );
481  if( val != INP(0) )
482  bck.insert( std::pair<Point3d, Void>( Point3d( x, y, z ),
483  Void() ) );
484  }
485  if( bck.empty() )
486  out.erase( t );
487  }
488  }
489 
490 
491  template <class INP,class OUTP>
492  inline aims::BucketMap<OUTP>*
494  ( const aims::BucketMap<INP> &in )
495  {
497  out->setHeader( in.header() );
498  out->header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
499  return out;
500  }
501 
502 
503  // VolumeRef implementation
504 
505  template <class INP,class OUTP>
506  inline rc_ptr<Volume<OUTP> >*
508  ( const aims::BucketMap<INP> &in )
509  {
510  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
512  Point3d bmin( 0, 0, 0 ), bmax( 0, 0, 0 );
513  bool first = true;
514 
515  // seek bounding box
516  for( it=in.begin(); it!=et; ++it )
517  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
518  {
519  const Point3d & pos = ib->first;
520  if( first )
521  {
522  bmin[0] = pos[0];
523  bmin[1] = pos[1];
524  bmin[2] = pos[2];
525  bmax[0] = pos[0];
526  bmax[1] = pos[1];
527  bmax[2] = pos[2];
528  first = false;
529  }
530  else
531  {
532  if( pos[0] < bmin[0] )
533  bmin[0] = pos[0];
534  if( pos[1] < bmin[1] )
535  bmin[1] = pos[1];
536  if( pos[2] < bmin[2] )
537  bmin[2] = pos[2];
538  if( pos[0] > bmax[0] )
539  bmax[0] = pos[0];
540  if( pos[1] > bmax[1] )
541  bmax[1] = pos[1];
542  if( pos[2] > bmax[2] )
543  bmax[2] = pos[2];
544  }
545  }
546 
547  if( bmin[0] >= 0 && bmin[1] >= 0 && bmin[2] >= 0 )
548  bmin = Point3d( 0, 0, 0 );
549 
551  *out( new VolumeRef<OUTP>( bmax[0] - bmin[0] + 1, bmax[1] - bmin[1] + 1,
552  bmax[2] - bmin[2] + 1, in.size() ) );
553  std::vector<float> vs( 4 );
554  vs[0] = in.sizeX();
555  vs[1] = in.sizeY();
556  vs[2] = in.sizeZ();
557  vs[3] = in.sizeT();
558  (*out)->header().setProperty( "voxel_size", vs );
559 
560  (*out)->fill( 0 );
561  return out;
562  }
563 
564 
565  template <class INP,class OUTP> inline
567  ( const aims::BucketMap<INP> &in, rc_ptr<Volume<OUTP> > &out ) const
568  {
569  std::vector<float> vs(4);
570  vs[0] = in.sizeX();
571  vs[1] = in.sizeY();
572  vs[2] = in.sizeZ();
573  vs[3] = in.sizeT();
574  out->header().setProperty( "voxel_size", vs );
575  printToVolume( in, out, Point3d( 0, 0, 0 ) );
576  }
577 
578 
579  template <class OUTP> inline
581  ( const aims::BucketMap<Void> &in, rc_ptr<Volume<OUTP> > &out ) const
582  {
583  std::vector<float> vs(4);
584  vs[0] = in.sizeX();
585  vs[1] = in.sizeY();
586  vs[2] = in.sizeZ();
587  vs[3] = in.sizeT();
588  out->header().setProperty( "voxel_size", vs );
589  printToVolume( in, out, Point3d( 0, 0, 0 ) );
590  }
591 
592 }
593 
594 #endif
595 
596 
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucketMap.h:99
float sizeZ() const
returns the Z resolution in mm
Definition: bucketMap.h:254
void insert(const Point3d &pos, const T &item)
Function redefined to omit time.
Definition: bucketMap.h:194
void setSizeXYZT(float sizex, float sizey, float sizez, float sizet)
sets X,Y,Z and T resolutions of the data
Definition: bucketMap.h:327
const aims::PythonHeader & header() const
Definition: bucketMap.h:155
void erase(const Point3d &pos)
Function redefined to omit time.
Definition: bucketMap.h:208
std::map< Point3d, T, BucketMapLess > Bucket
Definition: bucketMap.h:102
float sizeT() const
returns the T resolution in s
Definition: bucketMap.h:264
float sizeY() const
returns the Y resolution in mm
Definition: bucketMap.h:244
void setHeader(const aims::PythonHeader &hdr)
Definition: bucketMap.h:157
float sizeX() const
returns the X resolution in mm
Definition: bucketMap.h:234
std::map< int, Bucket >::const_iterator const_iterator
Definition: bucketMap.h:104
std::vector< float > getVoxelSize() const
Definition: bucketMap.h:224
static VolumeRef< OUTP > * alloc(const aims::BucketMap< INP > &in)
static OUTP * alloc(const INP &in)
void convert(const aims::BucketMap< INP > &in, VolumeRef< OUTP > &out) const
void printToVolume(const aims::BucketMap< INP > &in, VolumeRef< OUTP > &out, const Point3d &offset=Point3d(0, 0, 0)) const
writes bucket in an already allocated data
void printToVolume(const aims::BucketMap< INP > &in, rc_ptr< Volume< OUTP > > &out, const Point3d &offset=Point3d(0, 0, 0)) const
writes bucket in an already allocated data
void convert(const aims::BucketMap< Void > &in, VolumeRef< OUTP > &out) const
void printToVolume(const aims::BucketMap< Void > &in, VolumeRef< OUTP > &out, const Point3d &offset=Point3d(0, 0, 0)) const
void printToVolume(const aims::BucketMap< Void > &in, rc_ptr< Volume< OUTP > > &out, const Point3d &offset=Point3d(0, 0, 0)) const
RawConverter(bool timeislabel=false, bool withConstantValue=false, OUTP value=0)
void convert(const rc_ptr< Volume< INP > > &in, aims::BucketMap< OUTP > &out) const
void convert(const rc_ptr< Volume< INP > > &in, aims::BucketMap< Void > &out) const
void convert(const INP &in, OUTP &out) const
virtual void setProperty(const std::string &, Object)
int nt
The class for EcatSino data write operation.
Definition: borderfiller.h:13
void printToVolume_smart(const aims::BucketMap< Void > &in, carto::rc_ptr< Volume< OUTP > > &out, const Point3d &offset, bool timelabel)
static void printToVolume(const aims::BucketMap< INP > &in, carto::rc_ptr< Volume< OUTP > > &out, const Point3d &offset, bool)
static void printToVolume(const aims::BucketMap< INP > &in, carto::rc_ptr< Volume< OUTP > > &out, const Point3d &offset, bool timelabel)
static void printToVolume(const aims::BucketMap< Void > &in, carto::rc_ptr< Volume< OUTP > > &out, const Point3d &offset, bool timelabel)