aimsdata 6.0.0
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
40#include <cartobase/type/converter.h>
41#include <aims/bucket/bucket.h>
42#include <cartodata/volume/volume.h>
43
44namespace 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 :
81 VolumeRef<OUTP> & out ) const
82 {
84 in, out );
85 }
86
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 OUTP constValue() const { return _value; }
115 bool hasConstantValue() const { return _hasValue; }
116 void setConstValue( OUTP x ) { _value = x; }
117 void setHasConstValue( bool x ) { _hasValue = x; }
118
119 private:
120 bool _timeIsLabel;
121 bool _hasValue;
122 OUTP _value;
123 };
124
125
128 template <class OUTP>
129 class RawConverter<aims::BucketMap<Void>, VolumeRef<OUTP> >
130 : public RawConverter<aims::BucketMap<Void>, rc_ptr<Volume<OUTP> > >
131 {
132 public :
134 VolumeRef<OUTP> & out ) const
135 {
137 in, out );
138 }
140 VolumeRef<OUTP> & out,
141 const Point3d & offset = Point3d( 0, 0, 0 ) ) const
142 {
144 ::printToVolume( in, out, offset );
145 }
146 };
147
148
151 template <class INP,class OUTP>
152 class RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<OUTP> >
153 {
154 public:
155 void convert( const rc_ptr<Volume<INP> > &in,
156 aims::BucketMap<OUTP> & out ) const;
157 };
158
159
162 template <class INP,class OUTP>
163 class RawConverter<VolumeRef<INP>, aims::BucketMap<OUTP> >
164 : public RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<OUTP> >
165 {
166 public:
167// void convert( const VolumeRef<INP> &in,
168// aims::BucketMap<OUTP> & out ) const;
169 };
170
171
174 template <class INP>
175 class RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >
176 {
177 public:
178 RawConverter( bool timeislabel = false )
179 : _timeIsLabel( timeislabel ) {}
180
181 void convert( const rc_ptr<Volume<INP> > &in,
182 aims::BucketMap<Void> & out ) const;
183
184 void setTimeIsLabel( bool x ) { _timeIsLabel = x; }
185 bool timeIsLabel() const { return( _timeIsLabel ); }
186
187 private:
188 bool _timeIsLabel;
189 };
190
191
194 template <class INP>
195 class RawConverter<VolumeRef<INP>, aims::BucketMap<Void> >
196 : public RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >
197 {
198 public:
199 RawConverter( bool timeislabel = false )
200 : RawConverter<rc_ptr<Volume<INP> >, aims::BucketMap<Void> >(
201 timeislabel )
202 {}
203 };
204
205
206 template <typename INP,typename OUTP>
207 class ConverterAllocator<aims::BucketMap<INP>,aims::BucketMap<OUTP> >
208 {
209 public:
211 };
212
213
214 template <typename INP,typename OUTP>
215 class ConverterAllocator<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
216 {
217 public:
218 static rc_ptr<Volume<OUTP> >* alloc( const aims::BucketMap<INP> &in );
219 };
220
221
222 template <typename INP,typename OUTP>
223 class ConverterAllocator<aims::BucketMap<INP>, VolumeRef<OUTP> >
224 : public ConverterAllocator<aims::BucketMap<INP>, rc_ptr<Volume<OUTP> > >
225 {
226 public:
228 {
229 return static_cast<VolumeRef<OUTP>*>(
231 rc_ptr<Volume<OUTP> > >::alloc( in ) );
232 }
233 };
234
235
236 // implementation
237
238 template <class INP,class OUTP> inline
240 ( const aims::BucketMap<INP> &in, aims::BucketMap<OUTP> &out ) const
241 {
242 out.setSizeXYZT( in.sizeX(), in.sizeY(), in.sizeZ(), in.sizeT() );
243 typename aims::BucketMap<INP>::const_iterator i, e = in.end();
244 typename aims::BucketMap<INP>::Bucket::const_iterator ib, eb;
245 OUTP outval;
246 RawConverter<INP,OUTP> itemconv;
247 out.setHeader( in.header() );
248 out.header().setProperty( "data_type", DataTypeCode<OUTP>::name() );
249
250 for( i=in.begin(); i!=e; ++i )
251 {
252 typename aims::BucketMap<OUTP>::Bucket & b = out[ i->first ];
253 for( ib=i->second.begin(), eb=i->second.end(); ib!=eb; ++ib )
254 {
255 itemconv.convert( ib->second, outval );
256 b[ ib->first ] = outval;
257 }
258 }
259 }
260
261
262 template <class INP,class OUTP> inline
264 carto::rc_ptr<Volume<OUTP> > >::printToVolume
265 ( const aims::BucketMap<INP> & in, carto::rc_ptr<Volume<OUTP> > & out,
266 const Point3d & offset ) const
267 {
268 typename aims::BucketMap<INP>::const_iterator it, et = in.end();
269 typename aims::BucketMap<INP>::Bucket::const_iterator ib, eb;
270 unsigned i = 0;
271 int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ(),
272 dt = out->getSizeT();
273
275
276 for( it=in.begin(); it!=et; ++it, ++i )
277 {
278 if( i >= dt )
279 continue;
280 for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
281 {
282 const Point3d pos = ib->first + offset;
283 if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
284 && pos[0] < dx && pos[1] < dy && pos[2] < dz )
285 itemconv.convert( ib->second,
286 out->at( pos[0], pos[1], pos[2], i ) );
287 }
288 }
289 }
290
291
292 namespace internal
293 {
294 template <typename OUTP, bool MODE>
296 {
297 static
300 const Point3d & offset, bool timelabel );
301 };
302
303 template <typename OUTP>
304 struct VolumePrinter<OUTP, true>
305 {
306 template <typename INP>
307 static
309 carto::rc_ptr<Volume<OUTP> > & out, const Point3d & offset,
310 bool timelabel )
311 {
312 if( !timelabel )
313 {
314 VolumePrinter<OUTP, false>::printToVolume( in, out, offset, false );
315 return;
316 }
317
319 aims::BucketMap<Void>::Bucket::const_iterator ib, eb;
320 unsigned i = 0;
321 int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ();
322
323 for( it=in.begin(); it!=et; ++it, ++i )
324 {
325 for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
326 {
327 const Point3d pos = ib->first + offset;
328 if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
329 && pos[0] < dx && pos[1] < dy && pos[2] < dz )
330 out->at( pos ) = (OUTP) it->first;
331 }
332 }
333 }
334 };
335
336
337 template <typename OUTP>
338 struct VolumePrinter<OUTP, false>
339 {
340 template <typename INP>
341 static
344 const Point3d & offset, bool )
345 {
346 typename aims::BucketMap<INP>::const_iterator it, et = in.end();
347 typename aims::BucketMap<INP>::Bucket::const_iterator ib, eb;
348 unsigned i = 0;
349 int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ(),
350 dt = out->getSizeT();
351
352 RawConverter<INP,OUTP> itemconv;
353
354 for( it=in.begin(); it!=et; ++it, ++i )
355 {
356 if( i >= dt )
357 continue;
358 for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
359 {
360 const Point3d pos = ib->first + offset;
361 if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
362 && pos[0] < dx && pos[1] < dy && pos[2] < dz )
363 itemconv.convert( ib->second,
364 out->at( pos[0], pos[1], pos[2], i ) );
365 }
366 }
367 }
368 };
369
370 template <typename OUTP> inline
373 const Point3d & offset, bool timelabel )
374 {
376 ( in, out, offset, timelabel );
377 }
378
379 }
380
381
382 template <typename OUTP> inline
383 void RawConverter<aims::BucketMap<Void>,
384 carto::rc_ptr<Volume<OUTP> > >::printToVolume
385 ( const aims::BucketMap<Void> & in, carto::rc_ptr<Volume<OUTP> > & out,
386 const Point3d & offset ) const
387 {
388 if( _hasValue )
389 {
390 typename aims::BucketMap<Void>::const_iterator it, et = in.end();
391 typename aims::BucketMap<Void>::Bucket::const_iterator ib, eb;
392 unsigned i = 0;
393 int dx = out->getSizeX(), dy = out->getSizeY(), dz = out->getSizeZ();
394 for( it=in.begin(); it!=et; ++it, ++i )
395 {
396 for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
397 {
398 const Point3d & pos = ib->first + offset;
399 if( pos[0] >= 0 && pos[1] >= 0 && pos[2] >= 0
400 && pos[0] < dx && pos[1] < dy && pos[2] < dz )
401 out->at( pos[0], pos[1], pos[2], i ) = _value;
402 }
403 }
404 }
405 else
406 internal::printToVolume_smart( in, out, offset, timeIsLabel() );
407 }
408
409
410 template <class INP,class OUTP> inline
412 aims::BucketMap<OUTP> >::convert
413 ( const carto::rc_ptr<Volume<INP> > &in, aims::BucketMap<OUTP> &out ) const
414 {
415 unsigned t, nt = in->getSizeT();
416 int x, y, z, nx = in->getSizeX(), ny = in->getSizeY(), nz = in->getSizeZ();
417 INP val;
418 OUTP outval;
419 RawConverter<INP,OUTP> itemconv;
420
421 std::vector<float> vs = in->getVoxelSize();
422 out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
423 for( t=0; t<nt; ++t )
424 {
425 //cout << "time : " << t << endl;
426 typename aims::BucketMap<OUTP>::Bucket & bck = out[t];
427 for( z=0; z<nz; ++z )
428 for( y=0; y<ny; ++y )
429 for( x=0; x<nx; ++x )
430 {
431 /*if( y == 0 && x == 0 )
432 cout << "slice : " << z << endl;*/
433 val = in->at( x, y, z, t );
434 if( val != (INP)0 )
435 {
436 itemconv.convert( val, outval );
437 bck.insert( std::pair<Point3d, OUTP>
438 ( Point3d( x, y, z ), outval ) );
439 }
440 }
441 if( bck.empty() )
442 out.erase( t );
443 }
444 }
445
446
447 template <class INP> inline
449 aims::BucketMap<Void> >::convert
450 ( const carto::rc_ptr<Volume<INP> > &in, aims::BucketMap<Void> &out ) const
451 {
452 unsigned t, nt = in->getSizeT();
453 int x, y, z, nx = in->getSizeX(), ny = in->getSizeY(), nz = in->getSizeZ();
454 INP val;
455
456 std::vector<float> vs = in->getVoxelSize();
457 out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
458 if( nt == 1 && timeIsLabel() )
459 {
460 size_t t;
462 for( z=0; z<nz; ++z )
463 for( y=0; y<ny; ++y )
464 for( x=0; x<nx; ++x )
465 {
466 val = in->at( x, y, z );
467 if( val != INP(0) )
468 {
469 rc.convert( val, t );
470 out[ t ].insert( std::pair<Point3d, Void>
471 ( Point3d( x, y, z ), Void() ) );
472 }
473 }
474 }
475 else
476 for( t=0; t<nt; ++t )
477 {
478 //cout << "time : " << t << endl;
479 aims::BucketMap<Void>::Bucket & bck = out[t];
480 for( z=0; z<nz; ++z )
481 for( y=0; y<ny; ++y )
482 for( x=0; x<nx; ++x )
483 {
484 val = in->at( x, y, z, t );
485 if( val != INP(0) )
486 bck.insert( std::pair<Point3d, Void>( Point3d( x, y, z ),
487 Void() ) );
488 }
489 if( bck.empty() )
490 out.erase( t );
491 }
492 }
493
494
495 template <class INP,class OUTP>
496 inline aims::BucketMap<OUTP>*
505
506
507 // VolumeRef implementation
508
509 template <class INP,class OUTP>
510 inline rc_ptr<Volume<OUTP> >*
512 ( const aims::BucketMap<INP> &in )
513 {
514 typename aims::BucketMap<INP>::const_iterator it, et = in.end();
515 typename aims::BucketMap<INP>::Bucket::const_iterator ib, eb;
516 Point3d bmin( 0, 0, 0 ), bmax( 0, 0, 0 );
517 bool first = true;
518
519 // seek bounding box
520 for( it=in.begin(); it!=et; ++it )
521 for( ib=it->second.begin(), eb=it->second.end(); ib!=eb; ++ib )
522 {
523 const Point3d & pos = ib->first;
524 if( first )
525 {
526 bmin[0] = pos[0];
527 bmin[1] = pos[1];
528 bmin[2] = pos[2];
529 bmax[0] = pos[0];
530 bmax[1] = pos[1];
531 bmax[2] = pos[2];
532 first = false;
533 }
534 else
535 {
536 if( pos[0] < bmin[0] )
537 bmin[0] = pos[0];
538 if( pos[1] < bmin[1] )
539 bmin[1] = pos[1];
540 if( pos[2] < bmin[2] )
541 bmin[2] = pos[2];
542 if( pos[0] > bmax[0] )
543 bmax[0] = pos[0];
544 if( pos[1] > bmax[1] )
545 bmax[1] = pos[1];
546 if( pos[2] > bmax[2] )
547 bmax[2] = pos[2];
548 }
549 }
550
551 if( bmin[0] >= 0 && bmin[1] >= 0 && bmin[2] >= 0 )
552 bmin = Point3d( 0, 0, 0 );
553
555 *out( new VolumeRef<OUTP>( bmax[0] - bmin[0] + 1, bmax[1] - bmin[1] + 1,
556 bmax[2] - bmin[2] + 1, in.size() ) );
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
564 (*out)->fill( 0 );
565 return out;
566 }
567
568
569 template <class INP,class OUTP> inline
571 ( const aims::BucketMap<INP> &in, rc_ptr<Volume<OUTP> > &out ) const
572 {
573 std::vector<float> vs(4);
574 vs[0] = in.sizeX();
575 vs[1] = in.sizeY();
576 vs[2] = in.sizeZ();
577 vs[3] = in.sizeT();
578 out->header().setProperty( "voxel_size", vs );
579 printToVolume( in, out, Point3d( 0, 0, 0 ) );
580 }
581
582
583 template <class OUTP> inline
585 ( const aims::BucketMap<Void> &in, rc_ptr<Volume<OUTP> > &out ) const
586 {
587 std::vector<float> vs(4);
588 vs[0] = in.sizeX();
589 vs[1] = in.sizeY();
590 vs[2] = in.sizeZ();
591 vs[3] = in.sizeT();
592 out->header().setProperty( "voxel_size", vs );
593 printToVolume( in, out, Point3d( 0, 0, 0 ) );
594 }
595
596}
597
598#endif
599
600
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
std::map< int, Bucket >::const_iterator const_iterator
Definition bucketMap.h:104
void insert(const Point3d &pos, const T &item)
Function redefined to omit time.
Definition bucketMap.h:194
const aims::PythonHeader & header() const
Definition bucketMap.h:155
void setSizeXYZT(float sizex, float sizey, float sizez, float sizet)
sets X,Y,Z and T resolutions of the data
Definition bucketMap.h:327
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::vector< float > getVoxelSize() const
Definition bucketMap.h:224
static VolumeRef< OUTP > * alloc(const aims::BucketMap< INP > &in)
static aims::BucketMap< OUTP > * alloc(const aims::BucketMap< INP > &in)
static rc_ptr< Volume< OUTP > > * alloc(const aims::BucketMap< INP > &in)
static OUTP * alloc(const INP &in)
std::string name()
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 convert(const aims::BucketMap< INP > &in, aims::BucketMap< OUTP > &out) const
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< INP > &in, rc_ptr< Volume< OUTP > > &out) const
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 convert(const aims::BucketMap< Void > &in, rc_ptr< Volume< OUTP > > &out) 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)
The class for EcatSino data write operation.
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)
AimsVector< int16_t, 3 > Point3d