C Code to write VTK Structured Points

This simple C code is used to write out a 2-D array (A) of size N to an ASCII file. The legacy VTK data file format is used. The data is written out as structured points (also known as a Cartesian grid).

 1.[N]
 2.void Write_VTK_Structured_Points(float *A, int N, const char *filename)
 3.{
 4.   FILE *out;
 5.   out = fopen(filename,"w");
 6.   fprintf(out,"# vtk DataFile Version 3.0\n");
 7.   fprintf(out,"Laplace\n");
 8.   fprintf(out,"ASCII\n");
 9.   fprintf(out,"DATASET STRUCTURED_POINTS\n");
10.    fprintf(out,"DIMENSIONS %d %d %d\n",N,N,1);
11.    fprintf(out,"ORIGIN %d %d %d\n",0,0,0);
12.    fprintf(out,"SPACING %d %d %d\n",1,1,1);
13.    fprintf(out,"POINT_DATA %d\n",N*N);
14.    fprintf(out,"SCALARS Laplace float 1\n");
15.    fprintf(out,"LOOKUP_TABLE default\n");
16.    for(int i=0; i<N*N; i++) {
17.      fprintf(out,"%f\n",A[i]);
18.    }
19.    fclose(out);
20. }

Back to Types of Grids