Visualize2DPoints
VTKEx/Cxx/Visualization/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. An example data set can be found here.
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
Visualize2DPoints.cxx
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkProperty2D.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkXMLPolyDataReader.h>
int main(int argc, char* argv[])
{
if(argc != 2)
{
std::cout << "Required parameters: Filename" << std::endl;
return EXIT_FAILURE;
}
std::string inputFilename = argv[1];
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update();
vtkPolyData* polydata = reader->GetOutput();
//vtkPoints* Points = Polydata->GetPoints();
vtkSmartPointer<vtkActor2D> actor =
vtkSmartPointer<vtkActor2D>::New();
double color[3] = {1,1,1};
vtkSmartPointer<vtkPolyDataMapper2D> mapper =
vtkSmartPointer<vtkPolyDataMapper2D>::New();
mapper->SetInputData( polydata );
mapper->ScalarVisibilityOff();
actor->SetMapper( mapper );
vtkProperty2D *property2D = actor->GetProperty();
property2D->SetColor( color );
property2D->SetPointSize( 2.0 );
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
// Renderer and RenderWindow
renderer->SetBackground( 0, 0, 0);
renderer->ResetCamera();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize( 200,200 );
renderWindow->AddRenderer( renderer );
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow( renderWindow );
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(Visualize2DPoints)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersSources
vtkvtkIOXML
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping Visualize2DPoints: ${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(Visualize2DPoints MACOSX_BUNDLE Visualize2DPoints.cxx )
target_link_libraries(Visualize2DPoints PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
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}
)
endif ()
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.