ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.15
Committed: Fri Jan 12 09:27:11 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +23 -35 lines
Log Message:
simplified residual calculation

File Contents

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