ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.80
Committed: Thu Mar 12 17:19:18 2020 UTC (4 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.79: +2 -2 lines
Log Message:
Added precautionary testing when checking for FHUGE

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: raytrace.c,v 2.79 2019/07/25 16:50:54 greg Exp $";
3 #endif
4 /*
5 * raytrace.c - routines for tracing and shading rays.
6 *
7 * External symbols declared in ray.h
8 */
9
10 #include "copyright.h"
11
12 #include "ray.h"
13 #include "source.h"
14 #include "otypes.h"
15 #include "otspecial.h"
16 #include "random.h"
17 #include "pmap.h"
18
19 #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */
20
21 RNUMBER raynum = 0; /* next unique ray number */
22 RNUMBER nrays = 0; /* number of calls to localhit */
23
24 static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
25 OBJREC Lamb = {
26 OVOID, MAT_PLASTIC, "Lambertian",
27 {NULL, Lambfa, 0, 5}, NULL
28 }; /* a Lambertian surface */
29
30 OBJREC Aftplane; /* aft clipping plane object */
31
32 #define RAYHIT (-1) /* return value for intercepted ray */
33
34 static int raymove(FVECT pos, OBJECT *cxs, int dirf, RAY *r, CUBE *cu);
35 static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs);
36 static void checkset(OBJECT *os, OBJECT *cs);
37
38
39 int
40 rayorigin( /* start new ray from old one */
41 RAY *r,
42 int rt,
43 const RAY *ro,
44 const COLOR rc
45 )
46 {
47 double rw, re;
48 /* assign coefficient/weight */
49 if (rc == NULL) {
50 rw = 1.0;
51 setcolor(r->rcoef, 1., 1., 1.);
52 } else {
53 rw = intens(rc);
54 if (rw > 1.0)
55 rw = 1.0; /* avoid calculation growth */
56 if (rc != r->rcoef)
57 copycolor(r->rcoef, rc);
58 }
59 if ((r->parent = ro) == NULL) { /* primary ray */
60 r->rlvl = 0;
61 r->rweight = rw;
62 r->crtype = r->rtype = rt;
63 r->rsrc = -1;
64 r->clipset = NULL;
65 r->revf = raytrace;
66 copycolor(r->cext, cextinction);
67 copycolor(r->albedo, salbedo);
68 r->gecc = seccg;
69 r->slights = NULL;
70 } else { /* spawned ray */
71 if (ro->rot >= FHUGE*.99) {
72 memset(r, 0, sizeof(RAY));
73 return(-1); /* illegal continuation */
74 }
75 r->rlvl = ro->rlvl;
76 if (rt & RAYREFL) {
77 r->rlvl++;
78 r->rsrc = -1;
79 r->clipset = ro->clipset;
80 r->rmax = 0.0;
81 } else {
82 r->rsrc = ro->rsrc;
83 r->clipset = ro->newcset;
84 r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
85 }
86 r->revf = ro->revf;
87 copycolor(r->cext, ro->cext);
88 copycolor(r->albedo, ro->albedo);
89 r->gecc = ro->gecc;
90 r->slights = ro->slights;
91 r->crtype = ro->crtype | (r->rtype = rt);
92 VCOPY(r->rorg, ro->rop);
93 r->rweight = ro->rweight * rw;
94 /* estimate extinction */
95 re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
96 colval(ro->cext,RED) : colval(ro->cext,GRN);
97 if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
98 re *= ro->rot;
99 if (re > 0.1) {
100 if (re > 92.) {
101 r->rweight = 0.0;
102 } else {
103 r->rweight *= exp(-re);
104 }
105 }
106 }
107 rayclear(r);
108 if (r->rweight <= 0.0) /* check for expiration */
109 return(-1);
110 if (r->crtype & SHADOW) /* shadow commitment */
111 return(0);
112 /* ambient in photon map? */
113 if (ro != NULL && ro->crtype & AMBIENT) {
114 if (causticPhotonMapping)
115 return(-1);
116 if (photonMapping && rt != TRANS)
117 return(-1);
118 }
119 if ((maxdepth <= 0) & (rc != NULL)) { /* Russian roulette */
120 if (minweight <= 0.0)
121 error(USER, "zero ray weight in Russian roulette");
122 if ((maxdepth < 0) & (r->rlvl > -maxdepth))
123 return(-1); /* upper reflection limit */
124 if (r->rweight >= minweight)
125 return(0);
126 if (frandom() > r->rweight/minweight)
127 return(-1);
128 rw = minweight/r->rweight; /* promote survivor */
129 scalecolor(r->rcoef, rw);
130 r->rweight = minweight;
131 return(0);
132 }
133 return((r->rweight >= minweight) & (r->rlvl <= abs(maxdepth)) ? 0 : -1);
134 }
135
136
137 void
138 rayclear( /* clear a ray for (re)evaluation */
139 RAY *r
140 )
141 {
142 r->rno = raynum++;
143 r->newcset = r->clipset;
144 r->hitf = rayhit;
145 r->robj = OVOID;
146 r->ro = NULL;
147 r->rox = NULL;
148 r->rxt = r->rmt = r->rot = FHUGE;
149 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
150 r->rflips = 0;
151 r->uv[0] = r->uv[1] = 0.0;
152 setcolor(r->pcol, 1.0, 1.0, 1.0);
153 setcolor(r->mcol, 0.0, 0.0, 0.0);
154 setcolor(r->rcol, 0.0, 0.0, 0.0);
155 }
156
157
158 void
159 raytrace( /* trace a ray and compute its value */
160 RAY *r
161 )
162 {
163 if (localhit(r, &thescene))
164 raycont(r); /* hit local surface, evaluate */
165 else if (r->ro == &Aftplane) {
166 r->ro = NULL; /* hit aft clipping plane */
167 r->rot = FHUGE;
168 } else if (sourcehit(r))
169 rayshade(r, r->ro->omod); /* distant source */
170
171 if (trace != NULL)
172 (*trace)(r); /* trace execution */
173
174 rayparticipate(r); /* for participating medium */
175 }
176
177
178 void
179 raycont( /* check for clipped object and continue */
180 RAY *r
181 )
182 {
183 if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
184 !rayshade(r, r->ro->omod))
185 raytrans(r);
186 }
187
188
189 void
190 raytrans( /* transmit ray as is */
191 RAY *r
192 )
193 {
194 RAY tr;
195
196 rayorigin(&tr, TRANS, r, NULL); /* always continue */
197 VCOPY(tr.rdir, r->rdir);
198 rayvalue(&tr);
199 copycolor(r->mcol, tr.mcol);
200 copycolor(r->rcol, tr.rcol);
201 r->rmt = r->rot + tr.rmt;
202 r->rxt = r->rot + tr.rxt;
203 }
204
205
206 int
207 raytirrad( /* irradiance hack */
208 OBJREC *m,
209 RAY *r
210 )
211 {
212 if (ofun[m->otype].flags & (T_M|T_X) && m->otype != MAT_CLIP) {
213 if (istransp(m->otype) || isBSDFproxy(m)) {
214 raytrans(r);
215 return(1);
216 }
217 if (!islight(m->otype))
218 return((*ofun[Lamb.otype].funp)(&Lamb, r));
219 }
220 return(0); /* not a qualifying surface */
221 }
222
223
224 int
225 rayshade( /* shade ray r with material mod */
226 RAY *r,
227 int mod
228 )
229 {
230 int tst_irrad = do_irrad && !(r->crtype & ~(PRIMARY|TRANS));
231 OBJREC *m;
232
233 r->rxt = r->rot; /* preset effective ray length */
234 for ( ; mod != OVOID; mod = m->omod) {
235 m = objptr(mod);
236 /****** unnecessary test since modifier() is always called
237 if (!ismodifier(m->otype)) {
238 sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
239 error(USER, errmsg);
240 }
241 ******/
242 /* hack for irradiance calculation */
243 if (tst_irrad && raytirrad(m, r))
244 return(1);
245
246 if ((*ofun[m->otype].funp)(m, r))
247 return(1); /* materials call raytexture() */
248 }
249 return(0); /* no material! */
250 }
251
252
253 void
254 rayparticipate( /* compute ray medium participation */
255 RAY *r
256 )
257 {
258 COLOR ce, ca;
259 double re, ge, be;
260
261 if (intens(r->cext) <= 1./FHUGE)
262 return; /* no medium */
263 re = r->rot*colval(r->cext,RED);
264 ge = r->rot*colval(r->cext,GRN);
265 be = r->rot*colval(r->cext,BLU);
266 if (r->crtype & SHADOW) { /* no scattering for sources */
267 re *= 1. - colval(r->albedo,RED);
268 ge *= 1. - colval(r->albedo,GRN);
269 be *= 1. - colval(r->albedo,BLU);
270 }
271 setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re),
272 ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge),
273 be<=FTINY ? 1. : be>92. ? 0. : exp(-be));
274 multcolor(r->rcol, ce); /* path extinction */
275 if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
276 return; /* no scattering */
277
278 /* PMAP: indirect inscattering accounted for by volume photons? */
279 if (!volumePhotonMapping) {
280 setcolor(ca,
281 colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
282 colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
283 colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
284 addcolor(r->rcol, ca); /* ambient in scattering */
285 }
286
287 srcscatter(r); /* source in scattering */
288 }
289
290
291 void
292 raytexture( /* get material modifiers */
293 RAY *r,
294 OBJECT mod
295 )
296 {
297 OBJREC *m;
298 /* execute textures and patterns */
299 for ( ; mod != OVOID; mod = m->omod) {
300 m = objptr(mod);
301 /****** unnecessary test since modifier() is always called
302 if (!ismodifier(m->otype)) {
303 sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
304 error(USER, errmsg);
305 }
306 ******/
307 if ((*ofun[m->otype].funp)(m, r)) {
308 sprintf(errmsg, "conflicting material \"%s\"",
309 m->oname);
310 objerror(r->ro, USER, errmsg);
311 }
312 }
313 }
314
315
316 int
317 raymixture( /* mix modifiers */
318 RAY *r,
319 OBJECT fore,
320 OBJECT back,
321 double coef
322 )
323 {
324 RAY fr, br;
325 double mfore, mback;
326 int foremat, backmat;
327 int i;
328 /* bound coefficient */
329 if (coef > 1.0)
330 coef = 1.0;
331 else if (coef < 0.0)
332 coef = 0.0;
333 /* compute foreground and background */
334 foremat = backmat = 0;
335 /* foreground */
336 fr = *r;
337 if (coef > FTINY) {
338 fr.rweight *= coef;
339 scalecolor(fr.rcoef, coef);
340 foremat = rayshade(&fr, fore);
341 }
342 /* background */
343 br = *r;
344 if (coef < 1.0-FTINY) {
345 br.rweight *= 1.0-coef;
346 scalecolor(br.rcoef, 1.0-coef);
347 backmat = rayshade(&br, back);
348 }
349 /* check for transparency */
350 if (backmat ^ foremat) {
351 if (backmat && coef > FTINY)
352 raytrans(&fr);
353 else if (foremat && coef < 1.0-FTINY)
354 raytrans(&br);
355 }
356 /* mix perturbations */
357 for (i = 0; i < 3; i++)
358 r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
359 /* mix pattern colors */
360 scalecolor(fr.pcol, coef);
361 scalecolor(br.pcol, 1.0-coef);
362 copycolor(r->pcol, fr.pcol);
363 addcolor(r->pcol, br.pcol);
364 /* return value tells if material */
365 if (!foremat & !backmat)
366 return(0);
367 /* mix returned ray values */
368 scalecolor(fr.rcol, coef);
369 scalecolor(br.rcol, 1.0-coef);
370 copycolor(r->rcol, fr.rcol);
371 addcolor(r->rcol, br.rcol);
372 scalecolor(fr.mcol, coef);
373 scalecolor(br.mcol, 1.0-coef);
374 copycolor(r->mcol, fr.mcol);
375 addcolor(r->mcol, br.mcol);
376 mfore = bright(fr.mcol); mback = bright(br.mcol);
377 r->rmt = mfore > mback ? fr.rmt : br.rmt;
378 r->rxt = bright(fr.rcol)-mfore > bright(br.rcol)-mback ?
379 fr.rxt : br.rxt;
380 return(1);
381 }
382
383
384 double
385 raydist( /* compute (cumulative) ray distance */
386 const RAY *r,
387 int flags
388 )
389 {
390 double sum = 0.0;
391
392 while (r != NULL && r->crtype&flags) {
393 sum += r->rot;
394 r = r->parent;
395 }
396 return(sum);
397 }
398
399
400 void
401 raycontrib( /* compute (cumulative) ray contribution */
402 RREAL rc[3],
403 const RAY *r,
404 int flags
405 )
406 {
407 static int warnedPM = 0;
408
409 rc[0] = rc[1] = rc[2] = 1.;
410
411 while (r != NULL && r->crtype&flags) {
412 int i = 3;
413 while (i--)
414 rc[i] *= colval(r->rcoef,i);
415 /* check for participating medium */
416 if (!warnedPM && (bright(r->cext) > FTINY) |
417 (bright(r->albedo) > FTINY)) {
418 error(WARNING,
419 "ray contribution calculation does not support participating media");
420 warnedPM++;
421 }
422 r = r->parent;
423 }
424 }
425
426
427 double
428 raynormal( /* compute perturbed normal for ray */
429 FVECT norm,
430 RAY *r
431 )
432 {
433 double newdot;
434 int i;
435
436 /* The perturbation is added to the surface normal to obtain
437 * the new normal. If the new normal would affect the surface
438 * orientation wrt. the ray, a correction is made. The method is
439 * still fraught with problems since reflected rays and similar
440 * directions calculated from the surface normal may spawn rays behind
441 * the surface. The only solution is to curb textures at high
442 * incidence (namely, keep DOT(rdir,pert) < Rdot).
443 */
444
445 for (i = 0; i < 3; i++)
446 norm[i] = r->ron[i] + r->pert[i];
447
448 if (normalize(norm) == 0.0) {
449 objerror(r->ro, WARNING, "illegal normal perturbation");
450 VCOPY(norm, r->ron);
451 return(r->rod);
452 }
453 newdot = -DOT(norm, r->rdir);
454 if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */
455 for (i = 0; i < 3; i++)
456 norm[i] += 2.0*newdot*r->rdir[i];
457 newdot = -newdot;
458 }
459 return(newdot);
460 }
461
462
463 void
464 newrayxf( /* get new tranformation matrix for ray */
465 RAY *r
466 )
467 {
468 static struct xfn {
469 struct xfn *next;
470 FULLXF xf;
471 } xfseed = { &xfseed }, *xflast = &xfseed;
472 struct xfn *xp;
473 const RAY *rp;
474
475 /*
476 * Search for transform in circular list that
477 * has no associated ray in the tree.
478 */
479 xp = xflast;
480 for (rp = r->parent; rp != NULL; rp = rp->parent)
481 if (rp->rox == &xp->xf) { /* xp in use */
482 xp = xp->next; /* move to next */
483 if (xp == xflast) { /* need new one */
484 xp = (struct xfn *)bmalloc(sizeof(struct xfn));
485 if (xp == NULL)
486 error(SYSTEM,
487 "out of memory in newrayxf");
488 /* insert in list */
489 xp->next = xflast->next;
490 xflast->next = xp;
491 break; /* we're done */
492 }
493 rp = r; /* start check over */
494 }
495 /* got it */
496 r->rox = &xp->xf;
497 xflast = xp;
498 }
499
500
501 void
502 flipsurface( /* reverse surface orientation */
503 RAY *r
504 )
505 {
506 r->rod = -r->rod;
507 r->ron[0] = -r->ron[0];
508 r->ron[1] = -r->ron[1];
509 r->ron[2] = -r->ron[2];
510 r->pert[0] = -r->pert[0];
511 r->pert[1] = -r->pert[1];
512 r->pert[2] = -r->pert[2];
513 r->rflips++;
514 }
515
516
517 void
518 rayhit( /* standard ray hit test */
519 OBJECT *oset,
520 RAY *r
521 )
522 {
523 OBJREC *o;
524 int i;
525
526 for (i = oset[0]; i > 0; i--) {
527 o = objptr(oset[i]);
528 if ((*ofun[o->otype].funp)(o, r))
529 r->robj = oset[i];
530 }
531 }
532
533
534 int
535 localhit( /* check for hit in the octree */
536 RAY *r,
537 CUBE *scene
538 )
539 {
540 OBJECT cxset[MAXCSET+1]; /* set of checked objects */
541 FVECT curpos; /* current cube position */
542 int sflags; /* sign flags */
543 double t, dt;
544 int i;
545
546 nrays++; /* increment trace counter */
547 sflags = 0;
548 for (i = 0; i < 3; i++) {
549 curpos[i] = r->rorg[i];
550 if (r->rdir[i] > 1e-7)
551 sflags |= 1 << i;
552 else if (r->rdir[i] < -1e-7)
553 sflags |= 0x10 << i;
554 }
555 if (!sflags) {
556 error(WARNING, "zero ray direction in localhit");
557 return(0);
558 }
559 /* start off assuming nothing hit */
560 if (r->rmax > FTINY) { /* except aft plane if one */
561 r->ro = &Aftplane;
562 r->rot = r->rmax;
563 VSUM(r->rop, r->rorg, r->rdir, r->rot);
564 }
565 /* find global cube entrance point */
566 t = 0.0;
567 if (!incube(scene, curpos)) {
568 /* find distance to entry */
569 for (i = 0; i < 3; i++) {
570 /* plane in our direction */
571 if (sflags & 1<<i)
572 dt = scene->cuorg[i];
573 else if (sflags & 0x10<<i)
574 dt = scene->cuorg[i] + scene->cusize;
575 else
576 continue;
577 /* distance to the plane */
578 dt = (dt - r->rorg[i])/r->rdir[i];
579 if (dt > t)
580 t = dt; /* farthest face is the one */
581 }
582 t += FTINY; /* fudge to get inside cube */
583 if (t >= r->rot) /* clipped already */
584 return(0);
585 /* advance position */
586 VSUM(curpos, curpos, r->rdir, t);
587
588 if (!incube(scene, curpos)) /* non-intersecting ray */
589 return(0);
590 }
591 cxset[0] = 0;
592 raymove(curpos, cxset, sflags, r, scene);
593 return((r->ro != NULL) & (r->ro != &Aftplane));
594 }
595
596
597 static int
598 raymove( /* check for hit as we move */
599 FVECT pos, /* current position, modified herein */
600 OBJECT *cxs, /* checked objects, modified by checkhit */
601 int dirf, /* direction indicators to speed tests */
602 RAY *r,
603 CUBE *cu
604 )
605 {
606 int ax;
607 double dt, t;
608
609 if (istree(cu->cutree)) { /* recurse on subcubes */
610 CUBE cukid;
611 int br, sgn;
612
613 cukid.cusize = cu->cusize * 0.5; /* find subcube */
614 VCOPY(cukid.cuorg, cu->cuorg);
615 br = 0;
616 if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
617 cukid.cuorg[0] += cukid.cusize;
618 br |= 1;
619 }
620 if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
621 cukid.cuorg[1] += cukid.cusize;
622 br |= 2;
623 }
624 if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
625 cukid.cuorg[2] += cukid.cusize;
626 br |= 4;
627 }
628 for ( ; ; ) {
629 cukid.cutree = octkid(cu->cutree, br);
630 if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
631 return(RAYHIT);
632 sgn = 1 << ax;
633 if (sgn & dirf) /* positive axis? */
634 if (sgn & br)
635 return(ax); /* overflow */
636 else {
637 cukid.cuorg[ax] += cukid.cusize;
638 br |= sgn;
639 }
640 else
641 if (sgn & br) {
642 cukid.cuorg[ax] -= cukid.cusize;
643 br &= ~sgn;
644 } else
645 return(ax); /* underflow */
646 }
647 /*NOTREACHED*/
648 }
649 if (isfull(cu->cutree)) {
650 if (checkhit(r, cu, cxs))
651 return(RAYHIT);
652 } else if (r->ro == &Aftplane && incube(cu, r->rop))
653 return(RAYHIT);
654 /* advance to next cube */
655 if (dirf&0x11) {
656 dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
657 t = (dt - pos[0])/r->rdir[0];
658 ax = 0;
659 } else
660 t = FHUGE;
661 if (dirf&0x22) {
662 dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
663 dt = (dt - pos[1])/r->rdir[1];
664 if (dt < t) {
665 t = dt;
666 ax = 1;
667 }
668 }
669 if (dirf&0x44) {
670 dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
671 dt = (dt - pos[2])/r->rdir[2];
672 if (dt < t) {
673 t = dt;
674 ax = 2;
675 }
676 }
677 VSUM(pos, pos, r->rdir, t);
678 return(ax);
679 }
680
681
682 static int
683 checkhit( /* check for hit in full cube */
684 RAY *r,
685 CUBE *cu,
686 OBJECT *cxs
687 )
688 {
689 OBJECT oset[MAXSET+1];
690
691 objset(oset, cu->cutree);
692 checkset(oset, cxs); /* avoid double-checking */
693
694 (*r->hitf)(oset, r); /* test for hit in set */
695
696 if (r->robj == OVOID)
697 return(0); /* no scores yet */
698
699 return(incube(cu, r->rop)); /* hit OK if in current cube */
700 }
701
702
703 static void
704 checkset( /* modify checked set and set to check */
705 OBJECT *os, /* os' = os - cs */
706 OBJECT *cs /* cs' = cs + os */
707 )
708 {
709 OBJECT cset[MAXCSET+MAXSET+1];
710 int i, j;
711 int k;
712 /* copy os in place, cset <- cs */
713 cset[0] = 0;
714 k = 0;
715 for (i = j = 1; i <= os[0]; i++) {
716 while (j <= cs[0] && cs[j] < os[i])
717 cset[++cset[0]] = cs[j++];
718 if (j > cs[0] || os[i] != cs[j]) { /* object to check */
719 os[++k] = os[i];
720 cset[++cset[0]] = os[i];
721 }
722 }
723 if (!(os[0] = k)) /* new "to check" set size */
724 return; /* special case */
725 while (j <= cs[0]) /* get the rest of cs */
726 cset[++cset[0]] = cs[j++];
727 if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */
728 cset[0] = MAXCSET;
729 /* setcopy(cs, cset); */ /* copy cset back to cs */
730 os = cset;
731 for (i = os[0]; i-- >= 0; )
732 *cs++ = *os++;
733 }