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.14 by greg, Mon Mar 18 13:48:05 1991 UTC vs.
Revision 2.9 by greg, Fri Aug 18 10:20:10 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 24 | Line 28 | register VIEW  *v;
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  
# Line 55 | Line 63 | register VIEW  *v;
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_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 / 90.0;
80 <                v->vn2 = v->vert / 90.0;
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)
# Line 75 | Line 91 | register VIEW  *v;
91                  return("unknown view type");
92          }
93          if (v->type != VT_ANG) {
94 <                v->hvec[0] *= v->hn2;
95 <                v->hvec[1] *= v->hn2;
96 <                v->hvec[2] *= v->hn2;
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;
# Line 103 | Line 121 | int  *xp, *yp;                 /* x and y resolution in (or out if *
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;
# Line 115 | Line 134 | double  x, y;
134  
135          switch(v->type) {
136          case VT_PAR:                    /* parallel view */
137 <                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
138 <                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
139 <                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
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 <                return(0);
144 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
145          case VT_PER:                    /* perspective view */
124                VCOPY(orig, v->vp);
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);
150 <                return(0);
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);
157 >                        return(-1.0);
158                  z = sqrt(z);
135                VCOPY(orig, v->vp);
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 <                return(0);
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);
146 <                VCOPY(orig, v->vp);
147 <                if (d <= FTINY) {
148 <                        VCOPY(direc, v->vdir);
149 <                        return(0);
150 <                }
183 >                        return(-1.0);
184                  d = sqrt(d);
185                  z = cos(PI*d);
186 <                d = sqrt(1 - z*z)/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 <                return(0);
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);
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   {
# Line 176 | Line 212 | FVECT  p;
212  
213          switch (v->type) {
214          case VT_PAR:                    /* parallel view */
215 <                if (zp != NULL)
180 <                        *zp = DOT(disp,v->vdir);
215 >                ip[2] = DOT(disp,v->vdir) - v->vfore;
216                  break;
217          case VT_PER:                    /* perspective view */
218                  d = DOT(disp,v->vdir);
219 <                if (zp != NULL) {
220 <                        *zp = sqrt(DOT(disp,disp));
221 <                        if (d < 0.0)
187 <                                *zp = -*zp;
188 <                }
189 <                if (d < 0.0)            /* fold pyramid */
219 >                ip[2] = sqrt(DOT(disp,disp));
220 >                if (d < 0.0) {          /* fold pyramid */
221 >                        ip[2] = -ip[2];
222                          d = -d;
223 +                }
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 (zp != NULL) {
235 <                        if (DOT(disp,v->vdir) < 0.0)
236 <                                *zp = -d;
237 <                        else
238 <                                *zp = d;
205 <                }
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(ip[2],d) / 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 <                d = normalize(disp);
253 <                if (zp != NULL)
254 <                        *zp = d;
211 <                *xp = 0.5 - v->hoff;
212 <                *yp = 0.5 - v->voff;
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 <                        *xp += 180.0/v->horiz;
218 <                        *yp += 180.0/v->vert;
259 >                        ip[0] += 180.0/v->horiz;
260                          return;
261                  }
262                  d = acos(d)/PI / sqrt(1.0 - d*d);
263 <                *xp += DOT(disp,v->hvec)*d*180.0/v->horiz;
264 <                *yp += DOT(disp,v->vvec)*d*180.0/v->vert;
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 243 | 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 297 | 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 328 | 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 <
346 <        for (an = altname; *an != NULL; an++)
347 <                if (!strncmp(*an, s, strlen(*an))) {
348 <                        if (sscanview(hview, s+strlen(*an)) > 0)
349 <                                gothview++;
350 <                        return;
351 <                }
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;
369 <        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)
375 <                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