vtkCMBApplyBathymetryFilter.cxx 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
//=========================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt 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.
//=========================================================================
10
#include "smtk/extension/vtk/filter/vtkCMBApplyBathymetryFilter.h"
11
12
13
14

#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCellLocator.h"
15
#include "vtkCleanPolyData.h"
16
17
18
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkGenericCell.h"
19
20
#include "vtkImageData.h"
#include "vtkIncrementalOctreePointLocator.h"
21
22
23
24
25
26
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
27
#include "vtkUniformGrid.h"
28
29
30
31
#include "vtkWeakPointer.h"

#include <iostream>
#include <map>
32
#include <sstream>
33
34
35
36
37
38
39
40
41
42
43
44
45
46

vtkStandardNewMacro(vtkCMBApplyBathymetryFilter);

namespace{
  template<class T>
  void flatZValues(T *t, int size, double val)
    {
    for (int i=0; i < size; i+=3,t+=3)
      {
      t[2] = val;
      }
    }
  template<class T>
  void copyArrayZValues(T *t, vtkIdType size,
47
    std::vector<double>& idToElevation,
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    bool useHighLimit, double eleHigh, bool useLowLimit, double eleLow)
    {
    double dtmp;
    for (vtkIdType i=0; i < size; ++i)
      {
      dtmp = static_cast<double>(t[i]);
      dtmp = (useHighLimit && dtmp>eleHigh) ? eleHigh : dtmp;
      dtmp = (useLowLimit && dtmp<eleLow) ? eleLow : dtmp;
      idToElevation[i]=dtmp;
      }
    }
  };

class vtkCMBApplyBathymetryFilter::vtkCmbInternalTerrainInfo
  {
public:
64
  vtkCmbInternalTerrainInfo(vtkInformation* input, const double &radius, double &invalid,
65
66
67
68
69
70
71
72
73
74
75
76
77
    bool useHighLimit, double eleHigh, bool useLowLimit, double eleLow);
  ~vtkCmbInternalTerrainInfo()
    {
    this->IdToElevation.clear();
    }
  //returns the average elevation for a circle given the radius
  template<class T>
  T getElevation(T *point);
  void setRadius(const double &r)
    {
    Radius = r;
    }

78
  std::vector<double> IdToElevation;
79
80
protected:
  double Radius;
81
  double InvalidValue;
82
83
84
85
  vtkSmartPointer<vtkIncrementalOctreePointLocator> Locator;
  };

vtkCMBApplyBathymetryFilter::vtkCmbInternalTerrainInfo::vtkCmbInternalTerrainInfo(
86
87
88
89
  vtkInformation* inInfo, const double &radius, double &invalid,
  bool useHighLimit, double eleHigh, bool useLowLimit, double eleLow)
: Radius(radius), InvalidValue(invalid)
{
90
91
92
93
94
95
96
97
98
99
100
  //1. Create a set of points while removing all the z values from the points
  //and storing them in the map.
  //2. Create locater of the resulting 2D point set

  if(!inInfo)
    {
    return;
    }
  vtkIdType numPoints = 0;
  vtkPolyData* pd = vtkPolyData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
101
102
  vtkUniformGrid* gridInput = vtkUniformGrid::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  vtkImageData* imageInput = vtkImageData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  if (pd)
    {
    numPoints += pd->GetNumberOfPoints();
    }
  else if(imageInput)
    {
    numPoints += imageInput->GetNumberOfPoints();
    }
  if(numPoints <=0)
    {
    return;
    }

  //second iteration is building the point set and elevation mapping
  vtkPoints *inputPoints = NULL;
  vtkPoints *points = vtkPoints::New();
121
  vtkIdType size, i;
122
123
  double p[3];
  double dtmp;
124
  // Uniform Grids may not have the max number of points
125
  if (gridInput)
126
    {
127
128
129
130
131
    vtkDataArray *dataArray = gridInput->GetPointData()->GetScalars("Elevation");
    if(!dataArray || dataArray->GetNumberOfTuples() != numPoints)
      {
      return;
      }
132
133
134
    points->SetNumberOfPoints(numPoints);
    this->IdToElevation.resize(numPoints);
    vtkIdType at = 0;
135
    for (i = 0; i < numPoints; i++)
136
      {
137
138
139
140
141
142
      if (!gridInput->IsPointVisible(i))
        {
        continue;
        }
      dtmp = dataArray->GetTuple1(i);
      dtmp = (useHighLimit && dtmp>eleHigh) ? eleHigh : dtmp;
143
      dtmp = (useLowLimit && dtmp<eleLow) ? eleLow : dtmp;
144
145
146
      imageInput->GetPoint(i,p);

      //flatten z
147
      p[2] = 0.0;
148
149
150
      points->SetPoint(at, p);
      this->IdToElevation[at]=dtmp;
      ++at;
151
      }
152
153
    points->Resize(at);
    this->IdToElevation.resize(at);
154
    }
155
  else
156
    {
157
158
    points->SetNumberOfPoints(numPoints);
    if (pd)
159
      {
160
161
      inputPoints = pd->GetPoints();
      size = inputPoints->GetNumberOfPoints();
162
163
      points->SetNumberOfPoints(size);
      this->IdToElevation.resize(size);
164
165
166
167
168
169
170
171
172
173
      for (i=0; i < size; ++i)
        {
        //get the point
        inputPoints->GetPoint(i,p);
        // check elevation limit
        dtmp = (useHighLimit && p[2]>eleHigh) ? eleHigh : p[2];
        dtmp = (useLowLimit && dtmp<eleLow) ? eleLow : dtmp;
        //store the z value & flatten
        this->IdToElevation[i]=dtmp;
        p[2] = 0.0;
174
        points->SetPoint(i,p);
175
        }
176
      }
177
    else if(imageInput)
178
      {
179
      size = imageInput->GetNumberOfPoints();
180
181
      points->SetNumberOfPoints(size);
      this->IdToElevation.resize(size);
182
183
184
185
186
      vtkDataArray *dataArray = imageInput->GetPointData()->GetScalars("Elevation");
      if(!dataArray || dataArray->GetNumberOfTuples() != size)
        {
        return;
        }
187

188
189
190
191
192
193
194
195
196
197
198
199
200
      //store the z value
      if (dataArray->GetDataType() == VTK_FLOAT)
        {
        vtkFloatArray *floatArray = static_cast<vtkFloatArray *>(dataArray);
        copyArrayZValues(floatArray->GetPointer(0),
                         size,this->IdToElevation, useHighLimit, eleHigh, useLowLimit, eleLow);
        }
      else if (dataArray->GetDataType() == VTK_DOUBLE)
        {
        vtkDoubleArray *doubleArray = static_cast<vtkDoubleArray *>(dataArray);
        copyArrayZValues(doubleArray->GetPointer(0),
                         size,this->IdToElevation, useHighLimit, eleHigh, useLowLimit, eleLow);
        }
201

202
203
204
205
206
207
208
      for (i=0; i < size; ++i)
        {
        //get the point
        imageInput->GetPoint(i,p);

        //flatten z
        p[2] = 0.0;
209
        points->SetPoint(i,p);
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
      }
    }
  vtkPolyData *pointSet = vtkPolyData::New();
  pointSet->SetPoints(points);
  points->FastDelete();

  this->Locator = vtkSmartPointer<vtkIncrementalOctreePointLocator>::New();
  this->Locator->AutomaticOn();
  this->Locator->SetTolerance(0.0);
  this->Locator->SetDataSet(pointSet);
  this->Locator->BuildLocator();
  pointSet->Delete();
}

template<class T>
T vtkCMBApplyBathymetryFilter::vtkCmbInternalTerrainInfo::getElevation(
  T *point)
{
  double dpoint[3];
  dpoint[0] = static_cast<double>(point[0]);
  dpoint[1] = static_cast<double>(point[1]);
  dpoint[2] = 0.0;
  vtkIdList *ids = vtkIdList::New();
  this->Locator->FindPointsWithinRadius(this->Radius,dpoint,ids);
  double sum = 0;
  std::map<vtkIdType,double>::const_iterator it;
  vtkIdType size = ids->GetNumberOfIds();
  for ( vtkIdType i=0; i < size; ++i)
    {
    //average the elevation
241
    assert(ids->GetId(i) < static_cast<vtkIdType>(this->IdToElevation.size()));
242
    sum += this->IdToElevation[ids->GetId(i)];
243
244
245
246
    }
  ids->Delete();

  //handle the zero size use case
247
  T elev = static_cast<T>((size == 0) ? InvalidValue : sum/size);
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  return elev;
}

vtkCMBApplyBathymetryFilter::vtkCMBApplyBathymetryFilter()
{
  this->TerrainInfo = NULL;
  this->SetNumberOfInputPorts(2);
  this->ElevationRadius = 1.0;
  this->FlattenZValues = false;
  this->NoOP = false;
  this->FlatZValue = 0.0;
  this->UseHighestZValue = false;
  this->UseLowestZValue = false;
  this->HighestZValue=this->LowestZValue=0.0;
262
  this->InvalidValue = 0.0;
263
264
265
266
267
268
269
270
271
272
273
}

vtkCMBApplyBathymetryFilter::~vtkCMBApplyBathymetryFilter()
{
  if(this->TerrainInfo)
    {
    delete this->TerrainInfo;
    }
}

int vtkCMBApplyBathymetryFilter::FillInputPortInformation(int port,
274
                                                          vtkInformation *info)
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
  if ( port == 1 )
    {
    info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
    }
  return 1;
}

void vtkCMBApplyBathymetryFilter::RemoveInputConnections()
{
  this->SetInputConnection(0, NULL);
}

void vtkCMBApplyBathymetryFilter::RemoveSourceConnections()
{
  this->SetInputConnection(1, NULL);
}

int vtkCMBApplyBathymetryFilter::RequestData(vtkInformation* /*request*/,
296
297
                                             vtkInformationVector** inputVector,
                                             vtkInformationVector* outputVector)
298
299
300
301
302
303
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
{
  // get the info and input data
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkPointSet *pdInput = vtkPointSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkPointSet *output = vtkPointSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
  if(this->NoOP)
    {
    output->ShallowCopy(pdInput);
    return 1;
    }
  vtkPointSet* finalMesh = pdInput->NewInstance();
  finalMesh->DeepCopy(pdInput);
  bool validMesh = false;
  if(this->FlattenZValues)
    {
    validMesh = this->FlattenMesh(finalMesh->GetPoints());
    }
  else
    {
    int numInputs = inputVector[1]->GetNumberOfInformationObjects();
    if(numInputs != 1)
      {
      vtkErrorMacro("No bathymetry input.");
      return 0;
      }

    //Construct the TerrainInfo first
    this->TerrainInfo = new vtkCmbInternalTerrainInfo(
      inputVector[1]->GetInformationObject(0),
330
      this->ElevationRadius, this->InvalidValue, this->UseHighestZValue,
331
      this->HighestZValue, this->UseLowestZValue, this->LowestZValue);
332
    if(!this->TerrainInfo->IdToElevation.empty())
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
      {
      validMesh = this->ApplyBathymetry(finalMesh->GetPoints());
      }

    delete this->TerrainInfo;
    this->TerrainInfo = NULL;
    }

  if(validMesh)
    {
    output->ShallowCopy( finalMesh );
    }
  finalMesh->Delete();

  //force the progress to finish no matter what.
  this->UpdateProgress(1.0);
  return validMesh;
}

bool vtkCMBApplyBathymetryFilter::FlattenMesh(vtkPoints *points)
{
  bool valid = true;
  vtkDataArray *dataArray = points->GetData();
  int size = points->GetNumberOfPoints() * 3;
  if (dataArray->GetDataType() == VTK_FLOAT)
    {
    vtkFloatArray *floatArray = static_cast<vtkFloatArray *>(dataArray);
    float *pt = floatArray->GetPointer(0);
    flatZValues(pt,size,this->FlatZValue);
    }
  else if (dataArray->GetDataType() == VTK_DOUBLE)
    {
    vtkDoubleArray *doubleArray = static_cast<vtkDoubleArray *>(dataArray);
    double *pt = doubleArray->GetPointer(0);
    flatZValues(pt,size,this->FlatZValue);
    }
  else
    {
    valid = false;
    }
  return true;
}

bool vtkCMBApplyBathymetryFilter::ApplyBathymetry(vtkPoints *points)
{
  //get the point data void pointer
  vtkDataArray *dataArray = points->GetData();
  vtkIdType size = points->GetNumberOfPoints();
  if(size == 0)
    {
    return false;
    }
  double pnom = static_cast<double>(size);
  if (dataArray->GetDataType() == VTK_FLOAT)
    {
    vtkFloatArray *floatArray = static_cast<vtkFloatArray *>(dataArray);
    float *pos = floatArray->GetPointer(0);
    for ( vtkIdType i = 0; i < size; ++i,pos+=3)
      {
      pos[2] = this->TerrainInfo->getElevation(pos);
      this->UpdateProgress(static_cast<double>(i)/pnom);
      }
    }
  else
    {
    vtkDoubleArray *doubleArray = static_cast<vtkDoubleArray *>(dataArray);
    double *pos = doubleArray->GetPointer(0);
    for ( vtkIdType i = 0; i < size; ++i,pos+=3)
      {
      pos[2] = this->TerrainInfo->getElevation(pos);
      this->UpdateProgress(static_cast<double>(i)/pnom);
      }
    }
  return true;
}

void vtkCMBApplyBathymetryFilter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "NoOP: " << this->NoOP << std::endl;
  os << indent << "ElevationRadius: " << this->ElevationRadius << std::endl;
  os << indent << "FlatZValue: " << this->FlatZValue << std::endl;
  os << indent << "FlattenZValues: " << this->FlattenZValues << std::endl;
416
  os << indent << "InvalidValue: " << this->InvalidValue << std::endl;
Ben Boeckel's avatar
Ben Boeckel committed
417
}