[vtkusers] newbie VTK file format for vector data

Jean Favre jfavre at cscs.ch
Mon Jan 22 12:41:17 EST 2007


deusexcomputer at gmail.com wrote:
> I have trying to write out a vtk file with a custom cpp function
> however I am unsure of how to format a file for vector data. 
I suggest the following source code example, which you can test and then
adapt to your own needs.

#include "vtkXMLWriterC.h"
#include "vtkType.h"

#define NPOINTS 20

float vectordata[NPOINTS][NPOINTS][NPOINTS][3];

int main()
{
  int i, j, k;
  vtkXMLWriterC* writer = vtkXMLWriterC_New();
  const char filename[] = "cube.vti";

  double origin[3] = {-.5, -.5, -.5};

  double spacing[3] = { 10.0/(NPOINTS-1.0),
                        10.0/(NPOINTS-1.0),
                        10.0/(NPOINTS-1.0)};
  int extent[6] = {0, NPOINTS-1, 0, NPOINTS-1, 0, NPOINTS-1};

  for(i=0; i<NPOINTS; i++)
    {
    double x = origin[0]+i*spacing[0];
    for(j=0; j<NPOINTS; j++)
      {
      double y = origin[0]+j*spacing[1];
      for(k=0; k<NPOINTS; k++)
        {
        double z = origin[0]+k*spacing[2];
        vectordata[i][j][k][0] = (-10.0 * x + 10.0 * y);
        vectordata[i][j][k][1] = (28.0 * x - y - x*z);
        vectordata[i][j][k][2] = (-8.0/(3.0*z) + x*y);
        }
      }
    }

  /* #define VTK_IMAGE_DATA                      6*/
  vtkXMLWriterC_SetDataObjectType(writer, VTK_IMAGE_DATA);

  vtkXMLWriterC_SetExtent(writer, extent);

  vtkXMLWriterC_SetFileName(writer, filename);

  vtkXMLWriterC_SetOrigin(writer, origin);

  vtkXMLWriterC_SetSpacing(writer, spacing);

  vtkXMLWriterC_Start(writer);

  /* #define VTK_FLOAT          10 */
  vtkXMLWriterC_SetPointData(writer, "velocity", 10, vectordata,
                               NPOINTS * NPOINTS * NPOINTS, 3, "VECTORS");
  vtkXMLWriterC_Write(writer);
  vtkXMLWriterC_Stop(writer);
  vtkXMLWriterC_Delete(writer);

  return 0;
}

Compilation is trivial if you have cmake installed. The CMakeLists.txt
file would look like this:

PROJECT(CUBEIO)
FIND_PACKAGE(VTK)
IF(NOT VTK_FOUND)
  MESSAGE(FATAL_ERROR "VTK is required.  Set VTK_DIR.")
ENDIF(NOT VTK_FOUND)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(WriteCube WriteCube.c)
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR})
TARGET_LINK_LIBRARIES(WriteCube vtkIO)

Enjoy
-- 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070122/e21876dd/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jf-sign.gif
Type: image/gif
Size: 11512 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070122/e21876dd/attachment.gif>


More information about the vtkusers mailing list