ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
(Generate patch)

Comparing ray/src/common/image.c (file contents):
Revision 1.9 by greg, Tue Jan 16 16:21:07 1990 UTC vs.
Revision 2.10 by greg, Thu Aug 24 11:55:03 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1994 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
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  
# Line 21 | Line 25 | char *
25   setview(v)              /* set hvec and vvec, return message on error */
26   register VIEW  *v;
27   {
28 <        extern double  tan(), normalize();
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("illegal view up vector");
44 >                return("view up parallel to view direction");
45  
46          fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */
47  
48 <        if (v->type == VT_PAR)
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 <        else if (v->type == VT_PER)
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 <        else
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->hn2 <= FTINY || v->hn2 >= FHUGE)
94 <                return("illegal horizontal view size");
95 <
96 <        v->hvec[0] *= v->hn2;
97 <        v->hvec[1] *= v->hn2;
98 <        v->hvec[2] *= v->hn2;
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;
50
51        if (v->type == VT_PAR)
52                v->vn2 = v->vert;
53        else
54                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
55
56        if (v->vn2 <= FTINY || v->vn2 >= FHUGE)
57                return("illegal vertical view size");
58
59        v->vvec[0] *= v->vn2;
60        v->vvec[1] *= v->vn2;
61        v->vvec[2] *= v->vn2;
104          v->vn2 *= v->vn2;
105  
106          return(NULL);
# Line 73 | Line 115 | int  *xp, *yp;                 /* x and y resolution in (or out if *
115          if (*ap <= FTINY)
116                  *ap = va * *xp / *yp;           /* compute pixel aspect */
117          else if (va * *xp > *ap * *yp)
118 <                *xp = *yp / va * *ap;           /* reduce x resolution */
118 >                *xp = *yp / va * *ap + .5;      /* reduce x resolution */
119          else
120 <                *yp = *xp * va / *ap;           /* reduce y resolution */
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 <        if (v->type == VT_PAR) {        /* parallel view */
136 <                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
137 <                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
138 <                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
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 <        } else {                        /* perspective view */
145 <                VCOPY(orig, v->vp);
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 <                normalize(direc);
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 < viewpixel(xp, yp, zp, v, p)             /* find image location for point */
202 < double  *xp, *yp, *zp;
201 > viewloc(ip, v, p)               /* find image location for point */
202 > FVECT  ip;
203   register VIEW  *v;
204   FVECT  p;
205   {
110        extern double  sqrt();
206          double  d;
207          FVECT  disp;
208  
# Line 115 | Line 210 | FVECT  p;
210          disp[1] = p[1] - v->vp[1];
211          disp[2] = p[2] - v->vp[2];
212  
213 <        if (v->type == VT_PAR) {        /* parallel view */
214 <                if (zp != NULL)
215 <                        *zp = DOT(disp,v->vdir);
216 <        } else {                        /* perspective view */
217 <                d = 1.0/DOT(disp,v->vdir);
218 <                if (zp != NULL) {
219 <                        *zp = sqrt(DOT(disp,disp));
220 <                        if (d < 0.0)
221 <                                *zp = -*zp;
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 <                disp[0] *= d;
225 <                disp[1] *= d;
226 <                disp[2] *= d;
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 <        *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
268 <        *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
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,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1)
325 <        extern double  atof();
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);
# Line 149 | Line 330 | register char  *av[];
330          case 't':                       /* type */
331                  if (!av[0][3] || av[0][3]==' ')
332                          return(-1);
333 <                check(4,0);
333 >                check(4,"");
334                  v->type = av[0][3];
335                  return(0);
336          case 'p':                       /* point */
337 <                check(3,3);
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,3);
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,3);
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,1);
355 >                check(3,"f");
356                  v->horiz = atof(av[1]);
357                  return(1);
358          case 'v':                       /* vertical size */
359 <                check(3,1);
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,1);
371 >                check(3,"f");
372                  v->hoff = atof(av[1]);
373                  return(1);
374          case 'l':                       /* lift */
375 <                check(3,1);
375 >                check(3,"f");
376                  v->voff = atof(av[1]);
377                  return(1);
378          default:
# Line 203 | Line 392 | register char  *s;
392          int  na;
393          int  nvopts = 0;
394  
395 <        while (*s == ' ')
396 <                s++;
395 >        if (*s != '-')
396 >                s = sskip(s);
397          while (*s) {
398                  ac = 0;
399                  do {
# Line 234 | Line 423 | FILE  *fp;
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 < static VIEW  *hview;                    /* view from header */
432 < static int  gothview;                   /* success indicator */
433 < static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
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)                             /* get view from header */
467 > gethview(s, v)                          /* get view from header */
468   char  *s;
469 + register struct myview  *v;
470   {
471 <        register char  **an;
472 <
252 <        for (an = altname; *an != NULL; an++)
253 <                if (!strncmp(*an, s, strlen(*an))) {
254 <                        if (sscanview(hview, s+strlen(*an)) > 0)
255 <                                gothview++;
256 <                        return;
257 <                }
471 >        if (isview(s) && sscanview(v->hv, s) > 0)
472 >                v->ok++;
473   }
474  
475  
476   int
477 < viewfile(fname, vp, xp, yp)             /* get view from file */
477 > viewfile(fname, vp, rp)                 /* get view from file */
478   char  *fname;
479   VIEW  *vp;
480 < int  *xp, *yp;
480 > RESOLU  *rp;
481   {
482 <        extern char  *progname;
482 >        struct myview   mvs;
483          FILE  *fp;
484  
485          if ((fp = fopen(fname, "r")) == NULL)
486                  return(-1);
487  
488 <        altname[0] = progname;
489 <        hview = vp;
275 <        gothview = 0;
488 >        mvs.hv = vp;
489 >        mvs.ok = 0;
490  
491 <        getheader(fp, gethview);
491 >        getheader(fp, gethview, &mvs);
492  
493 <        if (xp != NULL && yp != NULL
494 <                        && fgetresolu(xp, yp, fp) == -1)
281 <                gothview = 0;
493 >        if (rp != NULL && !fgetsresolu(rp, fp))
494 >                mvs.ok = 0;
495  
496          fclose(fp);
497  
498 <        return(gothview);
498 >        return(mvs.ok);
499   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines