ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.10
Committed: Thu Aug 24 11:55:03 1995 UTC (28 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +1 -1 lines
Log Message:
fixed bug in viewloc() for cylindrical sources

File Contents

# Content
1 /* Copyright (c) 1994 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * image.c - routines for image generation.
9 *
10 * 10/17/85
11 */
12
13 #include "standard.h"
14
15 #include "view.h"
16
17 #include "resolu.h"
18
19 #include "paths.h"
20
21 VIEW stdview = STDVIEW; /* default view parameters */
22
23
24 char *
25 setview(v) /* set hvec and vvec, return message on error */
26 register VIEW *v;
27 {
28 static char ill_horiz[] = "illegal horizontal view size";
29 static char ill_vert[] = "illegal vertical view size";
30
31 if (v->vfore < -FTINY || v->vaft < -FTINY ||
32 (v->vaft > FTINY && v->vaft <= v->vfore))
33 return("illegal fore/aft clipping plane");
34
35 if (normalize(v->vdir) == 0.0) /* normalize direction */
36 return("zero view direction");
37
38 if (normalize(v->vup) == 0.0) /* normalize view up */
39 return("zero view up vector");
40
41 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
42
43 if (normalize(v->hvec) == 0.0)
44 return("view up parallel to view direction");
45
46 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
47
48 if (v->horiz <= FTINY)
49 return(ill_horiz);
50 if (v->vert <= FTINY)
51 return(ill_vert);
52
53 switch (v->type) {
54 case VT_PAR: /* parallel view */
55 v->hn2 = v->horiz;
56 v->vn2 = v->vert;
57 break;
58 case VT_PER: /* perspective view */
59 if (v->horiz >= 180.0-FTINY)
60 return(ill_horiz);
61 if (v->vert >= 180.0-FTINY)
62 return(ill_vert);
63 v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
64 v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
65 break;
66 case VT_CYL: /* cylindrical panorama */
67 if (v->horiz > 360.0+FTINY)
68 return(ill_horiz);
69 if (v->vert >= 180.0-FTINY)
70 return(ill_vert);
71 v->hn2 = v->horiz * (PI/180.0);
72 v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
73 break;
74 case VT_ANG: /* angular fisheye */
75 if (v->horiz > 360.0+FTINY)
76 return(ill_horiz);
77 if (v->vert > 360.0+FTINY)
78 return(ill_vert);
79 v->hn2 = v->horiz * (PI/180.0);
80 v->vn2 = v->vert * (PI/180.0);
81 break;
82 case VT_HEM: /* hemispherical fisheye */
83 if (v->horiz > 180.0+FTINY)
84 return(ill_horiz);
85 if (v->vert > 180.0+FTINY)
86 return(ill_vert);
87 v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
88 v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
89 break;
90 default:
91 return("unknown view type");
92 }
93 if (v->type != VT_ANG) {
94 if (v->type != VT_CYL) {
95 v->hvec[0] *= v->hn2;
96 v->hvec[1] *= v->hn2;
97 v->hvec[2] *= v->hn2;
98 }
99 v->vvec[0] *= v->vn2;
100 v->vvec[1] *= v->vn2;
101 v->vvec[2] *= v->vn2;
102 }
103 v->hn2 *= v->hn2;
104 v->vn2 *= v->vn2;
105
106 return(NULL);
107 }
108
109
110 normaspect(va, ap, xp, yp) /* fix pixel aspect or resolution */
111 double va; /* view aspect ratio */
112 double *ap; /* pixel aspect in (or out if 0) */
113 int *xp, *yp; /* x and y resolution in (or out if *ap!=0) */
114 {
115 if (*ap <= FTINY)
116 *ap = va * *xp / *yp; /* compute pixel aspect */
117 else if (va * *xp > *ap * *yp)
118 *xp = *yp / va * *ap + .5; /* reduce x resolution */
119 else
120 *yp = *xp * va / *ap + .5; /* reduce y resolution */
121 }
122
123
124 double
125 viewray(orig, direc, v, x, y) /* compute ray origin and direction */
126 FVECT orig, direc;
127 register VIEW *v;
128 double x, y;
129 {
130 double d, z;
131
132 x += v->hoff - 0.5;
133 y += v->voff - 0.5;
134
135 switch(v->type) {
136 case VT_PAR: /* parallel view */
137 orig[0] = v->vp[0] + v->vfore*v->vdir[0]
138 + x*v->hvec[0] + y*v->vvec[0];
139 orig[1] = v->vp[1] + v->vfore*v->vdir[1]
140 + x*v->hvec[1] + y*v->vvec[1];
141 orig[2] = v->vp[2] + v->vfore*v->vdir[2]
142 + x*v->hvec[2] + y*v->vvec[2];
143 VCOPY(direc, v->vdir);
144 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
145 case VT_PER: /* perspective view */
146 direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
147 direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
148 direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
149 orig[0] = v->vp[0] + v->vfore*direc[0];
150 orig[1] = v->vp[1] + v->vfore*direc[1];
151 orig[2] = v->vp[2] + v->vfore*direc[2];
152 d = normalize(direc);
153 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
154 case VT_HEM: /* hemispherical fisheye */
155 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
156 if (z < 0.0)
157 return(-1.0);
158 z = sqrt(z);
159 direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
160 direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
161 direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
162 orig[0] = v->vp[0] + v->vfore*direc[0];
163 orig[1] = v->vp[1] + v->vfore*direc[1];
164 orig[2] = v->vp[2] + v->vfore*direc[2];
165 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
166 case VT_CYL: /* cylindrical panorama */
167 d = x * v->horiz * (PI/180.0);
168 z = cos(d);
169 x = sin(d);
170 direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
171 direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
172 direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
173 orig[0] = v->vp[0] + v->vfore*direc[0];
174 orig[1] = v->vp[1] + v->vfore*direc[1];
175 orig[2] = v->vp[2] + v->vfore*direc[2];
176 d = normalize(direc);
177 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
178 case VT_ANG: /* angular fisheye */
179 x *= v->horiz/180.0;
180 y *= v->vert/180.0;
181 d = x*x + y*y;
182 if (d > 1.0)
183 return(-1.0);
184 d = sqrt(d);
185 z = cos(PI*d);
186 d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
187 x *= d;
188 y *= d;
189 direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
190 direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
191 direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
192 orig[0] = v->vp[0] + v->vfore*direc[0];
193 orig[1] = v->vp[1] + v->vfore*direc[1];
194 orig[2] = v->vp[2] + v->vfore*direc[2];
195 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
196 }
197 return(-1.0);
198 }
199
200
201 viewloc(ip, v, p) /* find image location for point */
202 FVECT ip;
203 register VIEW *v;
204 FVECT p;
205 {
206 double d;
207 FVECT disp;
208
209 disp[0] = p[0] - v->vp[0];
210 disp[1] = p[1] - v->vp[1];
211 disp[2] = p[2] - v->vp[2];
212
213 switch (v->type) {
214 case VT_PAR: /* parallel view */
215 ip[2] = DOT(disp,v->vdir) - v->vfore;
216 break;
217 case VT_PER: /* perspective view */
218 d = DOT(disp,v->vdir);
219 ip[2] = sqrt(DOT(disp,disp));
220 if (d < 0.0) { /* fold pyramid */
221 ip[2] = -ip[2];
222 d = -d;
223 }
224 if (d > FTINY) {
225 d = 1.0/d;
226 disp[0] *= d;
227 disp[1] *= d;
228 disp[2] *= d;
229 }
230 ip[2] *= (1.0 - v->vfore*d);
231 break;
232 case VT_HEM: /* hemispherical fisheye */
233 d = normalize(disp);
234 if (DOT(disp,v->vdir) < 0.0)
235 ip[2] = -d;
236 else
237 ip[2] = d;
238 ip[2] -= v->vfore;
239 break;
240 case VT_CYL: /* cylindrical panorama */
241 ip[2] = DOT(disp,v->vdir);
242 d = DOT(disp,v->hvec);
243 ip[0] = 180.0/PI * atan2(d,ip[2]) / v->horiz + 0.5 - v->hoff;
244 ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
245 if (v->vfore > FTINY)
246 ip[2] = sqrt(DOT(disp,disp)) *
247 (1.0 - v->vfore/sqrt(d*d + ip[2]*ip[2]));
248 else
249 ip[2] = sqrt(DOT(disp,disp));
250 return;
251 case VT_ANG: /* angular fisheye */
252 ip[0] = 0.5 - v->hoff;
253 ip[1] = 0.5 - v->voff;
254 ip[2] = normalize(disp) - v->vfore;
255 d = DOT(disp,v->vdir);
256 if (d >= 1.0-FTINY)
257 return;
258 if (d <= -(1.0-FTINY)) {
259 ip[0] += 180.0/v->horiz;
260 return;
261 }
262 d = acos(d)/PI / sqrt(1.0 - d*d);
263 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
264 ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
265 return;
266 }
267 ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
268 ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
269 }
270
271
272 pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */
273 FLOAT loc[2];
274 register RESOLU *rp;
275 int px, py;
276 {
277 register int x, y;
278
279 if (rp->or & YMAJOR) {
280 x = px;
281 y = py;
282 } else {
283 x = py;
284 y = px;
285 }
286 if (rp->or & XDECR)
287 x = rp->xr-1 - x;
288 if (rp->or & YDECR)
289 y = rp->yr-1 - y;
290 loc[0] = (x+.5)/rp->xr;
291 loc[1] = (y+.5)/rp->yr;
292 }
293
294
295 loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */
296 int pp[2];
297 register RESOLU *rp;
298 double lx, ly;
299 {
300 register int x, y;
301
302 x = lx * rp->xr;
303 y = ly * rp->yr;
304 if (rp->or & XDECR)
305 x = rp->xr-1 - x;
306 if (rp->or & YDECR)
307 y = rp->yr-1 - y;
308 if (rp->or & YMAJOR) {
309 pp[0] = x;
310 pp[1] = y;
311 } else {
312 pp[0] = y;
313 pp[1] = x;
314 }
315 }
316
317
318 int
319 getviewopt(v, ac, av) /* process view argument */
320 register VIEW *v;
321 int ac;
322 register char *av[];
323 {
324 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
325 badarg(ac-1,av+1,l)) return(-1)
326
327 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
328 return(-1);
329 switch (av[0][2]) {
330 case 't': /* type */
331 if (!av[0][3] || av[0][3]==' ')
332 return(-1);
333 check(4,"");
334 v->type = av[0][3];
335 return(0);
336 case 'p': /* point */
337 check(3,"fff");
338 v->vp[0] = atof(av[1]);
339 v->vp[1] = atof(av[2]);
340 v->vp[2] = atof(av[3]);
341 return(3);
342 case 'd': /* direction */
343 check(3,"fff");
344 v->vdir[0] = atof(av[1]);
345 v->vdir[1] = atof(av[2]);
346 v->vdir[2] = atof(av[3]);
347 return(3);
348 case 'u': /* up */
349 check(3,"fff");
350 v->vup[0] = atof(av[1]);
351 v->vup[1] = atof(av[2]);
352 v->vup[2] = atof(av[3]);
353 return(3);
354 case 'h': /* horizontal size */
355 check(3,"f");
356 v->horiz = atof(av[1]);
357 return(1);
358 case 'v': /* vertical size */
359 check(3,"f");
360 v->vert = atof(av[1]);
361 return(1);
362 case 'o': /* fore clipping plane */
363 check(3,"f");
364 v->vfore = atof(av[1]);
365 return(1);
366 case 'a': /* aft clipping plane */
367 check(3,"f");
368 v->vaft = atof(av[1]);
369 return(1);
370 case 's': /* shift */
371 check(3,"f");
372 v->hoff = atof(av[1]);
373 return(1);
374 case 'l': /* lift */
375 check(3,"f");
376 v->voff = atof(av[1]);
377 return(1);
378 default:
379 return(-1);
380 }
381 #undef check
382 }
383
384
385 int
386 sscanview(vp, s) /* get view parameters from string */
387 VIEW *vp;
388 register char *s;
389 {
390 int ac;
391 char *av[4];
392 int na;
393 int nvopts = 0;
394
395 if (*s != '-')
396 s = sskip(s);
397 while (*s) {
398 ac = 0;
399 do {
400 av[ac++] = s;
401 while (*s && *s != ' ')
402 s++;
403 while (*s == ' ')
404 s++;
405 } while (*s && ac < 4);
406 if ((na = getviewopt(vp, ac, av)) >= 0) {
407 if (na+1 < ac)
408 s = av[na+1];
409 nvopts++;
410 } else if (ac > 1)
411 s = av[1];
412 }
413 return(nvopts);
414 }
415
416
417 fprintview(vp, fp) /* write out view parameters */
418 register VIEW *vp;
419 FILE *fp;
420 {
421 fprintf(fp, " -vt%c", vp->type);
422 fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
423 fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
424 fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
425 fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
426 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
427 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
428 }
429
430
431 int
432 isview(s) /* is this a view string? */
433 char *s;
434 {
435 static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
436 extern char *progname;
437 register char *cp;
438 register char **an;
439 /* add program name to list */
440 if (altname[0] == NULL) {
441 for (cp = progname; *cp; cp++)
442 ;
443 while (cp > progname && !ISDIRSEP(cp[-1]))
444 cp--;
445 altname[0] = cp;
446 }
447 /* skip leading path */
448 cp = s;
449 while (*cp && *cp != ' ')
450 cp++;
451 while (cp > s && !ISDIRSEP(cp[-1]))
452 cp--;
453 for (an = altname; *an != NULL; an++)
454 if (!strncmp(*an, cp, strlen(*an)))
455 return(1);
456 return(0);
457 }
458
459
460 struct myview {
461 VIEW *hv;
462 int ok;
463 };
464
465
466 static
467 gethview(s, v) /* get view from header */
468 char *s;
469 register struct myview *v;
470 {
471 if (isview(s) && sscanview(v->hv, s) > 0)
472 v->ok++;
473 }
474
475
476 int
477 viewfile(fname, vp, rp) /* get view from file */
478 char *fname;
479 VIEW *vp;
480 RESOLU *rp;
481 {
482 struct myview mvs;
483 FILE *fp;
484
485 if ((fp = fopen(fname, "r")) == NULL)
486 return(-1);
487
488 mvs.hv = vp;
489 mvs.ok = 0;
490
491 getheader(fp, gethview, &mvs);
492
493 if (rp != NULL && !fgetsresolu(rp, fp))
494 mvs.ok = 0;
495
496 fclose(fp);
497
498 return(mvs.ok);
499 }