ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.45
Committed: Thu Sep 19 13:55:49 1991 UTC (33 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.44: +3 -8 lines
Log Message:
improved ALRM signal handling for SYSV

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