ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.3
Committed: Fri Oct 31 15:49:23 1997 UTC (26 years, 6 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.2: +27 -13 lines
Log Message:
bug fix in getradfile() for no variables to match

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Radiance holodeck generation controller
9 */
10
11 #include "rholo.h"
12 #include "paths.h"
13 #include <sys/types.h>
14
15 /* the following must be consistent with rholo.h */
16 int NVARS = NRHVARS; /* total number of variables */
17
18 VARIABLE vv[] = RHVINIT; /* variable-value pairs */
19
20 char *progname; /* our program name */
21 char *hdkfile; /* holodeck file name */
22 char froot[MAXPATH]; /* root file name */
23
24 int nowarn = 0; /* turn warnings off? */
25
26 double expval = 1.; /* global exposure value */
27
28 int ncprocs = 0; /* desired number of compute processes */
29
30 char *outdev = NULL; /* output device name */
31
32 time_t starttime; /* time we got started */
33 time_t endtime; /* time we should end by */
34 time_t reporttime; /* time for next report */
35
36 int rtargc = 1; /* rtrace command */
37 char *rtargv[128] = {"rtrace", NULL};
38
39 long nraysdone = 0L; /* number of rays done */
40 long npacksdone = 0L; /* number of packets done */
41
42 PACKET *freepacks; /* available packets */
43
44 extern time_t time();
45
46
47 main(argc, argv)
48 int argc;
49 char *argv[];
50 {
51 HDGRID hdg;
52 int i;
53 int force = 0;
54 /* mark start time */
55 starttime = time(NULL);
56 progname = argv[0]; /* get arguments */
57 for (i = 1; i < argc && argv[i][0] == '-'; i++)
58 switch (argv[i][1]) {
59 case 'w': /* turn off warnings */
60 nowarn++;
61 break;
62 case 'f': /* force overwrite */
63 force++;
64 break;
65 case 'n': /* compute processes */
66 if (i >= argc-2)
67 goto userr;
68 ncprocs = atoi(argv[++i]);
69 break;
70 case 'o': /* output display */
71 if (i >= argc-2)
72 goto userr;
73 outdev = argv[++i];
74 break;
75 default:
76 goto userr;
77 }
78 /* do we have a job? */
79 if (outdev == NULL && ncprocs <= 0)
80 goto userr;
81 /* get root file name */
82 rootname(froot, hdkfile=argv[i++]);
83 /* load... */
84 if (i < argc) { /* variables */
85 loadvars(argv[i++]);
86 /* cmdline settings */
87 for ( ; i < argc; i++)
88 if (setvariable(argv[i], matchvar) < 0) {
89 sprintf(errmsg, "unknown variable: %s",
90 argv[i]);
91 error(USER, errmsg);
92 }
93 /* check settings */
94 checkvalues();
95 /* load RIF if any */
96 if (vdef(RIF))
97 getradfile(vval(RIF));
98 /* set defaults */
99 setdefaults(&hdg);
100 /* holodeck exists? */
101 if (!force && access(hdkfile, R_OK|W_OK) == 0)
102 error(USER,
103 "holodeck file exists -- use -f to overwrite");
104 /* create holodeck */
105 creatholo(&hdg);
106 } else { /* else load holodeck */
107 loadholo();
108 if (vdef(RIF)) /* load RIF if any */
109 getradfile(vval(RIF));
110 }
111 /* initialize */
112 initrholo();
113 /* run */
114 while (rholo())
115 ;
116 /* done */
117 quit(0);
118 userr:
119 fprintf(stderr,
120 "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
121 progname);
122 quit(1);
123 }
124
125
126 initrholo() /* get our holodeck running */
127 {
128 extern int global_packet();
129 register int i;
130 /* check output device */
131 if (outdev != NULL)
132 open_display(outdev);
133 else if (ncprocs > 0) /* else use global ray feed */
134 init_global();
135 /* record end time */
136 if (!vdef(TIME) || vflt(TIME) <= FTINY)
137 endtime = 0;
138 else
139 endtime = starttime + vflt(TIME)*3600.;
140 /* set up memory cache */
141 hdcachesize = 1024.*1024.*vflt(CACHE);
142 /* open report file */
143 if (vdef(REPORT)) {
144 register char *s = sskip2(vval(REPORT), 1);
145 if (*s && freopen(s, "a", stderr) == NULL)
146 quit(2);
147 }
148 /* start rtrace */
149 if (ncprocs > 0) {
150 i = start_rtrace();
151 if (i < 1)
152 error(USER, "cannot start rtrace process");
153 if (vdef(REPORT)) { /* make first report */
154 printargs(rtargc, rtargv, stderr);
155 report(0);
156 }
157 /* allocate packets */
158 freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
159 if (freepacks == NULL)
160 goto memerr;
161 freepacks[--i].nr = 0;
162 freepacks[i].next = NULL;
163 if (!vbool(OBSTRUCTIONS)) {
164 freepacks[i].offset = (float *)bmalloc(
165 RPACKSIZ*sizeof(float)*(i+1) );
166 if (freepacks[i].offset == NULL)
167 goto memerr;
168 } else
169 freepacks[i].offset = NULL;
170 while (i--) {
171 freepacks[i].nr = 0;
172 freepacks[i].offset = freepacks[i+1].offset == NULL ?
173 NULL : freepacks[i+1].offset+RPACKSIZ ;
174 freepacks[i].next = &freepacks[i+1];
175 }
176 }
177 return;
178 memerr:
179 error(SYSTEM, "out of memory in initrholo");
180 }
181
182
183 rholo() /* holodeck main loop */
184 {
185 static int idle = 1;
186 PACKET *pl = NULL, *plend;
187 register PACKET *p;
188 time_t t;
189 long l;
190 /* check display */
191 if (outdev != NULL && !disp_check(idle))
192 return(0);
193 /* display only? */
194 if (ncprocs <= 0)
195 return(1);
196 /* check file size */
197 if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 &&
198 hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l)
199 return(0);
200 /* check time */
201 if (endtime > 0 || reporttime > 0)
202 t = time(NULL);
203 if (endtime > 0 && t >= endtime)
204 return(0);
205 if (reporttime > 0 && t >= reporttime)
206 report(t);
207 /* get packets to process */
208 while (freepacks != NULL) {
209 p = freepacks; freepacks = p->next; p->next = NULL;
210 if (!next_packet(p)) {
211 p->next = freepacks; freepacks = p;
212 break;
213 }
214 if (pl == NULL) pl = p;
215 else plend->next = p;
216 plend = p;
217 }
218 idle = pl == NULL && freepacks != NULL;
219 /* are we out of stuff to do? */
220 if (idle && outdev == NULL)
221 return(0);
222 /* else process packets */
223 done_packets(do_packets(pl));
224 return(1); /* and continue */
225 }
226
227
228 report(t) /* report progress so far */
229 time_t t;
230 {
231 if (t == 0)
232 t = time(NULL);
233 fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
234 progname, npacksdone, nraysdone, (t-starttime)/3600.);
235 fflush(stderr);
236 if (vdef(REPORT))
237 reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
238 }
239
240
241 setdefaults(gp) /* set default values */
242 register HDGRID *gp;
243 {
244 extern char *atos();
245 register int i;
246 double len[3], maxlen, d;
247 char buf[64];
248
249 if (!vdef(SECTION)) {
250 sprintf(errmsg, "%s must be defined", vnam(SECTION));
251 error(USER, errmsg);
252 }
253 if (vdef(SECTION) > 1) {
254 sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
255 error(WARNING, errmsg);
256 }
257 if (sscanf(vval(SECTION),
258 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
259 &gp->orig[0], &gp->orig[1], &gp->orig[2],
260 &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
261 &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
262 &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
263 badvalue(SECTION);
264 maxlen = 0.;
265 for (i = 0; i < 3; i++)
266 if ((len[i] = VLEN(gp->xv[i])) > maxlen)
267 maxlen = len[i];
268 if (!vdef(GRID)) {
269 sprintf(buf, "%.4f", maxlen/8.);
270 vval(GRID) = savqstr(buf);
271 vdef(GRID)++;
272 }
273 if ((d = vflt(GRID)) <= FTINY)
274 badvalue(GRID);
275 for (i = 0; i < 3; i++)
276 gp->grid[i] = len[i]/d + (1.-FTINY);
277 if (!vdef(EXPOSURE)) {
278 sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
279 error(USER, errmsg);
280 }
281 expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
282 pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
283 if (!vdef(OCTREE)) {
284 if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
285 error(SYSTEM, "out of memory");
286 sprintf(vval(OCTREE), "%s.oct", froot);
287 vdef(OCTREE)++;
288 }
289 if (!vdef(DISKSPACE)) {
290 sprintf(errmsg,
291 "no %s setting, assuming 100 Mbytes available",
292 vnam(DISKSPACE));
293 error(WARNING, errmsg);
294 vval(DISKSPACE) = "100";
295 vdef(DISKSPACE)++;
296 }
297 if (!vdef(CACHE)) {
298 sprintf(errmsg,
299 "no %s setting, assuming 10 Mbytes available",
300 vnam(CACHE));
301 error(WARNING, errmsg);
302 vval(CACHE) = "10";
303 vdef(CACHE)++;
304 }
305 if (!vdef(OBSTRUCTIONS)) {
306 vval(OBSTRUCTIONS) = "T";
307 vdef(OBSTRUCTIONS)++;
308 }
309 if (!vdef(OCCUPANCY)) {
310 vval(OCCUPANCY) = "U";
311 vdef(OCCUPANCY)++;
312 }
313 /* append rendering options */
314 if (vdef(RENDER))
315 rtargc += wordstring(rtargv+rtargc, vval(RENDER));
316 }
317
318
319 creatholo(gp) /* create a holodeck output file */
320 HDGRID *gp;
321 {
322 long endloc = 0;
323 FILE *fp;
324 /* open & truncate file */
325 if ((fp = fopen(hdkfile, "w+")) == NULL) {
326 sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
327 error(SYSTEM, errmsg);
328 }
329 /* write information header */
330 newheader("RADIANCE", fp);
331 printvars(fp);
332 fputformat(HOLOFMT, fp);
333 fputc('\n', fp);
334 putw(HOLOMAGIC, fp); /* put magic number & terminus */
335 fwrite(&endloc, sizeof(long), 1, fp);
336 fflush(fp); /* flush buffer */
337 hdinit(fileno(fp), gp); /* allocate and initialize index */
338 /* we're dropping fp here.... */
339 }
340
341
342 headline(s) /* process information header line */
343 char *s;
344 {
345 extern char FMTSTR[];
346 register char *cp;
347 char fmt[32];
348
349 if (formatval(fmt, s)) {
350 if (strcmp(fmt, HOLOFMT)) {
351 sprintf(errmsg, "%s file \"%s\" has %s%s",
352 HOLOFMT, hdkfile, FMTSTR, fmt);
353 error(USER, errmsg);
354 }
355 return;
356 }
357 for (cp = s; *cp; cp++) /* take off any comments */
358 if (*cp == '#') {
359 *cp = '\0';
360 break;
361 }
362 setvariable(s, matchvar); /* don't flag errors */
363 }
364
365
366 loadholo() /* start loading a holodeck from fname */
367 {
368 FILE *fp;
369 long endloc;
370 /* open input file */
371 if ((fp = fopen(hdkfile, "r+")) == NULL) {
372 sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile);
373 error(SYSTEM, errmsg);
374 }
375 /* load variables from header */
376 getheader(fp, headline, NULL);
377 /* check magic number */
378 if (getw(fp) != HOLOMAGIC) {
379 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
380 hdkfile);
381 error(USER, errmsg);
382 }
383 fread(&endloc, sizeof(long), 1, fp);
384 if (endloc != 0)
385 error(WARNING, "ignoring multiple sections in holodeck file");
386 fseek(fp, 0L, 1); /* align system file pointer */
387 hdinit(fileno(fp), NULL); /* allocate and load index */
388 /* we're dropping fp here.... */
389 }
390
391
392 done_packets(pl) /* handle finished packets */
393 PACKET *pl;
394 {
395 register PACKET *p;
396
397 while (pl != NULL) {
398 p = pl; pl = p->next; p->next = NULL;
399 if (p->nr > 0) { /* add to holodeck */
400 bcopy((char *)p->ra,
401 (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
402 p->nr*sizeof(RAYVAL));
403 if (outdev != NULL) /* display it */
404 disp_packet(p);
405 }
406 nraysdone += p->nr;
407 npacksdone++;
408 p->nr = 0; /* push onto free list */
409 p->next = freepacks;
410 freepacks = p;
411 }
412 }
413
414
415 getradfile(rfargs) /* run rad and get needed variables */
416 char *rfargs;
417 {
418 static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
419 static char tf1[] = TEMPLATE;
420 char tf2[64];
421 char combuf[256];
422 char *pippt;
423 register int i;
424 register char *cp;
425 /* create rad command */
426 mktemp(tf1);
427 sprintf(tf2, "%s.rif", tf1);
428 sprintf(combuf,
429 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
430 rfargs, tf1);
431 cp = combuf;
432 while (*cp){
433 if (*cp == '|') pippt = cp;
434 cp++;
435 } /* match unset variables */
436 for (i = 0; mvar[i] >= 0; i++)
437 if (!vdef(mvar[i])) {
438 *cp++ = '|';
439 strcpy(cp, vnam(mvar[i]));
440 while (*cp) cp++;
441 pippt = NULL;
442 }
443 if (pippt != NULL)
444 strcpy(pippt, "> /dev/null"); /* nothing to match */
445 else
446 sprintf(cp, ")[ \t]*=' > %s", tf2);
447 if (system(combuf)) {
448 error(SYSTEM, "cannot execute rad command");
449 unlink(tf2); /* clean up */
450 unlink(tf1);
451 quit(1);
452 }
453 if (pippt == NULL) {
454 loadvars(tf2); /* load variables */
455 unlink(tf2);
456 }
457 rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
458 unlink(tf1); /* clean up */
459 }
460
461
462 rootname(rn, fn) /* remove tail from end of fn */
463 register char *rn, *fn;
464 {
465 char *tp, *dp;
466
467 for (tp = NULL, dp = rn; *rn = *fn++; rn++)
468 if (ISDIRSEP(*rn))
469 dp = rn;
470 else if (*rn == '.')
471 tp = rn;
472 if (tp != NULL && tp > dp)
473 *tp = '\0';
474 }
475
476
477 badvalue(vc) /* report bad variable value and exit */
478 int vc;
479 {
480 sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
481 error(USER, errmsg);
482 }
483
484
485 eputs(s) /* put error message to stderr */
486 register char *s;
487 {
488 static int midline = 0;
489
490 if (!*s)
491 return;
492 if (!midline++) { /* prepend line with program name */
493 fputs(progname, stderr);
494 fputs(": ", stderr);
495 }
496 fputs(s, stderr);
497 if (s[strlen(s)-1] == '\n') {
498 fflush(stderr);
499 midline = 0;
500 }
501 }
502
503
504 wputs(s) /* put warning string to stderr */
505 char *s;
506 {
507 if (!nowarn)
508 eputs(s);
509 }
510
511
512 quit(ec) /* exit program gracefully */
513 int ec;
514 {
515 int status = 0;
516
517 if (hdlist[0] != NULL) { /* flush holodeck */
518 if (ncprocs > 0) {
519 done_packets(flush_queue());
520 status = end_rtrace(); /* close rtrace */
521 hdflush(NULL);
522 if (vdef(REPORT)) {
523 long fsiz, fuse;
524 report(0);
525 fsiz = lseek(hdlist[0]->fd, 0L, 2);
526 fuse = hdfiluse(hdlist[0]->fd, 1);
527 fprintf(stderr,
528 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
529 hdkfile, fsiz/(1024.*1024.),
530 100.*(fsiz-fuse)/fsiz);
531 }
532 } else
533 hdflush(NULL);
534 }
535 exit(ec ? ec : status); /* exit */
536 }