ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.50
Committed: Thu May 26 06:55:22 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.49: +4 -2 lines
Log Message:
Got rtcontrib working and wrote basic man page

File Contents

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