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.15 by greg, Thu Apr 18 14:52:55 1991 UTC vs.
Revision 2.26 by schorsch, Fri Jan 2 11:36:26 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  image.c - routines for image generation.
6   *
7 < *     10/17/85
7 > *  External symbols declared in view.h
8   */
9  
10 < #include  "standard.h"
10 > #include "copyright.h"
11  
12 + #include  "rtio.h"
13   #include  "view.h"
14  
15 +
16 + #define  FEQ(x,y)       (fabs((x)-(y)) <= FTINY)
17 + #define  VEQ(v,w)       (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \
18 +                                && FEQ((v)[2],(w)[2]))
19 +
20   VIEW  stdview = STDVIEW;                /* default view parameters */
21  
22 + static gethfunc gethview;
23  
24 +
25   char *
26   setview(v)              /* set hvec and vvec, return message on error */
27   register VIEW  *v;
# Line 24 | Line 29 | register VIEW  *v;
29          static char  ill_horiz[] = "illegal horizontal view size";
30          static char  ill_vert[] = "illegal vertical view size";
31          
32 +        if (v->vaft < -FTINY || (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 89 | Line 107 | register VIEW  *v;
107   }
108  
109  
110 + void
111   normaspect(va, ap, xp, yp)              /* fix pixel aspect or resolution */
112   double  va;                     /* view aspect ratio */
113   double  *ap;                    /* pixel aspect in (or out if 0) */
# Line 103 | Line 122 | int  *xp, *yp;                 /* x and y resolution in (or out if *
122   }
123  
124  
125 + double
126   viewray(orig, direc, v, x, y)           /* compute ray origin and direction */
127   FVECT  orig, direc;
128   register VIEW  *v;
# Line 115 | Line 135 | double  x, y;
135  
136          switch(v->type) {
137          case VT_PAR:                    /* parallel view */
138 <                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
139 <                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
140 <                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
138 >                orig[0] = v->vp[0] + v->vfore*v->vdir[0]
139 >                                + x*v->hvec[0] + y*v->vvec[0];
140 >                orig[1] = v->vp[1] + v->vfore*v->vdir[1]
141 >                                + x*v->hvec[1] + y*v->vvec[1];
142 >                orig[2] = v->vp[2] + v->vfore*v->vdir[2]
143 >                                + x*v->hvec[2] + y*v->vvec[2];
144                  VCOPY(direc, v->vdir);
145 <                return(0);
145 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
146          case VT_PER:                    /* perspective view */
124                VCOPY(orig, v->vp);
147                  direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
148                  direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
149                  direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
150 <                normalize(direc);
151 <                return(0);
150 >                orig[0] = v->vp[0] + v->vfore*direc[0];
151 >                orig[1] = v->vp[1] + v->vfore*direc[1];
152 >                orig[2] = v->vp[2] + v->vfore*direc[2];
153 >                d = normalize(direc);
154 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
155          case VT_HEM:                    /* hemispherical fisheye */
156                  z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
157                  if (z < 0.0)
158 <                        return(-1);
158 >                        return(-1.0);
159                  z = sqrt(z);
135                VCOPY(orig, v->vp);
160                  direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
161                  direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
162                  direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
163 <                return(0);
163 >                orig[0] = v->vp[0] + v->vfore*direc[0];
164 >                orig[1] = v->vp[1] + v->vfore*direc[1];
165 >                orig[2] = v->vp[2] + v->vfore*direc[2];
166 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
167 >        case VT_CYL:                    /* cylindrical panorama */
168 >                d = x * v->horiz * (PI/180.0);
169 >                z = cos(d);
170 >                x = sin(d);
171 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
172 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
173 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
174 >                orig[0] = v->vp[0] + v->vfore*direc[0];
175 >                orig[1] = v->vp[1] + v->vfore*direc[1];
176 >                orig[2] = v->vp[2] + v->vfore*direc[2];
177 >                d = normalize(direc);
178 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
179          case VT_ANG:                    /* angular fisheye */
180                  x *= v->horiz/180.0;
181                  y *= v->vert/180.0;
182                  d = x*x + y*y;
183                  if (d > 1.0)
184 <                        return(-1);
146 <                VCOPY(orig, v->vp);
147 <                if (d <= FTINY) {
148 <                        VCOPY(direc, v->vdir);
149 <                        return(0);
150 <                }
184 >                        return(-1.0);
185                  d = sqrt(d);
186                  z = cos(PI*d);
187 <                d = sqrt(1 - z*z)/d;
187 >                d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
188                  x *= d;
189                  y *= d;
190                  direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
191                  direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
192                  direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
193 <                return(0);
193 >                orig[0] = v->vp[0] + v->vfore*direc[0];
194 >                orig[1] = v->vp[1] + v->vfore*direc[1];
195 >                orig[2] = v->vp[2] + v->vfore*direc[2];
196 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
197          }
198 <        return(-1);
198 >        return(-1.0);
199   }
200  
201  
202 < viewpixel(xp, yp, zp, v, p)             /* find image location for point */
203 < double  *xp, *yp, *zp;
202 > void
203 > viewloc(ip, v, p)               /* find image location for point */
204 > FVECT  ip;
205   register VIEW  *v;
206   FVECT  p;
207   {
208 <        double  d;
208 >        double  d, d2;
209          FVECT  disp;
210  
211          disp[0] = p[0] - v->vp[0];
# Line 176 | Line 214 | FVECT  p;
214  
215          switch (v->type) {
216          case VT_PAR:                    /* parallel view */
217 <                if (zp != NULL)
180 <                        *zp = DOT(disp,v->vdir);
217 >                ip[2] = DOT(disp,v->vdir) - v->vfore;
218                  break;
219          case VT_PER:                    /* perspective view */
220                  d = DOT(disp,v->vdir);
221 <                if (zp != NULL) {
222 <                        *zp = sqrt(DOT(disp,disp));
223 <                        if (d < 0.0)
187 <                                *zp = -*zp;
188 <                }
189 <                if (d < 0.0)            /* fold pyramid */
221 >                ip[2] = VLEN(disp);
222 >                if (d < 0.0) {          /* fold pyramid */
223 >                        ip[2] = -ip[2];
224                          d = -d;
225 +                }
226                  if (d > FTINY) {
227                          d = 1.0/d;
228                          disp[0] *= d;
229                          disp[1] *= d;
230                          disp[2] *= d;
231                  }
232 +                ip[2] *= (1.0 - v->vfore*d);
233                  break;
234          case VT_HEM:                    /* hemispherical fisheye */
235                  d = normalize(disp);
236 <                if (zp != NULL) {
237 <                        if (DOT(disp,v->vdir) < 0.0)
238 <                                *zp = -d;
239 <                        else
240 <                                *zp = d;
205 <                }
236 >                if (DOT(disp,v->vdir) < 0.0)
237 >                        ip[2] = -d;
238 >                else
239 >                        ip[2] = d;
240 >                ip[2] -= v->vfore;
241                  break;
242 +        case VT_CYL:                    /* cylindrical panorama */
243 +                d = DOT(disp,v->hvec);
244 +                d2 = DOT(disp,v->vdir);
245 +                ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
246 +                d = 1.0/sqrt(d*d + d2*d2);
247 +                ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
248 +                ip[2] = VLEN(disp);
249 +                ip[2] *= (1.0 - v->vfore*d);
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 + void
273 + pix2loc(loc, rp, px, py)        /* compute image location from pixel pos. */
274 + RREAL  loc[2];
275 + register RESOLU  *rp;
276 + int  px, py;
277 + {
278 +        register int  x, y;
279 +
280 +        if (rp->rt & YMAJOR) {
281 +                x = px;
282 +                y = py;
283 +        } else {
284 +                x = py;
285 +                y = px;
286 +        }
287 +        if (rp->rt & XDECR)
288 +                x = rp->xr-1 - x;
289 +        if (rp->rt & YDECR)
290 +                y = rp->yr-1 - y;
291 +        loc[0] = (x+.5)/rp->xr;
292 +        loc[1] = (y+.5)/rp->yr;
293 + }
294 +
295 +
296 + void
297 + loc2pix(pp, rp, lx, ly)         /* compute pixel pos. from image location */
298 + int  pp[2];
299 + register RESOLU  *rp;
300 + double  lx, ly;
301 + {
302 +        register int  x, y;
303 +
304 +        x = lx * rp->xr;
305 +        y = ly * rp->yr;
306 +        if (rp->rt & XDECR)
307 +                x = rp->xr-1 - x;
308 +        if (rp->rt & YDECR)
309 +                y = rp->yr-1 - y;
310 +        if (rp->rt & YMAJOR) {
311 +                pp[0] = x;
312 +                pp[1] = y;
313 +        } else {
314 +                pp[0] = y;
315 +                pp[1] = x;
316 +        }
317 + }
318 +
319 +
320   int
321   getviewopt(v, ac, av)                   /* process view argument */
322   register VIEW  *v;
323   int  ac;
324   register char  *av[];
325   {
326 < #define check(c,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1)
327 <        extern double  atof();
326 > #define check(c,l)      if ((av[0][c]&&av[0][c]!=' ') || \
327 >                        badarg(ac-1,av+1,l)) return(-1)
328  
329          if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
330                  return(-1);
# Line 243 | Line 332 | register char  *av[];
332          case 't':                       /* type */
333                  if (!av[0][3] || av[0][3]==' ')
334                          return(-1);
335 <                check(4,0);
335 >                check(4,"");
336                  v->type = av[0][3];
337                  return(0);
338          case 'p':                       /* point */
339 <                check(3,3);
339 >                check(3,"fff");
340                  v->vp[0] = atof(av[1]);
341                  v->vp[1] = atof(av[2]);
342                  v->vp[2] = atof(av[3]);
343                  return(3);
344          case 'd':                       /* direction */
345 <                check(3,3);
345 >                check(3,"fff");
346                  v->vdir[0] = atof(av[1]);
347                  v->vdir[1] = atof(av[2]);
348                  v->vdir[2] = atof(av[3]);
349                  return(3);
350          case 'u':                       /* up */
351 <                check(3,3);
351 >                check(3,"fff");
352                  v->vup[0] = atof(av[1]);
353                  v->vup[1] = atof(av[2]);
354                  v->vup[2] = atof(av[3]);
355                  return(3);
356          case 'h':                       /* horizontal size */
357 <                check(3,1);
357 >                check(3,"f");
358                  v->horiz = atof(av[1]);
359                  return(1);
360          case 'v':                       /* vertical size */
361 <                check(3,1);
361 >                check(3,"f");
362                  v->vert = atof(av[1]);
363                  return(1);
364 +        case 'o':                       /* fore clipping plane */
365 +                check(3,"f");
366 +                v->vfore = atof(av[1]);
367 +                return(1);
368 +        case 'a':                       /* aft clipping plane */
369 +                check(3,"f");
370 +                v->vaft = atof(av[1]);
371 +                return(1);
372          case 's':                       /* shift */
373 <                check(3,1);
373 >                check(3,"f");
374                  v->hoff = atof(av[1]);
375                  return(1);
376          case 'l':                       /* lift */
377 <                check(3,1);
377 >                check(3,"f");
378                  v->voff = atof(av[1]);
379                  return(1);
380          default:
# Line 299 | Line 396 | register char  *s;
396  
397          while (*s == ' ')
398                  s++;
399 +        if (*s != '-')
400 +                s = sskip2(s,1);
401          while (*s) {
402                  ac = 0;
403                  do {
404 <                        av[ac++] = s;
404 >                        if (ac || *s == '-')
405 >                                av[ac++] = s;
406                          while (*s && *s != ' ')
407                                  s++;
408                          while (*s == ' ')
# Line 319 | Line 419 | register char  *s;
419   }
420  
421  
422 + void
423   fprintview(vp, fp)                      /* write out view parameters */
424   register VIEW  *vp;
425   FILE  *fp;
# Line 328 | Line 429 | FILE  *fp;
429          fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
430          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
431          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
432 +        fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
433          fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
434   }
435  
436  
437 < static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
437 > char *
438 > viewopt(vp)                             /* translate to minimal view string */
439 > register VIEW  *vp;
440 > {
441 >        static char  vwstr[128];
442 >        register char  *cp = vwstr;
443  
444 +        if (vp->type != stdview.type) {
445 +                sprintf(cp, " -vt%c", vp->type);
446 +                cp += strlen(cp);
447 +        }
448 +        if (!VEQ(vp->vp,stdview.vp)) {
449 +                sprintf(cp, " -vp %.6g %.6g %.6g",
450 +                                vp->vp[0], vp->vp[1], vp->vp[2]);
451 +                cp += strlen(cp);
452 +        }
453 +        if (!VEQ(vp->vdir,stdview.vdir)) {
454 +                sprintf(cp, " -vd %.6g %.6g %.6g",
455 +                                vp->vdir[0], vp->vdir[1], vp->vdir[2]);
456 +                cp += strlen(cp);
457 +        }
458 +        if (!VEQ(vp->vup,stdview.vup)) {
459 +                sprintf(cp, " -vu %.6g %.6g %.6g",
460 +                                vp->vup[0], vp->vup[1], vp->vup[2]);
461 +                cp += strlen(cp);
462 +        }
463 +        if (!FEQ(vp->horiz,stdview.horiz)) {
464 +                sprintf(cp, " -vh %.6g", vp->horiz);
465 +                cp += strlen(cp);
466 +        }
467 +        if (!FEQ(vp->vert,stdview.vert)) {
468 +                sprintf(cp, " -vv %.6g", vp->vert);
469 +                cp += strlen(cp);
470 +        }
471 +        if (!FEQ(vp->vfore,stdview.vfore)) {
472 +                sprintf(cp, " -vo %.6g", vp->vfore);
473 +                cp += strlen(cp);
474 +        }
475 +        if (!FEQ(vp->vaft,stdview.vaft)) {
476 +                sprintf(cp, " -va %.6g", vp->vaft);
477 +                cp += strlen(cp);
478 +        }
479 +        if (!FEQ(vp->hoff,stdview.hoff)) {
480 +                sprintf(cp, " -vs %.6g", vp->hoff);
481 +                cp += strlen(cp);
482 +        }
483 +        if (!FEQ(vp->voff,stdview.voff)) {
484 +                sprintf(cp, " -vl %.6g", vp->voff);
485 +                cp += strlen(cp);
486 +        }
487 +        return(vwstr);
488 + }
489 +
490 +
491 + int
492 + isview(s)                               /* is this a view string? */
493 + char  *s;
494 + {
495 +        static char  *altname[]={NULL,VIEWSTR,"rpict","rview","rvu","rpiece","pinterp",NULL};
496 +        extern char  *progname;
497 +        register char  *cp;
498 +        register char  **an;
499 +                                        /* add program name to list */
500 +        if (altname[0] == NULL) {
501 +                for (cp = progname; *cp; cp++)
502 +                        ;
503 +                while (cp > progname && !ISDIRSEP(cp[-1]))
504 +                        cp--;
505 +                altname[0] = cp;
506 +        }
507 +                                        /* skip leading path */
508 +        cp = s;
509 +        while (*cp && *cp != ' ')
510 +                cp++;
511 +        while (cp > s && !ISDIRSEP(cp[-1]))
512 +                cp--;
513 +        for (an = altname; *an != NULL; an++)
514 +                if (!strncmp(*an, cp, strlen(*an)))
515 +                        return(1);
516 +        return(0);
517 + }
518 +
519 +
520   struct myview {
521          VIEW    *hv;
522          int     ok;
523   };
524  
525  
526 < static
527 < gethview(s, v)                          /* get view from header */
528 < char  *s;
529 < register struct myview  *v;
526 > static int
527 > gethview(                               /* get view from header */
528 >        char  *s,
529 >        void  *v
530 > )
531   {
532 <        register char  **an;
533 <
534 <        for (an = altname; *an != NULL; an++)
351 <                if (!strncmp(*an, s, strlen(*an))) {
352 <                        if (sscanview(v->hv, s+strlen(*an)) > 0)
353 <                                v->ok++;
354 <                        return;
355 <                }
532 >        if (isview(s) && sscanview(((struct myview*)v)->hv, s) > 0)
533 >                ((struct myview*)v)->ok++;
534 >        return(0);
535   }
536  
537  
538   int
539 < viewfile(fname, vp, xp, yp)             /* get view from file */
539 > viewfile(fname, vp, rp)                 /* get view from file */
540   char  *fname;
541   VIEW  *vp;
542 < int  *xp, *yp;
542 > RESOLU  *rp;
543   {
365        extern char  *progname;
544          struct myview   mvs;
545          FILE  *fp;
546  
547 <        if ((fp = fopen(fname, "r")) == NULL)
547 >        if (fname == NULL || !strcmp(fname, "-"))
548 >                fp = stdin;
549 >        else if ((fp = fopen(fname, "r")) == NULL)
550                  return(-1);
551  
372        altname[0] = progname;
552          mvs.hv = vp;
553          mvs.ok = 0;
554  
555          getheader(fp, gethview, &mvs);
556  
557 <        if (xp != NULL && yp != NULL
379 <                        && fgetresolu(xp, yp, fp) == -1)
557 >        if (rp != NULL && !fgetsresolu(rp, fp))
558                  mvs.ok = 0;
559  
560          fclose(fp);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines