ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.7
Committed: Tue Dec 20 20:15:04 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +46 -20 lines
Log Message:
added -vo and -va (fore and aft clipping plane) parameters

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