ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.23
Committed: Wed Nov 26 19:16:23 2003 UTC (20 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +2 -2 lines
Log Message:
Minor code improvements

File Contents

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