| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkShapedNeighborhoodIterator.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:47 $ |
| 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 __itkShapedNeighborhoodIterator_h |
| 18 |
|
#define __itkShapedNeighborhoodIterator_h |
| 19 |
|
|
| 20 |
|
#include <vector> |
| 21 |
|
#include <list> |
| 22 |
|
#include "itkConstShapedNeighborhoodIterator.h" |
| 23 |
|
|
| 24 |
|
namespace itk { |
| 25 |
|
|
| 26 |
|
/** \class ShapedNeighborhoodIterator |
| 27 |
|
* |
| 28 |
|
* \brief A neighborhood iterator which can take on an arbitrary shape. |
| 29 |
|
* |
| 30 |
|
* \par General Information |
| 31 |
|
* The ShapedNeighborhoodIterator is a refinement of NeighborhoodIterator which |
| 32 |
|
* allows the user to specify which of the neighborhood elements are active and |
| 33 |
|
* which will be ignored. This is useful for applications which only need to |
| 34 |
|
* work with a subset of the neighborhood around a pixel such as morphological |
| 35 |
|
* operations or cellular automata. This iterator can also be used, for |
| 36 |
|
* example, to specify "cross-shaped" neighborhood where only elements along a |
| 37 |
|
* spatial axes are significant. |
| 38 |
|
* |
| 39 |
|
*\par Constructing a shaped neighborhood iterator |
| 40 |
|
* A shaped neighborhood iterator is constructed by constructing a list of |
| 41 |
|
* active neighbor locations. The list is called the ActiveIndexList. The |
| 42 |
|
* methods ActivateOffset, DeactivateOffset, and ClearActiveList are used to |
| 43 |
|
* construct the ActiveIndexList. The argument to Activate/DeactivateOffset is |
| 44 |
|
* an itk::Offset which represents the ND spatial offset from the center of the |
| 45 |
|
* neighborhood. For example, to activate the center pixel in the neighborhood, |
| 46 |
|
* you would do the following: |
| 47 |
|
* |
| 48 |
|
* \code |
| 49 |
|
* typedef Image<float, 3> ImageType; |
| 50 |
|
* ShapedNeighborhoodIterator<ImageType> it(radius, image, region); |
| 51 |
|
* ShapedNeighborhoodIterator<ImageType>::OffsetType offset = {{0,0,0}}; |
| 52 |
|
* it.ActivateOffset(offset); |
| 53 |
|
* \endcode |
| 54 |
|
* |
| 55 |
|
* where radius, image, and region are as described in NeighborhoodIterator. |
| 56 |
|
* |
| 57 |
|
* Once a neighborhood location has been activated, iteration (operator++, |
| 58 |
|
* operator--, operator+=, operator-=) will update the value at the active |
| 59 |
|
* location. Note that values at inactive locations will NOT be valid if |
| 60 |
|
* queried. |
| 61 |
|
* |
| 62 |
|
*\par Accessing elements in a shaped neighborhood. |
| 63 |
|
* To access the value at an active neighborhood location, you can use the |
| 64 |
|
* standard GetPixel, SetPixel methods. SetPixel is not defined for |
| 65 |
|
* ConstShapedNeighborhoodIterator. The class will not complain if you |
| 66 |
|
* attempt to access a value at a non-active location, but be aware that the |
| 67 |
|
* result will be undefined. Error checking is not done in this case for the |
| 68 |
|
* sake of efficiency. |
| 69 |
|
* |
| 70 |
|
* A second way to access active shaped neighborhood values is through a |
| 71 |
|
* ShapedNeighborhoodIterator::Iterator or |
| 72 |
|
* ConstShapedNeighborhoodIterator::ConstIterator. The following example |
| 73 |
|
* demonstrates the use of these iterators. |
| 74 |
|
* |
| 75 |
|
* \code |
| 76 |
|
* typedef Image<float, 3> ImageType; |
| 77 |
|
* ShapedNeighborhoodIterator<ImageType> it(radius, image, region); |
| 78 |
|
* . |
| 79 |
|
* . |
| 80 |
|
* . |
| 81 |
|
* it.ActivateOffset(offset1); |
| 82 |
|
* it.ActivateOffset(offset2); |
| 83 |
|
* it.ActivateOffset(offset3); |
| 84 |
|
* etc.. |
| 85 |
|
* . |
| 86 |
|
* . |
| 87 |
|
* . |
| 88 |
|
* ShapedNeighborhoodIterator<ImageType>::Iterator i; |
| 89 |
|
* for (i = it.Begin(); ! i.IsAtEnd(); i++) |
| 90 |
|
* { i.Set(i.Get() + 1.0); } |
| 91 |
|
* \\ you may also use i != i.End(), but IsAtEnd() may be slightly faster. |
| 92 |
|
* \endcode |
| 93 |
|
* |
| 94 |
|
* You can also iterate backward through the neighbohood active list. |
| 95 |
|
* |
| 96 |
|
* \code |
| 97 |
|
* i = it.End(); |
| 98 |
|
* i--; |
| 99 |
|
* while (i != it.Begin()) |
| 100 |
|
* { |
| 101 |
IND |
** i.Set(i.Get() + 1.0); |
| 102 |
IND |
** i--; |
| 103 |
IND |
** } |
| 104 |
|
* i.Set(i.Get() + 1.0); |
| 105 |
|
* \endcode |
| 106 |
|
* |
| 107 |
|
* The Get() Set() syntax was chosen versus defining operator* for these |
| 108 |
|
* iterators because lvalue vs. rvalue context information is needed to |
| 109 |
|
* determine whether bounds checking must take place. |
| 110 |
|
* |
| 111 |
|
* \sa Neighborhood |
| 112 |
IND |
*** |
| 113 |
|
* \par MORE INFORMATION |
| 114 |
|
* For a complete description of the ITK Image Iterators and their API, please |
| 115 |
|
* see the Iterators chapter in the ITK Software Guide. The ITK Software Guide |
| 116 |
|
* is available in print and as a free .pdf download from http://www.itk.org. |
| 117 |
|
* |
| 118 |
|
* \ingroup ImageIterators |
| 119 |
|
* |
| 120 |
|
* \sa ImageConstIterator \sa ConditionalConstIterator |
| 121 |
|
* \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator |
| 122 |
|
* \sa ConstSliceIterator \sa CorrespondenceDataStructureIterator |
| 123 |
|
* \sa FloodFilledFunctionConditionalConstIterator |
| 124 |
|
* \sa FloodFilledImageFunctionConditionalConstIterator |
| 125 |
|
* \sa FloodFilledImageFunctionConditionalIterator |
| 126 |
|
* \sa FloodFilledSpatialFunctionConditionalConstIterator |
| 127 |
|
* \sa FloodFilledSpatialFunctionConditionalIterator |
| 128 |
|
* \sa ImageConstIterator \sa ImageConstIteratorWithIndex |
| 129 |
|
* \sa ImageIterator \sa ImageIteratorWithIndex |
| 130 |
|
* \sa ImageLinearConstIteratorWithIndex \sa ImageLinearIteratorWithIndex |
| 131 |
|
* \sa ImageRandomConstIteratorWithIndex \sa ImageRandomIteratorWithIndex |
| 132 |
|
* \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex |
| 133 |
|
* \sa ImageRegionExclusionConstIteratorWithIndex |
| 134 |
|
* \sa ImageRegionExclusionIteratorWithIndex |
| 135 |
|
* \sa ImageRegionIterator \sa ImageRegionIteratorWithIndex |
| 136 |
|
* \sa ImageRegionReverseConstIterator \sa ImageRegionReverseIterator |
| 137 |
|
* \sa ImageReverseConstIterator \sa ImageReverseIterator |
| 138 |
|
* \sa ImageSliceConstIteratorWithIndex \sa ImageSliceIteratorWithIndex |
| 139 |
|
* \sa NeighborhoodIterator \sa PathConstIterator \sa PathIterator |
| 140 |
|
* \sa ShapedNeighborhoodIterator \sa SliceIterator |
| 141 |
|
* \sa ImageConstIteratorWithIndex */ |
| 142 |
|
template<class TImage, class TBoundaryCondition |
| 143 |
|
= ZeroFluxNeumannBoundaryCondition<TImage> > |
| 144 |
|
class ITK_EXPORT ShapedNeighborhoodIterator |
| 145 |
IND |
**: public ConstShapedNeighborhoodIterator<TImage, TBoundaryCondition> |
| 146 |
|
{ |
| 147 |
|
public: |
| 148 |
|
/** Extract image type information. */ |
| 149 |
|
typedef typename TImage::InternalPixelType InternalPixelType; |
| 150 |
TDA |
typedef typename TImage::PixelType PixelType; |
| 151 |
|
|
| 152 |
|
/** Save the image dimension. */ |
| 153 |
|
itkStaticConstMacro(Dimension, unsigned int, TImage::ImageDimension); |
| 154 |
|
|
| 155 |
|
/** Standard class typedefs. */ |
| 156 |
|
typedef ShapedNeighborhoodIterator Self; |
| 157 |
LEN,TDA |
typedef ConstShapedNeighborhoodIterator<TImage, TBoundaryCondition> Superclass; |
| 158 |
|
|
| 159 |
|
/** Inherit typedefs from superclass */ |
| 160 |
|
typedef typename Superclass::OffsetType OffsetType; |
| 161 |
TDA |
typedef typename OffsetType::OffsetValueType OffsetValueType; |
| 162 |
|
typedef typename Superclass::RadiusType RadiusType; |
| 163 |
TDA |
typedef typename Superclass::SizeType SizeType; |
| 164 |
TDA |
typedef typename Superclass::SizeValueType SizeValueType; |
| 165 |
TDA |
typedef typename Superclass::ConstIterator ConstIterator; |
| 166 |
TDA |
typedef typename Superclass::IndexListType IndexListType; |
| 167 |
TDA |
typedef typename Superclass::BoundaryConditionType BoundaryConditionType; |
| 168 |
LEN,TDA |
typedef typename Superclass::ImageBoundaryConditionPointerType ImageBoundaryConditionPointerType; |
| 169 |
TDA |
typedef typename Superclass::NeighborhoodType NeighborhoodType; |
| 170 |
TDA |
typedef typename Superclass::IndexType IndexType; |
| 171 |
TDA |
typedef typename Superclass::ImageType ImageType; |
| 172 |
|
typedef typename Superclass::RegionType RegionType; |
| 173 |
TDA |
typedef typename Superclass::IndexValueType IndexValueType; |
| 174 |
|
|
| 175 |
|
/** An iterator for the ShapedNeighborhood classes.*/ |
| 176 |
|
struct Iterator : public ConstIterator |
| 177 |
IND |
**{ |
| 178 |
|
Iterator() {} |
| 179 |
|
Iterator(Self *s) : ConstIterator(s) {} |
| 180 |
|
|
| 181 |
|
~Iterator() {} |
| 182 |
|
const Iterator &operator=(const Iterator &o) |
| 183 |
IND |
****{ |
| 184 |
|
ConstIterator::operator=(o); |
| 185 |
|
return *this; |
| 186 |
IND |
****} |
| 187 |
|
|
| 188 |
|
// Promote to public |
| 189 |
|
void Set(const PixelType &v) const |
| 190 |
IND |
****{ ConstIterator::ProtectedSet(v); } |
| 191 |
|
|
| 192 |
IND |
**}; |
| 193 |
|
|
| 194 |
|
/** Default constructor */ |
| 195 |
|
ShapedNeighborhoodIterator() |
| 196 |
IND |
**{ |
| 197 |
|
m_BeginIterator = Iterator(this); |
| 198 |
|
m_EndIterator = Iterator(this); |
| 199 |
|
m_EndIterator.GoToEnd(); |
| 200 |
IND |
**} |
| 201 |
|
|
| 202 |
|
/** Virtual destructor */ |
| 203 |
|
virtual ~ShapedNeighborhoodIterator() {} |
| 204 |
|
|
| 205 |
|
/** Constructor which establishes the region size, neighborhood, and image |
| 206 |
|
* over which to walk. */ |
| 207 |
|
ShapedNeighborhoodIterator(const SizeType &radius, |
| 208 |
|
const ImageType * ptr, |
| 209 |
|
const RegionType ®ion |
| 210 |
IND |
****) : Superclass(radius, const_cast<ImageType*>(ptr), |
| 211 |
IND |
*******************region) |
| 212 |
IND |
**{ |
| 213 |
|
m_BeginIterator = Iterator(this); |
| 214 |
|
m_EndIterator = Iterator(this); |
| 215 |
|
m_EndIterator.GoToEnd(); |
| 216 |
IND |
**} |
| 217 |
|
|
| 218 |
|
// Expose the following methods from the superclass. This is a restricted |
| 219 |
|
// subset of the methods available for NeighborhoodIterator. |
| 220 |
|
Superclass::SetPixel; |
| 221 |
|
Superclass::SetCenterPixel; |
| 222 |
|
|
| 223 |
|
/** Assignment operator */ |
| 224 |
|
Self &operator=(const Self& orig) |
| 225 |
IND |
**{ |
| 226 |
|
Superclass::operator=(orig); |
| 227 |
|
|
| 228 |
|
// Reset begin and end pointer locations |
| 229 |
|
m_BeginIterator.GoToBegin(); |
| 230 |
|
m_EndIterator.GoToEnd(); |
| 231 |
|
return *this; |
| 232 |
IND |
**} |
| 233 |
|
|
| 234 |
|
/** Standard itk print method */ |
| 235 |
|
virtual void PrintSelf(std::ostream &, Indent) const; |
| 236 |
|
|
| 237 |
|
/** Returns a const iterator for the neighborhood which points to the first |
| 238 |
|
* pixel in the neighborhood. */ |
| 239 |
|
Iterator &Begin() { return m_BeginIterator; } |
| 240 |
|
Iterator &End() { return m_EndIterator; } |
| 241 |
|
|
| 242 |
|
/** Returns a const iterator for the neighborhood which points to the last |
| 243 |
|
* pixel in the neighborhood. */ |
| 244 |
|
const ConstIterator &End() const |
| 245 |
IND |
**{ return this->m_ConstEndIterator; } |
| 246 |
|
|
| 247 |
|
void ClearActiveList() |
| 248 |
IND |
**{ |
| 249 |
|
Superclass::ClearActiveList(); |
| 250 |
|
m_EndIterator.GoToEnd(); |
| 251 |
|
m_BeginIterator.GoToBegin(); |
| 252 |
IND |
**} |
| 253 |
|
|
| 254 |
|
protected: |
| 255 |
|
|
| 256 |
IND |
***/** Copy constructor */ |
| 257 |
|
ShapedNeighborhoodIterator( const ShapedNeighborhoodIterator & o); |
| 258 |
|
// purposely not implemented |
| 259 |
|
|
| 260 |
|
|
| 261 |
|
void ActivateIndex(const unsigned int n) |
| 262 |
IND |
**{ |
| 263 |
|
Superclass::ActivateIndex(n); |
| 264 |
|
m_EndIterator.GoToEnd(); |
| 265 |
|
m_BeginIterator.GoToBegin(); |
| 266 |
IND |
**} |
| 267 |
|
|
| 268 |
|
void DeactivateIndex(const unsigned int n) |
| 269 |
IND |
**{ |
| 270 |
|
Superclass::DeactivateIndex(n); |
| 271 |
|
m_EndIterator.GoToEnd(); |
| 272 |
|
m_BeginIterator.GoToBegin(); |
| 273 |
IND |
**} |
| 274 |
|
|
| 275 |
|
|
| 276 |
|
Iterator m_EndIterator; |
| 277 |
|
Iterator m_BeginIterator; |
| 278 |
|
}; |
| 279 |
|
|
| 280 |
|
} // namespace itk |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 284 |
|
#include "itkShapedNeighborhoodIterator.txx" |
| 285 |
|
#endif |
| 286 |
|
|
| 287 |
|
#endif |
| 288 |
|
|
| 289 |
EOF |
|