VMTK
Loading...
Searching...
No Matches
vtkvmtkStaticTemporalInterpolatedVelocityField.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkvmtkStaticTemporalInterpolatedVelocityField.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
16
17#include "vtkTable.h"
18#include "vtkDataSet.h"
19#include "vtkGenericCell.h"
20#include "vtkPointData.h"
21#include "vtkDataArray.h"
22#include "vtkMath.h"
23#include "vtkObjectFactory.h"
24
25// The interpolated velocity fields were reworked in VTK 9.2: the DataSets
26// collection became DataSetsInfo, the GenCell/Cell scratch cells were removed
27// and the Weights member became a std::vector.
28#define VMTK_VTK_HAS_REWORKED_IVF \
29 (VTK_MAJOR_VERSION > 9 || (VTK_MAJOR_VERSION == 9 && VTK_MINOR_VERSION >= 2))
30
33
35{
36 this->Periodic = 0;
37 this->VelocityScale = 1.0;
38 this->TimeStepsTable = NULL;
39 this->UseVectorComponents = 0;
40 this->VectorPrefix = NULL;
41 this->Component0Prefix = NULL;
42 this->Component1Prefix = NULL;
43 this->Component2Prefix = NULL;
44#if VMTK_VTK_HAS_REWORKED_IVF
45 this->TemporalGenCell = vtkGenericCell::New();
46 this->TemporalCell = vtkGenericCell::New();
47#endif
48}
49
51{
52 if (this->VectorPrefix) {
53 delete[] this->VectorPrefix;
54 this->VectorPrefix = NULL;
55 }
56
57 if (this->Component0Prefix) {
58 delete[] this->Component0Prefix;
59 this->Component0Prefix = NULL;
60 }
61
62 if (this->Component1Prefix) {
63 delete[] this->Component1Prefix;
64 this->Component1Prefix = NULL;
65 }
66
67 if (this->Component2Prefix) {
68 delete[] this->Component2Prefix;
69 this->Component2Prefix = NULL;
70 }
71
72 this->SetTimeStepsTable(NULL);
73
74#if VMTK_VTK_HAS_REWORKED_IVF
75 this->TemporalGenCell->Delete();
76 this->TemporalCell->Delete();
77#endif
78}
79
81{
82 this->LastCellId = c;
83#if VMTK_VTK_HAS_REWORKED_IVF
84 this->LastDataSet = this->DataSetsInfo[dataindex].DataSet;
85
86 // if the dataset changes, then the cached cell is invalidated
87 // we might as well prefetch the cached cell either way
88 if ( this->LastCellId != -1 )
89 {
90 this->LastDataSet->GetCell( this->LastCellId, this->TemporalGenCell );
91 }
92#else
93 this->LastDataSet = ( *this->DataSets )[dataindex];
94
95 // if the dataset changes, then the cached cell is invalidated
96 // we might as well prefetch the cached cell either way
97 if ( this->LastCellId != -1 )
98 {
99 this->LastDataSet->GetCell( this->LastCellId, this->GenCell );
100 }
101#endif
102
103 this->LastDataSetIndex = dataindex;
104}
105
106void vtkvmtkStaticTemporalInterpolatedVelocityField::FindTimeRowId(double time, int& prevRowId, int& nextRowId, double& p)
107{
108 //cout<<"Time "<<time<<endl;
109 prevRowId = 0;
110 nextRowId = 0;
111 p = 0.0;
112 if (this->TimeStepsTable == NULL)
113 {
114 return;
115 }
116 int numberOfRows = this->TimeStepsTable->GetNumberOfRows();
117 if (this->TimeStepsTable->GetNumberOfColumns() < 2 || numberOfRows == 1)
118 {
119 return;
120 }
121 int previousRowIndex = this->TimeStepsTable->GetValue(0,0).ToInt();
122 double previousRowTime = this->TimeStepsTable->GetValue(0,1).ToDouble();
123 if (numberOfRows < 2 || previousRowTime == time)
124 {
125 return;
126 }
127
128 double shiftedTime = time;
129
130 double period = this->TimeStepsTable->GetValue(numberOfRows-1,1).ToDouble() - previousRowTime;
131
132 if (this->Periodic)
133 {
134 double ratio = (time - previousRowTime) / period;
135 shiftedTime = (ratio - floor(ratio)) * period + previousRowTime;
136 }
137
138 bool found = false;
139 int pRowId = 0;
140 for (int i=1; i<numberOfRows; i++)
141 {
142 int rowId = i;
143 //if (i == numberOfRows)
144 // {
145 // rowId = 0;
146 // }
147 int rowIndex = this->TimeStepsTable->GetValue(rowId,0).ToInt();
148 double rowTime = this->TimeStepsTable->GetValue(rowId,1).ToDouble();
149 if (shiftedTime > previousRowTime && shiftedTime <= rowTime)
150 {
151 prevRowId = pRowId;
152 nextRowId = rowId;
153 p = (shiftedTime - previousRowTime) / (rowTime - previousRowTime);
154 found = true;
155 break;
156 }
157 pRowId = rowId;
158 previousRowIndex = rowIndex;
159 previousRowTime = rowTime;
160 }
161
162 //cout<<"Index "<<prevRowId<<" "<<nextRowId<<endl;
163 if (!found)
164 {
165 //cout<<"Shifted time "<<shiftedTime<<endl;
166 prevRowId = numberOfRows - 2;
167 nextRowId = numberOfRows - 2;
168 p = 1.0;
169 }
170}
171
173{
174 sprintf(name,"%s%d",prefix,index);
175}
176
177int vtkvmtkStaticTemporalInterpolatedVelocityField::FunctionValues( vtkDataSet * dataset, double * x, double * f )
178{
179 int i, j, subId , numPts, id;
180 vtkDataArray * vectorsPrev = NULL;
181 vtkDataArray * vectorsNext = NULL;
182 vtkDataArray * component0Prev = NULL;
183 vtkDataArray * component0Next = NULL;
184 vtkDataArray * component1Prev = NULL;
185 vtkDataArray * component1Next = NULL;
186 vtkDataArray * component2Prev = NULL;
187 vtkDataArray * component2Next = NULL;
188 double vecPrev[3], vecNext[3];
189 double dist2;
190 int ret;
191 char arrayName[1024];
192
193 f[0] = f[1] = f[2] = 0.0;
194
195 // See if a dataset has been specified and if there are input vectors
196 if ( !dataset )
197 {
198 vtkErrorMacro( << "Can't evaluate dataset!" );
199 return 0;
200 }
201
202 double time = x[3];
203
204 int prevRowId, nextRowId;
205 double timeP;
206 this->FindTimeRowId(time,prevRowId,nextRowId,timeP);
207
208 int prevTimeIndex = this->TimeStepsTable->GetValue(prevRowId,0).ToInt();
209 int nextTimeIndex = this->TimeStepsTable->GetValue(nextRowId,0).ToInt();
210
211 if (this->UseVectorComponents)
212 {
213 this->BuildArrayName(this->Component0Prefix,prevTimeIndex,arrayName);
214 component0Prev = dataset->GetPointData()->GetArray(arrayName);
215 this->BuildArrayName(this->Component1Prefix,prevTimeIndex,arrayName);
216 component1Prev = dataset->GetPointData()->GetArray(arrayName);
217 this->BuildArrayName(this->Component2Prefix,prevTimeIndex,arrayName);
218 component2Prev = dataset->GetPointData()->GetArray(arrayName);
219
220 if (!component0Prev || !component1Prev || !component2Prev)
221 {
222 vtkErrorMacro(<<"Component array not found for index "<<prevTimeIndex);
223 return 0;
224 }
225
226 this->BuildArrayName(this->Component0Prefix,nextTimeIndex,arrayName);
227 component0Next = dataset->GetPointData()->GetArray(arrayName);
228 this->BuildArrayName(this->Component1Prefix,nextTimeIndex,arrayName);
229 component1Next = dataset->GetPointData()->GetArray(arrayName);
230 this->BuildArrayName(this->Component2Prefix,nextTimeIndex,arrayName);
231 component2Next = dataset->GetPointData()->GetArray(arrayName);
232
233 if (timeP > 0.0 && (!component0Next || !component1Next || !component2Next))
234 {
235 vtkErrorMacro(<<"Component array not found for index "<<nextTimeIndex);
236 return 0;
237 }
238 }
239 else
240 {
241 this->BuildArrayName(this->VectorPrefix,prevTimeIndex,arrayName);
242 vectorsPrev = dataset->GetPointData()->GetArray(arrayName);
243
244 if (timeP > 0.0 && !vectorsPrev)
245 {
246 vtkErrorMacro(<<"Vector array not found for index "<<prevTimeIndex);
247 return 0;
248 }
249
250 this->BuildArrayName(this->VectorPrefix,nextTimeIndex,arrayName);
251 vectorsNext = dataset->GetPointData()->GetArray(arrayName);
252
253 if (!vectorsNext)
254 {
255 vtkErrorMacro(<<"Vector array not found for index "<<nextTimeIndex);
256 return 0;
257 }
258 }
259
260 double tol2 = dataset->GetLength() *
261 vtkAbstractInterpolatedVelocityField::TOLERANCE_SCALE;
262
263#if VMTK_VTK_HAS_REWORKED_IVF
264 if (this->Weights.size() < static_cast<size_t>(dataset->GetMaxCellSize()))
265 {
266 this->Weights.resize(dataset->GetMaxCellSize());
267 }
268 double* weights = this->Weights.data();
269 vtkGenericCell* genCell = this->TemporalGenCell;
270 vtkGenericCell* scratchCell = this->TemporalCell;
271#else
272 double* weights = this->Weights;
273 vtkGenericCell* genCell = this->GenCell;
274 vtkGenericCell* scratchCell = this->Cell;
275#endif
276
277 int found = 0;
278
279 if ( this->Caching )
280 {
281 // See if the point is in the cached cell
282 if ( this->LastCellId == -1 ||
283 !( ret = genCell->EvaluatePosition
284 ( x, 0, subId, this->LastPCoords, dist2, weights)
285 )
286 || ret == -1
287 )
288 {
289 // if not, find and get it
290 if ( this->LastCellId != - 1 )
291 {
292 this->CacheMiss ++;
293
294 dataset->GetCell( this->LastCellId, scratchCell );
295
296 this->LastCellId =
297 dataset->FindCell( x, scratchCell, genCell, this->LastCellId,
298 tol2, subId, this->LastPCoords, weights );
299
300 if ( this->LastCellId != -1 )
301 {
302 dataset->GetCell( this->LastCellId, genCell );
303 found = 1;
304 }
305 }
306 }
307 else
308 {
309 this->CacheHit ++;
310 found = 1;
311 }
312 }
313
314 if ( !found )
315 {
316 // if the cell is not found, do a global search (ignore initial
317 // cell if there is one)
318 this->LastCellId =
319 dataset->FindCell( x, 0, genCell, -1, tol2,
320 subId, this->LastPCoords, weights );
321
322 if ( this->LastCellId != -1 )
323 {
324 dataset->GetCell( this->LastCellId, genCell );
325 }
326 else
327 {
328 return 0;
329 }
330 }
331
332 // if the cell is valid
333 if ( this->LastCellId >= 0 )
334 {
335 numPts = genCell->GetNumberOfPoints();
336
337 // interpolate the vectors
338 for ( j = 0; j < numPts; j ++ )
339 {
340 id = genCell->PointIds->GetId( j );
341 if (this->UseVectorComponents)
342 {
343 vecPrev[0] = this->VelocityScale * component0Prev->GetTuple1(id);
344 vecPrev[1] = this->VelocityScale * component1Prev->GetTuple1(id);
345 vecPrev[2] = this->VelocityScale * component2Prev->GetTuple1(id);
346
347 if (timeP > 0.0)
348 {
349 vecNext[0] = this->VelocityScale * component0Next->GetTuple1(id);
350 vecNext[1] = this->VelocityScale * component1Next->GetTuple1(id);
351 vecNext[2] = this->VelocityScale * component2Next->GetTuple1(id);
352 }
353 }
354 else
355 {
356 vectorsPrev->GetTuple( id, vecPrev );
357 for ( i = 0; i < 3; i ++ )
358 {
359 vecPrev[i] *= this->VelocityScale;
360 }
361 if (timeP > 0.0)
362 {
363 vectorsNext->GetTuple( id, vecNext );
364 for ( i = 0; i < 3; i ++ )
365 {
366 vecNext[i] *= this->VelocityScale;
367 }
368 }
369 }
370 if (timeP > 0.0)
371 {
372 for ( i = 0; i < 3; i ++ )
373 {
374 f[i] += (timeP * vecNext[i] + (1.0 - timeP) * vecPrev[i]) * weights[j];
375 }
376 }
377 else
378 {
379 for ( i = 0; i < 3; i ++ )
380 {
381 f[i] += vecPrev[i] * weights[j];
382 }
383 }
384 }
385
386 if ( this->NormalizeVector == true )
387 {
388 vtkMath::Normalize( f );
389 }
390 }
391 // if not, return false
392 else
393 {
394 return 0;
395 }
396
397 return 1;
398}
399
400void vtkvmtkStaticTemporalInterpolatedVelocityField::CopyParameters( vtkAbstractInterpolatedVelocityField * from )
401{
402 if (from->IsA("vtkvmtkStaticTemporalInterpolatedVelocityField"))
403 {
405 vtkTable* timeStepsTable = vtkTable::New();
406 timeStepsTable->DeepCopy(fromCast->GetTimeStepsTable());
407 this->SetTimeStepsTable(timeStepsTable);
408 this->SetPeriodic(fromCast->GetPeriodic());
409 this->SetVelocityScale(fromCast->GetVelocityScale());
411 this->SetVectorPrefix(fromCast->VectorPrefix);
412 this->SetComponent0Prefix(fromCast->Component0Prefix);
413 this->SetComponent1Prefix(fromCast->Component1Prefix);
414 this->SetComponent2Prefix(fromCast->Component2Prefix);
415 timeStepsTable->Delete();
416 }
417}
418
419void vtkvmtkStaticTemporalInterpolatedVelocityField::PrintSelf( std::ostream & os, vtkIndent indent )
420{
421 this->Superclass::PrintSelf( os, indent );
422}
Evaluates an interpolated velocity field from a time-tabulated, temporally piecewise-linear velocity ...
virtual void SetComponent1Prefix(const char *)
static vtkvmtkStaticTemporalInterpolatedVelocityField * SafeDownCast(vtkObjectBase *o)
virtual void SetComponent2Prefix(const char *)
virtual int FunctionValues(vtkDataSet *ds, double *x, double *f) override
virtual void CopyParameters(vtkAbstractInterpolatedVelocityField *from) override
virtual void SetVectorPrefix(const char *)
virtual void SetComponent0Prefix(const char *)
void FindTimeRowId(double time, int &prevRowId, int &nextRowId, double &p)
vtkStandardNewMacro(vtkvmtkStaticTemporalInterpolatedVelocityField)
vtkCxxSetObjectMacro(vtkvmtkStaticTemporalInterpolatedVelocityField, TimeStepsTable, vtkTable)