ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.15
Committed: Fri Jun 18 10:14:42 1993 UTC (30 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +31 -0 lines
Log Message:
added -ti, -te, -tI and -tE options to rtrace for exclude/include traces

File Contents

# User Rev Content
1 greg 2.6 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rtrace.c - program and variables for individual ray tracing.
9     *
10     * 6/11/86
11     */
12    
13     /*
14     * Input is in the form:
15     *
16     * xorg yorg zorg xdir ydir zdir
17     *
18     * The direction need not be normalized. Output is flexible.
19 greg 1.7 * If the direction vector is (0,0,0), then the output is flushed.
20 greg 1.1 * All values default to ascii representation of real
21     * numbers. Binary representations can be selected
22     * with '-ff' for float or '-fd' for double. By default,
23 greg 1.13 * radiance is computed. The '-i' or '-I' options indicate that
24 greg 1.1 * irradiance values are desired.
25     */
26    
27     #include "ray.h"
28    
29 greg 1.11 #include "octree.h"
30    
31 greg 1.1 #include "otypes.h"
32    
33 greg 2.6 #include "resolu.h"
34    
35 greg 1.14 int dimlist[MAXDIM]; /* sampling dimensions */
36     int ndims = 0; /* number of sampling dimensions */
37     int samplendx = 0; /* index for this sample */
38    
39 greg 1.13 int imm_irrad = 0; /* compute immediate irradiance? */
40    
41 greg 1.1 int inform = 'a'; /* input format */
42     int outform = 'a'; /* output format */
43     char *outvals = "v"; /* output specification */
44    
45 greg 2.15 char *tralist[128]; /* list of modifers to trace (or no) */
46     int traincl = -1; /* include == 1, exclude == 0 */
47     #define MAXTSET 511 /* maximum number in trace set */
48     OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */
49    
50 greg 1.1 int hresolu = 0; /* horizontal (scan) size */
51     int vresolu = 0; /* vertical resolution */
52    
53     double dstrsrc = 0.0; /* square source distribution */
54 greg 1.4 double shadthresh = .05; /* shadow threshold */
55 greg 1.5 double shadcert = .5; /* shadow certainty */
56 greg 1.20 int directrelay = 1; /* number of source relays */
57 greg 1.17 int vspretest = 512; /* virtual source pretest density */
58 greg 2.10 int directvis = 1; /* sources visible? */
59 greg 1.20 double srcsizerat = .25; /* maximum ratio source size/dist. */
60 greg 1.1
61 greg 2.4 double specthresh = .15; /* specular sampling threshold */
62 greg 2.3 double specjitter = 1.; /* specular sampling jitter */
63    
64 greg 1.1 int maxdepth = 6; /* maximum recursion depth */
65     double minweight = 4e-3; /* minimum ray weight */
66    
67     COLOR ambval = BLKCOLOR; /* ambient value */
68     double ambacc = 0.2; /* ambient accuracy */
69 greg 1.6 int ambres = 32; /* ambient resolution */
70 greg 1.1 int ambdiv = 128; /* ambient divisions */
71     int ambssamp = 0; /* ambient super-samples */
72     int ambounce = 0; /* ambient bounces */
73     char *amblist[128]; /* ambient include/exclude list */
74     int ambincl = -1; /* include == 1, exclude == 0 */
75    
76 greg 1.13 extern OBJREC Lamb; /* a Lambertian surface */
77    
78 greg 1.1 static RAY thisray; /* for our convenience */
79    
80 greg 2.5 static int oputo(), oputd(), oputv(), oputl(), oputL(),
81 greg 2.9 oputp(), oputn(), oputN(), oputs(), oputw(), oputm();
82 greg 1.1
83 greg 2.13 static int ourtrace(), tabin();
84 greg 2.14 static int (*ray_out[16])(), (*every_out[16])();
85     static int castonly = 0;
86 greg 1.1
87 greg 1.10 static int puta(), putf(), putd();
88 greg 1.1
89     static int (*putreal)();
90    
91    
92     quit(code) /* quit program */
93     int code;
94     {
95 greg 2.11 #ifndef NIX
96     headclean(); /* delete header file */
97     pfclean(); /* clean up persist files */
98     #endif
99 greg 1.1 exit(code);
100     }
101    
102    
103 greg 2.6 char *
104     formstr(f) /* return format identifier */
105     int f;
106     {
107     switch (f) {
108     case 'a': return("ascii");
109     case 'f': return("float");
110     case 'd': return("double");
111     case 'c': return(COLRFMT);
112     }
113     return("unknown");
114     }
115    
116    
117 greg 1.1 rtrace(fname) /* trace rays from file */
118     char *fname;
119     {
120     long vcount = hresolu>1 ? hresolu*vresolu : vresolu;
121     long nextflush = hresolu;
122     FILE *fp;
123 greg 1.2 FVECT orig, direc;
124 greg 1.1 /* set up input */
125     if (fname == NULL)
126     fp = stdin;
127     else if ((fp = fopen(fname, "r")) == NULL) {
128     sprintf(errmsg, "cannot open input file \"%s\"", fname);
129     error(SYSTEM, errmsg);
130     }
131 greg 2.8 #ifdef MSDOS
132     if (inform != 'a')
133     setmode(fileno(fp), O_BINARY);
134     #endif
135 greg 1.1 /* set up output */
136 greg 2.14 if (imm_irrad)
137     outvals = "v";
138     else
139     setoutput(outvals);
140 greg 1.1 switch (outform) {
141     case 'a': putreal = puta; break;
142     case 'f': putreal = putf; break;
143     case 'd': putreal = putd; break;
144 greg 2.6 case 'c':
145     if (strcmp(outvals, "v"))
146     error(USER, "color format with value output only");
147     break;
148     default:
149     error(CONSISTENCY, "botched output format");
150 greg 1.1 }
151 greg 2.12 if (hresolu > 0) {
152     if (vresolu > 0)
153     fprtresolu(hresolu, vresolu, stdout);
154     fflush(stdout);
155     }
156 greg 1.1 /* process file */
157     while (getvec(orig, inform, fp) == 0 &&
158     getvec(direc, inform, fp) == 0) {
159    
160 greg 1.7 if (normalize(direc) == 0.0) { /* zero ==> flush */
161     fflush(stdout);
162     continue;
163     }
164 greg 1.14 samplendx++;
165 greg 1.1 /* compute and print */
166 greg 1.13 if (imm_irrad)
167 greg 1.1 irrad(orig, direc);
168     else
169 greg 1.13 traceray(orig, direc);
170 greg 1.7 /* flush if time */
171 greg 1.1 if (--nextflush == 0) {
172     fflush(stdout);
173     nextflush = hresolu;
174     }
175     if (ferror(stdout))
176     error(SYSTEM, "write error");
177     if (--vcount == 0) /* check for end */
178     break;
179     }
180 greg 2.12 fflush(stdout);
181 greg 1.1 if (vcount > 0)
182     error(USER, "read error");
183 greg 2.12 if (fname != NULL)
184     fclose(fp);
185 greg 1.1 }
186    
187    
188     setoutput(vs) /* set up output tables */
189     register char *vs;
190     {
191 greg 2.13 extern int (*trace)();
192 greg 1.1 register int (**table)() = ray_out;
193    
194 greg 1.11 castonly = 1;
195 greg 1.1 while (*vs)
196     switch (*vs++) {
197     case 't': /* trace */
198     *table = NULL;
199     table = every_out;
200     trace = ourtrace;
201 greg 1.11 castonly = 0;
202 greg 1.1 break;
203     case 'o': /* origin */
204     *table++ = oputo;
205     break;
206     case 'd': /* direction */
207     *table++ = oputd;
208     break;
209     case 'v': /* value */
210     *table++ = oputv;
211 greg 1.11 castonly = 0;
212 greg 1.1 break;
213 greg 2.5 case 'l': /* effective distance */
214 greg 1.1 *table++ = oputl;
215 greg 1.11 castonly = 0;
216 greg 1.1 break;
217 greg 2.5 case 'L': /* single ray length */
218     *table++ = oputL;
219     break;
220 greg 1.1 case 'p': /* point */
221     *table++ = oputp;
222     break;
223 greg 2.9 case 'n': /* perturbed normal */
224 greg 1.1 *table++ = oputn;
225 greg 2.9 castonly = 0;
226 greg 1.1 break;
227 greg 2.9 case 'N': /* unperturbed normal */
228     *table++ = oputN;
229     break;
230 greg 1.1 case 's': /* surface */
231     *table++ = oputs;
232     break;
233     case 'w': /* weight */
234     *table++ = oputw;
235     break;
236     case 'm': /* modifier */
237     *table++ = oputm;
238     break;
239     }
240     *table = NULL;
241     }
242    
243    
244 greg 1.13 traceray(org, dir) /* compute and print ray value(s) */
245 greg 1.1 FVECT org, dir;
246     {
247     register int (**tp)();
248    
249     VCOPY(thisray.rorg, org);
250     VCOPY(thisray.rdir, dir);
251     rayorigin(&thisray, NULL, PRIMARY, 1.0);
252 greg 1.11 if (castonly)
253     localhit(&thisray, &thescene) || sourcehit(&thisray);
254     else
255     rayvalue(&thisray);
256 greg 1.1
257     if (ray_out[0] == NULL)
258     return;
259     for (tp = ray_out; *tp != NULL; tp++)
260     (**tp)(&thisray);
261     if (outform == 'a')
262     putchar('\n');
263     }
264    
265    
266 greg 1.13 irrad(org, dir) /* compute immediate irradiance value */
267 greg 1.1 FVECT org, dir;
268     {
269     register int i;
270    
271     for (i = 0; i < 3; i++) {
272     thisray.rorg[i] = org[i] + dir[i];
273     thisray.rdir[i] = -dir[i];
274     }
275     rayorigin(&thisray, NULL, PRIMARY, 1.0);
276     /* pretend we hit surface */
277     thisray.rot = 1.0;
278     thisray.rod = 1.0;
279     VCOPY(thisray.ron, dir);
280     for (i = 0; i < 3; i++) /* fudge factor */
281     thisray.rop[i] = org[i] + 1e-4*dir[i];
282     /* compute and print */
283     (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
284     oputv(&thisray);
285     if (outform == 'a')
286     putchar('\n');
287     }
288    
289    
290     getvec(vec, fmt, fp) /* get a vector from fp */
291     register FVECT vec;
292     int fmt;
293     FILE *fp;
294     {
295 greg 1.19 extern char *fgetword();
296 greg 1.1 static float vf[3];
297 greg 2.5 static double vd[3];
298 greg 1.19 char buf[32];
299     register int i;
300 greg 1.1
301     switch (fmt) {
302     case 'a': /* ascii */
303 greg 1.19 for (i = 0; i < 3; i++) {
304     if (fgetword(buf, sizeof(buf), fp) == NULL ||
305     !isflt(buf))
306     return(-1);
307     vec[i] = atof(buf);
308     }
309 greg 1.1 break;
310     case 'f': /* binary float */
311 greg 1.8 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
312 greg 1.1 return(-1);
313     vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
314     break;
315     case 'd': /* binary double */
316 greg 2.5 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
317 greg 1.1 return(-1);
318 greg 2.5 vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2];
319 greg 1.1 break;
320 greg 2.6 default:
321     error(CONSISTENCY, "botched input format");
322 greg 1.1 }
323     return(0);
324     }
325    
326    
327 greg 2.15 tranotify(obj) /* record new modifier */
328     OBJECT obj;
329     {
330     static int hitlimit = 0;
331     register OBJREC *o = objptr(obj);
332     register char **tralp;
333    
334     if (hitlimit || !ismodifier(o->otype))
335     return;
336     for (tralp = tralist; *tralp != NULL; tralp++)
337     if (!strcmp(o->oname, *tralp)) {
338     if (traset[0] >= MAXTSET) {
339     error(WARNING, "too many modifiers in trace list");
340     hitlimit++;
341     return; /* should this be fatal? */
342     }
343     insertelem(traset, obj);
344     return;
345     }
346     }
347    
348    
349 greg 1.1 static
350     ourtrace(r) /* print ray values */
351     RAY *r;
352     {
353     register int (**tp)();
354    
355     if (every_out[0] == NULL)
356 greg 2.15 return;
357     if (traincl == 1 && r->ro == NULL)
358     return;
359     if (traincl != -1 && traincl != inset(traset, r->ro->omod))
360 greg 1.1 return;
361     tabin(r);
362     for (tp = every_out; *tp != NULL; tp++)
363     (**tp)(r);
364     putchar('\n');
365     }
366    
367    
368     static
369     tabin(r) /* tab in appropriate amount */
370     RAY *r;
371     {
372     register RAY *rp;
373    
374     for (rp = r->parent; rp != NULL; rp = rp->parent)
375     putchar('\t');
376     }
377    
378    
379     static
380     oputo(r) /* print origin */
381     register RAY *r;
382     {
383     (*putreal)(r->rorg[0]);
384     (*putreal)(r->rorg[1]);
385     (*putreal)(r->rorg[2]);
386     }
387    
388    
389     static
390     oputd(r) /* print direction */
391     register RAY *r;
392     {
393     (*putreal)(r->rdir[0]);
394     (*putreal)(r->rdir[1]);
395     (*putreal)(r->rdir[2]);
396     }
397    
398    
399     static
400     oputv(r) /* print value */
401     register RAY *r;
402     {
403 greg 2.6 COLR cout;
404    
405     if (outform == 'c') {
406     setcolr(cout, colval(r->rcol,RED),
407     colval(r->rcol,GRN),
408     colval(r->rcol,BLU));
409     fwrite((char *)cout, sizeof(cout), 1, stdout);
410     return;
411     }
412 greg 1.1 (*putreal)(colval(r->rcol,RED));
413     (*putreal)(colval(r->rcol,GRN));
414     (*putreal)(colval(r->rcol,BLU));
415     }
416    
417    
418     static
419 greg 2.5 oputl(r) /* print effective distance */
420 greg 1.1 register RAY *r;
421     {
422 greg 1.9 (*putreal)(r->rt);
423 greg 2.5 }
424    
425    
426     static
427     oputL(r) /* print single ray length */
428     register RAY *r;
429     {
430     (*putreal)(r->rot);
431 greg 1.1 }
432    
433    
434     static
435     oputp(r) /* print point */
436     register RAY *r;
437     {
438     if (r->rot < FHUGE) {
439     (*putreal)(r->rop[0]);
440     (*putreal)(r->rop[1]);
441     (*putreal)(r->rop[2]);
442     } else {
443     (*putreal)(0.0);
444     (*putreal)(0.0);
445     (*putreal)(0.0);
446     }
447     }
448    
449    
450     static
451 greg 2.9 oputN(r) /* print unperturbed normal */
452 greg 1.1 register RAY *r;
453     {
454     if (r->rot < FHUGE) {
455     (*putreal)(r->ron[0]);
456     (*putreal)(r->ron[1]);
457     (*putreal)(r->ron[2]);
458     } else {
459     (*putreal)(0.0);
460     (*putreal)(0.0);
461     (*putreal)(0.0);
462     }
463 greg 2.9 }
464    
465    
466     static
467     oputn(r) /* print perturbed normal */
468     RAY *r;
469     {
470     FVECT pnorm;
471    
472     if (r->rot >= FHUGE) {
473     (*putreal)(0.0);
474     (*putreal)(0.0);
475     (*putreal)(0.0);
476     return;
477     }
478     raynormal(pnorm, r);
479     (*putreal)(pnorm[0]);
480     (*putreal)(pnorm[1]);
481     (*putreal)(pnorm[2]);
482 greg 1.1 }
483    
484    
485     static
486     oputs(r) /* print name */
487     register RAY *r;
488     {
489     if (r->ro != NULL)
490     fputs(r->ro->oname, stdout);
491     else
492     putchar('*');
493     putchar('\t');
494     }
495    
496    
497     static
498     oputw(r) /* print weight */
499     register RAY *r;
500     {
501     (*putreal)(r->rweight);
502     }
503    
504    
505     static
506     oputm(r) /* print modifier */
507     register RAY *r;
508     {
509     if (r->ro != NULL)
510     fputs(objptr(r->ro->omod)->oname, stdout);
511     else
512     putchar('*');
513     putchar('\t');
514     }
515    
516    
517     static
518     puta(v) /* print ascii value */
519     double v;
520     {
521     printf("%e\t", v);
522     }
523    
524    
525     static
526     putd(v) /* print binary double */
527     double v;
528     {
529 greg 1.8 fwrite((char *)&v, sizeof(v), 1, stdout);
530 greg 1.1 }
531    
532    
533     static
534     putf(v) /* print binary float */
535     double v;
536     {
537     float f = v;
538    
539 greg 1.8 fwrite((char *)&f, sizeof(f), 1, stdout);
540 greg 1.1 }