Search code examples
c++vtk

How to read data in vtkDataArray?


I am ver new to VTK. This is part of my code:

vtkDataSetReader *rdr = vtkDataSetReader::New();
rdr->SetFileName("proj7b.vtk");
rdr->SetScalarsName("hardyglobal");
rdr->Update();

int dims[3];
vtkRectilinearGrid *rgrid = (vtkRectilinearGrid *) rdr->GetOutput();
rgrid->GetDimensions(dims);



vtkDataArray *dataArray;
dataArray = vtkDoubleArray::New();

dataArray = rgrid->GetPointData()->GetScalars()->GetVoidPointer(0);

for(i=0;i<10;i++)
{
    cout<<"here----------"<<endl;
    cout<<" "<<dataArray[i]<<" ";
}

I want to read the data into my vtkDataArray. But this code cannot be compile:

proj7b.cxx:525:15: error: 
  assigning to 'vtkDataArray *' from incompatible type 'void *'
dataArray = rgrid->GetPointData()->GetScalars()->GetVoidPointer(0);
          ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated. Is there anyone knows how to read data into vtkDataArray?


Solution

  • Vtk provides the method for it, you don't need to use the "low level" method:

    rgrid->GetPointData()->GetArray(0)

    or rgrid->GetPointData()->GetArray("arrayname")

    It works the same way for FieldData and CellData ( http://www.vtk.org/doc/release/6.2/html/classvtkFieldData.html)

    What you get is a vtkArray, not a simple c++ array, so you will have to read it like:

    cout<<" "<<dataArray->GetValue(i) <<" ";
    

    There are a lot of examples in the wiki http://www.vtk.org/Wiki/VTK/Examples/Cxx