vtkCachingInterpolatedVelocityField.cxx 14.3 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
25
26
27
28
29
30
/*=========================================================================

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

#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkPointData.h"
#include "vtkGenericCell.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkAbstractCellLocator.h"
#include "vtkSmartPointer.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"

#include "vtkCellLocator.h"
#define Custom_TreeType vtkCellLocator

31
#include <vector>
32
#ifdef JB_BSP_TREE
33
34
35
36
37
38
39
40
41
 #include "vtkModifiedBSPTree.h"
 #undef  Custom_TreeType
 #define Custom_TreeType vtkModifiedBSPTree
#endif
//---------------------------------------------------------------------------
vtkStandardNewMacro(vtkCachingInterpolatedVelocityField);
//---------------------------------------------------------------------------
const double IVFDataSetInfo::TOLERANCE_SCALE = 1.0E-8;
//---------------------------------------------------------------------------
42
43
44
45
46
47
48
49
50
51
IVFDataSetInfo::IVFDataSetInfo()
{
  this->VelocityFloat  = NULL;
  this->VelocityDouble = NULL;
  this->DataSet        = NULL;
  this->Cell           = NULL;
  this->BSPTree        = NULL;
  this->Tolerance      = 0.0;
  this->StaticDataSet  = false;
}
52
53
54
55
56
57
58
59
60
61
//---------------------------------------------------------------------------
void IVFDataSetInfo::SetDataSet(vtkDataSet *data, char *velocity, bool staticdataset, vtkAbstractCellLocator *locator)
{
  this->VelocityFloat  = NULL;
  this->VelocityDouble = NULL;
  this->DataSet        = data;
  this->Cell           = vtkSmartPointer<vtkGenericCell>::New();
  this->StaticDataSet  = staticdataset;
  if (locator) {
    this->BSPTree = locator;
62
  }
63
64
65
66
67
68
69
70
  else if (this->DataSet->IsA("vtkUnstructuredGrid")) {
    if (!this->BSPTree) {
      this->BSPTree = vtkSmartPointer<Custom_TreeType>::New();
    }
    this->BSPTree->SetLazyEvaluation(1);
    this->BSPTree->SetDataSet(this->DataSet);
    this->BSPTree->SetUseExistingSearchStructure(this->StaticDataSet);
  }
71
  this->Tolerance =
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
    this->DataSet->GetLength() * IVFDataSetInfo::TOLERANCE_SCALE;
  //
  vtkDataArray *vectors = this->DataSet->GetPointData()->GetArray(velocity);
  if (vtkFloatArray::SafeDownCast(vectors)) {
    this->VelocityFloat = vtkFloatArray::SafeDownCast(vectors)->GetPointer(0);
  }
  else if (vtkDoubleArray::SafeDownCast(vectors)) {
    this->VelocityDouble = vtkDoubleArray::SafeDownCast(vectors)->GetPointer(0);
  }
  else {
    vtkGenericWarningMacro("We only support float/double velocity vectors at the current time");
  }
}
//---------------------------------------------------------------------------
IVFDataSetInfo::IVFDataSetInfo(const IVFDataSetInfo &ivfci)
{
  this->VelocityFloat  = ivfci.VelocityFloat;
  this->VelocityDouble = ivfci.VelocityDouble;
  this->DataSet        = ivfci.DataSet;
  this->Cell           = ivfci.Cell;
  this->BSPTree        = ivfci.BSPTree;
  this->Tolerance      = ivfci.Tolerance;
  this->StaticDataSet  = ivfci.StaticDataSet;
}
//---------------------------------------------------------------------------
IVFDataSetInfo &IVFDataSetInfo::operator=(const IVFDataSetInfo &ivfci)
{
  this->VelocityFloat  = ivfci.VelocityFloat;
  this->VelocityDouble = ivfci.VelocityDouble;
  this->DataSet        = ivfci.DataSet;
  this->Cell           = ivfci.Cell;
  this->BSPTree        = ivfci.BSPTree;
  this->Tolerance      = ivfci.Tolerance;
  this->StaticDataSet  = ivfci.StaticDataSet;
  return *this;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
vtkCachingInterpolatedVelocityField::vtkCachingInterpolatedVelocityField()
{
  this->NumFuncs         = 3; // u, v, w
  this->NumIndepVars     = 4; // x, y, z, t
  this->VectorsSelection = NULL;
  this->TempCell         = vtkGenericCell::New();
  this->CellCacheHit     = 0;
  this->DataSetCacheHit  = 0;
  this->CacheMiss        = 0;
  this->LastCacheIndex   = 0;
  this->Cache            = NULL;
  this->LastCellId       = -1;
}
//---------------------------------------------------------------------------
vtkCachingInterpolatedVelocityField::~vtkCachingInterpolatedVelocityField()
{
  this->NumFuncs     = 0;
  this->NumIndepVars = 0;
  this->TempCell->Delete();
  this->SetVectorsSelection(0);
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::SetDataSet(int I, vtkDataSet* dataset, bool staticdataset, vtkAbstractCellLocator *locator)
{
135
136
#define max(x,y) ((x>y) ? (x) : (y))
  int N = max(I+1, static_cast<int>(this->CacheList.size()));
137
138
139
  this->CacheList.resize(N);
  this->CacheList[I].SetDataSet(dataset, this->VectorsSelection, staticdataset, locator);
  //
140
  int maxsize = max(static_cast<int>(this->Weights.size()), dataset->GetMaxCellSize());
141
142
143
  this->Weights.assign(maxsize, 0.0);
}
//---------------------------------------------------------------------------
144
void vtkCachingInterpolatedVelocityField::SetLastCellInfo(vtkIdType c, int datasetindex)
145
{
146
  if ((this->LastCacheIndex != datasetindex) || (this->LastCellId != c))
147
    {
148
149
    this->LastCacheIndex = datasetindex;
    this->Cache          = &this->CacheList[this->LastCacheIndex];
150
    this->LastCellId     = c;
151
152
153
    // if the dataset changes, then the cached cell is invalidated
    // we might as well prefetch the new cached cell - we'll need it on the next test anyway
    if (this->LastCellId!=-1)
154
155
156
157
158
159
160
161
162
163
      {
      if(c < this->Cache->DataSet->GetNumberOfCells())
        {
        this->Cache->DataSet->GetCell(this->LastCellId, this->Cache->Cell);
        }
      else
        {
        this->LastCellId = -1;
        }
      }
164
    }
165
166
}
//---------------------------------------------------------------------------
167
168
169
void vtkCachingInterpolatedVelocityField::ClearLastCellInfo() {
  this->Cache      = NULL;
  this->LastCellId = -1;
170
171
}
//---------------------------------------------------------------------------
172
vtkGenericCell *vtkCachingInterpolatedVelocityField::GetLastCell()
173
{
174
  if (this->Cache)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    {
    return this->Cache->Cell;
    }
  return NULL;
}
//---------------------------------------------------------------------------
// Evaluate {u,v,w} at {x,y,z,t}
int vtkCachingInterpolatedVelocityField::FunctionValues(double* x, double* f)
{
  // Test using whatever cached information we have
  if (this->Cache) {
    if (this->FunctionValues(this->Cache, x, f)) {
      this->DataSetCacheHit++;
      return 1;
    }
  }
191
  // don't reset this->Cache as we don't want to test it again,
192
  // see == comparison below
193

194
195
  int oldCacheIndex = this->LastCacheIndex;
  // now try each of the datasets in turn
196
  for (this->LastCacheIndex = 0;
197
198
199
200
201
202
203
       this->LastCacheIndex < static_cast<int>(this->CacheList.size());
       this->LastCacheIndex++)
    {
    IVFDataSetInfo *data = &this->CacheList[this->LastCacheIndex];
    if (data==this->Cache) continue;
    //
    this->LastCellId = -1;
204
    if (this->FunctionValues(data, x, f))
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
      {
      this->Cache = data;
      this->CacheMiss++;
      return 1;
      }
    }
  // failed, so clear data and set the cache index to something sensible
  this->CacheMiss++;
  this->ClearLastCellInfo();
  this->LastCacheIndex = oldCacheIndex;
  return 0;
}
//---------------------------------------------------------------------------
// same as FunctionValues, but only testing in/out of cells
int vtkCachingInterpolatedVelocityField::InsideTest(double* x)
{
  // Test using whatever cached information we have
  if (this->Cache) {
    // check the last cell
    int subId;
    if (this->LastCellId!=-1 && this->Cache->Cell->EvaluatePosition(
226
        x, 0, subId, this->Cache->PCoords,
227
228
229
230
        this->Cache->Tolerance, &this->Weights[0])==1) return 1;
    // check this dataset
    if (this->InsideTest(this->Cache, x)) return 1;
  }
231
  // don't reset this->Cache as we don't want to test it again,
232
233
234
  // see != comparison below

  // now try each of the other datasets in turn
235
  for (this->LastCacheIndex = 0;
236
237
238
239
240
241
242
       this->LastCacheIndex < static_cast<int>(this->CacheList.size());
       this->LastCacheIndex++)
    {
    IVFDataSetInfo *data = &this->CacheList[this->LastCacheIndex];
    if (data==this->Cache) continue;
    //
    this->LastCellId = -1;
243
    if (this->InsideTest(data,  x))
244
245
246
247
248
      {
      this->Cache = data;
      return 1;
      }
    }
249
  // failed, so clear data
250
251
252
253
254
  this->ClearLastCellInfo();
  return 0;
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::InsideTest(IVFDataSetInfo *data, double* x)
255
{
256
  int cellId = data->BSPTree->FindCell(x, data->Tolerance, data->Cell, data->PCoords, &this->Weights[0]);
257
  if (cellId!=-1)
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    {
    this->LastCellId = cellId;
    return 1;
    }
  return 0;
}
//---------------------------------------------------------------------------
// Evaluate {u,v,w} at {x,y,z,t}
int vtkCachingInterpolatedVelocityField::FunctionValues(
  IVFDataSetInfo *data, double *x, double *f)
{
  int    subId;
  double dist2;

272
  if (this->LastCellId>=0)
273
274
275
276
277
278
    {
    bool inbox = true;
    if (data->BSPTree && !data->BSPTree->InsideCellBounds(x, this->LastCellId)) {
      inbox = false;
    }
    if (inbox && data->Cell->EvaluatePosition(
279
      x, 0, subId, data->PCoords, dist2, &this->Weights[0])==1)
280
281
282
283
284
285
286
287
288
289
      {
      this->FastCompute(data, f);
      this->CellCacheHit++;
      return 1;
      }
    }

  // we need to search the whole dataset
  if (data->BSPTree)
    {
290
    int cellId = data->BSPTree->FindCell(x, data->Tolerance,
291
292
293
294
295
      data->Cell, data->PCoords, &this->Weights[0]);
    this->LastCellId = cellId;
    }
  else
    {
296
297
298
299
300
301
    vtkGenericCell* tmpCell = 0;
    if (this->LastCellId >= 0)
      {
      data->DataSet->GetCell(this->LastCellId, this->TempCell);
      tmpCell = this->TempCell;
      }
302
    this->LastCellId =
303
      data->DataSet->FindCell(x, tmpCell, data->Cell, this->LastCellId,
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
      data->Tolerance, subId, data->PCoords, &this->Weights[0]);
    if (this->LastCellId != -1)
      {
      data->DataSet->GetCell(this->LastCellId, data->Cell);
      }
    }

  // if the cell is valid
  if (this->LastCellId != -1)
    {
    this->FastCompute(data, f);
    }
  else
    {
    return 0;
    }

  return 1;
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::FastCompute(
  IVFDataSetInfo *data, double f[3])
{
  f[0] = f[1] = f[2] = 0.0;
  int numPts = data->Cell->GetNumberOfPoints();
  // interpolate the vectors
  double *dvectors = data->VelocityDouble;
331
  if (dvectors)
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    {
    for (int j=0; j<numPts; j++)
      {
      int id = data->Cell->PointIds->GetId(j);
      f[0] +=  dvectors[id*3 + 0] * this->Weights[j];
      f[1] +=  dvectors[id*3 + 1] * this->Weights[j];
      f[2] +=  dvectors[id*3 + 2] * this->Weights[j];
      }
    }
  else
    {
    float *fvectors = data->VelocityFloat;
    for (int j=0; j<numPts; j++)
      {
      int id = data->Cell->PointIds->GetId(j);
      f[0] +=  fvectors[id*3 + 0] * this->Weights[j];
      f[1] +=  fvectors[id*3 + 1] * this->Weights[j];
      f[2] +=  fvectors[id*3 + 2] * this->Weights[j];
      }
    }
}
//---------------------------------------------------------------------------
bool vtkCachingInterpolatedVelocityField::InterpolatePoint(
  vtkPointData *outPD, vtkIdType outIndex)
{
  if (!this->Cache || !this->Cache->DataSet)
    {
    return 0;
    }
  outPD->InterpolatePoint(
362
    this->Cache->DataSet->GetPointData(), outIndex,
363
    this->Cache->Cell->PointIds, &this->Weights[0]);
364
365
366
367
368
369
370
371
372
373
374
375
376
  return 1;
}
//---------------------------------------------------------------------------
bool vtkCachingInterpolatedVelocityField::InterpolatePoint(
  vtkCachingInterpolatedVelocityField* inCIVF, vtkPointData *outPD, vtkIdType outIndex)
{
  if (!this->Cache || !this->Cache->DataSet)
    {
    return 0;
    }
  vtkPointData* inPD = inCIVF->Cache->DataSet->GetPointData();
  outPD->InterpolatePoint(
    inPD, outIndex, this->Cache->Cell->PointIds, &this->Weights[0]);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  return 1;
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::GetLastWeights(double* w)
{
  int j, numPts;

  // If last cell is valid, fill w with the interpolation weights
  // and return true
  if (this->Cache && this->LastCellId >= 0)
    {
    numPts = this->Cache->Cell->GetNumberOfPoints();
    for (j=0; j < numPts; j++)
      {
      w[j] = this->Weights[j];
      }
    return 1;
    }
  // otherwise, return false
  else
    {
    return 0;
    }
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::GetLastLocalCoordinates(double pcoords[3])
{
  int j;

  // If last cell is valid, fill p with the local coordinates
  // and return true
  if (this->Cache && this->LastCellId >= 0)
    {
    for (j=0; j < 3; j++)
      {
      pcoords[j] = this->Cache->PCoords[j];
      }
    return 1;
    }
  // otherwise, return false
  else
    {
    return 0;
    }
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
426
427
428
429
430
431
432
433
434
435

  if (Weights.size()>0)
    {
    os << indent << "Weights: " << &this->Weights[0] << endl;
    }
  else
    {
    os << indent << "Weights: (none)" << endl;
    }

436
437
438
  os << indent << "Cell Cache hit: " << this->CellCacheHit << endl;
  os << indent << "DataSet Cache hit: " << this->DataSetCacheHit << endl;
  os << indent << "Cache miss: " << this->CacheMiss << endl;
439
  os << indent << "VectorsSelection: "
440
     << (this->VectorsSelection?this->VectorsSelection:"(none)") << endl;
441
442
443
444
445
446
447
448
449
450
451

  if (this->Cache)
    {
    os << indent << "Cache->DataSet : "
        << this->Cache->DataSet << endl;
    }
  else
    {
    os << indent << "Cache->DataSet : (none)" << endl;
    }

452
453
454
455
  os << indent << "LastCacheIndex : "
     << this->LastCacheIndex << endl;
}
//---------------------------------------------------------------------------