cartodata  4.5.0
volumebase.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 #ifndef CARTODATA_VOLUME_VOLUMEBASE_H
35 #define CARTODATA_VOLUME_VOLUMEBASE_H
36 //--- cartodata --------------------------------------------------------------
39 //--- soma-io ----------------------------------------------------------------
42 //--- cartobase --------------------------------------------------------------
44 #include <cartobase/smart/rcptr.h>
45 //--- blitz ------------------------------------------------------------------
46 #ifdef CARTO_USE_BLITZ
47  #ifdef _WIN32
48  // disable thread-safe operations in blitz++ on windows since it uses pthread
49  #ifdef _REENTRANT
50  #define CARTO_REENTRANT_MEMO
51  #undef _REENTRANT
52  #endif
53  #include <blitz/blitz.h>
54  #ifdef CARTO_REENTRANT_MEMO
55  #define _REENTRANT
56  #undef CARTO_REENTRANT_MEMO
57  #endif
58  #endif
59  #include <blitz/array.h>
60 #endif
61 
62 //--- std --------------------------------------------------------------------
63 #include <vector>
64 #include <iostream>
65 //--- forward declarations ---------------------------------------------------
66 namespace carto {
67  class AllocatorContext;
68  class PropertyFilter;
69 }
70 //----------------------------------------------------------------------------
71 
72 namespace carto
73 {
74 //============================================================================
75 // V O L U M E
76 //============================================================================
108  template < typename T >
109  class Volume : public VolumeProxy< T >
110  {
111  public:
112  //========================================================================
113  // TYPES
114  //========================================================================
119  class Position4Di;
120 
122  typedef T datatype;
123 
128 #ifdef CARTO_USE_BLITZ
129  typedef typename blitz::Array<T,4>::iterator iterator;
130  typedef typename blitz::Array<T,4>::const_iterator const_iterator;
131 #else
132  typedef typename AllocatedVector<T>::iterator iterator;
133  typedef typename AllocatedVector<T>::const_iterator const_iterator;
134 #endif
135 
136  //========================================================================
137  // CONSTRUCTORS
138  //========================================================================
150  explicit Volume( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
151  int sizeT = 1,
152  const AllocatorContext& allocatorContext
153  = AllocatorContext(),
154  bool allocated = true );
163  explicit Volume( const Position4Di & size,
164  const AllocatorContext& allocatorContext
165  = AllocatorContext(),
166  bool allocated = true );
171  explicit Volume( int sizeX, int sizeY, int sizeZ,
172  int sizeT, int bordersize,
173  const AllocatorContext& allocatorContext
174  = AllocatorContext(),
175  bool allocated = true );
180  explicit Volume( const Position4Di & size, int bordersize,
181  const AllocatorContext& allocatorContext
182  = AllocatorContext(),
183  bool allocated = true );
187  explicit Volume( int sizeX, int sizeY, int sizeZ, int sizeT,
188  const Position4Di & border,
189  const AllocatorContext& allocatorContext
190  = AllocatorContext(),
191  bool allocated = true );
195  explicit Volume( const Position4Di & size,
196  const Position4Di & border,
197  const AllocatorContext& allocatorContext
198  = AllocatorContext(),
199  bool allocated = true );
202  Volume( int sizeX, int sizeY, int sizeZ, int sizeT, T* buffer );
206  Volume( const Position4Di & size, T* buffer );
212  Volume( rc_ptr<Volume<T> > other,
213  const Position4Di & pos = Position4Di( 0, 0, 0, 0 ),
214  const Position4Di & size = Position4Di( -1, -1, -1, -1 ),
215  const AllocatorContext & allocContext = AllocatorContext() );
220  Volume( const Volume< T >& other );
221  virtual ~Volume();
222 
223  Volume< T >& operator=( const Volume< T >& other );
224 
225  //========================================================================
226  // ITERATORS
227  //========================================================================
228 
234  iterator begin();
235  iterator end();
236  const_iterator begin() const;
237  const_iterator end() const;
238 
239  //========================================================================
240  // ACCESSORS
241  //========================================================================
242 
245  const T& operator()( long x, long y = 0, long z = 0, long t = 0 ) const;
246  T& operator() ( long x, long y = 0, long z = 0, long t = 0 );
247  const T& at( long x, long y = 0, long z = 0, long t = 0 ) const;
248  T& at( long x, long y = 0, long z = 0, long t = 0 );
249  const T& operator() ( const Position4Di & position ) const;
250  T& operator() ( const Position4Di & position );
251  const T& at( const Position4Di & position ) const;
252  T& at( const Position4Di & position );
253 
254 #ifdef CARTO_USE_BLITZ
255  const T & at( const blitz::TinyVector<int,1> & ) const;
256  T & at( const blitz::TinyVector<int,1> & );
257  const T & at( const blitz::TinyVector<int,2> & ) const;
258  T & at( const blitz::TinyVector<int,2> & );
259  const T & at( const blitz::TinyVector<int,3> & ) const;
260  T & at( const blitz::TinyVector<int,3> & );
261  const T & at( const blitz::TinyVector<int,4> & ) const;
262  T & at( const blitz::TinyVector<int,4> & );
263  blitz::Array<T,4> at( const blitz::RectDomain<4> & subdomain ) const;
264  blitz::Array<T,4> at( const blitz::StridedDomain<4> & subdomain ) const;
265  blitz::Array<T,4> at( const blitz::Range & r0 ) const;
266  blitz::Array<T,4> at( const blitz::Range & r0,
267  const blitz::Range & r1 ) const;
268  blitz::Array<T,4> at( const blitz::Range & r0, const blitz::Range & r1,
269  const blitz::Range & r2 ) const;
270  blitz::Array<T,4> at( const blitz::Range & r0, const blitz::Range & r1,
271  const blitz::Range & r2,
272  const blitz::Range & r3 ) const;
273 #endif
274 
275  //========================================================================
276  // INIT / ALLOCATION
277  //========================================================================
278 
280  virtual void initialize();
282  const AllocatorContext & allocatorContext() const;
288  void allocate();
290  virtual void reallocate( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
291  int sizeT = 1, bool keepcontents = false,
292  const AllocatorContext& allocatorContext
293  = AllocatorContext(), bool allocate = true );
294  virtual void reallocate( const Position4Di & size,
295  bool keepcontents = false,
296  const AllocatorContext& allocatorContext
297  = AllocatorContext(), bool allocate = true );
298 
299  //========================================================================
300  // COPY / VIEW
301  //========================================================================
302 
306  Volume<T> copy() const;
307  template <typename OUTP>
308  Volume<OUTP> copy() const;
311  Volume<T> deepcopy() const;
312  template <typename OUTP>
313  Volume<OUTP> deepcopy() const;
314 
316  Volume<T> copyStructure() const;
318  template <typename OUTP>
319  Volume<OUTP> copyStructure() const;
320 
322  template <typename OUTP>
323  operator Volume<OUTP>() const;
324 
331  rc_ptr<Volume<T> > refVolume() const;
333  void setRefVolume(const rc_ptr<Volume<T> > & refvol);
335  const Position4Di posInRefVolume() const;
337  void setPosInRefVolume(const Position4Di & pos);
338 
350  std::vector<int> getBorders() const;
351 
364  std::vector<size_t> getStrides() const;
365 
366  //========================================================================
367  // BOOLEANS / ACCUMULATED VALUES
368  //========================================================================
371  operator bool() const;
373  bool all() const;
375  bool any() const;
376  T min() const;
377  T max() const;
378  T sum() const;
379  template <typename OUTP>
380  OUTP sum() const;
381 
382  //========================================================================
383  // FILL / REPLACE
384  //========================================================================
386  void fill( const T & value );
387  Volume<T> & operator= ( const T & value );
388 
389  protected:
390  //========================================================================
391  // PRIVATE UTILS
392  //========================================================================
393  void allocate( int oldSizeX, int oldSizeY, int oldSizeZ, int oldSizeT,
394  bool allocate, const AllocatorContext& allocatorContext );
395  void slotSizeChanged( const PropertyFilter& propertyFilter );
396  void updateItemsBuffer();
397 
398  void constructBorders( const Position4Di & bordersize,
399  const AllocatorContext& allocatorContext,
400  bool allocated );
401 
402  AllocatedVector<T> _items;
403 #ifdef CARTO_USE_BLITZ
404  blitz::Array<T, 4> _blitz;
405 #else
406  size_t _lineoffset;
407  size_t _sliceoffset;
408  size_t _volumeoffset;
409 #endif
412  };
413 
414 //============================================================================
415 // POSITION VECTOR
416 //============================================================================
417  template <typename T>
418  class Volume<T>::Position4Di
419  {
420  public:
421  explicit Position4Di( int x = 0, int y = 0, int z = 0, int t = 0 )
422  : _coords( 4 )
423  {
424  _coords[0] = x;
425  _coords[1] = y;
426  _coords[2] = z;
427  _coords[3] = t;
428  }
429 
434  template <typename U>
435  Position4Di( const U & other ):
436  _coords(4)
437  {
438  for( int i = 0; i < other.size() && i < 4; ++i )
439  _coords[i] = other[i];
440  for( int i = other.size(); i < 4; ++i )
441  _coords[i] = 0;
442  }
443 
444  Position4Di( const Position4Di & pos ) : _coords( pos._coords )
445  {
446  }
447 
449 
450  int & operator [] ( int coord ) { return _coords[ coord ]; }
451  const int & operator [] ( int coord ) const { return _coords[ coord ]; }
452 
453  bool operator==(const Position4Di& p) const { return ( _coords[0] == p._coords[0]
454  && _coords[1] == p._coords[1]
455  && _coords[2] == p._coords[2]
456  && _coords[3] == p._coords[3] ); }
457  bool operator!=(const Position4Di& p) const { return !(this->operator ==(p)); }
458 
459  unsigned size() const { return 4; }
460 
461  private:
462  std::vector<int> _coords;
463  };
464 
465 //============================================================================
466 // UTILITIES
467 //============================================================================
468 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
469 
470  template <typename T>
471  class DataTypeCode<Volume<T> >
472  {
473  public:
474  static std::string objectType()
475  { return "CartoVolume"; }
476  static std::string dataType()
477  { return DataTypeCode<T>::dataType(); }
478  static std::string name()
479  {
480  return std::string("carto_volume of ") + DataTypeCode< T >::name();
481  }
482  };
483 
484  template <typename T>
485  class Creator<Volume<T> >
486  {
487  public:
488  static Volume<T>* create( Object, const AllocatorContext &, Object );
489  static void setup( Volume<T> &, Object, const AllocatorContext &, Object );
490  };
491 
492 #endif
493 
494 } // namespace carto
495 
496 //============================================================================
497 // STREAM
498 //============================================================================
499 
500 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
501 
502 namespace carto {
503 
504  // Warper for output streams that integrates parameters for volume
505  // printing
506  class VolumeOStream: public std::ostream
507  {
508  public:
509  VolumeOStream( std::ostream & ostream );
510  VolumeOStream( const VolumeOStream & other );
511 
512  std::ostream & ostream() const;
513 
514  const size_t & maxT() const;
515  const size_t & maxZ() const;
516  const size_t & maxY() const;
517  const size_t & maxX() const;
518 
519  size_t & maxT();
520  size_t & maxZ();
521  size_t & maxY();
522  size_t & maxX();
523 
524  private:
525  size_t _maxT;
526  size_t _maxZ;
527  size_t _maxY;
528  size_t _maxX;
529  std::ostream & _ostream;
530  };
531 
532  // Object returned by setMaxDim used to set Volume printing parameters
534  {
535  public:
537 
538  const size_t & maxT() const;
539  const size_t & maxZ() const;
540  const size_t & maxY() const;
541  const size_t & maxX() const;
542 
543  size_t & maxT();
544  size_t & maxZ();
545  size_t & maxY();
546  size_t & maxX();
547 
548  private:
549  size_t _maxT;
550  size_t _maxZ;
551  size_t _maxY;
552  size_t _maxX;
553  };
554 
555 
556 } // namespace carto:
557 
558 carto::VolumeOStream operator<< ( std::ostream & out,
559  const carto::VolumeOStreamSetter & setter );
560 
561 template <typename T>
562 std::ostream & operator<< ( const carto::VolumeOStream & out,
563  const carto::Volume<T> & volume );
564 
565 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
566 
567 namespace carto {
569  VolumeOStreamSetter setMaxDim( size_t m );
570 }
571 
585 template <typename T>
586 std::ostream & operator<< ( std::ostream & out,
587  const carto::Volume<T> & volume );
588 
589 
590 #endif // CARTODATA_VOLUME_VOLUMEBASE_H
Volume(int sizeX=1, int sizeY=1, int sizeZ=1, int sizeT=1, const AllocatorContext &allocatorContext=AllocatorContext(), bool allocated=true)
Volume construction and allocation.
Definition: volumebase_d.h:70
void setRefVolume(const rc_ptr< Volume< T > > &refvol)
Set parent volume.
Definition: volumebase_d.h:535
4D Volume main class
const Position4Di posInRefVolume() const
Get position in parent volume.
Definition: volumebase_d.h:451
void allocate()
This function is only useful in the particular context of an unallocated Volume, when the constructor...
Definition: volumebase_d.h:850
std::string dataType()
rc_ptr< Volume< T > > _refvol
Definition: volumebase.h:410
const T & operator()(long x, long y=0, long z=0, long t=0) const
Warning: this operator is not virtual, so may not have the expected result on inherited classes (see ...
iterator begin()
Iterators returned here are the most "basic" (and fastest) iterators: they go from the first voxel li...
Definition: volumebase_d.h:621
const AllocatorContext & allocatorContext() const
returns volume's AllocatorContext
Definition: volumebase_d.h:439
T datatype
Access to T type.
Definition: volumebase.h:119
blitz::Array< T, 4 >::const_iterator const_iterator
Definition: volumebase.h:130
std::vector< size_t > getStrides() const
Get strides for the volume.
Definition: volumebase_d.h:562
void updateItemsBuffer()
Definition: volumebase_d.h:457
const size_t & maxX() const
static std::string dataType()
Definition: volumebase.h:476
bool all() const
True if all values compare to true.
void setPosInRefVolume(const Position4Di &pos)
Set position in parent volume.
Definition: volumebase_d.h:527
std::string name()
blitz::Array< T, 4 > _blitz
Definition: volumebase.h:404
bool operator==(const Position4Di &p) const
Definition: volumebase.h:453
void fill(const T &value)
Fills the volume with a given value.
Volume< T > copy() const
Create a volume of same dimension and copy the data.
VolumeOStream(std::ostream &ostream)
std::ostream & ostream() const
virtual void reallocate(int sizeX=1, int sizeY=1, int sizeZ=1, int sizeT=1, bool keepcontents=false, const AllocatorContext &allocatorContext=AllocatorContext(), bool allocate=true)
allows resizing and changing allocator
Definition: volumebase_d.h:917
const size_t & maxT() const
rc_ptr< Volume< T > > refVolume() const
Get parent volume.
Definition: volumebase_d.h:445
blitz::Array< T, 4 >::iterator iterator
The most "basic" (and fastest) iterators: they go from the first voxel linerarly in memory...
Definition: volumebase.h:129
const size_t & maxZ() const
iterator end()
Definition: volumebase_d.h:634
std::vector< int > getBorders() const
Get borders for the volume.
Definition: volumebase_d.h:543
const size_t & maxY() const
const T & at(long x, long y=0, long z=0, long t=0) const
carto::VolumeOStream operator<<(std::ostream &out, const carto::VolumeOStreamSetter &setter)
carto::Volume< bool > operator==(const carto::Volume< T > &vol, const U &value)
void constructBorders(const Position4Di &bordersize, const AllocatorContext &allocatorContext, bool allocated)
Definition: volumebase_d.h:109
Volume< T > copyStructure() const
Copy the full data structure without copying the actual data.
const size_t & maxT() const
Volume< T > & operator=(const Volume< T > &other)
Definition: volumebase_d.h:581
const size_t & maxZ() const
Volume< T > deepcopy() const
Copy the full data structure.
unsigned size() const
Definition: volumebase.h:459
bool operator!=(const Position4Di &p) const
Definition: volumebase.h:457
void slotSizeChanged(const PropertyFilter &propertyFilter)
Definition: volumebase_d.h:860
Position4Di(const U &other)
Generic constructor from any "vector-like" object, i.e.
Definition: volumebase.h:435
const size_t & maxX() const
static std::string objectType()
Definition: volumebase.h:474
const size_t & maxY() const
Position4Di(int x=0, int y=0, int z=0, int t=0)
Definition: volumebase.h:421
Position4Di _pos
Definition: volumebase.h:411
Position4Di(const Position4Di &pos)
Definition: volumebase.h:444
AllocatedVector< T > _items
Definition: volumebase.h:402
virtual void initialize()
Initializes header info.
Definition: volumebase_d.h:673
bool any() const
True if at least one value compares to true.
virtual ~Volume()
Definition: volumebase_d.h:430