ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.27
Committed: Thu Feb 12 18:55:50 2004 UTC (20 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.26: +2 -1 lines
Log Message:
Moved paths.h out of rtio.h and included it manually where R_OK was needed

File Contents

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