ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.15
Committed: Fri Feb 26 12:24:07 1999 UTC (25 years, 2 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.14: +2 -4 lines
Log Message:
allowed negative fore clipping plane for holographic rendering

File Contents

# User Rev Content
1 greg 2.11 /* Copyright (c) 1996 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    
11     #include "standard.h"
12    
13     #include "view.h"
14    
15 greg 1.17 #include "resolu.h"
16    
17 greg 2.4 #include "paths.h"
18    
19 greg 2.11 #define FEQ(x,y) (fabs((x)-(y)) <= FTINY)
20     #define VEQ(v,w) (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \
21     && FEQ((v)[2],(w)[2]))
22    
23 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
24 greg 1.1
25    
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 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 1.17 viewloc(ip, v, p) /* find image location for point */
203     FVECT ip;
204 greg 1.3 register VIEW *v;
205     FVECT p;
206     {
207 gregl 2.13 double d, d2;
208 greg 1.3 FVECT disp;
209 greg 1.5
210 greg 1.3 disp[0] = p[0] - v->vp[0];
211     disp[1] = p[1] - v->vp[1];
212     disp[2] = p[2] - v->vp[2];
213    
214 greg 1.12 switch (v->type) {
215     case VT_PAR: /* parallel view */
216 greg 2.7 ip[2] = DOT(disp,v->vdir) - v->vfore;
217 greg 1.13 break;
218 greg 1.12 case VT_PER: /* perspective view */
219 greg 1.11 d = DOT(disp,v->vdir);
220 gregl 2.13 ip[2] = VLEN(disp);
221 greg 1.17 if (d < 0.0) { /* fold pyramid */
222     ip[2] = -ip[2];
223 greg 1.11 d = -d;
224 greg 2.7 }
225     if (d > FTINY) {
226 greg 1.11 d = 1.0/d;
227     disp[0] *= d;
228     disp[1] *= d;
229     disp[2] *= d;
230     }
231 greg 2.7 ip[2] *= (1.0 - v->vfore*d);
232 greg 1.13 break;
233 greg 1.12 case VT_HEM: /* hemispherical fisheye */
234     d = normalize(disp);
235 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
236     ip[2] = -d;
237     else
238     ip[2] = d;
239 greg 2.7 ip[2] -= v->vfore;
240 greg 1.13 break;
241 greg 2.9 case VT_CYL: /* cylindrical panorama */
242     d = DOT(disp,v->hvec);
243 gregl 2.13 d2 = DOT(disp,v->vdir);
244     ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
245     d = 1.0/sqrt(d*d + d2*d2);
246     ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
247     ip[2] = VLEN(disp);
248 gwlarson 2.15 ip[2] *= (1.0 - v->vfore*d);
249 greg 2.9 return;
250 greg 1.12 case VT_ANG: /* angular fisheye */
251 greg 1.17 ip[0] = 0.5 - v->hoff;
252     ip[1] = 0.5 - v->voff;
253 greg 2.7 ip[2] = normalize(disp) - v->vfore;
254 greg 1.12 d = DOT(disp,v->vdir);
255     if (d >= 1.0-FTINY)
256     return;
257     if (d <= -(1.0-FTINY)) {
258 greg 2.7 ip[0] += 180.0/v->horiz;
259 greg 1.12 return;
260     }
261     d = acos(d)/PI / sqrt(1.0 - d*d);
262 greg 1.17 ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz;
263     ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert;
264 greg 1.12 return;
265 greg 1.3 }
266 greg 1.17 ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
267     ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
268 greg 1.3 }
269    
270    
271 greg 1.17 pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */
272     FLOAT loc[2];
273     register RESOLU *rp;
274     int px, py;
275     {
276     register int x, y;
277    
278     if (rp->or & YMAJOR) {
279     x = px;
280     y = py;
281     } else {
282     x = py;
283     y = px;
284     }
285     if (rp->or & XDECR)
286     x = rp->xr-1 - x;
287     if (rp->or & YDECR)
288     y = rp->yr-1 - y;
289     loc[0] = (x+.5)/rp->xr;
290     loc[1] = (y+.5)/rp->yr;
291     }
292    
293    
294     loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */
295     int pp[2];
296     register RESOLU *rp;
297     double lx, ly;
298     {
299     register int x, y;
300    
301     x = lx * rp->xr;
302     y = ly * rp->yr;
303     if (rp->or & XDECR)
304     x = rp->xr-1 - x;
305     if (rp->or & YDECR)
306     y = rp->yr-1 - y;
307     if (rp->or & YMAJOR) {
308     pp[0] = x;
309     pp[1] = y;
310     } else {
311     pp[0] = y;
312     pp[1] = x;
313     }
314     }
315    
316    
317 greg 1.5 int
318     getviewopt(v, ac, av) /* process view argument */
319     register VIEW *v;
320     int ac;
321     register char *av[];
322     {
323 greg 1.16 #define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \
324     badarg(ac-1,av+1,l)) return(-1)
325 greg 1.5
326 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
327 greg 1.5 return(-1);
328     switch (av[0][2]) {
329     case 't': /* type */
330     if (!av[0][3] || av[0][3]==' ')
331     return(-1);
332 greg 1.16 check(4,"");
333 greg 1.5 v->type = av[0][3];
334     return(0);
335     case 'p': /* point */
336 greg 1.16 check(3,"fff");
337 greg 1.5 v->vp[0] = atof(av[1]);
338     v->vp[1] = atof(av[2]);
339     v->vp[2] = atof(av[3]);
340     return(3);
341     case 'd': /* direction */
342 greg 1.16 check(3,"fff");
343 greg 1.5 v->vdir[0] = atof(av[1]);
344     v->vdir[1] = atof(av[2]);
345     v->vdir[2] = atof(av[3]);
346     return(3);
347     case 'u': /* up */
348 greg 1.16 check(3,"fff");
349 greg 1.5 v->vup[0] = atof(av[1]);
350     v->vup[1] = atof(av[2]);
351     v->vup[2] = atof(av[3]);
352     return(3);
353     case 'h': /* horizontal size */
354 greg 1.16 check(3,"f");
355 greg 1.5 v->horiz = atof(av[1]);
356     return(1);
357     case 'v': /* vertical size */
358 greg 1.16 check(3,"f");
359 greg 1.5 v->vert = atof(av[1]);
360     return(1);
361 greg 2.7 case 'o': /* fore clipping plane */
362     check(3,"f");
363     v->vfore = atof(av[1]);
364     return(1);
365     case 'a': /* aft clipping plane */
366     check(3,"f");
367     v->vaft = atof(av[1]);
368     return(1);
369 greg 1.5 case 's': /* shift */
370 greg 1.16 check(3,"f");
371 greg 1.5 v->hoff = atof(av[1]);
372     return(1);
373     case 'l': /* lift */
374 greg 1.16 check(3,"f");
375 greg 1.5 v->voff = atof(av[1]);
376     return(1);
377     default:
378     return(-1);
379     }
380     #undef check
381     }
382    
383    
384     int
385     sscanview(vp, s) /* get view parameters from string */
386     VIEW *vp;
387 greg 1.1 register char *s;
388     {
389 greg 1.5 int ac;
390     char *av[4];
391     int na;
392     int nvopts = 0;
393    
394 greg 2.3 if (*s != '-')
395 greg 2.12 s = sskip2(s,1);
396 greg 1.9 while (*s) {
397 greg 1.5 ac = 0;
398     do {
399     av[ac++] = s;
400     while (*s && *s != ' ')
401     s++;
402     while (*s == ' ')
403     s++;
404     } while (*s && ac < 4);
405     if ((na = getviewopt(vp, ac, av)) >= 0) {
406     if (na+1 < ac)
407     s = av[na+1];
408     nvopts++;
409 greg 1.9 } else if (ac > 1)
410     s = av[1];
411     }
412 greg 1.5 return(nvopts);
413 greg 1.1 }
414    
415    
416     fprintview(vp, fp) /* write out view parameters */
417     register VIEW *vp;
418     FILE *fp;
419     {
420     fprintf(fp, " -vt%c", vp->type);
421     fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
422     fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
423     fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
424     fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
425 greg 2.7 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
426 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
427 greg 2.11 }
428    
429    
430     char *
431     viewopt(vp) /* translate to minimal view string */
432     register VIEW *vp;
433     {
434     static char vwstr[128];
435     register char *cp = vwstr;
436    
437     if (vp->type != stdview.type) {
438     sprintf(cp, " -vt%c", vp->type);
439     cp += strlen(cp);
440     }
441     if (!VEQ(vp->vp,stdview.vp)) {
442     sprintf(cp, " -vp %.6g %.6g %.6g",
443     vp->vp[0], vp->vp[1], vp->vp[2]);
444     cp += strlen(cp);
445     }
446     if (!VEQ(vp->vdir,stdview.vdir)) {
447     sprintf(cp, " -vd %.6g %.6g %.6g",
448     vp->vdir[0], vp->vdir[1], vp->vdir[2]);
449     cp += strlen(cp);
450     }
451     if (!VEQ(vp->vup,stdview.vup)) {
452     sprintf(cp, " -vu %.6g %.6g %.6g",
453     vp->vup[0], vp->vup[1], vp->vup[2]);
454     cp += strlen(cp);
455     }
456     if (!FEQ(vp->horiz,stdview.horiz)) {
457     sprintf(cp, " -vh %.6g", vp->horiz);
458     cp += strlen(cp);
459     }
460     if (!FEQ(vp->vert,stdview.vert)) {
461     sprintf(cp, " -vv %.6g", vp->vert);
462     cp += strlen(cp);
463     }
464     if (!FEQ(vp->vfore,stdview.vfore)) {
465     sprintf(cp, " -vo %.6g", vp->vfore);
466     cp += strlen(cp);
467     }
468     if (!FEQ(vp->vaft,stdview.vaft)) {
469     sprintf(cp, " -va %.6g", vp->vaft);
470     cp += strlen(cp);
471     }
472     if (!FEQ(vp->hoff,stdview.hoff)) {
473     sprintf(cp, " -vs %.6g", vp->hoff);
474     cp += strlen(cp);
475     }
476     if (!FEQ(vp->voff,stdview.voff)) {
477     sprintf(cp, " -vl %.6g", vp->voff);
478     cp += strlen(cp);
479     }
480     return(vwstr);
481 greg 1.1 }
482    
483    
484 greg 2.3 int
485     isview(s) /* is this a view string? */
486     char *s;
487     {
488 greg 2.5 static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL};
489     extern char *progname;
490 greg 2.3 register char *cp;
491     register char **an;
492     /* add program name to list */
493 greg 2.5 if (altname[0] == NULL) {
494     for (cp = progname; *cp; cp++)
495     ;
496     while (cp > progname && !ISDIRSEP(cp[-1]))
497     cp--;
498     altname[0] = cp;
499     }
500 greg 2.3 /* skip leading path */
501     cp = s;
502     while (*cp && *cp != ' ')
503     cp++;
504 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
505 greg 2.3 cp--;
506     for (an = altname; *an != NULL; an++)
507     if (!strncmp(*an, cp, strlen(*an)))
508     return(1);
509     return(0);
510     }
511 greg 1.1
512 greg 2.3
513 greg 1.15 struct myview {
514     VIEW *hv;
515     int ok;
516     };
517 greg 1.1
518 greg 1.15
519 gwlarson 2.14 static int
520 greg 1.15 gethview(s, v) /* get view from header */
521 greg 1.1 char *s;
522 greg 1.15 register struct myview *v;
523 greg 1.1 {
524 greg 2.3 if (isview(s) && sscanview(v->hv, s) > 0)
525     v->ok++;
526 gwlarson 2.14 return(0);
527 greg 1.1 }
528    
529    
530     int
531 greg 1.17 viewfile(fname, vp, rp) /* get view from file */
532 greg 1.1 char *fname;
533     VIEW *vp;
534 greg 1.17 RESOLU *rp;
535 greg 1.1 {
536 greg 1.15 struct myview mvs;
537 greg 1.1 FILE *fp;
538    
539     if ((fp = fopen(fname, "r")) == NULL)
540     return(-1);
541    
542 greg 1.15 mvs.hv = vp;
543     mvs.ok = 0;
544 greg 1.1
545 greg 1.15 getheader(fp, gethview, &mvs);
546 greg 1.8
547 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
548 greg 1.15 mvs.ok = 0;
549 greg 1.1
550     fclose(fp);
551    
552 greg 1.15 return(mvs.ok);
553 greg 1.1 }