ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond4.c
Revision: 3.3
Committed: Fri Oct 4 16:10:43 1996 UTC (27 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.2: +234 -0 lines
Log Message:
finally got variable acuity working

File Contents

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