ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.4
Committed: Thu Jan 16 12:05:20 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -1 lines
Log Message:
changed default values and handling of specthresh

File Contents

# Content
1 /* Copyright (c) 1986 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 int dimlist[MAXDIM]; /* sampling dimensions */
34 int ndims = 0; /* number of sampling dimensions */
35 int samplendx = 0; /* index for this sample */
36
37 int imm_irrad = 0; /* compute immediate irradiance? */
38
39 int inform = 'a'; /* input format */
40 int outform = 'a'; /* output format */
41 char *outvals = "v"; /* output specification */
42
43 int hresolu = 0; /* horizontal (scan) size */
44 int vresolu = 0; /* vertical resolution */
45
46 double dstrsrc = 0.0; /* square source distribution */
47 double shadthresh = .05; /* shadow threshold */
48 double shadcert = .5; /* shadow certainty */
49 int directrelay = 1; /* number of source relays */
50 int vspretest = 512; /* virtual source pretest density */
51 int directinvis = 0; /* sources invisible? */
52 double srcsizerat = .25; /* maximum ratio source size/dist. */
53
54 double specthresh = .15; /* specular sampling threshold */
55 double specjitter = 1.; /* specular sampling jitter */
56
57 int maxdepth = 6; /* maximum recursion depth */
58 double minweight = 4e-3; /* minimum ray weight */
59
60 COLOR ambval = BLKCOLOR; /* ambient value */
61 double ambacc = 0.2; /* ambient accuracy */
62 int ambres = 32; /* ambient resolution */
63 int ambdiv = 128; /* ambient divisions */
64 int ambssamp = 0; /* ambient super-samples */
65 int ambounce = 0; /* ambient bounces */
66 char *amblist[128]; /* ambient include/exclude list */
67 int ambincl = -1; /* include == 1, exclude == 0 */
68
69 extern OBJREC Lamb; /* a Lambertian surface */
70
71 static RAY thisray; /* for our convenience */
72
73 static int oputo(), oputd(), oputv(), oputl(),
74 oputp(), oputn(), oputs(), oputw(), oputm();
75
76 static int (*ray_out[10])(), (*every_out[10])();
77 static int castonly;
78
79 static int puta(), putf(), putd();
80
81 static int (*putreal)();
82
83
84 quit(code) /* quit program */
85 int code;
86 {
87 exit(code);
88 }
89
90
91 rtrace(fname) /* trace rays from file */
92 char *fname;
93 {
94 long vcount = hresolu>1 ? hresolu*vresolu : vresolu;
95 long nextflush = hresolu;
96 FILE *fp;
97 FVECT orig, direc;
98 /* set up input */
99 if (fname == NULL)
100 fp = stdin;
101 else if ((fp = fopen(fname, "r")) == NULL) {
102 sprintf(errmsg, "cannot open input file \"%s\"", fname);
103 error(SYSTEM, errmsg);
104 }
105 /* set up output */
106 setoutput(outvals);
107 switch (outform) {
108 case 'a': putreal = puta; break;
109 case 'f': putreal = putf; break;
110 case 'd': putreal = putd; break;
111 }
112 /* process file */
113 while (getvec(orig, inform, fp) == 0 &&
114 getvec(direc, inform, fp) == 0) {
115
116 if (normalize(direc) == 0.0) { /* zero ==> flush */
117 fflush(stdout);
118 continue;
119 }
120 samplendx++;
121 /* compute and print */
122 if (imm_irrad)
123 irrad(orig, direc);
124 else
125 traceray(orig, direc);
126 /* flush if time */
127 if (--nextflush == 0) {
128 fflush(stdout);
129 nextflush = hresolu;
130 }
131 if (ferror(stdout))
132 error(SYSTEM, "write error");
133 if (--vcount == 0) /* check for end */
134 break;
135 }
136 if (vcount > 0)
137 error(USER, "read error");
138 fclose(fp);
139 }
140
141
142 setoutput(vs) /* set up output tables */
143 register char *vs;
144 {
145 extern int ourtrace(), (*trace)();
146 register int (**table)() = ray_out;
147
148 castonly = 1;
149 while (*vs)
150 switch (*vs++) {
151 case 't': /* trace */
152 *table = NULL;
153 table = every_out;
154 trace = ourtrace;
155 castonly = 0;
156 break;
157 case 'o': /* origin */
158 *table++ = oputo;
159 break;
160 case 'd': /* direction */
161 *table++ = oputd;
162 break;
163 case 'v': /* value */
164 *table++ = oputv;
165 castonly = 0;
166 break;
167 case 'l': /* length */
168 *table++ = oputl;
169 castonly = 0;
170 break;
171 case 'p': /* point */
172 *table++ = oputp;
173 break;
174 case 'n': /* normal */
175 *table++ = oputn;
176 break;
177 case 's': /* surface */
178 *table++ = oputs;
179 break;
180 case 'w': /* weight */
181 *table++ = oputw;
182 break;
183 case 'm': /* modifier */
184 *table++ = oputm;
185 break;
186 }
187 *table = NULL;
188 }
189
190
191 traceray(org, dir) /* compute and print ray value(s) */
192 FVECT org, dir;
193 {
194 register int (**tp)();
195
196 VCOPY(thisray.rorg, org);
197 VCOPY(thisray.rdir, dir);
198 rayorigin(&thisray, NULL, PRIMARY, 1.0);
199 if (castonly)
200 localhit(&thisray, &thescene) || sourcehit(&thisray);
201 else
202 rayvalue(&thisray);
203
204 if (ray_out[0] == NULL)
205 return;
206 for (tp = ray_out; *tp != NULL; tp++)
207 (**tp)(&thisray);
208 if (outform == 'a')
209 putchar('\n');
210 }
211
212
213 irrad(org, dir) /* compute immediate irradiance value */
214 FVECT org, dir;
215 {
216 register int i;
217
218 for (i = 0; i < 3; i++) {
219 thisray.rorg[i] = org[i] + dir[i];
220 thisray.rdir[i] = -dir[i];
221 }
222 rayorigin(&thisray, NULL, PRIMARY, 1.0);
223 /* pretend we hit surface */
224 thisray.rot = 1.0;
225 thisray.rod = 1.0;
226 VCOPY(thisray.ron, dir);
227 for (i = 0; i < 3; i++) /* fudge factor */
228 thisray.rop[i] = org[i] + 1e-4*dir[i];
229 /* compute and print */
230 (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
231 oputv(&thisray);
232 if (outform == 'a')
233 putchar('\n');
234 }
235
236
237 getvec(vec, fmt, fp) /* get a vector from fp */
238 register FVECT vec;
239 int fmt;
240 FILE *fp;
241 {
242 extern char *fgetword();
243 static float vf[3];
244 char buf[32];
245 register int i;
246
247 switch (fmt) {
248 case 'a': /* ascii */
249 for (i = 0; i < 3; i++) {
250 if (fgetword(buf, sizeof(buf), fp) == NULL ||
251 !isflt(buf))
252 return(-1);
253 vec[i] = atof(buf);
254 }
255 break;
256 case 'f': /* binary float */
257 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
258 return(-1);
259 vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
260 break;
261 case 'd': /* binary double */
262 if (fread((char *)vec, sizeof(double), 3, fp) != 3)
263 return(-1);
264 break;
265 }
266 return(0);
267 }
268
269
270 static
271 ourtrace(r) /* print ray values */
272 RAY *r;
273 {
274 register int (**tp)();
275
276 if (every_out[0] == NULL)
277 return;
278 tabin(r);
279 for (tp = every_out; *tp != NULL; tp++)
280 (**tp)(r);
281 putchar('\n');
282 }
283
284
285 static
286 tabin(r) /* tab in appropriate amount */
287 RAY *r;
288 {
289 register RAY *rp;
290
291 for (rp = r->parent; rp != NULL; rp = rp->parent)
292 putchar('\t');
293 }
294
295
296 static
297 oputo(r) /* print origin */
298 register RAY *r;
299 {
300 (*putreal)(r->rorg[0]);
301 (*putreal)(r->rorg[1]);
302 (*putreal)(r->rorg[2]);
303 }
304
305
306 static
307 oputd(r) /* print direction */
308 register RAY *r;
309 {
310 (*putreal)(r->rdir[0]);
311 (*putreal)(r->rdir[1]);
312 (*putreal)(r->rdir[2]);
313 }
314
315
316 static
317 oputv(r) /* print value */
318 register RAY *r;
319 {
320 (*putreal)(colval(r->rcol,RED));
321 (*putreal)(colval(r->rcol,GRN));
322 (*putreal)(colval(r->rcol,BLU));
323 }
324
325
326 static
327 oputl(r) /* print length */
328 register RAY *r;
329 {
330 (*putreal)(r->rt);
331 }
332
333
334 static
335 oputp(r) /* print point */
336 register RAY *r;
337 {
338 if (r->rot < FHUGE) {
339 (*putreal)(r->rop[0]);
340 (*putreal)(r->rop[1]);
341 (*putreal)(r->rop[2]);
342 } else {
343 (*putreal)(0.0);
344 (*putreal)(0.0);
345 (*putreal)(0.0);
346 }
347 }
348
349
350 static
351 oputn(r) /* print normal */
352 register RAY *r;
353 {
354 if (r->rot < FHUGE) {
355 (*putreal)(r->ron[0]);
356 (*putreal)(r->ron[1]);
357 (*putreal)(r->ron[2]);
358 } else {
359 (*putreal)(0.0);
360 (*putreal)(0.0);
361 (*putreal)(0.0);
362 }
363 }
364
365
366 static
367 oputs(r) /* print name */
368 register RAY *r;
369 {
370 if (r->ro != NULL)
371 fputs(r->ro->oname, stdout);
372 else
373 putchar('*');
374 putchar('\t');
375 }
376
377
378 static
379 oputw(r) /* print weight */
380 register RAY *r;
381 {
382 (*putreal)(r->rweight);
383 }
384
385
386 static
387 oputm(r) /* print modifier */
388 register RAY *r;
389 {
390 if (r->ro != NULL)
391 fputs(objptr(r->ro->omod)->oname, stdout);
392 else
393 putchar('*');
394 putchar('\t');
395 }
396
397
398 static
399 puta(v) /* print ascii value */
400 double v;
401 {
402 printf("%e\t", v);
403 }
404
405
406 static
407 putd(v) /* print binary double */
408 double v;
409 {
410 fwrite((char *)&v, sizeof(v), 1, stdout);
411 }
412
413
414 static
415 putf(v) /* print binary float */
416 double v;
417 {
418 float f = v;
419
420 fwrite((char *)&f, sizeof(f), 1, stdout);
421 }