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

joseph insley insley at anl.gov
Tue Apr 2 15:23:17 EDT 2013


Hi, Paw,

I believe the problem is that you are mixing calls for scalars and vectors…

For scalars you want to use SetNumberOfValues()/SetValue():

    vtkIntArray* temperature = vtkIntArray::New();
    temperature->SetName("Temperature");
    temperature->SetNumberOfComponents(1);
    temperature->SetNumberOfValues(nx*ny*nz);
    for(int i=0;i<mesh.nn;i++){
       temperature->SetValue(i,10); // set everything to 10
    }
   rgrid->GetPointData()->AddArray(temperature);


For vectors you want to use SetNumberOfTuples()/SetTuple3() (assuming SetNumberOfComponents(3)):

    vtkFloatArray* velocity = vtkFloatArray::New();
    velocity->SetName("Velocity");
    velocity->SetNumberOfComponents(3);
    velocity->SetNumberOfTuples(nx*ny*nz);
    for(int i=0;i<mesh.nn;i++){
       velocity->SetTuple3(i,10, 10, 10); // set everything to 10
    }
   rgrid->GetPointData()->AddArray(velocity);


I also tend to use GetPointData()->AddArray() rather than SetScalars()/SetVectors(), which has caused me trouble if I have more than one scalar/vector.
I don't know if that is necessarily the best practice, but it has worked well for me.

Hope that helps,
joe.

On Apr 2, 2013, at 1:10 PM, Paw Møller wrote:

> 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
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the ParaView Wiki at: http://paraview.org/Wiki/ParaView
> 
> Follow this link to subscribe/unsubscribe:
> http://www.paraview.org/mailman/listinfo/paraview

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.paraview.org/pipermail/paraview/attachments/20130402/267e406d/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1961 bytes
Desc: not available
URL: <http://www.paraview.org/pipermail/paraview/attachments/20130402/267e406d/attachment.bin>


More information about the ParaView mailing list