123 |
|
int outfmt = 'a'; /* output format */ |
124 |
|
|
125 |
|
int header = 1; /* output header? */ |
126 |
+ |
int force_open = 0; /* truncate existing output? */ |
127 |
|
int xres = 0; /* horiz. output resolution */ |
128 |
|
int yres = 0; /* vert. output resolution */ |
129 |
|
|
267 |
|
continue; |
268 |
|
} |
269 |
|
break; |
270 |
< |
case 'f': /* file or i/o format */ |
270 |
> |
case 'f': /* file or force or format */ |
271 |
|
if (!argv[i][2]) { |
272 |
|
char *fpath; |
273 |
|
if (i >= argc-2) break; |
282 |
|
fcompile(fpath); |
283 |
|
continue; |
284 |
|
} |
285 |
+ |
if (argv[i][2] == 'o') { |
286 |
+ |
force_open++; |
287 |
+ |
continue; |
288 |
+ |
} |
289 |
|
setformat(argv[i]+2); |
290 |
|
continue; |
291 |
|
case 'e': /* expression */ |
707 |
|
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
708 |
|
if (sop == NULL) |
709 |
|
error(SYSTEM, "out of memory in getostream"); |
710 |
< |
sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; |
710 |
> |
sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; |
711 |
|
sop->ofp = NULL; /* open iff noopen==0 */ |
712 |
|
lep->data = (char *)sop; |
713 |
|
} |
714 |
|
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
715 |
< |
int i; |
715 |
> |
long i; |
716 |
|
if (oname[0] == '!') /* output to command */ |
717 |
|
sop->ofp = popen(oname+1, "w"); |
718 |
< |
else |
718 |
> |
else if (!force_open && access(oname, F_OK) == 0) |
719 |
> |
errno = EEXIST; /* file exists */ |
720 |
> |
else /* else open it */ |
721 |
|
sop->ofp = fopen(oname, "w"); |
722 |
|
if (sop->ofp == NULL) { |
723 |
|
sprintf(errmsg, "cannot open '%s' for writing", oname); |
741 |
|
} |
742 |
|
printresolu(sop->ofp); |
743 |
|
/* play catch-up */ |
744 |
< |
for (i = 0; i < lastdone; i++) { |
744 |
> |
for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; |
745 |
> |
i--; ) { |
746 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
747 |
|
put_contrib(nocontrib, sop->ofp); |
748 |
|
if (outfmt == 'a') |
760 |
|
int |
761 |
|
getinp(char *buf, FILE *fp) |
762 |
|
{ |
763 |
+ |
double dv[3]; |
764 |
+ |
float fv[3]; |
765 |
|
char *cp; |
766 |
|
int i; |
767 |
|
|
770 |
|
cp = buf; /* make sure we get 6 floats */ |
771 |
|
for (i = 0; i < 6; i++) { |
772 |
|
if (fgetword(cp, buf+127-cp, fp) == NULL) |
773 |
< |
return 0; |
773 |
> |
return -1; |
774 |
> |
if (i >= 3) |
775 |
> |
dv[i-3] = atof(cp); |
776 |
|
if ((cp = fskip(cp)) == NULL || *cp) |
777 |
< |
return 0; |
777 |
> |
return -1; |
778 |
|
*cp++ = ' '; |
779 |
|
} |
780 |
|
getc(fp); /* get/put eol */ |
781 |
|
*cp-- = '\0'; *cp = '\n'; |
782 |
+ |
if (DOT(dv,dv) <= FTINY*FTINY) |
783 |
+ |
return 0; /* dummy ray */ |
784 |
|
return strlen(buf); |
785 |
|
case 'f': |
786 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
787 |
< |
return 0; |
787 |
> |
return -1; |
788 |
> |
memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); |
789 |
> |
if (DOT(fv,fv) <= FTINY*FTINY) |
790 |
> |
return 0; /* dummy ray */ |
791 |
|
return sizeof(float)*6; |
792 |
|
case 'd': |
793 |
|
if (fread(buf, sizeof(double), 6, fp) < 6) |
794 |
< |
return 0; |
794 |
> |
return -1; |
795 |
> |
memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); |
796 |
> |
if (DOT(dv,dv) <= FTINY*FTINY) |
797 |
> |
return 0; /* dummy ray */ |
798 |
|
return sizeof(double)*6; |
799 |
|
} |
800 |
|
error(INTERNAL, "botched input format"); |
801 |
< |
return 0; /* pro forma return */ |
801 |
> |
return -1; /* pro forma return */ |
802 |
|
} |
803 |
|
|
804 |
|
static float rparams[9]; /* traced ray parameters */ |
972 |
|
} |
973 |
|
done_contrib(); /* sum up contributions & output */ |
974 |
|
lastdone = rtp->raynum; |
975 |
< |
free(rtp->buf); /* free up buffer space */ |
975 |
> |
if (rtp->buf != NULL) /* free up buffer space */ |
976 |
> |
free(rtp->buf); |
977 |
|
rt_unproc = rtp->next; |
978 |
|
free(rtp); /* done with this ray tree */ |
979 |
|
} |
1061 |
|
int iblen; |
1062 |
|
struct rtproc *rtp; |
1063 |
|
/* loop over input */ |
1064 |
< |
while ((iblen = getinp(inpbuf, fin)) > 0) { |
1065 |
< |
if (lastray+1 < lastray || /* need reset? */ |
1066 |
< |
queue_length() > 5*nrtprocs()) { |
1064 |
> |
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
1065 |
> |
if (!iblen || /* need reset? */ |
1066 |
> |
queue_length() > 10*nrtprocs() || |
1067 |
> |
lastray+1 < lastray) { |
1068 |
|
while (wait_rproc() != NULL) |
1069 |
|
process_queue(); |
1070 |
< |
lastdone = lastray = 0; |
1070 |
> |
if (lastray+1 < lastray) |
1071 |
> |
lastdone = lastray = 0; |
1072 |
|
} |
1073 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1074 |
< |
rtp->raynum = ++lastray; /* assign ray to it */ |
1075 |
< |
writebuf(rtp->pd.w, inpbuf, iblen); |
1074 |
> |
rtp->raynum = ++lastray; /* assign ray */ |
1075 |
> |
if (iblen) { /* trace ray if valid */ |
1076 |
> |
writebuf(rtp->pd.w, inpbuf, iblen); |
1077 |
> |
} else { /* else bypass dummy ray */ |
1078 |
> |
queue_raytree(rtp); /* empty tree */ |
1079 |
> |
if ((yres <= 0) | (waitflush > 0)) |
1080 |
> |
waitflush = 1; /* flush after this */ |
1081 |
> |
} |
1082 |
|
if (raysleft && !--raysleft) |
1083 |
|
break; |
1084 |
|
process_queue(); /* catch up with results */ |
1157 |
|
if (oent->data != NULL) { |
1158 |
|
sout = *(STREAMOUT *)oent->data; |
1159 |
|
} else { |
1160 |
< |
sout.reclen = 0; |
1160 |
> |
sout.reclen = CNT_UNKNOWN; |
1161 |
|
sout.ofp = NULL; |
1162 |
|
} |
1163 |
|
if (sout.ofp != NULL) { /* already open? */ |
1239 |
|
lu_doall(&ofiletab, myseeko, &nvals); |
1240 |
|
/* skip repeated input */ |
1241 |
|
for (nvals = 0; nvals < lastout; nvals++) |
1242 |
< |
if (getinp(oname, fin) <= 0) |
1242 |
> |
if (getinp(oname, fin) < 0) |
1243 |
|
error(USER, "unexpected EOF on input"); |
1244 |
|
lastray = lastdone = (unsigned long)lastout; |
1245 |
|
if (raysleft) |