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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines