ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.65
Committed: Wed Feb 19 14:12:48 2014 UTC (10 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2, rad4R2P1
Changes since 2.64: +17 -17 lines
Log Message:
Eliminated superfluous extern declarations

File Contents

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