--- ray/src/gen/mkillum2.c 2003/11/16 10:29:38 2.12 +++ ray/src/gen/mkillum2.c 2007/09/13 06:31:21 2.18 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: mkillum2.c,v 2.12 2003/11/16 10:29:38 schorsch Exp $"; +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 @@ -12,22 +12,92 @@ static const char RCSid[] = "$Id: mkillum2.c,v 2.12 20 #include "cone.h" #include "random.h" -//void o_default(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); -void o_face(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); -void o_sphere(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); -void o_ring(OBJREC *ob, struct illum_args *il, struct rtproc *rt, char *nm); -void raysamp(float res[3], FVECT org, FVECT dir, struct rtproc *rt); -void rayflush(struct rtproc *rt); -void mkaxes(FVECT u, FVECT v, FVECT n); -void rounddir(FVECT dv, double alt, double azi); -void flatdir(FVECT dv, double alt, double azi); +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 */ -o_default( /* default illum action */ +my_default( /* default illum action */ OBJREC *ob, struct illum_args *il, - struct rtproc *rt, char *nm ) { @@ -35,21 +105,20 @@ o_default( /* default illum action */ nm, ofun[ob->otype].funame, ob->oname); error(WARNING, errmsg); printobj(il->altmat, ob); + return(1); } -void -o_face( /* make an illum face */ +int +my_face( /* make an illum face */ OBJREC *ob, struct illum_args *il, - struct rtproc *rt, 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; @@ -61,8 +130,7 @@ o_face( /* make an illum face */ 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) @@ -73,9 +141,7 @@ o_face( /* make an illum face */ 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"); + 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]; @@ -126,18 +192,17 @@ o_face( /* make an illum face */ } while (!inface(org, fa) && nmisses++ < MAXMISS); if (nmisses > MAXMISS) { objerror(ob, WARNING, "bad aspect"); - rt->nrays = 0; + rayclean(); freeface(ob); free((void *)distarr); - o_default(ob, il, rt, nm); - return; + 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) @@ -147,22 +212,20 @@ o_face( /* make an illum face */ printobj(il->altmat, ob); /* clean up */ freeface(ob); - free((void *)distarr); + return(0); #undef MAXMISS } -void -o_sphere( /* make an illum sphere */ +int +my_sphere( /* make an illum sphere */ register OBJREC *ob, struct illum_args *il, - struct rtproc *rt, char *nm ) { int dim[3]; int n, nalt, nazi; - float *distarr; double sp[4], r1, r2, r3; FVECT org, dir; FVECT u, v; @@ -179,9 +242,7 @@ o_sphere( /* make an illum sphere */ 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]++) @@ -206,9 +267,9 @@ o_sphere( /* make an illum sphere */ 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) @@ -219,21 +280,19 @@ o_sphere( /* make an illum sphere */ } else printobj(il->altmat, ob); /* clean up */ - free((void *)distarr); + return(1); } -void -o_ring( /* make an illum ring */ +int +my_ring( /* make an illum ring */ OBJREC *ob, struct illum_args *il, - struct rtproc *rt, char *nm ) { int dim[3]; int n, nalt, nazi; - float *distarr; double sp[4], r1, r2, r3; FVECT dn, org, dir; FVECT u, v; @@ -250,9 +309,7 @@ o_ring( /* make an illum ring */ 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 */ @@ -278,9 +335,9 @@ o_ring( /* make an illum ring */ .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) @@ -290,56 +347,11 @@ o_ring( /* make an illum ring */ printobj(il->altmat, ob); /* clean up */ freecone(ob); - free((void *)distarr); + return(1); } -void -raysamp( /* compute a ray sample */ - float res[3], - FVECT org, - FVECT dir, - register struct rtproc *rt -) -{ - 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]; -} - - -void -rayflush( /* flush buffered rays */ - register struct rtproc *rt -) -{ - register int i; - - if (rt->nrays <= 0) - return; - memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float)); - errno = 0; - if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf, - 3*sizeof(float)*(rt->nrays+1), - 6*sizeof(float)*(rt->nrays+1)) < - 3*sizeof(float)*(rt->nrays+1) ) - 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; -} - - -void +static void mkaxes( /* compute u and v to go with n */ FVECT u, FVECT v, @@ -359,7 +371,7 @@ mkaxes( /* compute u and v to go with n */ } -void +static void rounddir( /* compute uniform spherical direction */ register FVECT dv, double alt, @@ -376,7 +388,7 @@ rounddir( /* compute uniform spherical direction */ } -void +static void flatdir( /* compute uniform hemispherical direction */ register FVECT dv, double alt,