ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.28
Committed: Tue Jun 8 19:48:29 2004 UTC (19 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.27: +2 -1 lines
Log Message:
Removed redundant #include's and fixed ordering on some headers

File Contents

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