ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.55
Committed: Sat Nov 17 06:21:58 2007 UTC (16 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R9
Changes since 2.54: +7 -3 lines
Log Message:
Increased limits to number of modifiers for trace and ambient include/exclude

File Contents

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