vtkExtractTemporalFieldData.cxx 7.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkExtractTemporalFieldData.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 "vtkExtractTemporalFieldData.h"

17
#include "vtkCompositeDataIterator.h"
18
#include "vtkDataSetAttributes.h"
19
#include "vtkDataSet.h"
20 21 22
#include "vtkDoubleArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
23 24
#include "vtkMultiBlockDataSet.h"
#include "vtkNew.h"
25 26
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
27
#include "vtkTable.h"
28

29
#include <vector>
30

31 32 33 34 35
class vtkExtractTemporalFieldData::vtkInternals
{
public:
  std::vector<double> TimeSteps;
};
36

37
vtkObjectFactoryNewMacro(vtkExtractTemporalFieldData);
38 39 40
//----------------------------------------------------------------------------
vtkExtractTemporalFieldData::vtkExtractTemporalFieldData()
{
41 42
  this->Internals = new vtkExtractTemporalFieldData::vtkInternals();
  this->HandleCompositeDataBlocksIndividually = true;
43 44 45 46 47
}

//----------------------------------------------------------------------------
vtkExtractTemporalFieldData::~vtkExtractTemporalFieldData()
{
48 49 50 51 52 53 54 55
  delete this->Internals;
  this->Internals = NULL;
}

//----------------------------------------------------------------------------
int vtkExtractTemporalFieldData::GetNumberOfTimeSteps()
{
  return static_cast<int>(this->Internals->TimeSteps.size());
56 57 58 59 60 61
}

//----------------------------------------------------------------------------
void vtkExtractTemporalFieldData::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
62 63
  os << indent << "HandleCompositeDataBlocksIndividually: "
     << this->HandleCompositeDataBlocksIndividually << endl;
64 65 66 67 68 69
}

//----------------------------------------------------------------------------
int vtkExtractTemporalFieldData::FillInputPortInformation(
  int vtkNotUsed(port), vtkInformation* info)
{
70
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
71
  info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet");
72 73 74 75
  return 1;
}

//----------------------------------------------------------------------------
76 77
int vtkExtractTemporalFieldData::RequestDataObject(
  vtkInformation*, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
78
{
79 80 81 82
  vtkDataObject* input = vtkDataObject::GetData(inputVector[0], 0);

  if (vtkCompositeDataSet::SafeDownCast(input) &&
    this->HandleCompositeDataBlocksIndividually)
83
    {
84 85 86 87 88 89
    if (vtkMultiBlockDataSet::GetData(outputVector, 0) == NULL)
      {
      vtkNew<vtkMultiBlockDataSet> mb;
      outputVector->GetInformationObject(0)->Set(
        vtkDataObject::DATA_OBJECT(), mb.Get());
      }
90
    }
91
  else if (vtkTable::GetData(outputVector, 0) == NULL)
92
    {
93 94 95
    vtkNew<vtkTable> table;
    outputVector->GetInformationObject(0)->Set(
      vtkDataObject::DATA_OBJECT(), table.Get());
96
    }
97
  return 1;
98 99 100 101 102 103 104 105 106
}

//----------------------------------------------------------------------------
int vtkExtractTemporalFieldData::RequestInformation(
  vtkInformation* vtkNotUsed(request),
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
{
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
107
  if (inInfo->Has(vtkStreamingDemandDrivenPipeline::TIME_STEPS()))
108
    {
109 110 111 112 113 114
    int size = inInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
    this->Internals->TimeSteps.resize(size);
    if (size > 0)
      {
      inInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), &this->Internals->TimeSteps[0]);
      }
115 116 117
    }
  else
    {
118
    this->Internals->TimeSteps.clear();
119 120
    }

121
  // The output of this filter does not contain a specific time, rather
122 123 124 125
  // it contains a collection of time steps. Also, this filter does not
  // respond to time requests. Therefore, we remove all time information
  // from the output.
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
126 127
  outInfo->Remove(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
  outInfo->Remove(vtkStreamingDemandDrivenPipeline::TIME_RANGE());
128 129 130 131 132 133 134 135 136
  return 1;
}

//----------------------------------------------------------------------------
int vtkExtractTemporalFieldData::RequestData(
  vtkInformation*,
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
{
137
  if (this->GetNumberOfTimeSteps() == 0)
138 139 140 141 142
    {
    vtkErrorMacro("No time steps in input data!");
    return 0;
    }

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
  vtkDataObject* inputDO = vtkDataObject::GetData(inputVector[0], 0);
  if (vtkCompositeDataSet* cd = vtkCompositeDataSet::SafeDownCast(inputDO))
    {
    vtkSmartPointer<vtkCompositeDataIterator> iter;
    iter.TakeReference(cd->NewIterator());
    if (this->HandleCompositeDataBlocksIndividually)
      {
      vtkMultiBlockDataSet* output = vtkMultiBlockDataSet::GetData(outputVector, 0);
      assert(output);
      output->CopyStructure(cd);
      for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
        {
        if (vtkDataSet* inputDS = vtkDataSet::SafeDownCast(iter->GetCurrentDataObject()))
          {
          vtkNew<vtkTable> outputBlock;
          this->CopyDataToOutput(inputDS, outputBlock.Get());
          output->SetDataSet(iter, outputBlock.Get());
          }
        }
      }
    else
      {
      vtkTable *output = vtkTable::GetData(outputVector, 0);
      assert(output);
      for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
        {
        if (vtkDataSet* inputDS = vtkDataSet::SafeDownCast(iter->GetCurrentDataObject()))
          {
          if (this->CopyDataToOutput(inputDS, output))
            {
            break;
            }
          }
        }
      }
    }
  else if (vtkDataSet* input = vtkDataSet::SafeDownCast(inputDO))
    {
    vtkTable *output = vtkTable::GetData(outputVector, 0);
    this->CopyDataToOutput(input, output);
    }
  else
    {
    vtkErrorMacro("Incorrect input type.");
    return 0;
    }
189 190 191 192
  return 1;
}

//----------------------------------------------------------------------------
193
bool vtkExtractTemporalFieldData::CopyDataToOutput(vtkDataSet *input, vtkTable *output)
194
{
195
  vtkDataSetAttributes *outRowData = output->GetRowData();
196
  vtkFieldData *ifd = input->GetFieldData();
197
  if (!ifd || !outRowData)
198
    {
199
    return false;
200 201
    }

202 203
  int numTimeSteps = this->GetNumberOfTimeSteps();
  assert(numTimeSteps > 0);
204
  for (vtkIdType j=0; j<ifd->GetNumberOfArrays(); j++)
205
    {
206
    vtkDataArray* inFieldArray = ifd->GetArray(j);
207 208
    if (inFieldArray &&
        inFieldArray->GetName() &&
209
        inFieldArray->GetNumberOfTuples() == numTimeSteps)
210
      {
211 212 213 214
      vtkDataArray *outArray = inFieldArray->NewInstance();
      outArray->ShallowCopy(inFieldArray);
      outRowData->AddArray(outArray);
      outArray->Delete();
215 216 217
      }
    }

218
  if (outRowData->GetNumberOfArrays() == 0)
219
    {
220
    return false;
221
    }
222 223

  // Add an array to hold the time at each step
224
  vtkNew<vtkDoubleArray> timeArray;
225
  timeArray->SetNumberOfComponents(1);
226
  timeArray->SetNumberOfTuples(numTimeSteps);
227 228 229 230 231 232 233 234
  if (ifd->GetArray("Time"))
    {
    timeArray->SetName("TimeData");
    }
  else
    {
    timeArray->SetName("Time");
    }
235 236 237 238
  std::copy(this->Internals->TimeSteps.begin(), this->Internals->TimeSteps.end(),
    timeArray->GetPointer(0));
  outRowData->AddArray(timeArray.Get());
  return true;
239
}