Skip to content

MaskPoints

Repository source: MaskPoints


Question

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

Code

MaskPoints.cxx

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

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

  // Create a set of points
  vtkNew<vtkPointSource> pointsSource;
  pointsSource->SetNumberOfPoints(40);
  pointsSource->Update();

  std::cout << "There are " << pointsSource->GetOutput()->GetNumberOfPoints()
            << " input points." << std::endl;

  // Create a point set
  vtkNew<vtkMaskPoints> maskPoints;
  maskPoints->SetOnRatio(2); // keep every 2nd point (half the number of points)
  maskPoints->SetInputConnection(pointsSource->GetOutputPort());
  maskPoints->Update();

  vtkNew<vtkVertexGlyphFilter> glyphFilter;
  glyphFilter->SetInputConnection(maskPoints->GetOutputPort());
  glyphFilter->Update();

  std::cout << "There are " << maskPoints->GetOutput()->GetNumberOfPoints()
            << " masked points." << std::endl;

  vtkNew<vtkPolyDataMapper> inputMapper;
  inputMapper->SetInputConnection(pointsSource->GetOutputPort());
  vtkNew<vtkActor> inputActor;
  inputActor->SetMapper(inputMapper);
  inputActor->GetProperty()->SetPointSize(5);
  inputActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkPolyDataMapper> maskedMapper;
  maskedMapper->SetInputConnection(glyphFilter->GetOutputPort());
  vtkNew<vtkActor> maskedActor;
  maskedActor->SetMapper(maskedMapper);
  maskedActor->GetProperty()->SetPointSize(5);
  maskedActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  // There will be one render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->SetWindowName("MaskPoints");

  // And one interactor
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Setup both renderers
  vtkNew<vtkRenderer> leftRenderer;
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(colors->GetColor3d("Chocolate").GetData());

  vtkNew<vtkRenderer> rightRenderer;
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  leftRenderer->AddActor(inputActor);
  rightRenderer->AddActor(maskedActor);

  leftRenderer->ResetCamera();
  rightRenderer->ResetCamera();

  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(MaskPoints)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersGeneral
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "MaskPoints: 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(MaskPoints MACOSX_BUNDLE MaskPoints.cxx )
  target_link_libraries(MaskPoints PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS MaskPoints
  MODULES ${VTK_LIBRARIES}
)

Download and Build MaskPoints

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

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

./MaskPoints

WINDOWS USERS

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