ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcontrib.c
(Generate patch)

Comparing ray/src/rt/rcontrib.c (file contents):
Revision 2.1 by greg, Sat Jun 9 07:16:47 2012 UTC vs.
Revision 2.16 by greg, Tue Aug 14 22:25:12 2012 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   */
8  
9   #include "rcontrib.h"
10 #include "source.h"
10   #include "otypes.h"
11 < #include "platform.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  
# Line 23 | Line 24 | void   (*trace)() = trace_contrib;
24  
25   int     do_irrad = 0;                   /* compute irradiance? */
26  
27 < int     rand_samp = 0;                  /* pure Monte Carlo sampling? */
27 > int     rand_samp = 1;                  /* pure Monte Carlo sampling? */
28  
29 < double  dstrsrc = 0.0;                  /* square source distribution */
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 */
# Line 43 | Line 44 | double specjitter = 1.;                /* specular sampling jitter *
44  
45   int     backvis = 1;                    /* back face visibility */
46  
47 < int     maxdepth = 8;                   /* maximum recursion depth */
48 < double  minweight = 5e-4;               /* minimum ray weight */
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 */
# Line 61 | Line 62 | int    account;                        /* current accumulation count */
62   RNUMBER raysleft;                       /* number of rays left to trace */
63   long    waitflush;                      /* how long until next flush */
64  
65 < int     lastray = 0;                    /* last ray number sent */
66 < int     lastdone = 0;                   /* last ray output */
65 > RNUMBER lastray = 0;                    /* last ray number sent */
66 > RNUMBER lastdone = 0;                   /* last ray output */
67  
67 /* Close output stream and free record */
68 static void
69 closestream(void *p)
70 {
71        STREAMOUT       *sop = (STREAMOUT *)p;
72        int             status = 0;
73        if (sop->outpipe)
74                status = pclose(sop->ofp);
75        else if (sop->ofp != stdout)
76                status = fclose(sop->ofp);
77        if (status)
78                error(SYSTEM, "error closing output stream");
79        free(p);
80 }
81
82 LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
83
84 #define OF_MODIFIER     01
85 #define OF_BIN          02
86
68   static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
69  
70   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
71  
91 static OBJECT           traset[MAXTSET+1]={0};  /* trace include set */
92
72   /************************** INITIALIZATION ROUTINES ***********************/
73  
95 void
96 tranotify(                      /* record new modifier */
97        OBJECT  obj
98 )
99 {
100        static int      hitlimit = 0;
101        OBJREC          *o = objptr(obj);
102        int             i;
103
104        if (obj == OVOID) {             /* starting over */
105                traset[0] = 0;
106                hitlimit = 0;
107                return;
108        }
109        if (hitlimit || !ismodifier(o->otype))
110                return;
111        for (i = nmods; i-- > 0; )
112                if (!strcmp(o->oname, modname[i])) {
113                        if (traset[0] >= MAXTSET) {
114                                error(WARNING, "too many same-named modifiers");
115                                hitlimit++;
116                                return;         /* should this be fatal? */
117                        }
118                        insertelem(traset, obj);
119                        break;
120                }
121 }
122
123
74   char *
75   formstr(                                /* return format identifier */
76          int  f
# Line 186 | Line 136 | addmodifier(char *modn, char *outf, char *binv, int bi
136   }
137  
138  
139 < /* add modifiers from a file list */
139 > /* Add modifiers from a file list */
140   void
141   addmodfile(char *fname, char *outf, char *binv, int bincnt)
142   {
# Line 208 | Line 158 | quit(                  /* quit program */
158   )
159   {
160          if (nchild > 0)         /* close children if any */
161 <                end_children();
161 >                end_children(code != 0);
162          exit(code);
163   }
164  
# Line 222 | Line 172 | rcinit()
172          if (nproc > MAXPROCESS)
173                  sprintf(errmsg, "too many processes requested -- reducing to %d",
174                                  nproc = MAXPROCESS);
175 <                
176 <        if ((nproc > 1) & (accumulate <= 0))
177 <                zero_record(0);         /* prime our queue to accumulate */
178 <
229 <        if (recover) {                  /* recover previous output? */
230 <                if (accumulate <= 0) {
231 <                        reload_output();
232 <                        if ((nproc > 1) & (accumulate <= 0))
233 <                                queue_modifiers();
234 <                } else
235 <                        recover_output();
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;
# Line 244 | Line 189 | rcinit()
189          if ((account = accumulate) > 1)
190                  raysleft *= accumulate;
191          waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
247                                        /* tracing to sources as well */
248        for (i = 0; i < nsources; i++)
249                source[i].sflags |= SFOLLOW;
192  
193 <        if (nproc == 1 || in_rchild())  /* single process or child */
193 >        if (nproc > 1 && in_rchild())   /* forked child? */
194                  return;                 /* return to main processing loop */
195  
196 <        parental_loop();                /* else run controller */
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  
# Line 265 | Line 219 | trace_contrib(RAY *r)
219          int     bn;
220          RREAL   contr[3];
221  
222 <        if (r->ro == NULL || !inset(traset, r->ro->omod))
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)
228 <                error(CONSISTENCY, "unexpected modifier in trace_contrib()");
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);                /* get bin number */
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);
239 >        raycontrib(contr, r, PRIMARY);          /* compute coefficient */
240          if (contrib)
241 <                multcolor(contr, r->rcol);
241 >                multcolor(contr, r->rcol);      /* -> contribution */
242          addcolor(mp->cbin[bn], contr);
243   }
244  
245  
246 + /* Evaluate irradiance contributions */
247   static void
248 < rayirrad(                       /* compute irradiance rather than radiance */
291 <        RAY *r
292 < )
248 > eval_irrad(FVECT org, FVECT dir)
249   {
250 <        r->rot = 1e-5;                  /* pretend we hit surface */
251 <        VSUM(r->rop, r->rorg, r->rdir, r->rot);
252 <        r->ron[0] = -r->rdir[0];
253 <        r->ron[1] = -r->rdir[1];
254 <        r->ron[2] = -r->rdir[2];
255 <        r->rod = 1.0;
256 <                                        /* compute result */
257 <        r->revf = raytrace;
258 <        (*ofun[Lamb.otype].funp)(&Lamb, r);
259 <        r->revf = rayirrad;
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 >                                        /* pretend we hit surface */
259 >        thisray.rt = thisray.rot = 1e-5;
260 >        thisray.rod = 1.0;
261 >        VCOPY(thisray.ron, dir);
262 >        VSUM(thisray.rop, org, dir, 1e-4);
263 >        samplendx++;                    /* compute result */
264 >        (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
265   }
266  
267  
268 < /* Evaluate ray contributions */
268 > /* Evaluate radiance contributions */
269   static void
270 < eval_ray(FVECT  org, FVECT  dir, double dmax)
270 > eval_rad(FVECT org, FVECT dir, double dmax)
271   {
272          RAY     thisray;
273                                          /* set up ray */
274 +        VCOPY(thisray.rorg, org);
275 +        VCOPY(thisray.rdir, dir);
276 +        thisray.rmax = dmax;
277          rayorigin(&thisray, PRIMARY, NULL, NULL);
314        if (imm_irrad) {
315                VSUM(thisray.rorg, org, dir, 1.1e-4);
316                thisray.rdir[0] = -dir[0];
317                thisray.rdir[1] = -dir[1];
318                thisray.rdir[2] = -dir[2];
319                thisray.rmax = 0.0;
320                thisray.revf = rayirrad;
321        } else {
322                VCOPY(thisray.rorg, org);
323                VCOPY(thisray.rdir, dir);
324                thisray.rmax = dmax;
325        }
278          samplendx++;                    /* call ray evaluation */
279          rayvalue(&thisray);
280   }
# Line 356 | Line 308 | rcontrib()
308          static int      ignore_warning_given = 0;
309          FVECT           orig, direc;
310          double          d;
311 <                                        /* initialize & fork */
311 >                                        /* initialize (& fork more of us) */
312          rcinit();
313                                          /* load rays from stdin & process */
314   #ifdef getc_unlocked
315 <        flockfile(stdin);               /* avoid lock/unlock overhead */
315 >        flockfile(stdin);               /* avoid mutex overhead */
316   #endif
317          while (getvec(orig) == 0 && getvec(direc) == 0) {
318                  d = normalize(direc);
319 <                if ((d == 0.0) & (accumulate != 1)) {
319 >                if (nchild != -1 && (d == 0.0) & (accumulate != 1)) {
320                          if (!ignore_warning_given++)
321                                  error(WARNING,
322                                  "dummy ray(s) ignored during accumulation\n");
# Line 376 | Line 328 | rcontrib()
328                  if (d == 0.0) {                         /* zero ==> flush */
329                          if ((yres <= 0) | (xres <= 0))
330                                  waitflush = 1;          /* flush right after */
331 <                } else {                                /* else compute */
332 <                        eval_ray(orig, direc, lim_dist ? d : 0.0);
331 >                        account = 1;
332 >                } else if (imm_irrad) {                 /* else compute */
333 >                        eval_irrad(orig, direc);
334 >                } else {
335 >                        eval_rad(orig, direc, lim_dist ? d : 0.0);
336                  }
337                  done_contrib();         /* accumulate/output */
338                  ++lastdone;
339                  if (raysleft && !--raysleft)
340                          break;          /* preemptive EOI */
341          }
342 <        if (accumulate <= 0 || account < accumulate) {
342 >        if (nchild != -1 && (accumulate <= 0) | (account < accumulate)) {
343                  if (account < accumulate) {
344                          error(WARNING, "partial accumulation in final record");
345                          accumulate -= account;
# Line 392 | Line 347 | rcontrib()
347                  account = 1;            /* output accumulated totals */
348                  done_contrib();
349          }
350 +        lu_done(&ofiletab);             /* close output files */
351          if (raysleft)
352                  error(USER, "unexpected EOF on input");
397        lu_done(&ofiletab);             /* close output files */
353   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines