/
vtkImageDataLIC2D.cxx
618 lines (529 loc) · 19.3 KB
/
vtkImageDataLIC2D.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageDataLIC2D.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 "vtkImageDataLIC2D.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkImageNoiseSource.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkDataTransferHelper.h"
#include "vtkImageDataLIC2DExtentTranslator.h"
#include "vtkLineIntegralConvolution2D.h"
#include "vtkStructuredExtent.h"
#include "vtkTextureObject.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLExtensionManager.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStructuredData.h"
#include "vtkFrameBufferObject.h"
#include "vtkgl.h"
#define PRINTEXTENT(ext) \
ext[0] << ", " << ext[1] << ", " << ext[2] << ", " << ext[3] << ", " << ext[4] << ", " << ext[5]
vtkStandardNewMacro(vtkImageDataLIC2D);
//----------------------------------------------------------------------------
vtkImageDataLIC2D::vtkImageDataLIC2D()
{
this->Context = 0;
this->Steps = 20;
this->StepSize = 1.0;
this->FBOSuccess = 0;
this->LICSuccess = 0;
this->Magnification = 1;
this->NoiseSource = vtkImageNoiseSource::New();
this->NoiseSource->SetWholeExtent(0, 127, 0, 127, 0, 0);
this->NoiseSource->SetMinimum(0.0);
this->NoiseSource->SetMaximum(1.0);
this->SetNumberOfInputPorts(2);
// by default process active point vectors
this->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
vtkDataSetAttributes::VECTORS);
this->OwnWindow = false;
this->OpenGLExtensionsSupported = 0;
this->ARBColorBufferFloatSupported = false;
}
//----------------------------------------------------------------------------
vtkImageDataLIC2D::~vtkImageDataLIC2D()
{
this->NoiseSource->Delete();
this->SetContext( NULL );
}
//----------------------------------------------------------------------------
int vtkImageDataLIC2D::SetContext( vtkRenderWindow * context )
{
if ( this->Context == context )
{
return this->OpenGLExtensionsSupported;
}
if ( this->Context && this->OwnWindow )
{
this->Context->Delete();
this->Context = NULL;
}
this->OwnWindow = false;
vtkOpenGLRenderWindow * openGLRenWin =
vtkOpenGLRenderWindow::SafeDownCast( context );
this->Context = openGLRenWin;
if ( openGLRenWin )
{
openGLRenWin->Render();
openGLRenWin->MakeCurrent();
vtkOpenGLExtensionManager * mgr = openGLRenWin->GetExtensionManager();
// optional for texture objects.
mgr->LoadSupportedExtension( "GL_EXT_texture_integer" );
//this->ARBColorBufferFloatSupported =
// mgr->LoadSupportedExtension("GL_ARB_color_buffer_float");
if ( !mgr->LoadSupportedExtension( "GL_VERSION_1_3" ) ||
!mgr->LoadSupportedExtension( "GL_VERSION_1_2" ) ||
!mgr->LoadSupportedExtension( "GL_VERSION_2_0" ) ||
!mgr->LoadSupportedExtension( "GL_ARB_texture_float" ) ||
!mgr->LoadSupportedExtension( "GL_ARB_texture_non_power_of_two" )
)
{
vtkErrorMacro( "Required OpenGL extensions not supported." );
mgr = NULL;
this->Context = 0;
openGLRenWin = NULL;
return 0;
}
mgr = NULL;
}
openGLRenWin = NULL;
this->Modified();
this->OpenGLExtensionsSupported = 1;
return 1;
}
//----------------------------------------------------------------------------
vtkRenderWindow* vtkImageDataLIC2D::GetContext()
{
return this->Context;
}
//----------------------------------------------------------------------------
// Description:
// Fill the input port information objects for this algorithm. This
// is invoked by the first call to GetInputPortInformation for each
// port so subclasses can specify what they can handle.
// Redefined from the superclass.
int vtkImageDataLIC2D::FillInputPortInformation(int port,
vtkInformation *info)
{
if (!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
if (port==1)
{
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
}
return 1;
}
//----------------------------------------------------------------------------
void vtkImageDataLIC2D::TranslateInputExtent(const int* inExt,
const int* inWholeExtent, int *resultExtent)
{
for (int axis = 0; axis < 3; axis++)
{
int whole_dim = (inWholeExtent[axis*2+1] - inWholeExtent[axis*2]) + 1;
int dim = (inExt[axis*2+1] - inExt[axis*2]) + 1;
int min = inExt[axis*2];
int max = inExt[axis*2+1];
// Scale the output extent
min = min * this->Magnification;
max = min + (whole_dim>1? (dim * this->Magnification - 1) : 0);
resultExtent[axis*2] = min;
resultExtent[axis*2+1] = max;
}
}
//----------------------------------------------------------------------------
// We need to report output extent after taking into consideration the
// magnification.
int vtkImageDataLIC2D::RequestInformation(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
int ext[6];
int wholeExtent[6];
double spacing[3];
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
inInfo->Get(vtkDataObject::SPACING(), spacing);
vtkDebugMacro( << "Input WHOLE_EXTENT: " << PRINTEXTENT( wholeExtent ) << endl );
this->TranslateInputExtent(wholeExtent, wholeExtent, ext);
for (int axis = 0; axis < 3; axis++)
{
// Change the data spacing
spacing[axis] /= this->Magnification;
}
vtkDebugMacro( << "WHOLE_EXTENT: " << PRINTEXTENT( ext ) << endl );
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext, 6);
outInfo->Set(vtkDataObject::SPACING(), spacing, 3);
vtkStreamingDemandDrivenPipeline* sddp =
vtkStreamingDemandDrivenPipeline::SafeDownCast(this->GetExecutive());
// Setup ExtentTranslator
vtkImageDataLIC2DExtentTranslator* extTranslator =
vtkImageDataLIC2DExtentTranslator::SafeDownCast(
sddp->GetExtentTranslator(outInfo));
if (!extTranslator)
{
extTranslator = vtkImageDataLIC2DExtentTranslator::New();
sddp->SetExtentTranslator(outInfo, extTranslator);
extTranslator->Delete();
}
extTranslator->SetAlgorithm(this);
extTranslator->SetInputWholeExtent(wholeExtent);
extTranslator->SetInputExtentTranslator(
vtkExtentTranslator::SafeDownCast(
inInfo->Get(vtkStreamingDemandDrivenPipeline::EXTENT_TRANSLATOR())));
return 1;
}
//----------------------------------------------------------------------------
int vtkImageDataLIC2D::RequestUpdateExtent (
vtkInformation * vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// Tell the vector field input the extents that we need from it.
// The downstream request needs to be downsized based on the Magnification.
int ext[6];
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext);
vtkDebugMacro( << "Requested UPDATE_EXTENT: " << PRINTEXTENT( ext ) << endl );
for (int axis = 0; axis < 3; axis++)
{
int wholeMin = ext[axis*2];
int wholeMax = ext[axis*2+1];
// Scale the output extent
wholeMin = wholeMin / this->Magnification;
wholeMax = wholeMax / this->Magnification;
ext[axis*2] = wholeMin;
ext[axis*2+1] = wholeMax;
}
vtkDebugMacro( << "UPDATE_EXTENT: " << PRINTEXTENT( ext ) << endl );
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext, 6);
inInfo = inputVector[1]->GetInformationObject(0);
if (inInfo)
{
// always request the whole noise image.
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),
6);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageDataLIC2D::RequestData(
vtkInformation * vtkNotUsed(request),
vtkInformationVector ** inputVector,
vtkInformationVector * outputVector )
{
vtkInformation * inInfo= inputVector[0]->GetInformationObject(0);
vtkImageData * input = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
// Check if the input image is a 2D image (not 0D, not 1D, not 3D)
int dims[3];
input->GetDimensions(dims);
int dataDescription = vtkStructuredData::GetDataDescription(dims);
if ( vtkStructuredData::GetDataDimension( dataDescription ) != 2 )
{
vtkErrorMacro( "Input is not a 2D image." );
input = NULL;
inInfo = NULL;
return 0;
}
vtkIdType numPoints = input->GetNumberOfPoints();
vtkSmartPointer<vtkDataArray> inVectors =
this->GetInputArrayToProcess( 0, inputVector );
if ( inVectors.GetPointer() == 0 )
{
vtkErrorMacro( "No input vectors selected. "
"Vectors are required for line integral convolution." );
input = NULL;
inInfo = NULL;
return 0;
}
if ( inVectors->GetNumberOfTuples() != numPoints )
{
vtkErrorMacro( "Only point vectors are supported." );
input = NULL;
inInfo = NULL;
return 0;
}
if ( !this->Context )
{
vtkRenderWindow * renWin = vtkRenderWindow::New();
if ( this->SetContext( renWin ) == 0 )
{
renWin->Delete();
renWin = NULL;
input = NULL;
inInfo = NULL;
return 0;
}
renWin = NULL; // will be released via this->Context
this->OwnWindow = true;
}
this->Context->MakeCurrent();
this->Context->SetReportGraphicErrors(1);
// Noise.
vtkInformation *noiseInfo = inputVector[1]->GetInformationObject(0);
vtkImageData *noise=0;
if (noiseInfo==0)
{
this->NoiseSource->Update();
noise = this->NoiseSource->GetOutput();
}
else
{
noise = vtkImageData::SafeDownCast(
noiseInfo->Get(vtkDataObject::DATA_OBJECT()));
if (noise->GetPointData()==0)
{
vtkErrorMacro("Provided noise does not have point data.");
return 0;
}
if (noise->GetPointData()->GetScalars()==0)
{
vtkErrorMacro("Provided noise does not have scalars on point data.");
return 0;
}
}
int firstComponent;
int secondComponent;
switch (dataDescription)
{
case VTK_XY_PLANE:
firstComponent = 0;
secondComponent = 1;
break;
case VTK_YZ_PLANE:
firstComponent = 1;
secondComponent = 2;
break;
case VTK_XZ_PLANE:
firstComponent = 0;
secondComponent = 2;
break;
default:
vtkErrorMacro("Sanity check failed. This filter can only handle 2D inputs.");
return 0;
}
double *spacing = input->GetSpacing();
// this->StepSize is specified as a unit of the cell length, while
// vtkLineIntegralConvolution2D expects step size in normalized image space,
// hence we do the conversion.
double cellLength = sqrt(spacing[firstComponent] * spacing[firstComponent] +
spacing[secondComponent] * spacing[secondComponent]);
double w = spacing[firstComponent] * dims[firstComponent];
double h = spacing[secondComponent] * dims[secondComponent];
double normalizationFactor = sqrt(w*w + h*h);
double stepSize = this->StepSize * cellLength / normalizationFactor;
vtkDebugMacro( << "** StepSize (Normalized Image Space): " << stepSize << endl );
vtkLineIntegralConvolution2D * internal=vtkLineIntegralConvolution2D::New();
if ( !internal->IsSupported( this->Context ) )
{
internal->Delete();
internal = NULL;
input = NULL;
noise = NULL;
inInfo = NULL;
spacing = NULL;
noiseInfo = NULL;
this->LICSuccess = 0;
return 0;
}
internal->SetNumberOfSteps(this->Steps);
internal->SetLICStepSize(stepSize);
internal->SetComponentIds(firstComponent,secondComponent);
internal->SetGridSpacings(spacing[firstComponent], spacing[secondComponent]);
internal->SetMagnification(this->Magnification);
vtkDataTransferHelper *vectorBus = vtkDataTransferHelper::New();
vectorBus->SetContext(this->Context);
vectorBus->SetCPUExtent(input->GetExtent());
vectorBus->SetGPUExtent(input->GetExtent());
// vectorBus->SetTextureExtent(input->GetExtent());
vectorBus->SetArray(inVectors);
vtkDataTransferHelper *noiseBus = vtkDataTransferHelper::New();
noiseBus->SetContext(this->Context);
noiseBus->SetCPUExtent(noise->GetExtent());
noiseBus->SetGPUExtent(noise->GetExtent());
// noiseBus->SetTextureExtent(noise->GetExtent());
noiseBus->SetArray(noise->GetPointData()->GetScalars());
// For uploading of float textures without clamping, we create a FBO with a
// float color buffer.
// Vector field in image space.
vtkTextureObject *tempBuffer = vtkTextureObject::New();
tempBuffer->SetContext(this->Context);
tempBuffer->Create2D(128, 128, 3, VTK_FLOAT, false);
vtkFrameBufferObject *fbo = vtkFrameBufferObject::New();
fbo->SetContext(this->Context);
fbo->SetColorBuffer(0, tempBuffer);
fbo->SetNumberOfRenderTargets(1);
fbo->SetActiveBuffer(0);
if ( !fbo->Start( 128, 128, false ) )
{
fbo->Delete();
internal->Delete();
noiseBus->Delete();
vectorBus->Delete();
tempBuffer->Delete();
fbo = NULL;
internal = NULL;
noiseBus = NULL;
vectorBus = NULL;
tempBuffer = NULL;
input = NULL;
noise = NULL;
inInfo = NULL;
spacing = NULL;
noiseInfo = NULL;
this->FBOSuccess = 0;
return 0;
}
this->FBOSuccess = 1;
tempBuffer->Delete();
tempBuffer = NULL;
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
vectorBus->Upload(0,0);
vectorBus->GetTexture()->Bind();
glTexParameteri(vectorBus->GetTexture()->GetTarget(),GL_TEXTURE_WRAP_S,
GL_CLAMP);
glTexParameteri(vectorBus->GetTexture()->GetTarget(),GL_TEXTURE_WRAP_T,
GL_CLAMP);
glTexParameteri(vectorBus->GetTexture()->GetTarget(), vtkgl::TEXTURE_WRAP_R,
GL_CLAMP);
glTexParameteri(vectorBus->GetTexture()->GetTarget(), GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
glTexParameteri(vectorBus->GetTexture()->GetTarget(), GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
internal->SetVectorField(vectorBus->GetTexture());
vtkgl::ActiveTexture(vtkgl::TEXTURE1);
noiseBus->Upload(0,0);
noiseBus->GetTexture()->Bind();
glTexParameteri(noiseBus->GetTexture()->GetTarget(),GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameteri(noiseBus->GetTexture()->GetTarget(),GL_TEXTURE_WRAP_T,
GL_REPEAT);
glTexParameteri(noiseBus->GetTexture()->GetTarget(),vtkgl::TEXTURE_WRAP_R,
GL_REPEAT);
glTexParameteri(noiseBus->GetTexture()->GetTarget(),GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(noiseBus->GetTexture()->GetTarget(),GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
internal->SetNoise(noiseBus->GetTexture());
fbo->Delete();
fbo = NULL;
int inputRequestedExtent[6];
inInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inputRequestedExtent);
// tranform inputRequestedExtent relative to the input's actual extent since the
// vtkLineIntegralConvolution2D needs extents relative to the input vector
// field.
vtkStructuredExtent::Transform(inputRequestedExtent, input->GetExtent());
int licextent[4];
switch (dataDescription)
{
case VTK_XY_PLANE:
licextent[0] = inputRequestedExtent[0];
licextent[1] = inputRequestedExtent[1];
licextent[2] = inputRequestedExtent[2];
licextent[3] = inputRequestedExtent[3];
break;
case VTK_YZ_PLANE:
licextent[0] = inputRequestedExtent[2];
licextent[1] = inputRequestedExtent[3];
licextent[2] = inputRequestedExtent[4];
licextent[3] = inputRequestedExtent[5];
break;
case VTK_XZ_PLANE:
licextent[0] = inputRequestedExtent[0];
licextent[1] = inputRequestedExtent[1];
licextent[2] = inputRequestedExtent[4];
licextent[3] = inputRequestedExtent[5];
break;
}
if ( internal->Execute(licextent) == 0 )
{
internal->Delete();
noiseBus->Delete();
vectorBus->Delete();
internal = NULL;
noiseBus = NULL;
vectorBus = NULL;
input = NULL;
noise = NULL;
inInfo = NULL;
spacing = NULL;
noiseInfo = NULL;
this->LICSuccess = 0;
return 0;
}
this->LICSuccess = 1;
glFlush(); // breakpoint for debugging.
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkImageData *output = vtkImageData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataTransferHelper *outputBus=vtkDataTransferHelper::New();
outputBus->SetContext(this->Context);
vtkTextureObject* licTexture = internal->GetLIC();
int gpuExtent[6];
inInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), gpuExtent);
gpuExtent[0] *= this->Magnification;
gpuExtent[2] *= this->Magnification;
gpuExtent[4] *= this->Magnification;
switch (dataDescription)
{
case VTK_XY_PLANE:
gpuExtent[1] = gpuExtent[0] + licTexture->GetWidth() - 1;
gpuExtent[3] = gpuExtent[2] + licTexture->GetHeight() - 1;
break;
case VTK_YZ_PLANE:
gpuExtent[3] = gpuExtent[2] + licTexture->GetWidth() - 1;
gpuExtent[5] = gpuExtent[4] + licTexture->GetHeight() -1;
break;
case VTK_XZ_PLANE:
gpuExtent[1] = gpuExtent[0] + licTexture->GetWidth() -1;
gpuExtent[5] = gpuExtent[4] + licTexture->GetHeight() - 1;
break;
}
vtkDebugMacro( << "GPU Extent: " << PRINTEXTENT( gpuExtent ) << endl );
// It is possible that GPU extent is larger than what the output expected,
// hence we allocate the output using the GPU extent and then crop it.
output->SetExtent(gpuExtent);
output->SetNumberOfScalarComponents(3);
output->AllocateScalars();
outputBus->SetCPUExtent(gpuExtent);
outputBus->SetGPUExtent(gpuExtent);
outputBus->SetTexture(internal->GetLIC());
outputBus->SetArray(output->GetPointData()->GetScalars());
output->GetPointData()->GetScalars()->SetName("LIC");
outputBus->Download();
outputBus->Delete();
internal->Delete();
vectorBus->Delete();
noiseBus->Delete();
// Ensures that the output extent is exactly same as what was asked for.
output->Crop();
return 1;
}
//----------------------------------------------------------------------------
void vtkImageDataLIC2D::PrintSelf( ostream & os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "Steps: " << this->Steps << "\n";
os << indent << "StepSize: " << this->StepSize << "\n";
os << indent << "FBOSuccess: " << this->FBOSuccess << "\n";
os << indent << "LICSuccess: " << this->LICSuccess << "\n";
os << indent << "Magnification: " << this->Magnification << "\n";
os << indent << "OpenGLExtensionsSupported: "
<< this->OpenGLExtensionsSupported << "\n";
}