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 2.45 by greg, Tue Feb 6 02:08:12 2018 UTC vs.
Revision 2.53 by greg, Wed Jan 12 21:07:39 2022 UTC

# Line 15 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  "paths.h"
16   #include  "view.h"
17  
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
18   VIEW  stdview = STDVIEW;                /* default view parameters */
19  
20   static gethfunc gethview;
# Line 68 | Line 63 | VIEW  *v
63                          return(ill_horiz);
64                  if (v->vert >= 180.0-FTINY)
65                          return(ill_vert);
66 <                v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
67 <                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
66 >                v->hn2 = 2.0 * tan(v->horiz*(PI/360.));
67 >                v->vn2 = 2.0 * tan(v->vert*(PI/360.));
68                  break;
69          case VT_CYL:                            /* cylindrical panorama */
70                  if (v->horiz > 360.0+FTINY)
# Line 77 | Line 72 | VIEW  *v
72                  if (v->vert >= 180.0-FTINY)
73                          return(ill_vert);
74                  v->hn2 = v->horiz * (PI/180.0);
75 <                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
75 >                v->vn2 = 2.0 * tan(v->vert*(PI/360.));
76                  break;
77          case VT_ANG:                            /* angular fisheye */
78                  if (v->horiz > 360.0+FTINY)
# Line 92 | Line 87 | VIEW  *v
87                          return(ill_horiz);
88                  if (v->vert > 180.0+FTINY)
89                          return(ill_vert);
90 <                v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
91 <                v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
90 >                v->hn2 = 2.0 * sin(v->horiz*(PI/360.));
91 >                v->vn2 = 2.0 * sin(v->vert*(PI/360.));
92                  break;
93          case VT_PLS:                            /* planispheric fisheye */
94                  if (v->horiz >= 360.0-FTINY)
95                          return(ill_horiz);
96                  if (v->vert >= 360.0-FTINY)
97                          return(ill_vert);
98 <                v->hn2 = 2.*sin(v->horiz*(PI/180.0/2.0)) /
99 <                                (1.0 + cos(v->horiz*(PI/180.0/2.0)));
100 <                v->vn2 = 2.*sin(v->vert*(PI/180.0/2.0)) /
101 <                                (1.0 + cos(v->vert*(PI/180.0/2.0)));
98 >                v->hn2 = 2.*sin(v->horiz*(PI/360.)) /
99 >                                (1.0 + cos(v->horiz*(PI/360.)));
100 >                v->vn2 = 2.*sin(v->vert*(PI/360.)) /
101 >                                (1.0 + cos(v->vert*(PI/360.)));
102                  break;
103          default:
104                  return("unknown view type");
# Line 125 | Line 120 | VIEW  *v
120   }
121  
122  
123 + char *
124 + cropview(                       /* crop a view to the indicated bounds */
125 + VIEW *v,
126 + double x0,
127 + double y0,
128 + double x1,
129 + double y1
130 + )
131 + {
132 +        static char     ill_crop[] = "zero crop area";
133 +        static char     ill_hemi[] = "illegal crop for hemispherical view";
134 +        double  d;
135 +                                        /* order crop extrema */
136 +        if (x0 > x1) { d=x0; x0=x1; x1=d; }
137 +        if (y0 > y1) { d=y0; y0=y1; y1=d; }
138 +
139 +        d = x1 - x0;
140 +        if (d == .0)
141 +                return(ill_crop);
142 +        if (!FABSEQ(d, 1.))             /* adjust horizontal size? */
143 +                switch (v->type) {
144 +                case VT_PER:
145 +                        v->horiz = 360./PI*atan( d*tan(PI/360.*v->horiz) );
146 +                        break;
147 +                case VT_PAR:
148 +                case VT_ANG:
149 +                case VT_CYL:
150 +                        v->horiz *= d;
151 +                        break;
152 +                case VT_HEM:
153 +                        d *= sin(PI/360.*v->horiz);
154 +                        if (d > 1.)
155 +                                return(ill_hemi);
156 +                        v->horiz = 360./PI*asin( d );
157 +                        break;
158 +                case VT_PLS:
159 +                        d *= sin(PI/360.*v->horiz) /
160 +                                        (1. + cos(PI/360.*v->horiz));
161 +                        v->horiz = 360./PI*acos( (1. - d*d) / (1. + d*d) );
162 +                        break;
163 +                }
164 +        d = y1 - y0;
165 +        if (d == .0)
166 +                return(ill_crop);
167 +        if (!FABSEQ(d, 1.))             /* adjust vertical size? */
168 +                switch (v->type) {
169 +                case VT_PER:
170 +                case VT_CYL:
171 +                        v->vert = 360./PI*atan( d*tan(PI/360.*v->vert) );
172 +                        break;
173 +                case VT_PAR:
174 +                case VT_ANG:
175 +                        v->vert *= d;
176 +                        break;
177 +                case VT_HEM:
178 +                        d *= sin(PI/360.*v->vert);
179 +                        if (d > 1.)
180 +                                return(ill_hemi);
181 +                        v->vert = 360./PI*asin( d );
182 +                        break;
183 +                case VT_PLS:
184 +                        d *= sin(PI/360.*v->vert) /
185 +                                        (1. + cos(PI/360.*v->vert));
186 +                        v->vert = 360./PI*acos( (1. - d*d) / (1. + d*d) );
187 +                        break;
188 +                }
189 +                                        /* fix offsets */
190 +        v->hoff = ((x0 + x1)*.5 - .5 + v->hoff) / (x1 - x0);
191 +        v->voff = ((y0 + y1)*.5 - .5 + v->voff) / (y1 - y0);
192 +
193 +        return(setview(v));             /* final error checks & set-up */
194 + }
195 +
196 +
197   void
198   normaspect(                             /* fix pixel aspect or resolution */
199   double  va,                     /* view aspect ratio */
# Line 231 | Line 300 | viewloc(                       /* find image location for point */
300   FVECT  ip,
301   VIEW  *v,
302   FVECT  p
303 < )       /* returns: Good=1, Bad=0, Behind=-1, OutOfFrame=2, Behind+OOF=-2 */
303 > )       /* Use VL_* flags to interpret return value */
304   {
305 +        int     rflags = VL_GOOD;
306          double  d, d2;
307          FVECT  disp;
308  
# Line 244 | Line 314 | FVECT  p
314                  break;
315          case VT_PER:                    /* perspective view */
316                  d = DOT(disp,v->vdir);
317 +                if ((v->vaft > FTINY) & (d >= v->vaft))
318 +                        rflags |= VL_BEYOND;
319                  ip[2] = VLEN(disp);
320                  if (d < -FTINY) {       /* fold pyramid */
321                          ip[2] = -ip[2];
322                          d = -d;
323                  } else if (d <= FTINY)
324 <                        return(0);      /* at infinite edge */
324 >                        return(VL_BAD); /* at infinite edge */
325                  d = 1.0/d;
326                  disp[0] *= d;
327                  disp[1] *= d;
# Line 269 | Line 341 | FVECT  p
341                  d = DOT(disp,v->hvec);
342                  d2 = DOT(disp,v->vdir);
343                  ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
344 <                d = d*d + d2*d2;
345 <                if (d <= FTINY*FTINY)
346 <                        return(0);      /* at pole */
347 <                d = 1.0/sqrt(d);
344 >                d2 = d*d + d2*d2;
345 >                if (d2 <= FTINY*FTINY)
346 >                        return(VL_BAD); /* at pole */
347 >                if ((v->vaft > FTINY) & (d2 >= v->vaft*v->vaft))
348 >                        rflags |= VL_BEYOND;
349 >                d = 1.0/sqrt(d2);
350                  ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
351                  ip[2] = VLEN(disp);
352                  ip[2] *= (1.0 - v->vfore*d);
# Line 300 | Line 374 | FVECT  p
374                  if (d >= 1.0-FTINY)
375                          goto gotall;
376                  if (d <= -(1.0-FTINY))
377 <                        return(0);
377 >                        return(VL_BAD);
378                  ip[0] += DOT(disp,v->hvec)/((1. + d)*sqrt(v->hn2));
379                  ip[1] += DOT(disp,v->vvec)/((1. + d)*sqrt(v->vn2));
380                  goto gotall;
381          default:
382 <                return(0);
382 >                return(VL_BAD);
383          }
384          ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
385          ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
386 < gotall:                                 /* compute return value */
387 <        return( (1 - 2*(ip[2] <= 0.0)) * (1 +
388 <        ((0.0 >= ip[0]) | (ip[0] >= 1.0) | (0.0 >= ip[1]) | (ip[1] >= 1.0))) );
386 > gotall:                                 /* add appropriate return flags */
387 >        if (ip[2] <= 0.0)
388 >                rflags |= VL_BEHIND;
389 >        else if ((v->type != VT_PER) & (v->type != VT_CYL))
390 >                rflags |= VL_BEYOND*((v->vaft > FTINY) &
391 >                                        (ip[2] >= v->vaft - v->vfore));
392 >        rflags |= VL_OUTSIDE*((0.0 >= ip[0]) | (ip[0] >= 1.0) |
393 >                                (0.0 >= ip[1]) | (ip[1] >= 1.0));
394 >        return(rflags);
395   }
396  
397  
# Line 375 | Line 455 | int  ac,
455   char  *av[]
456   )
457   {
458 < #define check(c,l)      if ((av[0][c]&&av[0][c]!=' ') || \
458 > #define check(c,l)      if ((av[0][c]&&!isspace(av[0][c])) || \
459                          badarg(ac-1,av+1,l)) return(-1)
460  
461          if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
462                  return(-1);
463          switch (av[0][2]) {
464          case 't':                       /* type */
465 <                if (!av[0][3] || av[0][3]==' ')
465 >                if (!av[0][3] || isspace(av[0][3]))
466                          return(-1);
467                  check(4,"");
468                  v->type = av[0][3];
# Line 503 | Line 583 | VIEW  *vp
583                  sprintf(cp, " -vt%c", vp->type);
584                  cp += strlen(cp);
585          }
586 <        if (!VEQ(vp->vp,stdview.vp)) {
586 >        if (!VABSEQ(vp->vp,stdview.vp)) {
587                  sprintf(cp, " -vp %.6g %.6g %.6g",
588                                  vp->vp[0], vp->vp[1], vp->vp[2]);
589                  cp += strlen(cp);
590          }
591 <        if (!FEQ(vp->vdist,stdview.vdist) || !VEQ(vp->vdir,stdview.vdir)) {
591 >        if (!FABSEQ(vp->vdist,stdview.vdist) || !VABSEQ(vp->vdir,stdview.vdir)) {
592                  sprintf(cp, " -vd %.6g %.6g %.6g",
593                                  vp->vdir[0]*vp->vdist,
594                                  vp->vdir[1]*vp->vdist,
595                                  vp->vdir[2]*vp->vdist);
596                  cp += strlen(cp);
597          }
598 <        if (!VEQ(vp->vup,stdview.vup)) {
598 >        if (!VABSEQ(vp->vup,stdview.vup)) {
599                  sprintf(cp, " -vu %.6g %.6g %.6g",
600                                  vp->vup[0], vp->vup[1], vp->vup[2]);
601                  cp += strlen(cp);
602          }
603 <        if (!FEQ(vp->horiz,stdview.horiz)) {
603 >        if (!FABSEQ(vp->horiz,stdview.horiz)) {
604                  sprintf(cp, " -vh %.6g", vp->horiz);
605                  cp += strlen(cp);
606          }
607 <        if (!FEQ(vp->vert,stdview.vert)) {
607 >        if (!FABSEQ(vp->vert,stdview.vert)) {
608                  sprintf(cp, " -vv %.6g", vp->vert);
609                  cp += strlen(cp);
610          }
611 <        if (!FEQ(vp->vfore,stdview.vfore)) {
611 >        if (!FABSEQ(vp->vfore,stdview.vfore)) {
612                  sprintf(cp, " -vo %.6g", vp->vfore);
613                  cp += strlen(cp);
614          }
615 <        if (!FEQ(vp->vaft,stdview.vaft)) {
615 >        if (!FABSEQ(vp->vaft,stdview.vaft)) {
616                  sprintf(cp, " -va %.6g", vp->vaft);
617                  cp += strlen(cp);
618          }
619 <        if (!FEQ(vp->hoff,stdview.hoff)) {
619 >        if (!FABSEQ(vp->hoff,stdview.hoff)) {
620                  sprintf(cp, " -vs %.6g", vp->hoff);
621                  cp += strlen(cp);
622          }
623 <        if (!FEQ(vp->voff,stdview.voff)) {
623 >        if (!FABSEQ(vp->voff,stdview.voff)) {
624                  sprintf(cp, " -vl %.6g", vp->voff);
625                  cp += strlen(cp);
626          }
# Line 567 | Line 647 | char  *s
647          }
648                                          /* skip leading path */
649          cp = s;
650 <        while (*cp && *cp != ' ')
650 >        while (*cp && !isspace(*cp))
651                  cp++;
652          while (cp > s && !ISDIRSEP(cp[-1]))
653                  cp--;
# Line 619 | Line 699 | RESOLU  *rp
699          if (rp != NULL && !fgetsresolu(rp, fp))
700                  mvs.ok = 0;
701  
702 <        fclose(fp);
702 >        if (fp != stdin)
703 >                fclose(fp);
704  
705          return(mvs.ok);
706   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines