vtkImageDataLIC2D.cxx 19.2 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*=========================================================================

  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 "vtkStructuredData.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkFloatArray.h"
#include "vtkUnsignedCharArray.h"
22
#include "vtkImageCast.h"
Ken Martin's avatar
Ken Martin committed
23 24 25 26 27 28 29
#include "vtkImageData.h"
#include "vtkImageNoiseSource.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStructuredExtent.h"
#include "vtkObjectFactory.h"
#include "vtkLineIntegralConvolution2D.h"
30
#include "vtkOpenGLFramebufferObject.h"
Ken Martin's avatar
Ken Martin committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "vtkRenderbuffer.h"
#include "vtkPixelBufferObject.h"
#include "vtkTextureObject.h"
#include "vtkPixelExtent.h"
#include "vtkPixelTransfer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkSmartPointer.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkShaderProgram.h"

#include <deque>
using std::deque;

Ken Martin's avatar
Ken Martin committed
46
#include "vtkOpenGLHelper.h"
Ken Martin's avatar
Ken Martin committed
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
#include "vtkTextureObjectVS.h"

#define vtkImageDataLIC2DDEBUG 0
#if (vtkImageDataLIC2DDEBUG >= 1)
#include "vtkTextureIO.h"
#endif

#define PRINTEXTENT(ext) \
  ext[0] << ", " << ext[1] << ", " << ext[2] << ", " << ext[3] << ", " << ext[4] << ", " << ext[5]

//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkImageDataLIC2D);

//----------------------------------------------------------------------------
vtkImageDataLIC2D::vtkImageDataLIC2D()
{
  this->Context = NULL;
  this->OwnWindow = false;
  this->OpenGLExtensionsSupported = 0;

  this->Steps = 20;
  this->StepSize = 1.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);

76 77 78 79
  this->ImageCast = vtkImageCast::New();
  this->ImageCast->SetOutputScalarTypeToFloat();
  this->ImageCast->SetInputConnection(this->NoiseSource->GetOutputPort(0));

Ken Martin's avatar
Ken Martin committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  this->SetNumberOfInputPorts(2);

  // by default process active point vectors
  this->SetInputArrayToProcess(
        0,
        0,
        0,
        vtkDataObject::FIELD_ASSOCIATION_POINTS,
        vtkDataSetAttributes::VECTORS);
}

//----------------------------------------------------------------------------
vtkImageDataLIC2D::~vtkImageDataLIC2D()
{
  this->NoiseSource->Delete();
95
  this->ImageCast->Delete();
Ken Martin's avatar
Ken Martin committed
96 97 98 99 100 101 102 103 104
  this->SetContext(NULL);
}

//----------------------------------------------------------------------------
int vtkImageDataLIC2D::SetContext(vtkRenderWindow * renWin)
{
  vtkOpenGLRenderWindow *rw = vtkOpenGLRenderWindow::SafeDownCast(renWin);

  if (this->Context == rw)
105
  {
Ken Martin's avatar
Ken Martin committed
106
    return this->OpenGLExtensionsSupported;
107
  }
Ken Martin's avatar
Ken Martin committed
108 109

  if (this->Context && this->OwnWindow)
110
  {
Ken Martin's avatar
Ken Martin committed
111
    this->Context->Delete();
112
  }
Ken Martin's avatar
Ken Martin committed
113 114 115 116 117 118 119
  this->Modified();
  this->Context = NULL;
  this->OwnWindow = false;
  this->OpenGLExtensionsSupported = 0;

  vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
  if (context)
120
  {
Ken Martin's avatar
Ken Martin committed
121 122 123 124 125 126
    context->Render();
    context->MakeCurrent();

    bool featureSupport
      = vtkLineIntegralConvolution2D::IsSupported(context)
      && vtkPixelBufferObject::IsSupported(context)
127
      && vtkOpenGLFramebufferObject::IsSupported(context)
Ken Martin's avatar
Ken Martin committed
128 129 130 131
      && vtkRenderbuffer::IsSupported(context)
      && vtkTextureObject::IsSupported(context);

    if (!featureSupport)
132
    {
Ken Martin's avatar
Ken Martin committed
133 134
      vtkErrorMacro("Required OpenGL extensions not supported.");
      return 0;
135
    }
Ken Martin's avatar
Ken Martin committed
136 137 138

    this->OpenGLExtensionsSupported = 1;
    this->Context = context;
139
  }
Ken Martin's avatar
Ken Martin committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153

  return 1;
}

//----------------------------------------------------------------------------
vtkRenderWindow* vtkImageDataLIC2D::GetContext()
{
  return this->Context;
}

//----------------------------------------------------------------------------
int vtkImageDataLIC2D::FillInputPortInformation(int port, vtkInformation *info)
{
  if (!this->Superclass::FillInputPortInformation(port, info))
154
  {
Ken Martin's avatar
Ken Martin committed
155
    return 0;
156
  }
Ken Martin's avatar
Ken Martin committed
157 158

  if (port == 1)
159
  {
Ken Martin's avatar
Ken Martin committed
160
    info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
161
  }
Ken Martin's avatar
Ken Martin committed
162 163 164 165 166 167 168 169 170 171 172 173

  return 1;
}

//----------------------------------------------------------------------------
void vtkImageDataLIC2D::TranslateInputExtent(
        const int* inExt,
        const int* inWholeExt,
        int *resultExt)
{
  int nPlanar = 0;
  for (int q=0; q<3; ++q)
174
  {
Ken Martin's avatar
Ken Martin committed
175 176
    int qq = 2*q;
    if (inWholeExt[qq] == inWholeExt[qq+1])
177
    {
Ken Martin's avatar
Ken Martin committed
178 179 180
      resultExt[qq] = inExt[qq];
      resultExt[qq+1] = inExt[qq];
      nPlanar += 1;
181
    }
Ken Martin's avatar
Ken Martin committed
182
    else
183
    {
Ken Martin's avatar
Ken Martin committed
184 185 186
      resultExt[qq] = inExt[qq] * this->Magnification;
      resultExt[qq+1] = (inExt[qq+1] + 1) * this->Magnification - 1;
    }
187
  }
Ken Martin's avatar
Ken Martin committed
188
  if (nPlanar != 1)
189
  {
Ken Martin's avatar
Ken Martin committed
190
    vtkErrorMacro("Non-planar dataset");
191
  }
Ken Martin's avatar
Ken Martin committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

//----------------------------------------------------------------------------
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++)
213
  {
Ken Martin's avatar
Ken Martin committed
214 215
    // Change the data spacing
    spacing[axis] /= this->Magnification;
216
  }
Ken Martin's avatar
Ken Martin committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  vtkDebugMacro( << "WHOLE_EXTENT: " << PRINTEXTENT( ext ) << endl );

  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), ext, 6);
  outInfo->Set(vtkDataObject::SPACING(), spacing, 3);

  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++)
241
  {
Ken Martin's avatar
Ken Martin committed
242 243 244 245 246 247 248 249 250
    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;
251
  }
Ken Martin's avatar
Ken Martin committed
252 253 254 255 256 257
  vtkDebugMacro( << "UPDATE_EXTENT: " <<  PRINTEXTENT( ext ) << endl );

  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext, 6);

  inInfo = inputVector[1]->GetInformationObject(0);
  if (inInfo)
258
  {
Ken Martin's avatar
Ken Martin committed
259 260 261 262 263
    // always request the whole noise image.
    inInfo->Set(
        vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
        inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),
        6);
264
  }
Ken Martin's avatar
Ken Martin committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

  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()));
  if ( !input )
280
  {
Ken Martin's avatar
Ken Martin committed
281 282
    vtkErrorMacro("Empty input");
    return 0;
283
  }
Ken Martin's avatar
Ken Martin committed
284 285 286 287 288 289 290

  int dims[3];
  input->GetDimensions(dims);

  int dataDescription = vtkStructuredData::GetDataDescription(dims);

  if (  vtkStructuredData::GetDataDimension( dataDescription ) != 2  )
291
  {
Ken Martin's avatar
Ken Martin committed
292 293
    vtkErrorMacro( "Input is not a 2D image." );
    return 0;
294
  }
Ken Martin's avatar
Ken Martin committed
295 296 297 298

  vtkIdType numPoints = input->GetNumberOfPoints();
  vtkDataArray *inVectors = this->GetInputArrayToProcess(0, inputVector);
  if ( !inVectors )
299
  {
Ken Martin's avatar
Ken Martin committed
300 301
    vtkErrorMacro("Vectors are required for line integral convolution.");
    return 0;
302
  }
Ken Martin's avatar
Ken Martin committed
303 304

  if ( inVectors->GetNumberOfTuples() != numPoints )
305
  {
Ken Martin's avatar
Ken Martin committed
306 307
    vtkErrorMacro( "Only point vectors are supported." );
    return 0;
308
  }
Ken Martin's avatar
Ken Martin committed
309 310

  if ( !this->Context )
311
  {
Ken Martin's avatar
Ken Martin committed
312 313
    vtkRenderWindow * renWin = vtkRenderWindow::New();
    if ( this->SetContext(renWin) == 0 )
314
    {
Ken Martin's avatar
Ken Martin committed
315 316 317 318
      vtkErrorMacro("Missing required OpenGL extensions");
      renWin->Delete();
      return 0;
    }
319 320
    this->OwnWindow = true;
  }
Ken Martin's avatar
Ken Martin committed
321 322 323 324 325 326 327 328

  this->Context->MakeCurrent();
  vtkOpenGLClearErrorMacro();

  // Noise.
  vtkInformation *noiseInfo = inputVector[1]->GetInformationObject(0);
  vtkImageData *noise = NULL;
  if ( noiseInfo )
329
  {
Ken Martin's avatar
Ken Martin committed
330 331 332
    noise
      = vtkImageData::SafeDownCast(noiseInfo->Get(vtkDataObject::DATA_OBJECT()));
    if ( !noise )
333
    {
Ken Martin's avatar
Ken Martin committed
334 335 336
      vtkErrorMacro(
        "Invalid noise dataset on input "
        "Default noise dataset is used");
337
    }
Ken Martin's avatar
Ken Martin committed
338 339 340

    if ( (noise->GetPointData()==0)
      || (noise->GetPointData()->GetScalars()==0) )
341
    {
Ken Martin's avatar
Ken Martin committed
342 343 344 345
      vtkErrorMacro(
        "Noise dataset missing point data scalars. "
        "Default noise dataset is used");
      noise = NULL;
346
    }
Ken Martin's avatar
Ken Martin committed
347 348 349 350 351

    double noiseRange[2];
    vtkDataArray *inVals = noise->GetPointData()->GetScalars();
    inVals->GetRange(noiseRange);
    if ( (noiseRange[0] < 0.0) || (noiseRange[1] > 1.0) )
352
    {
Ken Martin's avatar
Ken Martin committed
353 354 355 356 357
      vtkErrorMacro(
        "Noise dataset has values out of range 0.0 to 1.0."
        "Default noise dataset is used");
      noise = NULL;
    }
358
  }
Ken Martin's avatar
Ken Martin committed
359 360

  if ( !noise )
361
  {
362 363
    this->ImageCast->Update();
    noise = this->ImageCast->GetOutput();
364
  }
Ken Martin's avatar
Ken Martin committed
365 366 367

  int comp[3] = {0, 0, 0};
  switch (dataDescription)
368
  {
Ken Martin's avatar
Ken Martin committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  case VTK_XY_PLANE:
    comp[0] = 0;
    comp[1] = 1;
    comp[2] = 2;
    break;

  case VTK_YZ_PLANE:
    comp[0] = 1;
    comp[1] = 2;
    comp[2] = 0;
    break;

  case VTK_XZ_PLANE:
    comp[0] = 0;
    comp[1] = 2;
    comp[2] = 1;
    break;
386
  }
Ken Martin's avatar
Ken Martin committed
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

  // size of output
  int magDims[3];
  magDims[0] = this->Magnification*dims[0];
  magDims[1] = this->Magnification*dims[1];
  magDims[2] = this->Magnification*dims[2];

  // send vector data to a texture
  int inputExt[6];
  input->GetExtent(inputExt);

  vtkPixelExtent inVectorExtent(dims[comp[0]], dims[comp[1]]);

  vtkPixelBufferObject *vecPBO = vtkPixelBufferObject::New();
  vecPBO->SetContext(this->Context);

  vtkPixelTransfer::Blit(
        inVectorExtent,
        inVectorExtent,
        inVectorExtent,
        inVectorExtent,
        3,
        inVectors->GetDataType(),
        inVectors->GetVoidPointer(0),
        4,
        VTK_FLOAT,
        vecPBO->MapUnpackedBuffer(
        VTK_FLOAT,
        static_cast<unsigned int>(inVectorExtent.Size()),
        4));

  vecPBO->UnmapUnpackedBuffer();

  vtkTextureObject *vectorTex = vtkTextureObject::New();
  vectorTex->SetContext(this->Context);
  vectorTex->Create2D(dims[comp[0]], dims[comp[1]], 4, vecPBO, false);
  vtkLineIntegralConvolution2D::SetVectorTexParameters(vectorTex);

  vecPBO->Delete();

  #if (vtkImageDataLIC2DDEBUG >= 1)
  vtkTextureIO::Write(
              "idlic2d_vectors.vtk",
              vectorTex, NULL, NULL);
  #endif

  // magnify vectors
  vtkPixelExtent magVectorExtent(magDims[comp[0]], magDims[comp[1]]);
  int magVectorSize[2];
  magVectorExtent.Size(magVectorSize);

  vtkTextureObject *magVectorTex = vectorTex;
  if (this->Magnification > 1)
440
  {
Ken Martin's avatar
Ken Martin committed
441 442 443 444 445
    magVectorTex = vtkTextureObject::New();
    magVectorTex->SetContext(this->Context);
    magVectorTex->Create2D(magVectorSize[0], magVectorSize[1], 4, VTK_FLOAT, false);
    vtkLineIntegralConvolution2D::SetVectorTexParameters(magVectorTex);

446
    vtkOpenGLFramebufferObject *drawFbo = vtkOpenGLFramebufferObject::New();
Ken Martin's avatar
Ken Martin committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460
    drawFbo->SetContext(this->Context);
    drawFbo->SaveCurrentBindings();
    drawFbo->Bind(GL_FRAMEBUFFER);
    drawFbo->AddColorAttachment(GL_FRAMEBUFFER, 0U, magVectorTex);
    drawFbo->ActivateDrawBuffer(0U);
    //drawFbo->AddColorAttachment(vtkgl::FRAMEBUFFER_EXT, 0U, vectorTex);
    //drawFbo->ActivateReadBuffer(0U);
    drawFbo->CheckFrameBufferStatus(GL_FRAMEBUFFER);
    drawFbo->InitializeViewport(magVectorSize[0], magVectorSize[1]);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    float tcoords[] = {
461 462 463 464
      0.0f, 0.0f,
      1.0f, 0.0f,
      1.0f, 1.0f,
      0.0f, 1.0f};
Ken Martin's avatar
Ken Martin committed
465 466 467 468 469 470 471

    float verts[] = {
      -1.0f, -1.0f, 0.0f,
      1.0f, -1.0f, 0.0f,
      1.0f, 1.0f, 0.0f,
      -1.0f, 1.0f, 0.0f};

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    vtkOpenGLHelper shaderHelper;

    // build the shader source code
    shaderHelper.Program =
      this->Context->GetShaderCache()->ReadyShaderProgram(
        vtkTextureObjectVS,
        "//VTK::System::Dec\n"
        "varying vec2 tcoordVC;\n"
        "uniform sampler2D source;\n"
        "//VTK::Output::Dec\n"
        "void main(void) {\n"
        "  gl_FragData[0] = texture2D(source,tcoordVC); }\n",
        "");

    // bind and activate this texture
    vectorTex->Activate();
    int sourceId = vectorTex->GetTextureUnit();
    shaderHelper.Program->SetUniformi("source",sourceId);
    vectorTex->CopyToFrameBuffer(tcoords, verts,
      shaderHelper.Program, shaderHelper.VAO);
    vectorTex->Deactivate();
Ken Martin's avatar
Ken Martin committed
493
    vectorTex->Delete();
494
    shaderHelper.ReleaseGraphicsResources(this->Context);
Ken Martin's avatar
Ken Martin committed
495 496 497

    drawFbo->UnBind(GL_FRAMEBUFFER);
    drawFbo->Delete();
498
  }
Ken Martin's avatar
Ken Martin committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512

  #if (vtkImageDataLIC2DDEBUG >= 1)
  vtkTextureIO::Write(
              "idlic2d_magvectors.vtk",
              magVectorTex, NULL, NULL);
  #endif

  // send noise data to a texture
  vtkDataArray * inNoise = noise->GetPointData()->GetScalars();

  vtkPixelExtent noiseExt(noise->GetExtent());

  vtkPixelBufferObject *noisePBO = vtkPixelBufferObject::New();
  noisePBO->SetContext(this->Context);
513 514 515
  int noiseComp = inNoise->GetNumberOfComponents();

  if (inNoise->GetDataType() != VTK_FLOAT)
516
  {
517
    vtkErrorMacro("noise dataset was not float");
518
  }
Ken Martin's avatar
Ken Martin committed
519 520 521

  vtkPixelTransfer::Blit(
        noiseExt,
522
        noiseComp,
Ken Martin's avatar
Ken Martin committed
523 524 525 526 527 528
        inNoise->GetDataType(),
        inNoise->GetVoidPointer(0),
        VTK_FLOAT,
        noisePBO->MapUnpackedBuffer(
        VTK_FLOAT,
        static_cast<unsigned int>(noiseExt.Size()),
529
        noiseComp));
Ken Martin's avatar
Ken Martin committed
530 531 532 533 534 535 536 537

  noisePBO->UnmapUnpackedBuffer();

  int noiseTexSize[2];
  noiseExt.Size(noiseTexSize);

  vtkTextureObject *noiseTex = vtkTextureObject::New();
  noiseTex->SetContext(this->Context);
538 539
  noiseTex->Create2D(noiseTexSize[0], noiseTexSize[1],
    noiseComp, noisePBO, false);
Ken Martin's avatar
Ken Martin committed
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

  noisePBO->Delete();

  #if (vtkImageDataLIC2DDEBUG >= 1)
  vtkTextureIO::Write(
          "idlic2d_noise.vtk",
          noiseTex, NULL, NULL);
  #endif

  // step size conversion to normalize image space
  double *spacing = input->GetSpacing();
  spacing[comp[0]] /= this->Magnification;
  spacing[comp[1]] /= this->Magnification;

  double cellLength
    = sqrt(spacing[comp[0]]*spacing[comp[0]]+spacing[comp[1]]*spacing[comp[1]]);

  double w = spacing[comp[0]]*dims[comp[0]];
  double h = spacing[comp[1]]*dims[comp[1]];
  double normalizationFactor = sqrt(w*w+h*h);
  double stepSize = this->StepSize*cellLength/normalizationFactor;

  // compute the LIC
  int updateExt[6];
  inInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), updateExt);

  int magUpdateExt[6];
  magUpdateExt[2*comp[0]] = updateExt[2*comp[0]] * this->Magnification;
  magUpdateExt[2*comp[1]] = updateExt[2*comp[1]] * this->Magnification;
  magUpdateExt[2*comp[2]] = updateExt[comp[2]];
  magUpdateExt[2*comp[0]+1] = (updateExt[2*comp[0]+1] + 1) * this->Magnification - 1;
  magUpdateExt[2*comp[1]+1] = (updateExt[2*comp[1]+1] + 1) * this->Magnification - 1;
  magUpdateExt[2*comp[2]+1] = updateExt[comp[2]];

  vtkPixelExtent magLicExtent(
        magUpdateExt[2*comp[0]],
        magUpdateExt[2*comp[0]+1],
        magUpdateExt[2*comp[1]],
        magUpdateExt[2*comp[1]+1]);

  // add ghosts
  double rk4fac = 3.0;
  int nGhosts = static_cast<int>(this->Steps*this->StepSize*rk4fac);
  nGhosts = nGhosts < 1 ? 1 : nGhosts;
  nGhosts *= 2; // for second ee lic pass

  vtkPixelExtent magLicGuardExtent(magLicExtent);
  magLicGuardExtent.Grow(nGhosts);
  magLicGuardExtent &= magVectorExtent;

  vtkLineIntegralConvolution2D *LICer = vtkLineIntegralConvolution2D::New();
  LICer->SetContext(this->Context);
  LICer->SetNumberOfSteps(this->Steps);
  LICer->SetStepSize(stepSize);
  LICer->SetComponentIds(comp[0], comp[1]);
  //LICer->SetGridSpacings(spacing[comp[0]], spacing[comp[1]]);

  deque<vtkPixelExtent> magLicExtents(1, magLicExtent);
  deque<vtkPixelExtent> magLicGuardExtents(1, magLicGuardExtent);

  vtkTextureObject *licTex
     = LICer->Execute(
            magVectorExtent,
            magLicGuardExtents,
            magLicExtents,
            magVectorTex,
            NULL,
            noiseTex);

  LICer->Delete();
  noiseTex->Delete();
  magVectorTex->Delete();

  if ( !licTex )
614
  {
Ken Martin's avatar
Ken Martin committed
615 616
    vtkErrorMacro("Failed to compute LIC");
    return 0;
617
  }
Ken Martin's avatar
Ken Martin committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

  #if (vtkImageDataLIC2DDEBUG >= 1)
  vtkTextureIO::Write(
          "idlic2d_lic.vtk",
          licTex, NULL, NULL);
  #endif

  // transfer lic from texture to vtk array
  vtkIdType nOutTups = magLicExtent.Size();
  vtkFloatArray *licOut = vtkFloatArray::New();
  licOut->SetNumberOfComponents(3);
  licOut->SetNumberOfTuples(nOutTups);
  licOut->SetName("LIC");

  vtkPixelBufferObject *licPBO = licTex->Download();

  vtkPixelTransfer::Blit<float, float>(
        magVectorExtent,
        magLicExtent,
        magLicExtent,
        magLicExtent,
        4,
        (float*)licPBO->MapPackedBuffer(),
        3,
        licOut->GetPointer(0));

  licPBO->UnmapPackedBuffer();
  licPBO->Delete();
  licTex->Delete();

  // mask and convert to gray scale 3 components
  float *pLicOut = licOut->GetPointer(0);
  for (vtkIdType i=0; i<nOutTups; ++i)
651
  {
Ken Martin's avatar
Ken Martin committed
652 653 654
    float lic = pLicOut[3*i];
    float mask = pLicOut[3*i+1];
    if ( mask )
655
    {
Ken Martin's avatar
Ken Martin committed
656
      pLicOut[3*i+1] = pLicOut[3*i+2] = pLicOut[3*i] = 0.0f;
657
    }
Ken Martin's avatar
Ken Martin committed
658
    else
659
    {
Ken Martin's avatar
Ken Martin committed
660 661
      pLicOut[3*i+1] = pLicOut[3*i+2] = lic;
    }
662
  }
Ken Martin's avatar
Ken Martin committed
663 664 665 666 667 668

  // setup output
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkImageData *output
    = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
  if (!output)
669
  {
Ken Martin's avatar
Ken Martin committed
670 671
    vtkErrorMacro("Empty output");
    return 1;
672
  }
Ken Martin's avatar
Ken Martin committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

  output->SetExtent(magUpdateExt);
  output->SetSpacing(spacing);
  output->GetPointData()->SetScalars(licOut);
  licOut->Delete();

  // Ensures that the output extent is exactly same as what was asked for.
  //output->Crop(outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT()));

  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 << "Magnification: " << this->Magnification  << "\n";
  os << indent << "OpenGLExtensionsSupported: "
               << this->OpenGLExtensionsSupported << "\n";
}