ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.90
Committed: Fri Nov 15 20:47:42 2024 UTC (5 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.89: +11 -4 lines
Log Message:
feat(rpict): Experimental source skipping option with -DSSKIPOPT compile flag

File Contents

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