34 #ifndef CARTODATA_VOLUME_VOLUMEUTIL_H 35 #define CARTODATA_VOLUME_VOLUMEUTIL_H 57 namespace volumeutil {
69 template <
typename T,
typename UnaryFunction>
70 Volume<typename UnaryFunction::result_type>
71 apply(
const Volume<T> & vol, UnaryFunction func );
72 template <
typename T,
typename U,
typename BinaryFunction>
73 Volume<typename BinaryFunction::result_type>
74 apply(
const Volume<T> & vol1,
const Volume<U> & vol2, BinaryFunction func );
75 template <
typename T,
typename UnaryFunction>
76 rc_ptr<Volume<typename UnaryFunction::result_type> >
77 apply(
const rc_ptr<Volume<T> > & vol, UnaryFunction func );
78 template <
typename T,
typename U,
typename BinaryFunction>
79 rc_ptr<Volume<typename BinaryFunction::result_type> >
80 apply(
const rc_ptr<Volume<T> > & vol1,
const Volume<U> & vol2, BinaryFunction func );
85 template <
typename T,
typename UnaryFunction>
86 Volume<T> &
selfApply( Volume<T> & vol, UnaryFunction func );
87 template <
typename T,
typename U,
typename BinaryFunction>
88 Volume<T> &
selfApply( Volume<T> & vol1,
const Volume<U> & vol2, BinaryFunction func );
89 template <
typename T,
typename UnaryFunction>
90 rc_ptr<Volume<T> > &
selfApply( rc_ptr<Volume<T> > & vol, UnaryFunction func );
91 template <
typename T,
typename U,
typename BinaryFunction>
92 rc_ptr<Volume<T> > &
selfApply( rc_ptr<Volume<T> > & vol1,
const Volume<U> & vol2, BinaryFunction func );
101 template <
typename T,
typename OUTP,
typename UnaryFunction>
103 applyTowards(
const Volume<T> & vol, Volume<OUTP> & dst, UnaryFunction func );
104 template <
typename T,
typename U,
typename OUTP,
typename BinaryFunction>
106 applyTowards(
const Volume<T> & vol1,
const Volume<U> & vol2, Volume<OUTP> & dst, BinaryFunction func );
122 template <
typename OUTP,
typename T,
typename BinaryFunction>
123 OUTP
accumulate(
const Volume<T> & vol, BinaryFunction func, OUTP initial );
124 template <
typename OUTP,
typename T,
typename BinaryFunction>
125 OUTP
accumulate(
const rc_ptr<Volume<T> > & vol, BinaryFunction func, OUTP initial );
137 template <
typename T>
138 void transfer(
const Volume<T> & src, Volume<T> & dst );
139 template <
typename T>
140 void transfer(
const rc_ptr<Volume<T> > & src, rc_ptr<Volume<T> > & dst );
141 template <
typename OUTP,
typename INP>
142 void transfer(
const Volume<INP> & src, Volume<OUTP> & dst );
143 template <
typename OUTP,
typename INP>
144 void transfer(
const rc_ptr<Volume<INP> > & src, rc_ptr<Volume<OUTP> > & dst );
150 template <
typename T>
151 Volume<T>
deepcopy(
const Volume<T> & src );
152 template <
typename T>
153 rc_ptr<Volume<T> >
deepcopy(
const rc_ptr<Volume<T> > & src );
154 template <
typename OUTP,
typename INP>
155 Volume<OUTP>
deepcopy(
const Volume<INP> & src );
156 template <
typename OUTP,
typename INP>
157 rc_ptr<Volume<OUTP> >
deepcopy(
const rc_ptr<Volume<INP> > & src );
164 template <
typename T>
165 Volume<T>
copy(
const Volume<T> & src );
166 template <
typename T>
167 rc_ptr<Volume<T> >
copy(
const rc_ptr<Volume<T> > & src );
168 template <
typename OUTP,
typename INP>
169 Volume<OUTP>
copy(
const Volume<INP> & src );
170 template <
typename OUTP,
typename INP>
171 rc_ptr<Volume<OUTP> >
copy(
const rc_ptr<Volume<INP> > & src );
176 template <
typename T>
178 template <
typename T>
179 rc_ptr<Volume<T> >
copyStructure(
const rc_ptr<Volume<T> > & src );
180 template <
typename OUTP,
typename INP>
182 template <
typename OUTP,
typename INP>
183 rc_ptr<Volume<OUTP> >
copyStructure(
const rc_ptr<Volume<INP> > & src );
194 template <
typename T>
195 T
min(
const Volume<T> & vol );
196 template <
typename T>
197 T
min(
const rc_ptr<Volume<T> > & vol );
204 template <
typename T>
205 T
max(
const Volume<T> & vol );
206 template <
typename T>
207 T
max(
const rc_ptr<Volume<T> > & vol );
212 template <
typename T>
214 template <
typename OUTP,
typename T>
215 OUTP
sum(
const Volume<T> & vol );
216 template <
typename T>
218 template <
typename OUTP,
typename T>
219 OUTP
sum(
const rc_ptr<Volume<T> > & vol );
224 template <
typename T>
225 bool all(
const Volume<T> & vol );
226 template <
typename T>
227 bool all(
const rc_ptr<Volume<T> > & vol );
232 template <
typename T>
233 bool any(
const Volume<T> & vol );
234 template <
typename T>
235 bool any(
const rc_ptr<Volume<T> > & vol );
249 template <
typename T,
typename U>
250 Volume<bool>
valuesIn(
const Volume<T> & volume,
const U &
set );
251 template <
typename T,
typename U>
252 rc_ptr<Volume<bool> >
valuesIn(
const rc_ptr<Volume<T> > & volume,
const U &
set );
262 template <
typename T,
typename U>
263 Volume<bool>
valuesNotIn(
const Volume<T> & volume,
const U &
set );
264 template <
typename T,
typename U>
265 rc_ptr<Volume<bool> >
valuesNotIn(
const rc_ptr<Volume<T> > & volume,
const U &
set );
273 template <
typename T,
typename U>
274 void conditionalSet( Volume<T> & volume,
const Volume<U> & condition,
const T & value );
275 template <
typename T,
typename U>
276 void conditionalSet( rc_ptr<Volume<T> > & volume,
const rc_ptr<Volume<U> > & condition,
const T & value );
283 template <
typename T>
285 template <
typename T>
286 rc_ptr<Volume<T> >
invertMinMax(
const rc_ptr<Volume<T> > & volume );
301 template <
typename T>
303 const typename Volume<T>::Position4Di & top,
304 const typename Volume<T>::Position4Di & bottom
305 =
typename Volume<T>::Position4Di(-1, -1, -1, -1) );
308 template <
typename T>
310 const typename Volume<T>::Position4Di & top,
311 const typename Volume<T>::Position4Di & bottom
312 =
typename Volume<T>::Position4Di(-1, -1, -1, -1) );
322 template <
typename T>
324 const typename Volume<T>::Position4Di & top,
325 const typename Volume<T>::Position4Di & bottom
326 =
typename Volume<T>::Position4Di(-1, -1, -1, -1) );
329 template <
typename T>
331 const typename Volume<T>::Position4Di & top,
332 const typename Volume<T>::Position4Di & bottom
333 =
typename Volume<T>::Position4Di(-1, -1, -1, -1) );
343 namespace volumeutil {
351 template <
typename T,
typename UnaryFunction>
352 Volume<typename UnaryFunction::result_type>
355 typedef typename UnaryFunction::result_type OUTP;
361 template <
typename T,
typename U,
typename BinaryFunction>
365 typedef typename BinaryFunction::result_type OUTP;
373 template <
typename T,
typename UnaryFunction>
377 typedef typename UnaryFunction::result_type OUTP;
383 template <
typename T,
typename U,
typename BinaryFunction>
387 typedef typename BinaryFunction::result_type OUTP;
395 template <
typename T,
typename UnaryFunction>
402 template <
typename T,
typename U,
typename BinaryFunction>
411 template <
typename T,
typename UnaryFunction>
418 template <
typename T,
typename U,
typename BinaryFunction>
426 template <
typename T,
typename OUTP,
typename UnaryFunction>
430 for(
long t = 0; t < vol.
getSizeT(); ++t )
431 for(
long z = 0; z < vol.
getSizeZ(); ++z )
432 for(
long y = 0; y < vol.
getSizeY(); ++y )
433 for(
long x = 0; x < vol.
getSizeX(); ++x )
434 dst( x, y, z, t ) = func( vol( x, y, z, t ) );
438 template <
typename T,
typename U,
typename OUTP,
typename BinaryFunction>
446 dst( x, y, z, t ) = func( vol1( x, y, z, t ), vol2( x, y, z, t ) );
452 template <
typename OUTP,
typename T,
typename BinaryFunction>
455 for(
long t = 0; t < vol.
getSizeT(); ++t )
456 for(
long z = 0; z < vol.
getSizeZ(); ++z )
457 for(
long y = 0; y < vol.
getSizeY(); ++y )
458 for(
long x = 0; x < vol.
getSizeX(); ++x )
459 initial = func( initial, vol( x, y, z, t ) );
465 template <
typename OUTP,
typename T,
typename BinaryFunction>
468 return accumulate<OUTP, T, BinaryFunction>( *vol, func, initial );
479 template <
typename T>
482 transfer<T,T>( src, dst );
485 template <
typename T>
488 transfer<T,T>( src, dst );
491 template <
typename OUTP,
typename INP>
497 template <
typename OUTP,
typename INP>
505 template <
typename T>
508 return deepcopy<T,T>( src );
511 template <
typename T>
514 return deepcopy<T,T>( src );
517 template <
typename OUTP,
typename INP>
532 srcparent->getSizeX(), srcparent->getSizeY(),
533 srcparent->getSizeZ(), srcparent->getSizeT(),
534 srcparent->allocatorContext(),
535 srcparent->allocatorContext().isAllocated() ) );
536 if( srcparent->allocatorContext().isAllocated() )
538 dst.setRefVolume( dstparent );
545 while( srcchild->refVolume().
get() )
549 srcparent->getSizeX(), srcparent->getSizeY(),
550 srcparent->getSizeZ(), srcparent->getSizeT(),
551 srcparent->allocatorContext(),
552 srcparent->allocatorContext().isAllocated() ) );
553 dstparent->copyHeaderFrom( srcparent->header() );
554 if( srcparent->allocatorContext().isAllocated() )
556 dstchild->setRefVolume( dstparent );
557 dstchild->setPosInRefVolume( srcchild->posInRefVolume() );
558 srcchild = srcparent;
559 dstchild = dstparent;
565 template <
typename OUTP,
typename INP>
569 src->getSizeX(), src->getSizeY(),
570 src->getSizeZ(), src->getSizeT(),
571 src->allocatorContext(),
572 src->allocatorContext().isAllocated() ) );
573 dst->copyHeaderFrom( src->header() );
574 if( src->allocatorContext().isAllocated() )
580 while( srcchild->refVolume().
get() )
584 srcparent->getSizeX(), srcparent->getSizeY(),
585 srcparent->getSizeZ(), srcparent->getSizeT(),
586 srcparent->allocatorContext(),
587 srcparent->allocatorContext().isAllocated() ) );
588 dstparent->copyHeaderFrom( srcparent->header() );
589 if( srcparent->allocatorContext().isAllocated() )
591 dstchild->setRefVolume( dstparent );
592 dstchild->setPosInRefVolume( srcchild->posInRefVolume() );
593 srcchild = srcparent;
594 dstchild = dstparent;
603 template <
typename T>
606 return copy<T,T>( src );
609 template <
typename T>
612 return copy<T,T>( src );
615 template <
typename OUTP,
typename INP>
625 template <
typename OUTP,
typename INP>
629 src->getSizeX(), src->getSizeY(),
630 src->getSizeZ(), src->getSizeT() ) );
631 dst->copyHeaderFrom( src->header() );
639 template <
typename T>
642 return copyStructure<T,T>( src );
645 template <
typename T>
648 return copyStructure<T,T>( src );
651 template <
typename OUTP,
typename INP>
666 srcparent->getSizeX(), srcparent->getSizeY(),
667 srcparent->getSizeZ(), srcparent->getSizeT(),
668 srcparent->allocatorContext(),
669 srcparent->allocatorContext().isAllocated() ) );
672 dst.setRefVolume( dstparent );
678 while( srcchild->refVolume().
get() )
682 srcparent->getSizeX(), srcparent->getSizeY(),
683 srcparent->getSizeZ(), srcparent->getSizeT(),
684 srcparent->allocatorContext(),
685 srcparent->allocatorContext().isAllocated() ) );
686 dstparent->copyHeaderFrom( srcparent->header() );
689 dstchild->setRefVolume( dstparent );
690 dstchild->setPosInRefVolume( srcchild->posInRefVolume() );
691 srcchild = srcparent;
692 dstchild = dstparent;
700 template <
typename OUTP,
typename INP>
707 src->getSizeX(), src->getSizeY(),
708 src->getSizeZ(), src->getSizeT(),
709 src->allocatorContext(),
710 src->allocatorContext().isAllocated() ) );
711 dst->copyHeaderFrom( src->header() );
718 while( srcchild->refVolume().
get() )
722 srcparent->getSizeX(), srcparent->getSizeY(),
723 srcparent->getSizeZ(), srcparent->getSizeT(),
724 srcparent->allocatorContext(),
725 srcparent->allocatorContext().isAllocated() ) );
726 dstparent->copyHeaderFrom( srcparent->header() );
729 dstchild->setRefVolume( dstparent );
730 dstchild->setPosInRefVolume( srcchild->posInRefVolume() );
731 srcchild = srcparent;
732 dstchild = dstparent;
743 template <
typename T>
748 throw std::runtime_error(
"Cannot compute min of an empty volume");
752 template <
typename T>
755 if( !vol.get() || ( vol->getSizeX() == 0 && vol->getSizeY() == 0 &&
756 vol->getSizeZ() == 0 && vol->getSizeT() == 0 ) )
757 throw std::runtime_error(
"Cannot compute min of an empty volume");
761 template <
typename T>
766 throw std::runtime_error(
"Cannot compute max of an empty volume");
770 template <
typename T>
773 if( !vol.get() || ( vol->getSizeX() == 0 && vol->getSizeY() == 0 &&
774 vol->getSizeZ() == 0 && vol->getSizeT() == 0 ) )
775 throw std::runtime_error(
"Cannot compute max of an empty volume");
779 template <
typename T>
782 return sum<typename DataTypeTraits<T>::LongType,T>( vol );
786 template <
typename OUTP,
typename T>
792 template <
typename T>
795 return sum<typename DataTypeTraits<T>::LongType,T>( vol );
798 template <
typename OUTP,
typename T>
802 return static_cast<OUTP>(0);
806 template <
typename T>
812 template <
typename T>
818 template <
typename T>
824 template <
typename T>
835 template <
typename T,
typename U>
836 struct inSet:
public std::binary_function<T, U, bool>
838 bool operator() (
const T & x,
const U & y )
840 for(
typename U::const_iterator k = y.begin(); k != y.end(); ++k )
848 template <
typename T,
typename U>
856 template <
typename T,
typename U>
865 template <
typename T,
typename U>
866 struct notInSet:
public std::binary_function<T, U, bool>
868 bool operator() (
const T & x,
const U & y )
870 for(
typename U::const_iterator k = y.begin(); k != y.end(); ++k )
878 template <
typename T,
typename U>
886 template <
typename T,
typename U>
895 template <
typename T,
typename U>
896 struct changeIf:
public std::binary_function<T, U, T>
899 bool operator() (
const T & x,
const U & y )
901 return y ? _value : x;
908 template <
typename T,
typename U>
914 template <
typename T,
typename U>
921 template <
typename T>
929 T operator() (
const T & x )
939 template <
typename T>
947 template <
typename T>
959 template <
typename T>
968 if( b[0] != top[0] || b[1] != bot[0] ||
969 b[2] != top[1] || b[3] != bot[1] ||
970 b[4] != top[2] || b[5] != bot[2] ||
971 b[6] != top[3] || b[7] != bot[3] )
974 volume.
getSizeX() + top[0] + bot[0],
975 volume.
getSizeY() + top[1] + bot[1],
976 volume.
getSizeZ() + top[2] + bot[2],
977 volume.
getSizeT() + top[3] + bot[3] ) );
978 parent->copyHeaderFrom( volume.
header() );
993 template <
typename T>
1001 std::vector<int> b = volume->getBorders();
1002 if( b[0] != top[0] || b[1] != bot[0] ||
1003 b[2] != top[1] || b[3] != bot[1] ||
1004 b[4] != top[2] || b[5] != bot[2] ||
1005 b[6] != top[3] || b[7] != bot[3] )
1008 volume->getSizeX() + top[0] + bot[0],
1009 volume->getSizeY() + top[1] + bot[1],
1010 volume->getSizeZ() + top[2] + bot[2],
1011 volume->getSizeT() + top[3] + bot[3] ) );
1012 parent->copyHeaderFrom( volume->header() );
1016 volume->getSizeT() );
1019 volume->reallocate( volume->getSizeX(), volume->getSizeY(),
1020 volume->getSizeZ(), volume->getSizeT(),
1021 true, volume->allocatorContext(), false );
1022 volume->setRefVolume( parent );
1023 volume->setPosInRefVolume( top );
1027 template <
typename T>
1036 if( b[0] < top[0] || b[1] < bot[0] ||
1037 b[2] < top[1] || b[3] < bot[1] ||
1038 b[4] < top[2] || b[5] < bot[2] ||
1039 b[6] < top[3] || b[7] < bot[3] )
1042 volume.
getSizeX() + top[0] + bot[0],
1043 volume.
getSizeY() + top[1] + bot[1],
1044 volume.
getSizeZ() + top[2] + bot[2],
1045 volume.
getSizeT() + top[3] + bot[3] ) );
1046 parent->copyHeaderFrom( volume.
header() );
1061 template <
typename T>
1069 std::vector<int> b = volume->getBorders();
1070 if( b[0] < top[0] || b[1] < bot[0] ||
1071 b[2] < top[1] || b[3] < bot[1] ||
1072 b[4] < top[2] || b[5] < bot[2] ||
1073 b[6] < top[3] || b[7] < bot[3] )
1076 volume->getSizeX() + top[0] + bot[0],
1077 volume->getSizeY() + top[1] + bot[1],
1078 volume->getSizeZ() + top[2] + bot[2],
1079 volume->getSizeT() + top[3] + bot[3] ) );
1080 parent->copyHeaderFrom( volume->header() );
1084 volume->getSizeT() );
1087 volume->reallocate( volume->getSizeX(), volume->getSizeY(),
1088 volume->getSizeZ(), volume->getSizeT(),
1089 true, volume->allocatorContext(), false );
1090 volume->setRefVolume( parent );
1091 volume->setPosInRefVolume( top );
1108 template <
typename T,
class BinaryFunction>
1113 : value( x ), f( func ) {}
1114 inline T operator () (
const T & y )
const 1115 {
return f( y, value ); }
1125 template <
typename T,
class BinaryFunction>
1130 : value( x ), f( func ) {}
1131 inline T operator () (
const T & y )
const 1132 {
return f( value, y ); }
1140 template <
typename T,
typename U>
1145 inline T operator () (
const T & x )
const 1146 {
return (T) ( x * scale ); }
1153 return x * (float) scale;
1159 return x * (float) scale;
1165 return x * (double) scale;
1171 return x * (double) scale;
1178 template <
typename T,
typename U>
1183 inline T operator () (
const T & x )
const 1184 {
return (T) ( x / divisor ); }
1191 return x * (float) ( 1. / divisor );
1197 return x * (float) ( 1. / (
double) divisor );
1203 return x * ( 1. / divisor );
1209 return x * (double) ( 1. / divisor );
1215 return x * (double) ( 1. / divisor );
1218 template <
typename T,
bool Scalar>
1223 template <
typename T>
1236 template <
typename T>
1241 template <
typename T>
1246 template <
class UnaryFunction>
static 1249 template <
class BinaryFunction>
static 1254 template <
class UnaryFunction>
static 1258 template <
class BinaryFunction>
static 1271 template <
class BinaryFunction>
static 1274 template <
class BinaryFunction>
static 1280 #endif // CARTODATA_VOLUME_VOLUMEUTIL_H void setRefVolume(const rc_ptr< Volume< T > > &refvol)
Set parent volume.
Volume< T > copyStructure(const Volume< T > &src)
Performs a copy of the view structure without transfering the data.
const Position & posInRefVolume() const
Get position in parent volume.
Volume< T > & selfApply(Volume< T > &vol, UnaryFunction func)
Apply a function to all the elements of a volume (in place version)
const T & at(long x, long y=0, long z=0, long t=0) const
bool any(const Volume< T > &vol)
Returns true if at least one value compares to true.
T operator()(const T &x) const
void conditionalSet(Volume< T > &volume, const Volume< U > &condition, const T &value)
Conditionally fill a volume.
T max(const Volume< T > &vol)
Returns the maximum value of the volume.
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
Returns the sum of the volume values.
Convenient handle for a Volume - this is normally the entry point for all volumes handling...
T operator()(const T &x) const
void setMinBorders(Volume< T > &volume, const typename Volume< T >::Position4Di &top, const typename Volume< T >::Position4Di &bottom=typename Volume< T >::Position4Di(-1, -1, -1, -1))
Set border width.
void setPosInRefVolume(const Position4Di &pos)
Set position in parent volume.
invMinMax(const T &min, const T &max)
const AllocatorContext & allocatorContext() const
returns volume's AllocatorContext
std::complex< float > cfloat
T min(const Volume< T > &vol)
Returns the minimum value of the volume.
virtual void copyHeaderFrom(const PropertySet &other)
copy properties from other to this, avoiding forbidden properties like size.
UnaryFromConstantBinaryFunctor2(const T &x, BinaryFunction func)
std::vector< int > getBorders() const
Get borders for the volume.
void transfer(const Volume< T > &src, Volume< T > &dst)
Transfer data from an allocated volume to another Allocated sizes must be equal (same number of voxel...
Volume utilities classes.
Volume< bool > valuesNotIn(const Volume< T > &volume, const U &set)
Find values not contained in a set.
UnaryFromConstantBinaryFunctor(const T &x, BinaryFunction func)
rc_ptr< Volume< T > > refVolume() const
Get 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)
allows resizing and changing allocator
Volume< T > copy(const Volume< T > &src)
Performs a copy of the data (not only a reference copy) Only the data inside the view is copied...
Volume< T > deepcopy(const Volume< T > &src)
Performs a copy of the data (not only a reference copy) The whole view hierarchy is fully duplicated...
bool all(const Volume< T > &vol)
Returns true if all values compare to true.
void setBorders(Volume< T > &volume, const typename Volume< T >::Position4Di &top, const typename Volume< T >::Position4Di &bottom=typename Volume< T >::Position4Di(-1, -1, -1, -1))
Set border width.
Volume< bool > valuesIn(const Volume< T > &volume, const U &set)
Find values contained in a set.
Volume< OUTP > & applyTowards(const Volume< T > &vol, Volume< OUTP > &dst, UnaryFunction func)
Apply a function to all the elements of a volume (already allocated output version) ...
Volume< typename UnaryFunction::result_type > apply(const Volume< T > &vol, UnaryFunction func)
Used by the actual Volume and VolumeRef operators It allows to keep the loops in one place and to spe...
std::complex< double > cdouble
static T accumulate(BinaryFunction f, const VolumeRef< T > &o2, T initial)
Apply a binary function to each voxel of the volume, with a "current" value as second argument...
Volume< T > invertMinMax(const Volume< T > &volume)
Invert a volume between its min and max values.
OUTP accumulate(const Volume< T > &vol, BinaryFunction func, OUTP initial)
Accumulation over a volume.