-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSmoothDataArray.cxx
416 lines (341 loc) · 12.5 KB
/
vtkSmoothDataArray.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
/*=========================================================================
Name: vtkSmoothDataArray.cxx
Author: David Borland, The Renaissance Computing Institute (RENCI)
Copyright: The Renaissance Computing Institute (RENCI)
License: Licensed under the RENCI Open Source Software License v. 1.0.
See included License.txt or
http://www.renci.org/resources/open-source-software-license
for details.
=========================================================================*/
#include "vtkSmoothDataArray.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkFieldData.h"
#include "vtkGenericCell.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
//-----------------------------------------------------------------------------
vtkCxxRevisionMacro(vtkSmoothDataArray, "$Revision: 1.0 $");
vtkStandardNewMacro(vtkSmoothDataArray);
template<class data_type>
void vtkSmoothDataArrayDoComputePoints(vtkDataSet *structure,
data_type *smoothed,
data_type *temp,
int anisotropic,
double coefficient);
template<class data_type>
void vtkSmoothDataArrayDoComputeCells(vtkDataSet *structure,
data_type *smoothed,
data_type *temp,
int anisotropic,
double coefficient);
//-----------------------------------------------------------------------------
vtkSmoothDataArray::vtkSmoothDataArray()
{
this->ResultArrayName = NULL;
this->NumberOfIterations = 100;
this->AnisotropicDiffusion = 0;
this->ConductionConstant = 0.05;
this->SetInputScalars(vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS,
vtkDataSetAttributes::SCALARS);
}
vtkSmoothDataArray::~vtkSmoothDataArray()
{
this->SetResultArrayName(NULL);
}
//-----------------------------------------------------------------------------
void vtkSmoothDataArray::SetInputScalars(int fieldAssociation, const char *name)
{
if ( (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_POINTS)
&& (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_CELLS)
&& (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS) )
{
vtkErrorMacro(<<"Input scalars must be associated with points or cells.");
return;
}
this->SetInputArrayToProcess(0, 0, 0, fieldAssociation, name);
}
void vtkSmoothDataArray::SetInputScalars(int fieldAssociation, int fieldAttributeType)
{
if ( (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_POINTS)
&& (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_CELLS)
&& (fieldAssociation != vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS) )
{
vtkErrorMacro(<<"Input scalars must be associated with points or cells.");
return;
}
this->SetInputArrayToProcess(0, 0, 0, fieldAssociation, fieldAttributeType);
}
//-----------------------------------------------------------------------------
static int vtkSmoothDataArrayHasArray(vtkFieldData *fieldData,
vtkDataArray *array)
{
int numarrays = fieldData->GetNumberOfArrays();
for (int i = 0; i < numarrays; i++)
{
if (array == fieldData->GetArray(i))
{
return 1;
}
}
return 0;
}
static void vtkSmoothDataArrayComputeCentroid(vtkCell* cell, double centroid[3])
{
vtkPoints* points = cell->GetPoints();
int numPoints = points->GetNumberOfPoints();
centroid[0] = centroid[1] = centroid[2] = 0.0;
for (int j = 0; j < numPoints; j++)
{
double* p = points->GetPoint(j);
centroid[0] += p[0];
centroid[1] += p[1];
centroid[2] += p[2];
}
centroid[0] /= numPoints;
centroid[1] /= numPoints;
centroid[2] /= numPoints;
}
//-----------------------------------------------------------------------------
int vtkSmoothDataArray::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// Get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// Get the input and ouptut
vtkDataSet *input
= vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output
= vtkDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataArray *scalars = this->GetInputArrayToProcess(0, inputVector);
vtkDebugMacro(<<"Smoothing data array");
// Check scalar data
if (scalars == NULL)
{
vtkErrorMacro(<<"No input scalars.");
return 0;
}
if (scalars->GetNumberOfComponents() != 1)
{
vtkErrorMacro(<<"Input scalars must have one component.");
return 0;
}
// Set which type of scalar data
int fieldAssociation;
if (vtkSmoothDataArrayHasArray(input->GetPointData(), scalars))
{
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_POINTS;
}
else if (vtkSmoothDataArrayHasArray(input->GetCellData(), scalars))
{
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_CELLS;
}
else
{
vtkErrorMacro("Input scalars do not seem to be either point or cell scalars.");
return 0;
}
// Copy input data to output
output->CopyStructure(input);
output->GetPointData()->PassData(input->GetPointData());
output->GetCellData()->PassData(input->GetCellData());
// Create the output data array
vtkDataArray* smoothed = vtkDataArray::CreateDataArray(scalars->GetDataType());
smoothed->SetNumberOfComponents(1);
smoothed->SetNumberOfTuples(scalars->GetNumberOfTuples());
smoothed->DeepCopy(scalars);
char* name;
if (this->ResultArrayName)
{
int nameLength = strlen(this->ResultArrayName);
name = new char[nameLength];
sprintf_s(name, nameLength, "%s", this->ResultArrayName);
}
else
{
int nameLength = strlen(scalars->GetName()) + strlen(" Smoothed") + 1;
name = new char[nameLength];
sprintf_s(name, nameLength, "%s%s", scalars->GetName(), " Smoothed");
}
smoothed->SetName(name);
// Create a temporary data array for calculations
vtkDataArray* temp = vtkDataArray::CreateDataArray(scalars->GetDataType());
temp->SetNumberOfComponents(1);
temp->SetNumberOfTuples(scalars->GetNumberOfTuples());
if (fieldAssociation == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
for (int i = 0; i < this->NumberOfIterations; i++)
{
switch(scalars->GetDataType())
{
vtkTemplateMacro(vtkSmoothDataArrayDoComputePoints(
input,
static_cast<VTK_TT *>(smoothed->GetVoidPointer(0)),
static_cast<VTK_TT *>(temp->GetVoidPointer(0)),
this->AnisotropicDiffusion,
this->ConductionConstant));
}
}
output->GetPointData()->AddArray(smoothed);
output->GetPointData()->SetActiveScalars(name);
}
else
{
for (int i = 0; i < this->NumberOfIterations; i++)
{
switch(scalars->GetDataType())
{
vtkTemplateMacro(vtkSmoothDataArrayDoComputeCells(
input,
static_cast<VTK_TT *>(smoothed->GetVoidPointer(0)),
static_cast<VTK_TT *>(temp->GetVoidPointer(0)),
this->AnisotropicDiffusion,
this->ConductionConstant));
}
}
output->GetCellData()->AddArray(smoothed);
output->GetCellData()->SetActiveScalars(name);
}
delete [] name;
temp->Delete();
return 1;
}
//-----------------------------------------------------------------------------
template<class data_type>
void vtkSmoothDataArrayDoComputePoints(vtkDataSet *structure,
data_type *smoothed,
data_type *temp,
int anisotropic,
double coefficient)
{
vtkIdType numberOfCells = structure->GetNumberOfCells();
vtkIdType numberOfPoints = structure->GetNumberOfPoints();
double* weight = new double[numberOfPoints];
vtkGenericCell* cell = vtkGenericCell::New();
// Initialize temp array
for (int i = 0; i < numberOfPoints; i++)
{
temp[i] = 0.0;
weight[i] = 0.0;
}
// Compute the gradients along each edge
for (int i = 0; i < numberOfCells; i++)
{
// Get the edges for this cell
structure->GetCell(i, cell);
int numEdges = cell->GetNumberOfEdges();
for (int j = 0; j < numEdges; j++)
{
// Get the edge
vtkCell* edge = cell->GetEdge(j);
// Get the two points defining this edge
int id1 = edge->GetPointId(0);
int id2 = edge->GetPointId(1);
// Compute the distance between the two points
double p1[3];
double p2[3];
structure->GetPoint(id1, p1);
structure->GetPoint(id2, p2);
double d = sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
// Perform smoothing based on gradient
double K = coefficient;
double g = smoothed[id2] - smoothed[id1];
double c = anisotropic ? 1.0 / (1.0 + (g / K) * (g / K)) : 1.0;
double s = (g * c) / d;
temp[id1] += s;
temp[id2] -= s;
weight[id1] += 1.0 / d;
weight[id2] += 1.0 / d;
}
}
// Normalize and add to current
for (int i = 0; i < numberOfPoints; i++)
{
temp[i] = smoothed[i] + temp[i];// / weight[i];
smoothed[i] = temp[i];
}
// Clean up
delete [] weight;
cell->Delete();
}
template<class data_type>
void vtkSmoothDataArrayDoComputeCells(vtkDataSet *structure,
data_type *smoothed,
data_type *temp,
int anisotropic,
double coefficient)
{
vtkIdType numberOfCells = structure->GetNumberOfCells();
double* weight = new double[numberOfCells];
vtkGenericCell* cell = vtkGenericCell::New();
vtkGenericCell* neighbor = vtkGenericCell::New();
vtkIdList* cellIds = vtkIdList::New();
// Initialize temp array
for (int i = 0; i < numberOfCells; i++)
{
temp[i] = 0.0;
weight[i] = 0;
}
// Compute the gradients along each edge
for (int i = 0; i < numberOfCells; i++)
{
structure->GetCell(i, cell);
// Compute the centroid of this cell
double p1[3];
vtkSmoothDataArrayComputeCentroid(cell, p1);
// Loop over the edges for this cell
int numEdges = cell->GetNumberOfEdges();
for (int j = 0; j < numEdges; j++)
{
vtkCell* edge = cell->GetEdge(j);
// Get the cells that share this edge
structure->GetCellNeighbors(i, edge->GetPointIds(), cellIds);
int numCellNeighbors = cellIds->GetNumberOfIds();
for (int k = 0; k < numCellNeighbors; k++)
{
vtkIdType id = cellIds->GetId(k);
structure->GetCell(id, neighbor);
// Compute the centroid of this neighbor cell
double p2[3];
vtkSmoothDataArrayComputeCentroid(neighbor, p2);
// Compute the distance between the two cells
double d = sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
// Perform smoothing based on gradient
double K = coefficient;
double g = smoothed[id] - smoothed[i];
double c = anisotropic ? 1.0 / (1.0 + (g / K) * (g / K)) : 1.0;
temp[i] += (g * c) / d;
weight[i] += 1.0 / d;
}
}
}
// Normalize and add to current
for (int i = 0; i < numberOfCells; i++)
{
temp[i] = smoothed[i] + temp[i] / weight[i];
smoothed[i] = temp[i];
}
// Clean up
delete [] weight;
cell->Delete();
neighbor->Delete();
cellIds->Delete();
}
//-----------------------------------------------------------------------------
void vtkSmoothDataArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "ResultArrayName: " << this->ResultArrayName << "\n";
os << indent << "NumberOfIterations: " << this->NumberOfIterations << "\n";
os << indent << "AnisotropicDiffusion: " << this->AnisotropicDiffusion << "\n";
os << indent << "ConductionConstant: " << this->ConductionConstant << "\n";
}