/
vtkVectorDot.cxx
339 lines (295 loc) · 9.77 KB
/
vtkVectorDot.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVectorDot.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 "vtkVectorDot.h"
#include "vtkDataSet.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSMPTools.h"
vtkStandardNewMacro(vtkVectorDot);
// These are created to support a float,double fast path. They work in
// tandem with vtkTemplate2Macro found in vtkSetGet.h.
#define vtkTemplate2MacroFP(call) \
vtkTemplate2MacroCase1FP(VTK_DOUBLE, double, call); \
vtkTemplate2MacroCase1FP(VTK_FLOAT, float, call)
#define vtkTemplate2MacroCase1FP(type1N, type1, call) \
vtkTemplate2MacroCase2(type1N, type1, VTK_DOUBLE, double, call); \
vtkTemplate2MacroCase2(type1N, type1, VTK_FLOAT, float, call)
// The heart of the algorithm plus interface to the SMP tools. Double templated
// over point and scalar types.
template <class TN,class TV>
class vtkVectorDotAlgorithm
{
public:
vtkIdType NumPts;
double Min, Max;
double ScalarRange[2];
const TN *Normals;
const TV *Vectors;
float *Scalars;
// Constructor
vtkVectorDotAlgorithm();
// Interface between VTK and templated functions.
static void Dot(vtkVectorDot *self, vtkIdType numPts, TN *normals,
TV *vectors, float *scalars,
double range[2], double actualRange[2]);
// Interface dot product computation to SMP tools.
template <class T1,class T2> class DotOp
{
public:
vtkVectorDotAlgorithm *Algo;
vtkSMPThreadLocal<double> Min;
vtkSMPThreadLocal<double> Max;
DotOp(vtkVectorDotAlgorithm<T1,T2> *algo) :
Algo(algo), Min(VTK_DOUBLE_MAX), Max(VTK_DOUBLE_MIN) {}
void operator() (vtkIdType k, vtkIdType end)
{
double &min = this->Min.Local();
double &max = this->Max.Local();
const T1 *n = this->Algo->Normals + 3*k;
const T2 *v = this->Algo->Vectors + 3*k;
float *s = this->Algo->Scalars + k;
for ( ; k < end; ++k)
{
*s = n[0]*v[0] + n[1]*v[1] + n[2]*v[2];
min = ( *s < min ? *s : min );
max = ( *s > max ? *s : max );
s++;
n += 3;
v += 3;
}
}
};
// Interface normalize computation to SMP tools.
template <class T1,class T2> class MapOp
{
public:
vtkVectorDotAlgorithm *Algo;
MapOp(vtkVectorDotAlgorithm<T1,T2> *algo)
{ this->Algo = algo; }
void operator() (vtkIdType k, vtkIdType end)
{
const double dR = this->Algo->ScalarRange[1]-this->Algo->ScalarRange[0];
const double srMin = this->Algo->ScalarRange[0];
const double dS = this->Algo->Max - this->Algo->Min;
const double min = this->Algo->Min;
float *s = this->Algo->Scalars + k;
for ( ; k < end; ++k)
{
*s = srMin + ((*s - min)/dS)*dR;
s++;
}
}
};
};
//----------------------------------------------------------------------------
// Initialized mainly to eliminate compiler warnings.
template <class TN,class TV> vtkVectorDotAlgorithm<TN,TV>::
vtkVectorDotAlgorithm():Normals(nullptr),Vectors(nullptr),Scalars(nullptr)
{
this->NumPts = 0;
this->Min = this->Max = 0.0;
this->ScalarRange[0] = 0.0;
this->ScalarRange[1] = 1.0;
}
//----------------------------------------------------------------------------
// Templated class is glue between VTK and templated algorithms.
template <class TN,class TV> void vtkVectorDotAlgorithm<TN,TV>::
Dot(vtkVectorDot *self, vtkIdType numPts, TN *normals, TV *vectors,
float *scalars, double range[2], double actualRange[2])
{
// Populate data into local storage
vtkVectorDotAlgorithm<TN,TV> algo;
algo.NumPts = numPts;
algo.Normals = normals;
algo.Vectors = vectors;
algo.Scalars = scalars;
algo.ScalarRange[0] = range[0];
algo.ScalarRange[1] = range[1];
// Okay now generate samples using SMP tools
DotOp<TN,TV> dot(&algo);
vtkSMPTools::For(0,algo.NumPts, dot);
// Have to roll up the thread local storage and get the overall range
double min = VTK_DOUBLE_MAX;
double max = VTK_DOUBLE_MIN;
vtkSMPThreadLocal<double>::iterator itr;
for ( itr=dot.Min.begin(); itr != dot.Min.end(); ++itr )
{
if ( *itr < min )
{
min = *itr;
}
}
for ( itr=dot.Max.begin(); itr != dot.Max.end(); ++itr )
{
if ( *itr > max )
{
max = *itr;
}
}
// Return the global range
actualRange[0] = algo.Min = min;
actualRange[1] = algo.Max = max;
if ( self->GetMapScalars() )
{
MapOp<TN,TV> mapValues(&algo);
vtkSMPTools::For(0,algo.NumPts, mapValues);
}
}
//=================================Begin class proper=========================
//----------------------------------------------------------------------------
// Construct object with scalar range (-1,1).
vtkVectorDot::vtkVectorDot()
{
this->MapScalars = 1;
this->ScalarRange[0] = -1.0;
this->ScalarRange[1] = 1.0;
this->ActualRange[0] = -1.0;
this->ActualRange[1] = 1.0;
}
//----------------------------------------------------------------------------
// Compute dot product.
//
int vtkVectorDot::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 output
vtkDataSet *input = vtkDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output = vtkDataSet::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType numPts;
vtkFloatArray *newScalars;
vtkDataArray *inNormals;
vtkDataArray *inVectors;
vtkPointData *pd=input->GetPointData(), *outPD=output->GetPointData();
// Initialize
//
vtkDebugMacro(<<"Generating vector/normal dot product!");
// First, copy the input to the output as a starting point
output->CopyStructure( input );
if ( (numPts=input->GetNumberOfPoints()) < 1 )
{
vtkErrorMacro(<< "No points!");
return 1;
}
if ( (inNormals=pd->GetNormals()) == nullptr )
{
vtkErrorMacro(<< "No normals defined!");
return 1;
}
if ( (inVectors=pd->GetVectors()) == nullptr )
{
vtkErrorMacro(<< "No vectors defined!");
return 1;
}
// Allocate
//
newScalars = vtkFloatArray::New();
newScalars->SetNumberOfTuples(numPts);
// This is potentiall a two pass algorithm. The first pass computes the dot
// product and keeps track of min/max scalar values; and the second
// (optional pass) maps the output into a specified range. Passes two and
// three are optional.
//
float *scalars = static_cast<float*>(newScalars->GetVoidPointer(0));
void *normals = inNormals->GetVoidPointer(0);
void *vectors = inVectors->GetVoidPointer(0);
int fastPath=1;
double *range = this->ScalarRange;
double *actualRange = this->ActualRange;
switch (vtkTemplate2PackMacro(inNormals->GetDataType(),
inVectors->GetDataType()))
{
// Double explicit specification of multiple template arguments.
// Supports combinations of float and double.
vtkTemplate2MacroFP((vtkVectorDotAlgorithm<VTK_T1,VTK_T2>::
Dot(this,numPts,(VTK_T1*)normals,(VTK_T2*)vectors,
scalars,range,actualRange)));
default:
// Unknown input or output VTK type id.
fastPath = 0;
break;
}
// If we couldn't use the fast path, then take the scenic route
if ( ! fastPath )
{
// Compute initial scalars
//
int abort=0;
vtkIdType ptId;
double s, n[3], v[3], min, max, dR, dS;
vtkIdType progressInterval=numPts/20 + 1;
for (min=VTK_DOUBLE_MAX,max=(-VTK_DOUBLE_MAX),ptId=0;
ptId < numPts && !abort; ptId++)
{
if ( ! (ptId % progressInterval) )
{
this->UpdateProgress ((double)ptId/numPts);
abort = this->GetAbortExecute();
}
inNormals->GetTuple(ptId, n);
inVectors->GetTuple(ptId, v);
s = vtkMath::Dot(n,v);
if ( s < min )
{
min = s;
}
if ( s > max )
{
max = s;
}
newScalars->SetTuple(ptId,&s);
}
// Map scalars into scalar range
//
if ( (dR=this->ScalarRange[1]-this->ScalarRange[0]) == 0.0 )
{
dR = 1.0;
}
if ( (dS=max-min) == 0.0 )
{
dS = 1.0;
}
for ( ptId=0; ptId < numPts; ptId++ )
{
s = newScalars->GetComponent(ptId,0);
s = ((s - min)/dS) * dR + this->ScalarRange[0];
newScalars->SetTuple(ptId,&s);
}
}
// Update self and release memory
//
outPD->PassData(input->GetPointData());
int idx = outPD->AddArray(newScalars);
outPD->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
newScalars->Delete();
return 1;
}
//----------------------------------------------------------------------------
void vtkVectorDot::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "MapScalars: "
<< (this->MapScalars ? "On\n" : "Off\n");
os << indent << "Scalar Range: (" << this->ScalarRange[0] << ", "
<< this->ScalarRange[1] << ")\n";
os << indent << "Actual Range: (" << this->ActualRange[0] << ", "
<< this->ActualRange[1] << ")\n";
}