aimsdata  5.0.5
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>
42 #include <aims/data/data.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>, AimsData<OUTP> >
62  {
63  public :
64  void convert( const aims::BucketMap<INP> &in, AimsData<OUTP> & out ) const;
66  void printToVolume( const aims::BucketMap<INP> &in, AimsData<OUTP> & out,
67  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
68  };
69 
70 
73  template <class INP,class OUTP>
74  class RawConverter<aims::BucketMap<INP>, VolumeRef<OUTP> >
75  {
76  public :
77  void convert( const aims::BucketMap<INP> &in,
78  VolumeRef<OUTP> & out ) const;
80  void printToVolume( const aims::BucketMap<INP> &in, VolumeRef<OUTP> & out,
81  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
82  };
83 
84 
87  template <class OUTP>
88  class RawConverter<aims::BucketMap<Void>, AimsData<OUTP> >
89  {
90  public :
91  RawConverter( bool timeislabel = false, bool withConstantValue = false,
92  OUTP value = 0 )
93  : _timeIsLabel( timeislabel ), _hasValue( withConstantValue ),
94  _value( value ) {}
95  void convert( const aims::BucketMap<Void> &in,
96  AimsData<OUTP> & out ) const;
97  void printToVolume( const aims::BucketMap<Void> &in, AimsData<OUTP> & out,
98  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
99  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
100  bool timeIsLabel() const { return( _timeIsLabel ); }
101 
102  private:
103  bool _timeIsLabel;
104  bool _hasValue;
105  OUTP _value;
106  };
107 
108 
111  template <class OUTP>
112  class RawConverter<aims::BucketMap<Void>, VolumeRef<OUTP> >
113  {
114  public :
115  RawConverter( bool timeislabel = false )
116  : _timeIsLabel( timeislabel ) {}
117  void convert( const aims::BucketMap<Void> &in,
118  VolumeRef<OUTP> & out ) const;
119  void printToVolume( const aims::BucketMap<Void> &in, VolumeRef<OUTP> & out,
120  const Point3d & offset = Point3d( 0, 0, 0 ) ) const;
121  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
122  bool timeIsLabel() const { return( _timeIsLabel ); }
123 
124  private:
125  bool _timeIsLabel;
126  };
127 
128 
131  template <class INP,class OUTP>
132  class RawConverter<AimsData<INP>, aims::BucketMap<OUTP> >
133  {
134  public:
135  void convert( const AimsData<INP> &in, aims::BucketMap<OUTP> & out ) const;
136  };
137 
138 
141  template <class INP,class OUTP>
142  class RawConverter<VolumeRef<INP>, aims::BucketMap<OUTP> >
143  {
144  public:
145  void convert( const VolumeRef<INP> &in,
146  aims::BucketMap<OUTP> & out ) const;
147  };
148 
149 
152  template <class INP>
153  class RawConverter<AimsData<INP>, aims::BucketMap<Void> >
154  {
155  public:
156  RawConverter( bool timeislabel = false )
157  : _timeIsLabel( timeislabel ) {}
158 
159  void convert( const AimsData<INP> &in, aims::BucketMap<Void> & out ) const;
160 
161  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
162  bool timeIsLabel() const { return( _timeIsLabel ); }
163 
164  private:
165  bool _timeIsLabel;
166  };
167 
168 
171  template <class INP>
172  class RawConverter<VolumeRef<INP>, aims::BucketMap<Void> >
173  {
174  public:
175  RawConverter( bool timeislabel = false )
176  : _timeIsLabel( timeislabel ) {}
177 
178  void convert( const VolumeRef<INP> &in,
179  aims::BucketMap<Void> & out ) const;
180 
181  void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
182  bool timeIsLabel() const { return( _timeIsLabel ); }
183 
184  private:
185  bool _timeIsLabel;
186  };
187 
188 
189  template <typename INP,typename OUTP>
190  class ConverterAllocator<aims::BucketMap<INP>,aims::BucketMap<OUTP> >
191  {
192  public:
193  static aims::BucketMap<OUTP>* alloc( const aims::BucketMap<INP> &in );
194  };
195 
196 
197  template <typename INP,typename OUTP>
198  class ConverterAllocator<aims::BucketMap<INP>,AimsData<OUTP> >
199  {
200  public:
201  static AimsData<OUTP>* alloc( const aims::BucketMap<INP> &in );
202  };
203 
204 
205  template <typename INP,typename OUTP>
206  class ConverterAllocator<aims::BucketMap<INP>, VolumeRef<OUTP> >
207  {
208  public:
209  static VolumeRef<OUTP>* alloc( const aims::BucketMap<INP> &in );
210  };
211 
212 
213  // implementation
214 
215  template <class INP,class OUTP> inline
217  ( const aims::BucketMap<INP> &in, aims::BucketMap<OUTP> &out ) const
218  {
219  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
220  typename aims::BucketMap<INP>::const_iterator i, e = in.end();
222  OUTP outval;
223  RawConverter<INP,OUTP> itemconv;
224  out.setHeader( in.header() );
225  out.header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
226 
227  for( i=in.begin(); i!=e; ++i )
228  {
229  typename aims::BucketMap<OUTP>::Bucket & b = out[ i->first ];
230  for( ib=i->second.begin(), eb=i->second.end(); ib!=eb; ++ib )
231  {
232  itemconv.convert( ib->second, outval );
233  b[ ib->first ] = outval;
234  }
235  }
236  }
237 
238 
239  template <class INP,class OUTP> inline
240  void RawConverter<aims::BucketMap<INP>,AimsData<OUTP> >::convert
241  ( const aims::BucketMap<INP> &in, AimsData<OUTP> &out ) const
242  {
243  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
244  printToVolume( in, out, Point3d( 0, 0, 0 ) );
245  }
246 
247 
248  template <class INP,class OUTP> inline
249  void RawConverter<aims::BucketMap<INP>,AimsData<OUTP> >::printToVolume
250  ( const aims::BucketMap<INP> & in, AimsData<OUTP> & out,
251  const Point3d & offset ) const
252  {
253  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
255  unsigned i = 0;
256 
257  RawConverter<INP, OUTP> itemconv;
258 
259  for( it=in.begin(); it!=et; ++it, ++i )
260  {
261  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
262  {
263  const Point3d & pos = ib->first;
264  itemconv.convert( ib->second,
265  out( pos[0] + offset[0], pos[1] + offset[1],
266  pos[2] + offset[2], i ) );
267  }
268  }
269  }
270 
271 
272  template <class OUTP> inline
274  ( const aims::BucketMap<Void> &in, AimsData<OUTP> &out ) const
275  {
276  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
277  printToVolume( in, out, Point3d( 0, 0, 0 ) );
278  }
279 
280 
281  namespace internal
282  {
283  template <typename OUTP, bool MODE>
285  {
286  static
287  void printToVolume( const aims::BucketMap<Void> & in,
288  AimsData<OUTP> & out,
289  const Point3d & offset, bool timelabel );
290  };
291 
292  template <typename OUTP>
293  struct VolumePrinter<OUTP,true>
294  {
295  template <typename INP>
296  static
298  AimsData<OUTP> & out, const Point3d & offset,
299  bool timelabel )
300  {
301  if( !timelabel )
302  {
303  VolumePrinter<OUTP,false>::printToVolume( in, out, offset, false );
304  return;
305  }
306 
307  aims::BucketMap<Void>::const_iterator it, et = in.end();
309  unsigned i = 0;
310 
311  for( it=in.begin(); it!=et; ++it, ++i )
312  {
313  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
314  {
315  const Point3d & pos = ib->first;
316  out( pos[0] + offset[0], pos[1] + offset[1], pos[2]
317  + offset[2] )
318  = (OUTP) it->first;
319  }
320  }
321  }
322  };
323 
324 
325  template <typename OUTP>
326  struct VolumePrinter<OUTP,false>
327  {
328  template <typename INP>
329  static
331  AimsData<OUTP> & out,
332  const Point3d & offset, bool )
333  {
334  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
336  unsigned i = 0;
337 
338  RawConverter<INP,OUTP> itemconv;
339 
340  for( it=in.begin(); it!=et; ++it, ++i )
341  {
342  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
343  {
344  const Point3d & pos = ib->first;
345  itemconv.convert( ib->second,
346  out( pos[0] + offset[0], pos[1] + offset[1],
347  pos[2] + offset[2], i ) );
348  }
349  }
350  }
351  };
352 
353  template <typename OUTP> inline
355  AimsData<OUTP> & out,
356  const Point3d & offset, bool timelabel )
357  {
359  ( in, out, offset, timelabel );
360  }
361 
362  }
363 
364 
365  template <typename OUTP> inline
366  void RawConverter<aims::BucketMap<Void>,AimsData<OUTP> >::printToVolume
367  ( const aims::BucketMap<Void> & in, AimsData<OUTP> & out,
368  const Point3d & offset ) const
369  {
370  if( _hasValue )
371  {
372  typename aims::BucketMap<Void>::const_iterator it, et = in.end();
374  unsigned i = 0;
375  for( it=in.begin(); it!=et; ++it, ++i )
376  {
377  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
378  {
379  const Point3d & pos = ib->first;
380  out( pos[0] + offset[0], pos[1] + offset[1],
381  pos[2] + offset[2], i ) = _value;
382  }
383  }
384  }
385  else
386  internal::printToVolume_smart( in, out, offset, timeIsLabel() );
387  }
388 
389 
390  template <class INP,class OUTP> inline
392  ( const AimsData<INP> &in, aims::BucketMap<OUTP> &out ) const
393  {
394  short x, y, z;
395  unsigned t, nt = in.dimT();
396  INP val;
397  RawConverter<INP,OUTP> itemconv;
398 
399  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
400  OUTP outval;
401  for( t=0; t<nt; ++t )
402  {
403  //cout << "time : " << t << endl;
404  typename aims::BucketMap<OUTP>::Bucket & bck = out[t];
405  ForEach3d( in, x, y, z )
406  {
407  /*if( y == 0 && x == 0 )
408  cout << "slice : " << z << endl;*/
409  val = in( x, y, z, t );
410  if( val != (INP)0 )
411  {
412  itemconv.convert( val, outval );
413  bck.insert( std::pair<Point3d, OUTP>
414  ( Point3d( x, y, z ), outval ) );
415  }
416  }
417  if( bck.empty() )
418  out.erase( t );
419  }
420  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
421  }
422 
423 
424  template <class INP> inline
425  void RawConverter<AimsData<INP>,aims::BucketMap<Void> >::convert
426  ( const AimsData<INP> &in, aims::BucketMap<Void> &out ) const
427  {
428  short x, y, z;
429  unsigned t, nt = in.dimT();
430  INP val;
431 
432  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
433  if( nt == 1 && timeIsLabel() )
434  {
435  size_t t;
437  ForEach3d( in, x, y, z )
438  {
439  val = in( x, y, z );
440  if( val != INP(0) )
441  {
442  rc.convert( val, t );
443  out[ t ].insert( std::pair<Point3d, Void>
444  ( Point3d( x, y, z ), Void() ) );
445  }
446  }
447  }
448  else
449  for( t=0; t<nt; ++t )
450  {
451  //cout << "time : " << t << endl;
452  aims::BucketMap<Void>::Bucket & bck = out[t];
453  ForEach3d( in, x, y, z )
454  {
455  val = in( x, y, z, t );
456  if( val != INP(0) )
457  bck.insert( std::pair<Point3d, Void>( Point3d( x, y, z ),
458  Void() ) );
459  }
460  if( bck.empty() )
461  out.erase( t );
462  }
463  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
464  }
465 
466 
467  template <class INP,class OUTP>
468  inline aims::BucketMap<OUTP>*
470  ( const aims::BucketMap<INP> &in )
471  {
473  out->setHeader( in.header() );
474  out->header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
475  return out;
476  }
477 
478 
479  template <class INP,class OUTP>
480  inline AimsData<OUTP>*
481  ConverterAllocator<aims::BucketMap<INP>,AimsData<OUTP> >::alloc
482  ( const aims::BucketMap<INP> &in )
483  {
484  VolumeRef<OUTP> *vol
485  = ConverterAllocator<aims::BucketMap<INP>, VolumeRef<OUTP> >::alloc( in );
486  AimsData<OUTP> *out = new AimsData<OUTP>( *vol );
487  delete vol;
488  return out;
489  }
490 
491 
492  // VolumeRef implementation
493 
494  template <class INP,class OUTP>
495  inline VolumeRef<OUTP>*
496  ConverterAllocator<aims::BucketMap<INP>, VolumeRef<OUTP> >::alloc
497  ( const aims::BucketMap<INP> &in )
498  {
499  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
501  Point3d bmin( 0, 0, 0 ), bmax( 0, 0, 0 );
502  bool first = true;
503 
504  // seek bounding box
505  for( it=in.begin(); it!=et; ++it )
506  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
507  {
508  const Point3d & pos = ib->first;
509  if( first )
510  {
511  bmin[0] = pos[0];
512  bmin[1] = pos[1];
513  bmin[2] = pos[2];
514  bmax[0] = pos[0];
515  bmax[1] = pos[1];
516  bmax[2] = pos[2];
517  first = false;
518  }
519  else
520  {
521  if( pos[0] < bmin[0] )
522  bmin[0] = pos[0];
523  if( pos[1] < bmin[1] )
524  bmin[1] = pos[1];
525  if( pos[2] < bmin[2] )
526  bmin[2] = pos[2];
527  if( pos[0] > bmax[0] )
528  bmax[0] = pos[0];
529  if( pos[1] > bmax[1] )
530  bmax[1] = pos[1];
531  if( pos[2] > bmax[2] )
532  bmax[2] = pos[2];
533  }
534  }
535 
536  if( bmin[0] >= 0 && bmin[1] >= 0 && bmin[2] >= 0 )
537  bmin = Point3d( 0, 0, 0 );
538 
540  *out( new VolumeRef<OUTP>( bmax[0] - bmin[0] + 1, bmax[1] - bmin[1] + 1,
541  bmax[2] - bmin[2] + 1, in.size() ) );
542  std::vector<float> vs( 4 );
543  vs[0] = in.sizeX();
544  vs[1] = in.sizeY();
545  vs[2] = in.sizeZ();
546  vs[3] = in.sizeT();
547  out->header().setProperty( "voxel_size", vs );
548 
549  out->fill( 0 );
550  return out;
551  }
552 
553 
554  template <class INP,class OUTP> inline
555  void RawConverter<aims::BucketMap<INP>, VolumeRef<OUTP> >::convert
556  ( const aims::BucketMap<INP> &in, VolumeRef<OUTP> &out ) const
557  {
558  std::vector<float> vs(4);
559  vs[0] = in.sizeX();
560  vs[1] = in.sizeY();
561  vs[2] = in.sizeZ();
562  vs[3] = in.sizeT();
563  out.header().setProperty( "voxel_size", vs );
564  printToVolume( in, out, Point3d( 0, 0, 0 ) );
565  }
566 
567 
568  template <class INP,class OUTP> inline
569  void RawConverter<aims::BucketMap<INP>, VolumeRef<OUTP> >::printToVolume
571  const Point3d & offset ) const
572  {
573  AimsData<OUTP> data( out );
574  RawConverter<aims::BucketMap<INP>, AimsData<OUTP> > c;
575  c.printToVolume( in, data, offset );
576  }
577 
578 
579  template <class OUTP> inline
580  void RawConverter<aims::BucketMap<Void>, VolumeRef<OUTP> >::convert
581  ( const aims::BucketMap<Void> &in, VolumeRef<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  template <typename OUTP> inline
594  void RawConverter<aims::BucketMap<Void>, VolumeRef<OUTP> >::printToVolume
596  const Point3d & offset ) const
597  {
598  AimsData<OUTP> data( out );
599  RawConverter<aims::BucketMap<Void>, AimsData<OUTP> > c( _timeIsLabel );
600  c.printToVolume( in, data, offset );
601  }
602 
603 
604  template <class INP,class OUTP> inline
606  ( const VolumeRef<INP> &in, aims::BucketMap<OUTP> &out ) const
607  {
608  RawConverter<AimsData<INP>, aims::BucketMap<OUTP> > c;
609  AimsData<INP> data( in );
610  c.convert( data, out );
611  }
612 
613 
614  template <class INP> inline
615  void RawConverter<VolumeRef<INP>,aims::BucketMap<Void> >::convert
616  ( const VolumeRef<INP> &in, aims::BucketMap<Void> &out ) const
617  {
618  RawConverter<AimsData<INP>, aims::BucketMap<Void> > c( _timeIsLabel );
619  AimsData<INP> data( in );
620  c.convert( data, out );
621  }
622 
623 }
624 
625 #endif
626 
627 
void convert(const INP &in, OUTP &out) const
RawConverter(bool timeislabel=false, bool withConstantValue=false, OUTP value=0)
int nt
float sizeY() const
returns the Y resolution in mm
Definition: bucketMap.h:226
void erase(const Point3d &pos)
Function redefined to omit time.
Definition: bucketMap.h:201
#define ForEach3d(thing, x, y, z)
float sizeZ() const
returns the Z resolution in mm
Definition: bucketMap.h:236
float sizeZ() const
float sizeX() const
returns the X resolution in mm
Definition: bucketMap.h:216
float sizeT() const
The class for EcatSino data write operation.
Definition: border.h:44
static void printToVolume(const aims::BucketMap< INP > &in, AimsData< OUTP > &out, const Point3d &offset, bool)
static void printToVolume(const aims::BucketMap< INP > &in, AimsData< OUTP > &out, const Point3d &offset, bool timelabel)
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
float sizeX() const
std::map< Point3d, T, BucketMapLess > Bucket
Definition: bucketMap.h:102
const PropertySet & header() const
void fill(const T &value)
void setSizeXYZT(float sizex, float sizey, float sizez, float sizet)
sets X,Y,Z and T resolutions of the data
Definition: bucketMap.h:309
void insert(const Point3d &pos, const T &item)
Function redefined to omit time.
Definition: bucketMap.h:187
float sizeY() const
float sizeT() const
returns the T resolution in s
Definition: bucketMap.h:246
std::map< int, Bucket >::const_iterator const_iterator
Definition: bucketMap.h:104
int dimT() const
void setHeader(const aims::PythonHeader &hdr)
Definition: bucketMap.h:150
virtual void setProperty(const std::string &, Object)
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
const aims::PythonHeader & header() const
Definition: bucketMap.h:148
void printToVolume_smart(const aims::BucketMap< Void > &in, AimsData< OUTP > &out, const Point3d &offset, bool timelabel)