ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.61
Committed: Mon Dec 14 04:49:16 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.60: +2 -1 lines
Log Message:
Important bug fix in -I calculation

File Contents

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