Advertisement
hasegawa

csvvtk

Sep 20th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 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_1=0,newline_count_2=0,Lines;
  9.     FILE *fpi1,*fpi2,*fpo;
  10.     float x,y,z;
  11.  
  12.     if(argc!=4)
  13.     {
  14.         fprintf(stderr,"Usage: %s (1)csvfile\n(2)csvfile\n(3)vtkfile\n",argv[0]);
  15.  
  16.         return 0;
  17.     }
  18.  
  19.     printf("Open CSV File\n");
  20.     //Open CSV1 file
  21.     if((fpi1=fopen (argv[1],"r"))==NULL)
  22.     {
  23.         printf("The file can't opened. The program is exit.\n ");
  24.  
  25.         return 0;
  26.     }
  27.  
  28.     printf("Open CSV File\n");
  29.     //Open CSV2 file
  30.     if((fpi2=fopen (argv[2],"r"))==NULL)
  31.     {
  32.         printf("The file can't opened. The program is exit.\n ");
  33.  
  34.         return 0;
  35.     }
  36.  
  37.     printf("Open VTK File\n");
  38.     //Open VTK file for save
  39.     if((fpo=fopen (argv[3],"w"))==NULL)
  40.     {
  41.         printf("The file can't opened. The program is exit.\n ");
  42.  
  43.         return 0;
  44.     }
  45.  
  46.     //count how many lines
  47.     /*
  48.     while((c==fgetc(fpi))!=EOF)
  49.     {
  50.         if(c=='\n')
  51.             newline_count++;
  52.     }
  53.     */
  54.  
  55.     printf("CSVFile1 Count Lines\n");
  56.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  57.     {
  58.         newline_count_1=newline_count_1+1;
  59.     }
  60.  
  61.     printf("CSVFile2 Count Lines\n");
  62.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  63.     {
  64.         newline_count_2=newline_count_2+1;
  65.     }
  66.  
  67.     printf("total_1=%ld\n",newline_count_1);
  68.  
  69.     printf("total_2=%ld\n",newline_count_2);
  70.  
  71.     Lines=newline_count_1+newline_count_2;
  72.  
  73.     printf("total Lines=%ld\n",Lines);
  74.  
  75.     //writing VTK file
  76.     fprintf(fpo,"# vtk DataFile Version 3.0\n");
  77.     fprintf(fpo,"vtk output\n");
  78.     fprintf(fpo,"ASCII\n");
  79.     fprintf(fpo,"DATASET UNSTRUCTURED_GRID\n");
  80.     fprintf(fpo,"POINTS %ld float\n",Lines);
  81.  
  82.     fseek(fpi1,0,SEEK_SET);
  83.  
  84.     fseek(fpi2,0,SEEK_SET);
  85.  
  86.     printf("writing csv1 x y z\n");
  87.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  88.     {
  89.         //printf ("%f,%f,%f,%d,%d,%d\n",x,y,z,r,g,b);
  90.         fprintf(fpo,"%f %f %f\n",x,y,z);
  91.     }
  92.  
  93.     printf("writing csv2 x y z\n");
  94.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  95.     {
  96.         //printf ("%f,%f,%f,%d,%d,%d\n",x,y,z,r,g,b);
  97.         fprintf(fpo,"%f %f %f\n",x,y,z);
  98.     }
  99.  
  100.     fprintf(fpo,"CELLS %ld %ld\n",Lines,Lines*2);
  101.  
  102.     for(i=0;i<Lines;i++)
  103.     {
  104.         fprintf(fpo,"1 %d\n",i);
  105.     }
  106.  
  107.     fprintf(fpo,"CELL_TYPES %ld\n",Lines);
  108.  
  109.     for(i=0;i<Lines;i++)
  110.     {
  111.         fprintf(fpo,"1\n");
  112.     }
  113.  
  114.     fprintf(fpo,"POINT_DATA %ld\n",Lines);
  115.  
  116.     fprintf(fpo,"COLOR_SCALARS RGB 3\n");
  117.  
  118.     fseek(fpi1,0,SEEK_SET);
  119.  
  120.     fseek(fpi2,0,SEEK_SET);
  121.  
  122.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  123.     {
  124.         fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  125.     }
  126.  
  127.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
  128.     {
  129.         fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  130.     }
  131.  
  132.     fclose(fpi1);
  133.     fclose(fpi2);
  134.     fclose(fpo);
  135.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement