| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkLineConstIterator.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:41 $ |
| 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 __itkLineConstIterator_h |
| 18 |
|
#define __itkLineConstIterator_h |
| 19 |
|
|
| 20 |
|
#include "itkIndex.h" |
| 21 |
|
#include "itkImage.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
/** |
| 27 |
|
* \class LineConstIterator |
| 28 |
|
* \brief Iterator that walks a Bresenham line through an ND image. |
| 29 |
|
* |
| 30 |
|
* LineConstIterator is an iterator that walks a Bresenham line |
| 31 |
|
* through an image. The iterator is constructed similar to other |
| 32 |
|
* image iterators except for instead of specifying a region to |
| 33 |
|
* traverse, you specify two indices. The interval specified by |
| 34 |
|
* the two indices is closed. So, a line iterator specified with |
| 35 |
|
* the same start and end index will visit exactly one pixel. |
| 36 |
|
* |
| 37 |
|
* \code |
| 38 |
|
* LineConstIterator<ImageType> it(image, I1, I2); |
| 39 |
|
* while (!it.IsAtEnd()) |
| 40 |
|
* { |
| 41 |
IND |
** // visits at least 1 pixel |
| 42 |
IND |
** } |
| 43 |
|
* \endcode |
| 44 |
|
* |
| 45 |
|
* This class was contributed by Benjamin King, Experimentelle |
| 46 |
|
* Radiologie, Medizinische Hochschule Hannover. |
| 47 |
|
* |
| 48 |
|
*/ |
| 49 |
|
template<class TImage> |
| 50 |
|
class ITK_EXPORT LineConstIterator |
| 51 |
|
{ |
| 52 |
|
public: |
| 53 |
|
/** Standard class typedefs. */ |
| 54 |
|
typedef LineConstIterator Self; |
| 55 |
|
|
| 56 |
|
/** Dimension of the image the iterator walks. This constant is needed so |
| 57 |
|
* that functions that are templated over image iterator type (as opposed to |
| 58 |
|
* being templated over pixel type and dimension) can have compile time |
| 59 |
|
* access to the dimension of the image that the iterator walks. */ |
| 60 |
|
itkStaticConstMacro(ImageIteratorDimension, unsigned int, |
| 61 |
|
TImage::ImageDimension); |
| 62 |
|
|
| 63 |
|
/** Index typedef support. */ |
| 64 |
|
typedef typename TImage::IndexType IndexType; |
| 65 |
|
typedef typename TImage::IndexValueType IndexValueType; |
| 66 |
|
|
| 67 |
|
/** Offset typedef support. */ |
| 68 |
|
typedef typename TImage::OffsetType OffsetType; |
| 69 |
|
typedef typename TImage::OffsetValueType OffsetValueType; |
| 70 |
|
|
| 71 |
|
/** Size typedef support. */ |
| 72 |
|
typedef typename TImage::SizeType SizeType; |
| 73 |
|
typedef typename TImage::SizeValueType SizeValueType; |
| 74 |
|
|
| 75 |
|
/** Region typedef support */ |
| 76 |
|
typedef typename TImage::RegionType RegionType; |
| 77 |
|
|
| 78 |
|
/** Spacing typedef support */ |
| 79 |
|
typedef typename TImage::SpacingType SpacingType; |
| 80 |
|
|
| 81 |
|
/** Origin typedef support */ |
| 82 |
|
typedef typename TImage::PointType PointType; |
| 83 |
|
|
| 84 |
|
/** Image typedef support. */ |
| 85 |
|
typedef TImage ImageType; |
| 86 |
|
|
| 87 |
|
/** PixelContainer typedef support. Used to refer to the container for |
| 88 |
|
* the pixel data. While this was already typdef'ed in the superclass |
| 89 |
LEN |
* it needs to be redone here for this subclass to compile properly with gcc. */ |
| 90 |
|
typedef typename TImage::PixelContainer PixelContainer; |
| 91 |
|
typedef typename PixelContainer::Pointer PixelContainerPointer; |
| 92 |
|
|
| 93 |
|
/** Internal Pixel Type */ |
| 94 |
|
typedef typename TImage::InternalPixelType InternalPixelType; |
| 95 |
|
|
| 96 |
|
/** External Pixel Type */ |
| 97 |
|
typedef typename TImage::PixelType PixelType; |
| 98 |
|
|
| 99 |
|
/** Accessor type that convert data between internal and external |
| 100 |
|
* representations. */ |
| 101 |
|
typedef typename TImage::AccessorType AccessorType; |
| 102 |
|
|
| 103 |
|
/** Run-time type information (and related methods). */ |
| 104 |
|
itkTypeMacro(LineConstIterator, None); |
| 105 |
|
|
| 106 |
|
/** Get the dimension (size) of the index. */ |
| 107 |
|
static unsigned int GetImageIteratorDimension() |
| 108 |
|
{ |
| 109 |
|
return TImage::ImageDimension; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
/** Get the index. This provides a read only reference to the index. */ |
| 113 |
|
const IndexType GetIndex() |
| 114 |
|
{ |
| 115 |
|
return m_CurrentImageIndex; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
/** Get the pixel value */ |
| 119 |
|
const PixelType & Get(void) const |
| 120 |
|
{ |
| 121 |
|
return m_Image->GetPixel(m_CurrentImageIndex ); |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
/** Is the iterator at the end of the line? */ |
| 125 |
|
bool IsAtEnd() |
| 126 |
|
{ |
| 127 |
|
return m_IsAtEnd; |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
/** Move an iterator to the beginning of the line. */ |
| 131 |
|
void GoToBegin(); |
| 132 |
|
|
| 133 |
|
/** Walk forward along the line to the next index in the image. */ |
| 134 |
|
void operator++(); |
| 135 |
|
|
| 136 |
|
/** operator= is provided to make sure the handle to the image is properly |
| 137 |
|
* reference counted. */ |
| 138 |
|
Self &operator=(const Self& it); |
| 139 |
|
|
| 140 |
|
/** Constructor establishes an iterator to walk along a line */ |
| 141 |
LEN |
LineConstIterator(const ImageType *imagePtr, const IndexType &firstIndex, const IndexType &lastIndex); |
| 142 |
|
|
| 143 |
|
/** Default Destructor. */ |
| 144 |
|
virtual ~LineConstIterator() {}; |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
protected: //made protected so other iterators can access |
| 148 |
|
/** Smart pointer to the source image. */ |
| 149 |
|
typename ImageType::ConstWeakPointer m_Image; |
| 150 |
|
|
| 151 |
|
/** Region type to iterate over. */ |
| 152 |
|
RegionType m_Region; |
| 153 |
|
|
| 154 |
|
/** Is the iterator at the end of its walk? */ |
| 155 |
|
bool m_IsAtEnd; |
| 156 |
|
|
| 157 |
|
/** Start, end and current ND index position in the image of the line */ |
| 158 |
|
IndexType m_CurrentImageIndex; |
| 159 |
|
IndexType m_StartIndex; |
| 160 |
|
IndexType m_LastIndex; |
| 161 |
|
IndexType m_EndIndex; // one past the end of the line in the m_MainDirection |
| 162 |
|
|
| 163 |
|
/** Variables that drive the Bresenham-Algorithm */ |
| 164 |
|
// The dimension with the largest difference between start and end |
| 165 |
|
unsigned int m_MainDirection; |
| 166 |
|
|
| 167 |
|
// Accumulated error for the other dimensions |
| 168 |
|
IndexType m_AccumulateError; |
| 169 |
|
|
| 170 |
|
// Increment for the error for each step. Two times the difference between |
| 171 |
|
// start and end |
| 172 |
|
IndexType m_IncrementError; |
| 173 |
|
|
| 174 |
|
// If enough is accumulated for a dimension, the index has to be |
| 175 |
|
// incremented. Will be the number of pixels in the line |
| 176 |
|
IndexType m_MaximalError; |
| 177 |
|
|
| 178 |
|
// Direction of increment. -1 or 1 |
| 179 |
|
IndexType m_OverflowIncrement; |
| 180 |
|
|
| 181 |
|
// After an overflow, the accumulated error is reduced again. Will be |
| 182 |
|
// two times the number of pixels in the line |
| 183 |
|
IndexType m_ReduceErrorAfterIncrement; |
| 184 |
|
}; |
| 185 |
|
|
| 186 |
|
} // end namespace itk |
| 187 |
|
|
| 188 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 189 |
|
#include "itkLineConstIterator.txx" |
| 190 |
|
#endif |
| 191 |
|
|
| 192 |
|
#endif |
| 193 |
|
|