-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkOSPRayVolumeRayCastMapper.cxx
351 lines (297 loc) · 10.7 KB
/
vtkOSPRayVolumeRayCastMapper.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOSPRayVolumeRayCastMapper.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 "vtkOSPRayVolumeRayCastMapper.h"
#include "ospray/ospray.h"
#include "vtkCamera.h"
#include "vtkDataArray.h"
#include "vtkEncodedGradientEstimator.h"
#include "vtkEncodedGradientShader.h"
#include "vtkFiniteDifferenceGradientEstimator.h"
#include "vtkGarbageCollector.h"
#include "vtkGraphicsFactory.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkMultiThreader.h"
#include "vtkObjectFactory.h"
#include "vtkPlaneCollection.h"
#include "vtkPointData.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkTimerLog.h"
#include "vtkTransform.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastFunction.h"
#include "vtkRayCastImageDisplayHelper.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkTimerLog.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkInformationDoubleKey.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkOSPRayRenderer.h"
#include "vtkOSPRayManager.h"
#include <math.h>
#include <algorithm>
vtkStandardNewMacro(vtkOSPRayVolumeRayCastMapper);
// Construct a new vtkOSPRayVolumeRayCastMapper with default values
vtkOSPRayVolumeRayCastMapper::vtkOSPRayVolumeRayCastMapper()
{
this->VolumeAdded=false;
this->NumColors = 128;
this->SampleDistance = 1.0;
this->ImageSampleDistance = 1.0;
this->MinimumImageSampleDistance = 1.0;
this->MaximumImageSampleDistance = 10.0;
this->AutoAdjustSampleDistances = 1;
this->ZBuffer = NULL;
this->IntermixIntersectingGeometry = 1;
this->OSPRayManager = vtkOSPRayManager::Singleton();
this->SharedData = false;
if (SharedData)
OSPRayVolume = ospNewVolume("shared_structured_volume");
else
OSPRayVolume = ospNewVolume("block_bricked_volume");
transferFunction = ospNewTransferFunction("piecewise_linear");
ospCommit(transferFunction);
SamplingRate=0.25;
}
// Destruct a vtkOSPRayVolumeRayCastMapper - clean up any memory used
vtkOSPRayVolumeRayCastMapper::~vtkOSPRayVolumeRayCastMapper()
{
}
void vtkOSPRayVolumeRayCastMapper::SetNumberOfThreads( int num )
{
NumberOfThreads = num;
}
int vtkOSPRayVolumeRayCastMapper::GetNumberOfThreads()
{
return NumberOfThreads;
}
void vtkOSPRayVolumeRayCastMapper::ReleaseGraphicsResources(vtkWindow *)
{
}
void vtkOSPRayVolumeRayCastMapper::Render( vtkRenderer *ren, vtkVolume *vol )
{
// make sure that we have scalar input and update the scalar input
if ( this->GetInput() == NULL )
{
vtkErrorMacro(<< "No Input!");
return;
}
else
{
this->GetInputAlgorithm()->UpdateInformation();
// vtkStreamingDemandDrivenPipeline::SetUpdateExtentToWholeExtent(
// this->GetInputInformation());
this->GetInputAlgorithm()->Update();
}
// vol->UpdateTransferFunctions( ren );
//
// OSPRay
//
vtkOSPRayRenderer* OSPRayRenderer =
vtkOSPRayRenderer::SafeDownCast(ren);
if (!OSPRayRenderer)
{
return;
}
OSPRayModel = this->OSPRayManager->OSPRayVolumeModel;
OSPRenderer renderer = this->OSPRayManager->OSPRayVolumeRenderer;
vtkImageData *data = this->GetInput();
vtkDataArray * scalars = this->GetScalars(data, this->ScalarMode,
this->ArrayAccessMode, this->ArrayId, this->ArrayName, this->CellFlag);
void* ScalarDataPointer =
this->GetInput()->GetPointData()->GetScalars()->GetVoidPointer(0);
int ScalarDataType =
this->GetInput()->GetPointData()->GetScalars()->GetDataType();
int dim[3];
data->GetDimensions(dim);
size_t typeSize = 0;
std::string voxelType;
if (ScalarDataType == VTK_FLOAT)
{
typeSize = sizeof(float);
voxelType = "float";
}
else if (ScalarDataType == VTK_UNSIGNED_CHAR)
{
typeSize = sizeof(unsigned char);
voxelType = "uchar";
}
else if (ScalarDataType == VTK_DOUBLE)
{
typeSize = sizeof(double);
voxelType = "double";
}
else
{
std::cerr << "ERROR: Unsupported data type for ospray volumes, current supported data types are: "
<< " float, uchar, double\n";
return;
}
//
// Cache timesteps
//
double timestep=-1;
vtkInformation *inputInfo = this->GetInput()->GetInformation();
if (inputInfo && inputInfo->Has(vtkDataObject::DATA_TIME_STEP()))
{
timestep = inputInfo->Get(vtkDataObject::DATA_TIME_STEP());
}
vtkOSPRayVolumeCacheEntry* cacheEntry = Cache[vol][timestep];
if (!cacheEntry)
{
cacheEntry = new vtkOSPRayVolumeCacheEntry();
if (SharedData)
OSPRayVolume = ospNewVolume("shared_structured_volume");
else
OSPRayVolume = ospNewVolume("block_bricked_volume");
cacheEntry->Volume = OSPRayVolume;
Cache[vol][timestep] = cacheEntry;
//
// Send Volumetric data to OSPRay
//
char* buffer = NULL;
size_t sizeBytes = dim[0]*dim[1]*dim[2] *typeSize;
buffer = (char*)ScalarDataPointer;
ospSet3i(OSPRayVolume, "dimensions", dim[0], dim[1], dim[2]);
double origin[3];
vol->GetOrigin(origin);
double *bds = data->GetBounds();
origin[0] = bds[0];
origin[1] = bds[2];
origin[2] = bds[4];
double spacing[3];
data->GetSpacing(spacing);
ospSet3f(OSPRayVolume, "gridOrigin", origin[0], origin[1], origin[2]);
ospSet3f(OSPRayVolume, "gridSpacing", spacing[0],spacing[1],spacing[2]);
ospSetString(OSPRayVolume, "voxelType", voxelType.c_str());
if (SharedData)
{
OSPData voxelData = ospNewData(sizeBytes, OSP_UCHAR, ScalarDataPointer, OSP_DATA_SHARED_BUFFER);
ospSetData(OSPRayVolume, "voxelData", voxelData);
}
else
{
osp::vec3i ll, uu;
ll.x = 0, ll.y = 0, ll.z = 0;
uu.x = dim[0], uu.y = dim[1], uu.z = dim[2];
ospSetRegion(OSPRayVolume, ScalarDataPointer, ll, uu);
}
}
OSPRayVolume = cacheEntry->Volume;
// test for modifications to volume properties
if (vol->GetProperty()->GetMTime() > PropertyTime)
{
OSPRayRenderer->SetClearAccumFlag();
vtkVolumeProperty* volProperty = vol->GetProperty();
vtkColorTransferFunction* colorTF = volProperty->GetRGBTransferFunction(0);
vtkPiecewiseFunction *scalarTF = volProperty->GetScalarOpacity(0);
int numNodes = colorTF->GetSize();
double* tfData = colorTF->GetDataPointer();
TFVals.resize(NumColors*3);
TFOVals.resize(NumColors);
scalarTF->GetTable(data->GetScalarRange()[0],data->GetScalarRange()[1], NumColors, &TFOVals[0]);
colorTF->GetTable(data->GetScalarRange()[0],data->GetScalarRange()[1], NumColors, &TFVals[0]);
OSPData colorData = ospNewData(NumColors, OSP_FLOAT3, &TFVals[0]);// TODO: memory leak? does ospray manage this>
ospSetData(transferFunction, "colors", colorData);
OSPData tfAlphaData = ospNewData(NumColors, OSP_FLOAT, &TFOVals[0]);
ospSetData(transferFunction, "opacities", tfAlphaData);
ospCommit(transferFunction);
ospSet1i(OSPRayVolume, "gradientShadingEnabled", volProperty->GetShade());
PropertyTime.Modified();
}
// test for modifications to input
if (this->GetInput()->GetMTime() > this->BuildTime)
{
std::vector<float> isoValues;
if (this->GetInput()->GetPointData()->GetScalars("ospIsoValues"))
{
int num = this->GetInput()->GetPointData()->GetScalars("ospIsoValues")->GetComponent(0,0);
for( int i=0; i < num; i++)
{
float isoValue = this->GetInput()->GetPointData()->GetScalars("ospIsoValues")->GetComponent(0,i+1);
isoValues.push_back(isoValue);
}
}
if (isoValues.size())
{
OSPData isovaluesData = ospNewData(isoValues.size(), OSP_FLOAT, &isoValues[0]);
ospSetData(OSPRayVolume, "isovalues", isovaluesData);
}
if (this->GetInput()->GetPointData()->GetScalars("ospClipValues"))
{
float clipValue = this->GetInput()->GetPointData()->GetScalars("ospClipValues")->GetComponent(0,0);
int clipAxis = this->GetInput()->GetPointData()->GetScalars("ospClipValues")->GetComponent(0,1);
float uu[3], ll[3];
uu[0] = dim[0], uu[1] = dim[1], uu[2] = dim[2];
ll[0] = 0, ll[0] = 0, ll[0] = 0;
if (clipAxis >= 0 && clipAxis <= 2)
uu[clipAxis] = clipValue;
ospSet3fv(OSPRayVolume, "volumeClippingBoxLower", ll);
ospSet3fv(OSPRayVolume, "volumeClippingBoxUpper", uu);
}
ospSet2f(transferFunction, "valueRange", data->GetScalarRange()[0], data->GetScalarRange()[1]);
//! Commit the transfer function only after the initial colors and alphas have been set (workaround for Qt signalling issue).
ospCommit(transferFunction);
//TODO: manage memory
ospSetObject((OSPObject)OSPRayVolume, "transferFunction", transferFunction);
this->BuildTime.Modified();
}
if (SamplingRate == 0.0f)
{
//automatically determine sampling rate, for now just a simple switch
int maxBound = std::max(dim[0],dim[1]);
maxBound = std::max(maxBound,dim[2]);
if (maxBound < 1000)
{
float s = 1000.0f - maxBound;
s = (s/1000.0f*4.0f + 0.25f);
ospSet1f(OSPRayVolume, "samplingRate", s);
}
else
ospSet1f(OSPRayVolume, "samplingRate", 0.25f);
}
else
ospSet1f(OSPRayVolume, "samplingRate", SamplingRate);
ospCommit(OSPRayVolume);
ospAddVolume(OSPRayModel,(OSPVolume)OSPRayVolume);
// if (!VolumeAdded)
// VolumeAdded = true;
ospCommit(OSPRayModel);
ospSetObject(renderer, "model", OSPRayModel);
ospCommit(renderer);
this->OSPRayManager->OSPRayVolumeModel = OSPRayModel;
OSPRayRenderer->SetHasVolume(true);
}
// Print method for vtkOSPRayVolumeRayCastMapper
void vtkOSPRayVolumeRayCastMapper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Sample Distance: " << this->SampleDistance << "\n";
os << indent << "Image Sample Distance: "
<< this->ImageSampleDistance << "\n";
os << indent << "Minimum Image Sample Distance: "
<< this->MinimumImageSampleDistance << "\n";
os << indent << "Maximum Image Sample Distance: "
<< this->MaximumImageSampleDistance << "\n";
os << indent << "Auto Adjust Sample Distances: "
<< this->AutoAdjustSampleDistances << "\n";
os << indent << "Intermix Intersecting Geometry: "
<< (this->IntermixIntersectingGeometry ? "On\n" : "Off\n");
}
//----------------------------------------------------------------------------
void vtkOSPRayVolumeRayCastMapper::ReportReferences(vtkGarbageCollector* collector)
{
this->Superclass::ReportReferences(collector);
}