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.81 by greg, Thu Jul 4 01:25:07 2019 UTC vs.
Revision 2.101 by greg, Tue Feb 16 22:06:00 2021 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 */
# Line 54 | Line 54 | OBJECT traset[MAXTSET+1]={0};          /* trace include/exclud
54  
55   static RAY  thisray;                    /* for our convenience */
56  
57 + static FILE  *inpfp = NULL;             /* input stream pointer */
58 +
59 + static FVECT    *inp_queue = NULL;      /* ray input queue if flushing */
60 + static int      inp_qpos = 0;           /* next ray to return */
61 + static int      inp_qend = 0;           /* number of rays in this work group */
62 +
63   typedef void putf_t(RREAL *v, int n);
64   static putf_t puta, putd, putf, putrgbe;
65  
# Line 62 | Line 68 | static oputf_t  oputo, oputd, oputv, oputV, oputl, opu
68                  oputr, oputR, oputx, oputX, oputn, oputN, oputs,
69                  oputw, oputW, oputm, oputM, oputtilde;
70  
65 static void setoutput(char *vs);
71   extern void tranotify(OBJECT obj);
72 + static int is_fifo(FILE *fp);
73   static void bogusray(void);
74   static void raycast(RAY *r);
75   static void rayirrad(RAY *r);
76   static void rtcompute(FVECT org, FVECT dir, double dmax);
77   static int printvals(RAY *r);
78   static int getvec(FVECT vec, int fmt, FILE *fp);
79 + static int iszerovec(const FVECT vec);
80 + static double nextray(FVECT org, FVECT dir);
81   static void tabin(RAY *r);
82   static void ourtrace(RAY *r);
83  
# Line 84 | Line 92 | quit(                  /* quit program */
92   {
93          if (ray_pnprocs > 0)    /* close children if any */
94                  ray_pclose(0);          
95 +        else if (ray_pnprocs < 0)
96 +                _exit(code);    /* avoid flush() in child */
97   #ifndef  NON_POSIX
98 <        else if (!ray_pnprocs) {
98 >        else {
99                  headclean();    /* delete header file */
100                  pfclean();      /* clean up persist files */
101          }
# Line 109 | Line 119 | formstr(                               /* return format identifier */
119   }
120  
121  
122 < extern void
122 > void
123   rtrace(                         /* trace rays from file */
124          char  *fname,
125          int  nproc
# Line 117 | Line 127 | rtrace(                                /* trace rays from file */
127   {
128          unsigned long  vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
129                                                : (unsigned long)vresolu;
130 <        long  nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu;
130 >        long  nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
131          int  something2flush = 0;
132          FILE  *fp;
133          double  d;
134          FVECT  orig, direc;
135                                          /* set up input */
136          if (fname == NULL)
137 <                fp = stdin;
138 <        else if ((fp = fopen(fname, "r")) == NULL) {
137 >                inpfp = stdin;
138 >        else if ((inpfp = fopen(fname, "r")) == NULL) {
139                  sprintf(errmsg, "cannot open input file \"%s\"", fname);
140                  error(SYSTEM, errmsg);
141          }
142 + #ifdef getc_unlocked
143 +        flockfile(inpfp);               /* avoid lock/unlock overhead */
144 +        flockfile(stdout);
145 + #endif
146          if (inform != 'a')
147 <                SET_FILE_BINARY(fp);
147 >                SET_FILE_BINARY(inpfp);
148                                          /* set up output */
149 <        setoutput(outvals);
136 <        if (imm_irrad)
137 <                castonly = 0;
138 <        else if (castonly)
149 >        if (castonly || every_out[0] != NULL)
150                  nproc = 1;              /* don't bother multiprocessing */
151          if ((nextflush > 0) & (nproc > nextflush)) {
152                  error(WARNING, "reducing number of processes to match flush interval");
# Line 146 | Line 157 | rtrace(                                /* trace rays from file */
157          case 'f': putreal = putf; break;
158          case 'd': putreal = putd; break;
159          case 'c':
160 <                if (outvals[0] && (outvals[1] || !strchr("vrx", outvals[0])))
160 >                if (outvals[1] || !strchr("vrx", outvals[0]))
161                          error(USER, "color format only with -ov, -or, -ox");
162                  putreal = putrgbe; break;
163          default:
# Line 162 | Line 173 | rtrace(                                /* trace rays from file */
173                  else
174                          fflush(stdout);
175          }
176 <                                        /* process file */
177 <        while (getvec(orig, inform, fp) == 0 &&
178 <                        getvec(direc, inform, fp) == 0) {
179 <
169 <                d = normalize(direc);
170 <                if (d == 0.0) {                         /* zero ==> flush */
171 <                        if ((--nextflush <= 0) | !vcount && something2flush) {
176 >                                        /* process input rays */
177 >        while ((d = nextray(orig, direc)) >= 0.0) {
178 >                if (d == 0.0) {                         /* flush request? */
179 >                        if (something2flush) {
180                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
181                                          error(USER, "child(ren) died");
182                                  bogusray();
183                                  fflush(stdout);
184 +                                nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
185                                  something2flush = 0;
177                                nextflush = (vresolu > 0) & (hresolu > 1) ? 0 :
178                                                                hresolu;
186                          } else
187                                  bogusray();
188                  } else {                                /* compute and print */
189                          rtcompute(orig, direc, lim_dist ? d : 0.0);
190 <                                                        /* flush if time */
184 <                        if (!--nextflush) {
190 >                        if (!--nextflush) {             /* flush if time */
191                                  if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
192                                          error(USER, "child(ren) died");
193                                  fflush(stdout);
# Line 199 | Line 205 | rtrace(                                /* trace rays from file */
205                          error(USER, "unable to complete processing");
206                  ray_pclose(0);
207          }
208 +        if (vcount)
209 +                error(WARNING, "unexpected EOF on input");
210          if (fflush(stdout) < 0)
211                  error(SYSTEM, "write error");
212 <        if (vcount)
213 <                error(USER, "unexpected EOF on input");
214 <        if (fname != NULL)
215 <                fclose(fp);
212 >        if (fname != NULL) {
213 >                fclose(inpfp);
214 >                inpfp = NULL;
215 >        }
216 >        nextray(NULL, NULL);
217   }
218  
219  
# Line 218 | Line 227 | trace_sources(void)                    /* trace rays to light sources,
227   }
228  
229  
230 < static void
231 < setoutput(                              /* set up output tables */
223 <        char  *vs
224 < )
230 > int
231 > setrtoutput(void)                       /* set up output tables, return #comp */
232   {
233 +        char  *vs = outvals;
234          oputf_t **table = ray_out;
235 +        int  ncomp = 0;
236  
237 <        castonly = 1;
238 <        while (*vs)
239 <                switch (*vs++) {
237 >        if (!*vs)
238 >                error(USER, "empty output specification");
239 >
240 >        castonly = 1;                   /* sets castonly as side-effect */
241 >        do
242 >                switch (*vs) {
243                  case 'T':                               /* trace sources */
244 <                        if (!*vs) break;
244 >                        if (!vs[1]) break;
245                          trace_sources();
246                          /* fall through */
247                  case 't':                               /* trace */
248 <                        if (!*vs) break;
248 >                        if (!vs[1]) break;
249                          *table = NULL;
250                          table = every_out;
251                          trace = ourtrace;
# Line 241 | Line 253 | setoutput(                             /* set up output tables */
253                          break;
254                  case 'o':                               /* origin */
255                          *table++ = oputo;
256 +                        ncomp += 3;
257                          break;
258                  case 'd':                               /* direction */
259                          *table++ = oputd;
260 +                        ncomp += 3;
261                          break;
262                  case 'r':                               /* reflected contrib. */
263                          *table++ = oputr;
264 +                        ncomp += 3;
265                          castonly = 0;
266                          break;
267                  case 'R':                               /* reflected distance */
268                          *table++ = oputR;
269 +                        ncomp++;
270                          castonly = 0;
271                          break;
272                  case 'x':                               /* xmit contrib. */
273                          *table++ = oputx;
274 +                        ncomp += 3;
275                          castonly = 0;
276                          break;
277                  case 'X':                               /* xmit distance */
278                          *table++ = oputX;
279 +                        ncomp++;
280                          castonly = 0;
281                          break;
282                  case 'v':                               /* value */
283                          *table++ = oputv;
284 +                        ncomp += 3;
285                          castonly = 0;
286                          break;
287                  case 'V':                               /* contribution */
288                          *table++ = oputV;
289 +                        ncomp += 3;
290                          castonly = 0;
291                          if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
292                                  error(WARNING,
# Line 274 | Line 294 | setoutput(                             /* set up output tables */
294                          break;
295                  case 'l':                               /* effective distance */
296                          *table++ = oputl;
297 +                        ncomp++;
298                          castonly = 0;
299                          break;
300                  case 'c':                               /* local coordinates */
301                          *table++ = oputc;
302 +                        ncomp += 2;
303                          break;
304                  case 'L':                               /* single ray length */
305                          *table++ = oputL;
306 +                        ncomp++;
307                          break;
308                  case 'p':                               /* point */
309                          *table++ = oputp;
310 +                        ncomp += 3;
311                          break;
312                  case 'n':                               /* perturbed normal */
313                          *table++ = oputn;
314 +                        ncomp += 3;
315                          castonly = 0;
316                          break;
317                  case 'N':                               /* unperturbed normal */
318                          *table++ = oputN;
319 +                        ncomp += 3;
320                          break;
321                  case 's':                               /* surface */
322                          *table++ = oputs;
323 +                        ncomp++;
324                          break;
325                  case 'w':                               /* weight */
326                          *table++ = oputw;
327 +                        ncomp++;
328                          break;
329                  case 'W':                               /* coefficient */
330                          *table++ = oputW;
331 +                        ncomp += 3;
332                          castonly = 0;
333                          if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0))
334                                  error(WARNING,
# Line 307 | Line 336 | setoutput(                             /* set up output tables */
336                          break;
337                  case 'm':                               /* modifier */
338                          *table++ = oputm;
339 +                        ncomp++;
340                          break;
341                  case 'M':                               /* material */
342                          *table++ = oputM;
343 +                        ncomp++;
344                          break;
345                  case '~':                               /* tilde */
346                          *table++ = oputtilde;
347                          break;
348 +                default:
349 +                        sprintf(errmsg, "unrecognized output option '%c'", *vs);
350 +                        error(USER, errmsg);
351                  }
352 +        while (*++vs);
353 +
354          *table = NULL;
355 +        if (*every_out != NULL)
356 +                ncomp = 0;
357                                                          /* compatibility */
358 +        if ((do_irrad | imm_irrad) && castonly)
359 +                error(USER, "-I+ and -i+ options require some value output");
360          for (table = ray_out; *table != NULL; table++) {
361                  if ((*table == oputV) | (*table == oputW))
362                          error(WARNING, "-oVW options require trace mode");
# Line 325 | Line 365 | setoutput(                             /* set up output tables */
365                                  (*table == oputx) | (*table == oputX))
366                          error(WARNING, "-orRxX options incompatible with -I+ and -i+");
367          }
368 +        return(ncomp);
369   }
370  
371  
# Line 379 | Line 420 | rtcompute(                     /* compute and print ray value(s) */
420   )
421   {
422                                          /* set up ray */
382        rayorigin(&thisray, PRIMARY, NULL, NULL);
423          if (imm_irrad) {
424                  VSUM(thisray.rorg, org, dir, 1.1e-4);
425                  thisray.rdir[0] = -dir[0];
426                  thisray.rdir[1] = -dir[1];
427                  thisray.rdir[2] = -dir[2];
428                  thisray.rmax = 0.0;
389                thisray.revf = rayirrad;
429          } else {
430                  VCOPY(thisray.rorg, org);
431                  VCOPY(thisray.rdir, dir);
432                  thisray.rmax = dmax;
394                if (castonly)
395                        thisray.revf = raycast;
433          }
434 +        rayorigin(&thisray, PRIMARY, NULL, NULL);
435 +        if (imm_irrad)
436 +                thisray.revf = rayirrad;
437 +        else if (castonly)
438 +                thisray.revf = raycast;
439          if (ray_pnprocs > 1) {          /* multiprocessing FIFO? */
440                  if (ray_fifo_in(&thisray) < 0)
441                          error(USER, "lost children");
# Line 423 | Line 465 | printvals(                     /* print requested ray values */
465  
466  
467   static int
468 < getvec(         /* get a vector from fp */
468 > is_fifo(                /* check if file pointer connected to pipe */
469 >        FILE *fp
470 > )
471 > {
472 > #ifdef S_ISFIFO
473 >        struct stat  sbuf;
474 >
475 >        if (fstat(fileno(fp), &sbuf) < 0)
476 >                error(SYSTEM, "fstat() failed on input stream");
477 >        return(S_ISFIFO(sbuf.st_mode));
478 > #else
479 >        return (fp == stdin);           /* just a guess, really */
480 > #endif
481 > }
482 >
483 >
484 > static int
485 > getvec(                 /* get a vector from fp */
486          FVECT  vec,
487          int  fmt,
488          FILE  *fp
# Line 460 | Line 519 | getvec(                /* get a vector from fp */
519   }
520  
521  
522 + static int
523 + iszerovec(const FVECT vec)
524 + {
525 +        return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
526 + }
527 +
528 +
529 + static double
530 + nextray(                /* return next ray in work group (-1.0 if EOF) */
531 +        FVECT org,
532 +        FVECT dir
533 + )
534 + {
535 +        const size_t    qlength = !vresolu * hresolu;
536 +
537 +        if ((org == NULL) | (dir == NULL)) {
538 +                if (inp_queue != NULL)  /* asking to free queue */
539 +                        free(inp_queue);
540 +                inp_queue = NULL;
541 +                inp_qpos = inp_qend = 0;
542 +                return(-1.);
543 +        }
544 +        if (!inp_qend) {                /* initialize FIFO queue */
545 +                int     rsiz = 6*20;    /* conservative ascii ray size */
546 +                if (inform == 'f') rsiz = 6*sizeof(float);
547 +                else if (inform == 'd') rsiz = 6*sizeof(double);
548 +                                        /* check against pipe limit */
549 +                if (qlength*rsiz > 512 && is_fifo(inpfp))
550 +                        inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
551 +                inp_qend = -(inp_queue == NULL);        /* flag for no queue */
552 +        }
553 +        if (inp_qend < 0) {             /* not queuing? */
554 +                if (getvec(org, inform, inpfp) < 0 ||
555 +                                getvec(dir, inform, inpfp) < 0)
556 +                        return(-1.);
557 +                return normalize(dir);
558 +        }
559 +        if (inp_qpos >= inp_qend) {     /* need to refill input queue? */
560 +                for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
561 +                        if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
562 +                                        || getvec(inp_queue[2*inp_qend+1],
563 +                                                        inform, inpfp) < 0)
564 +                                break;          /* hit EOF */
565 +                        if (iszerovec(inp_queue[2*inp_qend+1])) {
566 +                                ++inp_qend;     /* flush request */
567 +                                break;
568 +                        }
569 +                }
570 +                inp_qpos = 0;
571 +        }
572 +        if (inp_qpos >= inp_qend)       /* unexpected EOF? */
573 +                return(-1.);
574 +        VCOPY(org, inp_queue[2*inp_qpos]);
575 +        VCOPY(dir, inp_queue[2*inp_qpos+1]);
576 +        ++inp_qpos;
577 +        return normalize(dir);
578 + }
579 +
580 +
581   void
582   tranotify(                      /* record new modifier */
583          OBJECT  obj
# Line 648 | Line 766 | static RREAL   vdummy[3] = {0.0, 0.0, 0.0};
766  
767  
768   static void
769 < oputp(                          /* print point */
769 > oputp(                          /* print intersection point */
770          RAY  *r
771   )
772   {
773 <        if (r->rot < FHUGE)
656 <                (*putreal)(r->rop, 3);
657 <        else
658 <                (*putreal)(vdummy, 3);
773 >        (*putreal)(r->rop, 3);  /* set to ray origin if distant or no hit */
774   }
775  
776  
# Line 664 | Line 779 | oputN(                         /* print unperturbed normal */
779          RAY  *r
780   )
781   {
782 <        if (r->rot < FHUGE)
668 <                (*putreal)(r->ron, 3);
669 <        else
782 >        if (r->ro == NULL) {    /* zero vector if clipped or no hit */
783                  (*putreal)(vdummy, 3);
784 +                return;
785 +        }
786 +        if (r->rflips & 1) {    /* undo any flippin' flips */
787 +                FVECT   unrm;
788 +                unrm[0] = -r->ron[0];
789 +                unrm[1] = -r->ron[1];
790 +                unrm[2] = -r->ron[2];
791 +                (*putreal)(unrm, 3);
792 +        } else
793 +                (*putreal)(r->ron, 3);
794   }
795  
796  
# Line 678 | Line 801 | oputn(                         /* print perturbed normal */
801   {
802          FVECT  pnorm;
803  
804 <        if (r->rot >= FHUGE) {
804 >        if (r->ro == NULL) {    /* clipped or no hit */
805                  (*putreal)(vdummy, 3);
806                  return;
807          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines