1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 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 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 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 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: TestTemporalCacheSimple.cxx,v $
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 "vtkActor.h"
#include "vtkCommand.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkContourFilter.h"
#include "vtkInformation.h"
#include "vtkMultiGroupPolyDataMapper.h"
#include "vtkPolyDataMapper.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTemporalDataSet.h"
#include "vtkTemporalDataSetCache.h"
#include "vtkTemporalInterpolator.h"
#include "vtkThreshold.h"
#include "vtkSphereSource.h"
#include "vtkObjectFactory.h"
#include "vtkInformationVector.h"
#include <vtkstd/algorithm>
#include <vtkstd/vector>
//
// This test is intended to test the ability of the temporal pipeline
// to loop a simple source over T and pass Temporal data downstream.
//
//-------------------------------------------------------------------------
// This is a dummy class which accepts time from the pipeline
// It doesn't do anything with the time, but it is useful for testing
//-------------------------------------------------------------------------
class vtkTemporalSphereSource : public vtkSphereSource {
public:
static vtkTemporalSphereSource *New();
vtkTypeRevisionMacro(vtkTemporalSphereSource, vtkSphereSource);
// Description:
// Set/Get the time value at which to get the value.
// These are not used. We get our time from the UPDATE_TIME_STEPS
// information key
vtkSetMacro(TimeStep, int);
vtkGetMacro(TimeStep, int);
// Save the range of valid timestep index values.
vtkGetVector2Macro(TimeStepRange, int);
//BTX
// void GetTimeStepValues(vtkstd::vector<double> &steps);
//ETX
protected:
vtkTemporalSphereSource();
virtual int RequestInformation(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector);
virtual int RequestData(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector);
public:
int TimeStepRange[2];
int TimeStep;
int ActualTimeStep;
vtkstd::vector<double> TimeStepValues;
};
//----------------------------------------------------------------------------
vtkCxxRevisionMacro(vtkTemporalSphereSource, "$Revision: 1.3 $");
vtkStandardNewMacro(vtkTemporalSphereSource);
//----------------------------------------------------------------------------
vtkTemporalSphereSource::vtkTemporalSphereSource()
{
this->TimeStepRange[0] = 0;
this->TimeStepRange[1] = 0;
this->TimeStep = 0;
this->ActualTimeStep = 0;
}
//----------------------------------------------------------------------------
int vtkTemporalSphereSource::RequestInformation(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation* outInfo = outputVector->GetInformationObject(0);
//
if (!this->Superclass::RequestInformation(request, inputVector, outputVector)) {
return 0;
}
this->TimeStepRange[0] = 0;
this->TimeStepRange[1] = 9;
this->TimeStepValues.resize(this->TimeStepRange[1]-this->TimeStepRange[0]+1);
for (int i=0; i<=this->TimeStepRange[1]; ++i)
{
this->TimeStepValues[i] = i;
}
outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
&this->TimeStepValues[0], this->TimeStepValues.size());
double timeRange[2];
timeRange[0] = this->TimeStepValues.front();
timeRange[1] = this->TimeStepValues.back();
outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), timeRange, 2);
return 1;
}
//----------------------------------------------------------------------------
class vtkTestTemporalCacheSimpleWithinTolerance: public vtkstd::binary_function<double, double, bool>
{
public:
result_type operator()(first_argument_type a, second_argument_type b) const
{
bool result = (fabs(a-b)<=(a*1E-6));
return (result_type)result;
}
};
//----------------------------------------------------------------------------
int vtkTemporalSphereSource::RequestData(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkDataObject* doOutput = outInfo->Get(vtkDataObject::DATA_OBJECT());
this->ActualTimeStep = this->TimeStep;
if (this->TimeStep==0 && outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS()))
{
double requestedTimeValue = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS())[0];
this->ActualTimeStep = vtkstd::find_if(
this->TimeStepValues.begin(),
this->TimeStepValues.end(),
vtkstd::bind2nd( vtkTestTemporalCacheSimpleWithinTolerance( ), requestedTimeValue ))
- this->TimeStepValues.begin();
this->ActualTimeStep = this->ActualTimeStep + this->TimeStepRange[0];
int N = outInfo->Length(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS());
doOutput->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(), &requestedTimeValue, 1);
vtkDebugMacro(<<"Got a timestep request from downstream t= " << requestedTimeValue
<< " Step : " << this->ActualTimeStep << "(Number of steps requested " << N << ")");
}
else
{
double timevalue[1];
timevalue[0] = this->TimeStepValues[this->ActualTimeStep-this->TimeStepRange[0]];
vtkDebugMacro(<<"Using manually set t= " << timevalue[0] << " Step : " << this->ActualTimeStep);
doOutput->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(), &timevalue[0], 1);
}
cout << "this->ActualTimeStep : " << this->ActualTimeStep << endl;
return Superclass::RequestData(request, inputVector, outputVector);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
class vtkTestTemporalCacheSimpleExecuteCallback
: public vtkCommand
{
public:
static vtkTestTemporalCacheSimpleExecuteCallback *New()
{ return new vtkTestTemporalCacheSimpleExecuteCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
// count the number of timesteps requested
vtkTemporalSphereSource *sph = vtkTemporalSphereSource::SafeDownCast(caller);
vtkInformation *info = sph->GetExecutive()->GetOutputInformation(0);
int Length = info->Length(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS());
this->Count += Length;
if (Length>0)
{
vtkstd::vector<double> steps;
steps.resize(Length);
info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS(), &steps[0]);
for (int i=0; i<Length; ++i)
{
cout << steps[i] << " ";
}
cout << endl;
}
}
unsigned int Count;
};
//-------------------------------------------------------------------------
int TestTemporalCacheSimple(int , char *[])
{
// we have to use a compsite pipeline
vtkCompositeDataPipeline* prototype = vtkCompositeDataPipeline::New();
vtkAlgorithm::SetDefaultExecutivePrototype(prototype);
prototype->Delete();
// create temporal fractals
vtkSmartPointer<vtkTemporalSphereSource> sphere =
vtkSmartPointer<vtkTemporalSphereSource>::New();
vtkTestTemporalCacheSimpleExecuteCallback *executecb
=vtkTestTemporalCacheSimpleExecuteCallback::New();
executecb->Count = 0;
sphere->AddObserver(vtkCommand::StartEvent,executecb);
executecb->Delete();
// cache the data to prevent regenerating some of it
vtkSmartPointer<vtkTemporalDataSetCache> cache =
vtkSmartPointer<vtkTemporalDataSetCache>::New();
cache->SetInputConnection(sphere->GetOutputPort());
cache->SetCacheSize(10);
// interpolate if needed
vtkSmartPointer<vtkTemporalInterpolator> interp =
vtkSmartPointer<vtkTemporalInterpolator>::New();
//interp->SetInputConnection(fractal->GetOutputPort());
interp->SetInputConnection(cache->GetOutputPort());
// map them
vtkSmartPointer<vtkMultiGroupPolyDataMapper> mapper =
vtkSmartPointer<vtkMultiGroupPolyDataMapper>::New();
mapper->SetInputConnection(interp->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderer->AddActor( actor );
renderer->SetBackground(0.5, 0.5, 0.5);
renWin->AddRenderer( renderer );
renWin->SetSize( 300, 300 );
iren->SetRenderWindow( renWin );
renWin->Render();
// ask for some specific data points
vtkStreamingDemandDrivenPipeline *sdd =
vtkStreamingDemandDrivenPipeline::SafeDownCast(interp->GetExecutive());
double times[1];
times[0] = 0;
int i;
int j;
for (j = 0; j < 5; ++j)
{
for (i = 0; i < 9; ++i)
{
times[0] = i+0.5;
// vtkDebugMacro(<<"SetUpdateTimeSteps t= " << times[0]);
sdd->SetUpdateTimeSteps(0, times, 1);
mapper->Modified();
renderer->ResetCameraClippingRange();
renWin->Render();
}
}
vtkAlgorithm::SetDefaultExecutivePrototype(0);
// there is a bug and ExecuteDataStart gets called twice when inside the
// Execute Block(Time), so this number is much too high, it should be
// be 11 at most and prefereble only 10 (but the first time always
// gets called twice).
if (executecb->Count == 22)
{
return 0;
}
return 1;
}
|