ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rmain.c
Revision: 1.10
Committed: Sat Jun 3 10:37:55 1989 UTC (36 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +44 -10 lines
Log Message:
Finally added better option error checking

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