ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
Revision: 2.9
Committed: Mon Apr 6 18:04:19 1992 UTC (32 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +11 -1 lines
Log Message:
changed view command to accept options

File Contents

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