SelectVisiblePoints
Repository source: 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 <vtkPointSource.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>
#include <iostream>
#include <string>
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.12 FATAL_ERROR)
project(SelectVisiblePoints)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "SelectVisiblePoints: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)
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.