ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
Revision: 1.30
Committed: Mon Oct 21 13:30:43 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.29: +4 -0 lines
Log Message:
added ds setting

File Contents

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