home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / gemsiii / bzrinter.c < prev    next >
C/C++ Source or Header  |  1992-03-16  |  8KB  |  199 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  * bzrintrp.c:
  5.  *   Bezier curve interpolation routines.
  6.  *
  7.  */
  8.  
  9. #define FloatType float
  10.  
  11. /*****************************************************************************
  12. The following arrays were created by symbolically solving the Bezier
  13. interpolant problem for orders 2 to 9. Each line holds the rational
  14. weights for the given points to interpolate at node values (i/k, k is
  15. the degree) as the i'th element divided by the zero element in line.
  16. *****************************************************************************/
  17. static long IB2[2][3] =
  18. {
  19.     {       1L,       1L,       0L },
  20.     {       1L,       0L,       1L }
  21. };
  22. static long *InterpBlend2[2] =
  23. {
  24.     IB2[0], IB2[1]
  25. };
  26.  
  27. static long IB3[3][4] =
  28. {
  29.     {       1L,       1L,       0L,       0L },
  30.     {       2L,      -1L,       4L,      -1L },
  31.     {       1L,       0L,       0L,       1L }
  32. };
  33. static long *InterpBlend3[3] =
  34. {
  35.     IB3[0], IB3[1], IB3[2]
  36. };
  37.  
  38. static long IB4[4][5] =
  39. {
  40.     {       1L,       1L,       0L,       0L,       0L },
  41.     {       6L,      -5L,      18L,      -9L,       2L },
  42.     {       6L,       2L,      -9L,      18L,      -5L },
  43.     {       1L,       0L,       0L,       0L,       1L }
  44. };
  45. static long *InterpBlend4[4] =
  46. {
  47.     IB4[0], IB4[1], IB4[2], IB4[3]
  48. };
  49.  
  50. static long IB5[5][6] =
  51. {
  52.     {       1L,       1L,       0L,       0L,       0L,       0L },
  53.     {      12L,     -13L,      48L,     -36L,      16L,      -3L },
  54.     {      18L,      13L,     -64L,     120L,     -64L,      13L },
  55.     {      12L,      -3L,      16L,     -36L,      48L,     -13L },
  56.     {       1L,       0L,       0L,       0L,       0L,       1L }
  57. };
  58. static long *InterpBlend5[5] =
  59. {
  60.     IB5[0], IB5[1], IB5[2], IB5[3], IB5[4]
  61. };
  62.  
  63. static long IB6[6][7] =
  64. {
  65.     {       1L,       1L,       0L,       0L,       0L,       0L,       0L },
  66.     {      60L,     -77L,     300L,    -300L,     200L,     -75L,      12L },
  67.     {     240L,     269L,   -1450L,    2950L,   -2300L,     925L,    -154L },
  68.     {     240L,    -154L,     925L,   -2300L,    2950L,   -1450L,     269L },
  69.     {      60L,      12L,     -75L,     200L,    -300L,     300L,     -77L },
  70.     {       1L,       0L,       0L,       0L,       0L,       0L,       1L }
  71. };
  72. static long *InterpBlend6[6] =
  73. {
  74.     IB6[0], IB6[1], IB6[2], IB6[3], IB6[4], IB6[5]
  75. };
  76.  
  77. static long IB7[7][8] =
  78. {
  79.     {       1L,       1L,       0L,       0L,       0L,       0L,       0L,
  80.                                                                         0L },
  81.     {      60L,     -87L,     360L,    -450L,     400L,    -225L,      72L,
  82.                                                                       -10L },
  83.     {     150L,     227L,   -1332L,    3015L,   -3080L,    1845L,    -612L,
  84.                                                                        87L },
  85.     {     200L,    -227L,    1512L,   -4185L,    6000L,   -4185L,    1512L,
  86.                                                                      -227L },
  87.     {     150L,      87L,    -612L,    1845L,   -3080L,    3015L,   -1332L,
  88.                                                                       227L },
  89.     {      60L,     -10L,      72L,    -225L,     400L,    -450L,     360L,
  90.                                                                       -87L },
  91.     {       1L,       0L,       0L,       0L,       0L,       0L,       0L,
  92.                                                                         1L }
  93. };
  94. static long *InterpBlend7[7] =
  95. {
  96.     IB7[0], IB7[1], IB7[2], IB7[3], IB7[4], IB7[5], IB7[6]
  97. };
  98.  
  99. static long IB8[8][9] =
  100. {
  101.     {       1L,       1L,       0L,       0L,       0L,       0L,       0L,
  102.                                                               0L,       0L },
  103.     {     420L,    -669L,    2940L,   -4410L,    4900L,   -3675L,    1764L,
  104.                                                            -490L,      60L },
  105.     {    7560L,   14318L,  -90846L,  228879L, -288610L,  229320L, -113778L,
  106.                                                           32291L,   -4014L },
  107.     {   25200L,  -42881L,  313012L, -960351L, 1568980L,-1409975L,  750876L,
  108.                                                         -223097L,   26836L },
  109.     {   25200L,   28636L, -223097L,  750876L,-1409975L, 1568980L, -960351L,
  110.                                                          313012L,  -42881L },
  111.     {    7560L,   -4014L,   32291L, -113778L,  229320L, -288610L,  228879L,
  112.                                                          -90846L,   14318L },
  113.     {     420L,      60L,    -490L,    1764L,   -3675L,    4900L,   -4410L,
  114.                                                            2940L,    -669L },
  115.     {       1L,       0L,       0L,       0L,       0L,       0L,       0L,
  116.                                                               0L,       1L }
  117. };
  118. static long *InterpBlend8[8] =
  119. {
  120.     IB8[0], IB8[1], IB8[2], IB8[3], IB8[4], IB8[5], IB8[6], IB8[7]
  121. };
  122.  
  123. static long IB9[9][10] =
  124. {
  125.     {       1L,       1L,       0L,       0L,       0L,       0L,       0L,
  126.                                                     0L,       0L,       0L },
  127.     {     840L,   -1443L,    6720L,  -11760L,   15680L,  -14700L,    9408L,
  128.                                                 -3920L,     960L,    -105L },
  129.     {    8820L,   19939L, -135936L,  379008L, -568064L,  561960L, -370944L,
  130.                                                157696L,  -39168L,    4329L },
  131.     {   17640L,  -40953L,  324544L,-1096144L, 2030784L,-2229500L, 1563968L,
  132.                                               -691824L,  176704L,  -19939L },
  133.     {   22050L,   40953L, -349184L, 1298304L,-2731008L, 3503920L,-2731008L,
  134.                                               1298304L, -349184L,   40953L },
  135.     {   17640L,  -19939L,  176704L, -691824L, 1563968L,-2229500L, 2030784L,
  136.                                              -1096144L,  324544L,  -40953L },
  137.     {    8820L,    4329L,  -39168L,  157696L, -370944L,  561960L, -568064L,
  138.                                                379008L, -135936L,   19939L },
  139.     {     840L,    -105L,     960L,   -3920L,    9408L,  -14700L,   15680L,
  140.                                                -11760L,    6720L,   -1443L },
  141.     {       1L,       0L,       0L,       0L,       0L,       0L,       0L,
  142.                                                     0L,       0L,       1L }
  143. };
  144. static long *InterpBlend9[9] =
  145. {
  146.     IB9[0], IB9[1], IB9[2], IB9[3], IB9[4], IB9[5], IB9[6], IB9[7], IB9[8]
  147. };
  148.  
  149. static long **InterpBlend[] =
  150. {
  151.     NULL,
  152.     NULL,
  153.     InterpBlend2,
  154.     InterpBlend3,
  155.     InterpBlend4,
  156.     InterpBlend5,
  157.     InterpBlend6,
  158.     InterpBlend7,
  159.     InterpBlend8,
  160.     InterpBlend9
  161. };
  162.  
  163. /*****************************************************************************
  164. Blends one Bezier control point using the Input points, the Bezier curve
  165. should interpolate, and Interp (Inverse(M)) arrays.
  166. *****************************************************************************/
  167. static FloatType BzrCrvInterpOnePoint(Input, Interp, Size)
  168. FloatType *Input;
  169. long *Interp;
  170. int Size;
  171. {
  172.     int i;
  173.     long Denom = *Interp++;
  174.     FloatType R = 0.0;
  175.  
  176.     for (i = 0; i < Size; i++)
  177.         R += (*Input++ * *Interp++) / Denom;
  178.  
  179.     return R;
  180. }
  181.  
  182. /*****************************************************************************
  183. Blends the Input points, the Bezier curve should interpolate, using the
  184. Interp array (Inverse(M)) into the Result Bezier control points array.
  185. Input and Result arrays are of size Size, which is the curve order from 2
  186. (linear) to 9.
  187. *****************************************************************************/
  188. void BzrCrvInterp(Result, Input, Size)
  189. FloatType *Result;
  190. FloatType *Input;
  191. int Size;
  192. {
  193.     int i;
  194.     long **Interp = InterpBlend[Size];
  195.  
  196.     for (i = 0; i < Size; i++)
  197.         *Result++ = BzrCrvInterpOnePoint(Input, *Interp++, Size);
  198. }
  199.