--- ray/src/gen/mkillum2.c 1991/08/22 12:12:23 1.11 +++ ray/src/gen/mkillum2.c 2003/06/30 14:59:11 2.11 @@ -1,19 +1,15 @@ -/* 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.11 2003/06/30 14:59:11 schorsch 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" @@ -26,8 +22,7 @@ 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); } @@ -67,7 +62,21 @@ char *nm; distarr = (float *)calloc(n, 3*sizeof(float)); if (distarr == NULL) error(SYSTEM, "out of memory in o_face"); - mkaxes(u, v, fa->norm); + /* 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++) { @@ -88,7 +97,7 @@ char *nm; h = ilhash(dim, 3) + i; multisamp(sp, 2, urand(h)); r1 = (dim[1] + sp[0])/nalt; - r2 = (dim[2] + sp[1])/nazi; + r2 = (dim[2] + sp[1] - .5)/nazi; flatdir(dn, r1, r2); for (j = 0; j < 3; j++) dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; @@ -105,7 +114,7 @@ char *nm; objerror(ob, WARNING, "bad aspect"); rt->nrays = 0; freeface(ob); - free((char *)distarr); + free((void *)distarr); o_default(ob, il, rt, nm); return; } @@ -116,13 +125,15 @@ char *nm; } rayflush(rt); /* write out the face and its distribution */ - average(il, distarr, nalt*nazi); - if (il->sampdens > 0) - flatout(il, distarr, nalt, nazi, u, v, fa->norm); - illumout(il, ob); + if (average(il, distarr, nalt*nazi)) { + if (il->sampdens > 0) + flatout(il, distarr, nalt, nazi, u, v, fa->norm); + illumout(il, ob); + } else + printobj(il->altmat, ob); /* clean up */ freeface(ob); - free((char *)distarr); + free((void *)distarr); #undef MAXMISS } @@ -148,8 +159,8 @@ 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)); @@ -164,7 +175,7 @@ char *nm; multisamp(sp, 4, urand(ilhash(dim,3)+i)); /* random direction */ r1 = (dim[1] + sp[0])/nalt; - r2 = (dim[2] + sp[1])/nazi; + r2 = (dim[2] + sp[1] - .5)/nazi; rounddir(dir, r1, r2); /* random location */ mkaxes(u, v, dir); /* yuck! */ @@ -183,14 +194,16 @@ char *nm; } rayflush(rt); /* write out the sphere and its distribution */ - average(il, distarr, nalt*nazi); - if (il->sampdens > 0) - roundout(il, distarr, nalt, nazi); - else - objerror(ob, WARNING, "diffuse distribution"); - illumout(il, ob); + if (average(il, distarr, nalt*nazi)) { + if (il->sampdens > 0) + roundout(il, distarr, nalt, nazi); + else + objerror(ob, WARNING, "diffuse distribution"); + illumout(il, ob); + } else + printobj(il->altmat, ob); /* clean up */ - free((char *)distarr); + free((void *)distarr); } @@ -232,7 +245,7 @@ char *nm; multisamp(sp, 4, urand(ilhash(dim,3)+i)); /* random direction */ r1 = (dim[1] + sp[0])/nalt; - r2 = (dim[2] + sp[1])/nalt; + r2 = (dim[2] + sp[1] - .5)/nazi; flatdir(dn, r1, r2); for (j = 0; j < 3; j++) dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; @@ -243,7 +256,7 @@ 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 */ @@ -251,13 +264,15 @@ char *nm; } rayflush(rt); /* write out the ring and its distribution */ - average(il, distarr, nalt*nazi); - if (il->sampdens > 0) - flatout(il, distarr, nalt, nazi, u, v, co->ad); - illumout(il, ob); + if (average(il, distarr, nalt*nazi)) { + if (il->sampdens > 0) + flatout(il, distarr, nalt, nazi, u, v, co->ad); + illumout(il, ob); + } else + printobj(il->altmat, ob); /* clean up */ freecone(ob); - free((char *)distarr); + free((void *)distarr); } @@ -284,11 +299,12 @@ register struct rtproc *rt; if (rt->nrays <= 0) return; - bzero(rt->buf+6*rt->nrays, 6*sizeof(float)); - if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf, - 3*sizeof(float)*rt->nrays, + 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 ) + 3*sizeof(float)*(rt->nrays+1) ) error(SYSTEM, "error reading from rtrace process"); i = rt->nrays; while (i--) {