vtkExtractGrid.cxx 7.07 KB
Newer Older
1 2
/*=========================================================================

3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkExtractGrid.cxx
5

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

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

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtkExtractGrid.h"
16

17
#include "vtkBoundingBox.h"
18
#include "vtkCellData.h"
19
#include "vtkExtractStructuredGridHelper.h"
20 21
#include "vtkInformation.h"
#include "vtkInformationVector.h"
22
#include "vtkObjectFactory.h"
23
#include "vtkPointData.h"
24
#include "vtkStreamingDemandDrivenPipeline.h"
25
#include "vtkStructuredGrid.h"
26

Brad King's avatar
Brad King committed
27
vtkStandardNewMacro(vtkExtractGrid);
28

29 30 31 32
// Construct object to extract all of the input data.
vtkExtractGrid::vtkExtractGrid()
{
  this->VOI[0] = this->VOI[2] = this->VOI[4] = 0;
33
  this->VOI[1] = this->VOI[3] = this->VOI[5] = VTK_INT_MAX;
34 35

  this->SampleRate[0] = this->SampleRate[1] = this->SampleRate[2] = 1;
36
  this->IncludeBoundary = 0;
37
  this->Internal = vtkExtractStructuredGridHelper::New();
38 39
}

40 41
vtkExtractGrid::~vtkExtractGrid()
{
42
  if( this->Internal != nullptr )
43
  {
44
    this->Internal->Delete();
45
  }
46 47
}

48
//------------------------------------------------------------------------------
49
int vtkExtractGrid::RequestInformation(
50 51 52
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
53
{
54 55 56 57
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

58
  int wholeExtent[6], outWholeExt[6];
59

60
  inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
61

62 63
  this->Internal->Initialize(
      this->VOI,wholeExtent,this->SampleRate,(this->IncludeBoundary==1));
64 65

  if (!this->Internal->IsValid())
66
  {
67
    vtkDebugMacro("Error while initializing filter.");
68
    return 0;
69
  }
70

71
  this->Internal->GetOutputWholeExtent(outWholeExt);
72

73 74
  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
               outWholeExt, 6);
75 76
  return 1;
}
77

78
int vtkExtractGrid::RequestUpdateExtent(
79 80 81
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
82
{
83
  if (!this->Internal->IsValid())
84
  {
85
    return 0;
86
  }
87

88 89
  int i;

90 91
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
92

93 94 95
  bool emptyExtent = false;
  int uExt[6];
  for (i=0; i<3; i++)
96
  {
97
    if (this->Internal->GetSize(i) < 1)
98
    {
99 100 101 102
      uExt[0] = uExt[2] = uExt[4] = 0;
      uExt[1] = uExt[3] = uExt[5] = -1;
      emptyExtent = true;
      break;
103
    }
104
  }
105

106
  if (!emptyExtent)
107
  {
108 109 110 111 112
    // Find input update extent based on requested output
    // extent
    int oUExt[6];
    outputVector->GetInformationObject(0)->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), oUExt);
113 114
    int oWExt[6]; // For parallel parititon this will be different.
    this->Internal->GetOutputWholeExtent(oWExt);
115
    for (i=0; i<3; i++)
116
    {
117 118
      int idx = oUExt[2*i] - oWExt[2*i]; // Extent value to index
      if (idx < 0 || idx >= (int)this->Internal->GetSize(i))
119
      {
120 121
        vtkWarningMacro("Requested extent outside whole extent.")
        idx = 0;
122
      }
123
      uExt[2*i] = this->Internal->GetMappedExtentValueFromIndex(i, idx);
124
      int jdx = oUExt[2*i+1] - oWExt[2*i]; // Extent value to index
125
      if (jdx < idx || jdx >= (int)this->Internal->GetSize(i))
126
      {
127 128
        vtkWarningMacro("Requested extent outside whole extent.")
        jdx = 0;
129
      }
130
      uExt[2*i + 1] = this->Internal->GetMappedExtentValueFromIndex(i, jdx);
131
    }
132
  }
133 134 135
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), uExt, 6);
  // We can handle anything.
  inInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 0);
136 137

  return 1;
138 139
}

140
//------------------------------------------------------------------------------
141 142 143 144
int vtkExtractGrid::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
145
{
146 147 148 149 150 151
  // Reset internal helper to the actual extents of the piece we're working on:
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkStructuredGrid *inGrid = vtkStructuredGrid::GetData(inInfo);
  this->Internal->Initialize(this->VOI, inGrid->GetExtent(), this->SampleRate,
                             (this->IncludeBoundary != 0));

152
  if (!this->Internal->IsValid())
153
  {
154
    return 0;
155
  }
156 157 158 159 160 161 162 163

  // Set the output extent -- this is how RequestDataImpl knows what to copy.
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkStructuredGrid *output = vtkStructuredGrid::SafeDownCast(
        outInfo->Get(vtkDataObject::DATA_OBJECT()));
  output->SetExtent(this->Internal->GetOutputWholeExtent());

  return this->RequestDataImpl(inputVector, outputVector) ? 1 : 0;
164 165 166
}

//------------------------------------------------------------------------------
167
bool vtkExtractGrid::RequestDataImpl(vtkInformationVector **inputVector,
168
                                     vtkInformationVector *outputVector)
169
{
170 171 172
  if( (this->SampleRate[0] < 1) ||
      (this->SampleRate[1] < 1) ||
      (this->SampleRate[2] < 1) )
173
  {
174 175
    vtkErrorMacro("SampleRate must be >= 1 in all 3 dimensions!");
    return false;
176
  }
177

178 179 180 181
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

182
  // get the input and output
183 184 185 186 187
  vtkStructuredGrid *input = vtkStructuredGrid::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkStructuredGrid *output = vtkStructuredGrid::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

188
  if (input->GetNumberOfPoints() == 0)
189
  {
190
    return true;
191
  }
192

193 194 195 196 197 198 199
  vtkPointData *pd=input->GetPointData();
  vtkCellData *cd=input->GetCellData();
  vtkPointData *outPD=output->GetPointData();
  vtkCellData *outCD=output->GetCellData();

  vtkPoints *inPts = input->GetPoints();
  int *inExt = input->GetExtent();
200

201 202
  vtkPoints *newPts = inPts->NewInstance();
  int *outExt = output->GetExtent();
203

204
  vtkDebugMacro(<< "Extracting Grid");
205

206
  this->Internal->CopyPointsAndPointData(inExt,outExt,pd,inPts,outPD,newPts);
207 208
  output->SetPoints(newPts);
  newPts->Delete();
209

210 211
  this->Internal->CopyCellData(inExt,outExt,cd,outCD);

212
  return true;
213
}
214

215
void vtkExtractGrid::PrintSelf(ostream& os, vtkIndent indent)
216
{
Brad King's avatar
Brad King committed
217
  this->Superclass::PrintSelf(os,indent);
218 219

  os << indent << "VOI: \n";
220
  os << indent << "  Imin,Imax: (" << this->VOI[0] << ", "
221
     << this->VOI[1] << ")\n";
222
  os << indent << "  Jmin,Jmax: (" << this->VOI[2] << ", "
223
     << this->VOI[3] << ")\n";
224
  os << indent << "  Kmin,Kmax: (" << this->VOI[4] << ", "
225
     << this->VOI[5] << ")\n";
226 227 228 229

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

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