home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / RT / O_CONE.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  4KB  |  137 lines

  1. /* Copyright (c) 1990 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)o_cone.c 2.1 11/12/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  o_cone.c - routine to determine ray intersection with cones.
  9.  *
  10.  *     2/13/86
  11.  */
  12.  
  13. #include  "ray.h"
  14.  
  15. #include  "otypes.h"
  16.  
  17. #include  "cone.h"
  18.  
  19.  
  20. o_cone(o, r)                    /* intersect ray with cone */
  21. OBJREC  *o;
  22. register RAY  *r;
  23. {
  24.     FVECT  rox, rdx;
  25.     double  a, b, c;
  26.     double  root[2];
  27.     int  nroots, rn;
  28.     register CONE  *co;
  29.     register int  i;
  30.                         /* get cone structure */
  31.     co = getcone(o, 1);
  32.  
  33.     /*
  34.      *     To intersect a ray with a cone, we transform the
  35.      *  ray into the cone's normalized space.  This greatly
  36.      *  simplifies the computation.
  37.      *     For a cone or cup, normalization results in the
  38.      *  equation:
  39.      *
  40.      *              x*x + y*y - z*z == 0
  41.      *
  42.      *     For a cylinder or tube, the normalized equation is:
  43.      *
  44.      *              x*x + y*y - r*r == 0
  45.      *
  46.      *     A normalized ring obeys the following set of equations:
  47.      *
  48.      *              z == 0                  &&
  49.      *              x*x + y*y >= r0*r0      &&
  50.      *              x*x + y*y <= r1*r1
  51.      */
  52.  
  53.                     /* transform ray */
  54.     multp3(rox, r->rorg, co->tm);
  55.     multv3(rdx, r->rdir, co->tm);
  56.                     /* compute intersection */
  57.  
  58.     if (o->otype == OBJ_CONE || o->otype == OBJ_CUP) {
  59.  
  60.         a = rdx[0]*rdx[0] + rdx[1]*rdx[1] - rdx[2]*rdx[2];
  61.         b = 2.0*(rdx[0]*rox[0] + rdx[1]*rox[1] - rdx[2]*rox[2]);
  62.         c = rox[0]*rox[0] + rox[1]*rox[1] - rox[2]*rox[2];
  63.  
  64.     } else if (o->otype == OBJ_CYLINDER || o->otype == OBJ_TUBE) {
  65.  
  66.         a = rdx[0]*rdx[0] + rdx[1]*rdx[1];
  67.         b = 2.0*(rdx[0]*rox[0] + rdx[1]*rox[1]);
  68.         c = rox[0]*rox[0] + rox[1]*rox[1] - CO_R0(co)*CO_R0(co);
  69.  
  70.     } else { /* OBJ_RING */
  71.  
  72.         if (rdx[2] <= FTINY && rdx[2] >= -FTINY)
  73.             return(0);                      /* parallel */
  74.         root[0] = -rox[2]/rdx[2];
  75.         if (root[0] <= FTINY || root[0] >= r->rot)
  76.             return(0);                      /* distance check */
  77.         b = root[0]*rdx[0] + rox[0];
  78.         c = root[0]*rdx[1] + rox[1];
  79.         a = b*b + c*c;
  80.         if (a < CO_R0(co)*CO_R0(co) || a > CO_R1(co)*CO_R1(co))
  81.             return(0);                      /* outside radii */
  82.         r->ro = o;
  83.         r->rot = root[0];
  84.         for (i = 0; i < 3; i++)
  85.             r->rop[i] = r->rorg[i] + r->rdir[i]*r->rot;
  86.         VCOPY(r->ron, co->ad);
  87.         r->rod = -rdx[2];
  88.         r->rox = NULL;
  89.         return(1);                              /* good */
  90.     }
  91.                     /* roots for cone, cup, cyl., tube */
  92.     nroots = quadratic(root, a, b, c);
  93.  
  94.     for (rn = 0; rn < nroots; rn++) {       /* check real roots */
  95.         if (root[rn] <= FTINY)
  96.             continue;               /* too small */
  97.         if (root[rn] >= r->rot)
  98.             break;                  /* too big */
  99.                         /* check endpoints */
  100.         for (i = 0; i < 3; i++) {
  101.             rox[i] = r->rorg[i] + root[rn]*r->rdir[i];
  102.             rdx[i] = rox[i] - CO_P0(co)[i];
  103.         }
  104.         b = DOT(rdx, co->ad); 
  105.         if (b < 0.0)
  106.             continue;               /* before p0 */
  107.         if (b > co->al)
  108.             continue;               /* after p1 */
  109.         r->ro = o;
  110.         r->rot = root[rn];
  111.         VCOPY(r->rop, rox);
  112.                         /* get normal */
  113.         if (o->otype == OBJ_CYLINDER)
  114.             a = CO_R0(co);
  115.         else if (o->otype == OBJ_TUBE)
  116.             a = -CO_R0(co);
  117.         else { /* OBJ_CONE || OBJ_CUP */
  118.             c = CO_R1(co) - CO_R0(co);
  119.             a = CO_R0(co) + b*c/co->al;
  120.             if (o->otype == OBJ_CUP) {
  121.                 c = -c;
  122.                 a = -a;
  123.             }
  124.         }
  125.         for (i = 0; i < 3; i++)
  126.             r->ron[i] = (rdx[i] - b*co->ad[i])/a;
  127.         if (o->otype == OBJ_CONE || o->otype == OBJ_CUP)
  128.             for (i = 0; i < 3; i++)
  129.                 r->ron[i] = (co->al*r->ron[i] - c*co->ad[i])
  130.                         /co->sl;
  131.         r->rod = -DOT(r->rdir, r->ron);
  132.         r->rox = NULL;
  133.         return(1);                      /* good */
  134.     }
  135.     return(0);
  136. }
  137.