| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageConstIteratorWithIndex.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:36 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkImageConstIteratorWithIndex_h |
| 18 |
|
#define __itkImageConstIteratorWithIndex_h |
| 19 |
|
|
| 20 |
|
#include "itkIndex.h" |
| 21 |
|
#include "itkImage.h" |
| 22 |
|
#include <memory> |
| 23 |
|
|
| 24 |
|
namespace itk |
| 25 |
|
{ |
| 26 |
|
|
| 27 |
|
/** \class ImageConstIteratorWithIndex |
| 28 |
|
* \brief A base class for multi-dimensional iterators templated over image |
| 29 |
|
* type that are designed to efficiently keep track of the iterator |
| 30 |
|
* position. |
| 31 |
|
* |
| 32 |
|
* ImageConstIteratorWithIndex is a templated class to represent a |
| 33 |
|
* multi-dimensional iterator. ImageConstIteratorWithIndex is templated |
| 34 |
|
* over the dimension of the image and the data type of the image. |
| 35 |
|
* |
| 36 |
|
* ImageConstIteratorWithIndex is a base class for the "WithIndex" family of |
| 37 |
|
* ITK image iterators, which are designed to effeciently keep track of the |
| 38 |
|
* image index position of the iterator during increment and decrement |
| 39 |
|
* operations. This iterator is a base class and provides only the basic |
| 40 |
|
* construction and comparison operations. It does not provide mechanisms for |
| 41 |
|
* moving the iterator. A subclass of ImageConstIteratorWithIndex must be used |
| 42 |
|
* to move the iterator. |
| 43 |
|
* |
| 44 |
|
* ImageConstIteratorWithIndex is a multi-dimensional iterator, requiring |
| 45 |
|
* more information be specified before the iterator can be used than |
| 46 |
|
* conventional iterators. Whereas the std::vector::iterator from the |
| 47 |
|
* STL only needs to be passed a pointer to establish the iterator, |
| 48 |
|
* the multi-dimensional image iterator needs a pointer, the size of |
| 49 |
|
* the buffer, the size of the region, the start index of the buffer, |
| 50 |
|
* and the start index of the region. To gain access to this |
| 51 |
|
* information, ImageConstIteratorWithIndex holds a reference to the image |
| 52 |
|
* over which it is traversing. |
| 53 |
|
* |
| 54 |
|
* ImageConstIteratorWithIndex assumes a particular layout of the image |
| 55 |
|
* data. In particular, the data is arranged in a 1D array as if it |
| 56 |
|
* were [][][][slice][row][col] with Index[0] = col, Index[1] = row, |
| 57 |
|
* Index[2] = slice, etc. |
| 58 |
|
* |
| 59 |
|
* \par MORE INFORMATION |
| 60 |
|
* For a complete description of the ITK Image Iterators and their API, please |
| 61 |
|
* see the Iterators chapter in the ITK Software Guide. The ITK Software Guide |
| 62 |
|
* is available in print and as a free .pdf download from http://www.itk.org. |
| 63 |
|
* |
| 64 |
|
* \ingroup ImageIterators |
| 65 |
|
* |
| 66 |
|
* \sa ImageConstIterator \sa ConditionalConstIterator |
| 67 |
|
* \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator |
| 68 |
|
* \sa ConstSliceIterator \sa CorrespondenceDataStructureIterator |
| 69 |
|
* \sa FloodFilledFunctionConditionalConstIterator |
| 70 |
|
* \sa FloodFilledImageFunctionConditionalConstIterator |
| 71 |
|
* \sa FloodFilledImageFunctionConditionalIterator |
| 72 |
|
* \sa FloodFilledSpatialFunctionConditionalConstIterator |
| 73 |
|
* \sa FloodFilledSpatialFunctionConditionalIterator |
| 74 |
|
* \sa ImageConstIterator \sa ImageConstIteratorWithIndex |
| 75 |
|
* \sa ImageIterator \sa ImageIteratorWithIndex |
| 76 |
|
* \sa ImageLinearConstIteratorWithIndex \sa ImageLinearIteratorWithIndex |
| 77 |
|
* \sa ImageRandomConstIteratorWithIndex \sa ImageRandomIteratorWithIndex |
| 78 |
|
* \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex |
| 79 |
|
* \sa ImageRegionExclusionConstIteratorWithIndex |
| 80 |
|
* \sa ImageRegionExclusionIteratorWithIndex |
| 81 |
|
* \sa ImageRegionIterator \sa ImageRegionIteratorWithIndex |
| 82 |
|
* \sa ImageRegionReverseConstIterator \sa ImageRegionReverseIterator |
| 83 |
|
* \sa ImageReverseConstIterator \sa ImageReverseIterator |
| 84 |
|
* \sa ImageSliceConstIteratorWithIndex \sa ImageSliceIteratorWithIndex |
| 85 |
|
* \sa NeighborhoodIterator \sa PathConstIterator \sa PathIterator |
| 86 |
|
* \sa ShapedNeighborhoodIterator \sa SliceIterator |
| 87 |
|
* |
| 88 |
|
* \ingroup ImageIterators |
| 89 |
|
*/ |
| 90 |
|
template<typename TImage> |
| 91 |
|
class ITK_EXPORT ImageConstIteratorWithIndex |
| 92 |
|
{ |
| 93 |
|
public: |
| 94 |
|
/** Standard class typedefs. */ |
| 95 |
|
typedef ImageConstIteratorWithIndex Self; |
| 96 |
|
|
| 97 |
|
/** Dimension of the image the iterator walks. This constant is needed so |
| 98 |
|
* functions that are templated over image iterator type (as opposed to |
| 99 |
|
* being templated over pixel type and dimension) can have compile time |
| 100 |
|
* access to the dimension of the image that the iterator walks. */ |
| 101 |
|
itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension); |
| 102 |
|
|
| 103 |
|
/** Index typedef support. */ |
| 104 |
|
typedef typename TImage::IndexType IndexType; |
| 105 |
|
typedef typename IndexType::IndexValueType IndexValueType; |
| 106 |
|
|
| 107 |
|
/** Size typedef support. */ |
| 108 |
|
typedef typename TImage::SizeType SizeType; |
| 109 |
|
typedef typename SizeType::SizeValueType SizeValueType; |
| 110 |
|
|
| 111 |
|
/** Region typedef support. */ |
| 112 |
|
typedef typename TImage::RegionType RegionType; |
| 113 |
|
|
| 114 |
|
/** Image typedef support. */ |
| 115 |
|
typedef TImage ImageType; |
| 116 |
|
|
| 117 |
|
/** PixelContainer typedef support. Used to refer to the container for |
| 118 |
|
* the pixel data. While this was already typdef'ed in the superclass |
| 119 |
LEN |
* it needs to be redone here for this subclass to compile properly with gcc. */ |
| 120 |
|
typedef typename TImage::PixelContainer PixelContainer; |
| 121 |
TDA |
typedef typename PixelContainer::Pointer PixelContainerPointer; |
| 122 |
|
|
| 123 |
|
/** Internal Pixel Type */ |
| 124 |
|
typedef typename TImage::InternalPixelType InternalPixelType; |
| 125 |
|
|
| 126 |
|
/** External Pixel Type */ |
| 127 |
|
typedef typename TImage::PixelType PixelType; |
| 128 |
|
|
| 129 |
|
/** Accessor type that convert data between internal and external |
| 130 |
|
* representations. */ |
| 131 |
|
typedef typename TImage::AccessorType AccessorType; |
| 132 |
|
typedef typename TImage::AccessorFunctorType AccessorFunctorType; |
| 133 |
|
|
| 134 |
|
/** Type of the Offset taken from the image */ |
| 135 |
|
typedef typename TImage::OffsetType OffsetType; |
| 136 |
|
typedef typename OffsetType::OffsetValueType OffsetValueType; |
| 137 |
|
|
| 138 |
|
/** Default Constructor. Need to provide a default constructor since we |
| 139 |
|
* provide a copy constructor. */ |
| 140 |
|
ImageConstIteratorWithIndex(); |
| 141 |
|
|
| 142 |
|
/** Copy Constructor. The copy constructor is provided to make sure the |
| 143 |
|
* handle to the image is properly reference counted. */ |
| 144 |
|
ImageConstIteratorWithIndex(const Self& it); |
| 145 |
|
|
| 146 |
|
/** Constructor establishes an iterator to walk a particular image and a |
| 147 |
|
* particular region of that image. */ |
| 148 |
|
ImageConstIteratorWithIndex( const TImage *ptr, |
| 149 |
|
const RegionType& region ); |
| 150 |
|
|
| 151 |
|
/** operator= is provided to make sure the handle to the image is properly |
| 152 |
|
* reference counted. */ |
| 153 |
|
Self &operator=(const Self& it); |
| 154 |
|
|
| 155 |
|
/** Get the dimension (size) of the index. */ |
| 156 |
|
static unsigned int GetImageDimension() |
| 157 |
|
{return ImageDimension;} |
| 158 |
|
|
| 159 |
|
/** Comparison operator. Two iterators are the same if they "point to" the |
| 160 |
|
* same memory location */ |
| 161 |
|
bool |
| 162 |
|
operator!=(const Self &it) const |
| 163 |
|
{ |
| 164 |
|
// two iterators are the same if they "point to" the same memory location |
| 165 |
|
return (m_Position) != (it.m_Position); |
| 166 |
|
}; |
| 167 |
|
|
| 168 |
|
/** Comparison operator. Two iterators are the same if they "point to" the |
| 169 |
|
* same memory location */ |
| 170 |
|
bool |
| 171 |
|
operator==(const Self &it) const |
| 172 |
|
{ |
| 173 |
|
// two iterators are the same if they "point to" the same memory location |
| 174 |
|
return (m_Position) == (it.m_Position); |
| 175 |
|
}; |
| 176 |
|
|
| 177 |
|
/** Comparison operator. An iterator is "less than" another if it "points to" |
| 178 |
|
* a lower memory location. */ |
| 179 |
|
bool |
| 180 |
|
operator<=(const Self &it) const |
| 181 |
|
{ |
| 182 |
|
// an iterator is "less than" another if it "points to" a lower |
| 183 |
|
// memory location |
| 184 |
|
return (m_Position) <= (it.m_Position); |
| 185 |
|
}; |
| 186 |
|
|
| 187 |
|
/** Comparison operator. An iterator is "less than" another if it "points to" |
| 188 |
|
* a lower memory location. */ |
| 189 |
|
bool |
| 190 |
|
operator<(const Self &it) const |
| 191 |
|
{ |
| 192 |
|
// an iterator is "less than" another if it "points to" a lower |
| 193 |
|
// memory location |
| 194 |
|
return (m_Position) < (it.m_Position); |
| 195 |
|
}; |
| 196 |
|
|
| 197 |
|
/** Comparison operator. An iterator is "greater than" another if it |
| 198 |
|
* "points to" a higher location. */ |
| 199 |
|
bool |
| 200 |
|
operator>=(const Self &it) const |
| 201 |
|
{ |
| 202 |
|
// an iterator is "greater than" another if it "points to" a higher |
| 203 |
|
// memory location |
| 204 |
|
return (m_Position) >= (it.m_Position); |
| 205 |
|
}; |
| 206 |
|
|
| 207 |
|
/** Comparison operator. An iterator is "greater than" another if it |
| 208 |
|
* "points to" a higher location. */ |
| 209 |
|
bool |
| 210 |
|
operator>(const Self &it) const |
| 211 |
|
{ |
| 212 |
|
// an iterator is "greater than" another if it "points to" a higher |
| 213 |
|
// memory location |
| 214 |
|
return (m_Position) > (it.m_Position); |
| 215 |
|
}; |
| 216 |
|
|
| 217 |
|
/** Get the index. This provides a read only reference to the index. |
| 218 |
|
* \sa SetIndex */ |
| 219 |
|
const IndexType & GetIndex() const |
| 220 |
|
{ return m_PositionIndex; } |
| 221 |
|
|
| 222 |
|
/** Get the region that this iterator walks. ImageIterators know the |
| 223 |
|
* beginning and the end of the region of the image to iterate over. */ |
| 224 |
|
const RegionType& GetRegion() const |
| 225 |
|
{ return m_Region; }; |
| 226 |
|
|
| 227 |
|
/** Set the index. No bounds checking is performed. |
| 228 |
|
* \sa GetIndex */ |
| 229 |
|
void SetIndex(const IndexType &ind) |
| 230 |
IND |
**{ m_Position = m_Image->GetBufferPointer() + m_Image->ComputeOffset( ind ); |
| 231 |
IND |
******m_PositionIndex = ind; } |
| 232 |
|
|
| 233 |
|
/** Get the pixel value */ |
| 234 |
|
PixelType Get(void) const |
| 235 |
|
{ return m_PixelAccessorFunctor.Get(*m_Position); } |
| 236 |
|
|
| 237 |
|
/** Return a const reference to the pixel |
| 238 |
|
* This method will provide the fastest access to pixel |
| 239 |
|
* data, but it will NOT support ImageAdaptors. */ |
| 240 |
|
const PixelType & Value(void) const |
| 241 |
|
{ return *m_Position; } |
| 242 |
|
|
| 243 |
|
/** Move an iterator to the beginning of the region. |
| 244 |
|
* \deprecated Use GoToBegin() instead */ |
| 245 |
|
Self Begin(void) const; |
| 246 |
|
|
| 247 |
|
/** Move an iterator to the beginning of the region. */ |
| 248 |
|
void GoToBegin(void); |
| 249 |
|
|
| 250 |
|
/** Move an iterator to the End of the region. |
| 251 |
|
* \deprecated Use GoToReverseBegin() instead */ |
| 252 |
|
Self End(void) const; |
| 253 |
|
|
| 254 |
|
/** Move an iterator to the End of the region. */ |
| 255 |
|
void GoToReverseBegin(void); |
| 256 |
|
|
| 257 |
|
/** Is the iterator at the beginning of the region? */ |
| 258 |
|
bool IsAtReverseEnd(void) const |
| 259 |
|
{ |
| 260 |
IND |
******return !m_Remaining; |
| 261 |
|
} |
| 262 |
|
|
| 263 |
|
/** Is the iterator at the end of the region? */ |
| 264 |
|
bool IsAtEnd(void) const |
| 265 |
|
{ |
| 266 |
IND |
******return !m_Remaining; |
| 267 |
|
} |
| 268 |
|
|
| 269 |
IND |
*/** Are there data remainning in the region ? */ |
| 270 |
|
bool Remaining() |
| 271 |
|
{ |
| 272 |
|
return m_Remaining; |
| 273 |
|
} |
| 274 |
|
|
| 275 |
|
protected: //made protected so other iterators can access |
| 276 |
|
typename TImage::ConstWeakPointer m_Image; |
| 277 |
|
|
| 278 |
|
IndexType m_PositionIndex; // Index where we currently are |
| 279 |
|
IndexType m_BeginIndex; // Index to start iterating over |
| 280 |
|
IndexType m_EndIndex; // Index to finish iterating: |
| 281 |
IND |
************************************// one pixel past the end of each |
| 282 |
IND |
************************************// row, col, slice, etc.... |
| 283 |
|
|
| 284 |
|
RegionType m_Region; // region to iterate over |
| 285 |
|
|
| 286 |
|
unsigned long m_OffsetTable[ ImageDimension + 1 ]; |
| 287 |
|
|
| 288 |
|
const InternalPixelType *m_Position; |
| 289 |
|
const InternalPixelType *m_Begin; |
| 290 |
|
const InternalPixelType *m_End; |
| 291 |
|
|
| 292 |
|
bool m_Remaining; |
| 293 |
|
|
| 294 |
|
AccessorType m_PixelAccessor; |
| 295 |
|
AccessorFunctorType m_PixelAccessorFunctor; |
| 296 |
|
|
| 297 |
|
}; |
| 298 |
|
|
| 299 |
|
} // end namespace itk |
| 300 |
|
|
| 301 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 302 |
|
#include "itkImageConstIteratorWithIndex.txx" |
| 303 |
|
#endif |
| 304 |
|
|
| 305 |
|
#endif |
| 306 |
|
|