ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.66
Committed: Tue Feb 24 19:39:27 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.65: +12 -6 lines
Log Message:
Initial check-in of photon map addition by Roland Schregle

File Contents

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