--- ray/src/gen/mkillum2.c 1991/11/08 13:17:47 1.14 +++ ray/src/gen/mkillum2.c 2007/09/13 06:31:21 2.18 @@ -1,46 +1,124 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: mkillum2.c,v 2.18 2007/09/13 06:31:21 greg Exp $"; #endif - /* * Routines to do the actual calculation for mkillum */ -#include "mkillum.h" +#include +#include "mkillum.h" #include "face.h" - #include "cone.h" - #include "random.h" -o_default(ob, il, rt, nm) /* default illum action */ -OBJREC *ob; -struct illum_args *il; -struct rtproc *rt; -char *nm; +static void mkaxes(FVECT u, FVECT v, FVECT n); +static void rounddir(FVECT dv, double alt, double azi); +static void flatdir(FVECT dv, double alt, double azi); + + +static COLORV * distarr = NULL; /* distribution array */ +static int distsiz = 0; + + +static void +newdist( /* allocate & clear distribution array */ + int siz +) { + if (siz == 0) { + if (distsiz > 0) + free((void *)distarr); + distarr = NULL; + distsiz = 0; + return; + } + if (distsiz < siz) { + free((void *)distarr); + distarr = (COLORV *)malloc(sizeof(COLORV)*3*siz); + if (distarr == NULL) + error(SYSTEM, "Out of memory in distalloc"); + distsiz = siz; + } + memset(distarr, '\0', sizeof(COLORV)*3*siz); +} + + +static int +process_ray(RAY *r, int rv) +{ + COLORV *colp; + + if (rv == 0) + return(0); + if (rv < 0) + error(USER, "Ray tracing process died"); + if (r->rno >= distsiz) + error(INTERNAL, "Bad returned index in process_ray"); + colp = &distarr[r->rno * 3]; + addcolor(colp, r->rcol); + return(1); +} + + +static void +raysamp( /* queue a ray sample */ + int ndx, + FVECT org, + FVECT dir +) +{ + RAY myRay; + int rv; + + if ((ndx < 0) | (ndx >= distsiz)) + error(INTERNAL, "Bad index in raysamp"); + VCOPY(myRay.rorg, org); + VCOPY(myRay.rdir, dir); + myRay.rmax = .0; + rayorigin(&myRay, PRIMARY, NULL, NULL); + myRay.rno = ndx; + /* queue ray, check result */ + process_ray(&myRay, ray_pqueue(&myRay)); +} + + +static void +rayclean() /* finish all pending rays */ +{ + RAY myRay; + + while (process_ray(&myRay, ray_presult(&myRay, 0))) + ; +} + + +int /* XXX type conflict with otypes.h */ +my_default( /* default illum action */ + OBJREC *ob, + struct illum_args *il, + char *nm +) +{ sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", nm, ofun[ob->otype].funame, ob->oname); error(WARNING, errmsg); - if (!(il->flags & IL_LIGHT)) - printobj(il->altmat, ob); + printobj(il->altmat, ob); + return(1); } -o_face(ob, il, rt, nm) /* make an illum face */ -OBJREC *ob; -struct illum_args *il; -struct rtproc *rt; -char *nm; +int +my_face( /* make an illum face */ + OBJREC *ob, + struct illum_args *il, + char *nm +) { #define MAXMISS (5*n*il->nsamps) int dim[3]; int n, nalt, nazi, h; - float *distarr; double sp[2], r1, r2; FVECT dn, org, dir; FVECT u, v; @@ -52,8 +130,7 @@ char *nm; fa = getface(ob); if (fa->area == 0.0) { freeface(ob); - o_default(ob, il, rt, nm); - return; + return(o_default(ob, il, nm)); } /* set up sampling */ if (il->sampdens <= 0) @@ -64,10 +141,22 @@ char *nm; nazi = PI*nalt + .5; } n = nalt*nazi; - distarr = (float *)calloc(n, 3*sizeof(float)); - if (distarr == NULL) - error(SYSTEM, "out of memory in o_face"); - mkaxes(u, v, fa->norm); + newdist(n); + /* take first edge longer than sqrt(area) */ + for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { + u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; + u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; + u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2]; + if ((r1 = DOT(u,u)) >= fa->area-FTINY) + break; + } + if (i < fa->nv) { /* got one! -- let's align our axes */ + r2 = 1.0/sqrt(r1); + u[0] *= r2; u[1] *= r2; u[2] *= r2; + fcross(v, fa->norm, u); + } else /* oh well, we'll just have to wing it */ + mkaxes(u, v, fa->norm); + /* now, find limits in (u,v) coordinates */ ur[0] = vr[0] = FHUGE; ur[1] = vr[1] = -FHUGE; for (i = 0; i < fa->nv; i++) { @@ -103,41 +192,40 @@ char *nm; } while (!inface(org, fa) && nmisses++ < MAXMISS); if (nmisses > MAXMISS) { objerror(ob, WARNING, "bad aspect"); - rt->nrays = 0; + rayclean(); freeface(ob); - free((char *)distarr); - o_default(ob, il, rt, nm); - return; + free((void *)distarr); + return(o_default(ob, il, nm)); } for (j = 0; j < 3; j++) org[j] += .001*fa->norm[j]; /* send sample */ - raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); + raysamp(dim[1]*nazi+dim[2], org, dir); } - rayflush(rt); + rayclean(); /* write out the face and its distribution */ if (average(il, distarr, nalt*nazi)) { if (il->sampdens > 0) flatout(il, distarr, nalt, nazi, u, v, fa->norm); illumout(il, ob); - } else if (!(il->flags & IL_LIGHT)) + } else printobj(il->altmat, ob); /* clean up */ freeface(ob); - free((char *)distarr); + return(0); #undef MAXMISS } -o_sphere(ob, il, rt, nm) /* make an illum sphere */ -register OBJREC *ob; -struct illum_args *il; -struct rtproc *rt; -char *nm; +int +my_sphere( /* make an illum sphere */ + register OBJREC *ob, + struct illum_args *il, + char *nm +) { int dim[3]; int n, nalt, nazi; - float *distarr; double sp[4], r1, r2, r3; FVECT org, dir; FVECT u, v; @@ -150,13 +238,11 @@ char *nm; nalt = nazi = 1; else { n = 4.*PI * il->sampdens; - nalt = sqrt(n/PI) + .5; - nazi = PI*nalt + .5; + nalt = sqrt(2./PI*n) + .5; + nazi = PI/2.*nalt + .5; } n = nalt*nazi; - distarr = (float *)calloc(n, 3*sizeof(float)); - if (distarr == NULL) - error(SYSTEM, "out of memory in o_sphere"); + newdist(n); dim[0] = random(); /* sample sphere */ for (dim[1] = 0; dim[1] < nalt; dim[1]++) @@ -181,9 +267,9 @@ char *nm; dir[j] = -dir[j]; } /* send sample */ - raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); + raysamp(dim[1]*nazi+dim[2], org, dir); } - rayflush(rt); + rayclean(); /* write out the sphere and its distribution */ if (average(il, distarr, nalt*nazi)) { if (il->sampdens > 0) @@ -191,22 +277,22 @@ char *nm; else objerror(ob, WARNING, "diffuse distribution"); illumout(il, ob); - } else if (!(il->flags & IL_LIGHT)) + } else printobj(il->altmat, ob); /* clean up */ - free((char *)distarr); + return(1); } -o_ring(ob, il, rt, nm) /* make an illum ring */ -OBJREC *ob; -struct illum_args *il; -struct rtproc *rt; -char *nm; +int +my_ring( /* make an illum ring */ + OBJREC *ob, + struct illum_args *il, + char *nm +) { int dim[3]; int n, nalt, nazi; - float *distarr; double sp[4], r1, r2, r3; FVECT dn, org, dir; FVECT u, v; @@ -223,9 +309,7 @@ char *nm; nazi = PI*nalt + .5; } n = nalt*nazi; - distarr = (float *)calloc(n, 3*sizeof(float)); - if (distarr == NULL) - error(SYSTEM, "out of memory in o_ring"); + newdist(n); mkaxes(u, v, co->ad); dim[0] = random(); /* sample disk */ @@ -247,71 +331,35 @@ char *nm; r1 = r3*cos(r2); r2 = r3*sin(r2); for (j = 0; j < 3; j++) - org[j] = CO_P0(co)[j] + r1*u[j] + r1*v[j] + + org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + .001*co->ad[j]; /* send sample */ - raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); + raysamp(dim[1]*nazi+dim[2], org, dir); } - rayflush(rt); + rayclean(); /* write out the ring and its distribution */ if (average(il, distarr, nalt*nazi)) { if (il->sampdens > 0) flatout(il, distarr, nalt, nazi, u, v, co->ad); illumout(il, ob); - } else if (!(il->flags & IL_LIGHT)) + } else printobj(il->altmat, ob); /* clean up */ freecone(ob); - free((char *)distarr); + return(1); } -raysamp(res, org, dir, rt) /* compute a ray sample */ -float res[3]; -FVECT org, dir; -register struct rtproc *rt; +static void +mkaxes( /* compute u and v to go with n */ + FVECT u, + FVECT v, + FVECT n +) { - register float *fp; - - if (rt->nrays == rt->bsiz) - rayflush(rt); - rt->dest[rt->nrays] = res; - fp = rt->buf + 6*rt->nrays++; - *fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2]; - *fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2]; -} - - -rayflush(rt) /* flush buffered rays */ -register struct rtproc *rt; -{ register int i; - if (rt->nrays <= 0) - return; - bzero(rt->buf+6*rt->nrays, 6*sizeof(float)); - errno = 0; - if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf, - 3*sizeof(float)*rt->nrays, - 6*sizeof(float)*(rt->nrays+1)) < - 3*sizeof(float)*rt->nrays ) - error(SYSTEM, "error reading from rtrace process"); - i = rt->nrays; - while (i--) { - rt->dest[i][0] += rt->buf[3*i]; - rt->dest[i][1] += rt->buf[3*i+1]; - rt->dest[i][2] += rt->buf[3*i+2]; - } - rt->nrays = 0; -} - - -mkaxes(u, v, n) /* compute u and v to go with n */ -FVECT u, v, n; -{ - register int i; - v[0] = v[1] = v[2] = 0.0; for (i = 0; i < 3; i++) if (n[i] < 0.6 && n[i] > -0.6) @@ -323,9 +371,12 @@ FVECT u, v, n; } -rounddir(dv, alt, azi) /* compute uniform spherical direction */ -register FVECT dv; -double alt, azi; +static void +rounddir( /* compute uniform spherical direction */ + register FVECT dv, + double alt, + double azi +) { double d1, d2; @@ -337,9 +388,12 @@ double alt, azi; } -flatdir(dv, alt, azi) /* compute uniform hemispherical direction */ -register FVECT dv; -double alt, azi; +static void +flatdir( /* compute uniform hemispherical direction */ + register FVECT dv, + double alt, + double azi +) { double d1, d2;