forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 26
/
vtkImageResample.cxx
173 lines (144 loc) · 5.1 KB
/
vtkImageResample.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageResample.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImageResample.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkStandardNewMacro(vtkImageResample);
//----------------------------------------------------------------------------
// Constructor: Sets default filter to be identity.
vtkImageResample::vtkImageResample()
{
this->MagnificationFactors[0] = 1.0;
this->MagnificationFactors[1] = 1.0;
this->MagnificationFactors[2] = 1.0;
this->OutputSpacing[0] = 0.0; // not specified
this->OutputSpacing[1] = 0.0; // not specified
this->OutputSpacing[2] = 0.0; // not specified
this->InterpolationMode = VTK_RESLICE_LINEAR;
this->Dimensionality = 3;
}
//----------------------------------------------------------------------------
void vtkImageResample::SetAxisOutputSpacing(int axis, double spacing)
{
if (axis < 0 || axis > 2)
{
vtkErrorMacro("Bad axis: " << axis);
return;
}
if (this->OutputSpacing[axis] != spacing)
{
this->OutputSpacing[axis] = spacing;
this->Modified();
if (spacing != 0.0)
{
// Delay computing the magnification factor.
// Input might not be set yet.
this->MagnificationFactors[axis] = 0.0; // Not computed yet.
}
}
}
//----------------------------------------------------------------------------
void vtkImageResample::SetAxisMagnificationFactor(int axis, double factor)
{
if (axis < 0 || axis > 2)
{
vtkErrorMacro("Bad axis: " << axis);
return;
}
if (this->MagnificationFactors[axis] == factor)
{
return;
}
this->Modified();
this->MagnificationFactors[axis] = factor;
// Spacing is no longer valid.
this->OutputSpacing[axis] = 0.0; // Not computed yet.
}
//----------------------------------------------------------------------------
double vtkImageResample::GetAxisMagnificationFactor(int axis,
vtkInformation *inInfo)
{
if (axis < 0 || axis > 2)
{
vtkErrorMacro("Bad axis: " << axis);
return 0.0;
}
if (this->MagnificationFactors[axis] == 0.0)
{
double *inputSpacing;
if ( ! this->GetInput())
{
vtkErrorMacro("GetMagnificationFactor: Input not set.");
return 0.0;
}
this->GetInput()->UpdateInformation();
if (!inInfo)
{
inInfo = this->GetExecutive()->GetInputInformation(0, 0);
}
inputSpacing = inInfo->Get(vtkDataObject::SPACING());
this->MagnificationFactors[axis] =
inputSpacing[axis] / this->OutputSpacing[axis];
}
vtkDebugMacro("Returning magnification factor "
<< this->MagnificationFactors[axis] << " for axis "
<< axis);
return this->MagnificationFactors[axis];
}
//----------------------------------------------------------------------------
// Computes any global image information associated with regions.
int vtkImageResample::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
int wholeMin, wholeMax, axis, ext[6];
double spacing[3], factor;
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext);
inInfo->Get(vtkDataObject::SPACING(), spacing);
for (axis = 0; axis < 3; axis++)
{
wholeMin = ext[axis*2];
wholeMax = ext[axis*2+1];
// Scale the output extent
factor = 1.0;
if (axis < this->Dimensionality)
{
factor = this->GetAxisMagnificationFactor(axis, inInfo);
}
wholeMin = static_cast<int>(ceil(static_cast<double>(wholeMin) * factor));
wholeMax = static_cast<int>(floor(static_cast<double>(wholeMax) * factor));
// Change the data spacing
spacing[axis] /= factor;
ext[axis*2] = wholeMin;
ext[axis*2+1] = wholeMax;
// just in case the input spacing has changed.
if (this->OutputSpacing[axis] != 0.0)
{
// Cause MagnificationFactor to recompute.
this->MagnificationFactors[axis] = 0.0;
}
}
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext, 6);
outInfo->Set(vtkDataObject::SPACING(), spacing, 3);
return 1;
}
void vtkImageResample::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Dimensionality: " << this->Dimensionality << "\n";
os << indent << "Interpolate: " << (this->GetInterpolate() ? "On\n" : "Off\n");
}