ObjtoF5.c

Converts a triangular surface given in Wavefront .obj format into F5.

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <hdf5.h>

#include <F5/F5X.h>
#include <F5/F5F.h>
#include <F5/F5B.h>
#include <F5/F5R.h>
#include <F5/F5coordinates.h>
#include <F5/F5surface.h>
#include <F5/F5defs.h>

#define MAX_VERTICES  100000
#define MAX_TRIANGLES 100000

F5_vec3_float_t VertexNormals[MAX_VERTICES];
F5_vec3_point_t Coords[MAX_VERTICES];
F5_texture_point_t Texture[MAX_VERTICES];
F5_triangle_t Triangles[MAX_TRIANGLES];

int     main(int argc, char*argv[])
{
        double  time = 0.0;
        hid_t   fileID;
        char    gridname[1024];
        char    buf[1024]; 
        int     nCoords = 0, 
                nVertexNormals = 0,
                nTexture = 0,
                nTriangles = 0;
        F5Path *myFields = 0;
        FILE*what;
        int t, i;

        if (argc<2)
        {
                puts("Please specify <inputfile.obj>. Thanks.");
                return 1;
        }

/*      putenv("F5_VERBOSITY=30");*/

        what = fopen(argv[1], "rt");
        if (!what)
        {
                printf("Cannot open '%s'. That's not good.\n", argv[1] );
                return 2;
        }
        strcpy(buf, argv[1]);
        {
        char*c = strrchr(buf, '.');
                if (c)
                {
                        *c = 0;
                        strcat(c, ".f5");
                        fileID = H5Fcreate(buf,       /* The FileName */
                                                H5F_ACC_TRUNC,    /* Creation Flags */
                                                H5P_DEFAULT,      /* File creation property list identifier */
                                                H5P_DEFAULT);     /* File access property list identifier */
                }
                else
                {
                        puts("invalid filename.");
                        return 3;
                }
                *c = 0;
                strcpy(gridname, buf);
        }
        

        while( fgets(buf, sizeof(buf), what) )
        {
                double  x,y,z; 

                switch(*buf)
                {
                case 'v': /* VERTEX Information */
                        switch(buf[1])
                        {
                        case 't': /* vertex texture coordinates, 2 floats following */
                                if (sscanf(buf, "vt %lg %lg %lg\n", &x, &y, &z) != 2)
                                        continue; 
                
                                Texture[nTexture].u = x; 
                                Texture[nTexture].v = y;                                
                                nTexture++;
                                continue;
                        
                        case 'n': /* cartesian normal vector, 3 floats following */
                                if (sscanf(buf, "vn %lg %lg %lg\n", &x, &y, &z) != 3)
                                        continue; 
                
                                VertexNormals[nVertexNormals].x = x; 
                                VertexNormals[nVertexNormals].y = y; 
                                VertexNormals[nVertexNormals].z = z; 
                                nVertexNormals++;
                                continue;

                        default:  /* cartesian coordinates, 3 floats following */
                                if (sscanf(buf, "v %lg %lg %lg\n", &x, &y, &z) != 3)
                                        continue; 
                
                                Coords[nCoords].x = x; 
                                Coords[nCoords].y = y; 
                                Coords[nCoords].z = z; 
                                nCoords++;
                                continue;
                        }

                case 'f':
                        {
                        int v0, vt0, vn0,  v1, vt1, vn1,  v2, vt2, vn2;
                        if (sscanf(buf, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
                                        &v0, &vt0, &vn0,  &v1, &vt1, &vn1,  &v2, &vt2, &vn2) != 9)
                                continue; 

                        Triangles[nTriangles].i = v0-1;
                        Triangles[nTriangles].j = v1-1;
                        Triangles[nTriangles].k = v2-1;
                        nTriangles++;
                        continue;
                        }
                }
        }
        if (!nCoords)
        {
                H5Fclose(fileID);       
                return 0;
        }
        printf("Triangular surface: %d vertices, %d normals, %d texture coordinates, %d triangles\n",
                        nCoords, nVertexNormals, nTexture, nTriangles);


        for(t=0; t<5; t++)
        {
                myFields = F5write_triangular_surface(fileID, time, gridname,
                                                   Coords,  nCoords,
                                                   Triangles, nTriangles);

                if (!myFields)
                        puts("ERROR in writing surface...");


                F5Fclose(myFields);


                for(i=0; i<nCoords; i++)
                {
                        Coords[i].x += 10*time;
                        Coords[i].y += 10*time;
                        //Coords[i].z; 
                }
                time = 0.2*t;
                printf("TIME: %lg\n", time);

        }
        H5Fclose(fileID);       
        return 0;
}