| 1 | 
greg | 
2.1 | 
#ifndef lint | 
| 2 | 
greg | 
2.20 | 
static const char RCSid[] = "$Id: rcontrib.c,v 2.19 2014/07/19 00:07:03 greg Exp $"; | 
| 3 | 
greg | 
2.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * Accumulate ray contributions for a set of materials | 
| 6 | 
  | 
  | 
 * Initialization and calculation routines | 
| 7 | 
  | 
  | 
 */ | 
| 8 | 
  | 
  | 
 | 
| 9 | 
greg | 
2.20 | 
#include "copyright.h" | 
| 10 | 
  | 
  | 
 | 
| 11 | 
  | 
  | 
#include <ctype.h> | 
| 12 | 
greg | 
2.1 | 
#include "rcontrib.h" | 
| 13 | 
  | 
  | 
#include "otypes.h" | 
| 14 | 
greg | 
2.14 | 
#include "source.h" | 
| 15 | 
greg | 
2.1 | 
 | 
| 16 | 
greg | 
2.2 | 
char    *shm_boundary = NULL;           /* boundary of shared memory */ | 
| 17 | 
  | 
  | 
 | 
| 18 | 
greg | 
2.1 | 
CUBE    thescene;                       /* our scene */ | 
| 19 | 
  | 
  | 
OBJECT  nsceneobjs;                     /* number of objects in our scene */ | 
| 20 | 
  | 
  | 
 | 
| 21 | 
  | 
  | 
int     dimlist[MAXDIM];                /* sampling dimensions */ | 
| 22 | 
  | 
  | 
int     ndims = 0;                      /* number of sampling dimensions */ | 
| 23 | 
  | 
  | 
int     samplendx = 0;                  /* index for this sample */ | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
static void     trace_contrib(RAY *r);  /* our trace callback */ | 
| 26 | 
  | 
  | 
void    (*trace)() = trace_contrib; | 
| 27 | 
  | 
  | 
 | 
| 28 | 
  | 
  | 
int     do_irrad = 0;                   /* compute irradiance? */ | 
| 29 | 
  | 
  | 
 | 
| 30 | 
greg | 
2.3 | 
int     rand_samp = 1;                  /* pure Monte Carlo sampling? */ | 
| 31 | 
greg | 
2.1 | 
 | 
| 32 | 
greg | 
2.8 | 
double  dstrsrc = 0.9;                  /* square source distribution */ | 
| 33 | 
greg | 
2.1 | 
double  shadthresh = .03;               /* shadow threshold */ | 
| 34 | 
  | 
  | 
double  shadcert = .75;                 /* shadow certainty */ | 
| 35 | 
  | 
  | 
int     directrelay = 3;                /* number of source relays */ | 
| 36 | 
  | 
  | 
int     vspretest = 512;                /* virtual source pretest density */ | 
| 37 | 
  | 
  | 
int     directvis = 1;                  /* sources visible? */ | 
| 38 | 
  | 
  | 
double  srcsizerat = .2;                /* maximum ratio source size/dist. */ | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
COLOR   cextinction = BLKCOLOR;         /* global extinction coefficient */ | 
| 41 | 
  | 
  | 
COLOR   salbedo = BLKCOLOR;             /* global scattering albedo */ | 
| 42 | 
  | 
  | 
double  seccg = 0.;                     /* global scattering eccentricity */ | 
| 43 | 
  | 
  | 
double  ssampdist = 0.;                 /* scatter sampling distance */ | 
| 44 | 
  | 
  | 
 | 
| 45 | 
  | 
  | 
double  specthresh = .15;               /* specular sampling threshold */ | 
| 46 | 
  | 
  | 
double  specjitter = 1.;                /* specular sampling jitter */ | 
| 47 | 
  | 
  | 
 | 
| 48 | 
  | 
  | 
int     backvis = 1;                    /* back face visibility */ | 
| 49 | 
  | 
  | 
 | 
| 50 | 
greg | 
2.3 | 
int     maxdepth = -10;                 /* maximum recursion depth */ | 
| 51 | 
  | 
  | 
double  minweight = 2e-3;               /* minimum ray weight */ | 
| 52 | 
greg | 
2.1 | 
 | 
| 53 | 
  | 
  | 
char    *ambfile = NULL;                /* ambient file name */ | 
| 54 | 
  | 
  | 
COLOR   ambval = BLKCOLOR;              /* ambient value */ | 
| 55 | 
  | 
  | 
int     ambvwt = 0;                     /* initial weight for ambient value */ | 
| 56 | 
  | 
  | 
double  ambacc = 0;                     /* ambient accuracy */ | 
| 57 | 
  | 
  | 
int     ambres = 256;                   /* ambient resolution */ | 
| 58 | 
  | 
  | 
int     ambdiv = 350;                   /* ambient divisions */ | 
| 59 | 
  | 
  | 
int     ambssamp = 0;                   /* ambient super-samples */ | 
| 60 | 
  | 
  | 
int     ambounce = 1;                   /* ambient bounces */ | 
| 61 | 
  | 
  | 
char    *amblist[AMBLLEN+1];            /* ambient include/exclude list */ | 
| 62 | 
  | 
  | 
int     ambincl = -1;                   /* include == 1, exclude == 0 */ | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
int     account;                        /* current accumulation count */ | 
| 65 | 
  | 
  | 
RNUMBER raysleft;                       /* number of rays left to trace */ | 
| 66 | 
  | 
  | 
long    waitflush;                      /* how long until next flush */ | 
| 67 | 
  | 
  | 
 | 
| 68 | 
greg | 
2.4 | 
RNUMBER lastray = 0;                    /* last ray number sent */ | 
| 69 | 
  | 
  | 
RNUMBER lastdone = 0;                   /* last ray output */ | 
| 70 | 
greg | 
2.1 | 
 | 
| 71 | 
  | 
  | 
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } | 
| 72 | 
  | 
  | 
 | 
| 73 | 
  | 
  | 
LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */ | 
| 74 | 
  | 
  | 
 | 
| 75 | 
  | 
  | 
/************************** INITIALIZATION ROUTINES ***********************/ | 
| 76 | 
  | 
  | 
 | 
| 77 | 
  | 
  | 
char * | 
| 78 | 
  | 
  | 
formstr(                                /* return format identifier */ | 
| 79 | 
  | 
  | 
        int  f | 
| 80 | 
  | 
  | 
) | 
| 81 | 
  | 
  | 
{ | 
| 82 | 
  | 
  | 
        switch (f) { | 
| 83 | 
  | 
  | 
        case 'a': return("ascii"); | 
| 84 | 
  | 
  | 
        case 'f': return("float"); | 
| 85 | 
  | 
  | 
        case 'd': return("double"); | 
| 86 | 
  | 
  | 
        case 'c': return(COLRFMT); | 
| 87 | 
  | 
  | 
        } | 
| 88 | 
  | 
  | 
        return("unknown"); | 
| 89 | 
  | 
  | 
} | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
 | 
| 92 | 
  | 
  | 
/* Add modifier to our list to track */ | 
| 93 | 
  | 
  | 
MODCONT * | 
| 94 | 
greg | 
2.20 | 
addmodifier(char *modn, char *outf, char *prms, char *binv, int bincnt) | 
| 95 | 
greg | 
2.1 | 
{ | 
| 96 | 
  | 
  | 
        LUENT   *lep = lu_find(&modconttab,modn); | 
| 97 | 
  | 
  | 
        MODCONT *mp; | 
| 98 | 
  | 
  | 
        EPNODE  *ebinv; | 
| 99 | 
  | 
  | 
        int     i; | 
| 100 | 
  | 
  | 
         | 
| 101 | 
  | 
  | 
        if (lep->data != NULL) { | 
| 102 | 
  | 
  | 
                sprintf(errmsg, "duplicate modifier '%s'", modn); | 
| 103 | 
  | 
  | 
                error(USER, errmsg); | 
| 104 | 
  | 
  | 
        } | 
| 105 | 
  | 
  | 
        if (nmods >= MAXMODLIST) | 
| 106 | 
  | 
  | 
                error(INTERNAL, "too many modifiers"); | 
| 107 | 
  | 
  | 
        modname[nmods++] = modn;        /* XXX assumes static string */ | 
| 108 | 
  | 
  | 
        lep->key = modn;                /* XXX assumes static string */ | 
| 109 | 
  | 
  | 
        if (binv == NULL) | 
| 110 | 
  | 
  | 
                binv = "0";             /* use single bin if unspecified */ | 
| 111 | 
  | 
  | 
        ebinv = eparse(binv); | 
| 112 | 
  | 
  | 
        if (ebinv->type == NUM) {       /* check value if constant */ | 
| 113 | 
  | 
  | 
                bincnt = (int)(evalue(ebinv) + 1.5); | 
| 114 | 
  | 
  | 
                if (bincnt != 1) { | 
| 115 | 
  | 
  | 
                        sprintf(errmsg, "illegal non-zero constant for bin (%s)", | 
| 116 | 
  | 
  | 
                                        binv); | 
| 117 | 
  | 
  | 
                        error(USER, errmsg); | 
| 118 | 
  | 
  | 
                } | 
| 119 | 
  | 
  | 
        } else if (bincnt <= 0) { | 
| 120 | 
  | 
  | 
                sprintf(errmsg, | 
| 121 | 
  | 
  | 
                        "unspecified or illegal bin count for modifier '%s'", | 
| 122 | 
  | 
  | 
                                modn); | 
| 123 | 
  | 
  | 
                error(USER, errmsg); | 
| 124 | 
  | 
  | 
        } | 
| 125 | 
  | 
  | 
                                        /* initialize results holder */ | 
| 126 | 
  | 
  | 
        mp = (MODCONT *)malloc(sizeof(MODCONT)+sizeof(DCOLOR)*(bincnt-1)); | 
| 127 | 
  | 
  | 
        if (mp == NULL) | 
| 128 | 
  | 
  | 
                error(SYSTEM, "out of memory in addmodifier"); | 
| 129 | 
  | 
  | 
        mp->outspec = outf;             /* XXX assumes static string */ | 
| 130 | 
  | 
  | 
        mp->modname = modn;             /* XXX assumes static string */ | 
| 131 | 
greg | 
2.20 | 
        mp->params = prms; | 
| 132 | 
greg | 
2.1 | 
        mp->binv = ebinv; | 
| 133 | 
  | 
  | 
        mp->nbins = bincnt; | 
| 134 | 
  | 
  | 
        memset(mp->cbin, 0, sizeof(DCOLOR)*bincnt); | 
| 135 | 
  | 
  | 
                                        /* allocate output streams */ | 
| 136 | 
  | 
  | 
        for (i = bincnt; i-- > 0; ) | 
| 137 | 
  | 
  | 
                getostream(mp->outspec, mp->modname, i, 1); | 
| 138 | 
  | 
  | 
        lep->data = (char *)mp; | 
| 139 | 
  | 
  | 
        return(mp); | 
| 140 | 
  | 
  | 
} | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
 | 
| 143 | 
greg | 
2.10 | 
/* Add modifiers from a file list */ | 
| 144 | 
greg | 
2.1 | 
void | 
| 145 | 
greg | 
2.20 | 
addmodfile(char *fname, char *outf, char *prms, char *binv, int bincnt) | 
| 146 | 
greg | 
2.1 | 
{ | 
| 147 | 
  | 
  | 
        char    *mname[MAXMODLIST]; | 
| 148 | 
  | 
  | 
        int     i; | 
| 149 | 
  | 
  | 
                                        /* find the file & store strings */ | 
| 150 | 
  | 
  | 
        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { | 
| 151 | 
  | 
  | 
                sprintf(errmsg, "cannot find modifier file '%s'", fname); | 
| 152 | 
  | 
  | 
                error(SYSTEM, errmsg); | 
| 153 | 
  | 
  | 
        } | 
| 154 | 
  | 
  | 
        for (i = 0; mname[i]; i++)      /* add each one */ | 
| 155 | 
greg | 
2.20 | 
                addmodifier(mname[i], outf, prms, binv, bincnt); | 
| 156 | 
greg | 
2.1 | 
} | 
| 157 | 
  | 
  | 
 | 
| 158 | 
  | 
  | 
 | 
| 159 | 
  | 
  | 
void | 
| 160 | 
  | 
  | 
quit(                   /* quit program */ | 
| 161 | 
  | 
  | 
        int  code | 
| 162 | 
  | 
  | 
) | 
| 163 | 
  | 
  | 
{ | 
| 164 | 
  | 
  | 
        if (nchild > 0)         /* close children if any */ | 
| 165 | 
greg | 
2.10 | 
                end_children(code != 0); | 
| 166 | 
greg | 
2.1 | 
        exit(code); | 
| 167 | 
  | 
  | 
} | 
| 168 | 
  | 
  | 
 | 
| 169 | 
  | 
  | 
 | 
| 170 | 
  | 
  | 
/* Initialize our process(es) */ | 
| 171 | 
  | 
  | 
static void | 
| 172 | 
  | 
  | 
rcinit() | 
| 173 | 
  | 
  | 
{ | 
| 174 | 
  | 
  | 
        int     i; | 
| 175 | 
  | 
  | 
 | 
| 176 | 
  | 
  | 
        if (nproc > MAXPROCESS) | 
| 177 | 
  | 
  | 
                sprintf(errmsg, "too many processes requested -- reducing to %d", | 
| 178 | 
  | 
  | 
                                nproc = MAXPROCESS); | 
| 179 | 
greg | 
2.2 | 
        if (nproc > 1) { | 
| 180 | 
  | 
  | 
                preload_objs();         /* preload auxiliary data */ | 
| 181 | 
  | 
  | 
                                        /* set shared memory boundary */ | 
| 182 | 
  | 
  | 
                shm_boundary = strcpy((char *)malloc(16), "SHM_BOUNDARY"); | 
| 183 | 
  | 
  | 
        } | 
| 184 | 
greg | 
2.14 | 
        for (i = 0; i < nsources; i++)  /* tracing to sources as well */ | 
| 185 | 
  | 
  | 
                source[i].sflags |= SFOLLOW; | 
| 186 | 
greg | 
2.1 | 
        if (yres > 0) {                 /* set up flushing & ray counts */ | 
| 187 | 
  | 
  | 
                if (xres > 0) | 
| 188 | 
  | 
  | 
                        raysleft = (RNUMBER)xres*yres; | 
| 189 | 
  | 
  | 
                else | 
| 190 | 
  | 
  | 
                        raysleft = yres; | 
| 191 | 
  | 
  | 
        } else | 
| 192 | 
  | 
  | 
                raysleft = 0; | 
| 193 | 
  | 
  | 
        if ((account = accumulate) > 1) | 
| 194 | 
  | 
  | 
                raysleft *= accumulate; | 
| 195 | 
  | 
  | 
        waitflush = (yres > 0) & (xres > 1) ? 0 : xres; | 
| 196 | 
  | 
  | 
 | 
| 197 | 
greg | 
2.5 | 
        if (nproc > 1 && in_rchild())   /* forked child? */ | 
| 198 | 
greg | 
2.1 | 
                return;                 /* return to main processing loop */ | 
| 199 | 
  | 
  | 
 | 
| 200 | 
greg | 
2.5 | 
        if (recover) {                  /* recover previous output? */ | 
| 201 | 
greg | 
2.8 | 
                if (accumulate <= 0) | 
| 202 | 
greg | 
2.5 | 
                        reload_output(); | 
| 203 | 
greg | 
2.8 | 
                else | 
| 204 | 
greg | 
2.5 | 
                        recover_output(); | 
| 205 | 
  | 
  | 
        } | 
| 206 | 
  | 
  | 
        if (nproc == 1)                 /* single process? */ | 
| 207 | 
  | 
  | 
                return; | 
| 208 | 
greg | 
2.8 | 
                                        /* else run appropriate controller */ | 
| 209 | 
  | 
  | 
        if (accumulate <= 0) | 
| 210 | 
  | 
  | 
                feeder_loop(); | 
| 211 | 
  | 
  | 
        else | 
| 212 | 
  | 
  | 
                parental_loop(); | 
| 213 | 
greg | 
2.1 | 
        quit(0);                        /* parent musn't return! */ | 
| 214 | 
  | 
  | 
} | 
| 215 | 
  | 
  | 
 | 
| 216 | 
  | 
  | 
/************************** MAIN CALCULATION PROCESS ***********************/ | 
| 217 | 
  | 
  | 
 | 
| 218 | 
greg | 
2.20 | 
/* Set parameters for current bin evaluation */ | 
| 219 | 
  | 
  | 
static void | 
| 220 | 
  | 
  | 
set_eparams(char *prms) | 
| 221 | 
  | 
  | 
{ | 
| 222 | 
  | 
  | 
        char    vname[64]; | 
| 223 | 
  | 
  | 
        double  value; | 
| 224 | 
  | 
  | 
        char    *cps, *cpd; | 
| 225 | 
  | 
  | 
                                        /* assign each variable */ | 
| 226 | 
  | 
  | 
        for (cps = prms; *cps; cps++) { | 
| 227 | 
  | 
  | 
                if (isspace(*cps)) | 
| 228 | 
  | 
  | 
                        continue; | 
| 229 | 
  | 
  | 
                if (!isalpha(*cps)) | 
| 230 | 
  | 
  | 
                        goto bad_params; | 
| 231 | 
  | 
  | 
                cpd = vname; | 
| 232 | 
  | 
  | 
                while (*cps && (*cps != '=') & !isspace(*cps)) { | 
| 233 | 
  | 
  | 
                        if (!isid(*cps)) | 
| 234 | 
  | 
  | 
                                goto bad_params; | 
| 235 | 
  | 
  | 
                        *cpd++ = *cps++; | 
| 236 | 
  | 
  | 
                } | 
| 237 | 
  | 
  | 
                if (cpd == vname) | 
| 238 | 
  | 
  | 
                        goto bad_params; | 
| 239 | 
  | 
  | 
                *cpd = '\0'; | 
| 240 | 
  | 
  | 
                while (isspace(*cps)) cps++; | 
| 241 | 
  | 
  | 
                if (*cps++ != '=') | 
| 242 | 
  | 
  | 
                        goto bad_params; | 
| 243 | 
  | 
  | 
                value = atof(cps); | 
| 244 | 
  | 
  | 
                if ((cps = fskip(cps)) == NULL) | 
| 245 | 
  | 
  | 
                        goto bad_params; | 
| 246 | 
  | 
  | 
                while (isspace(*cps)) cps++; | 
| 247 | 
  | 
  | 
                cps += (*cps == ',') | (*cps == ';'); | 
| 248 | 
  | 
  | 
                varset(vname, '=', value); | 
| 249 | 
  | 
  | 
        } | 
| 250 | 
  | 
  | 
        return; | 
| 251 | 
  | 
  | 
bad_params: | 
| 252 | 
  | 
  | 
        sprintf(errmsg, "bad parameter list '%s'", prms); | 
| 253 | 
  | 
  | 
        error(USER, errmsg); | 
| 254 | 
  | 
  | 
} | 
| 255 | 
  | 
  | 
 | 
| 256 | 
greg | 
2.1 | 
/* Our trace call to sum contributions */ | 
| 257 | 
  | 
  | 
static void | 
| 258 | 
  | 
  | 
trace_contrib(RAY *r) | 
| 259 | 
  | 
  | 
{ | 
| 260 | 
greg | 
2.20 | 
        static char     *last_params = NULL; | 
| 261 | 
greg | 
2.1 | 
        MODCONT *mp; | 
| 262 | 
greg | 
2.19 | 
        double  bval; | 
| 263 | 
greg | 
2.1 | 
        int     bn; | 
| 264 | 
  | 
  | 
        RREAL   contr[3]; | 
| 265 | 
  | 
  | 
 | 
| 266 | 
greg | 
2.6 | 
        if (r->ro == NULL || r->ro->omod == OVOID) | 
| 267 | 
greg | 
2.1 | 
                return; | 
| 268 | 
greg | 
2.20 | 
                                                /* shadow ray not on source? */ | 
| 269 | 
  | 
  | 
        if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) | 
| 270 | 
  | 
  | 
                return; | 
| 271 | 
greg | 
2.1 | 
 | 
| 272 | 
  | 
  | 
        mp = (MODCONT *)lu_find(&modconttab,objptr(r->ro->omod)->oname)->data; | 
| 273 | 
  | 
  | 
 | 
| 274 | 
greg | 
2.6 | 
        if (mp == NULL)                         /* not in our list? */ | 
| 275 | 
  | 
  | 
                return; | 
| 276 | 
greg | 
2.1 | 
 | 
| 277 | 
greg | 
2.14 | 
        worldfunc(RCCONTEXT, r);                /* else get bin number */ | 
| 278 | 
greg | 
2.20 | 
        if (mp->params != last_params) | 
| 279 | 
  | 
  | 
                set_eparams(last_params = (char *)mp->params); | 
| 280 | 
greg | 
2.19 | 
        if ((bval = evalue(mp->binv)) <= -.5) | 
| 281 | 
  | 
  | 
                return;                         /* silently ignore */ | 
| 282 | 
  | 
  | 
        if ((bn = (int)(bval + .5)) >= mp->nbins) { | 
| 283 | 
greg | 
2.1 | 
                error(WARNING, "bad bin number (ignored)"); | 
| 284 | 
  | 
  | 
                return; | 
| 285 | 
  | 
  | 
        } | 
| 286 | 
greg | 
2.14 | 
        raycontrib(contr, r, PRIMARY);          /* compute coefficient */ | 
| 287 | 
greg | 
2.1 | 
        if (contrib) | 
| 288 | 
greg | 
2.14 | 
                multcolor(contr, r->rcol);      /* -> contribution */ | 
| 289 | 
greg | 
2.1 | 
        addcolor(mp->cbin[bn], contr); | 
| 290 | 
  | 
  | 
} | 
| 291 | 
  | 
  | 
 | 
| 292 | 
  | 
  | 
 | 
| 293 | 
greg | 
2.7 | 
/* Evaluate irradiance contributions */ | 
| 294 | 
greg | 
2.1 | 
static void | 
| 295 | 
greg | 
2.7 | 
eval_irrad(FVECT org, FVECT dir) | 
| 296 | 
greg | 
2.1 | 
{ | 
| 297 | 
greg | 
2.7 | 
        RAY     thisray; | 
| 298 | 
  | 
  | 
 | 
| 299 | 
  | 
  | 
        VSUM(thisray.rorg, org, dir, 1.1e-4); | 
| 300 | 
  | 
  | 
        thisray.rdir[0] = -dir[0]; | 
| 301 | 
  | 
  | 
        thisray.rdir[1] = -dir[1]; | 
| 302 | 
  | 
  | 
        thisray.rdir[2] = -dir[2]; | 
| 303 | 
  | 
  | 
        thisray.rmax = 0.0; | 
| 304 | 
  | 
  | 
        rayorigin(&thisray, PRIMARY, NULL, NULL); | 
| 305 | 
greg | 
2.16 | 
                                        /* pretend we hit surface */ | 
| 306 | 
  | 
  | 
        thisray.rt = thisray.rot = 1e-5; | 
| 307 | 
greg | 
2.7 | 
        thisray.rod = 1.0; | 
| 308 | 
greg | 
2.15 | 
        VCOPY(thisray.ron, dir); | 
| 309 | 
greg | 
2.7 | 
        VSUM(thisray.rop, org, dir, 1e-4); | 
| 310 | 
  | 
  | 
        samplendx++;                    /* compute result */ | 
| 311 | 
  | 
  | 
        (*ofun[Lamb.otype].funp)(&Lamb, &thisray); | 
| 312 | 
greg | 
2.1 | 
} | 
| 313 | 
  | 
  | 
 | 
| 314 | 
  | 
  | 
 | 
| 315 | 
greg | 
2.7 | 
/* Evaluate radiance contributions */ | 
| 316 | 
greg | 
2.1 | 
static void | 
| 317 | 
greg | 
2.7 | 
eval_rad(FVECT org, FVECT dir, double dmax) | 
| 318 | 
greg | 
2.1 | 
{ | 
| 319 | 
  | 
  | 
        RAY     thisray; | 
| 320 | 
  | 
  | 
                                        /* set up ray */ | 
| 321 | 
greg | 
2.7 | 
        VCOPY(thisray.rorg, org); | 
| 322 | 
  | 
  | 
        VCOPY(thisray.rdir, dir); | 
| 323 | 
  | 
  | 
        thisray.rmax = dmax; | 
| 324 | 
greg | 
2.1 | 
        rayorigin(&thisray, PRIMARY, NULL, NULL); | 
| 325 | 
  | 
  | 
        samplendx++;                    /* call ray evaluation */ | 
| 326 | 
  | 
  | 
        rayvalue(&thisray); | 
| 327 | 
  | 
  | 
} | 
| 328 | 
  | 
  | 
 | 
| 329 | 
  | 
  | 
 | 
| 330 | 
  | 
  | 
/* Accumulate and/or output ray contributions (child or only process) */ | 
| 331 | 
  | 
  | 
static void | 
| 332 | 
  | 
  | 
done_contrib() | 
| 333 | 
  | 
  | 
{ | 
| 334 | 
  | 
  | 
        MODCONT *mp; | 
| 335 | 
  | 
  | 
        int     i; | 
| 336 | 
  | 
  | 
 | 
| 337 | 
  | 
  | 
        if (account <= 0 || --account) | 
| 338 | 
  | 
  | 
                return;                 /* not time yet */ | 
| 339 | 
  | 
  | 
 | 
| 340 | 
  | 
  | 
        for (i = 0; i < nmods; i++) {   /* output records & clear */ | 
| 341 | 
  | 
  | 
                mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; | 
| 342 | 
  | 
  | 
                mod_output(mp); | 
| 343 | 
  | 
  | 
                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); | 
| 344 | 
  | 
  | 
        } | 
| 345 | 
  | 
  | 
        end_record();                   /* end lines & flush if time */ | 
| 346 | 
  | 
  | 
 | 
| 347 | 
  | 
  | 
        account = accumulate;           /* reset accumulation counter */ | 
| 348 | 
  | 
  | 
} | 
| 349 | 
  | 
  | 
 | 
| 350 | 
  | 
  | 
 | 
| 351 | 
  | 
  | 
/* Principal calculation loop (called by main) */ | 
| 352 | 
  | 
  | 
void | 
| 353 | 
  | 
  | 
rcontrib() | 
| 354 | 
  | 
  | 
{ | 
| 355 | 
  | 
  | 
        static int      ignore_warning_given = 0; | 
| 356 | 
  | 
  | 
        FVECT           orig, direc; | 
| 357 | 
  | 
  | 
        double          d; | 
| 358 | 
greg | 
2.2 | 
                                        /* initialize (& fork more of us) */ | 
| 359 | 
greg | 
2.1 | 
        rcinit(); | 
| 360 | 
  | 
  | 
                                        /* load rays from stdin & process */ | 
| 361 | 
  | 
  | 
#ifdef getc_unlocked | 
| 362 | 
greg | 
2.2 | 
        flockfile(stdin);               /* avoid mutex overhead */ | 
| 363 | 
greg | 
2.1 | 
#endif | 
| 364 | 
  | 
  | 
        while (getvec(orig) == 0 && getvec(direc) == 0) { | 
| 365 | 
  | 
  | 
                d = normalize(direc); | 
| 366 | 
greg | 
2.17 | 
                if (nchild != -1 && (d == 0.0) & (accumulate == 0)) { | 
| 367 | 
greg | 
2.1 | 
                        if (!ignore_warning_given++) | 
| 368 | 
  | 
  | 
                                error(WARNING, | 
| 369 | 
  | 
  | 
                                "dummy ray(s) ignored during accumulation\n"); | 
| 370 | 
  | 
  | 
                        continue; | 
| 371 | 
  | 
  | 
                } | 
| 372 | 
  | 
  | 
                if (lastray+1 < lastray) | 
| 373 | 
  | 
  | 
                        lastray = lastdone = 0; | 
| 374 | 
  | 
  | 
                ++lastray; | 
| 375 | 
  | 
  | 
                if (d == 0.0) {                         /* zero ==> flush */ | 
| 376 | 
greg | 
2.18 | 
                        if ((yres <= 0) | (xres <= 1)) | 
| 377 | 
greg | 
2.17 | 
                                waitflush = 1;          /* flush after */ | 
| 378 | 
  | 
  | 
                        if (nchild == -1) | 
| 379 | 
  | 
  | 
                                account = 1; | 
| 380 | 
greg | 
2.7 | 
                } else if (imm_irrad) {                 /* else compute */ | 
| 381 | 
  | 
  | 
                        eval_irrad(orig, direc); | 
| 382 | 
  | 
  | 
                } else { | 
| 383 | 
  | 
  | 
                        eval_rad(orig, direc, lim_dist ? d : 0.0); | 
| 384 | 
greg | 
2.1 | 
                } | 
| 385 | 
  | 
  | 
                done_contrib();         /* accumulate/output */ | 
| 386 | 
  | 
  | 
                ++lastdone; | 
| 387 | 
  | 
  | 
                if (raysleft && !--raysleft) | 
| 388 | 
  | 
  | 
                        break;          /* preemptive EOI */ | 
| 389 | 
  | 
  | 
        } | 
| 390 | 
greg | 
2.11 | 
        if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) { | 
| 391 | 
greg | 
2.1 | 
                if (account < accumulate) { | 
| 392 | 
  | 
  | 
                        error(WARNING, "partial accumulation in final record"); | 
| 393 | 
  | 
  | 
                        accumulate -= account; | 
| 394 | 
  | 
  | 
                } | 
| 395 | 
  | 
  | 
                account = 1;            /* output accumulated totals */ | 
| 396 | 
  | 
  | 
                done_contrib(); | 
| 397 | 
  | 
  | 
        } | 
| 398 | 
greg | 
2.9 | 
        lu_done(&ofiletab);             /* close output files */ | 
| 399 | 
greg | 
2.1 | 
        if (raysleft) | 
| 400 | 
  | 
  | 
                error(USER, "unexpected EOF on input"); | 
| 401 | 
  | 
  | 
} |