ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.6
Committed: Sat Dec 5 16:23:37 1992 UTC (31 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -5 lines
Log Message:
fixed bug in -vta type with -DSMLFLT option in viewray()

File Contents

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