cartodata  5.0.5
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>
46 //--- blitz ------------------------------------------------------------------
47 #ifdef CARTO_USE_BLITZ
48  #ifdef _WIN32
49  // disable thread-safe operations in blitz++ on windows since it uses pthread
50  #ifdef _REENTRANT
51  #define CARTO_REENTRANT_MEMO
52  #undef _REENTRANT
53  #endif
54  #include <blitz/blitz.h>
55  #ifdef CARTO_REENTRANT_MEMO
56  #define _REENTRANT
57  #undef CARTO_REENTRANT_MEMO
58  #endif
59  #endif
60  #include <blitz/array.h>
61 
62 namespace carto {
63  typedef ptrdiff_t BlitzStridesType;
64 }
65 
66 #endif
67 
68 //--- std --------------------------------------------------------------------
69 #include <vector>
70 #include <iostream>
71 //--- forward declarations ---------------------------------------------------
72 namespace carto {
73  class AllocatorContext;
74  class PropertyFilter;
75 }
76 //----------------------------------------------------------------------------
77 
78 namespace carto
79 {
80 //============================================================================
81 // V O L U M E
82 //============================================================================
120  template < typename T >
121  class Volume : public VolumeProxy< T >
122  {
123  public:
124  //========================================================================
125  // TYPES
126  //========================================================================
131  class Position4Di;
132  typedef std::vector<int> Position;
133 
135  typedef T datatype;
136 
141 #ifdef CARTO_USE_BLITZ
142  // static const int DIM_MAX = 8; leads to build issues on old GCC versions
143  // so we need to use enum to be able to declare DIM_MAX in carto::Volume
144  enum { DIM_MAX = 8 };
145  typedef typename blitz::Array<T,Volume<T>::DIM_MAX>::iterator iterator;
146  typedef typename blitz::Array<T,Volume<T>::DIM_MAX>::const_iterator const_iterator;
147 #else
148  enum { DIM_MAX = 4 }; // still limited to 4D in this case.
149  typedef typename AllocatedVector<T>::iterator iterator;
150  typedef typename AllocatedVector<T>::const_iterator const_iterator;
151 #endif
152 
153  //========================================================================
154  // CONSTRUCTORS
155  //========================================================================
167  explicit Volume( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
168  int sizeT = 1,
169  const AllocatorContext& allocatorContext
170  = AllocatorContext(),
171  bool allocated = true );
180  explicit Volume( const Position4Di & size,
181  const AllocatorContext& allocatorContext
182  = AllocatorContext(),
183  bool allocated = true );
188  explicit Volume( int sizeX, int sizeY, int sizeZ,
189  int sizeT, int bordersize,
190  const AllocatorContext& allocatorContext
191  = AllocatorContext(),
192  bool allocated = true );
197  explicit Volume( const Position4Di & size, int bordersize,
198  const AllocatorContext& allocatorContext
199  = AllocatorContext(),
200  bool allocated = true );
204  explicit Volume( int sizeX, int sizeY, int sizeZ, int sizeT,
205  const Position4Di & border,
206  const AllocatorContext& allocatorContext
207  = AllocatorContext(),
208  bool allocated = true );
212  explicit Volume( const Position4Di & size,
213  const Position4Di & border,
214  const AllocatorContext& allocatorContext
215  = AllocatorContext(),
216  bool allocated = true );
220  explicit Volume( const std::vector<int> & size,
221  const AllocatorContext& allocatorContext
222  = AllocatorContext(),
223  bool allocated = true );
227  explicit Volume( const std::vector<int> & size,
228  const std::vector<int> & border,
229  const AllocatorContext& allocatorContext
230  = AllocatorContext(),
231  bool allocated = true );
234  Volume( int sizeX, int sizeY, int sizeZ, int sizeT, T* buffer );
238  Volume( const Position4Di & size, T* buffer );
239  Volume( const std::vector<int> & size, T* buffer );
245  Volume( rc_ptr<Volume<T> > other,
246  const Position4Di & pos = Position4Di( 0, 0, 0, 0 ),
247  const Position4Di & size = Position4Di( -1, -1, -1, -1 ),
248  const AllocatorContext & allocContext = AllocatorContext() );
254  Volume( rc_ptr<Volume<T> > other,
255  const Position & pos,
256  const Position & size = Position(),
257  const AllocatorContext & allocContext = AllocatorContext() );
262  Volume( const Volume< T >& other );
263  virtual ~Volume();
264 
265  Volume< T >& operator=( const Volume< T >& other );
266 
267  //========================================================================
268  // ITERATORS
269  //========================================================================
270 
276  iterator begin();
277  iterator end();
278  const_iterator begin() const;
279  const_iterator end() const;
280 
281  //========================================================================
282  // ACCESSORS
283  //========================================================================
284 
287  const T& operator()( long x, long y = 0, long z = 0, long t = 0 ) const;
288 // T& operator() ( long x );
289  T& operator() ( long x, long y = 0, long z = 0, long t = 0 );
290  const T& at( long x, long y = 0, long z = 0, long t = 0 ) const;
291  T& at( long x, long y = 0, long z = 0, long t = 0 );
292  const T& operator() ( const Position4Di & position ) const;
293  T& operator() ( const Position4Di & position );
294  const T& at( const Position4Di & position ) const;
295  T& at( const Position4Di & position );
296  const T & at( const std::vector<int> & ) const;
297  T & at( const std::vector<int> & );
298  const T& operator() ( const std::vector<int> & position ) const;
299  T& operator() ( const std::vector<int> & position );
300 
301 #ifdef CARTO_USE_BLITZ
302  const T & at( const blitz::TinyVector<int,1> & ) const;
303  T & at( const blitz::TinyVector<int,1> & );
304  const T & at( const blitz::TinyVector<int,2> & ) const;
305  T & at( const blitz::TinyVector<int,2> & );
306  const T & at( const blitz::TinyVector<int,3> & ) const;
307  T & at( const blitz::TinyVector<int,3> & );
308  const T & at( const blitz::TinyVector<int,4> & ) const;
309  T & at( const blitz::TinyVector<int,4> & );
310  const T & at( const blitz::TinyVector<int,Volume<T>::DIM_MAX> & ) const;
311  T & at( const blitz::TinyVector<int,Volume<T>::DIM_MAX> & );
312  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::RectDomain<Volume<T>::DIM_MAX>
313  & subdomain ) const;
314  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::StridedDomain<Volume<T>::DIM_MAX>
315  & subdomain ) const;
316  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0 ) const;
317  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
318  const blitz::Range & r1 ) const;
319  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
320  const blitz::Range & r1,
321  const blitz::Range & r2 ) const;
322  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
323  const blitz::Range & r1,
324  const blitz::Range & r2,
325  const blitz::Range & r3 ) const;
326  // to get beyond 4 dimensions
327  const T & at( long x1, long x2, long x3, long x4, long x5, long x6=0,
328  long x7=0, long x8=0 ) const;
329  T & at( long x1, long x2, long x3, long x4, long x5, long x6=0,
330  long x7=0, long x8=0 );
331  const T& operator()( long x1, long x2, long x3, long x4, long x5,
332  long x6=0, long x7=0, long x8=0 ) const;
333  T& operator() ( long x1, long x2, long x3, long x4, long x5, long x6=0,
334  long x7=0, long x8=0 );
335 #endif
336 
337  //========================================================================
338  // INIT / ALLOCATION
339  //========================================================================
340 
342  virtual void initialize();
344  const AllocatorContext & allocatorContext() const;
350  void allocate();
352  virtual void reallocate( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
353  int sizeT = 1, bool keepcontents = false,
354  const AllocatorContext& allocatorContext
355  = AllocatorContext(), bool allocate = true );
356  virtual void reallocate( const Position4Di & size,
357  bool keepcontents = false,
358  const AllocatorContext& allocatorContext
359  = AllocatorContext(), bool allocate = true );
360  virtual void reallocate( const std::vector<int> & size,
361  bool keepcontents = false,
362  const AllocatorContext& allocatorContext
363  = AllocatorContext(), bool allocate = true );
364 
365  //========================================================================
366  // COPY / VIEW
367  //========================================================================
368 
372  Volume<T> copy() const;
373  template <typename OUTP>
374  Volume<OUTP> copy() const;
377  Volume<T> deepcopy() const;
378  template <typename OUTP>
379  Volume<OUTP> deepcopy() const;
380 
382  Volume<T> copyStructure() const;
384  template <typename OUTP>
385  Volume<OUTP> copyStructure() const;
386 
388  template <typename OUTP>
389  operator Volume<OUTP>() const;
390 
393  int getLevelsCount() const;
394 
399  int refLevel(const int level) const;
400 
407  rc_ptr<Volume<T> > refVolume() const;
408 
415  rc_ptr<Volume<T> > refVolumeAtLevel(const int level) const;
416 
418  void setRefVolume(const rc_ptr<Volume<T> > & refvol);
420  const Position & posInRefVolume() const;
422  Position posInRefVolumeAtLevel(const int level) const;
423 
425  void setPosInRefVolume(const Position4Di & pos);
426  void setPosInRefVolume(const Position & pos);
427 
428 
440  std::vector<int> getBorders() const;
441 
454  std::vector<size_t> getStrides() const;
455 
456  //========================================================================
457  // BOOLEANS / ACCUMULATED VALUES
458  //========================================================================
461  operator bool() const;
463  bool all() const;
465  bool any() const;
466  T min() const;
467  T max() const;
470  typename DataTypeTraits<T>::LongType sum() const;
471 
472  //========================================================================
473  // FILL / REPLACE
474  //========================================================================
476  void fill( const T & value );
482  void fillBorder( const T & value );
497  Volume<T> & operator= ( const T & value );
498 
499  protected:
500  //========================================================================
501  // PRIVATE UTILS
502  //========================================================================
503  void allocate( int oldSizeX, int oldSizeY, int oldSizeZ, int oldSizeT,
504  bool allocate, const AllocatorContext& allocatorContext );
505  void allocate( const std::vector<int> & oldSize,
506  bool allocate, const AllocatorContext& allocatorContext );
507  void slotSizeChanged( const PropertyFilter& propertyFilter );
508  void updateItemsBuffer();
509 
510  void constructBorders( const Position & bordersize,
511  const AllocatorContext& allocatorContext,
512  bool allocated );
513 
514  AllocatedVector<T> _items;
515 #ifdef CARTO_USE_BLITZ
516  blitz::Array<T, Volume<T>::DIM_MAX> _blitz;
517 #else
518  size_t _lineoffset;
519  size_t _sliceoffset;
520  size_t _volumeoffset;
521 #endif
523  std::vector<int> _pos;
524  };
525 
526 //============================================================================
527 // POSITION VECTOR
528 //============================================================================
529  template <typename T>
530  class Volume<T>::Position4Di
531  {
532  public:
533  explicit Position4Di( int x = 0, int y = 0, int z = 0, int t = 0 )
534  : _coords( 4 )
535  {
536  _coords[0] = x;
537  _coords[1] = y;
538  _coords[2] = z;
539  _coords[3] = t;
540  }
541 
546  template <typename U>
547  Position4Di( const U & other ):
548  _coords(4)
549  {
550  for( size_t i = 0; i < size_t( other.size() ) && i < 4; ++i )
551  _coords[i] = other[i];
552  for( size_t i = size_t( other.size() ); i < 4; ++i )
553  _coords[i] = 0;
554  }
555 
556  Position4Di( const Position4Di & pos ) : _coords( pos._coords )
557  {
558  }
559 
561 
562  int & operator [] ( int coord ) { return _coords[ coord ]; }
563  const int & operator [] ( int coord ) const { return _coords[ coord ]; }
564 
565  bool operator==(const Position4Di& p) const
566  {
567  return ( _coords == p._coords );
568  }
569  bool operator!=(const Position4Di& p) const
570  { return !(this->operator ==(p)); }
571 
572  unsigned size() const { return _coords.size(); }
573  const std::vector<int> & toVector() const
574  { return _coords; }
575 
576  /* utility functions, not really related, but which we don't really know
577  where to put them */
578  static std::vector<int> fixed_position( const std::vector<int> & vec )
579  {
580  if( vec.size() >= 4 )
581  return vec;
582  std::vector<int> fixed( vec );
583  fixed.resize( 4 );
584  for( int i=vec.size(); i<4; ++i )
585  fixed[i] = 0;
586  return fixed;
587  }
588 
589  static std::vector<int> fixed_size( const std::vector<int> & vec )
590  {
591  if( vec.size() >= 4 )
592  return vec;
593  std::vector<int> fixed( vec );
594  fixed.resize( 4 );
595  for( int i=vec.size(); i<4; ++i )
596  fixed[i] = 1;
597  return fixed;
598  }
599 
600  static unsigned long long size_num_elements( const std::vector<int> & vec )
601  {
602  int i, n = vec.size();
603  unsigned long long num = 1;
604  for( i=0; i<n; ++i )
605  num *= vec[i];
606  return num;
607  }
608 
609  private:
610  std::vector<int> _coords;
611  };
612 
613 //============================================================================
614 // UTILITIES
615 //============================================================================
616 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
617 
618  template <typename T>
619  class DataTypeCode<Volume<T> >
620  {
621  public:
622  static std::string objectType()
623  { return "CartoVolume"; }
624  static std::string dataType()
625  { return DataTypeCode<T>::dataType(); }
626  static std::string name()
627  {
628  return std::string("carto_volume of ") + DataTypeCode< T >::name();
629  }
630  };
631 
632  template <typename T>
633  class Creator<Volume<T> >
634  {
635  public:
636  static Volume<T>* create( Object, const AllocatorContext &, Object );
637  static void setup( Volume<T> &, Object, const AllocatorContext &, Object );
638  };
639 
640 #endif
641 
642 //============================================================================
643 // STREAM
644 //============================================================================
645 
646 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
647 
648  // Warper for output streams that integrates parameters for volume
649  // printing
650  class VolumeOStream // : public std::ostream // is not really a stream
651  {
652  public:
653  VolumeOStream( std::ostream & ostream );
654  VolumeOStream( const VolumeOStream & other );
655  ~VolumeOStream();
656 
657  std::ostream & ostream() const;
658 
659  const size_t & maxT() const;
660  const size_t & maxZ() const;
661  const size_t & maxY() const;
662  const size_t & maxX() const;
663 
664  size_t & maxT();
665  size_t & maxZ();
666  size_t & maxY();
667  size_t & maxX();
668 
669  private:
670  size_t _maxT;
671  size_t _maxZ;
672  size_t _maxY;
673  size_t _maxX;
674  std::ostream & _ostream;
675  };
676 
677  // Object returned by setMaxDim used to set Volume printing parameters
679  {
680  public:
682 
683  const size_t & maxT() const;
684  const size_t & maxZ() const;
685  const size_t & maxY() const;
686  const size_t & maxX() const;
687 
688  size_t & maxT();
689  size_t & maxZ();
690  size_t & maxY();
691  size_t & maxX();
692 
693  private:
694  size_t _maxT;
695  size_t _maxZ;
696  size_t _maxY;
697  size_t _maxX;
698  };
699 
700  VolumeOStream operator<< ( std::ostream & out,
701  const VolumeOStreamSetter & setter );
702 
703  template <typename T>
704  std::ostream & operator<< ( const VolumeOStream & out,
705  const Volume<T> & volume );
706 
707  // Method used to set Volume printing parameters
708  VolumeOStreamSetter setMaxDim( size_t m );
709  VolumeOStreamSetter setMaxDim( size_t mx, size_t my, size_t mz, size_t mt );
710 
711  #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
712 
713 
730  template <typename T>
731  std::ostream & operator<< ( std::ostream & out,
732  const Volume<T> & volume );
733 
734 
736  template <typename T>
737  void displayRefVolumes(const Volume<T> & vol);
738 
739 } // namespace carto:
740 
741 
742 # ifdef CARTO_USE_BLITZ
743 // the CT macro defined in blitz++ interferes with QColor::CT in Qt
744 # ifdef CT
745 # undef CT
746 # endif
747 # endif
748 
749 #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:779
const Position & posInRefVolume() const
Get position in parent volume.
Definition: volumebase_d.h:607
int refLevel(const int level) const
Transform a level index to a valid level index in the volume hierarchy.
Definition: volumebase_d.h:626
N-D Volume main class.
void allocate()
This function is only useful in the particular context of an unallocated Volume, when the constructor...
static std::vector< int > fixed_position(const std::vector< int > &vec)
Definition: volumebase.h:578
std::string dataType()
rc_ptr< Volume< T > > _refvol
Definition: volumebase.h:522
void constructBorders(const Position &bordersize, const AllocatorContext &allocatorContext, bool allocated)
Definition: volumebase_d.h:109
blitz::Array< T, Volume< T >::DIM_MAX > _blitz
Definition: volumebase.h:516
const T & at(long x, long y=0, long z=0, long t=0) const
iterator begin()
Iterators returned here are the most "basic" (and fastest) iterators: they go from the first voxel li...
Definition: volumebase_d.h:874
rc_ptr< Volume< T > > refVolumeAtLevel(const int level) const
Get parent volume at a specified level in volume hierarchy.
Definition: volumebase_d.h:644
T datatype
Access to T type.
Definition: volumebase.h:135
Position posInRefVolumeAtLevel(const int level) const
Get position relatively to parent volume at specified level.
Definition: volumebase_d.h:659
VolumeOStreamSetter setMaxDim(size_t m)
blitz::Array< T, Volume< T >::DIM_MAX >::const_iterator const_iterator
Definition: volumebase.h:146
void updateItemsBuffer()
Definition: volumebase_d.h:680
static std::string dataType()
Definition: volumebase.h:624
std::ostream & operator<<(std::ostream &out, const VoxelValue< T, C > &aa)
void setPosInRefVolume(const Position4Di &pos)
Set position in parent volume.
Definition: volumebase_d.h:757
VolumeProxy is the base class for volumes.
Definition: volumeproxy.h:49
std::vector< size_t > getStrides() const
Get strides for the volume.
Definition: volumebase_d.h:806
std::string name()
const AllocatorContext & allocatorContext() const
returns volume&#39;s AllocatorContext
Definition: volumebase_d.h:595
carto::Volume< bool > operator==(const carto::Volume< T > &vol, const U &value)
std::vector< int > getBorders() const
Get borders for the volume.
Definition: volumebase_d.h:787
void fill(const T &value)
Fills the volume with a given value.
bool operator==(const Position4Di &p) const
Definition: volumebase.h:565
rc_ptr< Volume< T > > refVolume() const
Get parent volume.
Definition: volumebase_d.h:601
void fillBorder(const T &value)
Fill border with a constant value.
int getLevelsCount() const
Get levels count in volume hierarchy from the current volume to the topmost volume.
Definition: volumebase_d.h:614
std::vector< int > _pos
Definition: volumebase.h:523
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
static unsigned long long size_num_elements(const std::vector< int > &vec)
Definition: volumebase.h:600
Volume< T > copyStructure() const
Copy the full data structure without copying the actual data.
void displayRefVolumes(const Volume< T > &vol)
Display information about volumes hierarchy.
ptrdiff_t BlitzStridesType
Definition: volumebase.h:63
const std::vector< int > & toVector() const
Definition: volumebase.h:573
iterator end()
Definition: volumebase_d.h:887
Volume< T > copy() const
Create a volume of same dimension and copy the data.
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 ...
blitz::Array< T, Volume< T >::DIM_MAX >::iterator iterator
Definition: volumebase.h:145
Volume< T > deepcopy() const
Copy the full data structure.
Volume< T > & operator=(const Volume< T > &other)
Definition: volumebase_d.h:828
DataTypeTraits< T >::LongType sum() const
To avoid overflow, the biggest possible type (intmax_t, uintmax_t, double...) is used for computation...
bool any() const
True if at least one value compares to true.
bool operator!=(const Position4Di &p) const
Definition: volumebase.h:569
bool all() const
True if all values compare to true.
void slotSizeChanged(const PropertyFilter &propertyFilter)
Position4Di(const U &other)
Generic constructor from any "vector-like" object, i.e.
Definition: volumebase.h:547
static std::string objectType()
Definition: volumebase.h:622
Position4Di(int x=0, int y=0, int z=0, int t=0)
Definition: volumebase.h:533
std::vector< int > Position
Definition: volumebase.h:131
Position4Di(const Position4Di &pos)
Definition: volumebase.h:556
AllocatedVector< T > _items
Definition: volumebase.h:514
virtual void initialize()
Initializes header info.
Definition: volumebase_d.h:926
unsigned size() const
Definition: volumebase.h:572
virtual ~Volume()
Definition: volumebase_d.h:586
static std::vector< int > fixed_size(const std::vector< int > &vec)
Definition: volumebase.h:589