Difference between revisions of "VTK/Examples/Cxx/PolyData/ExtractCellsUsingPoints"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
m
Line 13: Line 13:
 
#include <vtkXMLPolyDataWriter.h>
 
#include <vtkXMLPolyDataWriter.h>
  
int main(int argc, char *argv[])
+
int main(int, char *[])
 
{
 
{
 
   vtkSmartPointer<vtkSphereSource> sphereSource =
 
   vtkSmartPointer<vtkSphereSource> sphereSource =

Revision as of 07:14, 1 June 2010

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>
#include <vtkXMLPolyDataWriter.h>

int main(int, char *[])
{
  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<vtkXMLPolyDataWriter> writer =
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("sphere.vtp");
  writer->SetInputConnection(sphereSource->GetOutputPort());
  writer->Write();
  }
  
  vtkSmartPointer<vtkIdTypeArray> ids = 
      vtkSmartPointer<vtkIdTypeArray>::New();
  ids->SetNumberOfComponents(1);
  
  //set values
  
  //for(unsigned int i = 10; i < 20; i++)
  for(vtkIdType i = 0; i < sphereSource->GetOutput()->GetNumberOfPoints()-15; i++)
    { 
    ids->InsertNextValue(i);
    }
  
  vtkSmartPointer<vtkSelectionNode> selectionNode = 
      vtkSmartPointer<vtkSelectionNode>::New();
  selectionNode->SetFieldType(vtkSelectionNode::POINT);
  selectionNode->SetContentType(vtkSelectionNode::INDICES);
  selectionNode->SetSelectionList(ids);
  selectionNode->GetProperties()->Set(vtkSelectionNode::CONTAINING_CELLS(), 1);

  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;

  {
  vtkSmartPointer<vtkXMLPolyDataWriter> writer =
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("output.vtp");
  writer->SetInputConnection(polydata->GetProducerPort());
  writer->Write();
  }
  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)