ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.31
Committed: Fri Feb 18 17:42:14 2005 UTC (19 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1
Changes since 2.30: +3 -1 lines
Log Message:
Added check for illegal view distance

File Contents

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