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

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.42 static const char RCSid[] = "$Id: rv3.c,v 2.41 2018/11/07 18:34:58 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     int x, y;
95     RAY thisray;
96 greg 2.22 int i;
97 greg 1.7
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 greg 2.2 if (!sscanvec(sskip(s), vec)) {
107 greg 1.7 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 greg 2.6 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
113     &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
114 greg 1.17 error(COMMAND, "not on image");
115     return(-1);
116     }
117 greg 1.7 if (!direc || ourview.type == VT_PAR) {
118 greg 2.17 rayorigin(&thisray, PRIMARY, NULL, NULL);
119 greg 2.41 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 greg 2.42 if ((thisray.ro == NULL) | (thisray.ro == &Aftplane)) {
131 greg 1.7 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 greg 2.15 } 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 greg 1.7 return(0);
152     }
153 greg 1.1
154    
155 greg 2.39 COLORV *
156 greg 2.22 greyof( /* convert color to greyscale */
157     COLOR col
158     )
159 greg 1.1 {
160     static COLOR gcol;
161     double b;
162    
163     b = bright(col);
164     setcolor(gcol, b, b, b);
165     return(gcol);
166     }
167    
168 greg 2.26 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 greg 1.1
189 greg 2.22 int
190     paint( /* compute and paint a rectangle */
191     PNODE *p
192     )
193 greg 1.1 {
194     static RAY thisray;
195     double h, v;
196    
197 greg 2.29 if ((p->xmax <= p->xmin) | (p->ymax <= p->ymin)) { /* empty */
198 greg 2.22 p->x = p->xmin;
199     p->y = p->ymin;
200 greg 1.1 setcolor(p->v, 0.0, 0.0, 0.0);
201 greg 2.22 return(0);
202 greg 1.1 }
203     /* jitter ray direction */
204 greg 2.22 p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
205     p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
206 greg 1.1
207 greg 2.6 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
208     h/hresolu, v/vresolu)) < -FTINY) {
209 greg 1.17 setcolor(thisray.rcol, 0.0, 0.0, 0.0);
210 greg 2.37 } else if (!ray_pnprocs) { /* immediate mode */
211 greg 2.29 ray_trace(&thisray);
212     } else { /* queuing mode */
213     int rval;
214 greg 2.17 rayorigin(&thisray, PRIMARY, NULL, NULL);
215 greg 2.32 thisray.rno = (RNUMBER)p;
216 greg 2.22 rval = ray_pqueue(&thisray);
217     if (!rval)
218     return(0);
219     if (rval < 0)
220     return(-1);
221 greg 2.33 /* get node for returned ray */
222 greg 2.22 p = (PNODE *)thisray.rno;
223 greg 1.17 }
224 greg 1.1
225     copycolor(p->v, thisray.rcol);
226     scalecolor(p->v, exposure);
227    
228 greg 2.26 recolor(p); /* paint it */
229 greg 1.13
230 greg 2.29 if (dev->flush != NULL) { /* shall we check for input? */
231 greg 2.32 static RNUMBER lastflush = 0;
232     RNUMBER counter = raynum;
233     int flushintvl;
234 greg 2.37 if (!ray_pnprocs) {
235 greg 2.29 counter = nrays;
236     flushintvl = WFLUSH1;
237     } else if (ambounce == 0)
238 greg 2.37 flushintvl = ray_pnprocs*WFLUSH;
239 greg 2.29 else if (niflush < WFLUSH)
240 greg 2.40 flushintvl = ray_pnprocs*niflush/(ambounce*(ambounce>0)+1);
241 greg 2.29 else
242 greg 2.40 flushintvl = ray_pnprocs*WFLUSH/(ambounce*(ambounce>0)+1);
243 greg 2.31 if (lastflush > counter)
244     lastflush = 0; /* counter wrapped */
245 greg 2.29
246     if (counter - lastflush >= flushintvl) {
247     lastflush = counter;
248     (*dev->flush)();
249     niflush++;
250     }
251 greg 1.13 }
252 greg 2.22 return(1);
253     }
254    
255    
256     int
257 greg 2.24 waitrays(void) /* finish up pending rays */
258 greg 2.22 {
259     int nwaited = 0;
260     int rval;
261     RAY raydone;
262 greg 2.29
263 greg 2.37 if (!ray_pnprocs) /* immediate mode? */
264 greg 2.29 return(0);
265 greg 2.22 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 greg 2.26 recolor(p);
270 greg 2.22 nwaited++;
271     }
272     if (rval < 0)
273     return(-1);
274     return(nwaited);
275 greg 1.1 }
276    
277    
278 greg 2.10 void
279 greg 2.22 newimage( /* start a new image */
280     char *s
281     )
282 greg 1.1 {
283 greg 2.35 int newnp = 0;
284     /* # rendering procs arg? */
285     if (s != NULL)
286     sscanf(s, "%d", &newnp);
287 greg 1.1 /* free old image */
288     freepkids(&ptrunk);
289 greg 1.9 /* compute resolution */
290 greg 1.10 hresolu = dev->xsiz;
291     vresolu = dev->ysiz;
292 greg 1.11 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
293 greg 2.22 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
294     ptrunk.xmax = pframe.r = hresolu;
295     ptrunk.ymax = pframe.u = vresolu;
296 greg 1.1 pdepth = 0;
297     /* clear device */
298 greg 1.9 (*dev->clear)(hresolu, vresolu);
299 greg 2.22
300     if (newparam) { /* (re)start rendering procs */
301 greg 2.37 if (ray_pnprocs)
302     ray_pclose(0); /* should already be closed */
303 greg 2.35 if (newnp > 0)
304     nproc = newnp;
305 greg 2.29 if (nproc > 1)
306     ray_popen(nproc);
307 greg 2.22 newparam = 0;
308 greg 2.35 } 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 greg 2.22 }
317 greg 2.23 niflush = 0; /* get first value */
318 greg 2.22 paint(&ptrunk);
319 greg 1.1 }
320    
321    
322 greg 2.10 void
323 greg 2.22 redraw(void) /* redraw the image */
324 greg 1.1 {
325 greg 1.9 (*dev->clear)(hresolu, vresolu);
326 greg 1.1 (*dev->comout)("redrawing...\n");
327 greg 1.9 repaint(0, 0, hresolu, vresolu);
328 greg 1.1 (*dev->comout)("\n");
329     }
330    
331    
332 greg 2.10 void
333 greg 2.22 repaint( /* repaint a region */
334     int xmin,
335     int ymin,
336     int xmax,
337     int ymax
338     )
339 greg 1.1 {
340     RECT reg;
341    
342     reg.l = xmin; reg.r = xmax;
343     reg.d = ymin; reg.u = ymax;
344    
345 greg 2.22 paintrect(&ptrunk, &reg);
346 greg 1.1 }
347    
348    
349 greg 2.10 void
350 greg 2.22 paintrect( /* paint picture rectangle */
351     PNODE *p,
352     RECT *r
353     )
354 greg 1.1 {
355     int mx, my;
356    
357 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
358 greg 1.1 return;
359    
360     if (p->kid == NULL) {
361     (*dev->paintr)(greyscale?greyof(p->v):p->v,
362 greg 2.22 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
363 greg 1.1 return;
364     }
365 greg 2.22 mx = (p->xmin + p->xmax) >> 1; /* do kids */
366     my = (p->ymin + p->ymax) >> 1;
367 greg 1.1 if (mx > r->l) {
368     if (my > r->d)
369 greg 2.22 paintrect(p->kid+DL, r);
370 greg 1.1 if (my < r->u)
371 greg 2.22 paintrect(p->kid+UL, r);
372 greg 1.1 }
373     if (mx < r->r) {
374     if (my > r->d)
375 greg 2.22 paintrect(p->kid+DR, r);
376 greg 1.1 if (my < r->u)
377 greg 2.22 paintrect(p->kid+UR, r);
378 greg 1.1 }
379     }
380    
381    
382     PNODE *
383 greg 2.22 findrect( /* find a rectangle */
384     int x,
385     int y,
386     PNODE *p,
387     int pd
388     )
389 greg 1.1 {
390     int mx, my;
391    
392     while (p->kid != NULL && pd--) {
393    
394 greg 2.22 mx = (p->xmin + p->xmax) >> 1;
395     my = (p->ymin + p->ymax) >> 1;
396 greg 1.1
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 greg 2.10 void
416 greg 2.27 compavg( /* recompute averages */
417     PNODE *p
418     )
419     {
420 greg 2.28 int i, navg;
421    
422 greg 2.27 if (p->kid == NULL)
423     return;
424 greg 2.28
425 greg 2.27 setcolor(p->v, .0, .0, .0);
426 greg 2.28 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 greg 2.27 }
437    
438    
439     void
440 greg 2.22 scalepict( /* scale picture values */
441     PNODE *p,
442     double sf
443     )
444 greg 1.1 {
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 greg 2.10 void
458 greg 2.22 getpictcolrs( /* get scanline from picture */
459     int yoff,
460     COLR *scan,
461     PNODE *p,
462     int xsiz,
463     int ysiz
464     )
465 greg 1.1 {
466 greg 2.22 int mx;
467 greg 1.1 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 greg 2.10 void
491 greg 2.22 freepkids( /* free pnode's children */
492     PNODE *p
493     )
494 greg 1.1 {
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 greg 2.10 free((void *)p->kid);
502 greg 1.1 p->kid = NULL;
503     }
504    
505    
506 greg 2.10 void
507 greg 2.22 newview( /* change viewing parameters */
508     VIEW *vp
509     )
510 greg 1.1 {
511     char *err;
512    
513 greg 1.6 if ((err = setview(vp)) != NULL) {
514 greg 1.1 sprintf(errmsg, "view not set - %s", err);
515     error(COMMAND, errmsg);
516 schorsch 2.12 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
517 schorsch 2.13 oldview = ourview;
518     ourview = *vp;
519 greg 2.22 newimage(NULL);
520 greg 1.1 }
521     }
522    
523    
524 greg 2.10 void
525 greg 2.22 moveview( /* move viewpoint */
526     double angle,
527     double elev,
528     double mag,
529     FVECT vc
530     )
531 greg 1.1 {
532     double d;
533 greg 2.14 VIEW nv = ourview;
534 greg 2.22 int i;
535 greg 1.1
536     spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
537 greg 2.38 if (elev != 0.0)
538     geodesic(nv.vdir, nv.vdir, nv.vup, elev*(-PI/180.), GEOD_RAD);
539    
540 greg 2.15 if (nv.type == VT_PAR) {
541     nv.horiz /= mag;
542     nv.vert /= mag;
543 greg 1.3 d = 0.0; /* don't move closer */
544 greg 1.2 for (i = 0; i < 3; i++)
545 greg 1.3 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
546 greg 1.1 } else {
547 greg 1.2 d = sqrt(dist2(ourview.vp, vc)) / mag;
548 greg 2.15 if (nv.vfore > FTINY) {
549 greg 2.8 nv.vfore += d - d*mag;
550     if (nv.vfore < 0.0) nv.vfore = 0.0;
551     }
552 greg 2.15 if (nv.vaft > FTINY) {
553 greg 2.8 nv.vaft += d - d*mag;
554     if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
555     }
556 greg 2.16 nv.vdist /= mag;
557 greg 1.1 }
558 greg 1.2 for (i = 0; i < 3; i++)
559     nv.vp[i] = vc[i] - d*nv.vdir[i];
560 greg 1.1 newview(&nv);
561 greg 1.17 }
562    
563    
564 greg 2.10 void
565 greg 2.22 pcopy( /* copy paint node p1 into p2 */
566     PNODE *p1,
567     PNODE *p2
568     )
569 greg 2.10 {
570     copycolor(p2->v, p1->v);
571     p2->x = p1->x;
572     p2->y = p1->y;
573     }
574    
575    
576     void
577 greg 2.22 zoomview( /* zoom in or out */
578     VIEW *vp,
579     double zf
580     )
581 greg 1.17 {
582     switch (vp->type) {
583     case VT_PAR: /* parallel view */
584 greg 2.9 vp->horiz /= zf;
585     vp->vert /= zf;
586     return;
587 greg 1.17 case VT_ANG: /* angular fisheye */
588     vp->horiz /= zf;
589 greg 2.9 if (vp->horiz > 360.)
590     vp->horiz = 360.;
591 greg 1.17 vp->vert /= zf;
592 greg 2.9 if (vp->vert > 360.)
593     vp->vert = 360.;
594     return;
595 greg 2.21 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 greg 2.9 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 greg 1.17 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 greg 1.1 }