ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.37
Committed: Sat Oct 27 03:01:21 2012 UTC (11 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.36: +4 -3 lines
Log Message:
Minor efficiency improvement in loc2pix()

File Contents

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