[vtkusers] vtkPolyData and extracted selections
Gonzalo Amadio
gonzaloamadio at gmail.com
Mon Aug 13 08:30:30 EDT 2012
Hello Paul, instead of using vtkExtractSelection, you can use
vtkExtractSelectedPolydataIds. Here is a code example you can use.
And you will have a polydata as a result instead of an UG .
vtkSmartPointer<vtkIdTypeArray> ids =
> vtkSmartPointer<vtkIdTypeArray>::New();
> ids->SetNumberOfComponents(1);
>
// Set values
>
std::vector<int> idsForNewPolydata ;
//Put ids you want into idsForNewPolydata
> std::vector<int>::iterator it;
> for (it = idsForNewPolydata.begin(); it != idsForNewPolydata.end(); it++)
> // idsForNewPolydata in your case should be id's from 0 to nCellsNum/2
{
> int currentIndice = *it;
> ids->InsertNextValue(currentIndice);
> }
>
vtkSmartPointer<vtkSelectionNode> selectionNode =
> vtkSmartPointer<vtkSelectionNode>::New();
> selectionNode->SetFieldType(vtkSelectionNode::CELL);
> selectionNode->SetContentType(vtkSelectionNode::INDICES);
> selectionNode->SetSelectionList(ids);
>
vtkSmartPointer<vtkSelection> selection =
> vtkSmartPointer<vtkSelection>::New();
> selection->AddNode(selectionNode);
>
vtkSmartPointer<vtkExtractSelectedPolyDataIds> extractSelectedIds =
> vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();
> extractSelectedIds->SetInput(0, polydata);
> extractSelectedIds->SetInput(1, selection);
> extractSelectedIds->Update();
>
vtkPolyData* finalPolyData = extractSelectedIds->GetOutput();
Cheers,
Gonzalo..
2012/8/13 Paul McIntosh <paul.mcintosh at internetscooter.com>
> 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
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
--
--------
Gonzalo Amadio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120813/351d85b0/attachment.htm>
More information about the vtkusers
mailing list