12 |
|
|
13 |
|
#include "ray.h" |
14 |
|
|
15 |
+ |
#include <sys/types.h> |
16 |
+ |
|
17 |
+ |
#ifndef NIX |
18 |
|
#ifdef BSD |
19 |
|
#include <sys/time.h> |
20 |
|
#include <sys/resource.h> |
21 |
+ |
#else |
22 |
+ |
#include <sys/times.h> |
23 |
+ |
#include <sys/utsname.h> |
24 |
+ |
#include <limits.h> |
25 |
|
#endif |
26 |
+ |
#endif |
27 |
|
|
28 |
+ |
extern time_t time(); |
29 |
+ |
|
30 |
|
#include <signal.h> |
31 |
|
|
32 |
|
#include "view.h" |
37 |
|
|
38 |
|
#include "paths.h" |
39 |
|
|
40 |
+ |
#define RFTEMPLATE "rfXXXXXX" |
41 |
+ |
|
42 |
+ |
#ifndef SIGCONT |
43 |
+ |
#define SIGCONT SIGIO |
44 |
+ |
#endif |
45 |
+ |
|
46 |
|
int dimlist[MAXDIM]; /* sampling dimensions */ |
47 |
|
int ndims = 0; /* number of sampling dimensions */ |
48 |
|
int samplendx; /* sample index number */ |
61 |
|
double shadcert = .5; /* shadow certainty */ |
62 |
|
int directrelay = 1; /* number of source relays */ |
63 |
|
int vspretest = 512; /* virtual source pretest density */ |
64 |
< |
int directinvis = 0; /* sources invisible? */ |
64 |
> |
int directvis = 1; /* sources visible? */ |
65 |
|
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
66 |
|
|
67 |
|
double specthresh = .15; /* specular sampling threshold */ |
87 |
|
|
88 |
|
double pctdone = 0.0; /* percentage done */ |
89 |
|
|
90 |
< |
long tlastrept = 0L; /* time at last report */ |
90 |
> |
time_t tlastrept = 0L; /* time at last report */ |
91 |
|
|
92 |
< |
extern long time(); |
77 |
< |
extern long tstart; /* starting time */ |
92 |
> |
extern time_t tstart; /* starting time */ |
93 |
|
|
94 |
< |
extern long nrays; /* number of rays traced */ |
94 |
> |
extern unsigned long nrays; /* number of rays traced */ |
95 |
|
|
96 |
|
#define MAXDIV 16 /* maximum sample size */ |
97 |
|
|
98 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
99 |
|
|
85 |
– |
#define RFTEMPLATE "rfXXXXXX" |
86 |
– |
#define HFTEMPLATE TEMPLATE |
87 |
– |
|
88 |
– |
static char *hfname = NULL; /* header file name */ |
89 |
– |
static FILE *hfp = NULL; /* header file pointer */ |
90 |
– |
|
100 |
|
static int hres, vres; /* resolution for this frame */ |
101 |
|
|
102 |
|
extern char *mktemp(); |
103 |
|
|
104 |
|
double pixvalue(); |
105 |
|
|
106 |
+ |
#ifdef NIX |
107 |
+ |
#define file_exists(f) (access(f,F_OK)==0) |
108 |
+ |
#else |
109 |
+ |
#include <sys/types.h> |
110 |
+ |
#include <sys/stat.h> |
111 |
+ |
int |
112 |
+ |
file_exists(fname) /* ordinary file exists? */ |
113 |
+ |
char *fname; |
114 |
+ |
{ |
115 |
+ |
struct stat sbuf; |
116 |
+ |
if (stat(fname, &sbuf) < 0) return(0); |
117 |
+ |
return((sbuf.st_mode & S_IFREG) != 0); |
118 |
+ |
} |
119 |
+ |
#endif |
120 |
|
|
121 |
+ |
|
122 |
|
quit(code) /* quit program */ |
123 |
|
int code; |
124 |
|
{ |
125 |
|
if (code) /* report status */ |
126 |
|
report(); |
127 |
< |
if (hfname != NULL) { /* delete header file */ |
128 |
< |
if (hfp != NULL) |
105 |
< |
fclose(hfp); |
106 |
< |
unlink(hfname); |
107 |
< |
} |
127 |
> |
headclean(); /* delete header file */ |
128 |
> |
pfclean(); /* clean up persist files */ |
129 |
|
exit(code); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
< |
#ifdef BSD |
133 |
> |
#ifndef NIX |
134 |
|
report() /* report progress */ |
135 |
|
{ |
136 |
+ |
double u, s; |
137 |
+ |
#ifdef BSD |
138 |
+ |
char hostname[257]; |
139 |
|
struct rusage rubuf; |
140 |
< |
double t; |
140 |
> |
#else |
141 |
> |
struct tms tbuf; |
142 |
> |
struct utsname nambuf; |
143 |
> |
#define hostname nambuf.sysname |
144 |
> |
#endif |
145 |
|
|
146 |
+ |
tlastrept = time((time_t *)NULL); |
147 |
+ |
#ifdef BSD |
148 |
|
getrusage(RUSAGE_SELF, &rubuf); |
149 |
< |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
150 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
149 |
> |
u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
150 |
> |
s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
151 |
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
152 |
< |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
153 |
< |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
152 |
> |
u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; |
153 |
> |
s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; |
154 |
> |
gethostname(hostname, sizeof(hostname)); |
155 |
> |
#else |
156 |
> |
times(&tbuf); |
157 |
> |
u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK; |
158 |
> |
s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK; |
159 |
> |
uname(&nambuf); |
160 |
> |
#endif |
161 |
|
|
162 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
163 |
< |
nrays, pctdone, t/3600.0); |
162 |
> |
sprintf(errmsg, |
163 |
> |
"%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", |
164 |
> |
nrays, pctdone, u/3600., s/3600., |
165 |
> |
(tlastrept-tstart)/3600., hostname); |
166 |
|
eputs(errmsg); |
167 |
< |
tlastrept = time((long *)0); |
167 |
> |
#undef hostname |
168 |
|
} |
169 |
|
#else |
170 |
|
report() /* report progress */ |
171 |
|
{ |
172 |
< |
tlastrept = time((long *)0); |
173 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
172 |
> |
tlastrept = time((time_t *)NULL); |
173 |
> |
sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", |
174 |
|
nrays, pctdone, (tlastrept-tstart)/3600.0); |
175 |
|
eputs(errmsg); |
176 |
< |
signal(SIGALRM, report); |
176 |
> |
signal(SIGCONT, report); |
177 |
|
} |
178 |
|
#endif |
179 |
|
|
180 |
|
|
142 |
– |
openheader() /* save standard output to header file */ |
143 |
– |
{ |
144 |
– |
hfname = mktemp(HFTEMPLATE); |
145 |
– |
if (freopen(hfname, "w", stdout) == NULL) { |
146 |
– |
sprintf(errmsg, "cannot open header file \"%s\"", hfname); |
147 |
– |
error(SYSTEM, errmsg); |
148 |
– |
} |
149 |
– |
} |
150 |
– |
|
151 |
– |
|
152 |
– |
closeheader() /* done with header output */ |
153 |
– |
{ |
154 |
– |
if (hfname == NULL) |
155 |
– |
return; |
156 |
– |
if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL) |
157 |
– |
error(SYSTEM, "error reopening header file"); |
158 |
– |
#ifdef MSDOS |
159 |
– |
setmode(fileno(hfp), O_BINARY); |
160 |
– |
#endif |
161 |
– |
} |
162 |
– |
|
163 |
– |
|
164 |
– |
dupheader() /* repeat header on standard output */ |
165 |
– |
{ |
166 |
– |
register int c; |
167 |
– |
|
168 |
– |
if (fseek(hfp, 0L, 0) < 0) |
169 |
– |
error(SYSTEM, "seek error on header file"); |
170 |
– |
while ((c = getc(hfp)) != EOF) |
171 |
– |
putchar(c); |
172 |
– |
} |
173 |
– |
|
174 |
– |
|
181 |
|
rpict(seq, pout, zout, prvr) /* generate image(s) */ |
182 |
|
int seq; |
183 |
|
char *pout, *zout, *prvr; |
193 |
|
* sequenced file naming. |
194 |
|
*/ |
195 |
|
{ |
196 |
< |
extern char *rindex(), *strncpy(), *strcat(); |
196 |
> |
extern char *rindex(), *strncpy(), *strcat(), *strcpy(); |
197 |
|
char fbuf[128], fbuf2[128]; |
198 |
+ |
int npicts; |
199 |
|
register char *cp; |
200 |
|
RESOLU rs; |
201 |
|
double pa; |
195 |
– |
/* finished writing header */ |
196 |
– |
closeheader(); |
202 |
|
/* check sampling */ |
203 |
|
if (psample < 1) |
204 |
|
psample = 1; |
222 |
|
error(USER, "unexpected EOF on view input"); |
223 |
|
prvr = fbuf; /* mark for renaming */ |
224 |
|
} |
225 |
< |
if (pout != NULL) { |
225 |
> |
if (pout != NULL & prvr != NULL) { |
226 |
|
sprintf(fbuf, pout, seq); |
227 |
< |
if (prvr != NULL && !strcmp(prvr, fbuf)) { /* rename */ |
228 |
< |
fbuf2[0] = '\0'; |
229 |
< |
if ((cp = rindex(fbuf, '/')) != NULL) |
230 |
< |
strncpy(fbuf2, fbuf, cp-fbuf+1); |
231 |
< |
strcat(fbuf2, RFTEMPLATE); |
227 |
> |
if (!strcmp(prvr, fbuf)) { /* rename */ |
228 |
> |
strcpy(fbuf2, fbuf); |
229 |
> |
for (cp = fbuf2; *cp; cp++) |
230 |
> |
; |
231 |
> |
while (cp > fbuf2 && !ISDIRSEP(cp[-1])) |
232 |
> |
cp--; |
233 |
> |
strcpy(cp, RFTEMPLATE); |
234 |
|
prvr = mktemp(fbuf2); |
235 |
< |
if (rename(fbuf, prvr) < 0 && errno != ENOENT) { |
236 |
< |
sprintf(errmsg, |
235 |
> |
if (rename(fbuf, prvr) < 0) |
236 |
> |
if (errno == ENOENT) { /* ghost file */ |
237 |
> |
sprintf(errmsg, |
238 |
> |
"new output file \"%s\"", |
239 |
> |
fbuf); |
240 |
> |
error(WARNING, errmsg); |
241 |
> |
prvr = NULL; |
242 |
> |
} else { /* serious error */ |
243 |
> |
sprintf(errmsg, |
244 |
|
"cannot rename \"%s\" to \"%s\"", |
245 |
|
fbuf, prvr); |
246 |
< |
error(SYSTEM, errmsg); |
247 |
< |
} |
246 |
> |
error(SYSTEM, errmsg); |
247 |
> |
} |
248 |
|
} |
249 |
|
} |
250 |
< |
/* render sequence */ |
250 |
> |
npicts = 0; /* render sequence */ |
251 |
|
do { |
252 |
|
if (seq && nextview(stdin) == EOF) |
253 |
|
break; |
254 |
+ |
pctdone = 0.0; |
255 |
|
if (pout != NULL) { |
256 |
|
sprintf(fbuf, pout, seq); |
257 |
+ |
if (file_exists(fbuf)) { |
258 |
+ |
if (prvr != NULL || !strcmp(fbuf, pout)) { |
259 |
+ |
sprintf(errmsg, |
260 |
+ |
"output file \"%s\" exists", |
261 |
+ |
fbuf); |
262 |
+ |
error(USER, errmsg); |
263 |
+ |
} |
264 |
+ |
continue; /* don't clobber */ |
265 |
+ |
} |
266 |
|
if (freopen(fbuf, "w", stdout) == NULL) { |
267 |
|
sprintf(errmsg, |
268 |
|
"cannot open output file \"%s\"", fbuf); |
310 |
|
cp = NULL; |
311 |
|
render(cp, prvr); |
312 |
|
prvr = NULL; |
313 |
+ |
npicts++; |
314 |
|
} while (seq++); |
315 |
+ |
/* check that we did something */ |
316 |
+ |
if (npicts == 0) |
317 |
+ |
error(WARNING, "no output produced"); |
318 |
|
} |
319 |
|
|
320 |
|
|
360 |
|
sampdens[i] = hstep; |
361 |
|
} else |
362 |
|
sampdens = NULL; |
363 |
< |
/* open z file */ |
363 |
> |
/* open z-file */ |
364 |
|
if (zfile != NULL) { |
365 |
|
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
366 |
< |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
366 |
> |
sprintf(errmsg, "cannot open z-file \"%s\"", zfile); |
367 |
|
error(SYSTEM, errmsg); |
368 |
|
} |
369 |
|
#ifdef MSDOS |
385 |
|
i = salvage(oldfile); |
386 |
|
if (zfd != -1 && i > 0 && |
387 |
|
lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) |
388 |
< |
error(SYSTEM, "z file seek error in render"); |
388 |
> |
error(SYSTEM, "z-file seek error in render"); |
389 |
|
pctdone = 100.0*i/vres; |
390 |
|
if (ralrm > 0) /* report init stats */ |
391 |
|
report(); |
392 |
|
#ifndef BSD |
393 |
|
else |
394 |
|
#endif |
395 |
< |
signal(SIGALRM, report); |
395 |
> |
signal(SIGCONT, report); |
396 |
|
ypos = vres-1 - i; |
397 |
|
fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); |
398 |
|
/* compute scanlines */ |
415 |
|
fillscanbar(scanbar, zbar, hres, ypos, ystep); |
416 |
|
/* write it out */ |
417 |
|
#ifndef BSD |
418 |
< |
signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ |
418 |
> |
signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ |
419 |
|
#endif |
420 |
|
for (i = ystep; i > 0; i--) { |
421 |
|
if (zfd != -1 && write(zfd, (char *)zbar[i], |
429 |
|
goto writerr; |
430 |
|
/* record progress */ |
431 |
|
pctdone = 100.0*(vres-1-ypos)/vres; |
432 |
< |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
432 |
> |
if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) |
433 |
|
report(); |
434 |
|
#ifndef BSD |
435 |
|
else |
436 |
< |
signal(SIGALRM, report); |
436 |
> |
signal(SIGCONT, report); |
437 |
|
#endif |
438 |
|
} |
439 |
|
/* clean up */ |
440 |
< |
signal(SIGALRM, SIG_IGN); |
440 |
> |
signal(SIGCONT, SIG_IGN); |
441 |
|
if (zfd != -1) { |
442 |
|
if (write(zfd, (char *)zbar[0], hres*sizeof(float)) |
443 |
|
< hres*sizeof(float)) |
457 |
|
pctdone = 100.0; |
458 |
|
if (ralrm > 0) |
459 |
|
report(); |
460 |
+ |
signal(SIGCONT, SIG_DFL); |
461 |
|
return; |
462 |
|
writerr: |
463 |
|
error(SYSTEM, "write error in render"); |
631 |
|
setmode(fileno(fp), O_BINARY); |
632 |
|
#endif |
633 |
|
/* discard header */ |
634 |
< |
getheader(fp, NULL); |
634 |
> |
getheader(fp, NULL, NULL); |
635 |
|
/* get picture size */ |
636 |
|
if (!fscnresolu(&x, &y, fp)) { |
637 |
< |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
637 |
> |
sprintf(errmsg, "bad recover file \"%s\" - not removed", |
638 |
> |
oldfile); |
639 |
|
error(WARNING, errmsg); |
640 |
|
fclose(fp); |
641 |
|
return(0); |