cartodata  5.1.2
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 _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 
61 namespace carto {
62  typedef ptrdiff_t BlitzStridesType;
63 }
64 
65 
66 //--- std --------------------------------------------------------------------
67 #include <vector>
68 //--- forward declarations ---------------------------------------------------
69 namespace carto {
70  class AllocatorContext;
71  class PropertyFilter;
72 }
73 //----------------------------------------------------------------------------
74 
75 namespace carto
76 {
77 //============================================================================
78 // V O L U M E
79 //============================================================================
117  template < typename T >
118  class Volume : public VolumeProxy< T >
119  {
120  public:
121  //========================================================================
122  // TYPES
123  //========================================================================
128  class Position4Di;
129  typedef std::vector<int> Position;
130 
132  typedef T datatype;
133 
138  // static const int DIM_MAX = 8; leads to build issues on old GCC versions
139  // so we need to use enum to be able to declare DIM_MAX in carto::Volume
140  enum { DIM_MAX = 8 };
141  typedef typename blitz::Array<T,Volume<T>::DIM_MAX>::iterator iterator;
142  typedef typename blitz::Array<T,Volume<T>::DIM_MAX>::const_iterator const_iterator;
143 
144  //========================================================================
145  // CONSTRUCTORS
146  //========================================================================
158  explicit Volume( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
159  int sizeT = 1,
160  const AllocatorContext& allocatorContext
161  = AllocatorContext(),
162  bool allocated = true );
171  explicit Volume( const Position4Di & size,
172  const AllocatorContext& allocatorContext
173  = AllocatorContext(),
174  bool allocated = true );
179  explicit Volume( int sizeX, int sizeY, int sizeZ,
180  int sizeT, int bordersize,
181  const AllocatorContext& allocatorContext
182  = AllocatorContext(),
183  bool allocated = true );
188  explicit Volume( const Position4Di & size, int bordersize,
189  const AllocatorContext& allocatorContext
190  = AllocatorContext(),
191  bool allocated = true );
195  explicit Volume( int sizeX, int sizeY, int sizeZ, int sizeT,
196  const Position4Di & border,
197  const AllocatorContext& allocatorContext
198  = AllocatorContext(),
199  bool allocated = true );
203  explicit Volume( const Position4Di & size,
204  const Position4Di & border,
205  const AllocatorContext& allocatorContext
206  = AllocatorContext(),
207  bool allocated = true );
211  explicit Volume( const std::vector<int> & size,
212  const AllocatorContext& allocatorContext
213  = AllocatorContext(),
214  bool allocated = true );
218  explicit Volume( const std::vector<int> & size,
219  const std::vector<int> & border,
220  const AllocatorContext& allocatorContext
221  = AllocatorContext(),
222  bool allocated = true );
225  Volume( int sizeX, int sizeY, int sizeZ, int sizeT, T* buffer,
226  const std::vector<size_t> *strides = 0 );
230  Volume( const Position4Di & size, T* buffer,
231  const std::vector<size_t> *strides = 0 );
232  Volume( const std::vector<int> & size, T* buffer,
233  const std::vector<size_t> *strides = 0 );
239  Volume( rc_ptr<Volume<T> > other,
240  const Position4Di & pos = Position4Di( 0, 0, 0, 0 ),
241  const Position4Di & size = Position4Di( -1, -1, -1, -1 ),
242  const AllocatorContext & allocContext = AllocatorContext() );
248  Volume( rc_ptr<Volume<T> > other,
249  const Position & pos,
250  const Position & size = Position(),
251  const AllocatorContext & allocContext = AllocatorContext() );
254  Volume( rc_ptr<Volume<T> > other,
255  const Position & pos,
256  const Position & size,
257  T* buffer, const std::vector<size_t> & strides );
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  const T & at( const blitz::TinyVector<int,1> & ) const;
302  T & at( const blitz::TinyVector<int,1> & );
303  const T & at( const blitz::TinyVector<int,2> & ) const;
304  T & at( const blitz::TinyVector<int,2> & );
305  const T & at( const blitz::TinyVector<int,3> & ) const;
306  T & at( const blitz::TinyVector<int,3> & );
307  const T & at( const blitz::TinyVector<int,4> & ) const;
308  T & at( const blitz::TinyVector<int,4> & );
309  const T & at( const blitz::TinyVector<int,Volume<T>::DIM_MAX> & ) const;
310  T & at( const blitz::TinyVector<int,Volume<T>::DIM_MAX> & );
311  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::RectDomain<Volume<T>::DIM_MAX>
312  & subdomain ) const;
313  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::StridedDomain<Volume<T>::DIM_MAX>
314  & subdomain ) const;
315  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0 ) const;
316  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
317  const blitz::Range & r1 ) const;
318  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
319  const blitz::Range & r1,
320  const blitz::Range & r2 ) const;
321  blitz::Array<T,Volume<T>::DIM_MAX> at( const blitz::Range & r0,
322  const blitz::Range & r1,
323  const blitz::Range & r2,
324  const blitz::Range & r3 ) const;
325  // to get beyond 4 dimensions
326  const T & at( long x1, long x2, long x3, long x4, long x5, long x6=0,
327  long x7=0, long x8=0 ) const;
328  T & at( long x1, long x2, long x3, long x4, long x5, long x6=0,
329  long x7=0, long x8=0 );
330  const T& operator()( long x1, long x2, long x3, long x4, long x5,
331  long x6=0, long x7=0, long x8=0 ) const;
332  T& operator() ( long x1, long x2, long x3, long x4, long x5, long x6=0,
333  long x7=0, long x8=0 );
334 
335  //========================================================================
336  // INIT / ALLOCATION
337  //========================================================================
338 
340  virtual void initialize();
342  const AllocatorContext & allocatorContext() const;
348  void allocate();
350  virtual void reallocate( int sizeX = 1, int sizeY = 1, int sizeZ = 1,
351  int sizeT = 1, bool keepcontents = false,
352  const AllocatorContext& allocatorContext
353  = AllocatorContext(), bool allocate = true,
354  const std::vector<size_t> *strides = 0 );
355  virtual void reallocate( const Position4Di & size,
356  bool keepcontents = false,
357  const AllocatorContext& allocatorContext
358  = AllocatorContext(), bool allocate = true,
359  const std::vector<size_t> *strides = 0 );
360  virtual void reallocate( const std::vector<int> & size,
361  bool keepcontents = false,
362  const AllocatorContext& allocatorContext
363  = AllocatorContext(), bool allocate = true,
364  const std::vector<size_t> *strides = 0 );
365 
366  //========================================================================
367  // COPY / VIEW
368  //========================================================================
369 
373  Volume<T> copy() const;
374  template <typename OUTP>
378  Volume<T> deepcopy() const;
379  template <typename OUTP>
381 
383  Volume<T> copyStructure() const;
385  template <typename OUTP>
387 
393  void copySubVolume( const Volume<T> & source,
394  const std::vector<int> & pos = std::vector<int>() );
400  void copySubVolume( const rc_ptr<Volume<T> > & source,
401  const std::vector<int> & pos = std::vector<int>() );
402 
404  template <typename OUTP>
405  operator Volume<OUTP>() const;
406 
409  int getLevelsCount() const;
410 
415  int refLevel(const int level) const;
416 
423  rc_ptr<Volume<T> > refVolume() const;
424 
431  rc_ptr<Volume<T> > refVolumeAtLevel(const int level) const;
432 
434  void setRefVolume(const rc_ptr<Volume<T> > & refvol);
436  const Position & posInRefVolume() const;
438  Position posInRefVolumeAtLevel(const int level) const;
439 
441  void setPosInRefVolume(const Position4Di & pos);
442  void setPosInRefVolume(const Position & pos);
443 
444 
456  std::vector<int> getBorders() const;
457 
470  std::vector<size_t> getStrides() const;
471 
472  //========================================================================
473  // BOOLEANS / ACCUMULATED VALUES
474  //========================================================================
477  operator bool() const;
479  bool all() const;
481  bool any() const;
482  T min() const;
483  T max() const;
486  typename DataTypeTraits<T>::LongType sum() const;
487 
488  //========================================================================
489  // FILL / REPLACE
490  //========================================================================
492  void fill( const T & value );
498  void fillBorder( const T & value );
513  Volume<T> & operator= ( const T & value );
514 
515  protected:
516  //========================================================================
517  // PRIVATE UTILS
518  //========================================================================
519  void allocate( int oldSizeX, int oldSizeY, int oldSizeZ, int oldSizeT,
520  bool allocate, const AllocatorContext& allocatorContext,
521  const std::vector<size_t> *strides = 0 );
522  void allocate( const std::vector<int> & oldSize,
523  bool allocate, const AllocatorContext& allocatorContext,
524  const std::vector<size_t> *strides = 0 );
525  void slotSizeChanged( const PropertyFilter& propertyFilter );
526  void updateItemsBuffer();
527 
528  void constructBorders( const Position & bordersize,
529  const AllocatorContext& allocatorContext,
530  bool allocated );
531 
532  AllocatedVector<T> _items;
533  blitz::Array<T, Volume<T>::DIM_MAX> _blitz;
535  std::vector<int> _pos;
536  };
537 
538 //============================================================================
539 // POSITION VECTOR
540 //============================================================================
541  template <typename T>
542  class Volume<T>::Position4Di
543  {
544  public:
545  explicit Position4Di( int x = 0, int y = 0, int z = 0, int t = 0 )
546  : _coords( 4 )
547  {
548  _coords[0] = x;
549  _coords[1] = y;
550  _coords[2] = z;
551  _coords[3] = t;
552  }
553 
558  template <typename U>
559  Position4Di( const U & other ):
560  _coords(4)
561  {
562  for( size_t i = 0; i < size_t( other.size() ) && i < 4; ++i )
563  _coords[i] = other[i];
564  for( size_t i = size_t( other.size() ); i < 4; ++i )
565  _coords[i] = 0;
566  }
567 
568  Position4Di( const Position4Di & pos ) : _coords( pos._coords )
569  {
570  }
571 
573 
574  int & operator [] ( int coord ) { return _coords[ coord ]; }
575  const int & operator [] ( int coord ) const { return _coords[ coord ]; }
576 
577  bool operator==(const Position4Di& p) const
578  {
579  return ( _coords == p._coords );
580  }
581  bool operator!=(const Position4Di& p) const
582  { return !(this->operator ==(p)); }
583 
584  unsigned size() const { return _coords.size(); }
585  const std::vector<int> & toVector() const
586  { return _coords; }
587 
588  /* utility functions, not really related, but which we don't really know
589  where to put them */
590  static std::vector<int> fixed_position( const std::vector<int> & vec )
591  {
592  if( vec.size() >= 4 )
593  return vec;
594  std::vector<int> fixed( vec );
595  fixed.resize( 4 );
596  for( int i=vec.size(); i<4; ++i )
597  fixed[i] = 0;
598  return fixed;
599  }
600 
601  static std::vector<int> fixed_size( const std::vector<int> & vec )
602  {
603  if( vec.size() >= 4 )
604  return vec;
605  std::vector<int> fixed( vec );
606  fixed.resize( 4 );
607  for( int i=vec.size(); i<4; ++i )
608  fixed[i] = 1;
609  return fixed;
610  }
611 
612  static unsigned long long size_num_elements( const std::vector<int> & vec )
613  {
614  int i, n = vec.size();
615  unsigned long long num = 1;
616  for( i=0; i<n; ++i )
617  num *= vec[i];
618  return num;
619  }
620 
621  private:
622  std::vector<int> _coords;
623  };
624 
625 //============================================================================
626 // UTILITIES
627 //============================================================================
628 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
629 
630  template <typename T>
631  class DataTypeCode<Volume<T> >
632  {
633  public:
634  static std::string objectType()
635  { return "CartoVolume"; }
636  static std::string dataType()
637  { return DataTypeCode<T>::dataType(); }
638  static std::string name()
639  {
640  return std::string("carto_volume of ") + DataTypeCode< T >::name();
641  }
642  };
643 
644  template <typename T>
645  class Creator<Volume<T> >
646  {
647  public:
648  static Volume<T>* create( Object, const AllocatorContext &, Object );
649  static void setup( Volume<T> &, Object, const AllocatorContext &, Object );
650  };
651 
652 #endif
653 
654 //============================================================================
655 // STREAM
656 //============================================================================
657 
658 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
659 
660  // Warper for output streams that integrates parameters for volume
661  // printing
662  class VolumeOStream // : public std::ostream // is not really a stream
663  {
664  public:
665  VolumeOStream( std::ostream & ostream );
666  VolumeOStream( const VolumeOStream & other );
668 
669  std::ostream & ostream() const;
670 
671  const size_t & maxT() const;
672  const size_t & maxZ() const;
673  const size_t & maxY() const;
674  const size_t & maxX() const;
675 
676  size_t & maxT();
677  size_t & maxZ();
678  size_t & maxY();
679  size_t & maxX();
680 
681  private:
682  size_t _maxT;
683  size_t _maxZ;
684  size_t _maxY;
685  size_t _maxX;
686  std::ostream & _ostream;
687  };
688 
689  // Object returned by setMaxDim used to set Volume printing parameters
691  {
692  public:
694 
695  const size_t & maxT() const;
696  const size_t & maxZ() const;
697  const size_t & maxY() const;
698  const size_t & maxX() const;
699 
700  size_t & maxT();
701  size_t & maxZ();
702  size_t & maxY();
703  size_t & maxX();
704 
705  private:
706  size_t _maxT;
707  size_t _maxZ;
708  size_t _maxY;
709  size_t _maxX;
710  };
711 
712  VolumeOStream operator<< ( std::ostream & out,
713  const VolumeOStreamSetter & setter );
714 
715  template <typename T>
716  std::ostream & operator<< ( const VolumeOStream & out,
717  const Volume<T> & volume );
718 
719  // Method used to set Volume printing parameters
721  VolumeOStreamSetter setMaxDim( size_t mx, size_t my, size_t mz, size_t mt );
722 
723  #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
724 
725 
742  template <typename T>
743  std::ostream & operator<< ( std::ostream & out,
744  const Volume<T> & volume );
745 
746 
748  template <typename T>
749  void displayRefVolumes(const Volume<T> & vol);
750 
751 } // namespace carto:
752 
753 
754 // the CT macro defined in blitz++ interferes with QColor::CT in Qt
755 # ifdef CT
756 # undef CT
757 # endif
758 
759 #endif // CARTODATA_VOLUME_VOLUMEBASE_H
static std::string objectType()
Definition: volumebase.h:634
static std::string dataType()
Definition: volumebase.h:636
std::string name()
std::string dataType()
const size_t & maxX() const
const size_t & maxZ() const
const size_t & maxY() const
const size_t & maxT() const
VolumeOStream(const VolumeOStream &other)
const size_t & maxT() const
VolumeOStream(std::ostream &ostream)
const size_t & maxY() const
std::ostream & ostream() const
const size_t & maxZ() const
const size_t & maxX() const
VolumeProxy is the base class for volumes.
Definition: volumeproxy.h:50
Position4Di(const Position4Di &pos)
Definition: volumebase.h:568
static std::vector< int > fixed_position(const std::vector< int > &vec)
Definition: volumebase.h:590
bool operator!=(const Position4Di &p) const
Definition: volumebase.h:581
Position4Di(int x=0, int y=0, int z=0, int t=0)
Definition: volumebase.h:545
static unsigned long long size_num_elements(const std::vector< int > &vec)
Definition: volumebase.h:612
Position4Di(const U &other)
Generic constructor from any "vector-like" object, i.e.
Definition: volumebase.h:559
const std::vector< int > & toVector() const
Definition: volumebase.h:585
static std::vector< int > fixed_size(const std::vector< int > &vec)
Definition: volumebase.h:601
unsigned size() const
Definition: volumebase.h:584
bool operator==(const Position4Di &p) const
Definition: volumebase.h:577
N-D Volume main class.
Definition: volumebase.h:119
iterator begin()
Iterators returned here are the most "basic" (and fastest) iterators: they go from the first voxel li...
Definition: volumebase_d.h:819
Volume< T > copyStructure() const
Copy the full data structure without copying the actual data.
std::vector< int > _pos
Definition: volumebase.h:535
std::vector< int > Position
Definition: volumebase.h:128
const AllocatorContext & allocatorContext() const
returns volume's AllocatorContext
Definition: volumebase_d.h:521
rc_ptr< Volume< T > > _refvol
Definition: volumebase.h:534
bool all() const
True if all values compare to true.
Volume< T > deepcopy() const
Copy the full data structure.
void setPosInRefVolume(const Position4Di &pos)
Set position in parent volume.
Definition: volumebase_d.h:677
Volume< T > copy() const
Create a volume of same dimension and copy the data.
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, const std::vector< size_t > *strides=0)
allows resizing and changing allocator
Volume< T > & operator=(const Volume< T > &other)
Definition: volumebase_d.h:739
Volume< OUTP > copyStructure() const
Copy the full data structure without copying the actual data.
blitz::Array< T, Volume< T >::DIM_MAX >::const_iterator const_iterator
Definition: volumebase.h:142
bool any() const
True if at least one value compares to true.
Volume< OUTP > deepcopy() const
virtual void initialize()
Initializes header info.
Definition: volumebase_d.h:855
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:71
const Position & posInRefVolume() const
Get position in parent volume.
Definition: volumebase_d.h:533
void constructBorders(const Position &bordersize, const AllocatorContext &allocatorContext, bool allocated)
Definition: volumebase_d.h:100
std::vector< size_t > getStrides() const
Get strides for the volume.
Definition: volumebase_d.h:726
void allocate()
This function is only useful in the particular context of an unallocated Volume, when the constructor...
void setRefVolume(const rc_ptr< Volume< T > > &refvol)
Set parent volume.
Definition: volumebase_d.h:699
iterator end()
Definition: volumebase_d.h:828
virtual ~Volume()
Definition: volumebase_d.h:512
blitz::Array< T, Volume< T >::DIM_MAX >::iterator iterator
Definition: volumebase.h:141
Position posInRefVolumeAtLevel(const int level) const
Get position relatively to parent volume at specified level.
Definition: volumebase_d.h:585
rc_ptr< Volume< T > > refVolume() const
Get parent volume.
Definition: volumebase_d.h:527
void copySubVolume(const Volume< T > &source, const std::vector< int > &pos=std::vector< int >())
Copy voxels values from another volume.
Definition: volumebase_d.h:779
std::vector< int > getBorders() const
Get borders for the volume.
Definition: volumebase_d.h:707
int refLevel(const int level) const
Transform a level index to a valid level index in the volume hierarchy.
Definition: volumebase_d.h:552
DataTypeTraits< T >::LongType sum() const
To avoid overflow, the biggest possible type (intmax_t, uintmax_t, double...) is used for computation...
Volume< OUTP > copy() const
rc_ptr< Volume< T > > refVolumeAtLevel(const int level) const
Get parent volume at a specified level in volume hierarchy.
Definition: volumebase_d.h:570
void fill(const T &value)
Fills the volume with a given value.
const T & at(long x, long y=0, long z=0, long t=0) const
int getLevelsCount() const
Get levels count in volume hierarchy from the current volume to the topmost volume.
Definition: volumebase_d.h:540
void slotSizeChanged(const PropertyFilter &propertyFilter)
blitz::Array< T, Volume< T >::DIM_MAX > _blitz
Definition: volumebase.h:533
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 ...
void updateItemsBuffer()
Definition: volumebase_d.h:606
AllocatedVector< T > _items
Definition: volumebase.h:532
void fillBorder(const T &value)
Fill border with a constant value.
T datatype
Access to T type.
Definition: volumebase.h:132
std::ostream & operator<<(std::ostream &out, const VoxelValue< T, C > &aa)
void displayRefVolumes(const Volume< T > &vol)
Display information about volumes hierarchy.
VolumeOStreamSetter setMaxDim(size_t m)
carto::Volume< bool > operator==(const carto::Volume< T > &vol, const U &value)
ptrdiff_t BlitzStridesType
Definition: volumebase.h:62