ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.42
Committed: Thu Nov 8 00:54:07 2018 UTC (5 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.41: +2 -3 lines
Log Message:
Moved findmaterial() from source.c to initotypes.c

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rv3.c,v 2.41 2018/11/07 18:34:58 greg Exp $";
3 #endif
4 /*
5 * rv3.c - miscellaneous routines for rview.
6 *
7 * External symbols declared in rpaint.h
8 */
9
10 #include "copyright.h"
11
12 #include <string.h>
13
14 #include "ray.h"
15 #include "rpaint.h"
16 #include "otypes.h"
17 #include "otspecial.h"
18 #include "random.h"
19
20 #ifndef WFLUSH
21 #define WFLUSH 64 /* flush after this many primary rays */
22 #endif
23 #ifndef WFLUSH1
24 #define WFLUSH1 512 /* or this many total rays */
25 #endif
26
27
28 #ifdef SMLFLT
29 #define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3)
30 #else
31 #define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3)
32 #endif
33
34 extern int ray_pnprocs;
35
36 static RNUMBER niflush; /* flushes since newimage() */
37
38 int
39 getrect( /* get a box */
40 char *s,
41 RECT *r
42 )
43 {
44 int x0, y0, x1, y1;
45
46 if (*s && !strncmp(s, "all", strlen(s))) {
47 r->l = r->d = 0;
48 r->r = hresolu;
49 r->u = vresolu;
50 return(0);
51 }
52 if (sscanf(s, "%d %d %d %d", &x0, &y0, &x1, &y1) != 4) {
53 if (dev->getcur == NULL)
54 return(-1);
55 (*dev->comout)("Pick first corner\n");
56 if ((*dev->getcur)(&x0, &y0) == ABORT)
57 return(-1);
58 (*dev->comout)("Pick second corner\n");
59 if ((*dev->getcur)(&x1, &y1) == ABORT)
60 return(-1);
61 }
62 if (x0 < x1) {
63 r->l = x0;
64 r->r = x1;
65 } else {
66 r->l = x1;
67 r->r = x0;
68 }
69 if (y0 < y1) {
70 r->d = y0;
71 r->u = y1;
72 } else {
73 r->d = y1;
74 r->u = y0;
75 }
76 if (r->l < 0) r->l = 0;
77 if (r->d < 0) r->d = 0;
78 if (r->r > hresolu) r->r = hresolu;
79 if (r->u > vresolu) r->u = vresolu;
80 if (r->l > r->r) r->l = r->r;
81 if (r->d > r->u) r->d = r->u;
82 return(0);
83 }
84
85
86 int
87 getinterest( /* get area of interest */
88 char *s,
89 int direc,
90 FVECT vec,
91 double *mp
92 )
93 {
94 int x, y;
95 RAY thisray;
96 int i;
97
98 if (sscanf(s, "%lf", mp) != 1)
99 *mp = 1.0;
100 else if (*mp < -FTINY) /* negative zoom is reduction */
101 *mp = -1.0 / *mp;
102 else if (*mp <= FTINY) { /* too small */
103 error(COMMAND, "illegal magnification");
104 return(-1);
105 }
106 if (!sscanvec(sskip(s), vec)) {
107 if (dev->getcur == NULL)
108 return(-1);
109 (*dev->comout)("Pick view center\n");
110 if ((*dev->getcur)(&x, &y) == ABORT)
111 return(-1);
112 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
113 &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
114 error(COMMAND, "not on image");
115 return(-1);
116 }
117 if (!direc || ourview.type == VT_PAR) {
118 rayorigin(&thisray, PRIMARY, NULL, NULL);
119 while (localhit(&thisray, &thescene)) {
120 OBJREC *m = findmaterial(thisray.ro);
121 if (m != NULL && !istransp(m->otype) &&
122 !isBSDFproxy(m) &&
123 (thisray.clipset == NULL ||
124 !inset(thisray.clipset,
125 thisray.ro->omod)))
126 break; /* found something */
127 VCOPY(thisray.rorg, thisray.rop);
128 rayclear(&thisray); /* skip invisible */
129 }
130 if ((thisray.ro == NULL) | (thisray.ro == &Aftplane)) {
131 error(COMMAND, "not a local object");
132 return(-1);
133 }
134 }
135 if (direc)
136 if (ourview.type == VT_PAR)
137 for (i = 0; i < 3; i++)
138 vec[i] = thisray.rop[i] - ourview.vp[i];
139 else
140 VCOPY(vec, thisray.rdir);
141 else
142 VCOPY(vec, thisray.rop);
143 } else if (direc) {
144 for (i = 0; i < 3; i++)
145 vec[i] -= ourview.vp[i];
146 if (normalize(vec) == 0.0) {
147 error(COMMAND, "point at view origin");
148 return(-1);
149 }
150 }
151 return(0);
152 }
153
154
155 COLORV *
156 greyof( /* convert color to greyscale */
157 COLOR col
158 )
159 {
160 static COLOR gcol;
161 double b;
162
163 b = bright(col);
164 setcolor(gcol, b, b, b);
165 return(gcol);
166 }
167
168 static void
169 recolor( /* recolor the given node */
170 PNODE *p
171 )
172 {
173 while (p->kid != NULL) { /* need to propogate down */
174 int mx = (p->xmin + p->xmax) >> 1;
175 int my = (p->ymin + p->ymax) >> 1;
176 int ki;
177 if (p->x >= mx)
178 ki = (p->y >= my) ? UR : DR;
179 else
180 ki = (p->y >= my) ? UL : DL;
181 pcopy(p, p->kid+ki);
182 p = p->kid + ki;
183 }
184
185 (*dev->paintr)(greyscale?greyof(p->v):p->v,
186 p->xmin, p->ymin, p->xmax, p->ymax);
187 }
188
189 int
190 paint( /* compute and paint a rectangle */
191 PNODE *p
192 )
193 {
194 static RAY thisray;
195 double h, v;
196
197 if ((p->xmax <= p->xmin) | (p->ymax <= p->ymin)) { /* empty */
198 p->x = p->xmin;
199 p->y = p->ymin;
200 setcolor(p->v, 0.0, 0.0, 0.0);
201 return(0);
202 }
203 /* jitter ray direction */
204 p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
205 p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
206
207 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
208 h/hresolu, v/vresolu)) < -FTINY) {
209 setcolor(thisray.rcol, 0.0, 0.0, 0.0);
210 } else if (!ray_pnprocs) { /* immediate mode */
211 ray_trace(&thisray);
212 } else { /* queuing mode */
213 int rval;
214 rayorigin(&thisray, PRIMARY, NULL, NULL);
215 thisray.rno = (RNUMBER)p;
216 rval = ray_pqueue(&thisray);
217 if (!rval)
218 return(0);
219 if (rval < 0)
220 return(-1);
221 /* get node for returned ray */
222 p = (PNODE *)thisray.rno;
223 }
224
225 copycolor(p->v, thisray.rcol);
226 scalecolor(p->v, exposure);
227
228 recolor(p); /* paint it */
229
230 if (dev->flush != NULL) { /* shall we check for input? */
231 static RNUMBER lastflush = 0;
232 RNUMBER counter = raynum;
233 int flushintvl;
234 if (!ray_pnprocs) {
235 counter = nrays;
236 flushintvl = WFLUSH1;
237 } else if (ambounce == 0)
238 flushintvl = ray_pnprocs*WFLUSH;
239 else if (niflush < WFLUSH)
240 flushintvl = ray_pnprocs*niflush/(ambounce*(ambounce>0)+1);
241 else
242 flushintvl = ray_pnprocs*WFLUSH/(ambounce*(ambounce>0)+1);
243 if (lastflush > counter)
244 lastflush = 0; /* counter wrapped */
245
246 if (counter - lastflush >= flushintvl) {
247 lastflush = counter;
248 (*dev->flush)();
249 niflush++;
250 }
251 }
252 return(1);
253 }
254
255
256 int
257 waitrays(void) /* finish up pending rays */
258 {
259 int nwaited = 0;
260 int rval;
261 RAY raydone;
262
263 if (!ray_pnprocs) /* immediate mode? */
264 return(0);
265 while ((rval = ray_presult(&raydone, 0)) > 0) {
266 PNODE *p = (PNODE *)raydone.rno;
267 copycolor(p->v, raydone.rcol);
268 scalecolor(p->v, exposure);
269 recolor(p);
270 nwaited++;
271 }
272 if (rval < 0)
273 return(-1);
274 return(nwaited);
275 }
276
277
278 void
279 newimage( /* start a new image */
280 char *s
281 )
282 {
283 int newnp = 0;
284 /* # rendering procs arg? */
285 if (s != NULL)
286 sscanf(s, "%d", &newnp);
287 /* free old image */
288 freepkids(&ptrunk);
289 /* compute resolution */
290 hresolu = dev->xsiz;
291 vresolu = dev->ysiz;
292 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
293 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
294 ptrunk.xmax = pframe.r = hresolu;
295 ptrunk.ymax = pframe.u = vresolu;
296 pdepth = 0;
297 /* clear device */
298 (*dev->clear)(hresolu, vresolu);
299
300 if (newparam) { /* (re)start rendering procs */
301 if (ray_pnprocs)
302 ray_pclose(0); /* should already be closed */
303 if (newnp > 0)
304 nproc = newnp;
305 if (nproc > 1)
306 ray_popen(nproc);
307 newparam = 0;
308 } else if ((newnp > 0) & (newnp != nproc)) {
309 if (newnp == 1) /* change # rendering procs */
310 ray_pclose(0);
311 else if (newnp < ray_pnprocs)
312 ray_pclose(ray_pnprocs - newnp);
313 else
314 ray_popen(newnp - ray_pnprocs);
315 nproc = newnp;
316 }
317 niflush = 0; /* get first value */
318 paint(&ptrunk);
319 }
320
321
322 void
323 redraw(void) /* redraw the image */
324 {
325 (*dev->clear)(hresolu, vresolu);
326 (*dev->comout)("redrawing...\n");
327 repaint(0, 0, hresolu, vresolu);
328 (*dev->comout)("\n");
329 }
330
331
332 void
333 repaint( /* repaint a region */
334 int xmin,
335 int ymin,
336 int xmax,
337 int ymax
338 )
339 {
340 RECT reg;
341
342 reg.l = xmin; reg.r = xmax;
343 reg.d = ymin; reg.u = ymax;
344
345 paintrect(&ptrunk, &reg);
346 }
347
348
349 void
350 paintrect( /* paint picture rectangle */
351 PNODE *p,
352 RECT *r
353 )
354 {
355 int mx, my;
356
357 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
358 return;
359
360 if (p->kid == NULL) {
361 (*dev->paintr)(greyscale?greyof(p->v):p->v,
362 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
363 return;
364 }
365 mx = (p->xmin + p->xmax) >> 1; /* do kids */
366 my = (p->ymin + p->ymax) >> 1;
367 if (mx > r->l) {
368 if (my > r->d)
369 paintrect(p->kid+DL, r);
370 if (my < r->u)
371 paintrect(p->kid+UL, r);
372 }
373 if (mx < r->r) {
374 if (my > r->d)
375 paintrect(p->kid+DR, r);
376 if (my < r->u)
377 paintrect(p->kid+UR, r);
378 }
379 }
380
381
382 PNODE *
383 findrect( /* find a rectangle */
384 int x,
385 int y,
386 PNODE *p,
387 int pd
388 )
389 {
390 int mx, my;
391
392 while (p->kid != NULL && pd--) {
393
394 mx = (p->xmin + p->xmax) >> 1;
395 my = (p->ymin + p->ymax) >> 1;
396
397 if (x < mx) {
398 if (y < my) {
399 p = p->kid+DL;
400 } else {
401 p = p->kid+UL;
402 }
403 } else {
404 if (y < my) {
405 p = p->kid+DR;
406 } else {
407 p = p->kid+UR;
408 }
409 }
410 }
411 return(p);
412 }
413
414
415 void
416 compavg( /* recompute averages */
417 PNODE *p
418 )
419 {
420 int i, navg;
421
422 if (p->kid == NULL)
423 return;
424
425 setcolor(p->v, .0, .0, .0);
426 navg = 0;
427 for (i = 0; i < 4; i++) {
428 if (p->kid[i].xmin >= p->kid[i].xmax) continue;
429 if (p->kid[i].ymin >= p->kid[i].ymax) continue;
430 compavg(p->kid+i);
431 addcolor(p->v, p->kid[i].v);
432 navg++;
433 }
434 if (navg > 1)
435 scalecolor(p->v, 1./navg);
436 }
437
438
439 void
440 scalepict( /* scale picture values */
441 PNODE *p,
442 double sf
443 )
444 {
445 scalecolor(p->v, sf); /* do this node */
446
447 if (p->kid == NULL)
448 return;
449 /* do children */
450 scalepict(p->kid+DL, sf);
451 scalepict(p->kid+DR, sf);
452 scalepict(p->kid+UL, sf);
453 scalepict(p->kid+UR, sf);
454 }
455
456
457 void
458 getpictcolrs( /* get scanline from picture */
459 int yoff,
460 COLR *scan,
461 PNODE *p,
462 int xsiz,
463 int ysiz
464 )
465 {
466 int mx;
467 int my;
468
469 if (p->kid == NULL) { /* do this node */
470 setcolr(scan[0], colval(p->v,RED),
471 colval(p->v,GRN),
472 colval(p->v,BLU));
473 for (mx = 1; mx < xsiz; mx++)
474 copycolr(scan[mx], scan[0]);
475 return;
476 }
477 /* do kids */
478 mx = xsiz >> 1;
479 my = ysiz >> 1;
480 if (yoff < my) {
481 getpictcolrs(yoff, scan, p->kid+DL, mx, my);
482 getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
483 } else {
484 getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
485 getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
486 }
487 }
488
489
490 void
491 freepkids( /* free pnode's children */
492 PNODE *p
493 )
494 {
495 if (p->kid == NULL)
496 return;
497 freepkids(p->kid+DL);
498 freepkids(p->kid+DR);
499 freepkids(p->kid+UL);
500 freepkids(p->kid+UR);
501 free((void *)p->kid);
502 p->kid = NULL;
503 }
504
505
506 void
507 newview( /* change viewing parameters */
508 VIEW *vp
509 )
510 {
511 char *err;
512
513 if ((err = setview(vp)) != NULL) {
514 sprintf(errmsg, "view not set - %s", err);
515 error(COMMAND, errmsg);
516 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
517 oldview = ourview;
518 ourview = *vp;
519 newimage(NULL);
520 }
521 }
522
523
524 void
525 moveview( /* move viewpoint */
526 double angle,
527 double elev,
528 double mag,
529 FVECT vc
530 )
531 {
532 double d;
533 VIEW nv = ourview;
534 int i;
535
536 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
537 if (elev != 0.0)
538 geodesic(nv.vdir, nv.vdir, nv.vup, elev*(-PI/180.), GEOD_RAD);
539
540 if (nv.type == VT_PAR) {
541 nv.horiz /= mag;
542 nv.vert /= mag;
543 d = 0.0; /* don't move closer */
544 for (i = 0; i < 3; i++)
545 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
546 } else {
547 d = sqrt(dist2(ourview.vp, vc)) / mag;
548 if (nv.vfore > FTINY) {
549 nv.vfore += d - d*mag;
550 if (nv.vfore < 0.0) nv.vfore = 0.0;
551 }
552 if (nv.vaft > FTINY) {
553 nv.vaft += d - d*mag;
554 if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
555 }
556 nv.vdist /= mag;
557 }
558 for (i = 0; i < 3; i++)
559 nv.vp[i] = vc[i] - d*nv.vdir[i];
560 newview(&nv);
561 }
562
563
564 void
565 pcopy( /* copy paint node p1 into p2 */
566 PNODE *p1,
567 PNODE *p2
568 )
569 {
570 copycolor(p2->v, p1->v);
571 p2->x = p1->x;
572 p2->y = p1->y;
573 }
574
575
576 void
577 zoomview( /* zoom in or out */
578 VIEW *vp,
579 double zf
580 )
581 {
582 switch (vp->type) {
583 case VT_PAR: /* parallel view */
584 vp->horiz /= zf;
585 vp->vert /= zf;
586 return;
587 case VT_ANG: /* angular fisheye */
588 vp->horiz /= zf;
589 if (vp->horiz > 360.)
590 vp->horiz = 360.;
591 vp->vert /= zf;
592 if (vp->vert > 360.)
593 vp->vert = 360.;
594 return;
595 case VT_PLS: /* planisphere fisheye */
596 vp->horiz = sin((PI/180./2.)*vp->horiz) /
597 (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
598 vp->horiz *= vp->horiz;
599 vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
600 (1. + vp->horiz));
601 vp->vert = sin((PI/180./2.)*vp->vert) /
602 (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
603 vp->vert *= vp->vert;
604 vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
605 (1. + vp->vert));
606 return;
607 case VT_CYL: /* cylindrical panorama */
608 vp->horiz /= zf;
609 if (vp->horiz > 360.)
610 vp->horiz = 360.;
611 vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
612 return;
613 case VT_PER: /* perspective view */
614 vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /
615 (PI/180./2.);
616 vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) /
617 (PI/180./2.);
618 return;
619 case VT_HEM: /* hemispherical fisheye */
620 vp->horiz = sin(vp->horiz*(PI/180./2.))/zf;
621 if (vp->horiz >= 1.0-FTINY)
622 vp->horiz = 180.;
623 else
624 vp->horiz = asin(vp->horiz) / (PI/180./2.);
625 vp->vert = sin(vp->vert*(PI/180./2.))/zf;
626 if (vp->vert >= 1.0-FTINY)
627 vp->vert = 180.;
628 else
629 vp->vert = asin(vp->vert) / (PI/180./2.);
630 return;
631 }
632 }