vtkExtractTimeSteps.cxx 7.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*=========================================================================

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

#include "vtkDataObject.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"

#include <algorithm>
#include <vector>
25
#include <cmath>
26 27 28 29


vtkStandardNewMacro(vtkExtractTimeSteps);

30
vtkExtractTimeSteps::vtkExtractTimeSteps() :
31
  UseRange(false), TimeStepInterval(1), TimeEstimationMode(PREVIOUS_TIMESTEP)
32
{
Ken Martin's avatar
Ken Martin committed
33 34
  this->Range[0] = 0;
  this->Range[1] = 0;
35 36
}

37 38 39 40 41 42 43
//----------------------------------------------------------------------------
void vtkExtractTimeSteps::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  int count = static_cast<int>(this->TimeStepIndices.size());
  os << indent << "Number of Time Steps: " << count << std::endl;
  if (count > 0)
44
  {
45 46 47
    std::set<int>::iterator it = this->TimeStepIndices.begin();
    os << indent << "Time Step Indices: " << *it++;
    for (int i = 1; i < std::min(count, 4); ++i)
48
    {
49
      os << ", " << *it++;
50
    }
51
    if (count > 9)
52
    {
53 54
      std::advance(it, count - 8);
      os << ", ... ";
55
    }
56
    while (it != this->TimeStepIndices.end())
57
    {
58 59
      os << ", " << *it++;
    }
60 61
    os << std::endl;
  }
62 63 64 65 66 67

  os << indent << "UseRange: " << (this->UseRange ? "true" : "false")
     << std::endl;
  os << indent << "Range: " << this->Range[0] << ", " << this->Range[1]
     << std::endl;
  os << indent << "TimeStepInterval: " << this->TimeStepInterval << std::endl;
68 69 70 71 72 73 74 75 76 77 78 79 80
  os << indent << "TimeEstimationMode: ";
  switch (this->TimeEstimationMode)
  {
  case PREVIOUS_TIMESTEP:
    os << "Previous Timestep" << std::endl;
    break;
  case NEXT_TIMESTEP:
    os << "Next Timestep" << std::endl;
    break;
  case NEAREST_TIMESTEP:
    os << "Nearest Timestep" << std::endl;
    break;
  }
81 82 83 84 85 86
}

//----------------------------------------------------------------------------
void vtkExtractTimeSteps::AddTimeStepIndex(int timeStepIndex)
{
  if (this->TimeStepIndices.insert(timeStepIndex).second)
87
  {
88
    this->Modified();
89
  }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

void vtkExtractTimeSteps::SetTimeStepIndices(int count, const int *timeStepIndices)
{
  this->TimeStepIndices.clear();
  this->TimeStepIndices.insert(timeStepIndices, timeStepIndices + count);
  this->Modified();
}

void vtkExtractTimeSteps::GetTimeStepIndices(int *timeStepIndices) const
{
  std::copy(this->TimeStepIndices.begin(), this->TimeStepIndices.end(), timeStepIndices);
}

void vtkExtractTimeSteps::GenerateTimeStepIndices(int begin, int end, int step)
{
  if (step != 0)
107
  {
108 109
    this->TimeStepIndices.clear();
    for (int i = begin; i < end; i += step)
110
    {
111 112
      this->TimeStepIndices.insert(i);
    }
113 114
    this->Modified();
  }
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
namespace
{

void getTimeSteps(vtkInformation* inInfo, const std::set<int>& timeStepIndices,
    bool useRange, int* range, int timeStepInterval, std::vector<double>& outTimes)
{
  double *inTimes =
    inInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
  int numTimes =
    inInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());

  if (!useRange)
  {
    for (std::set<int>::iterator it = timeStepIndices.begin();
         it != timeStepIndices.end(); ++it)
    {
      if (*it >= 0 && *it < numTimes)
      {
        outTimes.push_back(inTimes[*it]);
      }
    }
  }
  else
  {
    for (int i = 0; i < numTimes; ++i)
    {
      if (i >= range[0] && i <= range[1])
      {
        if ((i - range[0]) % timeStepInterval == 0)
        {
          outTimes.push_back(inTimes[i]);
        }
      }
    }
  }
}

}

156 157 158 159 160 161 162 163 164 165 166
//----------------------------------------------------------------------------
int vtkExtractTimeSteps::RequestInformation(vtkInformation*,
                                            vtkInformationVector **inputVector,
                                            vtkInformationVector *outputVector)
{
  // get the info objects
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);

  if (!this->TimeStepIndices.empty() &&
      inInfo->Has(vtkStreamingDemandDrivenPipeline::TIME_STEPS()))
167
  {
168 169

    std::vector<double> outTimes;
170 171 172
    getTimeSteps(inInfo, this->TimeStepIndices, this->UseRange, this->Range, this->TimeStepInterval, outTimes);

    if (!outTimes.empty())
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
      outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), &outTimes[0],
                  static_cast<int>(outTimes.size()));

      double range[2] = { outTimes.front(), outTimes.back() };
      outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), range, 2);
    }
  }

  return 1;
}

//----------------------------------------------------------------------------
int vtkExtractTimeSteps::RequestUpdateExtent(vtkInformation* vtkNotUsed(request),
                                           vtkInformationVector **inputVector,
                                           vtkInformationVector *outputVector)
{
  vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
  {
    double updateTime =
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());


    std::vector<double> outTimes;
    getTimeSteps(inInfo, this->TimeStepIndices, this->UseRange, this->Range, this->TimeStepInterval, outTimes);

    double inputTime;
    if (updateTime >= outTimes.back())
    {
      inputTime = outTimes.back();
    }
    else if (updateTime <= outTimes.front())
    {
      inputTime = outTimes.front();
209 210
    }
    else
211
    {
212 213 214 215 216 217 218
      auto gtindex = std::upper_bound(outTimes.begin(), outTimes.end(), updateTime);
      auto leindex = gtindex - 1;
      if (updateTime == *leindex)
      {
        inputTime = updateTime;
      }
      else
219
      {
220
        switch (this->TimeEstimationMode)
221
        {
222 223 224 225 226 227 228 229 230
        default:
        case PREVIOUS_TIMESTEP:
          inputTime = *leindex;
          break;
        case NEXT_TIMESTEP:
          inputTime = *gtindex;
          break;
        case NEAREST_TIMESTEP:
          if (std::abs(updateTime - *leindex) <= std::abs(*gtindex - updateTime))
231
          {
232
            inputTime = *leindex;
233
          }
234 235 236 237 238
          else
          {
            inputTime = *gtindex;
          }
          break;
239
        }
240
      }
241
    }
242 243
    inInfo->Set(
      vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP(), inputTime);
244
  }
245 246 247 248 249 250 251 252 253 254 255 256
  return 1;
}

//----------------------------------------------------------------------------
int vtkExtractTimeSteps::RequestData(vtkInformation *,
                                     vtkInformationVector **inputVector,
                                     vtkInformationVector *outputVector)
{
  vtkDataObject* inData = vtkDataObject::GetData(inputVector[0], 0);
  vtkDataObject* outData = vtkDataObject::GetData(outputVector, 0);

  if (inData && outData)
257
  {
258
    outData->ShallowCopy(inData);
259
  }
260 261
  return 1;
}