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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtrace.c,v 2.45 2005/06/10 16:49:42 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
33 CUBE thescene; /* our scene */
34 OBJECT nsceneobjs; /* number of objects in our scene */
35
36 int dimlist[MAXDIM]; /* sampling dimensions */
37 int ndims = 0; /* number of sampling dimensions */
38 int samplendx = 0; /* index for this sample */
39
40 int imm_irrad = 0; /* compute immediate irradiance? */
41 int lim_dist = 0; /* limit distance? */
42
43 int inform = 'a'; /* input format */
44 int outform = 'a'; /* output format */
45 char *outvals = "v"; /* output specification */
46
47 int do_irrad = 0; /* compute irradiance? */
48
49 void (*trace)() = NULL; /* trace call */
50
51 char *tralist[128]; /* list of modifers to trace (or no) */
52 int traincl = -1; /* include == 1, exclude == 0 */
53 #ifndef MAXTSET
54 #define MAXTSET 1024 /* maximum number in trace set */
55 #endif
56 OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */
57
58 int hresolu = 0; /* horizontal (scan) size */
59 int vresolu = 0; /* vertical resolution */
60
61 double dstrsrc = 0.0; /* square source distribution */
62 double shadthresh = .03; /* shadow threshold */
63 double shadcert = .75; /* shadow certainty */
64 int directrelay = 2; /* number of source relays */
65 int vspretest = 512; /* virtual source pretest density */
66 int directvis = 1; /* sources visible? */
67 double srcsizerat = .2; /* maximum ratio source size/dist. */
68
69 COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
70 COLOR salbedo = BLKCOLOR; /* global scattering albedo */
71 double seccg = 0.; /* global scattering eccentricity */
72 double ssampdist = 0.; /* scatter sampling distance */
73
74 double specthresh = .15; /* specular sampling threshold */
75 double specjitter = 1.; /* specular sampling jitter */
76
77 int backvis = 1; /* back face visibility */
78
79 int maxdepth = -10; /* maximum recursion depth */
80 double minweight = 2e-3; /* minimum ray weight */
81
82 char *ambfile = NULL; /* ambient file name */
83 COLOR ambval = BLKCOLOR; /* ambient value */
84 int ambvwt = 0; /* initial weight for ambient value */
85 double ambacc = 0.15; /* ambient accuracy */
86 int ambres = 256; /* ambient resolution */
87 int ambdiv = 1024; /* ambient divisions */
88 int ambssamp = 512; /* ambient super-samples */
89 int ambounce = 0; /* ambient bounces */
90 char *amblist[AMBLLEN]; /* ambient include/exclude list */
91 int ambincl = -1; /* include == 1, exclude == 0 */
92
93 static int castonly = 0;
94
95 static RAY thisray; /* for our convenience */
96
97 typedef void putf_t(double v);
98 static putf_t puta, putd, putf;
99
100 typedef void oputf_t(RAY *r);
101 static oputf_t oputo, oputd, oputv, oputl, oputL, oputc, oputp,
102 oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde;
103
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
114 static oputf_t *ray_out[16], *every_out[16];
115 static putf_t *putreal;
116
117 void (*addobjnotify[])() = {ambnotify, tranotify, NULL};
118
119
120 void
121 quit( /* quit program */
122 int code
123 )
124 {
125 #ifndef NON_POSIX /* XXX we don't clean up elsewhere? */
126 headclean(); /* delete header file */
127 pfclean(); /* clean up persist files */
128 #endif
129 exit(code);
130 }
131
132
133 extern char *
134 formstr( /* return format identifier */
135 int f
136 )
137 {
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 extern void
149 rtrace( /* trace rays from file */
150 char *fname
151 )
152 {
153 unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
154 : vresolu;
155 long nextflush = hresolu;
156 FILE *fp;
157 double d;
158 FVECT orig, direc;
159 /* 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 if (inform != 'a')
167 SET_FILE_BINARY(fp);
168 /* set up output */
169 setoutput(outvals);
170 switch (outform) {
171 case 'a': putreal = puta; break;
172 case 'f': putreal = putf; break;
173 case 'd': putreal = putd; break;
174 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 }
181 if (hresolu > 0) {
182 if (vresolu > 0)
183 fprtresolu(hresolu, vresolu, stdout);
184 fflush(stdout);
185 }
186 /* process file */
187 while (getvec(orig, inform, fp) == 0 &&
188 getvec(direc, inform, fp) == 0) {
189
190 d = normalize(direc);
191 if (d == 0.0) { /* zero ==> flush */
192 bogusray();
193 if (--nextflush <= 0 || !vcount) {
194 fflush(stdout);
195 nextflush = hresolu;
196 }
197 } else {
198 samplendx++;
199 /* compute and print */
200 if (imm_irrad)
201 irrad(orig, direc);
202 else
203 rad(orig, direc, lim_dist ? d : 0.0);
204 /* flush if time */
205 if (!--nextflush) {
206 fflush(stdout);
207 nextflush = hresolu;
208 }
209 }
210 if (ferror(stdout))
211 error(SYSTEM, "write error");
212 if (vcount && !--vcount) /* check for end */
213 break;
214 }
215 fflush(stdout);
216 if (vcount)
217 error(USER, "unexpected EOF on input");
218 if (fname != NULL)
219 fclose(fp);
220 }
221
222
223 static void
224 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 setoutput( /* set up output tables */
235 register char *vs
236 )
237 {
238 register oputf_t **table = ray_out;
239
240 castonly = 1;
241 while (*vs)
242 switch (*vs++) {
243 case 'T': /* trace sources */
244 if (!*vs) break;
245 trace_sources();
246 /* fall through */
247 case 't': /* trace */
248 if (!*vs) break;
249 *table = NULL;
250 table = every_out;
251 trace = ourtrace;
252 castonly = 0;
253 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 castonly = 0;
263 break;
264 case 'l': /* effective distance */
265 *table++ = oputl;
266 castonly = 0;
267 break;
268 case 'c': /* local coordinates */
269 *table++ = oputc;
270 break;
271 case 'L': /* single ray length */
272 *table++ = oputL;
273 break;
274 case 'p': /* point */
275 *table++ = oputp;
276 break;
277 case 'n': /* perturbed normal */
278 *table++ = oputn;
279 castonly = 0;
280 break;
281 case 'N': /* unperturbed normal */
282 *table++ = oputN;
283 break;
284 case 's': /* surface */
285 *table++ = oputs;
286 break;
287 case 'w': /* weight */
288 *table++ = oputw;
289 break;
290 case 'W': /* coefficient */
291 *table++ = oputW;
292 if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
293 error(WARNING,
294 "-otW accuracy depends on -aa 0 -as 0");
295 break;
296 case 'm': /* modifier */
297 *table++ = oputm;
298 break;
299 case 'M': /* material */
300 *table++ = oputM;
301 break;
302 case '~': /* tilde */
303 *table++ = oputtilde;
304 break;
305 }
306 *table = NULL;
307 }
308
309
310 static void
311 bogusray(void) /* print out empty record */
312 {
313 thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] =
314 thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0;
315 thisray.rmax = 0.0;
316 rayorigin(&thisray, PRIMARY, NULL, NULL);
317 printvals(&thisray);
318 }
319
320
321 static void
322 rad( /* compute and print ray value(s) */
323 FVECT org,
324 FVECT dir,
325 double dmax
326 )
327 {
328 VCOPY(thisray.rorg, org);
329 VCOPY(thisray.rdir, dir);
330 thisray.rmax = dmax;
331 rayorigin(&thisray, PRIMARY, NULL, NULL);
332 if (castonly) {
333 if (!localhit(&thisray, &thescene)) {
334 if (thisray.ro == &Aftplane) { /* clipped */
335 thisray.ro = NULL;
336 thisray.rot = FHUGE;
337 } else
338 sourcehit(&thisray);
339 }
340 } else
341 rayvalue(&thisray);
342 printvals(&thisray);
343 }
344
345
346 static void
347 irrad( /* compute immediate irradiance value */
348 FVECT org,
349 FVECT dir
350 )
351 {
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 thisray.rmax = 0.0;
359 rayorigin(&thisray, PRIMARY, NULL, NULL);
360 /* pretend we hit surface */
361 thisray.rot = 1.0-1e-4;
362 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 printvals(&thisray);
369 }
370
371
372 static void
373 printvals( /* print requested ray values */
374 RAY *r
375 )
376 {
377 register oputf_t **tp;
378
379 if (ray_out[0] == NULL)
380 return;
381 for (tp = ray_out; *tp != NULL; tp++)
382 (**tp)(r);
383 if (outform == 'a')
384 putchar('\n');
385 }
386
387
388 static int
389 getvec( /* get a vector from fp */
390 register FVECT vec,
391 int fmt,
392 FILE *fp
393 )
394 {
395 static float vf[3];
396 static double vd[3];
397 char buf[32];
398 register int i;
399
400 switch (fmt) {
401 case 'a': /* ascii */
402 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 break;
409 case 'f': /* binary float */
410 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
411 return(-1);
412 vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
413 break;
414 case 'd': /* binary double */
415 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
416 return(-1);
417 vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2];
418 break;
419 default:
420 error(CONSISTENCY, "botched input format");
421 }
422 return(0);
423 }
424
425
426 static void
427 tranotify( /* record new modifier */
428 OBJECT obj
429 )
430 {
431 static int hitlimit = 0;
432 register OBJREC *o = objptr(obj);
433 register char **tralp;
434
435 if (obj == OVOID) { /* starting over */
436 traset[0] = 0;
437 hitlimit = 0;
438 return;
439 }
440 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 static void
456 ourtrace( /* print ray values */
457 RAY *r
458 )
459 {
460 register oputf_t **tp;
461
462 if (every_out[0] == NULL)
463 return;
464 if (r->ro == NULL) {
465 if (traincl == 1)
466 return;
467 } else if (traincl != -1 && traincl != inset(traset, r->ro->omod))
468 return;
469 tabin(r);
470 for (tp = every_out; *tp != NULL; tp++)
471 (**tp)(r);
472 if (outform == 'a')
473 putchar('\n');
474 }
475
476
477 static void
478 tabin( /* tab in appropriate amount */
479 RAY *r
480 )
481 {
482 const RAY *rp;
483
484 for (rp = r->parent; rp != NULL; rp = rp->parent)
485 putchar('\t');
486 }
487
488
489 static void
490 oputo( /* print origin */
491 RAY *r
492 )
493 {
494 (*putreal)(r->rorg[0]);
495 (*putreal)(r->rorg[1]);
496 (*putreal)(r->rorg[2]);
497 }
498
499
500 static void
501 oputd( /* print direction */
502 RAY *r
503 )
504 {
505 (*putreal)(r->rdir[0]);
506 (*putreal)(r->rdir[1]);
507 (*putreal)(r->rdir[2]);
508 }
509
510
511 static void
512 oputv( /* print value */
513 RAY *r
514 )
515 {
516 if (outform == 'c') {
517 COLR cout;
518 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 (*putreal)(colval(r->rcol,RED));
525 (*putreal)(colval(r->rcol,GRN));
526 (*putreal)(colval(r->rcol,BLU));
527 }
528
529
530 static void
531 oputl( /* print effective distance */
532 RAY *r
533 )
534 {
535 (*putreal)(r->rt);
536 }
537
538
539 static void
540 oputL( /* print single ray length */
541 RAY *r
542 )
543 {
544 (*putreal)(r->rot);
545 }
546
547
548 static void
549 oputc( /* print local coordinates */
550 RAY *r
551 )
552 {
553 (*putreal)(r->uv[0]);
554 (*putreal)(r->uv[1]);
555 }
556
557
558 static void
559 oputp( /* print point */
560 RAY *r
561 )
562 {
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 static void
576 oputN( /* print unperturbed normal */
577 RAY *r
578 )
579 {
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 }
590
591
592 static void
593 oputn( /* print perturbed normal */
594 RAY *r
595 )
596 {
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 }
610
611
612 static void
613 oputs( /* print name */
614 RAY *r
615 )
616 {
617 if (r->ro != NULL)
618 fputs(r->ro->oname, stdout);
619 else
620 putchar('*');
621 putchar('\t');
622 }
623
624
625 static void
626 oputw( /* print weight */
627 RAY *r
628 )
629 {
630 (*putreal)(r->rweight);
631 }
632
633
634 static void
635 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 oputm( /* print modifier */
650 RAY *r
651 )
652 {
653 if (r->ro != NULL)
654 if (r->ro->omod != OVOID)
655 fputs(objptr(r->ro->omod)->oname, stdout);
656 else
657 fputs(VOIDID, stdout);
658 else
659 putchar('*');
660 putchar('\t');
661 }
662
663
664 static void
665 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 oputtilde( /* output tilde (spacer) */
684 RAY *r
685 )
686 {
687 fputs("~\t", stdout);
688 }
689
690
691 static void
692 puta( /* print ascii value */
693 double v
694 )
695 {
696 printf("%e\t", v);
697 }
698
699
700 static void
701 putd(v) /* print binary double */
702 double v;
703 {
704 fwrite((char *)&v, sizeof(v), 1, stdout);
705 }
706
707
708 static void
709 putf(v) /* print binary float */
710 double v;
711 {
712 float f = v;
713
714 fwrite((char *)&f, sizeof(f), 1, stdout);
715 }