/
itkFiniteDifferenceImageFilter.hxx
307 lines (266 loc) · 8.7 KB
/
itkFiniteDifferenceImageFilter.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkFiniteDifferenceImageFilter_hxx
#define itkFiniteDifferenceImageFilter_hxx
#include "itkImageRegionIterator.h"
#include "itkMacro.h"
#include "itkEventObject.h"
namespace itk
{
template <typename TInputImage, typename TOutputImage>
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::FiniteDifferenceImageFilter()
{
m_UseImageSpacing = true;
m_ElapsedIterations = 0;
m_DifferenceFunction = nullptr;
m_NumberOfIterations = NumericTraits<IdentifierType>::max();
m_MaximumRMSError = 0.0;
m_RMSChange = 0.0;
m_IsInitialized = false;
m_ManualReinitialization = false;
this->InPlaceOff();
}
template <typename TInputImage, typename TOutputImage>
void
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::GenerateData()
{
// Test whether the output pixel type (or its components) are not of type
// float or double:
if (NumericTraits<OutputPixelValueType>::is_integer)
{
itkWarningMacro("Output pixel type MUST be float or double to prevent computational errors");
}
if (!m_IsInitialized)
{
// Allocate the output image
this->AllocateOutputs();
// Copy the input image to the output image. Algorithms will operate
// directly on the output image and the update buffer.
this->CopyInputToOutput();
// Set the coefficients of the Function and consider the use of images
// spacing.
this->InitializeFunctionCoefficients();
// Perform any other necessary pre-iteration initialization.
this->Initialize();
// Allocate the internal update buffer. This takes place entirely within
// the subclass, since this class cannot define an update buffer type.
this->AllocateUpdateBuffer();
m_IsInitialized = true;
m_ElapsedIterations = 0;
}
// Iterative algorithm
while (!this->Halt())
{
this->InitializeIteration(); // An optional method for precalculating
// global values, or otherwise setting up
// for the next iteration
TimeStepType dt = this->CalculateChange();
this->ApplyUpdate(dt);
++m_ElapsedIterations;
// Invoke the iteration event.
this->InvokeEvent(IterationEvent());
if (this->GetAbortGenerateData())
{
this->InvokeEvent(IterationEvent());
this->ResetPipeline();
throw ProcessAborted(__FILE__, __LINE__);
}
}
if (!m_ManualReinitialization)
{
// Reset the state once execution is completed
m_IsInitialized = false;
}
// Any further processing of the solution can be done here.
this->PostProcessOutput();
}
/**
*
*/
template <typename TInputImage, typename TOutputImage>
void
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
// copy the output requested region to the input requested region
Superclass::GenerateInputRequestedRegion();
// get pointers to the input
typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput());
if (!inputPtr)
{
return;
}
// Get the size of the neighborhood on which we are going to operate. This
// radius is supplied by the difference function we are using.
RadiusType radius = this->GetDifferenceFunction()->GetRadius();
// Try to set up a buffered region that will accommodate our
// neighborhood operations. This may not be possible and we
// need to be careful not to request a region outside the largest
// possible region, because the pipeline will give us whatever we
// ask for.
// get a copy of the input requested region (should equal the output
// requested region)
typename TInputImage::RegionType inputRequestedRegion;
inputRequestedRegion = inputPtr->GetRequestedRegion();
// pad the input requested region by the operator radius
inputRequestedRegion.PadByRadius(radius);
// crop the input requested region at the input's largest possible region
if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
{
inputPtr->SetRequestedRegion(inputRequestedRegion);
return;
}
else
{
// Couldn't crop the region (requested region is outside the largest
// possible region). Throw an exception.
// store what we tried to request (prior to trying to crop)
inputPtr->SetRequestedRegion(inputRequestedRegion);
// build an exception
InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputPtr);
throw e;
}
}
template <typename TInputImage, typename TOutputImage>
typename FiniteDifferenceImageFilter<TInputImage, TOutputImage>::TimeStepType
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::ResolveTimeStep(const std::vector<TimeStepType> & timeStepList,
const BooleanStdVectorType & valid) const
{
TimeStepType oMin{};
bool flag = false;
// grab first valid value
auto t_it = timeStepList.begin();
auto t_end = timeStepList.end();
auto v_it = valid.begin();
while (t_it != t_end)
{
if (*v_it)
{
oMin = *t_it;
flag = true;
break;
}
++t_it;
++v_it;
}
if (!flag)
{
// no values!
itkGenericExceptionMacro(<< "there is no satisfying value");
}
t_it = timeStepList.begin();
v_it = valid.begin();
// find minimum value
while (t_it != t_end)
{
if (*v_it && (*t_it < oMin))
{
oMin = *t_it;
}
++t_it;
++v_it;
}
return oMin;
}
template <typename TInputImage, typename TOutputImage>
bool
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::Halt()
{
if (m_NumberOfIterations != 0)
{
this->UpdateProgress(static_cast<float>(this->GetElapsedIterations()) / static_cast<float>(m_NumberOfIterations));
}
if (this->GetElapsedIterations() >= m_NumberOfIterations)
{
return true;
}
else if (this->GetElapsedIterations() == 0)
{
return false;
}
else if (this->GetMaximumRMSError() > m_RMSChange)
{
return true;
}
else
{
return false;
}
}
template <typename TInputImage, typename TOutputImage>
void
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::InitializeFunctionCoefficients()
{
// Set the coefficients for the derivatives
double coeffs[ImageDimension];
if (this->m_UseImageSpacing)
{
const TOutputImage * outputImage = this->GetOutput();
if (outputImage == nullptr)
{
itkExceptionMacro("Output image is nullptr");
}
using SpacingType = typename TOutputImage::SpacingType;
const SpacingType & spacing = outputImage->GetSpacing();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
coeffs[i] = 1.0 / static_cast<double>(spacing[i]);
}
}
else
{
for (unsigned int i = 0; i < ImageDimension; ++i)
{
coeffs[i] = 1.0;
}
}
if (m_DifferenceFunction)
{
m_DifferenceFunction->SetScaleCoefficients(coeffs);
}
}
template <typename TInputImage, typename TOutputImage>
void
FiniteDifferenceImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "ElapsedIterations: " << m_ElapsedIterations << std::endl;
os << indent << "UseImageSpacing: " << (m_UseImageSpacing ? "On" : "Off") << std::endl;
os << indent << "State: " << (m_IsInitialized ? "INITIALIZED" : "UNINITIALIZED") << std::endl;
os << indent << "MaximumRMSError: " << m_MaximumRMSError << std::endl;
os << indent << "NumberOfIterations: " << m_NumberOfIterations << std::endl;
os << indent << "ManualReinitialization: " << m_ManualReinitialization << std::endl;
os << indent << "RMSChange: " << m_RMSChange << std::endl;
os << std::endl;
if (m_DifferenceFunction)
{
os << indent << "DifferenceFunction: " << std::endl;
m_DifferenceFunction->Print(os, indent.GetNextIndent());
}
else
{
os << indent << "DifferenceFunction: "
<< "(None)" << std::endl;
}
os << std::endl;
}
} // end namespace itk
#endif