Skip to content

DelimitedTextReader

vtk-examples/Cxx/InfoVis/DelimitedTextReader


Description

This example takes a plain text file of coordinates and normals (x y z nx ny nz) and reads them into a vtkPolyData and displays them on the screen. This can be easily changed to reading a file with any delimiter by changing the argument of Reader->SetFieldDelimiterCharacters(" ");

Here is an example file:

 0.0 0.0 0.0 1.0 2.0 3.0
 1.0 0.0 0.0 4.0 5.0 6.1
 0.0 1.0 0.0 7.2 8.3 9.4
 ```
!!! question
    If you have a question about this example, please use the [VTK Discourse Forum](https://discourse.vtk.org/)

###Code
**DelimitedTextReader.cxx**
``` c++ hl_lines="1 2 3 4 5 6 7 8 9 10 11 12 13 14 18 30 36 38 39 48 70 74 79 82 87 88 92"

#include <vtkActor.h>
#include <vtkDelimitedTextReader.h>
#include <vtkDoubleArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTable.h>
#include <vtkVersionMacros.h> // For version macros
#include <vtkVertexGlyphFilter.h>

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

  // Verify input arguments
  if (argc != 2)
  {
    std::cout << "Usage: " << argv[0] << " Filename e.g DelimitedData.txt"
              << std::endl;
    return EXIT_FAILURE;
  }

  std::string inputFilename = argv[1];

  vtkNew<vtkDelimitedTextReader> reader;
  reader->SetFileName(inputFilename.c_str());
  reader->DetectNumericColumnsOn();
  reader->SetFieldDelimiterCharacters(" ");
  reader->Update();

  vtkTable* table = reader->GetOutput();

  vtkNew<vtkPoints> points;
  vtkNew<vtkDoubleArray> normals;

  normals->SetNumberOfComponents(3); // 3d normals (ie x,y,z)

  std::cout << "Table has " << table->GetNumberOfRows() << " rows."
            << std::endl;
  std::cout << "Table has " << table->GetNumberOfColumns() << " columns."
            << std::endl;

  for (vtkIdType i = 0; i < table->GetNumberOfRows(); i++)
  {
    std::cout << "x: " << (table->GetValue(i, 0)).ToDouble()
              << " y: " << (table->GetValue(i, 1)).ToDouble()
              << " z: " << (table->GetValue(i, 2)).ToDouble();

    points->InsertNextPoint((table->GetValue(i, 0)).ToDouble(),
                            (table->GetValue(i, 1)).ToDouble(),
                            (table->GetValue(i, 2)).ToDouble());

    double n[3];
    n[0] = (table->GetValue(i, 3)).ToDouble();
    n[1] = (table->GetValue(i, 4)).ToDouble();
    n[2] = (table->GetValue(i, 5)).ToDouble();

    std::cout << " n: " << n[0] << " " << n[1] << " " << n[2] << std::endl;
    normals->InsertNextTuple(n);
  }

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

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->GetPointData()->SetNormals(normals);

  vtkNew<vtkVertexGlyphFilter> glyphFilter;
  glyphFilter->SetInputData(polydata);
  glyphFilter->Update();

  // Visualize
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(glyphFilter->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetPointSize(30);
  actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("DelimitedTextReader");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("Mint").GetData());

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DelimitedTextReader)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersGeneral
  vtkIOInfovis
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping DelimitedTextReader: ${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(DelimitedTextReader MACOSX_BUNDLE DelimitedTextReader.cxx )
  target_link_libraries(DelimitedTextReader PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(DelimitedTextReader MACOSX_BUNDLE DelimitedTextReader.cxx )
  target_link_libraries(DelimitedTextReader PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS DelimitedTextReader
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build DelimitedTextReader

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

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

./DelimitedTextReader

WINDOWS USERS

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