[Paraview] Assign scalars/vectors to mesh points in vtkRectilinearGrid, C++

Paw Møller pawsen at gmail.com
Tue Apr 2 14:10:55 EDT 2013


Hi,

Using the C++ VTK library for writing an .vtr file, I'm able to create the
mesh, but not assign any skalars/vectors to the grid points. This is more
or less given in the example file RGrid.cxx.

But how do I assign values at the mesh-points?

Write the mesh:

#include <stdio.h>
#include <vtkRectilinearGrid.h>
#include <vtkXMLRectilinearGridWriter.h>
#include <vtkDoubleArray.h>
#include <vtkIntArray.h>
#include <vtkSmartPointer.h>

int main(){
  int nx = 3, ny = 3, nz = 3;

  // coordinates
  vtkDoubleArray *xCoords = vtkDoubleArray::New();
  for (int i=0; i<nx; i++) xCoords->InsertNextValue(i);
  vtkDoubleArray *yCoords = vtkDoubleArray::New();
  for (int i=0; i<ny; i++) yCoords->InsertNextValue(i);
  vtkDoubleArray *zCoords = vtkDoubleArray::New();
  for (int i=0; i<nz; i++) zCoords->InsertNextValue(i);

  // The coordinates are assigned to the rectilinear grid.
  vtkRectilinearGrid *rgrid = vtkRectilinearGrid::New();
  rgrid->SetDimensions(nx,ny,nz);
  rgrid->SetXCoordinates(xCoords);
  rgrid->SetYCoordinates(yCoords);
  rgrid->SetZCoordinates(zCoords);

  /* Write to file */
  vtkSmartPointer<vtkXMLRectilinearGridWriter>
    writer = vtkSmartPointer<vtkXMLRectilinearGridWriter>::New();
  writer->SetFileName("test.vtr");

#if VTK_MAJOR_VERSION <= 5
  writer->SetInput(rgrid);
#else
  writer->SetInputData(rgrid);
#endif
  writer->Write();

  /* clean up */
  xCoords->Delete();
  yCoords->Delete();
  zCoords->Delete();
  rgrid->Delete();

  printf("DONE printing\n");
}

For setting values at the mesh-points, I assumed something like:
    vtkIntArray* temperature = vtkIntArray::New();
    temperature->SetName("Temperature");
    temperature->SetNumberOfComponents(1);
    temperature->SetNumberOfTuples(nx*ny*nz);
    for(int i=0;i<mesh.nn;i++){
       temperature->SetValue(i,10); // set everything to 10
    }

   rgrid->GetPointData()->SetScalars(temperature);

But that doesn't work. As expected, when looking at
http://www.vtk.org/doc/nightly/html/classvtkRectilinearGrid.html

So how do I set values/vectors for all the points in the mesh.

Regards,
Paw
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.paraview.org/pipermail/paraview/attachments/20130402/df2a992a/attachment.htm>


More information about the ParaView mailing list