forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkImageGaussianSmooth.cxx
463 lines (410 loc) · 15.3 KB
/
vtkImageGaussianSmooth.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageGaussianSmooth.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 "vtkImageGaussianSmooth.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <math.h>
vtkStandardNewMacro(vtkImageGaussianSmooth);
//----------------------------------------------------------------------------
vtkImageGaussianSmooth::vtkImageGaussianSmooth()
{
this->Dimensionality = 3; // note: this overrides Standard deviation.
this->StandardDeviations[0] = 2.0;
this->StandardDeviations[1] = 2.0;
this->StandardDeviations[2] = 2.0;
this->RadiusFactors[0] = 1.5;
this->RadiusFactors[1] = 1.5;
this->RadiusFactors[2] = 1.5;
}
//----------------------------------------------------------------------------
vtkImageGaussianSmooth::~vtkImageGaussianSmooth()
{
}
//----------------------------------------------------------------------------
void vtkImageGaussianSmooth::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
// int idx;
//os << indent << "BoundaryRescale: " << this->BoundaryRescale << "\n";
os << indent << "Dimensionality: " << this->Dimensionality << "\n";
os << indent << "RadiusFactors: ( "
<< this->RadiusFactors[0] << ", "
<< this->RadiusFactors[1] << ", "
<< this->RadiusFactors[2] << " )\n";
os << indent << "StandardDeviations: ( "
<< this->StandardDeviations[0] << ", "
<< this->StandardDeviations[1] << ", "
<< this->StandardDeviations[2] << " )\n";
}
//----------------------------------------------------------------------------
void vtkImageGaussianSmooth::ComputeKernel(double *kernel, int min, int max,
double std)
{
int x;
double sum;
// handle special case
if (std == 0.0)
{
kernel[0] = 1.0;
return;
}
// fill in kernel
sum = 0.0;
for (x = min; x <= max; ++x)
{
sum += kernel[x-min] =
exp(- (static_cast<double>(x*x)) / (std * std * 2.0));
}
// normalize
for (x = min; x <= max; ++x)
{
kernel[x-min] /= sum;
}
}
//----------------------------------------------------------------------------
int vtkImageGaussianSmooth::RequestUpdateExtent (
vtkInformation * vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
int wholeExtent[6], inExt[6];
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inExt);
// Expand filtered axes
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
this->InternalRequestUpdateExtent(inExt, wholeExtent);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inExt, 6);
return 1;
}
//----------------------------------------------------------------------------
void vtkImageGaussianSmooth::InternalRequestUpdateExtent(int *inExt,
int *wholeExtent)
{
int idx, radius;
// Expand filtered axes
for (idx = 0; idx < this->Dimensionality; ++idx)
{
radius = static_cast<int>(this->StandardDeviations[idx]
* this->RadiusFactors[idx]);
inExt[idx*2] -= radius;
if (inExt[idx*2] < wholeExtent[idx*2])
{
inExt[idx*2] = wholeExtent[idx*2];
}
inExt[idx*2+1] += radius;
if (inExt[idx*2+1] > wholeExtent[idx*2+1])
{
inExt[idx*2+1] = wholeExtent[idx*2+1];
}
}
}
//----------------------------------------------------------------------------
// For a given position along the convolution axis, this method loops over
// all other axes, and performs the convolution. Boundary conditions handled
// previously.
template <class T>
void
vtkImageGaussianSmoothExecute(vtkImageGaussianSmooth *self, int axis,
double *kernel, int kernelSize,
vtkImageData *inData, T *inPtrC,
vtkImageData *outData, int outExt[6],
T *outPtrC, int *pcycle, int target,
int *pcount, int total)
{
int maxC, max0 = 0, max1 = 0;
int idxC, idx0, idx1, idxK;
vtkIdType *inIncs, *outIncs;
vtkIdType inInc0 = 0, inInc1 = 0, inIncK, outInc0 = 0, outInc1 = 0;
T *outPtr1, *outPtr0;
T *inPtr1, *inPtr0, *inPtrK;
double *ptrK, sum;
// I am counting on the fact that tight loops (component on outside)
// is more important than cache misses from shuffled access.
// Do the correct shuffling of the axes (increments, extents)
inIncs = inData->GetIncrements();
outIncs = outData->GetIncrements();
inIncK = inIncs[axis];
maxC = outData->GetNumberOfScalarComponents();
switch (axis)
{
case 0:
inInc0 = inIncs[1]; inInc1 = inIncs[2];
outInc0 = outIncs[1]; outInc1 = outIncs[2];
max0 = outExt[3] - outExt[2] + 1; max1 = outExt[5] - outExt[4] + 1;
break;
case 1:
inInc0 = inIncs[0]; inInc1 = inIncs[2];
outInc0 = outIncs[0]; outInc1 = outIncs[2];
max0 = outExt[1] - outExt[0] + 1; max1 = outExt[5] - outExt[4] + 1;
break;
case 2:
inInc0 = inIncs[0]; inInc1 = inIncs[1];
outInc0 = outIncs[0]; outInc1 = outIncs[1];
max0 = outExt[1] - outExt[0] + 1; max1 = outExt[3] - outExt[2] + 1;
break;
}
for (idxC = 0; idxC < maxC; ++idxC)
{
inPtr1 = inPtrC;
outPtr1 = outPtrC;
for (idx1 = 0; !self->AbortExecute && idx1 < max1; ++idx1)
{
inPtr0 = inPtr1;
outPtr0 = outPtr1;
for (idx0 = 0; idx0 < max0; ++idx0)
{
inPtrK = inPtr0;
ptrK = kernel;
sum = 0.0;
// too bad this short loop has to be the inner most loop
for (idxK = 0; idxK < kernelSize; ++idxK)
{
sum += *ptrK * static_cast<double>(*inPtrK);
++ptrK;
inPtrK += inIncK;
}
*outPtr0 = static_cast<T>(sum);
inPtr0 += inInc0;
outPtr0 += outInc0;
}
inPtr1 += inInc1;
outPtr1 += outInc1;
// we finished a row ... do we update ???
if (total)
{ // yes this is the main thread
*pcycle += max0;
if (*pcycle > target)
{ // yes
*pcycle -= target;
*pcount += target;
self->UpdateProgress(static_cast<double>(*pcount) /
static_cast<double>(total));
//fprintf(stderr, "count: %d, total: %d, progress: %f\n",
//*pcount, total, (double)(*pcount) / (double)total);
}
}
}
++inPtrC;
++outPtrC;
}
}
//----------------------------------------------------------------------------
template <class T>
size_t vtkImageGaussianSmoothGetTypeSize(T*)
{
return sizeof(T);
}
//----------------------------------------------------------------------------
// This method convolves over one axis. It loops over the convolved axis,
// and handles boundary conditions.
void vtkImageGaussianSmooth::ExecuteAxis(int axis,
vtkImageData *inData, int inExt[6],
vtkImageData *outData, int outExt[6],
int *pcycle, int target,
int *pcount, int total,
vtkInformation *inInfo)
{
int idxA, max;
int wholeExtent[6], wholeMax, wholeMin;
double *kernel;
// previousClip and currentClip rembers that the previous was not clipped
// keeps from recomputing kernels for center pixels.
int kernelSize = 0;
int kernelLeftClip, kernelRightClip;
int previousClipped, currentClipped;
int radius, size;
void *inPtr;
void *outPtr;
int coords[3];
vtkIdType *outIncs, outIncA;
// Get the correct starting pointer of the output
outPtr = outData->GetScalarPointerForExtent(outExt);
outIncs = outData->GetIncrements();
outIncA = outIncs[axis];
// trick to account for the scalar type of the output(used to be only float)
switch (outData->GetScalarType())
{
vtkTemplateMacro(
outIncA *= vtkImageGaussianSmoothGetTypeSize(static_cast<VTK_TT*>(0))
);
default:
vtkErrorMacro("Unknown scalar type");
return;
}
// Determine default starting position of input
coords[0] = inExt[0];
coords[1] = inExt[2];
coords[2] = inExt[4];
// get whole extent for boundary checking ...
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
wholeMin = wholeExtent[axis*2];
wholeMax = wholeExtent[axis*2+1];
// allocate memory for the kernel
radius = static_cast<int>(this->StandardDeviations[axis]
* this->RadiusFactors[axis]);
size = 2*radius + 1;
kernel = new double[size];
// loop over the convolution axis
previousClipped = currentClipped = 1;
max = outExt[axis*2+1];
for (idxA = outExt[axis*2]; idxA <= max; ++idxA)
{
// left boundary condition
coords[axis] = idxA - radius;
kernelLeftClip = wholeMin - coords[axis];
if (kernelLeftClip > 0)
{ // front of kernel is cut off ("kernelStart" samples)
coords[axis] += kernelLeftClip;
}
else
{
kernelLeftClip = 0;
}
// Right boundary condition
kernelRightClip = (idxA + radius) - wholeMax;
if (kernelRightClip < 0)
{
kernelRightClip = 0;
}
// We can only use previous kernel if it is not clipped and new
// kernel is also not clipped.
currentClipped = kernelLeftClip + kernelRightClip;
if (currentClipped || previousClipped)
{
this->ComputeKernel(kernel, -radius+kernelLeftClip,
radius-kernelRightClip,
static_cast<double>(this->StandardDeviations[axis]));
kernelSize = size - kernelLeftClip - kernelRightClip;
}
previousClipped = currentClipped;
/* now do the convolution on the rest of the axes */
inPtr = inData->GetScalarPointer(coords);
switch (inData->GetScalarType())
{
vtkTemplateMacro(
vtkImageGaussianSmoothExecute(this, axis, kernel, kernelSize,
inData, static_cast<VTK_TT*>(inPtr),
outData, outExt,
static_cast<VTK_TT*>(outPtr),
pcycle, target, pcount, total)
);
default:
vtkErrorMacro("Unknown scalar type");
return;
}
outPtr = static_cast<void *>(
static_cast<unsigned char *>(outPtr) + outIncA);
}
// get rid of temporary kernel
delete [] kernel;
}
//----------------------------------------------------------------------------
// This method decomposes the gaussian and smooths along each axis.
void vtkImageGaussianSmooth::ThreadedRequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector,
vtkImageData ***inData,
vtkImageData **outData,
int outExt[6], int id)
{
int inExt[6];
int target, count, total, cycle;
// for feed back, determine line target to get 50 progress update
// update is called every target lines. Progress is computed from
// the number of pixels processed so far.
count = 0; target = 0; total = 0; cycle = 0;
if (id == 0)
{
// determine the number of pixels.
total = this->Dimensionality * (outExt[1] - outExt[0] + 1)
* (outExt[3] - outExt[2] + 1) * (outExt[5] - outExt[4] + 1)
* inData[0][0]->GetNumberOfScalarComponents();
// pixels per update (50 updates)
target = total / 50;
}
// this filter expects that input is the same type as output.
if (inData[0][0]->GetScalarType() != outData[0]->GetScalarType())
{
vtkErrorMacro("Execute: input ScalarType, "
<< inData[0][0]->GetScalarType()
<< ", must match out ScalarType "
<< outData[0]->GetScalarType());
return;
}
// Decompose
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int wholeExt[6];
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExt);
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inExt);
this->InternalRequestUpdateExtent(inExt, wholeExt);
switch (this->Dimensionality)
{
case 1:
this->ExecuteAxis(0, inData[0][0], inExt, outData[0], outExt,
&cycle, target, &count, total, inInfo);
break;
case 2:
int tempExt[6];
vtkImageData *tempData;
// compute intermediate extent
tempExt[0] = inExt[0]; tempExt[1] = inExt[1];
tempExt[2] = outExt[2]; tempExt[3] = outExt[3];
tempExt[4] = inExt[4]; tempExt[5] = inExt[5];
// create a temp data for intermediate results
tempData = vtkImageData::New();
tempData->SetExtent(tempExt);
tempData->AllocateScalars(inData[0][0]->GetScalarType(),
inData[0][0]->GetNumberOfScalarComponents());
this->ExecuteAxis(1, inData[0][0], inExt, tempData, tempExt,
&cycle, target, &count, total, inInfo);
this->ExecuteAxis(0, tempData, tempExt, outData[0], outExt,
&cycle, target, &count, total, inInfo);
// release temporary data
tempData->Delete();
break;
case 3:
// we do z first because it is most likely smallest
int temp0Ext[6], temp1Ext[6];
vtkImageData *temp0Data, *temp1Data;
// compute intermediate extents
temp0Ext[0] = inExt[0]; temp0Ext[1] = inExt[1];
temp0Ext[2] = inExt[2]; temp0Ext[3] = inExt[3];
temp0Ext[4] = outExt[4]; temp0Ext[5] = outExt[5];
temp1Ext[0] = inExt[0]; temp1Ext[1] = inExt[1];
temp1Ext[2] = outExt[2]; temp1Ext[3] = outExt[3];
temp1Ext[4] = outExt[4]; temp1Ext[5] = outExt[5];
// create a temp data for intermediate results
temp0Data = vtkImageData::New();
temp0Data->SetExtent(temp0Ext);
temp0Data->AllocateScalars(inData[0][0]->GetScalarType(),
inData[0][0]->GetNumberOfScalarComponents());
temp1Data = vtkImageData::New();
temp1Data->SetExtent(temp1Ext);
temp1Data->AllocateScalars(inData[0][0]->GetScalarType(),
inData[0][0]->GetNumberOfScalarComponents());
this->ExecuteAxis(2, inData[0][0], inExt, temp0Data, temp0Ext,
&cycle, target, &count, total, inInfo);
this->ExecuteAxis(1, temp0Data, temp0Ext, temp1Data, temp1Ext,
&cycle, target, &count, total, inInfo);
temp0Data->Delete();
this->ExecuteAxis(0, temp1Data, temp1Ext, outData[0], outExt,
&cycle, target, &count, total, inInfo);
temp1Data->Delete();
break;
}
}