ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
(Generate patch)

Comparing ray/src/rt/raytrace.c (file contents):
Revision 1.10 by greg, Tue Mar 27 11:40:05 1990 UTC vs.
Revision 2.7 by greg, Wed Jul 21 15:36:13 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include  "otypes.h"
18  
19 + #include  "otspecial.h"
20 +
21 + #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
22 +
23   extern CUBE  thescene;                  /* our scene */
24   extern int  maxdepth;                   /* maximum recursion depth */
25   extern double  minweight;               /* minimum ray weight */
26 + extern int  do_irrad;                   /* compute irradiance? */
27  
28 < long  nrays = 0L;                       /* number of rays traced */
28 > unsigned long  raynum = 0;              /* next unique ray number */
29 > unsigned long  nrays = 0;               /* number of calls to localhit */
30  
31 + static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
32 + OBJREC  Lamb = {
33 +        OVOID, MAT_PLASTIC, "Lambertian",
34 +        {0, 5, NULL, Lambfa}, NULL,
35 + };                                      /* a Lambertian surface */
36 +
37 + static int  raymove(), checkset(), checkhit();
38 +
39   #define  MAXLOOP        128             /* modifier loop detection */
40  
41   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 38 | Line 52 | double  rw;
52                  r->crtype = r->rtype = rt;
53                  r->rsrc = -1;
54                  r->clipset = NULL;
55 +                r->revf = raytrace;
56          } else {                                /* spawned ray */
57                  r->rlvl = ro->rlvl;
58                  if (rt & RAYREFL) {
# Line 48 | Line 63 | double  rw;
63                          r->rsrc = ro->rsrc;
64                          r->clipset = ro->newcset;
65                  }
66 +                r->revf = ro->revf;
67                  r->rweight = ro->rweight * rw;
68                  r->crtype = ro->crtype | (r->rtype = rt);
69                  VCOPY(r->rorg, ro->rop);
70          }
71 <        r->rno = nrays;
71 >        rayclear(r);
72 >        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
73 > }
74 >
75 >
76 > rayclear(r)                     /* clear a ray for (re)evaluation */
77 > register RAY  *r;
78 > {
79 >        r->rno = raynum++;
80          r->newcset = r->clipset;
81          r->ro = NULL;
82          r->rot = FHUGE;
# Line 60 | Line 84 | double  rw;
84          setcolor(r->pcol, 1.0, 1.0, 1.0);
85          setcolor(r->rcol, 0.0, 0.0, 0.0);
86          r->rt = 0.0;
63        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
87   }
88  
89  
90 < rayvalue(r)                     /* compute a ray's value */
90 > raytrace(r)                     /* trace a ray and compute its value */
91   RAY  *r;
92   {
93          extern int  (*trace)();
94  
95 <        if (localhit(r, &thescene) || sourcehit(r))
95 >        if (localhit(r, &thescene))
96                  raycont(r);
97 +        else if (sourcehit(r))
98 +                rayshade(r, r->ro->omod);
99  
100          if (trace != NULL)
101                  (*trace)(r);            /* trace execution */
# Line 80 | Line 105 | RAY  *r;
105   raycont(r)                      /* check for clipped object and continue */
106   register RAY  *r;
107   {
108 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
108 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
109 >                        r->ro->omod == OVOID)
110                  raytrans(r);
111          else
112                  rayshade(r, r->ro->omod);
# Line 110 | Line 136 | int  mod;
136                                          /* check for infinite loop */
137          if (depth++ >= MAXLOOP)
138                  objerror(r->ro, USER, "possible modifier loop");
139 +        r->rt = r->rot;                 /* set effective ray length */
140          for ( ; mod != OVOID; mod = m->omod) {
141                  m = objptr(mod);
142                  /****** unnecessary test since modifier() is always called
# Line 118 | Line 145 | int  mod;
145                          error(USER, errmsg);
146                  }
147                  ******/
148 +                                        /* hack for irradiance calculation */
149 +                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
150 +                        if (irr_ignore(m->otype)) {
151 +                                depth--;
152 +                                raytrans(r);
153 +                                return;
154 +                        }
155 +                        if (!islight(m->otype))
156 +                                m = &Lamb;
157 +                }
158                  (*ofun[m->otype].funp)(m, r);   /* execute function */
122                m->lastrno = r->rno;
159                  if (ismaterial(m->otype)) {     /* materials call raytexture */
160                          depth--;
161                          return;         /* we're done */
# Line 146 | Line 182 | int  mod;
182                          error(USER, errmsg);
183                  }
184                  (*ofun[m->otype].funp)(m, r);
149                m->lastrno = r->rno;
185          }
186          depth--;                        /* end here */
187   }
# Line 233 | Line 268 | register RAY  *r;
268   }
269  
270  
271 + newrayxf(r)                     /* get new tranformation matrix for ray */
272 + RAY  *r;
273 + {
274 +        static struct xfn {
275 +                struct xfn  *next;
276 +                FULLXF  xf;
277 +        }  xfseed = { &xfseed }, *xflast = &xfseed;
278 +        register struct xfn  *xp;
279 +        register RAY  *rp;
280 +
281 +        /*
282 +         * Search for transform in circular list that
283 +         * has no associated ray in the tree.
284 +         */
285 +        xp = xflast;
286 +        for (rp = r->parent; rp != NULL; rp = rp->parent)
287 +                if (rp->rox == &xp->xf) {               /* xp in use */
288 +                        xp = xp->next;                  /* move to next */
289 +                        if (xp == xflast) {             /* need new one */
290 +                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
291 +                                if (xp == NULL)
292 +                                        error(SYSTEM,
293 +                                                "out of memory in newrayxf");
294 +                                                        /* insert in list */
295 +                                xp->next = xflast->next;
296 +                                xflast->next = xp;
297 +                                break;                  /* we're done */
298 +                        }
299 +                        rp = r;                 /* start check over */
300 +                }
301 +                                        /* got it */
302 +        r->rox = &xp->xf;
303 +        xflast = xp;
304 + }
305 +
306 +
307   flipsurface(r)                  /* reverse surface orientation */
308   register RAY  *r;
309   {
# Line 250 | Line 321 | localhit(r, scene)             /* check for hit in the octree */
321   register RAY  *r;
322   register CUBE  *scene;
323   {
324 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
325          FVECT  curpos;                  /* current cube position */
326 <        int  mpos, mneg;                /* sign flags */
326 >        int  sflags;                    /* sign flags */
327          double  t, dt;
328          register int  i;
329  
330          nrays++;                        /* increment trace counter */
331 <
260 <        mpos = mneg = 0;
331 >        sflags = 0;
332          for (i = 0; i < 3; i++) {
333                  curpos[i] = r->rorg[i];
334                  if (r->rdir[i] > FTINY)
335 <                        mpos |= 1 << i;
335 >                        sflags |= 1 << i;
336                  else if (r->rdir[i] < -FTINY)
337 <                        mneg |= 1 << i;
337 >                        sflags |= 0x10 << i;
338          }
339 +        if (sflags == 0)
340 +                error(CONSISTENCY, "zero ray direction in localhit");
341          t = 0.0;
342          if (!incube(scene, curpos)) {
343                                          /* find distance to entry */
344                  for (i = 0; i < 3; i++) {
345                                          /* plane in our direction */
346 <                        if (mpos & 1<<i)
346 >                        if (sflags & 1<<i)
347                                  dt = scene->cuorg[i];
348 <                        else if (mneg & 1<<i)
348 >                        else if (sflags & 0x10<<i)
349                                  dt = scene->cuorg[i] + scene->cusize;
350                          else
351                                  continue;
# Line 289 | Line 362 | register CUBE  *scene;
362                  if (!incube(scene, curpos))     /* non-intersecting ray */
363                          return(0);
364          }
365 <        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT);
365 >        cxset[0] = 0;
366 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT);
367   }
368  
369  
370   static int
371 < raymove(pos, plus, minus, r, cu)        /* check for hit as we move */
372 < FVECT  pos;                     /* modified */
373 < int  plus, minus;               /* direction indicators to speed tests */
371 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
372 > FVECT  pos;                     /* current position, modified herein */
373 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
374 > int  dirf;                      /* direction indicators to speed tests */
375   register RAY  *r;
376   register CUBE  *cu;
377   {
378          int  ax;
379          double  dt, t;
305        register int  sgn;
380  
381          if (istree(cu->cutree)) {               /* recurse on subcubes */
382                  CUBE  cukid;
383 <                register int  br;
383 >                register int  br, sgn;
384  
385                  cukid.cusize = cu->cusize * 0.5;        /* find subcube */
386                  VCOPY(cukid.cuorg, cu->cuorg);
# Line 325 | Line 399 | register CUBE  *cu;
399                  }
400                  for ( ; ; ) {
401                          cukid.cutree = octkid(cu->cutree, br);
402 <                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT)
402 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
403                                  return(RAYHIT);
404                          sgn = 1 << ax;
405 <                        if (sgn & minus)                /* negative axis? */
332 <                                if (sgn & br) {
333 <                                        cukid.cuorg[ax] -= cukid.cusize;
334 <                                        br &= ~sgn;
335 <                                } else
336 <                                        return(ax);     /* underflow */
337 <                        else
405 >                        if (sgn & dirf)                 /* positive axis? */
406                                  if (sgn & br)
407                                          return(ax);     /* overflow */
408                                  else {
409                                          cukid.cuorg[ax] += cukid.cusize;
410                                          br |= sgn;
411                                  }
412 +                        else
413 +                                if (sgn & br) {
414 +                                        cukid.cuorg[ax] -= cukid.cusize;
415 +                                        br &= ~sgn;
416 +                                } else
417 +                                        return(ax);     /* underflow */
418                  }
419                  /*NOTREACHED*/
420          }
421 <        if (isfull(cu->cutree) && checkhit(r, cu))
421 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
422                  return(RAYHIT);
423                                          /* advance to next cube */
424 <        sgn = plus | minus;
425 <        if (sgn&1) {
352 <                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
424 >        if (dirf&0x11) {
425 >                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
426                  t = (dt - pos[0])/r->rdir[0];
427                  ax = 0;
428          } else
429                  t = FHUGE;
430 <        if (sgn&2) {
431 <                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
430 >        if (dirf&0x22) {
431 >                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
432                  dt = (dt - pos[1])/r->rdir[1];
433                  if (dt < t) {
434                          t = dt;
435                          ax = 1;
436                  }
437          }
438 <        if (sgn&4) {
439 <                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
438 >        if (dirf&0x44) {
439 >                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
440                  dt = (dt - pos[2])/r->rdir[2];
441                  if (dt < t) {
442                          t = dt;
# Line 378 | Line 451 | register CUBE  *cu;
451  
452  
453   static
454 < checkhit(r, cu)                 /* check for hit in full cube */
454 > checkhit(r, cu, cxs)            /* check for hit in full cube */
455   register RAY  *r;
456   CUBE  *cu;
457 + OBJECT  *cxs;
458   {
459          OBJECT  oset[MAXSET+1];
460          register OBJREC  *o;
461          register int  i;
462  
463          objset(oset, cu->cutree);
464 +        checkset(oset, cxs);                    /* eliminate double-checking */
465          for (i = oset[0]; i > 0; i--) {
466                  o = objptr(oset[i]);
392                if (o->lastrno == r->rno)               /* checked already? */
393                        continue;
467                  (*ofun[o->otype].funp)(o, r);
395                o->lastrno = r->rno;
468          }
469          if (r->ro == NULL)
470                  return(0);                      /* no scores yet */
471  
472          return(incube(cu, r->rop));             /* hit OK if in current cube */
473 + }
474 +
475 +
476 + static
477 + checkset(os, cs)                /* modify checked set and set to check */
478 + register OBJECT  *os;                   /* os' = os - cs */
479 + register OBJECT  *cs;                   /* cs' = cs + os */
480 + {
481 +        OBJECT  cset[MAXCSET+MAXSET+1];
482 +        register int  i, j;
483 +        int  k;
484 +                                        /* copy os in place, cset <- cs */
485 +        cset[0] = 0;
486 +        k = 0;
487 +        for (i = j = 1; i <= os[0]; i++) {
488 +                while (j <= cs[0] && cs[j] < os[i])
489 +                        cset[++cset[0]] = cs[j++];
490 +                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */
491 +                        os[++k] = os[i];
492 +                        cset[++cset[0]] = os[i];
493 +                }
494 +        }
495 +        if (!(os[0] = k))               /* new "to check" set size */
496 +                return;                 /* special case */
497 +        while (j <= cs[0])              /* get the rest of cs */
498 +                cset[++cset[0]] = cs[j++];
499 +        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
500 +                cset[0] = MAXCSET;
501 +        /* setcopy(cs, cset); */        /* copy cset back to cs */
502 +        os = cset;
503 +        for (i = os[0]; i-- >= 0; )
504 +                *cs++ = *os++;
505   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines