A.I.M.S


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  // implementation
206 
207  template <class INP,class OUTP> inline
209  ( const aims::BucketMap<INP> &in, aims::BucketMap<OUTP> &out ) const
210  {
211  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
212  typename aims::BucketMap<INP>::const_iterator i, e = in.end();
214  OUTP outval;
215  RawConverter<INP,OUTP> itemconv;
216  out.setHeader( in.header() );
217  out.header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
218 
219  for( i=in.begin(); i!=e; ++i )
220  {
221  typename aims::BucketMap<OUTP>::Bucket & b = out[ i->first ];
222  for( ib=i->second.begin(), eb=i->second.end(); ib!=eb; ++ib )
223  {
224  itemconv.convert( ib->second, outval );
225  b[ ib->first ] = outval;
226  }
227  }
228  }
229 
230 
231  template <class INP,class OUTP> inline
233  ( const aims::BucketMap<INP> &in, AimsData<OUTP> &out ) const
234  {
235  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
236  printToVolume( in, out, Point3d( 0, 0, 0 ) );
237  }
238 
239 
240  template <class INP,class OUTP> inline
242  ( const aims::BucketMap<INP> & in, AimsData<OUTP> & out,
243  const Point3d & offset ) const
244  {
245  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
247  unsigned i = 0;
248 
249  RawConverter<INP, OUTP> itemconv;
250 
251  for( it=in.begin(); it!=et; ++it, ++i )
252  {
253  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
254  {
255  const Point3d & pos = ib->first;
256  itemconv.convert( ib->second,
257  out( pos[0] + offset[0], pos[1] + offset[1],
258  pos[2] + offset[2], i ) );
259  }
260  }
261  }
262 
263 
264  template <class OUTP> inline
266  ( const aims::BucketMap<Void> &in, AimsData<OUTP> &out ) const
267  {
268  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
269  printToVolume( in, out, Point3d( 0, 0, 0 ) );
270  }
271 
272 
273  namespace internal
274  {
275  template <typename OUTP, bool MODE>
277  {
278  static
279  void printToVolume( const aims::BucketMap<Void> & in,
280  AimsData<OUTP> & out,
281  const Point3d & offset, bool timelabel );
282  };
283 
284  template <typename OUTP>
285  struct VolumePrinter<OUTP,true>
286  {
287  template <typename INP>
288  static
290  AimsData<OUTP> & out, const Point3d & offset,
291  bool timelabel )
292  {
293  if( !timelabel )
294  {
295  VolumePrinter<OUTP,false>::printToVolume( in, out, offset, false );
296  return;
297  }
298 
299  aims::BucketMap<Void>::const_iterator it, et = in.end();
301  unsigned i = 0;
302 
303  for( it=in.begin(); it!=et; ++it, ++i )
304  {
305  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
306  {
307  const Point3d & pos = ib->first;
308  out( pos[0] + offset[0], pos[1] + offset[1], pos[2]
309  + offset[2] )
310  = (OUTP) it->first;
311  }
312  }
313  }
314  };
315 
316 
317  template <typename OUTP>
318  struct VolumePrinter<OUTP,false>
319  {
320  template <typename INP>
321  static
323  AimsData<OUTP> & out,
324  const Point3d & offset, bool )
325  {
326  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
328  unsigned i = 0;
329 
330  RawConverter<INP,OUTP> itemconv;
331 
332  for( it=in.begin(); it!=et; ++it, ++i )
333  {
334  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
335  {
336  const Point3d & pos = ib->first;
337  itemconv.convert( ib->second,
338  out( pos[0] + offset[0], pos[1] + offset[1],
339  pos[2] + offset[2], i ) );
340  }
341  }
342  }
343  };
344 
345  template <typename OUTP> inline
346  void printToVolume_smart( const aims::BucketMap<Void> & in,
347  AimsData<OUTP> & out,
348  const Point3d & offset, bool timelabel )
349  {
350  VolumePrinter<OUTP, std::numeric_limits<OUTP>::is_integer>::printToVolume
351  ( in, out, offset, timelabel );
352  }
353 
354  }
355 
356 
357  template <typename OUTP> inline
358  void RawConverter<aims::BucketMap<Void>,AimsData<OUTP> >::printToVolume
359  ( const aims::BucketMap<Void> & in, AimsData<OUTP> & out,
360  const Point3d & offset ) const
361  {
362  if( _hasValue )
363  {
364  typename aims::BucketMap<Void>::const_iterator it, et = in.end();
366  unsigned i = 0;
367  for( it=in.begin(); it!=et; ++it, ++i )
368  {
369  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
370  {
371  const Point3d & pos = ib->first;
372  out( pos[0] + offset[0], pos[1] + offset[1],
373  pos[2] + offset[2], i ) = _value;
374  }
375  }
376  }
377  else
378  internal::printToVolume_smart( in, out, offset, timeIsLabel() );
379  }
380 
381 
382  template <class INP,class OUTP> inline
384  ( const AimsData<INP> &in, aims::BucketMap<OUTP> &out ) const
385  {
386  short x, y, z;
387  unsigned t, nt = in.dimT();
388  INP val;
389  RawConverter<INP,OUTP> itemconv;
390 
391  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
392  OUTP outval;
393  for( t=0; t<nt; ++t )
394  {
395  //cout << "time : " << t << endl;
396  typename aims::BucketMap<OUTP>::Bucket & bck = out[t];
397  ForEach3d( in, x, y, z )
398  {
399  /*if( y == 0 && x == 0 )
400  cout << "slice : " << z << endl;*/
401  val = in( x, y, z, t );
402  if( val != 0 )
403  {
404  itemconv.convert( val, outval );
405  bck.insert( std::pair<Point3d, OUTP>
406  ( Point3d( x, y, z ), outval ) );
407  }
408  }
409  if( bck.empty() )
410  out.erase( t );
411  }
412  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
413  }
414 
415 
416  template <class INP> inline
418  ( const AimsData<INP> &in, aims::BucketMap<Void> &out ) const
419  {
420  short x, y, z;
421  unsigned t, nt = in.dimT();
422  INP val;
423 
424  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
425  if( nt == 1 && timeIsLabel() )
426  {
427  size_t t;
429  ForEach3d( in, x, y, z )
430  {
431  val = in( x, y, z );
432  if( val != INP(0) )
433  {
434  rc.convert( val, t );
435  out[ t ].insert( std::pair<Point3d, Void>
436  ( Point3d( x, y, z ), Void() ) );
437  }
438  }
439  }
440  else
441  for( t=0; t<nt; ++t )
442  {
443  //cout << "time : " << t << endl;
444  aims::BucketMap<Void>::Bucket & bck = out[t];
445  ForEach3d( in, x, y, z )
446  {
447  val = in( x, y, z, t );
448  if( val != INP(0) )
449  bck.insert( std::pair<Point3d, Void>( Point3d( x, y, z ),
450  Void() ) );
451  }
452  if( bck.empty() )
453  out.erase( t );
454  }
455  out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
456  }
457 
458 
459  template <class INP,class OUTP>
460  inline aims::BucketMap<OUTP>*
462  ( const aims::BucketMap<INP> &in )
463  {
465  out->setHeader( in.header() );
466  out->header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
467  return out;
468  }
469 
470 
471  template <class INP,class OUTP>
472  inline AimsData<OUTP>*
474  ( const aims::BucketMap<INP> &in )
475  {
476  typename aims::BucketMap<INP>::const_iterator it, et = in.end();
478  Point3d bmin( 0, 0, 0 ), bmax( 0, 0, 0 );
479  bool first = true;
480 
481  // seek bounding box
482  for( it=in.begin(); it!=et; ++it )
483  for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
484  {
485  const Point3d & pos = ib->first;
486  if( first )
487  {
488  bmin[0] = pos[0];
489  bmin[1] = pos[1];
490  bmin[2] = pos[2];
491  bmax[0] = pos[0];
492  bmax[1] = pos[1];
493  bmax[2] = pos[2];
494  first = false;
495  }
496  else
497  {
498  if( pos[0] < bmin[0] )
499  bmin[0] = pos[0];
500  if( pos[1] < bmin[1] )
501  bmin[1] = pos[1];
502  if( pos[2] < bmin[2] )
503  bmin[2] = pos[2];
504  if( pos[0] > bmax[0] )
505  bmax[0] = pos[0];
506  if( pos[1] > bmax[1] )
507  bmax[1] = pos[1];
508  if( pos[2] > bmax[2] )
509  bmax[2] = pos[2];
510  }
511  }
512 
513  if( bmin[0] >= 0 && bmin[1] >= 0 && bmin[2] >= 0 )
514  bmin = Point3d( 0, 0, 0 );
515 
517  *out( new AimsData<OUTP>( bmax[0] - bmin[0] + 1, bmax[1] - bmin[1] + 1,
518  bmax[2] - bmin[2] + 1, in.size() ) );
519  out->setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
520 
521  *out = 0;
522  return out;
523  }
524 
525 
526  // VolumeRef implementation
527 
528  template <class INP,class OUTP> inline
530  ( const aims::BucketMap<INP> &in, VolumeRef<OUTP> &out ) const
531  {
532  std::vector<float> vs(4);
533  vs[0] = in.sizeX();
534  vs[1] = in.sizeY();
535  vs[2] = in.sizeZ();
536  vs[3] = in.sizeT();
537  out.header().setProperty( "voxel_size", vs );
538  printToVolume( in, out, Point3d( 0, 0, 0 ) );
539  }
540 
541 
542  template <class INP,class OUTP> inline
545  const Point3d & offset ) const
546  {
547  AimsData<OUTP> data( out );
549  c.printToVolume( in, data, offset );
550  }
551 
552 
553  template <class OUTP> inline
555  ( const aims::BucketMap<Void> &in, VolumeRef<OUTP> &out ) const
556  {
557  std::vector<float> vs(4);
558  vs[0] = in.sizeX();
559  vs[1] = in.sizeY();
560  vs[2] = in.sizeZ();
561  vs[3] = in.sizeT();
562  out.header().setProperty( "voxel_size", vs );
563  printToVolume( in, out, Point3d( 0, 0, 0 ) );
564  }
565 
566 
567  template <typename OUTP> inline
570  const Point3d & offset ) const
571  {
572  AimsData<OUTP> data( out );
574  c.printToVolume( in, data, offset );
575  }
576 
577 
578  template <class INP,class OUTP> inline
580  ( const VolumeRef<INP> &in, aims::BucketMap<OUTP> &out ) const
581  {
583  AimsData<INP> data( in );
584  c.convert( data, out );
585  }
586 
587 
588  template <class INP> inline
590  ( const VolumeRef<INP> &in, aims::BucketMap<Void> &out ) const
591  {
593  AimsData<INP> data( in );
594  c.convert( data, out );
595  }
596 
597 }
598 
599 #endif
600 
601 
RawConverter(bool timeislabel=false, bool withConstantValue=false, OUTP value=0)
int nt
void erase(const Point3d &pos)
Function redefined to omit time.
Definition: bucketMap.h:201
float sizeY() const
#define ForEach3d(thing, x, y, z)
int dimT() const
float sizeX() const
returns the X resolution in mm
Definition: bucketMap.h:216
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)
float sizeT() const
returns the T resolution in s
Definition: bucketMap.h:246
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
std::map< Point3d, T, BucketMapLess > Bucket
Definition: bucketMap.h:102
void setProperty(const std::string &, const T &)
float sizeZ() const
returns the Z resolution in mm
Definition: bucketMap.h:236
void setSizeXYZT(float sizex, float sizey, float sizez, float sizet)
sets X,Y,Z and T resolutions of the data
Definition: bucketMap.h:309
static void printToVolume(const aims::BucketMap< Void > &in, AimsData< OUTP > &out, const Point3d &offset, bool timelabel)
void insert(const Point3d &pos, const T &item)
Function redefined to omit time.
Definition: bucketMap.h:187
float sizeT() const
float sizeZ() const
static OUTP * alloc(const INP &in)
AimsVector< int16_t, 3 > Point3d
Definition: vector.h:212
std::map< int, Bucket >::const_iterator const_iterator
Definition: bucketMap.h:104
void setHeader(const aims::PythonHeader &hdr)
Definition: bucketMap.h:150
float sizeX() const
float sizeY() const
returns the Y resolution in mm
Definition: bucketMap.h:226
const PropertySet & header() const
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