[vtkusers] How to copy cell from a polydata and append to another polydata?

donyyo donyyo at gmail.com
Fri Dec 27 04:12:08 EST 2013


I deal with it following 
http://www.itk.org/Wiki/VTK/Examples/Cxx/Broken/PolyData/DeleteCells
<http://www.itk.org/Wiki/VTK/Examples/Cxx/Broken/PolyData/DeleteCells>  . I
don't know if it's the best way, but it works.

    // Import a polyData at first.

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points = polyData->GetPoints();

    vtkSmartPointer<vtkIdList> cellPointIds =
vtkSmartPointer<vtkIdList>::New();
    double cellPoint[3][3] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

    vtkSmartPointer<vtkAppendPolyData> appendPolyData =
vtkSmartPointer<vtkAppendPolyData>::New();
    for (vtkIdType ii = 0; ii < polyData->GetNumberOfCells(); ii++)
    {
        polyData->GetCellPoints(ii, cellPointIds);
        vtkSmartPointer<vtkPoints> cellPoints =
vtkSmartPointer<vtkPoints>::New();
        for (vtkIdType jj = 0; jj < 3; jj++)
        {
            points->GetPoint(cellPointIds->GetId(jj), cellPoint[jj]);
            cellPoints->InsertNextPoint(cellPoint[jj]);
        }

        vtkSmartPointer<vtkLine> line0 = vtkSmartPointer<vtkLine>::New();
        line0->GetPointIds()->SetId(0, 0);
        line0->GetPointIds()->SetId(1, 1);
        vtkSmartPointer<vtkLine> line1 = vtkSmartPointer<vtkLine>::New();
        line1->GetPointIds()->SetId(0, 1);
        line1->GetPointIds()->SetId(1, 2);
        vtkSmartPointer<vtkLine> line2 = vtkSmartPointer<vtkLine>::New();
        line2->GetPointIds()->SetId(0, 2);
        line2->GetPointIds()->SetId(1, 0);

        vtkSmartPointer<vtkCellArray> lines =
vtkSmartPointer<vtkCellArray>::New();
        lines->InsertNextCell(line0);
        lines->InsertNextCell(line1);
        lines->InsertNextCell(line2);

        vtkSmartPointer<vtkPolyData> cellPolyData =
vtkSmartPointer<vtkPolyData>::New();
        cellPolyData->SetPoints(cellPoints);
        cellPolyData->SetLines(lines);
        cellPolyData->BuildLinks();
        cellPolyData->Update();

        vtkSmartPointer<vtkStripper> cellStripper =
vtkSmartPointer<vtkStripper>::New();
        cellStripper->SetInput(cellPolyData);
        cellStripper->Update();

        vtkSmartPointer<vtkCleanPolyData> cleanCellPolyData =
vtkSmartPointer<vtkCleanPolyData>::New();
       
cleanCellPolyData->SetInputConnection(cellStripper->GetOutputPort());
        cleanCellPolyData->SetTolerance(0.0);          // Specify tolerance
in terms of fraction of bounding box length. If tolerance is specified
precisely=0.0, then vtkCleanPolyData will use the vtkMergePoints object to
merge points (which is faster). vtkMergePoints merge exactly coincident
points.
        cleanCellPolyData->SetConvertLinesToPoints(0); // Turn on/off
conversion of degenerate lines to points.
        cleanCellPolyData->SetConvertPolysToLines(0);  // Turn on/off
conversion of degenerate polys to lines.
        cleanCellPolyData->SetConvertStripsToPolys(0); // Turn on/off
conversion of degenerate strips to polys.
        cleanCellPolyData->Update();

        appendPolyData->AddInput(cleanCellPolyData->GetOutput());
        appendPolyData->Update();
    }

    vtkSmartPointer<vtkCleanPolyData> cleanPolyData =
vtkSmartPointer<vtkCleanPolyData>::New();
    cleanPolyData->SetInputConnection(appendPolyData->GetOutputPort());
    cleanPolyData->SetTolerance(0.0);          // Specify tolerance in terms
of fraction of bounding box length. If tolerance is specified precisely=0.0,
then vtkCleanPolyData will use the vtkMergePoints object to merge points
(which is faster). vtkMergePoints merge exactly coincident points.
    cleanPolyData->SetConvertLinesToPoints(0); // Turn on/off conversion of
degenerate lines to points.
    cleanPolyData->SetConvertPolysToLines(0);  // Turn on/off conversion of
degenerate polys to lines.
    cleanPolyData->SetConvertStripsToPolys(0); // Turn on/off conversion of
degenerate strips to polys.
    cleanPolyData->Update();

    copyPolyData->ShallowCopy(cleanPolyData->GetOutput());
    copyPolyData->Update();



--
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-copy-cell-from-a-polydata-and-append-to-another-polydata-tp5725141p5725147.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list