ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.7
Committed: Tue Dec 20 20:15:04 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +46 -20 lines
Log Message:
added -vo and -va (fore and aft clipping plane) parameters

File Contents

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