Hello Paul, instead of using vtkExtractSelection, you can use vtkExtractSelectedPolydataIds. Here is a code example you can use.<div>And you will have a polydata as a result instead of an UG .<br><div><br></div><div><br></div>
<div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New();<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>ids->SetNumberOfComponents(1);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span> // Set values<br>
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> std::vector<int> idsForNewPolydata ;</blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> //Put ids you want into idsForNewPolydata</blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class="Apple-tab-span" style="white-space:pre">        </span>std::vector<int>::iterator it;<br><span class="Apple-tab-span" style="white-space:pre">        </span>for (it = idsForNewPolydata.begin(); it != idsForNewPolydata.end(); it++) // idsForNewPolydata in your case should be id's from 0 to <span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">nCellsNum</span>/2</blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>{<br>
<span class="Apple-tab-span" style="white-space:pre">                </span>int currentIndice = *it;<br><span class="Apple-tab-span" style="white-space:pre">                </span>ids->InsertNextValue(currentIndice);<br><span class="Apple-tab-span" style="white-space:pre">        </span>}<br>
<span class="Apple-tab-span" style="white-space:pre"></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New();<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>selectionNode->SetFieldType(vtkSelectionNode::CELL);<br><span class="Apple-tab-span" style="white-space:pre">        </span>selectionNode->SetContentType(vtkSelectionNode::INDICES);<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>selectionNode->SetSelectionList(ids);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New();<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>selection->AddNode(selectionNode);<br><span class="Apple-tab-span" style="white-space:pre"></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>vtkSmartPointer<vtkExtractSelectedPolyDataIds> extractSelectedIds =<span class="Apple-tab-span" style="white-space:pre">        </span>vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>extractSelectedIds->SetInput(0, polydata);<br><span class="Apple-tab-span" style="white-space:pre">        </span>extractSelectedIds->SetInput(1, selection);<br>
<span class="Apple-tab-span" style="white-space:pre">        </span>extractSelectedIds->Update();<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="Apple-tab-span" style="white-space:pre">        </span>vtkPolyData* finalPolyData = extractSelectedIds->GetOutput();</blockquote>
</div><div><br></div><div><br>Cheers, </div><div>Gonzalo..</div><div><br></div><div><br><div class="gmail_quote">2012/8/13 Paul McIntosh <span dir="ltr"><<a href="mailto:paul.mcintosh@internetscooter.com" target="_blank">paul.mcintosh@internetscooter.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi David,<br>
<br>
Thanks for your response - attached is a stl generated from a sphere source<br>
and program the output from using this example.<br>
<br>
The input polydata has "Number Of Cells: 96" (so is read ok) - I select half<br>
of those cells and it appears to be correct as following reports "48" :<br>
<div class="im">"cout << "Selected Cells: " <<<br>
cellSelection->GetNode(0)->GetSelectionList()->GetNumberOfTuples()<br>
<br>
</div>However the output polydata is missing the cells.<br>
<br>
Number Of Points: 35<br>
Number Of Cells: 0<br>
<br>
I am probably just missing an setting that brings the cells but I have been<br>
banging my head against this for a while and not making much headway.<br>
<br>
Any help much appreciated and I am also happy to add this to the vtk<br>
examples once it is working.<br>
<br>
Cheers,<br>
<br>
Paul<br>
<div><div class="h5"><br>
-----Original Message-----<br>
From: David Doria [mailto:<a href="mailto:daviddoria@gmail.com">daviddoria@gmail.com</a>]<br>
Sent: Sunday, 12 August 2012 11:07 PM<br>
To: Paul McIntosh<br>
Cc: <a href="mailto:vtkusers@vtk.org">vtkusers@vtk.org</a><br>
Subject: Re: [vtkusers] vtkPolyData and extracted selections<br>
<br>
On Sun, Aug 12, 2012 at 7:44 AM, Paul McIntosh<br>
<<a href="mailto:paul.mcintosh@internetscooter.com">paul.mcintosh@internetscooter.com</a>> wrote:<br>
> Hi,<br>
><br>
> I am creating a utility that will calculate the frontal area of a<br>
> mesh. The first thing I am trying is extracting cells from a mesh by<br>
> their id's (later I will change this to select cells that are visible<br>
> from a particular direction). I am getting stuck though creating a new<br>
> polydata object which is a subset of the original, using the<br>
> selection. The VTK examples use an unstructuredgrid, but the same code<br>
> doesn't work with polydata - can anyone see what I am doing wrong?<br>
><br>
> Paul<br>
><br>
><br>
> #include <vtkPolyData.h><br>
> #include <vtkSTLWriter.h><br>
> #include <vtkSTLReader.h><br>
> #include <vtkTransform.h><br>
> #include <vtkTransformFilter.h><br>
> #include <vtkTriangle.h><br>
> #include <vtkSphereSource.h><br>
> #include <vtkSmartPointer.h><br>
> #include <vtkPolyDataMapper.h><br>
> #include <vtkIdTypeArray.h><br>
> #include <vtkSelectionNode.h><br>
> #include <vtkActor.h><br>
> #include <vtkRenderWindow.h><br>
> #include <vtkRenderer.h><br>
> #include <vtkRenderWindowInteractor.h> #include<br>
> <vtkHardwareSelector.h> #include <vtkInteractorStyleTrackballCamera.h><br>
> #include <vtkRendererCollection.h><br>
> #include <vtkSelection.h><br>
> #include <vtkExtractSelection.h><br>
> #include <vtkDataSetMapper.h><br>
> #include <vtkProperty.h><br>
> #include <vtkObjectFactory.h><br>
> #include <vtkCellArray.h><br>
> #include <vtkCell.h><br>
> #include <vtkInformation.h><br>
> #include <vtkUnstructuredGrid.h><br>
><br>
> // C++<br>
> #include <list><br>
> #include <iostream><br>
> #include <fstream><br>
><br>
> using namespace std;<br>
><br>
> int main(int argc, char *argv[])<br>
> {<br>
> // check and get the stl input file provided<br>
> if ( argc != 2 )<br>
> {<br>
> cout << "Required parameters: Filename" << endl;<br>
> return EXIT_FAILURE;<br>
> }<br>
> std::string inputfile = argv[1];<br>
><br>
> // read STL and print out some info<br>
> std::cout << "Reading: " << inputfile << std::endl;<br>
> vtkSmartPointer<vtkSTLReader> stlReader =<br>
> vtkSmartPointer<vtkSTLReader>::New();<br>
> stlReader->SetFileName(inputfile.c_str());<br>
> vtkSmartPointer<vtkPolyData> polydata =<br>
> vtkSmartPointer<vtkPolyData>::New();<br>
> polydata = stlReader->GetOutput();<br>
> polydata->Update();<br>
> cout << "Cells: " << polydata->GetNumberOfCells() << endl;<br>
> cout << "Points: " << polydata->GetNumberOfPoints() << endl;<br>
> cout << "Polys: " << polydata->GetNumberOfPolys() << endl;<br>
> cout << "Verts: " << polydata->GetNumberOfVerts() << endl;<br>
> polydata->Print(cout);<br>
><br>
> // select cells of interest<br>
> // ref: <a href="http://www.kitware.com/media/html/SelectionsInVTK.html" target="_blank">http://www.kitware.com/media/html/SelectionsInVTK.html</a><br>
> vtkSmartPointer<vtkSelectionNode> cellSelectionNode =<br>
> vtkSmartPointer<vtkSelectionNode>::New();<br>
> cellSelectionNode->SetFieldType(vtkSelectionNode::CELL);<br>
> cellSelectionNode->SetContentType(vtkSelectionNode::INDICES);<br>
><br>
> vtkSmartPointer<vtkIdTypeArray> ids =<br>
> vtkSmartPointer<vtkIdTypeArray>::New();<br>
> ids->SetNumberOfComponents(1);<br>
><br>
> int nCellsNum = polydata->GetNumberOfCells();<br>
> for (int nCellID = 0; nCellID < nCellsNum/2; nCellID++)<br>
> {<br>
> vtkCell* cell;<br>
> cell = polydata->GetCell(nCellID);<br>
> ids->InsertNextValue(nCellID);<br>
> }<br>
> cellSelectionNode->SetSelectionList(ids);<br>
> vtkSmartPointer<vtkSelection> cellSelection =<br>
> vtkSmartPointer<vtkSelection>::New();<br>
> cellSelection->AddNode(cellSelectionNode);<br>
> cout << "Selected Cells: " <<<br>
> cellSelection->GetNode(0)->GetSelectionList()->GetNumberOfTuples() <<<br>
</div></div>> cellSelection->endl;<br>
<div class="HOEnZb"><div class="h5">> //cellSelection->Print(cout);<br>
><br>
> // Extract Selection<br>
> vtkSmartPointer<vtkExtractSelection> extractSelection =<br>
> vtkSmartPointer<vtkExtractSelection>::New();<br>
> extractSelection->SetInput(0, polydata); // The<br>
> dataset is given on its first input port<br>
> extractSelection->SetInput(1, cellSelection); // The subset is<br>
> described by the contents of the vtkSelection on its second input port<br>
> extractSelection->Update();<br>
> extractSelection->Print(cout);<br>
><br>
> vtkSmartPointer<vtkPolyData> selectedPolydata =<br>
> vtkSmartPointer<vtkPolyData>::New();<br>
> selectedPolydata->ShallowCopy(extractSelection->GetOutput());<br>
> selectedPolydata->Print(cout);<br>
><br>
> // Visualise<br>
> vtkSmartPointer<vtkPolyDataMapper> selectedPolydataMapper =<br>
> vtkSmartPointer<vtkPolyDataMapper>::New();<br>
> selectedPolydataMapper->SetInput(selectedPolydata);<br>
> vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();<br>
> //actor->SetMapper(mapper);<br>
> actor->SetMapper(selectedPolydataMapper);<br>
><br>
> vtkSmartPointer<vtkRenderer> renderer =<br>
> vtkSmartPointer<vtkRenderer>::New();<br>
> vtkSmartPointer<vtkRenderWindow> renderWindow =<br>
> vtkSmartPointer<vtkRenderWindow>::New();<br>
> renderWindow->AddRenderer(renderer);<br>
> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor<br>
> = vtkSmartPointer<vtkRenderWindowInteractor>::New();<br>
> renderWindowInteractor->SetRenderWindow(renderWindow);<br>
><br>
> renderer->AddActor(actor);<br>
> renderer->SetBackground(.3, .6, .3); // Background color green<br>
><br>
> renderWindow->Render();<br>
> renderWindowInteractor->Start();<br>
><br>
> return EXIT_SUCCESS;<br>
> }<br>
><br>
> Cheers,<br>
><br>
> Paul<br>
<br>
What do you mean "doesn't work"? What is the error? Please make your example<br>
self-contained (i.e. generate data (perhaps vtkSphereSource) instead of<br>
reading it from a file, etc) so it is easier for us to take a look.<br>
<br>
David<br>
</div></div><br>_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>--------<br>Gonzalo Amadio<br><br>
</div></div>