-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkPProjectedTetrahedraMapper.cxx
356 lines (311 loc) · 11.5 KB
/
vtkPProjectedTetrahedraMapper.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
#include "vtkPProjectedTetrahedraMapper.h"
#include "vtkArrayDispatch.h"
#include "vtkCellArray.h"
#include "vtkPCellCenterDepthSort.h"//
#include "vtkCellData.h"
#include "vtkColorTransferFunction.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkGarbageCollector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPointData.h"
#include "vtkTimerLog.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPVisibilitySort.h"//
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"
#include <cmath>
#include <algorithm>
//-----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkPProjectedTetrahedraMapper,
VisibilitySort, vtkPVisibilitySort);
//-----------------------------------------------------------------------------
// Return NULL if no override is supplied.
vtkAbstractObjectFactoryNewMacro(vtkPProjectedTetrahedraMapper)
//-----------------------------------------------------------------------------
vtkPProjectedTetrahedraMapper::vtkPProjectedTetrahedraMapper()
{
this->VisibilitySort = vtkPCellCenterDepthSort::New();
}
vtkPProjectedTetrahedraMapper::~vtkPProjectedTetrahedraMapper()
{
this->SetVisibilitySort(NULL);
}
void vtkPProjectedTetrahedraMapper::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PVisibilitySort: " << this->VisibilitySort << endl;
}
//-----------------------------------------------------------------------------
void vtkPProjectedTetrahedraMapper::ReportReferences(vtkGarbageCollector *collector)
{
this->Superclass::ReportReferences(collector);
vtkGarbageCollectorReport(collector, this->VisibilitySort, "PVisibilitySort");
}
//-----------------------------------------------------------------------------
namespace {
struct TransformPointsWorker
{
const float *Proj;
const float *ModelView;
float *OutPoints;
TransformPointsWorker(const float *proj, const float *mv, float *out)
: Proj(proj), ModelView(mv), OutPoints(out)
{}
template<class ArrayT>
void operator()(ArrayT *in_points)
{
float mat[16];
int row, col;
vtkIdType i;
vtkIdType num_points = in_points->GetNumberOfTuples();
typename ArrayT::ValueType in_p[3];
float *out_p;
// Combine two transforms into one transform.
for (col = 0; col < 4; col++)
{
for (row = 0; row < 4; row++)
{
mat[col*4+row] = ( this->Proj[0*4+row] * this->ModelView[col*4+0]
+ this->Proj[1*4+row] * this->ModelView[col*4+1]
+ this->Proj[2*4+row] * this->ModelView[col*4+2]
+ this->Proj[3*4+row] * this->ModelView[col*4+3]);
}
}
// Transform all points.
for (i = 0, out_p = this->OutPoints; i < num_points; i++, out_p += 3)
{
in_points->GetTypedTuple(i, in_p);
for (row = 0; row < 3; row++)
{
out_p[row] = ( mat[0*4+row] * in_p[0] + mat[1*4+row] * in_p[1]
+ mat[2*4+row] * in_p[2] + mat[3*4+row]);
}
}
// Check to see if we need to divide by w.
if ( (mat[0*4+3] != 0) || (mat[1*4+3] != 0)
|| (mat[2*4+3] != 0) || (mat[3*4+3] != 1) )
{
for (i = 0, out_p = this->OutPoints; i < num_points; i++, out_p += 3)
{
in_points->GetTypedTuple(i, in_p);
float w = ( mat[0*4+3]*in_p[0] + mat[1*4+3]*in_p[1]
+ mat[2*4+3]*in_p[2] + mat[3*4+3]);
if (w > 0.0)
{
out_p[0] /= w;
out_p[1] /= w;
out_p[2] /= w;
}
else
{
// A negative w probably means the point is behind the viewer. Things
// can get screwy if we try to inverse-project that. Instead, just
// set the position somewhere very far behind us.
out_p[2] = -VTK_FLOAT_MAX;
}
}
}
}
};
} // end anon namespace
//-----------------------------------------------------------------------------
void vtkPProjectedTetrahedraMapper::TransformPoints(
vtkPoints *inPoints,
const float projection_mat[16],
const float modelview_mat[16],
vtkFloatArray *outPoints)
{
if (!inPoints)
{
return;
}
outPoints->SetNumberOfComponents(3);
outPoints->SetNumberOfTuples(inPoints->GetNumberOfPoints());
TransformPointsWorker worker(projection_mat, modelview_mat,
outPoints->GetPointer(0));
vtkArrayDispatch::Dispatch::Execute(inPoints->GetData(), worker);
}
//-----------------------------------------------------------------------------
namespace vtkPProjectedTetrahedraMapperNamespace
{
template <typename ColorArrayT, typename ScalarArrayT>
void MapScalarsToColorsImpl(ColorArrayT *colors, vtkVolumeProperty *property,
ScalarArrayT *scalars);
template <typename ColorArrayT, typename ScalarArrayT>
void MapIndependentComponents(ColorArrayT *colors,
vtkVolumeProperty *property,
ScalarArrayT *scalars);
template <typename ColorArrayT, typename ScalarArrayT>
void Map2DependentComponents(ColorArrayT *colors, vtkVolumeProperty *property,
ScalarArrayT *scalars);
template <typename ColorArrayT, typename ScalarArrayT>
void Map4DependentComponents(ColorArrayT *colors, ScalarArrayT *scalars);
struct Worker
{
vtkVolumeProperty *Property;
Worker(vtkVolumeProperty *property) : Property(property) {}
template <typename ColorArrayT, typename ScalarArrayT>
void operator()(ColorArrayT *colors, ScalarArrayT *scalars)
{
MapScalarsToColorsImpl(colors, this->Property, scalars);
}
};
}
void vtkPProjectedTetrahedraMapper::MapScalarsToColors(
vtkDataArray *colors,
vtkVolumeProperty *property,
vtkDataArray *scalars)
{
using namespace vtkPProjectedTetrahedraMapperNamespace;
vtkDataArray *tmpColors;
int castColors;
if ( (colors->GetDataType() == VTK_UNSIGNED_CHAR)
&& (( (scalars->GetDataType() != VTK_UNSIGNED_CHAR)
|| (property->GetIndependentComponents()) )
|| ((!property->GetIndependentComponents())
&& (scalars->GetNumberOfComponents() == 2))) )
{
// Special case. Need to convert from range [0,1] to [0,255].
tmpColors = vtkDoubleArray::New();
castColors = 1;
}
else
{
tmpColors = colors;
castColors = 0;
}
vtkIdType numscalars = scalars->GetNumberOfTuples();
tmpColors->Initialize();
tmpColors->SetNumberOfComponents(4);
tmpColors->SetNumberOfTuples(numscalars);
Worker worker(property);
if (!vtkArrayDispatch::Dispatch2::Execute(tmpColors, scalars, worker))
{
vtkGenericWarningMacro("Dispatch failed for scalar array "
<< scalars->GetName());
}
if (castColors)
{
// Special case. Need to convert from range [0,1] to [0,255].
colors->Initialize();
colors->SetNumberOfComponents(4);
colors->SetNumberOfTuples(scalars->GetNumberOfTuples());
unsigned char *c
= static_cast<vtkUnsignedCharArray *>(colors)->GetPointer(0);
for (vtkIdType i = 0; i < numscalars; i++, c+= 4)
{
double *dc = tmpColors->GetTuple(i);
c[0] = static_cast<unsigned char>(dc[0]*255.9999);
c[1] = static_cast<unsigned char>(dc[1]*255.9999);
c[2] = static_cast<unsigned char>(dc[2]*255.9999);
c[3] = static_cast<unsigned char>(dc[3]*255.9999);
}
tmpColors->Delete();
}
}
//-----------------------------------------------------------------------------
namespace vtkPProjectedTetrahedraMapperNamespace
{
template <typename ColorArrayT, typename ScalarArrayT>
void MapScalarsToColorsImpl(ColorArrayT *colors, vtkVolumeProperty *property,
ScalarArrayT *scalars)
{
if (property->GetIndependentComponents())
{
MapIndependentComponents(colors, property, scalars);
}
else
{
switch (scalars->GetNumberOfComponents())
{
case 2:
Map2DependentComponents(colors, property, scalars);
break;
case 4:
Map4DependentComponents(colors, scalars);
break;
default:
vtkGenericWarningMacro("Attempted to map scalar with "
<< scalars->GetNumberOfComponents()
<< " with dependent components");
break;
}
}
}
template <typename ColorArrayT, typename ScalarArrayT>
void MapIndependentComponents(ColorArrayT *colors,
vtkVolumeProperty *property,
ScalarArrayT *scalars)
{
// I don't really know what to do if there is more than one component.
// How am I supposed to mix the resulting colors? Since I don't know
// what to do, and the whole thing seems kinda pointless anyway, I'm just
// going to punt and copy over the first scalar.
vtkIdType i;
vtkIdType num_scalars = scalars->GetNumberOfTuples();
typedef typename ScalarArrayT::ValueType ScalarType;
typedef typename ColorArrayT::ValueType ColorType;
ColorType c[4];
if (property->GetColorChannels() == 1)
{
vtkPiecewiseFunction *gray = property->GetGrayTransferFunction();
vtkPiecewiseFunction *alpha = property->GetScalarOpacity();
for (i = 0; i < num_scalars; i++)
{
ScalarType s = scalars->GetTypedComponent(i, 0);
c[0] = c[1] = c[2] = static_cast<ColorType>(gray->GetValue(s));
c[3] = static_cast<ColorType>(alpha->GetValue(s));
colors->SetTypedTuple(i, c);
}
}
else
{
vtkColorTransferFunction *rgb = property->GetRGBTransferFunction();
vtkPiecewiseFunction *alpha = property->GetScalarOpacity();
for (i = 0; i < num_scalars; i++)
{
ScalarType s = scalars->GetTypedComponent(i, 0);
double trgb[3];
rgb->GetColor(s, trgb);
c[0] = static_cast<ColorType>(trgb[0]);
c[1] = static_cast<ColorType>(trgb[1]);
c[2] = static_cast<ColorType>(trgb[2]);
c[3] = static_cast<ColorType>(alpha->GetValue(s));
colors->SetTypedTuple(i, c);
}
}
}
template <typename ColorArrayT, typename ScalarArrayT>
void Map2DependentComponents(ColorArrayT *colors, vtkVolumeProperty *property,
ScalarArrayT *scalars)
{
typedef typename ScalarArrayT::ValueType ScalarType;
vtkColorTransferFunction *rgb = property->GetRGBTransferFunction();
vtkPiecewiseFunction *alpha = property->GetScalarOpacity();
vtkIdType num_scalars = scalars->GetNumberOfTuples();
double rgbColor[4];
ScalarType scalar[2];
for (vtkIdType i = 0; i < num_scalars; i++)
{
scalars->GetTypedTuple(i, scalar);
rgb->GetColor(scalar[0], rgbColor);
rgbColor[3] = alpha->GetValue(scalar[1]);
colors->SetTuple(i, rgbColor);
}
}
template <typename ColorArrayT, typename ScalarArrayT>
void Map4DependentComponents(ColorArrayT *colors, ScalarArrayT *scalars)
{
double val[4];
vtkIdType num_scalars = scalars->GetNumberOfTuples();
for (vtkIdType i = 0; i < num_scalars; i++)
{
scalars->GetTuple(i, val);
colors->SetTuple(i, val);
}
}
}