[vtkusers] Binary File Format for VTK

Angus Grandison A.J.Grandison at gre.ac.uk
Tue Jan 22 13:25:06 EST 2002


I have successfully created an ASCII VTK file output stream for my code
but have failed to get this to work for binary when loading the data
into MayaVi.

Help! Please see code fragment below with the old ascii stuff commented
out.

Has anybody written an ASCII to BINARY VTK converter?

TIA

Angus

Code Fragment.....
   maya_file_unit = fopen( mayavi_file_name, "wb" );

   fprintf(maya_file_unit, "# vtk DataFile Version 2.0\n");
   fprintf(maya_file_unit, "Title\n");
   fprintf(maya_file_unit, "BINARY\n");
   fprintf(maya_file_unit, " \n");
   fprintf(maya_file_unit, " \n");
   fprintf(maya_file_unit, "DATASET UNSTRUCTURED_GRID\n");

   // Do points here

   fprintf(maya_file_unit, "POINTS %i float\n", domain.num_of_points);

   for ( i = 1; i <= domain.num_of_points; i++ ) {

      // fprintf(maya_file_unit, "%f %f %f\n", point[i]->x, point[i]->y,

point[i]->z);
      value = point[i]->x;
      fwrite((char*)(&value), sizeof(Float_Type), 1, maya_file_unit);
      value = point[i]->y;
      fwrite((char*)(&value), sizeof(Float_Type), 1, maya_file_unit);
      value = point[i]->z;
      fwrite((char*)(&value), sizeof(Float_Type), 1, maya_file_unit);

   }


   Int_Type num_of_cell_point_vals;

   num_of_cell_point_vals = domain.num_of_cells * 9;

   fprintf(maya_file_unit, "\nCELLS %i %i\n", domain.num_of_cells,
num_of_cell_point_vals);

   for ( icell = 1; icell <= domain.num_of_cells; icell++ ) {

      // fprintf(maya_file_unit, "8 ");

      ivalue = 8;

      fwrite((char*)(&ivalue), sizeof(Int_Type), 1, maya_file_unit);

      for ( i = 1; i <= 4; i++ ) {

         // fprintf(maya_file_unit, "%i ",
face[cell[icell]->face_num[1]]->pt_num[i] - 1);
         ivalue = face[cell[icell]->face_num[1]]->pt_num[i] - 1;
         fwrite((char*)(&ivalue), sizeof(Int_Type), 1, maya_file_unit);

      }

      for ( i = 1; i <= 4; i++ ) {

         // fprintf(maya_file_unit, "%i ",
face[cell[icell]->face_num[2]]->pt_num[i] - 1);
         ivalue = face[cell[icell]->face_num[2]]->pt_num[i] - 1;
         fwrite((char*)(&ivalue), sizeof(Int_Type), 1, maya_file_unit);
      }

      // fprintf(maya_file_unit, "\n");

   }

   fprintf(maya_file_unit, "\nCELL_TYPES %i\n", domain.num_of_cells);

   for (icell = 1; icell <= domain.num_of_cells; icell++) {

      // fprintf(maya_file_unit, "12\n");
      ivalue = 12;
      fwrite((char*)(&ivalue), sizeof(Int_Type), 1, maya_file_unit);

   }

   fprintf(maya_file_unit, "\nPOINT_DATA %i\n", domain.num_of_points);

   Var_Index_Type var_point;
   float val;
   int  ivar;

   for ( ivar = 1; ivar <= total_vars; ivar++ ){

      var_point = full_var_list[ivar];

      fprintf(maya_file_unit, "\nSCALARS %-s float 1\n",
variable[var_point].get_full_variable_name());
      fprintf(maya_file_unit, "LOOKUP_TABLE default\n");

      for (ipoint = 1; ipoint <= domain.num_of_points; ipoint++ ) {

         val = 0.0;

         for ( i = 1; i <= point_cell[ipoint][0]; i++ ) {

            val +=
(cell[point_cell[ipoint][i]]->access(NEWEST,var_point) *
point_cell_w[ipoint][i]);

         }

         // fprintf(maya_file_unit, "%f\n", val);
         fwrite((char*)(&val), sizeof(Float_Type), 1, maya_file_unit);
      }

   }

   fprintf(maya_file_unit, "\nVECTORS Velocity float\n");

   for (ipoint =1; ipoint <= domain.num_of_points; ipoint++ ) {

      float vx, vy, vz;

      vx = vy = vz = 0.0;

      for ( i = 1; i <= point_cell[ipoint][0]; i++ ) {

         vx += (cell[point_cell[ipoint][i]]->access(NEWEST, U_VELOCITY)
* point_cell_w[ipoint][i]);
         vy += (cell[point_cell[ipoint][i]]->access(NEWEST, V_VELOCITY)
* point_cell_w[ipoint][i]);
         vz += (cell[point_cell[ipoint][i]]->access(NEWEST, W_VELOCITY)
* point_cell_w[ipoint][i]);

      }

      // fprintf(maya_file_unit, "%f %f %f\n", vx, vy, vz);
      fwrite((char*)(&vx), sizeof(Float_Type), 1, maya_file_unit);
      fwrite((char*)(&vy), sizeof(Float_Type), 1, maya_file_unit);
      fwrite((char*)(&vz), sizeof(Float_Type), 1, maya_file_unit);
   }

   fclose(maya_file_unit);
}



--
_____________________________________________________________________

Angus Grandison
School of CMS
University of Greenwich
Maritime Greenwich Campus
Old Royal Naval College
Greenwich
SE10 9LS.
UNITED KINGDOM

+44 (0)20 8331 7912      mailto:A.J.Grandison at gre.ac.uk
_____________________________________________________________________








More information about the vtkusers mailing list