20 #ifndef MIRTK_ConstImageIterator_H 21 #define MIRTK_ConstImageIterator_H 23 #include "mirtk/Voxel.h" 24 #include "mirtk/Vector4D.h" 25 #include "mirtk/ImageAttributes.h" 26 #include "mirtk/BaseImage.h" 92 void SetData(
const void *,
int = MIRTK_VOXEL_UNKNOWN);
150 void SetRegion(
int,
int,
int,
int,
int,
int);
180 void SetRegion(
int,
int,
int,
int,
int,
int,
int,
int);
195 void IndexToVoxel(
int,
int &,
int &,
int &,
int &)
const;
210 void PosToVoxel(
int,
int &,
int &,
int &)
const;
213 void PosToVoxel(
int,
int &,
int &,
int &,
int &)
const;
225 int VoxelToPos(
int,
int,
int = 0,
int = 0)
const;
237 void Voxel(
int &,
int &)
const;
240 void Voxel(
int &,
int &,
int &)
const;
243 void Voxel(
int &,
int &,
int &,
int &)
const;
249 void GoToVoxel(
int,
int,
int = -1,
int = -1);
270 operator bool()
const;
287 template <
class VoxelType>
288 const VoxelType *
Current()
const;
297 template <
class VoxelType>
298 const VoxelType *
Current(
int)
const;
303 template <
class VoxelType>
304 const VoxelType *
Next();
313 template <
class VoxelType>
314 const VoxelType *
Next(
int);
319 template <
class VoxelType>
320 const VoxelType &
Value()
const;
329 template <
class VoxelType>
330 const VoxelType &
Value(
int t)
const;
376 template <
class VoxelType>
377 void Move(
const VoxelType *&)
const;
381 template <
class VoxelType>
382 void Move(VoxelType *&)
const;
418 _DataSize (attr._x, attr._y, attr._z, attr._t),
422 _End (attr._x-1, attr._y-1, attr._z-1, attr._t-1),
423 _Size (attr._x, attr._y, attr._z, attr._t),
424 _XYZ (attr._x * attr._y * attr._z),
437 _Data (reinterpret_cast<const char *>(data)),
438 _DataSize (attr._x, attr._y, attr._z, attr._t),
442 _End (attr._x-1, attr._y-1, attr._z-1, attr._t-1),
443 _Size (attr._x, attr._y, attr._z, attr._t),
444 _XYZ (attr._x * attr._y * attr._z),
457 _Data (reinterpret_cast<const char *>(image.GetScalarPointer())),
458 _DataSize (image.GetX(), image.GetY(), image.GetZ(), image.GetT()),
462 _End (image.GetX()-1, image.GetY()-1, image.GetZ()-1, image.GetT()-1),
463 _Size (image.GetX(), image.GetY(), image.GetZ(), image.GetT()),
464 _XYZ (image.GetX() * image.GetY() * image.GetZ()),
477 _Data (reinterpret_cast<const char *>(image->GetScalarPointer())),
478 _DataSize (image->GetX(), image->GetY(), image->GetZ(), image->GetT()),
482 _End (image->GetX()-1, image->GetY()-1, image->GetZ()-1, image->GetT()-1),
483 _Size (image->GetX(), image->GetY(), image->GetZ(), image->GetT()),
484 _XYZ (image->GetX() * image->GetY() * image->GetZ()),
584 const int size = DataTypeSize(type);
586 cerr <<
"ConstImageIterator::SetDataType: Unknown data type: " << type << endl;
598 if (type != MIRTK_VOXEL_UNKNOWN) {
601 _Data =
reinterpret_cast<const char *
>(data);
708 cerr <<
"ConstImageIterator::SetRegion: Region at least partially outside of image domain" << endl;
726 r.cols().end() - r.cols().begin(),
727 r.rows().end() - r.rows().begin());
736 r.cols ().end() - r.cols ().begin(),
737 r.rows ().end() - r.rows ().begin(),
738 r.pages().end() - r.pages().begin());
746 cerr <<
"ConstImageIterator::SetChannel/Component: Channel index out of bounds (c=" << c <<
")" << endl;
752 cerr <<
"ConstImageIterator::SetChannel/Component: Channel range out of bounds (c=" << c <<
", nc=" << nc <<
")" << endl;
761 }
else if (c != 0 || nc != 1) {
762 cerr <<
"ConstImageIterator::SetChannel/Component: Channel index/range out of bounds (c=" << c <<
", nc=" << nc <<
")" << endl;
782 SetFrame(r.begin(), r.end() - r.begin());
790 cerr <<
"ConstImageIterator::SetFrame: Frame index out of bounds (l=" << l <<
")" << endl;
796 cerr <<
"ConstImageIterator::SetFrame: Frame range out of bounds (l=" << l <<
", nl=" << nl <<
")" << endl;
805 }
else if (l != 0 || nl != 1) {
806 cerr <<
"ConstImageIterator::SetFrame: Frame index/range out of bounds (l=" << l <<
", nl=" << nl <<
")" << endl;
814 SetFrame(r.begin(), r.end() - r.begin());
846 SetRegion(i1, j1, k1, i2-i1+1, j2-j1+1, k2-k1+1);
848 cerr <<
"ConstImageIterator::SetNeighborhood: Neighborhood out of bounds" << endl;
875 cerr <<
"ConstImageIterator::SetNeighborhood: Neighborhood out of bounds" << endl;
878 }
else if (l != 0 || rl != 0) {
879 cerr <<
"ConstImageIterator::SetNeighborhood: 4D neighborhood only valid for image sequences (i.e., dt > 0)" << endl;
944 pos = pos % (n *
_Size.
_z) % n;
1054 else _Next =
nullptr;
1119 inline ConstImageIterator::operator bool()
const 1179 template <
class VoxelType>
1182 return reinterpret_cast<const VoxelType *
>(
_Next);
1186 template <
class VoxelType>
1189 return reinterpret_cast<const VoxelType *
>(
_Next + t *
_XYZ);
1193 template <
class VoxelType>
1196 const VoxelType *current =
reinterpret_cast<const VoxelType *
>(
_Next);
1202 template <
class VoxelType>
1205 const VoxelType *current =
reinterpret_cast<const VoxelType *
>(
_Next + t *
_XYZ);
1211 template <
class VoxelType>
1214 return *
reinterpret_cast<const VoxelType *
>(
_Next);
1218 template <
class VoxelType>
1221 return *
reinterpret_cast<const VoxelType *
>(
_Next + t *
_XYZ);
1227 return Value<double>();
1233 return Value<double>(t);
1237 template <
class VoxelType>
1244 template <
class VoxelType>
1253 #endif // MIRTK_ConstImageIterator_H void SetChannel(int, int=1)
Set temporal region (start and size)
void CalculateStride()
Calculate pointer increments.
int NumberOfFrames() const
Get number of frames in image region.
void SetComponent(int, int=1)
Set temporal region (start and size)
int PosToIndex(int) const
Convert iterator position to voxel index.
void GoToIndex(int)
Go to voxel with specified index relative to entire image.
int _XYZ
Number of voxels per frame of the image.
Vector4D< int > _DataSize
Size of the entire image.
virtual ~ConstImageIterator()
Destructor.
void GoToEnd()
Go to end of region.
int LineStride() const
Get stride between rows/lines in number of voxels.
int NumberOfComponents() const
Get number of vector components considered.
void Move(const VoxelType *&) const
Vector4D< int > _End
Last index of the image region.
int _Inc
Previous pointer increment in number of bytes.
int NumberOfVectorComponents() const
int _FrameStride
Increment at end of frame in number of bytes.
int _SliceStride
Increment at end of slice in number of bytes.
int Index() const
Index of voxel at current iterator position.
void GoToBegin()
Go to begin of region.
const VoxelType & Value() const
int SliceStride() const
Get stride between slices in number of voxels.
const char * _Next
Pointer to next image voxel.
void SetDataType(int)
Set data type - defines number of bytes per voxel.
void Initialize()
Initialize iterator, called by first GoTo command.
void PosToVoxel(int, int &, int &) const
Convert iterator position to voxel coordinates.
void SetNeighborhood(int, int, int, int)
Set 2D neighborhood (center and radius)
void GoToVoxel(int, int, int=-1, int=-1)
Go to voxel with specified coordinates relative to entire image.
void SetData(const void *, int=MIRTK_VOXEL_UNKNOWN)
Set raw data pointer to start of entire image.
int ColumnStride() const
Get stride between columns in number of voxels.
Vector4D< int > _Index
Start index of the image region.
bool _IsImageSequence
Whether the image is a sequence.
ConstImageIterator(const ImageAttributes &, int)
Constructor.
int VoxelToIndex(int, int, int=0, int=0) const
Convert voxel coordinates to index.
const char * _Data
Pointer to begin of entire image.
void GoToPos(int)
Go to specified position within image region.
void SetRegion(int, int, int, int)
Set 2D image region (start and size)
int NumberOfImageVoxels() const
Number of voxels in the entire image.
int NumberOfImageChannels() const
int NumberOfSequenceFrames() const
int VoxelToPos(int, int, int=0, int=0) const
Convert voxel cooridnates to iterator position.
Vector4D< int > _Size
Size of image region.
int _LineStride
Increment at end of line in number of bytes.
bool IsAtEnd() const
Whether iterator reached the end of the region and is invalid now.
virtual double ValueAsDouble() const
Get current voxel value casted to double.
void operator--()
Pre-decrement operator.
void SetFrame(int, int=1)
Set temporal region (start and size)
const VoxelType * Current() const
int Pos() const
Current iterator position within image region.
int FrameStride() const
Get stride between frames in number of voxels.
bool IsScalarImage() const
bool IsImageSequence() const
void operator++()
Pre-increment operator.
Vector4D< int > _Begin
First index of the image region.
ConstImageIterator & operator=(const ConstImageIterator &)
Assignment operator.
void GoToCenter()
Go to center of region.
bool IsAtBegin() const
Whether iterator reached the start of the region and is invalid now.
int NumberOfChannels() const
Get number of channels in image region.
void IndexToVoxel(int, int &, int &) const
Convert iterator position to voxel coordinates.
int _ColumnStride
Increment in number of bytes (size of voxel type)
int IndexToPos(int) const
Convert voxel index to iterator position.
int NumberOfVoxels() const
Number of voxels in image region.
void Voxel(int &, int &) const
Coordinates of voxel at current iterator position.