--- ray/src/rt/ambcomp.c 1998/06/24 09:35:00 2.7 +++ ray/src/rt/ambcomp.c 2003/07/27 22:12:03 2.12 @@ -1,33 +1,21 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.12 2003/07/27 22:12:03 schorsch Exp $"; #endif - /* * Routines to compute "ambient" values using Monte Carlo + * + * Declarations of external symbols in ambient.h */ +#include "copyright.h" + #include "ray.h" #include "ambient.h" #include "random.h" -typedef struct { - short t, p; /* theta, phi indices */ - COLOR v; /* value sum */ - float r; /* 1/distance sum */ - float k; /* variance for this division */ - int n; /* number of subsamples */ -} AMBSAMP; /* ambient sample division */ -typedef struct { - FVECT ux, uy, uz; /* x, y and z axis directions */ - short nt, np; /* number of theta and phi directions */ -} AMBHEMI; /* ambient sample hemisphere */ - - static int ambcmp(d1, d2) /* decreasing order */ AMBSAMP *d1, *d2; @@ -46,12 +34,13 @@ AMBSAMP *d1, *d2; { register int c; - if (c = d1->t - d2->t) + if ( (c = d1->t - d2->t) ) return(c); return(d1->p - d2->p); } +int divsample(dp, h, r) /* sample a division */ register AMBSAMP *dp; AMBHEMI *h; @@ -73,8 +62,8 @@ RAY *r; multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); zd = sqrt((dp->t + spt[0])/h->nt); phi = 2.0*PI * (dp->p + spt[1])/h->np; - xd = cos(phi) * zd; - yd = sin(phi) * zd; + xd = tcos(phi) * zd; + yd = tsin(phi) * zd; zd = sqrt(1.0 - zd*zd); for (i = 0; i < 3; i++) ar.rdir[i] = xd*h->ux[i] + @@ -84,9 +73,9 @@ RAY *r; rayvalue(&ar); ndims--; addcolor(dp->v, ar.rcol); - /* be conservative and use rot */ - if (ar.rot > FTINY && ar.rot < FHUGE) - dp->r += 1.0/ar.rot; + /* use rt to improve gradient calc */ + if (ar.rt > FTINY && ar.rt < FHUGE) + dp->r += 1.0/ar.rt; /* (re)initialize error */ if (dp->n++) { b2 = bright(dp->v)/dp->n - bright(ar.rcol); @@ -153,17 +142,17 @@ FVECT pg, dg; qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ /* super-sample */ for (i = ns; i > 0; i--) { - copystruct(&dnew, div); + dnew = *div; if (divsample(&dnew, &hemi, r) < 0) goto oopsy; /* reinsert */ dp = div; j = ndivs < i ? ndivs : i; while (--j > 0 && dnew.k < dp[1].k) { - copystruct(dp, dp+1); + *dp = *(dp+1); dp++; } - copystruct(dp, &dnew); + *dp = dnew; } if (pg != NULL || dg != NULL) /* restore order */ qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); @@ -202,7 +191,7 @@ FVECT pg, dg; for (i = 0; i < 3; i++) dg[i] = 0.0; } - free((char *)div); + free((void *)div); } b = 1.0/ndivs; scalecolor(acol, b); @@ -228,11 +217,12 @@ FVECT pg, dg; return(arad); oopsy: if (div != NULL) - free((char *)div); + free((void *)div); return(0.0); } +void inithemi(hp, r, wt) /* initialize sampling hemisphere */ register AMBHEMI *hp; RAY *r; @@ -261,6 +251,7 @@ double wt; } +void comperrs(da, hp) /* compute initial error estimates */ AMBSAMP *da; /* assumes standard ordering */ register AMBHEMI *hp; @@ -311,6 +302,7 @@ register AMBHEMI *hp; } +void posgradient(gv, da, hp) /* compute position gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */ @@ -358,7 +350,7 @@ register AMBHEMI *hp; } mag0 *= 2.0*PI / hp->np; phi = 2.0*PI * (double)j/hp->np; - cosp = cos(phi); sinp = sin(phi); + cosp = tcos(phi); sinp = tsin(phi); xd += mag0*cosp - mag1*sinp; yd += mag0*sinp + mag1*cosp; } @@ -367,6 +359,7 @@ register AMBHEMI *hp; } +void dirgradient(gv, da, hp) /* compute direction gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */ @@ -392,8 +385,8 @@ register AMBHEMI *hp; dp += hp->np; } phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; - xd += mag * cos(phi); - yd += mag * sin(phi); + xd += mag * tcos(phi); + yd += mag * tsin(phi); } for (i = 0; i < 3; i++) gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);