ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.51
Committed: Tue May 31 18:01:09 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.50: +16 -1 lines
Log Message:
Added Russian roulette ray termination and fixed ambient weights & measures

File Contents

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