Back to index

radiance  4R0+20100331
Functions | Variables
readmesh.c File Reference
#include <time.h>
#include "platform.h"
#include "standard.h"
#include "octree.h"
#include "object.h"
#include "mesh.h"
#include "resolu.h"

Go to the source code of this file.

Functions

static void mesherror (int etyp, char *msg)
static long mgetint (int siz)
static double mgetflt ()
static OCTREE getfullnode ()
static OCTREE gettree ()
static void skiptree ()
static void getpatch (MESHPATCH *pp)
void readmesh (MESH *mp, char *path, int flags)

Variables

static const char RCSid [] = "$Id: readmesh.c,v 2.13 2004/09/14 02:53:50 greg Exp $"
static char * meshfn
static FILE * meshfp
static int objsize

Function Documentation

static OCTREE getfullnode ( void  ) [static]

Definition at line 65 of file readmesh.c.

{
       OBJECT set[MAXSET+1];
       register int  i;

       if ((set[0] = mgetint(objsize)) > MAXSET)
              mesherror(USER, "bad set in getfullnode");
       for (i = 1; i <= set[0]; i++)
              set[i] = mgetint(objsize);
       return(fullnode(set));
}      

Here is the call graph for this function:

Here is the caller graph for this function:

static void getpatch ( MESHPATCH pp) [static]

Definition at line 130 of file readmesh.c.

{
       int    flags;
       int    i, j;
                                   /* vertex flags */
       flags = mgetint(1);
       if (!(flags & MT_V) || flags & ~(MT_V|MT_N|MT_UV))
              mesherror(USER, "bad patch flags");
                                   /* allocate vertices */
       pp->nverts = mgetint(2);
       if (pp->nverts <= 0 || pp->nverts > 256)
              mesherror(USER, "bad number of patch vertices");
       pp->xyz = (uint32 (*)[3])malloc(pp->nverts*3*sizeof(uint32));
       if (pp->xyz == NULL)
              goto nomem;
       if (flags & MT_N) {
              pp->norm = (int32 *)calloc(pp->nverts, sizeof(int32));
              if (pp->norm == NULL)
                     goto nomem;
       } else
              pp->norm = NULL;
       if (flags & MT_UV) {
              pp->uv = (uint32 (*)[2])calloc(pp->nverts, 2*sizeof(uint32));
              if (pp->uv == NULL)
                     goto nomem;
       } else
              pp->uv = NULL;
                                   /* vertex xyz locations */
       for (i = 0; i < pp->nverts; i++)
              for (j = 0; j < 3; j++)
                     pp->xyz[i][j] = mgetint(4);
                                   /* vertex normals */
       if (flags & MT_N)
              for (i = 0; i < pp->nverts; i++)
                     pp->norm[i] = mgetint(4);
                                   /* uv coordinates */
       if (flags & MT_UV)
              for (i = 0; i < pp->nverts; i++)
                     for (j = 0; j < 2; j++)
                            pp->uv[i][j] = mgetint(4);
                                   /* local triangles */
       pp->ntris = mgetint(2);
       if (pp->ntris < 0 || pp->ntris > 512)
              mesherror(USER, "bad number of local triangles");
       if (pp->ntris) {
              pp->tri = (struct PTri *)malloc(pp->ntris *
                                          sizeof(struct PTri));
              if (pp->tri == NULL)
                     goto nomem;
              for (i = 0; i < pp->ntris; i++) {
                     pp->tri[i].v1 = mgetint(1);
                     pp->tri[i].v2 = mgetint(1);
                     pp->tri[i].v3 = mgetint(1);
              }
       } else
              pp->tri = NULL;
                                   /* local triangle material(s) */
       if (mgetint(2) > 1) {
              pp->trimat = (int16 *)malloc(pp->ntris*sizeof(int16));
              if (pp->trimat == NULL)
                     goto nomem;
              for (i = 0; i < pp->ntris; i++)
                     pp->trimat[i] = mgetint(2);
       } else {
              pp->solemat = mgetint(2);
              pp->trimat = NULL;
       }
                                   /* joiner triangles */
       pp->nj1tris = mgetint(2);
       if (pp->nj1tris < 0 || pp->nj1tris > 512)
              mesherror(USER, "bad number of joiner triangles");
       if (pp->nj1tris) {
              pp->j1tri = (struct PJoin1 *)malloc(pp->nj1tris *
                                          sizeof(struct PJoin1));
              if (pp->j1tri == NULL)
                     goto nomem;
              for (i = 0; i < pp->nj1tris; i++) {
                     pp->j1tri[i].v1j = mgetint(4);
                     pp->j1tri[i].v2 = mgetint(1);
                     pp->j1tri[i].v3 = mgetint(1);
                     pp->j1tri[i].mat = mgetint(2);
              }
       } else
              pp->j1tri = NULL;
                                   /* double joiner triangles */
       pp->nj2tris = mgetint(2);
       if (pp->nj2tris < 0 || pp->nj2tris > 256)
              mesherror(USER, "bad number of double joiner triangles");
       if (pp->nj2tris) {
              pp->j2tri = (struct PJoin2 *)malloc(pp->nj2tris *
                                          sizeof(struct PJoin2));
              if (pp->j2tri == NULL)
                     goto nomem;
              for (i = 0; i < pp->nj2tris; i++) {
                     pp->j2tri[i].v1j = mgetint(4);
                     pp->j2tri[i].v2j = mgetint(4);
                     pp->j2tri[i].v3 = mgetint(1);
                     pp->j2tri[i].mat = mgetint(2);
              }
       } else
              pp->j2tri = NULL;
       return;
nomem:
       error(SYSTEM, "out of mesh memory in getpatch");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static OCTREE gettree ( void  ) [static]

Definition at line 79 of file readmesh.c.

{
       register OCTREE       ot;
       register int  i;
       
       switch (getc(meshfp)) {
              case OT_EMPTY:
                     return(EMPTY);
              case OT_FULL:
                     return(getfullnode());
              case OT_TREE:
                     if ((ot = octalloc()) == EMPTY)
                            mesherror(SYSTEM, "out of tree space in readmesh");
                     for (i = 0; i < 8; i++)
                            octkid(ot, i) = gettree();
                     return(ot);
              case EOF:
                     mesherror(USER, "truncated mesh octree");
              default:
                     mesherror(USER, "damaged mesh octree");
       }
       return (OCTREE)NULL; /* pro forma return */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void mesherror ( int  etyp,
char *  msg 
) [static]

Definition at line 28 of file readmesh.c.

{
       char  msgbuf[128];

       sprintf(msgbuf, "(%s): %s", meshfn, msg);
       error(etyp, msgbuf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static double mgetflt ( ) [static]

Definition at line 53 of file readmesh.c.

{
       double r;

       r = getflt(meshfp);
       if (feof(meshfp))
              mesherror(USER, "truncated mesh file");
       return(r);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static long mgetint ( int  siz) [static]

Definition at line 40 of file readmesh.c.

{
       register long  r;

       r = getint(siz, meshfp);
       if (feof(meshfp))
              mesherror(USER, "truncated mesh file");
       return(r);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void readmesh ( MESH mp,
char *  path,
int  flags 
)

Definition at line 239 of file readmesh.c.

{
       char   *err;
       char   sbuf[64];
       int    i;
                                   /* check what's loaded */
       flags &= (IO_INFO|IO_BOUNDS|IO_TREE|IO_SCENE) & ~mp->ldflags;
                                   /* open input file */
       if (path == NULL) {
              meshfn = "standard input";
              meshfp = stdin;
       } else if ((meshfp = fopen(meshfn=path, "r")) == NULL) {
              sprintf(errmsg, "cannot open mesh file \"%s\"", path);
              error(SYSTEM, errmsg);
       }
       SET_FILE_BINARY(meshfp);
                                   /* read header */
       checkheader(meshfp, MESHFMT, flags&IO_INFO ? stdout : (FILE *)NULL);
                                   /* read format number */
       objsize = getint(2, meshfp) - MESHMAGIC;
       if (objsize <= 0 || objsize > MAXOBJSIZ || objsize > sizeof(long))
              mesherror(USER, "incompatible mesh format");
                                   /* read boundaries */
       if (flags & IO_BOUNDS) {
              for (i = 0; i < 3; i++)
                     mp->mcube.cuorg[i] = atof(getstr(sbuf, meshfp));
              mp->mcube.cusize = atof(getstr(sbuf, meshfp));
              for (i = 0; i < 2; i++) {
                     mp->uvlim[0][i] = mgetflt();
                     mp->uvlim[1][i] = mgetflt();
              }
       } else {
              for (i = 0; i < 4; i++)
                     getstr(sbuf, meshfp);
              for (i = 0; i < 4; i++)
                     mgetflt();
       }
                                   /* read the octree */
       if (flags & IO_TREE)
              mp->mcube.cutree = gettree();
       else if (flags & IO_SCENE)
              skiptree();
                                   /* read materials and patches */
       if (flags & IO_SCENE) {
              mp->mat0 = nobjects;
              readscene(meshfp, objsize);
              mp->nmats = nobjects - mp->mat0;
              mp->npatches = mgetint(4);
              mp->patch = (MESHPATCH *)calloc(mp->npatches,
                                   sizeof(MESHPATCH));
              if (mp->patch == NULL)
                     mesherror(SYSTEM, "out of patch memory");
              for (i = 0; i < mp->npatches; i++)
                     getpatch(&mp->patch[i]);
       }
                                   /* clean up */
       fclose(meshfp);
       mp->ldflags |= flags;
                                   /* verify data */
       if ((err = checkmesh(mp)) != NULL)
              mesherror(USER, err);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void skiptree ( void  ) [static]

Definition at line 105 of file readmesh.c.

{
       register int  i;
       
       switch (getc(meshfp)) {
       case OT_EMPTY:
              return;
       case OT_FULL:
              for (i = mgetint(objsize)*objsize; i-- > 0; )
                     if (getc(meshfp) == EOF)
                            mesherror(USER, "truncated mesh octree");
              return;
       case OT_TREE:
              for (i = 0; i < 8; i++)
                     skiptree();
              return;
       case EOF:
              mesherror(USER, "truncated mesh octree");
       default:
              mesherror(USER, "damaged mesh octree");
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char* meshfn [static]

Definition at line 22 of file readmesh.c.

FILE* meshfp [static]

Definition at line 23 of file readmesh.c.

int objsize [static]

Definition at line 24 of file readmesh.c.

const char RCSid[] = "$Id: readmesh.c,v 2.13 2004/09/14 02:53:50 greg Exp $" [static]

Definition at line 2 of file readmesh.c.