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.12 by greg, Sat Oct 13 20:56:01 1990 UTC vs.
Revision 2.12 by greg, Wed Jan 17 10:55:40 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  image.c - routines for image generation.
9 *
10 *     10/17/85
9   */
10  
11   #include  "standard.h"
12  
13   #include  "view.h"
14  
15 + #include  "resolu.h"
16 +
17 + #include  "paths.h"
18 +
19 + #define  FEQ(x,y)       (fabs((x)-(y)) <= FTINY)
20 + #define  VEQ(v,w)       (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \
21 +                                && FEQ((v)[2],(w)[2]))
22 +
23   VIEW  stdview = STDVIEW;                /* default view parameters */
24  
25  
# Line 24 | Line 30 | register VIEW  *v;
30          static char  ill_horiz[] = "illegal horizontal view size";
31          static char  ill_vert[] = "illegal vertical view size";
32          
33 +        if (v->vfore < -FTINY || v->vaft < -FTINY ||
34 +                        (v->vaft > FTINY && v->vaft <= v->vfore))
35 +                return("illegal fore/aft clipping plane");
36 +
37          if (normalize(v->vdir) == 0.0)          /* normalize direction */
38                  return("zero view direction");
39  
40 +        if (normalize(v->vup) == 0.0)           /* normalize view up */
41 +                return("zero view up vector");
42 +
43          fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */
44  
45          if (normalize(v->hvec) == 0.0)
46 <                return("illegal view up vector");
46 >                return("view up parallel to view direction");
47  
48          fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */
49  
# Line 52 | Line 65 | register VIEW  *v;
65                  v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
66                  v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
67                  break;
68 +        case VT_CYL:                            /* cylindrical panorama */
69 +                if (v->horiz > 360.0+FTINY)
70 +                        return(ill_horiz);
71 +                if (v->vert >= 180.0-FTINY)
72 +                        return(ill_vert);
73 +                v->hn2 = v->horiz * (PI/180.0);
74 +                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
75 +                break;
76          case VT_ANG:                            /* angular fisheye */
77                  if (v->horiz > 360.0+FTINY)
78                          return(ill_horiz);
79                  if (v->vert > 360.0+FTINY)
80                          return(ill_vert);
81 <                v->hn2 = v->horiz / 90.0;
82 <                v->vn2 = v->vert / 90.0;
81 >                v->hn2 = v->horiz * (PI/180.0);
82 >                v->vn2 = v->vert * (PI/180.0);
83                  break;
84          case VT_HEM:                            /* hemispherical fisheye */
85                  if (v->horiz > 180.0+FTINY)
# Line 71 | Line 92 | register VIEW  *v;
92          default:
93                  return("unknown view type");
94          }
95 <        if (v->type == VT_PAR || v->type == VT_PER) {
96 <                v->hvec[0] *= v->hn2;
97 <                v->hvec[1] *= v->hn2;
98 <                v->hvec[2] *= v->hn2;
95 >        if (v->type != VT_ANG) {
96 >                if (v->type != VT_CYL) {
97 >                        v->hvec[0] *= v->hn2;
98 >                        v->hvec[1] *= v->hn2;
99 >                        v->hvec[2] *= v->hn2;
100 >                }
101                  v->vvec[0] *= v->vn2;
102                  v->vvec[1] *= v->vn2;
103                  v->vvec[2] *= v->vn2;
# Line 100 | Line 123 | int  *xp, *yp;                 /* x and y resolution in (or out if *
123   }
124  
125  
126 + double
127   viewray(orig, direc, v, x, y)           /* compute ray origin and direction */
128   FVECT  orig, direc;
129   register VIEW  *v;
# Line 112 | Line 136 | double  x, y;
136  
137          switch(v->type) {
138          case VT_PAR:                    /* parallel view */
139 <                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
140 <                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
141 <                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
139 >                orig[0] = v->vp[0] + v->vfore*v->vdir[0]
140 >                                + x*v->hvec[0] + y*v->vvec[0];
141 >                orig[1] = v->vp[1] + v->vfore*v->vdir[1]
142 >                                + x*v->hvec[1] + y*v->vvec[1];
143 >                orig[2] = v->vp[2] + v->vfore*v->vdir[2]
144 >                                + x*v->hvec[2] + y*v->vvec[2];
145                  VCOPY(direc, v->vdir);
146 <                return(0);
146 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
147          case VT_PER:                    /* perspective view */
121                VCOPY(orig, v->vp);
148                  direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
149                  direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
150                  direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
151 <                normalize(direc);
152 <                return(0);
151 >                orig[0] = v->vp[0] + v->vfore*direc[0];
152 >                orig[1] = v->vp[1] + v->vfore*direc[1];
153 >                orig[2] = v->vp[2] + v->vfore*direc[2];
154 >                d = normalize(direc);
155 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
156          case VT_HEM:                    /* hemispherical fisheye */
157 <                x *= v->horiz/90.0;
129 <                y *= v->vert/90.0;
130 <                z = 1.0 - x*x - y*y;
157 >                z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
158                  if (z < 0.0)
159 <                        return(-1);
159 >                        return(-1.0);
160                  z = sqrt(z);
134                VCOPY(orig, v->vp);
161                  direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
162                  direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
163                  direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
164 <                return(0);
164 >                orig[0] = v->vp[0] + v->vfore*direc[0];
165 >                orig[1] = v->vp[1] + v->vfore*direc[1];
166 >                orig[2] = v->vp[2] + v->vfore*direc[2];
167 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
168 >        case VT_CYL:                    /* cylindrical panorama */
169 >                d = x * v->horiz * (PI/180.0);
170 >                z = cos(d);
171 >                x = sin(d);
172 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
173 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
174 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
175 >                orig[0] = v->vp[0] + v->vfore*direc[0];
176 >                orig[1] = v->vp[1] + v->vfore*direc[1];
177 >                orig[2] = v->vp[2] + v->vfore*direc[2];
178 >                d = normalize(direc);
179 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
180          case VT_ANG:                    /* angular fisheye */
181                  x *= v->horiz/180.0;
182                  y *= v->vert/180.0;
183                  d = x*x + y*y;
184                  if (d > 1.0)
185 <                        return(-1);
145 <                VCOPY(orig, v->vp);
146 <                if (d <= FTINY) {
147 <                        VCOPY(direc, v->vdir);
148 <                        return(0);
149 <                }
185 >                        return(-1.0);
186                  d = sqrt(d);
187                  z = cos(PI*d);
188 <                d = sqrt(1 - z*z)/d;
188 >                d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
189                  x *= d;
190                  y *= d;
191                  direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
192                  direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
193                  direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
194 <                return(0);
194 >                orig[0] = v->vp[0] + v->vfore*direc[0];
195 >                orig[1] = v->vp[1] + v->vfore*direc[1];
196 >                orig[2] = v->vp[2] + v->vfore*direc[2];
197 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
198          }
199 <        return(-1);
199 >        return(-1.0);
200   }
201  
202  
203 < viewpixel(xp, yp, zp, v, p)             /* find image location for point */
204 < double  *xp, *yp, *zp;
203 > viewloc(ip, v, p)               /* find image location for point */
204 > FVECT  ip;
205   register VIEW  *v;
206   FVECT  p;
207   {
# Line 175 | Line 214 | FVECT  p;
214  
215          switch (v->type) {
216          case VT_PAR:                    /* parallel view */
217 <                if (zp != NULL)
218 <                        *zp = DOT(disp,v->vdir);
180 <                *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
181 <                *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
182 <                return;
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)
188 <                                *zp = -*zp;
189 <                }
190 <                if (d < 0.0)            /* fold pyramid */
221 >                ip[2] = sqrt(DOT(disp,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 <                *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
233 <                *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
200 <                return;
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;
241 <                }
242 <                *xp = DOT(disp,v->hvec)*90.0/v->horiz + 0.5 - v->hoff;
243 <                *yp = DOT(disp,v->vvec)*90.0/v->vert + 0.5 - v->voff;
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 >                ip[2] = DOT(disp,v->vdir);
244 >                d = DOT(disp,v->hvec);
245 >                ip[0] = 180.0/PI * atan2(d,ip[2]) / v->horiz + 0.5 - v->hoff;
246 >                ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
247 >                if (v->vfore > FTINY)
248 >                        ip[2] = sqrt(DOT(disp,disp)) *
249 >                                (1.0 - v->vfore/sqrt(d*d + ip[2]*ip[2]));
250 >                else
251 >                        ip[2] = sqrt(DOT(disp,disp));
252                  return;
253          case VT_ANG:                    /* angular fisheye */
254 <                d = normalize(disp);
255 <                if (zp != NULL)
256 <                        *zp = d;
216 <                *xp = 0.5 - v->hoff;
217 <                *yp = 0.5 - v->voff;
254 >                ip[0] = 0.5 - v->hoff;
255 >                ip[1] = 0.5 - v->voff;
256 >                ip[2] = normalize(disp) - v->vfore;
257                  d = DOT(disp,v->vdir);
258                  if (d >= 1.0-FTINY)
259                          return;
260                  if (d <= -(1.0-FTINY)) {
261 <                        *xp += 180.0/v->horiz;
223 <                        *yp += 180.0/v->vert;
261 >                        ip[0] += 180.0/v->horiz;
262                          return;
263                  }
264                  d = acos(d)/PI / sqrt(1.0 - d*d);
265 <                *xp += DOT(disp,v->hvec)*d*180.0/v->horiz;
266 <                *yp += DOT(disp,v->vvec)*d*180.0/v->vert;
265 >                ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
266 >                ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
267                  return;
268          }
269 +        ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
270 +        ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
271   }
272  
273  
274 + pix2loc(loc, rp, px, py)        /* compute image location from pixel pos. */
275 + FLOAT  loc[2];
276 + register RESOLU  *rp;
277 + int  px, py;
278 + {
279 +        register int  x, y;
280 +
281 +        if (rp->or & YMAJOR) {
282 +                x = px;
283 +                y = py;
284 +        } else {
285 +                x = py;
286 +                y = px;
287 +        }
288 +        if (rp->or & XDECR)
289 +                x = rp->xr-1 - x;
290 +        if (rp->or & YDECR)
291 +                y = rp->yr-1 - y;
292 +        loc[0] = (x+.5)/rp->xr;
293 +        loc[1] = (y+.5)/rp->yr;
294 + }
295 +
296 +
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->or & XDECR)
307 +                x = rp->xr-1 - x;
308 +        if (rp->or & YDECR)
309 +                y = rp->yr-1 - y;
310 +        if (rp->or & 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 246 | 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 300 | Line 394 | register char  *s;
394          int  na;
395          int  nvopts = 0;
396  
397 <        while (*s == ' ')
398 <                s++;
397 >        if (*s != '-')
398 >                s = sskip2(s,1);
399          while (*s) {
400                  ac = 0;
401                  do {
# Line 331 | Line 425 | FILE  *fp;
425          fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
426          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
427          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
428 +        fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
429          fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
430   }
431  
432  
433 < static VIEW  *hview;                    /* view from header */
434 < static int  gothview;                   /* success indicator */
435 < static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
433 > char *
434 > viewopt(vp)                             /* translate to minimal view string */
435 > register VIEW  *vp;
436 > {
437 >        static char  vwstr[128];
438 >        register char  *cp = vwstr;
439  
440 +        if (vp->type != stdview.type) {
441 +                sprintf(cp, " -vt%c", vp->type);
442 +                cp += strlen(cp);
443 +        }
444 +        if (!VEQ(vp->vp,stdview.vp)) {
445 +                sprintf(cp, " -vp %.6g %.6g %.6g",
446 +                                vp->vp[0], vp->vp[1], vp->vp[2]);
447 +                cp += strlen(cp);
448 +        }
449 +        if (!VEQ(vp->vdir,stdview.vdir)) {
450 +                sprintf(cp, " -vd %.6g %.6g %.6g",
451 +                                vp->vdir[0], vp->vdir[1], vp->vdir[2]);
452 +                cp += strlen(cp);
453 +        }
454 +        if (!VEQ(vp->vup,stdview.vup)) {
455 +                sprintf(cp, " -vu %.6g %.6g %.6g",
456 +                                vp->vup[0], vp->vup[1], vp->vup[2]);
457 +                cp += strlen(cp);
458 +        }
459 +        if (!FEQ(vp->horiz,stdview.horiz)) {
460 +                sprintf(cp, " -vh %.6g", vp->horiz);
461 +                cp += strlen(cp);
462 +        }
463 +        if (!FEQ(vp->vert,stdview.vert)) {
464 +                sprintf(cp, " -vv %.6g", vp->vert);
465 +                cp += strlen(cp);
466 +        }
467 +        if (!FEQ(vp->vfore,stdview.vfore)) {
468 +                sprintf(cp, " -vo %.6g", vp->vfore);
469 +                cp += strlen(cp);
470 +        }
471 +        if (!FEQ(vp->vaft,stdview.vaft)) {
472 +                sprintf(cp, " -va %.6g", vp->vaft);
473 +                cp += strlen(cp);
474 +        }
475 +        if (!FEQ(vp->hoff,stdview.hoff)) {
476 +                sprintf(cp, " -vs %.6g", vp->hoff);
477 +                cp += strlen(cp);
478 +        }
479 +        if (!FEQ(vp->voff,stdview.voff)) {
480 +                sprintf(cp, " -vl %.6g", vp->voff);
481 +                cp += strlen(cp);
482 +        }
483 +        return(vwstr);
484 + }
485  
486 < static
487 < gethview(s)                             /* get view from header */
486 >
487 > int
488 > isview(s)                               /* is this a view string? */
489   char  *s;
490   {
491 +        static char  *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
492 +        extern char  *progname;
493 +        register char  *cp;
494          register char  **an;
495 <
495 >                                        /* add program name to list */
496 >        if (altname[0] == NULL) {
497 >                for (cp = progname; *cp; cp++)
498 >                        ;
499 >                while (cp > progname && !ISDIRSEP(cp[-1]))
500 >                        cp--;
501 >                altname[0] = cp;
502 >        }
503 >                                        /* skip leading path */
504 >        cp = s;
505 >        while (*cp && *cp != ' ')
506 >                cp++;
507 >        while (cp > s && !ISDIRSEP(cp[-1]))
508 >                cp--;
509          for (an = altname; *an != NULL; an++)
510 <                if (!strncmp(*an, s, strlen(*an))) {
511 <                        if (sscanview(hview, s+strlen(*an)) > 0)
512 <                                gothview++;
353 <                        return;
354 <                }
510 >                if (!strncmp(*an, cp, strlen(*an)))
511 >                        return(1);
512 >        return(0);
513   }
514  
515  
516 + struct myview {
517 +        VIEW    *hv;
518 +        int     ok;
519 + };
520 +
521 +
522 + static
523 + gethview(s, v)                          /* get view from header */
524 + char  *s;
525 + register struct myview  *v;
526 + {
527 +        if (isview(s) && sscanview(v->hv, s) > 0)
528 +                v->ok++;
529 + }
530 +
531 +
532   int
533 < viewfile(fname, vp, xp, yp)             /* get view from file */
533 > viewfile(fname, vp, rp)                 /* get view from file */
534   char  *fname;
535   VIEW  *vp;
536 < int  *xp, *yp;
536 > RESOLU  *rp;
537   {
538 <        extern char  *progname;
538 >        struct myview   mvs;
539          FILE  *fp;
540  
541          if ((fp = fopen(fname, "r")) == NULL)
542                  return(-1);
543  
544 <        altname[0] = progname;
545 <        hview = vp;
372 <        gothview = 0;
544 >        mvs.hv = vp;
545 >        mvs.ok = 0;
546  
547 <        getheader(fp, gethview);
547 >        getheader(fp, gethview, &mvs);
548  
549 <        if (xp != NULL && yp != NULL
550 <                        && fgetresolu(xp, yp, fp) == -1)
378 <                gothview = 0;
549 >        if (rp != NULL && !fgetsresolu(rp, fp))
550 >                mvs.ok = 0;
551  
552          fclose(fp);
553  
554 <        return(gothview);
554 >        return(mvs.ok);
555   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines