| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: ambcomp.c,v 2.18 2005/06/06 19:14:28 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Routines to compute "ambient" values using Monte Carlo
|
| 6 |
*
|
| 7 |
* Declarations of external symbols in ambient.h
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "copyright.h"
|
| 11 |
|
| 12 |
#include "ray.h"
|
| 13 |
|
| 14 |
#include "ambient.h"
|
| 15 |
|
| 16 |
#include "random.h"
|
| 17 |
|
| 18 |
|
| 19 |
void
|
| 20 |
inithemi( /* initialize sampling hemisphere */
|
| 21 |
register AMBHEMI *hp,
|
| 22 |
COLOR ac,
|
| 23 |
RAY *r,
|
| 24 |
double wt
|
| 25 |
)
|
| 26 |
{
|
| 27 |
double d;
|
| 28 |
register int i;
|
| 29 |
/* set number of divisions */
|
| 30 |
if (ambacc <= FTINY &&
|
| 31 |
wt > (d = 0.8*bright(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 |
|
| 59 |
int
|
| 60 |
divsample( /* sample a division */
|
| 61 |
register AMBSAMP *dp,
|
| 62 |
AMBHEMI *h,
|
| 63 |
RAY *r
|
| 64 |
)
|
| 65 |
{
|
| 66 |
RAY ar;
|
| 67 |
int hlist[3];
|
| 68 |
double spt[2];
|
| 69 |
double xd, yd, zd;
|
| 70 |
double b2;
|
| 71 |
double phi;
|
| 72 |
register int i;
|
| 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;
|
| 87 |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
|
| 88 |
zd = sqrt((dp->t + spt[0])/h->nt);
|
| 89 |
phi = 2.0*PI * (dp->p + spt[1])/h->np;
|
| 90 |
xd = tcos(phi) * zd;
|
| 91 |
yd = tsin(phi) * zd;
|
| 92 |
zd = sqrt(1.0 - zd*zd);
|
| 93 |
for (i = 0; i < 3; i++)
|
| 94 |
ar.rdir[i] = xd*h->ux[i] +
|
| 95 |
yd*h->uy[i] +
|
| 96 |
zd*h->uz[i];
|
| 97 |
dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
|
| 98 |
rayvalue(&ar);
|
| 99 |
ndims--;
|
| 100 |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */
|
| 101 |
addcolor(dp->v, ar.rcol);
|
| 102 |
/* use rt to improve gradient calc */
|
| 103 |
if (ar.rt > FTINY && ar.rt < FHUGE)
|
| 104 |
dp->r += 1.0/ar.rt;
|
| 105 |
/* (re)initialize error */
|
| 106 |
if (dp->n++) {
|
| 107 |
b2 = bright(dp->v)/dp->n - bright(ar.rcol);
|
| 108 |
b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1));
|
| 109 |
dp->k = b2/(dp->n*dp->n);
|
| 110 |
} else
|
| 111 |
dp->k = 0.0;
|
| 112 |
return(0);
|
| 113 |
}
|
| 114 |
|
| 115 |
|
| 116 |
static int
|
| 117 |
ambcmp( /* decreasing order */
|
| 118 |
const void *p1,
|
| 119 |
const void *p2
|
| 120 |
)
|
| 121 |
{
|
| 122 |
const AMBSAMP *d1 = (const AMBSAMP *)p1;
|
| 123 |
const AMBSAMP *d2 = (const AMBSAMP *)p2;
|
| 124 |
|
| 125 |
if (d1->k < d2->k)
|
| 126 |
return(1);
|
| 127 |
if (d1->k > d2->k)
|
| 128 |
return(-1);
|
| 129 |
return(0);
|
| 130 |
}
|
| 131 |
|
| 132 |
|
| 133 |
static int
|
| 134 |
ambnorm( /* standard order */
|
| 135 |
const void *p1,
|
| 136 |
const void *p2
|
| 137 |
)
|
| 138 |
{
|
| 139 |
const AMBSAMP *d1 = (const AMBSAMP *)p1;
|
| 140 |
const AMBSAMP *d2 = (const AMBSAMP *)p2;
|
| 141 |
register int c;
|
| 142 |
|
| 143 |
if ( (c = d1->t - d2->t) )
|
| 144 |
return(c);
|
| 145 |
return(d1->p - d2->p);
|
| 146 |
}
|
| 147 |
|
| 148 |
|
| 149 |
double
|
| 150 |
doambient( /* compute ambient component */
|
| 151 |
COLOR acol,
|
| 152 |
RAY *r,
|
| 153 |
double wt,
|
| 154 |
FVECT pg,
|
| 155 |
FVECT dg
|
| 156 |
)
|
| 157 |
{
|
| 158 |
double b, d;
|
| 159 |
AMBHEMI hemi;
|
| 160 |
AMBSAMP *div;
|
| 161 |
AMBSAMP dnew;
|
| 162 |
register AMBSAMP *dp;
|
| 163 |
double arad;
|
| 164 |
int divcnt;
|
| 165 |
register int i, j;
|
| 166 |
/* initialize hemisphere */
|
| 167 |
inithemi(&hemi, acol, r, wt);
|
| 168 |
divcnt = hemi.nt * hemi.np;
|
| 169 |
/* initialize */
|
| 170 |
if (pg != NULL)
|
| 171 |
pg[0] = pg[1] = pg[2] = 0.0;
|
| 172 |
if (dg != NULL)
|
| 173 |
dg[0] = dg[1] = dg[2] = 0.0;
|
| 174 |
setcolor(acol, 0.0, 0.0, 0.0);
|
| 175 |
if (divcnt == 0)
|
| 176 |
return(0.0);
|
| 177 |
/* allocate super-samples */
|
| 178 |
if (hemi.ns > 0 || pg != NULL || dg != NULL) {
|
| 179 |
div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
|
| 180 |
if (div == NULL)
|
| 181 |
error(SYSTEM, "out of memory in doambient");
|
| 182 |
} else
|
| 183 |
div = NULL;
|
| 184 |
/* sample the divisions */
|
| 185 |
arad = 0.0;
|
| 186 |
if ((dp = div) == NULL)
|
| 187 |
dp = &dnew;
|
| 188 |
divcnt = 0;
|
| 189 |
for (i = 0; i < hemi.nt; i++)
|
| 190 |
for (j = 0; j < hemi.np; j++) {
|
| 191 |
dp->t = i; dp->p = j;
|
| 192 |
setcolor(dp->v, 0.0, 0.0, 0.0);
|
| 193 |
dp->r = 0.0;
|
| 194 |
dp->n = 0;
|
| 195 |
if (divsample(dp, &hemi, r) < 0) {
|
| 196 |
if (div != NULL)
|
| 197 |
dp++;
|
| 198 |
continue;
|
| 199 |
}
|
| 200 |
arad += dp->r;
|
| 201 |
divcnt++;
|
| 202 |
if (div != NULL)
|
| 203 |
dp++;
|
| 204 |
else
|
| 205 |
addcolor(acol, dp->v);
|
| 206 |
}
|
| 207 |
if (!divcnt)
|
| 208 |
return(0.0); /* no samples taken */
|
| 209 |
if (divcnt < hemi.nt*hemi.np) {
|
| 210 |
pg = dg = NULL; /* incomplete sampling */
|
| 211 |
hemi.ns = 0;
|
| 212 |
} else if (arad > FTINY && divcnt/arad < minarad) {
|
| 213 |
hemi.ns = 0; /* close enough */
|
| 214 |
} else if (hemi.ns > 0) { /* else perform super-sampling? */
|
| 215 |
comperrs(div, &hemi); /* compute errors */
|
| 216 |
qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */
|
| 217 |
/* super-sample */
|
| 218 |
for (i = hemi.ns; i > 0; i--) {
|
| 219 |
dnew = *div;
|
| 220 |
if (divsample(&dnew, &hemi, r) < 0) {
|
| 221 |
dp++;
|
| 222 |
continue;
|
| 223 |
}
|
| 224 |
dp = div; /* reinsert */
|
| 225 |
j = divcnt < i ? divcnt : i;
|
| 226 |
while (--j > 0 && dnew.k < dp[1].k) {
|
| 227 |
*dp = *(dp+1);
|
| 228 |
dp++;
|
| 229 |
}
|
| 230 |
*dp = dnew;
|
| 231 |
}
|
| 232 |
if (pg != NULL || dg != NULL) /* restore order */
|
| 233 |
qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
|
| 234 |
}
|
| 235 |
/* compute returned values */
|
| 236 |
if (div != NULL) {
|
| 237 |
arad = 0.0; /* note: divcnt may be < nt*np */
|
| 238 |
for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
|
| 239 |
arad += dp->r;
|
| 240 |
if (dp->n > 1) {
|
| 241 |
b = 1.0/dp->n;
|
| 242 |
scalecolor(dp->v, b);
|
| 243 |
dp->r *= b;
|
| 244 |
dp->n = 1;
|
| 245 |
}
|
| 246 |
addcolor(acol, dp->v);
|
| 247 |
}
|
| 248 |
b = bright(acol);
|
| 249 |
if (b > FTINY) {
|
| 250 |
b = 1.0/b; /* compute & normalize gradient(s) */
|
| 251 |
if (pg != NULL) {
|
| 252 |
posgradient(pg, div, &hemi);
|
| 253 |
for (i = 0; i < 3; i++)
|
| 254 |
pg[i] *= b;
|
| 255 |
}
|
| 256 |
if (dg != NULL) {
|
| 257 |
dirgradient(dg, div, &hemi);
|
| 258 |
for (i = 0; i < 3; i++)
|
| 259 |
dg[i] *= b;
|
| 260 |
}
|
| 261 |
}
|
| 262 |
free((void *)div);
|
| 263 |
}
|
| 264 |
if (arad <= FTINY)
|
| 265 |
arad = maxarad;
|
| 266 |
else
|
| 267 |
arad = (divcnt+hemi.ns)/arad;
|
| 268 |
if (pg != NULL) { /* reduce radius if gradient large */
|
| 269 |
d = DOT(pg,pg);
|
| 270 |
if (d*arad*arad > 1.0)
|
| 271 |
arad = 1.0/sqrt(d);
|
| 272 |
}
|
| 273 |
if (arad < minarad) {
|
| 274 |
arad = minarad;
|
| 275 |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */
|
| 276 |
d = 1.0/arad/sqrt(d);
|
| 277 |
for (i = 0; i < 3; i++)
|
| 278 |
pg[i] *= d;
|
| 279 |
}
|
| 280 |
}
|
| 281 |
if ((arad /= sqrt(wt)) > maxarad)
|
| 282 |
arad = maxarad;
|
| 283 |
return(arad);
|
| 284 |
}
|
| 285 |
|
| 286 |
|
| 287 |
void
|
| 288 |
comperrs( /* compute initial error estimates */
|
| 289 |
AMBSAMP *da, /* assumes standard ordering */
|
| 290 |
register AMBHEMI *hp
|
| 291 |
)
|
| 292 |
{
|
| 293 |
double b, b2;
|
| 294 |
int i, j;
|
| 295 |
register AMBSAMP *dp;
|
| 296 |
/* sum differences from neighbors */
|
| 297 |
dp = da;
|
| 298 |
for (i = 0; i < hp->nt; i++)
|
| 299 |
for (j = 0; j < hp->np; j++) {
|
| 300 |
#ifdef DEBUG
|
| 301 |
if (dp->t != i || dp->p != j)
|
| 302 |
error(CONSISTENCY,
|
| 303 |
"division order in comperrs");
|
| 304 |
#endif
|
| 305 |
b = bright(dp[0].v);
|
| 306 |
if (i > 0) { /* from above */
|
| 307 |
b2 = bright(dp[-hp->np].v) - b;
|
| 308 |
b2 *= b2 * 0.25;
|
| 309 |
dp[0].k += b2;
|
| 310 |
dp[-hp->np].k += b2;
|
| 311 |
}
|
| 312 |
if (j > 0) { /* from behind */
|
| 313 |
b2 = bright(dp[-1].v) - b;
|
| 314 |
b2 *= b2 * 0.25;
|
| 315 |
dp[0].k += b2;
|
| 316 |
dp[-1].k += b2;
|
| 317 |
} else { /* around */
|
| 318 |
b2 = bright(dp[hp->np-1].v) - b;
|
| 319 |
b2 *= b2 * 0.25;
|
| 320 |
dp[0].k += b2;
|
| 321 |
dp[hp->np-1].k += b2;
|
| 322 |
}
|
| 323 |
dp++;
|
| 324 |
}
|
| 325 |
/* divide by number of neighbors */
|
| 326 |
dp = da;
|
| 327 |
for (j = 0; j < hp->np; j++) /* top row */
|
| 328 |
(dp++)->k *= 1.0/3.0;
|
| 329 |
if (hp->nt < 2)
|
| 330 |
return;
|
| 331 |
for (i = 1; i < hp->nt-1; i++) /* central region */
|
| 332 |
for (j = 0; j < hp->np; j++)
|
| 333 |
(dp++)->k *= 0.25;
|
| 334 |
for (j = 0; j < hp->np; j++) /* bottom row */
|
| 335 |
(dp++)->k *= 1.0/3.0;
|
| 336 |
}
|
| 337 |
|
| 338 |
|
| 339 |
void
|
| 340 |
posgradient( /* compute position gradient */
|
| 341 |
FVECT gv,
|
| 342 |
AMBSAMP *da, /* assumes standard ordering */
|
| 343 |
register AMBHEMI *hp
|
| 344 |
)
|
| 345 |
{
|
| 346 |
register int i, j;
|
| 347 |
double nextsine, lastsine, b, d;
|
| 348 |
double mag0, mag1;
|
| 349 |
double phi, cosp, sinp, xd, yd;
|
| 350 |
register AMBSAMP *dp;
|
| 351 |
|
| 352 |
xd = yd = 0.0;
|
| 353 |
for (j = 0; j < hp->np; j++) {
|
| 354 |
dp = da + j;
|
| 355 |
mag0 = mag1 = 0.0;
|
| 356 |
lastsine = 0.0;
|
| 357 |
for (i = 0; i < hp->nt; i++) {
|
| 358 |
#ifdef DEBUG
|
| 359 |
if (dp->t != i || dp->p != j)
|
| 360 |
error(CONSISTENCY,
|
| 361 |
"division order in posgradient");
|
| 362 |
#endif
|
| 363 |
b = bright(dp->v);
|
| 364 |
if (i > 0) {
|
| 365 |
d = dp[-hp->np].r;
|
| 366 |
if (dp[0].r > d) d = dp[0].r;
|
| 367 |
/* sin(t)*cos(t)^2 */
|
| 368 |
d *= lastsine * (1.0 - (double)i/hp->nt);
|
| 369 |
mag0 += d*(b - bright(dp[-hp->np].v));
|
| 370 |
}
|
| 371 |
nextsine = sqrt((double)(i+1)/hp->nt);
|
| 372 |
if (j > 0) {
|
| 373 |
d = dp[-1].r;
|
| 374 |
if (dp[0].r > d) d = dp[0].r;
|
| 375 |
mag1 += d * (nextsine - lastsine) *
|
| 376 |
(b - bright(dp[-1].v));
|
| 377 |
} else {
|
| 378 |
d = dp[hp->np-1].r;
|
| 379 |
if (dp[0].r > d) d = dp[0].r;
|
| 380 |
mag1 += d * (nextsine - lastsine) *
|
| 381 |
(b - bright(dp[hp->np-1].v));
|
| 382 |
}
|
| 383 |
dp += hp->np;
|
| 384 |
lastsine = nextsine;
|
| 385 |
}
|
| 386 |
mag0 *= 2.0*PI / hp->np;
|
| 387 |
phi = 2.0*PI * (double)j/hp->np;
|
| 388 |
cosp = tcos(phi); sinp = tsin(phi);
|
| 389 |
xd += mag0*cosp - mag1*sinp;
|
| 390 |
yd += mag0*sinp + mag1*cosp;
|
| 391 |
}
|
| 392 |
for (i = 0; i < 3; i++)
|
| 393 |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
|
| 394 |
}
|
| 395 |
|
| 396 |
|
| 397 |
void
|
| 398 |
dirgradient( /* compute direction gradient */
|
| 399 |
FVECT gv,
|
| 400 |
AMBSAMP *da, /* assumes standard ordering */
|
| 401 |
register AMBHEMI *hp
|
| 402 |
)
|
| 403 |
{
|
| 404 |
register int i, j;
|
| 405 |
double mag;
|
| 406 |
double phi, xd, yd;
|
| 407 |
register AMBSAMP *dp;
|
| 408 |
|
| 409 |
xd = yd = 0.0;
|
| 410 |
for (j = 0; j < hp->np; j++) {
|
| 411 |
dp = da + j;
|
| 412 |
mag = 0.0;
|
| 413 |
for (i = 0; i < hp->nt; i++) {
|
| 414 |
#ifdef DEBUG
|
| 415 |
if (dp->t != i || dp->p != j)
|
| 416 |
error(CONSISTENCY,
|
| 417 |
"division order in dirgradient");
|
| 418 |
#endif
|
| 419 |
/* tan(t) */
|
| 420 |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0);
|
| 421 |
dp += hp->np;
|
| 422 |
}
|
| 423 |
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
|
| 424 |
xd += mag * tcos(phi);
|
| 425 |
yd += mag * tsin(phi);
|
| 426 |
}
|
| 427 |
for (i = 0; i < 3; i++)
|
| 428 |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
|
| 429 |
}
|