ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.34
Committed: Tue Apr 9 09:31:04 1991 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.33: +6 -3 lines
Log Message:
changed reporting so that alarm() is not used.

File Contents

# User Rev Content
1 greg 1.34 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rmain.c - main for ray tracing programs
9     *
10     * 3/24/87
11     */
12    
13     /* for flaky pre-processors */
14     #ifndef RPICT
15     #define RPICT 0
16     #endif
17     #ifndef RTRACE
18     #define RTRACE 0
19     #endif
20     #ifndef RVIEW
21     #define RVIEW 0
22     #endif
23    
24     #include "ray.h"
25    
26     #include "octree.h"
27    
28     #include <signal.h>
29    
30     #include "view.h"
31    
32     #ifndef DEFPATH
33     #define DEFPATH ":/usr/local/lib/ray"
34     #endif
35 greg 1.17 #ifndef ULIBVAR
36 greg 1.1 #define ULIBVAR "RAYPATH"
37 greg 1.17 #endif
38 greg 1.1
39     char *progname; /* argv[0] */
40    
41 greg 1.19 char *octname; /* octree name */
42    
43 greg 1.1 char *libpath; /* library directory list */
44    
45     char *sigerr[NSIG]; /* signal error messages */
46    
47     extern int stderr_v(); /* standard error output */
48     int (*errvec)() = stderr_v; /* error output vector */
49     int (*wrnvec)() = stderr_v; /* warning output vector */
50     int (*cmdvec)() = NULL; /* command error vector */
51    
52     int (*trace)() = NULL; /* trace call */
53    
54 greg 1.34 extern long time();
55     long tstart; /* start time */
56    
57 greg 1.24 extern int ambnotify(); /* new object notify functions */
58     int (*addobjnotify[])() = {ambnotify, NULL};
59    
60 greg 1.1 CUBE thescene; /* our scene */
61    
62     extern int ralrm; /* seconds between reports */
63     extern float pctdone; /* percentage done */
64    
65     extern int greyscale; /* map colors to brightness? */
66     extern char *devname; /* output device name */
67    
68     extern int inform; /* input format */
69     extern int outform; /* output format */
70     extern char *outvals; /* output values */
71    
72     extern VIEW ourview; /* viewing parameters */
73    
74     extern int hresolu; /* horizontal resolution */
75     extern int vresolu; /* vertical resolution */
76 greg 1.22 extern double pixaspect; /* pixel aspect ratio */
77 greg 1.1
78     extern int psample; /* pixel sample size */
79     extern double maxdiff; /* max. sample difference */
80 greg 1.11 extern double dstrpix; /* square pixel distribution */
81 greg 1.1
82     extern double dstrsrc; /* square source distribution */
83 greg 1.11 extern double shadthresh; /* shadow threshold */
84 greg 1.15 extern double shadcert; /* shadow testing certainty */
85 greg 1.1
86     extern int maxdepth; /* maximum recursion depth */
87     extern double minweight; /* minimum ray weight */
88     extern long nrays; /* number of rays traced */
89    
90     extern COLOR ambval; /* ambient value */
91     extern double ambacc; /* ambient accuracy */
92     extern int ambres; /* ambient resolution */
93     extern int ambdiv; /* ambient divisions */
94     extern int ambssamp; /* ambient super-samples */
95     extern int ambounce; /* ambient bounces */
96     extern char *amblist[]; /* ambient include/exclude list */
97     extern int ambincl; /* include == 1, exclude == 0 */
98    
99    
100     main(argc, argv)
101     int argc;
102     char *argv[];
103     {
104 greg 1.16 #define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt
105 greg 1.1 double atof();
106     char *getenv();
107     int report();
108     char *err;
109     char *recover = NULL;
110 greg 1.20 char *zfile = NULL;
111 greg 1.1 char *errfile = NULL;
112     char *ambfile = NULL;
113     char **amblp = amblist;
114     int loadflags = ~IO_FILES;
115 greg 1.4 int rval, gotvfile = 0;
116 greg 1.1 int i;
117 greg 1.34 /* record start time */
118     tstart = time((long *)0);
119 greg 1.1 /* global program name */
120     progname = argv[0];
121     /* get library path */
122     if ((libpath = getenv(ULIBVAR)) == NULL)
123     libpath = DEFPATH;
124 greg 1.31 /* initialize object types */
125     initotypes();
126 greg 1.10 /* option city */
127 greg 1.1 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
128 greg 1.18 if (!strcmp(argv[i], "-defaults") ||
129     !strcmp(argv[i], "-help")) {
130 greg 1.1 printdefaults();
131     quit(0);
132     }
133     #if RVIEW
134     if (!strcmp(argv[i], "-devices")) {
135     printdevices();
136     quit(0);
137     }
138     #endif
139 greg 1.21 #if RPICT|RVIEW
140     rval = getviewopt(&ourview, argc-i, argv+i);
141     if (rval >= 0) {
142     i += rval;
143     continue;
144     }
145     #endif
146 greg 1.1 switch (argv[i][1]) {
147     #if RPICT|RVIEW
148 greg 1.21 case 'v': /* view file */
149     if (argv[i][2] != 'f')
150 greg 1.10 goto badopt;
151 greg 1.21 check(3,1);
152 greg 1.25 rval = viewfile(argv[++i], &ourview, 0, 0);
153 greg 1.21 if (rval < 0) {
154     sprintf(errmsg,
155     "cannot open view file \"%s\"",
156     argv[i]);
157     error(SYSTEM, errmsg);
158     } else if (rval == 0) {
159     sprintf(errmsg,
160     "bad view file \"%s\"",
161     argv[i]);
162     error(USER, errmsg);
163     } else
164     gotvfile += rval;
165 greg 1.1 break;
166     #endif
167 greg 1.11 case 'd': /* direct */
168 greg 1.1 switch (argv[i][2]) {
169 greg 1.13 case 't': /* threshold */
170 greg 1.10 check(3,1);
171 greg 1.11 shadthresh = atof(argv[++i]);
172 greg 1.1 break;
173 greg 1.15 case 'c': /* certainty */
174     check(3,1);
175     shadcert = atof(argv[++i]);
176     break;
177 greg 1.11 case 'j': /* jitter */
178 greg 1.10 check(3,1);
179 greg 1.1 dstrsrc = atof(argv[++i]);
180     break;
181     default:
182 greg 1.10 goto badopt;
183 greg 1.1 }
184     break;
185     #if RPICT|RVIEW
186     case 's': /* sample */
187     switch (argv[i][2]) {
188     case 'p': /* pixel */
189 greg 1.10 check(3,1);
190 greg 1.1 psample = atoi(argv[++i]);
191     break;
192 greg 1.13 case 't': /* threshold */
193 greg 1.10 check(3,1);
194 greg 1.1 maxdiff = atof(argv[++i]);
195     break;
196 greg 1.11 #if RPICT
197     case 'j': /* jitter */
198     check(3,1);
199     dstrpix = atof(argv[++i]);
200     break;
201     #endif
202 greg 1.1 default:
203 greg 1.10 goto badopt;
204 greg 1.1 }
205     break;
206     #endif
207 greg 1.22 #if RPICT|RTRACE
208 greg 1.1 case 'x': /* x resolution */
209 greg 1.10 check(2,1);
210 greg 1.1 hresolu = atoi(argv[++i]);
211     break;
212     case 'y': /* y resolution */
213 greg 1.10 check(2,1);
214 greg 1.1 vresolu = atoi(argv[++i]);
215     break;
216 greg 1.22 #endif
217     #if RPICT
218     case 'p': /* pixel aspect */
219 greg 1.23 check(2,1);
220 greg 1.22 pixaspect = atof(argv[++i]);
221     break;
222     #endif
223 greg 1.1 case 'w': /* warnings */
224 greg 1.10 check(2,0);
225 greg 1.32 if (wrnvec == NULL)
226     wrnvec = stderr_v;
227     else
228     wrnvec = NULL;
229 greg 1.1 break;
230     case 'e': /* error file */
231 greg 1.10 check(2,1);
232 greg 1.1 errfile = argv[++i];
233     break;
234     case 'l': /* limit */
235     switch (argv[i][2]) {
236     case 'r': /* recursion */
237 greg 1.10 check(3,1);
238 greg 1.1 maxdepth = atoi(argv[++i]);
239     break;
240     case 'w': /* weight */
241 greg 1.10 check(3,1);
242 greg 1.1 minweight = atof(argv[++i]);
243     break;
244     default:
245 greg 1.10 goto badopt;
246 greg 1.1 }
247     break;
248     #if RPICT
249 greg 1.20 case 'z': /* z file */
250     check(2,1);
251     zfile = argv[++i];
252     break;
253 greg 1.1 case 'r': /* recover file */
254 greg 1.10 check(2,1);
255 greg 1.1 recover = argv[++i];
256 greg 1.25 rval = viewfile(recover, &ourview, &hresolu, &vresolu);
257 greg 1.5 if (rval <= 0) {
258     sprintf(errmsg,
259     "cannot recover view parameters from \"%s\"", recover);
260     error(WARNING, errmsg);
261 greg 1.25 } else {
262 greg 1.4 gotvfile += rval;
263 greg 1.25 pixaspect = 0.0;
264     }
265 greg 1.1 break;
266     case 't': /* timer */
267 greg 1.10 check(2,1);
268 greg 1.1 ralrm = atoi(argv[++i]);
269     break;
270     #endif
271     case 'a': /* ambient */
272     switch (argv[i][2]) {
273     case 'v': /* value */
274 greg 1.10 check(3,3);
275 greg 1.1 setcolor(ambval, atof(argv[i+1]),
276     atof(argv[i+2]),
277     atof(argv[i+3]));
278     i += 3;
279     break;
280     case 'a': /* accuracy */
281 greg 1.10 check(3,1);
282 greg 1.1 ambacc = atof(argv[++i]);
283     break;
284     case 'r': /* resolution */
285 greg 1.10 check(3,1);
286 greg 1.1 ambres = atoi(argv[++i]);
287     break;
288     case 'd': /* divisions */
289 greg 1.10 check(3,1);
290 greg 1.1 ambdiv = atoi(argv[++i]);
291     break;
292     case 's': /* super-samp */
293 greg 1.10 check(3,1);
294 greg 1.1 ambssamp = atoi(argv[++i]);
295     break;
296     case 'b': /* bounces */
297 greg 1.10 check(3,1);
298 greg 1.1 ambounce = atoi(argv[++i]);
299     break;
300     case 'i': /* include */
301 greg 1.10 check(3,1);
302 greg 1.1 if (ambincl != 1) {
303     ambincl = 1;
304     amblp = amblist;
305     }
306     *amblp++ = argv[++i];
307     break;
308     case 'e': /* exclude */
309 greg 1.10 check(3,1);
310 greg 1.1 if (ambincl != 0) {
311     ambincl = 0;
312     amblp = amblist;
313     }
314     *amblp++ = argv[++i];
315     break;
316     case 'f': /* file */
317 greg 1.10 check(3,1);
318 greg 1.1 ambfile= argv[++i];
319     break;
320     default:
321 greg 1.10 goto badopt;
322 greg 1.1 }
323     break;
324     #if RTRACE
325     case 'f': /* format i/o */
326     switch (argv[i][2]) {
327     case 'a': /* ascii */
328     case 'f': /* float */
329     case 'd': /* double */
330     inform = argv[i][2];
331     break;
332     default:
333 greg 1.10 goto badopt;
334 greg 1.1 }
335     switch (argv[i][3]) {
336     case '\0':
337     outform = inform;
338     break;
339     case 'a': /* ascii */
340     case 'f': /* float */
341     case 'd': /* double */
342 greg 1.12 check(4,0);
343 greg 1.1 outform = argv[i][3];
344     break;
345     default:
346 greg 1.10 goto badopt;
347 greg 1.1 }
348     break;
349     case 'o': /* output */
350     outvals = argv[i]+2;
351     break;
352 greg 1.6 case 'h': /* toggle header */
353 greg 1.10 check(2,0);
354 greg 1.6 loadflags ^= IO_INFO;
355 greg 1.1 break;
356     #endif
357     #if RVIEW
358     case 'b': /* black and white */
359 greg 1.10 check(2,0);
360 greg 1.1 greyscale = !greyscale;
361     break;
362     case 'o': /* output device */
363 greg 1.10 check(2,1);
364 greg 1.1 devname = argv[++i];
365     break;
366     #endif
367     default:
368 greg 1.19 goto badopt;
369 greg 1.1 }
370     }
371 greg 1.24 *amblp = NULL;
372 greg 1.1 #if RPICT|RVIEW
373     err = setview(&ourview); /* set viewing parameters */
374     if (err != NULL)
375     error(USER, err);
376     #endif
377 greg 1.22 #if RPICT
378 greg 1.23 normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
379 greg 1.22 #endif
380 greg 1.1 /* set up signal handling */
381     sigdie(SIGINT, "Interrupt");
382     sigdie(SIGHUP, "Hangup");
383     sigdie(SIGTERM, "Terminate");
384     sigdie(SIGPIPE, "Broken pipe");
385     #ifdef SIGXCPU
386     sigdie(SIGXCPU, "CPU limit exceeded");
387     sigdie(SIGXFSZ, "File size exceeded");
388     #endif
389     #if RPICT
390     signal(SIGALRM, report);
391     #else
392     sigdie(SIGALRM, "Alarm clock");
393     #endif
394     /* open error file */
395     if (errfile != NULL) {
396     if (freopen(errfile, "a", stderr) == NULL)
397 greg 1.14 quit(2);
398 greg 1.1 fprintf(stderr, "**************\n*** PID %5d: ",
399     getpid());
400     printargs(argc, argv, stderr);
401     fputs("\n", stderr);
402     fflush(stderr);
403     }
404     #ifdef NICE
405     nice(NICE); /* lower priority */
406     #endif
407 greg 1.19 /* get octree */
408 greg 1.1 #if RVIEW
409     loadflags &= ~IO_INFO;
410     #endif
411     if (i == argc)
412 greg 1.19 octname = NULL;
413     else if (i == argc-1)
414     octname = argv[i];
415 greg 1.1 else
416 greg 1.19 goto badopt;
417     #if RVIEW|RTRACE
418     if (octname == NULL)
419     error(USER, "missing octree argument");
420 greg 1.1 #endif
421 greg 1.19 readoct(octname, loadflags, &thescene, NULL);
422 greg 1.1
423     if (loadflags & IO_INFO) { /* print header */
424     printargs(i, argv, stdout);
425     #if RPICT
426     if (gotvfile) {
427     printf(VIEWSTR);
428     fprintview(&ourview, stdout);
429     printf("\n");
430     }
431 greg 1.23 if (pixaspect < .99 || pixaspect > 1.01)
432 greg 1.22 fputaspect(pixaspect, stdout);
433 greg 1.1 #endif
434     printf("\n");
435     }
436    
437     marksources(); /* find and mark sources */
438    
439 greg 1.24 setambient(ambfile); /* initialize ambient calculation */
440 greg 1.1
441     #if RPICT
442 greg 1.20 render(zfile, recover); /* render the scene */
443 greg 1.1 #endif
444     #if RTRACE
445     rtrace(NULL); /* trace rays from stdin */
446     #endif
447     #if RVIEW
448     rview(); /* go */
449     #endif
450     quit(0);
451 greg 1.19
452     badopt:
453     sprintf(errmsg, "command line error at '%s'", argv[i]);
454     error(USER, errmsg);
455    
456 greg 1.10 #undef check
457 greg 1.1 }
458    
459    
460     eputs(s) /* error output */
461     char *s;
462     {
463     if (errvec != NULL)
464     (*errvec)(s);
465     }
466    
467    
468     wputs(s) /* warning output */
469     char *s;
470     {
471     if (wrnvec != NULL)
472     (*wrnvec)(s);
473     }
474    
475    
476     cputs(s) /* command error output */
477     char *s;
478     {
479     if (cmdvec != NULL)
480     (*cmdvec)(s);
481     }
482    
483    
484     stderr_v(s) /* put string to stderr */
485     register char *s;
486     {
487 greg 1.26 static int midline = 0;
488 greg 1.1
489 greg 1.27 if (!*s)
490     return;
491 greg 1.26 if (!midline++) {
492 greg 1.1 fputs(progname, stderr);
493     fputs(": ", stderr);
494     }
495     fputs(s, stderr);
496 greg 1.27 if (s[strlen(s)-1] == '\n') {
497 greg 1.1 fflush(stderr);
498 greg 1.26 midline = 0;
499 greg 1.1 }
500     }
501    
502    
503     onsig(signo) /* fatal signal */
504     int signo;
505     {
506 greg 1.8 static int gotsig = 0;
507    
508     if (gotsig++) /* two signals and we're gone! */
509 greg 1.9 _exit(signo);
510 greg 1.8
511 greg 1.1 eputs("signal - ");
512     eputs(sigerr[signo]);
513     eputs("\n");
514 greg 1.14 quit(3);
515 greg 1.1 }
516    
517    
518     sigdie(signo, msg) /* set fatal signal */
519     int signo;
520     char *msg;
521     {
522     if (signal(signo, onsig) == SIG_IGN)
523     signal(signo, SIG_IGN);
524     sigerr[signo] = msg;
525     }
526    
527    
528     printdefaults() /* print default values to stdout */
529     {
530     register char *cp;
531    
532     #if RPICT|RVIEW
533 greg 1.7 printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
534 greg 1.1 ourview.type==VT_PER ? "perspective" :
535     ourview.type==VT_PAR ? "parallel" :
536 greg 1.28 ourview.type==VT_HEM ? "hemispherical" :
537     ourview.type==VT_ANG ? "angular" :
538 greg 1.1 "unknown");
539     printf("-vp %f %f %f\t# view point\n",
540     ourview.vp[0], ourview.vp[1], ourview.vp[2]);
541     printf("-vd %f %f %f\t# view direction\n",
542     ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
543     printf("-vu %f %f %f\t# view up\n",
544     ourview.vup[0], ourview.vup[1], ourview.vup[2]);
545     printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
546     printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
547 greg 1.22 printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
548     printf("-vl %f\t\t\t# view lift\n", ourview.voff);
549 greg 1.1 #endif
550 greg 1.22 #if RPICT|RTRACE
551 greg 1.1 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
552     printf("-y %-9d\t\t\t# y resolution\n", vresolu);
553 greg 1.22 #endif
554     #if RPICT
555 greg 1.23 printf("-p %f\t\t\t# pixel aspect ratio\n", pixaspect);
556 greg 1.22 #endif
557 greg 1.1 #if RPICT|RVIEW
558     printf("-sp %-9d\t\t\t# sample pixel\n", psample);
559 greg 1.13 printf("-st %f\t\t\t# sample threshold\n", maxdiff);
560 greg 1.22 #endif
561 greg 1.11 #if RPICT
562     printf("-sj %f\t\t\t# sample jitter\n", dstrpix);
563 greg 1.1 #endif
564 greg 1.29 printf("-dt %f\t\t\t# direct threshold\n", shadthresh);
565     printf("-dc %f\t\t\t# direct certainty\n", shadcert);
566     printf("-dj %f\t\t\t# direct jitter\n", dstrsrc);
567 greg 1.1 printf("-av %f %f %f\t# ambient value\n", colval(ambval,RED),
568     colval(ambval,GRN), colval(ambval, BLU));
569     printf("-ab %-9d\t\t\t# ambient bounces\n", ambounce);
570     printf("-aa %f\t\t\t# ambient accuracy\n", ambacc);
571     printf("-ar %-9d\t\t\t# ambient resolution\n", ambres);
572     printf("-ad %-9d\t\t\t# ambient divisions\n", ambdiv);
573     printf("-as %-9d\t\t\t# ambient super-samples\n", ambssamp);
574     printf("-lr %-9d\t\t\t# limit reflection\n", maxdepth);
575     printf("-lw %f\t\t\t# limit weight\n", minweight);
576     #if RPICT
577     printf("-t %-9d\t\t\t# time between reports\n", ralrm);
578     #endif
579     #if RVIEW
580     printf("-o %s\t\t\t\t# output device\n", devname);
581     #endif
582     #if RTRACE
583     printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
584     inform, outform,
585     inform=='a' ? "ascii" :
586     inform=='f' ? "float" : "double",
587     outform=='a' ? "ascii" :
588     outform=='f' ? "float" : "double");
589     printf("-o%s\t\t\t\t# output", outvals);
590     for (cp = outvals; *cp; cp++)
591     switch (*cp) {
592     case 'i': printf(" irradiance"); break;
593     case 't': printf(" trace"); break;
594     case 'o': printf(" origin"); break;
595     case 'd': printf(" direction"); break;
596     case 'v': printf(" value"); break;
597     case 'l': printf(" length"); break;
598     case 'p': printf(" point"); break;
599     case 'n': printf(" normal"); break;
600     case 's': printf(" surface"); break;
601     case 'w': printf(" weight"); break;
602     case 'm': printf(" modifier"); break;
603     }
604     printf("\n");
605     #endif
606     }