| 1 | greg | 1.1 | /* Copyright (c) 1986 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  image.c - routines for image generation. | 
| 9 |  |  | * | 
| 10 |  |  | *     10/17/85 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include  "standard.h" | 
| 14 |  |  |  | 
| 15 |  |  | #include  "view.h" | 
| 16 |  |  |  | 
| 17 | greg | 1.5 | VIEW  stdview = STDVIEW;                /* default view parameters */ | 
| 18 | greg | 1.1 |  | 
| 19 |  |  |  | 
| 20 |  |  | char * | 
| 21 | greg | 1.5 | setview(v)              /* set hvec and vvec, return message on error */ | 
| 22 |  |  | register VIEW  *v; | 
| 23 | greg | 1.3 | { | 
| 24 | greg | 1.5 | extern double  tan(), normalize(); | 
| 25 | greg | 1.3 |  | 
| 26 | greg | 1.1 | if (normalize(v->vdir) == 0.0)          /* normalize direction */ | 
| 27 |  |  | return("zero view direction"); | 
| 28 |  |  |  | 
| 29 | greg | 1.5 | fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */ | 
| 30 |  |  |  | 
| 31 |  |  | if (normalize(v->hvec) == 0.0) | 
| 32 | greg | 1.1 | return("illegal view up vector"); | 
| 33 |  |  |  | 
| 34 | greg | 1.5 | fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */ | 
| 35 |  |  |  | 
| 36 | greg | 1.1 | if (v->type == VT_PAR) | 
| 37 | greg | 1.5 | v->hn2 = v->horiz; | 
| 38 | greg | 1.1 | else if (v->type == VT_PER) | 
| 39 | greg | 1.5 | v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0)); | 
| 40 | greg | 1.1 | else | 
| 41 |  |  | return("unknown view type"); | 
| 42 |  |  |  | 
| 43 | greg | 1.5 | if (v->hn2 <= FTINY || v->hn2 >= FHUGE) | 
| 44 | greg | 1.1 | return("illegal horizontal view size"); | 
| 45 |  |  |  | 
| 46 | greg | 1.5 | v->hvec[0] *= v->hn2; | 
| 47 |  |  | v->hvec[1] *= v->hn2; | 
| 48 |  |  | v->hvec[2] *= v->hn2; | 
| 49 |  |  | v->hn2 *= v->hn2; | 
| 50 | greg | 1.1 |  | 
| 51 |  |  | if (v->type == VT_PAR) | 
| 52 | greg | 1.5 | v->vn2 = v->vert; | 
| 53 | greg | 1.1 | else | 
| 54 | greg | 1.5 | v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); | 
| 55 | greg | 1.1 |  | 
| 56 | greg | 1.5 | if (v->vn2 <= FTINY || v->vn2 >= FHUGE) | 
| 57 | greg | 1.1 | return("illegal vertical view size"); | 
| 58 |  |  |  | 
| 59 | greg | 1.5 | v->vvec[0] *= v->vn2; | 
| 60 |  |  | v->vvec[1] *= v->vn2; | 
| 61 |  |  | v->vvec[2] *= v->vn2; | 
| 62 |  |  | v->vn2 *= v->vn2; | 
| 63 | greg | 1.1 |  | 
| 64 |  |  | return(NULL); | 
| 65 |  |  | } | 
| 66 |  |  |  | 
| 67 |  |  |  | 
| 68 | greg | 1.7 | normaspect(va, ap, xp, yp)              /* fix pixel aspect or resolution */ | 
| 69 |  |  | double  va;                     /* view aspect ratio */ | 
| 70 |  |  | double  *ap;                    /* pixel aspect in (or out if 0) */ | 
| 71 |  |  | int  *xp, *yp;                  /* x and y resolution in (or out if *ap!=0) */ | 
| 72 | greg | 1.6 | { | 
| 73 |  |  | if (*ap <= FTINY) | 
| 74 | greg | 1.7 | *ap = va * *xp / *yp;           /* compute pixel aspect */ | 
| 75 | greg | 1.6 | else if (va * *xp > *ap * *yp) | 
| 76 |  |  | *xp = *yp / va * *ap;           /* reduce x resolution */ | 
| 77 |  |  | else | 
| 78 |  |  | *yp = *xp * va / *ap;           /* reduce y resolution */ | 
| 79 |  |  | } | 
| 80 |  |  |  | 
| 81 |  |  |  | 
| 82 | greg | 1.5 | viewray(orig, direc, v, x, y)           /* compute ray origin and direction */ | 
| 83 | greg | 1.1 | FVECT  orig, direc; | 
| 84 |  |  | register VIEW  *v; | 
| 85 |  |  | double  x, y; | 
| 86 |  |  | { | 
| 87 | greg | 1.5 | x += v->hoff - 0.5; | 
| 88 |  |  | y += v->voff - 0.5; | 
| 89 | greg | 1.1 |  | 
| 90 |  |  | if (v->type == VT_PAR) {        /* parallel view */ | 
| 91 | greg | 1.5 | orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 92 |  |  | orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 93 |  |  | orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 94 | greg | 1.1 | VCOPY(direc, v->vdir); | 
| 95 |  |  | } else {                        /* perspective view */ | 
| 96 |  |  | VCOPY(orig, v->vp); | 
| 97 | greg | 1.5 | direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; | 
| 98 |  |  | direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; | 
| 99 |  |  | direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; | 
| 100 | greg | 1.1 | normalize(direc); | 
| 101 |  |  | } | 
| 102 |  |  | } | 
| 103 |  |  |  | 
| 104 |  |  |  | 
| 105 | greg | 1.5 | viewpixel(xp, yp, zp, v, p)             /* find image location for point */ | 
| 106 | greg | 1.3 | double  *xp, *yp, *zp; | 
| 107 |  |  | register VIEW  *v; | 
| 108 |  |  | FVECT  p; | 
| 109 |  |  | { | 
| 110 |  |  | extern double  sqrt(); | 
| 111 |  |  | double  d; | 
| 112 |  |  | FVECT  disp; | 
| 113 | greg | 1.5 |  | 
| 114 | greg | 1.3 | disp[0] = p[0] - v->vp[0]; | 
| 115 |  |  | disp[1] = p[1] - v->vp[1]; | 
| 116 |  |  | disp[2] = p[2] - v->vp[2]; | 
| 117 |  |  |  | 
| 118 |  |  | if (v->type == VT_PAR) {        /* parallel view */ | 
| 119 |  |  | if (zp != NULL) | 
| 120 |  |  | *zp = DOT(disp,v->vdir); | 
| 121 |  |  | } else {                        /* perspective view */ | 
| 122 |  |  | d = 1.0/DOT(disp,v->vdir); | 
| 123 |  |  | if (zp != NULL) { | 
| 124 |  |  | *zp = sqrt(DOT(disp,disp)); | 
| 125 |  |  | if (d < 0.0) | 
| 126 |  |  | *zp = -*zp; | 
| 127 |  |  | } | 
| 128 |  |  | disp[0] *= d; | 
| 129 |  |  | disp[1] *= d; | 
| 130 |  |  | disp[2] *= d; | 
| 131 |  |  | } | 
| 132 | greg | 1.5 | *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; | 
| 133 |  |  | *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; | 
| 134 | greg | 1.3 | } | 
| 135 |  |  |  | 
| 136 |  |  |  | 
| 137 | greg | 1.5 | int | 
| 138 |  |  | getviewopt(v, ac, av)                   /* process view argument */ | 
| 139 |  |  | register VIEW  *v; | 
| 140 |  |  | int  ac; | 
| 141 |  |  | register char  *av[]; | 
| 142 |  |  | { | 
| 143 |  |  | #define check(c,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1) | 
| 144 |  |  | extern double  atof(); | 
| 145 |  |  |  | 
| 146 |  |  | if (av[0][0] != '-' || av[0][1] != 'v') | 
| 147 |  |  | return(-1); | 
| 148 |  |  | switch (av[0][2]) { | 
| 149 |  |  | case 't':                       /* type */ | 
| 150 |  |  | if (!av[0][3] || av[0][3]==' ') | 
| 151 |  |  | return(-1); | 
| 152 |  |  | check(4,0); | 
| 153 |  |  | v->type = av[0][3]; | 
| 154 |  |  | return(0); | 
| 155 |  |  | case 'p':                       /* point */ | 
| 156 |  |  | check(3,3); | 
| 157 |  |  | v->vp[0] = atof(av[1]); | 
| 158 |  |  | v->vp[1] = atof(av[2]); | 
| 159 |  |  | v->vp[2] = atof(av[3]); | 
| 160 |  |  | return(3); | 
| 161 |  |  | case 'd':                       /* direction */ | 
| 162 |  |  | check(3,3); | 
| 163 |  |  | v->vdir[0] = atof(av[1]); | 
| 164 |  |  | v->vdir[1] = atof(av[2]); | 
| 165 |  |  | v->vdir[2] = atof(av[3]); | 
| 166 |  |  | return(3); | 
| 167 |  |  | case 'u':                       /* up */ | 
| 168 |  |  | check(3,3); | 
| 169 |  |  | v->vup[0] = atof(av[1]); | 
| 170 |  |  | v->vup[1] = atof(av[2]); | 
| 171 |  |  | v->vup[2] = atof(av[3]); | 
| 172 |  |  | return(3); | 
| 173 |  |  | case 'h':                       /* horizontal size */ | 
| 174 |  |  | check(3,1); | 
| 175 |  |  | v->horiz = atof(av[1]); | 
| 176 |  |  | return(1); | 
| 177 |  |  | case 'v':                       /* vertical size */ | 
| 178 |  |  | check(3,1); | 
| 179 |  |  | v->vert = atof(av[1]); | 
| 180 |  |  | return(1); | 
| 181 |  |  | case 's':                       /* shift */ | 
| 182 |  |  | check(3,1); | 
| 183 |  |  | v->hoff = atof(av[1]); | 
| 184 |  |  | return(1); | 
| 185 |  |  | case 'l':                       /* lift */ | 
| 186 |  |  | check(3,1); | 
| 187 |  |  | v->voff = atof(av[1]); | 
| 188 |  |  | return(1); | 
| 189 |  |  | default: | 
| 190 |  |  | return(-1); | 
| 191 |  |  | } | 
| 192 |  |  | #undef check | 
| 193 |  |  | } | 
| 194 |  |  |  | 
| 195 |  |  |  | 
| 196 |  |  | int | 
| 197 |  |  | sscanview(vp, s)                        /* get view parameters from string */ | 
| 198 |  |  | VIEW  *vp; | 
| 199 | greg | 1.1 | register char  *s; | 
| 200 |  |  | { | 
| 201 | greg | 1.5 | int  ac; | 
| 202 |  |  | char  *av[4]; | 
| 203 |  |  | int  na; | 
| 204 |  |  | int  nvopts = 0; | 
| 205 |  |  |  | 
| 206 |  |  | while (*s == ' ') | 
| 207 |  |  | s++; | 
| 208 |  |  | do { | 
| 209 |  |  | ac = 0; | 
| 210 |  |  | do { | 
| 211 |  |  | av[ac++] = s; | 
| 212 |  |  | while (*s && *s != ' ') | 
| 213 |  |  | s++; | 
| 214 |  |  | while (*s == ' ') | 
| 215 |  |  | s++; | 
| 216 |  |  | } while (*s && ac < 4); | 
| 217 |  |  | if ((na = getviewopt(vp, ac, av)) >= 0) { | 
| 218 |  |  | if (na+1 < ac) | 
| 219 |  |  | s = av[na+1]; | 
| 220 |  |  | nvopts++; | 
| 221 | greg | 1.1 | } | 
| 222 | greg | 1.5 | } while (*s); | 
| 223 |  |  | return(nvopts); | 
| 224 | greg | 1.1 | } | 
| 225 |  |  |  | 
| 226 |  |  |  | 
| 227 |  |  | fprintview(vp, fp)                      /* write out view parameters */ | 
| 228 |  |  | register VIEW  *vp; | 
| 229 |  |  | FILE  *fp; | 
| 230 |  |  | { | 
| 231 |  |  | fprintf(fp, " -vt%c", vp->type); | 
| 232 |  |  | fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]); | 
| 233 |  |  | fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); | 
| 234 |  |  | fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); | 
| 235 |  |  | fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); | 
| 236 | greg | 1.5 | fprintf(fp, " -vs %d -vl %d", vp->hoff, vp->voff); | 
| 237 | greg | 1.1 | } | 
| 238 |  |  |  | 
| 239 |  |  |  | 
| 240 |  |  | static VIEW  *hview;                    /* view from header */ | 
| 241 |  |  | static int  gothview;                   /* success indicator */ | 
| 242 | greg | 1.3 | static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL}; | 
| 243 | greg | 1.1 |  | 
| 244 |  |  |  | 
| 245 |  |  | static | 
| 246 |  |  | gethview(s)                             /* get view from header */ | 
| 247 |  |  | char  *s; | 
| 248 |  |  | { | 
| 249 |  |  | register char  **an; | 
| 250 |  |  |  | 
| 251 |  |  | for (an = altname; *an != NULL; an++) | 
| 252 |  |  | if (!strncmp(*an, s, strlen(*an))) { | 
| 253 | greg | 1.5 | if (sscanview(hview, s+strlen(*an)) > 0) | 
| 254 | greg | 1.1 | gothview++; | 
| 255 |  |  | return; | 
| 256 |  |  | } | 
| 257 |  |  | } | 
| 258 |  |  |  | 
| 259 |  |  |  | 
| 260 |  |  | int | 
| 261 |  |  | viewfile(fname, vp)                     /* get view from file */ | 
| 262 |  |  | char  *fname; | 
| 263 |  |  | VIEW  *vp; | 
| 264 |  |  | { | 
| 265 |  |  | extern char  *progname; | 
| 266 |  |  | FILE  *fp; | 
| 267 |  |  |  | 
| 268 |  |  | if ((fp = fopen(fname, "r")) == NULL) | 
| 269 |  |  | return(-1); | 
| 270 |  |  |  | 
| 271 |  |  | altname[0] = progname; | 
| 272 |  |  | hview = vp; | 
| 273 |  |  | gothview = 0; | 
| 274 |  |  |  | 
| 275 |  |  | getheader(fp, gethview); | 
| 276 |  |  |  | 
| 277 |  |  | fclose(fp); | 
| 278 |  |  |  | 
| 279 |  |  | return(gothview); | 
| 280 |  |  | } |