A.I.M.S


bucketMap.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  * Bucket class
36  */
37 #ifndef AIMS_BUCKET_BUCKETMAP_H
38 #define AIMS_BUCKET_BUCKETMAP_H
39 
42 #include <aims/bucket/bucket.h>
43 #include <aims/math/dtitensor.h>
44 #include <cartobase/type/types.h>
45 #include <iostream>
46 #include <map>
47 
48 
49 namespace aims
50 {
51  template <class T> class BucketMap;
52  //typedef BucketMap<short> BucketMapShort ;
53 
54 
56  template <class T, int D>
58  {};
59 
60  // Specialization
61  template <class T>
62  struct CoordinatesLess<T, 3>: public std::binary_function<T, T, bool>
63  {
64  bool operator () ( const T & p1, const T & p2 ) const
65  {
66  return p1[2] < p2[2]
67  || ( p1[2] == p2[2]
68  && ( p1[1] < p2[1]
69  || ( p1[1] == p2[1] && ( p1[0] < p2[0] ) ) ) );
70  }
71  };
72 
74 // /// Helper class for ordering Point3d by their z, y, x coordinates
75 // struct BucketMapLess : public std::binary_function<Point3d, Point3d, bool>
76 // {
77 // bool operator () ( const Point3d & p1, const Point3d & p2 ) const
78 // {
79 // return p1[2] < p2[2]
80 // || ( p1[2] == p2[2]
81 // && ( p1[1] < p2[1]
82 // || ( p1[1] == p2[1] && ( p1[0] < p2[0] ) ) ) );
83 // }
84 // };
85 
86 
95  template <class T>
96  class AIMSDATA_API BucketMap
97  : public virtual carto::RCObject,
98  public std::map< int , std::map< Point3d, T, BucketMapLess > >
99  {
100  public:
101  typedef std::map< int , std::map< Point3d, T, BucketMapLess > > BaseMap;
102  typedef std::map< Point3d, T, BucketMapLess > Bucket;
103  typedef typename std::map<int, Bucket >::iterator iterator;
104  typedef typename std::map<int, Bucket >::const_iterator const_iterator;
105 
106  BucketMap() : std::map< int , Bucket >()
107  { }
108  BucketMap( const BucketMap<T> & other )
109  : RCObject(), std::map< int , Bucket >( other ), _header( other._header )
110  { }
112  BucketMap( const AimsBucket<T> & );
113  virtual ~BucketMap() { }
114 
115  BucketMap<T> & operator = ( const AimsBucket<T> & );
117  inline void insert( const Point3d & pos, const T & item );
119  inline void insert( const std::pair<const Point3d,T> & item );
120  using BaseMap::insert; // make overloads from std::map visible
121 
123  inline void erase( const Point3d & pos );
124  using BaseMap::erase; // make overloads from std::map visible
125 
126  inline void merge( const BucketMap<T> & );
127 
129  inline float sizeX() const;
131  inline float sizeY() const;
133  inline float sizeZ() const;
135  inline float sizeT() const;
136 
138  inline void setSizeX(float sizex);
140  inline void setSizeY(float sizey);
142  inline void setSizeZ(float sizez);
144  inline void setSizeT(float sizet);
146  inline void setSizeXYZT(float sizex,float sizey,float sizez,float sizet);
147 
148  inline const aims::PythonHeader &header() const { return _header; }
149  inline aims::PythonHeader &header() { return _header; }
150  void setHeader( const aims::PythonHeader &hdr ) { _header = hdr; }
151 
152  protected:
154  };
155 
156 }
157 
158 
159 
160 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
161 
162 namespace carto
163 {
164 
165  template<class T> class DataTypeCode<aims::BucketMap<T> >
166  {
167  public:
168  static std::string objectType()
169  { return "Bucket"; }
170  static std::string dataType()
171  { return DataTypeCode<T>::dataType(); }
172  static std::string name()
173  {
174  return std::string("bucket of ") + DataTypeCode< T >::name();
175  }
176  };
177 
178 }
179 
180 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
181 
182 
183 namespace aims
184 {
185 
186  template <class T> inline
187  void BucketMap<T>::insert( const Point3d & pos, const T & item )
188  {
189  (*this)[0][pos] = item;
190  }
191 
192 
193  template <class T> inline
194  void BucketMap<T>::insert( const std::pair<const Point3d, T> & item )
195  {
196  (*this)[0].insert(item);
197  }
198 
199 
200  template <class T> inline
201  void BucketMap<T>::erase( const Point3d & pos )
202  {
203  (*this)[0].erase( pos );
204  }
205 
206 
207  template <class T> inline
208  void BucketMap<T>::merge( const BucketMap<T> & bck )
209  {
210  typename BucketMap<T>::const_iterator i, e = bck.end();
211  for( i=bck.begin(); i!=e; ++i )
212  (*this)[ i->first ].insert( i->second.begin(), i->second.end() );
213  }
214 
215 
216  template <class T> inline float BucketMap<T>::sizeX() const
217  {
218  std::vector<float> vs;
219  _header.getProperty( "voxel_size", vs );
220  if( vs.empty() )
221  return 1.;
222  return vs[0];
223  }
224 
225 
226  template <class T> inline float BucketMap<T>::sizeY() const
227  {
228  std::vector<float> vs;
229  _header.getProperty( "voxel_size", vs );
230  if( vs.size() < 2 )
231  return 1.;
232  return vs[1];
233  }
234 
235 
236  template <class T> inline float BucketMap<T>::sizeZ() const
237  {
238  std::vector<float> vs;
239  _header.getProperty( "voxel_size", vs );
240  if( vs.size() < 3 )
241  return 1.;
242  return vs[2];
243  }
244 
245 
246  template <class T> inline float BucketMap<T>::sizeT() const
247  {
248  std::vector<float> vs;
249  _header.getProperty( "voxel_size", vs );
250  if( vs.size() < 4 )
251  return 1.;
252  return vs[3];
253  }
254 
255 
256  template <class T> inline
257  void BucketMap<T>::setSizeX(float sizex)
258  {
259  ASSERT(sizex>0);
260  std::vector<float> vs;
261  _header.getProperty( "voxel_size", vs );
262  while( vs.size() < 4 )
263  vs.push_back( 1. );
264  vs[0] = sizex;
265  _header.setProperty( "voxel_size", vs );
266  }
267 
268 
269  template <class T> inline
270  void BucketMap<T>::setSizeY(float sizey)
271  {
272  ASSERT(sizey>0);
273  std::vector<float> vs;
274  _header.getProperty( "voxel_size", vs );
275  while( vs.size() < 4 )
276  vs.push_back( 1. );
277  vs[1] = sizey;
278  _header.setProperty( "voxel_size", vs );
279  }
280 
281 
282  template <class T> inline
283  void BucketMap<T>::setSizeZ(float sizez)
284  {
285  ASSERT(sizez>0);
286  std::vector<float> vs;
287  _header.getProperty( "voxel_size", vs );
288  while( vs.size() < 4 )
289  vs.push_back( 1. );
290  vs[2] = sizez;
291  _header.setProperty( "voxel_size", vs );
292  }
293 
294 
295  template <class T> inline
296  void BucketMap<T>::setSizeT(float sizet)
297  {
298  ASSERT(sizet>0);
299  std::vector<float> vs;
300  _header.getProperty( "voxel_size", vs );
301  while( vs.size() < 4 )
302  vs.push_back( 1. );
303  vs[3] = sizet;
304  _header.setProperty( "voxel_size", vs );
305  }
306 
307 
308  template <class T> inline
309  void BucketMap<T>::setSizeXYZT( float sizex, float sizey, float sizez,
310  float sizet)
311  {
312  std::vector<float> vs(4);
313  vs[0] = sizex;
314  vs[1] = sizey;
315  vs[2] = sizez;
316  vs[3] = sizet;
317  _header.setProperty( "voxel_size", vs );
318  }
319 
320 
321  template<class T> inline
323  : _header( other.header() )
324  {
325  *this = other;
326  }
327 
328 
329  template<class T> inline BucketMap<T> &
331  {
332  this->clear();
333  typename AimsBucket<T>::const_iterator ib, eb = other.end();
334  typename std::list<AimsBucketItem<T> >::const_iterator ibi, ebi;
335 
336  for ( ib=other.begin(); ib!=eb; ++ib )
337  {
338  Bucket & bk = (*this)[ ib->first ];
339  for ( ibi=ib->second.begin(), ebi=ib->second.end(); ibi!=ebi; ++ibi )
340  bk[ ibi->location() ] = ibi->value();
341  }
342  _header = other.header();
343  return *this;
344  }
345 
346 
347 }
348 
349 template <class T> inline
350 std::ostream& operator << (std::ostream& out, const aims::BucketMap<T> & thing)
351 {
352  out << "{";
353 
354  typename aims::BucketMap<T>::const_iterator it1, e1 = thing.end();
355 
356  for ( it1=thing.begin(); it1!=e1; ++it1 )
357  {
358  typename aims::BucketMap<T>::Bucket::const_iterator it2 , e2 = it1->second.end();
359  out << "{";
360  out << "t=" << (*it1).first << ",";
361  for ( it2=it1->second.begin(); it2!=e2; ++it2 )
362  out << *it2 << ",";
363  out << "NULL},";
364  }
365 
366  return out << "NULL}" << std::flush;
367 }
368 
369 // Implementation of some AimsBucket conversion functions
370 
371 template<class T> inline
373 {
374  *this = b2;
375 }
376 
377 
378 template<class T> inline AimsBucket<T> &
380 {
381  this->clear();
382  typename aims::BucketMap<T>::const_iterator ib, eb = b2.end();
384  AimsBucketItem<T> bitem;
385 
386  for ( ib=b2.begin(); ib!=eb; ++ib )
387  {
388  std::list<AimsBucketItem<T> > & bk = (*this)[ ib->first ];
389  for ( ibi=ib->second.begin(), ebi=ib->second.end(); ibi!=ebi; ++ibi )
390  {
391  bitem.location() = ibi->first;
392  bitem.value() = ibi->second;
393  bk.push_back( bitem );
394  }
395  }
396  _header = b2.header();
397  return *this;
398 }
399 
400 namespace carto {
401 
403 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<int16_t> )
404 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<uint16_t> )
405 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<int32_t> )
406 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<uint32_t> )
407 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<float> )
408 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<double> )
409 DECLARE_GENERIC_OBJECT_TYPE( aims::BucketMap<DtiTensor> )
410 
411 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<Void> > )
412 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<int16_t> > )
413 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<uint16_t> > )
414 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<int32_t> > )
415 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<uint32_t> > )
416 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<float> > )
417 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<double> > )
418 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< aims::BucketMap<DtiTensor> > )
419 
420 } // namespace carto
421 
422 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
AimsBucket< T > & operator=(const aims::BucketMap< T > &)
Definition: bucketMap.h:379
BucketMap< T > & operator=(const AimsBucket< T > &)
Definition: bucketMap.h:330
std::string dataType()
std::map< int, std::list< AimsBucketItem< T > > >::const_iterator const_iterator
Definition: bucket.h:76
void setSizeT(float sizet)
sets the T resolution of the data in s
Definition: bucketMap.h:296
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:51
std::map< int, std::map< Point3d, T, BucketMapLess > > BaseMap
Definition: bucketMap.h:101
void erase(const Point3d &pos)
Function redefined to omit time.
Definition: bucketMap.h:201
BucketMap(const BucketMap< T > &other)
Definition: bucketMap.h:108
const T & value() const
Get a const reference to the value of the bucket item.
Definition: item.h:87
#define AIMSDATA_API
const aims::PythonHeader & header() const
Definition: bucket.h:140
float sizeX() const
returns the X resolution in mm
Definition: bucketMap.h:216
virtual ~BucketMap()
Definition: bucketMap.h:113
std::string name()
float sizeT() const
returns the T resolution in s
Definition: bucketMap.h:246
void setSizeY(float sizey)
sets the Y resolution of the data in mm
Definition: bucketMap.h:270
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
std::map< Point3d, T, BucketMapLess > Bucket
Definition: bucketMap.h:102
void setSizeX(float sizex)
sets the X resolution of the data in mm
Definition: bucketMap.h:257
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
The bucket base class to manage packages of points associated to their value during time...
Definition: bucket.h:48
void insert(const Point3d &pos, const T &item)
Function redefined to omit time.
Definition: bucketMap.h:187
void merge(const BucketMap< T > &)
Definition: bucketMap.h:208
const AimsVector< short, 3 > & location() const
Get a const reference to the location of the bucket item.
Definition: item.h:92
The template base class for all types of bucket items.
Definition: item.h:47
void setSizeZ(float sizez)
sets the Z resolution of the data in mm
Definition: bucketMap.h:283
std::map< int, Bucket >::iterator iterator
Definition: bucketMap.h:103
aims::PythonHeader _header
Definition: bucketMap.h:153
aims::PythonHeader & header()
Definition: bucketMap.h:149
std::map< int, Bucket >::const_iterator const_iterator
Definition: bucketMap.h:104
void setHeader(const aims::PythonHeader &hdr)
Definition: bucketMap.h:150
float sizeY() const
returns the Y resolution in mm
Definition: bucketMap.h:226
AimsBucket()
Definition: bucket.h:80
const aims::PythonHeader & header() const
Definition: bucketMap.h:148
#define ASSERT(EX)
Helper class for ordering Coordinates by their z, y, x coordinates.
Definition: bucketMap.h:57
CoordinatesLess< Point3d, 3 > BucketMapLess
Definition: bucketMap.h:73