ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.79
Committed: Mon May 6 16:49:38 2019 UTC (5 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.78: +9 -5 lines
Log Message:
Check that number of processes does not exceed flush interval

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtrace.c,v 2.78 2019/05/04 23:10:32 greg Exp $";
3 #endif
4 /*
5 * rtrace.c - program and variables for individual ray tracing.
6 */
7
8 #include "copyright.h"
9
10 /*
11 * Input is in the form:
12 *
13 * xorg yorg zorg xdir ydir zdir
14 *
15 * The direction need not be normalized. Output is flexible.
16 * If the direction vector is (0,0,0), then the output is flushed.
17 * All values default to ascii representation of real
18 * numbers. Binary representations can be selected
19 * with '-ff' for float or '-fd' for double. By default,
20 * radiance is computed. The '-i' or '-I' options indicate that
21 * irradiance values are desired.
22 */
23
24 #include <time.h>
25
26 #include "platform.h"
27 #include "ray.h"
28 #include "ambient.h"
29 #include "source.h"
30 #include "otypes.h"
31 #include "otspecial.h"
32 #include "resolu.h"
33 #include "random.h"
34
35 extern int inform; /* input format */
36 extern int outform; /* output format */
37 extern char *outvals; /* output values */
38
39 extern int imm_irrad; /* compute immediate irradiance? */
40 extern int lim_dist; /* limit distance? */
41
42 extern char *tralist[]; /* list of modifers to trace (or no) */
43 extern int traincl; /* include == 1, exclude == 0 */
44
45 extern int hresolu; /* horizontal resolution */
46 extern int vresolu; /* vertical resolution */
47
48 static int castonly = 0;
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 RAY thisray; /* for our convenience */
56
57 typedef void putf_t(RREAL *v, int n);
58 static putf_t puta, putd, putf;
59
60 typedef void oputf_t(RAY *r);
61 static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp,
62 oputr, oputR, oputx, oputX, oputn, oputN, oputs,
63 oputw, oputW, oputm, oputM, oputtilde;
64
65 static void setoutput(char *vs);
66 extern void tranotify(OBJECT obj);
67 static void bogusray(void);
68 static void raycast(RAY *r);
69 static void rayirrad(RAY *r);
70 static void rtcompute(FVECT org, FVECT dir, double dmax);
71 static int printvals(RAY *r);
72 static int getvec(FVECT vec, int fmt, FILE *fp);
73 static void tabin(RAY *r);
74 static void ourtrace(RAY *r);
75
76 static oputf_t *ray_out[32], *every_out[32];
77 static putf_t *putreal;
78
79
80 void
81 quit( /* quit program */
82 int code
83 )
84 {
85 if (ray_pnprocs > 0) /* close children if any */
86 ray_pclose(0);
87 #ifndef NON_POSIX
88 else if (!ray_pnprocs) {
89 headclean(); /* delete header file */
90 pfclean(); /* clean up persist files */
91 }
92 #endif
93 exit(code);
94 }
95
96
97 char *
98 formstr( /* return format identifier */
99 int f
100 )
101 {
102 switch (f) {
103 case 'a': return("ascii");
104 case 'f': return("float");
105 case 'd': return("double");
106 case 'c': return(COLRFMT);
107 }
108 return("unknown");
109 }
110
111
112 extern void
113 rtrace( /* trace rays from file */
114 char *fname,
115 int nproc
116 )
117 {
118 unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
119 : (unsigned long)vresolu;
120 long nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu;
121 int something2flush = 0;
122 FILE *fp;
123 double d;
124 FVECT orig, direc;
125 /* set up input */
126 if (fname == NULL)
127 fp = stdin;
128 else if ((fp = fopen(fname, "r")) == NULL) {
129 sprintf(errmsg, "cannot open input file \"%s\"", fname);
130 error(SYSTEM, errmsg);
131 }
132 if (inform != 'a')
133 SET_FILE_BINARY(fp);
134 /* set up output */
135 setoutput(outvals);
136 if (imm_irrad)
137 castonly = 0;
138 else if (castonly)
139 nproc = 1; /* don't bother multiprocessing */
140 if ((nextflush > 0) & (nproc > nextflush)) {
141 error(WARNING, "reducing number of processes to match flush interval");
142 nproc = nextflush;
143 }
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 (strcmp(outvals, "v"))
150 error(USER, "color format with value output only");
151 break;
152 default:
153 error(CONSISTENCY, "botched output format");
154 }
155 if (nproc > 1) { /* start multiprocessing */
156 ray_popen(nproc);
157 ray_fifo_out = printvals;
158 }
159 if (hresolu > 0) {
160 if (vresolu > 0)
161 fprtresolu(hresolu, vresolu, stdout);
162 else
163 fflush(stdout);
164 }
165 /* process file */
166 while (getvec(orig, inform, fp) == 0 &&
167 getvec(direc, inform, fp) == 0) {
168
169 d = normalize(direc);
170 if (d == 0.0) { /* zero ==> flush */
171 if ((--nextflush <= 0) | !vcount && something2flush) {
172 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
173 error(USER, "child(ren) died");
174 bogusray();
175 fflush(stdout);
176 something2flush = 0;
177 nextflush = (vresolu > 0) & (hresolu > 1) ? 0 :
178 hresolu;
179 } else
180 bogusray();
181 } else { /* compute and print */
182 rtcompute(orig, direc, lim_dist ? d : 0.0);
183 /* flush if time */
184 if (!--nextflush) {
185 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
186 error(USER, "child(ren) died");
187 fflush(stdout);
188 nextflush = hresolu;
189 } else
190 something2flush = 1;
191 }
192 if (ferror(stdout))
193 error(SYSTEM, "write error");
194 if (vcount && !--vcount) /* check for end */
195 break;
196 }
197 if (ray_pnprocs > 1) { /* clean up children */
198 if (ray_fifo_flush() < 0)
199 error(USER, "unable to complete processing");
200 ray_pclose(0);
201 }
202 if (fflush(stdout) < 0)
203 error(SYSTEM, "write error");
204 if (vcount)
205 error(USER, "unexpected EOF on input");
206 if (fname != NULL)
207 fclose(fp);
208 }
209
210
211 static void
212 trace_sources(void) /* trace rays to light sources, also */
213 {
214 int sn;
215
216 for (sn = 0; sn < nsources; sn++)
217 source[sn].sflags |= SFOLLOW;
218 }
219
220
221 static void
222 setoutput( /* set up output tables */
223 char *vs
224 )
225 {
226 oputf_t **table = ray_out;
227
228 castonly = 1;
229 while (*vs)
230 switch (*vs++) {
231 case 'T': /* trace sources */
232 if (!*vs) break;
233 trace_sources();
234 /* fall through */
235 case 't': /* trace */
236 if (!*vs) break;
237 *table = NULL;
238 table = every_out;
239 trace = ourtrace;
240 castonly = 0;
241 break;
242 case 'o': /* origin */
243 *table++ = oputo;
244 break;
245 case 'd': /* direction */
246 *table++ = oputd;
247 break;
248 case 'r': /* reflected contrib. */
249 *table++ = oputr;
250 castonly = 0;
251 break;
252 case 'R': /* reflected distance */
253 *table++ = oputR;
254 castonly = 0;
255 break;
256 case 'x': /* xmit contrib. */
257 *table++ = oputx;
258 castonly = 0;
259 break;
260 case 'X': /* xmit distance */
261 *table++ = oputX;
262 castonly = 0;
263 break;
264 case 'v': /* value */
265 *table++ = oputv;
266 castonly = 0;
267 break;
268 case 'V': /* contribution */
269 *table++ = oputV;
270 castonly = 0;
271 if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
272 error(WARNING,
273 "-otV accuracy depends on -aa 0 -as 0");
274 break;
275 case 'l': /* effective distance */
276 *table++ = oputl;
277 castonly = 0;
278 break;
279 case 'c': /* local coordinates */
280 *table++ = oputc;
281 break;
282 case 'L': /* single ray length */
283 *table++ = oputL;
284 break;
285 case 'p': /* point */
286 *table++ = oputp;
287 break;
288 case 'n': /* perturbed normal */
289 *table++ = oputn;
290 castonly = 0;
291 break;
292 case 'N': /* unperturbed normal */
293 *table++ = oputN;
294 break;
295 case 's': /* surface */
296 *table++ = oputs;
297 break;
298 case 'w': /* weight */
299 *table++ = oputw;
300 break;
301 case 'W': /* coefficient */
302 *table++ = oputW;
303 castonly = 0;
304 if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
305 error(WARNING,
306 "-otW accuracy depends on -aa 0 -as 0");
307 break;
308 case 'm': /* modifier */
309 *table++ = oputm;
310 break;
311 case 'M': /* material */
312 *table++ = oputM;
313 break;
314 case '~': /* tilde */
315 *table++ = oputtilde;
316 break;
317 }
318 *table = NULL;
319 }
320
321
322 static void
323 bogusray(void) /* print out empty record */
324 {
325 rayorigin(&thisray, PRIMARY, NULL, NULL);
326 printvals(&thisray);
327 }
328
329
330 static void
331 raycast( /* compute first ray intersection only */
332 RAY *r
333 )
334 {
335 if (!localhit(r, &thescene)) {
336 if (r->ro == &Aftplane) { /* clipped */
337 r->ro = NULL;
338 r->rot = FHUGE;
339 } else
340 sourcehit(r);
341 }
342 }
343
344
345 static void
346 rayirrad( /* compute irradiance rather than radiance */
347 RAY *r
348 )
349 {
350 void (*old_revf)(RAY *) = r->revf;
351 /* pretend we hit surface */
352 r->rxt = r->rot = 1e-5;
353 VSUM(r->rop, r->rorg, r->rdir, r->rot);
354 r->ron[0] = -r->rdir[0];
355 r->ron[1] = -r->rdir[1];
356 r->ron[2] = -r->rdir[2];
357 r->rod = 1.0;
358 /* compute result */
359 r->revf = raytrace;
360 (*ofun[Lamb.otype].funp)(&Lamb, r);
361 r->revf = old_revf;
362 }
363
364
365 static void
366 rtcompute( /* compute and print ray value(s) */
367 FVECT org,
368 FVECT dir,
369 double dmax
370 )
371 {
372 /* set up ray */
373 rayorigin(&thisray, PRIMARY, NULL, NULL);
374 if (imm_irrad) {
375 VSUM(thisray.rorg, org, dir, 1.1e-4);
376 thisray.rdir[0] = -dir[0];
377 thisray.rdir[1] = -dir[1];
378 thisray.rdir[2] = -dir[2];
379 thisray.rmax = 0.0;
380 thisray.revf = rayirrad;
381 } else {
382 VCOPY(thisray.rorg, org);
383 VCOPY(thisray.rdir, dir);
384 thisray.rmax = dmax;
385 if (castonly)
386 thisray.revf = raycast;
387 }
388 if (ray_pnprocs > 1) { /* multiprocessing FIFO? */
389 if (ray_fifo_in(&thisray) < 0)
390 error(USER, "lost children");
391 return;
392 }
393 samplendx++; /* else do it ourselves */
394 rayvalue(&thisray);
395 printvals(&thisray);
396 }
397
398
399 static int
400 printvals( /* print requested ray values */
401 RAY *r
402 )
403 {
404 oputf_t **tp;
405
406 if (ray_out[0] == NULL)
407 return(0);
408 for (tp = ray_out; *tp != NULL; tp++)
409 (**tp)(r);
410 if (outform == 'a')
411 putchar('\n');
412 return(1);
413 }
414
415
416 static int
417 getvec( /* get a vector from fp */
418 FVECT vec,
419 int fmt,
420 FILE *fp
421 )
422 {
423 static float vf[3];
424 static double vd[3];
425 char buf[32];
426 int i;
427
428 switch (fmt) {
429 case 'a': /* ascii */
430 for (i = 0; i < 3; i++) {
431 if (fgetword(buf, sizeof(buf), fp) == NULL ||
432 !isflt(buf))
433 return(-1);
434 vec[i] = atof(buf);
435 }
436 break;
437 case 'f': /* binary float */
438 if (getbinary(vf, sizeof(float), 3, fp) != 3)
439 return(-1);
440 VCOPY(vec, vf);
441 break;
442 case 'd': /* binary double */
443 if (getbinary(vd, sizeof(double), 3, fp) != 3)
444 return(-1);
445 VCOPY(vec, vd);
446 break;
447 default:
448 error(CONSISTENCY, "botched input format");
449 }
450 return(0);
451 }
452
453
454 void
455 tranotify( /* record new modifier */
456 OBJECT obj
457 )
458 {
459 static int hitlimit = 0;
460 OBJREC *o = objptr(obj);
461 char **tralp;
462
463 if (obj == OVOID) { /* starting over */
464 traset[0] = 0;
465 hitlimit = 0;
466 return;
467 }
468 if (hitlimit || !ismodifier(o->otype))
469 return;
470 for (tralp = tralist; *tralp != NULL; tralp++)
471 if (!strcmp(o->oname, *tralp)) {
472 if (traset[0] >= MAXTSET) {
473 error(WARNING, "too many modifiers in trace list");
474 hitlimit++;
475 return; /* should this be fatal? */
476 }
477 insertelem(traset, obj);
478 return;
479 }
480 }
481
482
483 static void
484 ourtrace( /* print ray values */
485 RAY *r
486 )
487 {
488 oputf_t **tp;
489
490 if (every_out[0] == NULL)
491 return;
492 if (r->ro == NULL) {
493 if (traincl == 1)
494 return;
495 } else if (traincl != -1 && traincl != inset(traset, r->ro->omod))
496 return;
497 tabin(r);
498 for (tp = every_out; *tp != NULL; tp++)
499 (**tp)(r);
500 if (outform == 'a')
501 putchar('\n');
502 }
503
504
505 static void
506 tabin( /* tab in appropriate amount */
507 RAY *r
508 )
509 {
510 const RAY *rp;
511
512 for (rp = r->parent; rp != NULL; rp = rp->parent)
513 putchar('\t');
514 }
515
516
517 static void
518 oputo( /* print origin */
519 RAY *r
520 )
521 {
522 (*putreal)(r->rorg, 3);
523 }
524
525
526 static void
527 oputd( /* print direction */
528 RAY *r
529 )
530 {
531 (*putreal)(r->rdir, 3);
532 }
533
534
535 static void
536 oputr( /* print mirrored contribution */
537 RAY *r
538 )
539 {
540 RREAL cval[3];
541
542 cval[0] = colval(r->mcol,RED);
543 cval[1] = colval(r->mcol,GRN);
544 cval[2] = colval(r->mcol,BLU);
545 (*putreal)(cval, 3);
546 }
547
548
549
550 static void
551 oputR( /* print mirrored distance */
552 RAY *r
553 )
554 {
555 (*putreal)(&r->rmt, 1);
556 }
557
558
559 static void
560 oputx( /* print unmirrored contribution */
561 RAY *r
562 )
563 {
564 RREAL cval[3];
565
566 cval[0] = colval(r->rcol,RED) - colval(r->mcol,RED);
567 cval[1] = colval(r->rcol,GRN) - colval(r->mcol,GRN);
568 cval[2] = colval(r->rcol,BLU) - colval(r->mcol,BLU);
569 (*putreal)(cval, 3);
570 }
571
572
573 static void
574 oputX( /* print unmirrored distance */
575 RAY *r
576 )
577 {
578 (*putreal)(&r->rxt, 1);
579 }
580
581
582 static void
583 oputv( /* print value */
584 RAY *r
585 )
586 {
587 RREAL cval[3];
588
589 if (outform == 'c') {
590 COLR cout;
591 setcolr(cout, colval(r->rcol,RED),
592 colval(r->rcol,GRN),
593 colval(r->rcol,BLU));
594 putbinary(cout, sizeof(cout), 1, stdout);
595 return;
596 }
597 cval[0] = colval(r->rcol,RED);
598 cval[1] = colval(r->rcol,GRN);
599 cval[2] = colval(r->rcol,BLU);
600 (*putreal)(cval, 3);
601 }
602
603
604 static void
605 oputV( /* print value contribution */
606 RAY *r
607 )
608 {
609 RREAL contr[3];
610
611 raycontrib(contr, r, PRIMARY);
612 multcolor(contr, r->rcol);
613 (*putreal)(contr, 3);
614 }
615
616
617 static void
618 oputl( /* print effective distance */
619 RAY *r
620 )
621 {
622 RREAL d = raydistance(r);
623
624 (*putreal)(&d, 1);
625 }
626
627
628 static void
629 oputL( /* print single ray length */
630 RAY *r
631 )
632 {
633 (*putreal)(&r->rot, 1);
634 }
635
636
637 static void
638 oputc( /* print local coordinates */
639 RAY *r
640 )
641 {
642 (*putreal)(r->uv, 2);
643 }
644
645
646 static RREAL vdummy[3] = {0.0, 0.0, 0.0};
647
648
649 static void
650 oputp( /* print point */
651 RAY *r
652 )
653 {
654 if (r->rot < FHUGE)
655 (*putreal)(r->rop, 3);
656 else
657 (*putreal)(vdummy, 3);
658 }
659
660
661 static void
662 oputN( /* print unperturbed normal */
663 RAY *r
664 )
665 {
666 if (r->rot < FHUGE)
667 (*putreal)(r->ron, 3);
668 else
669 (*putreal)(vdummy, 3);
670 }
671
672
673 static void
674 oputn( /* print perturbed normal */
675 RAY *r
676 )
677 {
678 FVECT pnorm;
679
680 if (r->rot >= FHUGE) {
681 (*putreal)(vdummy, 3);
682 return;
683 }
684 raynormal(pnorm, r);
685 (*putreal)(pnorm, 3);
686 }
687
688
689 static void
690 oputs( /* print name */
691 RAY *r
692 )
693 {
694 if (r->ro != NULL)
695 fputs(r->ro->oname, stdout);
696 else
697 putchar('*');
698 putchar('\t');
699 }
700
701
702 static void
703 oputw( /* print weight */
704 RAY *r
705 )
706 {
707 RREAL rwt = r->rweight;
708
709 (*putreal)(&rwt, 1);
710 }
711
712
713 static void
714 oputW( /* print coefficient */
715 RAY *r
716 )
717 {
718 RREAL contr[3];
719 /* shadow ray not on source? */
720 if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
721 setcolor(contr, 0.0, 0.0, 0.0);
722 else
723 raycontrib(contr, r, PRIMARY);
724
725 (*putreal)(contr, 3);
726 }
727
728
729 static void
730 oputm( /* print modifier */
731 RAY *r
732 )
733 {
734 if (r->ro != NULL)
735 if (r->ro->omod != OVOID)
736 fputs(objptr(r->ro->omod)->oname, stdout);
737 else
738 fputs(VOIDID, stdout);
739 else
740 putchar('*');
741 putchar('\t');
742 }
743
744
745 static void
746 oputM( /* print material */
747 RAY *r
748 )
749 {
750 OBJREC *mat;
751
752 if (r->ro != NULL) {
753 if ((mat = findmaterial(r->ro)) != NULL)
754 fputs(mat->oname, stdout);
755 else
756 fputs(VOIDID, stdout);
757 } else
758 putchar('*');
759 putchar('\t');
760 }
761
762
763 static void
764 oputtilde( /* output tilde (spacer) */
765 RAY *r
766 )
767 {
768 fputs("~\t", stdout);
769 }
770
771
772 static void
773 puta( /* print ascii value(s) */
774 RREAL *v, int n
775 )
776 {
777 if (n == 3) {
778 printf("%e\t%e\t%e\t", v[0], v[1], v[2]);
779 return;
780 }
781 while (n--)
782 printf("%e\t", *v++);
783 }
784
785
786 static void
787 putd(RREAL *v, int n) /* print binary double(s) */
788 {
789 #ifdef SMLFLT
790 double da[3];
791 int i;
792
793 if (n > 3)
794 error(INTERNAL, "code error in putd()");
795 for (i = n; i--; )
796 da[i] = v[i];
797 putbinary(da, sizeof(double), n, stdout);
798 #else
799 putbinary(v, sizeof(RREAL), n, stdout);
800 #endif
801 }
802
803
804 static void
805 putf(RREAL *v, int n) /* print binary float(s) */
806 {
807 #ifndef SMLFLT
808 float fa[3];
809 int i;
810
811 if (n > 3)
812 error(INTERNAL, "code error in putf()");
813 for (i = n; i--; )
814 fa[i] = v[i];
815 putbinary(fa, sizeof(float), n, stdout);
816 #else
817 putbinary(v, sizeof(RREAL), n, stdout);
818 #endif
819 }