File: vtkExtractGeometry.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (312 lines) | stat: -rw-r--r-- 9,007 bytes parent folder | download | duplicates (3)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*=========================================================================

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

#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkCellIterator.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkImplicitFunction.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkUnstructuredGrid.h"

vtkStandardNewMacro(vtkExtractGeometry);
vtkCxxSetObjectMacro(vtkExtractGeometry,ImplicitFunction,vtkImplicitFunction);

//----------------------------------------------------------------------------
// Construct object with ExtractInside turned on.
vtkExtractGeometry::vtkExtractGeometry(vtkImplicitFunction *f)
{
  this->ImplicitFunction = f;
  if (this->ImplicitFunction)
  {
    this->ImplicitFunction->Register(this);
  }

  this->ExtractInside = 1;
  this->ExtractBoundaryCells = 0;
  this->ExtractOnlyBoundaryCells = 0;
}

//----------------------------------------------------------------------------
vtkExtractGeometry::~vtkExtractGeometry()
{
  this->SetImplicitFunction(NULL);
}

// Overload standard modified time function. If implicit function is modified,
// then this object is modified as well.
vtkMTimeType vtkExtractGeometry::GetMTime()
{
  vtkMTimeType mTime=this->MTime.GetMTime();
  vtkMTimeType impFuncMTime;

  if ( this->ImplicitFunction != NULL )
  {
    impFuncMTime = this->ImplicitFunction->GetMTime();
    mTime = ( impFuncMTime > mTime ? impFuncMTime : mTime );
  }

  return mTime;
}

//----------------------------------------------------------------------------
int vtkExtractGeometry::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the input and output
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  // May be NULL, check before dereferencing.
  vtkUnstructuredGrid *gridInput = vtkUnstructuredGrid::SafeDownCast(input);

  vtkIdType ptId, numPts, numCells, i, newCellId, newId, *pointMap;
  vtkSmartPointer<vtkCellIterator> cellIter =
      vtkSmartPointer<vtkCellIterator>::Take(input->NewCellIterator());
  vtkIdList *pointIdList;
  int cellType;
  vtkIdType numCellPts;
  double x[3];
  double multiplier;
  vtkPoints *newPts;
  vtkIdList *newCellPts;
  vtkPointData *pd = input->GetPointData();
  vtkCellData *cd = input->GetCellData();
  vtkPointData *outputPD = output->GetPointData();
  vtkCellData *outputCD = output->GetCellData();
  int npts;

  vtkDebugMacro(<< "Extracting geometry");

  if ( ! this->ImplicitFunction )
  {
    vtkErrorMacro(<<"No implicit function specified");
    return 1;
  }

  // As this filter is doing a subsetting operation, set the Copy Tuple flag
  // for GlobalIds array so that, if present, it will be copied to the output.
  outputPD->CopyGlobalIdsOn();
  outputCD->CopyGlobalIdsOn();

  newCellPts = vtkIdList::New();
  newCellPts->Allocate(VTK_CELL_SIZE);

  if ( this->ExtractInside )
  {
    multiplier = 1.0;
  }
  else
  {
    multiplier = -1.0;
  }

  // Loop over all points determining whether they are inside the
  // implicit function. Copy the points and point data if they are.
  //
  numPts = input->GetNumberOfPoints();
  numCells = input->GetNumberOfCells();
  pointMap = new vtkIdType[numPts]; // maps old point ids into new
  for (i=0; i < numPts; i++)
  {
    pointMap[i] = -1;
  }

  output->Allocate(numCells/4); //allocate storage for geometry/topology
  newPts = vtkPoints::New();
  newPts->Allocate(numPts/4,numPts);
  outputPD->CopyAllocate(pd);
  outputCD->CopyAllocate(cd);
  vtkFloatArray *newScalars = NULL;

  if ( ! this->ExtractBoundaryCells )
  {
    for ( ptId=0; ptId < numPts; ptId++ )
    {
      input->GetPoint(ptId, x);
      if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
      {
        newId = newPts->InsertNextPoint(x);
        pointMap[ptId] = newId;
        outputPD->CopyData(pd,ptId,newId);
      }
    }
  }
  else
  {
    // To extract boundary cells, we have to create supplemental information
    double val;
    newScalars = vtkFloatArray::New();
    newScalars->SetNumberOfValues(numPts);

    for (ptId=0; ptId < numPts; ptId++ )
    {
      input->GetPoint(ptId, x);
      val = this->ImplicitFunction->FunctionValue(x) * multiplier;
      newScalars->SetValue(ptId, val);
    }
  }

  // Now loop over all cells to see whether they are inside implicit
  // function (or on boundary if ExtractBoundaryCells is on).
  //
  for (cellIter->InitTraversal(); !cellIter->IsDoneWithTraversal();
       cellIter->GoToNextCell())
  {
    cellType = cellIter->GetCellType();
    numCellPts = cellIter->GetNumberOfPoints();
    pointIdList = cellIter->GetPointIds();

    newCellPts->Reset();
    if ( ! this->ExtractBoundaryCells ) //requires less work
    {
      for ( npts=0, i=0; i < numCellPts; i++, npts++)
      {
        ptId = pointIdList->GetId(i);
        if ( pointMap[ptId] < 0 )
        {
          break; //this cell won't be inserted
        }
        else
        {
          newCellPts->InsertId(i,pointMap[ptId]);
        }
      }
    } //if don't want to extract boundary cells

    else //want boundary cells
    {
      for ( npts=0, i=0; i < numCellPts; i++ )
      {
        ptId = pointIdList->GetId(i);
        if ( newScalars->GetValue(ptId) <= 0.0 )
        {
          npts++;
        }
      }
      int extraction_condition = 0;
      if ( this->ExtractOnlyBoundaryCells )
      {
        if ( ( npts > 0 ) && ( npts != numCellPts ) )
        {
          extraction_condition = 1;
        }
      }
      else
      {
        if ( npts > 0 )
        {
          extraction_condition = 1;
        }
      }
      if ( extraction_condition )
      {
        for ( i=0; i < numCellPts; i++ )
        {
          ptId = pointIdList->GetId(i);
          if ( pointMap[ptId] < 0 )
          {
            input->GetPoint(ptId, x);
            newId = newPts->InsertNextPoint(x);
            pointMap[ptId] = newId;
            outputPD->CopyData(pd,ptId,newId);
          }
          newCellPts->InsertId(i,pointMap[ptId]);
        }
      }//a boundary or interior cell
    }//if mapping boundary cells

    int extraction_condition = 0;
    if ( this->ExtractOnlyBoundaryCells )
    {
      if ( npts != numCellPts && (this->ExtractBoundaryCells && npts > 0) )
      {
        extraction_condition = 1;
      }
    }
    else
    {
      if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
      {
        extraction_condition = 1;
      }
    }
    if ( extraction_condition )
    {
      // special handling for polyhedron cells
      if (gridInput && cellType == VTK_POLYHEDRON)
      {
        newCellPts->Reset();
        gridInput->GetFaceStream(cellIter->GetCellId(), newCellPts);
        vtkUnstructuredGrid::ConvertFaceStreamPointIds(newCellPts, pointMap);
      }
      newCellId = output->InsertNextCell(cellType,newCellPts);
      outputCD->CopyData(cd, cellIter->GetCellId(), newCellId);
    }
  }//for all cells

  // Update ourselves and release memory
  //
  delete [] pointMap;
  newCellPts->Delete();
  output->SetPoints(newPts);
  newPts->Delete();

  if ( this->ExtractBoundaryCells )
  {
    newScalars->Delete();
  }

  output->Squeeze();

  return 1;
}

//----------------------------------------------------------------------------
int vtkExtractGeometry::FillInputPortInformation(int, vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  return 1;
}

//----------------------------------------------------------------------------
void vtkExtractGeometry::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Implicit Function: "
     << static_cast<void *>(this->ImplicitFunction) << "\n";
  os << indent << "Extract Inside: "
     << (this->ExtractInside ? "On\n" : "Off\n");
  os << indent << "Extract Boundary Cells: "
     << (this->ExtractBoundaryCells ? "On\n" : "Off\n");
  os << indent << "Extract Only Boundary Cells: "
     << (this->ExtractOnlyBoundaryCells ? "On\n" : "Off\n");
}