ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.41
Committed: Wed May 25 04:44:26 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +19 -14 lines
Log Message:
Created rtcontrib program for computing ray contributions and coefficients

File Contents

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