VTK/Examples/Cxx/IO/ReadTextFile

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 20:12, 9 December 2012 by Lorensen (Talk | contribs)

Jump to: navigation, search
VTK Examples Baseline IO TestReadTextFile.png

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

ReadTextFile.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkParticleReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.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 fin(filename.c_str());
 
  std::string line;
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
 
  while(std::getline(fin, line))
    {
    double x,y,z;
    std::stringstream linestream;
    linestream << line;
    linestream >> x >> y >> z;
 
    points->InsertNextPoint(x, y, z);
    }
 
  fin.close();
 
  vtkSmartPointer<vtkPolyData> polydata =
    vtkSmartPointer<vtkPolyData>::New();
 
  polydata->SetPoints(points);
 
  vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
    vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
  glyphFilter->SetInputConnection(polydata->GetProducerPort());
#else
  glyphFilter->SetInputData(polydata);
#endif
  glyphFilter->Update();
 
  // Visualize
  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(glyphFilter->GetOutputPort());
 
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
 
  //Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  //Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(.3, .6, .3); // Background color green
 
  //Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(ReadTextFile)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(ReadTextFile MACOSX_BUNDLE ReadTextFile.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(ReadTextFile ${VTK_LIBRARIES})
else()
  target_link_libraries(ReadTextFile vtkHybrid vtkWidgets)
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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.