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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGenericDataSetTessellator.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 "vtkGenericDataSetTessellator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPointData.h"
#include "vtkTetra.h"
#include "vtkCellArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkIdTypeArray.h"
#include "vtkDoubleArray.h"
#include "vtkMergePoints.h"
#include "vtkGenericDataSet.h"
#include "vtkGenericCellIterator.h"
#include "vtkGenericAdaptorCell.h"
#include "vtkGenericAttributeCollection.h"
#include "vtkGenericAttribute.h"
#include "vtkCellData.h"
#include "vtkGenericCellTessellator.h"
#include "vtkIncrementalPointLocator.h"
vtkStandardNewMacro(vtkGenericDataSetTessellator);
vtkCxxSetObjectMacro(vtkGenericDataSetTessellator,Locator,vtkIncrementalPointLocator);
//----------------------------------------------------------------------------
//
vtkGenericDataSetTessellator::vtkGenericDataSetTessellator()
{
this->InternalPD = vtkPointData::New();
this->KeepCellIds = 1;
this->Merging = 1;
this->Locator = NULL;
}
//----------------------------------------------------------------------------
vtkGenericDataSetTessellator::~vtkGenericDataSetTessellator()
{
if ( this->Locator )
{
this->Locator->UnRegister(this);
this->Locator = NULL;
}
this->InternalPD->Delete();
}
//----------------------------------------------------------------------------
//
int vtkGenericDataSetTessellator::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
vtkGenericDataSet *input = vtkGenericDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDebugMacro(<< "Executing vtkGenericDataSetTessellator...");
// vtkGenericDataSet *input = this->GetInput();
// vtkUnstructuredGrid *output = this->GetOutput();
vtkIdType numPts = input->GetNumberOfPoints();
vtkIdType numCells = input->GetNumberOfCells();
vtkPointData *outputPD = output->GetPointData();
vtkCellData *outputCD = output->GetCellData();
vtkGenericAdaptorCell *cell;
vtkIdType numInserted=0, numNew, i;
vtkIdType npts, *pts;
int abortExecute=0;
// Copy original points and point data
vtkPoints *newPts = vtkPoints::New();
newPts->Allocate(2*numPts,numPts);
// loop over region
vtkUnsignedCharArray *types = vtkUnsignedCharArray::New();
types->Allocate(numCells);
vtkIdTypeArray *locs = vtkIdTypeArray::New();
locs->Allocate(numCells);
vtkCellArray *conn = vtkCellArray::New();
conn->Allocate(numCells);
// prepare the output attributes
vtkGenericAttributeCollection *attributes=input->GetAttributes();
vtkGenericAttribute *attribute;
vtkDataArray *attributeArray;
int c=attributes->GetNumberOfAttributes();
vtkDataSetAttributes *dsAttributes;
int attributeType;
i=0;
while(i<c)
{
attribute=attributes->GetAttribute(i);
attributeType=attribute->GetType();
if(attribute->GetCentering()==vtkPointCentered)
{
dsAttributes=outputPD;
attributeArray=vtkDataArray::CreateDataArray(attribute->GetComponentType());
attributeArray->SetNumberOfComponents(attribute->GetNumberOfComponents());
attributeArray->SetName(attribute->GetName());
this->InternalPD->AddArray(attributeArray);
attributeArray->Delete();
if(this->InternalPD->GetAttribute(attributeType)==0)
{
this->InternalPD->SetActiveAttribute(this->InternalPD->GetNumberOfArrays()-1,attributeType);
}
}
else // vtkCellCentered
{
dsAttributes=outputCD;
}
attributeArray=vtkDataArray::CreateDataArray(attribute->GetComponentType());
attributeArray->SetNumberOfComponents(attribute->GetNumberOfComponents());
attributeArray->SetName(attribute->GetName());
dsAttributes->AddArray(attributeArray);
attributeArray->Delete();
if(dsAttributes->GetAttribute(attributeType)==0)
{
dsAttributes->SetActiveAttribute(dsAttributes->GetNumberOfArrays()-1,attributeType);
}
++i;
}
vtkIdTypeArray *cellIdArray=0;
if(this->KeepCellIds)
{
cellIdArray=vtkIdTypeArray::New();
cellIdArray->SetName("OriginalIds");
}
vtkGenericCellIterator *cellIt = input->NewCellIterator();
vtkIdType updateCount = numCells/20 + 1; // update roughly every 5%
vtkIdType count = 0;
input->GetTessellator()->InitErrorMetrics(input);
vtkIncrementalPointLocator *locator=0;
if ( this->Merging )
{
if ( this->Locator == NULL )
{
this->CreateDefaultLocator();
}
this->Locator->InitPointInsertion (newPts, input->GetBounds());
locator=this->Locator;
}
for(cellIt->Begin(); !cellIt->IsAtEnd() && !abortExecute; cellIt->Next(), count++)
{
if ( !(count % updateCount) )
{
this->UpdateProgress(static_cast<double>(count) / numCells);
abortExecute = this->GetAbortExecute();
}
cell = cellIt->GetCell();
cell->Tessellate(input->GetAttributes(), input->GetTessellator(),
newPts, locator, conn, this->InternalPD,outputPD,
outputCD,types);
numNew = conn->GetNumberOfCells() - numInserted;
numInserted = conn->GetNumberOfCells();
vtkIdType cellId=cell->GetId();
if(this->KeepCellIds)
{
for(i=0;i<numNew;i++)
{
cellIdArray->InsertNextValue(cellId);
}
}
for (i=0; i < numNew; i++)
{
locs->InsertNextValue(conn->GetTraversalLocation());
conn->GetNextCell(npts,pts); //side effect updates traversal location
} //insert each new cell
} //for all cells
cellIt->Delete();
// Send to the output
if(this->KeepCellIds)
{
outputCD->AddArray(cellIdArray);
cellIdArray->Delete();
}
output->SetPoints(newPts);
output->SetCells(types, locs, conn);
if (!this->Merging && this->Locator)
{
this->Locator->Initialize();
}
vtkDebugMacro(<<"Subdivided " << numCells << " cells to produce "
<< conn->GetNumberOfCells() << "new cells");
newPts->Delete();
types->Delete();
locs->Delete();
conn->Delete();
output->Squeeze();
return 1;
}
//----------------------------------------------------------------------------
int vtkGenericDataSetTessellator::FillInputPortInformation(
int port,
vtkInformation* info)
{
if(!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGenericDataSet");
return 1;
}
//----------------------------------------------------------------------------
// Specify a spatial locator for merging points. By
// default an instance of vtkMergePoints is used.
void vtkGenericDataSetTessellator::CreateDefaultLocator()
{
if ( this->Locator == NULL )
{
this->Locator = vtkMergePoints::New();
}
}
//----------------------------------------------------------------------------
void vtkGenericDataSetTessellator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "keep cells ids=";
if(this->KeepCellIds)
{
os << "true" << endl;
}
else
{
os << "false" << endl;
}
os << indent << "Merging: " << (this->Merging ? "On\n" : "Off\n");
if ( this->Locator )
{
os << indent << "Locator: " << this->Locator << "\n";
}
else
{
os << indent << "Locator: (none)\n";
}
}
//----------------------------------------------------------------------------
vtkMTimeType vtkGenericDataSetTessellator::GetMTime()
{
vtkMTimeType mTime = this->Superclass::GetMTime();
vtkMTimeType time;
if ( this->Locator != NULL )
{
time = this->Locator->GetMTime();
mTime = ( time > mTime ? time : mTime );
}
return mTime;
}
|