[vtkusers] writing VTK XML file using vtkXMLWriterC.h...
fred
fredmfp at gmail.com
Thu Sep 21 10:40:51 EDT 2006
Hi all,
I'm trying to write VTK files in XML format for unstructured grid using
vtkXMLWriterC.h.
I read the TestXMLCInterface.c example, which displays _one_ cell.
I wrote my C code following this example.
It works fine for _one_ cell (ie dim_x = dim_y = dim_z = 1)
But when there is more than one cell (dim_x = 2), it fails with segfault :
GNU gdb 6.3-debian
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-linux"...Using host libthread_db
library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) run
Starting program: /home/fred/tmp/test
[Thread debugging using libthread_db enabled]
[New Thread -1220281536 (LWP 4761)]
vtk_points : 3×12
0 : 0 0 0
1 : 1 0 0
2 : 2 0 0
3 : 0 1 0
4 : 1 1 0
5 : 2 1 0
6 : 0 0 1
7 : 1 0 1
8 : 2 0 1
9 : 0 1 1
10 : 1 1 1
11 : 2 1 1
vtk_cells : 2×8
0 : 0 1 4 3 6 7 10 9
1 : 1 2 5 4 7 8 11 10
vtk_tab : 12
0 1 2 3 4 5 6 7 8 9 10 11
Writing foo.vtu file...
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1220281536 (LWP 4761)]
0xb7d61fdd in vtkDataCompressor::Compress () from
/usr/local/lib/libvtkIO.so.5.0
What am I doing wrong ?
Any suggestion ?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include "vtkXMLWriterC.h"
#define real float
#define VTK_UNSTRUCTURED_GRID 4
#define VTK_FLOAT32 10
#define VTK_HEXAHEDRON 12
#define VTK_NB_POINTS_HEXAHEDRON 8
#define dim_x 2
#define dim_y 1
#define dim_z 1
#define nbcells (dim_x*dim_y*dim_z)
#define nbpoints ((dim_x+1)*(dim_y+1)*(dim_z+1))
#define dx 1.
#define dy 1.
#define dz 1.
#define LENSTR 256
real indx[dim_x+1], indy[dim_y+1], indz[dim_z+1];
void write_vtk_file(real[][][], char*);
int main()
{
int i, j, k;
int p;
int dimx=dim_x+1, dimy=dim_y+1, dimz=dim_z+1;
real tab[dim_x+1][dim_y+1][dim_z+1];
for (i=0; i<dimx; i++)
indx[i] = i*dx;
for (j=0; j<dimy; j++)
indy[j] = j*dy;
for (k=0; k<dimz; k++)
indz[k] = k*dz;
p = 0;
for (k=0; k<dimz; k++)
for (j=0; j<dimy; j++)
for (i=0; i<dimx; i++)
tab[i][j][k] = p++;
write_vtk_file(tab, "foo");
return 0;
}
void write_vtk_file(real tab[dim_x+1][dim_y+1][dim_z+1], char *car)
{
char filename[LENSTR];
int i, j, k;
int p;
int dimx=dim_x+1, dimy=dim_y+1, dimz=dim_z+1;
int *vtk_cells;
real *vtk_points, *vtk_tab;
vtkXMLWriterC *writer=vtkXMLWriterC_New();
snprintf(filename, sizeof(filename), "%s.vtu", car);
/* vtk_points */
vtk_points = malloc(3*nbpoints*sizeof(real));
p = 0;
for (k=0; k<dimz; k++)
for (j=0; j<dimy; j++)
for (i=0; i<dimx; i++)
{
vtk_points[p++] = indx[i];
vtk_points[p++] = indy[j];
vtk_points[p++] = indz[k];
}
printf("vtk_points : 3%d\n", nbpoints);
for (p=0; p<3*nbpoints; p+=3)
printf("%d : %g %g %g\n", p/3, vtk_points[p], vtk_points[p+1],
vtk_points[p+2]);
printf("\n");
/* vtk_cells */
vtk_cells = malloc((nbcells*(VTK_NB_POINTS_HEXAHEDRON+1))*sizeof(int));
p = 0;
for (k=0; k<dim_z; k++)
for (j=0; j<dim_y; j++)
for (i=0; i<dim_x; i++)
{
vtk_cells[p++] = VTK_NB_POINTS_HEXAHEDRON;
vtk_cells[p++] = k*(dimx*dimy) + j*dimx + i;
vtk_cells[p++] = k*(dimx*dimy) + j*dimx + i+1;
vtk_cells[p++] = k*(dimx*dimy) + (j+1)*dimx + i+1;
vtk_cells[p++] = k*(dimx*dimy) + (j+1)*dimx + i;
vtk_cells[p++] = (k+1)*(dimx*dimy) + j*dimx + i;
vtk_cells[p++] = (k+1)*(dimx*dimy) + j*dimx + i+1;
vtk_cells[p++] = (k+1)*(dimx*dimy) + (j+1)*dimx + i+1;
vtk_cells[p++] = (k+1)*(dimx*dimy) + (j+1)*dimx + i;
}
printf("vtk_cells : %d%d\n", nbcells, VTK_NB_POINTS_HEXAHEDRON);
for (p=0; p<nbcells*(VTK_NB_POINTS_HEXAHEDRON+1);
p+=(VTK_NB_POINTS_HEXAHEDRON+1))
printf("%d : %d %d %d %d %d %d %d %d\n", p/VTK_NB_POINTS_HEXAHEDRON,
vtk_cells[p+1], vtk_cells[p+2], vtk_cells[p+3], vtk_cells[p+4],
vtk_cells[p+5], vtk_cells[p+6], vtk_cells[p+7], vtk_cells[p+8]);
printf("\n");
/* vtk_tab */
vtk_tab = malloc(nbpoints*sizeof(real));
p = 0;
for (k=0; k<dimz; k++)
for (j=0; j<dimy; j++)
for (i=0; i<dimx; i++)
vtk_tab[p++] = tab[i][j][k];
printf("vtk_tab : %d\n", nbpoints);
for (i=0; i<nbpoints; i++)
printf("%g ", vtk_tab[i]);
printf("\n");
printf("Writing %s file... ", filename); fflush(stdout);
vtkXMLWriterC_SetDataObjectType(writer, VTK_UNSTRUCTURED_GRID);
vtkXMLWriterC_SetPoints(writer, VTK_FLOAT32, vtk_points, nbpoints);
vtkXMLWriterC_SetCellsWithType(writer,
VTK_HEXAHEDRON, nbcells, vtk_cells,
VTK_NB_POINTS_HEXAHEDRON+1);
vtkXMLWriterC_SetFileName(writer, filename);
vtkXMLWriterC_SetPointData(writer, car, VTK_FLOAT32, vtk_tab, nbpoints,
1, "SCALARS");
vtkXMLWriterC_Write(writer);
vtkXMLWriterC_Delete(writer);
printf("done !\n"); fflush(stdout);
free(vtk_tab); vtk_tab = NULL;
free(vtk_cells); vtk_cells = NULL;
free(vtk_points); vtk_points = NULL;
}
/* Local Variables: */
/* mode: C */
/* compile-command: "gcc -Wall -ggdb3 test.c -I /usr/local/include -L
/usr/local/lib -lvtkIO -o test" */
/* End: */
Cheers,
--
F.
More information about the vtkusers
mailing list