ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.34
Committed: Sat Feb 22 02:07:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +76 -24 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.34 static const char RCSid[] = "$Id$";
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     /* ====================================================================
11     * The Radiance Software License, Version 1.0
12     *
13     * Copyright (c) 1990 - 2002 The Regents of the University of California,
14     * through Lawrence Berkeley National Laboratory. All rights reserved.
15     *
16     * Redistribution and use in source and binary forms, with or without
17     * modification, are permitted provided that the following conditions
18     * are met:
19     *
20     * 1. Redistributions of source code must retain the above copyright
21     * notice, this list of conditions and the following disclaimer.
22     *
23     * 2. Redistributions in binary form must reproduce the above copyright
24     * notice, this list of conditions and the following disclaimer in
25     * the documentation and/or other materials provided with the
26     * distribution.
27     *
28     * 3. The end-user documentation included with the redistribution,
29     * if any, must include the following acknowledgment:
30     * "This product includes Radiance software
31     * (http://radsite.lbl.gov/)
32     * developed by the Lawrence Berkeley National Laboratory
33     * (http://www.lbl.gov/)."
34     * Alternately, this acknowledgment may appear in the software itself,
35     * if and wherever such third-party acknowledgments normally appear.
36     *
37     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38     * and "The Regents of the University of California" must
39     * not be used to endorse or promote products derived from this
40     * software without prior written permission. For written
41     * permission, please contact [email protected].
42     *
43     * 5. Products derived from this software may not be called "Radiance",
44     * nor may "Radiance" appear in their name, without prior written
45     * permission of Lawrence Berkeley National Laboratory.
46     *
47     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58     * SUCH DAMAGE.
59     * ====================================================================
60     *
61     * This software consists of voluntary contributions made by many
62     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
63     * information on Lawrence Berkeley National Laboratory, please see
64     * <http://www.lbl.gov/>.
65 greg 1.1 */
66    
67     #include "ray.h"
68    
69     #include "otypes.h"
70    
71 greg 1.15 #include "otspecial.h"
72    
73 greg 2.3 #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */
74    
75 greg 2.6 unsigned long raynum = 0; /* next unique ray number */
76     unsigned long nrays = 0; /* number of calls to localhit */
77 greg 1.1
78 greg 1.23 static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
79 greg 1.15 OBJREC Lamb = {
80     OVOID, MAT_PLASTIC, "Lambertian",
81 greg 2.2 {0, 5, NULL, Lambfa}, NULL,
82 greg 1.15 }; /* a Lambertian surface */
83    
84 greg 2.17 OBJREC Aftplane; /* aft clipping plane object */
85 greg 2.16
86 greg 2.34 static int raymove(), checkhit();
87     static void checkset();
88 greg 2.5
89 gwlarson 2.33 #ifndef MAXLOOP
90     #define MAXLOOP 0 /* modifier loop detection */
91     #endif
92 greg 1.1
93     #define RAYHIT (-1) /* return value for intercepted ray */
94    
95    
96 greg 2.34 int
97 greg 1.1 rayorigin(r, ro, rt, rw) /* start new ray from old one */
98     register RAY *r, *ro;
99     int rt;
100     double rw;
101     {
102 gwlarson 2.31 double re;
103    
104 greg 1.1 if ((r->parent = ro) == NULL) { /* primary ray */
105     r->rlvl = 0;
106     r->rweight = rw;
107     r->crtype = r->rtype = rt;
108     r->rsrc = -1;
109     r->clipset = NULL;
110 greg 1.21 r->revf = raytrace;
111 greg 2.23 copycolor(r->cext, cextinction);
112 greg 2.26 copycolor(r->albedo, salbedo);
113 greg 2.23 r->gecc = seccg;
114     r->slights = NULL;
115 greg 1.1 } else { /* spawned ray */
116     r->rlvl = ro->rlvl;
117     if (rt & RAYREFL) {
118     r->rlvl++;
119     r->rsrc = -1;
120     r->clipset = ro->clipset;
121 greg 2.22 r->rmax = 0.0;
122 greg 1.1 } else {
123     r->rsrc = ro->rsrc;
124     r->clipset = ro->newcset;
125 greg 2.22 r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
126 greg 1.1 }
127 greg 1.21 r->revf = ro->revf;
128 greg 2.23 copycolor(r->cext, ro->cext);
129 greg 2.26 copycolor(r->albedo, ro->albedo);
130 greg 2.23 r->gecc = ro->gecc;
131     r->slights = ro->slights;
132 greg 1.1 r->crtype = ro->crtype | (r->rtype = rt);
133     VCOPY(r->rorg, ro->rop);
134 gwlarson 2.31 r->rweight = ro->rweight * rw;
135     /* estimate absorption */
136     re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
137     colval(ro->cext,RED) : colval(ro->cext,GRN);
138     if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
139     if (re > 0.)
140     r->rweight *= exp(-re*ro->rot);
141 greg 1.1 }
142 greg 1.22 rayclear(r);
143     return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
144     }
145    
146    
147 greg 2.34 void
148 greg 1.22 rayclear(r) /* clear a ray for (re)evaluation */
149     register RAY *r;
150     {
151 greg 1.20 r->rno = raynum++;
152 greg 1.1 r->newcset = r->clipset;
153 greg 2.28 r->robj = OVOID;
154 greg 2.17 r->ro = NULL;
155 greg 2.34 r->rox = NULL;
156 gregl 2.29 r->rt = r->rot = FHUGE;
157 greg 1.1 r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
158     setcolor(r->pcol, 1.0, 1.0, 1.0);
159     setcolor(r->rcol, 0.0, 0.0, 0.0);
160     }
161    
162    
163 greg 2.34 void
164 greg 1.21 raytrace(r) /* trace a ray and compute its value */
165 greg 1.8 RAY *r;
166 greg 1.1 {
167 greg 1.15 if (localhit(r, &thescene))
168 greg 2.24 raycont(r); /* hit local surface, evaluate */
169 greg 2.16 else if (r->ro == &Aftplane) {
170 greg 2.23 r->ro = NULL; /* hit aft clipping plane */
171 greg 2.16 r->rot = FHUGE;
172     } else if (sourcehit(r))
173 greg 2.24 rayshade(r, r->ro->omod); /* distant source */
174 greg 1.1
175 greg 2.23 rayparticipate(r); /* for participating medium */
176    
177 greg 1.1 if (trace != NULL)
178     (*trace)(r); /* trace execution */
179     }
180    
181    
182 greg 2.34 void
183 greg 1.8 raycont(r) /* check for clipped object and continue */
184     register RAY *r;
185     {
186 greg 2.7 if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
187 greg 2.24 !rayshade(r, r->ro->omod))
188 greg 1.8 raytrans(r);
189     }
190    
191    
192 greg 2.34 void
193 greg 1.1 raytrans(r) /* transmit ray as is */
194 greg 1.8 register RAY *r;
195 greg 1.1 {
196     RAY tr;
197    
198     if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
199     VCOPY(tr.rdir, r->rdir);
200     rayvalue(&tr);
201     copycolor(r->rcol, tr.rcol);
202 greg 1.10 r->rt = r->rot + tr.rt;
203 greg 1.1 }
204     }
205    
206    
207 greg 2.34 int
208 greg 1.1 rayshade(r, mod) /* shade ray r with material mod */
209     register RAY *r;
210     int mod;
211     {
212 greg 2.9 int gotmat;
213 greg 1.1 register OBJREC *m;
214 gwlarson 2.33 #if MAXLOOP
215     static int depth = 0;
216 greg 1.1 /* check for infinite loop */
217     if (depth++ >= MAXLOOP)
218 greg 1.4 objerror(r->ro, USER, "possible modifier loop");
219 gwlarson 2.33 #endif
220 greg 1.19 r->rt = r->rot; /* set effective ray length */
221 greg 2.9 for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
222 greg 1.1 m = objptr(mod);
223 greg 1.4 /****** unnecessary test since modifier() is always called
224 greg 1.1 if (!ismodifier(m->otype)) {
225     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
226     error(USER, errmsg);
227     }
228 greg 1.4 ******/
229 greg 1.16 /* hack for irradiance calculation */
230     if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
231     if (irr_ignore(m->otype)) {
232 gwlarson 2.33 #if MAXLOOP
233 greg 1.16 depth--;
234 gwlarson 2.33 #endif
235 greg 1.16 raytrans(r);
236 greg 2.15 return(1);
237 greg 1.16 }
238 greg 1.18 if (!islight(m->otype))
239 greg 1.16 m = &Lamb;
240     }
241 greg 2.9 /* materials call raytexture */
242     gotmat = (*ofun[m->otype].funp)(m, r);
243 greg 1.1 }
244 gwlarson 2.33 #if MAXLOOP
245 greg 2.9 depth--;
246 gwlarson 2.33 #endif
247 greg 2.9 return(gotmat);
248 greg 2.23 }
249    
250    
251 greg 2.34 void
252 greg 2.23 rayparticipate(r) /* compute ray medium participation */
253     register RAY *r;
254     {
255     COLOR ce, ca;
256     double re, ge, be;
257    
258     if (intens(r->cext) <= 1./FHUGE)
259     return; /* no medium */
260 greg 2.27 re = r->rot*colval(r->cext,RED);
261     ge = r->rot*colval(r->cext,GRN);
262     be = r->rot*colval(r->cext,BLU);
263 greg 2.26 if (r->crtype & SHADOW) { /* no scattering for sources */
264     re *= 1. - colval(r->albedo,RED);
265     ge *= 1. - colval(r->albedo,GRN);
266     be *= 1. - colval(r->albedo,BLU);
267     }
268     setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re),
269     ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
270     be<=0. ? 1. : be>92. ? 0. : exp(-be));
271 greg 2.23 multcolor(r->rcol, ce); /* path absorption */
272 greg 2.26 if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
273 greg 2.23 return; /* no scattering */
274 greg 2.26 setcolor(ca,
275     colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
276     colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
277     colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
278 greg 2.23 addcolor(r->rcol, ca); /* ambient in scattering */
279     srcscatter(r); /* source in scattering */
280 greg 1.1 }
281    
282    
283     raytexture(r, mod) /* get material modifiers */
284     RAY *r;
285     int mod;
286     {
287 gwlarson 2.33 register OBJREC *m;
288     #if MAXLOOP
289 greg 1.1 static int depth = 0;
290     /* check for infinite loop */
291     if (depth++ >= MAXLOOP)
292     objerror(r->ro, USER, "modifier loop");
293 gwlarson 2.33 #endif
294 greg 1.1 /* execute textures and patterns */
295     for ( ; mod != OVOID; mod = m->omod) {
296     m = objptr(mod);
297 greg 2.9 /****** unnecessary test since modifier() is always called
298     if (!ismodifier(m->otype)) {
299 greg 1.1 sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
300     error(USER, errmsg);
301     }
302 greg 2.9 ******/
303 greg 2.20 if ((*ofun[m->otype].funp)(m, r)) {
304     sprintf(errmsg, "conflicting material \"%s\"",
305     m->oname);
306     objerror(r->ro, USER, errmsg);
307     }
308 greg 1.1 }
309 gwlarson 2.33 #if MAXLOOP
310 greg 1.1 depth--; /* end here */
311 gwlarson 2.33 #endif
312 greg 1.1 }
313    
314    
315 greg 2.34 int
316 greg 1.1 raymixture(r, fore, back, coef) /* mix modifiers */
317     register RAY *r;
318     OBJECT fore, back;
319     double coef;
320     {
321 greg 2.9 RAY fr, br;
322     int foremat, backmat;
323 greg 1.1 register int i;
324 greg 2.24 /* bound coefficient */
325 greg 1.1 if (coef > 1.0)
326     coef = 1.0;
327     else if (coef < 0.0)
328     coef = 0.0;
329 greg 2.13 /* compute foreground and background */
330 greg 2.24 foremat = backmat = 0;
331 greg 2.9 /* foreground */
332     copystruct(&fr, r);
333 greg 2.24 if (coef > FTINY)
334 greg 2.9 foremat = rayshade(&fr, fore);
335     /* background */
336     copystruct(&br, r);
337 greg 2.24 if (coef < 1.0-FTINY)
338 greg 2.9 backmat = rayshade(&br, back);
339 greg 2.24 /* check for transparency */
340     if (backmat ^ foremat)
341 gwlarson 2.33 if (backmat && coef > FTINY)
342 greg 2.24 raytrans(&fr);
343 gwlarson 2.33 else if (foremat && coef < 1.0-FTINY)
344 greg 2.24 raytrans(&br);
345 greg 2.12 /* mix perturbations */
346 greg 1.1 for (i = 0; i < 3; i++)
347 greg 2.12 r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
348     /* mix pattern colors */
349 greg 2.9 scalecolor(fr.pcol, coef);
350     scalecolor(br.pcol, 1.0-coef);
351 greg 2.12 copycolor(r->pcol, fr.pcol);
352     addcolor(r->pcol, br.pcol);
353 greg 2.24 /* return value tells if material */
354     if (!foremat & !backmat)
355     return(0);
356 greg 2.12 /* mix returned ray values */
357 greg 2.24 scalecolor(fr.rcol, coef);
358     scalecolor(br.rcol, 1.0-coef);
359     copycolor(r->rcol, fr.rcol);
360     addcolor(r->rcol, br.rcol);
361     r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
362     return(1);
363 greg 1.1 }
364    
365    
366     double
367 greg 2.21 raydist(r, flags) /* compute (cumulative) ray distance */
368     register RAY *r;
369     register int flags;
370     {
371     double sum = 0.0;
372    
373     while (r != NULL && r->crtype&flags) {
374     sum += r->rot;
375     r = r->parent;
376     }
377     return(sum);
378     }
379    
380    
381     double
382 greg 1.1 raynormal(norm, r) /* compute perturbed normal for ray */
383     FVECT norm;
384     register RAY *r;
385     {
386     double newdot;
387     register int i;
388    
389     /* The perturbation is added to the surface normal to obtain
390     * the new normal. If the new normal would affect the surface
391     * orientation wrt. the ray, a correction is made. The method is
392     * still fraught with problems since reflected rays and similar
393     * directions calculated from the surface normal may spawn rays behind
394     * the surface. The only solution is to curb textures at high
395 greg 1.9 * incidence (namely, keep DOT(rdir,pert) < Rdot).
396 greg 1.1 */
397    
398     for (i = 0; i < 3; i++)
399     norm[i] = r->ron[i] + r->pert[i];
400    
401     if (normalize(norm) == 0.0) {
402     objerror(r->ro, WARNING, "illegal normal perturbation");
403     VCOPY(norm, r->ron);
404     return(r->rod);
405     }
406     newdot = -DOT(norm, r->rdir);
407     if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */
408     for (i = 0; i < 3; i++)
409     norm[i] += 2.0*newdot*r->rdir[i];
410     newdot = -newdot;
411     }
412     return(newdot);
413 greg 1.12 }
414    
415    
416 greg 2.34 void
417 greg 1.12 newrayxf(r) /* get new tranformation matrix for ray */
418     RAY *r;
419     {
420     static struct xfn {
421     struct xfn *next;
422     FULLXF xf;
423     } xfseed = { &xfseed }, *xflast = &xfseed;
424     register struct xfn *xp;
425     register RAY *rp;
426    
427     /*
428     * Search for transform in circular list that
429     * has no associated ray in the tree.
430     */
431     xp = xflast;
432     for (rp = r->parent; rp != NULL; rp = rp->parent)
433     if (rp->rox == &xp->xf) { /* xp in use */
434     xp = xp->next; /* move to next */
435     if (xp == xflast) { /* need new one */
436 greg 2.34 xp = (struct xfn *)malloc(sizeof(struct xfn));
437 greg 1.12 if (xp == NULL)
438     error(SYSTEM,
439     "out of memory in newrayxf");
440     /* insert in list */
441     xp->next = xflast->next;
442     xflast->next = xp;
443     break; /* we're done */
444     }
445     rp = r; /* start check over */
446     }
447     /* got it */
448     r->rox = &xp->xf;
449     xflast = xp;
450 greg 1.1 }
451    
452    
453 greg 2.34 void
454 greg 1.1 flipsurface(r) /* reverse surface orientation */
455     register RAY *r;
456     {
457     r->rod = -r->rod;
458     r->ron[0] = -r->ron[0];
459     r->ron[1] = -r->ron[1];
460     r->ron[2] = -r->ron[2];
461     r->pert[0] = -r->pert[0];
462     r->pert[1] = -r->pert[1];
463     r->pert[2] = -r->pert[2];
464     }
465    
466    
467 greg 2.34 int
468 greg 1.1 localhit(r, scene) /* check for hit in the octree */
469     register RAY *r;
470     register CUBE *scene;
471     {
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     return(r->ro != NULL & r->ro != &Aftplane);
526 greg 1.1 }
527    
528    
529     static int
530 greg 2.3 raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */
531     FVECT pos; /* current position, modified herein */
532     OBJECT *cxs; /* checked objects, modified by checkhit */
533 greg 1.11 int dirf; /* direction indicators to speed tests */
534 greg 1.1 register RAY *r;
535     register CUBE *cu;
536     {
537     int ax;
538     double dt, t;
539    
540     if (istree(cu->cutree)) { /* recurse on subcubes */
541     CUBE cukid;
542 greg 1.11 register int br, sgn;
543 greg 1.1
544     cukid.cusize = cu->cusize * 0.5; /* find subcube */
545     VCOPY(cukid.cuorg, cu->cuorg);
546     br = 0;
547     if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
548     cukid.cuorg[0] += cukid.cusize;
549     br |= 1;
550     }
551     if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
552     cukid.cuorg[1] += cukid.cusize;
553     br |= 2;
554     }
555     if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
556     cukid.cuorg[2] += cukid.cusize;
557     br |= 4;
558     }
559     for ( ; ; ) {
560     cukid.cutree = octkid(cu->cutree, br);
561 greg 2.3 if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
562 greg 1.1 return(RAYHIT);
563     sgn = 1 << ax;
564 greg 1.11 if (sgn & dirf) /* positive axis? */
565 greg 1.1 if (sgn & br)
566     return(ax); /* overflow */
567     else {
568     cukid.cuorg[ax] += cukid.cusize;
569     br |= sgn;
570     }
571 greg 1.11 else
572     if (sgn & br) {
573     cukid.cuorg[ax] -= cukid.cusize;
574     br &= ~sgn;
575     } else
576     return(ax); /* underflow */
577 greg 1.1 }
578     /*NOTREACHED*/
579     }
580 greg 2.18 if (isfull(cu->cutree)) {
581     if (checkhit(r, cu, cxs))
582     return(RAYHIT);
583     } else if (r->ro == &Aftplane && incube(cu, r->rop))
584 greg 1.1 return(RAYHIT);
585     /* advance to next cube */
586 greg 1.11 if (dirf&0x11) {
587     dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
588 greg 1.1 t = (dt - pos[0])/r->rdir[0];
589     ax = 0;
590     } else
591     t = FHUGE;
592 greg 1.11 if (dirf&0x22) {
593     dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
594 greg 1.1 dt = (dt - pos[1])/r->rdir[1];
595     if (dt < t) {
596     t = dt;
597     ax = 1;
598     }
599     }
600 greg 1.11 if (dirf&0x44) {
601     dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
602 greg 1.1 dt = (dt - pos[2])/r->rdir[2];
603     if (dt < t) {
604     t = dt;
605     ax = 2;
606     }
607     }
608     pos[0] += r->rdir[0]*t;
609     pos[1] += r->rdir[1]*t;
610     pos[2] += r->rdir[2]*t;
611     return(ax);
612     }
613    
614    
615 greg 2.34 static int
616 greg 2.3 checkhit(r, cu, cxs) /* check for hit in full cube */
617 greg 1.1 register RAY *r;
618     CUBE *cu;
619 greg 2.3 OBJECT *cxs;
620 greg 1.1 {
621     OBJECT oset[MAXSET+1];
622     register OBJREC *o;
623     register int i;
624    
625     objset(oset, cu->cutree);
626 greg 2.3 checkset(oset, cxs); /* eliminate double-checking */
627 greg 1.1 for (i = oset[0]; i > 0; i--) {
628     o = objptr(oset[i]);
629 greg 2.28 if ((*ofun[o->otype].funp)(o, r))
630     r->robj = oset[i];
631 greg 1.1 }
632     if (r->ro == NULL)
633     return(0); /* no scores yet */
634    
635     return(incube(cu, r->rop)); /* hit OK if in current cube */
636 greg 2.2 }
637    
638    
639 greg 2.34 static void
640 greg 2.2 checkset(os, cs) /* modify checked set and set to check */
641 greg 2.3 register OBJECT *os; /* os' = os - cs */
642     register OBJECT *cs; /* cs' = cs + os */
643 greg 2.2 {
644     OBJECT cset[MAXCSET+MAXSET+1];
645 greg 2.3 register int i, j;
646     int k;
647 greg 2.2 /* copy os in place, cset <- cs */
648     cset[0] = 0;
649     k = 0;
650     for (i = j = 1; i <= os[0]; i++) {
651     while (j <= cs[0] && cs[j] < os[i])
652     cset[++cset[0]] = cs[j++];
653     if (j > cs[0] || os[i] != cs[j]) { /* object to check */
654     os[++k] = os[i];
655     cset[++cset[0]] = os[i];
656     }
657     }
658 greg 2.3 if (!(os[0] = k)) /* new "to check" set size */
659     return; /* special case */
660 greg 2.2 while (j <= cs[0]) /* get the rest of cs */
661     cset[++cset[0]] = cs[j++];
662 greg 2.3 if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */
663 greg 2.2 cset[0] = MAXCSET;
664 greg 2.3 /* setcopy(cs, cset); */ /* copy cset back to cs */
665     os = cset;
666     for (i = os[0]; i-- >= 0; )
667     *cs++ = *os++;
668 greg 1.1 }