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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkShrinkFilter.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 "vtkShrinkFilter.h"
#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkUnstructuredGrid.h"
vtkStandardNewMacro(vtkShrinkFilter);
//----------------------------------------------------------------------------
vtkShrinkFilter::vtkShrinkFilter()
{
this->ShrinkFactor = 0.5;
}
//----------------------------------------------------------------------------
vtkShrinkFilter::~vtkShrinkFilter()
{
}
//----------------------------------------------------------------------------
void vtkShrinkFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Shrink Factor: " << this->ShrinkFactor << "\n";
}
//----------------------------------------------------------------------------
int vtkShrinkFilter::FillInputPortInformation(int, vtkInformation* info)
{
// This filter uses the vtkDataSet cell traversal methods so it
// suppors any data set type as input.
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
//----------------------------------------------------------------------------
int vtkShrinkFilter::RequestData(vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// Get input and output data.
vtkDataSet* input = vtkDataSet::GetData(inputVector[0]);
vtkUnstructuredGrid* output = vtkUnstructuredGrid::GetData(outputVector);
// We are now executing this filter.
vtkDebugMacro("Shrinking cells");
// Skip execution if there is no input geometry.
vtkIdType numCells = input->GetNumberOfCells();
vtkIdType numPts = input->GetNumberOfPoints();
if(numCells < 1 || numPts < 1)
{
vtkDebugMacro("No data to shrink!");
return 1;
}
// Allocate working space for new and old cell point lists.
vtkSmartPointer<vtkIdList> ptIds = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> newPtIds = vtkSmartPointer<vtkIdList>::New();
ptIds->Allocate(VTK_CELL_SIZE);
newPtIds->Allocate(VTK_CELL_SIZE);
// Allocate approximately the space needed for the output cells.
output->Allocate(numCells);
// Allocate space for a new set of points.
vtkSmartPointer<vtkPoints> newPts = vtkSmartPointer<vtkPoints>::New();
newPts->Allocate(numPts*8, numPts);
// Allocate space for data associated with the new set of points.
vtkPointData* inPD = input->GetPointData();
vtkPointData* outPD = output->GetPointData();
outPD->CopyAllocate(inPD, numPts*8, numPts);
// Support progress and abort.
vtkIdType tenth = (numCells >= 10? numCells/10 : 1);
double numCellsInv = 1.0/numCells;
int abort = 0;
// Point Id map.
vtkIdType* pointMap = new vtkIdType[input->GetNumberOfPoints()];
// Traverse all cells, obtaining node coordinates. Compute "center"
// of cell, then create new vertices shrunk towards center.
for(vtkIdType cellId = 0; cellId < numCells && !abort; ++cellId)
{
// Get the list of points for this cell.
input->GetCellPoints(cellId, ptIds);
vtkIdType numIds = ptIds->GetNumberOfIds();
// Periodically update progress and check for an abort request.
if(cellId % tenth == 0)
{
this->UpdateProgress((cellId+1)*numCellsInv);
abort = this->GetAbortExecute();
}
// Compute the center of mass of the cell points.
double center[3] = {0,0,0};
for(vtkIdType i=0; i < numIds; ++i)
{
double p[3];
input->GetPoint(ptIds->GetId(i), p);
for(int j=0; j < 3; ++j)
{
center[j] += p[j];
}
}
for(int j=0; j < 3; ++j)
{
center[j] /= numIds;
}
// Create new points for this cell.
newPtIds->Reset();
for(vtkIdType i=0; i < numIds; ++i)
{
// Get the old point location.
double p[3];
input->GetPoint(ptIds->GetId(i), p);
// Compute the new point location.
double newPt[3];
for(int j=0; j < 3; ++j)
{
newPt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
}
// Create the new point for this cell.
vtkIdType newId = newPts->InsertNextPoint(newPt);
// Copy point data from the old point.
vtkIdType oldId = ptIds->GetId(i);
outPD->CopyData(inPD, oldId, newId);
pointMap[oldId] = newId;
}
// special handling for polyhedron cells
if (vtkUnstructuredGrid::SafeDownCast(input) &&
input->GetCellType(cellId) == VTK_POLYHEDRON)
{
vtkUnstructuredGrid::SafeDownCast(input)->GetFaceStream(cellId, newPtIds);
vtkUnstructuredGrid::ConvertFaceStreamPointIds(newPtIds, pointMap);
}
else
{
for(vtkIdType i=0; i < numIds; ++i)
{
newPtIds->InsertId(i, pointMap[ptIds->GetId(i)]);
}
}
// Store the new cell in the output.
output->InsertNextCell(input->GetCellType(cellId), newPtIds);
}
// Store the new set of points in the output.
output->SetPoints(newPts);
// Just pass cell data through because we still have the same number
// and type of cells.
output->GetCellData()->PassData(input->GetCellData());
// Avoid keeping extra memory around.
output->Squeeze();
delete [] pointMap;
return 1;
}
|