Skip to content

Visualize2DPoints

Repository source: Visualize2DPoints


Description

This example shows how to render a set of points. The example reads the points from a vtp file specified as the first command line argument. Ring is an example data set.

Question

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

Code

Visualize2DPoints.cxx

#include <vtkActor2D.h>
#include <vtkCellArray.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
// #include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  if (argc != 2)
  {
    std::cout << "Required parameters: Filename e.g. Ring.vtp" << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  std::string inputFilename = argv[1];

  vtkNew<vtkXMLPolyDataReader> reader;
  reader->SetFileName(inputFilename.c_str());
  reader->Update();
  vtkPolyData* polydata = reader->GetOutput();
  // vtkPoints* Points = Polydata->GetPoints();

  vtkNew<vtkPolyDataMapper2D> mapper;
  mapper->SetInputData(polydata);
  mapper->ScalarVisibilityOff();
  vtkNew<vtkActor2D> actor;
  actor->SetMapper(mapper);

  vtkProperty2D* property2D = actor->GetProperty();
  property2D->SetColor(colors->GetColor3d("Gold").GetData());
  property2D->SetPointSize(2.0);

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  // Renderer and RenderWindow.
  renderer->ResetCamera();
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetWindowName("Visualize2DPoints");

  renderWindow->SetSize(200, 200);
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderWindow->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Visualize2DPoints)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersSources
  IOXML
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

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

Download and Build Visualize2DPoints

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

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

./Visualize2DPoints

WINDOWS USERS

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