ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.41
Committed: Wed Nov 7 18:34:58 2018 UTC (5 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.40: +16 -2 lines
Log Message:
Made picking of visible points in scene more reliable in rvu

File Contents

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