VTK/Examples/Cxx/Broken/ExtractSelectedThresholds

From KitwarePublic
Jump to navigationJump to search

This example uses vtkSelectEnclosedPoints to generate a mask (0 or 1) if points from one sphere are inside the other. We then try to use vtkExtractSelectedThresholds to extract only points that have an associated "1" in the "SelectPoints" array.

ExtractSelectedThresholds.cxx

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkPointData.h>
#include <vtkSphereSource.h>
#include <vtkFloatArray.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkExtractSelectedThresholds.h>
#include <vtkIntArray.h>
#include <vtkDataArray.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkXMLPolyDataWriter.h>
 
int main(int, char *[])
{ 
  vtkSmartPointer<vtkSphereSource> sphereSource1 = 
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource1->SetCenter(0.0, 0.0, 0.0);
  sphereSource1->SetRadius(1.0);
  sphereSource1->Update();
  
    { 
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
      vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("sphere1.vtp");
  writer->SetInputConnection(sphereSource1->GetOutputPort());
  writer->Write();
  }
  
  vtkSmartPointer<vtkSphereSource> sphereSource2 = 
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource2->SetCenter(1.0, 0.0, 0.0);
  sphereSource2->SetRadius(1.0);
  sphereSource2->Update();
    { 
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
      vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("sphere2.vtp");
  writer->SetInputConnection(sphereSource2->GetOutputPort());
  writer->Write();
  }
  
  vtkSmartPointer<vtkSelectEnclosedPoints> selectEnclosedPoints = 
      vtkSmartPointer<vtkSelectEnclosedPoints>::New();
  selectEnclosedPoints->SetInput(sphereSource1->GetOutput());
  selectEnclosedPoints->SetSurface(sphereSource2->GetOutput());
  selectEnclosedPoints->Update();
   
  { 
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
      vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("enclosed.vtp");
  writer->SetInputConnection(selectEnclosedPoints->GetOutputPort());
  writer->Write();
  }

  vtkSmartPointer<vtkFloatArray> thresh =
      vtkSmartPointer<vtkFloatArray>::New();
  thresh->SetNumberOfComponents(1);
  thresh->InsertNextValue(0.5);
  thresh->InsertNextValue(2.0);
  thresh->SetName("SelectedPoints");
  
  vtkSmartPointer<vtkSelection> selection = 
      vtkSmartPointer<vtkSelection>::New();
  vtkSmartPointer<vtkSelectionNode> selectionNode = 
      vtkSmartPointer<vtkSelectionNode>::New();
  selectionNode->SetContentType(vtkSelectionNode::THRESHOLDS);
  selectionNode->SetFieldType(vtkSelectionNode::POINT);
  selectionNode->SetSelectionList(thresh);
  selection->AddNode(selectionNode);

  vtkSmartPointer<vtkExtractSelectedThresholds> extract = 
      vtkSmartPointer<vtkExtractSelectedThresholds>::New();
  extract->SetInput(0, selectEnclosedPoints->GetOutput());
  extract->SetInput(1, selection);
  extract->SetInputArrayToProcess(0,0,0,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "SelectedPoints");
  extract->Update(); //outputs a vtkUnstructuredGrid
  
  vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer = 
      vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
  writer->SetFileName("output.vtu");
  writer->SetInputConnection(extract->GetOutputPort());
  writer->Write();
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(ExtractSelectedThresholds)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(ExtractSelectedThresholds MACOSX_BUNDLE ExtractSelectedThresholds.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(ExtractSelectedThresholds ${VTK_LIBRARIES})
else()
  target_link_libraries(ExtractSelectedThresholds vtkHybrid vtkWidgets)
endif()

Download and Build ExtractSelectedThresholds

Click here to download ExtractSelectedThresholds. and its CMakeLists.txt file.

Once the tarball ExtractSelectedThresholds.tar has been downloaded and extracted,

cd ExtractSelectedThresholds/build 
  • If VTK is installed:
cmake ..
  • If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ExtractSelectedThresholds

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.