vtkVisItExtractGrid.C 13.4 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2 3
/*=========================================================================

  Program:   Visualization Toolkit
hrchilds's avatar
hrchilds committed
4
  Module:    $RCSfile: vtkVisItExtractGrid.cxx,v $
hrchilds's avatar
hrchilds committed
5

hrchilds's avatar
hrchilds committed
6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
hrchilds's avatar
hrchilds committed
7 8 9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

hrchilds's avatar
hrchilds committed
10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
hrchilds's avatar
hrchilds committed
12 13 14
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
hrchilds's avatar
hrchilds committed
15
#include "vtkVisItExtractGrid.h"
hrchilds's avatar
hrchilds committed
16 17

#include <vtkCellData.h>
hrchilds's avatar
hrchilds committed
18 19
#include <vtkInformation.h>
#include <vtkInformationVector.h>
hrchilds's avatar
hrchilds committed
20 21
#include <vtkObjectFactory.h>
#include <vtkPointData.h>
hrchilds's avatar
hrchilds committed
22
#include <vtkStreamingDemandDrivenPipeline.h>
hrchilds's avatar
hrchilds committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <vtkStructuredGrid.h>

vtkStandardNewMacro(vtkVisItExtractGrid);

// Construct object to extract all of the input data.
vtkVisItExtractGrid::vtkVisItExtractGrid()
{
  this->VOI[0] = this->VOI[2] = this->VOI[4] = 0;
  this->VOI[1] = this->VOI[3] = this->VOI[5] = VTK_LARGE_INTEGER;

  this->SampleRate[0] = this->SampleRate[1] = this->SampleRate[2] = 1;
  
  this->IncludeBoundary = 0;
}

hrchilds's avatar
hrchilds committed
38 39 40 41
int vtkVisItExtractGrid::RequestUpdateExtent(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
42
{
hrchilds's avatar
hrchilds committed
43 44 45 46
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

hrchilds's avatar
hrchilds committed
47 48 49 50
  int i, ext[6], voi[6];
  int *inWholeExt, *outWholeExt, *updateExt;
  int rate[3];

hrchilds's avatar
hrchilds committed
51 52 53
  inWholeExt = inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
  outWholeExt = outInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
  updateExt = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT());
hrchilds's avatar
hrchilds committed
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

  for (i = 0; i < 3; ++i)
    {
    rate[i] = this->SampleRate[i];
    if (rate[i] < 1)
      {
      rate[i] = 1;
      }
    }

  // Once again, clip the VOI with the input whole extent.
  for (i = 0; i < 3; ++i)
    {
    voi[i*2] = this->VOI[2*i];
    if (voi[2*i] < inWholeExt[2*i])
      {
      voi[2*i] = inWholeExt[2*i];
      }
    voi[i*2+1] = this->VOI[2*i+1];
    if (voi[2*i+1] > inWholeExt[2*i+1])
      {
      voi[2*i+1] = inWholeExt[2*i+1];
      }
    }

  ext[0] = voi[0] + (updateExt[0]-outWholeExt[0])*rate[0];
  ext[1] = voi[0] + (updateExt[1]-outWholeExt[0])*rate[0];
  if (ext[1] > voi[1])
    { // This handles the IncludeBoundary condition.
    ext[1] = voi[1];
    }
  ext[2] = voi[2] + (updateExt[2]-outWholeExt[2])*rate[1];
  ext[3] = voi[2] + (updateExt[3]-outWholeExt[2])*rate[1];
  if (ext[3] > voi[3])
    { // This handles the IncludeBoundary condition.
    ext[3] = voi[3];
    }
  ext[4] = voi[4] + (updateExt[4]-outWholeExt[4])*rate[2];
  ext[5] = voi[4] + (updateExt[5]-outWholeExt[4])*rate[2];
  if (ext[5] > voi[5])
    { // This handles the IncludeBoundary condition.
    ext[5] = voi[5];
    }
  
  // I do not think we need this extra check, but it cannot hurt.
  if (ext[0] < inWholeExt[0])
    {
    ext[0] = inWholeExt[0];
    }
  if (ext[1] > inWholeExt[1])
    {
    ext[1] = inWholeExt[1];
    }
  
  if (ext[2] < inWholeExt[2])
    {
    ext[2] = inWholeExt[2];
    }
  if (ext[3] > inWholeExt[3])
    {
    ext[3] = inWholeExt[3];
    }
  
  if (ext[4] < inWholeExt[4])
    {
    ext[4] = inWholeExt[4];
    }
  if (ext[5] > inWholeExt[5])
    {
    ext[5] = inWholeExt[5];
    }  
  
hrchilds's avatar
hrchilds committed
126
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), ext, 6);
hrchilds's avatar
hrchilds committed
127
  // We can handle anything.
hrchilds's avatar
hrchilds committed
128
  inInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 0);
hrchilds's avatar
hrchilds committed
129

hrchilds's avatar
hrchilds committed
130 131
  return 1;
}
hrchilds's avatar
hrchilds committed
132

hrchilds's avatar
hrchilds committed
133 134 135 136
int vtkVisItExtractGrid::RequestInformation(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
137
{
hrchilds's avatar
hrchilds committed
138 139 140 141
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

hrchilds's avatar
hrchilds committed
142 143 144 145
  int i, outDims[3], voi[6], wholeExtent[6];
  int mins[3];
  int rate[3];

hrchilds's avatar
hrchilds committed
146
  inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
hrchilds's avatar
hrchilds committed
147 148 149 150 151 152 153 154 155 156 157 158 159

  // Copy because we need to take union of voi and whole extent.
  for ( i=0; i < 6; i++ )
    {
    voi[i] = this->VOI[i];
    }

  for ( i=0; i < 3; i++ )
    {
    // Empty request.
    if (voi[2*i+1] < voi[2*i] || voi[2*i+1] < wholeExtent[2*i] || 
        voi[2*i] > wholeExtent[2*i+1])
      {
hrchilds's avatar
hrchilds committed
160 161 162
      outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
                   0,-1,0,-1,0,-1);
      return 1;
hrchilds's avatar
hrchilds committed
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
      }

    // Make sure VOI is in the whole extent.
    if ( voi[2*i+1] > wholeExtent[2*i+1] )
      {
      voi[2*i+1] = wholeExtent[2*i+1];
      }
    else if ( voi[2*i+1] < wholeExtent[2*i] )
      {
      voi[2*i+1] = wholeExtent[2*i];
      }
    if ( voi[2*i] > wholeExtent[2*i+1] )
      {
      voi[2*i] = wholeExtent[2*i+1];
      }
    else if ( voi[2*i] < wholeExtent[2*i] )
      {
      voi[2*i] = wholeExtent[2*i];
      }

    if ( (rate[i] = this->SampleRate[i]) < 1 )
      {
      rate[i] = 1;
      }

    outDims[i] = (voi[2*i+1] - voi[2*i]) / rate[i] + 1;
    if ( outDims[i] < 1 )
      {
      outDims[i] = 1;
      }
    // We might as well make this work for negative extents.
    mins[i] = (int)(floor((float)voi[2*i] / (float)rate[i]));
    }

  // Adjust the output dimensions if the boundaries are to be
  // included and the sample rate is not 1.
  if ( this->IncludeBoundary && 
       (rate[0] != 1 || rate[1] != 1 || rate[2] != 1) )
    {
    int diff;
    for (i=0; i<3; i++)
      {
      if ( ((diff=voi[2*i+1]-voi[2*i]) > 0) && rate[i] != 1 &&
           ((diff % rate[i]) != 0) )
        {
        outDims[i]++;
        }
      }
    }

  // Set the whole extent of the output
  wholeExtent[0] = mins[0];
  wholeExtent[1] = mins[0] + outDims[0] - 1;
  wholeExtent[2] = mins[1];
  wholeExtent[3] = mins[1] + outDims[1] - 1;
  wholeExtent[4] = mins[2];
  wholeExtent[5] = mins[2] + outDims[2] - 1;

hrchilds's avatar
hrchilds committed
221 222 223 224
  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
               wholeExtent, 6);

  return 1;
hrchilds's avatar
hrchilds committed
225 226 227 228 229 230 231 232 233
}


// ****************************************************************************
// Modifications:
//   Kathleen Bonnell, Tue May 10 11:09:36 PDT 2005
//   Test for boundary condition with cell data, so we don't increment past
//   array boundaries.
//
hrchilds's avatar
hrchilds committed
234 235 236 237 238 239 240
//   Hank Childs, Fri Dec 29 14:32:53 PST 2006
//   Since AVT isn't participating in the whole "extents" game, discontinue
//   its passing downstream.  This is screwing up repeated uses of
//   this filter (i.e. twice in one pipeline), which happens with a 3D
//   structured grid getting index selected and also facelisted.  Also pass
//   through field data.
//
hrchilds's avatar
hrchilds committed
241 242
// ****************************************************************************

hrchilds's avatar
hrchilds committed
243 244 245 246
int vtkVisItExtractGrid::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
hrchilds's avatar
hrchilds committed
247
{
hrchilds's avatar
hrchilds committed
248 249 250 251 252 253 254 255 256 257
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the input and ouptut
  vtkStructuredGrid *input = vtkStructuredGrid::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkStructuredGrid *output = vtkStructuredGrid::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

hrchilds's avatar
hrchilds committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  vtkPointData *pd=input->GetPointData();
  vtkCellData *cd=input->GetCellData();
  vtkPointData *outPD=output->GetPointData();
  vtkCellData *outCD=output->GetCellData();
  int i, j, k, uExt[6], voi[6];
  int *inExt;
  int *inWholeExt;
  int iIn, jIn, kIn;
  int outSize, jOffset, kOffset, rate[3];
  vtkIdType idx, newIdx, newCellId;
  vtkPoints *newPts, *inPts;
  int inInc1, inInc2;
  // Function to convert output index to input index f(i) = Rate*I + shift
  int shift[3];

  vtkDebugMacro(<< "Extracting Grid");

  inPts = input->GetPoints();

hrchilds's avatar
hrchilds committed
277
  outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), uExt);
hrchilds's avatar
hrchilds committed
278 279 280 281 282 283 284 285 286 287 288 289 290
  inExt = input->GetExtent();
  inInc1 = (inExt[1]-inExt[0]+1);
  inInc2 = inInc1*(inExt[3]-inExt[2]+1);

  for (i = 0; i < 3; ++i)
    {
    if ( (rate[i] = this->SampleRate[i]) < 1 )
      {
      rate[i] = 1;
      }
    }

  // Clip the VOI by the input whole extent
hrchilds's avatar
hrchilds committed
291
  inWholeExt = inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT());
hrchilds's avatar
hrchilds committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305
  for (i = 0; i < 3; ++i)
    {
    voi[i*2] = this->VOI[2*i];
    if (voi[2*i] < inWholeExt[2*i])
      {
      voi[2*i] = inWholeExt[2*i];
      }
    voi[i*2+1] = this->VOI[2*i+1];
    if (voi[2*i+1] > inWholeExt[2*i+1])
      {
      voi[2*i+1] = inWholeExt[2*i+1];
      }
    }

hrchilds's avatar
hrchilds committed
306 307 308 309
  // Compute the shift.
  // The shift is necessary because the starting VOI may not be on stride boundary.
  // We need to duplicate the computation done in 
  // ExecuteInformtation for the output whole extent.
hrchilds's avatar
hrchilds committed
310 311 312 313
  // Use shift as temporary variable (output mins).
  shift[0] = (int)(floor( (float)(voi[0])/(float)(rate[0]) ));
  shift[1] = (int)(floor( (float)(voi[2])/(float)(rate[1]) ));
  shift[2] = (int)(floor( (float)(voi[4])/(float)(rate[2]) ));
hrchilds's avatar
hrchilds committed
314
  // Take the different between the output and input mins (in input coordinates).
hrchilds's avatar
hrchilds committed
315 316 317 318
  shift[0] = voi[0] - (shift[0]*rate[0]);
  shift[1] = voi[2] - (shift[1]*rate[1]);
  shift[2] = voi[4] - (shift[2]*rate[2]);

hrchilds's avatar
hrchilds committed
319 320 321 322 323 324 325 326
  int tmpExt[6];
  tmpExt[0] = 0;
  tmpExt[1] = uExt[1] - uExt[0];
  tmpExt[2] = 0;
  tmpExt[3] = uExt[3] - uExt[2];
  tmpExt[4] = 0;
  tmpExt[5] = uExt[5] - uExt[4];
  output->SetExtent(tmpExt);
hrchilds's avatar
hrchilds committed
327 328 329 330 331 332 333 334 335 336

  // If output same as input, just pass data through
  if ( uExt[0] <= inExt[0] && uExt[1] >= inExt[1] &&
       uExt[2] <= inExt[2] && uExt[3] >= inExt[3] &&
       uExt[4] <= inExt[4] && uExt[5] >= inExt[5] &&
       rate[0] == 1 && rate[1] == 1 && rate[2] == 1)
    { 
    output->SetPoints(inPts);
    output->GetPointData()->PassData(input->GetPointData());
    output->GetCellData()->PassData(input->GetCellData());
hrchilds's avatar
hrchilds committed
337
    output->GetFieldData()->PassData(input->GetFieldData());
hrchilds's avatar
hrchilds committed
338
    vtkDebugMacro(<<"Passed data through bacause input and output are the same");
hrchilds's avatar
hrchilds committed
339
    return 1;
hrchilds's avatar
hrchilds committed
340 341 342 343 344 345 346 347 348 349
    }

  // Allocate necessary objects
  //
  outSize = (uExt[1]-uExt[0]+1)*(uExt[3]-uExt[2]+1)*(uExt[5]-uExt[4]+1);
  newPts = inPts->NewInstance(); 
  newPts->SetDataType(inPts->GetDataType());
  newPts->SetNumberOfPoints(outSize);
  outPD->CopyAllocate(pd,outSize,outSize);
  outCD->CopyAllocate(cd,outSize,outSize);
hrchilds's avatar
hrchilds committed
350
  output->GetFieldData()->PassData(input->GetFieldData());
hrchilds's avatar
hrchilds committed
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

  // Traverse input data and copy point attributes to output
  // iIn,jIn,kIn are in input grid coordinates.
  newIdx = 0;
  for ( k=uExt[4]; k <= uExt[5]; ++k)
    { // Convert out coords to in coords.
    kIn = shift[2] + (k*rate[2]);
    if (kIn > voi[5])
      { // This handles the IncludeBoundaryOn condition.
      kIn = voi[5];
      }
    kOffset = (kIn-inExt[4]) * inInc2;
    for ( j=uExt[2]; j <= uExt[3]; ++j)
      { // Convert out coords to in coords.
      jIn = shift[1] + (j*rate[1]);
      if (jIn > voi[3])
        { // This handles the IncludeBoundaryOn condition.
        jIn = voi[3];
        }
      jOffset = (jIn-inExt[2]) * inInc1;
      for ( i=uExt[0]; i <= uExt[1]; ++i)
        { // Convert out coords to in coords.
        iIn = shift[0] + (i*rate[0]);
        if (iIn > voi[1])
          { // This handles the IncludeBoundaryOn condition.
          iIn = voi[1];
          }
        idx = (iIn-inExt[0]) + jOffset + kOffset;
        newPts->SetPoint(newIdx,inPts->GetPoint(idx));
        outPD->CopyData(pd, idx, newIdx++);
        }
      }
    }

  // Traverse input data and copy cell attributes to output
  //
  newCellId = 0;
  inInc1 = (inExt[1]-inExt[0]);
  inInc2 = inInc1*(inExt[3]-inExt[2]);
  // This will take care of 2D and 1D cells.
  // Each loop has to excute at least once.
  if (uExt[4] == uExt[5])
    {
    if (uExt[4] < inExt[5] || uExt[4] == inExt[4])
      uExt[5] = uExt[5] + 1;
    else  // along max boundary, cannot increment!
      uExt[4] = uExt[4] - 1;
    }
  if (uExt[2] == uExt[3])
    {
    if (uExt[2] < inExt[3] || uExt[2] == inExt[2])
      uExt[3] = uExt[3] + 1;
    else  // along max boundary, cannot increment!
      uExt[2] = uExt[2] - 1;
    }
  if (uExt[0] == uExt[1])
    {
    if (uExt[0] < inExt[1] || uExt[0] == inExt[0])
      uExt[1] = uExt[1] + 1;
    else  // along max boundary, cannot increment!
      uExt[0] = uExt[0] - 1;
    }
  // No need to consider IncludeBoundary for cell data.
  for ( k=uExt[4]; k < uExt[5]; ++k )
    { // Convert out coords to in coords.
    kIn = shift[2] + (k*rate[2]);
    kOffset = (kIn-inExt[4]) * inInc2;
    for ( j=uExt[2]; j < uExt[3]; ++j )
      { // Convert out coords to in coords.
      jIn = shift[1] + (j*rate[1]);
      jOffset = (jIn-inExt[2]) * inInc1;
      for ( i=uExt[0]; i < uExt[1]; ++i )
        {
        iIn = shift[0] + (i*rate[0]);
        idx = (iIn-inExt[0]) + jOffset + kOffset;
        outCD->CopyData(cd, idx, newCellId++);
        }
      }
    }

  output->SetPoints(newPts);
  newPts->Delete();

hrchilds's avatar
hrchilds committed
434 435
  return 1;
}
hrchilds's avatar
hrchilds committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

void vtkVisItExtractGrid::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "VOI: \n";
  os << indent << "  Imin,Imax: (" << this->VOI[0] << ", " 
     << this->VOI[1] << ")\n";
  os << indent << "  Jmin,Jmax: (" << this->VOI[2] << ", " 
     << this->VOI[3] << ")\n";
  os << indent << "  Kmin,Kmax: (" << this->VOI[4] << ", " 
     << this->VOI[5] << ")\n";

  os << indent << "Sample Rate: (" << this->SampleRate[0] << ", "
               << this->SampleRate[1] << ", "
               << this->SampleRate[2] << ")\n";

  os << indent << "Include Boundary: " 
     << (this->IncludeBoundary ? "On\n" : "Off\n");
}