home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / povsrc.sit / SOURCE / VECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  21.7 KB  |  821 lines

  1. /****************************************************************************
  2. *                vect.c
  3. *
  4. *  This file was written by Alexander Enzmann.  He wrote the code for
  5. *  4th-6th order shapes and generously provided us these enhancements.
  6. *
  7. *  from Persistence of Vision Raytracer 
  8. *  Copyright 1992 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. *  Copying, distribution and legal info is in the file povlegal.doc which
  11. *  should be distributed with this file. If povlegal.doc is not available
  12. *  or for more info please contact:
  13. *
  14. *       Drew Wells [POV-Team Leader] 
  15. *       CIS: 73767,1244  Internet: 73767.1244@compuserve.com
  16. *       Phone: (213) 254-4041
  17. * This program is based on the popular DKB raytracer version 2.12.
  18. * DKBTrace was originally written by David K. Buck.
  19. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  20. *
  21. *****************************************************************************/
  22.  
  23. #include "frame.h"
  24. #include "povproto.h"
  25. #include "vector.h"
  26.  
  27. #ifdef ABS
  28. #undef ABS
  29. #endif
  30.  
  31. #ifdef MAX
  32. #undef MAX
  33. #endif
  34.  
  35. #undef EPSILON
  36. #define EPSILON 1.0e-10
  37. #define COEFF_LIMIT 1.0e-20
  38.  
  39. /*                  WARNING     WARNING    WARNING
  40.  
  41.    The following three constants have been defined so that quartic equations
  42.    will properly render on fpu/compiler combinations that only have 64 bit
  43.    IEEE precision.  Do not arbitrarily change any of these values.
  44.  
  45.    If you have a machine with a proper fpu/compiler combo - like a Mac with
  46.    a 68881, then use the native floating format (96 bits) and tune the
  47.    values for a little less tolerance: something like: factor1 = 1.0e15,
  48.    factor2 = -1.0e-7, factor3 = 1.0e-10.
  49.  
  50.    The meaning of the three constants are:
  51.       factor1 - the magnitude of difference between coefficients in a
  52.                 quartic equation at which the Sturmian root solver will
  53.         kick in.  The Sturm solver is quite a bit slower than
  54.         the algebraic solver, so the bigger this is, the faster
  55.         the root solving will go.  The algebraic solver is less
  56.         accurate so the bigger this is, the more likely you will
  57.         get bad roots.
  58.  
  59.       factor2 - Tolerance value that defines how close the quartic equation
  60.         is to being a square of a quadratic.  The closer this can
  61.         get to zero before roots disappear, the less the chance
  62.         you will get spurious roots.
  63.  
  64.       factor3 - Similar to factor2 at a later stage of the algebraic solver.
  65. */
  66. #define FUDGE_FACTOR1 1.0e11
  67. #define FUDGE_FACTOR2 -1.0e-5
  68. #define FUDGE_FACTOR3 1.0e-7
  69.  
  70. #define ABS(x) ((x) < 0.0 ? (0.0 - x) : (x))
  71. #define MAX(x,y) (x<y?y:x)
  72. #define TWO_PI 6.283185207179586476925286766560
  73. #define TWO_PI_3  2.0943951023931954923084
  74. #define TWO_PI_43 4.1887902047863909846168
  75. #define MAX_ITERATIONS 50
  76. #define MAXPOW 32
  77.  
  78. typedef struct p {
  79.    int ord;
  80.    DBL coef[MAX_ORDER+1];
  81. } polynomial;
  82.  
  83. static int modp PARAMS((polynomial *u, polynomial *v, polynomial *r));
  84. int regula_falsa PARAMS((int order, DBL *coef, DBL a, DBL b, DBL *val));
  85. void sbisect PARAMS((int np, polynomial *sseq, DBL min, DBL max, int atmin, int atmax, DBL *roots));
  86. int numchanges PARAMS((int np, polynomial *sseq, DBL a));
  87. DBL polyeval PARAMS((DBL x, int n, DBL *Coeffs));
  88. int buildsturm PARAMS((int ord, polynomial *sseq));
  89. int visible_roots PARAMS((int np, polynomial *sseq, int *atneg, int *atpos));
  90. static int difficult_coeffs PARAMS((int n, DBL *x));
  91.  
  92. extern int Shadow_Test_Flag;
  93.  
  94. /*
  95.  * modp
  96.  *
  97.  *   calculates the modulus of u(x) / v(x) leaving it in r, it
  98.  *  returns 0 if r(x) is a constant.
  99.  *  note: this function assumes the leading coefficient of v 
  100.  *   is 1 or -1
  101.  */
  102. static int modp(u, v, r)
  103. polynomial   *u, *v, *r;
  104. {
  105.    int    i, k, j;
  106.    for (i=0;i<u->ord;i++)
  107.       r[i] = u[i];
  108.  
  109.    if (v->coef[v->ord] < 0.0) {
  110.       for (k = u->ord - v->ord - 1; k >= 0; k -= 2)
  111.          r->coef[k] = -r->coef[k];
  112.       for (k = u->ord - v->ord; k >= 0; k--)
  113.          for (j = v->ord + k - 1; j >= k; j--)
  114.             r->coef[j] = -r->coef[j] - r->coef[v->ord + k] * v->coef[j - k];
  115.    }
  116.    else {
  117.       for (k = u->ord - v->ord; k >= 0; k--)
  118.             for (j = v->ord + k - 1; j >= k; j--)
  119.                r->coef[j] -= r->coef[v->ord + k] * v->coef[j - k];
  120.    }
  121.  
  122.    k = v->ord - 1;
  123.    while (k >= 0 && fabs(r->coef[k]) < COEFF_LIMIT) {
  124.       r->coef[k] = 0.0;
  125.       k--;
  126.    }
  127.    r->ord = (k < 0) ? 0 : k;
  128.    return(r->ord);
  129. }
  130.  
  131. /* Build the sturmian sequence for a polynomial */
  132. int buildsturm(ord, sseq)
  133. int      ord;
  134. polynomial   *sseq;
  135. {
  136.    int      i;
  137.    DBL   f, *fp, *fc;
  138.    polynomial   *sp;
  139.  
  140.    sseq[0].ord = ord;
  141.    sseq[1].ord = ord - 1;
  142.  
  143.    /* calculate the derivative and normalize the leading coefficient. */
  144.    f = fabs(sseq[0].coef[ord] * ord);
  145.    fp = sseq[1].coef;
  146.    fc = sseq[0].coef + 1;
  147.    for (i = 1; i <= ord; i++)
  148.       *fp++ = *fc++ * i / f;
  149.  
  150.    /* construct the rest of the Sturm sequence */
  151.    for (sp = sseq + 2;modp(sp - 2, sp - 1, sp); sp++) {
  152.       /* reverse the sign and normalize */
  153.       f = -fabs(sp->coef[sp->ord]);
  154.       for (fp = &sp->coef[sp->ord]; fp >= sp->coef; fp--)
  155.          *fp /= f;
  156.    }
  157.    sp->coef[0] = -sp->coef[0];   /* reverse the sign */
  158.    return(sp - sseq);
  159. }
  160.  
  161. /* Find out how many visible intersections there are */
  162. int visible_roots(np, sseq, atzer, atpos)
  163. int np;
  164. polynomial *sseq;
  165. int *atzer, *atpos;
  166. {
  167.    int atposinf, atzero;
  168.    polynomial *s;
  169.    DBL f, lf;
  170.  
  171.    atposinf = atzero = 0;
  172.    /* changes at positve infinity */
  173.    lf = sseq[0].coef[sseq[0].ord];
  174.    for (s = sseq + 1; s <= sseq + np; s++) {
  175.       f = s->coef[s->ord];
  176.       if (lf == 0.0 || lf * f < 0)
  177.          atposinf++;
  178.       lf = f;
  179.    }
  180.  
  181.    /* Changes at zero */
  182.    lf = sseq[0].coef[0];
  183.    for (s = sseq+1; s <= sseq + np; s++) {
  184.       f = s->coef[0];
  185.       if (lf == 0.0 || lf * f < 0)
  186.          atzero++;
  187.       lf = f;
  188.    }
  189.  
  190.    *atzer = atzero;
  191.    *atpos = atposinf;
  192.    return(atzero - atposinf);
  193. }
  194.  
  195. /*
  196.  * numchanges
  197.  *
  198.  *   return the number of sign changes in the Sturm sequence in
  199.  * sseq at the value a.
  200.  */
  201. int numchanges(np, sseq, a)
  202. int      np;
  203. polynomial   *sseq;
  204. DBL   a;
  205.  
  206. {
  207.    int      changes;
  208.    DBL   f, lf;
  209.    polynomial   *s;
  210.    changes = 0;
  211.    lf = polyeval(a, sseq[0].ord, sseq[0].coef);
  212.    for (s = sseq + 1; s <= sseq + np; s++) {
  213.       f = polyeval(a, s->ord, s->coef);
  214.       if (lf == 0.0 || lf * f < 0)
  215.          changes++;
  216.       lf = f;
  217.    }
  218.    return(changes);
  219. }
  220.  
  221. /*
  222.  * sbisect
  223.  *
  224.  *   uses a bisection based on the sturm sequence for the polynomial
  225.  * described in sseq to isolate intervals in which roots occur,
  226.  * the roots are returned in the roots array in order of magnitude.
  227.  
  228. Note: This routine has one severe bug: When the interval containing the
  229.       root [min, max] has a root at one of its endpoints, as well as one
  230.       within the interval, the root at the endpoint will be returned rather
  231.       than the one inside.
  232.  
  233.  */
  234. void sbisect(np, sseq, min_value, max_value, atmin, atmax, roots)
  235. int      np;
  236. polynomial   *sseq;
  237. DBL   min_value, max_value;
  238. int      atmin, atmax;
  239. DBL   *roots;
  240. {
  241.    DBL  mid;
  242.    int  n1, n2, its, atmid, nroot;
  243.  
  244.    if ((nroot = atmin - atmax) == 1) {
  245.       /* first try using regula-falsa to find the root.  */
  246.       if (regula_falsa(sseq->ord, sseq->coef, min_value, max_value, roots))
  247.          return;
  248.       else {
  249.          /* That failed, so now find it by bisection */
  250.             for (its = 0; its < MAX_ITERATIONS; its++) {
  251.                mid = (min_value + max_value) / 2;
  252.                atmid = numchanges(np, sseq, mid);
  253.                if (fabs(mid) > EPSILON) {
  254.                   if (fabs((max_value - min_value) / mid) < EPSILON) {
  255.                      roots[0] = mid;
  256.                      return;
  257.                   }
  258.                }
  259.                else if (fabs(max_value - min_value) < EPSILON) {
  260.                   roots[0] = mid;
  261.                   return;
  262.                }
  263.                else if ((atmin - atmid) == 0)
  264.                   min_value = mid;
  265.                else
  266.                   max_value = mid;
  267.             }
  268.          /* Bisection took too long - just return what we got */
  269.          roots[0] = mid;
  270.          return;
  271.       }
  272.    }
  273.  
  274.    /* There is more than one root in the interval.
  275.       Bisect to find new intervals */
  276.    for (its = 0; its < MAX_ITERATIONS; its++) {
  277.       mid = (min_value + max_value) / 2;
  278.       atmid = numchanges(np, sseq, mid);
  279.       n1 = atmin - atmid;
  280.       n2 = atmid - atmax;
  281.       if (n1 != 0 && n2 != 0) {
  282.          sbisect(np, sseq, min_value, mid, atmin, atmid, roots);
  283.          sbisect(np, sseq, mid, max_value, atmid, atmax, &roots[n1]);
  284.          return;
  285.       }
  286.       if (n1 == 0)
  287.          min_value = mid;
  288.       else
  289.          max_value = mid;
  290.    }
  291.  
  292.    /* Took too long to bisect.  Just return what we got. */
  293.    for (n1 = atmax; n1 < atmin; n1++)
  294.       roots[n1 - atmax] = mid;
  295. }
  296.  
  297. DBL polyeval(x, n, Coeffs)
  298. DBL x, *Coeffs;
  299. int n;
  300. {
  301.    register int i;
  302.    DBL val;
  303.    val = Coeffs[n];
  304.    for (i=n-1;i>=0;i--) val = val * x + Coeffs[i];
  305.    return val;
  306. }
  307.  
  308. /* Close in on a root by using regula-falsa */
  309. int regula_falsa(order, coef, a, b, val)
  310. int order;
  311. DBL *coef;
  312. DBL a, b, *val;
  313. {
  314.    int its;
  315.    DBL fa, fb, x, lx, fx, lfx;
  316.  
  317.    fa = polyeval(a, order, coef);
  318.    fb = polyeval(b, order, coef);
  319.  
  320.    if (fa * fb > 0.0)
  321.       return 0;
  322.  
  323.    if (fabs(fa) < COEFF_LIMIT) {
  324.       *val = a;
  325.       return 1;
  326.    }
  327.  
  328.    if (fabs(fb) < COEFF_LIMIT) {
  329.       *val = b;
  330.       return 1;
  331.    }
  332.  
  333.    lfx = fa;
  334.    lx = a;
  335.  
  336.    for (its = 0; its < MAX_ITERATIONS; its++) {
  337.       x = (fb * a - fa * b) / (fb - fa);
  338.       fx = polyeval(x, order, coef);
  339.  
  340.       if (fabs(x) > EPSILON) {
  341.          if (fabs(fx / x) < EPSILON) {
  342.             *val = x;
  343.             return 1;
  344.          }
  345.       }
  346.       else if (fabs(fx) < EPSILON) {
  347.          *val = x;
  348.          return 1;
  349.       }
  350.  
  351.       if (fa < 0)
  352.          if (fx < 0) {
  353.             a = x;
  354.             fa = fx;
  355.             if ((lfx * fx) > 0)
  356.                fb /= 2;
  357.          }
  358.          else {
  359.             b = x;
  360.             fb = fx;
  361.             if ((lfx * fx) > 0)
  362.                fa /= 2;
  363.          }
  364.       else if (fx < 0) {
  365.          b = x;
  366.          fb = fx;
  367.          if ((lfx * fx) > 0)
  368.             fa /= 2;
  369.       }
  370.       else {
  371.          a = x;
  372.          fa = fx;
  373.          if ((lfx * fx) > 0)
  374.             fb /= 2;
  375.       }
  376.       if (fabs(b-a) < EPSILON) {
  377.          /* Check for underflow in the domain */
  378.          *val = x;
  379.          return 1;
  380.       }
  381.       lx = x;
  382.       lfx = fx;
  383.    }
  384.    return 0;
  385. }
  386.  
  387. /*
  388.    Solve the quadratic equation:
  389.               x[0] * x^2 + x[1] * x + x[2] = 0.
  390.  
  391.    The value returned by this function is the number of real roots.
  392.    The roots themselves are returned in y[0], y[1].
  393. */
  394. solve_quadratic(x, y)
  395. DBL *x, *y;
  396. {
  397.    DBL d, t, a, b, c;
  398.    a = x[0];
  399.    b = -x[1];
  400.    c = x[2];
  401.    if (a == 0.0) {
  402.       if (b == 0.0)
  403.          return 0;
  404.       y[0] = c / b;
  405.       return 1;
  406.    }
  407.    d = b * b - 4.0 * a * c;
  408.    if (d < 0.0)
  409.       return 0;
  410.    else if (fabs(d) < COEFF_LIMIT) {
  411.       y[0] = 0.5 * b / a;
  412.       return 1;
  413.    }
  414.    d = sqrt(d);
  415.    t = 2.0 * a;
  416.    y[0] = (b + d) / t;
  417.    y[1] = (b - d) / t;
  418.    return 2;
  419. }
  420.  
  421. /*
  422.    Solve the cubic equation:
  423.  
  424.       x[0] * x^3 + x[1] * x^2 + x[2] * x + x[3] = 0.
  425.  
  426.    The result of this function is an integer that tells how many real
  427.    roots exist.  Determination of how many are distinct is up to the
  428.    process that calls this routine.  The roots that exist are stored
  429.    in (y[0], y[1], y[2]).
  430.  
  431.    Note: this function relies very heavily on trigonometric functions and
  432.    the square root function.  If an alternative solution is found that does
  433.    not rely on transcendentals this code will be replaced.
  434. */
  435. int solve_cubic(x, y)
  436. DBL *x, *y;
  437. {
  438.    DBL Q, R, Q3, R2, sQ, d, an, theta;
  439.    DBL A2, a0, a1, a2, a3;
  440.    a0 = x[0];
  441.    if (a0 == 0.0) return solve_quadratic(&x[1], y);
  442.    else if (a0 != 1.0) {
  443.       a1 = x[1] / a0;
  444.       a2 = x[2] / a0;
  445.       a3 = x[3] / a0;
  446.    }
  447.    else {
  448.       a1 = x[1];
  449.       a2 = x[2];
  450.       a3 = x[3];
  451.    }
  452.    A2 = a1 * a1;
  453.    Q = (A2 - 3.0 * a2) / 9.0;
  454.    R = (2.0 * A2 * a1 - 9.0 * a1 * a2 + 27.0 * a3) / 54.0;
  455.    Q3 = Q * Q * Q;
  456.    R2 = R * R;
  457.    d = Q3 - R2;
  458.    an = a1 / 3.0;
  459.    if (d >= 0.0) {
  460.       /* Three real roots. */
  461.       d = R / sqrt(Q3);
  462.       theta = acos(d) / 3.0;
  463.       sQ = -2.0 * sqrt(Q);
  464.       y[0] = sQ * cos(theta) - an;
  465.       y[1] = sQ * cos(theta + TWO_PI_3) - an;
  466.       y[2] = sQ * cos(theta + TWO_PI_43) - an;
  467.       return 3;
  468.    }
  469.    else {
  470.       sQ = pow(sqrt(R2 - Q3) + ABS(R), 1.0 / 3.0);
  471.       if (R < 0)
  472.          y[0] = (sQ + Q / sQ) - an;
  473.       else
  474.          y[0] = -(sQ + Q / sQ) - an;
  475.       return 1;
  476.    }
  477. }
  478.  
  479. /* Test to see if any coeffs are more than 6 orders of magnitude
  480.    larger than the smallest */
  481. static int
  482. difficult_coeffs(n, x)
  483. int n;
  484. DBL *x;
  485. {
  486.    int i;
  487.    DBL biggest;
  488.  
  489.    biggest = 0.0;
  490.    for (i=0;i<=n;i++)
  491.       if (fabs(x[i]) > biggest)
  492.          biggest = x[i];
  493.  
  494.    /* Everything is zero no sense in doing any more */
  495.    if (biggest == 0.0) return 0;
  496.  
  497.    for (i=0;i<=n;i++)
  498.       if (x[i] != 0.0)
  499.          if (fabs(biggest / x[i]) > FUDGE_FACTOR1)
  500.             return 1;
  501.  
  502.    return 0;
  503. }
  504.  
  505. int solve_quartic(x, results)
  506. DBL *x, *results;
  507. {
  508.    DBL cubic[4], roots[3];
  509.    DBL a0, a1, y, d1, x1, t1, t2;
  510.    DBL c0, c1, c2, c3, c4, d2, q1, q2;
  511.    int i;
  512.  
  513.    /* Figure out the size difference between coefficients */
  514.    if (difficult_coeffs(4, x)) {
  515.       if (fabs(x[0]) < COEFF_LIMIT)
  516.          if (fabs(x[1]) < COEFF_LIMIT)
  517.             return solve_quadratic(&x[2], results);
  518.          else
  519.             return solve_cubic(&x[1], results);
  520.       else
  521.          return polysolve(4, x, results);
  522.    }
  523.  
  524.    c0 = x[0];
  525.    if (fabs(c0) < COEFF_LIMIT)
  526.       return solve_cubic(&x[1], results);
  527.    else if (fabs(x[4]) < COEFF_LIMIT) {
  528.       return solve_cubic(x, results);
  529.    }
  530.    else if (c0 != 1.0) {
  531.       c1 = x[1] / c0;
  532.       c2 = x[2] / c0;
  533.       c3 = x[3] / c0;
  534.       c4 = x[4] / c0;
  535.    }
  536.    else {
  537.       c1 = x[1];
  538.       c2 = x[2];
  539.       c3 = x[3];
  540.       c4 = x[4];
  541.    }
  542.  
  543.       /* The first step is to take the original equation:
  544.  
  545.      x^4 + b*x^3 + c*x^2 + d*x + e = 0
  546.  
  547.       and rewrite it as:
  548.  
  549.      x^4 + b*x^3 = -c*x^2 - d*x - e,
  550.  
  551.       adding (b*x/2)^2 + (x^2 + b*x/2)y + y^2/4 to each side gives a
  552.       perfect square on the lhs:
  553.  
  554.      (x^2 + b*x/2 + y/2)^2 = (b^2/4 - c + y)x^2 + (b*y/2 - d)x + y^2/4 - e
  555.  
  556.       By choosing the appropriate value for y, the rhs can be made a perfect
  557.       square also.  This value is found when the rhs is treated as a quadratic
  558.       in x with the discriminant equal to 0.  This will be true when:
  559.  
  560.      (b*y/2 - d)^2 - 4.0 * (b^2/4 - c*y)*(y^2/4 - e) = 0, or
  561.  
  562.      y^3 - c*y^2 + (b*d - 4*e)*y - b^2*e + 4*c*e - d^2 = 0.
  563.  
  564.       This is called the resolvent of the quartic equation.  */
  565.  
  566.       a0 = 4.0 * c4;
  567.    cubic[0] = 1.0;
  568.    cubic[1] = -1.0 * c2;
  569.    cubic[2] = c1 * c3 - a0;
  570.    cubic[3] = a0 * c2 - c1 * c1 * c4 - c3 * c3;
  571.    i = solve_cubic(&cubic[0], &roots[0]);
  572.    if (i > 0)
  573.       y = roots[0];
  574.    else
  575.       return 0;
  576.  
  577.    /* What we are left with is a quadratic squared on the lhs and a
  578.       linear term on the right.  The linear term has one of two signs,
  579.       take each and add it to the lhs.  The form of the quartic is now:
  580.  
  581.      a' = b^2/4 - c + y,    b' = b*y/2 - d, (from rhs quadritic above)
  582.  
  583.      (x^2 + b*x/2 + y/2) = +sqrt(a'*(x + 1/2 * b'/a')^2), and
  584.      (x^2 + b*x/2 + y/2) = -sqrt(a'*(x + 1/2 * b'/a')^2).
  585.  
  586.       By taking the linear term from each of the right hand sides and
  587.       adding to the appropriate part of the left hand side, two quadratic
  588.       formulas are created.  By solving each of these the four roots of
  589.       the quartic are determined.
  590.    */
  591.    i = 0;
  592.    a0 = c1 / 2.0;
  593.    a1 = y / 2.0;
  594.  
  595.    t1 = a0 * a0 - c2 + y;
  596.    if (t1 < 0.0) {
  597.       if (t1 > FUDGE_FACTOR2)
  598.          t1 = 0.0;
  599.       else {
  600.          /* First Special case, a' < 0 means all roots are complex. */
  601.             return 0;
  602.       }
  603.    }
  604.    if (t1 < FUDGE_FACTOR3) {
  605.       /* Second special case, the "x" term on the right hand side above
  606.      has vanished.  In this case:
  607.         (x^2 + b*x/2 + y/2) = +sqrt(y^2/4 - e), and
  608.         (x^2 + b*x/2 + y/2) = -sqrt(y^2/4 - e).  */
  609.       t2 = a1 * a1 - c4;
  610.       if (t2 < 0.0) {
  611.          return 0;
  612.       }
  613.       x1 = 0.0;
  614.       d1 = sqrt(t2);
  615.    }
  616.    else {
  617.       x1 = sqrt(t1);
  618.       d1 = 0.5 * (a0 * y - c3) / x1;
  619.    }
  620.    /* Solve the first quadratic */
  621.       q1 = -a0 - x1;
  622.    q2 = a1 + d1;
  623.    d2 = q1 * q1 - 4.0 * q2;
  624.    if (d2 >= 0.0) {
  625.       d2 = sqrt(d2);
  626.       results[0] = 0.5 * (q1 + d2);
  627.       results[1] = 0.5 * (q1 - d2);
  628.       i = 2;
  629.    }
  630.    /* Solve the second quadratic */
  631.    q1 = q1 + x1 + x1;
  632.    q2 = a1 - d1;
  633.    d2 = q1 * q1 - 4.0 * q2;
  634.    if (d2 >= 0.0) {
  635.       d2 = sqrt(d2);
  636.       results[i++] = 0.5 * (q1 + d2);
  637.       results[i++] = 0.5 * (q1 - d2);
  638.    }
  639.    return i;
  640. }
  641.  
  642. /* Root solver based on the Sturm sequences for a polynomial. */
  643. int polysolve(order, Coeffs, roots)
  644. int order;
  645. DBL *Coeffs, *roots;
  646. {
  647.    polynomial sseq[MAX_ORDER+1];
  648.    DBL min_value, max_value;
  649.    int i, nroots, nchanges, np, atmin, atmax;
  650.  
  651.    /* Put the coefficients into the top of the stack. */
  652.    for (i=0;i<=order;i++)
  653.       sseq[0].coef[order-i] = Coeffs[i];
  654.  
  655.    /* Build the Sturm sequence */
  656.    np = buildsturm(order, &sseq[0]);
  657.  
  658.    /* Get the total number of visible roots */
  659.    if ((nroots = visible_roots(np, sseq, &atmin, &atmax)) == 0)
  660.       return 0;
  661.  
  662.    /* Bracket the roots */
  663.    if (Shadow_Test_Flag)
  664.       min_value = 0.05;
  665.    else
  666.       min_value = 0.0;
  667.    max_value = Max_Distance;
  668.  
  669.    atmin = numchanges(np, sseq, min_value);
  670.    atmax = numchanges(np, sseq, max_value);
  671.    nroots = atmin - atmax;
  672.    if (nroots == 0) return 0;
  673.  
  674.    /* perform the bisection. */
  675.    sbisect(np, sseq, min_value, max_value, atmin, atmax, roots);
  676.  
  677.    return nroots;
  678. }
  679.  
  680. #ifdef TEST     /* This is not used anywhere or tested.  Interesting? */
  681.  
  682. #define MAX_POLYGON_SIDES 8
  683. #define Crossing_Point(x1, y1, x2, y2) (x1 - y1 * (x2 - x1) / (y2 - y1))
  684.  
  685. /* See if "Ray" intersects the polygon defined by the coordinate list "vertices". */
  686. int Intersect_Polygon(Ray, vertex_count, vertices, n, d, Depth, Intersect_Point)
  687. RAY *Ray;
  688. int vertex_count;
  689. VECTOR *vertices, *n, *Intersect_Point;
  690. DBL d, *Depth;
  691. {
  692.    DBL s, t, x, y, z;
  693.    int Sign_Holder, Next_Sign, Crossings;
  694.    int i, this_vertex, next_vertex;
  695.  
  696.    static DBL temp_x[MAX_POLYGON_SIDES],
  697.    temp_y[MAX_POLYGON_SIDES];
  698.  
  699.    /* Calculate the point of intersection and the depth. */
  700.    VDot(s, Ray->Direction, *n);
  701.    if (s == 0.0)
  702.       return 0;
  703.    VDot(t, Ray->Initial, *n);
  704.    *Depth = 0.0 - (d + t) / s;
  705.    if (*Depth < Small_Tolerance)
  706.       return 0;
  707.    VScale(*Intersect_Point, Ray->Direction, *Depth);
  708.    VAdd(*Intersect_Point, *Intersect_Point, Ray->Initial);
  709.  
  710.    /* Map the intersection point and the triangle onto a plane. */
  711.    x = ABS(n->x); y = ABS(n->y); z = ABS(n->z);
  712.    if (x>y)
  713.       if (x>z)
  714.          for (i=0;i<vertex_count;i++) {
  715.             temp_x[i] = vertices[i].y - Intersect_Point->y;
  716.             temp_y[i] = vertices[i].z - Intersect_Point->z;
  717.          }
  718.       else
  719.          for (i=0;i<vertex_count;i++) {
  720.             temp_x[i] = vertices[i].x - Intersect_Point->x;
  721.             temp_y[i] = vertices[i].y - Intersect_Point->y;
  722.          }
  723.    else if (y>z)
  724.       for (i=0;i<vertex_count;i++) {
  725.          temp_x[i] = vertices[i].x - Intersect_Point->x;
  726.          temp_y[i] = vertices[i].z - Intersect_Point->z;
  727.       }
  728.    else
  729.       for (i=0;i<vertex_count;i++) {
  730.          temp_x[i] = vertices[i].x - Intersect_Point->x;
  731.          temp_y[i] = vertices[i].y - Intersect_Point->y;
  732.       }
  733.  
  734.    /* Determine crossing count */
  735.    Crossings = 0;
  736.    if (temp_y[0] < 0) Sign_Holder = -1;
  737.    else Sign_Holder = 1;
  738.  
  739.       for (i=0;i<vertex_count;i++) {
  740.          /* Start of winding tests, test the segment from v1 to v2 */
  741.          this_vertex = i;
  742.          next_vertex = (i + 1) % vertex_count;
  743.          if (temp_y[next_vertex] < 0) Next_Sign = -1;
  744.          else Next_Sign = 1;
  745.          if (Sign_Holder != Next_Sign) {
  746.             if (temp_x[this_vertex] > 0.0) {
  747.                if (temp_x[next_vertex] > 0.0)
  748.                   Crossings++;
  749.                else if (Crossing_Point(temp_x[this_vertex], temp_y[this_vertex],
  750.                   temp_x[next_vertex], temp_y[next_vertex]) > 0.0)
  751.                   Crossings++;
  752.             }
  753.             else if (temp_x[next_vertex] > 0.0) {
  754.                if (Crossing_Point(temp_x[this_vertex], temp_y[this_vertex],
  755.                   temp_x[next_vertex], temp_y[next_vertex]) > 0.0)
  756.                   Crossings++;
  757.             }
  758.             Sign_Holder = Next_Sign;
  759.          }
  760.       }
  761.  
  762.    return (Crossings&1); /* Inside only if # of crossings odd. */
  763. }
  764.  
  765. /* Translate screen coordinates into world coordinates. */
  766. void World_Coordinate (Viewpoint, Ray, width, height, x, y)
  767. VIEWPOINT *Viewpoint;
  768. RAY *Ray;
  769. int width, height;
  770. DBL x, y;
  771. {
  772.    DBL scalex, scaley;
  773.    VECTOR V1, V2;
  774.  
  775.    /* Convert the X Coordinate to be a DBL from -0.5 to 0.5 */
  776.    scalex = (x - (DBL)width / 2.0) / (DBL)width;
  777.    /* Convert the Y Coordinate to be a DBL from -0.5 to 0.5 */
  778.    scaley = (((DBL)(height - 1) - y) - (DBL)height / 2.0) / (DBL)height;
  779.    /* Compute the direction of the screen point from the viewpoint */
  780.    VScale (V1, Viewpoint->Up, scaley);
  781.    VScale (V2, Viewpoint->Right, scalex);
  782.    VAdd (Ray->Direction, V1, V2);
  783.    VAdd (Ray->Direction, Ray->Direction, Viewpoint->Direction);
  784.    VNormalize (Ray->Direction, Ray->Direction);
  785. }
  786. /* Uncomment these two declarations if your compiler needs them */
  787. /* They give Microsoft C an out of macro expansion space error */
  788. /* void show_univariate_poly PARAMS((char *label, int order,DBL *Coeffs));  */
  789. /* void show_points PARAMS((char *label,int count,DBL *point_list);  */
  790.  
  791. void show_univariate_poly(label, order, Coeffs)
  792. char *label;
  793. int order;
  794. DBL *Coeffs;
  795. {
  796.    int i;
  797.    printf("%s", label);
  798.    for (i=0;i<=order;i++) {
  799.       printf("%.2lf x^%d", Coeffs[i], order-i);
  800.       if (i==order) printf("\n");
  801.       else printf(" + ");
  802.    }
  803. }
  804.  
  805.    void show_points(label, count, point_list)
  806.       char *label;
  807. int count;
  808. DBL *point_list;
  809. {
  810.    int i;
  811.    printf("%s", label);
  812.    for (i=0;i<count;i++) {
  813.       printf("%lf", point_list[i]);
  814.       if (i==(count-1)) printf("\n");
  815.       else printf(", ");
  816.    }
  817. }
  818.  
  819. #endif
  820.