--- ray/src/common/mesh.c 2003/04/23 00:52:33 2.6 +++ ray/src/common/mesh.c 2012/11/06 01:56:37 2.25 @@ -1,11 +1,16 @@ #ifndef lint -static const char RCSid[] = "$Id: mesh.c,v 2.6 2003/04/23 00:52:33 greg Exp $"; +static const char RCSid[] = "$Id: mesh.c,v 2.25 2012/11/06 01:56:37 greg Exp $"; #endif /* * Mesh support routines */ -#include "standard.h" +#include + +#include "rtio.h" +#include "rtmath.h" +#include "rterror.h" +#include "paths.h" #include "octree.h" #include "object.h" #include "otypes.h" @@ -14,9 +19,9 @@ static const char RCSid[] = "$Id: mesh.c,v 2.6 2003/04 /* An encoded mesh vertex */ typedef struct { int fl; - uint4 xyz[3]; - int4 norm; - uint4 uv[2]; + uint32 xyz[3]; + int32 norm; + uint32 uv[2]; } MCVERT; #define MPATCHBLKSIZ 128 /* patch allocation block size */ @@ -27,9 +32,9 @@ static MESH *mlist = NULL; /* list of loaded meshes * static unsigned long -cvhash(cvp) /* hash an encoded vertex */ -MCVERT *cvp; +cvhash(const char *p) /* hash an encoded vertex */ { + const MCVERT *cvp = (const MCVERT *)p; unsigned long hval; if (!(cvp->fl & MT_V)) @@ -44,9 +49,9 @@ MCVERT *cvp; static int -cvcmp(v1, v2) /* compare encoded vertices */ -register MCVERT *v1, *v2; +cvcmp(const char *vv1, const char *vv2) /* compare encoded vertices */ { + const MCVERT *v1 = (const MCVERT *)vv1, *v2 = (const MCVERT *)vv2; if (v1->fl != v2->fl) return(1); if (v1->xyz[0] != v2->xyz[0]) @@ -68,23 +73,21 @@ register MCVERT *v1, *v2; MESH * -getmesh(mname, flags) /* get mesh data */ -char *mname; -int flags; +getmesh( /* get new mesh data reference */ + char *mname, + int flags +) { char *pathname; - register MESH *ms; + MESH *ms; flags &= IO_LEGAL; for (ms = mlist; ms != NULL; ms = ms->next) if (!strcmp(mname, ms->name)) { - if ((ms->ldflags & flags) == flags) { - ms->nref++; - return(ms); /* loaded */ - } - break; /* load the rest */ + ms->nref++; /* increase reference count */ + break; } - if (ms == NULL) { + if (ms == NULL) { /* load first time */ ms = (MESH *)calloc(1, sizeof(MESH)); if (ms == NULL) error(SYSTEM, "out of memory in getmesh"); @@ -94,7 +97,7 @@ int flags; ms->next = mlist; mlist = ms; } - if ((pathname = getpath(mname, getlibpath(), R_OK)) == NULL) { + if ((pathname = getpath(mname, getrlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find mesh file \"%s\"", mname); error(USER, errmsg); } @@ -106,11 +109,12 @@ int flags; MESHINST * -getmeshinst(o, flags) /* create mesh instance */ -OBJREC *o; -int flags; +getmeshinst( /* create mesh instance */ + OBJREC *o, + int flags +) { - register MESHINST *ins; + MESHINST *ins; flags &= IO_LEGAL; if ((ins = (MESHINST *)o->os) == NULL) { @@ -128,19 +132,69 @@ int flags; ins->msh = NULL; o->os = (char *)ins; } - if (ins->msh == NULL || (ins->msh->ldflags & flags) != flags) + if (ins->msh == NULL) ins->msh = getmesh(o->oargs.sarg[0], flags); + else if ((flags &= ~ins->msh->ldflags)) + readmesh(ins->msh, + getpath(o->oargs.sarg[0], getrlibpath(), R_OK), + flags); return(ins); } int -getmeshtrivid(tvid, mo, mp, ti) /* get triangle vertex ID's */ -int4 tvid[3]; -OBJECT *mo; -MESH *mp; -OBJECT ti; +nextmeshtri( /* get next triangle ID */ + OBJECT *tip, + MESH *mp +) { + int advance = 1; + int pn; + MESHPATCH *pp; + + if (*tip == OVOID) { /* check for first index */ + *tip = 0; + advance = 0; + } + pn = *tip >> 10; + while (pn < mp->npatches) { + pp = &mp->patch[pn]; + if (!(*tip & 0x200)) { /* local triangle? */ + if ((*tip & 0x1ff) < pp->ntris - advance) { + *tip += advance; + return(1); + } + *tip &= ~0x1ff; /* move on to single-joiners */ + *tip |= 0x200; + advance = 0; + } + if (!(*tip & 0x100)) { /* single joiner? */ + if ((*tip & 0xff) < pp->nj1tris - advance) { + *tip += advance; + return(1); + } + *tip &= ~0xff; /* move on to double-joiners */ + *tip |= 0x100; + advance = 0; + } + if ((*tip & 0xff) < pp->nj2tris - advance) { + *tip += advance; + return(1); + } + *tip = ++pn << 10; /* first in next patch */ + advance = 0; + } + return(0); /* out of patches */ +} + +int +getmeshtrivid( /* get triangle vertex ID's */ + int32 tvid[3], + OBJECT *mo, + MESH *mp, + OBJECT ti +) +{ int pn = ti >> 10; MESHPATCH *pp; @@ -196,16 +250,17 @@ OBJECT ti; int -getmeshvert(vp, mp, vid, what) /* get triangle vertex from ID */ -MESHVERT *vp; -MESH *mp; -int4 vid; -int what; +getmeshvert( /* get triangle vertex from ID */ + MESHVERT *vp, + MESH *mp, + int32 vid, + int what +) { int pn = vid >> 8; MESHPATCH *pp; double vres; - register int i; + int i; vp->fl = 0; if (pn >= mp->npatches) @@ -240,14 +295,15 @@ int what; OBJREC * -getmeshpseudo(mp, mo) /* get mesh pseudo object for material */ -MESH *mp; -OBJECT mo; +getmeshpseudo( /* get mesh pseudo object for material */ + MESH *mp, + OBJECT mo +) { if (mo < mp->mat0 || mo >= mp->mat0 + mp->nmats) error(INTERNAL, "modifier out of range in getmeshpseudo"); if (mp->pseudo == NULL) { - register int i; + int i; mp->pseudo = (OBJREC *)calloc(mp->nmats, sizeof(OBJREC)); if (mp->pseudo == NULL) error(SYSTEM, "out of memory in getmeshpseudo"); @@ -262,14 +318,15 @@ OBJECT mo; int -getmeshtri(tv, mo, mp, ti, wha) /* get triangle vertices */ -MESHVERT tv[3]; -OBJECT *mo; -MESH *mp; -OBJECT ti; -int wha; +getmeshtri( /* get triangle vertices */ + MESHVERT tv[3], + OBJECT *mo, + MESH *mp, + OBJECT ti, + int wha +) { - int4 tvid[3]; + int32 tvid[3]; if (!getmeshtrivid(tvid, mo, mp, ti)) return(0); @@ -282,14 +339,15 @@ int wha; } -int4 -addmeshvert(mp, vp) /* find/add a mesh vertex */ -register MESH *mp; -MESHVERT *vp; +int32 +addmeshvert( /* find/add a mesh vertex */ + MESH *mp, + MESHVERT *vp +) { LUENT *lvp; MCVERT cv; - register int i; + int i; if (!(vp->fl & MT_V)) return(-1); @@ -299,11 +357,11 @@ MESHVERT *vp; return(-1); if (vp->v[i] >= mp->mcube.cuorg[i] + mp->mcube.cusize) return(-1); - cv.xyz[i] = (uint4)(4294967296. * + cv.xyz[i] = (uint32)(4294967296. * (vp->v[i] - mp->mcube.cuorg[i]) / mp->mcube.cusize); } - if (vp->fl & MT_N) + if (vp->fl & MT_N) /* assumes normalized! */ cv.norm = encodedir(vp->n); if (vp->fl & MT_UV) for (i = 0; i < 2; i++) { @@ -311,7 +369,7 @@ MESHVERT *vp; return(-1); if (vp->uv[i] >= mp->uvlim[1][i]) return(-1); - cv.uv[i] = (uint4)(4294967296. * + cv.uv[i] = (uint32)(4294967296. * (vp->uv[i] - mp->uvlim[0][i]) / (mp->uvlim[1][i] - mp->uvlim[0][i])); } @@ -328,11 +386,11 @@ MESHVERT *vp; if (lvp == NULL) goto nomem; if (lvp->key == NULL) { - lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int4)); - bcopy((void *)&cv, (void *)lvp->key, sizeof(MCVERT)); + lvp->key = (char *)malloc(sizeof(MCVERT)+sizeof(int32)); + memcpy((void *)lvp->key, (void *)&cv, sizeof(MCVERT)); } if (lvp->data == NULL) { /* new vertex */ - register MESHPATCH *pp; + MESHPATCH *pp; if (mp->npatches <= 0) { mp->patch = (MESHPATCH *)calloc(MPATCHBLKSIZ, sizeof(MESHPATCH)); @@ -345,7 +403,7 @@ MESHVERT *vp; (void *)mp->patch, (mp->npatches + MPATCHBLKSIZ)* sizeof(MESHPATCH)); - bzero((void *)(mp->patch + mp->npatches), + memset((void *)(mp->patch + mp->npatches), '\0', MPATCHBLKSIZ*sizeof(MESHPATCH)); } if (mp->npatches++ >= 1L<<22) @@ -353,7 +411,7 @@ MESHVERT *vp; } pp = &mp->patch[mp->npatches-1]; if (pp->xyz == NULL) { - pp->xyz = (uint4 (*)[3])calloc(256, 3*sizeof(int4)); + pp->xyz = (uint32 (*)[3])calloc(256, 3*sizeof(int32)); if (pp->xyz == NULL) goto nomem; } @@ -361,7 +419,7 @@ MESHVERT *vp; pp->xyz[pp->nverts][i] = cv.xyz[i]; if (cv.fl & MT_N) { if (pp->norm == NULL) { - pp->norm = (int4 *)calloc(256, sizeof(int4)); + pp->norm = (int32 *)calloc(256, sizeof(int32)); if (pp->norm == NULL) goto nomem; } @@ -369,8 +427,8 @@ MESHVERT *vp; } if (cv.fl & MT_UV) { if (pp->uv == NULL) { - pp->uv = (uint4 (*)[2])calloc(256, - 2*sizeof(uint4)); + pp->uv = (uint32 (*)[2])calloc(256, + 2*sizeof(uint32)); if (pp->uv == NULL) goto nomem; } @@ -379,9 +437,9 @@ MESHVERT *vp; } pp->nverts++; lvp->data = lvp->key + sizeof(MCVERT); - *(int4 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); + *(int32 *)lvp->data = (mp->npatches-1) << 8 | (pp->nverts-1); } - return(*(int4 *)lvp->data); + return(*(int32 *)lvp->data); nomem: error(SYSTEM, "out of memory in addmeshvert"); return(-1); @@ -389,14 +447,15 @@ nomem: OBJECT -addmeshtri(mp, tv, mo) /* add a new mesh triangle */ -MESH *mp; -MESHVERT tv[3]; -OBJECT mo; +addmeshtri( /* add a new mesh triangle */ + MESH *mp, + MESHVERT tv[3], + OBJECT mo +) { - int4 vid[3], t; + int32 vid[3], t; int pn[3], i; - register MESHPATCH *pp; + MESHPATCH *pp; if (!(tv[0].fl & tv[1].fl & tv[2].fl & MT_V)) return(OVOID); @@ -407,11 +466,12 @@ OBJECT mo; pn[i] = vid[i] >> 8; } /* normalize material index */ - if (mo != OVOID) + if (mo != OVOID) { if ((mo -= mp->mat0) >= mp->nmats) mp->nmats = mo+1; else if (mo < 0) error(INTERNAL, "modifier range error in addmeshtri"); + } /* assign triangle */ if (pn[0] == pn[1] && pn[1] == pn[2]) { /* local case */ pp = &mp->patch[pn[0]]; @@ -428,8 +488,8 @@ OBJECT mo; if (pp->ntris == 0) pp->solemat = mo; else if (pp->trimat == NULL && mo != pp->solemat) { - pp->trimat = (int2 *)malloc( - 512*sizeof(int2)); + pp->trimat = (int16 *)malloc( + 512*sizeof(int16)); if (pp->trimat == NULL) goto nomem; for (i = pp->ntris; i--; ) @@ -485,12 +545,11 @@ nomem: char * -checkmesh(mp) /* validate mesh data */ -register MESH *mp; +checkmesh(MESH *mp) /* validate mesh data */ { static char embuf[128]; int nouvbounds = 1; - register int i; + int i; /* basic checks */ if (mp == NULL) return("NULL mesh pointer"); @@ -530,7 +589,7 @@ register MESH *mp; if (mp->npatches <= 0) error(WARNING, "no patches in mesh"); for (i = 0; i < mp->npatches; i++) { - register MESHPATCH *pp = &mp->patch[i]; + MESHPATCH *pp = &mp->patch[i]; if (pp->nverts <= 0) error(WARNING, "no vertices in patch"); else { @@ -539,8 +598,6 @@ register MESH *mp; if (nouvbounds && pp->uv != NULL) return("unreferenced uv coordinates"); } - if (pp->ntris + pp->nj1tris + pp->nj2tris <= 0) - error(WARNING, "no triangles in patch"); if (pp->ntris > 0 && pp->tri == NULL) return("missing patch triangle list"); if (pp->nj1tris > 0 && pp->j1tri == NULL) @@ -554,9 +611,12 @@ register MESH *mp; static void -tallyoctree(ot, ecp, lcp, ocp) /* tally octree size */ -OCTREE ot; -int *ecp, *lcp, *ocp; +tallyoctree( /* tally octree size */ + OCTREE ot, + int *ecp, + int *lcp, + int *ocp +) { int i; @@ -577,9 +637,10 @@ int *ecp, *lcp, *ocp; void -printmeshstats(ms, fp) /* print out mesh statistics */ -MESH *ms; -FILE *fp; +printmeshstats( /* print out mesh statistics */ + MESH *ms, + FILE *fp +) { int lfcnt=0, lecnt=0, locnt=0; int vcnt=0, ncnt=0, uvcnt=0; @@ -589,7 +650,7 @@ FILE *fp; tallyoctree(ms->mcube.cutree, &lecnt, &lfcnt, &locnt); for (i = 0; i < ms->npatches; i++) { - register MESHPATCH *pp = &ms->patch[i]; + MESHPATCH *pp = &ms->patch[i]; vcnt += pp->nverts; if (pp->norm != NULL) { for (j = pp->nverts; j--; ) @@ -608,12 +669,12 @@ FILE *fp; t2cnt += pp->nj2tris; } fprintf(fp, "Mesh statistics:\n"); - fprintf(fp, "\t%d materials\n", ms->nmats); + fprintf(fp, "\t%ld materials\n", (long)ms->nmats); fprintf(fp, "\t%d patches (%.2f MBytes)\n", ms->npatches, (ms->npatches*sizeof(MESHPATCH) + - vcnt*3*sizeof(uint4) + - nscnt*sizeof(int4) + - uvscnt*2*sizeof(uint4) + + vcnt*3*sizeof(uint32) + + nscnt*sizeof(int32) + + uvscnt*2*sizeof(uint32) + tcnt*sizeof(struct PTri) + t1cnt*sizeof(struct PJoin1) + t2cnt*sizeof(struct PJoin2))/(1024.*1024.)); @@ -631,8 +692,7 @@ FILE *fp; void -freemesh(ms) /* free mesh data */ -register MESH *ms; +freemesh(MESH *ms) /* free mesh data */ { MESH mhead; MESH *msp; @@ -660,7 +720,7 @@ register MESH *ms; octfree(ms->mcube.cutree); lu_done(&ms->lut); if (ms->npatches > 0) { - register MESHPATCH *pp = ms->patch + ms->npatches; + MESHPATCH *pp = ms->patch + ms->npatches; while (pp-- > ms->patch) { if (pp->j2tri != NULL) free((void *)pp->j2tri); @@ -684,8 +744,7 @@ register MESH *ms; void -freemeshinst(o) /* free mesh instance */ -OBJREC *o; +freemeshinst(OBJREC *o) /* free mesh instance */ { if (o->os == NULL) return;