| 1 | greg | 1.1 | #ifndef lint | 
| 2 | greg | 2.56 | static const char       RCSid[] = "$Id: image.c,v 2.55 2022/07/16 00:26:34 greg Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  image.c - routines for image generation. | 
| 6 | greg | 2.16 | * | 
| 7 |  |  | *  External symbols declared in view.h | 
| 8 |  |  | */ | 
| 9 |  |  |  | 
| 10 | greg | 2.17 | #include "copyright.h" | 
| 11 | greg | 1.1 |  | 
| 12 | greg | 2.32 | #include  <ctype.h> | 
| 13 | schorsch | 2.22 | #include  "rtio.h" | 
| 14 | greg | 2.28 | #include  "rtmath.h" | 
| 15 | greg | 2.27 | #include  "paths.h" | 
| 16 | greg | 1.1 | #include  "view.h" | 
| 17 |  |  |  | 
| 18 | greg | 1.5 | VIEW  stdview = STDVIEW;                /* default view parameters */ | 
| 19 | greg | 1.1 |  | 
| 20 | schorsch | 2.26 | static gethfunc gethview; | 
| 21 |  |  |  | 
| 22 | greg | 1.1 |  | 
| 23 |  |  | char * | 
| 24 | greg | 2.35 | setview(                /* set hvec and vvec, return message on error */ | 
| 25 |  |  | VIEW  *v | 
| 26 |  |  | ) | 
| 27 | greg | 1.3 | { | 
| 28 | greg | 1.12 | static char  ill_horiz[] = "illegal horizontal view size"; | 
| 29 |  |  | static char  ill_vert[] = "illegal vertical view size"; | 
| 30 |  |  |  | 
| 31 | greg | 2.39 | if ((v->vfore < -FTINY) | (v->vaft < -FTINY) || | 
| 32 |  |  | (v->vaft > FTINY) & (v->vaft <= v->vfore)) | 
| 33 | greg | 2.7 | return("illegal fore/aft clipping plane"); | 
| 34 |  |  |  | 
| 35 | greg | 2.31 | if (v->vdist <= FTINY) | 
| 36 |  |  | return("illegal view distance"); | 
| 37 | greg | 2.29 | v->vdist *= normalize(v->vdir);         /* normalize direction */ | 
| 38 |  |  | if (v->vdist == 0.0) | 
| 39 | greg | 1.1 | return("zero view direction"); | 
| 40 |  |  |  | 
| 41 | greg | 1.14 | if (normalize(v->vup) == 0.0)           /* normalize view up */ | 
| 42 |  |  | return("zero view up vector"); | 
| 43 |  |  |  | 
| 44 | greg | 1.5 | fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */ | 
| 45 |  |  |  | 
| 46 |  |  | if (normalize(v->hvec) == 0.0) | 
| 47 | greg | 1.14 | return("view up parallel to view direction"); | 
| 48 | greg | 1.1 |  | 
| 49 | greg | 1.5 | fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */ | 
| 50 |  |  |  | 
| 51 | greg | 1.12 | if (v->horiz <= FTINY) | 
| 52 |  |  | return(ill_horiz); | 
| 53 |  |  | if (v->vert <= FTINY) | 
| 54 |  |  | return(ill_vert); | 
| 55 |  |  |  | 
| 56 |  |  | switch (v->type) { | 
| 57 |  |  | case VT_PAR:                            /* parallel view */ | 
| 58 | greg | 1.5 | v->hn2 = v->horiz; | 
| 59 | greg | 1.12 | v->vn2 = v->vert; | 
| 60 |  |  | break; | 
| 61 |  |  | case VT_PER:                            /* perspective view */ | 
| 62 |  |  | if (v->horiz >= 180.0-FTINY) | 
| 63 |  |  | return(ill_horiz); | 
| 64 |  |  | if (v->vert >= 180.0-FTINY) | 
| 65 |  |  | return(ill_vert); | 
| 66 | greg | 2.53 | v->hn2 = 2.0 * tan(v->horiz*(PI/360.)); | 
| 67 |  |  | v->vn2 = 2.0 * tan(v->vert*(PI/360.)); | 
| 68 | greg | 1.12 | break; | 
| 69 | greg | 2.9 | case VT_CYL:                            /* cylindrical panorama */ | 
| 70 |  |  | if (v->horiz > 360.0+FTINY) | 
| 71 |  |  | return(ill_horiz); | 
| 72 |  |  | if (v->vert >= 180.0-FTINY) | 
| 73 |  |  | return(ill_vert); | 
| 74 |  |  | v->hn2 = v->horiz * (PI/180.0); | 
| 75 | greg | 2.53 | v->vn2 = 2.0 * tan(v->vert*(PI/360.)); | 
| 76 | greg | 2.9 | break; | 
| 77 | greg | 1.12 | case VT_ANG:                            /* angular fisheye */ | 
| 78 |  |  | if (v->horiz > 360.0+FTINY) | 
| 79 |  |  | return(ill_horiz); | 
| 80 |  |  | if (v->vert > 360.0+FTINY) | 
| 81 |  |  | return(ill_vert); | 
| 82 | greg | 2.9 | v->hn2 = v->horiz * (PI/180.0); | 
| 83 |  |  | v->vn2 = v->vert * (PI/180.0); | 
| 84 | greg | 1.12 | break; | 
| 85 |  |  | case VT_HEM:                            /* hemispherical fisheye */ | 
| 86 |  |  | if (v->horiz > 180.0+FTINY) | 
| 87 |  |  | return(ill_horiz); | 
| 88 |  |  | if (v->vert > 180.0+FTINY) | 
| 89 |  |  | return(ill_vert); | 
| 90 | greg | 2.53 | v->hn2 = 2.0 * sin(v->horiz*(PI/360.)); | 
| 91 |  |  | v->vn2 = 2.0 * sin(v->vert*(PI/360.)); | 
| 92 | greg | 1.12 | break; | 
| 93 | greg | 2.34 | 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 | greg | 2.53 | 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 | greg | 2.34 | break; | 
| 103 | greg | 1.12 | default: | 
| 104 | greg | 1.1 | return("unknown view type"); | 
| 105 | greg | 1.12 | } | 
| 106 | greg | 2.34 | if (v->type != VT_ANG && v->type != VT_PLS) { | 
| 107 | greg | 2.9 | if (v->type != VT_CYL) { | 
| 108 |  |  | v->hvec[0] *= v->hn2; | 
| 109 |  |  | v->hvec[1] *= v->hn2; | 
| 110 |  |  | v->hvec[2] *= v->hn2; | 
| 111 |  |  | } | 
| 112 | greg | 1.12 | v->vvec[0] *= v->vn2; | 
| 113 |  |  | v->vvec[1] *= v->vn2; | 
| 114 |  |  | v->vvec[2] *= v->vn2; | 
| 115 |  |  | } | 
| 116 | greg | 1.5 | v->hn2 *= v->hn2; | 
| 117 |  |  | v->vn2 *= v->vn2; | 
| 118 | greg | 1.1 |  | 
| 119 |  |  | return(NULL); | 
| 120 |  |  | } | 
| 121 |  |  |  | 
| 122 |  |  |  | 
| 123 | greg | 2.53 | 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_hemi[] = "illegal crop for hemispherical view"; | 
| 133 |  |  | double  d; | 
| 134 |  |  | /* order crop extrema */ | 
| 135 |  |  | if (x0 > x1) { d=x0; x0=x1; x1=d; } | 
| 136 |  |  | if (y0 > y1) { d=y0; y0=y1; y1=d; } | 
| 137 |  |  |  | 
| 138 | greg | 2.54 | if ((x1-x0 <= FTINY) | (y1-y0 <= FTINY)) | 
| 139 |  |  | return("zero crop area"); | 
| 140 |  |  |  | 
| 141 |  |  | d = x1 - x0;                    /* adjust horizontal size? */ | 
| 142 |  |  | if (!FABSEQ(d, 1.)) | 
| 143 | greg | 2.53 | 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 | greg | 2.54 |  | 
| 165 |  |  | d = y1 - y0;                    /* adjust vertical size? */ | 
| 166 |  |  | if (!FABSEQ(d, 1.)) | 
| 167 | greg | 2.53 | switch (v->type) { | 
| 168 |  |  | case VT_PER: | 
| 169 |  |  | case VT_CYL: | 
| 170 |  |  | v->vert = 360./PI*atan( d*tan(PI/360.*v->vert) ); | 
| 171 |  |  | break; | 
| 172 |  |  | case VT_PAR: | 
| 173 |  |  | case VT_ANG: | 
| 174 |  |  | v->vert *= d; | 
| 175 |  |  | break; | 
| 176 |  |  | case VT_HEM: | 
| 177 |  |  | d *= sin(PI/360.*v->vert); | 
| 178 |  |  | if (d > 1.) | 
| 179 |  |  | return(ill_hemi); | 
| 180 |  |  | v->vert = 360./PI*asin( d ); | 
| 181 |  |  | break; | 
| 182 |  |  | case VT_PLS: | 
| 183 |  |  | d *= sin(PI/360.*v->vert) / | 
| 184 |  |  | (1. + cos(PI/360.*v->vert)); | 
| 185 |  |  | v->vert = 360./PI*acos( (1. - d*d) / (1. + d*d) ); | 
| 186 |  |  | break; | 
| 187 |  |  | } | 
| 188 | greg | 2.54 | /* adjust offsets */ | 
| 189 | greg | 2.53 | v->hoff = ((x0 + x1)*.5 - .5 + v->hoff) / (x1 - x0); | 
| 190 |  |  | v->voff = ((y0 + y1)*.5 - .5 + v->voff) / (y1 - y0); | 
| 191 |  |  |  | 
| 192 |  |  | return(setview(v));             /* final error checks & set-up */ | 
| 193 |  |  | } | 
| 194 |  |  |  | 
| 195 |  |  |  | 
| 196 | greg | 2.16 | void | 
| 197 | greg | 2.35 | normaspect(                             /* fix pixel aspect or resolution */ | 
| 198 |  |  | double  va,                     /* view aspect ratio */ | 
| 199 |  |  | double  *ap,                    /* pixel aspect in (or out if 0) */ | 
| 200 |  |  | int  *xp, | 
| 201 |  |  | int  *yp                        /* x and y resolution in (or out if *ap!=0) */ | 
| 202 |  |  | ) | 
| 203 | greg | 1.6 | { | 
| 204 |  |  | if (*ap <= FTINY) | 
| 205 | greg | 1.7 | *ap = va * *xp / *yp;           /* compute pixel aspect */ | 
| 206 | greg | 1.6 | else if (va * *xp > *ap * *yp) | 
| 207 | greg | 1.10 | *xp = *yp / va * *ap + .5;      /* reduce x resolution */ | 
| 208 | greg | 1.6 | else | 
| 209 | greg | 1.10 | *yp = *xp * va / *ap + .5;      /* reduce y resolution */ | 
| 210 | greg | 1.6 | } | 
| 211 |  |  |  | 
| 212 |  |  |  | 
| 213 | greg | 2.7 | double | 
| 214 | greg | 2.35 | viewray(                                /* compute ray origin and direction */ | 
| 215 |  |  | FVECT  orig, | 
| 216 |  |  | FVECT  direc, | 
| 217 |  |  | VIEW  *v, | 
| 218 |  |  | double  x, | 
| 219 |  |  | double  y | 
| 220 |  |  | ) | 
| 221 | greg | 1.1 | { | 
| 222 | greg | 1.12 | double  d, z; | 
| 223 |  |  |  | 
| 224 | greg | 1.5 | x += v->hoff - 0.5; | 
| 225 |  |  | y += v->voff - 0.5; | 
| 226 | greg | 1.1 |  | 
| 227 | greg | 1.12 | switch(v->type) { | 
| 228 |  |  | case VT_PAR:                    /* parallel view */ | 
| 229 | greg | 2.7 | orig[0] = v->vp[0] + v->vfore*v->vdir[0] | 
| 230 |  |  | + x*v->hvec[0] + y*v->vvec[0]; | 
| 231 |  |  | orig[1] = v->vp[1] + v->vfore*v->vdir[1] | 
| 232 |  |  | + x*v->hvec[1] + y*v->vvec[1]; | 
| 233 |  |  | orig[2] = v->vp[2] + v->vfore*v->vdir[2] | 
| 234 |  |  | + x*v->hvec[2] + y*v->vvec[2]; | 
| 235 | greg | 1.1 | VCOPY(direc, v->vdir); | 
| 236 | greg | 2.8 | return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); | 
| 237 | greg | 1.12 | case VT_PER:                    /* perspective view */ | 
| 238 | greg | 1.5 | direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 239 |  |  | direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 240 |  |  | direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 241 | greg | 2.40 | VSUM(orig, v->vp, direc, v->vfore); | 
| 242 | greg | 2.7 | d = normalize(direc); | 
| 243 | greg | 2.8 | return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); | 
| 244 | greg | 1.12 | case VT_HEM:                    /* hemispherical fisheye */ | 
| 245 | greg | 1.13 | z = 1.0 - x*x*v->hn2 - y*y*v->vn2; | 
| 246 | greg | 1.12 | if (z < 0.0) | 
| 247 | greg | 2.7 | return(-1.0); | 
| 248 | greg | 1.12 | z = sqrt(z); | 
| 249 |  |  | direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 250 |  |  | direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 251 |  |  | direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 252 | greg | 2.40 | VSUM(orig, v->vp, direc, v->vfore); | 
| 253 | greg | 2.8 | return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); | 
| 254 | greg | 2.9 | case VT_CYL:                    /* cylindrical panorama */ | 
| 255 |  |  | d = x * v->horiz * (PI/180.0); | 
| 256 |  |  | z = cos(d); | 
| 257 |  |  | x = sin(d); | 
| 258 |  |  | direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 259 |  |  | direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 260 |  |  | direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 261 | greg | 2.40 | VSUM(orig, v->vp, direc, v->vfore); | 
| 262 | greg | 2.9 | d = normalize(direc); | 
| 263 |  |  | return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); | 
| 264 | greg | 1.12 | case VT_ANG:                    /* angular fisheye */ | 
| 265 | greg | 2.34 | x *= (1.0/180.0)*v->horiz; | 
| 266 |  |  | y *= (1.0/180.0)*v->vert; | 
| 267 | greg | 1.12 | d = x*x + y*y; | 
| 268 |  |  | if (d > 1.0) | 
| 269 | greg | 2.7 | return(-1.0); | 
| 270 | greg | 1.12 | d = sqrt(d); | 
| 271 |  |  | z = cos(PI*d); | 
| 272 | greg | 2.34 | d = d <= FTINY ? PI : sqrt(1.0 - z*z)/d; | 
| 273 |  |  | x *= d; | 
| 274 |  |  | y *= d; | 
| 275 |  |  | direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 276 |  |  | direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 277 |  |  | direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 278 | greg | 2.40 | VSUM(orig, v->vp, direc, v->vfore); | 
| 279 | greg | 2.34 | return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); | 
| 280 |  |  | case VT_PLS:                    /* planispheric fisheye */ | 
| 281 |  |  | x *= sqrt(v->hn2); | 
| 282 |  |  | y *= sqrt(v->vn2); | 
| 283 |  |  | d = x*x + y*y; | 
| 284 |  |  | z = (1. - d)/(1. + d); | 
| 285 | greg | 2.38 | x *= (1. + z); | 
| 286 |  |  | y *= (1. + z); | 
| 287 | greg | 1.12 | direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 288 |  |  | direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 289 |  |  | direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 290 | greg | 2.40 | VSUM(orig, v->vp, direc, v->vfore); | 
| 291 | greg | 2.8 | return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); | 
| 292 | greg | 1.1 | } | 
| 293 | greg | 2.7 | return(-1.0); | 
| 294 | greg | 1.1 | } | 
| 295 |  |  |  | 
| 296 |  |  |  | 
| 297 | greg | 2.41 | int | 
| 298 | greg | 2.35 | viewloc(                        /* find image location for point */ | 
| 299 |  |  | FVECT  ip, | 
| 300 |  |  | VIEW  *v, | 
| 301 |  |  | FVECT  p | 
| 302 | greg | 2.46 | )       /* Use VL_* flags to interpret return value */ | 
| 303 | greg | 1.3 | { | 
| 304 | greg | 2.46 | int     rflags = VL_GOOD; | 
| 305 | gregl | 2.13 | double  d, d2; | 
| 306 | greg | 1.3 | FVECT  disp; | 
| 307 | greg | 1.5 |  | 
| 308 | greg | 2.34 | VSUB(disp, p, v->vp); | 
| 309 | greg | 1.3 |  | 
| 310 | greg | 1.12 | switch (v->type) { | 
| 311 |  |  | case VT_PAR:                    /* parallel view */ | 
| 312 | greg | 2.7 | ip[2] = DOT(disp,v->vdir) - v->vfore; | 
| 313 | greg | 1.13 | break; | 
| 314 | greg | 1.12 | case VT_PER:                    /* perspective view */ | 
| 315 | greg | 1.11 | d = DOT(disp,v->vdir); | 
| 316 | greg | 2.55 | rflags |= VL_BEYOND*((v->vaft > FTINY) & | 
| 317 |  |  | (d >= v->vaft)); | 
| 318 | gregl | 2.13 | ip[2] = VLEN(disp); | 
| 319 | greg | 2.42 | if (d < -FTINY) {       /* fold pyramid */ | 
| 320 | greg | 1.17 | ip[2] = -ip[2]; | 
| 321 | greg | 1.11 | d = -d; | 
| 322 | greg | 2.42 | } else if (d <= FTINY) | 
| 323 | greg | 2.46 | return(VL_BAD); /* at infinite edge */ | 
| 324 | greg | 2.41 | d = 1.0/d; | 
| 325 |  |  | disp[0] *= d; | 
| 326 |  |  | disp[1] *= d; | 
| 327 |  |  | disp[2] *= d; | 
| 328 | greg | 2.42 | if (ip[2] < 0.0) d = -d; | 
| 329 | greg | 2.7 | ip[2] *= (1.0 - v->vfore*d); | 
| 330 | greg | 1.13 | break; | 
| 331 | greg | 1.12 | case VT_HEM:                    /* hemispherical fisheye */ | 
| 332 |  |  | d = normalize(disp); | 
| 333 | greg | 1.17 | if (DOT(disp,v->vdir) < 0.0) | 
| 334 |  |  | ip[2] = -d; | 
| 335 |  |  | else | 
| 336 |  |  | ip[2] = d; | 
| 337 | greg | 2.7 | ip[2] -= v->vfore; | 
| 338 | greg | 1.13 | break; | 
| 339 | greg | 2.9 | case VT_CYL:                    /* cylindrical panorama */ | 
| 340 |  |  | d = DOT(disp,v->hvec); | 
| 341 | gregl | 2.13 | d2 = DOT(disp,v->vdir); | 
| 342 |  |  | ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff; | 
| 343 | greg | 2.51 | d2 = d*d + d2*d2; | 
| 344 |  |  | if (d2 <= FTINY*FTINY) | 
| 345 | greg | 2.46 | return(VL_BAD); /* at pole */ | 
| 346 | greg | 2.55 | rflags |= VL_BEYOND*((v->vaft > FTINY) & | 
| 347 |  |  | (d2 >= v->vaft*v->vaft)); | 
| 348 | greg | 2.51 | d = 1.0/sqrt(d2); | 
| 349 | gregl | 2.13 | ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff; | 
| 350 |  |  | ip[2] = VLEN(disp); | 
| 351 | gwlarson | 2.15 | ip[2] *= (1.0 - v->vfore*d); | 
| 352 | greg | 2.42 | goto gotall; | 
| 353 | greg | 1.12 | case VT_ANG:                    /* angular fisheye */ | 
| 354 | greg | 1.17 | ip[0] = 0.5 - v->hoff; | 
| 355 |  |  | ip[1] = 0.5 - v->voff; | 
| 356 | greg | 2.7 | ip[2] = normalize(disp) - v->vfore; | 
| 357 | greg | 1.12 | d = DOT(disp,v->vdir); | 
| 358 |  |  | if (d >= 1.0-FTINY) | 
| 359 | greg | 2.42 | goto gotall; | 
| 360 | greg | 1.12 | if (d <= -(1.0-FTINY)) { | 
| 361 | greg | 2.7 | ip[0] += 180.0/v->horiz; | 
| 362 | greg | 2.42 | goto gotall; | 
| 363 | greg | 1.12 | } | 
| 364 | greg | 2.34 | d = (180.0/PI)*acos(d) / sqrt(1.0 - d*d); | 
| 365 |  |  | ip[0] += DOT(disp,v->hvec)*d/v->horiz; | 
| 366 |  |  | ip[1] += DOT(disp,v->vvec)*d/v->vert; | 
| 367 | greg | 2.42 | goto gotall; | 
| 368 | greg | 2.34 | case VT_PLS:                    /* planispheric fisheye */ | 
| 369 |  |  | ip[0] = 0.5 - v->hoff; | 
| 370 |  |  | ip[1] = 0.5 - v->voff; | 
| 371 |  |  | ip[2] = normalize(disp) - v->vfore; | 
| 372 |  |  | d = DOT(disp,v->vdir); | 
| 373 |  |  | if (d >= 1.0-FTINY) | 
| 374 | greg | 2.42 | goto gotall; | 
| 375 | greg | 2.34 | if (d <= -(1.0-FTINY)) | 
| 376 | greg | 2.46 | return(VL_BAD); | 
| 377 | greg | 2.38 | ip[0] += DOT(disp,v->hvec)/((1. + d)*sqrt(v->hn2)); | 
| 378 |  |  | ip[1] += DOT(disp,v->vvec)/((1. + d)*sqrt(v->vn2)); | 
| 379 | greg | 2.42 | goto gotall; | 
| 380 | greg | 2.45 | default: | 
| 381 | greg | 2.46 | return(VL_BAD); | 
| 382 | greg | 1.3 | } | 
| 383 | greg | 1.17 | ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; | 
| 384 |  |  | ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; | 
| 385 | greg | 2.46 | gotall:                                 /* add appropriate return flags */ | 
| 386 | greg | 2.48 | if (ip[2] <= 0.0) | 
| 387 |  |  | rflags |= VL_BEHIND; | 
| 388 |  |  | else if ((v->type != VT_PER) & (v->type != VT_CYL)) | 
| 389 |  |  | rflags |= VL_BEYOND*((v->vaft > FTINY) & | 
| 390 |  |  | (ip[2] >= v->vaft - v->vfore)); | 
| 391 | greg | 2.46 | rflags |= VL_OUTSIDE*((0.0 >= ip[0]) | (ip[0] >= 1.0) | | 
| 392 |  |  | (0.0 >= ip[1]) | (ip[1] >= 1.0)); | 
| 393 |  |  | return(rflags); | 
| 394 | greg | 1.3 | } | 
| 395 |  |  |  | 
| 396 |  |  |  | 
| 397 | greg | 2.16 | void | 
| 398 | greg | 2.35 | pix2loc(                /* compute image location from pixel pos. */ | 
| 399 |  |  | RREAL  loc[2], | 
| 400 |  |  | RESOLU  *rp, | 
| 401 |  |  | int  px, | 
| 402 |  |  | int  py | 
| 403 |  |  | ) | 
| 404 | greg | 1.17 | { | 
| 405 | greg | 2.36 | int  x, y; | 
| 406 | greg | 1.17 |  | 
| 407 | greg | 2.16 | if (rp->rt & YMAJOR) { | 
| 408 | greg | 1.17 | x = px; | 
| 409 |  |  | y = py; | 
| 410 |  |  | } else { | 
| 411 |  |  | x = py; | 
| 412 |  |  | y = px; | 
| 413 |  |  | } | 
| 414 | greg | 2.16 | if (rp->rt & XDECR) | 
| 415 | greg | 1.17 | x = rp->xr-1 - x; | 
| 416 | greg | 2.16 | if (rp->rt & YDECR) | 
| 417 | greg | 1.17 | y = rp->yr-1 - y; | 
| 418 |  |  | loc[0] = (x+.5)/rp->xr; | 
| 419 |  |  | loc[1] = (y+.5)/rp->yr; | 
| 420 |  |  | } | 
| 421 |  |  |  | 
| 422 |  |  |  | 
| 423 | greg | 2.16 | void | 
| 424 | greg | 2.35 | loc2pix(                        /* compute pixel pos. from image location */ | 
| 425 |  |  | int  pp[2], | 
| 426 |  |  | RESOLU  *rp, | 
| 427 |  |  | double  lx, | 
| 428 |  |  | double  ly | 
| 429 |  |  | ) | 
| 430 | greg | 1.17 | { | 
| 431 | greg | 2.36 | int  x, y; | 
| 432 | greg | 1.17 |  | 
| 433 | greg | 2.37 | x = (int)(lx*rp->xr + .5 - (lx < 0.0)); | 
| 434 |  |  | y = (int)(ly*rp->yr + .5 - (ly < 0.0)); | 
| 435 |  |  |  | 
| 436 | greg | 2.16 | if (rp->rt & XDECR) | 
| 437 | greg | 1.17 | x = rp->xr-1 - x; | 
| 438 | greg | 2.16 | if (rp->rt & YDECR) | 
| 439 | greg | 1.17 | y = rp->yr-1 - y; | 
| 440 | greg | 2.16 | if (rp->rt & YMAJOR) { | 
| 441 | greg | 1.17 | pp[0] = x; | 
| 442 |  |  | pp[1] = y; | 
| 443 |  |  | } else { | 
| 444 |  |  | pp[0] = y; | 
| 445 |  |  | pp[1] = x; | 
| 446 |  |  | } | 
| 447 |  |  | } | 
| 448 |  |  |  | 
| 449 |  |  |  | 
| 450 | greg | 1.5 | int | 
| 451 | greg | 2.35 | getviewopt(                             /* process view argument */ | 
| 452 |  |  | VIEW  *v, | 
| 453 |  |  | int  ac, | 
| 454 |  |  | char  *av[] | 
| 455 |  |  | ) | 
| 456 | greg | 1.5 | { | 
| 457 | greg | 2.49 | #define check(c,l)      if ((av[0][c]&&!isspace(av[0][c])) || \ | 
| 458 | greg | 1.16 | badarg(ac-1,av+1,l)) return(-1) | 
| 459 | greg | 1.5 |  | 
| 460 | greg | 1.9 | if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v') | 
| 461 | greg | 1.5 | return(-1); | 
| 462 |  |  | switch (av[0][2]) { | 
| 463 |  |  | case 't':                       /* type */ | 
| 464 | greg | 2.49 | if (!av[0][3] || isspace(av[0][3])) | 
| 465 | greg | 1.5 | return(-1); | 
| 466 | greg | 1.16 | check(4,""); | 
| 467 | greg | 1.5 | v->type = av[0][3]; | 
| 468 |  |  | return(0); | 
| 469 |  |  | case 'p':                       /* point */ | 
| 470 | greg | 1.16 | check(3,"fff"); | 
| 471 | greg | 1.5 | v->vp[0] = atof(av[1]); | 
| 472 |  |  | v->vp[1] = atof(av[2]); | 
| 473 |  |  | v->vp[2] = atof(av[3]); | 
| 474 |  |  | return(3); | 
| 475 |  |  | case 'd':                       /* direction */ | 
| 476 | greg | 1.16 | check(3,"fff"); | 
| 477 | greg | 1.5 | v->vdir[0] = atof(av[1]); | 
| 478 |  |  | v->vdir[1] = atof(av[2]); | 
| 479 |  |  | v->vdir[2] = atof(av[3]); | 
| 480 | greg | 2.30 | v->vdist = 1.; | 
| 481 | greg | 1.5 | return(3); | 
| 482 |  |  | case 'u':                       /* up */ | 
| 483 | greg | 1.16 | check(3,"fff"); | 
| 484 | greg | 1.5 | v->vup[0] = atof(av[1]); | 
| 485 |  |  | v->vup[1] = atof(av[2]); | 
| 486 |  |  | v->vup[2] = atof(av[3]); | 
| 487 |  |  | return(3); | 
| 488 |  |  | case 'h':                       /* horizontal size */ | 
| 489 | greg | 1.16 | check(3,"f"); | 
| 490 | greg | 1.5 | v->horiz = atof(av[1]); | 
| 491 |  |  | return(1); | 
| 492 |  |  | case 'v':                       /* vertical size */ | 
| 493 | greg | 1.16 | check(3,"f"); | 
| 494 | greg | 1.5 | v->vert = atof(av[1]); | 
| 495 |  |  | return(1); | 
| 496 | greg | 2.7 | case 'o':                       /* fore clipping plane */ | 
| 497 |  |  | check(3,"f"); | 
| 498 |  |  | v->vfore = atof(av[1]); | 
| 499 |  |  | return(1); | 
| 500 |  |  | case 'a':                       /* aft clipping plane */ | 
| 501 |  |  | check(3,"f"); | 
| 502 |  |  | v->vaft = atof(av[1]); | 
| 503 |  |  | return(1); | 
| 504 | greg | 1.5 | case 's':                       /* shift */ | 
| 505 | greg | 1.16 | check(3,"f"); | 
| 506 | greg | 1.5 | v->hoff = atof(av[1]); | 
| 507 |  |  | return(1); | 
| 508 |  |  | case 'l':                       /* lift */ | 
| 509 | greg | 1.16 | check(3,"f"); | 
| 510 | greg | 1.5 | v->voff = atof(av[1]); | 
| 511 |  |  | return(1); | 
| 512 |  |  | default: | 
| 513 |  |  | return(-1); | 
| 514 |  |  | } | 
| 515 |  |  | #undef check | 
| 516 |  |  | } | 
| 517 |  |  |  | 
| 518 |  |  |  | 
| 519 |  |  | int | 
| 520 | greg | 2.35 | sscanview(                              /* get view parameters from string */ | 
| 521 |  |  | VIEW  *vp, | 
| 522 |  |  | char  *s | 
| 523 |  |  | ) | 
| 524 | greg | 1.1 | { | 
| 525 | greg | 1.5 | int  ac; | 
| 526 |  |  | char  *av[4]; | 
| 527 |  |  | int  na; | 
| 528 |  |  | int  nvopts = 0; | 
| 529 |  |  |  | 
| 530 | greg | 2.32 | while (isspace(*s)) | 
| 531 |  |  | if (!*s++) | 
| 532 |  |  | return(0); | 
| 533 | greg | 1.9 | while (*s) { | 
| 534 | greg | 1.5 | ac = 0; | 
| 535 |  |  | do { | 
| 536 | greg | 2.21 | if (ac || *s == '-') | 
| 537 |  |  | av[ac++] = s; | 
| 538 | greg | 2.32 | while (*s && !isspace(*s)) | 
| 539 | greg | 1.5 | s++; | 
| 540 | greg | 2.32 | while (isspace(*s)) | 
| 541 | greg | 1.5 | s++; | 
| 542 |  |  | } while (*s && ac < 4); | 
| 543 |  |  | if ((na = getviewopt(vp, ac, av)) >= 0) { | 
| 544 |  |  | if (na+1 < ac) | 
| 545 |  |  | s = av[na+1]; | 
| 546 |  |  | nvopts++; | 
| 547 | greg | 1.9 | } else if (ac > 1) | 
| 548 |  |  | s = av[1]; | 
| 549 |  |  | } | 
| 550 | greg | 1.5 | return(nvopts); | 
| 551 | greg | 1.1 | } | 
| 552 |  |  |  | 
| 553 |  |  |  | 
| 554 | greg | 2.16 | void | 
| 555 | greg | 2.35 | fprintview(                             /* write out view parameters */ | 
| 556 |  |  | VIEW  *vp, | 
| 557 |  |  | FILE  *fp | 
| 558 |  |  | ) | 
| 559 | greg | 1.1 | { | 
| 560 |  |  | fprintf(fp, " -vt%c", vp->type); | 
| 561 |  |  | fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]); | 
| 562 | greg | 2.29 | fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0]*vp->vdist, | 
| 563 |  |  | vp->vdir[1]*vp->vdist, | 
| 564 |  |  | vp->vdir[2]*vp->vdist); | 
| 565 | greg | 1.1 | fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); | 
| 566 |  |  | fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); | 
| 567 | greg | 2.7 | fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft); | 
| 568 | greg | 1.9 | fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff); | 
| 569 | greg | 2.11 | } | 
| 570 |  |  |  | 
| 571 |  |  |  | 
| 572 |  |  | char * | 
| 573 | greg | 2.35 | viewopt(                                /* translate to minimal view string */ | 
| 574 |  |  | VIEW  *vp | 
| 575 |  |  | ) | 
| 576 | greg | 2.11 | { | 
| 577 |  |  | static char  vwstr[128]; | 
| 578 | greg | 2.36 | char  *cp = vwstr; | 
| 579 | greg | 2.11 |  | 
| 580 | greg | 2.33 | *cp = '\0'; | 
| 581 | greg | 2.11 | if (vp->type != stdview.type) { | 
| 582 |  |  | sprintf(cp, " -vt%c", vp->type); | 
| 583 |  |  | cp += strlen(cp); | 
| 584 |  |  | } | 
| 585 | greg | 2.52 | if (!VABSEQ(vp->vp,stdview.vp)) { | 
| 586 | greg | 2.11 | sprintf(cp, " -vp %.6g %.6g %.6g", | 
| 587 |  |  | vp->vp[0], vp->vp[1], vp->vp[2]); | 
| 588 |  |  | cp += strlen(cp); | 
| 589 |  |  | } | 
| 590 | greg | 2.52 | if (!FABSEQ(vp->vdist,stdview.vdist) || !VABSEQ(vp->vdir,stdview.vdir)) { | 
| 591 | greg | 2.11 | sprintf(cp, " -vd %.6g %.6g %.6g", | 
| 592 | greg | 2.29 | vp->vdir[0]*vp->vdist, | 
| 593 |  |  | vp->vdir[1]*vp->vdist, | 
| 594 |  |  | vp->vdir[2]*vp->vdist); | 
| 595 | greg | 2.11 | cp += strlen(cp); | 
| 596 |  |  | } | 
| 597 | greg | 2.52 | if (!VABSEQ(vp->vup,stdview.vup)) { | 
| 598 | greg | 2.11 | sprintf(cp, " -vu %.6g %.6g %.6g", | 
| 599 |  |  | vp->vup[0], vp->vup[1], vp->vup[2]); | 
| 600 |  |  | cp += strlen(cp); | 
| 601 |  |  | } | 
| 602 | greg | 2.52 | if (!FABSEQ(vp->horiz,stdview.horiz)) { | 
| 603 | greg | 2.11 | sprintf(cp, " -vh %.6g", vp->horiz); | 
| 604 |  |  | cp += strlen(cp); | 
| 605 |  |  | } | 
| 606 | greg | 2.52 | if (!FABSEQ(vp->vert,stdview.vert)) { | 
| 607 | greg | 2.11 | sprintf(cp, " -vv %.6g", vp->vert); | 
| 608 |  |  | cp += strlen(cp); | 
| 609 |  |  | } | 
| 610 | greg | 2.52 | if (!FABSEQ(vp->vfore,stdview.vfore)) { | 
| 611 | greg | 2.11 | sprintf(cp, " -vo %.6g", vp->vfore); | 
| 612 |  |  | cp += strlen(cp); | 
| 613 |  |  | } | 
| 614 | greg | 2.52 | if (!FABSEQ(vp->vaft,stdview.vaft)) { | 
| 615 | greg | 2.11 | sprintf(cp, " -va %.6g", vp->vaft); | 
| 616 |  |  | cp += strlen(cp); | 
| 617 |  |  | } | 
| 618 | greg | 2.52 | if (!FABSEQ(vp->hoff,stdview.hoff)) { | 
| 619 | greg | 2.11 | sprintf(cp, " -vs %.6g", vp->hoff); | 
| 620 |  |  | cp += strlen(cp); | 
| 621 |  |  | } | 
| 622 | greg | 2.52 | if (!FABSEQ(vp->voff,stdview.voff)) { | 
| 623 | greg | 2.11 | sprintf(cp, " -vl %.6g", vp->voff); | 
| 624 |  |  | cp += strlen(cp); | 
| 625 |  |  | } | 
| 626 |  |  | return(vwstr); | 
| 627 | greg | 1.1 | } | 
| 628 |  |  |  | 
| 629 |  |  |  | 
| 630 | greg | 2.3 | int | 
| 631 | greg | 2.35 | isview(                                 /* is this a view string? */ | 
| 632 |  |  | char  *s | 
| 633 |  |  | ) | 
| 634 | greg | 2.3 | { | 
| 635 | greg | 2.25 | static char  *altname[]={NULL,VIEWSTR,"rpict","rview","rvu","rpiece","pinterp",NULL}; | 
| 636 | greg | 2.36 | char  *cp; | 
| 637 |  |  | char  **an; | 
| 638 | greg | 2.3 | /* add program name to list */ | 
| 639 | greg | 2.5 | if (altname[0] == NULL) { | 
| 640 |  |  | for (cp = progname; *cp; cp++) | 
| 641 |  |  | ; | 
| 642 |  |  | while (cp > progname && !ISDIRSEP(cp[-1])) | 
| 643 |  |  | cp--; | 
| 644 |  |  | altname[0] = cp; | 
| 645 |  |  | } | 
| 646 | greg | 2.3 | /* skip leading path */ | 
| 647 |  |  | cp = s; | 
| 648 | greg | 2.49 | while (*cp && !isspace(*cp)) | 
| 649 | greg | 2.3 | cp++; | 
| 650 | greg | 2.5 | while (cp > s && !ISDIRSEP(cp[-1])) | 
| 651 | greg | 2.3 | cp--; | 
| 652 |  |  | for (an = altname; *an != NULL; an++) | 
| 653 |  |  | if (!strncmp(*an, cp, strlen(*an))) | 
| 654 |  |  | return(1); | 
| 655 |  |  | return(0); | 
| 656 |  |  | } | 
| 657 | greg | 1.1 |  | 
| 658 | greg | 2.3 |  | 
| 659 | greg | 1.15 | struct myview { | 
| 660 |  |  | VIEW    *hv; | 
| 661 |  |  | int     ok; | 
| 662 |  |  | }; | 
| 663 | greg | 1.1 |  | 
| 664 | greg | 1.15 |  | 
| 665 | gwlarson | 2.14 | static int | 
| 666 | schorsch | 2.26 | gethview(                               /* get view from header */ | 
| 667 |  |  | char  *s, | 
| 668 |  |  | void  *v | 
| 669 |  |  | ) | 
| 670 | greg | 1.1 | { | 
| 671 | schorsch | 2.26 | if (isview(s) && sscanview(((struct myview*)v)->hv, s) > 0) | 
| 672 |  |  | ((struct myview*)v)->ok++; | 
| 673 | gwlarson | 2.14 | return(0); | 
| 674 | greg | 1.1 | } | 
| 675 |  |  |  | 
| 676 |  |  |  | 
| 677 |  |  | int | 
| 678 | greg | 2.35 | viewfile(                               /* get view from file */ | 
| 679 |  |  | char  *fname, | 
| 680 |  |  | VIEW  *vp, | 
| 681 |  |  | RESOLU  *rp | 
| 682 |  |  | ) | 
| 683 | greg | 1.1 | { | 
| 684 | greg | 1.15 | struct myview   mvs; | 
| 685 | greg | 1.1 | FILE  *fp; | 
| 686 |  |  |  | 
| 687 | greg | 2.16 | if (fname == NULL || !strcmp(fname, "-")) | 
| 688 |  |  | fp = stdin; | 
| 689 |  |  | else if ((fp = fopen(fname, "r")) == NULL) | 
| 690 | greg | 1.1 | return(-1); | 
| 691 |  |  |  | 
| 692 | greg | 1.15 | mvs.hv = vp; | 
| 693 |  |  | mvs.ok = 0; | 
| 694 | greg | 1.1 |  | 
| 695 | schorsch | 2.26 | getheader(fp, gethview, &mvs); | 
| 696 | greg | 1.8 |  | 
| 697 | greg | 1.17 | if (rp != NULL && !fgetsresolu(rp, fp)) | 
| 698 | greg | 1.15 | mvs.ok = 0; | 
| 699 | greg | 1.1 |  | 
| 700 | greg | 2.50 | if (fp != stdin) | 
| 701 |  |  | fclose(fp); | 
| 702 | greg | 1.1 |  | 
| 703 | greg | 1.15 | return(mvs.ok); | 
| 704 | greg | 1.1 | } |