--- ray/src/ot/cvmesh.c 2003/05/14 03:08:22 2.5 +++ ray/src/ot/cvmesh.c 2014/01/24 01:26:44 2.12 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: cvmesh.c,v 2.5 2003/05/14 03:08:22 greg Exp $"; +static const char RCSid[] = "$Id: cvmesh.c,v 2.12 2014/01/24 01:26:44 greg Exp $"; #endif /* * Radiance triangle mesh conversion routines @@ -10,6 +10,7 @@ static const char RCSid[] = "$Id: cvmesh.c,v 2.5 2003/ #include "cvmesh.h" #include "otypes.h" #include "face.h" +#include "tmesh.h" /* * We need to divide faces into triangles and record auxiliary information @@ -23,11 +24,11 @@ typedef struct { int fl; /* flags of what we're storing */ OBJECT obj; /* mesh triangle ID */ FVECT vn[3]; /* normals */ - FLOAT vc[3][2]; /* uv coords. */ + RREAL vc[3][2]; /* uv coords. */ } TRIDATA; #define tdsize(fl) ((fl)&MT_UV ? sizeof(TRIDATA) : \ - (fl)&MT_N ? sizeof(TRIDATA)-6*sizeof(FLOAT) : \ + (fl)&MT_N ? sizeof(TRIDATA)-6*sizeof(RREAL) : \ sizeof(int)+sizeof(OBJECT)) #define OMARGIN (10*FTINY) /* margin around global cube */ @@ -36,10 +37,16 @@ MESH *ourmesh = NULL; /* our global mesh data structu FVECT meshbounds[2]; /* mesh bounding box */ +static void add2bounds(FVECT vp, RREAL vc[2]); +static OBJECT cvmeshtri(OBJECT obj); +static OCTREE cvmeshoct(OCTREE ot); + + MESH * -cvinit(nm) /* initialize empty mesh */ -char *nm; +cvinit( /* initialize empty mesh */ + char *nm +) { /* free old mesh, first */ if (ourmesh != NULL) { @@ -68,72 +75,11 @@ nomem: } -int -cvpoly(mo, n, vp, vn, vc) /* convert a polygon to extended triangles */ -OBJECT mo; -int n; -FVECT *vp; -FVECT *vn; -FLOAT (*vc)[2]; -{ - int tcnt = 0; - int flags; - FLOAT *tn[3], *tc[3]; - int *ord; - int i, j; - - if (n < 3) /* degenerate face */ - return(0); - flags = MT_V; - if (vn != NULL) { - tn[0] = vn[0]; tn[1] = vn[1]; tn[2] = vn[2]; - flags |= MT_N; - } else { - tn[0] = tn[1] = tn[2] = NULL; - } - if (vc != NULL) { - tc[0] = vc[0]; tc[1] = vc[1]; tc[2] = vc[2]; - flags |= MT_UV; - } else { - tc[0] = tc[1] = tc[2] = NULL; - } - if (n == 3) /* output single triangle */ - return(cvtri(mo, vp[0], vp[1], vp[2], - tn[0], tn[1], tn[2], - tc[0], tc[1], tc[2])); - - /* decimate polygon (assumes convex) */ - ord = (int *)malloc(n*sizeof(int)); - if (ord == NULL) - error(SYSTEM, "out of memory in cvpoly"); - for (i = n; i--; ) - ord[i] = i; - while (n >= 3) { - if (flags & MT_N) - for (i = 3; i--; ) - tn[i] = vn[ord[i]]; - if (flags & MT_UV) - for (i = 3; i--; ) - tc[i] = vc[ord[i]]; - tcnt += cvtri(mo, vp[ord[0]], vp[ord[1]], vp[ord[2]], - tn[0], tn[1], tn[2], - tc[0], tc[1], tc[2]); - /* remove vertex and rotate */ - n--; - j = ord[0]; - for (i = 0; i < n-1; i++) - ord[i] = ord[i+2]; - ord[i] = j; - } - free((void *)ord); - return(tcnt); -} - - static void -add2bounds(vp, vc) /* add point and uv coordinate to bounds */ -FVECT vp; -FLOAT vc[2]; +add2bounds( /* add point and uv coordinate to bounds */ + FVECT vp, + RREAL vc[2] +) { register int j; @@ -155,11 +101,18 @@ FLOAT vc[2]; int /* create an extended triangle */ -cvtri(mo, vp1, vp2, vp3, vn1, vn2, vn3, vc1, vc2, vc3) -OBJECT mo; -FVECT vp1, vp2, vp3; -FVECT vn1, vn2, vn3; -FLOAT vc1[2], vc2[2], vc3[2]; +cvtri( + OBJECT mo, + FVECT vp1, + FVECT vp2, + FVECT vp3, + FVECT vn1, + FVECT vn2, + FVECT vn3, + RREAL vc1[2], + RREAL vc2[2], + RREAL vc3[2] +) { static OBJECT fobj = OVOID; char buf[32]; @@ -169,9 +122,30 @@ FLOAT vc1[2], vc2[2], vc3[2]; OBJREC *fop; int j; - flags = MT_V; - if (vn1 != NULL && vn2 != NULL && vn3 != NULL) - flags |= MT_N; + flags = MT_V; /* check what we have */ + if (vn1 != NULL && vn2 != NULL && vn3 != NULL) { + RREAL *rp; + switch (flat_tri(vp1, vp2, vp3, vn1, vn2, vn3)) { + case ISBENT: + flags |= MT_N; + /* fall through */ + case ISFLAT: + break; + case RVBENT: + flags |= MT_N; + rp = vn1; vn1 = vn3; vn3 = rp; + /* fall through */ + case RVFLAT: + rp = vp1; vp1 = vp3; vp3 = rp; + rp = vc1; vc1 = vc3; vc3 = rp; + break; + case DEGEN: + error(WARNING, "degenerate triangle"); + return(0); + default: + error(INTERNAL, "bad return from flat_tri()"); + } + } if (vc1 != NULL && vc2 != NULL && vc3 != NULL) flags |= MT_UV; if (fobj == OVOID) { /* create new triangle object */ @@ -181,10 +155,10 @@ FLOAT vc1[2], vc2[2], vc3[2]; fop = objptr(fobj); fop->omod = mo; fop->otype = OBJ_FACE; - sprintf(buf, "t%d", fobj); + sprintf(buf, "t%ld", (long)fobj); fop->oname = savqstr(buf); fop->oargs.nfargs = 9; - fop->oargs.farg = (FLOAT *)malloc(9*sizeof(FLOAT)); + fop->oargs.farg = (RREAL *)malloc(9*sizeof(RREAL)); if (fop->oargs.farg == NULL) goto nomem; } else { /* else reuse failed one */ @@ -240,8 +214,9 @@ nomem: static OBJECT -cvmeshtri(obj) /* add an extended triangle to our mesh */ -OBJECT obj; +cvmeshtri( /* add an extended triangle to our mesh */ + OBJECT obj +) { OBJREC *o = objptr(obj); TRIDATA *ts; @@ -277,7 +252,7 @@ OBJECT obj; void -cvmeshbounds() /* set mesh boundaries */ +cvmeshbounds(void) /* set mesh boundaries */ { int i; @@ -301,9 +276,10 @@ cvmeshbounds() /* set mesh boundaries */ ourmesh->uvlim[1][0] = ourmesh->uvlim[1][1] = 0.; } else { for (i = 0; i < 2; i++) { - double marg; - marg = 1e-6*(ourmesh->uvlim[1][i] - - ourmesh->uvlim[0][i]); + double marg; /* expand past endpoints */ + marg = (2./(1L<<(8*sizeof(uint16)))) * + (ourmesh->uvlim[1][i] - + ourmesh->uvlim[0][i]) + FTINY; ourmesh->uvlim[0][i] -= marg; ourmesh->uvlim[1][i] += marg; } @@ -313,8 +289,9 @@ cvmeshbounds() /* set mesh boundaries */ static OCTREE -cvmeshoct(ot) /* convert triangles in subtree */ -OCTREE ot; +cvmeshoct( /* convert triangles in subtree */ + OCTREE ot +) { int i; @@ -338,7 +315,7 @@ OCTREE ot; MESH * -cvmesh() /* convert mesh and octree leaf nodes */ +cvmesh(void) /* convert mesh and octree leaf nodes */ { if (ourmesh == NULL) return(NULL);