ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.8
Committed: Thu Jun 1 14:14:53 1989 UTC (35 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +5 -11 lines
Log Message:
Replaced signal blocking with two signal instant death

File Contents

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