ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.46
Committed: Fri Jun 10 20:44:00 2005 UTC (18 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.45: +4 -2 lines
Log Message:
Fixe in rtcontrib -M option

File Contents

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