[vtkusers] Question ? [Reading data from txt file]

Hal Canary hal at cs.unc.edu
Mon Aug 6 13:00:12 EDT 2012


On 08/06/2012 12:50 PM, David Doria wrote:
>     What I could read it in VTK ?
>
>     I have already tried with  example:
>     http://www.vtk.org/Wiki/VTK/Examples/Cxx/InfoVis/ReadDelimitedFile
>
>     But I couldn't read well data value of points/triangle.
>     I received : output :
>     x: 1.#QNANy: 0z: 0 n: 0 0 0
>
>
> If you're going to use a custom file format, you'll have to write a
> custom reader :)
>
> That DelimitedTextReader is expecting only point data in the file (no
> header like you have). You'd then also have to construct the triangles
> and tell it when to reinterpret the data in the output table as triangle
> connectivity rather than points.
>
> I'd suggest you just do it manually using something like this:
> http://programmingexamples.net/wiki/CPP/IO/FileInput and parsing the
> stream manually. Then you can put it all together with:
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Triangle
>
> David


For example, something like this might work:

#include "vtkSmartPointer.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
vtkPolyData * f(std::istream & infile)
{
   vtkIdType number_of_points, number_of_triangles;
   infile >> number_of_points >> number_of_triangles;
   vtkSmartPointer<vtkPoints> points
     = vtkSmartPointer<vtkPoints>::New();
   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);
     }
   vtkSmartPointer<vtkCellArray> polys
     = vtkSmartPointer<vtkCellArray>::New();
   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);
     }
   vtkPolyData * polydata = vtkPolyData::New();
   polydata->SetPoints(points);
   polydata->SetPolys(polys);
   return polydata;
}




More information about the vtkusers mailing list