ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond4.c
Revision: 3.15
Committed: Wed Aug 13 11:24:48 1997 UTC (26 years, 8 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.14: +2 -1 lines
Log Message:
fixed bug in veil computation for fisheye views (divide by zero)

File Contents

# Content
1 /* Copyright (c) 1997 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines for veiling glare and loss of acuity.
9 */
10
11 #include "pcond.h"
12
13 /************** VEILING STUFF *****************/
14
15 #define VADAPT 0.08 /* fraction of adaptation from veil */
16
17 static COLOR *veilimg = NULL; /* veiling image */
18
19 #define veilscan(y) (veilimg+(y)*fvxr)
20
21 static float (*raydir)[3] = NULL; /* ray direction for each pixel */
22
23 #define rdirscan(y) (raydir+(y)*fvxr)
24
25
26 compraydir() /* compute ray directions */
27 {
28 FVECT rorg, rdir;
29 double h, v;
30 register int x, y;
31
32 if (raydir != NULL) /* already done? */
33 return;
34 raydir = (float (*)[3])malloc(fvxr*fvyr*3*sizeof(float));
35 if (raydir == NULL)
36 syserror("malloc");
37
38 for (y = 0; y < fvyr; y++) {
39 switch (inpres.or) {
40 case YMAJOR: case YMAJOR|XDECR:
41 v = (y+.5)/fvyr; break;
42 case YMAJOR|YDECR: case YMAJOR|YDECR|XDECR:
43 v = 1. - (y+.5)/fvyr; break;
44 case 0: case YDECR:
45 h = (y+.5)/fvyr; break;
46 case XDECR: case XDECR|YDECR:
47 h = 1. - (y+.5)/fvyr; break;
48 }
49 for (x = 0; x < fvxr; x++) {
50 switch (inpres.or) {
51 case YMAJOR: case YMAJOR|YDECR:
52 h = (x+.5)/fvxr; break;
53 case YMAJOR|XDECR: case YMAJOR|XDECR|YDECR:
54 h = 1. - (x+.5)/fvxr; break;
55 case 0: case XDECR:
56 v = (x+.5)/fvxr; break;
57 case YDECR: case YDECR|XDECR:
58 v = 1. - (x+.5)/fvxr; break;
59 }
60 if (viewray(rorg, rdir, &ourview, h, v)
61 >= -FTINY) {
62 rdirscan(y)[x][0] = rdir[0];
63 rdirscan(y)[x][1] = rdir[1];
64 rdirscan(y)[x][2] = rdir[2];
65 } else {
66 rdirscan(y)[x][0] =
67 rdirscan(y)[x][1] =
68 rdirscan(y)[x][2] = 0.0;
69 }
70 }
71 }
72 }
73
74
75 compveil() /* compute veiling image */
76 {
77 double t2, t2sum;
78 COLOR ctmp, vsum;
79 int px, py;
80 register int x, y;
81
82 if (veilimg != NULL) /* already done? */
83 return;
84 /* compute ray directions */
85 compraydir();
86 /* compute veil image */
87 veilimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR));
88 if (veilimg == NULL)
89 syserror("malloc");
90 for (py = 0; py < fvyr; py++)
91 for (px = 0; px < fvxr; px++) {
92 t2sum = 0.;
93 setcolor(vsum, 0., 0., 0.);
94 for (y = 0; y < fvyr; y++)
95 for (x = 0; x < fvxr; x++) {
96 if (x == px && y == py) continue;
97 t2 = DOT(rdirscan(py)[px],
98 rdirscan(y)[x]);
99 if (t2 <= FTINY) continue;
100 /* use approximation instead
101 t3 = acos(t2);
102 t2 = t2/(t3*t3);
103 */
104 t2 *= .5 / (1. - t2);
105 copycolor(ctmp, fovscan(y)[x]);
106 scalecolor(ctmp, t2);
107 addcolor(vsum, ctmp);
108 t2sum += t2;
109 }
110 /* VADAPT of original is subtracted in addveil() */
111 if (t2sum > FTINY)
112 scalecolor(vsum, VADAPT/t2sum);
113 copycolor(veilscan(py)[px], vsum);
114 }
115 /* modify FOV sample image */
116 for (y = 0; y < fvyr; y++)
117 for (x = 0; x < fvxr; x++) {
118 scalecolor(fovscan(y)[x], 1.-VADAPT);
119 addcolor(fovscan(y)[x], veilscan(y)[x]);
120 }
121 comphist(); /* recompute histogram */
122 }
123
124
125 addveil(sl, y) /* add veil to scanline */
126 COLOR *sl;
127 int y;
128 {
129 int vx, vy;
130 double dx, dy;
131 double lv, uv;
132 register int x, i;
133
134 vy = dy = (y+.5)/numscans(&inpres)*fvyr - .5;
135 while (vy >= fvyr-1) vy--;
136 dy -= (double)vy;
137 for (x = 0; x < scanlen(&inpres); x++) {
138 vx = dx = (x+.5)/scanlen(&inpres)*fvxr - .5;
139 while (vx >= fvxr-1) vx--;
140 dx -= (double)vx;
141 for (i = 0; i < 3; i++) {
142 lv = (1.-dy)*colval(veilscan(vy)[vx],i) +
143 dy*colval(veilscan(vy+1)[vx],i);
144 uv = (1.-dy)*colval(veilscan(vy)[vx+1],i) +
145 dy*colval(veilscan(vy+1)[vx+1],i);
146 colval(sl[x],i) = (1.-VADAPT)*colval(sl[x],i) +
147 (1.-dx)*lv + dx*uv;
148 }
149 }
150 }
151
152
153 /****************** ACUITY STUFF *******************/
154
155 typedef struct {
156 short sampe; /* sample area size (exponent of 2) */
157 short nscans; /* number of scanlines in this bar */
158 int len; /* individual scanline length */
159 int nread; /* number of scanlines loaded */
160 COLOR *sdata; /* scanbar data */
161 } SCANBAR;
162
163 #define bscan(sb,y) ((COLOR *)(sb)->sdata+((y)%(sb)->nscans)*(sb)->len)
164
165 SCANBAR *rootbar; /* root scan bar (lowest resolution) */
166
167 float *inpacuD; /* input acuity data (cycles/degree) */
168
169 #define tsampr(x,y) inpacuD[(y)*fvxr+(x)]
170
171
172 double
173 hacuity(La) /* return visual acuity in cycles/degree */
174 double La;
175 {
176 /* functional fit */
177 return(17.25*atan(1.4*log10(La) + 0.35) + 25.72);
178 }
179
180
181 COLOR *
182 getascan(sb, y) /* find/read scanline y for scanbar sb */
183 register SCANBAR *sb;
184 int y;
185 {
186 register COLOR *sl0, *sl1, *mysl;
187 register int i;
188
189 if (y < sb->nread - sb->nscans) /* too far back? */
190 return(NULL);
191 for ( ; y >= sb->nread; sb->nread++) { /* read as necessary */
192 mysl = bscan(sb, sb->nread);
193 if (sb->sampe == 0) {
194 if (freadscan(mysl, sb->len, infp) < 0) {
195 fprintf(stderr, "%s: %s: scanline read error\n",
196 progname, infn);
197 exit(1);
198 }
199 } else {
200 sl0 = getascan(sb+1, 2*y);
201 if (sl0 == NULL)
202 return(NULL);
203 sl1 = getascan(sb+1, 2*y+1);
204 for (i = 0; i < sb->len; i++) {
205 copycolor(mysl[i], sl0[2*i]);
206 addcolor(mysl[i], sl0[2*i+1]);
207 addcolor(mysl[i], sl1[2*i]);
208 addcolor(mysl[i], sl1[2*i+1]);
209 scalecolor(mysl[i], 0.25);
210 }
211 }
212 }
213 return(bscan(sb, y));
214 }
215
216
217 acuscan(scln, y) /* get acuity-sampled scanline */
218 COLOR *scln;
219 int y;
220 {
221 double sr;
222 double dx, dy;
223 int ix, iy;
224 register int x;
225 /* compute foveal y position */
226 iy = dy = (y+.5)/numscans(&inpres)*fvyr - .5;
227 while (iy >= fvyr-1) iy--;
228 dy -= (double)iy;
229 for (x = 0; x < scanlen(&inpres); x++) {
230 /* compute foveal x position */
231 ix = dx = (x+.5)/scanlen(&inpres)*fvxr - .5;
232 while (ix >= fvxr-1) ix--;
233 dx -= (double)ix;
234 /* interpolate sample rate */
235 sr = (1.-dy)*((1.-dx)*tsampr(ix,iy) + dx*tsampr(ix+1,iy)) +
236 dy*((1.-dx)*tsampr(ix,iy+1) + dx*tsampr(ix+1,iy+1));
237
238 acusample(scln[x], x, y, sr); /* compute sample */
239 }
240 }
241
242
243 acusample(col, x, y, sr) /* interpolate sample at (x,y) using rate sr */
244 COLOR col;
245 int x, y;
246 double sr;
247 {
248 COLOR c1;
249 double d;
250 register SCANBAR *sb0;
251
252 for (sb0 = rootbar; sb0->sampe != 0 && 1<<sb0[1].sampe > sr; sb0++)
253 ;
254 ascanval(col, x, y, sb0);
255 if (sb0->sampe == 0) /* don't extrapolate highest */
256 return;
257 ascanval(c1, x, y, sb0+1);
258 d = ((1<<sb0->sampe) - sr)/(1<<sb0[1].sampe);
259 scalecolor(col, 1.-d);
260 scalecolor(c1, d);
261 addcolor(col, c1);
262 }
263
264
265 ascanval(col, x, y, sb) /* interpolate scanbar at orig. coords (x,y) */
266 COLOR col;
267 int x, y;
268 SCANBAR *sb;
269 {
270 COLOR *sl0, *sl1, c1, c1y;
271 double dx, dy;
272 int ix, iy;
273
274 if (sb->sampe == 0) { /* no need to interpolate */
275 sl0 = getascan(sb, y);
276 copycolor(col, sl0[x]);
277 return;
278 }
279 /* compute coordinates for sb */
280 ix = dx = (x+.5)/(1<<sb->sampe) - .5;
281 while (ix >= sb->len-1) ix--;
282 dx -= (double)ix;
283 iy = dy = (y+.5)/(1<<sb->sampe) - .5;
284 while (iy >= (numscans(&inpres)>>sb->sampe)-1) iy--;
285 dy -= (double)iy;
286 /* get scanlines */
287 sl0 = getascan(sb, iy);
288 #ifdef DEBUG
289 if (sl0 == NULL)
290 error(INTERNAL, "cannot backspace in ascanval");
291 #endif
292 sl1 = getascan(sb, iy+1);
293 /* 2D linear interpolation */
294 copycolor(col, sl0[ix]);
295 scalecolor(col, 1.-dx);
296 copycolor(c1, sl0[ix+1]);
297 scalecolor(c1, dx);
298 addcolor(col, c1);
299 copycolor(c1y, sl1[ix]);
300 scalecolor(c1y, 1.-dx);
301 copycolor(c1, sl1[ix+1]);
302 scalecolor(c1, dx);
303 addcolor(c1y, c1);
304 scalecolor(col, 1.-dy);
305 scalecolor(c1y, dy);
306 addcolor(col, c1y);
307 for (ix = 0; ix < 3; ix++) /* make sure no negative */
308 if (colval(col,ix) < 0.)
309 colval(col,ix) = 0.;
310 }
311
312
313 SCANBAR *
314 sballoc(se, ns, sl) /* allocate scanbar */
315 int se; /* sampling rate exponent */
316 int ns; /* number of scanlines */
317 int sl; /* original scanline length */
318 {
319 SCANBAR *sbarr;
320 register SCANBAR *sb;
321
322 sbarr = sb = (SCANBAR *)malloc((se+1)*sizeof(SCANBAR));
323 if (sb == NULL)
324 syserror("malloc");
325 do {
326 sb->sampe = se;
327 sb->len = sl>>se;
328 sb->nscans = ns;
329 sb->sdata = (COLOR *)malloc(sb->len*ns*sizeof(COLOR));
330 if (sb->sdata == NULL)
331 syserror("malloc");
332 sb->nread = 0;
333 ns <<= 1;
334 sb++;
335 } while (--se >= 0);
336 return(sbarr);
337 }
338
339
340 initacuity() /* initialize variable acuity sampling */
341 {
342 FVECT diffx, diffy, cp;
343 double omega, maxsr;
344 register int x, y, i;
345
346 compraydir(); /* compute ray directions */
347
348 inpacuD = (float *)malloc(fvxr*fvyr*sizeof(float));
349 if (inpacuD == NULL)
350 syserror("malloc");
351 maxsr = 1.; /* compute internal sample rates */
352 for (y = 1; y < fvyr-1; y++)
353 for (x = 1; x < fvxr-1; x++) {
354 for (i = 0; i < 3; i++) {
355 diffx[i] = 0.5*fvxr/scanlen(&inpres) *
356 (rdirscan(y)[x+1][i] -
357 rdirscan(y)[x-1][i]);
358 diffy[i] = 0.5*fvyr/numscans(&inpres) *
359 (rdirscan(y+1)[x][i] -
360 rdirscan(y-1)[x][i]);
361 }
362 fcross(cp, diffx, diffy);
363 omega = 0.5 * sqrt(DOT(cp,cp));
364 if (omega <= FTINY*FTINY)
365 tsampr(x,y) = 1.;
366 else if ((tsampr(x,y) = PI/180. / sqrt(omega) /
367 hacuity(plum(fovscan(y)[x]))) > maxsr)
368 maxsr = tsampr(x,y);
369 }
370 /* copy perimeter (easier) */
371 for (x = 1; x < fvxr-1; x++) {
372 tsampr(x,0) = tsampr(x,1);
373 tsampr(x,fvyr-1) = tsampr(x,fvyr-2);
374 }
375 for (y = 0; y < fvyr; y++) {
376 tsampr(0,y) = tsampr(1,y);
377 tsampr(fvxr-1,y) = tsampr(fvxr-2,y);
378 }
379 /* initialize with next power of two */
380 rootbar = sballoc((int)(log(maxsr)/log(2.))+1, 2, scanlen(&inpres));
381 }