ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
Revision: 2.19
Committed: Thu Aug 26 11:24:04 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +8 -4 lines
Log Message:
minor improvements in use of getpath()

File Contents

# Content
1 /* Copyright (c) 1993 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * rv2.c - command routines used in tracing a view.
9 *
10 * 3/24/87
11 */
12
13 #include "ray.h"
14
15 #include "octree.h"
16
17 #include "otypes.h"
18
19 #include "rpaint.h"
20
21 #include "resolu.h"
22
23 #include <ctype.h>
24
25 #define CTRL(c) ((c)-'@')
26
27 #ifdef SMLFLT
28 #define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3)
29 #else
30 #define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3)
31 #endif
32
33 extern char *atos();
34
35 extern char rifname[]; /* rad input file name */
36
37 extern char VersionID[];
38 extern char *progname;
39 extern char *octname;
40
41
42 getframe(s) /* get a new frame */
43 char *s;
44 {
45 if (getrect(s, &pframe) < 0)
46 return;
47 pdepth = 0;
48 }
49
50
51 getrepaint(s) /* get area and repaint */
52 char *s;
53 {
54 RECT box;
55
56 if (getrect(s, &box) < 0)
57 return;
58 paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box);
59 }
60
61
62 getview(s) /* get/show view parameters */
63 char *s;
64 {
65 FILE *fp;
66 char buf[128];
67 char *fname;
68 int change = 0;
69 VIEW nv;
70
71 while (isspace(*s))
72 s++;
73 if (*s == '-') { /* command line parameters */
74 copystruct(&nv, &ourview);
75 if (sscanview(&nv, s))
76 newview(&nv);
77 else
78 error(COMMAND, "bad view option(s)");
79 return;
80 }
81 if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */
82 if ((fname = getpath(buf, NULL, 0)) == NULL ||
83 (fp = fopen(fname, "a")) == NULL) {
84 sprintf(errmsg, "cannot open \"%s\"", buf);
85 error(COMMAND, errmsg);
86 return;
87 }
88 fputs(progname, fp);
89 fprintview(&ourview, fp);
90 fputs(sskip(s), fp);
91 putc('\n', fp);
92 fclose(fp);
93 return;
94 }
95 sprintf(buf, "view type (%c): ", ourview.type);
96 (*dev->comout)(buf);
97 (*dev->comin)(buf, NULL);
98 if (buf[0] == CTRL('C')) return;
99 if (buf[0] && buf[0] != ourview.type) {
100 nv.type = buf[0];
101 change++;
102 } else
103 nv.type = ourview.type;
104 sprintf(buf, "view point (%.6g %.6g %.6g): ",
105 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
106 (*dev->comout)(buf);
107 (*dev->comin)(buf, NULL);
108 if (buf[0] == CTRL('C')) return;
109 if (sscanvec(buf, nv.vp))
110 change++;
111 else
112 VCOPY(nv.vp, ourview.vp);
113 sprintf(buf, "view direction (%.6g %.6g %.6g): ",
114 ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
115 (*dev->comout)(buf);
116 (*dev->comin)(buf, NULL);
117 if (buf[0] == CTRL('C')) return;
118 if (sscanvec(buf, nv.vdir))
119 change++;
120 else
121 VCOPY(nv.vdir, ourview.vdir);
122 sprintf(buf, "view up (%.6g %.6g %.6g): ",
123 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
124 (*dev->comout)(buf);
125 (*dev->comin)(buf, NULL);
126 if (buf[0] == CTRL('C')) return;
127 if (sscanvec(buf, nv.vup))
128 change++;
129 else
130 VCOPY(nv.vup, ourview.vup);
131 sprintf(buf, "view horiz and vert size (%.6g %.6g): ",
132 ourview.horiz, ourview.vert);
133 (*dev->comout)(buf);
134 (*dev->comin)(buf, NULL);
135 if (buf[0] == CTRL('C')) return;
136 if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2)
137 change++;
138 else {
139 nv.horiz = ourview.horiz; nv.vert = ourview.vert;
140 }
141 sprintf(buf, "view shift and lift (%.6g %.6g): ",
142 ourview.hoff, ourview.voff);
143 (*dev->comout)(buf);
144 (*dev->comin)(buf, NULL);
145 if (buf[0] == CTRL('C')) return;
146 if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2)
147 change++;
148 else {
149 nv.hoff = ourview.hoff; nv.voff = ourview.voff;
150 }
151 if (change)
152 newview(&nv);
153 }
154
155
156 lastview(s) /* return to a previous view */
157 char *s;
158 {
159 char buf[128];
160 char *fname;
161 int success;
162 VIEW nv;
163
164 if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */
165 copystruct(&nv, &stdview);
166 if ((fname = getpath(buf, "", R_OK)) == NULL ||
167 (success = viewfile(fname, &nv, NULL)) == -1) {
168 sprintf(errmsg, "cannot open \"%s\"", buf);
169 error(COMMAND, errmsg);
170 return;
171 }
172 if (!success)
173 error(COMMAND, "wrong file format");
174 else
175 newview(&nv);
176 return;
177 }
178 if (oldview.type == 0) { /* no old view! */
179 error(COMMAND, "no previous view");
180 return;
181 }
182 copystruct(&nv, &ourview);
183 copystruct(&ourview, &oldview);
184 copystruct(&oldview, &nv);
185 newimage();
186 }
187
188
189 saveview(s) /* save view to rad file */
190 char *s;
191 {
192 char view[64];
193 char *fname;
194 FILE *fp;
195
196 if (*atos(view, sizeof(view), s)) {
197 if (isint(view)) {
198 error(COMMAND, "cannot write view by number");
199 return;
200 }
201 s = sskip(s);
202 }
203 if (sscanf(s, "%s", rifname) != 1 && rifname[0] == '\0') {
204 error(COMMAND, "no previous rad file");
205 return;
206 }
207 if ((fname = getpath(rifname, NULL, 0)) == NULL ||
208 (fp = fopen(fname, "a")) == NULL) {
209 sprintf(errmsg, "cannot open \"%s\"", rifname);
210 error(COMMAND, errmsg);
211 return;
212 }
213 fputs("view= ", fp);
214 fputs(view, fp);
215 fprintview(&ourview, fp);
216 putc('\n', fp);
217 fclose(fp);
218 }
219
220
221 loadview(s) /* load view from rad file */
222 char *s;
223 {
224 char buf[512];
225 char *fname;
226 FILE *fp;
227 VIEW nv;
228
229 strcpy(buf, "rad -n -s -V -v ");
230 if (sscanf(s, "%s", buf+strlen(buf)) == 1)
231 s = sskip(s);
232 else
233 strcat(buf, "1");
234 if (sscanf(s, "%s", rifname) != 1 && rifname[0] == '\0') {
235 error(COMMAND, "no previous rad file");
236 return;
237 }
238 if ((fname = getpath(rifname, "", R_OK)) == NULL) {
239 sprintf(errmsg, "cannot access \"%s\"", rifname);
240 error(COMMAND, errmsg);
241 return;
242 }
243 sprintf(buf+strlen(buf), " %s", fname);
244 if ((fp = popen(buf, "r")) == NULL) {
245 error(COMMAND, "cannot run rad");
246 return;
247 }
248 buf[0] = '\0';
249 fgets(buf, sizeof(buf), fp);
250 pclose(fp);
251 copystruct(&nv, &stdview);
252 if (!sscanview(&nv, buf)) {
253 error(COMMAND, "rad error -- no such view?");
254 return;
255 }
256 newview(&nv);
257 }
258
259
260 getaim(s) /* aim camera */
261 char *s;
262 {
263 double zfact;
264 VIEW nv;
265
266 if (getinterest(s, 1, nv.vdir, &zfact) < 0)
267 return;
268 nv.type = ourview.type;
269 VCOPY(nv.vp, ourview.vp);
270 VCOPY(nv.vup, ourview.vup);
271 nv.hoff = ourview.hoff; nv.voff = ourview.voff;
272 nv.horiz = ourview.horiz; nv.vert = ourview.vert;
273 zoomview(&nv, zfact);
274 newview(&nv);
275 }
276
277
278 getmove(s) /* move camera */
279 char *s;
280 {
281 FVECT vc;
282 double mag;
283
284 if (getinterest(s, 0, vc, &mag) < 0)
285 return;
286 moveview(0.0, 0.0, mag, vc);
287 }
288
289
290 getrotate(s) /* rotate camera */
291 char *s;
292 {
293 VIEW nv;
294 FVECT v1;
295 double angle, elev, zfact;
296
297 elev = 0.0; zfact = 1.0;
298 if (sscanf(s, "%lf %lf %lf", &angle, &elev, &zfact) < 1) {
299 error(COMMAND, "missing angle");
300 return;
301 }
302 nv.type = ourview.type;
303 VCOPY(nv.vp, ourview.vp);
304 VCOPY(nv.vup, ourview.vup);
305 nv.hoff = ourview.hoff; nv.voff = ourview.voff;
306 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
307 if (elev != 0.0) {
308 fcross(v1, nv.vdir, ourview.vup);
309 normalize(v1);
310 spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
311 }
312 nv.horiz = ourview.horiz; nv.vert = ourview.vert;
313 zoomview(&nv, zfact);
314 newview(&nv);
315 }
316
317
318 getpivot(s) /* pivot viewpoint */
319 register char *s;
320 {
321 FVECT vc;
322 double angle, elev, mag;
323
324 elev = 0.0;
325 if (sscanf(s, "%lf %lf", &angle, &elev) < 1) {
326 error(COMMAND, "missing angle");
327 return;
328 }
329 if (getinterest(sskip(sskip(s)), 0, vc, &mag) < 0)
330 return;
331 moveview(angle, elev, mag, vc);
332 }
333
334
335 getexposure(s) /* get new exposure */
336 char *s;
337 {
338 char buf[128];
339 register char *cp;
340 RECT r;
341 int x, y;
342 register PNODE *p = &ptrunk;
343 int adapt = 0;
344 double e = 1.0;
345 start:
346 for (cp = s; isspace(*cp); cp++)
347 ;
348 if (*cp == '@') {
349 adapt++;
350 goto start;
351 }
352 if (*cp == '\0') { /* normalize to point */
353 if (dev->getcur == NULL)
354 return;
355 (*dev->comout)("Pick point for exposure\n");
356 if ((*dev->getcur)(&x, &y) == ABORT)
357 return;
358 r.l = r.d = 0;
359 r.r = hresolu; r.u = vresolu;
360 p = findrect(x, y, &ptrunk, &r, -1);
361 } else {
362 if (*cp == '=') { /* absolute setting */
363 p = NULL;
364 e = 1.0/exposure;
365 for (cp++; isspace(*cp); cp++)
366 ;
367 if (*cp == '\0') { /* interactive */
368 sprintf(buf, "exposure (%f): ", exposure);
369 (*dev->comout)(buf);
370 (*dev->comin)(buf, NULL);
371 for (cp = buf; isspace(*cp); cp++)
372 ;
373 if (*cp == '\0')
374 return;
375 }
376 }
377 if (*cp == '+' || *cp == '-') /* f-stops */
378 e *= pow(2.0, atof(cp));
379 else /* multiplier */
380 e *= atof(cp);
381 }
382 if (p != NULL) { /* relative setting */
383 if (bright(p->v) < 1e-15) {
384 error(COMMAND, "cannot normalize to zero");
385 return;
386 }
387 if (adapt)
388 e *= 106./pow(1.219+pow(luminance(p->v)/exposure,.4),2.5)/exposure;
389 else
390 e *= 0.5 / bright(p->v);
391 }
392 if (e <= FTINY || fabs(1.0 - e) <= FTINY)
393 return;
394 scalepict(&ptrunk, e);
395 exposure *= e;
396 redraw();
397 }
398
399
400 getparam(str, dsc, typ, ptr) /* get variable from user */
401 char *str, *dsc;
402 int typ;
403 register union {int i; double d; COLOR C;} *ptr;
404 {
405 extern char *index();
406 int i0;
407 double d0, d1, d2;
408 char buf[48];
409
410 switch (typ) {
411 case 'i': /* integer */
412 if (sscanf(str, "%d", &i0) != 1) {
413 (*dev->comout)(dsc);
414 sprintf(buf, " (%d): ", ptr->i);
415 (*dev->comout)(buf);
416 (*dev->comin)(buf, NULL);
417 if (sscanf(buf, "%d", &i0) != 1)
418 return(0);
419 }
420 ptr->i = i0;
421 return(1);
422 case 'r': /* real */
423 if (sscanf(str, "%lf", &d0) != 1) {
424 (*dev->comout)(dsc);
425 sprintf(buf, " (%.6g): ", ptr->d);
426 (*dev->comout)(buf);
427 (*dev->comin)(buf, NULL);
428 if (sscanf(buf, "%lf", &d0) != 1)
429 return(0);
430 }
431 ptr->d = d0;
432 return(1);
433 case 'b': /* boolean */
434 if (sscanf(str, "%1s", buf) != 1) {
435 (*dev->comout)(dsc);
436 sprintf(buf, "? (%c): ", ptr->i ? 'y' : 'n');
437 (*dev->comout)(buf);
438 (*dev->comin)(buf, NULL);
439 if (buf[0] == '\0' ||
440 index("yY+1tTnN-0fF", buf[0]) == NULL)
441 return(0);
442 }
443 ptr->i = index("yY+1tT", buf[0]) != NULL;
444 return(1);
445 case 'C': /* color */
446 if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) {
447 (*dev->comout)(dsc);
448 sprintf(buf, " (%.6g %.6g %.6g): ",
449 colval(ptr->C,RED),
450 colval(ptr->C,GRN),
451 colval(ptr->C,BLU));
452 (*dev->comout)(buf);
453 (*dev->comin)(buf, NULL);
454 if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3)
455 return(0);
456 }
457 setcolor(ptr->C, d0, d1, d2);
458 return(1);
459 }
460 }
461
462
463 setparam(s) /* get/set program parameter */
464 register char *s;
465 {
466 extern int psample;
467 extern double maxdiff;
468 extern double minweight;
469 extern int maxdepth;
470 extern double dstrsrc;
471 extern double shadthresh;
472 extern double shadcert;
473 extern COLOR ambval;
474 extern double ambacc;
475 extern int ambres;
476 extern int ambdiv;
477 extern int ambssamp;
478 extern int ambounce;
479 extern int directvis;
480 extern double srcsizerat;
481 extern int do_irrad;
482 extern double specjitter;
483 extern double specthresh;
484 char buf[128];
485
486 if (s[0] == '\0') {
487 (*dev->comout)(
488 "aa ab ad ar as av b dc di dj ds dt i lr lw ps pt sj st: ");
489 (*dev->comin)(buf, NULL);
490 s = buf;
491 }
492 switch (s[0]) {
493 case 'l': /* limit */
494 switch (s[1]) {
495 case 'w': /* weight */
496 getparam(s+2, "limit weight", 'r', &minweight);
497 break;
498 case 'r': /* reflection */
499 getparam(s+2, "limit reflection", 'i', &maxdepth);
500 break;
501 default:
502 goto badparam;
503 }
504 break;
505 case 'd': /* direct */
506 switch (s[1]) {
507 case 'j': /* jitter */
508 getparam(s+2, "direct jitter", 'r', &dstrsrc);
509 break;
510 case 'c': /* certainty */
511 getparam(s+2, "direct certainty", 'r', &shadcert);
512 break;
513 case 't': /* threshold */
514 getparam(s+2, "direct threshold", 'r', &shadthresh);
515 break;
516 case 'v': /* visibility */
517 getparam(s+2, "direct visibility",
518 'b', &directvis);
519 break;
520 case 's': /* sampling */
521 getparam(s+2, "direct sampling", 'r', &srcsizerat);
522 break;
523 default:
524 goto badparam;
525 }
526 break;
527 case 'b': /* black and white */
528 getparam(s+1, "black and white", 'b', &greyscale);
529 break;
530 case 'i': /* irradiance */
531 getparam(s+1, "irradiance", 'b', &do_irrad);
532 break;
533 case 'a': /* ambient */
534 switch (s[1]) {
535 case 'v': /* value */
536 getparam(s+2, "ambient value", 'C', (COLOR *)ambval);
537 break;
538 case 'a': /* accuracy */
539 if (getparam(s+2, "ambient accuracy", 'r', &ambacc))
540 setambacc(ambacc);
541 break;
542 case 'd': /* divisions */
543 getparam(s+2, "ambient divisions", 'i', &ambdiv);
544 break;
545 case 's': /* samples */
546 getparam(s+2, "ambient super-samples", 'i', &ambssamp);
547 break;
548 case 'b': /* bounces */
549 getparam(s+2, "ambient bounces", 'i', &ambounce);
550 break;
551 case 'r':
552 if (getparam(s+2, "ambient resolution", 'i', &ambres))
553 setambres(ambres);
554 break;
555 default:
556 goto badparam;
557 }
558 break;
559 case 'p': /* pixel */
560 switch (s[1]) {
561 case 's': /* sample */
562 if (getparam(s+2, "pixel sample", 'i', &psample))
563 pdepth = 0;
564 break;
565 case 't': /* threshold */
566 if (getparam(s+2, "pixel threshold", 'r', &maxdiff))
567 pdepth = 0;
568 break;
569 default:
570 goto badparam;
571 }
572 break;
573 case 's': /* specular */
574 switch (s[1]) {
575 case 'j': /* jitter */
576 getparam(s+2, "specular jitter", 'r', &specjitter);
577 break;
578 case 't': /* threshold */
579 getparam(s+2, "specular threshold", 'r', &specthresh);
580 break;
581 default:
582 goto badparam;
583 }
584 break;
585 case '\0': /* nothing */
586 break;
587 default:;
588 badparam:
589 *sskip(s) = '\0';
590 sprintf(errmsg, "%s: unknown variable", s);
591 error(COMMAND, errmsg);
592 break;
593 }
594 }
595
596
597 traceray(s) /* trace a single ray */
598 char *s;
599 {
600 char buf[128];
601 int x, y;
602 RAY thisray;
603
604 if (!sscanvec(s, thisray.rorg) ||
605 !sscanvec(sskip(sskip(sskip(s))), thisray.rdir)) {
606
607 if (dev->getcur == NULL)
608 return;
609 (*dev->comout)("Pick ray\n");
610 if ((*dev->getcur)(&x, &y) == ABORT)
611 return;
612
613 if (viewray(thisray.rorg, thisray.rdir, &ourview,
614 (x+.5)/hresolu, (y+.5)/vresolu) < 0) {
615 error(COMMAND, "not on image");
616 return;
617 }
618
619 } else if (normalize(thisray.rdir) == 0.0) {
620 error(COMMAND, "zero ray direction");
621 return;
622 }
623
624 rayorigin(&thisray, NULL, PRIMARY, 1.0);
625
626 rayvalue(&thisray);
627
628 if (thisray.ro == NULL)
629 (*dev->comout)("ray hit nothing");
630 else {
631 sprintf(buf, "ray hit %s %s \"%s\"",
632 objptr(thisray.ro->omod)->oname,
633 ofun[thisray.ro->otype].funame,
634 thisray.ro->oname);
635 (*dev->comout)(buf);
636 (*dev->comin)(buf, NULL);
637 if (thisray.rot >= FHUGE)
638 (*dev->comout)("at infinity");
639 else {
640 sprintf(buf, "at (%.6g %.6g %.6g)", thisray.rop[0],
641 thisray.rop[1], thisray.rop[2]);
642 (*dev->comout)(buf);
643 }
644 (*dev->comin)(buf, NULL);
645 sprintf(buf, "value (%.5g %.5g %.5g) (%.1fL)",
646 colval(thisray.rcol,RED),
647 colval(thisray.rcol,GRN),
648 colval(thisray.rcol,BLU),
649 luminance(thisray.rcol));
650 (*dev->comout)(buf);
651 }
652 (*dev->comin)(buf, NULL);
653 }
654
655
656 writepict(s) /* write the picture to a file */
657 char *s;
658 {
659 static char buf[128];
660 char *fname;
661 FILE *fp;
662 COLR *scanline;
663 int y;
664
665 if (sscanf(s, "%s", buf) != 1 && buf[0] == '\0') {
666 error(COMMAND, "no file");
667 return;
668 }
669 if ((fname = getpath(buf, NULL, 0)) == NULL ||
670 (fp = fopen(fname, "w")) == NULL) {
671 sprintf(errmsg, "cannot open \"%s\"", buf);
672 error(COMMAND, errmsg);
673 return;
674 }
675 #ifdef MSDOS
676 setmode(fileno(fp), O_BINARY);
677 #endif
678 (*dev->comout)("writing \"");
679 (*dev->comout)(fname);
680 (*dev->comout)("\"...\n");
681 /* write header */
682 fputs(progname, fp);
683 fprintview(&ourview, fp);
684 if (octname != NULL)
685 fprintf(fp, " %s\n", octname);
686 else
687 putc('\n', fp);
688 fprintf(fp, "SOFTWARE= %s\n", VersionID);
689 if (exposure != 1.0)
690 fputexpos(exposure, fp);
691 if (dev->pixaspect != 1.0)
692 fputaspect(dev->pixaspect, fp);
693 fputformat(COLRFMT, fp);
694 putc('\n', fp);
695 fprtresolu(hresolu, vresolu, fp);
696
697 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
698 if (scanline == NULL) {
699 error(COMMAND, "not enough memory!");
700 fclose(fp);
701 unlink(fname);
702 return;
703 }
704 for (y = vresolu-1; y >= 0; y--) {
705 getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu);
706 if (fwritecolrs(scanline, hresolu, fp) < 0)
707 break;
708 }
709 free((char *)scanline);
710 if (fclose(fp) < 0)
711 error(COMMAND, "write error");
712 }