Difference between revisions of "VTK/Examples/Cxx/Filtering/SelectionSource"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
(Fix by setting SetContainingCells(false).)
Line 1: Line 1:
The output currently has 50 points when it should only have 10.
 
 
 
==SelectionSource.cxx==
 
==SelectionSource.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
#include <vtkSmartPointer.h>
 
 
#include <vtkPointSource.h>
 
#include <vtkPointSource.h>
 
#include <vtkExtractSelection.h>
 
#include <vtkExtractSelection.h>
//#include <vtkSelection.h>
+
#include <vtkPolyData.h>
#include <vtkSelectionNode.h>
+
#include <vtkSelectionNode.h> // for POINT and INDICES enum values
 
#include <vtkSelectionSource.h>
 
#include <vtkSelectionSource.h>
#include <vtkPolyData.h>
+
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
 
#include <vtkIdTypeArray.h>
 
  
int main(int argc, char *argv[])
+
int main(int, char *[])
 
{
 
{
 +
  // Note - this generates 50 points and a single poly-vertex cell.
 
   vtkSmartPointer<vtkPointSource> pointSource =  
 
   vtkSmartPointer<vtkPointSource> pointSource =  
 
       vtkSmartPointer<vtkPointSource>::New();
 
       vtkSmartPointer<vtkPointSource>::New();
 
   pointSource->SetNumberOfPoints(50);
 
   pointSource->SetNumberOfPoints(50);
 
   pointSource->Update();
 
   pointSource->Update();
 
+
 
   cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints() << " input points." << endl;
+
   std::cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints() << " input points." << std::endl;
 
    
 
    
 
   vtkSmartPointer<vtkSelectionSource> selectionSource =  
 
   vtkSmartPointer<vtkSelectionSource> selectionSource =  
 
       vtkSmartPointer<vtkSelectionSource>::New();
 
       vtkSmartPointer<vtkSelectionSource>::New();
 +
  selectionSource->SetFieldType(vtkSelectionNode::POINT);
 
   selectionSource->SetContentType(vtkSelectionNode::INDICES);
 
   selectionSource->SetContentType(vtkSelectionNode::INDICES);
   selectionSource->SetFieldType(vtkSelectionNode::POINT);
+
 
 
+
  // Without this line, all points are passed through because the 11 points
   for (int i = 10; i <= 20; i++)
+
  // we will select below are some of the points of the poly-vertex created
 +
  // by the PointSource, so the cell (by default) gets passed through since
 +
  // it contains some selected points, so therefore all of the points
 +
  // (the 50 belonging to the poly-vertex) also get passed through, which
 +
  // is not what we are trying to demonstrate.
 +
   selectionSource->SetContainingCells(false);
 +
 
 +
   for (vtkIdType i = 10; i <= 20; i++)
 
     {
 
     {
     selectionSource->AddID(-1, i);
+
     selectionSource->AddID(0, i);
 
     }
 
     }
 
+
 
 
   selectionSource->Update();
 
   selectionSource->Update();
       
+
 
 
   vtkSmartPointer<vtkExtractSelection> extractSelection =  
 
   vtkSmartPointer<vtkExtractSelection> extractSelection =  
 
       vtkSmartPointer<vtkExtractSelection>::New();
 
       vtkSmartPointer<vtkExtractSelection>::New();
 
+
   extractSelection->SetInputConnection(0, pointSource->GetOutputPort());
   extractSelection->SetInput(0, pointSource->GetOutput());
+
   extractSelection->SetInputConnection(1, selectionSource->GetOutputPort());
   extractSelection->SetInput(1, selectionSource->GetOutput());
 
 
   extractSelection->Update();
 
   extractSelection->Update();
 
+
 
 
   vtkDataSet* ds = vtkDataSet::SafeDownCast (extractSelection->GetOutput());
 
   vtkDataSet* ds = vtkDataSet::SafeDownCast (extractSelection->GetOutput());
 
    
 
    
   cout << "There are " << ds->GetNumberOfPoints() << " output points." << endl;
+
   std::cout << "There are " << ds->GetNumberOfPoints() << " output points." << std::endl;
 
+
 
 
   return EXIT_SUCCESS;
 
   return EXIT_SUCCESS;
 
}
 
}

Revision as of 08:38, 12 July 2013

SelectionSource.cxx

#include <vtkPointSource.h>
#include <vtkExtractSelection.h>
#include <vtkPolyData.h>
#include <vtkSelectionNode.h> // for POINT and INDICES enum values
#include <vtkSelectionSource.h>
#include <vtkSmartPointer.h>

int main(int, char *[])
{
  // Note - this generates 50 points and a single poly-vertex cell.
  vtkSmartPointer<vtkPointSource> pointSource = 
      vtkSmartPointer<vtkPointSource>::New();
  pointSource->SetNumberOfPoints(50);
  pointSource->Update();

  std::cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints() << " input points." << std::endl;
  
  vtkSmartPointer<vtkSelectionSource> selectionSource = 
      vtkSmartPointer<vtkSelectionSource>::New();
  selectionSource->SetFieldType(vtkSelectionNode::POINT);
  selectionSource->SetContentType(vtkSelectionNode::INDICES);

  // Without this line, all points are passed through because the 11 points
  // we will select below are some of the points of the poly-vertex created
  // by the PointSource, so the cell (by default) gets passed through since
  // it contains some selected points, so therefore all of the points
  // (the 50 belonging to the poly-vertex) also get passed through, which
  // is not what we are trying to demonstrate.
  selectionSource->SetContainingCells(false);

  for (vtkIdType i = 10; i <= 20; i++)
    {
    selectionSource->AddID(0, i);
    }

  selectionSource->Update();

  vtkSmartPointer<vtkExtractSelection> extractSelection = 
      vtkSmartPointer<vtkExtractSelection>::New();
  extractSelection->SetInputConnection(0, pointSource->GetOutputPort());
  extractSelection->SetInputConnection(1, selectionSource->GetOutputPort());
  extractSelection->Update();

  vtkDataSet* ds = vtkDataSet::SafeDownCast (extractSelection->GetOutput());
  
  std::cout << "There are " << ds->GetNumberOfPoints() << " output points." << std::endl;

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(SelectionSource)

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

add_executable(SelectionSource MACOSX_BUNDLE SelectionSource.cxx)

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

Download and Build SelectionSource

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

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

cd SelectionSource/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:

./SelectionSource

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.