-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtkCenterOfMassFilter.cxx
266 lines (248 loc) · 8.49 KB
/
vtkCenterOfMassFilter.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
/*=========================================================================
Program: AstroViz plugin for ParaView
Module: $RCSfile: vtkCenterOfMassFilter.cxx,v $
=========================================================================*/
#include "vtkCenterOfMassFilter.h"
#include "vtkPolyData.h"
#include "vtkDataSetAttributes.h"
#include "vtkPointData.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"
#include "vtkCellArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkMultiProcessController.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"
//----------------------------------------------------------------------------
vtkCxxRevisionMacro(vtkCenterOfMassFilter, "$Revision: 1.72 $");
vtkStandardNewMacro(vtkCenterOfMassFilter);
vtkCxxSetObjectMacro(vtkCenterOfMassFilter,Controller, vtkMultiProcessController);
//----------------------------------------------------------------------------
vtkCenterOfMassFilter::vtkCenterOfMassFilter()
{
this->UpdatePiece = 0;
this->UpdateNumPieces = 0;
this->SetInputArrayToProcess(
0,
0,
0,
vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS,
vtkDataSetAttributes::SCALARS);
this->MassArray = NULL;
this->Controller = NULL;
this->SetController(vtkMultiProcessController::GetGlobalController());
}
//----------------------------------------------------------------------------
vtkCenterOfMassFilter::~vtkCenterOfMassFilter()
{
this->SetController(0);
}
//----------------------------------------------------------------------------
void vtkCenterOfMassFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
int vtkCenterOfMassFilter::FillInputPortInformation(int, vtkInformation* info)
{
// This filter uses the vtkDataSet cell traversal methods so it
// supports any vtkPointSet type as input.
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
return 1;
}
//----------------------------------------------------------------------------
int vtkCenterOfMassFilter::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
// now add our info
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
return 1;
}
//----------------------------------------------------------------------------
void vtkCenterOfMassFilter::ComputeCenterOfMassFinal(
double &totalMass, double totalWeightedMass[], double *result)
{
// calculating the result
if(totalMass!=0)
{
for(int i = 0; i < 3; ++i)
{
result[i] = totalWeightedMass[i]/totalMass;
}
}
else
{
vtkErrorMacro("total mass is zero, cannot calculate center of mass, setting center to 0,0,0");
for(int i = 0; i < 3; ++i)
{
result[i] = 0;
}
}
}
//----------------------------------------------------------------------------
template <typename T>
void WeightedSum(T* data, double mass, double *weightedsum) {
for (int i=0; i<3; ++i)
{
weightedsum[i] += data[i]*mass;
}
}
//----------------------------------------------------------------------------
void vtkCenterOfMassFilter::UpdateCenterOfMassVariables(
double &totalMass, double totalWeightedMass[])
{
vtkIdType index = 0;
for (vtkIdType id=0; id<this->NumberOfPoints; ++id)
{
double mass=this->MassArray->GetTuple1(id);
if (this->dPointData) {
WeightedSum<double>(&this->dPointData[index], mass, totalWeightedMass);
}
else {
WeightedSum<float>(&this->fPointData[index], mass, totalWeightedMass);
}
totalMass += mass;
index += 3;
}
}
//----------------------------------------------------------------------------
double* vtkCenterOfMassFilter::ComputeWeightedMass(double& mass,double* point)
{
double* weightedMass = new double[3];
for(int i = 0; i < 3; ++i)
{
weightedMass[i]=mass*point[i];
}
return weightedMass;
}
//----------------------------------------------------------------------------
bool vtkCenterOfMassFilter::ComputeCenterOfMass(
vtkPoints *points, vtkDataArray *mass, double COM[3])
{
//
// Setup input data pointers
//
this->fPointData = NULL;
this->dPointData = NULL;
this->MassArray = mass;
//
if (vtkFloatArray::SafeDownCast(points->GetData())) {
this->fPointData = vtkFloatArray::SafeDownCast(points->GetData())->GetPointer(0);
}
else {
this->dPointData = vtkDoubleArray::SafeDownCast(points->GetData())->GetPointer(0);
}
this->NumberOfPoints = points->GetNumberOfPoints();
//
// Check parallel operation
//
if (this->Controller) {
this->UpdatePiece = this->Controller->GetLocalProcessId();
this->UpdateNumPieces = this->Controller->GetNumberOfProcesses();
}
else {
this->UpdateNumPieces = 1;
this->UpdatePiece = 0;
}
// Allocating data arrays and setting to zero
double totalMass = 0.0;
double totalWeightedMass[3] = {0,0,0};
// testing to make sure we can get to work with D3
if (this->UpdateNumPieces>1)
{
if (this->UpdatePiece!=0)
{
// We are at non-root process so simply update and move on
// Private variables to aid computation of COM
this->UpdateCenterOfMassVariables(totalMass, totalWeightedMass);
// Sending to root
this->Controller->Send(&totalMass,1,0,TOTAL_MASS);
this->Controller->Send(&totalWeightedMass[0],3,0,TOTAL_WEIGHTED_MASS);
this->MassArray = NULL;
return false;
}
else
{
// We are at root process so update results from root process
this->UpdateCenterOfMassVariables(totalMass, totalWeightedMass);
// Now gather results from each process other than this one
for (int proc = 1; proc < this->UpdateNumPieces; ++proc)
{
double recTotalMass;
double recTotalWeightedMass[3];
// Receiving
this->Controller->Receive(&recTotalMass, 1, proc, TOTAL_MASS);
this->Controller->Receive(&recTotalWeightedMass[0], 3, proc, TOTAL_WEIGHTED_MASS);
// Updating
totalMass += recTotalMass;
for(int i = 0; i < 3; ++i)
{
totalWeightedMass[i] += recTotalWeightedMass[i];
}
}
this->ComputeCenterOfMassFinal(totalMass, totalWeightedMass, COM);
}
}
else
{
// we aren't using MPI or have only one process
this->UpdateCenterOfMassVariables(totalMass, totalWeightedMass);
this->ComputeCenterOfMassFinal(totalMass, totalWeightedMass, COM);
}
this->MassArray = NULL;
return true;
}
//----------------------------------------------------------------------------
int vtkCenterOfMassFilter::RequestData(vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkSmartPointer<vtkTimerLog> timer = vtkSmartPointer<vtkTimerLog>::New();
timer->StartTimer();
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// Get input and output data.
vtkPointSet* input = vtkPointSet::GetData(inputVector[0]);
// Get name of data array containing mass
this->MassArray = this->GetInputArrayToProcess(0, inputVector);
if (!this->MassArray)
{
vtkErrorMacro("Failed to locate mass array");
return 0;
}
// Setup the output
vtkPolyData* output = vtkPolyData::GetData(outputVector);
//
vtkSmartPointer<vtkPoints> newPoints = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
output->SetPoints(newPoints);
output->SetVerts(vertices);
// Compute centre of Mass on local points
double dbCenterOfMass[3] = {0,0,0};
bool ok = this->ComputeCenterOfMass(input->GetPoints(), this->MassArray, dbCenterOfMass);
if (ok)
{
// we are in serial or at process 0
newPoints->SetNumberOfPoints(1);
newPoints->SetPoint(0, dbCenterOfMass);
vtkIdType *cells = vertices->WritePointer(1, 2);
cells[0] = 1;
cells[1] = 0;
}
// Also saving it as a data array for easy csv export
vtkSmartPointer<vtkFloatArray> c_of_m = vtkSmartPointer<vtkFloatArray>::New();
c_of_m->SetName("CentreOfMass");
c_of_m->SetNumberOfComponents(3);
c_of_m->SetNumberOfTuples(1);
c_of_m->SetTuple(0, dbCenterOfMass);
output->GetPointData()->AddArray(c_of_m);
//
timer->StopTimer();
if (this->UpdatePiece==0) {
// vtkErrorMacro(<< "Centre Of Mass Calculation : " << timer->GetElapsedTime() << " seconds\n");
}
return 1;
}