ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.46
Committed: Wed Oct 23 23:51:20 2024 UTC (6 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.45: +13 -7 lines
Log Message:
fix(rcontrib): Added a check that the number of color samples doesn't change after the first modifier is specified

File Contents

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