| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkFiniteDifferenceImageFilter.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:35 $ |
| 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 __itkFiniteDifferenceImageFilter_txx_ |
| 18 |
DEF |
#define __itkFiniteDifferenceImageFilter_txx_ |
| 19 |
|
|
| 20 |
|
#include "itkImageRegionIterator.h" |
| 21 |
|
#include "itkImageRegionConstIterator.h" |
| 22 |
|
#include "itkExceptionObject.h" |
| 23 |
|
#include "itkEventObject.h" |
| 24 |
|
|
| 25 |
|
namespace itk { |
| 26 |
|
|
| 27 |
|
template <class TInputImage, class TOutputImage> |
| 28 |
|
void |
| 29 |
|
FiniteDifferenceImageFilter<TInputImage, TOutputImage> |
| 30 |
|
::GenerateData() |
| 31 |
|
{ |
| 32 |
|
if (this->GetState() == UNINITIALIZED) |
| 33 |
|
{ |
| 34 |
|
// Set the coefficients for the deriviatives |
| 35 |
|
double coeffs[TInputImage::ImageDimension]; |
| 36 |
|
if (m_UseImageSpacing) |
| 37 |
|
{ |
| 38 |
|
for (unsigned int i = 0; i < TInputImage::ImageDimension; i++) |
| 39 |
|
{ |
| 40 |
|
coeffs[i] = 1.0 / this->GetInput()->GetSpacing()[i]; |
| 41 |
|
} |
| 42 |
|
} |
| 43 |
|
else |
| 44 |
|
{ |
| 45 |
|
for (unsigned int i = 0; i < TInputImage::ImageDimension; i++) |
| 46 |
|
{ |
| 47 |
|
coeffs[i] = 1.0; |
| 48 |
|
} |
| 49 |
|
} |
| 50 |
|
m_DifferenceFunction->SetScaleCoefficients(coeffs); |
| 51 |
|
|
| 52 |
|
// Allocate the output image |
| 53 |
|
this->AllocateOutputs(); |
| 54 |
|
|
| 55 |
|
// Copy the input image to the output image. Algorithms will operate |
| 56 |
|
// directly on the output image and the update buffer. |
| 57 |
|
this->CopyInputToOutput(); |
| 58 |
|
|
| 59 |
|
// Perform any other necessary pre-iteration initialization. |
| 60 |
|
this->Initialize(); |
| 61 |
|
|
| 62 |
|
// Allocate the internal update buffer. This takes place entirely within |
| 63 |
|
// the subclass, since this class cannot define an update buffer type. |
| 64 |
|
this->AllocateUpdateBuffer(); |
| 65 |
|
|
| 66 |
|
this->SetStateToInitialized(); |
| 67 |
|
m_ElapsedIterations = 0; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
// Iterative algorithm |
| 71 |
|
TimeStepType dt; |
| 72 |
|
|
| 73 |
|
while ( ! this->Halt() ) |
| 74 |
|
{ |
| 75 |
|
this->InitializeIteration(); // An optional method for precalculating |
| 76 |
IND |
*********************************// global values, or otherwise setting up |
| 77 |
IND |
*********************************// for the next iteration |
| 78 |
|
dt = this->CalculateChange(); |
| 79 |
|
this->ApplyUpdate(dt); |
| 80 |
|
++m_ElapsedIterations; |
| 81 |
|
|
| 82 |
|
// Invoke the iteration event. |
| 83 |
|
this->InvokeEvent( IterationEvent() ); |
| 84 |
|
if( this->GetAbortGenerateData() ) |
| 85 |
|
{ |
| 86 |
|
this->InvokeEvent( IterationEvent() ); |
| 87 |
|
this->ResetPipeline(); |
| 88 |
|
throw ProcessAborted(__FILE__,__LINE__); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
if (m_ManualReinitialization == false) |
| 93 |
|
{ |
| 94 |
|
this->SetStateToUninitialized(); // Reset the state once execution is |
| 95 |
IND |
*************************************// completed |
| 96 |
|
} |
| 97 |
|
// Any further processing of the solution can be done here. |
| 98 |
|
this->PostProcessOutput(); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* |
| 103 |
|
*/ |
| 104 |
|
template <class TInputImage, class TOutputImage> |
| 105 |
|
void |
| 106 |
|
FiniteDifferenceImageFilter<TInputImage,TOutputImage> |
| 107 |
|
::GenerateInputRequestedRegion() |
| 108 |
|
{ |
| 109 |
|
// call the superclass' implementation of this method |
| 110 |
|
// copy the output requested region to the input requested region |
| 111 |
|
Superclass::GenerateInputRequestedRegion(); |
| 112 |
|
|
| 113 |
|
// get pointers to the input |
| 114 |
|
typename Superclass::InputImagePointer inputPtr = |
| 115 |
IND |
****const_cast< TInputImage * >( this->GetInput()); |
| 116 |
|
|
| 117 |
|
if ( !inputPtr ) |
| 118 |
|
{ |
| 119 |
|
return; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
// Get the size of the neighborhood on which we are going to operate. This |
| 123 |
|
// radius is supplied by the difference function we are using. |
| 124 |
|
typename FiniteDifferenceFunctionType::RadiusType radius |
| 125 |
IND |
****= this->GetDifferenceFunction()->GetRadius(); |
| 126 |
|
|
| 127 |
|
// Try to set up a buffered region that will accommodate our |
| 128 |
|
// neighborhood operations. This may not be possible and we |
| 129 |
|
// need to be careful not to request a region outside the largest |
| 130 |
|
// possible region, because the pipeline will give us whatever we |
| 131 |
|
// ask for. |
| 132 |
|
|
| 133 |
|
// get a copy of the input requested region (should equal the output |
| 134 |
|
// requested region) |
| 135 |
|
typename TInputImage::RegionType inputRequestedRegion; |
| 136 |
|
inputRequestedRegion = inputPtr->GetRequestedRegion(); |
| 137 |
|
|
| 138 |
|
// pad the input requested region by the operator radius |
| 139 |
|
inputRequestedRegion.PadByRadius( radius ); |
| 140 |
|
|
| 141 |
LEN,IND |
// std::cout << "inputRequestedRegion: " << inputRequestedRegion << std::endl; |
| 142 |
LEN |
// std::cout << "largestPossibleRegion: " << inputPtr->GetLargestPossibleRegion() << std::endl; |
| 143 |
|
|
| 144 |
|
// crop the input requested region at the input's largest possible region |
| 145 |
|
if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) ) |
| 146 |
|
{ |
| 147 |
|
inputPtr->SetRequestedRegion( inputRequestedRegion ); |
| 148 |
|
return; |
| 149 |
|
} |
| 150 |
|
else |
| 151 |
|
{ |
| 152 |
|
// Couldn't crop the region (requested region is outside the largest |
| 153 |
|
// possible region). Throw an exception. |
| 154 |
|
|
| 155 |
|
// store what we tried to request (prior to trying to crop) |
| 156 |
|
inputPtr->SetRequestedRegion( inputRequestedRegion ); |
| 157 |
|
|
| 158 |
|
// build an exception |
| 159 |
|
InvalidRequestedRegionError e(__FILE__, __LINE__); |
| 160 |
|
e.SetLocation(ITK_LOCATION); |
| 161 |
LEN |
e.SetDescription("Requested region is (at least partially) outside the largest possible region."); |
| 162 |
|
e.SetDataObject(inputPtr); |
| 163 |
|
throw e; |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
template <class TInputImage, class TOutputImage> |
| 169 |
|
typename FiniteDifferenceImageFilter<TInputImage, TOutputImage>::TimeStepType |
| 170 |
|
FiniteDifferenceImageFilter<TInputImage, TOutputImage> |
| 171 |
|
::ResolveTimeStep(const TimeStepType *timeStepList, const bool *valid, int size) |
| 172 |
|
{ |
| 173 |
|
TimeStepType min; |
| 174 |
|
bool flag; |
| 175 |
|
min = NumericTraits<TimeStepType>::Zero; |
| 176 |
|
|
| 177 |
|
// grab first valid value |
| 178 |
|
flag = false; |
| 179 |
|
for (int i = 0; i < size; ++i) |
| 180 |
|
{ |
| 181 |
|
if (valid[i]) |
| 182 |
|
{ |
| 183 |
|
min = timeStepList[i]; |
| 184 |
|
flag = true; |
| 185 |
|
break; |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
if (!flag) |
| 190 |
|
{ // no values! |
| 191 |
|
throw ExceptionObject(__FILE__, __LINE__); |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
// find minimum value |
| 195 |
|
for (int i = 0; i < size; ++i) |
| 196 |
LEN |
{ if ( valid[i] && (timeStepList[i] < min) ) min = timeStepList[i]; } |
| 197 |
|
|
| 198 |
|
return min; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
template <class TInputImage, class TOutputImage> |
| 202 |
|
bool |
| 203 |
|
FiniteDifferenceImageFilter<TInputImage, TOutputImage> |
| 204 |
|
::Halt() |
| 205 |
|
{ |
| 206 |
|
if (m_NumberOfIterations != 0) |
| 207 |
|
{ |
| 208 |
|
this->UpdateProgress( static_cast<float>( this->GetElapsedIterations() ) / |
| 209 |
IND |
**************************static_cast<float>( m_NumberOfIterations ) ); |
| 210 |
|
} |
| 211 |
|
|
| 212 |
|
if (this->GetElapsedIterations() >= m_NumberOfIterations) |
| 213 |
|
{ |
| 214 |
|
return true; |
| 215 |
|
} |
| 216 |
|
else if ( this->GetElapsedIterations() == 0) |
| 217 |
|
{ |
| 218 |
|
return false; |
| 219 |
|
} |
| 220 |
|
else if ( this->GetMaximumRMSError() > m_RMSChange ) |
| 221 |
|
{ |
| 222 |
|
return true; |
| 223 |
|
} |
| 224 |
|
else |
| 225 |
|
{ |
| 226 |
|
return false; |
| 227 |
|
} |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
template <class TInputImage, class TOutputImage> |
| 232 |
|
void |
| 233 |
|
FiniteDifferenceImageFilter<TInputImage, TOutputImage> |
| 234 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 235 |
|
{ |
| 236 |
|
Superclass::PrintSelf(os, indent); |
| 237 |
|
|
| 238 |
|
os << indent << "ElapsedIterations: " << m_ElapsedIterations << std::endl; |
| 239 |
LEN |
os << indent << "UseImageSpacing: " << (m_UseImageSpacing ? "On" : "Off") << std::endl; |
| 240 |
|
os << indent << "State: " << m_State << std::endl; |
| 241 |
|
os << indent << "MaximumRMSError: " << m_MaximumRMSError << std::endl; |
| 242 |
|
os << indent << "NumberOfIterations: " << m_NumberOfIterations << std::endl; |
| 243 |
LEN |
os << indent << "ManualReinitialization: " << m_ManualReinitialization << std::endl; |
| 244 |
|
os << indent << "RMSChange: " << m_RMSChange << std::endl; |
| 245 |
|
os << std::endl; |
| 246 |
|
if (m_DifferenceFunction) |
| 247 |
|
{ |
| 248 |
|
os << indent << "DifferenceFunction: " << std::endl; |
| 249 |
|
m_DifferenceFunction->Print(os,indent.GetNextIndent()); |
| 250 |
|
} |
| 251 |
|
else |
| 252 |
|
{ |
| 253 |
|
os << indent << "DifferenceFunction: " << "(None)" << std::endl; |
| 254 |
|
} |
| 255 |
|
os << std::endl; |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
}// end namespace itk |
| 260 |
|
|
| 261 |
|
#endif |
| 262 |
|
|