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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 362 363 364
|
#include "vtkCellDistanceSelector.h"
#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkIdList.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkDataSet.h"
#include "vtkUnstructuredGrid.h"
#include "vtkStructuredGrid.h"
#include "vtkPolyData.h"
#include "vtkCellLinks.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkCompositeDataSet.h"
#include "vtkCompositeDataIterator.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkIdTypeArray.h"
#include "vtkSmartPointer.h"
#include <map>
#include <vector>
vtkStandardNewMacro(vtkCellDistanceSelector);
// ----------------------------------------------------------------------
vtkCellDistanceSelector::vtkCellDistanceSelector()
{
this->Distance = 1;
this->IncludeSeed = 1;
this->AddIntermediate = 1;
this->SetNumberOfInputPorts( 2 );
}
// ----------------------------------------------------------------------
vtkCellDistanceSelector::~vtkCellDistanceSelector()
{
}
// ----------------------------------------------------------------------
void vtkCellDistanceSelector::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
}
// ----------------------------------------------------------------------
int vtkCellDistanceSelector::FillInputPortInformation( int port, vtkInformation* info )
{
switch ( port )
{
case INPUT_MESH:
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet" );
break;
case INPUT_SELECTION:
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection" );
break;
}
return 1;
}
// ----------------------------------------------------------------------
void vtkCellDistanceSelector::AddSelectionNode( vtkSelection* output,
vtkSmartPointer<vtkDataArray> outIndices,
int composite_index, int d )
{
vtkSmartPointer<vtkSelectionNode> outSelNode = vtkSmartPointer<vtkSelectionNode>::New();
outSelNode->SetContentType( vtkSelectionNode::INDICES );
outSelNode->SetFieldType( vtkSelectionNode::CELL );
outSelNode->GetProperties()->Set( vtkSelectionNode::COMPOSITE_INDEX(), composite_index );
// NB: Use HIERARCHICAL_LEVEL key to store distance to original cells
outSelNode->GetProperties()->Set( vtkSelectionNode::HIERARCHICAL_LEVEL(), d );
outSelNode->SetSelectionList( outIndices );
output->AddNode( outSelNode );
}
// ----------------------------------------------------------------------
int vtkCellDistanceSelector::RequestData( vtkInformation* vtkNotUsed( request ),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector )
{
// Retrieve input mesh as composite object
vtkInformation* inDataObjectInfo = inputVector[INPUT_MESH]->GetInformationObject( 0 );
vtkCompositeDataSet* compositeInput =
vtkCompositeDataSet::SafeDownCast( inDataObjectInfo->Get(vtkDataObject::DATA_OBJECT() ) );
// Retrieve input selection
vtkInformation* inSelectionInfo = inputVector[INPUT_SELECTION]->GetInformationObject( 0 );
vtkSelection* inputSelection =
vtkSelection::SafeDownCast( inSelectionInfo->Get( vtkDataObject::DATA_OBJECT() ) );
// Retrieve output selection
vtkInformation* outInfo = outputVector->GetInformationObject( 0 );
vtkSelection* output =
vtkSelection::SafeDownCast(outInfo->Get( vtkDataObject::DATA_OBJECT() ) );
if ( ! compositeInput )
{
vtkErrorMacro(<<"Missing input data object");
return 0;
}
if ( ! inputSelection )
{
vtkErrorMacro(<<"Missing input selection");
return 0;
}
std::map<int,std::vector<vtkSelectionNode*> > partSelections;
int nSelNodes = inputSelection->GetNumberOfNodes();
for ( int i = 0; i < nSelNodes; ++ i )
{
vtkSelectionNode* sn = inputSelection->GetNode( i );
int composite_index = sn->GetProperties()->Get(vtkSelectionNode::COMPOSITE_INDEX() ) ;
partSelections[composite_index].push_back( sn );
}
vtkCompositeDataIterator* inputIterator = compositeInput->NewIterator();
inputIterator->SkipEmptyNodesOn();
inputIterator->InitTraversal();
inputIterator->GoToFirstItem();
while ( ! inputIterator->IsDoneWithTraversal() )
{
vtkDataSet * input = vtkDataSet::SafeDownCast( inputIterator->GetCurrentDataObject() );
// NB: composite indices start at 1
int composite_index = inputIterator->GetCurrentFlatIndex();
inputIterator->GoToNextItem();
std::vector<vtkSelectionNode*>::iterator selNodeIt = partSelections[composite_index].begin();
while ( selNodeIt != partSelections[composite_index].end() )
{
vtkSelectionNode* selectionNode = *selNodeIt;
++ selNodeIt;
vtkDataArray* selectionList = vtkArrayDownCast<vtkDataArray>( selectionNode->GetSelectionList() );
vtkIdType numSeeds = selectionList->GetNumberOfTuples();
if ( numSeeds > 0
&& selectionNode->GetContentType() == vtkSelectionNode::INDICES
&& selectionNode->GetFieldType() == vtkSelectionNode::CELL
&& input->GetNumberOfCells() > 0 )
{
vtkIdType numCells = input->GetNumberOfCells();
vtkUnstructuredGrid* ug_input = vtkUnstructuredGrid::SafeDownCast( input );
vtkStructuredGrid* sg_input = vtkStructuredGrid::SafeDownCast( input );
vtkPolyData* pd_input = vtkPolyData::SafeDownCast( input);
vtkCellLinks * links = 0;
if ( ug_input )
{
if ( ! ug_input->GetCellLinks() )
{
ug_input->BuildLinks();
}
links = ug_input->GetCellLinks();
}
std::vector<int> flags( numCells, 0 );
vtkSmartPointer<vtkIdTypeArray> outIndices = vtkSmartPointer<vtkIdTypeArray>::New();
outIndices->SetNumberOfTuples( numSeeds );
int seedCount = 0;
for ( int i = 0; i < numSeeds; ++ i )
{
vtkIdType cellIndex = static_cast<vtkIdType> ( selectionList->GetTuple1( i ) );
if( cellIndex>=0 && cellIndex<numCells )
{
flags[cellIndex] = true;
outIndices->SetTuple1( seedCount++, cellIndex );
}
else
{
vtkWarningMacro(<<"Cell index out of bounds in selection ("<<cellIndex<<"/"<<numCells<<")\n");
}
}
outIndices->SetNumberOfTuples( seedCount );
vtkSmartPointer<vtkIdTypeArray> finalIndices = vtkSmartPointer<vtkIdTypeArray>::New();
vtkSmartPointer<vtkIntArray> cellDistance = vtkSmartPointer<vtkIntArray>::New();
cellDistance->SetName("Cell Distance");
// Iterate over increasing topological distance until desired distance is met
for ( int d = 0; d < this->Distance; ++ d )
{
vtkSmartPointer<vtkIdTypeArray> nextIndices = vtkSmartPointer<vtkIdTypeArray>::New();
if ( ug_input )
{
int nIndices = outIndices->GetNumberOfTuples();
for ( int i = 0; i < nIndices; ++ i )
{
vtkIdType cellIndex = static_cast<vtkIdType>( outIndices->GetTuple1( i ) );
vtkIdType * points;
vtkIdType n;
ug_input->GetCellPoints(cellIndex, n, points);
for ( int k = 0; k < n; ++ k )
{
vtkIdType pid = points[k];
int np = links->GetNcells( pid );
vtkIdType* cells = links->GetCells( pid );
for ( int j = 0; j < np; ++ j )
{
vtkIdType cid = cells[j];
if( cid >= 0 && cid < numCells )
{
if ( ! flags[cid] )
{
flags[cid] = true;
nextIndices->InsertNextValue( cid );
}
}
else
{
vtkWarningMacro(<<"Selection's cell index out of bounds ("<<cid<<"/"<<numCells<<")\n");
}
}
}
}
} // if ( ug_input )
else if ( pd_input )
{
pd_input->BuildLinks();
int nIndices = outIndices->GetNumberOfTuples();
for ( int i = 0; i < nIndices; ++ i )
{
vtkIdType cellIndex = static_cast<vtkIdType>( outIndices->GetTuple1( i) );
vtkIdType* points;
vtkIdType n;
pd_input->GetCellPoints(cellIndex, n, points);
for ( int k = 0; k < n; ++ k )
{
vtkIdType pid = points[k];
short unsigned int np;
vtkIdType* cells;
pd_input->GetPointCells(pid, np, cells);
for ( int j = 0; j < np; j++)
{
vtkIdType cid = cells[j];
if( cid>=0 && cid<numCells )
{
if (!flags[cid])
{
flags[cid] = true;
nextIndices->InsertNextValue(cid);
}
}
else
{
vtkWarningMacro(<<"Selection's cell index out of bounds ("<<cid<<"/"<<numCells<<")\n");
}
}
}
}
} // else if ( ug_input )
else if ( sg_input )
{
int dim[3];
sg_input->GetDimensions( dim );
-- dim[0];
-- dim[1];
-- dim[2];
int nIndices = outIndices->GetNumberOfTuples();
for ( int idx = 0; idx < nIndices; ++ idx )
{
vtkIdType cellIndex = static_cast<vtkIdType>( outIndices->GetTuple1( idx ) );
vtkIdType cellId = cellIndex;
vtkIdType ijk[3];
for ( int c = 0; c < 3; ++ c )
{
if ( dim[c] <= 1 )
{
ijk[c] = 0;
}
else
{
ijk[c] = cellId % dim[c];
cellId /= dim[c];
}
} // c
for ( int k = -1; k <= 1; ++ k )
{
for ( int j = -1; j <= 1; ++ j )
{
for ( int i = -1; i <= 1; ++ i )
{
int I = ijk[0] + i;
int J = ijk[1] + j;
int K = ijk[2] + k;
if ( I >= 0 && I < dim[0]
&& J >= 0 && J < dim[1]
&& K >= 0 && K < dim[2] )
{
cellId = I + J * dim[0] + K * dim[0] * dim[1];
if( cellId >= 0 && cellId < numCells )
{
if ( ! flags[cellId] )
{
flags[cellId] = true;
nextIndices->InsertNextValue( cellId );
}
}
else
{
vtkWarningMacro(<<"Selection's cell index out of bounds ("<<cellId<<"/"<<numCells<<")\n");
}
}
} // i
} // j
} // k
} // idx
} // else if ( sg_input )
else
{
vtkErrorMacro(<<"Unsupported data type : "<<input->GetClassName()<<"\n");
}
if ( ( ! d && this->IncludeSeed ) || ( d > 0 && this->AddIntermediate ) )
{
int ni = outIndices->GetNumberOfTuples();
for( int i = 0; i < ni; ++ i )
{
cellDistance->InsertNextTuple1( d );
finalIndices->InsertNextTuple1( outIndices->GetTuple1( i ) );
} // i
} // if ( ( ! d && this->IncludeSeed ) || ( d > 0 && this->AddIntermediate ) )
outIndices = nextIndices;
} // for ( int d = 0; d < this->Distance; ++ d )
// if( ( ! this->Distance && this->IncludeSeed ) || ( this->Distance > 0 && this->AddIntermediate ) )
if( ( ! this->Distance && this->IncludeSeed ) || this->Distance > 0 )
{
int ni = outIndices->GetNumberOfTuples();
cerr << "There are " << ni << " tuples\n";
for( int i = 0; i < ni; ++ i )
{
cellDistance->InsertNextTuple1( this->Distance );
finalIndices->InsertNextTuple1( outIndices->GetTuple1( i ) );
} // i
}
// Store selected cells for given seed cell
if( finalIndices->GetNumberOfTuples() > 0 )
{
vtkSmartPointer<vtkSelectionNode> outSelNode = vtkSmartPointer<vtkSelectionNode>::New();
outSelNode->SetContentType( vtkSelectionNode::INDICES );
outSelNode->SetFieldType( vtkSelectionNode::CELL );
outSelNode->GetProperties()->Set( vtkSelectionNode::COMPOSITE_INDEX(), composite_index );
outSelNode->SetSelectionList( finalIndices );
outSelNode->GetSelectionData()->AddArray( cellDistance );
output->AddNode( outSelNode );
}
} // if numSeeds > 0 etc.
} // while selNodeIt
} // while inputIterator
// Clean up
inputIterator->Delete();
return 1;
}
|