-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathitkDICOMOrientImageFilter.hxx
More file actions
322 lines (275 loc) · 9.73 KB
/
itkDICOMOrientImageFilter.hxx
File metadata and controls
322 lines (275 loc) · 9.73 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*=========================================================================
*
* 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 itkDICOMOrientImageFilter_hxx
#define itkDICOMOrientImageFilter_hxx
#include "itkDICOMOrientImageFilter.h"
#include "itkPermuteAxesImageFilter.h"
#include "itkFlipImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkProgressAccumulator.h"
namespace itk
{
template <typename TInputImage>
DICOMOrientImageFilter<TInputImage>::DICOMOrientImageFilter()
: m_FlipAxes{ false }
{
for (unsigned int dimension = 0; dimension < ImageDimension; ++dimension)
{
this->m_PermuteOrder[dimension] = dimension;
}
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
// Get pointers to the input and output
typename ImageType::Pointer inputPtr = const_cast<ImageType *>(this->GetInput());
if (inputPtr)
{
inputPtr->SetRequestedRegionToLargestPossibleRegion();
}
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::DeterminePermutationsAndFlips(DICOMOrientation desired, DICOMOrientation given)
{
for (unsigned int j = 0; j < ImageDimension; j++)
{
m_PermuteOrder[j] = j;
}
m_FlipAxes.Fill(false);
constexpr unsigned int CodeAxisDir = 0x1;
const DICOMOrientation::CoordinateEnum desired_codes[] = { desired.GetPrimaryTerm(),
desired.GetSecondaryTerm(),
desired.GetTertiaryTerm() };
const DICOMOrientation::CoordinateEnum given_codes[] = { given.GetPrimaryTerm(),
given.GetSecondaryTerm(),
given.GetTertiaryTerm() };
// i, j, k will be the indexes in the Majorness code of the axes to flip;
// they encode the axes as the reader will find them, 0 is the lowest order
// axis of whatever spatial interpretation, and 2 is the highest order axis.
// Perhaps rename them moving_image_reader_axis_i, etc.
for (unsigned int i = 0; i < ImageDimension - 1; i++)
{
if (!DICOMOrientation::SameOrientationAxes(given_codes[i], desired_codes[i]))
{
for (unsigned int j = 0; j < ImageDimension; j++)
{
if (DICOMOrientation::SameOrientationAxes(given_codes[i], desired_codes[j]))
{
if (i == j)
{
// Axis i is already in place
continue;
}
if (DICOMOrientation::SameOrientationAxes(given_codes[j], desired_codes[i]))
{
// The cyclic permutation (i j) applies. Therefore the remainder
// is (k), i.e., stationary
m_PermuteOrder[i] = j;
m_PermuteOrder[j] = i;
}
else
{
// work out an (i j k) cyclic permutation
for (unsigned int k = 0; k < ImageDimension; k++)
{
if (DICOMOrientation::SameOrientationAxes(given_codes[j], desired_codes[k]))
{
// At this point, we can pick off (i j k)
m_PermuteOrder[i] = k;
m_PermuteOrder[j] = i;
m_PermuteOrder[k] = j;
break;
}
}
// Effectively, if (k==3) continue
}
break;
}
}
// Effectively, if (j==3) continue
}
}
for (unsigned int i = 0; i < ImageDimension; i++)
{
const unsigned int j = m_PermuteOrder[i];
if ((static_cast<uint8_t>(given_codes[j]) & CodeAxisDir) != (static_cast<uint8_t>(desired_codes[i]) & CodeAxisDir))
{
m_FlipAxes[i] = true;
}
}
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::SetInputCoordinateOrientation(OrientationEnum newCode)
{
m_InputCoordinateOrientation = newCode;
this->DeterminePermutationsAndFlips(m_DesiredCoordinateOrientation, m_InputCoordinateOrientation);
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::SetDesiredCoordinateOrientation(OrientationEnum newCode)
{
if (m_DesiredCoordinateOrientation != newCode)
{
m_DesiredCoordinateOrientation = newCode;
this->DeterminePermutationsAndFlips(m_DesiredCoordinateOrientation, m_InputCoordinateOrientation);
this->Modified();
}
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::SetDesiredCoordinateOrientation(const std::string & desired)
{
DICOMOrientation o(desired);
if (OrientationEnum(o) == OrientationEnum::INVALID)
{
itkWarningMacro("Invalid desired coordinate direction string: \"" << desired << "\"!");
}
this->SetDesiredCoordinateOrientation(o);
}
template <typename TInputImage>
bool
DICOMOrientImageFilter<TInputImage>::NeedToPermute()
{
for (unsigned int j = 0; j < ImageDimension; j++)
{
if (m_PermuteOrder[j] != j)
{
return true;
}
}
return false;
}
template <typename TInputImage>
bool
DICOMOrientImageFilter<TInputImage>::NeedToFlip()
{
for (unsigned int j = 0; j < ImageDimension; j++)
{
if (m_FlipAxes[j])
{
return true;
}
}
return false;
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::GenerateData()
{
// Create a process accumulator for tracking the progress of this minipipeline
typename ProgressAccumulator::Pointer progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);
// No need to allocate the output since the minipipeline does it
// this->AllocateOutputs();
using PermuteFilterType = PermuteAxesImageFilter<ImageType>;
using FlipFilterType = FlipImageFilter<ImageType>;
using CastToOutputFilterType = CastImageFilter<ImageType, ImageType>;
typename PermuteFilterType::Pointer permute = PermuteFilterType::New();
typename FlipFilterType::Pointer flip = FlipFilterType::New();
typename CastToOutputFilterType::Pointer cast = CastToOutputFilterType::New();
progress->RegisterInternalFilter(permute, .45f);
progress->RegisterInternalFilter(flip, .45);
progress->RegisterInternalFilter(cast, .1f);
typename ImageType::Pointer inputPtr = ImageType::New();
inputPtr->Graft(const_cast<ImageType *>(this->GetInput()));
typename ImageType::Pointer nextInput = inputPtr;
// Only run those filters that will do something
if (NeedToPermute())
{
permute->SetInput(nextInput);
permute->SetOrder(m_PermuteOrder);
permute->ReleaseDataFlagOn();
nextInput = permute->GetOutput();
}
else
{
itkDebugMacro("No need to permute");
}
if (NeedToFlip())
{
flip->SetInput(nextInput);
flip->SetFlipAxes(m_FlipAxes);
flip->FlipAboutOriginOff();
nextInput = flip->GetOutput();
}
else
{
itkDebugMacro(<< "No need to flip");
}
//
// Cast might not be necessary, but CastImagefilter is optimized for
// the case where the ImageType == OutputImageType
cast->SetInput(nextInput);
cast->GraftOutput(this->GetOutput());
cast->Update();
this->GraftOutput(cast->GetOutput());
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::GenerateOutputInformation()
{
// Call the superclass' implementation of this method
Superclass::GenerateOutputInformation();
// Get pointers to the input and output
typename ImageType::Pointer inputPtr = ImageType::New();
inputPtr->Graft(const_cast<ImageType *>(this->GetInput()));
ImageType * outputPtr = this->GetOutput();
// Compute the GivenOrientation from the image's direction cosines
const DICOMOrientation inputOrientation(inputPtr->GetDirection());
this->SetInputCoordinateOrientation(inputOrientation.GetAsOrientation());
using PermuteFilterType = PermuteAxesImageFilter<ImageType>;
using FlipFilterType = FlipImageFilter<ImageType>;
typename PermuteFilterType::Pointer permute = PermuteFilterType::New();
typename FlipFilterType::Pointer flip = FlipFilterType::New();
permute->SetInput(inputPtr);
permute->SetOrder(m_PermuteOrder);
flip->SetInput(permute->GetOutput());
flip->SetFlipAxes(m_FlipAxes);
flip->FlipAboutOriginOff();
flip->GraftOutput(this->GetOutput());
flip->UpdateOutputInformation();
outputPtr->CopyInformation(flip->GetOutput());
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::VerifyPreconditions() ITKv5_CONST
{
Superclass::VerifyPreconditions();
if (this->m_DesiredCoordinateOrientation == OrientationEnum::INVALID)
{
itkExceptionMacro(<< "DesiredCoordinateOrientation is 'INVALID'.");
}
}
template <typename TInputImage>
void
DICOMOrientImageFilter<TInputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Desired Coordinate Orientation: " << static_cast<long>(this->GetDesiredCoordinateOrientation())
<< " (" << this->GetDesiredCoordinateOrientation() << ")" << std::endl;
os << indent << "Input Coordinate Orientation: " << static_cast<long>(this->GetInputCoordinateOrientation()) << " ("
<< this->GetInputCoordinateOrientation() << ")" << std::endl;
os << indent << "Permute Axes: " << m_PermuteOrder << std::endl;
os << indent << "Flip Axes: " << m_FlipAxes << std::endl;
}
} // end namespace itk
#endif