ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 1.17
Committed: Mon Nov 11 14:00:14 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +72 -33 lines
Log Message:
Improved handling of scanline ordering

File Contents

# User Rev Content
1 greg 1.17 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
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.17 #include "resolu.h"
18    
19 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
20 greg 1.1
21    
22     char *
23 greg 1.5 setview(v) /* set hvec and vvec, return message on error */
24     register VIEW *v;
25 greg 1.3 {
26 greg 1.12 static char ill_horiz[] = "illegal horizontal view size";
27     static char ill_vert[] = "illegal vertical view size";
28    
29 greg 1.1 if (normalize(v->vdir) == 0.0) /* normalize direction */
30     return("zero view direction");
31    
32 greg 1.14 if (normalize(v->vup) == 0.0) /* normalize view up */
33     return("zero view up vector");
34    
35 greg 1.5 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
36    
37     if (normalize(v->hvec) == 0.0)
38 greg 1.14 return("view up parallel to view direction");
39 greg 1.1
40 greg 1.5 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
41    
42 greg 1.12 if (v->horiz <= FTINY)
43     return(ill_horiz);
44     if (v->vert <= FTINY)
45     return(ill_vert);
46    
47     switch (v->type) {
48     case VT_PAR: /* parallel view */
49 greg 1.5 v->hn2 = v->horiz;
50 greg 1.12 v->vn2 = v->vert;
51     break;
52     case VT_PER: /* perspective view */
53     if (v->horiz >= 180.0-FTINY)
54     return(ill_horiz);
55     if (v->vert >= 180.0-FTINY)
56     return(ill_vert);
57 greg 1.5 v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
58 greg 1.12 v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
59     break;
60     case VT_ANG: /* angular fisheye */
61     if (v->horiz > 360.0+FTINY)
62     return(ill_horiz);
63     if (v->vert > 360.0+FTINY)
64     return(ill_vert);
65     v->hn2 = v->horiz / 90.0;
66     v->vn2 = v->vert / 90.0;
67     break;
68     case VT_HEM: /* hemispherical fisheye */
69     if (v->horiz > 180.0+FTINY)
70     return(ill_horiz);
71     if (v->vert > 180.0+FTINY)
72     return(ill_vert);
73     v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
74     v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
75     break;
76     default:
77 greg 1.1 return("unknown view type");
78 greg 1.12 }
79 greg 1.13 if (v->type != VT_ANG) {
80 greg 1.12 v->hvec[0] *= v->hn2;
81     v->hvec[1] *= v->hn2;
82     v->hvec[2] *= v->hn2;
83     v->vvec[0] *= v->vn2;
84     v->vvec[1] *= v->vn2;
85     v->vvec[2] *= v->vn2;
86     }
87 greg 1.5 v->hn2 *= v->hn2;
88     v->vn2 *= v->vn2;
89 greg 1.1
90     return(NULL);
91     }
92    
93    
94 greg 1.7 normaspect(va, ap, xp, yp) /* fix pixel aspect or resolution */
95     double va; /* view aspect ratio */
96     double *ap; /* pixel aspect in (or out if 0) */
97     int *xp, *yp; /* x and y resolution in (or out if *ap!=0) */
98 greg 1.6 {
99     if (*ap <= FTINY)
100 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
101 greg 1.6 else if (va * *xp > *ap * *yp)
102 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
103 greg 1.6 else
104 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
105 greg 1.6 }
106    
107    
108 greg 1.5 viewray(orig, direc, v, x, y) /* compute ray origin and direction */
109 greg 1.1 FVECT orig, direc;
110     register VIEW *v;
111     double x, y;
112     {
113 greg 1.12 double d, z;
114    
115 greg 1.5 x += v->hoff - 0.5;
116     y += v->voff - 0.5;
117 greg 1.1
118 greg 1.12 switch(v->type) {
119     case VT_PAR: /* parallel view */
120 greg 1.5 orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
121     orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
122     orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
123 greg 1.1 VCOPY(direc, v->vdir);
124 greg 1.12 return(0);
125     case VT_PER: /* perspective view */
126 greg 1.1 VCOPY(orig, v->vp);
127 greg 1.5 direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
128     direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
129     direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
130 greg 1.1 normalize(direc);
131 greg 1.12 return(0);
132     case VT_HEM: /* hemispherical fisheye */
133 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
134 greg 1.12 if (z < 0.0)
135     return(-1);
136     z = sqrt(z);
137     VCOPY(orig, v->vp);
138     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
139     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
140     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
141     return(0);
142     case VT_ANG: /* angular fisheye */
143     x *= v->horiz/180.0;
144     y *= v->vert/180.0;
145     d = x*x + y*y;
146     if (d > 1.0)
147     return(-1);
148     VCOPY(orig, v->vp);
149     if (d <= FTINY) {
150     VCOPY(direc, v->vdir);
151     return(0);
152     }
153     d = sqrt(d);
154     z = cos(PI*d);
155     d = sqrt(1 - z*z)/d;
156     x *= d;
157     y *= d;
158     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
159     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
160     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
161     return(0);
162 greg 1.1 }
163 greg 1.12 return(-1);
164 greg 1.1 }
165    
166    
167 greg 1.17 viewloc(ip, v, p) /* find image location for point */
168     FVECT ip;
169 greg 1.3 register VIEW *v;
170     FVECT p;
171     {
172     double d;
173     FVECT disp;
174 greg 1.5
175 greg 1.3 disp[0] = p[0] - v->vp[0];
176     disp[1] = p[1] - v->vp[1];
177     disp[2] = p[2] - v->vp[2];
178    
179 greg 1.12 switch (v->type) {
180     case VT_PAR: /* parallel view */
181 greg 1.17 ip[2] = DOT(disp,v->vdir);
182 greg 1.13 break;
183 greg 1.12 case VT_PER: /* perspective view */
184 greg 1.11 d = DOT(disp,v->vdir);
185 greg 1.17 ip[2] = sqrt(DOT(disp,disp));
186     if (d < 0.0) { /* fold pyramid */
187     ip[2] = -ip[2];
188 greg 1.11 d = -d;
189 greg 1.17 } else if (d > FTINY) {
190 greg 1.11 d = 1.0/d;
191     disp[0] *= d;
192     disp[1] *= d;
193     disp[2] *= d;
194     }
195 greg 1.13 break;
196 greg 1.12 case VT_HEM: /* hemispherical fisheye */
197     d = normalize(disp);
198 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
199     ip[2] = -d;
200     else
201     ip[2] = d;
202 greg 1.13 break;
203 greg 1.12 case VT_ANG: /* angular fisheye */
204 greg 1.17 ip[0] = 0.5 - v->hoff;
205     ip[1] = 0.5 - v->voff;
206     ip[2] = normalize(disp);
207 greg 1.12 d = DOT(disp,v->vdir);
208     if (d >= 1.0-FTINY)
209     return;
210     if (d <= -(1.0-FTINY)) {
211 greg 1.17 ip[1] += 180.0/v->horiz;
212     ip[2] += 180.0/v->vert;
213 greg 1.12 return;
214     }
215     d = acos(d)/PI / sqrt(1.0 - d*d);
216 greg 1.17 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
217     ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
218 greg 1.12 return;
219 greg 1.3 }
220 greg 1.17 ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
221     ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
222 greg 1.3 }
223    
224    
225 greg 1.17 pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */
226     FLOAT loc[2];
227     register RESOLU *rp;
228     int px, py;
229     {
230     register int x, y;
231    
232     if (rp->or & YMAJOR) {
233     x = px;
234     y = py;
235     } else {
236     x = py;
237     y = px;
238     }
239     if (rp->or & XDECR)
240     x = rp->xr-1 - x;
241     if (rp->or & YDECR)
242     y = rp->yr-1 - y;
243     loc[0] = (x+.5)/rp->xr;
244     loc[1] = (y+.5)/rp->yr;
245     }
246    
247    
248     loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */
249     int pp[2];
250     register RESOLU *rp;
251     double lx, ly;
252     {
253     register int x, y;
254    
255     x = lx * rp->xr;
256     y = ly * rp->yr;
257     if (rp->or & XDECR)
258     x = rp->xr-1 - x;
259     if (rp->or & YDECR)
260     y = rp->yr-1 - y;
261     if (rp->or & YMAJOR) {
262     pp[0] = x;
263     pp[1] = y;
264     } else {
265     pp[0] = y;
266     pp[1] = x;
267     }
268     }
269    
270    
271 greg 1.5 int
272     getviewopt(v, ac, av) /* process view argument */
273     register VIEW *v;
274     int ac;
275     register char *av[];
276     {
277 greg 1.16 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
278     badarg(ac-1,av+1,l)) return(-1)
279 greg 1.5 extern double atof();
280    
281 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
282 greg 1.5 return(-1);
283     switch (av[0][2]) {
284     case 't': /* type */
285     if (!av[0][3] || av[0][3]==' ')
286     return(-1);
287 greg 1.16 check(4,"");
288 greg 1.5 v->type = av[0][3];
289     return(0);
290     case 'p': /* point */
291 greg 1.16 check(3,"fff");
292 greg 1.5 v->vp[0] = atof(av[1]);
293     v->vp[1] = atof(av[2]);
294     v->vp[2] = atof(av[3]);
295     return(3);
296     case 'd': /* direction */
297 greg 1.16 check(3,"fff");
298 greg 1.5 v->vdir[0] = atof(av[1]);
299     v->vdir[1] = atof(av[2]);
300     v->vdir[2] = atof(av[3]);
301     return(3);
302     case 'u': /* up */
303 greg 1.16 check(3,"fff");
304 greg 1.5 v->vup[0] = atof(av[1]);
305     v->vup[1] = atof(av[2]);
306     v->vup[2] = atof(av[3]);
307     return(3);
308     case 'h': /* horizontal size */
309 greg 1.16 check(3,"f");
310 greg 1.5 v->horiz = atof(av[1]);
311     return(1);
312     case 'v': /* vertical size */
313 greg 1.16 check(3,"f");
314 greg 1.5 v->vert = atof(av[1]);
315     return(1);
316     case 's': /* shift */
317 greg 1.16 check(3,"f");
318 greg 1.5 v->hoff = atof(av[1]);
319     return(1);
320     case 'l': /* lift */
321 greg 1.16 check(3,"f");
322 greg 1.5 v->voff = atof(av[1]);
323     return(1);
324     default:
325     return(-1);
326     }
327     #undef check
328     }
329    
330    
331     int
332     sscanview(vp, s) /* get view parameters from string */
333     VIEW *vp;
334 greg 1.1 register char *s;
335     {
336 greg 1.5 int ac;
337     char *av[4];
338     int na;
339     int nvopts = 0;
340    
341     while (*s == ' ')
342     s++;
343 greg 1.9 while (*s) {
344 greg 1.5 ac = 0;
345     do {
346     av[ac++] = s;
347     while (*s && *s != ' ')
348     s++;
349     while (*s == ' ')
350     s++;
351     } while (*s && ac < 4);
352     if ((na = getviewopt(vp, ac, av)) >= 0) {
353     if (na+1 < ac)
354     s = av[na+1];
355     nvopts++;
356 greg 1.9 } else if (ac > 1)
357     s = av[1];
358     }
359 greg 1.5 return(nvopts);
360 greg 1.1 }
361    
362    
363     fprintview(vp, fp) /* write out view parameters */
364     register VIEW *vp;
365     FILE *fp;
366     {
367     fprintf(fp, " -vt%c", vp->type);
368     fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
369     fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
370     fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
371     fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
372 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
373 greg 1.1 }
374    
375    
376 greg 1.3 static char *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
377 greg 1.1
378 greg 1.15 struct myview {
379     VIEW *hv;
380     int ok;
381     };
382 greg 1.1
383 greg 1.15
384 greg 1.1 static
385 greg 1.15 gethview(s, v) /* get view from header */
386 greg 1.1 char *s;
387 greg 1.15 register struct myview *v;
388 greg 1.1 {
389     register char **an;
390    
391     for (an = altname; *an != NULL; an++)
392     if (!strncmp(*an, s, strlen(*an))) {
393 greg 1.15 if (sscanview(v->hv, s+strlen(*an)) > 0)
394     v->ok++;
395 greg 1.1 return;
396     }
397     }
398    
399    
400     int
401 greg 1.17 viewfile(fname, vp, rp) /* get view from file */
402 greg 1.1 char *fname;
403     VIEW *vp;
404 greg 1.17 RESOLU *rp;
405 greg 1.1 {
406     extern char *progname;
407 greg 1.15 struct myview mvs;
408 greg 1.1 FILE *fp;
409    
410     if ((fp = fopen(fname, "r")) == NULL)
411     return(-1);
412    
413     altname[0] = progname;
414 greg 1.15 mvs.hv = vp;
415     mvs.ok = 0;
416 greg 1.1
417 greg 1.15 getheader(fp, gethview, &mvs);
418 greg 1.8
419 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
420 greg 1.15 mvs.ok = 0;
421 greg 1.1
422     fclose(fp);
423    
424 greg 1.15 return(mvs.ok);
425 greg 1.1 }