home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / RasMol2 / pixutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  42.4 KB  |  1,665 lines  |  [TEXT/KAHL]

  1. /* pixutils.c
  2.  * RasMol2 Molecular Graphics
  3.  * Roger Sayle, October 1994
  4.  * Version 2.5
  5.  */
  6. #include "rasmol.h"
  7.  
  8. #ifdef IBMPC
  9. #include <windows.h>
  10. #include <malloc.h>
  11. #endif
  12. #ifdef APPLEMAC
  13. #ifdef __CONDITIONALMACROS__
  14. #include <Printing.h>
  15. #else
  16. #include <PrintTraps.h>
  17. #endif
  18. #include <Types.h>
  19. #endif
  20. #ifdef sun386
  21. #include <stdlib.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <math.h>
  26.  
  27. #define PIXUTILS
  28. #include "pixutils.h"
  29. #include "graphics.h"
  30. #include "molecule.h"
  31. #include "transfor.h"
  32. #include "render.h"
  33. #include "font.h"
  34.  
  35. #ifdef INVERT
  36. #define InvertY(y) (y)
  37. #else
  38. #define InvertY(y) (-(y))
  39. #endif
  40.  
  41. /* Sutherland-Cohen Line Clipping Macros */
  42. #define BitAbove    0x01
  43. #define BitBelow    0x02
  44. #define BitRight    0x04
  45. #define BitLeft     0x08
  46. #define BitFront    0x10
  47.  
  48. #define Reject(x,y)   ((x)&(y))
  49. #define Accept(x,y)   (!((x)|(y)))
  50. #define RootSix       2.44948974278
  51.  
  52. #define SETPIXEL(dptr,fptr,d,c)    if( (d) > *(dptr) )              \
  53.                                    {   *(dptr) = (d);               \
  54.                                        *(fptr) = (c);               \
  55.                                    }
  56.  
  57.  
  58. #define MAXVERT 10
  59. typedef struct {
  60.                 Real dx,dz,di;
  61.                 Real x,z,i;
  62.                } Edge;
  63.  
  64. typedef struct {
  65.                 int x, y, z;
  66.                 int inten;
  67.                } Vert;
  68.  
  69. typedef struct {
  70.                 Vert v[MAXVERT];
  71.                 int count;
  72.                } Poly;
  73.  
  74.  
  75. typedef struct {
  76.                 short dx,dy,dz;
  77.                 short inten;
  78.         Long offset;
  79.            } ArcEntry;
  80.  
  81.  
  82. /* Note: DrawCylinderCaps currently employs an
  83.  *       extremely crude hack to avoid stripes
  84.  *       appearing along cylinders.
  85.  */
  86. #define ARCSIZE  2048
  87.  
  88. static ArcEntry __far *ArcAcPtr;
  89. static ArcEntry __far *ArcDnPtr;
  90. #if defined(IBMPC) || defined(APPLEMAC)
  91. static ArcEntry __far *ArcAc;
  92. static ArcEntry __far *ArcDn;
  93. #else
  94. static ArcEntry ArcAc[ARCSIZE];
  95. static ArcEntry ArcDn[ARCSIZE];
  96. #endif
  97.  
  98.  
  99. static char FontDimen[23];
  100. static int ClipStatus;
  101.  
  102.  
  103. static int OutCode(x,y,z)
  104.     register int x,y,z;
  105. {
  106.     register int result;
  107.  
  108.     result=0;
  109.     if( y<0 )
  110.     {   result |= BitAbove;
  111.     } else if( y>=YRange )
  112.         result |= BitBelow;
  113.  
  114.     if( x<0 )
  115.     {   result |= BitLeft;
  116.     } else if( x>=XRange )
  117.         result |= BitRight;
  118.  
  119.     if( !ZValid(z) )
  120.         result |= BitFront;
  121.     return result;
  122. }
  123.  
  124. #ifndef PIXUTILS  /* Unused Function */
  125. void PlotPoint(x,y,z,col)
  126.     int x,y,z,col;
  127. {
  128.     register Pixel __huge *fptr;
  129.     register short __huge *dptr;
  130.     register Long offset;
  131.  
  132.     /* SETPIXEL(dptr,fptr,z,Lut[col]); */
  133.  
  134.     offset = (Long)y*XRange+x;
  135.     dptr = DBuffer+offset;
  136.     if( z > *dptr )
  137.     {   fptr = FBuffer+offset;
  138.         *fptr = Lut[col];
  139.         *dptr = z;
  140.     }
  141. }
  142. #endif
  143.  
  144. void ClipPoint(x,y,z,col)
  145.     int x,y,z,col;
  146. {
  147.     register Pixel __huge *fptr;
  148.     register short __huge *dptr;
  149.     register Long offset;
  150.  
  151.     if( XValid(x) && YValid(y) && ZValid(z) )
  152.     {   /* PlotPoint(x,y,z,col); */
  153.         offset = (Long)y*XRange+x;
  154.         dptr = DBuffer+offset;
  155.         if( z > *dptr )
  156.         {   fptr = FBuffer+offset;
  157.             *fptr = Lut[col];
  158.             *dptr = z;
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. void PlotDeepPoint(x,y,z,col)
  165.     int x,y,z,col;
  166. {
  167.     register Long offset;
  168.     register Pixel __huge *fptr;
  169.     register short __huge *dptr;
  170.     register int inten;
  171.  
  172.     offset = (Long)y*XRange+x;
  173.     dptr = DBuffer+offset;
  174.  
  175.     if( z > *dptr )
  176.     {  fptr = FBuffer+offset;
  177.        inten = (ColourDepth*(z+ImageRadius-ZOffset))/ImageSize;
  178.        *fptr = Lut[col+inten];
  179.        *dptr = z;
  180.     }
  181. }
  182.  
  183. void ClipDeepPoint(x,y,z,col)
  184.     int x,y,z,col;
  185. {
  186.     register Long offset;
  187.     register Pixel __huge *fptr;
  188.     register short __huge *dptr;
  189.     register int inten;
  190.  
  191.     if( XValid(x) && YValid(y) && ZValid(z) )
  192.     {   /* PlotDeepPoint(x,y,z,col); */
  193.         offset = (Long)y*XRange+x;
  194.         dptr = DBuffer+offset;
  195.  
  196.         if( z > *dptr )
  197.         {  fptr = FBuffer+offset;
  198.            inten = (ColourDepth*(z+ImageRadius-ZOffset))/ImageSize;
  199.            *fptr = Lut[col+inten];
  200.            *dptr = z;
  201.         }
  202.     }
  203. }
  204.  
  205.  
  206. /* Macros for Bresenhams Line Drawing Algorithm */
  207. #define CommonStep(s)  z1 += zrate; SETPIXEL(dptr,fptr,z1,c);     \
  208.                        if( (zerr+=dz)>0 ) { zerr-=(s); z1+=iz; }
  209.  
  210. #define XStep  { if((err+=dy)>0) { fptr+=ystep; dptr+=ystep; err-=dx; } \
  211.                  fptr+=ix; dptr+=ix; x1+=ix; CommonStep(dx); }
  212.  
  213. #define YStep  { if((err+=dx)>0) { fptr+=ix; dptr+=ix; err-=dy; } \
  214.                  fptr+=ystep; dptr+=ystep; y1+=iy; CommonStep(dy); }
  215.                      
  216.  
  217. void DrawTwinLine(x1,y1,z1,x2,y2,z2,col1,col2)
  218.     int x1,y1,z1,x2,y2,z2,col1,col2;
  219. {
  220.     register Long offset;
  221.     register Pixel __huge *fptr;
  222.     register short __huge *dptr;
  223.     register int zrate, zerr;
  224.     register int ystep,err;
  225.     register int ix,iy,iz;
  226.     register int dx,dy,dz;
  227.     register int mid;
  228.     register Pixel c;
  229.  
  230.     c = Lut[col1];
  231.  
  232.     offset = (Long)y1*XRange + x1;
  233.     fptr = FBuffer+offset;
  234.     dptr = DBuffer+offset;
  235.  
  236.     SETPIXEL(dptr,fptr,z1,c);
  237.  
  238.     dx = x2-x1;  dy = y2-y1; dz = z2-z1;
  239.     if( !dx && !dy ) return;
  240.  
  241.     ystep = XRange;
  242.     ix = iy = iz = 1;
  243.     if( dy<0 ) { dy = -dy; iy = -1; ystep = -ystep; }
  244.     if( dx<0 ) { dx = -dx; ix = -1; }
  245.     if( dz<0 ) { dz = -dz; iz = -1; }
  246.  
  247.     if( dx>dy )
  248.     {   zrate = dz/dx;  
  249.         dz -= dx*zrate;
  250.         err = zerr = -(dx>>1);
  251.  
  252.         if( col1 != col2 )
  253.         {   mid = (x1+x2)>>1;
  254.             while( x1!=mid ) XStep;
  255.             c = Lut[col2];
  256.         }
  257.         while( x1!=x2 ) XStep;
  258.  
  259.     } else
  260.     {   zrate = dz/dy;
  261.         dz -= dy*zrate;
  262.         err = zerr = -(dy>>1);
  263.  
  264.         if( col1 != col2 )
  265.         {   mid = (y1+y2)>>1;
  266.             while( y1!=mid ) YStep;
  267.             c = Lut[col2];
  268.         }
  269.         while( y1!=y2 ) YStep;
  270.     }
  271. }
  272.  
  273.  
  274. static void ClipLine(x1,y1,z1,x2,y2,z2,col)
  275.     int x1,y1,z1,x2,y2,z2,col;
  276. {
  277.     register int code1,code2;
  278.     register int delta,rest;
  279.     register int temp;
  280.  
  281.     while( True )
  282.     {   code1 = OutCode(x1,y1,z2);
  283.         code2 = OutCode(x2,y2,z2);
  284.         if( Reject(code1,code2) ) return;
  285.         if( Accept(code1,code2) ) break;
  286.  
  287.         if( !code1 )
  288.         {   temp=x1; x1=x2; x2=temp;
  289.             temp=y1; y1=y2; y2=temp;
  290.             temp=z1; z1=z2; z2=temp;
  291.             code1 = code2;
  292.         }
  293.  
  294.         if( code1 & BitAbove )
  295.         {   delta = y2-y1;
  296.             x1 += (int)(((Long)y1*(x1-x2))/delta);  
  297.             z1 += (int)(((Long)y1*(z1-z2))/delta);
  298.             y1 = 0;
  299.         } else if( code1 & BitLeft )
  300.         {   delta = x2-x1;
  301.             y1 += (int)(((Long)x1*(y1-y2))/delta);
  302.             z1 += (int)(((Long)x1*(z1-z2))/delta);
  303.             x1 = 0;
  304.         } else if( code1 & BitRight )
  305.         {   delta = x2-x1;
  306.             temp=XRange-1; rest=temp-x1;
  307.             y1 += (int)(((Long)rest*(y2-y1))/delta);
  308.             z1 += (int)(((Long)rest*(z2-z1))/delta);
  309.             x1 = temp;
  310.         } else if( code1 & BitBelow )
  311.         {   delta = y2-y1;
  312.             temp=YRange-1; rest=temp-y1;
  313.             x1 += (int)(((Long)rest*(x2-x1))/delta);
  314.             z1 += (int)(((Long)rest*(z2-z1))/delta);
  315.             y1 = temp;
  316.         } else /* SLAB */
  317.         {   delta = z2-z1;
  318.             rest = SlabValue-z1;
  319.             x1 += (int)(((Long)rest*(x2-x1))/delta);
  320.             y1 += (int)(((Long)rest*(y2-y1))/delta);
  321.             z1 = SlabValue;
  322.         }
  323.     }
  324.     DrawTwinLine(x1,y1,z1,x2,y2,z2,col,col);
  325. }
  326.  
  327.  
  328. void ClipTwinLine(x1,y1,z1,x2,y2,z2,col1,col2)
  329.     int x1,y1,z1,x2,y2,z2,col1,col2;
  330. {
  331.     register int xmid,ymid,zmid;
  332.     register int code1,code2;
  333.  
  334.  
  335.     if( col1!=col2 )
  336.     {   code1 = OutCode(x1,y1,z1);
  337.         code2 = OutCode(x2,y2,z2);
  338.         if( !Reject(code1,code2) )
  339.         {   if( !Accept(code1,code2) )
  340.             {  xmid = (x1+x2)/2;
  341.                ymid = (y1+y2)/2;
  342.                zmid = (z1+z2)/2;
  343.                ClipLine(x1,y1,z1,xmid,ymid,zmid,col1);
  344.                ClipLine(xmid,ymid,zmid,x2,y2,z2,col2);
  345.             } else
  346.                DrawTwinLine(x1,y1,z1,x2,y2,z2,col1,col2);
  347.         }
  348.     } else
  349.         ClipLine(x1,y1,z1,x2,y2,z2,col1);
  350. }
  351.  
  352.  
  353.  
  354. /* Macros for 3D Bresenhams Vector Algorithm */
  355. #define CommonVectStep(s)  z1 += zrate;   c1 += crate;                    \
  356.                            SETPIXEL(dptr,fptr,z1,Lut[col+c1]);            \
  357.                            if( (zerr+=dz)>0 ) { zerr -= (s); z1 += iz; }  \
  358.                            if( (cerr+=dc)>0 ) { cerr -= (s); c1 += iz; }
  359.  
  360. #define XVectStep  { if((err+=dy)>0) { fptr+=ystep; dptr+=ystep; err-=dx; } \
  361.                      fptr+=ix; dptr+=ix; x1+=ix; CommonVectStep(dx); }
  362.  
  363. #define YVectStep  { if((err+=dx)>0) { fptr+=ix; dptr+=ix; err-=dy; } \
  364.                      fptr+=ystep; dptr+=ystep; y1+=iy; CommonVectStep(dy); }
  365.  
  366.  
  367. void DrawTwinVector(x1,y1,z1,x2,y2,z2,col1,col2)
  368.     int x1,y1,z1,x2,y2,z2,col1,col2;
  369. {
  370.     register Long offset;
  371.     register Pixel __huge *fptr;
  372.     register short __huge *dptr;
  373.     register int dx,dy,dz,dc;
  374.     register int crate, cerr;
  375.     register int zrate, zerr;
  376.     register int ystep,err;
  377.     register int ix,iy,iz;
  378.     register int col, mid;
  379.     register int c1, c2;
  380.  
  381.     c1 = (ColourDepth*(z1+ImageRadius-ZOffset))/ImageSize;
  382.     c2 = (ColourDepth*(z2+ImageRadius-ZOffset))/ImageSize;
  383.  
  384.     offset = (Long)y1*XRange + x1;
  385.     fptr = FBuffer+offset;
  386.     dptr = DBuffer+offset;
  387.  
  388.     SETPIXEL(dptr,fptr,z1,Lut[col1+c1]);
  389.  
  390.     dx = x2 - x1;  dy = y2 - y1;
  391.     dz = z2 - z1;  dc = c2 - c1;
  392.     if( !dx && !dy ) return;
  393.  
  394.     if( dy<0 ) 
  395.     {   ystep = -XRange;
  396.         dy = -dy; 
  397.         iy = -1; 
  398.     } else
  399.     {   ystep = XRange;
  400.         iy = 1;
  401.     }
  402.  
  403.     if( dx<0 ) 
  404.     {   dx = -dx; 
  405.         ix = -1; 
  406.     } else ix = 1;
  407.  
  408.     iz = (dz<0)? -1 : 1;
  409.  
  410.     if( dx>dy )
  411.     {   zrate = dz/dx;  dz -= dx*zrate; 
  412.         crate = dc/dx;  dc -= dx*crate;
  413.         err = zerr = cerr = -(dx>>1);
  414.         col = col1;
  415.  
  416.         if( dz<0 )
  417.         {   dz = -dz;
  418.             dc = -dc;
  419.         }
  420.         
  421.         if( col1 != col2 )
  422.         {   mid = (x1+x2)>>1;
  423.             while( x1!=mid ) XVectStep;
  424.             col = col2;
  425.         }
  426.         while( x1!=x2 ) XVectStep;
  427.     } else
  428.     {   zrate = dz/dy;  dz -= dy*zrate;
  429.         crate = dc/dy;  dc -= dy*crate;
  430.         err = zerr = cerr = -(dy>>1);
  431.         col = col1;
  432.  
  433.         if( dz<0 )
  434.         {   dz = -dz;
  435.             dc = -dc;
  436.         }
  437.  
  438.         if( col1 != col2 )
  439.         {   mid = (y1+y2)>>1;
  440.             while( y1!=mid ) YVectStep;
  441.             col=col2;
  442.         }
  443.         while( y1!=y2 ) YVectStep;
  444.     }
  445. }
  446.  
  447. static void ClipVector(x1,y1,z1,x2,y2,z2,col)
  448.     int x1,y1,z1,x2,y2,z2,col;
  449. {
  450.     register int code1,code2;
  451.     register int delta,rest;
  452.     register int temp;
  453.  
  454.     code1 = OutCode(x1,y1,z1);
  455.     code2 = OutCode(x2,y2,z2);
  456.  
  457.     while( True )
  458.     {   if( Accept(code1,code2) ) break;
  459.         if( Reject(code1,code2) ) return;
  460.  
  461.         if( !code1 )
  462.         {   code1 = code2; code2 = 0;
  463.             temp=x1; x1=x2; x2=temp;
  464.             temp=y1; y1=y2; y2=temp;
  465.             temp=z1; z1=z2; z2=temp;
  466.         }
  467.  
  468.         if( code1 & BitAbove )
  469.         {   delta = y2-y1;
  470.             x1 += (int)(((Long)y1*(x1-x2))/delta);  
  471.             z1 += (int)(((Long)y1*(z1-z2))/delta);
  472.             y1 = 0;
  473.         } else if( code1 & BitLeft )
  474.         {   delta = x2-x1;
  475.             y1 += (int)(((Long)x1*(y1-y2))/delta);
  476.             z1 += (int)(((Long)x1*(z1-z2))/delta);
  477.             x1 = 0;
  478.         } else if( code1 & BitRight )
  479.         {   delta = x2-x1;
  480.             temp=XRange-1; rest=temp-x1;
  481.             y1 += (int)(((Long)rest*(y2-y1))/delta);
  482.             z1 += (int)(((Long)rest*(z2-z1))/delta);
  483.             x1 = temp;
  484.         } else if( code1 & BitBelow )
  485.         {   delta = y2-y1;
  486.             temp=YRange-1; rest=temp-y1;
  487.             x1 += (int)(((Long)rest*(x2-x1))/delta);
  488.             z1 += (int)(((Long)rest*(z2-z1))/delta);
  489.             y1 = temp;
  490.         } else /* SLAB */
  491.         {   delta = z2-z1;
  492.             rest = (SlabValue-1)-z1;
  493.             x1 += (int)(((Long)rest*(x2-x1))/delta);
  494.             y1 += (int)(((Long)rest*(y2-y1))/delta);
  495.             z1 = SlabValue-1;
  496.         }
  497.         code1 = OutCode(x1,y1,z1);
  498.     }
  499.     DrawTwinVector(x1,y1,z1,x2,y2,z2,col,col);
  500. }
  501.  
  502.  
  503. void ClipTwinVector(x1,y1,z1,x2,y2,z2,col1,col2)
  504.     int x1,y1,z1,x2,y2,z2,col1,col2;
  505. {
  506.     register int xmid,ymid,zmid;
  507.     register int code1,code2;
  508.  
  509.     if( col1!=col2 )
  510.     {   code1 = OutCode(x1,y1,z1);
  511.         code2 = OutCode(x2,y2,z2);
  512.         if( !Reject(code1,code2) )
  513.         {   if( !Accept(code1,code2) )
  514.             {  xmid = (x1+x2)/2;
  515.                ymid = (y1+y2)/2;
  516.                zmid = (z1+z2)/2;
  517.                ClipVector(x1,y1,z1,xmid,ymid,zmid,col1);
  518.                ClipVector(xmid,ymid,zmid,x2,y2,z2,col2);
  519.             } else
  520.                DrawTwinVector(x1,y1,z1,x2,y2,z2,col1,col2);
  521.         }
  522.     } else
  523.         ClipVector(x1,y1,z1,x2,y2,z2,col1);
  524. }
  525.  
  526.  
  527. void ClipDashVector(x1,y1,z1,x2,y2,z2,col1,col2)
  528.     int x1,y1,z1,x2,y2,z2,col1,col2;
  529. {
  530.     register Long offset;
  531.     register Pixel __huge *fptr;
  532.     register short __huge *dptr;
  533.     register int ix,iy,iz,ic;
  534.     register int dx,dy,dz,dc;
  535.     register int crate, cerr;
  536.     register int zrate, zerr;
  537.     register int ystep,err;
  538.     register int col, mid;
  539.     register int c1, c2;
  540.  
  541.  
  542.     if( (x1==x2) && (y1==y2) ) return;
  543.     if( Reject(OutCode(x1,y1,z1),OutCode(x2,y2,z2)) )
  544.         return;
  545.  
  546.     c1 = (ColourDepth*(z1+ImageRadius-ZOffset))/ImageSize;
  547.     c2 = (ColourDepth*(z2+ImageRadius-ZOffset))/ImageSize;
  548.  
  549.     dz = (z2 - z1)<<1;  
  550.     dc = (c2 - c1)<<1;
  551.     dx = x2 - x1;  
  552.     dy = y2 - y1;
  553.  
  554.     offset = (Long)y1*XRange + x1;
  555.     fptr = FBuffer+offset;
  556.     dptr = DBuffer+offset;
  557.  
  558.  
  559.     ystep = XRange;
  560.     ix = iy = iz = ic = 1;
  561.     if( dy<0 ) { dy = -dy; iy = -1; ystep = -ystep; }
  562.     if( dx<0 ) { dx = -dx; ix = -1; }
  563.     if( dz<0 ) { dz = -dz; iz = -1; }
  564.     if( dc<0 ) { dc = -dc; ic = -1; }
  565.  
  566.  
  567.     if( dx>dy )
  568.     {   if( x2<x1 )
  569.         {   mid = col1;
  570.             col1 = col2;
  571.             col2 = mid;
  572.         }
  573.         zrate = dz/dx;  dz -= dx*zrate;
  574.         crate = dc/dx;  dc -= dx*crate;
  575.         err = zerr = cerr = -(dx>>1);
  576.         mid = (x1+x2)/2;
  577.  
  578.         while( x1!=x2 )
  579.         {   if( XValid(x1) && YValid(y1) && ZValid(z1) )
  580.             {   col = (x1<mid)? col1 : col2;
  581.                 SETPIXEL(dptr,fptr,z1,Lut[col+c1]);
  582.             }
  583.  
  584.             if( (x1+ix)==x2 ) 
  585.                 break;
  586.  
  587.             if( (err+=dy+dy)>0 )
  588.                 if( (err-=dx)>0 )
  589.                 {   err-=dx;
  590.                     fptr+=ystep+ystep;
  591.                     dptr+=ystep+ystep;
  592.                     y1+=iy+iy;
  593.                 } else
  594.                 {   fptr+=ystep;
  595.                     dptr+=ystep;
  596.                     y1+=iy;
  597.                 }
  598.  
  599.             if( (zerr+=dz)>0 )
  600.                 if( (zerr-=dx)>0 )
  601.                 {   zerr -= dx;
  602.                     z1 += iz+iz;
  603.                 } else z1 += iz;
  604.  
  605.             if( (cerr+=dc)>0 )
  606.                 if( (cerr-=dx)>0 )
  607.                 {   cerr -= dx;
  608.                     c1 += ic+ic;
  609.                 } else c1 += ic;
  610.  
  611.             fptr+=ix+ix; dptr+=ix+ix; x1+=ix+ix;
  612.             z1 += zrate;   c1 += crate;
  613.         }
  614.     } else
  615.     {   if( y1>y2 )
  616.         {   mid = col1;
  617.             col1 = col2;
  618.             col2 = mid;
  619.         }
  620.  
  621.         zrate = dz/dy;  dz -= dy*zrate;
  622.         crate = dc/dy;  dc -= dy*crate;
  623.         err = zerr = cerr = -(dy>>1);
  624.         mid = (y1+y2)/2;
  625.  
  626.         
  627.         while( y1!=y2 )
  628.         {   if( XValid(x1) && YValid(y1) && ZValid(z1) )
  629.             {   col = (y1<mid)? col1 : col2;
  630.                 SETPIXEL(dptr,fptr,z1,Lut[col+c1]);
  631.             }
  632.  
  633.             if( (y1+iy)==y2 ) 
  634.                 break;
  635.  
  636.             if( (err+=dx+dx)>0 )
  637.                 if( (err-=dy)>0 )
  638.                 {   err-=dy;
  639.                     fptr+=ix+ix;
  640.                     dptr+=ix+ix;
  641.                     x1+=ix+ix;
  642.                 } else
  643.                 {   fptr+=ix;
  644.                     dptr+=ix;
  645.                     x1+=ix;
  646.                 }
  647.  
  648.             if( (zerr+=dz)>0 )
  649.                 if( (zerr-=dy)>0 )
  650.                 {   zerr -= dy;
  651.                     z1 += iz+iz;
  652.                 } else z1 += iz;
  653.  
  654.             if( (cerr+=dc)>0 )
  655.                 if( (cerr-=dy)>0 )
  656.                 {   cerr -= dy;
  657.                     c1 += ic+ic;
  658.                 } else c1 += ic;
  659.  
  660.             fptr+=ystep+ystep; dptr+=ystep+ystep; y1+=iy+iy;
  661.             z1 += zrate;   c1 += crate;
  662.         }
  663.     }
  664. }
  665.  
  666.  
  667. /* SplineCount is either 1, 2, 3, 4, 5 or 9! */
  668.  
  669. void StrandRibbon( src, dst, col1, col2 )
  670.     Knot *src, *dst;  int col1, col2;
  671. {
  672.     register int hsx, hsy, hsz;
  673.     register int hdx, hdy, hdz;
  674.     register int qsx, qsy, qsz;
  675.     register int qdx, qdy, qdz;
  676.     register int col;
  677.  
  678.     if( SplineCount != 4 )
  679.     {   if( SplineCount == 1 )
  680.         {   ClipVector( src->px, src->py, src->pz,
  681.                         dst->px, dst->py, dst->pz, col2 );
  682.             return;
  683.         } else if( SplineCount != 2 )
  684.             ClipVector( src->px, src->py, src->pz,
  685.                         dst->px, dst->py, dst->pz, col1 );
  686.  
  687.         ClipVector( src->px+src->wx, src->py+src->wy, src->pz+src->wz,
  688.                     dst->px+dst->wx, dst->py+dst->wy, dst->pz+dst->wz, col2 );
  689.         ClipVector( src->px-src->wx, src->py-src->wy, src->pz-src->wz,
  690.                     dst->px-dst->wx, dst->py-dst->wy, dst->pz-dst->wz, col2 );
  691.         if( SplineCount<=3 ) return;
  692.  
  693.         hsx = src->wx/2;  hsy = src->wy/2;  hsz = src->wz/2;
  694.         hdx = dst->wx/2;  hdy = dst->wy/2;  hdz = dst->wz/2;
  695.  
  696.         ClipVector( src->px+hsx, src->py+hsy, src->pz+hsz,
  697.                     dst->px+hdx, dst->py+hdy, dst->pz+hdz, col1 );
  698.         ClipVector( src->px-hsx, src->py-hsy, src->pz-hsz,
  699.                     dst->px-hdx, dst->py-hdy, dst->pz-hdz, col1 );
  700.         if( SplineCount==5 ) 
  701.             return;
  702.         col = col1;
  703.     } else /* SplineCount == 4 */
  704.     {   hsx = src->wx/2;  hsy = src->wy/2;  hsz = src->wz/2;
  705.         hdx = dst->wx/2;  hdy = dst->wy/2;  hdz = dst->wz/2;
  706.         col = col2;
  707.     }
  708.  
  709.     qsx = hsx/2;  qsy = hsy/2;  qsz = hsz/2;
  710.     qdx = hdx/2;  qdy = hdy/2;  qdz = hdz/2;
  711.  
  712.     ClipVector( src->px+hsx+qsx, src->py+hsy+qsy, src->pz+hsz+qsz,
  713.                 dst->px+hdx+qdx, dst->py+hdy+qdy, dst->pz+hdz+qdz, col );
  714.     ClipVector( src->px+hsx-qsx, src->py+hsy-qsy, src->pz+hsz-qsz,
  715.                 dst->px+hdx-qdx, dst->py+hdy-qdy, dst->pz+hdz-qdz, col1 );
  716.     ClipVector( src->px-hsx+qsx, src->py-hsy+qsy, src->pz-hsz+qsz,
  717.                 dst->px-hdx+qdx, dst->py-hdy+qdy, dst->pz-hdz+qdz, col1 );
  718.     ClipVector( src->px-hsx-qsx, src->py-hsy-qsy, src->pz-hsz-qsz,
  719.                 dst->px-hdx-qdx, dst->py-hdy-qdy, dst->pz-hdz-qdz, col );
  720. }
  721.  
  722.  
  723. #ifndef PIXUTILS  /* Unused Function */
  724. static void OutLinePolygon( p )
  725.     Poly *p;
  726. {
  727.     register int i;
  728.  
  729.     for( i=0; i<p->count-1; i++ )
  730.          ClipLine( p->v[i].x, p->v[i].y, p->v[i].z, 
  731.                    p->v[i+1].x, p->v[i+1].y, p->v[i+1].z,
  732.                    p->v[i].inten);
  733.     ClipLine( p->v[i].x, p->v[i].y, p->v[i].z,
  734.               p->v[0].x, p->v[0].y, p->v[0].z,
  735.               p->v[i].inten);
  736. }
  737. #endif
  738.  
  739.  
  740. static void RenderPolygon( p )
  741.     Poly *p;
  742. {
  743.     static Edge lft, rgt;
  744.     register Edge *pmin, *pmax;
  745.     register Pixel __huge *fbase;
  746.     register Pixel __huge *fptr;
  747.     register short __huge *dbase;
  748.     register short __huge *dptr;
  749.     register Long offset;
  750.  
  751.     register Real z,inten;
  752.     register Real dy,dz,di;
  753.     register int ri,li,ry,ly;
  754.     register int xmin,xmax;
  755.     register int ymin;
  756.     register int top,rem;
  757.     register int x,y,i;
  758.  
  759.  
  760.     /* Reject Clip Polygon */
  761.     if( UseSlabPlane )
  762.         for( i=0; i<p->count; i++ )
  763.             if( p->v[i].z >= SlabValue )
  764.                 return;
  765.  
  766.     /* Find top vertex */
  767.     top = 0;  
  768.     ymin = p->v[0].y;
  769.     for( i=1; i<p->count; i++ )
  770.        if( p->v[i].y < ymin )
  771.        {   ymin = p->v[i].y;
  772.            top = i;
  773.        }
  774.  
  775.     rem = p->count;
  776.     ly = ry = y = ymin;
  777.     li = ri = top;
  778.  
  779.     if( y<0 )
  780.     {   rem--;
  781.  
  782.         while( ly<=0 && rem )
  783.         {   i = li-1; if( i<0 ) i=p->count-1;
  784.             if( p->v[i].y > 0 )
  785.             {   dy = 1.0/(p->v[i].y - ly);
  786.                 lft.di = dy*(p->v[i].inten - p->v[li].inten);
  787.                 lft.dx = dy*(p->v[i].x - p->v[li].x);
  788.                 lft.dz = dy*(p->v[i].z - p->v[li].z);
  789.  
  790.                 lft.i = p->v[li].inten - ly*lft.di;
  791.                 lft.x = p->v[li].x - ly*lft.dx;
  792.                 lft.z = p->v[li].z - ly*lft.dz;
  793.             } else rem--;
  794.             ly = p->v[i].y;
  795.             li = i;
  796.         }
  797.  
  798.         while( ry<=0 && rem )
  799.         {   i = ri+1; if( i>=p->count ) i = 0;
  800.             if( p->v[i].y > 0 )
  801.             {   dy = 1.0/(p->v[i].y - ry);
  802.                 rgt.di = dy*(p->v[i].inten - p->v[ri].inten);
  803.                 rgt.dx = dy*(p->v[i].x - p->v[ri].x);
  804.                 rgt.dz = dy*(p->v[i].z - p->v[ri].z);
  805.  
  806.                 rgt.i = p->v[ri].inten - ry*rgt.di;
  807.                 rgt.x = p->v[ri].x - ry*rgt.dx;
  808.                 rgt.z = p->v[ri].z - ry*rgt.dz;
  809.             } else rem--;
  810.             ry = p->v[i].y;
  811.             ri = i;
  812.         }
  813.  
  814.         fbase = FBuffer;
  815.         dbase = DBuffer;
  816.         y = 0;
  817.     } else /* y >= 0 */
  818.     {   offset = (Long)y*XRange;
  819.         fbase = FBuffer+offset;
  820.         dbase = DBuffer+offset;
  821.     }
  822.  
  823.     while( rem )
  824.     {   while( ly<=y && rem )
  825.         {   i = li-1; if( i<0 ) i=p->count-1;
  826.             if( p->v[i].y > y )
  827.             {   dy = 1.0/(p->v[i].y - ly);
  828.                 lft.di = dy*(p->v[i].inten - p->v[li].inten);
  829.                 lft.dx = dy*(p->v[i].x - p->v[li].x);
  830.                 lft.dz = dy*(p->v[i].z - p->v[li].z);
  831.  
  832.                 lft.i = p->v[li].inten;
  833.                 lft.x = p->v[li].x;
  834.                 lft.z = p->v[li].z;
  835.             }
  836.             ly = p->v[i].y;
  837.             rem--;  li = i;
  838.         }
  839.  
  840.         while( ry<=y && rem )
  841.         {   i = ri+1; if( i>=p->count ) i = 0;
  842.             if( p->v[i].y > y )
  843.             {   dy = 1.0/(p->v[i].y - ry);
  844.                 rgt.di = dy*(p->v[i].inten - p->v[ri].inten);
  845.                 rgt.dx = dy*(p->v[i].x - p->v[ri].x);
  846.                 rgt.dz = dy*(p->v[i].z - p->v[ri].z);
  847.  
  848.                 rgt.i = p->v[ri].inten;
  849.                 rgt.x = p->v[ri].x;
  850.                 rgt.z = p->v[ri].z;
  851.             }
  852.             ry = p->v[i].y;
  853.             rem--; ri = i;
  854.         }
  855.  
  856.  
  857.         ymin = MinFun(ly,ry);
  858.         if( ymin>YRange )
  859.         {   ymin = YRange;
  860.             rem = 0;
  861.         }
  862.         
  863.         while( y<ymin )
  864.         {   if( lft.x < rgt.x )
  865.             {   pmin = &lft;
  866.                 pmax = &rgt;
  867.             } else
  868.             {   pmin = &rgt;
  869.                 pmax = &lft;
  870.             }
  871.  
  872.             xmax = (int)(pmax->x)+1;
  873.             xmin = (int)(pmin->x);
  874.  
  875.             if( (xmin<XRange) && (xmax>=0) )
  876.             {   di = (pmax->i-pmin->i)/(xmax-xmin);
  877.                 dz = (pmax->z-pmin->z)/(xmax-xmin);
  878.                 if( xmin<0 )
  879.                 {   inten = pmin->i - xmin*di;
  880.                     z = pmin->z - xmin*dz;
  881.                     xmin = 0;
  882.                 } else /* xmin >= 0 */
  883.                 {   inten = pmin->i;  
  884.                     z = pmin->z;
  885.                 }
  886.  
  887.                 if( xmax>=XRange )
  888.                     xmax = XRange;
  889.  
  890.                 fptr = fbase+xmin;
  891.                 dptr = dbase+xmin;
  892.                 for( x=xmin; x<xmax; x++ )
  893.                 {   if( (int)z > *dptr )
  894.                     {   *fptr = Lut[(int)inten];
  895.                         *dptr = (int)z;
  896.                     }
  897.                     inten += di;
  898.                     z += dz;
  899.                     fptr++;
  900.                     dptr++;
  901.                 }
  902.             }
  903.  
  904.             lft.x += lft.dx;  rgt.x += rgt.dx;
  905.             lft.z += lft.dz;  rgt.z += rgt.dz;
  906.             lft.i += lft.di;  rgt.i += rgt.di;
  907.             dbase += XRange;
  908.             fbase += XRange;
  909.             y++;
  910.         }
  911.     }
  912. }
  913.  
  914.  
  915. void SolidRibbon( src, dst, col )
  916.     Knot *src, *dst;  int col;
  917. {
  918.     static Poly p;
  919.  
  920.     p.v[0].x = src->px+src->wx;  
  921.     p.v[0].y = src->py+src->wy;  
  922.     p.v[0].z = src->pz+src->wz;
  923.     p.v[0].inten = src->inten+col;
  924.  
  925.     p.v[1].x = dst->px+dst->wx;  
  926.     p.v[1].y = dst->py+dst->wy;  
  927.     p.v[1].z = dst->pz+dst->wz;
  928.     p.v[1].inten = dst->inten+col;
  929.  
  930.     p.v[2].x = dst->px-dst->wx;
  931.     p.v[2].y = dst->py-dst->wy;  
  932.     p.v[2].z = dst->pz-dst->wz;
  933.     p.v[2].inten = dst->inten+col;
  934.  
  935.     p.v[3].x = src->px-src->wx;  
  936.     p.v[3].y = src->py-src->wy;  
  937.     p.v[3].z = src->pz-src->wz;
  938.     p.v[3].inten = src->inten+col;
  939.  
  940.     p.count = 4;
  941.     /* OutLinePolygon( &p ); */
  942.     RenderPolygon( &p );
  943. }
  944.  
  945.  
  946.  
  947. static int TestSphere( x, y, z, rad )
  948.     register int x, y, z, rad;
  949. {
  950.     register int temp;
  951.  
  952.     ClipStatus = 0;
  953.  
  954.     if( UseSlabPlane )
  955.     {   if( z-rad>=SlabValue )
  956.             return( False );
  957.  
  958.         if( z+rad>=SlabValue )
  959.         {   if( SlabMode )
  960.             {   ClipStatus |= BitFront;
  961.             } else return( False );
  962.         } else if( SlabMode==SlabSection )
  963.             return( False );
  964.     }
  965.  
  966.     temp = y-rad;
  967.     if( temp>=YRange ) return( False );
  968.     if( temp<0 ) ClipStatus |= BitAbove;
  969.  
  970.     temp = y+rad;
  971.     if( temp<0 ) return( False );
  972.     if( temp>=YRange ) ClipStatus |= BitBelow;
  973.  
  974.     temp = x+rad;
  975.     if( temp<0 ) return( False );
  976.     if( temp>=XRange ) ClipStatus |= BitRight;
  977.  
  978.     temp = x-rad;
  979.     if( temp>=XRange ) return( False );
  980.     if( temp<0 ) ClipStatus |= BitLeft;
  981.  
  982.     return True;
  983. }
  984.  
  985.  
  986. #ifdef INVERT
  987. #define CalcInten(dz)    inten = (dz)+(dz)+dx+dy
  988. #else
  989. #define CalcInten(dz)    inten = (dz)+(dz)+dx-dy
  990. #endif
  991.  
  992. #define UpdateAcross(dz)    \
  993.         depth = (dz)+z;                    \
  994.         if( depth > *dptr )                \
  995.         {   *dptr = depth;                 \
  996.             fptr = fold+dx;                \
  997.             CalcInten((dz));               \
  998.             if( inten>0 )                  \
  999.             {      inten = (int)((inten*ColConst[rad])>>ColBits); \
  1000.                    *fptr = Lut[col+inten]; \
  1001.             } else *fptr = Lut[col];       \
  1002.         }                                  \
  1003.         dptr++;  dx++;
  1004.  
  1005.  
  1006. #define UpdateLine  \
  1007.         dx = -wide;                   \
  1008.         dptr = dold-wide;             \
  1009.         tptr = LookUp[wide]+wide;     \
  1010.         while( dx<0 ) { UpdateAcross(*tptr); tptr--; }       \
  1011.         do { UpdateAcross(*tptr); tptr++; } while(dx<=wide); \
  1012.         dold += XRange;  fold += XRange;                     \
  1013.         dy++;
  1014.  
  1015.  
  1016. void DrawSphere(x,y,z,rad,col)
  1017.     int x,y,z,rad,col;
  1018. {
  1019.     register Pixel __huge *fptr, __huge *fold;
  1020.     register short __huge *dptr, __huge *dold;
  1021.     register Byte __far *tptr;
  1022.  
  1023.     register Long offset;
  1024.     register int depth,wide,inten;
  1025.     register int dx,dy;
  1026.  
  1027.     offset = (Long)(y-rad)*XRange + x;
  1028.     fold=FBuffer+offset;  
  1029.     dold=DBuffer+offset;
  1030.  
  1031.     dy = -rad;
  1032.     while( dy<0 ) 
  1033.     {   wide = LookUp[rad][-dy]; 
  1034.         UpdateLine; 
  1035.     }
  1036.  
  1037.     do { 
  1038.         wide = LookUp[rad][dy];  
  1039.         UpdateLine; 
  1040.     } while( dy<=rad );
  1041. }
  1042.  
  1043.  
  1044. void ClipSphere(x,y,z,rad,col)
  1045.     int x,y,z,rad,col;
  1046. {
  1047.     register Pixel __huge *fptr, __huge *fold;
  1048.     register short __huge *dptr, __huge *dold;
  1049.  
  1050.     register int lastx,lasty,dx,dy,dz;
  1051.     register int depth,wide,inten,side;
  1052.     register int crad,cwide,temp;
  1053.     register Long offset;
  1054.  
  1055.  
  1056.     /* Visibility Tests */
  1057.     if( !TestSphere(x,y,z,rad) )
  1058.         return;
  1059.  
  1060.     if( !ClipStatus )
  1061.     {   DrawSphere(x,y,z,rad,col);
  1062.         return;
  1063.     }
  1064.  
  1065.     if( ClipStatus&BitAbove )
  1066.     {   dy = -y;
  1067.         fold = FBuffer + x;
  1068.         dold = DBuffer + x;
  1069.     } else
  1070.     {   dy = -rad;
  1071.         offset = (Long)(y+dy)*XRange+x;
  1072.         fold = FBuffer + offset;
  1073.         dold = DBuffer + offset;
  1074.     }
  1075.  
  1076.     if( ClipStatus&BitBelow )
  1077.     {   lasty = (YRange-1)-y;
  1078.     } else lasty = rad;
  1079.  
  1080.  
  1081.     side = (XRange-1)-x;
  1082.     /* No Slab Plane Clipping */
  1083.     if( !(ClipStatus&BitFront) )
  1084.     {   while( dy<=lasty )
  1085.         {   wide = LookUp[rad][AbsFun(dy)];
  1086.             lastx = MinFun(wide,side);
  1087.             dx = - MinFun(wide,x);
  1088.             dptr = dold + dx;
  1089.  
  1090.             while( dx<=lastx )
  1091.             {   dz = LookUp[wide][AbsFun(dx)];
  1092.                 UpdateAcross(dz);
  1093.             }
  1094.             dold += XRange;
  1095.             fold += XRange;
  1096.             dy++;
  1097.         }
  1098.         return;
  1099.     }
  1100.  
  1101.  
  1102.     dz = SlabValue-z;
  1103.     crad = LookUp[rad][AbsFun(dz)];
  1104.  
  1105.    
  1106.     if( (z>SlabValue) || (SlabMode==SlabSection) )
  1107.     {   if( crad<lasty ) lasty = crad;
  1108.         if( -crad>dy ) 
  1109.         {   dy = -crad;
  1110.             offset = (Long)(y+dy)*XRange+x;
  1111.             fold = FBuffer + offset;
  1112.             dold = DBuffer + offset;
  1113.         }
  1114.     }
  1115.  
  1116.     while( dy<=lasty )
  1117.     {   temp = AbsFun(dy);
  1118.         wide = LookUp[rad][temp];
  1119.         lastx = MinFun(wide,side);
  1120.         dx = - MinFun(x,wide);
  1121.         dptr = dold + dx;
  1122.  
  1123.         if( temp<=crad )
  1124.         {   cwide = LookUp[crad][temp];
  1125.             while( dx<=lastx )
  1126.             {   temp = AbsFun(dx);
  1127.                 if( temp<=cwide )
  1128.                 {    /* Slab Plane Clipping Modes */
  1129.                     switch( SlabMode )
  1130.                     {   case( SlabFinal ):
  1131.                                 fold[dx] = Lut[col+SlabInten];
  1132.                                 *dptr = SliceValue;
  1133.                                 break;
  1134.  
  1135.                         case( SlabHollow ):
  1136.                                 dz = LookUp[wide][temp];
  1137.                                 depth = z - dz;
  1138.                                 if( depth>*dptr )
  1139.                                 {   *dptr = depth;
  1140. #ifdef INVERT
  1141.                                     inten = dz+dz-dx-dy;
  1142. #else
  1143.                                     inten = dz+dz-dx+dy;
  1144. #endif
  1145.                                     if( inten>0 )
  1146.                                     {   inten=(int)( (inten*ColConst[rad])
  1147.                                                      >>(ColBits+1));
  1148.                                         fold[dx] = Lut[col+inten];
  1149.                                     } else fold[dx] = Lut[col];
  1150.                                 }
  1151.                                 break;
  1152.  
  1153.                         case( SlabSection ):
  1154.                         case( SlabClose ):
  1155.                                 dz = SlabValue-z;
  1156.                                 depth = dx*dx+dy*dy+dz*dz+SliceValue;
  1157.                                 if( (*dptr<SliceValue) || (depth<*dptr) )
  1158.                                 {   fold[dx] = Lut[col+SlabInten];
  1159.                                     *dptr = depth;
  1160.                                 }
  1161.                                 break;
  1162.                     }
  1163.                     dptr++;  dx++;
  1164.                 } else if( (z<SlabValue) && (SlabMode!=SlabSection) )
  1165.                 {    dz = LookUp[wide][temp];
  1166.                      UpdateAcross(dz);
  1167.                 } else
  1168.                 {   dptr++;  dx++;
  1169.                 }
  1170.             }
  1171.         } else /* Slabless ScanLine */
  1172.             while( dx<=lastx )
  1173.             {   dz = LookUp[wide][AbsFun(dx)];
  1174.                 UpdateAcross(dz);
  1175.             }
  1176.  
  1177.         dold += XRange;
  1178.         fold += XRange;
  1179.         dy++;
  1180.     }
  1181. }
  1182.  
  1183.  
  1184. #ifdef FUNCPROTO
  1185. /* Function Prototypes */
  1186. static void DrawArcDn( short __huge*, Pixel __huge*, int, int );
  1187. static void DrawArcAc( short __huge*, Pixel __huge*, int, int );
  1188. static void ClipArcDn( short __huge*, Pixel __huge*, int, int, int, int );
  1189. static void ClipArcAc( short __huge*, Pixel __huge*, int, int, int, int );
  1190. #endif
  1191.  
  1192.  
  1193. static void DrawArcAc(dbase,fbase,z,c)
  1194.     register short __huge *dbase;
  1195.     register Pixel __huge *fbase;
  1196.     register int z,c;
  1197. {
  1198.     register ArcEntry __far *ptr;
  1199.     register short __huge *dptr;
  1200.     register short depth;
  1201.  
  1202.     for( ptr=ArcAc; ptr<ArcAcPtr; ptr++ )
  1203.     {   dptr = dbase+ptr->offset;  depth = ptr->dz+z;
  1204.         SETPIXEL(dptr,fbase+ptr->offset,depth,Lut[ptr->inten+c]);
  1205.     }
  1206. }
  1207.  
  1208. static void DrawArcDn(dbase,fbase,z,c)
  1209.     register short __huge *dbase;
  1210.     register Pixel __huge *fbase;
  1211.     register int z,c;
  1212. {
  1213.     register ArcEntry __far *ptr;
  1214.     register short __huge *dptr;
  1215.     register short depth;
  1216.  
  1217.     for( ptr=ArcDn; ptr<ArcDnPtr; ptr++ )
  1218.     {   dptr = dbase+ptr->offset;  depth = ptr->dz+z;
  1219.         SETPIXEL(dptr,fbase+ptr->offset,depth,Lut[ptr->inten+c]);
  1220.     }
  1221. }
  1222.  
  1223.  
  1224. static void DrawCylinderCaps( x1,y1,z1,c1,x2,y2,z2,c2,rad )
  1225.     int x1,y1,z1,c1, x2,y2,z2,c2, rad;
  1226. {
  1227.     register short __huge *dold, __huge *dptr;
  1228.     register Pixel __huge *fold;
  1229.  
  1230.     register Long offset,temp,end;
  1231.     register int inten,zrate,absx;
  1232.     register int wide,depth;
  1233.     register int ix,iy,ax,ay;
  1234.     register int lx,ly,lz;
  1235.     register int dx,dy,dz;
  1236.  
  1237.     lx = x2-x1;
  1238.     ly = y2-y1;
  1239.     lz = z2-z1;
  1240.  
  1241.     if( ly>0 ) { ay = ly; iy = 1; }
  1242.     else { ay = -ly; iy = -1; }
  1243.     if( lx>0 ) { ax = lx; ix = 1; }
  1244.     else { ax = -lx; ix = -1; }
  1245.     zrate = lz/MaxFun(ax,ay);
  1246.  
  1247.  
  1248.     end = (Long)ly*XRange+lx;
  1249.     temp = (Long)y1*XRange+x1;
  1250.     fold = FBuffer+temp;
  1251.     dold = DBuffer+temp;
  1252.  
  1253.     ArcAcPtr = ArcAc;
  1254.     ArcDnPtr = ArcDn;
  1255.  
  1256.     temp = (Long)-(rad*XRange);
  1257.     for( dy= -rad; dy<=rad; dy++ )
  1258.     {   wide = LookUp[rad][AbsFun(dy)];
  1259.  
  1260.         for( dx= -wide; dx<=wide; dx++ )
  1261.         {   absx = AbsFun(dx);
  1262.             dz = LookUp[wide][absx];
  1263.             CalcInten(dz);
  1264.             if( inten>0 )
  1265.             {   inten = (int)((inten*ColConst[rad])>>ColBits);
  1266.             } else inten = 0;
  1267.             offset = temp+dx;
  1268.  
  1269.             if( XValid(x1+dx) && YValid(y1+dy) )
  1270.             {   dptr = dold+offset; depth = z1+dz;
  1271.                 SETPIXEL(dptr,fold+offset,depth,Lut[c1+inten]);
  1272.             }
  1273.  
  1274.             if( XValid(x2+dx) && YValid(y2+dy) )
  1275.             {   dptr = dold+(offset+end); depth = z2+dz;
  1276.                 SETPIXEL(dptr,fold+(offset+end),depth,Lut[c2+inten]);
  1277.             }
  1278.  
  1279. #ifndef PIXUTILS
  1280.             k1 = AbsFun(dx+ix); 
  1281.             k2 = AbsFun(dx-ix);
  1282.  
  1283.             if( ((k1>wide)||(dz>=LookUp[wide][k1]-zrate)) &&
  1284.                 ((k2>wide)||(dz>LookUp[wide][k2]+zrate)) )
  1285. #endif
  1286.             {   ArcAcPtr->offset = offset; ArcAcPtr->inten = inten;
  1287.                 ArcAcPtr->dx=dx; ArcAcPtr->dy=dy; ArcAcPtr->dz=dz;
  1288.                 ArcAcPtr++;
  1289.             }
  1290.  
  1291. #ifndef PIXUTILS
  1292.             k1 = AbsFun(dy+iy);
  1293.             k2 = AbsFun(dy-iy);
  1294.  
  1295.             high = LookUp[rad][absx];
  1296.             if( ((k1>high)||(dz>=LookUp[LookUp[rad][k1]][absx]-zrate)) &&
  1297.                 ((k2>high)||(dz>LookUp[LookUp[rad][k2]][absx]+zrate)) )
  1298. #endif
  1299.             {   ArcDnPtr->offset = offset; ArcDnPtr->inten = inten;
  1300.                 ArcDnPtr->dx=dx; ArcDnPtr->dy=dy; ArcDnPtr->dz=dz;
  1301.                 ArcDnPtr++;
  1302.             }
  1303.         }
  1304.         temp += XRange;
  1305.     }
  1306. }
  1307.  
  1308.  
  1309. void DrawCylinder( x1,y1,z1,c1,x2,y2,z2,c2,rad )
  1310.     int x1,y1,z1,c1, x2,y2,z2,c2, rad;
  1311. {
  1312.     register short __huge *dbase;
  1313.     register Pixel __huge *fbase;
  1314.  
  1315.     register int zrate,zerr,ystep,err;
  1316.     register int ix,iy,ax,ay;
  1317.     register int lx,ly,lz;
  1318.     register int mid,tmp;
  1319.     register Long temp;
  1320.  
  1321.  
  1322.     /* Trivial Case */
  1323.     if( (x1==x2) && (y1==y2) )
  1324.     {   if( z1>z2 )
  1325.         {      DrawSphere(x1,y1,z1,rad,c1);
  1326.         } else DrawSphere(x2,y2,z2,rad,c2);
  1327.         return;
  1328.     }
  1329.  
  1330.     if( z1<z2 )
  1331.     {   tmp=x1; x1=x2; x2=tmp;
  1332.         tmp=y1; y1=y2; y2=tmp;
  1333.         tmp=z1; z1=z2; z2=tmp;
  1334.         tmp=c1; c1=c2; c2=tmp;
  1335.     }
  1336.  
  1337.     DrawCylinderCaps(x1,y1,z1,c1,x2,y2,z2,c2,rad);
  1338.  
  1339.     lx = x2-x1;
  1340.     ly = y2-y1;
  1341.     lz = z2-z1;
  1342.  
  1343.     if( ly>0 ) { ystep = XRange; ay = ly; iy = 1; }
  1344.     else {   ystep = -XRange; ay = -ly; iy = -1; }
  1345.     if( lx>0 ) { ax = lx; ix = 1; }
  1346.     else { ax = -lx; ix = -1; }
  1347.     zrate = lz/MaxFun(ax,ay);
  1348.  
  1349.     temp = (Long)y1*XRange+x1;
  1350.     fbase = FBuffer+temp;
  1351.     dbase = DBuffer+temp;
  1352.  
  1353.     if( ax>ay )
  1354.     {   lz -= ax*zrate;
  1355.         zerr = err = -(ax>>1);
  1356.  
  1357.         if( c1 != c2 )
  1358.         {   mid = (x1+x2)>>1;
  1359.             while( x1!=mid )
  1360.             {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ax; z1--; }
  1361.                 fbase+=ix; dbase+=ix; x1+=ix;
  1362.                 if( (err+=ay)>0 )
  1363.                 {   fbase+=ystep; dbase+=ystep; err-=ax;
  1364.                        DrawArcDn(dbase,fbase,z1,c1);
  1365.                 } else DrawArcAc(dbase,fbase,z1,c1);
  1366.             }
  1367.         }
  1368.  
  1369.         while( x1!=x2 )
  1370.         {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ax; z1--; }
  1371.             fbase+=ix; dbase+=ix; x1+=ix;
  1372.             if( (err+=ay)>0 )
  1373.             {   fbase+=ystep; dbase+=ystep; err-=ax;
  1374.                    DrawArcDn(dbase,fbase,z1,c2);
  1375.             } else DrawArcAc(dbase,fbase,z1,c2);
  1376.         }
  1377.     } else /*ay>=ax*/
  1378.     {   lz -= ay*zrate;
  1379.         zerr = err = -(ay>>1);
  1380.  
  1381.         if( c1 != c2 )
  1382.         {   mid = (y1+y2)>>1;
  1383.             while( y1!=mid )
  1384.             {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ay; z1--; }
  1385.                 fbase+=ystep; dbase+=ystep; y1+=iy;
  1386.                 if( (err+=ax)>0 )
  1387.                 {   fbase+=ix; dbase+=ix; err-=ay; 
  1388.                        DrawArcAc(dbase,fbase,z1,c1);
  1389.                 } else DrawArcDn(dbase,fbase,z1,c1);
  1390.             }
  1391.         }
  1392.  
  1393.         while( y1!=y2 )
  1394.         {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ay; z1--; }
  1395.             fbase+=ystep; dbase+=ystep; y1+=iy;
  1396.             if( (err+=ax)>0 )
  1397.             {   fbase+=ix; dbase+=ix; err-=ay; 
  1398.                    DrawArcAc(dbase,fbase,z1,c2);
  1399.             } else DrawArcDn(dbase,fbase,z1,c2);
  1400.         }
  1401.     }
  1402. }
  1403.  
  1404.  
  1405. static int TestCylinder( x1,y1,z1,x2,y2,z2,rad )
  1406.     int x1,y1,z1,x2,y2,z2,rad;
  1407. {
  1408.     register int tmp1, tmp2;
  1409.  
  1410.     if( UseSlabPlane )
  1411.         if( (z1+rad>SlabValue) || (z2+rad>SlabValue) )
  1412.             return(False);
  1413.  
  1414.     ClipStatus = False;
  1415.  
  1416.     tmp1 = x1+rad;  tmp2 = x2+rad;
  1417.     if( (tmp1<0) && (tmp2<0) )
  1418.         return( False );
  1419.     if( (tmp1>=XRange) || (tmp2>=XRange) )
  1420.         ClipStatus = True;
  1421.  
  1422.     tmp1 = x1-rad;  tmp2 = x2-rad;
  1423.     if( (tmp1>=XRange) && (tmp2>=XRange) )
  1424.         return( False );
  1425.     if( (tmp1<0) || (tmp2<0) )
  1426.         ClipStatus = True;
  1427.  
  1428.     tmp1 = y1+rad;  tmp2 = y2+rad;
  1429.     if( (tmp1<0) && (tmp2<0) )
  1430.         return( False );
  1431.     if( (tmp1>=YRange) || (tmp2>=YRange) )
  1432.         ClipStatus = True;
  1433.  
  1434.     tmp1 = y1-rad;  tmp2 = y2-rad;
  1435.     if( (tmp1>=YRange) && (tmp2>=YRange) )
  1436.         return( False );
  1437.     if( (tmp1<0) || (tmp2<0) )
  1438.         ClipStatus = True;
  1439.  
  1440.     return( True );
  1441. }
  1442.  
  1443.  
  1444.  
  1445. static void ClipArcAc(dbase,fbase,x,y,z,c)
  1446.     register short __huge *dbase;
  1447.     register Pixel __huge *fbase;
  1448.     register int x,y,z,c;
  1449. {
  1450.     register ArcEntry __far *ptr;
  1451.     register short __huge *dptr;
  1452.     register short depth;
  1453.     register int temp;
  1454.  
  1455.     ptr = ArcAc;
  1456.     while( (temp=y+ptr->dy) < 0 )
  1457.         if( ++ptr == ArcAcPtr )
  1458.             return;
  1459.  
  1460.     while( (temp<YRange) && (ptr<ArcAcPtr) )
  1461.     {   temp = x+ptr->dx;
  1462.         if( XValid(temp) )
  1463.         {   dptr = dbase+ptr->offset;  depth = ptr->dz+z;
  1464.             SETPIXEL(dptr,fbase+ptr->offset,depth,Lut[ptr->inten+c]);
  1465.         }
  1466.         ptr++;
  1467.         temp = y+ptr->dy;
  1468.     }
  1469. }
  1470.  
  1471. static void ClipArcDn(dbase,fbase,x,y,z,c)
  1472.     register short __huge *dbase;
  1473.     register Pixel __huge *fbase;
  1474.     register int x,y,z,c;
  1475. {
  1476.     register ArcEntry __far *ptr;
  1477.     register short __huge *dptr;
  1478.     register short depth;
  1479.     register int temp;
  1480.  
  1481.     ptr = ArcDn;
  1482.     while( (temp=y+ptr->dy) < 0 )
  1483.         if( ++ptr == ArcDnPtr )
  1484.             return;
  1485.  
  1486.     while( (temp<YRange) && (ptr<ArcDnPtr) )
  1487.     {   temp = x+ptr->dx;
  1488.         if( XValid(temp) )
  1489.         {   dptr = dbase+ptr->offset;  depth = ptr->dz+z;
  1490.             SETPIXEL(dptr,fbase+ptr->offset,depth,Lut[ptr->inten+c]);
  1491.         }
  1492.         ptr++;
  1493.         temp = y+ptr->dy;
  1494.     }
  1495. }
  1496.  
  1497.  
  1498. void ClipCylinder( x1,y1,z1,c1,x2,y2,z2,c2,rad )
  1499.     int x1,y1,z1,c1, x2,y2,z2,c2, rad;
  1500. {
  1501.     register short __huge *dbase;
  1502.     register Pixel __huge *fbase;
  1503.  
  1504.     register int zrate,zerr,ystep,err;
  1505.     register int ix,iy,ax,ay;
  1506.     register int lx,ly,lz;
  1507.     register int mid,tmp;
  1508.     register Long temp;
  1509.  
  1510.     /* Visibility Tests */
  1511.     if( !TestCylinder(x1,y1,z1,x2,y2,z2,rad) )
  1512.         return;
  1513.  
  1514.     if( !ClipStatus )
  1515.     {   DrawCylinder(x1,y1,z1,c1,x2,y2,z2,c2,rad);
  1516.         return;
  1517.     }
  1518.  
  1519.     /* Trivial Case */
  1520.     if( (x1==x2) && (y1==y2) )
  1521.     {   if( z1>z2 )
  1522.         {      ClipSphere(x1,y1,z1,rad,c1);
  1523.         } else ClipSphere(x2,y2,z2,rad,c2);
  1524.         return;
  1525.     }
  1526.  
  1527.     if( z1<z2 )
  1528.     {   tmp=x1; x1=x2; x2=tmp;
  1529.         tmp=y1; y1=y2; y2=tmp;
  1530.         tmp=z1; z1=z2; z2=tmp;
  1531.         tmp=c1; c1=c2; c2=tmp;
  1532.     }
  1533.  
  1534.     DrawCylinderCaps(x1,y1,z1,c1,x2,y2,z2,c2,rad);
  1535.  
  1536.     lx = x2-x1;
  1537.     ly = y2-y1;
  1538.     lz = z2-z1;
  1539.  
  1540.     if( ly>0 ) { ystep = XRange; ay = ly; iy = 1; }
  1541.     else {   ystep = -XRange; ay = -ly; iy = -1; }
  1542.     if( lx>0 ) { ax = lx; ix = 1; }
  1543.     else { ax = -lx; ix = -1; }
  1544.     zrate = lz/MaxFun(ax,ay);
  1545.  
  1546.     temp = (Long)y1*XRange+x1;
  1547.     fbase = FBuffer+temp;
  1548.     dbase = DBuffer+temp;
  1549.  
  1550.     if( ax>ay )
  1551.     {   if( x2<x1 )
  1552.         {   tmp = c1;
  1553.             c1 = c2;
  1554.             c2 = tmp;
  1555.         }
  1556.         lz -= ax*zrate;
  1557.         zerr = err = -(ax>>1);
  1558.         mid = (x1+x2)/2;
  1559.  
  1560.         while( x1!=x2 )
  1561.         {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ax; z1--; }
  1562.             fbase+=ix; dbase+=ix; x1+=ix;
  1563.             if( (err+=ay)>0 )
  1564.             {   fbase += ystep;  err -= ax;
  1565.                 dbase += ystep;  y1 += iy;
  1566.                    ClipArcDn(dbase,fbase,x1,y1,z1,(x1<mid?c1:c2));
  1567.             } else ClipArcAc(dbase,fbase,x1,y1,z1,(x1<mid?c1:c2));
  1568.         }
  1569.     } else /*ay>=ax*/
  1570.     {   if( y2<y1 )
  1571.         {   tmp = c1;
  1572.             c1 = c2;
  1573.             c2 = tmp;
  1574.         }
  1575.         lz -= ay*zrate;
  1576.         zerr = err = -(ay>>1);
  1577.         mid = (y1+y2)/2;
  1578.  
  1579.         while( y1!=y2 )
  1580.         {   z1 += zrate;  if( (zerr-=lz)>0 ) { zerr-=ay; z1--; }
  1581.             fbase+=ystep; dbase+=ystep; y1+=iy;
  1582.             if( (err+=ax)>0 )
  1583.             {   fbase += ix;  err -= ay;
  1584.                 dbase += ix;  x1 += ix; 
  1585.                    ClipArcAc(dbase,fbase,x1,y1,z1,(y1<mid?c1:c2));
  1586.             } else ClipArcDn(dbase,fbase,x1,y1,z1,(y1<mid?c1:c2));
  1587.         }
  1588.     }
  1589. }
  1590.  
  1591.  
  1592. void SetFontSize( size )
  1593.     int size;
  1594. {
  1595.     register int count;
  1596.     register int i;
  1597.  
  1598.     count = 0;
  1599.     for( i=0; i<23; i++ )
  1600.     {   FontDimen[i] = count>>4;
  1601.         count += size;
  1602.     }
  1603.     FontSize = size;
  1604. }
  1605.  
  1606.  
  1607. void DisplayString( x, y, z, label, col )
  1608.     int x, y, z;  char *label;  int col;
  1609. {
  1610.     register int sx,sy;
  1611.     register int ex,ey;
  1612.     register char *ptr;
  1613.     register int high;
  1614.  
  1615.  
  1616.     high = (FontSize*3)>>1;
  1617. #ifdef INVERT
  1618.     if( ((y+high)<0) || (y>=YRange) ) return;
  1619. #else
  1620.     if( (y<0) || ((y-high)>=YRange) ) return;
  1621. #endif
  1622.  
  1623.     while( (x<=-FontSize) && *label )
  1624.     {   x += FontSize;  label++;
  1625.     }
  1626.  
  1627.     while( *label && (x<XRange) )
  1628.     {  ptr = VectFont[*label-32];
  1629.        while( *ptr )
  1630.        {   /* Uppercase test */
  1631.            if( ptr[0] < 'a' )
  1632.            {   sx = x + FontDimen[ptr[0]-'A'];
  1633.                sy = y + InvertY(FontDimen[ptr[1]-'a']);
  1634.                ptr += 2;
  1635.            } else
  1636.            {   sx = ex;
  1637.                sy = ey;
  1638.            }
  1639.  
  1640.            ex = x + FontDimen[ptr[0]-'a'];
  1641.            ey = y + InvertY(FontDimen[ptr[1]-'a']);
  1642.            if( (ex!=sx) || (ey!=sy) )
  1643.            {   ClipLine(sx,sy,z,ex,ey,z,col);
  1644.            } else ClipPoint(ex,ey,z,col);
  1645.            ptr += 2;
  1646.        }
  1647.        x += FontSize;
  1648.        label++;
  1649.     }
  1650. }
  1651.  
  1652.  
  1653.  
  1654. void InitialisePixUtils()
  1655. {
  1656. #if defined(IBMPC) || defined(APPLEMAC)
  1657.     ArcAc = (ArcEntry __far*)_fmalloc(ARCSIZE*sizeof(ArcEntry));
  1658.     ArcDn = (ArcEntry __far*)_fmalloc(ARCSIZE*sizeof(ArcEntry));
  1659. #endif
  1660.     SplineCount = 5;
  1661.     SetFontSize(8);
  1662. }
  1663.  
  1664.  
  1665.