VTK/Examples/Cxx/PolyData/ExtractCellsUsingPoints

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 11:17, 30 May 2010 by Daviddoria (talk | contribs) (Created page with '==ExtractCellsUsingPoints.cxx== <source lang="cpp"> #include <vtkSmartPointer.h> #include <vtkInformation.h> #include <vtkSphereSource.h> #include <vtkExtractSelection.h> #includ…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

ExtractCellsUsingPoints.cxx

#include <vtkSmartPointer.h>
#include <vtkInformation.h>
#include <vtkSphereSource.h>
#include <vtkExtractSelection.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkPolyData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkIdTypeArray.h>
#include <vtkDataSetSurfaceFilter.h>

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource =
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
  
  std::cout << "There are " << sphereSource->GetOutput()->GetNumberOfPoints()
    << " input points." << std::endl;
  std::cout << "There are " << sphereSource->GetOutput()->GetNumberOfCells()
    << " input cells." << std::endl;
  
  vtkSmartPointer<vtkIdTypeArray> ids = 
      vtkSmartPointer<vtkIdTypeArray>::New();
  ids->SetNumberOfComponents(1);
  
  //set values
  for(unsigned int i = 10; i < 20; i++)
    { 
    ids->InsertNextValue(i);
    }
  
  vtkSmartPointer<vtkSelectionNode> selectionNode = 
      vtkSmartPointer<vtkSelectionNode>::New();
  selectionNode->SetFieldType(vtkSelectionNode::POINT);
  selectionNode->SetContentType(vtkSelectionNode::INDICES);
  selectionNode->SetSelectionList(ids);
  
  vtkSmartPointer<vtkSelection> selection = 
      vtkSmartPointer<vtkSelection>::New();
  selection->AddNode(selectionNode);
  
  vtkSmartPointer<vtkExtractSelection> extractSelection = 
      vtkSmartPointer<vtkExtractSelection>::New();
  
  extractSelection->SetInput(0, sphereSource->GetOutput());
  extractSelection->SetInput(1, selection);
  extractSelection->Update();
  
  //in selection
  vtkDataSet* ds = vtkDataSet::SafeDownCast (extractSelection->GetOutput());
  std::cout << "ds is type: " << ds->GetClassName() << std::endl;
  std::cout << "There are " << ds->GetNumberOfPoints() << " points in the selection." << std::endl;
  std::cout << "There are " << ds->GetNumberOfCells() << " cells in the selection." << std::endl;

  //we want the output type to match the input type (vtkPolyData)
  vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter =
    vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
  surfaceFilter->SetInputConnection(ds->GetProducerPort());
  surfaceFilter->Update();
  
  vtkPolyData* polydata = surfaceFilter->GetOutput();
  std::cout << "ps is type: " << polydata->GetClassName() << std::endl;
  std::cout << "There are " << polydata->GetNumberOfPoints() << " points in the selection." << std::endl;
  std::cout << "There are " << polydata->GetNumberOfCells() << " cells in the selection." << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(ExtractCellsUsingPoints)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(ExtractCellsUsingPoints ExtractCellsUsingPoints.cxx)
TARGET_LINK_LIBRARIES(ExtractCellsUsingPoints vtkHybrid)