[vtkusers] writing VTK binary file...

Dominik Szczerba domi at vision.ee.ethz.ch
Sat Apr 15 07:05:23 EDT 2006


dont you need to swap bytes by any chance?
-ds

fred wrote:
> Hello,
> 
> I'm trying to write VTK file in binary format,
> but I don't understand how it works.
> 
> It seems that mayavi can't get reading my binary file
> (works fine in ASCII).
> 
> I attached a small C source code for example.
> 
> Where am I wrong ?
> 
> 
> Thanks in advance.
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> #include <stdio.h>
> #include <math.h>
> #include <string.h>
> 
> int main(void)
> {
>  char s[256];
>  int i, j, k;
>  int Nx=76, Ny=60, Nz=72;
>  float dx=0.1, dy=0.1, dz=0.1;
>  float x, y, z, foo;
>  FILE *fichier;
>  
>  fichier = fopen("essai.vtk","w");
>  strcpy(s, "# vtk DataFile Version 2.0\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  strcpy(s, "essai\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  strcpy(s, "BINARY\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  strcpy(s, "DATASET RECTILINEAR_GRID\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  sprintf(s, "DIMENSIONS %d %d %d\n", Nx, Ny, Nz); fwrite(s, sizeof(char), strlen(s), fichier);
>  sprintf(s, "X_COORDINATES %d float\n", Nx); fwrite(s, sizeof(char), strlen(s), fichier);
>  for (i=0; i<Nx; i++)
>  {
>   x = i*dx;
>   fwrite(&x, sizeof(float), 1, fichier);
>  }
>  fprintf(fichier, "\n");
>  sprintf(s, "Y_COORDINATES %d float\n", Ny); fwrite(s, sizeof(char), strlen(s), fichier);
>  for (j=0; j<Ny; j++)
>  {
>   y = j*dy;
>   fwrite(&y, sizeof(float), 1, fichier);
>  }
>  fprintf(fichier, "\n");
>  sprintf(s, "Z_COORDINATES %d float\n", Nz); fwrite(s, sizeof(char), strlen(s), fichier);
>  for (k=0; k<Nz; k++)
>  {
>   z = k*dz;
>   fwrite(&z, sizeof(float), 1, fichier);
>  }
>  fprintf(fichier, "\n");
>  sprintf(s, "POINT_DATA %d\n", Nx*Ny*Nz); fwrite(s, sizeof(char), strlen(s), fichier);
>  sprintf(s, "SCALARS foo float\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  sprintf(s, "LOOKUP_TABLE default\n"); fwrite(s, sizeof(char), strlen(s), fichier);
>  for (k=0; k<Nz; k++)
>    for (j=0; j<Ny; j++)
>    {
>     for (i=0; i<Nx; i++)
>     {
>      x = i*dx; y = j*dy; z = k*dz;
>      foo = (cos(M_PI*x/(Nx*dx))*sin(M_PI*y/(Ny*dy))*sin(M_PI*z/(Nz*dz))*
> 	    (cos(M_PI*x/(Nx*dx))*sin(M_PI*y/(Ny*dy))*sin(M_PI*z/(Nz*dz)))) +
> 	   (sin(M_PI*x/(Nx*dx))*cos(M_PI*y/(Ny*dy))*sin(M_PI*z/(Nz*dz))*
> 	    (sin(M_PI*x/(Nx*dx))*cos(M_PI*y/(Ny*dy))*sin(M_PI*z/(Nz*dz)))) +
> 	   (sin(M_PI*x/(Nx*dx))*sin(M_PI*y/(Ny*dy))*cos(M_PI*z/(Nz*dz))*
> 	    (sin(M_PI*x/(Nx*dx))*sin(M_PI*y/(Ny*dy))*cos(M_PI*z/(Nz*dz))));
>      fwrite(&foo, sizeof(float), 1, fichier);
>     }
>    }
>  
>  fclose(fichier);
>  
>  return 0;
> }
> 
> /* Local Variables:  */
> /* mode: C */
> /* compile-command: "gcc -O3 -Wall vtk.c -lm -o vtk" */
> /* End:  */
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-- 
Dominik Szczerba, Dr.
Computer Vision Lab CH-8092 Zurich
http://www.vision.ee.ethz.ch/~domi



More information about the vtkusers mailing list