| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageRegionConstIterator.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:39 $ |
| 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 |
DEF |
#ifndef _itkImageRegionConstIterator_txx |
| 18 |
DEF |
#define _itkImageRegionConstIterator_txx |
| 19 |
|
|
| 20 |
|
#include "itkImageRegionConstIterator.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
//---------------------------------------------------------------------------- |
| 26 |
|
// Begin() is the first pixel in the region. |
| 27 |
|
template<class TImage> |
| 28 |
|
ImageRegionConstIterator<TImage> |
| 29 |
|
ImageRegionConstIterator<TImage> |
| 30 |
|
::Begin() const |
| 31 |
|
{ |
| 32 |
|
// Copy the current iterator |
| 33 |
|
Self it( *this ); |
| 34 |
|
|
| 35 |
|
// Set the iterator to the beginning of the region |
| 36 |
|
it.GoToBegin(); |
| 37 |
|
|
| 38 |
|
return it; |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
//---------------------------------------------------------------------------- |
| 42 |
|
// End() is one pixel past the last pixel in the current region. |
| 43 |
|
// The index of this pixel is |
| 44 |
|
// [m_StartIndex[0] + m_Size[0], |
| 45 |
|
// m_StartIndex[1] + m_Size[1]-1, ..., |
| 46 |
|
// m_StartIndex[VImageDimension-2] + m_Size[VImageDimension-2]-1, |
| 47 |
|
// m_StartIndex[VImageDimension-1] + m_Size[VImageDimension-1]-1] |
| 48 |
|
// |
| 49 |
|
template<class TImage> |
| 50 |
|
ImageRegionConstIterator<TImage> |
| 51 |
|
ImageRegionConstIterator<TImage> |
| 52 |
|
::End() const |
| 53 |
|
{ |
| 54 |
|
// Copy the current iterator |
| 55 |
|
Self it( *this ); |
| 56 |
|
|
| 57 |
|
// Set the iterator to the end of the region |
| 58 |
|
it.GoToEnd(); |
| 59 |
|
|
| 60 |
|
return it; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
//---------------------------------------------------------------------------- |
| 64 |
|
// Increment when the fastest moving direction has reached its bound. |
| 65 |
|
// This method should *ONLY* be invoked from the operator++() method. |
| 66 |
|
template<class TImage> |
| 67 |
|
void |
| 68 |
|
ImageRegionConstIterator<TImage> |
| 69 |
|
::Increment() |
| 70 |
|
{ |
| 71 |
|
// We have reached the end of the span (row), need to wrap around. |
| 72 |
|
|
| 73 |
|
// First back up one pixel, because we are going to use a different |
| 74 |
|
// algorithm to compute the next pixel |
| 75 |
|
--this->m_Offset; |
| 76 |
|
|
| 77 |
|
// Get the index of the last pixel on the span (row) |
| 78 |
|
typename ImageIterator<TImage>::IndexType |
| 79 |
LEN,IND |
****ind = this->m_Image->ComputeIndex( static_cast<typename Superclass::OffsetValueType>(this->m_Offset) ); |
| 80 |
|
|
| 81 |
|
const typename ImageIterator<TImage>::IndexType& |
| 82 |
IND |
****startIndex = this->m_Region.GetIndex(); |
| 83 |
|
const typename ImageIterator<TImage>::SizeType& |
| 84 |
IND |
****size = this->m_Region.GetSize(); |
| 85 |
|
|
| 86 |
|
// Increment along a row, then wrap at the end of the region row. |
| 87 |
|
unsigned int dim; |
| 88 |
|
|
| 89 |
|
// Check to see if we are past the last pixel in the region |
| 90 |
|
// Note that ++ind[0] moves to the next pixel along the row. |
| 91 |
|
++ind[0]; |
| 92 |
LEN |
bool done = (ind[0] == startIndex[0] + static_cast<typename Superclass::IndexValueType>(size[0])); |
| 93 |
|
for (unsigned int i=1; done && i < ImageIteratorDimension; i++) |
| 94 |
|
{ |
| 95 |
LEN |
done = (ind[i] == startIndex[i] + static_cast<typename Superclass::IndexValueType>(size[i]) - 1); |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
// if the iterator is outside the region (but not past region end) then |
| 99 |
|
// we need to wrap around the region |
| 100 |
|
dim = 0; |
| 101 |
|
if (!done) |
| 102 |
|
{ |
| 103 |
|
while ( ( dim+1 < ImageIteratorDimension ) |
| 104 |
LEN |
&& (ind[dim] > startIndex[dim] + static_cast<typename Superclass::IndexValueType>(size[dim]) - 1) ) |
| 105 |
|
{ |
| 106 |
|
ind[dim] = startIndex[dim]; |
| 107 |
|
ind[++dim]++; |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
this->m_Offset = this->m_Image->ComputeOffset( ind ); |
| 111 |
|
m_SpanEndOffset = this->m_Offset + static_cast<long>(size[0]); |
| 112 |
|
m_SpanBeginOffset = this->m_Offset; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
//---------------------------------------------------------------------------- |
| 117 |
|
// Decrement when the fastest moving direction has reached its bound. |
| 118 |
|
// This method should *ONLY* be invoked from the operator--() method. |
| 119 |
|
template<class TImage> |
| 120 |
|
void |
| 121 |
|
ImageRegionConstIterator<TImage> |
| 122 |
|
::Decrement() |
| 123 |
|
{ |
| 124 |
|
// We have pasted the beginning of the span (row), need to wrap around. |
| 125 |
|
|
| 126 |
|
// First move forward one pixel, because we are going to use a different |
| 127 |
|
// algorithm to compute the next pixel |
| 128 |
|
this->m_Offset++; |
| 129 |
|
|
| 130 |
|
// Get the index of the first pixel on the span (row) |
| 131 |
|
typename ImageIterator<TImage>::IndexType |
| 132 |
LEN,IND |
****ind = this->m_Image->ComputeIndex( static_cast<typename Superclass::IndexValueType>(this->m_Offset) ); |
| 133 |
|
|
| 134 |
|
const typename ImageIterator<TImage>::IndexType& |
| 135 |
IND |
****startIndex = this->m_Region.GetIndex(); |
| 136 |
|
const typename ImageIterator<TImage>::SizeType& |
| 137 |
IND |
****size = this->m_Region.GetSize(); |
| 138 |
|
|
| 139 |
|
// Deccrement along a row, then wrap at the beginning of the region row. |
| 140 |
|
bool done; |
| 141 |
|
unsigned int dim; |
| 142 |
|
|
| 143 |
|
// Check to see if we are past the first pixel in the region |
| 144 |
|
// Note that --ind[0] moves to the previous pixel along the row. |
| 145 |
|
done = (--ind[0] == startIndex[0] - 1); |
| 146 |
|
for (unsigned int i=1; done && i < ImageIteratorDimension; i++) |
| 147 |
|
{ |
| 148 |
|
done = (ind[i] == startIndex[i]); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
// if the iterator is outside the region (but not past region begin) then |
| 152 |
|
// we need to wrap around the region |
| 153 |
|
dim = 0; |
| 154 |
|
if (!done) |
| 155 |
|
{ |
| 156 |
|
while ( (dim < ImageIteratorDimension - 1) |
| 157 |
|
&& (ind[dim] < startIndex[dim]) ) |
| 158 |
|
{ |
| 159 |
LEN |
ind[dim] = startIndex[dim] + static_cast<typename Superclass::IndexValueType>(size[dim]) - 1; |
| 160 |
|
ind[++dim]--; |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
this->m_Offset = this->m_Image->ComputeOffset( ind ); |
| 164 |
|
m_SpanEndOffset = this->m_Offset + 1; |
| 165 |
|
m_SpanBeginOffset = m_SpanEndOffset - static_cast<long>(size[0]); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
} // end namespace itk |
| 169 |
|
|
| 170 |
|
#endif |
| 171 |
|
|