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

# User Rev Content
1 greg 2.4 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
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 greg 1.17 #include "resolu.h"
18    
19 greg 2.4 #include "paths.h"
20    
21 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
22 greg 1.1
23    
24     char *
25 greg 1.5 setview(v) /* set hvec and vvec, return message on error */
26     register VIEW *v;
27 greg 1.3 {
28 greg 1.12 static char ill_horiz[] = "illegal horizontal view size";
29     static char ill_vert[] = "illegal vertical view size";
30    
31 greg 1.1 if (normalize(v->vdir) == 0.0) /* normalize direction */
32     return("zero view direction");
33    
34 greg 1.14 if (normalize(v->vup) == 0.0) /* normalize view up */
35     return("zero view up vector");
36    
37 greg 1.5 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
38    
39     if (normalize(v->hvec) == 0.0)
40 greg 1.14 return("view up parallel to view direction");
41 greg 1.1
42 greg 1.5 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
43    
44 greg 1.12 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 greg 1.5 v->hn2 = v->horiz;
52 greg 1.12 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 greg 1.5 v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
60 greg 1.12 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 greg 1.1 return("unknown view type");
80 greg 1.12 }
81 greg 1.13 if (v->type != VT_ANG) {
82 greg 1.12 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 greg 1.5 v->hn2 *= v->hn2;
90     v->vn2 *= v->vn2;
91 greg 1.1
92     return(NULL);
93     }
94    
95    
96 greg 1.7 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 greg 1.6 {
101     if (*ap <= FTINY)
102 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
103 greg 1.6 else if (va * *xp > *ap * *yp)
104 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
105 greg 1.6 else
106 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
107 greg 1.6 }
108    
109    
110 greg 1.5 viewray(orig, direc, v, x, y) /* compute ray origin and direction */
111 greg 1.1 FVECT orig, direc;
112     register VIEW *v;
113     double x, y;
114     {
115 greg 1.12 double d, z;
116    
117 greg 1.5 x += v->hoff - 0.5;
118     y += v->voff - 0.5;
119 greg 1.1
120 greg 1.12 switch(v->type) {
121     case VT_PAR: /* parallel view */
122 greg 1.5 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 greg 1.1 VCOPY(direc, v->vdir);
126 greg 1.12 return(0);
127     case VT_PER: /* perspective view */
128 greg 1.1 VCOPY(orig, v->vp);
129 greg 1.5 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 greg 1.1 normalize(direc);
133 greg 1.12 return(0);
134     case VT_HEM: /* hemispherical fisheye */
135 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
136 greg 1.12 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 greg 2.6 d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
154 greg 1.12 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 greg 1.1 }
161 greg 1.12 return(-1);
162 greg 1.1 }
163    
164    
165 greg 1.17 viewloc(ip, v, p) /* find image location for point */
166     FVECT ip;
167 greg 1.3 register VIEW *v;
168     FVECT p;
169     {
170     double d;
171     FVECT disp;
172 greg 1.5
173 greg 1.3 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 greg 1.12 switch (v->type) {
178     case VT_PAR: /* parallel view */
179 greg 1.17 ip[2] = DOT(disp,v->vdir);
180 greg 1.13 break;
181 greg 1.12 case VT_PER: /* perspective view */
182 greg 1.11 d = DOT(disp,v->vdir);
183 greg 1.17 ip[2] = sqrt(DOT(disp,disp));
184     if (d < 0.0) { /* fold pyramid */
185     ip[2] = -ip[2];
186 greg 1.11 d = -d;
187 greg 1.17 } else if (d > FTINY) {
188 greg 1.11 d = 1.0/d;
189     disp[0] *= d;
190     disp[1] *= d;
191     disp[2] *= d;
192     }
193 greg 1.13 break;
194 greg 1.12 case VT_HEM: /* hemispherical fisheye */
195     d = normalize(disp);
196 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
197     ip[2] = -d;
198     else
199     ip[2] = d;
200 greg 1.13 break;
201 greg 1.12 case VT_ANG: /* angular fisheye */
202 greg 1.17 ip[0] = 0.5 - v->hoff;
203     ip[1] = 0.5 - v->voff;
204     ip[2] = normalize(disp);
205 greg 1.12 d = DOT(disp,v->vdir);
206     if (d >= 1.0-FTINY)
207     return;
208     if (d <= -(1.0-FTINY)) {
209 greg 1.17 ip[1] += 180.0/v->horiz;
210     ip[2] += 180.0/v->vert;
211 greg 1.12 return;
212     }
213     d = acos(d)/PI / sqrt(1.0 - d*d);
214 greg 1.17 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
215     ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
216 greg 1.12 return;
217 greg 1.3 }
218 greg 1.17 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 greg 1.3 }
221    
222    
223 greg 1.17 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 greg 1.5 int
270     getviewopt(v, ac, av) /* process view argument */
271     register VIEW *v;
272     int ac;
273     register char *av[];
274     {
275 greg 1.16 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
276     badarg(ac-1,av+1,l)) return(-1)
277 greg 1.5
278 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
279 greg 1.5 return(-1);
280     switch (av[0][2]) {
281     case 't': /* type */
282     if (!av[0][3] || av[0][3]==' ')
283     return(-1);
284 greg 1.16 check(4,"");
285 greg 1.5 v->type = av[0][3];
286     return(0);
287     case 'p': /* point */
288 greg 1.16 check(3,"fff");
289 greg 1.5 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 greg 1.16 check(3,"fff");
295 greg 1.5 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 greg 1.16 check(3,"fff");
301 greg 1.5 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 greg 1.16 check(3,"f");
307 greg 1.5 v->horiz = atof(av[1]);
308     return(1);
309     case 'v': /* vertical size */
310 greg 1.16 check(3,"f");
311 greg 1.5 v->vert = atof(av[1]);
312     return(1);
313     case 's': /* shift */
314 greg 1.16 check(3,"f");
315 greg 1.5 v->hoff = atof(av[1]);
316     return(1);
317     case 'l': /* lift */
318 greg 1.16 check(3,"f");
319 greg 1.5 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 greg 1.1 register char *s;
332     {
333 greg 1.5 int ac;
334     char *av[4];
335     int na;
336     int nvopts = 0;
337    
338 greg 2.3 if (*s != '-')
339     s = sskip(s);
340 greg 1.9 while (*s) {
341 greg 1.5 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 greg 1.9 } else if (ac > 1)
354     s = av[1];
355     }
356 greg 1.5 return(nvopts);
357 greg 1.1 }
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 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
370 greg 1.1 }
371    
372    
373 greg 2.3 int
374     isview(s) /* is this a view string? */
375     char *s;
376     {
377 greg 2.5 static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
378     extern char *progname;
379 greg 2.3 register char *cp;
380     register char **an;
381     /* add program name to list */
382 greg 2.5 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 greg 2.3 /* skip leading path */
390     cp = s;
391     while (*cp && *cp != ' ')
392     cp++;
393 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
394 greg 2.3 cp--;
395     for (an = altname; *an != NULL; an++)
396     if (!strncmp(*an, cp, strlen(*an)))
397     return(1);
398     return(0);
399     }
400 greg 1.1
401 greg 2.3
402 greg 1.15 struct myview {
403     VIEW *hv;
404     int ok;
405     };
406 greg 1.1
407 greg 1.15
408 greg 1.1 static
409 greg 1.15 gethview(s, v) /* get view from header */
410 greg 1.1 char *s;
411 greg 1.15 register struct myview *v;
412 greg 1.1 {
413 greg 2.3 if (isview(s) && sscanview(v->hv, s) > 0)
414     v->ok++;
415 greg 1.1 }
416    
417    
418     int
419 greg 1.17 viewfile(fname, vp, rp) /* get view from file */
420 greg 1.1 char *fname;
421     VIEW *vp;
422 greg 1.17 RESOLU *rp;
423 greg 1.1 {
424 greg 1.15 struct myview mvs;
425 greg 1.1 FILE *fp;
426    
427     if ((fp = fopen(fname, "r")) == NULL)
428     return(-1);
429    
430 greg 1.15 mvs.hv = vp;
431     mvs.ok = 0;
432 greg 1.1
433 greg 1.15 getheader(fp, gethview, &mvs);
434 greg 1.8
435 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
436 greg 1.15 mvs.ok = 0;
437 greg 1.1
438     fclose(fp);
439    
440 greg 1.15 return(mvs.ok);
441 greg 1.1 }