Skip to content

ReadPlainTextTriangles

vtk-examples/Cxx/IO/ReadPlainTextTriangles

Description

Here is an example of a custom file-format reader that produces a VTK XML PolyData file from a plain-text input format.

Input Format

number_of_points
number_of_triangles
point[point[0]Y point[0](0]X)Z
point[point[1]Y point[1](1]X)Z
...
point[point[N]Y point[N](N]X)Z
triangle[triangle[0]B triangle[0](0]A)C
triangle[triangle[1]B triangle[1](1]A)C
...
triangle[triangle[M]B triangle[M](M]A)C

Example Input

3
1
0.0 0.0 0.0
0.0 0.0 1.0
0.0 1.0 0.0
0 1 2

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ReadPlainTextTriangles.cxx

#include <vtkCellArray.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataWriter.h>

#include <iostream>

namespace {
vtkSmartPointer<vtkPolyData> custom_reader(std::ifstream& infile);
}

int main(int argc, char* argv[])
{
  // Verify command line arguments
  if (argc != 2)
  {
    std::cerr << "Required arguments: triangleFile.txt" << std::endl;
    return EXIT_FAILURE;
  }

  std::string inputFilename = argv[1];

  std::ifstream fin(inputFilename.c_str());

  auto polydata = custom_reader(fin);

  vtkNew<vtkXMLPolyDataWriter> writer;
  writer->SetInputData(polydata);
  writer->SetFileName("x.vtp");
  writer->Write();
  return EXIT_SUCCESS;
}

namespace {
vtkSmartPointer<vtkPolyData> custom_reader(std::ifstream& infile)
{
  vtkIdType number_of_points, number_of_triangles;
  infile >> number_of_points >> number_of_triangles;

  vtkNew<vtkPoints> points;
  points->SetNumberOfPoints(number_of_points);
  for (vtkIdType i = 0; i < number_of_points; i++)
  {
    double x, y, z;
    infile >> x >> y >> z;
    points->SetPoint(i, x, y, z);
  }

  vtkNew<vtkCellArray> polys;
  for (vtkIdType i = 0; i < number_of_triangles; i++)
  {
    vtkIdType a, b, c;
    infile >> a >> b >> c;
    polys->InsertNextCell(3);
    polys->InsertCellPoint(a);
    polys->InsertCellPoint(b);
    polys->InsertCellPoint(c);
  }
  auto polydata = vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
  polydata->SetPolys(polys);
  return polydata;
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ReadPlainTextTriangles)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOXML
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ReadPlainTextTriangles: 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(ReadPlainTextTriangles MACOSX_BUNDLE ReadPlainTextTriangles.cxx )
  target_link_libraries(ReadPlainTextTriangles PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ReadPlainTextTriangles
  MODULES ${VTK_LIBRARIES}
)

Download and Build ReadPlainTextTriangles

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

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

./ReadPlainTextTriangles

WINDOWS USERS

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