home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / povsrc.sit / SOURCE / BEZIER.C next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  49.6 KB  |  1,481 lines

  1. /****************************************************************************
  2. *                   bezier.c
  3. *
  4. *  This module implements the code for Bezier bicubic patch shapes
  5. *
  6. *  This file was written by Alexander Enzmann.    He wrote the code for
  7. *  bezier bicubic patches and generously provided us these enhancements.
  8. *
  9. *  from Persistence of Vision Raytracer 
  10. *  Copyright 1992 Persistence of Vision Team
  11. *---------------------------------------------------------------------------
  12. *  Copying, distribution and legal info is in the file povlegal.doc which
  13. *  should be distributed with this file. If povlegal.doc is not available
  14. *  or for more info please contact:
  15. *
  16. *       Drew Wells [POV-Team Leader] 
  17. *       CIS: 73767,1244  Internet: 73767.1244@compuserve.com
  18. *       Phone: (213) 254-4041
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24.  
  25. #include "frame.h"
  26. #include "vector.h"
  27. #include "povproto.h"
  28.  
  29. #undef EPSILON
  30. #ifndef EPSILON
  31. #define EPSILON 1.0e-10
  32. #endif
  33.  
  34. METHODS Bicubic_Patch_Methods =
  35. { Object_Intersect, All_Bicubic_Patch_Intersections,
  36.    Inside_Bicubic_Patch, Bicubic_Patch_Normal, Copy_Bicubic_Patch,
  37.    Translate_Bicubic_Patch, Rotate_Bicubic_Patch,
  38.    Scale_Bicubic_Patch, Invert_Bicubic_Patch};
  39.  
  40. extern long Ray_Bicubic_Tests, Ray_Bicubic_Tests_Succeeded;
  41. extern unsigned int Options;
  42. extern RAY *VP_Ray;
  43. extern int Shadow_Test_Flag;
  44.  
  45. int max_depth_reached;
  46.  
  47. static void bezier_value PARAMS((VECTOR *Result, DBL u, DBL v,
  48. VECTOR (*Control_Points)[4][4]));
  49. static void bezier_partial PARAMS((VECTOR *Result, DBL u, DBL v,
  50. BICUBIC_PATCH *Shape));
  51. static int subpatch_normal PARAMS((VECTOR *v1, VECTOR *v2, VECTOR *v3,
  52. VECTOR *Result, DBL *d));
  53. static int intersect_subpatch PARAMS((int Patch_Type, RAY *Ray,
  54. VECTOR *v1, VECTOR *v2, VECTOR *v3,
  55. VECTOR *n, DBL d,
  56. VECTOR *n1, VECTOR *n2, VECTOR *n3,
  57. DBL *Depth, VECTOR *IP, VECTOR *IP_Norm));
  58. static void find_average PARAMS((int vector_count, VECTOR *vectors,
  59. VECTOR *center, DBL *radius));
  60. static int spherical_bounds_check PARAMS((RAY *Ray, VECTOR *center,
  61. DBL radius));
  62. static int intersect_bicubic_patch0 PARAMS((RAY *Ray, BICUBIC_PATCH *Shape,
  63. DBL *Depths));
  64. static int intersect_bicubic_patch1 PARAMS((RAY *Ray, BICUBIC_PATCH *Shape,
  65. DBL *Depths));
  66. static int intersect_bicubic_patch2 PARAMS((RAY *Ray, BICUBIC_PATCH *Shape,
  67. DBL *Depths));
  68. static int intersect_bicubic_patch3 PARAMS((RAY *Ray, BICUBIC_PATCH *Shape,
  69. DBL *Depths));
  70. static int intersect_bicubic_patch4 PARAMS((RAY *Ray, BICUBIC_PATCH *Shape,
  71. DBL *Depths));
  72. static DBL point_plane_distance PARAMS((VECTOR *, VECTOR *, DBL *));
  73. static DBL determine_subpatch_flatness PARAMS((VECTOR (*)[4][4]));
  74. static int flat_enough PARAMS((BICUBIC_PATCH *, VECTOR (*)[4][4]));
  75. static void bezier_bounding_sphere PARAMS((VECTOR (*)[4][4], VECTOR *,DBL *));
  76. static void bezier_subpatch_intersect PARAMS((RAY *, BICUBIC_PATCH *,
  77. VECTOR (*)[4][4], DBL, DBL, DBL,
  78. int, int *, DBL *, DBL *, DBL *));
  79. static void bezier_split_left_right PARAMS((VECTOR (*)[4][4],VECTOR (*)[4][4],
  80. VECTOR (*)[4][4]));
  81. static void bezier_split_up_down PARAMS((VECTOR (*)[4][4], VECTOR (*)[4][4],
  82. VECTOR (*)[4][4]));
  83. static void bezier_subdivider PARAMS((RAY *, BICUBIC_PATCH *,VECTOR (*)[4][4],
  84. DBL, DBL, DBL, DBL, int, int *, DBL *,
  85. DBL *, DBL *));
  86. static void bezier_tree_deleter PARAMS((BEZIER_NODE *Node));
  87. static BEZIER_NODE *bezier_tree_builder PARAMS((BICUBIC_PATCH *,
  88. VECTOR(*)[4][4], int));
  89. static void bezier_tree_walker PARAMS((RAY *, BICUBIC_PATCH *, BEZIER_NODE *,
  90. int, int *, DBL *));
  91. static BEZIER_NODE *create_new_bezier_node PARAMS((void));
  92. static BEZIER_VERTICES *create_bezier_vertex_block PARAMS((void));
  93. static BEZIER_CHILDREN *create_bezier_child_block PARAMS((void));
  94.  
  95.  
  96. static BEZIER_NODE *
  97. create_new_bezier_node()
  98. {
  99.    BEZIER_NODE *Node = (BEZIER_NODE *)malloc(sizeof(BEZIER_NODE));
  100.    if (Node == NULL) {
  101.       printf("Failed to allocate Bezier node\n");
  102.       exit(0);
  103.    }
  104.    Node->Data_Ptr = NULL;
  105.    return Node;
  106. }
  107.  
  108. static BEZIER_VERTICES *
  109. create_bezier_vertex_block()
  110. {
  111.    BEZIER_VERTICES *Vertices = (BEZIER_VERTICES *)malloc(sizeof(BEZIER_VERTICES));
  112.    if (Vertices == NULL) {
  113.       printf("Failed to allocate Bezier vertices\n");
  114.       exit(0);
  115.    }
  116.    return Vertices;
  117. }
  118.  
  119. static BEZIER_CHILDREN *
  120. create_bezier_child_block()
  121. {
  122.    BEZIER_CHILDREN *Children = (BEZIER_CHILDREN *)malloc(sizeof(BEZIER_CHILDREN));
  123.    if (Children == NULL) {
  124.       printf("Failed to allocate Bezier children\n");
  125.       exit(0);
  126.    }
  127.    return Children;
  128. }
  129.  
  130. static BEZIER_NODE *bezier_tree_builder(Object, Patch, depth)
  131. BICUBIC_PATCH *Object;
  132. VECTOR (*Patch)[4][4];
  133. int depth;
  134. {
  135.    VECTOR Lower_Left[4][4], Lower_Right[4][4];
  136.    VECTOR Upper_Left[4][4], Upper_Right[4][4];
  137.    BEZIER_CHILDREN *Children;
  138.    BEZIER_VERTICES *Vertices;
  139.    BEZIER_NODE *Node = create_new_bezier_node();
  140.  
  141.    if (depth > max_depth_reached) max_depth_reached = depth;
  142.  
  143.    /* Build the bounding sphere for this subpatch */
  144.    bezier_bounding_sphere(Patch, &(Node->Center), &(Node->Radius_Squared));
  145.  
  146.    /* If the patch is close to being flat, then just perform a ray-plane
  147.       intersection test. */
  148.    if (flat_enough(Object, Patch)) {
  149.       /* The patch is now flat enough to simply store the corners */
  150.       Node->Node_Type = BEZIER_LEAF_NODE;
  151.       Vertices = create_bezier_vertex_block();
  152.       Vertices->Vertices[0] = (*Patch)[0][0];
  153.       Vertices->Vertices[1] = (*Patch)[0][3];
  154.       Vertices->Vertices[2] = (*Patch)[3][3];
  155.       Vertices->Vertices[3] = (*Patch)[3][0];
  156.       Node->Data_Ptr = (void *)Vertices;
  157.    }
  158.    else if (depth >= Object->U_Steps)
  159.       if (depth >= Object->V_Steps) {
  160.          /* We are at the max recursion depth. Just store corners. */
  161.          Node->Node_Type = BEZIER_LEAF_NODE;
  162.          Vertices = create_bezier_vertex_block();
  163.          Vertices->Vertices[0] = (*Patch)[0][0];
  164.          Vertices->Vertices[1] = (*Patch)[0][3];
  165.          Vertices->Vertices[2] = (*Patch)[3][3];
  166.          Vertices->Vertices[3] = (*Patch)[3][0];
  167.          Node->Data_Ptr = (void *)Vertices;
  168.       }
  169.       else {
  170.          bezier_split_up_down(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Upper_Left);
  171.          Node->Node_Type = BEZIER_INTERIOR_NODE;
  172.          Children = create_bezier_child_block();
  173.          Children->Children[0]=bezier_tree_builder(Object,(VECTOR (*)[4][4])Lower_Left,depth+1);
  174.          Children->Children[1]=bezier_tree_builder(Object,(VECTOR (*)[4][4])Upper_Left,depth+1);
  175.          Node->Count = 2;
  176.          Node->Data_Ptr = (void *)Children;
  177.       }
  178.    else if (depth >= Object->V_Steps) {
  179.       bezier_split_left_right(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Right);
  180.       Node->Node_Type = BEZIER_INTERIOR_NODE;
  181.       Children = create_bezier_child_block();
  182.       Children->Children[0] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Lower_Left,depth+1);
  183.       Children->Children[1] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Lower_Right,depth+1);
  184.       Node->Count = 2;
  185.       Node->Data_Ptr = (void *)Children;
  186.    }
  187.    else {
  188.       bezier_split_left_right(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Right);
  189.       bezier_split_up_down((VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Upper_Left);
  190.       bezier_split_up_down((VECTOR (*)[4][4])Lower_Right, (VECTOR (*)[4][4])Lower_Right, (VECTOR (*)[4][4])Upper_Right);
  191.       Node->Node_Type = BEZIER_INTERIOR_NODE;
  192.       Children = create_bezier_child_block();
  193.       Children->Children[0] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Lower_Left,depth+1);
  194.       Children->Children[1] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Upper_Left,depth+1);
  195.       Children->Children[2] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Lower_Right,depth+1);
  196.       Children->Children[3] = bezier_tree_builder(Object,(VECTOR (*)[4][4])Upper_Right,depth+1);
  197.       Node->Count = 4;
  198.       Node->Data_Ptr = (void *)Children;
  199.    }
  200.    return Node;
  201. }
  202.  
  203.    /* Evaluate a single coordinate point (u, v) on a bezier patch. */
  204.    static void bezier_value(Result, u, v, Control_Points)
  205.       VECTOR *Result;
  206. DBL u, v;
  207. VECTOR (*Control_Points)[4][4];
  208. {
  209.    DBL u2, u3, v2, v3, uu1, uu2, uu3, vv1, vv2, vv3;
  210.    DBL t[4][4];
  211.    int i, j;
  212.  
  213.    u2 = u * u; u3 = u * u2; v2 = v * v; v3 = v * v2;
  214.    uu1 = 1.0 - u; uu2 = uu1 * uu1; uu3 = uu1 * uu2;
  215.    vv1 = 1.0 - v; vv2 = vv1 * vv1; vv3 = vv1 * vv2;
  216.    t[0][0] =         uu3 *        vv3;
  217.    t[0][1] = 3.0 *     uu3 * v  * vv2;
  218.    t[0][2] = 3.0 *     uu3 * v2 * vv1;
  219.    t[0][3] =         uu3 * v3;
  220.    t[1][0] = 3.0 *  u  * uu2 *        vv3;
  221.    t[1][1] = 9.0 *  u  * uu2 * v  * vv2;
  222.    t[1][2] = 9.0 *  u  * uu2 * v2 * vv1;
  223.    t[1][3] = 3.0 *  u  * uu2 * v3;
  224.    t[2][0] = 3.0 *  u2 * uu1 *        vv3;
  225.    t[2][1] = 9.0 *  u2 * uu1 * v  * vv2;
  226.    t[2][2] = 9.0 *  u2 * uu1 * v2 * vv1;
  227.    t[2][3] = 3.0 *  u2 * uu1 * v3;
  228.    t[3][0] =        u3 *        vv3;
  229.    t[3][1] = 3.0 *  u3 *       v  * vv2;
  230.    t[3][2] = 3.0 *  u3 *       v2 * vv1;
  231.    t[3][3] =        u3 *       v3;
  232.  
  233.    Result->x = 0.0;
  234.    Result->y = 0.0;
  235.    Result->z = 0.0;
  236.    for (i=0;i<4;i++) for (j=0;j<4;j++) {
  237.       Result->x += t[i][j] * (*Control_Points)[i][j].x;
  238.       Result->y += t[i][j] * (*Control_Points)[i][j].y;
  239.       Result->z += t[i][j] * (*Control_Points)[i][j].z;
  240.    }
  241. }
  242.  
  243. /* Calculate the normal to a bezier patch for a particular axis, at
  244.    a particular point on the patch.
  245.  
  246.    The normal at a point of a parametric surface z = f(u, v) is:
  247.  
  248.       (|[[dy/du, dy/dv],[dz/du, dz/dv]]|,
  249.        |[[dz/du, dz/dv],[dx/du, dx/dv]]|,
  250.        |[[dx/du, dx/dv],[dy/du, dy/dv]]|)
  251.  
  252.    The normal is undefined where the determinants vanish.
  253. */
  254. static void bezier_partial(Result, u, v, Shape)
  255. VECTOR *Result;
  256. DBL u, v;
  257. BICUBIC_PATCH *Shape;
  258. {
  259.    VECTOR U, V; /* Partial derivatives with respect to u, and v. */
  260.    DBL u2, u3, v2, v3;
  261.    DBL t[4][4], Temp;
  262.    int i, j;
  263.  
  264.    u2 = u * u; u3 = u * u2; v2 = v * v; v3 = v * v2;
  265.  
  266.    /* Calculate the derivative with respect to u */
  267.    t[0][0] = 3.0 *    (v3-3.0*v2+3.0*v-1.0) * (u2-2.0*u+1.0);
  268.    t[0][1] = 9.0 * v  * (v2-2.0*v+1.0) * (u2-2.0*u+1.0);
  269.    t[0][2] = 9.0 * v2 * (v-1.0) * (u2-2.0*u+1.0);
  270.    t[0][3] = 3.0 * v3 * (u2-2.0*u+1.0);
  271.    t[1][0] = 3.0 *    (v3-3.0*v2+3.0*v-1) * (3.0*u2-4.0*u+1);
  272.    t[1][1] = 9.0 * v  * (v2-2.0*v+1.0) * (3.0*u2-4.0*u+1.0);
  273.    t[1][2] = 9.0 * v2 * (v-1.0) * (3.0*u2-4.0*u+1.0);
  274.    t[1][3] = 3.0 * v3 * (3.0*u2-4.0*u+1.0);
  275.    t[2][0] = 3.0 * u  * (v3-3.0*v2+3.0*v-1.0)*(3.0*u-2.0);
  276.    t[2][1] = 9.0 * u  * v  * (v2-2.0*v+1.0)*(3.0*u-2.0);
  277.    t[2][2] = 9.0 * u  * v2 * (v-1.0)*(3.0*u-2.0);
  278.    t[2][3] = 3.0 * u  * v3 * (3.0*u-2.0);
  279.    t[3][0] = 3.0 * u2 * (v3-3.0*v2+3.0*v-1.0);
  280.    t[3][1] = 9.0 * u2 * v  * (v2-2.0*v+1.0);
  281.    t[3][2] = 9.0 * u2 * v2 * (v-1.0);
  282.    t[3][3] = 3.0 * u2 * v3;
  283.    U.x = 0.0; U.y = 0.0; U.z = 0.0;
  284.    for (i=0;i<4;i++) for (j=0;j<4;j++) {
  285.       U.x += t[i][j] * Shape->Control_Points[i][j].x;
  286.       U.y += t[i][j] * Shape->Control_Points[i][j].y;
  287.       U.z += t[i][j] * Shape->Control_Points[i][j].z;
  288.    }
  289.    Temp = U.x * U.x + U.y * U.y + U.z * U.z;
  290.    if (Temp < EPSILON) {
  291.       /* Partial with respect to u is undefined. */
  292.       Result->x = 1.0; Result->y = 0.0; Result->z = 0.0;
  293.       /* *Result = *n; */
  294.       return;
  295.    }
  296.    else {
  297.       Temp = sqrt(Temp);
  298.       VInverseScale(U, U, Temp);
  299.    }
  300.    /* Calculate the derivative with respect to v */
  301.       t[0][0] = 3.0 * (v2-2.0*v+1.0) * (u3-3.0*u2+3.0*u-1.0);
  302.    t[0][1] = 3.0 * (3.0*v2-4.0*v+1.0) * (u3-3.0*u2+3.0*u-1.0);
  303.    t[0][2] = 3.0 * v  * (3.0*v-2.0) * (u3-3.0*u2+3.0*u-1.0);
  304.    t[0][3] = 3.0 * v2 * (u3-3.0*u2+3.0*u-1.0);
  305.    t[1][0] = 9.0 * u  * (v2-2.0*v+1.0) * (u2-2.0*u+1.0);
  306.    t[1][1] = 9.0 * u  * (3.0*v2-4.0*v+1.0) * (u2-2.0*u+1.0);
  307.    t[1][2] = 9.0 * u  * v * (3.0*v-2.0) * (u2-2.0*u+1.0);
  308.    t[1][3] = 9.0 * u  * v2 * (u2-2.0*u+1.0);
  309.    t[2][0] = 9.0 * u2 * (v2-2.0*v+1.0) * (u-1.0);
  310.    t[2][1] = 9.0 * u2 * (3.0*v2-4.0*v+1.0) * (u-1.0);
  311.    t[2][2] = 9.0 * u2 * v * (3.0*v-2.0) * (u-1.0);
  312.    t[2][3] = 9.0 * u2 * v2 * (u-1.0);
  313.    t[3][0] = 3.0 * u3 * (v2-2.0*v+1.0);
  314.    t[3][1] = 3.0 * u3 * (3.0*v2-4.0*v+1.0);
  315.    t[3][2] = 3.0 * u3 * v  * (3.0*v-2.0);
  316.    t[3][3] = 3.0 * u3 * v2;
  317.    V.x = 0.0; V.y = 0.0; V.z = 0.0;
  318.    for (i=0;i<4;i++) for (j=0;j<4;j++) {
  319.       V.x += t[i][j] * Shape->Control_Points[i][j].x;
  320.       V.y += t[i][j] * Shape->Control_Points[i][j].y;
  321.       V.z += t[i][j] * Shape->Control_Points[i][j].z;
  322.    }
  323.    Temp = V.x * V.x + V.y * V.y + V.z * V.z;
  324.    if (Temp < EPSILON) {
  325.       /* Partial with respect to u is undefined. */
  326.       Result->x = 1.0; Result->y = 0.0; Result->z = 0.0;
  327.       return;
  328.    }
  329.    else {
  330.       Temp = sqrt(Temp);
  331.       VInverseScale(V, V, Temp);
  332.    }
  333.    VCross(*Result, U, V);
  334. }
  335.  
  336.    /* Calculate the normal to a subpatch (triangle) return the vector
  337.    <1.0 0.0 0.0> if the triangle is degenerate. */
  338.    static int subpatch_normal(v1, v2, v3, Result, d)
  339.       VECTOR *v1, *v2, *v3, *Result;
  340. DBL *d;
  341. {
  342.    VECTOR V1, V2;
  343.    DBL Length;
  344.  
  345.    VSub(V1, *v1, *v2);
  346.    VSub(V2, *v3, *v2);
  347.    VCross(*Result, V1, V2);
  348.    VLength(Length, *Result);
  349.    if (Length < EPSILON) {
  350.       Result->x = 1.0;
  351.       Result->y = 0.0;
  352.       Result->z = 0.0;
  353.       *d = -1.0 * v1->x;
  354.       return 0;
  355.    }
  356.    else {
  357.       VInverseScale(*Result, *Result, Length);
  358.       VDot(*d, *Result, *v1);
  359.       *d = 0.0 - *d;
  360.       return 1;
  361.    }
  362. }
  363.  
  364.    /* See if Ray intersects the triangular subpatch defined by the three
  365.    points v1, v2, v3 and the normal to the triangle n. If so, Depth will
  366.    be set to the distance along Ray at which the intersection occurs. */
  367.    static int intersect_subpatch(Patch_Type, Ray, v1, v2, v3, n, d, n1, n2, n3,
  368.       Depth, IP, IP_Norm)
  369.       int Patch_Type;
  370. RAY *Ray;
  371. VECTOR *v1, *v2, *v3, *n, *n1, *n2, *n3, *IP, *IP_Norm;
  372. DBL d, *Depth;
  373. {
  374.    VECTOR TempV1, TempV2, Perp;
  375.    DBL s, t, Proj, mu;
  376.    DBL x1, y1, x2, y2, x3, y3;
  377.    DBL x, y, z;
  378.    int Sign_Holder, Next_Sign, Crossings;
  379.  
  380.    /* Calculate the point of intersection and the depth. */
  381.    VDot(s, Ray->Direction, *n);
  382.    if (s == 0.0)
  383.       return 0;
  384.    VDot(t, Ray->Initial, *n);
  385.    *Depth = 0.0 - (d + t) / s;
  386.    if (*Depth < Small_Tolerance)
  387.       return 0;
  388.    VScale(*IP, Ray->Direction, *Depth);
  389.    VAdd(*IP, *IP, Ray->Initial);
  390.  
  391.    /* Map the intersection point and the triangle onto a plane. */
  392.    x = fabs(n->x); y = fabs(n->y); z = fabs(n->z);
  393.    if (x>y)
  394.       if (x>z) {
  395.          x1 = v1->y - IP->y; y1 = v1->z - IP->z;
  396.          x2 = v2->y - IP->y; y2 = v2->z - IP->z;
  397.          x3 = v3->y - IP->y; y3 = v3->z - IP->z;
  398.       }
  399.       else {
  400.          x1 = v1->x - IP->x; y1 = v1->y - IP->y;
  401.          x2 = v2->x - IP->x; y2 = v2->y - IP->y;
  402.          x3 = v3->x - IP->x; y3 = v3->y - IP->y;
  403.       }
  404.    else if (y>z) {
  405.       x1 = v1->x - IP->x; y1 = v1->z - IP->z;
  406.       x2 = v2->x - IP->x; y2 = v2->z - IP->z;
  407.       x3 = v3->x - IP->x; y3 = v3->z - IP->z;
  408.    }
  409.    else {
  410.       x1 = v1->x - IP->x; y1 = v1->y - IP->y;
  411.       x2 = v2->x - IP->x; y2 = v2->y - IP->y;
  412.       x3 = v3->x - IP->x; y3 = v3->y - IP->y;
  413.    }
  414.  
  415.       /* Determine crossing count */
  416.       Crossings = 0;
  417.    if (y1 < 0) Sign_Holder = -1;
  418.    else Sign_Holder = 1;
  419.  
  420.       /* Start of winding tests, test the segment from v1 to v2 */
  421.       if (y2 < 0) Next_Sign = -1;
  422.       else Next_Sign = 1;
  423.    if (Sign_Holder != Next_Sign) {
  424.       if (x1 > 0.0) {
  425.          if (x2 > 0.0)
  426.             Crossings++;
  427.          else if ((x1 - y1 * (x2 - x1) / (y2 - y1)) > 0.0)
  428.             Crossings++;
  429.       }
  430.       else if (x2 > 0.0) {
  431.          if ((x1 - y1 * (x2 - x1) / (y2 - y1)) > 0.0)
  432.             Crossings++;
  433.       }
  434.       Sign_Holder = Next_Sign;
  435.    }
  436.  
  437.    /* Test the segment from v2 to v3 */
  438.    if (y3 < 0) Next_Sign = -1;
  439.    else Next_Sign = 1;
  440.    if (Sign_Holder != Next_Sign) {
  441.       if (x2 > 0.0) {
  442.          if (x3 > 0.0)
  443.             Crossings++;
  444.          else if ((x2 - y2 * (x3 - x2) / (y3 - y2)) > 0.0)
  445.             Crossings++;
  446.       }
  447.       else if (x3 > 0.0) {
  448.          if ((x2 - y2 * (x3 - x2) / (y3 - y2)) > 0.0)
  449.             Crossings++;
  450.       }
  451.       Sign_Holder = Next_Sign;
  452.    }
  453.  
  454.    /* Test the segment from v3 to v1 */
  455.    if (y1 < 0) Next_Sign = -1;
  456.    else Next_Sign = 1;
  457.    if (Sign_Holder != Next_Sign) {
  458.       if (x3 > 0.0) {
  459.          if (x1 > 0.0)
  460.             Crossings++;
  461.          else if ((x3 - y3 * (x1 - x3) / (y1 - y3)) > 0.0)
  462.             Crossings++;
  463.       }
  464.       else if (x1 > 0.0) {
  465.          if ((x3 - y3 * (x1 - x3) / (y1 - y3)) > 0.0)
  466.             Crossings++;
  467.       }
  468.       Sign_Holder = Next_Sign;
  469.    }
  470.    if (Crossings != 1)
  471.       return 0;
  472.  
  473.    if (Patch_Type == 0 || Patch_Type == 1 || Patch_Type == 2 ||
  474.       Patch_Type == 3) {
  475.       *IP_Norm = *n;
  476.       return 1;
  477.    }
  478.    else if (Patch_Type == 4) {
  479.       VSub(TempV1, *v2, *v3);
  480.       VNormalize(TempV1, TempV1);
  481.       VSub(TempV2, *v1, *v3);
  482.       VDot(Proj, TempV2, TempV1);
  483.       VScale(TempV1, TempV1, Proj);
  484.       VSub(Perp, TempV1, TempV2);
  485.       VNormalize(Perp, Perp);
  486.       VDot(mu, TempV2, Perp);
  487.       mu = -1.0 / mu;
  488.       VScale(Perp, Perp, mu);
  489.       VSub(TempV1, *IP, *v1);
  490.       VDot(s, TempV1, Perp);
  491.       if (s < EPSILON) {
  492.          *IP_Norm = *n1;
  493.          return 1;
  494.       }
  495.       VSub(TempV1, *v3, *v2);
  496.       x = fabs(TempV1.x); y = fabs(TempV1.y); z = fabs(TempV1.z);
  497.       if (x>y)
  498.          if (x>z)
  499.             t = (TempV1.x / s + v1->x - v2->x) / TempV1.x;
  500.          else
  501.             t = (TempV1.z / s + v1->z - v2->z) / TempV1.z;
  502.       else if (y > z)
  503.          t = (TempV1.y / s + v1->y - v2->y) / TempV1.y;
  504.       else
  505.          t = (TempV1.z / s + v1->z - v2->z) / TempV1.z;
  506.       VSub(TempV1, *n2, *n1);
  507.       VScale(TempV1, TempV1, s);
  508.       VAdd(TempV1, TempV1, *n1);
  509.       VSub(TempV2, *n3, *n1);
  510.       VScale(TempV2, TempV2, s);
  511.       VAdd(TempV2, TempV2, *n1);
  512.       VSub(*IP_Norm, TempV2, TempV1);
  513.       VScale(*IP_Norm, *IP_Norm, t);
  514.       VAdd(*IP_Norm, *IP_Norm, TempV1);
  515.       VNormalize(*IP_Norm, *IP_Norm);
  516.       return 1;
  517.    }
  518.    else
  519.       return 0;
  520. }
  521.  
  522. /* Find a sphere that contains all of the points in the list "vectors" */
  523. static void find_average(vector_count, vectors, center, radius)
  524. int vector_count;
  525. VECTOR *vectors, *center;
  526. DBL *radius;
  527. {
  528.    DBL r0, r1, xc=0, yc=0, zc=0;
  529.    DBL x0, y0, z0;
  530.    int i;
  531.    for (i=0;i<vector_count;i++) {
  532.       xc += vectors[i].x;
  533.       yc += vectors[i].y;
  534.       zc += vectors[i].z;
  535.    }
  536.    xc /= (DBL)vector_count;
  537.    yc /= (DBL)vector_count;
  538.    zc /= (DBL)vector_count;
  539.    r0 = 0.0;
  540.    for (i=0;i<vector_count;i++) {
  541.       x0 = vectors[i].x - xc;
  542.       y0 = vectors[i].y - yc;
  543.       z0 = vectors[i].z - zc;
  544.       r1 = x0 * x0 + y0 * y0 + z0 * z0;
  545.       if (r1 > r0) r0 = r1;
  546.    }
  547.    center->x = xc; center->y = yc; center->z = zc;
  548.    *radius = r0;
  549. }
  550.  
  551. #define SUBDIVISION_EPSILON 0.001
  552. #define MAX_RECURSION_DEPTH 20
  553.  
  554. static int spherical_bounds_check(Ray, center, radius)
  555. RAY *Ray;
  556. VECTOR *center;
  557. DBL radius;
  558. {
  559.    DBL x, y, z, dist1, dist2;
  560.    x = center->x - Ray->Initial.x;
  561.    y = center->y - Ray->Initial.y;
  562.    z = center->z - Ray->Initial.z;
  563.    dist1 = x * x + y * y + z * z;
  564.    if (dist1 < radius)
  565.       /* ray starts inside sphere - assume it intersects. */
  566.       return 1;
  567.    else {
  568.       dist2 = x*Ray->Direction.x + y*Ray->Direction.y + z*Ray->Direction.z;
  569.       dist2 = dist2 * dist2;
  570.       if (dist2 > 0 && (dist1 - dist2 < radius))
  571.          return 1;
  572.    }
  573.    return 0;
  574. }
  575.  
  576. /* Find a sphere that bounds all of the control points of a Bezier patch.
  577.    The values returned are: the center of the bounding sphere, and the
  578.    square of the radius of the bounding sphere. */
  579. static void bezier_bounding_sphere(Patch, center, radius)
  580. VECTOR (*Patch)[4][4], *center;
  581. DBL *radius;
  582. {
  583.    DBL r0, r1, xc=0, yc=0, zc=0;
  584.    DBL x0, y0, z0;
  585.    int i, j;
  586.    for (i=0;i<4;i++) {
  587.       for (j=0;j<4;j++) {
  588.          xc += (*Patch)[i][j].x;
  589.          yc += (*Patch)[i][j].y;
  590.          zc += (*Patch)[i][j].z;
  591.       }
  592.    }
  593.    xc /= 16.0;
  594.    yc /= 16.0;
  595.    zc /= 16.0;
  596.    r0 = 0.0;
  597.    for (i=0;i<4;i++) {
  598.       for (j=0;j<4;j++) {
  599.          x0 = (*Patch)[i][j].x - xc;
  600.          y0 = (*Patch)[i][j].y - yc;
  601.          z0 = (*Patch)[i][j].z - zc;
  602.          r1 = x0 * x0 + y0 * y0 + z0 * z0;
  603.          if (r1 > r0) r0 = r1;
  604.       }
  605.    }
  606.    center->x = xc; center->y = yc; center->z = zc;
  607.    *radius = r0;
  608. }
  609.  
  610. /* Precompute grid points and normals for a bezier patch */
  611. void
  612. Precompute_Patch_Values(Shape)
  613. BICUBIC_PATCH *Shape;
  614. {
  615.    int i, j;
  616.    DBL d, u, v, delta_u, delta_v;
  617.    VECTOR v0, v1, v2, v3, n;
  618.    VECTOR Control_Points[16];
  619.    VECTOR (*Patch_Ptr)[4][4] = (VECTOR (*)[4][4]) Shape->Control_Points;
  620.  
  621.    /* Calculate the bounding sphere for the entire patch. */
  622.    for (i=0;i<4;i++) for (j=0;j<4;j++)
  623.       Control_Points[4*i+j] = Shape->Control_Points[i][j];
  624.    find_average(16, &Control_Points[0], &Shape->Bounding_Sphere_Center,
  625.       &Shape->Bounding_Sphere_Radius);
  626.    /* Shape->Node_Tree = NULL; */
  627.    if (Shape->Patch_Type == 0 || Shape->Patch_Type == 2)
  628.       return;
  629.    else if (Shape->Patch_Type == 3) {
  630.       if (Shape->Node_Tree != NULL)
  631.          bezier_tree_deleter(Shape->Node_Tree);
  632.       Shape->Node_Tree = bezier_tree_builder(Shape, Patch_Ptr, 0);
  633.       return;
  634.    }
  635.    delta_u = 1.0 / (DBL)Shape->U_Steps;
  636.    delta_v = 1.0 / (DBL)Shape->V_Steps;
  637.    if (Shape->Interpolated_Grid == NULL) {
  638.       Shape->Interpolated_Grid = (VECTOR **)
  639.          malloc((Shape->U_Steps+1) * sizeof(VECTOR *));
  640.       if (Shape->Interpolated_Grid == NULL)
  641.          Error("Failed to allocate Interpolated_Grid");
  642.       for (i=0;i<=Shape->U_Steps;i++) {
  643.          Shape->Interpolated_Grid[i] = (VECTOR *)
  644.             malloc((Shape->V_Steps+1) * sizeof(VECTOR));
  645.          if (Shape->Interpolated_Grid == NULL)
  646.             Error("Failed to allocate component of Interpolated_Grid");
  647.       }
  648.       Shape->Interpolated_Normals = (VECTOR **)
  649.          malloc((Shape->U_Steps+1) * sizeof(VECTOR *));
  650.       if (Shape->Interpolated_Normals == NULL)
  651.          Error("Failed to allocate Interpolated_Normals");
  652.       for (i=0;i<=Shape->U_Steps;i++) {
  653.          Shape->Interpolated_Normals[i] = (VECTOR *)
  654.             malloc(2*(Shape->V_Steps+1)*sizeof(VECTOR));
  655.          if (Shape->Interpolated_Normals == NULL)
  656.             Error("Failed to allocate component of Interpolated_Normals");
  657.       }
  658.  
  659.       if (Shape->Patch_Type == 4) {
  660.          Shape->Smooth_Normals = (VECTOR **)
  661.             malloc((Shape->U_Steps+1) * sizeof(VECTOR *));
  662.          if (Shape->Smooth_Normals == NULL)
  663.             Error("Failed to allocate Smooth_Normals");
  664.          for (i=0;i<=Shape->U_Steps;i++) {
  665.             Shape->Smooth_Normals[i] = (VECTOR *)
  666.                malloc((Shape->V_Steps+1)*sizeof(VECTOR));
  667.             if (Shape->Smooth_Normals == NULL)
  668.                Error("Failed to allocate component of Smooth_Normals");
  669.          }
  670.       }
  671.  
  672.       Shape->Interpolated_D = (DBL **)malloc((Shape->U_Steps+1) * sizeof(DBL *));
  673.       if (Shape->Interpolated_D == NULL)
  674.          Error("Failed to allocate Interpolated_D");
  675.       for (i=0;i<=Shape->U_Steps;i++) {
  676.          Shape->Interpolated_D[i] = (DBL *)
  677.             malloc(2*(Shape->V_Steps+1)*sizeof(DBL));
  678.          if (Shape->Interpolated_D == NULL)
  679.             Error("Failed to allocate component of Interpolated_D");
  680.       }
  681.    }
  682.  
  683.    /* Calculate the grid values for the given subdivision values. */
  684.    for (i=0;i<=Shape->U_Steps;i++) {
  685.       u = (DBL)i / (DBL)Shape->U_Steps;
  686.       for (j=0;j<Shape->V_Steps;j++) {
  687.          v = (DBL) j / (DBL)Shape->V_Steps;
  688.          bezier_value(&Shape->Interpolated_Grid[i][j], u, v, Patch_Ptr);
  689.       }
  690.    }
  691.  
  692.    for (i=0;i<Shape->U_Steps;i++) {
  693.       u = (DBL)i / (DBL)Shape->U_Steps;
  694.       for (j=0;j<Shape->V_Steps;j++) {
  695.          v = (DBL)j / (DBL)Shape->V_Steps;
  696.  
  697.          /* Calculate surface values for the current patch. */
  698.          bezier_value(&v0, u, v, Patch_Ptr);
  699.          bezier_value(&v1, u+delta_u, v, Patch_Ptr);
  700.          bezier_value(&v2, u, v+delta_v, Patch_Ptr);
  701.          bezier_value(&v3, u+delta_u, v+delta_v, Patch_Ptr);
  702.  
  703.          Shape->Interpolated_Grid[i][j] = v0;
  704.          Shape->Interpolated_Grid[i+1][j] = v1;
  705.          Shape->Interpolated_Grid[i][j+1] = v2;
  706.          Shape->Interpolated_Grid[i+1][j+1] = v3;
  707.          if (Shape->Patch_Type == 1 || Shape->Patch_Type == 4) {
  708.             /* Calculate the normals */
  709.             if (subpatch_normal(&v0, &v2, &v1, &n, &d)) {
  710.                Shape->Interpolated_Normals[i][2*j] = n;
  711.                Shape->Interpolated_D[i][2*j] = d;
  712.             }
  713.             else {
  714.                Shape->Interpolated_Normals[i][2*j].x = 0.0;
  715.                Shape->Interpolated_Normals[i][2*j].y = 0.0;
  716.                Shape->Interpolated_Normals[i][2*j].z = 0.0;
  717.                Shape->Interpolated_D[i][2*j] = 0.0;
  718.             }
  719.  
  720.                if (subpatch_normal(&v1, &v2, &v3, &n, &d)) {
  721.                   Shape->Interpolated_Normals[i][2*j+1] = n;
  722.                   Shape->Interpolated_D[i][2*j+1] = d;
  723.                }
  724.                else {
  725.                   Shape->Interpolated_Normals[i][2*j+1].x = 0.0;
  726.                   Shape->Interpolated_Normals[i][2*j+1].y = 0.0;
  727.                   Shape->Interpolated_Normals[i][2*j+1].z = 0.0;
  728.                   Shape->Interpolated_D[i][2*j+1] = 0.0;
  729.                }
  730.          }
  731.       }
  732.    }
  733.  
  734.       if (Shape->Patch_Type == 4) {
  735.          /* Calculate normals at the corners of the subpatches */
  736.          for (i=0;i<=Shape->U_Steps;i++) {
  737.             u = (DBL)i / (DBL)Shape->U_Steps;
  738.             for (j=0;j<=Shape->V_Steps;j++) {
  739.                v = (DBL)j / (DBL)Shape->V_Steps;
  740.                bezier_partial(&Shape->Smooth_Normals[i][j], u, v, Shape);
  741.             }
  742.          }
  743.       }
  744. }
  745.  
  746. /* Determine the distance from a point to a plane. */
  747. static DBL point_plane_distance(p, n, d)
  748. VECTOR *p, *n;
  749. DBL *d;
  750. {
  751.    DBL temp1, temp2;
  752.  
  753.    VDot(temp1, *p, *n);
  754.    temp1 += *d;
  755.    VLength(temp2, *n);
  756.    if (fabs(temp2) < EPSILON) return 0;
  757.    temp1 /= temp2;
  758.    return temp1;
  759. }
  760.  
  761. static void bezier_subpatch_intersect(Ray, Shape, Patch, u0, u1, v0,
  762. recursion_depth, depth_count, Depths, U_Values, V_Values)
  763. RAY *Ray;
  764. BICUBIC_PATCH *Shape;
  765. VECTOR (*Patch)[4][4];
  766. DBL u0, u1, v0;
  767. int recursion_depth, *depth_count;
  768. DBL *Depths, *U_Values, *V_Values;
  769. {
  770.    int tcnt = Shape->Intersection_Count;
  771.    VECTOR vv0, vv1, vv2, vv3, n, ip;
  772.    DBL d, Depth;
  773.  
  774.    if (tcnt + *depth_count >= MAX_BICUBIC_INTERSECTIONS) return;
  775.  
  776.    vv0 = (*Patch)[0][0]; vv1 = (*Patch)[0][3];
  777.    vv2 = (*Patch)[3][3]; vv3 = (*Patch)[3][0];
  778.  
  779.    /* Triangulate this subpatch, then check for intersections in
  780.       the triangles. */
  781.    if (subpatch_normal(&vv0, &vv1, &vv2, &n, &d)) {
  782.       if (intersect_subpatch(Shape->Patch_Type, Ray, &vv0, &vv1, &vv2, &n, d,
  783.          NULL, NULL, NULL, &Depth, &ip, &n)) {
  784.          Shape->Intersection_Point[tcnt + *depth_count] = ip;
  785.          Shape->Normal_Vector[tcnt + *depth_count] = n;
  786.          Depths[*depth_count] = Depth;
  787.          *depth_count += 1;
  788.       }
  789.    }
  790.  
  791.    if (*depth_count + tcnt >= MAX_BICUBIC_INTERSECTIONS) return;
  792.  
  793.    if (subpatch_normal(&vv0, &vv2, &vv3, &n, &d)) {
  794.       if (intersect_subpatch(Shape->Patch_Type, Ray, &vv0, &vv2, &vv3, &n, d,
  795.          NULL, NULL, NULL, &Depth, &ip, &n)) {
  796.          Shape->Intersection_Point[tcnt + *depth_count] = ip;
  797.          Shape->Normal_Vector[tcnt + *depth_count] = n;
  798.          Depths[*depth_count] = Depth;
  799.          *depth_count += 1;
  800.       }
  801.    }
  802. }
  803.  
  804. static void bezier_split_left_right(Patch, Left_Patch, Right_Patch)
  805. VECTOR (*Patch)[4][4], (*Left_Patch)[4][4], (*Right_Patch)[4][4];
  806. {
  807.    VECTOR Temp1[4], Temp2[4], Half;
  808.    int i, j;
  809.    for (i=0;i<4;i++) {
  810.       Temp1[0] = (*Patch)[i][0];
  811.       VHalf(Temp1[1], (*Patch)[i][0], (*Patch)[i][1]);
  812.       VHalf(Half, (*Patch)[i][1], (*Patch)[i][2]);
  813.       VHalf(Temp1[2], Temp1[1], Half);
  814.       VHalf(Temp2[2], (*Patch)[i][2], (*Patch)[i][3]);
  815.       VHalf(Temp2[1], Half, Temp2[2]);
  816.       VHalf(Temp1[3], Temp1[2], Temp2[1]);
  817.       Temp2[0] = Temp1[3];
  818.       Temp2[3] = (*Patch)[i][3];
  819.       for (j=0;j<4;j++) {
  820.          (*Left_Patch)[i][j]  = Temp1[j];
  821.          (*Right_Patch)[i][j] = Temp2[j];
  822.       }
  823.    }
  824. }
  825.  
  826. static void bezier_split_up_down(Patch, Top_Patch, Bottom_Patch)
  827. VECTOR (*Patch)[4][4], (*Top_Patch)[4][4], (*Bottom_Patch)[4][4];
  828. {
  829.    VECTOR Temp1[4], Temp2[4], Half;
  830.    int i, j;
  831.  
  832.    for (i=0;i<4;i++) {
  833.       /* Split Left */
  834.       Temp1[0] = (*Patch)[0][i];
  835.       VHalf(Temp1[1], (*Patch)[0][i], (*Patch)[1][i]);
  836.       VHalf(Half, (*Patch)[1][i], (*Patch)[2][i]);
  837.       VHalf(Temp1[2], Temp1[1], Half);
  838.       VHalf(Temp2[2], (*Patch)[2][i], (*Patch)[3][i]);
  839.       VHalf(Temp2[1], Half, Temp2[2]);
  840.       VHalf(Temp1[3], Temp1[2], Temp2[1]);
  841.       Temp2[0] = Temp1[3];
  842.       Temp2[3] = (*Patch)[3][i];
  843.       for (j=0;j<4;j++) {
  844.          (*Bottom_Patch)[j][i] = Temp1[j];
  845.          (*Top_Patch)[j][i]       = Temp2[j];
  846.       }
  847.    }
  848. }
  849.  
  850. /* See how close to a plane a subpatch is, the patch must have at least
  851.    three distinct vertices. A negative result from this function indicates
  852.    that a degenerate value of some sort was encountered. */
  853. static DBL determine_subpatch_flatness(Patch)
  854. VECTOR (*Patch)[4][4];
  855. {
  856.    VECTOR vertices[4], n, TempV;
  857.    DBL d, dist, temp1;
  858.    int i, j;
  859.  
  860.    vertices[0] = (*Patch)[0][0];
  861.    vertices[1] = (*Patch)[0][3];
  862.    VSub(TempV, vertices[0], vertices[1]);
  863.    VLength(temp1, TempV);
  864.    if (fabs(temp1) < EPSILON) {
  865.       /* Degenerate in the V direction for U = 0. This is ok if the other
  866.      two corners are distinct from the lower left corner - I'm sure there
  867.      are cases where the corners coincide and the middle has good values,
  868.      but that is somewhat pathalogical and won't be considered. */
  869.       vertices[1] = (*Patch)[3][3];
  870.       VSub(TempV, vertices[0], vertices[1]);
  871.       VLength(temp1, TempV);
  872.       if (fabs(temp1) < EPSILON) return -1.0;
  873.       vertices[2] = (*Patch)[3][0];
  874.       VSub(TempV, vertices[0], vertices[1]);
  875.       VLength(temp1, TempV);
  876.       if (fabs(temp1) < EPSILON) return -1.0;
  877.       VSub(TempV, vertices[1], vertices[2]);
  878.       VLength(temp1, TempV);
  879.       if (fabs(temp1) < EPSILON) return -1.0;
  880.    }
  881.    else {
  882.       vertices[2] = (*Patch)[3][0];
  883.       VSub(TempV, vertices[0], vertices[1]);
  884.       VLength(temp1, TempV);
  885.       if (fabs(temp1) < EPSILON) {
  886.          vertices[2] = (*Patch)[3][3];
  887.          VSub(TempV, vertices[0], vertices[2]);
  888.          VLength(temp1, TempV);
  889.          if (fabs(temp1) < EPSILON)
  890.             return -1.0;
  891.          VSub(TempV, vertices[1], vertices[2]);
  892.          VLength(temp1, TempV);
  893.          if (fabs(temp1) < EPSILON)
  894.             return -1.0;
  895.       }
  896.       else {
  897.          VSub(TempV, vertices[1], vertices[2]);
  898.          VLength(temp1, TempV);
  899.          if (fabs(temp1) < EPSILON)
  900.             return -1.0;
  901.       }
  902.    }
  903.    /* Now that a good set of candidate points has been found, find the
  904.       plane equations for the patch */
  905.    if (subpatch_normal(&vertices[0], &vertices[1], &vertices[2], &n, &d)) {
  906.       /* Step through all vertices and see what the maximum distance from the
  907.          plane happens to be. */
  908.       dist = 0.0;
  909.       for (i=0;i<4;i++) {
  910.          for (j=0;j<4;j++) {
  911.             temp1 = fabs(point_plane_distance(&((*Patch)[i][j]), &n, &d));
  912.             if (temp1 > dist)
  913.                dist = temp1;
  914.          }
  915.       }
  916.       return dist;
  917.    }
  918.    else {
  919.       /* printf("Subpatch normal failed in determine_subpatch_flatness\n"); */
  920.          return -1.0;
  921.    }
  922. }
  923.  
  924. static int flat_enough(Object, Patch)
  925. BICUBIC_PATCH *Object;
  926. VECTOR (*Patch)[4][4];
  927. {
  928.    DBL Dist;
  929.  
  930.    Dist = determine_subpatch_flatness(Patch);
  931.    if (Dist < 0.0)
  932.       return 0;
  933.    else if (Dist < Object->Flatness_Value)
  934.       return 1;
  935.    else
  936.       return 0;
  937. }
  938.  
  939. static void bezier_subdivider(Ray, Object, Patch, u0, u1, v0, v1,
  940. recursion_depth, depth_count, Depths, U_Values, V_Values)
  941. RAY *Ray;
  942. BICUBIC_PATCH *Object;
  943. VECTOR (*Patch)[4][4];
  944. DBL u0, u1, v0, v1;
  945. int recursion_depth, *depth_count;
  946. DBL *Depths, *U_Values, *V_Values;
  947. {
  948.    VECTOR Lower_Left[4][4], Lower_Right[4][4];
  949.    VECTOR Upper_Left[4][4], Upper_Right[4][4];
  950.    VECTOR center;
  951.    DBL ut, vt, radius;
  952.    int tcnt = Object->Intersection_Count;
  953.  
  954.    /* Don't waste time if there are already too many intersections */
  955.    if (tcnt >= MAX_BICUBIC_INTERSECTIONS) return;
  956.  
  957.    /* Make sure the ray passes through a sphere bounding the control points of
  958.       the patch */
  959.    bezier_bounding_sphere(Patch, ¢er, &radius);
  960.    if (!spherical_bounds_check(Ray, ¢er, radius))
  961.       return;
  962.  
  963.    /* If the patch is close to being flat, then just perform a ray-plane
  964.       intersection test. */
  965.    if (flat_enough(Object, Patch))
  966.       bezier_subpatch_intersect(Ray, Object, Patch, u0, u1, v0,
  967.          recursion_depth+1, depth_count, Depths,
  968.          U_Values, V_Values);
  969.  
  970.    if (recursion_depth >= Object->U_Steps)
  971.       if (recursion_depth >= Object->V_Steps)
  972.          bezier_subpatch_intersect(Ray, Object, Patch, u0, u1, v0,
  973.             recursion_depth+1, depth_count, Depths,
  974.             U_Values, V_Values);
  975.       else {
  976.          bezier_split_up_down(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Upper_Left);
  977.          vt = (v1 - v0) / 2.0;
  978.          bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Lower_Left, u0, u1, v0, vt,
  979.             recursion_depth+1, depth_count, Depths,
  980.                U_Values, V_Values);
  981.          bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Upper_Left, u0, u1, vt, v1,
  982.             recursion_depth+1, depth_count, Depths,
  983.             U_Values, V_Values);
  984.       }
  985.    else if (recursion_depth >= Object->V_Steps) {
  986.       bezier_split_left_right(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Right);
  987.       ut = (u1 - u0) / 2.0;
  988.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Lower_Left, u0, ut, v0, v1,
  989.          recursion_depth+1, depth_count, Depths,
  990.          U_Values, V_Values);
  991.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Lower_Right, ut, u1, v0, v1,
  992.          recursion_depth+1, depth_count, Depths,
  993.          U_Values, V_Values);
  994.    }
  995.    else {
  996.       ut = (u1 - u0) / 2.0;
  997.       vt = (v1 - v0) / 2.0;
  998.       bezier_split_left_right(Patch, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Right);
  999.       bezier_split_up_down((VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Lower_Left, (VECTOR (*)[4][4])Upper_Left);
  1000.       bezier_split_up_down((VECTOR (*)[4][4])Lower_Right, (VECTOR (*)[4][4])Lower_Right, (VECTOR (*)[4][4])Upper_Right);
  1001.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Lower_Left, u0, ut, v0, vt,
  1002.          recursion_depth+1, depth_count, Depths,
  1003.             U_Values, V_Values);
  1004.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Upper_Left, u0, ut, vt, v1,
  1005.          recursion_depth+1, depth_count, Depths,
  1006.          U_Values, V_Values);
  1007.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Lower_Right, ut, u1, v0, vt,
  1008.          recursion_depth+1, depth_count, Depths,
  1009.          U_Values, V_Values);
  1010.       bezier_subdivider(Ray, Object, (VECTOR (*)[4][4])Upper_Right, ut, u1, vt, v1,
  1011.          recursion_depth+1, depth_count, Depths,
  1012.          U_Values, V_Values);
  1013.    }
  1014. }
  1015.  
  1016. static void bezier_tree_deleter(Node)
  1017. BEZIER_NODE *Node;
  1018. {
  1019.    BEZIER_CHILDREN *Children;
  1020.    int i;
  1021.  
  1022.    /* If this is an interior node then continue the descent */
  1023.    if (Node->Node_Type == BEZIER_INTERIOR_NODE) {
  1024.       Children = (BEZIER_CHILDREN *)Node->Data_Ptr;
  1025.       for (i=0;i<Node->Count;i++)
  1026.          bezier_tree_deleter(Children->Children[i]);
  1027.       free((void *)Children);
  1028.    }
  1029.    else if (Node->Node_Type == BEZIER_LEAF_NODE) {
  1030.       /* Free the memory used for the vertices. */
  1031.       free((void *)Node->Data_Ptr);
  1032.    }
  1033.    /* Free the memory used for the node. */
  1034.    free((void *)Node);
  1035. }
  1036.  
  1037. static void bezier_tree_walker(Ray, Shape, Node, depth, depth_count, Depths)
  1038. RAY *Ray;
  1039. BICUBIC_PATCH *Shape;
  1040. BEZIER_NODE *Node;
  1041. int depth, *depth_count;
  1042. DBL *Depths;
  1043. {
  1044.    BEZIER_CHILDREN *Children;
  1045.    BEZIER_VERTICES *Vertices;
  1046.    VECTOR n, ip, vv0, vv1, vv2, vv3;
  1047.    DBL d, Depth;
  1048.    int i, tcnt = Shape->Intersection_Count;
  1049.  
  1050.    /* Don't waste time if there are already too many intersections */
  1051.    if (tcnt >= MAX_BICUBIC_INTERSECTIONS) return;
  1052.  
  1053.    /* Make sure the ray passes through a sphere bounding the control points of
  1054.       the patch */
  1055.    if (!spherical_bounds_check(Ray, &(Node->Center), Node->Radius_Squared))
  1056.       return;
  1057.  
  1058.    /* If this is an interior node then continue the descent, else
  1059.       do a check against the vertices. */
  1060.    if (Node->Node_Type == BEZIER_INTERIOR_NODE) {
  1061.       Children = (BEZIER_CHILDREN *)Node->Data_Ptr;
  1062.       for (i=0;i<Node->Count;i++)
  1063.          bezier_tree_walker(Ray, Shape, Children->Children[i],
  1064.             depth+1, depth_count, Depths);
  1065.    }
  1066.    else if (Node->Node_Type == BEZIER_LEAF_NODE) {
  1067.       Vertices = (BEZIER_VERTICES *)Node->Data_Ptr;
  1068.       vv0 = Vertices->Vertices[0]; vv1 = Vertices->Vertices[1];
  1069.       vv2 = Vertices->Vertices[2]; vv3 = Vertices->Vertices[3];
  1070.  
  1071.       /* Triangulate this subpatch, then check for intersections in
  1072.      the triangles. */
  1073.       if (subpatch_normal(&vv0, &vv1, &vv2, &n, &d)) {
  1074.          if (intersect_subpatch(Shape->Patch_Type, Ray, &vv0, &vv1, &vv2, &n, d,
  1075.             NULL, NULL, NULL, &Depth, &ip, &n)) {
  1076.             Shape->Intersection_Point[tcnt + *depth_count] = ip;
  1077.             Shape->Normal_Vector[tcnt + *depth_count] = n;
  1078.             Depths[*depth_count] = Depth;
  1079.             *depth_count += 1;
  1080.          }
  1081.       }
  1082.  
  1083.       if (*depth_count + tcnt >= MAX_BICUBIC_INTERSECTIONS) return;
  1084.  
  1085.       if (subpatch_normal(&vv0, &vv2, &vv3, &n, &d)) {
  1086.          if (intersect_subpatch(Shape->Patch_Type, Ray, &vv0, &vv2, &vv3, &n, d,
  1087.             NULL, NULL, NULL, &Depth, &ip, &n)) {
  1088.             Shape->Intersection_Point[tcnt + *depth_count] = ip;
  1089.             Shape->Normal_Vector[tcnt + *depth_count] = n;
  1090.             Depths[*depth_count] = Depth;
  1091.             *depth_count += 1;
  1092.          }
  1093.       }
  1094.    }
  1095.    else {
  1096.       printf("Bad Node type at depth %d\n", depth);
  1097.    }
  1098. }
  1099.  
  1100.    /* Intersection of a ray and a bezier patch. */
  1101.    /* Note: There is MUCH repeated work being done here. During the subdivision
  1102.    process, the values of surface points are not retained from one subpatch
  1103.    to the next, even though there are two points in common between one subpatch
  1104.    and the one adjacent to it.    An obvious optimization is to retain this
  1105.    surface information during the subdivision process.
  1106.  
  1107.    A second optimization is to make use of the fact that the surface never
  1108.    goes outside the convex hull generated by the control points.  By testing
  1109.    the ray against that hull first many unnecessary tests can be avoided.  The
  1110.    hull really should be generated at the time the patch is first defined -
  1111.    not at run time.
  1112. */
  1113.    static int intersect_bicubic_patch0(Ray, Shape, Depths)
  1114.       RAY *Ray;
  1115. BICUBIC_PATCH *Shape;
  1116. DBL *Depths;
  1117. {
  1118.    int cnt = 0;
  1119.    int tcnt = Shape->Intersection_Count;
  1120.    int i, j;
  1121.    DBL Depth, d, u, v, delta_u, delta_v;
  1122.    VECTOR v0, v1, v2, v3, n, ip;
  1123.    VECTOR (*Patch_Ptr)[4][4] = (VECTOR (*)[4][4]) Shape->Control_Points;
  1124.  
  1125.    if (!spherical_bounds_check(Ray, &(Shape->Bounding_Sphere_Center),
  1126.       Shape->Bounding_Sphere_Radius))
  1127.       return 0;
  1128.  
  1129.    delta_u = 1.0 / (DBL)Shape->U_Steps;
  1130.    delta_v = 1.0 / (DBL)Shape->V_Steps;
  1131.  
  1132.    /* Calculate the initial point */
  1133.    for (i=0;i<Shape->U_Steps;i++) {
  1134.       u = (DBL)i / (DBL)Shape->U_Steps;
  1135.       for (j=0;j<Shape->V_Steps;j++) {
  1136.          v = (DBL)j / (DBL)Shape->V_Steps;
  1137.  
  1138.          /* Calculate surface values for the current patch. */
  1139.          bezier_value(&v0, u, v, Patch_Ptr);
  1140.          bezier_value(&v1, u+delta_u, v, Patch_Ptr);
  1141.          bezier_value(&v2, u, v+delta_v, Patch_Ptr);
  1142.          bezier_value(&v3, u+delta_u, v+delta_v, Patch_Ptr);
  1143.  
  1144.          /* Triangulate this subpatch, then check for intersections in
  1145.         the triangles. */
  1146.          if (subpatch_normal(&v0, &v2, &v1, &n, &d)) {
  1147.             if (intersect_subpatch(Shape->Patch_Type, Ray, &v0, &v2, &v1, &n, d,
  1148.                NULL, NULL, NULL, &Depth, &ip, &n)) {
  1149.                Shape->Intersection_Point[tcnt+cnt] = ip;
  1150.                Shape->Normal_Vector[tcnt+cnt] = n;
  1151.                Depths[cnt] = Depth;
  1152.                if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1153.                   /* Too many intersections. Stop looking for more. */
  1154.                   return cnt;
  1155.             }
  1156.          }
  1157.          if (subpatch_normal(&v1, &v2, &v3, &n, &d)) {
  1158.             if (intersect_subpatch(Shape->Patch_Type, Ray, &v1, &v2, &v3, &n, d,
  1159.                NULL, NULL, NULL, &Depth, &ip, &n)) {
  1160.                Shape->Intersection_Point[tcnt+cnt] = ip;
  1161.                Shape->Normal_Vector[tcnt+cnt] = n;
  1162.                Depths[cnt] = Depth;
  1163.                if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1164.                   /* Too many intersections. Stop looking for more. */
  1165.                   return cnt;
  1166.             }
  1167.          }
  1168.       }
  1169.    }
  1170.    return cnt;
  1171. }
  1172.  
  1173. static int intersect_bicubic_patch1(Ray, Shape, Depths)
  1174. RAY *Ray;
  1175. BICUBIC_PATCH *Shape;
  1176. DBL *Depths;
  1177. {
  1178.    int cnt = 0;
  1179.    int tcnt = Shape->Intersection_Count;
  1180.    int i, j;
  1181.    DBL Depth, d, radius;
  1182.    VECTOR v[4], n, ip, center;
  1183.  
  1184.    if (!spherical_bounds_check(Ray, &(Shape->Bounding_Sphere_Center),
  1185.       Shape->Bounding_Sphere_Radius))
  1186.       return 0;
  1187.  
  1188.    /* Calculate the initial point */
  1189.    for (i=0;i<Shape->U_Steps;i++) {
  1190.       for (j=0;j<Shape->V_Steps;j++) {
  1191.  
  1192.          /* Grab precomputed surface values for the current patch. */
  1193.          v[0] = Shape->Interpolated_Grid[i][j];
  1194.          v[1] = Shape->Interpolated_Grid[i+1][j];
  1195.          v[2] = Shape->Interpolated_Grid[i][j+1];
  1196.          v[3] = Shape->Interpolated_Grid[i+1][j+1];
  1197.  
  1198.          /* Check the ray against the bounding sphere for this subpatch */
  1199.          find_average(4, &v[0], ¢er, &radius);
  1200.          if (!spherical_bounds_check(Ray, ¢er, radius)) continue;
  1201.  
  1202.          n = Shape->Interpolated_Normals[i][2*j];
  1203.          if (n.x == 0.0 && n.y == 0.0 && n.z == 0.0) {
  1204.             goto l0;
  1205.          }
  1206.          d = Shape->Interpolated_D[i][2*j];
  1207.  
  1208.          /* Check for intersections in this subpatch. */
  1209.          if (intersect_subpatch(Shape->Patch_Type, Ray, &v[0], &v[2], &v[1], &n, d,
  1210.             NULL, NULL, NULL, &Depth, &ip, &n)) {
  1211.             Shape->Intersection_Point[tcnt+cnt] = ip;
  1212.             Shape->Normal_Vector[tcnt+cnt] = n;
  1213.             Depths[cnt] = Depth;
  1214.             if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1215.                /* Too many intersections. Stop looking for more. */
  1216.                return cnt;
  1217.          }
  1218. l0:
  1219.          n = Shape->Interpolated_Normals[i][2*j+1];
  1220.          if (n.x == 0.0 && n.y == 0.0 && n.z == 0.0) {
  1221.             continue;
  1222.          }
  1223.          d = Shape->Interpolated_D[i][2*j+1];
  1224.          if (intersect_subpatch(Shape->Patch_Type, Ray, &v[1], &v[2], &v[3], &n, d,
  1225.             NULL, NULL, NULL, &Depth, &ip, &n)) {
  1226.             Shape->Intersection_Point[tcnt+cnt] = ip;
  1227.             Shape->Normal_Vector[tcnt+cnt] = n;
  1228.             Depths[cnt] = Depth;
  1229.             if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1230.                /* Too many intersections. Stop looking for more. */
  1231.                return cnt;
  1232.          }
  1233.       }
  1234.    }
  1235.    return cnt;
  1236. }
  1237.  
  1238. static int intersect_bicubic_patch2(Ray, Shape, Depths)
  1239. RAY *Ray;
  1240. BICUBIC_PATCH *Shape;
  1241. DBL *Depths;
  1242. {
  1243.    int cnt = 0;
  1244.    DBL U_Values[MAX_BICUBIC_INTERSECTIONS];
  1245.    DBL V_Values[MAX_BICUBIC_INTERSECTIONS];
  1246.    VECTOR (*Patch)[4][4] = (VECTOR (*)[4][4]) Shape->Control_Points;
  1247.  
  1248.    bezier_subdivider(Ray, Shape, Patch, 0.0, 1.0, 0.0, 1.0,
  1249.       0, &cnt, Depths, &U_Values[0], &V_Values[0]);
  1250.    return cnt;
  1251. }
  1252.  
  1253. static int intersect_bicubic_patch3(Ray, Shape, Depths)
  1254. RAY *Ray;
  1255. BICUBIC_PATCH *Shape;
  1256. DBL *Depths;
  1257. {
  1258.    int cnt = 0;
  1259.    bezier_tree_walker(Ray, Shape, Shape->Node_Tree, 0, &cnt, Depths);
  1260.    return cnt;
  1261. }
  1262.  
  1263. static int intersect_bicubic_patch4(Ray, Shape, Depths)
  1264. RAY *Ray;
  1265. BICUBIC_PATCH *Shape;
  1266. DBL *Depths;
  1267. {
  1268.    int cnt = 0;
  1269.    int tcnt = Shape->Intersection_Count;
  1270.    int i, j;
  1271.    DBL Depth, d, t;
  1272.    VECTOR v0, v1, v2, v3, n;
  1273.    VECTOR n0, n1, n2, n3, ip, ip_norm;
  1274.  
  1275.    if (!spherical_bounds_check(Ray, &(Shape->Bounding_Sphere_Center),
  1276.       Shape->Bounding_Sphere_Radius))
  1277.       return 0;
  1278.  
  1279.    /* Calculate the initial point */
  1280.    for (i=0;i<Shape->U_Steps;i++) {
  1281.       for (j=0;j<Shape->V_Steps;j++) {
  1282.  
  1283.          /* Grab precomputed surface values for the current patch. */
  1284.          v0 = Shape->Interpolated_Grid[i][j];
  1285.          v1 = Shape->Interpolated_Grid[i+1][j];
  1286.          v2 = Shape->Interpolated_Grid[i][j+1];
  1287.          v3 = Shape->Interpolated_Grid[i+1][j+1];
  1288.  
  1289.          n0 = Shape->Smooth_Normals[i][j];
  1290.          n1 = Shape->Smooth_Normals[i+1][j];
  1291.          n2 = Shape->Smooth_Normals[i][j+1];
  1292.          n3 = Shape->Smooth_Normals[i+1][j+1];
  1293.  
  1294.          n = Shape->Interpolated_Normals[i][2*j];
  1295.          if (n.x == 0.0 && n.y == 0.0 && n.z == 0.0) goto l0;
  1296.          d = Shape->Interpolated_D[i][2*j];
  1297.  
  1298.          /* Make sure the smooth normals point in the same direction as the normal */
  1299.          VDot(t, n0, n); if (t < 0) VScale(n0, n0, -1.0);
  1300.          VDot(t, n1, n); if (t < 0) VScale(n1, n1, -1.0);
  1301.          VDot(t, n2, n); if (t < 0) VScale(n2, n2, -1.0);
  1302.  
  1303.          /* Check for intersections in this subpatch. */
  1304.          if (intersect_subpatch(Shape->Patch_Type, Ray, &v0, &v2, &v1, &n,
  1305.             d, &n0, &n2, &n1, &Depth, &ip, &ip_norm)) {
  1306.             Shape->Intersection_Point[tcnt + cnt] = ip;
  1307.             Shape->Normal_Vector[tcnt + cnt] = ip_norm;
  1308.             Depths[cnt] = Depth;
  1309.             if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1310.                /* Too many intersections. Stop looking for more. */
  1311.                return cnt;
  1312.          }
  1313. l0:
  1314.          n = Shape->Interpolated_Normals[i][2*j+1];
  1315.          if (n.x == 0.0 && n.y == 0.0 && n.z == 0.0) continue;
  1316.          d = Shape->Interpolated_D[i][2*j+1];
  1317.  
  1318.          /* Make sure the smooth normals point in the same direction as the normal */
  1319.          VDot(t, n1, n); if (t > 0) VScale(n1, n0, -1.0);
  1320.          VDot(t, n2, n); if (t > 0) VScale(n2, n1, -1.0);
  1321.          VDot(t, n3, n); if (t > 0) VScale(n3, n2, -1.0);
  1322.  
  1323.          if (intersect_subpatch(Shape->Patch_Type, Ray, &v1, &v2, &v3, &n,
  1324.             d, &n1, &n2, &n3, &Depth, &ip, &ip_norm)) {
  1325.             Shape->Intersection_Point[tcnt + cnt] = ip;
  1326.             Shape->Normal_Vector[tcnt + cnt] = ip_norm;
  1327.             Depths[cnt] = Depth;
  1328.             if (tcnt + ++cnt >= MAX_BICUBIC_INTERSECTIONS)
  1329.                /* Too many intersections. Stop looking for more. */
  1330.                return cnt;
  1331.          }
  1332.       }
  1333.    }
  1334.    return cnt;
  1335. }
  1336.  
  1337. int All_Bicubic_Patch_Intersections(Object, Ray, Depth_Queue)
  1338. OBJECT *Object;
  1339. RAY *Ray;
  1340. PRIOQ *Depth_Queue;
  1341. {
  1342.    BICUBIC_PATCH *Shape = (BICUBIC_PATCH *) Object;
  1343.    DBL Depths[MAX_BICUBIC_INTERSECTIONS];
  1344.    INTERSECTION Local_Element;
  1345.    int cnt, tcnt, i, Intersection_Found;
  1346.  
  1347.    Intersection_Found = 0;
  1348.    Ray_Bicubic_Tests++;
  1349.    if (Ray == VP_Ray)
  1350.       Shape->Intersection_Count = 0;
  1351.    tcnt = Shape->Intersection_Count;
  1352.    if (Shape->Patch_Type == 0)
  1353.       cnt = intersect_bicubic_patch0(Ray, Shape, &Depths[0]);
  1354.    else if (Shape->Patch_Type == 1)
  1355.       cnt = intersect_bicubic_patch1(Ray, Shape, &Depths[0]);
  1356.    else if (Shape->Patch_Type == 2)
  1357.       cnt = intersect_bicubic_patch2(Ray, Shape, &Depths[0]);
  1358.    else if (Shape->Patch_Type == 3)
  1359.       cnt = intersect_bicubic_patch3(Ray, Shape, &Depths[0]);
  1360.    else if (Shape->Patch_Type == 4)
  1361.       cnt = intersect_bicubic_patch4(Ray, Shape, &Depths[0]);
  1362.    else {
  1363.       Error("Bad patch type\n");
  1364.    }
  1365.    if (cnt > 0) Ray_Bicubic_Tests_Succeeded++;
  1366.    for (i=0;i<cnt;i++) {
  1367.       if (!Shadow_Test_Flag)
  1368.          Shape->Intersection_Count++;
  1369.       Local_Element.Depth = Depths[i];
  1370.       Local_Element.Object = Shape->Parent_Object;
  1371.       Local_Element.Point = Shape->Intersection_Point[tcnt + i];
  1372.       Local_Element.Shape = (SHAPE *)Shape;
  1373.       pq_add (Depth_Queue, &Local_Element);
  1374.       Intersection_Found = 1;
  1375.    }
  1376.    return (Intersection_Found);
  1377. }
  1378.  
  1379. /* A patch is not a solid, so an inside test doesn't make sense. */
  1380. int Inside_Bicubic_Patch (Test_Point, Object)
  1381. VECTOR *Test_Point;
  1382. OBJECT *Object;
  1383. {
  1384.    return 0;
  1385. }
  1386.  
  1387. void Bicubic_Patch_Normal (Result, Object, Intersection_Point)
  1388. OBJECT *Object;
  1389. VECTOR *Result, *Intersection_Point;
  1390. {
  1391.    BICUBIC_PATCH *Patch = (BICUBIC_PATCH *)Object;
  1392.    int i;
  1393.  
  1394.    /* If all is going well, the normal was computed at the time the intersection
  1395.       was computed.  Look on the list of associated intersection points and normals */
  1396.    for (i=0;i<Patch->Intersection_Count;i++)
  1397.       if (Intersection_Point->x == Patch->Intersection_Point[i].x &&
  1398.          Intersection_Point->y == Patch->Intersection_Point[i].y &&
  1399.          Intersection_Point->z == Patch->Intersection_Point[i].z) {
  1400.          Result->x = Patch->Normal_Vector[i].x;
  1401.          Result->y = Patch->Normal_Vector[i].y;
  1402.          Result->z = Patch->Normal_Vector[i].z;
  1403.          return;
  1404.       }
  1405.    if (Options & DEBUGGING) {
  1406.       printf("Bicubic patch normal for unknown intersection point\n");
  1407.       fflush(stdout);
  1408.    }
  1409.    Result->x = 1.0;
  1410.    Result->y = 0.0;
  1411.    Result->z = 0.0;
  1412. }
  1413.  
  1414. void *Copy_Bicubic_Patch (Object)
  1415. OBJECT *Object;
  1416. {
  1417.    BICUBIC_PATCH *New_Shape;
  1418.  
  1419.    New_Shape = Get_Bicubic_Patch_Shape ();
  1420.    *New_Shape = * ((BICUBIC_PATCH *)Object);
  1421.    New_Shape -> Next_Object = NULL;
  1422.  
  1423.    New_Shape->Interpolated_Grid = NULL;
  1424.    Precompute_Patch_Values(New_Shape);
  1425.    if (New_Shape->Shape_Texture != NULL)
  1426.       New_Shape->Shape_Texture = Copy_Texture (New_Shape->Shape_Texture);
  1427.  
  1428.    return (void *)(New_Shape);
  1429. }
  1430.  
  1431. void Translate_Bicubic_Patch (Object, Vector)
  1432. OBJECT *Object;
  1433. VECTOR *Vector;
  1434. {
  1435.    BICUBIC_PATCH *Patch = (BICUBIC_PATCH *) Object;
  1436.    int i, j;
  1437.    for (i=0;i<4;i++) for (j=0;j<4;j++)
  1438.       VAdd (Patch->Control_Points[i][j], Patch->Control_Points[i][j], *Vector)
  1439.          Precompute_Patch_Values(Patch);
  1440.    Translate_Texture (&((BICUBIC_PATCH *) Object)->Shape_Texture, Vector);
  1441. }
  1442.  
  1443. void Rotate_Bicubic_Patch (Object, Vector)
  1444. OBJECT *Object;
  1445. VECTOR *Vector;
  1446. {
  1447.    TRANSFORMATION Transformation;
  1448.    BICUBIC_PATCH *Patch = (BICUBIC_PATCH *) Object;
  1449.    int i, j;
  1450.  
  1451.    Get_Rotation_Transformation (&Transformation, Vector);
  1452.    for (i=0;i<4;i++) for (j=0;j<4;j++)
  1453.       MTransformVector(&(Patch->Control_Points[i][j]),
  1454.          &(Patch->Control_Points[i][j]),
  1455.          &Transformation);
  1456.    Precompute_Patch_Values(Patch);
  1457.    Rotate_Texture (&((BICUBIC_PATCH *) Object)->Shape_Texture, Vector);
  1458. }
  1459.  
  1460. void Scale_Bicubic_Patch (Object, Vector)
  1461. OBJECT *Object;
  1462. VECTOR *Vector;
  1463. {
  1464.    BICUBIC_PATCH *Patch = (BICUBIC_PATCH *) Object;
  1465.    int i, j;
  1466.    for (i=0;i<4;i++) for (j=0;j<4;j++)
  1467.       VEvaluate(Patch->Control_Points[i][j],
  1468.          Patch->Control_Points[i][j], *Vector);
  1469.    Precompute_Patch_Values(Patch);
  1470.    Scale_Texture (&((BICUBIC_PATCH *) Object)->Shape_Texture, Vector);
  1471. }
  1472.  
  1473.  
  1474. /* Inversion of a patch really doesn't make sense. */
  1475. void Invert_Bicubic_Patch (Object)
  1476. OBJECT *Object;
  1477. {
  1478.    ;
  1479. }
  1480.