| 16 |
|
#include "random.h" |
| 17 |
|
|
| 18 |
|
|
| 19 |
< |
static int |
| 20 |
< |
ambcmp(d1, d2) /* decreasing order */ |
| 21 |
< |
AMBSAMP *d1, *d2; |
| 19 |
> |
void |
| 20 |
> |
inithemi( /* initialize sampling hemisphere */ |
| 21 |
> |
register AMBHEMI *hp, |
| 22 |
> |
COLOR ac, |
| 23 |
> |
RAY *r, |
| 24 |
> |
double wt |
| 25 |
> |
) |
| 26 |
|
{ |
| 27 |
< |
if (d1->k < d2->k) |
| 28 |
< |
return(1); |
| 29 |
< |
if (d1->k > d2->k) |
| 30 |
< |
return(-1); |
| 31 |
< |
return(0); |
| 27 |
> |
double d; |
| 28 |
> |
register int i; |
| 29 |
> |
/* set number of divisions */ |
| 30 |
> |
if (ambacc <= FTINY && |
| 31 |
> |
wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) |
| 32 |
> |
wt = d; /* avoid ray termination */ |
| 33 |
> |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
| 34 |
> |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
| 35 |
> |
if (hp->nt < i) |
| 36 |
> |
hp->nt = i; |
| 37 |
> |
hp->np = PI * hp->nt + 0.5; |
| 38 |
> |
/* set number of super-samples */ |
| 39 |
> |
hp->ns = ambssamp * wt + 0.5; |
| 40 |
> |
/* assign coefficient */ |
| 41 |
> |
copycolor(hp->acoef, ac); |
| 42 |
> |
d = 1.0/(hp->nt*hp->np); |
| 43 |
> |
scalecolor(hp->acoef, d); |
| 44 |
> |
/* make axes */ |
| 45 |
> |
VCOPY(hp->uz, r->ron); |
| 46 |
> |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
| 47 |
> |
for (i = 0; i < 3; i++) |
| 48 |
> |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
| 49 |
> |
break; |
| 50 |
> |
if (i >= 3) |
| 51 |
> |
error(CONSISTENCY, "bad ray direction in inithemi"); |
| 52 |
> |
hp->uy[i] = 1.0; |
| 53 |
> |
fcross(hp->ux, hp->uy, hp->uz); |
| 54 |
> |
normalize(hp->ux); |
| 55 |
> |
fcross(hp->uy, hp->uz, hp->ux); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 31 |
– |
static int |
| 32 |
– |
ambnorm(d1, d2) /* standard order */ |
| 33 |
– |
AMBSAMP *d1, *d2; |
| 34 |
– |
{ |
| 35 |
– |
register int c; |
| 36 |
– |
|
| 37 |
– |
if ( (c = d1->t - d2->t) ) |
| 38 |
– |
return(c); |
| 39 |
– |
return(d1->p - d2->p); |
| 40 |
– |
} |
| 41 |
– |
|
| 42 |
– |
|
| 59 |
|
int |
| 60 |
< |
divsample(dp, h, r) /* sample a division */ |
| 61 |
< |
register AMBSAMP *dp; |
| 62 |
< |
AMBHEMI *h; |
| 63 |
< |
RAY *r; |
| 60 |
> |
divsample( /* sample a division */ |
| 61 |
> |
register AMBSAMP *dp, |
| 62 |
> |
AMBHEMI *h, |
| 63 |
> |
RAY *r |
| 64 |
> |
) |
| 65 |
|
{ |
| 66 |
|
RAY ar; |
| 67 |
|
int hlist[3]; |
| 70 |
|
double b2; |
| 71 |
|
double phi; |
| 72 |
|
register int i; |
| 73 |
< |
|
| 74 |
< |
if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) |
| 73 |
> |
/* ambient coefficient for weight */ |
| 74 |
> |
if (ambacc > FTINY) |
| 75 |
> |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
| 76 |
> |
else |
| 77 |
> |
copycolor(ar.rcoef, h->acoef); |
| 78 |
> |
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
| 79 |
|
return(-1); |
| 80 |
+ |
if (ambacc > FTINY) { |
| 81 |
+ |
multcolor(ar.rcoef, h->acoef); |
| 82 |
+ |
scalecolor(ar.rcoef, 1./AVGREFL); |
| 83 |
+ |
} |
| 84 |
|
hlist[0] = r->rno; |
| 85 |
|
hlist[1] = dp->t; |
| 86 |
|
hlist[2] = dp->p; |
| 94 |
|
ar.rdir[i] = xd*h->ux[i] + |
| 95 |
|
yd*h->uy[i] + |
| 96 |
|
zd*h->uz[i]; |
| 97 |
+ |
checknorm(ar.rdir); |
| 98 |
|
dimlist[ndims++] = dp->t*h->np + dp->p + 90171; |
| 99 |
|
rayvalue(&ar); |
| 100 |
|
ndims--; |
| 101 |
+ |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
| 102 |
|
addcolor(dp->v, ar.rcol); |
| 103 |
|
/* use rt to improve gradient calc */ |
| 104 |
|
if (ar.rt > FTINY && ar.rt < FHUGE) |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
|
| 117 |
+ |
static int |
| 118 |
+ |
ambcmp( /* decreasing order */ |
| 119 |
+ |
const void *p1, |
| 120 |
+ |
const void *p2 |
| 121 |
+ |
) |
| 122 |
+ |
{ |
| 123 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
| 124 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
| 125 |
+ |
|
| 126 |
+ |
if (d1->k < d2->k) |
| 127 |
+ |
return(1); |
| 128 |
+ |
if (d1->k > d2->k) |
| 129 |
+ |
return(-1); |
| 130 |
+ |
return(0); |
| 131 |
+ |
} |
| 132 |
+ |
|
| 133 |
+ |
|
| 134 |
+ |
static int |
| 135 |
+ |
ambnorm( /* standard order */ |
| 136 |
+ |
const void *p1, |
| 137 |
+ |
const void *p2 |
| 138 |
+ |
) |
| 139 |
+ |
{ |
| 140 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
| 141 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
| 142 |
+ |
register int c; |
| 143 |
+ |
|
| 144 |
+ |
if ( (c = d1->t - d2->t) ) |
| 145 |
+ |
return(c); |
| 146 |
+ |
return(d1->p - d2->p); |
| 147 |
+ |
} |
| 148 |
+ |
|
| 149 |
+ |
|
| 150 |
|
double |
| 151 |
< |
doambient(acol, r, wt, pg, dg) /* compute ambient component */ |
| 152 |
< |
COLOR acol; |
| 153 |
< |
RAY *r; |
| 154 |
< |
double wt; |
| 155 |
< |
FVECT pg, dg; |
| 151 |
> |
doambient( /* compute ambient component */ |
| 152 |
> |
COLOR acol, |
| 153 |
> |
RAY *r, |
| 154 |
> |
double wt, |
| 155 |
> |
FVECT pg, |
| 156 |
> |
FVECT dg |
| 157 |
> |
) |
| 158 |
|
{ |
| 159 |
|
double b, d; |
| 160 |
|
AMBHEMI hemi; |
| 162 |
|
AMBSAMP dnew; |
| 163 |
|
register AMBSAMP *dp; |
| 164 |
|
double arad; |
| 165 |
< |
int ndivs, ns; |
| 165 |
> |
int divcnt; |
| 166 |
|
register int i, j; |
| 105 |
– |
/* initialize color */ |
| 106 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
| 167 |
|
/* initialize hemisphere */ |
| 168 |
< |
inithemi(&hemi, r, wt); |
| 169 |
< |
ndivs = hemi.nt * hemi.np; |
| 170 |
< |
if (ndivs == 0) |
| 168 |
> |
inithemi(&hemi, acol, r, wt); |
| 169 |
> |
divcnt = hemi.nt * hemi.np; |
| 170 |
> |
/* initialize */ |
| 171 |
> |
if (pg != NULL) |
| 172 |
> |
pg[0] = pg[1] = pg[2] = 0.0; |
| 173 |
> |
if (dg != NULL) |
| 174 |
> |
dg[0] = dg[1] = dg[2] = 0.0; |
| 175 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
| 176 |
> |
if (divcnt == 0) |
| 177 |
|
return(0.0); |
| 178 |
< |
/* set number of super-samples */ |
| 179 |
< |
ns = ambssamp * wt + 0.5; |
| 180 |
< |
if (ns > 0 || pg != NULL || dg != NULL) { |
| 115 |
< |
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
| 178 |
> |
/* allocate super-samples */ |
| 179 |
> |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
| 180 |
> |
div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP)); |
| 181 |
|
if (div == NULL) |
| 182 |
|
error(SYSTEM, "out of memory in doambient"); |
| 183 |
|
} else |
| 186 |
|
arad = 0.0; |
| 187 |
|
if ((dp = div) == NULL) |
| 188 |
|
dp = &dnew; |
| 189 |
+ |
divcnt = 0; |
| 190 |
|
for (i = 0; i < hemi.nt; i++) |
| 191 |
|
for (j = 0; j < hemi.np; j++) { |
| 192 |
|
dp->t = i; dp->p = j; |
| 193 |
|
setcolor(dp->v, 0.0, 0.0, 0.0); |
| 194 |
|
dp->r = 0.0; |
| 195 |
|
dp->n = 0; |
| 196 |
< |
if (divsample(dp, &hemi, r) < 0) |
| 197 |
< |
goto oopsy; |
| 196 |
> |
if (divsample(dp, &hemi, r) < 0) { |
| 197 |
> |
if (div != NULL) |
| 198 |
> |
dp++; |
| 199 |
> |
continue; |
| 200 |
> |
} |
| 201 |
|
arad += dp->r; |
| 202 |
+ |
divcnt++; |
| 203 |
|
if (div != NULL) |
| 204 |
|
dp++; |
| 205 |
|
else |
| 206 |
|
addcolor(acol, dp->v); |
| 207 |
|
} |
| 208 |
< |
if (ns > 0 && arad > FTINY && ndivs/arad < minarad) |
| 209 |
< |
ns = 0; /* close enough */ |
| 210 |
< |
else if (ns > 0) { /* else perform super-sampling */ |
| 208 |
> |
if (!divcnt) { |
| 209 |
> |
if (div != NULL) |
| 210 |
> |
free((void *)div); |
| 211 |
> |
return(0.0); /* no samples taken */ |
| 212 |
> |
} |
| 213 |
> |
if (divcnt < hemi.nt*hemi.np) { |
| 214 |
> |
pg = dg = NULL; /* incomplete sampling */ |
| 215 |
> |
hemi.ns = 0; |
| 216 |
> |
} else if (arad > FTINY && divcnt/arad < minarad) { |
| 217 |
> |
hemi.ns = 0; /* close enough */ |
| 218 |
> |
} else if (hemi.ns > 0) { /* else perform super-sampling? */ |
| 219 |
|
comperrs(div, &hemi); /* compute errors */ |
| 220 |
< |
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
| 220 |
> |
qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
| 221 |
|
/* super-sample */ |
| 222 |
< |
for (i = ns; i > 0; i--) { |
| 222 |
> |
for (i = hemi.ns; i > 0; i--) { |
| 223 |
|
dnew = *div; |
| 224 |
< |
if (divsample(&dnew, &hemi, r) < 0) |
| 225 |
< |
goto oopsy; |
| 226 |
< |
/* reinsert */ |
| 227 |
< |
dp = div; |
| 228 |
< |
j = ndivs < i ? ndivs : i; |
| 224 |
> |
if (divsample(&dnew, &hemi, r) < 0) { |
| 225 |
> |
dp++; |
| 226 |
> |
continue; |
| 227 |
> |
} |
| 228 |
> |
dp = div; /* reinsert */ |
| 229 |
> |
j = divcnt < i ? divcnt : i; |
| 230 |
|
while (--j > 0 && dnew.k < dp[1].k) { |
| 231 |
|
*dp = *(dp+1); |
| 232 |
|
dp++; |
| 234 |
|
*dp = dnew; |
| 235 |
|
} |
| 236 |
|
if (pg != NULL || dg != NULL) /* restore order */ |
| 237 |
< |
qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); |
| 237 |
> |
qsort(div, divcnt, sizeof(AMBSAMP), ambnorm); |
| 238 |
|
} |
| 239 |
|
/* compute returned values */ |
| 240 |
|
if (div != NULL) { |
| 241 |
< |
arad = 0.0; |
| 242 |
< |
for (i = ndivs, dp = div; i-- > 0; dp++) { |
| 241 |
> |
arad = 0.0; /* note: divcnt may be < nt*np */ |
| 242 |
> |
for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) { |
| 243 |
|
arad += dp->r; |
| 244 |
|
if (dp->n > 1) { |
| 245 |
|
b = 1.0/dp->n; |
| 251 |
|
} |
| 252 |
|
b = bright(acol); |
| 253 |
|
if (b > FTINY) { |
| 254 |
< |
b = ndivs/b; |
| 254 |
> |
b = 1.0/b; /* compute & normalize gradient(s) */ |
| 255 |
|
if (pg != NULL) { |
| 256 |
|
posgradient(pg, div, &hemi); |
| 257 |
|
for (i = 0; i < 3; i++) |
| 262 |
|
for (i = 0; i < 3; i++) |
| 263 |
|
dg[i] *= b; |
| 264 |
|
} |
| 186 |
– |
} else { |
| 187 |
– |
if (pg != NULL) |
| 188 |
– |
for (i = 0; i < 3; i++) |
| 189 |
– |
pg[i] = 0.0; |
| 190 |
– |
if (dg != NULL) |
| 191 |
– |
for (i = 0; i < 3; i++) |
| 192 |
– |
dg[i] = 0.0; |
| 265 |
|
} |
| 266 |
|
free((void *)div); |
| 267 |
|
} |
| 196 |
– |
b = 1.0/ndivs; |
| 197 |
– |
scalecolor(acol, b); |
| 268 |
|
if (arad <= FTINY) |
| 269 |
|
arad = maxarad; |
| 270 |
|
else |
| 271 |
< |
arad = (ndivs+ns)/arad; |
| 271 |
> |
arad = (divcnt+hemi.ns)/arad; |
| 272 |
|
if (pg != NULL) { /* reduce radius if gradient large */ |
| 273 |
|
d = DOT(pg,pg); |
| 274 |
|
if (d*arad*arad > 1.0) |
| 285 |
|
if ((arad /= sqrt(wt)) > maxarad) |
| 286 |
|
arad = maxarad; |
| 287 |
|
return(arad); |
| 218 |
– |
oopsy: |
| 219 |
– |
if (div != NULL) |
| 220 |
– |
free((void *)div); |
| 221 |
– |
return(0.0); |
| 288 |
|
} |
| 289 |
|
|
| 290 |
|
|
| 291 |
|
void |
| 292 |
< |
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
| 293 |
< |
register AMBHEMI *hp; |
| 294 |
< |
RAY *r; |
| 295 |
< |
double wt; |
| 292 |
> |
comperrs( /* compute initial error estimates */ |
| 293 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
| 294 |
> |
register AMBHEMI *hp |
| 295 |
> |
) |
| 296 |
|
{ |
| 231 |
– |
register int i; |
| 232 |
– |
/* set number of divisions */ |
| 233 |
– |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
| 234 |
– |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
| 235 |
– |
if (hp->nt < i) |
| 236 |
– |
hp->nt = i; |
| 237 |
– |
hp->np = PI * hp->nt + 0.5; |
| 238 |
– |
/* make axes */ |
| 239 |
– |
VCOPY(hp->uz, r->ron); |
| 240 |
– |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
| 241 |
– |
for (i = 0; i < 3; i++) |
| 242 |
– |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
| 243 |
– |
break; |
| 244 |
– |
if (i >= 3) |
| 245 |
– |
error(CONSISTENCY, "bad ray direction in inithemi"); |
| 246 |
– |
hp->uy[i] = 1.0; |
| 247 |
– |
fcross(hp->ux, hp->uy, hp->uz); |
| 248 |
– |
normalize(hp->ux); |
| 249 |
– |
fcross(hp->uy, hp->uz, hp->ux); |
| 250 |
– |
} |
| 251 |
– |
|
| 252 |
– |
|
| 253 |
– |
void |
| 254 |
– |
comperrs(da, hp) /* compute initial error estimates */ |
| 255 |
– |
AMBSAMP *da; /* assumes standard ordering */ |
| 256 |
– |
register AMBHEMI *hp; |
| 257 |
– |
{ |
| 297 |
|
double b, b2; |
| 298 |
|
int i, j; |
| 299 |
|
register AMBSAMP *dp; |
| 341 |
|
|
| 342 |
|
|
| 343 |
|
void |
| 344 |
< |
posgradient(gv, da, hp) /* compute position gradient */ |
| 345 |
< |
FVECT gv; |
| 346 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
| 347 |
< |
register AMBHEMI *hp; |
| 344 |
> |
posgradient( /* compute position gradient */ |
| 345 |
> |
FVECT gv, |
| 346 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
| 347 |
> |
register AMBHEMI *hp |
| 348 |
> |
) |
| 349 |
|
{ |
| 350 |
|
register int i, j; |
| 351 |
|
double nextsine, lastsine, b, d; |
| 394 |
|
yd += mag0*sinp + mag1*cosp; |
| 395 |
|
} |
| 396 |
|
for (i = 0; i < 3; i++) |
| 397 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI; |
| 397 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI; |
| 398 |
|
} |
| 399 |
|
|
| 400 |
|
|
| 401 |
|
void |
| 402 |
< |
dirgradient(gv, da, hp) /* compute direction gradient */ |
| 403 |
< |
FVECT gv; |
| 404 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
| 405 |
< |
register AMBHEMI *hp; |
| 402 |
> |
dirgradient( /* compute direction gradient */ |
| 403 |
> |
FVECT gv, |
| 404 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
| 405 |
> |
register AMBHEMI *hp |
| 406 |
> |
) |
| 407 |
|
{ |
| 408 |
|
register int i, j; |
| 409 |
|
double mag; |
| 429 |
|
yd += mag * tsin(phi); |
| 430 |
|
} |
| 431 |
|
for (i = 0; i < 3; i++) |
| 432 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); |
| 432 |
> |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i]; |
| 433 |
|
} |