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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSliceAlongPolyPlane.h
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 "vtkSliceAlongPolyPlane.h"
#include "vtkAppendArcLength.h"
#include "vtkCellArray.h"
#include "vtkCleanPolyData.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCutter.h"
#include "vtkDataSetSurfaceFilter.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkLine.h"
#include "vtkMPIMoveData.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyLine.h"
#include "vtkPolyPlane.h"
#include "vtkProbeFilter.h"
#include "vtkSmartPointer.h"
#include "vtkThreshold.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkType.h"
#include <list>
#include <algorithm>
#include <iostream>
vtkStandardNewMacro(vtkSliceAlongPolyPlane)
//----------------------------------------------------------------------------
void vtkSliceAlongPolyPlane::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Tolerance: " << this->Tolerance << endl;
}
//----------------------------------------------------------------------------
vtkSliceAlongPolyPlane::vtkSliceAlongPolyPlane()
{
this->SetNumberOfInputPorts(2);
this->Tolerance = 10;
}
//----------------------------------------------------------------------------
vtkSliceAlongPolyPlane::~vtkSliceAlongPolyPlane()
{
}
//----------------------------------------------------------------------------
int vtkSliceAlongPolyPlane::RequestDataObject(
vtkInformation*,
vtkInformationVector** inputVector ,
vtkInformationVector* outputVector)
{
// If input is vtkCompositeDataSet, output will be a vtkMultiBlockDataSet
// otherwise it will be a vtkPolyData.
vtkDataObject* inputDO = vtkDataObject::GetData(inputVector[0], 0);
if (vtkCompositeDataSet::SafeDownCast(inputDO))
{
if (vtkMultiBlockDataSet::GetData(outputVector, 0) == NULL)
{
vtkNew<vtkMultiBlockDataSet> output;
outputVector->GetInformationObject(0)->Set(
vtkDataObject::DATA_OBJECT(), output.GetPointer());
}
return 1;
}
else if (vtkDataSet::GetData(inputVector[0], 0))
{
if (vtkPolyData::GetData(outputVector, 0) == NULL)
{
vtkNew<vtkPolyData> output;
outputVector->GetInformationObject(0)->Set(
vtkDataObject::DATA_OBJECT(), output.GetPointer());
}
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkSliceAlongPolyPlane::FillInputPortInformation(int port, vtkInformation *info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
// A struct that holds the id and direction of a polyline to be appended
typedef struct {
vtkIdType id;
bool forward;
} linePiece;
//----------------------------------------------------------------------------
static bool operator==(const linePiece &a, const linePiece &b)
{
return a.id == b.id;
}
//----------------------------------------------------------------------------
static void appendLinesIntoOnePolyLine(vtkPolyData* input, vtkPolyData *output)
{
if (input->GetNumberOfPoints() == 0 || input->GetNumberOfCells() == 0)
{
output->ShallowCopy(input);
return;
}
output->Initialize();
output->Allocate();
// copy points to the output
output->GetPointData()->ShallowCopy(input->GetPointData());
vtkSmartPointer< vtkPoints > points =
vtkSmartPointer< vtkPoints >::New();
points->ShallowCopy(input->GetPoints());
output->SetPoints(points);
vtkIdType numCells = input->GetNumberOfCells();
// make sure the first cell is a line or polyline
vtkCell* first = input->GetCell(0);
vtkLine* firstLine = vtkLine::SafeDownCast(first);
vtkPolyLine* firstPolyLine = vtkPolyLine::SafeDownCast(first);
if (!firstLine && !firstPolyLine)
{
// if not it is an error
return;
}
linePiece firstPiece = {0,true};
vtkIdType totalPointsInLine = first->GetNumberOfPoints();
std::list< linePiece > pieceList;
pieceList.push_back(firstPiece);
bool foundANewPiece = true;
while (foundANewPiece)
{
foundANewPiece = false;
// look for pieces that end at the start of the first segment
{
vtkCell* frontCell = input->GetCell(pieceList.front().id);
vtkIdType frontPointNumberInCell =
pieceList.front().forward ? 0 : frontCell->GetNumberOfPoints() - 1;
vtkIdType firstPoint = frontCell->GetPointId(frontPointNumberInCell);
for (int i = 0 ; i < numCells; ++i)
{
vtkCell* c = input->GetCell(i);
if (!vtkPolyLine::SafeDownCast(c) && !vtkLine::SafeDownCast(c))
{
continue;
}
if (c->GetPointId(0) == firstPoint || c->GetPointId(c->GetNumberOfPoints() - 1) == firstPoint)
{
linePiece l = {i, c->GetPointId(0) != firstPoint};
if (std::find(pieceList.begin(),pieceList.end(),l) == pieceList.end())
{
pieceList.push_front(l);
foundANewPiece = true;
totalPointsInLine += c->GetNumberOfPoints() - 1;
break;
}
}
}
}
// look for pieces that start at the end of the last segment
{
vtkCell* backCell = input->GetCell(pieceList.back().id);
vtkIdType backPointNumberInCell =
pieceList.back().forward ? backCell->GetNumberOfPoints() - 1 : 0;
vtkIdType lastPoint = backCell->GetPointId(backPointNumberInCell);
for (int i = 0; i < numCells; ++i)
{
vtkCell* c = input->GetCell(i);
if (!vtkPolyLine::SafeDownCast(c) && !vtkLine::SafeDownCast(c))
{
continue;
}
if (c->GetPointId(0) == lastPoint || c->GetPointId(c->GetNumberOfPoints() - 1 == lastPoint))
{
linePiece l = {i, c->GetPointId(0) == lastPoint};
if (std::find(pieceList.begin(),pieceList.end(),l) == pieceList.end())
{
pieceList.push_back(l);
foundANewPiece = true;
totalPointsInLine += c->GetNumberOfPoints() - 1;
break;
}
}
}
}
}
vtkSmartPointer< vtkIdList > idList = vtkSmartPointer< vtkIdList >::New();
idList->SetNumberOfIds(totalPointsInLine);
vtkIdType current = 0;
// Append the lines into a polyline
for (std::list<linePiece>::iterator itr = pieceList.begin(); itr != pieceList.end();++itr)
{
vtkCell* cell = input->GetCell((*itr).id);
vtkIdType start = (*itr).forward ? 0 : cell->GetNumberOfPoints() - 1;
vtkIdType end = (*itr).forward ? cell->GetNumberOfPoints() : -1;
vtkIdType increment = (*itr).forward ? 1 : -1;
// except for the first time, we already have the first point,
// it was the last point in the previous cell
if (current != 0)
{
start += increment;
}
for (vtkIdType i = start; i != end; i += increment)
{
vtkIdType id = cell->GetPointId(i);
assert(id < input->GetNumberOfPoints());
idList->SetId(current,id);
++current;
}
}
assert(current == totalPointsInLine);
output->InsertNextCell(VTK_POLY_LINE,idList);
}
//----------------------------------------------------------------------------
void vtkSliceAlongPolyPlane::CleanPolyLine(
vtkPolyData* input, vtkPolyData* output)
{
output->ShallowCopy(input);
// remove other cell types that we don't care about.
output->SetVerts(NULL);
output->SetPolys(NULL);
output->SetVerts(NULL);
vtkMultiProcessController* controller = vtkMultiProcessController::GetGlobalController();
if (controller && controller->GetNumberOfProcesses() > 1)
{
// communicate this dataset among all processes. This will ensure that the
// polyline is cloned and communicated to all ranks.
vtkNew<vtkMPIMoveData> moveData;
moveData->SetController(controller);
moveData->SetServerToDataServer();
moveData->SetMoveModeToClone();
moveData->SetOutputDataType(VTK_POLY_DATA);
moveData->SetInputData(output);
moveData->Update();
output->ShallowCopy(moveData->GetOutputDataObject(0));
}
// this method conditions the input polydata to extract a single polyline from
// it. It also handles parallel use-cases.
vtkNew< vtkCleanPolyData > cleanFilter;
cleanFilter->SetInputData(output);
cleanFilter->Update();
appendLinesIntoOnePolyLine(cleanFilter->GetOutput(), output);
}
//----------------------------------------------------------------------------
int vtkSliceAlongPolyPlane::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkNew<vtkPolyData> polyLinePD;
this->CleanPolyLine(vtkPolyData::GetData(inputVector[1], 0), polyLinePD.GetPointer());
if (vtkPolyLine::SafeDownCast(polyLinePD->GetCell(0)) == NULL)
{
vtkErrorMacro(<< " First cell in input polydata is not a vtkPolyLine.");
return 0;
}
vtkDataObject* inputDO = vtkDataObject::GetData(inputVector[0], 0);
if (vtkCompositeDataSet* cd = vtkCompositeDataSet::SafeDownCast(inputDO))
{
vtkCompositeDataSet* output = vtkCompositeDataSet::GetData(outputVector, 0);
assert(output);
output->CopyStructure(cd);
vtkSmartPointer<vtkCompositeDataIterator> iter;
iter.TakeReference(cd->NewIterator());
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
if (vtkDataSet* ds = vtkDataSet::SafeDownCast(iter->GetCurrentDataObject()))
{
vtkNew<vtkPolyData> pd;
if (!this->Execute(ds, polyLinePD.GetPointer(), pd.GetPointer()))
{
return 0;
}
output->SetDataSet(iter, pd.GetPointer());
}
}
return 1;
}
else if (vtkDataSet* ds = vtkDataSet::SafeDownCast(inputDO))
{
vtkPolyData* output = vtkPolyData::GetData(outputVector, 0);
assert(output);
return this->Execute(ds, polyLinePD.GetPointer(), output)? 1 : 0;
}
return 0;
}
//----------------------------------------------------------------------------
bool vtkSliceAlongPolyPlane::Execute(
vtkDataSet* inputDataset, vtkPolyData* lineDataSet, vtkPolyData* output)
{
assert(inputDataset && lineDataSet && output);
vtkPolyLine* line = vtkPolyLine::SafeDownCast(lineDataSet->GetCell(0));
assert(line);
vtkNew< vtkPolyPlane > planes;
planes->SetPolyLine(line);
vtkNew< vtkCutter > cutter;
cutter->SetCutFunction(planes.GetPointer());
cutter->SetInputData(inputDataset);
cutter->Update();
vtkDataSet* cutterOutput = vtkDataSet::SafeDownCast(cutter->GetOutputDataObject(0));
if (cutterOutput == NULL || cutterOutput->GetNumberOfPoints() == 0)
{
// shortcut if the cut produces an empty dataset.
return 1;
}
vtkNew< vtkTransformPolyDataFilter > transformOutputFilter;
vtkSmartPointer< vtkTransform > transform = vtkSmartPointer< vtkTransform >::New();
transform->Identity();
transform->Scale(1,1,0);
transformOutputFilter->SetTransform(transform);
transformOutputFilter->SetInputData(cutterOutput);
transformOutputFilter->Update();
vtkNew< vtkAppendArcLength > arcLengthFilter;
arcLengthFilter->SetInputData(lineDataSet);
arcLengthFilter->Update();
vtkNew< vtkTransformPolyDataFilter > transformLineFilter;
transformLineFilter->SetTransform(transform);
transformLineFilter->SetInputConnection(arcLengthFilter->GetOutputPort());
transformLineFilter->Update();
vtkNew< vtkProbeFilter > probe;
probe->SetInputConnection(transformOutputFilter->GetOutputPort());
probe->SetSourceConnection(transformLineFilter->GetOutputPort());
probe->SetPassPointArrays(1);
probe->SetPassCellArrays(1);
probe->SetValidPointMaskArrayName("vtkValidPointMaskArrayName");
probe->ComputeToleranceOff();
probe->SetTolerance(this->Tolerance);
probe->Update();
vtkNew< vtkPolyData > combinationPolyData;
combinationPolyData->ShallowCopy(cutter->GetOutput());
combinationPolyData->GetPointData()->ShallowCopy(
probe->GetOutput()->GetPointData());
vtkNew< vtkThreshold > threshold;
threshold->SetInputData(combinationPolyData.GetPointer());
threshold->SetInputArrayToProcess(
0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,"vtkValidPointMaskArrayName");
threshold->ThresholdBetween(0.5,1.5);
threshold->Update();
vtkNew< vtkDataSetSurfaceFilter > toPolyData;
toPolyData->SetInputConnection(threshold->GetOutputPort());
toPolyData->Update();
output->ShallowCopy(toPolyData->GetOutput());
output->GetPointData()->RemoveArray("functionValues");
output->GetPointData()->RemoveArray("vtkValidPointMaskArrayName");
return true;
}
|