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.84 by greg, Wed Aug 14 22:33:02 2019 UTC vs.
Revision 2.91 by greg, Sun Apr 5 15:47:02 2020 UTC

# 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 70 | Line 76 | 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 117 | Line 125 | rtrace(                                /* trace rays from file */
125   {
126          unsigned long  vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
127                                                : (unsigned long)vresolu;
128 <        long  nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu;
128 >        long  nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
129          int  something2flush = 0;
130          FILE  *fp;
131          double  d;
132          FVECT  orig, direc;
133                                          /* set up input */
134          if (fname == NULL)
135 <                fp = stdin;
136 <        else if ((fp = fopen(fname, "r")) == NULL) {
135 >                inpfp = stdin;
136 >        else if ((inpfp = fopen(fname, "r")) == NULL) {
137                  sprintf(errmsg, "cannot open input file \"%s\"", fname);
138                  error(SYSTEM, errmsg);
139          }
140 + #ifdef getc_unlocked
141 +        flockfile(inpfp);               /* avoid lock/unlock overhead */
142 +        flockfile(stdout);
143 + #endif
144          if (inform != 'a')
145 <                SET_FILE_BINARY(fp);
145 >                SET_FILE_BINARY(inpfp);
146                                          /* set up output */
135        setoutput(outvals);
147          if (imm_irrad)
148                  castonly = 0;
149 <        else if (castonly)
149 >        else 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 +        if (!*vs)
238 +                error(USER, "empty output specification");
239 +
240          castonly = 1;
241 <        while (*vs)
242 <                switch (*vs++) {
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                          if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
291                                  error(WARNING,
292                                          "-otV accuracy depends on -aa 0 -as 0");
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 306 | 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          for (table = ray_out; *table != NULL; table++) {
358                  if ((*table == oputV) | (*table == oputW))
# Line 324 | Line 362 | setoutput(                             /* set up output tables */
362                                  (*table == oputx) | (*table == oputX))
363                          error(WARNING, "-orRxX options incompatible with -I+ and -i+");
364          }
365 +        return(ncomp);
366   }
367  
368  
# Line 422 | Line 461 | printvals(                     /* print requested ray values */
461  
462  
463   static int
464 < getvec(         /* get a vector from fp */
464 > getvec(                 /* get a vector from fp */
465          FVECT  vec,
466          int  fmt,
467          FILE  *fp
# Line 459 | Line 498 | getvec(                /* get a vector from fp */
498   }
499  
500  
501 + static int
502 + iszerovec(const FVECT vec)
503 + {
504 +        return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
505 + }
506 +
507 +
508 + static double
509 + nextray(                /* return next ray in work group (-1.0 if EOF) */
510 +        FVECT org,
511 +        FVECT dir
512 + )
513 + {
514 +        const size_t    qlength = !vresolu * hresolu;
515 +
516 +        if ((org == NULL) | (dir == NULL)) {
517 +                if (inp_queue != NULL)  /* asking to free queue */
518 +                        free(inp_queue);
519 +                inp_queue = NULL;
520 +                inp_qpos = inp_qend = 0;
521 +                return(-1.);
522 +        }
523 +        if (!inp_qend) {                /* initialize FIFO queue */
524 +                int     rsiz = 6*20;    /* conservative ascii ray size */
525 +                if (inform == 'f') rsiz = 6*sizeof(float);
526 +                else if (inform == 'd') rsiz = 6*sizeof(double);
527 +                if ((inpfp == stdin) & (qlength*rsiz > 512))    /* pipe limit */
528 +                        inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
529 +                inp_qend = -(inp_queue == NULL);        /* flag for no queue */
530 +        }
531 +        if (inp_qend < 0) {             /* not queuing? */
532 +                if (getvec(org, inform, inpfp) < 0 ||
533 +                                getvec(dir, inform, inpfp) < 0)
534 +                        return(-1.);
535 +                return normalize(dir);
536 +        }
537 +        if (inp_qpos >= inp_qend) {     /* need to refill input queue? */
538 +                for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
539 +                        if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
540 +                                        || getvec(inp_queue[2*inp_qend+1],
541 +                                                        inform, inpfp) < 0)
542 +                                break;          /* hit EOF */
543 +                        if (iszerovec(inp_queue[2*inp_qend+1])) {
544 +                                ++inp_qend;     /* flush request */
545 +                                break;
546 +                        }
547 +                }
548 +                inp_qpos = 0;
549 +        }
550 +        if (inp_qpos >= inp_qend)       /* unexpected EOF? */
551 +                return(-1.);
552 +        VCOPY(org, inp_queue[2*inp_qpos]);
553 +        VCOPY(dir, inp_queue[2*inp_qpos+1]);
554 +        ++inp_qpos;
555 +        return normalize(dir);
556 + }
557 +
558 +
559   void
560   tranotify(                      /* record new modifier */
561          OBJECT  obj
# Line 651 | Line 748 | oputp(                         /* print point */
748          RAY  *r
749   )
750   {
751 <        if (r->rot < FHUGE)
751 >        if (r->rot < FHUGE*.99)
752                  (*putreal)(r->rop, 3);
753          else
754                  (*putreal)(vdummy, 3);
# Line 663 | Line 760 | oputN(                         /* print unperturbed normal */
760          RAY  *r
761   )
762   {
763 <        if (r->rot < FHUGE) {
763 >        if (r->rot < FHUGE*.99) {
764                  if (r->rflips & 1) {    /* undo any flippin' flips */
765                          FVECT   unrm;
766                          unrm[0] = -r->ron[0];
# Line 684 | Line 781 | oputn(                         /* print perturbed normal */
781   {
782          FVECT  pnorm;
783  
784 <        if (r->rot >= FHUGE) {
784 >        if (r->rot >= FHUGE*.99) {
785                  (*putreal)(vdummy, 3);
786                  return;
787          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines