VTK/Examples/Cxx/PolyData/ExtractSelectionCells

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 08:52, 12 May 2010 by Daviddoria (talk | contribs)
Jump to navigationJump to search

ExtractSelectionCells.cxx

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

int main(int, char *[])
{
  vtkSmartPointer<vtkPointSource> pointSource = 
      vtkSmartPointer<vtkPointSource>::New();
  pointSource->SetNumberOfPoints(50);
  pointSource->Update();
  
  cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints() << " input points." << 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::CELL);
  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, selection);
  //extractSelection->SetInput(1, pointSource->GetOutput());
  
  extractSelection->SetInput(0, pointSource->GetOutput());
  extractSelection->SetInput(1, selection);
    
  extractSelection->Update();
  
  //in selection
  vtkDataSet* ds = vtkDataSet::SafeDownCast (extractSelection->GetOutput());
  cout << "There are " << ds->GetNumberOfPoints() << " points in the selection." << endl;
  cout << "There are " << ds->GetNumberOfCells() << " cells in the selection." << endl;
  
  //not in selection
  selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(), 1); //invert the selection
  extractSelection->Update();
  cout << "There are " << vtkDataSet::SafeDownCast (extractSelection->GetOutput())->GetNumberOfPoints() << " points not in the selection." << endl;
  
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(ExtractSelectionCells)

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

ADD_EXECUTABLE(ExtractSelectionCells ExtractSelectionCells.cxx)
TARGET_LINK_LIBRARIES(ExtractSelectionCells vtkHybrid)