ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
Revision: 2.18
Committed: Wed Aug 25 14:13:51 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +71 -1 lines
Log Message:
added L and V commands

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, NULL, 0)) == 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 s = sskip(s);
198 if (sscanf(s, "%s", rifname) != 1 && rifname[0] == '\0') {
199 error(COMMAND, "no previous rad file");
200 return;
201 }
202 if ((fname = getpath(rifname, NULL, 0)) == NULL ||
203 (fp = fopen(fname, "a")) == NULL) {
204 sprintf(errmsg, "cannot open \"%s\"", rifname);
205 error(COMMAND, errmsg);
206 return;
207 }
208 fputs("view= ", fp);
209 fputs(view, fp);
210 fprintview(&ourview, fp);
211 putc('\n', fp);
212 fclose(fp);
213 }
214
215
216 loadview(s) /* load view from rad file */
217 char *s;
218 {
219 char buf[512];
220 char *fname;
221 FILE *fp;
222 VIEW nv;
223
224 strcpy(buf, "rad -n -s -V -v ");
225 if (sscanf(s, "%s", buf+strlen(buf)) == 1)
226 s = sskip(s);
227 else
228 strcat(buf, "1");
229 if (sscanf(s, "%s", rifname) != 1 && rifname[0] == '\0') {
230 error(COMMAND, "no previous rad file");
231 return;
232 }
233 fname = getpath(rifname, NULL, 0);
234 if (fname == NULL || access(fname, R_OK) == -1) {
235 sprintf(errmsg, "cannot access \"%s\"", rifname);
236 error(COMMAND, errmsg);
237 return;
238 }
239 sprintf(buf+strlen(buf), " %s", fname);
240 if ((fp = popen(buf, "r")) == NULL) {
241 error(COMMAND, "cannot run rad");
242 return;
243 }
244 buf[0] = '\0';
245 fgets(buf, sizeof(buf), fp);
246 pclose(fp);
247 copystruct(&nv, &stdview);
248 if (!sscanview(&nv, buf)) {
249 error(COMMAND, "rad error -- no such view?");
250 return;
251 }
252 newview(&nv);
253 }
254
255
256 getaim(s) /* aim camera */
257 char *s;
258 {
259 double zfact;
260 VIEW nv;
261
262 if (getinterest(s, 1, nv.vdir, &zfact) < 0)
263 return;
264 nv.type = ourview.type;
265 VCOPY(nv.vp, ourview.vp);
266 VCOPY(nv.vup, ourview.vup);
267 nv.hoff = ourview.hoff; nv.voff = ourview.voff;
268 nv.horiz = ourview.horiz; nv.vert = ourview.vert;
269 zoomview(&nv, zfact);
270 newview(&nv);
271 }
272
273
274 getmove(s) /* move camera */
275 char *s;
276 {
277 FVECT vc;
278 double mag;
279
280 if (getinterest(s, 0, vc, &mag) < 0)
281 return;
282 moveview(0.0, 0.0, mag, vc);
283 }
284
285
286 getrotate(s) /* rotate camera */
287 char *s;
288 {
289 VIEW nv;
290 FVECT v1;
291 double angle, elev, zfact;
292
293 elev = 0.0; zfact = 1.0;
294 if (sscanf(s, "%lf %lf %lf", &angle, &elev, &zfact) < 1) {
295 error(COMMAND, "missing angle");
296 return;
297 }
298 nv.type = ourview.type;
299 VCOPY(nv.vp, ourview.vp);
300 VCOPY(nv.vup, ourview.vup);
301 nv.hoff = ourview.hoff; nv.voff = ourview.voff;
302 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
303 if (elev != 0.0) {
304 fcross(v1, nv.vdir, ourview.vup);
305 normalize(v1);
306 spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
307 }
308 nv.horiz = ourview.horiz; nv.vert = ourview.vert;
309 zoomview(&nv, zfact);
310 newview(&nv);
311 }
312
313
314 getpivot(s) /* pivot viewpoint */
315 register char *s;
316 {
317 FVECT vc;
318 double angle, elev, mag;
319
320 elev = 0.0;
321 if (sscanf(s, "%lf %lf", &angle, &elev) < 1) {
322 error(COMMAND, "missing angle");
323 return;
324 }
325 if (getinterest(sskip(sskip(s)), 0, vc, &mag) < 0)
326 return;
327 moveview(angle, elev, mag, vc);
328 }
329
330
331 getexposure(s) /* get new exposure */
332 char *s;
333 {
334 char buf[128];
335 register char *cp;
336 RECT r;
337 int x, y;
338 register PNODE *p = &ptrunk;
339 int adapt = 0;
340 double e = 1.0;
341 start:
342 for (cp = s; isspace(*cp); cp++)
343 ;
344 if (*cp == '@') {
345 adapt++;
346 goto start;
347 }
348 if (*cp == '\0') { /* normalize to point */
349 if (dev->getcur == NULL)
350 return;
351 (*dev->comout)("Pick point for exposure\n");
352 if ((*dev->getcur)(&x, &y) == ABORT)
353 return;
354 r.l = r.d = 0;
355 r.r = hresolu; r.u = vresolu;
356 p = findrect(x, y, &ptrunk, &r, -1);
357 } else {
358 if (*cp == '=') { /* absolute setting */
359 p = NULL;
360 e = 1.0/exposure;
361 for (cp++; isspace(*cp); cp++)
362 ;
363 if (*cp == '\0') { /* interactive */
364 sprintf(buf, "exposure (%f): ", exposure);
365 (*dev->comout)(buf);
366 (*dev->comin)(buf, NULL);
367 for (cp = buf; isspace(*cp); cp++)
368 ;
369 if (*cp == '\0')
370 return;
371 }
372 }
373 if (*cp == '+' || *cp == '-') /* f-stops */
374 e *= pow(2.0, atof(cp));
375 else /* multiplier */
376 e *= atof(cp);
377 }
378 if (p != NULL) { /* relative setting */
379 if (bright(p->v) < 1e-15) {
380 error(COMMAND, "cannot normalize to zero");
381 return;
382 }
383 if (adapt)
384 e *= 106./pow(1.219+pow(luminance(p->v)/exposure,.4),2.5)/exposure;
385 else
386 e *= 0.5 / bright(p->v);
387 }
388 if (e <= FTINY || fabs(1.0 - e) <= FTINY)
389 return;
390 scalepict(&ptrunk, e);
391 exposure *= e;
392 redraw();
393 }
394
395
396 getparam(str, dsc, typ, ptr) /* get variable from user */
397 char *str, *dsc;
398 int typ;
399 register union {int i; double d; COLOR C;} *ptr;
400 {
401 extern char *index();
402 int i0;
403 double d0, d1, d2;
404 char buf[48];
405
406 switch (typ) {
407 case 'i': /* integer */
408 if (sscanf(str, "%d", &i0) != 1) {
409 (*dev->comout)(dsc);
410 sprintf(buf, " (%d): ", ptr->i);
411 (*dev->comout)(buf);
412 (*dev->comin)(buf, NULL);
413 if (sscanf(buf, "%d", &i0) != 1)
414 return(0);
415 }
416 ptr->i = i0;
417 return(1);
418 case 'r': /* real */
419 if (sscanf(str, "%lf", &d0) != 1) {
420 (*dev->comout)(dsc);
421 sprintf(buf, " (%.6g): ", ptr->d);
422 (*dev->comout)(buf);
423 (*dev->comin)(buf, NULL);
424 if (sscanf(buf, "%lf", &d0) != 1)
425 return(0);
426 }
427 ptr->d = d0;
428 return(1);
429 case 'b': /* boolean */
430 if (sscanf(str, "%1s", buf) != 1) {
431 (*dev->comout)(dsc);
432 sprintf(buf, "? (%c): ", ptr->i ? 'y' : 'n');
433 (*dev->comout)(buf);
434 (*dev->comin)(buf, NULL);
435 if (buf[0] == '\0' ||
436 index("yY+1tTnN-0fF", buf[0]) == NULL)
437 return(0);
438 }
439 ptr->i = index("yY+1tT", buf[0]) != NULL;
440 return(1);
441 case 'C': /* color */
442 if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) {
443 (*dev->comout)(dsc);
444 sprintf(buf, " (%.6g %.6g %.6g): ",
445 colval(ptr->C,RED),
446 colval(ptr->C,GRN),
447 colval(ptr->C,BLU));
448 (*dev->comout)(buf);
449 (*dev->comin)(buf, NULL);
450 if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3)
451 return(0);
452 }
453 setcolor(ptr->C, d0, d1, d2);
454 return(1);
455 }
456 }
457
458
459 setparam(s) /* get/set program parameter */
460 register char *s;
461 {
462 extern int psample;
463 extern double maxdiff;
464 extern double minweight;
465 extern int maxdepth;
466 extern double dstrsrc;
467 extern double shadthresh;
468 extern double shadcert;
469 extern COLOR ambval;
470 extern double ambacc;
471 extern int ambres;
472 extern int ambdiv;
473 extern int ambssamp;
474 extern int ambounce;
475 extern int directvis;
476 extern double srcsizerat;
477 extern int do_irrad;
478 extern double specjitter;
479 extern double specthresh;
480 char buf[128];
481
482 if (s[0] == '\0') {
483 (*dev->comout)(
484 "aa ab ad ar as av b dc di dj ds dt i lr lw ps pt sj st: ");
485 (*dev->comin)(buf, NULL);
486 s = buf;
487 }
488 switch (s[0]) {
489 case 'l': /* limit */
490 switch (s[1]) {
491 case 'w': /* weight */
492 getparam(s+2, "limit weight", 'r', &minweight);
493 break;
494 case 'r': /* reflection */
495 getparam(s+2, "limit reflection", 'i', &maxdepth);
496 break;
497 default:
498 goto badparam;
499 }
500 break;
501 case 'd': /* direct */
502 switch (s[1]) {
503 case 'j': /* jitter */
504 getparam(s+2, "direct jitter", 'r', &dstrsrc);
505 break;
506 case 'c': /* certainty */
507 getparam(s+2, "direct certainty", 'r', &shadcert);
508 break;
509 case 't': /* threshold */
510 getparam(s+2, "direct threshold", 'r', &shadthresh);
511 break;
512 case 'v': /* visibility */
513 getparam(s+2, "direct visibility",
514 'b', &directvis);
515 break;
516 case 's': /* sampling */
517 getparam(s+2, "direct sampling", 'r', &srcsizerat);
518 break;
519 default:
520 goto badparam;
521 }
522 break;
523 case 'b': /* black and white */
524 getparam(s+1, "black and white", 'b', &greyscale);
525 break;
526 case 'i': /* irradiance */
527 getparam(s+1, "irradiance", 'b', &do_irrad);
528 break;
529 case 'a': /* ambient */
530 switch (s[1]) {
531 case 'v': /* value */
532 getparam(s+2, "ambient value", 'C', (COLOR *)ambval);
533 break;
534 case 'a': /* accuracy */
535 if (getparam(s+2, "ambient accuracy", 'r', &ambacc))
536 setambacc(ambacc);
537 break;
538 case 'd': /* divisions */
539 getparam(s+2, "ambient divisions", 'i', &ambdiv);
540 break;
541 case 's': /* samples */
542 getparam(s+2, "ambient super-samples", 'i', &ambssamp);
543 break;
544 case 'b': /* bounces */
545 getparam(s+2, "ambient bounces", 'i', &ambounce);
546 break;
547 case 'r':
548 if (getparam(s+2, "ambient resolution", 'i', &ambres))
549 setambres(ambres);
550 break;
551 default:
552 goto badparam;
553 }
554 break;
555 case 'p': /* pixel */
556 switch (s[1]) {
557 case 's': /* sample */
558 if (getparam(s+2, "pixel sample", 'i', &psample))
559 pdepth = 0;
560 break;
561 case 't': /* threshold */
562 if (getparam(s+2, "pixel threshold", 'r', &maxdiff))
563 pdepth = 0;
564 break;
565 default:
566 goto badparam;
567 }
568 break;
569 case 's': /* specular */
570 switch (s[1]) {
571 case 'j': /* jitter */
572 getparam(s+2, "specular jitter", 'r', &specjitter);
573 break;
574 case 't': /* threshold */
575 getparam(s+2, "specular threshold", 'r', &specthresh);
576 break;
577 default:
578 goto badparam;
579 }
580 break;
581 case '\0': /* nothing */
582 break;
583 default:;
584 badparam:
585 *sskip(s) = '\0';
586 sprintf(errmsg, "%s: unknown variable", s);
587 error(COMMAND, errmsg);
588 break;
589 }
590 }
591
592
593 traceray(s) /* trace a single ray */
594 char *s;
595 {
596 char buf[128];
597 int x, y;
598 RAY thisray;
599
600 if (!sscanvec(s, thisray.rorg) ||
601 !sscanvec(sskip(sskip(sskip(s))), thisray.rdir)) {
602
603 if (dev->getcur == NULL)
604 return;
605 (*dev->comout)("Pick ray\n");
606 if ((*dev->getcur)(&x, &y) == ABORT)
607 return;
608
609 if (viewray(thisray.rorg, thisray.rdir, &ourview,
610 (x+.5)/hresolu, (y+.5)/vresolu) < 0) {
611 error(COMMAND, "not on image");
612 return;
613 }
614
615 } else if (normalize(thisray.rdir) == 0.0) {
616 error(COMMAND, "zero ray direction");
617 return;
618 }
619
620 rayorigin(&thisray, NULL, PRIMARY, 1.0);
621
622 rayvalue(&thisray);
623
624 if (thisray.ro == NULL)
625 (*dev->comout)("ray hit nothing");
626 else {
627 sprintf(buf, "ray hit %s %s \"%s\"",
628 objptr(thisray.ro->omod)->oname,
629 ofun[thisray.ro->otype].funame,
630 thisray.ro->oname);
631 (*dev->comout)(buf);
632 (*dev->comin)(buf, NULL);
633 if (thisray.rot >= FHUGE)
634 (*dev->comout)("at infinity");
635 else {
636 sprintf(buf, "at (%.6g %.6g %.6g)", thisray.rop[0],
637 thisray.rop[1], thisray.rop[2]);
638 (*dev->comout)(buf);
639 }
640 (*dev->comin)(buf, NULL);
641 sprintf(buf, "value (%.5g %.5g %.5g) (%.1fL)",
642 colval(thisray.rcol,RED),
643 colval(thisray.rcol,GRN),
644 colval(thisray.rcol,BLU),
645 luminance(thisray.rcol));
646 (*dev->comout)(buf);
647 }
648 (*dev->comin)(buf, NULL);
649 }
650
651
652 writepict(s) /* write the picture to a file */
653 char *s;
654 {
655 static char buf[128];
656 char *fname;
657 FILE *fp;
658 COLR *scanline;
659 int y;
660
661 if (sscanf(s, "%s", buf) != 1 && buf[0] == '\0') {
662 error(COMMAND, "no file");
663 return;
664 }
665 if ((fname = getpath(buf, NULL, 0)) == NULL ||
666 (fp = fopen(fname, "w")) == NULL) {
667 sprintf(errmsg, "cannot open \"%s\"", buf);
668 error(COMMAND, errmsg);
669 return;
670 }
671 #ifdef MSDOS
672 setmode(fileno(fp), O_BINARY);
673 #endif
674 (*dev->comout)("writing \"");
675 (*dev->comout)(fname);
676 (*dev->comout)("\"...\n");
677 /* write header */
678 fputs(progname, fp);
679 fprintview(&ourview, fp);
680 if (octname != NULL)
681 fprintf(fp, " %s\n", octname);
682 else
683 putc('\n', fp);
684 fprintf(fp, "SOFTWARE= %s\n", VersionID);
685 if (exposure != 1.0)
686 fputexpos(exposure, fp);
687 if (dev->pixaspect != 1.0)
688 fputaspect(dev->pixaspect, fp);
689 fputformat(COLRFMT, fp);
690 putc('\n', fp);
691 fprtresolu(hresolu, vresolu, fp);
692
693 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
694 if (scanline == NULL) {
695 error(COMMAND, "not enough memory!");
696 fclose(fp);
697 unlink(fname);
698 return;
699 }
700 for (y = vresolu-1; y >= 0; y--) {
701 getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu);
702 if (fwritecolrs(scanline, hresolu, fp) < 0)
703 break;
704 }
705 free((char *)scanline);
706 if (fclose(fp) < 0)
707 error(COMMAND, "write error");
708 }