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

Comparing ray/src/rt/rtrace.c (file contents):
Revision 2.85 by greg, Sun Mar 1 05:38:22 2020 UTC vs.
Revision 2.106 by greg, Mon Feb 6 22:40:21 2023 UTC

# Line 45 | Line 45 | extern int  traincl;                   /* include == 1, exclude == 0 *
45   extern int  hresolu;                    /* horizontal resolution */
46   extern int  vresolu;                    /* vertical resolution */
47  
48 < static int  castonly = 0;
48 > extern int  castonly;                   /* only doing ray-casting? */
49  
50   #ifndef  MAXTSET
51   #define  MAXTSET        8191            /* maximum number in trace set */
52   #endif
53   OBJECT  traset[MAXTSET+1]={0};          /* trace include/exclude set */
54  
55 + static int  Tflag = 0;                  /* source tracing enabled? */
56 +
57   static RAY  thisray;                    /* for our convenience */
58  
59 + static FILE  *inpfp = NULL;             /* input stream pointer */
60 +
61 + static FVECT    *inp_queue = NULL;      /* ray input queue if flushing */
62 + static int      inp_qpos = 0;           /* next ray to return */
63 + static int      inp_qend = 0;           /* number of rays in this work group */
64 +
65   typedef void putf_t(RREAL *v, int n);
66   static putf_t puta, putd, putf, putrgbe;
67  
# Line 62 | Line 70 | static oputf_t  oputo, oputd, oputv, oputV, oputl, opu
70                  oputr, oputR, oputx, oputX, oputn, oputN, oputs,
71                  oputw, oputW, oputm, oputM, oputtilde;
72  
65 static void setoutput(char *vs);
73   extern void tranotify(OBJECT obj);
74 + static int is_fifo(FILE *fp);
75   static void bogusray(void);
76   static void raycast(RAY *r);
77   static void rayirrad(RAY *r);
78   static void rtcompute(FVECT org, FVECT dir, double dmax);
79   static int printvals(RAY *r);
80   static int getvec(FVECT vec, int fmt, FILE *fp);
81 + static int iszerovec(const FVECT vec);
82 + static double nextray(FVECT org, FVECT dir);
83   static void tabin(RAY *r);
84   static void ourtrace(RAY *r);
85  
# Line 84 | Line 94 | quit(                  /* quit program */
94   {
95          if (ray_pnprocs > 0)    /* close children if any */
96                  ray_pclose(0);          
97 +        else if (ray_pnprocs < 0)
98 +                _exit(code);    /* avoid flush() in child */
99   #ifndef  NON_POSIX
100 <        else if (!ray_pnprocs) {
100 >        else {
101                  headclean();    /* delete header file */
102                  pfclean();      /* clean up persist files */
103          }
# Line 94 | Line 106 | quit(                  /* quit program */
106   }
107  
108  
109 < char *
109 > const char *
110   formstr(                                /* return format identifier */
111          int  f
112   )
# Line 109 | Line 121 | formstr(                               /* return format identifier */
121   }
122  
123  
124 < extern void
124 > static void
125 > trace_sources(void)                     /* trace rays to light sources, also */
126 > {
127 >        int     sn;
128 >
129 >        for (sn = 0; sn < nsources; sn++)
130 >                source[sn].sflags |= SFOLLOW;
131 > }
132 >
133 >
134 > void
135   rtrace(                         /* trace rays from file */
136          char  *fname,
137          int  nproc
# Line 124 | Line 146 | rtrace(                                /* trace rays from file */
146          FVECT  orig, direc;
147                                          /* set up input */
148          if (fname == NULL)
149 <                fp = stdin;
150 <        else if ((fp = fopen(fname, "r")) == NULL) {
149 >                inpfp = stdin;
150 >        else if ((inpfp = fopen(fname, "r")) == NULL) {
151                  sprintf(errmsg, "cannot open input file \"%s\"", fname);
152                  error(SYSTEM, errmsg);
153          }
154 + #ifdef getc_unlocked
155 +        flockfile(inpfp);               /* avoid lock/unlock overhead */
156 +        flockfile(stdout);
157 + #endif
158          if (inform != 'a')
159 <                SET_FILE_BINARY(fp);
159 >                SET_FILE_BINARY(inpfp);
160                                          /* set up output */
161 <        setoutput(outvals);
136 <        if (imm_irrad)
137 <                castonly = 0;
138 <        else if (castonly)
161 >        if (castonly || every_out[0] != NULL)
162                  nproc = 1;              /* don't bother multiprocessing */
163 +        if (every_out[0] != NULL) {
164 +                trace = ourtrace;       /* enable full tree tracing */
165 +                if (Tflag)              /* light sources, too? */
166 +                        trace_sources();
167 +        }
168          if ((nextflush > 0) & (nproc > nextflush)) {
169                  error(WARNING, "reducing number of processes to match flush interval");
170                  nproc = nextflush;
171          }
144        switch (outform) {
145        case 'a': putreal = puta; break;
146        case 'f': putreal = putf; break;
147        case 'd': putreal = putd; break;
148        case 'c':
149                if (outvals[0] && (outvals[1] || !strchr("vrx", outvals[0])))
150                        error(USER, "color format only with -ov, -or, -ox");
151                putreal = putrgbe; break;
152        default:
153                error(CONSISTENCY, "botched output format");
154        }
172          if (nproc > 1) {                /* start multiprocessing */
173                  ray_popen(nproc);
174                  ray_fifo_out = printvals;
# Line 162 | Line 179 | rtrace(                                /* trace rays from file */
179                  else
180                          fflush(stdout);
181          }
182 <                                        /* process file */
183 <        while (getvec(orig, inform, fp) == 0 &&
184 <                        getvec(direc, inform, fp) == 0) {
168 <
169 <                d = normalize(direc);
170 <                if (d == 0.0) {                         /* zero ==> flush */
182 >                                        /* process input rays */
183 >        while ((d = nextray(orig, direc)) >= 0.0) {
184 >                if (d == 0.0) {                         /* flush request? */
185                          if (something2flush) {
186                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
187                                          error(USER, "child(ren) died");
188                                  bogusray();
189                                  fflush(stdout);
176                                something2flush = 0;
190                                  nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
191 +                                something2flush = 0;
192                          } else
193                                  bogusray();
194                  } else {                                /* compute and print */
195                          rtcompute(orig, direc, lim_dist ? d : 0.0);
196 <                                                        /* flush if time */
183 <                        if (!--nextflush) {
196 >                        if (!--nextflush) {             /* flush if time */
197                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
198                                          error(USER, "child(ren) died");
199                                  fflush(stdout);
# Line 198 | Line 211 | rtrace(                                /* trace rays from file */
211                          error(USER, "unable to complete processing");
212                  ray_pclose(0);
213          }
214 +        if (vcount)
215 +                error(WARNING, "unexpected EOF on input");
216          if (fflush(stdout) < 0)
217                  error(SYSTEM, "write error");
218 <        if (vcount)
219 <                error(USER, "unexpected EOF on input");
220 <        if (fname != NULL)
221 <                fclose(fp);
218 >        if (fname != NULL) {
219 >                fclose(inpfp);
220 >                inpfp = NULL;
221 >        }
222 >        nextray(NULL, NULL);
223   }
224  
225  
226 < static void
227 < trace_sources(void)                     /* trace rays to light sources, also */
226 > int
227 > setrtoutput(void)                       /* set up output tables, return #comp */
228   {
229 <        int     sn;
214 <        
215 <        for (sn = 0; sn < nsources; sn++)
216 <                source[sn].sflags |= SFOLLOW;
217 < }
218 <
219 <
220 < static void
221 < setoutput(                              /* set up output tables */
222 <        char  *vs
223 < )
224 < {
229 >        char  *vs = outvals;
230          oputf_t **table = ray_out;
231 +        int  ncomp = 0;
232  
233 <        castonly = 1;
234 <        while (*vs)
235 <                switch (*vs++) {
233 >        if (!*vs)
234 >                error(USER, "empty output specification");
235 >
236 >        switch (outform) {      /* make sure (*putreal)() calls someone! */
237 >        case 'a': putreal = puta; break;
238 >        case 'f': putreal = putf; break;
239 >        case 'd': putreal = putd; break;
240 >        case 'c':
241 >                if (outvals[1] || !strchr("vrx", outvals[0]))
242 >                        error(USER, "color format only with -ov, -or, -ox");
243 >                putreal = putrgbe; break;
244 >        default:
245 >                error(CONSISTENCY, "botched output format");
246 >        }
247 >        castonly = 1;                   /* sets castonly as side-effect */
248 >        do
249 >                switch (*vs) {
250                  case 'T':                               /* trace sources */
251 <                        if (!*vs) break;
232 <                        trace_sources();
251 >                        Tflag++;
252                          /* fall through */
253                  case 't':                               /* trace */
254 <                        if (!*vs) break;
254 >                        if (!vs[1]) break;
255                          *table = NULL;
256                          table = every_out;
238                        trace = ourtrace;
257                          castonly = 0;
258                          break;
259                  case 'o':                               /* origin */
260                          *table++ = oputo;
261 +                        ncomp += 3;
262                          break;
263                  case 'd':                               /* direction */
264                          *table++ = oputd;
265 +                        ncomp += 3;
266                          break;
267                  case 'r':                               /* reflected contrib. */
268                          *table++ = oputr;
269 +                        ncomp += 3;
270                          castonly = 0;
271                          break;
272                  case 'R':                               /* reflected distance */
273                          *table++ = oputR;
274 +                        ncomp++;
275                          castonly = 0;
276                          break;
277                  case 'x':                               /* xmit contrib. */
278                          *table++ = oputx;
279 +                        ncomp += 3;
280                          castonly = 0;
281                          break;
282                  case 'X':                               /* xmit distance */
283                          *table++ = oputX;
284 +                        ncomp++;
285                          castonly = 0;
286                          break;
287                  case 'v':                               /* value */
288                          *table++ = oputv;
289 +                        ncomp += 3;
290                          castonly = 0;
291                          break;
292                  case 'V':                               /* contribution */
293                          *table++ = oputV;
294 +                        ncomp += 3;
295 +                        castonly = 0;
296                          if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
297                                  error(WARNING,
298                                          "-otV accuracy depends on -aa 0 -as 0");
299                          break;
300                  case 'l':                               /* effective distance */
301                          *table++ = oputl;
302 +                        ncomp++;
303                          castonly = 0;
304                          break;
305                  case 'c':                               /* local coordinates */
306                          *table++ = oputc;
307 +                        ncomp += 2;
308                          break;
309                  case 'L':                               /* single ray length */
310                          *table++ = oputL;
311 +                        ncomp++;
312                          break;
313                  case 'p':                               /* point */
314                          *table++ = oputp;
315 +                        ncomp += 3;
316                          break;
317                  case 'n':                               /* perturbed normal */
318                          *table++ = oputn;
319 +                        ncomp += 3;
320                          castonly = 0;
321                          break;
322                  case 'N':                               /* unperturbed normal */
323                          *table++ = oputN;
324 +                        ncomp += 3;
325                          break;
326                  case 's':                               /* surface */
327                          *table++ = oputs;
328 +                        ncomp++;
329                          break;
330                  case 'w':                               /* weight */
331                          *table++ = oputw;
332 +                        ncomp++;
333                          break;
334                  case 'W':                               /* coefficient */
335                          *table++ = oputW;
336 +                        ncomp += 3;
337                          castonly = 0;
338                          if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0))
339                                  error(WARNING,
# Line 305 | Line 341 | setoutput(                             /* set up output tables */
341                          break;
342                  case 'm':                               /* modifier */
343                          *table++ = oputm;
344 +                        ncomp++;
345                          break;
346                  case 'M':                               /* material */
347                          *table++ = oputM;
348 +                        ncomp++;
349                          break;
350                  case '~':                               /* tilde */
351                          *table++ = oputtilde;
352                          break;
353 +                default:
354 +                        sprintf(errmsg, "unrecognized output option '%c'", *vs);
355 +                        error(USER, errmsg);
356                  }
357 +        while (*++vs);
358 +
359          *table = NULL;
360 +        if (*every_out != NULL)
361 +                ncomp = 0;
362                                                          /* compatibility */
363 +        if ((do_irrad | imm_irrad) && castonly)
364 +                error(USER, "-I+ and -i+ options require some value output");
365          for (table = ray_out; *table != NULL; table++) {
366                  if ((*table == oputV) | (*table == oputW))
367                          error(WARNING, "-oVW options require trace mode");
# Line 323 | Line 370 | setoutput(                             /* set up output tables */
370                                  (*table == oputx) | (*table == oputX))
371                          error(WARNING, "-orRxX options incompatible with -I+ and -i+");
372          }
373 +        return(ncomp);
374   }
375  
376  
# Line 377 | Line 425 | rtcompute(                     /* compute and print ray value(s) */
425   )
426   {
427                                          /* set up ray */
380        rayorigin(&thisray, PRIMARY, NULL, NULL);
428          if (imm_irrad) {
429                  VSUM(thisray.rorg, org, dir, 1.1e-4);
430                  thisray.rdir[0] = -dir[0];
431                  thisray.rdir[1] = -dir[1];
432                  thisray.rdir[2] = -dir[2];
433                  thisray.rmax = 0.0;
387                thisray.revf = rayirrad;
434          } else {
435                  VCOPY(thisray.rorg, org);
436                  VCOPY(thisray.rdir, dir);
437                  thisray.rmax = dmax;
392                if (castonly)
393                        thisray.revf = raycast;
438          }
439 +        rayorigin(&thisray, PRIMARY, NULL, NULL);
440 +        if (imm_irrad)
441 +                thisray.revf = rayirrad;
442 +        else if (castonly)
443 +                thisray.revf = raycast;
444          if (ray_pnprocs > 1) {          /* multiprocessing FIFO? */
445                  if (ray_fifo_in(&thisray) < 0)
446                          error(USER, "lost children");
# Line 421 | Line 470 | printvals(                     /* print requested ray values */
470  
471  
472   static int
473 < getvec(         /* get a vector from fp */
473 > is_fifo(                /* check if file pointer connected to pipe */
474 >        FILE *fp
475 > )
476 > {
477 > #ifdef S_ISFIFO
478 >        struct stat  sbuf;
479 >
480 >        if (fstat(fileno(fp), &sbuf) < 0)
481 >                error(SYSTEM, "fstat() failed on input stream");
482 >        return(S_ISFIFO(sbuf.st_mode));
483 > #else
484 >        return (fp == stdin);           /* just a guess, really */
485 > #endif
486 > }
487 >
488 >
489 > static int
490 > getvec(                 /* get a vector from fp */
491          FVECT  vec,
492          int  fmt,
493          FILE  *fp
# Line 458 | Line 524 | getvec(                /* get a vector from fp */
524   }
525  
526  
527 + static int
528 + iszerovec(const FVECT vec)
529 + {
530 +        return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
531 + }
532 +
533 +
534 + static double
535 + nextray(                /* return next ray in work group (-1.0 if EOF) */
536 +        FVECT org,
537 +        FVECT dir
538 + )
539 + {
540 +        const size_t    qlength = !vresolu * hresolu;
541 +
542 +        if ((org == NULL) | (dir == NULL)) {
543 +                if (inp_queue != NULL)  /* asking to free queue */
544 +                        free(inp_queue);
545 +                inp_queue = NULL;
546 +                inp_qpos = inp_qend = 0;
547 +                return(-1.);
548 +        }
549 +        if (!inp_qend) {                /* initialize FIFO queue */
550 +                int     rsiz = 6*20;    /* conservative ascii ray size */
551 +                if (inform == 'f') rsiz = 6*sizeof(float);
552 +                else if (inform == 'd') rsiz = 6*sizeof(double);
553 +                                        /* check against pipe limit */
554 +                if (qlength*rsiz > 512 && is_fifo(inpfp))
555 +                        inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
556 +                inp_qend = -(inp_queue == NULL);        /* flag for no queue */
557 +        }
558 +        if (inp_qend < 0) {             /* not queuing? */
559 +                if (getvec(org, inform, inpfp) < 0 ||
560 +                                getvec(dir, inform, inpfp) < 0)
561 +                        return(-1.);
562 +                return normalize(dir);
563 +        }
564 +        if (inp_qpos >= inp_qend) {     /* need to refill input queue? */
565 +                for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
566 +                        if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
567 +                                        || getvec(inp_queue[2*inp_qend+1],
568 +                                                        inform, inpfp) < 0)
569 +                                break;          /* hit EOF */
570 +                        if (iszerovec(inp_queue[2*inp_qend+1])) {
571 +                                ++inp_qend;     /* flush request */
572 +                                break;
573 +                        }
574 +                }
575 +                inp_qpos = 0;
576 +        }
577 +        if (inp_qpos >= inp_qend)       /* unexpected EOF? */
578 +                return(-1.);
579 +        VCOPY(org, inp_queue[2*inp_qpos]);
580 +        VCOPY(dir, inp_queue[2*inp_qpos+1]);
581 +        ++inp_qpos;
582 +        return normalize(dir);
583 + }
584 +
585 +
586   void
587   tranotify(                      /* record new modifier */
588          OBJECT  obj
# Line 646 | Line 771 | static RREAL   vdummy[3] = {0.0, 0.0, 0.0};
771  
772  
773   static void
774 < oputp(                          /* print point */
774 > oputp(                          /* print intersection point */
775          RAY  *r
776   )
777   {
778 <        if (r->rot < FHUGE)
654 <                (*putreal)(r->rop, 3);
655 <        else
656 <                (*putreal)(vdummy, 3);
778 >        (*putreal)(r->rop, 3);  /* set to ray origin if distant or no hit */
779   }
780  
781  
# Line 662 | Line 784 | oputN(                         /* print unperturbed normal */
784          RAY  *r
785   )
786   {
787 <        if (r->rot < FHUGE) {
666 <                if (r->rflips & 1) {    /* undo any flippin' flips */
667 <                        FVECT   unrm;
668 <                        unrm[0] = -r->ron[0];
669 <                        unrm[1] = -r->ron[1];
670 <                        unrm[2] = -r->ron[2];
671 <                        (*putreal)(unrm, 3);
672 <                } else
673 <                        (*putreal)(r->ron, 3);
674 <        } else
787 >        if (r->ro == NULL) {    /* zero vector if clipped or no hit */
788                  (*putreal)(vdummy, 3);
789 +                return;
790 +        }
791 +        if (r->rflips & 1) {    /* undo any flippin' flips */
792 +                FVECT   unrm;
793 +                unrm[0] = -r->ron[0];
794 +                unrm[1] = -r->ron[1];
795 +                unrm[2] = -r->ron[2];
796 +                (*putreal)(unrm, 3);
797 +        } else
798 +                (*putreal)(r->ron, 3);
799   }
800  
801  
# Line 683 | Line 806 | oputn(                         /* print perturbed normal */
806   {
807          FVECT  pnorm;
808  
809 <        if (r->rot >= FHUGE) {
809 >        if (r->ro == NULL) {    /* clipped or no hit */
810                  (*putreal)(vdummy, 3);
811                  return;
812          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines