FiberBundleHDF5  $Id: FiberHDF5.dfg,v 1.8 2006/12/12 12:32:50 werner Exp $
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>
#include <math.h>
#include <stdlib.h> /* atof */
#define MAX_VERTICES 49325149
#define MAX_TRIANGLES 1000000
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[])
{
printf(" \n");
printf(" \n");
printf("##########################################################################\n");
printf(" OBJ to F5 \n");
printf(" Usage: Either: \n");
printf(" <ObjtoF5 file_name.obj> OR \n");
printf(" <ObjtoF5 fileprefix_ 0.5 10 out> whereas 0.5 is delta t, 10 file numbers and out f5 file-name \nn");
printf(" 'FilePrefix must be: fileprefix_000000.obj fileprefix_000001,obj.... \n");
printf("##########################################################################\n");
printf(" \n");
printf(" \n");
hid_t fileID;
char gridname[1024];
// char buf_second[1024];
char buf[1024];
F5Path *myFields = 0;
FILE*what;
int t, i;
if (argc<2)
{
puts("Please specify <inputfile.obj>. Thanks.");
return 1;
}
/* putenv("F5_VERBOSITY=30");*/
printf("\n");
unsigned k=0;
for (k=0; k < argc ; k++)
printf("argv is: %s at position %d \n", argv[k],k);
printf("\n");
unsigned file_number = 0;
double delta_t = 0;
if(argc > 2)
{
printf("time series is used: delta t: %s and file numbers: %s\n", argv[2],argv[3],argv[4]);
delta_t = atof(argv[2]);
file_number = atoi(argv[3]);
printf("delta t %f", delta_t );
printf("delta t %d", file_number );
}
else
{
file_number = 1;
}
printf("\n");
if (argc == 5)
strcpy(buf, argv[4]);
else
strcpy(buf, argv[1]);
char*c = strrchr(buf, '.');
if (c)
{
*c = 0;
char*n = strrchr(buf,'/');
if( n != NULL )
{
strcpy(gridname, n+1);
}
else
{
strcpy(gridname, buf);
}
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 */
printf("F5 file with single obj is: %s\n",buf);
}
else
{
char*n = strrchr(buf,'/');
if( n != NULL )
{
strcpy(gridname, n+1);
}
else
{
strcpy(gridname, buf);
}
strcat(buf, ".f5");
printf("right4 %s\n",buf);
fileID = H5Fcreate(buf, /* The FileName */
H5F_ACC_TRUNC, /* Creation Flags */
H5P_DEFAULT, /* File creation property list identifier */
H5P_DEFAULT); /* File access property list identifier */
printf("F5 file with multiple obj is: %s\n",buf);
}
// open single or multiple files (times series)
unsigned which_file = 0;
for (which_file=0; which_file < file_number; which_file++)
{
printf("This is file: %d\n", which_file);
int nCoords = 0,
nVertexNormals = 0,
nTexture = 0,
nTriangles = 0;
char fname[40]; fname;
sprintf(fname,"%s%06d.obj", argv[1],which_file);
if(argc < 3)
sprintf(fname,"%s", argv[1]);
what = fopen(fname, "rt");
if (!what)
{
printf("Cannot open '%s'. That's not good.\n", fname );
return 2;
}
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=0, vt0=0, vn0=0, v1=0, vt1=0, vn1=0, v2=0, vt2=0, vn2=0;
if (sscanf(buf, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
&v0, &vt0, &vn0, &v1, &vt1, &vn1, &v2, &vt2, &vn2) != 9)
if (sscanf(buf, "f %d %d %d\n",
&v0, &v1, &v2 ) != 3)
if(sscanf(buf, "f %d/%d %d/%d %d/%d\n",
&v0, &vt0, &v1, &vt1, &v2, &vt2) != 6)
if(sscanf(buf, "f %d//%d %d//%d %d//%d\n",
&v0, &vn0, &v1, &vn1, &v2, &vn2) != 6)
// printf(" v %d %d %d\n", v0, v1, v2);
v0 = v0; // lol
// printf("%s:\n", buf);
Triangles[nTriangles].i = v0-1;
Triangles[nTriangles].j = v1-1;
Triangles[nTriangles].k = v2-1;
// printf(" t %d %d %d\n", Triangles[nTriangles].i, Triangles[nTriangles].j, Triangles[nTriangles].k);
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);
double time = which_file*delta_t;
myFields = F5write_triangular_surface(fileID, time , gridname,
Coords, nCoords,
Triangles, nTriangles);
} // file reading finished...
if (!myFields)
puts("ERROR in writing surface...");
F5Fclose(myFields);
H5Fclose(fileID);
puts(" **** Done ****");
return 0;
}
void F5Fclose(F5Path *f)
Definition: F5F.c:92
F5Path * F5write_triangular_surface(hid_t file_id, double time, const char *name, const F5_vec3_point_t *Coords, int nCoords, const F5_triangle_t *Triangles, int nTriangles)
Definition: F5surface.c:9
Definition: F5Path.h:31
Definition: F5coordinates.h:58
Definition: F5coordinates.h:208
Definition: F5coordinates.h:246
Definition: F5coordinates.h:57