ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
Revision: 2.15
Committed: Fri Jul 13 05:07:06 2012 UTC (11 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +2 -1 lines
Log Message:
Added missing assignment of ray intersection normal

File Contents

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