ReadTextFile
Description¶
This example takes a plain text file of coordinates and reads them into a vtkPoints, which is then put into a vtkPolyData and displayed on the screen using a vtkVertexGlyphFilter.
An example file may look like: 1 2 3 4 5 6 7 8 9
Note
for a simpler way to read such a file, see SimplePointsReader.
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¶
ReadTextFile.cxx
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <sstream>
int main(int argc, char* argv[])
{
// Verify input arguments
if ( argc != 2 )
{
std::cout << "Usage: " << argv[0]
<< " Filename(.xyz)" << std::endl;
return EXIT_FAILURE;
}
// Get all data from the file
std::string filename = argv[1];
std::ifstream filestream(filename.c_str());
std::string line;
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
while(std::getline(filestream, line))
{
double x, y, z;
std::stringstream linestream;
linestream << line;
linestream >> x >> y >> z;
points->InsertNextPoint(x, y, z);
}
filestream.close();
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polyData);
glyphFilter->Update();
// Visualize
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyphFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color green
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ReadTextFile)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkFiltersGeneral
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping ReadTextFile: ${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(ReadTextFile MACOSX_BUNDLE ReadTextFile.cxx )
target_link_libraries(ReadTextFile PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ReadTextFile MACOSX_BUNDLE ReadTextFile.cxx )
target_link_libraries(ReadTextFile PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ReadTextFile
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ReadTextFile¶
Click here to download ReadTextFile and its CMakeLists.txt file. Once the tarball ReadTextFile.tar has been downloaded and extracted,
cd ReadTextFile/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:
./ReadTextFile
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.