34 #ifndef CARTODATA_VOLUME_VOLUMEBASE_D_H
35 #define CARTODATA_VOLUME_VOLUMEBASE_D_H
70 template <
typename T >
72 const AllocatorContext& allocatorContext,
75 _items( 0U, allocatorContext ),
81 template <
typename T >
83 const AllocatorContext& allocatorContext,
86 size[1] > 0 ? size[1] : 1,
87 size[2] > 0 ? size[2] : 1,
88 size[3] > 0 ? size[3] : 1 ),
89 _items( 0U, allocatorContext ),
99 template <
typename T >
101 const AllocatorContext& allocatorContext,
104 if( !bordersize.empty()
108 size_t bsize =
sizeof(T);
109 std::vector<int> large_size( n );
113 if( i < bordersize.size() )
114 large_size[i] += bordersize[i] * 2;
117 _refvol.reset(
new Volume<T>( large_size,
118 allocatorContext, allocated ) );
120 allocate( -1, -1, -1, -1,
true,
121 _refvol->allocatorContext().isAllocated()
122 ? AllocatorContext( AllocatorStrategy::NotOwner,
124 ( (
char *) &(*_refvol)( bordersize ),
126 : allocatorContext );
127 if( _refvol->allocatorContext().isAllocated() )
130 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
134 for( ; i<Volume<T>::DIM_MAX; ++i )
140 _refvol->_blitz.stride(),
141 blitz::GeneralArrayStorage<8>
142 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
true ) ) );
147 allocate( -1, -1, -1, -1, allocated, allocatorContext );
151 template <
typename T >
154 const AllocatorContext& allocatorContext,
157 _items( 0U, AllocatorContext( AllocatorStrategy::NotOwner,
158 DataSource::
none() ) ),
159 _pos( bordersize.toVector() )
164 template <
typename T >
167 const AllocatorContext& allocatorContext,
170 size[1] > 0 ? size[1] : 1,
171 size[2] > 0 ? size[2] : 1,
172 size[3] > 0 ? size[3] : 1 ),
173 _items( 0U, AllocatorContext( AllocatorStrategy::NotOwner,
174 DataSource::
none() ) ),
175 _pos( bordersize.toVector() )
180 template <
typename T >
182 int sizeT,
int bordersize,
183 const AllocatorContext& allocatorContext,
186 _items( 0U, AllocatorContext( AllocatorStrategy::NotOwner,
187 DataSource::
none() ) ),
190 _pos[0] = bordersize;
191 _pos[1] = bordersize;
192 _pos[2] = bordersize;
195 Position4Di( bordersize, bordersize, bordersize, 0 ).toVector(),
199 template <
typename T >
202 const AllocatorContext& allocatorContext,
205 size[1] > 0 ? size[1] : 1,
206 size[2] > 0 ? size[2] : 1,
207 size[3] > 0 ? size[3] : 1 ),
208 _items( 0U, AllocatorContext( AllocatorStrategy::NotOwner,
209 DataSource::
none() ) ),
212 _pos[0] = bordersize;
213 _pos[1] = bordersize;
214 _pos[2] = bordersize;
216 Position4Di( bordersize, bordersize, bordersize, 0 ).toVector(),
221 template <
typename T >
223 const AllocatorContext& allocatorContext,
226 _items( 0U, allocatorContext ),
233 template <
typename T >
235 const std::vector<int> & bordersize,
236 const AllocatorContext& allocatorContext,
239 _items( 0U, AllocatorContext( AllocatorStrategy::NotOwner,
240 DataSource::
none() ) ),
243 while(
_pos.size() < 4 )
251 template <
typename T >
253 const std::vector<size_t> *strides )
255 _items( sizeX * sizeY * sizeZ * sizeT, buffer ),
262 template <
typename T >
264 const std::vector<size_t> *strides ):
266 size[1] > 0 ? size[1] : 1,
267 size[2] > 0 ? size[2] : 1,
268 size[3] > 0 ? size[3] : 1 ),
269 _items( (long)(size[0] > 0 ? size[0] : 1) *
270 (long)(size[1] > 0 ? size[1] : 1) *
271 (long)(size[2] > 0 ? size[2] : 1) *
272 (long)(size[3] > 0 ? size[3] : 1), buffer ),
279 template <
typename T >
281 const std::vector<size_t> *strides ):
283 _items( (long)
Position4Di::size_num_elements( size ), buffer ),
293 template<
typename T>
inline
296 const AllocatorContext & allocContext )
298 other->allocatorContext().isAllocated() ? other->getSizeX() : 1,
299 size[1] >= 0 ? size[1] :
300 other->allocatorContext().isAllocated() ? other->getSizeY() : 1,
301 size[2] >= 0 ? size[2] :
302 other->allocatorContext().isAllocated() ? other->getSizeZ() : 1,
303 size[3] >= 0 ? size[3] :
304 other->allocatorContext().isAllocated() ? other->getSizeT() : 1 ),
305 _items( 0U, allocContext ),
307 _pos( pos.toVector() )
309 if( other->allocatorContext().isAllocated() )
311 size_t bsize =
sizeof(T);
312 int i, n = size.
size();
317 AllocatorContext( AllocatorStrategy::NotOwner,
319 ( (
char *) &(*other)( pos.
toVector() ),
322 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
326 for( ; i<Volume<T>::DIM_MAX; ++i )
332 other->_blitz.stride(),
334 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
true ) ) );
337 allocate( -1, -1, -1, -1,
true, allocContext );
350 carto::Object vs = other->header().getProperty(
"voxel_size" );
351 size_t n = this->
getSize().size();
356 std::vector<carto::Object> vs2( n );
358 vs2[i] = vs->getArrayItem( i );
371 template<
typename T>
inline
374 const AllocatorContext & allocContext )
376 _items( 0U, allocContext ),
380 if( other->allocatorContext().isAllocated() )
382 size_t bsize =
sizeof(T);
383 int i, n = size.size();
388 AllocatorContext( AllocatorStrategy::NotOwner,
390 ( (
char *) &(*other)( pos ),
394 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
398 for( ; i<Volume<T>::DIM_MAX; ++i )
404 other->_blitz.stride(),
406 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
true ) ) );
409 allocate( -1, -1, -1, -1,
true, allocContext );
422 carto::Object vs = other->header().getProperty(
"voxel_size" );
423 size_t n = this->
getSize().size();
428 std::vector<carto::Object> vs2( n );
430 vs2[i] = vs->getArrayItem( i );
445 template <
typename T >
448 const std::vector<size_t> & strides )
450 _items( (long)
Position4Di::size_num_elements( size ), buffer ),
461 template <
typename T >
465 _items( other._items ),
470 other._blitz.shape(),
471 other._blitz.stride(),
472 blitz::GeneralArrayStorage<8>
473 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ), true ) ),
474 _refvol( other.refVolume().get() ?
475 new
Volume<T>( *other.refVolume() ) : 0 ),
476 _pos( other.posInRefVolume() )
481 std::vector<int> oldSize(1, -1);
483 ? AllocatorContext( AllocatorStrategy::NotOwner,
485 ( (
char *) &(*
_refvol)( pos[0], pos[1], pos[2], pos[3] ),
491 if(
_refvol->allocatorContext().isAllocated() )
494 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
498 for( ; i<Volume<T>::DIM_MAX; ++i )
506 ( blitz::shape( 0, 1, 2, 3, 5, 6, 7, 8 ),
true ) ) );
511 template <
typename T >
520 template <
typename T >
inline
523 return _items.allocatorContext();
526 template <
typename T>
inline
532 template <
typename T>
inline
538 template <
typename T>
550 template <
typename T>
553 int c = getLevelsCount();
559 if ((l < 0) && (l >= c)) {
568 template <
typename T>
571 int l = refLevel(level);
575 while ((c < l) && (!v.
isNull())) {
583 template <
typename T>
586 const int level)
const {
587 int l = refLevel(level);
588 size_t s = this->getSize().size();
592 while ((c < l) && (!v.
isNull())) {
593 const Position & pos = v->posInRefVolume();
595 for (
size_t i = 0; i < s; ++i)
605 template <
typename T>
inline
609 if ( !allocatorContext().isAllocated()
610 || (allocatorContext().accessMode() == AllocatorStrategy::NotOwner) )
620 if( _refvol->allocatorContext().isAllocated() )
622 size_t size =
sizeof(T);
628 AllocatorContext( AllocatorStrategy::NotOwner,
630 ( (
char *) &(*(_refvol))( _pos ),
634 _items.allocate( 0U, allocatorContext() );
636 if ( _refvol->allocatorContext().isAllocated() )
639 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
643 for( ; i<Volume<T>::DIM_MAX; ++i )
649 _refvol->_blitz.stride(),
651 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
true ) ) );
665 carto::Object vs = _refvol->header().getProperty(
"voxel_size" );
666 this->header().setProperty(
"voxel_size", vs );
676 template <
typename T>
inline
687 template <
typename T>
inline
692 while( _pos.size() < 4 )
698 template <
typename T>
inline
700 if (refvol.get() != _refvol.get()) {
706 template <
typename T>
inline
710 if( _refvol.get() && _refvol->allocatorContext().isAllocated() )
715 for( i=0, n=_pos.size(); i<n; ++i )
717 borders[i*2] = _pos[i];
718 borders[i*2+1] -= _pos[i];
725 template <
typename T>
inline
729 const blitz::TinyVector<BlitzStridesType, Volume<T>::DIM_MAX>& bstrides = _blitz.stride();
731 std::vector<size_t> strides( n );
732 for (d = 0; d < n; ++d)
733 strides[d] = bstrides[d];
738 template <
typename T >
753 blitz::TinyVector<long, Volume<T>::DIM_MAX> dims;
757 for( ; i<Volume<T>::DIM_MAX; ++i )
765 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
true ) ) );
778 template <
typename T>
780 const std::vector<int> & pos )
782 std::vector<int> size = this->getSize();
783 std::vector<int> osize = source.
getSize();
786 for( i=0; i<size.size() && i<osize.size(); ++i )
791 size[i] =
std::min( size[i] - ip, osize[i] );
793 for( ; i<size.size(); ++i )
801 for( ; !it.
ended(); ++it, ++oit )
811 template <
typename T>
813 const std::vector<int> & pos )
815 copySubVolume( *source, pos );
818 template <
typename T >
822 return _blitz.begin();
827 template <
typename T >
836 template <
typename T >
840 return _blitz.begin();
845 template <
typename T >
854 template <
typename T >
862 std::set< std::string > sizePropertyNames;
863 sizePropertyNames.insert(
"sizeX" );
864 sizePropertyNames.insert(
"sizeY" );
865 sizePropertyNames.insert(
"sizeZ" );
866 sizePropertyNames.insert(
"sizeT" );
867 sizePropertyNames.insert(
"volume_dimension" );
869 sizeRcPropertyFilter(
new PropertyFilter(
"size", sizePropertyNames ) );
879 template <
typename T >
885 const AllocatorContext& ac,
886 const std::vector<size_t> *strides )
888 std::vector<int> oldSize(4);
889 oldSize[0] = oldSizeX;
890 oldSize[1] = oldSizeY;
891 oldSize[2] = oldSizeZ;
892 oldSize[3] = oldSizeT;
897 template <
typename T >
900 const AllocatorContext& ac,
901 const std::vector<size_t> *nstrides )
906 unsigned long long int stride_max = 0;
907 unsigned long long int total_len = 0;
911 for( ; i<
std::min(nstrides->size(),
size_t(nn)); ++i )
913 strides[i] = (*nstrides)[i];
914 if( strides[i] > stride_max )
915 stride_max = strides[i];
924 if( strides[i] > stride_max )
925 stride_max = strides[i];
929 for( ; i<Volume<T>::DIM_MAX; ++i )
933 if ( total_len *
sizeof(T) >
937 throw std::runtime_error
938 ( std::string(
"attempt to allocate a volume which size is greater "
939 "than allowed by the system (" )
946 if( oldSize[i] != -1 )
953 || !_items.allocatorContext().isAllocated()
959 _items.allocate( (
size_t ) total_len, ac );
962 || &ac != &_items.allocatorContext() )
966 AllocatedVector<T> newItems( (
size_t ) total_len, ac );
975 if( newItems.allocatorContext().allocatorType()
976 != AllocatorStrategy::ReadOnlyMap )
977 for ( t = 0; t <
minSize[3]; t++ )
980 for ( z = 0; z <
minSize[2]; z++ )
983 for ( y = 0; y <
minSize[1]; y++ )
986 for ( x = 0; x <
minSize[0]; x++ )
989 newItems[ x * strides[0] +
991 z * ( size_t ) strides[2] +
992 t * (
size_t ) strides[3] ] =
995 z * ( size_t ) oldSize[0]
996 * (
size_t ) oldSize[1] +
997 t * ( size_t ) oldSize[0] *
998 (
size_t ) oldSize[1]
999 * ( size_t ) oldSize[2] ];
1014 blitz::TinyVector<BlitzStridesType, Volume<T>::DIM_MAX> bstrides;
1017 for( i=0; i<nn; ++i )
1018 bstrides[i] = strides[i];
1020 for( ; i<Volume<T>::DIM_MAX; ++i )
1026 bstrides[i] = ( i == 0 ? n : n * stride_max );
1027 if( strides[i] > stride_max )
1028 stride_max = bstrides[i];
1041 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
1042 for( i=0; i<nn; ++i )
1044 for( ; i<Volume<T>::DIM_MAX; ++i )
1052 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
1059 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ),
1075 blitz::TinyVector<int, Volume<T>::DIM_MAX> dims;
1076 for( i=0; i<nn; ++i )
1078 for( ; i<Volume<T>::DIM_MAX; ++i )
1087 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ), true ) ) );
1094 ( blitz::shape( 0, 1, 2, 3, 4, 5, 6, 7 ), true ) ) );
1100 template <
typename T >
1103 if( !allocatorContext().isAllocated() )
1105 std::vector<size_t> strides = getStrides();
1112 template <
typename T >
1126 propertyFilter.
getOldValue(
"sizeX" )->GenericObject::value<
int >();
1133 propertyFilter.
getOldValue(
"sizeY" )->GenericObject::value<
int >();
1140 propertyFilter.
getOldValue(
"sizeZ" )->GenericObject::value<
int >();
1147 propertyFilter.
getOldValue(
"sizeT" )->GenericObject::value<
int >();
1159 _items.allocatorContext().isAllocated(), allocatorContext() );
1164 template <
typename T >
1170 const AllocatorContext & ac,
1172 const std::vector<size_t> *strides )
1183 if( keepcontents || ( sizeX == oldx && sizeY == oldy && sizeZ == oldz
1184 && sizeT == oldt ) )
1185 allocate( oldx, oldy, oldz, oldt, alloc, ac, strides );
1187 allocate( std::vector<int>(1, -1), alloc, ac, strides );
1191 template <
typename T >
1194 const AllocatorContext & ac,
1196 const std::vector<size_t> *strides )
1198 return reallocate( size[0] > 0 ? size[0] : 1,
1199 size[1] > 0 ? size[1] : 1,
1200 size[2] > 0 ? size[2] : 1,
1201 size[3] > 0 ? size[3] : 1,
1202 keepcontents, ac, alloc, strides );
1205 template <
typename T >
1208 const AllocatorContext & ac,
1210 const std::vector<size_t> *strides )
1227 if( keepcontents || size == old )
1228 allocate( old, alloc, ac, strides );
1230 allocate( std::vector<int>(1, -1), alloc, ac, strides );
1238 template <
typename T>
1240 const AllocatorContext & context,
1243 std::vector<int> dims( 4, 1 );
1244 bool unalloc =
false;
1245 if( !header->getProperty(
"volume_dimension", dims ) )
1247 header->getProperty(
"sizeX", dims[0] );
1248 header->getProperty(
"sizeY", dims[1] );
1249 header->getProperty(
"sizeZ", dims[2] );
1250 header->getProperty(
"sizeT", dims[3] );
1252 options->getProperty(
"unallocated", unalloc );
1253 std::vector<int> borders( 3, 0 );
1255 borders[0] = (int) rint( options->getProperty(
"border" )->getScalar() );
1256 borders[1] = (int) rint( options->getProperty(
"border" )->getScalar() );
1257 borders[2] = (int) rint( options->getProperty(
"border" )->getScalar() );
1260 borders[0] = (int) rint( options->getProperty(
"bx" )->getScalar() );
1263 borders[1] = (int) rint( options->getProperty(
"by" )->getScalar() );
1266 borders[2] = (int) rint( options->getProperty(
"bz" )->getScalar() );
1270 if( borders[0] != 0 || borders[1] != 0 || borders[2] != 0 )
1272 std::vector<int> big_dims = dims;
1273 big_dims[0] += borders[0] * 2;
1274 big_dims[1] += borders[1] * 2;
1275 big_dims[2] += borders[2] * 2;
1276 obj =
new Volume<T>( big_dims, context, !unalloc );
1280 obj =
new Volume<T>( dims, context, !unalloc );
1288 template <
typename T>
1290 const AllocatorContext & context,
1293 std::vector<int> dims( 4, 1 );
1294 bool unalloc =
false, partial =
false, keep_allocation =
false;
1297 carto::Object p = options->getProperty(
"partial_reading" );
1298 partial = bool( p->getScalar() );
1303 if( !header->getProperty(
"volume_dimension", dims ) )
1305 header->getProperty(
"sizeX", dims[0] );
1306 header->getProperty(
"sizeY", dims[1] );
1307 header->getProperty(
"sizeZ", dims[2] );
1308 header->getProperty(
"sizeT", dims[3] );
1313 unalloc = bool( p->getScalar() );
1318 carto::Object p = options->getProperty(
"keep_allocation" );
1319 keep_allocation = bool( p->getScalar() );
1324 != AllocatorStrategy::Unallocated )
1325 obj.
reallocate( dims,
false, context, !unalloc );
1330 ( context.dataSource() );
virtual void copyProperties(Object source)
Object getOldValue(const std::string &propertyName) const
bool hasOldValue(const std::string &propertyName) const
void setProperty(const std::string &, const T &)
void changeBuiltinProperty(const std::string &, T &)
VolumeProxy is the base class for volumes.
VolumeProxy< T > & operator=(const VolumeProxy< T > &other)
std::vector< int > getSize() const
get the 4 dimensions in a vector
const std::vector< int > & toVector() const
iterator begin()
Iterators returned here are the most "basic" (and fastest) iterators: they go from the first voxel li...
std::vector< int > Position
const AllocatorContext & allocatorContext() const
returns volume's AllocatorContext
rc_ptr< Volume< T > > _refvol
void setPosInRefVolume(const Position4Di &pos)
Set position in parent volume.
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)
blitz::Array< T, Volume< T >::DIM_MAX >::const_iterator const_iterator
virtual void initialize()
Initializes header info.
Volume(int sizeX=1, int sizeY=1, int sizeZ=1, int sizeT=1, const AllocatorContext &allocatorContext=AllocatorContext(), bool allocated=true)
Volume construction and allocation.
const Position & posInRefVolume() const
Get position in parent volume.
void constructBorders(const Position &bordersize, const AllocatorContext &allocatorContext, bool allocated)
std::vector< size_t > getStrides() const
Get strides for the volume.
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.
blitz::Array< T, Volume< T >::DIM_MAX >::iterator iterator
Position posInRefVolumeAtLevel(const int level) const
Get position relatively to parent volume at specified level.
rc_ptr< Volume< T > > refVolume() const
Get parent volume.
void copySubVolume(const Volume< T > &source, const std::vector< int > &pos=std::vector< int >())
Copy voxels values from another volume.
std::vector< int > getBorders() const
Get borders for the volume.
int refLevel(const int level) const
Transform a level index to a valid level index in the volume hierarchy.
rc_ptr< Volume< T > > refVolumeAtLevel(const int level) const
Get parent volume at a specified level in volume hierarchy.
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.
void slotSizeChanged(const PropertyFilter &propertyFilter)
blitz::Array< T, Volume< T >::DIM_MAX > _blitz
AllocatedVector< T > _items
void inc_line_ptr(const T *&p) const
void inc_line_ptr(T *&p) const
std::vector< int > minSize(const std::vector< int > &s1, const std::vector< int > &s2)
return the smallest dimensions between s1 and s2
std::string toString(const T &object)
T min(const Volume< T > &vol)
Returns the minimum value of the volume.