16 |
|
#include "lookup.h" |
17 |
|
#include "calcomp.h" |
18 |
|
|
19 |
+ |
#ifdef _WIN32 |
20 |
+ |
typedef int ssize_t; |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
#ifndef MAXMODLIST |
24 |
|
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
25 |
|
#endif |
26 |
|
|
27 |
< |
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
27 |
> |
ssize_t treebufsiz = BUFSIZ; /* current tree buffer size */ |
28 |
|
|
29 |
|
typedef double DCOLOR[3]; /* double-precision color */ |
30 |
|
|
96 |
|
struct rtproc *next; /* next in list of processes */ |
97 |
|
SUBPROC pd; /* rtrace pipe descriptors */ |
98 |
|
unsigned long raynum; /* ray number for this tree */ |
99 |
< |
int bsiz; /* ray tree buffer length */ |
99 |
> |
size_t bsiz; /* ray tree buffer length */ |
100 |
|
char *buf; /* ray tree buffer */ |
101 |
< |
int nbr; /* number of bytes from rtrace */ |
101 |
> |
size_t nbr; /* number of bytes from rtrace */ |
102 |
|
}; /* rtrace process buffer */ |
103 |
|
|
104 |
|
/* rtrace command and defaults */ |
105 |
|
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
106 |
< |
"-dj", ".5", "-dr", "3", |
106 |
> |
"-dj", ".9", "-dr", "3", |
107 |
|
"-ab", "1", "-ad", "350", }; |
108 |
|
|
109 |
|
int rtargc = 9; |
111 |
|
char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", |
112 |
|
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
113 |
|
|
114 |
< |
#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ |
115 |
< |
#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ |
114 |
> |
#define RTCOEFF "-o~~~TmWdp" /* compute coefficients only */ |
115 |
> |
#define RTCONTRIB "-o~~~TmVdp" /* compute ray contributions */ |
116 |
|
|
117 |
|
struct rtproc rt0; /* head of rtrace process list */ |
118 |
|
|
250 |
|
while ((j = expandarg(&argc, &argv, i)) > 0) |
251 |
|
; |
252 |
|
if (j < 0) { |
253 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
253 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
254 |
|
argv[0], argv[i]); |
255 |
|
exit(1); |
256 |
|
} |
344 |
|
case 'b': /* bin expression/count */ |
345 |
|
if (i >= argc-2) break; |
346 |
|
if (argv[i][2] == 'n') { |
347 |
< |
bincnt = atoi(argv[++i]); |
347 |
> |
bincnt = (int)(eval(argv[++i]) + .5); |
348 |
|
continue; |
349 |
|
} |
350 |
|
if (argv[i][2]) break; |
382 |
|
rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; |
383 |
|
/* just asking for defaults? */ |
384 |
|
if (!strcmp(argv[i], "-defaults")) { |
385 |
< |
char sxres[16], syres[16]; |
385 |
> |
char nps[8], sxres[16], syres[16]; |
386 |
|
char *rtpath; |
383 |
– |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
387 |
|
printf("-c %-5d\t\t\t# accumulated rays per record\n", |
388 |
|
accumulate); |
389 |
|
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', |
390 |
|
contrib ? "contributions" : "coefficients"); |
391 |
|
fflush(stdout); /* report OUR options */ |
392 |
+ |
rtargv[rtargc++] = "-n"; |
393 |
+ |
sprintf(nps, "%d", nprocs); |
394 |
+ |
rtargv[rtargc++] = nps; |
395 |
|
rtargv[rtargc++] = header ? "-h+" : "-h-"; |
396 |
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt); |
397 |
|
rtargv[rtargc++] = fmt; |
549 |
|
raysleft = 0; |
550 |
|
if ((account = accumulate) > 0) |
551 |
|
raysleft *= accumulate; |
552 |
< |
waitflush = xres; |
552 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
553 |
|
if (!recover) |
554 |
|
return; |
555 |
|
/* recover previous values */ |
586 |
|
error(USER, errmsg); |
587 |
|
} |
588 |
|
if (nmods >= MAXMODLIST) |
589 |
< |
error(USER, "too many modifiers"); |
589 |
> |
error(INTERNAL, "too many modifiers"); |
590 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
591 |
|
lep->key = modn; /* XXX assumes static string */ |
592 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
594 |
|
error(SYSTEM, "out of memory in addmodifier"); |
595 |
|
mp->outspec = outf; /* XXX assumes static string */ |
596 |
|
mp->modname = modn; /* XXX assumes static string */ |
597 |
< |
if (binv != NULL) |
598 |
< |
mp->binv = eparse(binv); |
599 |
< |
else |
600 |
< |
mp->binv = eparse("0"); |
601 |
< |
mp->nbins = 1; |
597 |
> |
if (binv == NULL) |
598 |
> |
binv = "0"; /* use single bin if unspecified */ |
599 |
> |
mp->binv = eparse(binv); |
600 |
> |
if (mp->binv->type == NUM) { /* check value if constant */ |
601 |
> |
bincnt = (int)(evalue(mp->binv) + 1.5); |
602 |
> |
if (bincnt != 1) { |
603 |
> |
sprintf(errmsg, "illegal non-zero constant for bin (%s)", |
604 |
> |
binv); |
605 |
> |
error(USER, errmsg); |
606 |
> |
} |
607 |
> |
} |
608 |
> |
mp->nbins = 1; /* initialize results holder */ |
609 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
610 |
< |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
598 |
< |
bincnt = 1; |
599 |
< |
else if (bincnt > 1) |
610 |
> |
if (bincnt > 1) |
611 |
|
mp = growmodifier(mp, bincnt); |
612 |
|
lep->data = (char *)mp; |
613 |
|
/* allocate output streams */ |
670 |
|
mnp = cp; |
671 |
|
break; |
672 |
|
case 'd': |
673 |
+ |
case 'i': |
674 |
+ |
case 'o': |
675 |
+ |
case 'x': |
676 |
+ |
case 'X': |
677 |
|
if (bnp != NULL) |
678 |
|
return -1; |
679 |
|
bnp = cp; |
706 |
|
printheader(FILE *fout, const char *info) |
707 |
|
{ |
708 |
|
extern char VersionID[]; |
709 |
< |
FILE *fin = fopen(octree, "r"); |
710 |
< |
|
711 |
< |
if (fin == NULL) |
712 |
< |
quit(1); |
713 |
< |
checkheader(fin, "ignore", fout); /* copy octree header */ |
714 |
< |
fclose(fin); |
709 |
> |
/* copy octree header */ |
710 |
> |
if (octree[0] == '!') { |
711 |
> |
newheader("RADIANCE", fout); |
712 |
> |
fputs(octree+1, fout); |
713 |
> |
if (octree[strlen(octree)-1] != '\n') |
714 |
> |
fputc('\n', fout); |
715 |
> |
} else { |
716 |
> |
FILE *fin = fopen(octree, "r"); |
717 |
> |
if (fin == NULL) |
718 |
> |
quit(1); |
719 |
> |
checkheader(fin, "ignore", fout); |
720 |
> |
fclose(fin); |
721 |
> |
} |
722 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
723 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
724 |
|
fputnow(fout); |
747 |
|
{ |
748 |
|
if ((xr > 0) & (yr > 0)) /* resolution string */ |
749 |
|
fprtresolu(xr, yr, fout); |
728 |
– |
if (xres > 0) /* global flush flag */ |
729 |
– |
fflush(fout); |
750 |
|
} |
751 |
|
|
752 |
|
/* Get output stream pointer (open and write header if new and noopen==0) */ |
767 |
|
if (header) |
768 |
|
printheader(stdout, NULL); |
769 |
|
printresolu(stdout, xres, yres); |
770 |
+ |
if (waitflush > 0) |
771 |
+ |
fflush(stdout); |
772 |
|
stdos.xr = xres; stdos.yr = yres; |
773 |
|
using_stdout = 1; |
774 |
|
} |
837 |
|
if (outfmt == 'a') |
838 |
|
putc('\n', sop->ofp); |
839 |
|
} |
840 |
< |
if (xres > 0) |
840 |
> |
if (waitflush > 0) |
841 |
|
fflush(sop->ofp); |
842 |
|
} |
843 |
|
sop->reclen += noopen; /* add to length if noopen */ |
1004 |
|
if (using_stdout & (outfmt == 'a')) |
1005 |
|
putc('\n', stdout); |
1006 |
|
if (!waitflush) { |
1007 |
< |
waitflush = xres; |
1007 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
1008 |
|
if (using_stdout) |
1009 |
|
fflush(stdout); |
1010 |
|
} |
1086 |
|
{ |
1087 |
|
struct rtproc *rtfree = NULL; |
1088 |
|
fd_set readset, errset; |
1089 |
< |
int nr; |
1089 |
> |
ssize_t nr; |
1090 |
|
struct rtproc *rt; |
1091 |
|
int n; |
1092 |
|
|
1117 |
|
continue; |
1118 |
|
if (rt->buf == NULL) { |
1119 |
|
rt->bsiz = treebufsiz; |
1120 |
< |
rt->buf = (char *)malloc(treebufsiz); |
1120 |
> |
rt->buf = (char *)malloc(rt->bsiz); |
1121 |
|
} else if (rt->nbr + BUFSIZ > rt->bsiz) { |
1122 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
1123 |
|
rt->bsiz = treebufsiz; |
1124 |
< |
else |
1125 |
< |
treebufsiz = rt->bsiz += BUFSIZ; |
1124 |
> |
else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0) |
1125 |
> |
error(INTERNAL, |
1126 |
> |
"ray buffer does not fit memory"); |
1127 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
1128 |
|
} |
1129 |
|
if (rt->buf == NULL) |
1130 |
|
error(SYSTEM, "out of memory in wait_rproc"); |
1131 |
< |
nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); |
1132 |
< |
if (nr <= 0) |
1131 |
> |
nr = rt->bsiz - rt->nbr; |
1132 |
> |
if (nr & ~0x7fffffff) /* avoid 32-bit OS issues */ |
1133 |
> |
nr = 0x7fffffff; |
1134 |
> |
nr = read(rt->pd.r, rt->buf+rt->nbr, nr); |
1135 |
> |
if (nr < 0) |
1136 |
> |
error(SYSTEM, "read error from rtrace"); |
1137 |
> |
if (!nr) |
1138 |
|
error(USER, "rtrace process died"); |
1139 |
|
rt->nbr += nr; /* advance & check */ |
1140 |
< |
if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, |
1141 |
< |
"~\t~\t", 4)) { |
1142 |
< |
rt->nbr -= 4; /* elide terminator */ |
1140 |
> |
if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6, |
1141 |
> |
"~\t~\t~\t", 6)) { |
1142 |
> |
rt->nbr -= 6; /* elide terminator */ |
1143 |
|
queue_raytree(rt); |
1144 |
|
rtfree = rt; /* ready for next ray */ |
1145 |
|
} |
1176 |
|
"dummy ray(s) ignored during accumulation\n"); |
1177 |
|
continue; |
1178 |
|
} |
1179 |
< |
if (!iblen || /* need reset? */ |
1179 |
> |
if (!iblen || /* need flush/reset? */ |
1180 |
|
queue_length() > 10*nrtprocs() || |
1181 |
|
lastray+1 < lastray) { |
1182 |
|
while (wait_rproc() != NULL) |
1183 |
|
process_queue(); |
1184 |
< |
if (lastray+1 < lastray) |
1157 |
< |
lastdone = lastray = 0; |
1184 |
> |
lastdone = lastray = 0; |
1185 |
|
} |
1186 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1187 |
|
rtp->raynum = ++lastray; /* assign ray */ |
1188 |
|
if (iblen) { /* trace ray if valid */ |
1189 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
1190 |
|
} else { /* else bypass dummy ray */ |
1191 |
< |
queue_raytree(rtp); /* empty tree */ |
1192 |
< |
if ((yres <= 0) | (waitflush > 1)) |
1193 |
< |
waitflush = 1; /* flush after this */ |
1191 |
> |
queue_raytree(rtp); /* queue empty ray/record */ |
1192 |
> |
if ((yres <= 0) | (xres <= 0)) |
1193 |
> |
waitflush = 1; /* flush right after */ |
1194 |
|
} |
1195 |
|
process_queue(); /* catch up with results */ |
1196 |
|
if (raysleft && !--raysleft) |
1245 |
|
STREAMOUT *sop = (STREAMOUT *)e->data; |
1246 |
|
|
1247 |
|
if (sop->ofp == NULL) |
1248 |
< |
return; |
1248 |
> |
return(0); |
1249 |
|
fclose(sop->ofp); |
1250 |
|
sop->ofp = NULL; |
1251 |
+ |
return(0); |
1252 |
|
} |
1253 |
|
|
1254 |
|
/* load previously accumulated values */ |