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 14 18 30 36 38 39 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.12 FATAL_ERROR)

project(DelimitedTextReader)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersGeneral
  IOInfovis
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DelimitedTextReader: 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(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}
)

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.