Advertisement
d10070dd

vtkfile

Sep 14th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     //Decralation of variables
  6.     int i;
  7.     int r,g,b;
  8.     unsigned long newline_count=0;
  9.     FILE *fpi,*fpo;
  10.     float x,y,z;
  11.  
  12.     if(argc!=3)
  13.     {
  14.         fprintf(stderr,"Usage: %s (1)CSVFile\n(2)VTKFile\n",argv[0]);
  15.  
  16.         return 0;
  17.     }
  18.  
  19.     //Open CSV file
  20.     if((fpi=fopen(argv[1],"r"))==NULL)
  21.     {
  22.         printf ("The file can't be opened. The program is exit.\n");
  23.  
  24.         return 0;
  25.     }
  26.  
  27.     //Open VTK file for save
  28.     if((fpo=fopen(argv[2],"w"))==NULL)
  29.     {
  30.             printf("The file can't be opened. The program is exit.\n");
  31.  
  32.             return 0;
  33. }
  34.  
  35.  
  36. //count how many lines
  37. /*
  38. while((c=fgetc(fpi))!=EOF)
  39. {
  40.     if(c=='\n')
  41.         newline_count++;
  42. }
  43. */
  44.  
  45. while(fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  46. {
  47.     newline_count=newline_count+1;
  48. }
  49.  
  50. printf("total=%ld\n",newline_count);
  51.  
  52. //writing VTK file
  53. fprintf(fpo,"# vtk DataFile Version 3.0\n");
  54. fprintf(fpo,"vtk output\n");
  55. fprintf(fpo,"ASCII\n");
  56. fprintf(fpo,"DATASET UNSTRUCTURED_GRID\n");
  57. fprintf(fpo,"POINTS %ld float\n",newline_count);
  58.  
  59.  
  60. fseek(fpi,0,SEEK_SET);
  61.  
  62. while (fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  63. {
  64.     printf("%f,%f,%f,%d,%d,%d\n",x,y,z,r,g,b);
  65.     fprintf(fpo,"%f%f%f\n",x,y,z);
  66. }
  67.  
  68. fprintf(fpo,"CELLS %ld %ld\n",newline_count,newline_count*2);
  69.  
  70. for(i=0;i<newline_count;i++)
  71. {
  72.     fprintf(fpo,"1 %d/n",i);
  73. }
  74.  
  75. fprintf(fpo,"CELL_TYPES %ld\n",newline_count);
  76.  
  77. for(i=0;i<newline_count;i++)
  78. {
  79.     fprintf(fpo,"1\n");
  80. }
  81.  
  82. fprintf(fpo,"POINT_DATA %ld\n",newline_count);
  83.  
  84. fprintf(fpo,"COLOR_SCALARS RGB 3\n");
  85.  
  86. fseek(fpi,0,SEEK_SET);
  87.  
  88. while(fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  89. {
  90.     fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  91. }
  92.  
  93. fclose(fpi);
  94. fclose(fpo);
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement