| 6 | 
  | 
 * Initialization and calculation routines | 
| 7 | 
  | 
 */ | 
| 8 | 
  | 
 | 
| 9 | 
+ | 
#include "copyright.h" | 
| 10 | 
+ | 
 | 
| 11 | 
  | 
#include "rcontrib.h" | 
| 10 | 
– | 
#include "source.h" | 
| 12 | 
  | 
#include "otypes.h" | 
| 13 | 
< | 
#include "platform.h" | 
| 13 | 
> | 
#include "source.h" | 
| 14 | 
  | 
 | 
| 15 | 
  | 
char    *shm_boundary = NULL;           /* boundary of shared memory */ | 
| 16 | 
  | 
 | 
| 28 | 
  | 
 | 
| 29 | 
  | 
int     rand_samp = 1;                  /* pure Monte Carlo sampling? */ | 
| 30 | 
  | 
 | 
| 31 | 
< | 
double  dstrsrc = 0.0;                  /* square source distribution */ | 
| 31 | 
> | 
double  dstrsrc = 0.9;                  /* square source distribution */ | 
| 32 | 
  | 
double  shadthresh = .03;               /* shadow threshold */ | 
| 33 | 
  | 
double  shadcert = .75;                 /* shadow certainty */ | 
| 34 | 
  | 
int     directrelay = 3;                /* number of source relays */ | 
| 71 | 
  | 
 | 
| 72 | 
  | 
LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */ | 
| 73 | 
  | 
 | 
| 73 | 
– | 
static OBJECT           traset[MAXTSET+1]={0};  /* trace include set */ | 
| 74 | 
– | 
 | 
| 74 | 
  | 
/************************** INITIALIZATION ROUTINES ***********************/ | 
| 75 | 
  | 
 | 
| 77 | 
– | 
void | 
| 78 | 
– | 
tranotify(                      /* record new modifier */ | 
| 79 | 
– | 
        OBJECT  obj | 
| 80 | 
– | 
) | 
| 81 | 
– | 
{ | 
| 82 | 
– | 
        static int      hitlimit = 0; | 
| 83 | 
– | 
        OBJREC          *o = objptr(obj); | 
| 84 | 
– | 
        int             i; | 
| 85 | 
– | 
 | 
| 86 | 
– | 
        if (obj == OVOID) {             /* starting over */ | 
| 87 | 
– | 
                traset[0] = 0; | 
| 88 | 
– | 
                hitlimit = 0; | 
| 89 | 
– | 
                return; | 
| 90 | 
– | 
        } | 
| 91 | 
– | 
        if (hitlimit || !ismodifier(o->otype)) | 
| 92 | 
– | 
                return; | 
| 93 | 
– | 
        for (i = nmods; i-- > 0; ) | 
| 94 | 
– | 
                if (!strcmp(o->oname, modname[i])) { | 
| 95 | 
– | 
                        if (traset[0] >= MAXTSET) { | 
| 96 | 
– | 
                                error(WARNING, "too many same-named modifiers"); | 
| 97 | 
– | 
                                hitlimit++; | 
| 98 | 
– | 
                                return;         /* should this be fatal? */ | 
| 99 | 
– | 
                        } | 
| 100 | 
– | 
                        insertelem(traset, obj); | 
| 101 | 
– | 
                        break; | 
| 102 | 
– | 
                } | 
| 103 | 
– | 
} | 
| 104 | 
– | 
 | 
| 105 | 
– | 
 | 
| 76 | 
  | 
char * | 
| 77 | 
  | 
formstr(                                /* return format identifier */ | 
| 78 | 
  | 
        int  f | 
| 90 | 
  | 
 | 
| 91 | 
  | 
/* Add modifier to our list to track */ | 
| 92 | 
  | 
MODCONT * | 
| 93 | 
< | 
addmodifier(char *modn, char *outf, char *binv, int bincnt) | 
| 93 | 
> | 
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) | 
| 94 | 
  | 
{ | 
| 95 | 
  | 
        LUENT   *lep = lu_find(&modconttab,modn); | 
| 96 | 
  | 
        MODCONT *mp; | 
| 101 | 
  | 
                sprintf(errmsg, "duplicate modifier '%s'", modn); | 
| 102 | 
  | 
                error(USER, errmsg); | 
| 103 | 
  | 
        } | 
| 104 | 
< | 
        if (nmods >= MAXMODLIST) | 
| 105 | 
< | 
                error(INTERNAL, "too many modifiers"); | 
| 104 | 
> | 
        if (nmods >= MAXMODLIST) { | 
| 105 | 
> | 
                sprintf(errmsg, "too many modifiers (%d limit)", MAXMODLIST); | 
| 106 | 
> | 
                error(INTERNAL, errmsg); | 
| 107 | 
> | 
        } | 
| 108 | 
> | 
        if (!strcmp(modn, VOIDID)) { | 
| 109 | 
> | 
                sprintf(errmsg, "cannot track '%s' modifier", VOIDID); | 
| 110 | 
> | 
                error(USER, errmsg); | 
| 111 | 
> | 
        } | 
| 112 | 
  | 
        modname[nmods++] = modn;        /* XXX assumes static string */ | 
| 113 | 
  | 
        lep->key = modn;                /* XXX assumes static string */ | 
| 114 | 
  | 
        if (binv == NULL) | 
| 133 | 
  | 
                error(SYSTEM, "out of memory in addmodifier"); | 
| 134 | 
  | 
        mp->outspec = outf;             /* XXX assumes static string */ | 
| 135 | 
  | 
        mp->modname = modn;             /* XXX assumes static string */ | 
| 136 | 
+ | 
        mp->params = prms; | 
| 137 | 
  | 
        mp->binv = ebinv; | 
| 138 | 
  | 
        mp->nbins = bincnt; | 
| 139 | 
  | 
        memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); | 
| 145 | 
  | 
} | 
| 146 | 
  | 
 | 
| 147 | 
  | 
 | 
| 148 | 
< | 
/* add modifiers from a file list */ | 
| 148 | 
> | 
/* Add modifiers from a file list */ | 
| 149 | 
  | 
void | 
| 150 | 
< | 
addmodfile(char *fname, char *outf, char *binv, int bincnt) | 
| 150 | 
> | 
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) | 
| 151 | 
  | 
{ | 
| 152 | 
  | 
        char    *mname[MAXMODLIST]; | 
| 153 | 
  | 
        int     i; | 
| 154 | 
  | 
                                        /* find the file & store strings */ | 
| 155 | 
< | 
        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { | 
| 155 | 
> | 
        i = wordfile(mname, MAXMODLIST, getpath(fname, getrlibpath(), R_OK)); | 
| 156 | 
> | 
        if (i < 0) { | 
| 157 | 
  | 
                sprintf(errmsg, "cannot find modifier file '%s'", fname); | 
| 158 | 
  | 
                error(SYSTEM, errmsg); | 
| 159 | 
  | 
        } | 
| 160 | 
+ | 
        if (i >= MAXMODLIST-1) { | 
| 161 | 
+ | 
                sprintf(errmsg, "too many modifiers (%d limit) in file '%s'", | 
| 162 | 
+ | 
                                MAXMODLIST-1, fname); | 
| 163 | 
+ | 
                error(INTERNAL, errmsg); | 
| 164 | 
+ | 
        } | 
| 165 | 
  | 
        for (i = 0; mname[i]; i++)      /* add each one */ | 
| 166 | 
< | 
                addmodifier(mname[i], outf, binv, bincnt); | 
| 166 | 
> | 
                addmodifier(mname[i], outf, prms, binv, bincnt); | 
| 167 | 
  | 
} | 
| 168 | 
  | 
 | 
| 169 | 
  | 
 | 
| 173 | 
  | 
) | 
| 174 | 
  | 
{ | 
| 175 | 
  | 
        if (nchild > 0)         /* close children if any */ | 
| 176 | 
< | 
                end_children(); | 
| 176 | 
> | 
                end_children(code != 0); | 
| 177 | 
  | 
        exit(code); | 
| 178 | 
  | 
} | 
| 179 | 
  | 
 | 
| 192 | 
  | 
                                        /* set shared memory boundary */ | 
| 193 | 
  | 
                shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); | 
| 194 | 
  | 
        } | 
| 195 | 
< | 
        if ((nproc > 1) & (accumulate <= 0)) | 
| 196 | 
< | 
                put_zero_record(0);     /* prime our queue to accumulate */ | 
| 214 | 
< | 
 | 
| 195 | 
> | 
        for (i = 0; i < nsources; i++)  /* tracing to sources as well */ | 
| 196 | 
> | 
                source[i].sflags |= SFOLLOW; | 
| 197 | 
  | 
        if (yres > 0) {                 /* set up flushing & ray counts */ | 
| 198 | 
  | 
                if (xres > 0) | 
| 199 | 
  | 
                        raysleft = (RNUMBER)xres*yres; | 
| 204 | 
  | 
        if ((account = accumulate) > 1) | 
| 205 | 
  | 
                raysleft *= accumulate; | 
| 206 | 
  | 
        waitflush = (yres > 0) & (xres > 1) ? 0 : xres; | 
| 225 | 
– | 
                                        /* tracing to sources as well */ | 
| 226 | 
– | 
        for (i = 0; i < nsources; i++) | 
| 227 | 
– | 
                source[i].sflags |= SFOLLOW; | 
| 207 | 
  | 
 | 
| 208 | 
  | 
        if (nproc > 1 && in_rchild())   /* forked child? */ | 
| 209 | 
  | 
                return;                 /* return to main processing loop */ | 
| 210 | 
  | 
 | 
| 211 | 
  | 
        if (recover) {                  /* recover previous output? */ | 
| 212 | 
< | 
                if (accumulate <= 0) { | 
| 212 | 
> | 
                if (accumulate <= 0) | 
| 213 | 
  | 
                        reload_output(); | 
| 214 | 
< | 
                        if (nproc > 1) | 
| 236 | 
< | 
                                queue_modifiers(); | 
| 237 | 
< | 
                } else | 
| 214 | 
> | 
                else | 
| 215 | 
  | 
                        recover_output(); | 
| 216 | 
  | 
        } | 
| 217 | 
  | 
        if (nproc == 1)                 /* single process? */ | 
| 218 | 
  | 
                return; | 
| 219 | 
< | 
 | 
| 220 | 
< | 
        parental_loop();                /* else run controller */ | 
| 219 | 
> | 
                                        /* else run appropriate controller */ | 
| 220 | 
> | 
        if (accumulate <= 0) | 
| 221 | 
> | 
                feeder_loop(); | 
| 222 | 
> | 
        else | 
| 223 | 
> | 
                parental_loop(); | 
| 224 | 
  | 
        quit(0);                        /* parent musn't return! */ | 
| 225 | 
  | 
} | 
| 226 | 
  | 
 | 
| 231 | 
  | 
trace_contrib(RAY *r) | 
| 232 | 
  | 
{ | 
| 233 | 
  | 
        MODCONT *mp; | 
| 234 | 
+ | 
        double  bval; | 
| 235 | 
  | 
        int     bn; | 
| 236 | 
  | 
        RREAL   contr[3]; | 
| 237 | 
  | 
 | 
| 238 | 
< | 
        if (r->ro == NULL || !inset(traset, r->ro->omod)) | 
| 238 | 
> | 
        if (r->ro == NULL || r->ro->omod == OVOID) | 
| 239 | 
  | 
                return; | 
| 240 | 
+ | 
                                                /* shadow ray not on source? */ | 
| 241 | 
+ | 
        if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) | 
| 242 | 
+ | 
                return; | 
| 243 | 
  | 
 | 
| 244 | 
  | 
        mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; | 
| 245 | 
  | 
 | 
| 246 | 
< | 
        if (mp == NULL) | 
| 247 | 
< | 
                error(CONSISTENCY, "unexpected modifier in trace_contrib()"); | 
| 246 | 
> | 
        if (mp == NULL)                         /* not in our list? */ | 
| 247 | 
> | 
                return; | 
| 248 | 
  | 
 | 
| 249 | 
< | 
        worldfunc(RCCONTEXT, r);                /* get bin number */ | 
| 250 | 
< | 
        bn = (int)(evalue(mp->binv) + .5); | 
| 251 | 
< | 
        if ((bn < 0) | (bn >= mp->nbins)) { | 
| 252 | 
< | 
                error(WARNING, "bad bin number (ignored)"); | 
| 249 | 
> | 
        worldfunc(RCCONTEXT, r);                /* else set context */ | 
| 250 | 
> | 
        set_eparams((char *)mp->params); | 
| 251 | 
> | 
        if ((bval = evalue(mp->binv)) <= -.5)   /* and get bin number */ | 
| 252 | 
> | 
                return;                         /* silently ignore negatives */ | 
| 253 | 
> | 
        if ((bn = (int)(bval + .5)) >= mp->nbins) { | 
| 254 | 
> | 
                sprintf(errmsg, "bad bin number (%d ignored)", bn); | 
| 255 | 
> | 
                error(WARNING, errmsg); | 
| 256 | 
  | 
                return; | 
| 257 | 
  | 
        } | 
| 258 | 
< | 
        raycontrib(contr, r, PRIMARY); | 
| 258 | 
> | 
        raycontrib(contr, r, PRIMARY);          /* compute coefficient */ | 
| 259 | 
  | 
        if (contrib) | 
| 260 | 
< | 
                multcolor(contr, r->rcol); | 
| 260 | 
> | 
                multcolor(contr, r->rcol);      /* -> contribution */ | 
| 261 | 
  | 
        addcolor(mp->cbin[bn], contr); | 
| 262 | 
  | 
} | 
| 263 | 
  | 
 | 
| 264 | 
  | 
 | 
| 265 | 
+ | 
/* Evaluate irradiance contributions */ | 
| 266 | 
  | 
static void | 
| 267 | 
< | 
rayirrad(                       /* compute irradiance rather than radiance */ | 
| 280 | 
< | 
        RAY *r | 
| 281 | 
< | 
) | 
| 267 | 
> | 
eval_irrad(FVECT org, FVECT dir) | 
| 268 | 
  | 
{ | 
| 269 | 
< | 
        r->rot = 1e-5;                  /* pretend we hit surface */ | 
| 270 | 
< | 
        VSUM(r->rop, r->rorg, r->rdir, r->rot); | 
| 271 | 
< | 
        r->ron[0] = -r->rdir[0]; | 
| 272 | 
< | 
        r->ron[1] = -r->rdir[1]; | 
| 273 | 
< | 
        r->ron[2] = -r->rdir[2]; | 
| 274 | 
< | 
        r->rod = 1.0; | 
| 275 | 
< | 
                                        /* compute result */ | 
| 276 | 
< | 
        r->revf = raytrace; | 
| 277 | 
< | 
        (*ofun[Lamb.otype].funp)(&Lamb, r); | 
| 278 | 
< | 
        r->revf = rayirrad; | 
| 269 | 
> | 
        RAY     thisray; | 
| 270 | 
> | 
 | 
| 271 | 
> | 
        VSUM(thisray.rorg, org, dir, 1.1e-4); | 
| 272 | 
> | 
        thisray.rdir[0] = -dir[0]; | 
| 273 | 
> | 
        thisray.rdir[1] = -dir[1]; | 
| 274 | 
> | 
        thisray.rdir[2] = -dir[2]; | 
| 275 | 
> | 
        thisray.rmax = 0.0; | 
| 276 | 
> | 
        rayorigin(&thisray, PRIMARY, NULL, NULL); | 
| 277 | 
> | 
                                        /* pretend we hit surface */ | 
| 278 | 
> | 
        thisray.rt = thisray.rot = 1e-5; | 
| 279 | 
> | 
        thisray.rod = 1.0; | 
| 280 | 
> | 
        VCOPY(thisray.ron, dir); | 
| 281 | 
> | 
        VSUM(thisray.rop, org, dir, 1e-4); | 
| 282 | 
> | 
        samplendx++;                    /* compute result */ | 
| 283 | 
> | 
        (*ofun[Lamb.otype].funp)(&Lamb, &thisray); | 
| 284 | 
  | 
} | 
| 285 | 
  | 
 | 
| 286 | 
  | 
 | 
| 287 | 
< | 
/* Evaluate ray contributions */ | 
| 287 | 
> | 
/* Evaluate radiance contributions */ | 
| 288 | 
  | 
static void | 
| 289 | 
< | 
eval_ray(FVECT  org, FVECT  dir, double dmax) | 
| 289 | 
> | 
eval_rad(FVECT org, FVECT dir, double dmax) | 
| 290 | 
  | 
{ | 
| 291 | 
  | 
        RAY     thisray; | 
| 292 | 
  | 
                                        /* set up ray */ | 
| 293 | 
+ | 
        VCOPY(thisray.rorg, org); | 
| 294 | 
+ | 
        VCOPY(thisray.rdir, dir); | 
| 295 | 
+ | 
        thisray.rmax = dmax; | 
| 296 | 
  | 
        rayorigin(&thisray, PRIMARY, NULL, NULL); | 
| 303 | 
– | 
        if (imm_irrad) { | 
| 304 | 
– | 
                VSUM(thisray.rorg, org, dir, 1.1e-4); | 
| 305 | 
– | 
                thisray.rdir[0] = -dir[0]; | 
| 306 | 
– | 
                thisray.rdir[1] = -dir[1]; | 
| 307 | 
– | 
                thisray.rdir[2] = -dir[2]; | 
| 308 | 
– | 
                thisray.rmax = 0.0; | 
| 309 | 
– | 
                thisray.revf = rayirrad; | 
| 310 | 
– | 
        } else { | 
| 311 | 
– | 
                VCOPY(thisray.rorg, org); | 
| 312 | 
– | 
                VCOPY(thisray.rdir, dir); | 
| 313 | 
– | 
                thisray.rmax = dmax; | 
| 314 | 
– | 
        } | 
| 297 | 
  | 
        samplendx++;                    /* call ray evaluation */ | 
| 298 | 
  | 
        rayvalue(&thisray); | 
| 299 | 
  | 
} | 
| 335 | 
  | 
#endif | 
| 336 | 
  | 
        while (getvec(orig) == 0 && getvec(direc) == 0) { | 
| 337 | 
  | 
                d = normalize(direc); | 
| 338 | 
< | 
                if (nchild != -1 && (d == 0.0) & (accumulate != 1)) { | 
| 338 | 
> | 
                if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { | 
| 339 | 
  | 
                        if (!ignore_warning_given++) | 
| 340 | 
  | 
                                error(WARNING, | 
| 341 | 
  | 
                                "dummy ray(s) ignored during accumulation\n"); | 
| 345 | 
  | 
                        lastray = lastdone = 0; | 
| 346 | 
  | 
                ++lastray; | 
| 347 | 
  | 
                if (d == 0.0) {                         /* zero ==> flush */ | 
| 348 | 
< | 
                        if ((yres <= 0) | (xres <= 0)) | 
| 349 | 
< | 
                                waitflush = 1;          /* flush right after */ | 
| 350 | 
< | 
                        account = 1; | 
| 351 | 
< | 
                } else {                                /* else compute */ | 
| 352 | 
< | 
                        eval_ray(orig, direc, lim_dist ? d : 0.0); | 
| 348 | 
> | 
                        if ((yres <= 0) | (xres <= 1)) | 
| 349 | 
> | 
                                waitflush = 1;          /* flush after */ | 
| 350 | 
> | 
                        if (nchild == -1) | 
| 351 | 
> | 
                                account = 1; | 
| 352 | 
> | 
                } else if (imm_irrad) {                 /* else compute */ | 
| 353 | 
> | 
                        eval_irrad(orig, direc); | 
| 354 | 
> | 
                } else { | 
| 355 | 
> | 
                        eval_rad(orig, direc, lim_dist ? d : 0.0); | 
| 356 | 
  | 
                } | 
| 357 | 
  | 
                done_contrib();         /* accumulate/output */ | 
| 358 | 
  | 
                ++lastdone; | 
| 367 | 
  | 
                account = 1;            /* output accumulated totals */ | 
| 368 | 
  | 
                done_contrib(); | 
| 369 | 
  | 
        } | 
| 370 | 
+ | 
        lu_done(&ofiletab);             /* close output files */ | 
| 371 | 
  | 
        if (raysleft) | 
| 372 | 
  | 
                error(USER, "unexpected EOF on input"); | 
| 387 | 
– | 
        lu_done(&ofiletab);             /* close output files */ | 
| 373 | 
  | 
} |