ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.58
Committed: Sat Dec 12 23:08:13 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.57: +10 -7 lines
Log Message:
Bug fixes and performance improvements to rtrace -n option

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.58 static const char RCSid[] = "$Id: rtrace.c,v 2.57 2009/12/12 19:01:00 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * rtrace.c - program and variables for individual ray tracing.
6 greg 2.27 */
7    
8 greg 2.28 #include "copyright.h"
9 greg 1.1
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 greg 1.7 * If the direction vector is (0,0,0), then the output is flushed.
17 greg 1.1 * 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 greg 1.13 * radiance is computed. The '-i' or '-I' options indicate that
21 greg 1.1 * irradiance values are desired.
22     */
23 greg 2.32
24     #include <time.h>
25 greg 1.1
26 schorsch 2.30 #include "platform.h"
27 greg 1.1 #include "ray.h"
28 schorsch 2.37 #include "ambient.h"
29     #include "source.h"
30 greg 1.1 #include "otypes.h"
31 greg 2.6 #include "resolu.h"
32 schorsch 2.49 #include "random.h"
33 greg 2.6
34 greg 2.57 extern int inform; /* input format */
35     extern int outform; /* output format */
36     extern char *outvals; /* output values */
37 greg 2.27
38 greg 2.57 extern int imm_irrad; /* compute immediate irradiance? */
39     extern int lim_dist; /* limit distance? */
40 greg 1.14
41 greg 2.57 extern char *tralist[]; /* list of modifers to trace (or no) */
42     extern int traincl; /* include == 1, exclude == 0 */
43 greg 1.13
44 greg 2.57 extern int hresolu; /* horizontal resolution */
45     extern int vresolu; /* vertical resolution */
46 greg 1.1
47 greg 2.57 static int castonly = 0;
48 greg 2.55
49 greg 2.46 #ifndef MAXTSET
50 greg 2.55 #define MAXTSET 8191 /* maximum number in trace set */
51 greg 2.46 #endif
52 greg 2.15 OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */
53    
54 greg 1.1 static RAY thisray; /* for our convenience */
55    
56 schorsch 2.37 typedef void putf_t(double v);
57     static putf_t puta, putd, putf;
58 greg 1.1
59 schorsch 2.37 typedef void oputf_t(RAY *r);
60 greg 2.51 static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp,
61 greg 2.42 oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde;
62 schorsch 2.37
63     static void setoutput(char *vs);
64 greg 2.57 extern void tranotify(OBJECT obj);
65 schorsch 2.37 static void bogusray(void);
66 greg 2.57 static void rad(FVECT org, FVECT dir, double dmax);
67     static void irrad(FVECT org, FVECT dir);
68     static int printvals(RAY *r);
69     static int getvec(FVECT vec, int fmt, FILE *fp);
70     static void tabin(RAY *r);
71     static void ourtrace(RAY *r);
72 greg 1.1
73 schorsch 2.37 static oputf_t *ray_out[16], *every_out[16];
74     static putf_t *putreal;
75 greg 2.27
76 greg 1.1
77 greg 2.27 void
78 schorsch 2.37 quit( /* quit program */
79     int code
80     )
81 greg 1.1 {
82 greg 2.57 if (ray_pnprocs > 0) /* close children if any */
83     ray_pclose(0);
84     #ifndef NON_POSIX
85 greg 2.58 else if (!ray_pnprocs) {
86     headclean(); /* delete header file */
87     pfclean(); /* clean up persist files */
88     }
89 greg 2.11 #endif
90 greg 1.1 exit(code);
91     }
92    
93    
94 greg 2.57 char *
95 schorsch 2.37 formstr( /* return format identifier */
96     int f
97     )
98 greg 2.6 {
99     switch (f) {
100     case 'a': return("ascii");
101     case 'f': return("float");
102     case 'd': return("double");
103     case 'c': return(COLRFMT);
104     }
105     return("unknown");
106     }
107    
108    
109 schorsch 2.37 extern void
110     rtrace( /* trace rays from file */
111     char *fname
112     )
113 greg 1.1 {
114 greg 2.45 unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
115     : vresolu;
116 greg 1.1 long nextflush = hresolu;
117     FILE *fp;
118 gregl 2.25 double d;
119 greg 1.2 FVECT orig, direc;
120 greg 1.1 /* set up input */
121     if (fname == NULL)
122     fp = stdin;
123     else if ((fp = fopen(fname, "r")) == NULL) {
124     sprintf(errmsg, "cannot open input file \"%s\"", fname);
125     error(SYSTEM, errmsg);
126     }
127 greg 2.8 if (inform != 'a')
128 schorsch 2.30 SET_FILE_BINARY(fp);
129 greg 1.1 /* set up output */
130 greg 2.16 setoutput(outvals);
131 greg 1.1 switch (outform) {
132     case 'a': putreal = puta; break;
133     case 'f': putreal = putf; break;
134     case 'd': putreal = putd; break;
135 greg 2.6 case 'c':
136     if (strcmp(outvals, "v"))
137     error(USER, "color format with value output only");
138     break;
139     default:
140     error(CONSISTENCY, "botched output format");
141 greg 1.1 }
142 greg 2.58 if (ray_pnprocs > 1)
143     ray_fifo_out = printvals;
144 greg 2.12 if (hresolu > 0) {
145     if (vresolu > 0)
146     fprtresolu(hresolu, vresolu, stdout);
147     fflush(stdout);
148     }
149 greg 1.1 /* process file */
150     while (getvec(orig, inform, fp) == 0 &&
151     getvec(direc, inform, fp) == 0) {
152    
153 gregl 2.25 d = normalize(direc);
154     if (d == 0.0) { /* zero ==> flush */
155 greg 2.57 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
156     error(USER, "lost children");
157 gregl 2.24 bogusray();
158 greg 2.45 if (--nextflush <= 0 || !vcount) {
159 gregl 2.24 fflush(stdout);
160     nextflush = hresolu;
161     }
162 greg 2.57 } else { /* compute and print */
163 gregl 2.24 if (imm_irrad)
164     irrad(orig, direc);
165     else
166 gregl 2.25 rad(orig, direc, lim_dist ? d : 0.0);
167 greg 1.7 /* flush if time */
168 greg 2.45 if (!--nextflush) {
169 greg 2.57 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
170     error(USER, "lost children");
171 gregl 2.24 fflush(stdout);
172     nextflush = hresolu;
173     }
174 greg 1.1 }
175     if (ferror(stdout))
176     error(SYSTEM, "write error");
177 greg 2.45 if (vcount && !--vcount) /* check for end */
178 greg 1.1 break;
179     }
180 greg 2.57 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
181     error(USER, "unable to complete processing");
182 greg 2.50 if (fflush(stdout) < 0)
183     error(SYSTEM, "write error");
184 greg 2.45 if (vcount)
185 greg 2.42 error(USER, "unexpected EOF on input");
186 greg 2.12 if (fname != NULL)
187     fclose(fp);
188 greg 1.1 }
189    
190    
191 schorsch 2.37 static void
192 greg 2.40 trace_sources(void) /* trace rays to light sources, also */
193     {
194     int sn;
195    
196     for (sn = 0; sn < nsources; sn++)
197     source[sn].sflags |= SFOLLOW;
198     }
199    
200    
201     static void
202 schorsch 2.37 setoutput( /* set up output tables */
203 greg 2.56 char *vs
204 schorsch 2.37 )
205 greg 1.1 {
206 greg 2.56 oputf_t **table = ray_out;
207 greg 1.1
208 greg 1.11 castonly = 1;
209 greg 1.1 while (*vs)
210     switch (*vs++) {
211 greg 2.40 case 'T': /* trace sources */
212 greg 2.42 if (!*vs) break;
213 greg 2.40 trace_sources();
214     /* fall through */
215 greg 1.1 case 't': /* trace */
216 greg 2.42 if (!*vs) break;
217 greg 1.1 *table = NULL;
218     table = every_out;
219     trace = ourtrace;
220 greg 1.11 castonly = 0;
221 greg 1.1 break;
222     case 'o': /* origin */
223     *table++ = oputo;
224     break;
225     case 'd': /* direction */
226     *table++ = oputd;
227     break;
228     case 'v': /* value */
229     *table++ = oputv;
230 greg 1.11 castonly = 0;
231 greg 1.1 break;
232 greg 2.51 case 'V': /* contribution */
233     *table++ = oputV;
234     if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
235     error(WARNING,
236     "-otV accuracy depends on -aa 0 -as 0");
237     break;
238 greg 2.5 case 'l': /* effective distance */
239 greg 1.1 *table++ = oputl;
240 greg 1.11 castonly = 0;
241 greg 1.1 break;
242 greg 2.29 case 'c': /* local coordinates */
243     *table++ = oputc;
244     break;
245 greg 2.5 case 'L': /* single ray length */
246     *table++ = oputL;
247     break;
248 greg 1.1 case 'p': /* point */
249     *table++ = oputp;
250     break;
251 greg 2.9 case 'n': /* perturbed normal */
252 greg 1.1 *table++ = oputn;
253 greg 2.9 castonly = 0;
254 greg 1.1 break;
255 greg 2.9 case 'N': /* unperturbed normal */
256     *table++ = oputN;
257     break;
258 greg 1.1 case 's': /* surface */
259     *table++ = oputs;
260     break;
261     case 'w': /* weight */
262     *table++ = oputw;
263     break;
264 greg 2.40 case 'W': /* coefficient */
265     *table++ = oputW;
266     if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
267     error(WARNING,
268 greg 2.41 "-otW accuracy depends on -aa 0 -as 0");
269 greg 2.40 break;
270 greg 1.1 case 'm': /* modifier */
271     *table++ = oputm;
272     break;
273 greg 2.39 case 'M': /* material */
274     *table++ = oputM;
275     break;
276 greg 2.42 case '~': /* tilde */
277     *table++ = oputtilde;
278 greg 2.41 break;
279 greg 1.1 }
280     *table = NULL;
281 gregl 2.24 }
282    
283    
284 schorsch 2.37 static void
285     bogusray(void) /* print out empty record */
286 gregl 2.24 {
287     thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] =
288     thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0;
289 greg 2.40 thisray.rmax = 0.0;
290     rayorigin(&thisray, PRIMARY, NULL, NULL);
291 gregl 2.24 printvals(&thisray);
292 greg 1.1 }
293    
294    
295 schorsch 2.37 static void
296     rad( /* compute and print ray value(s) */
297     FVECT org,
298     FVECT dir,
299     double dmax
300     )
301 greg 1.1 {
302     VCOPY(thisray.rorg, org);
303     VCOPY(thisray.rdir, dir);
304 gregl 2.25 thisray.rmax = dmax;
305 greg 2.58 if (!castonly && ray_pnprocs > 1) {
306 greg 2.57 if (ray_fifo_in(&thisray) < 0)
307     error(USER, "lost children");
308     return;
309     }
310 greg 2.40 rayorigin(&thisray, PRIMARY, NULL, NULL);
311 gregl 2.25 if (castonly) {
312 schorsch 2.33 if (!localhit(&thisray, &thescene)) {
313 gregl 2.25 if (thisray.ro == &Aftplane) { /* clipped */
314     thisray.ro = NULL;
315     thisray.rot = FHUGE;
316     } else
317     sourcehit(&thisray);
318 schorsch 2.33 }
319 gregl 2.25 } else
320 greg 2.57 ray_trace(&thisray);
321 greg 2.16 printvals(&thisray);
322 greg 1.1 }
323    
324    
325 schorsch 2.37 static void
326     irrad( /* compute immediate irradiance value */
327     FVECT org,
328     FVECT dir
329     )
330 greg 1.1 {
331 greg 2.56 VSUM(thisray.rorg, org, dir, 1.1e-4);
332     thisray.rdir[0] = -dir[0];
333     thisray.rdir[1] = -dir[1];
334     thisray.rdir[2] = -dir[2];
335 greg 2.40 thisray.rmax = 0.0;
336     rayorigin(&thisray, PRIMARY, NULL, NULL);
337 greg 1.1 /* pretend we hit surface */
338 greg 2.56 thisray.rot = 1e-5;
339 greg 1.1 thisray.rod = 1.0;
340     VCOPY(thisray.ron, dir);
341 greg 2.56 VSUM(thisray.rop, org, dir, 1e-4);
342 greg 1.1 /* compute and print */
343     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
344 greg 2.16 printvals(&thisray);
345     }
346    
347    
348 greg 2.57 static int
349 schorsch 2.37 printvals( /* print requested ray values */
350     RAY *r
351     )
352 greg 2.16 {
353 greg 2.56 oputf_t **tp;
354 greg 2.16
355     if (ray_out[0] == NULL)
356 greg 2.57 return(0);
357 greg 2.16 for (tp = ray_out; *tp != NULL; tp++)
358     (**tp)(r);
359 greg 1.1 if (outform == 'a')
360     putchar('\n');
361 greg 2.57 return(1);
362 greg 1.1 }
363    
364    
365 schorsch 2.37 static int
366     getvec( /* get a vector from fp */
367 greg 2.56 FVECT vec,
368 schorsch 2.37 int fmt,
369     FILE *fp
370     )
371 greg 1.1 {
372     static float vf[3];
373 greg 2.5 static double vd[3];
374 greg 1.19 char buf[32];
375 greg 2.56 int i;
376 greg 1.1
377     switch (fmt) {
378     case 'a': /* ascii */
379 greg 1.19 for (i = 0; i < 3; i++) {
380     if (fgetword(buf, sizeof(buf), fp) == NULL ||
381     !isflt(buf))
382     return(-1);
383     vec[i] = atof(buf);
384     }
385 greg 1.1 break;
386     case 'f': /* binary float */
387 greg 1.8 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
388 greg 1.1 return(-1);
389 greg 2.58 VCOPY(vec, vf);
390 greg 1.1 break;
391     case 'd': /* binary double */
392 greg 2.5 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
393 greg 1.1 return(-1);
394 greg 2.58 VCOPY(vec, vd);
395 greg 1.1 break;
396 greg 2.6 default:
397     error(CONSISTENCY, "botched input format");
398 greg 1.1 }
399     return(0);
400     }
401    
402    
403 greg 2.57 void
404 schorsch 2.37 tranotify( /* record new modifier */
405     OBJECT obj
406     )
407 greg 2.15 {
408     static int hitlimit = 0;
409 greg 2.56 OBJREC *o = objptr(obj);
410     char **tralp;
411 greg 2.15
412 greg 2.27 if (obj == OVOID) { /* starting over */
413     traset[0] = 0;
414     hitlimit = 0;
415     return;
416     }
417 greg 2.15 if (hitlimit || !ismodifier(o->otype))
418     return;
419     for (tralp = tralist; *tralp != NULL; tralp++)
420     if (!strcmp(o->oname, *tralp)) {
421     if (traset[0] >= MAXTSET) {
422     error(WARNING, "too many modifiers in trace list");
423     hitlimit++;
424     return; /* should this be fatal? */
425     }
426     insertelem(traset, obj);
427     return;
428     }
429     }
430    
431    
432 greg 2.27 static void
433 schorsch 2.37 ourtrace( /* print ray values */
434     RAY *r
435     )
436 greg 1.1 {
437 greg 2.56 oputf_t **tp;
438 greg 1.1
439     if (every_out[0] == NULL)
440 greg 2.15 return;
441 greg 2.16 if (r->ro == NULL) {
442     if (traincl == 1)
443     return;
444     } else if (traincl != -1 && traincl != inset(traset, r->ro->omod))
445 greg 1.1 return;
446     tabin(r);
447     for (tp = every_out; *tp != NULL; tp++)
448     (**tp)(r);
449 greg 2.41 if (outform == 'a')
450     putchar('\n');
451 greg 1.1 }
452    
453    
454 greg 2.27 static void
455 schorsch 2.37 tabin( /* tab in appropriate amount */
456     RAY *r
457     )
458 greg 1.1 {
459 greg 2.40 const RAY *rp;
460 greg 1.1
461     for (rp = r->parent; rp != NULL; rp = rp->parent)
462     putchar('\t');
463     }
464    
465    
466 greg 2.27 static void
467 schorsch 2.37 oputo( /* print origin */
468     RAY *r
469     )
470 greg 1.1 {
471     (*putreal)(r->rorg[0]);
472     (*putreal)(r->rorg[1]);
473     (*putreal)(r->rorg[2]);
474     }
475    
476    
477 greg 2.27 static void
478 schorsch 2.37 oputd( /* print direction */
479     RAY *r
480     )
481 greg 1.1 {
482     (*putreal)(r->rdir[0]);
483     (*putreal)(r->rdir[1]);
484     (*putreal)(r->rdir[2]);
485     }
486    
487    
488 greg 2.27 static void
489 schorsch 2.37 oputv( /* print value */
490     RAY *r
491     )
492 greg 1.1 {
493 greg 2.6 if (outform == 'c') {
494 greg 2.41 COLR cout;
495 greg 2.6 setcolr(cout, colval(r->rcol,RED),
496     colval(r->rcol,GRN),
497     colval(r->rcol,BLU));
498     fwrite((char *)cout, sizeof(cout), 1, stdout);
499     return;
500     }
501 greg 1.1 (*putreal)(colval(r->rcol,RED));
502     (*putreal)(colval(r->rcol,GRN));
503     (*putreal)(colval(r->rcol,BLU));
504     }
505    
506    
507 greg 2.27 static void
508 greg 2.51 oputV( /* print value contribution */
509     RAY *r
510     )
511     {
512     double contr[3];
513    
514     raycontrib(contr, r, PRIMARY);
515     multcolor(contr, r->rcol);
516     (*putreal)(contr[RED]);
517     (*putreal)(contr[GRN]);
518     (*putreal)(contr[BLU]);
519     }
520    
521    
522     static void
523 schorsch 2.37 oputl( /* print effective distance */
524     RAY *r
525     )
526 greg 1.1 {
527 greg 1.9 (*putreal)(r->rt);
528 greg 2.5 }
529    
530    
531 greg 2.27 static void
532 schorsch 2.37 oputL( /* print single ray length */
533     RAY *r
534     )
535 greg 2.5 {
536     (*putreal)(r->rot);
537 greg 1.1 }
538    
539    
540 greg 2.27 static void
541 schorsch 2.37 oputc( /* print local coordinates */
542     RAY *r
543     )
544 greg 2.29 {
545     (*putreal)(r->uv[0]);
546     (*putreal)(r->uv[1]);
547     }
548    
549    
550     static void
551 schorsch 2.37 oputp( /* print point */
552     RAY *r
553     )
554 greg 1.1 {
555     if (r->rot < FHUGE) {
556     (*putreal)(r->rop[0]);
557     (*putreal)(r->rop[1]);
558     (*putreal)(r->rop[2]);
559     } else {
560     (*putreal)(0.0);
561     (*putreal)(0.0);
562     (*putreal)(0.0);
563     }
564     }
565    
566    
567 greg 2.27 static void
568 schorsch 2.37 oputN( /* print unperturbed normal */
569     RAY *r
570     )
571 greg 1.1 {
572     if (r->rot < FHUGE) {
573     (*putreal)(r->ron[0]);
574     (*putreal)(r->ron[1]);
575     (*putreal)(r->ron[2]);
576     } else {
577     (*putreal)(0.0);
578     (*putreal)(0.0);
579     (*putreal)(0.0);
580     }
581 greg 2.9 }
582    
583    
584 greg 2.27 static void
585 schorsch 2.37 oputn( /* print perturbed normal */
586     RAY *r
587     )
588 greg 2.9 {
589     FVECT pnorm;
590    
591     if (r->rot >= FHUGE) {
592     (*putreal)(0.0);
593     (*putreal)(0.0);
594     (*putreal)(0.0);
595     return;
596     }
597     raynormal(pnorm, r);
598     (*putreal)(pnorm[0]);
599     (*putreal)(pnorm[1]);
600     (*putreal)(pnorm[2]);
601 greg 1.1 }
602    
603    
604 greg 2.27 static void
605 schorsch 2.37 oputs( /* print name */
606     RAY *r
607     )
608 greg 1.1 {
609     if (r->ro != NULL)
610     fputs(r->ro->oname, stdout);
611     else
612     putchar('*');
613     putchar('\t');
614     }
615    
616    
617 greg 2.27 static void
618 schorsch 2.37 oputw( /* print weight */
619     RAY *r
620     )
621 greg 1.1 {
622     (*putreal)(r->rweight);
623     }
624    
625    
626 greg 2.27 static void
627 greg 2.51 oputW( /* print coefficient */
628 greg 2.40 RAY *r
629     )
630     {
631 greg 2.48 double contr[3];
632 greg 2.40
633     raycontrib(contr, r, PRIMARY);
634 greg 2.48 (*putreal)(contr[RED]);
635     (*putreal)(contr[GRN]);
636     (*putreal)(contr[BLU]);
637 greg 2.40 }
638    
639    
640     static void
641 schorsch 2.37 oputm( /* print modifier */
642     RAY *r
643     )
644 greg 1.1 {
645     if (r->ro != NULL)
646 greg 2.23 if (r->ro->omod != OVOID)
647     fputs(objptr(r->ro->omod)->oname, stdout);
648     else
649     fputs(VOIDID, stdout);
650 greg 1.1 else
651     putchar('*');
652     putchar('\t');
653     }
654    
655    
656 greg 2.27 static void
657 greg 2.39 oputM( /* print material */
658     RAY *r
659     )
660     {
661     OBJREC *mat;
662    
663     if (r->ro != NULL) {
664     if ((mat = findmaterial(r->ro)) != NULL)
665     fputs(mat->oname, stdout);
666     else
667     fputs(VOIDID, stdout);
668     } else
669     putchar('*');
670     putchar('\t');
671     }
672    
673    
674     static void
675 greg 2.42 oputtilde( /* output tilde (spacer) */
676 greg 2.41 RAY *r
677     )
678     {
679 greg 2.42 fputs("~\t", stdout);
680 greg 2.41 }
681    
682    
683     static void
684 schorsch 2.37 puta( /* print ascii value */
685     double v
686     )
687 greg 1.1 {
688     printf("%e\t", v);
689     }
690    
691    
692 greg 2.27 static void
693 greg 1.1 putd(v) /* print binary double */
694     double v;
695     {
696 greg 1.8 fwrite((char *)&v, sizeof(v), 1, stdout);
697 greg 1.1 }
698    
699    
700 greg 2.27 static void
701 greg 1.1 putf(v) /* print binary float */
702     double v;
703     {
704     float f = v;
705    
706 greg 1.8 fwrite((char *)&f, sizeof(f), 1, stdout);
707 greg 1.1 }