ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rvmain.c
Revision: 2.25
Committed: Sat Jun 7 05:09:46 2025 UTC (16 hours, 31 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.24: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.25 static const char RCSid[] = "$Id: rvmain.c,v 2.24 2025/04/23 02:35:26 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * rvmain.c - main for rview interactive viewer
6     */
7    
8 greg 2.2 #include "copyright.h"
9 greg 2.1
10 schorsch 2.3 #include <signal.h>
11 greg 2.5 #include <time.h>
12 schorsch 2.3
13     #include "platform.h"
14 greg 2.1 #include "ray.h"
15     #include "source.h"
16     #include "ambient.h"
17 schorsch 2.4 #include "rpaint.h"
18 greg 2.1 #include "random.h"
19     #include "view.h"
20 greg 2.12 #include "pmapray.h"
21 greg 2.1
22 greg 2.9 extern char *progname; /* global argv[0] */
23 greg 2.1
24 greg 2.9 VIEW ourview = STDVIEW; /* viewing parameters */
25     int hresolu, vresolu; /* image resolution */
26 greg 2.1
27 greg 2.9 int psample = 8; /* pixel sample size */
28     double maxdiff = .15; /* max. sample difference */
29    
30     int greyscale = 0; /* map colors to brightness? */
31     char *dvcname = dev_default; /* output device name */
32    
33     double exposure = 1.0; /* exposure for scene */
34 greg 2.1
35 greg 2.9 int newparam = 1; /* parameter setting changed */
36    
37     struct driver *dev = NULL; /* driver functions */
38 greg 2.1
39 greg 2.9 char rifname[128]; /* rad input file name */
40 greg 2.1
41 greg 2.9 VIEW oldview; /* previous view parameters */
42 greg 2.1
43 greg 2.9 PNODE ptrunk; /* the base of our image */
44     RECT pframe; /* current frame boundaries */
45     int pdepth; /* image depth in current frame */
46 greg 2.1
47 greg 2.9 char *errfile = NULL; /* error output file */
48 greg 2.1
49 greg 2.9 int nproc = 1; /* number of processes */
50 greg 2.1
51 greg 2.9 char *sigerr[NSIG]; /* signal error messages */
52 greg 2.1
53 schorsch 2.4 static void onsig(int signo);
54     static void sigdie(int signo, char *msg);
55     static void printdefaults(void);
56 greg 2.1
57 greg 2.23 static void
58     set_defaults(void)
59     {
60     shadthresh = .1;
61     shadcert = .25;
62     directrelay = 0;
63     vspretest = 128;
64     srcsizerat = 0.;
65     specthresh = .3;
66     specjitter = 1.;
67     maxdepth = 6;
68     minweight = 1e-3;
69     ambacc = 0.3;
70     ambres = 32;
71     ambdiv = 256;
72     ambssamp = 64;
73     }
74    
75 greg 2.17 int
76 greg 2.9 main(int argc, char *argv[])
77 greg 2.1 {
78     #define check(ol,al) if (argv[i][ol] || \
79     badarg(argc-i-1,argv+i+1,al)) \
80     goto badopt
81 schorsch 2.15 #define check_bool(olen,var) switch (argv[i][olen]) { \
82 greg 2.1 case '\0': var = !var; break; \
83     case 'y': case 'Y': case 't': case 'T': \
84     case '+': case '1': var = 1; break; \
85     case 'n': case 'N': case 'f': case 'F': \
86     case '-': case '0': var = 0; break; \
87     default: goto badopt; }
88 greg 2.9 char *octnm = NULL;
89 greg 2.1 char *err;
90     int rval;
91     int i;
92     /* global program name */
93     progname = argv[0] = fixargv0(argv[0]);
94 greg 2.9 /* set our defaults */
95 greg 2.23 set_defaults();
96 greg 2.1 /* option city */
97     for (i = 1; i < argc; i++) {
98     /* expand arguments */
99     while ((rval = expandarg(&argc, &argv, i)) > 0)
100     ;
101     if (rval < 0) {
102     sprintf(errmsg, "cannot expand '%s'", argv[i]);
103     error(SYSTEM, errmsg);
104     }
105     if (argv[i] == NULL || argv[i][0] != '-')
106     break; /* break from options */
107     if (!strcmp(argv[i], "-version")) {
108     puts(VersionID);
109     quit(0);
110     }
111     if (!strcmp(argv[i], "-defaults") ||
112     !strcmp(argv[i], "-help")) {
113     printdefaults();
114     quit(0);
115     }
116     if (!strcmp(argv[i], "-devices")) {
117     printdevices();
118     quit(0);
119     }
120     rval = getrenderopt(argc-i, argv+i);
121     if (rval >= 0) {
122     i += rval;
123     continue;
124     }
125     rval = getviewopt(&ourview, argc-i, argv+i);
126     if (rval >= 0) {
127     i += rval;
128     continue;
129     }
130     switch (argv[i][1]) {
131 greg 2.9 case 'n': /* # processes */
132     check(2,"i");
133     nproc = atoi(argv[++i]);
134     if (nproc <= 0)
135     error(USER, "bad number of processes");
136     break;
137 greg 2.1 case 'v': /* view file */
138     if (argv[i][2] != 'f')
139     goto badopt;
140     check(3,"s");
141     rval = viewfile(argv[++i], &ourview, NULL);
142     if (rval < 0) {
143     sprintf(errmsg,
144     "cannot open view file \"%s\"",
145     argv[i]);
146     error(SYSTEM, errmsg);
147     } else if (rval == 0) {
148     sprintf(errmsg,
149     "bad view file \"%s\"",
150     argv[i]);
151     error(USER, errmsg);
152     }
153     break;
154     case 'b': /* grayscale */
155 schorsch 2.15 check_bool(2,greyscale);
156 greg 2.1 break;
157     case 'p': /* pixel */
158     switch (argv[i][2]) {
159     case 's': /* sample */
160     check(3,"i");
161     psample = atoi(argv[++i]);
162     break;
163     case 't': /* threshold */
164     check(3,"f");
165     maxdiff = atof(argv[++i]);
166     break;
167     case 'e': /* exposure */
168     check(3,"f");
169     exposure = atof(argv[++i]);
170     if (argv[i][0] == '+' || argv[i][0] == '-')
171     exposure = pow(2.0, exposure);
172     break;
173     default:
174     goto badopt;
175     }
176     break;
177     case 'w': /* warnings */
178     rval = erract[WARNING].pf != NULL;
179 schorsch 2.15 check_bool(2,rval);
180 greg 2.1 if (rval) erract[WARNING].pf = wputs;
181     else erract[WARNING].pf = NULL;
182     break;
183     case 'e': /* error file */
184     check(2,"s");
185     errfile = argv[++i];
186     break;
187     case 'o': /* output device */
188     check(2,"s");
189     dvcname = argv[++i];
190     break;
191     case 'R': /* render input file */
192     check(2,"s");
193     strcpy(rifname, argv[++i]);
194     break;
195     default:
196     goto badopt;
197     }
198     }
199 greg 2.20 /* set/check spectral sampling */
200 greg 2.21 if (setspectrsamp(CNDX, WLPART) <= 0)
201 greg 2.20 error(USER, "unsupported spectral sampling");
202    
203 greg 2.1 err = setview(&ourview); /* set viewing parameters */
204     if (err != NULL)
205     error(USER, err);
206 greg 2.9 /* set up signal handling */
207 greg 2.1 sigdie(SIGINT, "Interrupt");
208 greg 2.11 sigdie(SIGTERM, "Terminate");
209 schorsch 2.14 #if !defined(_WIN32) && !defined(_WIN64)
210 greg 2.1 sigdie(SIGHUP, "Hangup");
211     sigdie(SIGPIPE, "Broken pipe");
212     sigdie(SIGALRM, "Alarm clock");
213 greg 2.11 #endif
214 greg 2.1 /* open error file */
215     if (errfile != NULL) {
216     if (freopen(errfile, "a", stderr) == NULL)
217     quit(2);
218     fprintf(stderr, "**************\n*** PID %5d: ",
219     getpid());
220     printargs(argc, argv, stderr);
221     putc('\n', stderr);
222     fflush(stderr);
223     }
224     #ifdef NICE
225     nice(NICE); /* lower priority */
226     #endif
227     /* get octree */
228     if (i == argc)
229 greg 2.9 octnm = NULL;
230 greg 2.1 else if (i == argc-1)
231 greg 2.9 octnm = argv[i];
232 greg 2.1 else
233     goto badopt;
234 greg 2.9 if (octnm == NULL)
235 greg 2.1 error(USER, "missing octree argument");
236 greg 2.9 /* set up output & start process(es) */
237 schorsch 2.3 SET_FILE_BINARY(stdout);
238 greg 2.9
239 greg 2.13 ray_init(octnm); /* also calls ray_init_pmap() */
240 greg 2.12
241 schorsch 2.16 /* temporary shortcut, until winrview is refactored into a "device" */
242     #ifndef WIN_RVIEW
243 greg 2.1 rview(); /* run interactive viewer */
244    
245 schorsch 2.16
246 greg 2.9 devclose(); /* close output device */
247 schorsch 2.16 #endif
248 greg 2.1
249 greg 2.12 /* PMAP: free photon maps */
250     ray_done_pmap();
251    
252 schorsch 2.16 #ifdef WIN_RVIEW
253     return 1;
254     #endif
255 greg 2.1 quit(0);
256    
257     badopt:
258     sprintf(errmsg, "command line error at '%s'", argv[i]);
259     error(USER, errmsg);
260 schorsch 2.4 return 1; /* pro forma return */
261 greg 2.1
262     #undef check
263 schorsch 2.15 #undef check_bool
264 greg 2.1 }
265    
266    
267     void
268 schorsch 2.4 wputs( /* warning output function */
269 greg 2.19 const char *s
270 schorsch 2.4 )
271 greg 2.1 {
272     int lasterrno = errno;
273 greg 2.22 if (erract[WARNING].pf == NULL)
274     return; /* called by calcomp or someone */
275 greg 2.1 eputs(s);
276     errno = lasterrno;
277     }
278    
279    
280     void
281 schorsch 2.4 eputs( /* put string to stderr */
282 greg 2.19 const char *s
283 schorsch 2.4 )
284 greg 2.1 {
285     static int midline = 0;
286    
287     if (!*s)
288     return;
289     if (!midline++) {
290     fputs(progname, stderr);
291     fputs(": ", stderr);
292     }
293     fputs(s, stderr);
294     if (s[strlen(s)-1] == '\n') {
295     fflush(stderr);
296     midline = 0;
297     }
298     }
299    
300    
301 schorsch 2.4 static void
302     onsig( /* fatal signal */
303     int signo
304     )
305 greg 2.1 {
306     static int gotsig = 0;
307    
308     if (gotsig++) /* two signals and we're gone! */
309     _exit(signo);
310    
311 schorsch 2.14 #if !defined(_WIN32) && !defined(_WIN64)
312 greg 2.1 alarm(15); /* allow 15 seconds to clean up */
313     signal(SIGALRM, SIG_DFL); /* make certain we do die */
314 greg 2.11 #endif
315 greg 2.1 eputs("signal - ");
316     eputs(sigerr[signo]);
317     eputs("\n");
318 greg 2.9 devclose();
319 greg 2.1 quit(3);
320     }
321    
322    
323 schorsch 2.4 static void
324     sigdie( /* set fatal signal */
325     int signo,
326     char *msg
327     )
328 greg 2.1 {
329     if (signal(signo, onsig) == SIG_IGN)
330     signal(signo, SIG_IGN);
331     sigerr[signo] = msg;
332     }
333    
334    
335 schorsch 2.4 static void
336     printdefaults(void) /* print default values to stdout */
337 greg 2.1 {
338 greg 2.9 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
339 greg 2.1 printf(greyscale ? "-b+\t\t\t\t# greyscale on\n" :
340     "-b-\t\t\t\t# greyscale off\n");
341     printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
342     ourview.type==VT_PER ? "perspective" :
343     ourview.type==VT_PAR ? "parallel" :
344     ourview.type==VT_HEM ? "hemispherical" :
345     ourview.type==VT_ANG ? "angular" :
346     ourview.type==VT_CYL ? "cylindrical" :
347 greg 2.8 ourview.type==VT_PLS ? "planisphere" :
348 greg 2.1 "unknown");
349     printf("-vp %f %f %f\t# view point\n",
350     ourview.vp[0], ourview.vp[1], ourview.vp[2]);
351     printf("-vd %f %f %f\t# view direction\n",
352     ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
353     printf("-vu %f %f %f\t# view up\n",
354     ourview.vup[0], ourview.vup[1], ourview.vup[2]);
355     printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
356     printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
357     printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore);
358     printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft);
359     printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
360     printf("-vl %f\t\t\t# view lift\n", ourview.voff);
361     printf("-pe %f\t\t\t# pixel exposure\n", exposure);
362     printf("-ps %-9d\t\t\t# pixel sample\n", psample);
363     printf("-pt %f\t\t\t# pixel threshold\n", maxdiff);
364     printf("-o %s\t\t\t\t# output device\n", dvcname);
365     printf(erract[WARNING].pf != NULL ?
366     "-w+\t\t\t\t# warning messages on\n" :
367     "-w-\t\t\t\t# warning messages off\n");
368     print_rdefaults();
369     }