ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.39
Committed: Thu Apr 14 18:04:12 2005 UTC (19 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +23 -2 lines
Log Message:
Added -oM option to rtrace to output material

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.39 static const char RCSid[] = "$Id: rtrace.c,v 2.38 2004/11/05 17:36:56 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     static oputf_t oputo, oputd, oputv, oputl, oputL, oputc,
100 greg 2.39 oputp, oputn, oputN, oputs, oputw, oputm, oputM;
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     setoutput( /* set up output tables */
224     register char *vs
225     )
226 greg 1.1 {
227 schorsch 2.37 register oputf_t **table = ray_out;
228 greg 1.1
229 greg 1.11 castonly = 1;
230 greg 1.1 while (*vs)
231     switch (*vs++) {
232     case 't': /* trace */
233     *table = NULL;
234     table = every_out;
235     trace = ourtrace;
236 greg 1.11 castonly = 0;
237 greg 1.1 break;
238     case 'o': /* origin */
239     *table++ = oputo;
240     break;
241     case 'd': /* direction */
242     *table++ = oputd;
243     break;
244     case 'v': /* value */
245     *table++ = oputv;
246 greg 1.11 castonly = 0;
247 greg 1.1 break;
248 greg 2.5 case 'l': /* effective distance */
249 greg 1.1 *table++ = oputl;
250 greg 1.11 castonly = 0;
251 greg 1.1 break;
252 greg 2.29 case 'c': /* local coordinates */
253     *table++ = oputc;
254     break;
255 greg 2.5 case 'L': /* single ray length */
256     *table++ = oputL;
257     break;
258 greg 1.1 case 'p': /* point */
259     *table++ = oputp;
260     break;
261 greg 2.9 case 'n': /* perturbed normal */
262 greg 1.1 *table++ = oputn;
263 greg 2.9 castonly = 0;
264 greg 1.1 break;
265 greg 2.9 case 'N': /* unperturbed normal */
266     *table++ = oputN;
267     break;
268 greg 1.1 case 's': /* surface */
269     *table++ = oputs;
270     break;
271     case 'w': /* weight */
272     *table++ = oputw;
273     break;
274     case 'm': /* modifier */
275     *table++ = oputm;
276     break;
277 greg 2.39 case 'M': /* material */
278     *table++ = oputM;
279     break;
280 greg 1.1 }
281     *table = NULL;
282 gregl 2.24 }
283    
284    
285 schorsch 2.37 static void
286     bogusray(void) /* print out empty record */
287 gregl 2.24 {
288     thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] =
289     thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0;
290     rayorigin(&thisray, NULL, PRIMARY, 1.0);
291     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 1.1 rayorigin(&thisray, NULL, PRIMARY, 1.0);
306 gregl 2.25 if (castonly) {
307 schorsch 2.33 if (!localhit(&thisray, &thescene)) {
308 gregl 2.25 if (thisray.ro == &Aftplane) { /* clipped */
309     thisray.ro = NULL;
310     thisray.rot = FHUGE;
311     } else
312     sourcehit(&thisray);
313 schorsch 2.33 }
314 gregl 2.25 } else
315 greg 1.11 rayvalue(&thisray);
316 greg 2.16 printvals(&thisray);
317 greg 1.1 }
318    
319    
320 schorsch 2.37 static void
321     irrad( /* compute immediate irradiance value */
322     FVECT org,
323     FVECT dir
324     )
325 greg 1.1 {
326     register int i;
327    
328     for (i = 0; i < 3; i++) {
329     thisray.rorg[i] = org[i] + dir[i];
330     thisray.rdir[i] = -dir[i];
331     }
332     rayorigin(&thisray, NULL, PRIMARY, 1.0);
333     /* pretend we hit surface */
334 greg 2.16 thisray.rot = 1.0-1e-4;
335 greg 1.1 thisray.rod = 1.0;
336     VCOPY(thisray.ron, dir);
337     for (i = 0; i < 3; i++) /* fudge factor */
338     thisray.rop[i] = org[i] + 1e-4*dir[i];
339     /* compute and print */
340     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
341 greg 2.16 printvals(&thisray);
342     }
343    
344    
345 schorsch 2.37 static void
346     printvals( /* print requested ray values */
347     RAY *r
348     )
349 greg 2.16 {
350 schorsch 2.37 register oputf_t **tp;
351 greg 2.16
352     if (ray_out[0] == NULL)
353     return;
354     for (tp = ray_out; *tp != NULL; tp++)
355     (**tp)(r);
356 greg 1.1 if (outform == 'a')
357     putchar('\n');
358     }
359    
360    
361 schorsch 2.37 static int
362     getvec( /* get a vector from fp */
363     register FVECT vec,
364     int fmt,
365     FILE *fp
366     )
367 greg 1.1 {
368     static float vf[3];
369 greg 2.5 static double vd[3];
370 greg 1.19 char buf[32];
371     register int i;
372 greg 1.1
373     switch (fmt) {
374     case 'a': /* ascii */
375 greg 1.19 for (i = 0; i < 3; i++) {
376     if (fgetword(buf, sizeof(buf), fp) == NULL ||
377     !isflt(buf))
378     return(-1);
379     vec[i] = atof(buf);
380     }
381 greg 1.1 break;
382     case 'f': /* binary float */
383 greg 1.8 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
384 greg 1.1 return(-1);
385     vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
386     break;
387     case 'd': /* binary double */
388 greg 2.5 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
389 greg 1.1 return(-1);
390 greg 2.5 vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2];
391 greg 1.1 break;
392 greg 2.6 default:
393     error(CONSISTENCY, "botched input format");
394 greg 1.1 }
395     return(0);
396     }
397    
398    
399 schorsch 2.37 static void
400     tranotify( /* record new modifier */
401     OBJECT obj
402     )
403 greg 2.15 {
404     static int hitlimit = 0;
405     register OBJREC *o = objptr(obj);
406     register char **tralp;
407    
408 greg 2.27 if (obj == OVOID) { /* starting over */
409     traset[0] = 0;
410     hitlimit = 0;
411     return;
412     }
413 greg 2.15 if (hitlimit || !ismodifier(o->otype))
414     return;
415     for (tralp = tralist; *tralp != NULL; tralp++)
416     if (!strcmp(o->oname, *tralp)) {
417     if (traset[0] >= MAXTSET) {
418     error(WARNING, "too many modifiers in trace list");
419     hitlimit++;
420     return; /* should this be fatal? */
421     }
422     insertelem(traset, obj);
423     return;
424     }
425     }
426    
427    
428 greg 2.27 static void
429 schorsch 2.37 ourtrace( /* print ray values */
430     RAY *r
431     )
432 greg 1.1 {
433 schorsch 2.37 register oputf_t **tp;
434 greg 1.1
435     if (every_out[0] == NULL)
436 greg 2.15 return;
437 greg 2.16 if (r->ro == NULL) {
438     if (traincl == 1)
439     return;
440     } else if (traincl != -1 && traincl != inset(traset, r->ro->omod))
441 greg 1.1 return;
442     tabin(r);
443     for (tp = every_out; *tp != NULL; tp++)
444     (**tp)(r);
445     putchar('\n');
446     }
447    
448    
449 greg 2.27 static void
450 schorsch 2.37 tabin( /* tab in appropriate amount */
451     RAY *r
452     )
453 greg 1.1 {
454     register RAY *rp;
455    
456     for (rp = r->parent; rp != NULL; rp = rp->parent)
457     putchar('\t');
458     }
459    
460    
461 greg 2.27 static void
462 schorsch 2.37 oputo( /* print origin */
463     RAY *r
464     )
465 greg 1.1 {
466     (*putreal)(r->rorg[0]);
467     (*putreal)(r->rorg[1]);
468     (*putreal)(r->rorg[2]);
469     }
470    
471    
472 greg 2.27 static void
473 schorsch 2.37 oputd( /* print direction */
474     RAY *r
475     )
476 greg 1.1 {
477     (*putreal)(r->rdir[0]);
478     (*putreal)(r->rdir[1]);
479     (*putreal)(r->rdir[2]);
480     }
481    
482    
483 greg 2.27 static void
484 schorsch 2.37 oputv( /* print value */
485     RAY *r
486     )
487 greg 1.1 {
488 greg 2.6 COLR cout;
489    
490     if (outform == 'c') {
491     setcolr(cout, colval(r->rcol,RED),
492     colval(r->rcol,GRN),
493     colval(r->rcol,BLU));
494     fwrite((char *)cout, sizeof(cout), 1, stdout);
495     return;
496     }
497 greg 1.1 (*putreal)(colval(r->rcol,RED));
498     (*putreal)(colval(r->rcol,GRN));
499     (*putreal)(colval(r->rcol,BLU));
500     }
501    
502    
503 greg 2.27 static void
504 schorsch 2.37 oputl( /* print effective distance */
505     RAY *r
506     )
507 greg 1.1 {
508 greg 1.9 (*putreal)(r->rt);
509 greg 2.5 }
510    
511    
512 greg 2.27 static void
513 schorsch 2.37 oputL( /* print single ray length */
514     RAY *r
515     )
516 greg 2.5 {
517     (*putreal)(r->rot);
518 greg 1.1 }
519    
520    
521 greg 2.27 static void
522 schorsch 2.37 oputc( /* print local coordinates */
523     RAY *r
524     )
525 greg 2.29 {
526     (*putreal)(r->uv[0]);
527     (*putreal)(r->uv[1]);
528     }
529    
530    
531     static void
532 schorsch 2.37 oputp( /* print point */
533     RAY *r
534     )
535 greg 1.1 {
536     if (r->rot < FHUGE) {
537     (*putreal)(r->rop[0]);
538     (*putreal)(r->rop[1]);
539     (*putreal)(r->rop[2]);
540     } else {
541     (*putreal)(0.0);
542     (*putreal)(0.0);
543     (*putreal)(0.0);
544     }
545     }
546    
547    
548 greg 2.27 static void
549 schorsch 2.37 oputN( /* print unperturbed normal */
550     RAY *r
551     )
552 greg 1.1 {
553     if (r->rot < FHUGE) {
554     (*putreal)(r->ron[0]);
555     (*putreal)(r->ron[1]);
556     (*putreal)(r->ron[2]);
557     } else {
558     (*putreal)(0.0);
559     (*putreal)(0.0);
560     (*putreal)(0.0);
561     }
562 greg 2.9 }
563    
564    
565 greg 2.27 static void
566 schorsch 2.37 oputn( /* print perturbed normal */
567     RAY *r
568     )
569 greg 2.9 {
570     FVECT pnorm;
571    
572     if (r->rot >= FHUGE) {
573     (*putreal)(0.0);
574     (*putreal)(0.0);
575     (*putreal)(0.0);
576     return;
577     }
578     raynormal(pnorm, r);
579     (*putreal)(pnorm[0]);
580     (*putreal)(pnorm[1]);
581     (*putreal)(pnorm[2]);
582 greg 1.1 }
583    
584    
585 greg 2.27 static void
586 schorsch 2.37 oputs( /* print name */
587     RAY *r
588     )
589 greg 1.1 {
590     if (r->ro != NULL)
591     fputs(r->ro->oname, stdout);
592     else
593     putchar('*');
594     putchar('\t');
595     }
596    
597    
598 greg 2.27 static void
599 schorsch 2.37 oputw( /* print weight */
600     RAY *r
601     )
602 greg 1.1 {
603     (*putreal)(r->rweight);
604     }
605    
606    
607 greg 2.27 static void
608 schorsch 2.37 oputm( /* print modifier */
609     RAY *r
610     )
611 greg 1.1 {
612     if (r->ro != NULL)
613 greg 2.23 if (r->ro->omod != OVOID)
614     fputs(objptr(r->ro->omod)->oname, stdout);
615     else
616     fputs(VOIDID, stdout);
617 greg 1.1 else
618     putchar('*');
619     putchar('\t');
620     }
621    
622    
623 greg 2.27 static void
624 greg 2.39 oputM( /* print material */
625     RAY *r
626     )
627     {
628     OBJREC *mat;
629    
630     if (r->ro != NULL) {
631     if ((mat = findmaterial(r->ro)) != NULL)
632     fputs(mat->oname, stdout);
633     else
634     fputs(VOIDID, stdout);
635     } else
636     putchar('*');
637     putchar('\t');
638     }
639    
640    
641     static void
642 schorsch 2.37 puta( /* print ascii value */
643     double v
644     )
645 greg 1.1 {
646     printf("%e\t", v);
647     }
648    
649    
650 greg 2.27 static void
651 greg 1.1 putd(v) /* print binary double */
652     double v;
653     {
654 greg 1.8 fwrite((char *)&v, sizeof(v), 1, stdout);
655 greg 1.1 }
656    
657    
658 greg 2.27 static void
659 greg 1.1 putf(v) /* print binary float */
660     double v;
661     {
662     float f = v;
663    
664 greg 1.8 fwrite((char *)&f, sizeof(f), 1, stdout);
665 greg 1.1 }