ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.43
Committed: Thu Jan 23 19:20:54 2020 UTC (4 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3
Changes since 2.42: +25 -18 lines
Log Message:
Changed behavior of object picking to choose transparent surfaces if nothing behind

File Contents

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