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.87 by greg, Wed Mar 4 01:23:31 2020 UTC vs.
Revision 2.103 by greg, Sun Apr 24 15:40:33 2022 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 > int  castonly = 0;                      /* 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 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 (Tflag && every_out[0] != NULL)
164 +                trace_sources();        /* asking to trace light sources */
165          if ((nextflush > 0) & (nproc > nextflush)) {
166                  error(WARNING, "reducing number of processes to match flush interval");
167                  nproc = nextflush;
168          }
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        }
169          if (nproc > 1) {                /* start multiprocessing */
170                  ray_popen(nproc);
171                  ray_fifo_out = printvals;
# Line 162 | Line 176 | rtrace(                                /* trace rays from file */
176                  else
177                          fflush(stdout);
178          }
179 <                                        /* process file */
180 <        while (getvec(orig, inform, fp) == 0 &&
167 <                        getvec(direc, inform, fp) == 0) {
168 <
169 <                d = normalize(direc);
179 >                                        /* process input rays */
180 >        while ((d = nextray(orig, direc)) >= 0.0) {
181                  if (d == 0.0) {                         /* flush request? */
182                          if (something2flush) {
183                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
# Line 179 | Line 190 | rtrace(                                /* trace rays from file */
190                                  bogusray();
191                  } else {                                /* compute and print */
192                          rtcompute(orig, direc, lim_dist ? d : 0.0);
193 <                                                        /* flush if time */
183 <                        if (!--nextflush) {
193 >                        if (!--nextflush) {             /* flush if time */
194                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
195                                          error(USER, "child(ren) died");
196                                  fflush(stdout);
# Line 198 | Line 208 | rtrace(                                /* trace rays from file */
208                          error(USER, "unable to complete processing");
209                  ray_pclose(0);
210          }
211 +        if (vcount)
212 +                error(WARNING, "unexpected EOF on input");
213          if (fflush(stdout) < 0)
214                  error(SYSTEM, "write error");
215 <        if (vcount)
216 <                error(USER, "unexpected EOF on input");
217 <        if (fname != NULL)
218 <                fclose(fp);
215 >        if (fname != NULL) {
216 >                fclose(inpfp);
217 >                inpfp = NULL;
218 >        }
219 >        nextray(NULL, NULL);
220   }
221  
222  
223 < static void
224 < trace_sources(void)                     /* trace rays to light sources, also */
223 > int
224 > setrtoutput(void)                       /* set up output tables, return #comp */
225   {
226 <        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 < {
226 >        char  *vs = outvals;
227          oputf_t **table = ray_out;
228 +        int  ncomp = 0;
229  
230 <        castonly = 1;
231 <        while (*vs)
232 <                switch (*vs++) {
230 >        if (!*vs)
231 >                error(USER, "empty output specification");
232 >
233 >        switch (outform) {      /* make sure (*putreal)() calls someone! */
234 >        case 'a': putreal = puta; break;
235 >        case 'f': putreal = putf; break;
236 >        case 'd': putreal = putd; break;
237 >        case 'c':
238 >                if (outvals[1] || !strchr("vrx", outvals[0]))
239 >                        error(USER, "color format only with -ov, -or, -ox");
240 >                putreal = putrgbe; break;
241 >        default:
242 >                error(CONSISTENCY, "botched output format");
243 >        }
244 >        castonly = 1;                   /* sets castonly as side-effect */
245 >        do
246 >                switch (*vs) {
247                  case 'T':                               /* trace sources */
248 <                        if (!*vs) break;
232 <                        trace_sources();
248 >                        Tflag++;
249                          /* fall through */
250                  case 't':                               /* trace */
251 <                        if (!*vs) break;
251 >                        if (!vs[1]) break;
252                          *table = NULL;
253                          table = every_out;
254                          trace = ourtrace;
# Line 240 | Line 256 | setoutput(                             /* set up output tables */
256                          break;
257                  case 'o':                               /* origin */
258                          *table++ = oputo;
259 +                        ncomp += 3;
260                          break;
261                  case 'd':                               /* direction */
262                          *table++ = oputd;
263 +                        ncomp += 3;
264                          break;
265                  case 'r':                               /* reflected contrib. */
266                          *table++ = oputr;
267 +                        ncomp += 3;
268                          castonly = 0;
269                          break;
270                  case 'R':                               /* reflected distance */
271                          *table++ = oputR;
272 +                        ncomp++;
273                          castonly = 0;
274                          break;
275                  case 'x':                               /* xmit contrib. */
276                          *table++ = oputx;
277 +                        ncomp += 3;
278                          castonly = 0;
279                          break;
280                  case 'X':                               /* xmit distance */
281                          *table++ = oputX;
282 +                        ncomp++;
283                          castonly = 0;
284                          break;
285                  case 'v':                               /* value */
286                          *table++ = oputv;
287 +                        ncomp += 3;
288                          castonly = 0;
289                          break;
290                  case 'V':                               /* contribution */
291                          *table++ = oputV;
292 +                        ncomp += 3;
293 +                        castonly = 0;
294                          if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
295                                  error(WARNING,
296                                          "-otV accuracy depends on -aa 0 -as 0");
297                          break;
298                  case 'l':                               /* effective distance */
299                          *table++ = oputl;
300 +                        ncomp++;
301                          castonly = 0;
302                          break;
303                  case 'c':                               /* local coordinates */
304                          *table++ = oputc;
305 +                        ncomp += 2;
306                          break;
307                  case 'L':                               /* single ray length */
308                          *table++ = oputL;
309 +                        ncomp++;
310                          break;
311                  case 'p':                               /* point */
312                          *table++ = oputp;
313 +                        ncomp += 3;
314                          break;
315                  case 'n':                               /* perturbed normal */
316                          *table++ = oputn;
317 +                        ncomp += 3;
318                          castonly = 0;
319                          break;
320                  case 'N':                               /* unperturbed normal */
321                          *table++ = oputN;
322 +                        ncomp += 3;
323                          break;
324                  case 's':                               /* surface */
325                          *table++ = oputs;
326 +                        ncomp++;
327                          break;
328                  case 'w':                               /* weight */
329                          *table++ = oputw;
330 +                        ncomp++;
331                          break;
332                  case 'W':                               /* coefficient */
333                          *table++ = oputW;
334 +                        ncomp += 3;
335                          castonly = 0;
336                          if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0))
337                                  error(WARNING,
# Line 305 | Line 339 | setoutput(                             /* set up output tables */
339                          break;
340                  case 'm':                               /* modifier */
341                          *table++ = oputm;
342 +                        ncomp++;
343                          break;
344                  case 'M':                               /* material */
345                          *table++ = oputM;
346 +                        ncomp++;
347                          break;
348                  case '~':                               /* tilde */
349                          *table++ = oputtilde;
350                          break;
351 +                default:
352 +                        sprintf(errmsg, "unrecognized output option '%c'", *vs);
353 +                        error(USER, errmsg);
354                  }
355 +        while (*++vs);
356 +
357          *table = NULL;
358 +        if (*every_out != NULL)
359 +                ncomp = 0;
360                                                          /* compatibility */
361 +        if ((do_irrad | imm_irrad) && castonly)
362 +                error(USER, "-I+ and -i+ options require some value output");
363          for (table = ray_out; *table != NULL; table++) {
364                  if ((*table == oputV) | (*table == oputW))
365                          error(WARNING, "-oVW options require trace mode");
# Line 323 | Line 368 | setoutput(                             /* set up output tables */
368                                  (*table == oputx) | (*table == oputX))
369                          error(WARNING, "-orRxX options incompatible with -I+ and -i+");
370          }
371 +        return(ncomp);
372   }
373  
374  
# Line 377 | Line 423 | rtcompute(                     /* compute and print ray value(s) */
423   )
424   {
425                                          /* set up ray */
380        rayorigin(&thisray, PRIMARY, NULL, NULL);
426          if (imm_irrad) {
427                  VSUM(thisray.rorg, org, dir, 1.1e-4);
428                  thisray.rdir[0] = -dir[0];
429                  thisray.rdir[1] = -dir[1];
430                  thisray.rdir[2] = -dir[2];
431                  thisray.rmax = 0.0;
387                thisray.revf = rayirrad;
432          } else {
433                  VCOPY(thisray.rorg, org);
434                  VCOPY(thisray.rdir, dir);
435                  thisray.rmax = dmax;
392                if (castonly)
393                        thisray.revf = raycast;
436          }
437 +        rayorigin(&thisray, PRIMARY, NULL, NULL);
438 +        if (imm_irrad)
439 +                thisray.revf = rayirrad;
440 +        else if (castonly)
441 +                thisray.revf = raycast;
442          if (ray_pnprocs > 1) {          /* multiprocessing FIFO? */
443                  if (ray_fifo_in(&thisray) < 0)
444                          error(USER, "lost children");
# Line 421 | Line 468 | printvals(                     /* print requested ray values */
468  
469  
470   static int
471 < getvec(         /* get a vector from fp */
471 > is_fifo(                /* check if file pointer connected to pipe */
472 >        FILE *fp
473 > )
474 > {
475 > #ifdef S_ISFIFO
476 >        struct stat  sbuf;
477 >
478 >        if (fstat(fileno(fp), &sbuf) < 0)
479 >                error(SYSTEM, "fstat() failed on input stream");
480 >        return(S_ISFIFO(sbuf.st_mode));
481 > #else
482 >        return (fp == stdin);           /* just a guess, really */
483 > #endif
484 > }
485 >
486 >
487 > static int
488 > getvec(                 /* get a vector from fp */
489          FVECT  vec,
490          int  fmt,
491          FILE  *fp
# Line 458 | Line 522 | getvec(                /* get a vector from fp */
522   }
523  
524  
525 + static int
526 + iszerovec(const FVECT vec)
527 + {
528 +        return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
529 + }
530 +
531 +
532 + static double
533 + nextray(                /* return next ray in work group (-1.0 if EOF) */
534 +        FVECT org,
535 +        FVECT dir
536 + )
537 + {
538 +        const size_t    qlength = !vresolu * hresolu;
539 +
540 +        if ((org == NULL) | (dir == NULL)) {
541 +                if (inp_queue != NULL)  /* asking to free queue */
542 +                        free(inp_queue);
543 +                inp_queue = NULL;
544 +                inp_qpos = inp_qend = 0;
545 +                return(-1.);
546 +        }
547 +        if (!inp_qend) {                /* initialize FIFO queue */
548 +                int     rsiz = 6*20;    /* conservative ascii ray size */
549 +                if (inform == 'f') rsiz = 6*sizeof(float);
550 +                else if (inform == 'd') rsiz = 6*sizeof(double);
551 +                                        /* check against pipe limit */
552 +                if (qlength*rsiz > 512 && is_fifo(inpfp))
553 +                        inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
554 +                inp_qend = -(inp_queue == NULL);        /* flag for no queue */
555 +        }
556 +        if (inp_qend < 0) {             /* not queuing? */
557 +                if (getvec(org, inform, inpfp) < 0 ||
558 +                                getvec(dir, inform, inpfp) < 0)
559 +                        return(-1.);
560 +                return normalize(dir);
561 +        }
562 +        if (inp_qpos >= inp_qend) {     /* need to refill input queue? */
563 +                for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
564 +                        if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
565 +                                        || getvec(inp_queue[2*inp_qend+1],
566 +                                                        inform, inpfp) < 0)
567 +                                break;          /* hit EOF */
568 +                        if (iszerovec(inp_queue[2*inp_qend+1])) {
569 +                                ++inp_qend;     /* flush request */
570 +                                break;
571 +                        }
572 +                }
573 +                inp_qpos = 0;
574 +        }
575 +        if (inp_qpos >= inp_qend)       /* unexpected EOF? */
576 +                return(-1.);
577 +        VCOPY(org, inp_queue[2*inp_qpos]);
578 +        VCOPY(dir, inp_queue[2*inp_qpos+1]);
579 +        ++inp_qpos;
580 +        return normalize(dir);
581 + }
582 +
583 +
584   void
585   tranotify(                      /* record new modifier */
586          OBJECT  obj
# Line 646 | Line 769 | static RREAL   vdummy[3] = {0.0, 0.0, 0.0};
769  
770  
771   static void
772 < oputp(                          /* print point */
772 > oputp(                          /* print intersection point */
773          RAY  *r
774   )
775   {
776 <        if (r->rot < FHUGE)
654 <                (*putreal)(r->rop, 3);
655 <        else
656 <                (*putreal)(vdummy, 3);
776 >        (*putreal)(r->rop, 3);  /* set to ray origin if distant or no hit */
777   }
778  
779  
# Line 662 | Line 782 | oputN(                         /* print unperturbed normal */
782          RAY  *r
783   )
784   {
785 <        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
785 >        if (r->ro == NULL) {    /* zero vector if clipped or no hit */
786                  (*putreal)(vdummy, 3);
787 +                return;
788 +        }
789 +        if (r->rflips & 1) {    /* undo any flippin' flips */
790 +                FVECT   unrm;
791 +                unrm[0] = -r->ron[0];
792 +                unrm[1] = -r->ron[1];
793 +                unrm[2] = -r->ron[2];
794 +                (*putreal)(unrm, 3);
795 +        } else
796 +                (*putreal)(r->ron, 3);
797   }
798  
799  
# Line 683 | Line 804 | oputn(                         /* print perturbed normal */
804   {
805          FVECT  pnorm;
806  
807 <        if (r->rot >= FHUGE) {
807 >        if (r->ro == NULL) {    /* clipped or no hit */
808                  (*putreal)(vdummy, 3);
809                  return;
810          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines