| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkLineConstIterator.txx.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 |
DEF |
#ifndef _itkLineConstIterator_txx |
| 18 |
DEF |
#define _itkLineConstIterator_txx |
| 19 |
|
|
| 20 |
|
#include "itkLineConstIterator.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
template<class TImage> |
| 26 |
|
LineConstIterator<TImage> |
| 27 |
LEN |
::LineConstIterator(const ImageType *imagePtr, const IndexType &firstIndex, const IndexType &lastIndex) |
| 28 |
|
{ |
| 29 |
|
unsigned int i; |
| 30 |
|
|
| 31 |
|
m_Image = imagePtr; |
| 32 |
|
|
| 33 |
|
m_StartIndex = firstIndex; |
| 34 |
|
m_LastIndex = lastIndex; |
| 35 |
|
|
| 36 |
|
IndexType difference; |
| 37 |
|
for (i = 0; i < TImage::ImageDimension; ++i) |
| 38 |
|
{ |
| 39 |
|
difference[i] = lastIndex[i] - firstIndex[i]; |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
IndexValueType maxDistance = 0; |
| 43 |
|
int maxDistanceDimension = 0; |
| 44 |
|
for (i = 0; i < TImage::ImageDimension; ++i) |
| 45 |
|
{ |
| 46 |
|
IndexValueType distance = abs(difference[i]); |
| 47 |
|
if (distance > maxDistance) |
| 48 |
|
{ |
| 49 |
|
maxDistance = distance; |
| 50 |
|
maxDistanceDimension = i; |
| 51 |
|
} |
| 52 |
|
m_IncrementError[i] = 2*distance; |
| 53 |
|
m_OverflowIncrement[i] = (difference[i] < 0 ? -1 : 1); |
| 54 |
|
} |
| 55 |
|
m_MainDirection = maxDistanceDimension; |
| 56 |
|
m_MaximalError.Fill(maxDistance); |
| 57 |
|
m_ReduceErrorAfterIncrement.Fill(2*maxDistance); |
| 58 |
|
|
| 59 |
|
// Need to set m_EndIndex to be one pixel past the m_LastIndex along |
| 60 |
|
// the bresenham line. THis is tricky to |
| 61 |
|
// do. m_EndIndex[m_MainDirection] is always offset by one from the |
| 62 |
|
// m_LastIndex[m_MainDirection]. The other indices may or may not |
| 63 |
|
// be incremented depending on the accumulated error to that point. |
| 64 |
|
// |
| 65 |
|
// To avoid traversing the line to determine whether the other |
| 66 |
|
// components need to be adjusted, we merely set the main direction |
| 67 |
|
// to be incremented and keep the remaining indices to be same as |
| 68 |
|
// LastIndex. THen in the test for IsAtEnd, we just check the |
| 69 |
|
// MainDirection component of the index. |
| 70 |
|
for (i = 0; i < TImage::ImageDimension; ++i) |
| 71 |
|
{ |
| 72 |
|
if (i == m_MainDirection) |
| 73 |
|
{ |
| 74 |
|
m_EndIndex[i] = m_LastIndex[i] + m_OverflowIncrement[i]; |
| 75 |
|
} |
| 76 |
|
else |
| 77 |
|
{ |
| 78 |
|
m_EndIndex[i] = m_LastIndex[i]; |
| 79 |
|
} |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
m_Region = m_Image->GetBufferedRegion(); |
| 85 |
|
|
| 86 |
|
this->GoToBegin(); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
template<class TImage> |
| 91 |
|
LineConstIterator<TImage> & |
| 92 |
|
LineConstIterator<TImage> |
| 93 |
|
::operator=(const Self & it) |
| 94 |
|
{ |
| 95 |
|
m_Image = it.m_Image; // copy the smart pointer |
| 96 |
|
m_Region = it.m_Region; |
| 97 |
|
m_IsAtEnd = it.m_IsAtEnd; |
| 98 |
|
m_CurrentImageIndex = it.m_CurrentImageIndex; |
| 99 |
|
m_StartIndex = it.m_StartIndex; |
| 100 |
|
m_LastIndex = it.m_LastIndex; |
| 101 |
|
m_EndIndex = it.m_EndIndex; |
| 102 |
|
m_MainDirection = it.m_MainDirection; |
| 103 |
|
m_AccumulateError = it.m_AccumulateError; |
| 104 |
|
m_IncrementError = it.m_IncrementError; |
| 105 |
|
m_MaximalError = it.m_MaximalError; |
| 106 |
|
m_OverflowIncrement = it.m_OverflowIncrement; |
| 107 |
|
m_ReduceErrorAfterIncrement = it.m_ReduceErrorAfterIncrement; |
| 108 |
|
|
| 109 |
|
return *this; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
template<class TImage> |
| 114 |
|
void |
| 115 |
|
LineConstIterator<TImage> |
| 116 |
|
::GoToBegin() |
| 117 |
|
{ |
| 118 |
|
m_CurrentImageIndex = m_StartIndex; |
| 119 |
|
m_AccumulateError.Fill(0); |
| 120 |
|
m_IsAtEnd = (m_StartIndex[m_MainDirection] == m_EndIndex[m_MainDirection]); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
template<class TImage> |
| 125 |
|
void |
| 126 |
|
LineConstIterator<TImage> |
| 127 |
|
::operator++() |
| 128 |
|
{ |
| 129 |
|
// We need to modify m_AccumulateError, m_CurrentImageIndex, m_IsAtEnd |
| 130 |
|
for (unsigned int i = 0; i < TImage::ImageDimension; ++i) |
| 131 |
|
{ |
| 132 |
|
if (i == m_MainDirection) |
| 133 |
|
{ |
| 134 |
|
m_CurrentImageIndex[i] += m_OverflowIncrement[i]; |
| 135 |
|
} |
| 136 |
|
else |
| 137 |
|
{ |
| 138 |
|
m_AccumulateError[i] += m_IncrementError[i]; |
| 139 |
|
if (m_AccumulateError[i] >= m_MaximalError[i]) |
| 140 |
|
{ |
| 141 |
|
m_CurrentImageIndex[i] += m_OverflowIncrement[i]; |
| 142 |
|
m_AccumulateError[i] -= m_ReduceErrorAfterIncrement[i]; |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
if (m_CurrentImageIndex[m_MainDirection] == m_EndIndex[m_MainDirection]) |
| 148 |
|
{ |
| 149 |
|
m_IsAtEnd = true; |
| 150 |
|
} |
| 151 |
|
else if( ! m_Region.IsInside( m_CurrentImageIndex ) ) |
| 152 |
|
{ |
| 153 |
|
// The new index is outside the acceptable region. We can iterate no |
| 154 |
|
// farther, call this the end. NOTE THAT INPUT IS STILL INCREMENTED. |
| 155 |
|
m_IsAtEnd = true; |
| 156 |
|
itkWarningMacro(<<"Line left region; unable to finish tracing it"); |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
} // end namespace itk |
| 161 |
|
|
| 162 |
|
#endif |
| 163 |
|
|