ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.21
Committed: Sun Sep 7 05:32:02 2003 UTC (20 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.20: +3 -2 lines
Log Message:
Prototype fix

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.21 static const char RCSid[] = "$Id: image.c,v 2.20 2003/08/29 23:03:13 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     #include "standard.h"
13    
14     #include "view.h"
15    
16 greg 2.4 #include "paths.h"
17    
18 greg 2.11 #define FEQ(x,y) (fabs((x)-(y)) <= FTINY)
19     #define VEQ(v,w) (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \
20     && FEQ((v)[2],(w)[2]))
21    
22 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
23 greg 1.1
24    
25     char *
26 greg 1.5 setview(v) /* set hvec and vvec, return message on error */
27     register VIEW *v;
28 greg 1.3 {
29 greg 1.12 static char ill_horiz[] = "illegal horizontal view size";
30     static char ill_vert[] = "illegal vertical view size";
31    
32 gwlarson 2.15 if (v->vaft < -FTINY || (v->vaft > FTINY && v->vaft <= v->vfore))
33 greg 2.7 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 greg 2.9 case VT_CYL: /* cylindrical panorama */
67     if (v->horiz > 360.0+FTINY)
68     return(ill_horiz);
69     if (v->vert >= 180.0-FTINY)
70     return(ill_vert);
71     v->hn2 = v->horiz * (PI/180.0);
72     v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
73     break;
74 greg 1.12 case VT_ANG: /* angular fisheye */
75     if (v->horiz > 360.0+FTINY)
76     return(ill_horiz);
77     if (v->vert > 360.0+FTINY)
78     return(ill_vert);
79 greg 2.9 v->hn2 = v->horiz * (PI/180.0);
80     v->vn2 = v->vert * (PI/180.0);
81 greg 1.12 break;
82     case VT_HEM: /* hemispherical fisheye */
83     if (v->horiz > 180.0+FTINY)
84     return(ill_horiz);
85     if (v->vert > 180.0+FTINY)
86     return(ill_vert);
87     v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
88     v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
89     break;
90     default:
91 greg 1.1 return("unknown view type");
92 greg 1.12 }
93 greg 1.13 if (v->type != VT_ANG) {
94 greg 2.9 if (v->type != VT_CYL) {
95     v->hvec[0] *= v->hn2;
96     v->hvec[1] *= v->hn2;
97     v->hvec[2] *= v->hn2;
98     }
99 greg 1.12 v->vvec[0] *= v->vn2;
100     v->vvec[1] *= v->vn2;
101     v->vvec[2] *= v->vn2;
102     }
103 greg 1.5 v->hn2 *= v->hn2;
104     v->vn2 *= v->vn2;
105 greg 1.1
106     return(NULL);
107     }
108    
109    
110 greg 2.16 void
111 greg 1.7 normaspect(va, ap, xp, yp) /* fix pixel aspect or resolution */
112     double va; /* view aspect ratio */
113     double *ap; /* pixel aspect in (or out if 0) */
114     int *xp, *yp; /* x and y resolution in (or out if *ap!=0) */
115 greg 1.6 {
116     if (*ap <= FTINY)
117 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
118 greg 1.6 else if (va * *xp > *ap * *yp)
119 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
120 greg 1.6 else
121 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
122 greg 1.6 }
123    
124    
125 greg 2.7 double
126 greg 1.5 viewray(orig, direc, v, x, y) /* compute ray origin and direction */
127 greg 1.1 FVECT orig, direc;
128     register VIEW *v;
129     double x, y;
130     {
131 greg 1.12 double d, z;
132    
133 greg 1.5 x += v->hoff - 0.5;
134     y += v->voff - 0.5;
135 greg 1.1
136 greg 1.12 switch(v->type) {
137     case VT_PAR: /* parallel view */
138 greg 2.7 orig[0] = v->vp[0] + v->vfore*v->vdir[0]
139     + x*v->hvec[0] + y*v->vvec[0];
140     orig[1] = v->vp[1] + v->vfore*v->vdir[1]
141     + x*v->hvec[1] + y*v->vvec[1];
142     orig[2] = v->vp[2] + v->vfore*v->vdir[2]
143     + x*v->hvec[2] + y*v->vvec[2];
144 greg 1.1 VCOPY(direc, v->vdir);
145 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
146 greg 1.12 case VT_PER: /* perspective view */
147 greg 1.5 direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
148     direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
149     direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
150 greg 2.7 orig[0] = v->vp[0] + v->vfore*direc[0];
151     orig[1] = v->vp[1] + v->vfore*direc[1];
152     orig[2] = v->vp[2] + v->vfore*direc[2];
153     d = normalize(direc);
154 greg 2.8 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
155 greg 1.12 case VT_HEM: /* hemispherical fisheye */
156 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
157 greg 1.12 if (z < 0.0)
158 greg 2.7 return(-1.0);
159 greg 1.12 z = sqrt(z);
160     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
161     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
162     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
163 greg 2.7 orig[0] = v->vp[0] + v->vfore*direc[0];
164     orig[1] = v->vp[1] + v->vfore*direc[1];
165     orig[2] = v->vp[2] + v->vfore*direc[2];
166 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
167 greg 2.9 case VT_CYL: /* cylindrical panorama */
168     d = x * v->horiz * (PI/180.0);
169     z = cos(d);
170     x = sin(d);
171     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
172     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
173     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
174     orig[0] = v->vp[0] + v->vfore*direc[0];
175     orig[1] = v->vp[1] + v->vfore*direc[1];
176     orig[2] = v->vp[2] + v->vfore*direc[2];
177     d = normalize(direc);
178     return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
179 greg 1.12 case VT_ANG: /* angular fisheye */
180     x *= v->horiz/180.0;
181     y *= v->vert/180.0;
182     d = x*x + y*y;
183     if (d > 1.0)
184 greg 2.7 return(-1.0);
185 greg 1.12 d = sqrt(d);
186     z = cos(PI*d);
187 greg 2.6 d = d <= FTINY ? PI : sqrt(1 - z*z)/d;
188 greg 1.12 x *= d;
189     y *= d;
190     direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
191     direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
192     direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
193 greg 2.7 orig[0] = v->vp[0] + v->vfore*direc[0];
194     orig[1] = v->vp[1] + v->vfore*direc[1];
195     orig[2] = v->vp[2] + v->vfore*direc[2];
196 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
197 greg 1.1 }
198 greg 2.7 return(-1.0);
199 greg 1.1 }
200    
201    
202 greg 2.16 void
203 greg 1.17 viewloc(ip, v, p) /* find image location for point */
204     FVECT ip;
205 greg 1.3 register VIEW *v;
206     FVECT p;
207     {
208 gregl 2.13 double d, d2;
209 greg 1.3 FVECT disp;
210 greg 1.5
211 greg 1.3 disp[0] = p[0] - v->vp[0];
212     disp[1] = p[1] - v->vp[1];
213     disp[2] = p[2] - v->vp[2];
214    
215 greg 1.12 switch (v->type) {
216     case VT_PAR: /* parallel view */
217 greg 2.7 ip[2] = DOT(disp,v->vdir) - v->vfore;
218 greg 1.13 break;
219 greg 1.12 case VT_PER: /* perspective view */
220 greg 1.11 d = DOT(disp,v->vdir);
221 gregl 2.13 ip[2] = VLEN(disp);
222 greg 1.17 if (d < 0.0) { /* fold pyramid */
223     ip[2] = -ip[2];
224 greg 1.11 d = -d;
225 greg 2.7 }
226     if (d > FTINY) {
227 greg 1.11 d = 1.0/d;
228     disp[0] *= d;
229     disp[1] *= d;
230     disp[2] *= d;
231     }
232 greg 2.7 ip[2] *= (1.0 - v->vfore*d);
233 greg 1.13 break;
234 greg 1.12 case VT_HEM: /* hemispherical fisheye */
235     d = normalize(disp);
236 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
237     ip[2] = -d;
238     else
239     ip[2] = d;
240 greg 2.7 ip[2] -= v->vfore;
241 greg 1.13 break;
242 greg 2.9 case VT_CYL: /* cylindrical panorama */
243     d = DOT(disp,v->hvec);
244 gregl 2.13 d2 = DOT(disp,v->vdir);
245     ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
246     d = 1.0/sqrt(d*d + d2*d2);
247     ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
248     ip[2] = VLEN(disp);
249 gwlarson 2.15 ip[2] *= (1.0 - v->vfore*d);
250 greg 2.9 return;
251 greg 1.12 case VT_ANG: /* angular fisheye */
252 greg 1.17 ip[0] = 0.5 - v->hoff;
253     ip[1] = 0.5 - v->voff;
254 greg 2.7 ip[2] = normalize(disp) - v->vfore;
255 greg 1.12 d = DOT(disp,v->vdir);
256     if (d >= 1.0-FTINY)
257     return;
258     if (d <= -(1.0-FTINY)) {
259 greg 2.7 ip[0] += 180.0/v->horiz;
260 greg 1.12 return;
261     }
262     d = acos(d)/PI / sqrt(1.0 - d*d);
263 greg 1.17 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
264     ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
265 greg 1.12 return;
266 greg 1.3 }
267 greg 1.17 ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
268     ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
269 greg 1.3 }
270    
271    
272 greg 2.16 void
273 greg 1.17 pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */
274 schorsch 2.18 RREAL loc[2];
275 greg 1.17 register RESOLU *rp;
276     int px, py;
277     {
278     register int x, y;
279    
280 greg 2.16 if (rp->rt & YMAJOR) {
281 greg 1.17 x = px;
282     y = py;
283     } else {
284     x = py;
285     y = px;
286     }
287 greg 2.16 if (rp->rt & XDECR)
288 greg 1.17 x = rp->xr-1 - x;
289 greg 2.16 if (rp->rt & YDECR)
290 greg 1.17 y = rp->yr-1 - y;
291     loc[0] = (x+.5)/rp->xr;
292     loc[1] = (y+.5)/rp->yr;
293     }
294    
295    
296 greg 2.16 void
297 greg 1.17 loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */
298     int pp[2];
299     register RESOLU *rp;
300     double lx, ly;
301     {
302     register int x, y;
303    
304     x = lx * rp->xr;
305     y = ly * rp->yr;
306 greg 2.16 if (rp->rt & XDECR)
307 greg 1.17 x = rp->xr-1 - x;
308 greg 2.16 if (rp->rt & YDECR)
309 greg 1.17 y = rp->yr-1 - y;
310 greg 2.16 if (rp->rt & YMAJOR) {
311 greg 1.17 pp[0] = x;
312     pp[1] = y;
313     } else {
314     pp[0] = y;
315     pp[1] = x;
316     }
317     }
318    
319    
320 greg 1.5 int
321     getviewopt(v, ac, av) /* process view argument */
322     register VIEW *v;
323     int ac;
324     register char *av[];
325     {
326 greg 1.16 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
327     badarg(ac-1,av+1,l)) return(-1)
328 greg 1.5
329 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
330 greg 1.5 return(-1);
331     switch (av[0][2]) {
332     case 't': /* type */
333     if (!av[0][3] || av[0][3]==' ')
334     return(-1);
335 greg 1.16 check(4,"");
336 greg 1.5 v->type = av[0][3];
337     return(0);
338     case 'p': /* point */
339 greg 1.16 check(3,"fff");
340 greg 1.5 v->vp[0] = atof(av[1]);
341     v->vp[1] = atof(av[2]);
342     v->vp[2] = atof(av[3]);
343     return(3);
344     case 'd': /* direction */
345 greg 1.16 check(3,"fff");
346 greg 1.5 v->vdir[0] = atof(av[1]);
347     v->vdir[1] = atof(av[2]);
348     v->vdir[2] = atof(av[3]);
349     return(3);
350     case 'u': /* up */
351 greg 1.16 check(3,"fff");
352 greg 1.5 v->vup[0] = atof(av[1]);
353     v->vup[1] = atof(av[2]);
354     v->vup[2] = atof(av[3]);
355     return(3);
356     case 'h': /* horizontal size */
357 greg 1.16 check(3,"f");
358 greg 1.5 v->horiz = atof(av[1]);
359     return(1);
360     case 'v': /* vertical size */
361 greg 1.16 check(3,"f");
362 greg 1.5 v->vert = atof(av[1]);
363     return(1);
364 greg 2.7 case 'o': /* fore clipping plane */
365     check(3,"f");
366     v->vfore = atof(av[1]);
367     return(1);
368     case 'a': /* aft clipping plane */
369     check(3,"f");
370     v->vaft = atof(av[1]);
371     return(1);
372 greg 1.5 case 's': /* shift */
373 greg 1.16 check(3,"f");
374 greg 1.5 v->hoff = atof(av[1]);
375     return(1);
376     case 'l': /* lift */
377 greg 1.16 check(3,"f");
378 greg 1.5 v->voff = atof(av[1]);
379     return(1);
380     default:
381     return(-1);
382     }
383     #undef check
384     }
385    
386    
387     int
388     sscanview(vp, s) /* get view parameters from string */
389     VIEW *vp;
390 greg 1.1 register char *s;
391     {
392 greg 1.5 int ac;
393     char *av[4];
394     int na;
395     int nvopts = 0;
396    
397 greg 2.20 while (*s == ' ')
398 greg 2.19 s++;
399 greg 2.20 if (*s != '-')
400     s = sskip2(s,1);
401 greg 1.9 while (*s) {
402 greg 1.5 ac = 0;
403     do {
404 greg 2.21 if (ac || *s == '-')
405     av[ac++] = s;
406 greg 1.5 while (*s && *s != ' ')
407     s++;
408     while (*s == ' ')
409     s++;
410     } while (*s && ac < 4);
411     if ((na = getviewopt(vp, ac, av)) >= 0) {
412     if (na+1 < ac)
413     s = av[na+1];
414     nvopts++;
415 greg 1.9 } else if (ac > 1)
416     s = av[1];
417     }
418 greg 1.5 return(nvopts);
419 greg 1.1 }
420    
421    
422 greg 2.16 void
423 greg 1.1 fprintview(vp, fp) /* write out view parameters */
424     register VIEW *vp;
425     FILE *fp;
426     {
427     fprintf(fp, " -vt%c", vp->type);
428     fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
429     fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
430     fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
431     fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
432 greg 2.7 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
433 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
434 greg 2.11 }
435    
436    
437     char *
438     viewopt(vp) /* translate to minimal view string */
439     register VIEW *vp;
440     {
441     static char vwstr[128];
442     register char *cp = vwstr;
443    
444     if (vp->type != stdview.type) {
445     sprintf(cp, " -vt%c", vp->type);
446     cp += strlen(cp);
447     }
448     if (!VEQ(vp->vp,stdview.vp)) {
449     sprintf(cp, " -vp %.6g %.6g %.6g",
450     vp->vp[0], vp->vp[1], vp->vp[2]);
451     cp += strlen(cp);
452     }
453     if (!VEQ(vp->vdir,stdview.vdir)) {
454     sprintf(cp, " -vd %.6g %.6g %.6g",
455     vp->vdir[0], vp->vdir[1], vp->vdir[2]);
456     cp += strlen(cp);
457     }
458     if (!VEQ(vp->vup,stdview.vup)) {
459     sprintf(cp, " -vu %.6g %.6g %.6g",
460     vp->vup[0], vp->vup[1], vp->vup[2]);
461     cp += strlen(cp);
462     }
463     if (!FEQ(vp->horiz,stdview.horiz)) {
464     sprintf(cp, " -vh %.6g", vp->horiz);
465     cp += strlen(cp);
466     }
467     if (!FEQ(vp->vert,stdview.vert)) {
468     sprintf(cp, " -vv %.6g", vp->vert);
469     cp += strlen(cp);
470     }
471     if (!FEQ(vp->vfore,stdview.vfore)) {
472     sprintf(cp, " -vo %.6g", vp->vfore);
473     cp += strlen(cp);
474     }
475     if (!FEQ(vp->vaft,stdview.vaft)) {
476     sprintf(cp, " -va %.6g", vp->vaft);
477     cp += strlen(cp);
478     }
479     if (!FEQ(vp->hoff,stdview.hoff)) {
480     sprintf(cp, " -vs %.6g", vp->hoff);
481     cp += strlen(cp);
482     }
483     if (!FEQ(vp->voff,stdview.voff)) {
484     sprintf(cp, " -vl %.6g", vp->voff);
485     cp += strlen(cp);
486     }
487     return(vwstr);
488 greg 1.1 }
489    
490    
491 greg 2.3 int
492     isview(s) /* is this a view string? */
493     char *s;
494     {
495 greg 2.5 static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
496     extern char *progname;
497 greg 2.3 register char *cp;
498     register char **an;
499     /* add program name to list */
500 greg 2.5 if (altname[0] == NULL) {
501     for (cp = progname; *cp; cp++)
502     ;
503     while (cp > progname && !ISDIRSEP(cp[-1]))
504     cp--;
505     altname[0] = cp;
506     }
507 greg 2.3 /* skip leading path */
508     cp = s;
509     while (*cp && *cp != ' ')
510     cp++;
511 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
512 greg 2.3 cp--;
513     for (an = altname; *an != NULL; an++)
514     if (!strncmp(*an, cp, strlen(*an)))
515     return(1);
516     return(0);
517     }
518 greg 1.1
519 greg 2.3
520 greg 1.15 struct myview {
521     VIEW *hv;
522     int ok;
523     };
524 greg 1.1
525 greg 1.15
526 gwlarson 2.14 static int
527 greg 1.15 gethview(s, v) /* get view from header */
528 greg 1.1 char *s;
529 greg 1.15 register struct myview *v;
530 greg 1.1 {
531 greg 2.3 if (isview(s) && sscanview(v->hv, s) > 0)
532     v->ok++;
533 gwlarson 2.14 return(0);
534 greg 1.1 }
535    
536    
537     int
538 greg 1.17 viewfile(fname, vp, rp) /* get view from file */
539 greg 1.1 char *fname;
540     VIEW *vp;
541 greg 1.17 RESOLU *rp;
542 greg 1.1 {
543 greg 1.15 struct myview mvs;
544 greg 1.1 FILE *fp;
545    
546 greg 2.16 if (fname == NULL || !strcmp(fname, "-"))
547     fp = stdin;
548     else if ((fp = fopen(fname, "r")) == NULL)
549 greg 1.1 return(-1);
550    
551 greg 1.15 mvs.hv = vp;
552     mvs.ok = 0;
553 greg 1.1
554 greg 2.16 getheader(fp, (int (*)(char *, char *))&gethview, (char *)&mvs);
555 greg 1.8
556 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
557 greg 1.15 mvs.ok = 0;
558 greg 1.1
559     fclose(fp);
560    
561 greg 1.15 return(mvs.ok);
562 greg 1.1 }