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 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAMRCutPlane.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 "vtkAMRCutPlane.h"
#include "vtkObjectFactory.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkMultiProcessController.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIndent.h"
#include "vtkPlane.h"
#include "vtkAMRUtilities.h"
#include "vtkUniformGrid.h"
#include "vtkCutter.h"
#include "vtkUnstructuredGrid.h"
#include "vtkIdList.h"
#include "vtkDoubleArray.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkOverlappingAMR.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include <cassert>
#include <algorithm>
vtkStandardNewMacro(vtkAMRCutPlane);
//------------------------------------------------------------------------------
vtkAMRCutPlane::vtkAMRCutPlane()
{
this->SetNumberOfInputPorts( 1 );
this->SetNumberOfOutputPorts( 1 );
this->LevelOfResolution = 0;
this->initialRequest = true;
for( int i=0; i < 3; ++i )
{
this->Center[i] = 0.0;
this->Normal[i] = 0.0;
}
this->Controller = vtkMultiProcessController::GetGlobalController();
this->UseNativeCutter = 1;
}
//------------------------------------------------------------------------------
vtkAMRCutPlane::~vtkAMRCutPlane()
{
this->BlocksToLoad.clear();
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::PrintSelf( std::ostream &oss, vtkIndent indent )
{
this->Superclass::PrintSelf( oss, indent );
oss << indent << "LevelOfResolution: "
<< this->LevelOfResolution << endl;
oss << indent << "UseNativeCutter: "
<< this->UseNativeCutter << endl;
oss << indent << "Controller: "
<< this->Controller << endl;
oss << indent << "Center: ";
for( int i=0; i < 3; ++i )
{
oss << this->Center[i ] << " ";
}
oss << endl;
oss << indent << "Normal: ";
for( int i=0; i < 3; ++i )
{
oss << this->Normal[i] << " ";
}
oss << endl;
}
//------------------------------------------------------------------------------
int vtkAMRCutPlane::FillInputPortInformation(
int vtkNotUsed(port), vtkInformation *info )
{
assert( "pre: information object is NULL!" && (info != NULL) );
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),"vtkOverlappingAMR");
return 1;
}
//------------------------------------------------------------------------------
int vtkAMRCutPlane::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation *info )
{
assert( "pre: information object is NULL!" && (info != NULL) );
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet" );
return 1;
}
//------------------------------------------------------------------------------
int vtkAMRCutPlane::RequestInformation(
vtkInformation* vtkNotUsed(rqst), vtkInformationVector** inputVector,
vtkInformationVector* vtkNotUsed(outputVector) )
{
this->BlocksToLoad.clear();
vtkInformation *input = inputVector[0]->GetInformationObject(0);
assert( "pre: input information object is NULL" && (input != NULL) );
if( input->Has(vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA() ) )
{
vtkOverlappingAMR *metadata =
vtkOverlappingAMR::SafeDownCast(
input->Get(vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA()));
vtkPlane *cutPlane = this->GetCutPlane( metadata );
assert( "Cut plane is NULL" && (cutPlane != NULL) );
this->ComputeAMRBlocksToLoad(cutPlane, metadata);
cutPlane->Delete();
}
this->Modified();
return 1;
}
//------------------------------------------------------------------------------
int vtkAMRCutPlane::RequestUpdateExtent(
vtkInformation* vtkNotUsed(rqst), vtkInformationVector** inputVector,
vtkInformationVector* vtkNotUsed(outputVector) )
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
assert( "pre: inInfo is NULL" && (inInfo != NULL) );
inInfo->Set(
vtkCompositeDataPipeline::UPDATE_COMPOSITE_INDICES(),
&this->BlocksToLoad[0], static_cast<int>(this->BlocksToLoad.size()));
return 1;
}
//------------------------------------------------------------------------------
int vtkAMRCutPlane::RequestData(
vtkInformation* vtkNotUsed(rqst), vtkInformationVector** inputVector,
vtkInformationVector* outputVector )
{
// STEP 0: Get input object
vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
assert( "pre: input information object is NULL" && (input != NULL) );
vtkOverlappingAMR *inputAMR=
vtkOverlappingAMR::SafeDownCast(
input->Get( vtkDataObject::DATA_OBJECT() ) );
assert( "pre: input AMR dataset is NULL!" && (inputAMR != NULL) );
// STEP 1: Get output object
vtkInformation *output = outputVector->GetInformationObject( 0 );
assert( "pre: output information is NULL" && (output != NULL) );
vtkMultiBlockDataSet *mbds=
vtkMultiBlockDataSet::SafeDownCast(
output->Get( vtkDataObject::DATA_OBJECT() ) );
assert( "pre: output multi-block dataset is NULL" && (mbds != NULL) );
if( this->IsAMRData2D( inputAMR ) )
{
// Return an empty multi-block, we cannot cut a 2-D dataset
return 1;
}
vtkPlane *cutPlane = this->GetCutPlane( inputAMR );
assert("pre: cutPlane should not be NULL!" && (cutPlane != NULL) );
unsigned int blockIdx = 0;
unsigned int level = 0;
for( ; level < inputAMR->GetNumberOfLevels(); ++level )
{
unsigned int dataIdx = 0;
for( ; dataIdx < inputAMR->GetNumberOfDataSets( level ); ++dataIdx )
{
vtkUniformGrid *grid = inputAMR->GetDataSet( level, dataIdx );
if( this->UseNativeCutter == 1 )
{
if( grid != NULL )
{
vtkCutter *myCutter = vtkCutter::New();
myCutter->SetInputData( grid );
myCutter->SetCutFunction( cutPlane );
myCutter->Update();
mbds->SetBlock( blockIdx, myCutter->GetOutput( ) );
++blockIdx;
myCutter->Delete();
}
else
{
mbds->SetBlock(blockIdx,NULL);
++blockIdx;
}
}
else
{
if( grid != NULL )
{
this->CutAMRBlock( cutPlane, blockIdx, grid, mbds );
++blockIdx;
}
else
{
mbds->SetBlock(blockIdx,NULL);
++blockIdx;
}
}
} // END for all data
} // END for all levels
cutPlane->Delete();
return 1;
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::CutAMRBlock(
vtkPlane *cutPlane,
unsigned int blockIdx, vtkUniformGrid *grid, vtkMultiBlockDataSet *output )
{
assert("pre: multiblock output object is NULL!" && (output != NULL));
assert("pre: grid is NULL" && (grid != NULL) );
vtkUnstructuredGrid *mesh = vtkUnstructuredGrid::New();
vtkPoints *meshPts = vtkPoints::New();
meshPts->SetDataTypeToDouble();
vtkCellArray *cells = vtkCellArray::New();
// Maps points from the input grid to the output grid
std::map< vtkIdType, vtkIdType > grdPntMapping;
std::vector< vtkIdType > extractedCells;
vtkIdType cellIdx = 0;
for( ; cellIdx < grid->GetNumberOfCells(); ++cellIdx )
{
if( grid->IsCellVisible( cellIdx ) &&
this->PlaneIntersectsCell( cutPlane, grid->GetCell(cellIdx) ) )
{
extractedCells.push_back( cellIdx );
this->ExtractCellFromGrid(
grid,grid->GetCell(cellIdx),grdPntMapping,meshPts,cells );
} // END if
} // END for all cells
// Sanity checks
assert("post: Number of mesh points should match map size!" &&
(static_cast<int>(grdPntMapping.size())==meshPts->GetNumberOfPoints()));
assert("post: Number of cells mismatch"&&
(cells->GetNumberOfCells()==static_cast<int>(extractedCells.size())));
// Insert the points
mesh->SetPoints( meshPts );
meshPts->Delete();
std::vector<int> types;
if( grid->GetDataDimension() == 3 )
{
types.resize( cells->GetNumberOfCells(), VTK_VOXEL );
}
else
{
vtkErrorMacro("Cannot cut a grid of dimension=" << grid->GetDataDimension());
output->SetBlock( blockIdx, NULL );
return;
}
// Insert the cells
mesh->SetCells( &types[0], cells );
cells->Delete();
// Extract fields
this->ExtractPointDataFromGrid(
grid,grdPntMapping,mesh->GetNumberOfPoints(),mesh->GetPointData());
this->ExtractCellDataFromGrid(
grid,extractedCells,mesh->GetCellData());
output->SetBlock( blockIdx, mesh );
mesh->Delete();
grdPntMapping.clear();
extractedCells.clear();
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::ExtractCellFromGrid(
vtkUniformGrid *grid,
vtkCell* cell, std::map< vtkIdType, vtkIdType >& grdPntMapping,
vtkPoints *nodes,
vtkCellArray *cells)
{
assert( "pre: grid is NULL" && (grid != NULL) );
assert( "pre: cell is NULL" && (cell != NULL) );
assert( "pre: cells is NULL" && (cells != NULL) );
cells->InsertNextCell( cell->GetNumberOfPoints() );
vtkIdType nodeIdx = 0;
for( ; nodeIdx < cell->GetNumberOfPoints(); ++nodeIdx )
{
// Get the point ID w.r.t. the grid
vtkIdType meshPntIdx = cell->GetPointId( nodeIdx );
assert( "pre: mesh point ID should within grid range point ID" &&
(meshPntIdx < grid->GetNumberOfPoints()));
if( grdPntMapping.find( meshPntIdx ) != grdPntMapping.end() )
{
// Point already exists in nodes
cells->InsertCellPoint( grdPntMapping[meshPntIdx] );
}
else
{
// Push point to the end of the list
vtkIdType nidx = nodes->GetNumberOfPoints();
double *pnt = grid->GetPoint(meshPntIdx);
nodes->InsertPoint( nidx, pnt );
assert("post: number of points should be increased by 1" &&
(nodes->GetNumberOfPoints()==(nidx+1)));
grdPntMapping[ meshPntIdx ] = nidx;
cells->InsertCellPoint( nidx );
}
} // END for all nodes
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::ExtractPointDataFromGrid(
vtkUniformGrid *grid,
std::map<vtkIdType,vtkIdType>& gridPntMapping,
vtkIdType NumNodes,
vtkPointData *PD)
{
assert("pre: grid is NULL!" && (grid != NULL) );
assert("pre: target point data is NULL!" && (PD != NULL) );
if( (grid->GetPointData()->GetNumberOfArrays()==0) ||
(gridPntMapping.size() == 0))
{
// Nothing to extract short-circuit here
return;
}
vtkPointData *GPD = grid->GetPointData();
for( int fieldArray=0; fieldArray < GPD->GetNumberOfArrays(); ++fieldArray)
{
vtkDataArray *sourceArray = GPD->GetArray(fieldArray);
int dataType = sourceArray->GetDataType();
vtkDataArray *array = vtkDataArray::CreateDataArray( dataType );
assert( "pre: failed to create array!" && (array != NULL) );
array->SetName(sourceArray->GetName());
array->SetNumberOfComponents(sourceArray->GetNumberOfComponents());
array->SetNumberOfTuples(NumNodes);
// Copy tuples from source array
std::map<vtkIdType,vtkIdType>::iterator iter = gridPntMapping.begin();
for( ; iter != gridPntMapping.end(); ++iter )
{
vtkIdType srcIdx = iter->first;
vtkIdType targetIdx = iter->second;
assert( "pre: source node index is out-of-bounds" &&
(srcIdx >= 0) && (srcIdx < grid->GetNumberOfPoints()) );
assert( "pre: target node index is out-of-bounds" &&
(targetIdx >=0) && (targetIdx < NumNodes) );
array->SetTuple( targetIdx, srcIdx, sourceArray );
} // END for all extracted nodes
PD->AddArray( array );
array->Delete();
} // END for all arrays
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::ExtractCellDataFromGrid(
vtkUniformGrid *grid,
std::vector<vtkIdType>& cellIdxList,
vtkCellData *CD)
{
assert("pre: grid is NULL!" && (grid != NULL) );
assert("pre: target cell data is NULL!" && (CD != NULL) );
if( (grid->GetCellData()->GetNumberOfArrays()==0) ||
(cellIdxList.size()==0) )
{
// Nothing to extract short-circuit here
return;
}
int NumCells = static_cast<int>(cellIdxList.size());
vtkCellData *GCD = grid->GetCellData();
for( int fieldArray=0; fieldArray < GCD->GetNumberOfArrays(); ++fieldArray)
{
vtkDataArray *sourceArray = GCD->GetArray(fieldArray);
int dataType = sourceArray->GetDataType();
vtkDataArray *array = vtkDataArray::CreateDataArray(dataType);
assert( "pre: failed to create array!" && (array != NULL) );
array->SetName(sourceArray->GetName());
array->SetNumberOfComponents(sourceArray->GetNumberOfComponents());
array->SetNumberOfTuples(NumCells);
// Copy tuples from source array
for( int i=0; i < NumCells; ++i )
{
vtkIdType cellIdx = cellIdxList[ i ];
assert( "pre: cell index is out-of-bounds!" &&
(cellIdx >= 0) && (cellIdx < grid->GetNumberOfCells()) );
array->SetTuple(i,cellIdx,sourceArray);
} // END for all extracted cells
CD->AddArray( array );
array->Delete();
} // END for all arrays
}
//------------------------------------------------------------------------------
vtkPlane* vtkAMRCutPlane::GetCutPlane( vtkOverlappingAMR *metadata )
{
assert( "pre: metadata is NULL" && (metadata != NULL) );
vtkPlane *pl = vtkPlane::New();
double bounds[6];
metadata->GetBounds(bounds);
// Get global bounds
double minBounds[3] = {bounds[0],bounds[2],bounds[4]};
double maxBounds[3] = {bounds[1],bounds[3],bounds[5]};
this->InitializeCenter( minBounds, maxBounds );
pl->SetNormal( this->Normal );
pl->SetOrigin( this->Center );
return( pl );
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::ComputeAMRBlocksToLoad(
vtkPlane* p, vtkOverlappingAMR *m)
{
assert( "pre: Plane object is NULL" && (p != NULL) );
assert( "pre: metadata is NULL" && (m != NULL) );
// Store A,B,C,D from the plane equation
double plane[4];
plane[0] = p->GetNormal()[0];
plane[1] = p->GetNormal()[1];
plane[2] = p->GetNormal()[2];
plane[3] = p->GetNormal()[0]*p->GetOrigin()[0] +
p->GetNormal()[1]*p->GetOrigin()[1] +
p->GetNormal()[2]*p->GetOrigin()[2];
double bounds[6];
int NumLevels = m->GetNumberOfLevels();
int maxLevelToLoad =
(this->LevelOfResolution < NumLevels )?
this->LevelOfResolution : NumLevels;
unsigned int level = 0;
for( ; level <= static_cast<unsigned int>(maxLevelToLoad); ++level )
{
unsigned int dataIdx = 0;
for( ; dataIdx < m->GetNumberOfDataSets( level ); ++dataIdx )
{
m->GetBounds( level, dataIdx, bounds);
if( this->PlaneIntersectsAMRBox( plane, bounds ) )
{
unsigned int amrGridIdx = m->GetCompositeIndex(level,dataIdx);
this->BlocksToLoad.push_back( amrGridIdx );
}
} // END for all data
} // END for all levels
std::sort( this->BlocksToLoad.begin(), this->BlocksToLoad.end() );
}
//------------------------------------------------------------------------------
void vtkAMRCutPlane::InitializeCenter( double min[3], double max[3] )
{
if( !this->initialRequest )
{
return;
}
this->Center[0] = 0.5*( max[0]-min[0] );
this->Center[1] = 0.5*( max[1]-min[1] );
this->Center[2] = 0.5*( max[2]-min[2] );
this->initialRequest = false;
}
//------------------------------------------------------------------------------
bool vtkAMRCutPlane::PlaneIntersectsCell( vtkPlane *pl, vtkCell *cell )
{
assert( "pre: plane is NULL" && (pl != NULL) );
assert( "pre: cell is NULL!" && (cell != NULL) );
return( this->PlaneIntersectsAMRBox( pl, cell->GetBounds() ) );
}
//------------------------------------------------------------------------------
bool vtkAMRCutPlane::PlaneIntersectsAMRBox(vtkPlane *pl, double bounds[6] )
{
assert( "pre: plane is NULL" && (pl != NULL) );
// Store A,B,C,D from the plane equation
double plane[4];
plane[0] = pl->GetNormal()[0];
plane[1] = pl->GetNormal()[1];
plane[2] = pl->GetNormal()[2];
plane[3] = pl->GetNormal()[0]*pl->GetOrigin()[0] +
pl->GetNormal()[1]*pl->GetOrigin()[1] +
pl->GetNormal()[2]*pl->GetOrigin()[2];
return( this->PlaneIntersectsAMRBox( plane,bounds) );
}
//------------------------------------------------------------------------------
bool vtkAMRCutPlane::PlaneIntersectsAMRBox( double plane[4], double bounds[6] )
{
bool lowPnt = false;
bool highPnt = false;
for( int i=0; i < 8; ++i )
{
// Get box coordinates
double x = ( i&1 ) ? bounds[1] : bounds[0];
double y = ( i&2 ) ? bounds[3] : bounds[2];
double z = ( i&3 ) ? bounds[5] : bounds[4];
// Plug-in coordinates to the plane equation
double v = plane[3] - plane[0]*x - plane[1]*y - plane[2]*z;
if( v == 0.0 ) // Point is on a plane
{
return true;
}
if( v < 0.0 )
{
lowPnt = true;
}
else
{
highPnt = true;
}
if( lowPnt && highPnt )
{
return true;
}
}
return false;
}
//------------------------------------------------------------------------------
bool vtkAMRCutPlane::IsAMRData2D( vtkOverlappingAMR *input )
{
assert( "pre: Input AMR dataset is NULL" && (input != NULL) );
if( input->GetGridDescription() != VTK_XYZ_GRID )
{
return true;
}
return false;
}
|