Store Polydata

Some Special polydata can’t be stored by vtkSTLWriter.
The following polydata contains points and a line in its structure, it can’t be saved by vtkSTLWriter, but vtkXMLPolyDataWriter works.

    vtkObject::GlobalWarningDisplayOn();

    int numPts = 10;
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    int offset = numPts / 4;
    for (int i = 0; i < numPts; i++)
    {
        const double angle = 2.0*vtkMath::Pi()*(i - offset)/numPts;
        points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle), 0.1*sin(angle), 0.0 );
    }

    // Create a cell array to connect the points into meaningful geometry
    vtkIdType* vertexIndices = new vtkIdType[numPts];
    for (int i = 0; i < numPts; i++) { vertexIndices[i] = static_cast<vtkIdType>(i); }

    vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
    lines->InsertNextCell(numPts, vertexIndices);

    // Create polydata to hold the geometry just created, and populate it
    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(points);
    polydata->SetLines(lines);

The I/O operations for vtkXMLPolyDataWriter.

vtkSmartPointer<vtkXMLPolyDataWriter> writer =
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName( "curve.vtp" );
writer->SetInputData( polydata );   
writer->Write();


vtkSmartPointer<vtkXMLPolyDataReader> reader =
    vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName("curve.vtp");
reader->Update();
vtkPolyData *polydata = reader->GetOutput();

SetCells

vtkSPtrNew( splinePd, vtkPolyData );
splinePd->SetPoints( betaPoints );
vtkSPtrNew( splineCells, vtkCellArray );
for( int i = 0; i < betaPoints->GetNumberOfPoints()-1; ++i )
{
    vtkIdType ids[2] = { i, i + 1 };
    splineCells->InsertNextCell( 2, ids );
}
splinePd->SetLines( splineCells );

Don’t set cells for splinePd with splineCells or it will failed when buildLinks, because cells
eg:

splinePd->SetPolys( splineCells );
splinePd->BuildLinks();
Warning: In /Users/weiyang/Downloads/VTK-8.1.1/Common/DataModel/vtkPolyData.cxx, line 1023
vtkPolyData (0x7f9b3676ff10): Building VTK_TRIANGLE 4997 with less than three points, but VTK_TRIANGLE needs at least three points. Check the input.
Categories: VTK

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
A prohibited operation
0
Would love your thoughts, please comment.x
()
x