ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.33
Committed: Thu Oct 10 09:18:58 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.32: +8 -5 lines
Log Message:
changed to quincunx pattern for initial image samples

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rpict.c - routines and variables for picture generation.
9 *
10 * 8/14/85
11 */
12
13 #include "ray.h"
14
15 #ifdef BSD
16 #include <sys/time.h>
17 #include <sys/resource.h>
18 #else
19 #include <signal.h>
20 #endif
21 #include <fcntl.h>
22
23 #include "view.h"
24
25 #include "random.h"
26
27 int dimlist[MAXDIM]; /* sampling dimensions */
28 int ndims = 0; /* number of sampling dimensions */
29 int samplendx; /* sample index number */
30
31 VIEW ourview = STDVIEW; /* view parameters */
32 int hresolu = 512; /* horizontal resolution */
33 int vresolu = 512; /* vertical resolution */
34 double pixaspect = 1.0; /* pixel aspect ratio */
35
36 int psample = 4; /* pixel sample size */
37 double maxdiff = .05; /* max. difference for interpolation */
38 double dstrpix = 0.67; /* square pixel distribution */
39
40 double dstrsrc = 0.0; /* square source distribution */
41 double shadthresh = .05; /* shadow threshold */
42 double shadcert = .5; /* shadow certainty */
43 int directrelay = 0; /* number of source relays */
44 int vspretest = 512; /* virtual source pretest density */
45 int directinvis = 0; /* sources invisible? */
46
47 int maxdepth = 6; /* maximum recursion depth */
48 double minweight = 5e-3; /* minimum ray weight */
49
50 COLOR ambval = BLKCOLOR; /* ambient value */
51 double ambacc = 0.2; /* ambient accuracy */
52 int ambres = 32; /* ambient resolution */
53 int ambdiv = 128; /* ambient divisions */
54 int ambssamp = 0; /* ambient super-samples */
55 int ambounce = 0; /* ambient bounces */
56 char *amblist[128]; /* ambient include/exclude list */
57 int ambincl = -1; /* include == 1, exclude == 0 */
58
59 int ralrm = 0; /* seconds between reports */
60
61 double pctdone = 0.0; /* percentage done */
62
63 long tlastrept = 0L; /* time at last report */
64
65 extern long time();
66 extern long tstart; /* starting time */
67
68 extern long nrays; /* number of rays traced */
69
70 #define MAXDIV 15 /* maximum sample size */
71
72 #define pixjitter() (.5+dstrpix*(.5-frandom()))
73
74 double pixvalue();
75
76
77 quit(code) /* quit program */
78 int code;
79 {
80 if (code || ralrm > 0) /* report status */
81 report();
82
83 exit(code);
84 }
85
86
87 #ifdef BSD
88 report() /* report progress */
89 {
90 struct rusage rubuf;
91 double t;
92
93 getrusage(RUSAGE_SELF, &rubuf);
94 t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
95 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
96 getrusage(RUSAGE_CHILDREN, &rubuf);
97 t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
98 t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
99
100 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
101 nrays, pctdone, t/3600.0);
102 eputs(errmsg);
103 tlastrept = time((long *)0);
104 }
105 #else
106 report() /* report progress */
107 {
108 signal(SIGALRM, report);
109 tlastrept = time((long *)0);
110 sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
111 nrays, pctdone, (tlastrept-tstart)/3600.0);
112 eputs(errmsg);
113 }
114 #endif
115
116
117 render(zfile, oldfile) /* render the scene */
118 char *zfile, *oldfile;
119 {
120 extern long lseek();
121 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
122 float *zbar[MAXDIV+1]; /* z values */
123 int ypos; /* current scanline */
124 int ystep; /* current y step size */
125 int hstep; /* h step size */
126 int zfd;
127 COLOR *colptr;
128 float *zptr;
129 register int i;
130 /* check sampling */
131 if (psample < 1)
132 psample = 1;
133 else if (psample > MAXDIV) {
134 sprintf(errmsg, "pixel sampling reduced from %d to %d",
135 psample, MAXDIV);
136 error(WARNING, errmsg);
137 psample = MAXDIV;
138 }
139 /* allocate scanlines */
140 for (i = 0; i <= psample; i++) {
141 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
142 if (scanbar[i] == NULL)
143 goto memerr;
144 }
145 /* open z file */
146 if (zfile != NULL) {
147 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
148 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
149 error(SYSTEM, errmsg);
150 }
151 for (i = 0; i <= psample; i++) {
152 zbar[i] = (float *)malloc(hresolu*sizeof(float));
153 if (zbar[i] == NULL)
154 goto memerr;
155 }
156 } else {
157 zfd = -1;
158 for (i = 0; i <= psample; i++)
159 zbar[i] = NULL;
160 }
161 /* write out boundaries */
162 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
163 /* recover file and compute first */
164 i = salvage(oldfile);
165 if (zfd != -1 && i > 0 &&
166 lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
167 error(SYSTEM, "z file seek error in render");
168 pctdone = 100.0*i/vresolu;
169 if (ralrm > 0) /* report init stats */
170 report();
171 #ifndef BSD
172 else
173 #endif
174 signal(SIGALRM, report);
175 ypos = vresolu-1 - i;
176 hstep = (psample*140+49)/99; /* quincunx sampling */
177 ystep = (psample*99+70)/140;
178 fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
179 /* compute scanlines */
180 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
181 /* bottom adjust? */
182 if (ypos < 0) {
183 ystep += ypos;
184 ypos = 0;
185 }
186 colptr = scanbar[ystep]; /* move base to top */
187 scanbar[ystep] = scanbar[0];
188 scanbar[0] = colptr;
189 zptr = zbar[ystep];
190 zbar[ystep] = zbar[0];
191 zbar[0] = zptr;
192 /* fill base line */
193 fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
194 /* fill bar */
195 fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
196 /* write it out */
197 #ifndef BSD
198 signal(SIGALRM, SIG_IGN); /* don't interrupt writes */
199 #endif
200 for (i = ystep; i > 0; i--) {
201 if (zfd != -1 && write(zfd, (char *)zbar[i],
202 hresolu*sizeof(float))
203 < hresolu*sizeof(float))
204 goto writerr;
205 if (fwritescan(scanbar[i], hresolu, stdout) < 0)
206 goto writerr;
207 }
208 if (fflush(stdout) == EOF)
209 goto writerr;
210 /* record progress */
211 pctdone = 100.0*(vresolu-1-ypos)/vresolu;
212 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
213 report();
214 #ifndef BSD
215 else
216 signal(SIGALRM, report);
217 #endif
218 }
219 /* clean up */
220 if (zfd != -1) {
221 if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
222 < hresolu*sizeof(float))
223 goto writerr;
224 if (close(zfd) == -1)
225 goto writerr;
226 for (i = 0; i <= psample; i++)
227 free((char *)zbar[i]);
228 }
229 fwritescan(scanbar[0], hresolu, stdout);
230 if (fflush(stdout) == EOF)
231 goto writerr;
232 for (i = 0; i <= psample; i++)
233 free((char *)scanbar[i]);
234 pctdone = 100.0;
235 return;
236 writerr:
237 error(SYSTEM, "write error in render");
238 memerr:
239 error(SYSTEM, "out of memory in render");
240 }
241
242
243 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
244 register COLOR *scanline;
245 register float *zline;
246 int xres, y, xstep;
247 {
248 static int nc = 0; /* number of calls */
249 int b = xstep;
250 double z;
251 register int i;
252
253 z = pixvalue(scanline[0], 0, y);
254 if (zline) zline[0] = z;
255 /* zig-zag start for quincunx pattern */
256 for (i = nc++ & 1 ? xstep/2 : xstep; i < xres-1+xstep; i += xstep) {
257 if (i >= xres) {
258 xstep += xres-1-i;
259 i = xres-1;
260 }
261 z = pixvalue(scanline[i], i, y);
262 if (zline) zline[i] = z;
263
264 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
265 i-xstep, y, xstep, 0, b/2);
266 }
267 }
268
269
270 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
271 register COLOR *scanbar[];
272 register float *zbar[];
273 int xres, y, ysize;
274 {
275 COLOR vline[MAXDIV+1];
276 float zline[MAXDIV+1];
277 int b = ysize;
278 register int i, j;
279
280 for (i = 0; i < xres; i++) {
281
282 copycolor(vline[0], scanbar[0][i]);
283 copycolor(vline[ysize], scanbar[ysize][i]);
284 if (zbar[0]) {
285 zline[0] = zbar[0][i];
286 zline[ysize] = zbar[ysize][i];
287 }
288
289 b = fillsample(vline, zbar[0] ? zline : NULL,
290 i, y, 0, ysize, b/2);
291
292 for (j = 1; j < ysize; j++)
293 copycolor(scanbar[j][i], vline[j]);
294 if (zbar[0])
295 for (j = 1; j < ysize; j++)
296 zbar[j][i] = zline[j];
297 }
298 }
299
300
301 int
302 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
303 register COLOR *colline;
304 register float *zline;
305 int x, y;
306 int xlen, ylen;
307 int b;
308 {
309 extern double fabs();
310 double ratio;
311 double z;
312 COLOR ctmp;
313 int ncut;
314 register int len;
315
316 if (xlen > 0) /* x or y length is zero */
317 len = xlen;
318 else
319 len = ylen;
320
321 if (len <= 1) /* limit recursion */
322 return(0);
323
324 if (b > 0
325 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
326 || bigdiff(colline[0], colline[len], maxdiff)) {
327
328 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
329 if (zline) zline[len>>1] = z;
330 ncut = 1;
331
332 } else { /* interpolate */
333
334 copycolor(colline[len>>1], colline[len]);
335 ratio = (double)(len>>1) / len;
336 scalecolor(colline[len>>1], ratio);
337 if (zline) zline[len>>1] = zline[len] * ratio;
338 ratio = 1.0 - ratio;
339 copycolor(ctmp, colline[0]);
340 scalecolor(ctmp, ratio);
341 addcolor(colline[len>>1], ctmp);
342 if (zline) zline[len>>1] += zline[0] * ratio;
343 ncut = 0;
344 }
345 /* recurse */
346 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
347
348 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
349 x+(xlen>>1), y+(ylen>>1),
350 xlen-(xlen>>1), ylen-(ylen>>1), b/2);
351
352 return(ncut);
353 }
354
355
356 double
357 pixvalue(col, x, y) /* compute pixel value */
358 COLOR col; /* returned color */
359 int x, y; /* pixel position */
360 {
361 static RAY thisray;
362
363 if (viewray(thisray.rorg, thisray.rdir, &ourview,
364 (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
365 setcolor(col, 0.0, 0.0, 0.0);
366 return(0.0);
367 }
368
369 rayorigin(&thisray, NULL, PRIMARY, 1.0);
370
371 samplendx = pixnumber(x,y,hresolu,vresolu); /* set pixel index */
372
373 rayvalue(&thisray); /* trace ray */
374
375 copycolor(col, thisray.rcol); /* return color */
376
377 return(thisray.rt); /* return distance */
378 }
379
380
381 int
382 salvage(oldfile) /* salvage scanlines from killed program */
383 char *oldfile;
384 {
385 COLR *scanline;
386 FILE *fp;
387 int x, y;
388
389 if (oldfile == NULL)
390 return(0);
391
392 if ((fp = fopen(oldfile, "r")) == NULL) {
393 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
394 error(WARNING, errmsg);
395 return(0);
396 }
397 /* discard header */
398 getheader(fp, NULL);
399 /* get picture size */
400 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
401 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
402 error(WARNING, errmsg);
403 fclose(fp);
404 return(0);
405 }
406
407 if (x != hresolu || y != vresolu) {
408 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
409 oldfile);
410 error(USER, errmsg);
411 }
412
413 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
414 if (scanline == NULL)
415 error(SYSTEM, "out of memory in salvage");
416 for (y = 0; y < vresolu; y++) {
417 if (freadcolrs(scanline, hresolu, fp) < 0)
418 break;
419 if (fwritecolrs(scanline, hresolu, stdout) < 0)
420 goto writerr;
421 }
422 if (fflush(stdout) == EOF)
423 goto writerr;
424 free((char *)scanline);
425 fclose(fp);
426 unlink(oldfile);
427 return(y);
428 writerr:
429 error(SYSTEM, "write error in salvage");
430 }
431
432
433 int
434 pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
435 register int x, y;
436 int xres, yres;
437 {
438 x -= y;
439 while (x < 0)
440 x += xres;
441 return((((x>>2)*yres + y) << 2) + (x & 3));
442 }