/*                                     */
/* iv2obj.c by Gerhard.Hippmann@dlr.de */
/* iv2obj.b <filename> converts        */
/* Open Inventor File <filename>.iv    */
/* to Wavefront File  <filename>.obj   */
/*                                     */
/* gcc -o iv2obj.b iv2obj.c            */
/*                                     */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main( int argc, char *argv[] );


int main( int narg, char *arg[] )
{
   long  ix;
   long  nvert;
   long  nface;
   long  f1, f2, f3;
   char  ch;
   char  infile[128];
   char  outfile[128];
   char  line[128];
   float  scal;
   float  shfx, shfy, shfz;
   float  vx, vy, vz;
   FILE  *ivfile;
   FILE  *objfile;

   scal = 1.0e0;
   shfx = 0.0e0;
   shfy = 0.0e0;
   shfz = 0.0e0;

   if ( narg == 6 )
   {
      strcpy( infile, arg[1] );
      strcpy( outfile, arg[1] );
      strcat( infile, ".iv" );
      strcat( outfile, ".obj" );
      narg = sscanf( arg[2], "%e", &scal );
      narg = sscanf( arg[3], "%e", &shfx );
      narg = sscanf( arg[4], "%e", &shfy );
      narg = sscanf( arg[5], "%e", &shfz );

      printf( " * Input-File     = %s\n", infile );
      printf( " * Scaling-Factor = %e\n", scal );
      printf( " * Shift in x     = %e\n", shfx );
      printf( " * Shift in y     = %e\n", shfy );
      printf( " * Shift in z     = %e\n", shfz );
   }
   else if ( narg == 3 )
   {
      strcpy( infile, arg[1] );
      strcpy( outfile, arg[1] );
      strcat( infile, ".iv" );
      strcat( outfile, ".obj" );
      narg = sscanf( arg[2], "%e", &scal );

      printf( " * Input-File     = %s\n", infile );
      printf( " * Scaling-Factor = %e\n", scal );
   }
   else if ( narg == 2 )
   {
      strcpy( infile, arg[1] );
      strcpy( outfile, arg[1] );
      strcat( infile, ".iv" );
      strcat( outfile, ".obj" );

      printf( " * Input-File     = %s\n", infile );
   }
   else
   {
      printf( "** ERROR: Wrong Number of Arguments narg = %d\n", narg-1 );
      return 1;
   }

   ivfile = fopen( infile, "rt" );

   if ( ivfile == NULL )
   {
      printf( "** ERROR: Cannot open File: %s\n", infile );
      return 1;
   }

   objfile = fopen( outfile, "wt" );

   if ( outfile == NULL )
   {
      printf( "** ERROR: Cannot open File: %s\n", outfile );
      return 1;
   }

   printf( " * Converting..." );

   fprintf( objfile, "\n" );
   fprintf( objfile, "# Vertex List\n" );

   ix = 0;
   nvert = 0;
   nface = 0;
   do
   {
      /* Read next Character */
      ch = getc( ivfile );

      if ( ch == '\n' || ch == EOF )
      {
         /* Process Line */
         line[ix] = '\0';
         narg = sscanf( line, "%d, %d, %d,", &f1, &f2, &f3 );
         if ( narg == 3 )
         {
            nface++;
            f1++;
            f2++;
            f3++;
/*
printf( "f %d %d %d\n", f1,f2,f3 );
*/
            fprintf( objfile, "f %d %d %d\n", f1, f2, f3 );
         }
         else
         {
            narg = sscanf( line, "%e %e %e", &vx, &vy, &vz );
            if ( narg == 3 )
            {
               nvert++;
               vx = (vx + shfx) * scal;
               vy = (vy + shfy) * scal;
               vz = (vz + shfz) * scal;
/*
printf( "v2 %e %e %e\n", vx,vy,vz );
*/
               fprintf( objfile, "v %13e %13e %13e\n", vx, vy, vz );
            }
            else
            {
               narg = sscanf( line, "    coordIndex [ %d, %d, %d,", &f1, &f2, &f3 );
               if ( narg == 3 )
               {
                  nface++;
                  f1++;
                  f2++;
                  f3++;
/*
printf( "f %d %d %d\n", f1,f2,f3 );
*/
                  fprintf( objfile, "\n" );
                  fprintf( objfile, "# Face List\n" );
                  fprintf( objfile, "f %d %d %d\n", f1, f2, f3 );
               }
            }
         }

         ix = 0;
      }
      else if ( ix <= 126 )
         /* Apend next Character to line */
         line[ix++] = ch;
   }
   while ( ch != EOF );

   fprintf( objfile, "\n" );
   fprintf( objfile, "# Number of Vertices: %d\n", nvert );
   fprintf( objfile, "# Number of Faces:    %d\n", nface );
   fprintf( objfile, "\n" );
   fprintf( objfile, "# End of File\n" );

   printf( "done\n" );
   printf( " * Number of Vertices: %d\n", nvert );
   printf( " * Number of Faces:    %d\n", nface );

   fclose( ivfile );

   fclose( objfile );

   return 0;

}
