Skip to content

SelectVisiblePoints

vtk-examples/Cxx/PolyData/SelectVisiblePoints


Description

This example demonstrates how to determine which objects are visible.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

SelectVisiblePoints.cxx

#include <vtkActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkPointSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelectVisiblePoints.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

namespace {
class MyInteractor : public vtkInteractorStyleTrackballCamera
{
public:
  static MyInteractor* New();
  vtkTypeMacro(MyInteractor, vtkInteractorStyleTrackballCamera);

  virtual void OnLeftButtonDown() override
  {
    this->VisibleFilter->Update();
    std::cout << "There are currently: "
              << this->VisibleFilter->GetOutput()->GetNumberOfPoints()
              << " visible." << std::endl;
    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
  }

  void SetVisibleFilter(vtkSmartPointer<vtkSelectVisiblePoints> vis)
  {
    this->VisibleFilter = vis;
  }

private:
  vtkSmartPointer<vtkSelectVisiblePoints> VisibleFilter;
};

vtkStandardNewMacro(MyInteractor);

} // namespace

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetCenter(5.0, 0, 0);
  sphereSource->Update();

  vtkNew<vtkPointSource> pointSource;
  pointSource->SetRadius(2.0);
  pointSource->SetNumberOfPoints(200);
  pointSource->Update();

  // Create an actor and mapper
  vtkNew<vtkPolyDataMapper> sphereMapper;
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());

  vtkNew<vtkActor> sphereActor;
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkPolyDataMapper> pointsMapper;
  pointsMapper->SetInputConnection(pointSource->GetOutputPort());

  vtkNew<vtkActor> pointsActor;
  pointsActor->SetMapper(pointsMapper);
  sphereActor->GetProperty()->SetColor(
      colors->GetColor3d("Honeydew").GetData());

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("SelectVisiblePoints");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(sphereActor);
  renderer->AddActor(pointsActor);
  renderer->SetBackground(colors->GetColor3d("ivory_black").GetData());

  vtkNew<vtkSelectVisiblePoints> selectVisiblePoints;
  selectVisiblePoints->SetInputConnection(pointSource->GetOutputPort());
  selectVisiblePoints->SetRenderer(renderer);
  selectVisiblePoints->Update();

  vtkNew<MyInteractor> style;
  renderWindowInteractor->SetInteractorStyle(style);
  style->SetVisibleFilter(selectVisiblePoints);

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(SelectVisiblePoints)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping SelectVisiblePoints: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(SelectVisiblePoints MACOSX_BUNDLE SelectVisiblePoints.cxx )
  target_link_libraries(SelectVisiblePoints PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(SelectVisiblePoints MACOSX_BUNDLE SelectVisiblePoints.cxx )
  target_link_libraries(SelectVisiblePoints PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS SelectVisiblePoints
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build SelectVisiblePoints

Click here to download SelectVisiblePoints and its CMakeLists.txt file. Once the tarball SelectVisiblePoints.tar has been downloaded and extracted,

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

./SelectVisiblePoints

WINDOWS USERS

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