ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.16
Committed: Wed Jul 14 10:38:49 1993 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +22 -20 lines
Log Message:
consistency improvements related to -I option

File Contents

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