[vtkusers] vtkPolyData and extracted selections

Paul McIntosh paul.mcintosh at internetscooter.com
Sun Aug 12 19:46:21 EDT 2012


Hi David,

Thanks for your response - attached is a stl generated from a sphere source
and program the output from using this example. 

The input polydata has "Number Of Cells: 96" (so is read ok) - I select half
of those cells and it appears to be correct as following reports "48" :
"cout << "Selected Cells: " <<
cellSelection->GetNode(0)->GetSelectionList()->GetNumberOfTuples()

However the output polydata is missing the cells.

  Number Of Points: 35
  Number Of Cells: 0

I am probably just missing an setting that brings the cells but I have been
banging my head against this for a while and not making much headway.

Any help much appreciated and I am also happy to add this to the vtk
examples once it is working.

Cheers,

Paul

-----Original Message-----
From: David Doria [mailto:daviddoria at gmail.com] 
Sent: Sunday, 12 August 2012 11:07 PM
To: Paul McIntosh
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] vtkPolyData and extracted selections

On Sun, Aug 12, 2012 at 7:44 AM, Paul McIntosh
<paul.mcintosh at internetscooter.com> wrote:
> Hi,
>
> I am creating a utility that will calculate the frontal area of a 
> mesh. The first thing I am trying is extracting cells from a mesh by 
> their id's (later I will change this to select cells that are visible 
> from a particular direction). I am getting stuck though creating a new 
> polydata object which is a subset of the original, using the 
> selection. The VTK examples use an unstructuredgrid, but the same code 
> doesn't work with polydata - can anyone see what I am doing wrong?
>
> Paul
>
>
> #include <vtkPolyData.h>
> #include <vtkSTLWriter.h>
> #include <vtkSTLReader.h>
> #include <vtkTransform.h>
> #include <vtkTransformFilter.h>
> #include <vtkTriangle.h>
> #include <vtkSphereSource.h>
> #include <vtkSmartPointer.h>
> #include <vtkPolyDataMapper.h>
> #include <vtkIdTypeArray.h>
> #include <vtkSelectionNode.h>
> #include <vtkActor.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderer.h>
> #include <vtkRenderWindowInteractor.h> #include 
> <vtkHardwareSelector.h> #include <vtkInteractorStyleTrackballCamera.h>
> #include <vtkRendererCollection.h>
> #include <vtkSelection.h>
> #include <vtkExtractSelection.h>
> #include <vtkDataSetMapper.h>
> #include <vtkProperty.h>
> #include <vtkObjectFactory.h>
> #include <vtkCellArray.h>
> #include <vtkCell.h>
> #include <vtkInformation.h>
> #include <vtkUnstructuredGrid.h>
>
> // C++
> #include <list>
> #include <iostream>
> #include <fstream>
>
> using namespace std;
>
> int main(int argc, char *argv[])
> {
>         // check and get the stl input file provided
>     if ( argc != 2 )
>     {
>         cout << "Required parameters: Filename" << endl;
>         return EXIT_FAILURE;
>     }
>     std::string inputfile = argv[1];
>
>     // read STL and print out some info
>     std::cout << "Reading: " << inputfile << std::endl;
>     vtkSmartPointer<vtkSTLReader> stlReader = 
> vtkSmartPointer<vtkSTLReader>::New();
>     stlReader->SetFileName(inputfile.c_str());
>     vtkSmartPointer<vtkPolyData> polydata = 
> vtkSmartPointer<vtkPolyData>::New();
>     polydata = stlReader->GetOutput();
>     polydata->Update();
>     cout << "Cells: " << polydata->GetNumberOfCells() << endl;
>     cout << "Points: " << polydata->GetNumberOfPoints() << endl;
>     cout << "Polys: " << polydata->GetNumberOfPolys() << endl;
>     cout << "Verts: " << polydata->GetNumberOfVerts() << endl;
>         polydata->Print(cout);
>
>         // select cells of interest
>         // ref: http://www.kitware.com/media/html/SelectionsInVTK.html
>     vtkSmartPointer<vtkSelectionNode> cellSelectionNode = 
> vtkSmartPointer<vtkSelectionNode>::New();
>         cellSelectionNode->SetFieldType(vtkSelectionNode::CELL);
>         cellSelectionNode->SetContentType(vtkSelectionNode::INDICES);
>
>     vtkSmartPointer<vtkIdTypeArray> ids = 
> vtkSmartPointer<vtkIdTypeArray>::New();
>     ids->SetNumberOfComponents(1);
>
>         int nCellsNum = polydata->GetNumberOfCells();
>         for (int nCellID = 0; nCellID < nCellsNum/2; nCellID++)
>         {
>                 vtkCell* cell;
>                 cell = polydata->GetCell(nCellID);
>                 ids->InsertNextValue(nCellID);
>         }
>         cellSelectionNode->SetSelectionList(ids);
>         vtkSmartPointer<vtkSelection> cellSelection = 
> vtkSmartPointer<vtkSelection>::New();
>       cellSelection->AddNode(cellSelectionNode);
>         cout << "Selected Cells: " <<
> cellSelection->GetNode(0)->GetSelectionList()->GetNumberOfTuples() << 
> cellSelection->endl;
>         //cellSelection->Print(cout);
>
>         // Extract Selection
>         vtkSmartPointer<vtkExtractSelection> extractSelection = 
> vtkSmartPointer<vtkExtractSelection>::New();
>         extractSelection->SetInput(0, polydata);                // The
> dataset is given on its first input port
>         extractSelection->SetInput(1, cellSelection);   // The subset is
> described by the contents of the vtkSelection on its second input port
>         extractSelection->Update();
>         extractSelection->Print(cout);
>
>         vtkSmartPointer<vtkPolyData> selectedPolydata = 
> vtkSmartPointer<vtkPolyData>::New();
>         selectedPolydata->ShallowCopy(extractSelection->GetOutput());
>         selectedPolydata->Print(cout);
>
>     // Visualise
>     vtkSmartPointer<vtkPolyDataMapper> selectedPolydataMapper = 
> vtkSmartPointer<vtkPolyDataMapper>::New();
>         selectedPolydataMapper->SetInput(selectedPolydata);
>      vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
>      //actor->SetMapper(mapper);
>      actor->SetMapper(selectedPolydataMapper);
>
>      vtkSmartPointer<vtkRenderer> renderer = 
> vtkSmartPointer<vtkRenderer>::New();
>      vtkSmartPointer<vtkRenderWindow> renderWindow = 
> vtkSmartPointer<vtkRenderWindow>::New();
>      renderWindow->AddRenderer(renderer);
>      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor 
> = vtkSmartPointer<vtkRenderWindowInteractor>::New();
>      renderWindowInteractor->SetRenderWindow(renderWindow);
>
>      renderer->AddActor(actor);
>      renderer->SetBackground(.3, .6, .3); // Background color green
>
>      renderWindow->Render();
>      renderWindowInteractor->Start();
>
>     return EXIT_SUCCESS;
> }
>
> Cheers,
>
> Paul

What do you mean "doesn't work"? What is the error? Please make your example
self-contained (i.e. generate data (perhaps vtkSphereSource) instead of
reading it from a file, etc) so it is easier for us to take a look.

David
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: output.txt
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120813/b1cc8ea1/attachment.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sphere.stl
Type: application/vnd.ms-pki.stl
Size: 17997 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120813/b1cc8ea1/attachment.stl>


More information about the vtkusers mailing list