ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.49
Committed: Tue Apr 19 01:15:06 2005 UTC (19 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.48: +59 -21 lines
Log Message:
Extensive changes to enable rtrace -oTW option for tracking ray contributions

File Contents

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