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

# User Rev Content
1 greg 2.7 /* Copyright (c) 1994 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 2.7 if (v->vfore < -FTINY || v->vaft < -FTINY ||
32     (v->vaft > FTINY && v->vaft <= v->vfore))
33     return("illegal fore/aft clipping plane");
34    
35 greg 1.1 if (normalize(v->vdir) == 0.0) /* normalize direction */
36     return("zero view direction");
37    
38 greg 1.14 if (normalize(v->vup) == 0.0) /* normalize view up */
39     return("zero view up vector");
40    
41 greg 1.5 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
42    
43     if (normalize(v->hvec) == 0.0)
44 greg 1.14 return("view up parallel to view direction");
45 greg 1.1
46 greg 1.5 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
47    
48 greg 1.12 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 greg 1.5 v->hn2 = v->horiz;
56 greg 1.12 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 greg 1.5 v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
64 greg 1.12 v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
65     break;
66 greg 2.9 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 greg 1.12 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 greg 2.9 v->hn2 = v->horiz * (PI/180.0);
80     v->vn2 = v->vert * (PI/180.0);
81 greg 1.12 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 greg 1.1 return("unknown view type");
92 greg 1.12 }
93 greg 1.13 if (v->type != VT_ANG) {
94 greg 2.9 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 greg 1.12 v->vvec[0] *= v->vn2;
100     v->vvec[1] *= v->vn2;
101     v->vvec[2] *= v->vn2;
102     }
103 greg 1.5 v->hn2 *= v->hn2;
104     v->vn2 *= v->vn2;
105 greg 1.1
106     return(NULL);
107     }
108    
109    
110 greg 1.7 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 greg 1.6 {
115     if (*ap <= FTINY)
116 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
117 greg 1.6 else if (va * *xp > *ap * *yp)
118 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
119 greg 1.6 else
120 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
121 greg 1.6 }
122    
123    
124 greg 2.7 double
125 greg 1.5 viewray(orig, direc, v, x, y) /* compute ray origin and direction */
126 greg 1.1 FVECT orig, direc;
127     register VIEW *v;
128     double x, y;
129     {
130 greg 1.12 double d, z;
131    
132 greg 1.5 x += v->hoff - 0.5;
133     y += v->voff - 0.5;
134 greg 1.1
135 greg 1.12 switch(v->type) {
136     case VT_PAR: /* parallel view */
137 greg 2.7 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 greg 1.1 VCOPY(direc, v->vdir);
144 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
145 greg 1.12 case VT_PER: /* perspective view */
146 greg 1.5 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 greg 2.7 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 greg 2.8 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
154 greg 1.12 case VT_HEM: /* hemispherical fisheye */
155 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
156 greg 1.12 if (z < 0.0)
157 greg 2.7 return(-1.0);
158 greg 1.12 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 greg 2.7 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 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
166 greg 2.9 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 greg 1.12 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 greg 2.7 return(-1.0);
184 greg 1.12 d = sqrt(d);
185     z = cos(PI*d);
186 greg 2.6 d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
187 greg 1.12 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 greg 2.7 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 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
196 greg 1.1 }
197 greg 2.7 return(-1.0);
198 greg 1.1 }
199    
200    
201 greg 1.17 viewloc(ip, v, p) /* find image location for point */
202     FVECT ip;
203 greg 1.3 register VIEW *v;
204     FVECT p;
205     {
206     double d;
207     FVECT disp;
208 greg 1.5
209 greg 1.3 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 greg 1.12 switch (v->type) {
214     case VT_PAR: /* parallel view */
215 greg 2.7 ip[2] = DOT(disp,v->vdir) - v->vfore;
216 greg 1.13 break;
217 greg 1.12 case VT_PER: /* perspective view */
218 greg 1.11 d = DOT(disp,v->vdir);
219 greg 1.17 ip[2] = sqrt(DOT(disp,disp));
220     if (d < 0.0) { /* fold pyramid */
221     ip[2] = -ip[2];
222 greg 1.11 d = -d;
223 greg 2.7 }
224     if (d > FTINY) {
225 greg 1.11 d = 1.0/d;
226     disp[0] *= d;
227     disp[1] *= d;
228     disp[2] *= d;
229     }
230 greg 2.7 ip[2] *= (1.0 - v->vfore*d);
231 greg 1.13 break;
232 greg 1.12 case VT_HEM: /* hemispherical fisheye */
233     d = normalize(disp);
234 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
235     ip[2] = -d;
236     else
237     ip[2] = d;
238 greg 2.7 ip[2] -= v->vfore;
239 greg 1.13 break;
240 greg 2.9 case VT_CYL: /* cylindrical panorama */
241     ip[2] = DOT(disp,v->vdir);
242     d = DOT(disp,v->hvec);
243 greg 2.10 ip[0] = 180.0/PI * atan2(d,ip[2]) / v->horiz + 0.5 - v->hoff;
244 greg 2.9 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 greg 1.12 case VT_ANG: /* angular fisheye */
252 greg 1.17 ip[0] = 0.5 - v->hoff;
253     ip[1] = 0.5 - v->voff;
254 greg 2.7 ip[2] = normalize(disp) - v->vfore;
255 greg 1.12 d = DOT(disp,v->vdir);
256     if (d >= 1.0-FTINY)
257     return;
258     if (d <= -(1.0-FTINY)) {
259 greg 2.7 ip[0] += 180.0/v->horiz;
260 greg 1.12 return;
261     }
262     d = acos(d)/PI / sqrt(1.0 - d*d);
263 greg 1.17 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
264     ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
265 greg 1.12 return;
266 greg 1.3 }
267 greg 1.17 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 greg 1.3 }
270    
271    
272 greg 1.17 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 greg 1.5 int
319     getviewopt(v, ac, av) /* process view argument */
320     register VIEW *v;
321     int ac;
322     register char *av[];
323     {
324 greg 1.16 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
325     badarg(ac-1,av+1,l)) return(-1)
326 greg 1.5
327 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
328 greg 1.5 return(-1);
329     switch (av[0][2]) {
330     case 't': /* type */
331     if (!av[0][3] || av[0][3]==' ')
332     return(-1);
333 greg 1.16 check(4,"");
334 greg 1.5 v->type = av[0][3];
335     return(0);
336     case 'p': /* point */
337 greg 1.16 check(3,"fff");
338 greg 1.5 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 greg 1.16 check(3,"fff");
344 greg 1.5 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 greg 1.16 check(3,"fff");
350 greg 1.5 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 greg 1.16 check(3,"f");
356 greg 1.5 v->horiz = atof(av[1]);
357     return(1);
358     case 'v': /* vertical size */
359 greg 1.16 check(3,"f");
360 greg 1.5 v->vert = atof(av[1]);
361     return(1);
362 greg 2.7 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 greg 1.5 case 's': /* shift */
371 greg 1.16 check(3,"f");
372 greg 1.5 v->hoff = atof(av[1]);
373     return(1);
374     case 'l': /* lift */
375 greg 1.16 check(3,"f");
376 greg 1.5 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 greg 1.1 register char *s;
389     {
390 greg 1.5 int ac;
391     char *av[4];
392     int na;
393     int nvopts = 0;
394    
395 greg 2.3 if (*s != '-')
396     s = sskip(s);
397 greg 1.9 while (*s) {
398 greg 1.5 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 greg 1.9 } else if (ac > 1)
411     s = av[1];
412     }
413 greg 1.5 return(nvopts);
414 greg 1.1 }
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 greg 2.7 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
427 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
428 greg 1.1 }
429    
430    
431 greg 2.3 int
432     isview(s) /* is this a view string? */
433     char *s;
434     {
435 greg 2.5 static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
436     extern char *progname;
437 greg 2.3 register char *cp;
438     register char **an;
439     /* add program name to list */
440 greg 2.5 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 greg 2.3 /* skip leading path */
448     cp = s;
449     while (*cp && *cp != ' ')
450     cp++;
451 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
452 greg 2.3 cp--;
453     for (an = altname; *an != NULL; an++)
454     if (!strncmp(*an, cp, strlen(*an)))
455     return(1);
456     return(0);
457     }
458 greg 1.1
459 greg 2.3
460 greg 1.15 struct myview {
461     VIEW *hv;
462     int ok;
463     };
464 greg 1.1
465 greg 1.15
466 greg 1.1 static
467 greg 1.15 gethview(s, v) /* get view from header */
468 greg 1.1 char *s;
469 greg 1.15 register struct myview *v;
470 greg 1.1 {
471 greg 2.3 if (isview(s) && sscanview(v->hv, s) > 0)
472     v->ok++;
473 greg 1.1 }
474    
475    
476     int
477 greg 1.17 viewfile(fname, vp, rp) /* get view from file */
478 greg 1.1 char *fname;
479     VIEW *vp;
480 greg 1.17 RESOLU *rp;
481 greg 1.1 {
482 greg 1.15 struct myview mvs;
483 greg 1.1 FILE *fp;
484    
485     if ((fp = fopen(fname, "r")) == NULL)
486     return(-1);
487    
488 greg 1.15 mvs.hv = vp;
489     mvs.ok = 0;
490 greg 1.1
491 greg 1.15 getheader(fp, gethview, &mvs);
492 greg 1.8
493 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
494 greg 1.15 mvs.ok = 0;
495 greg 1.1
496     fclose(fp);
497    
498 greg 1.15 return(mvs.ok);
499 greg 1.1 }