Abstract:
A system for generating a surface representation from binary sampled data cludes means for iteratively smoothing the surface representation, while constraining the surface to conform to details in the surface of the binary data. Means are also provided to convert the surface representation into a form useful for polygon rendering and for volume rendering, with the subject system useable to generate a smooth surfaces for high quality rendering of objects stored as binary-sampled data.

Description:
FIELD OF INVENTION 
     The field of the invention pertains to computer graphics, volume graphics, and scientific visualization and more specifically to the generation of smooth surface representations for objects stored in a 3D array of binary-sampled data values. 
     BACKGROUND OF THE INVENTION 
     High quality object rendering in computer graphics with shading and surface reflections requires an accurate model of object surfaces and their surface normals. However, when objects are stored in a sampled volume, as in Volume Graphics, such a description of the surface does not exist and the surface must be estimated from the sampled data. There are a number of ways to estimate surfaces from sampled data as described, for example, by A. Kaufman, in &#34;Volume Visualization&#34;, IEEE Computer Society Press, 1991. However, according to this article, it is stated that compared with the results achieved in surface shading, volumetric shading is still rudimentary, exhibiting problems such as accuracy of the normal values, ambiguity, discretization artifacts, and aliasing. The conclusion from this article is that volume visualization still lacks a simple yet general paradigm that provides high fidelity shading for natural appearance and optimal recognition. 
     Volumetric data can be stored as grayscale data, where the sample values take on a range of intensities, or as binary data, where one value is given to points inside an object and a different value is given to points outside the object. Surface estimation is especially hard for binary sampled data because the intensity values change abruptly at object surfaces. These abrupt intensity changes result in high frequency components in the intensity data at object surfaces. High spatial frequencies mean that no sampling rate will be large enough to guarantee that the object and its surface normals can be accurately reconstructed. The result is jagged edges and aliasing artifacts in shaded volume rendered images. 
     Existing rendering methods that estimate surface normals from binary sampled data apply local low-pass filters to the data that remove high spatial frequencies and smooth out jagged binary surfaces. However, there are two problems with this approach. First, in addition to smoothing out the object surface, a low pass filter smoothes out important features such as cracks and thin protrusions. Second, a local filter is often not sufficient to achieve a smooth surface. This is the case, for example, when the surface is at a shallow angle to the sampling grid of the volume or when volumetric data is sampled more frequently within planar slices than between slice planes. In these cases, the object surface appears to be terraced, with planar surface regions separated by sudden elevation changes. Local filters can round out the elevation changes but they do not remove the terraces unless the filter size is as large as the width of the terrace. In medical data, where terraces can be tens of pixels wide, a local filter large enough to remove terraces would also unacceptably smooth out important detail from the rendered image. 
     An alternative to estimating surface normals from volume data during rendering is to reconstruct the surface as a polygonal model and render it using polygon rendering. The most common method for constructing a polygonal model from volumetric data is to use the Marching Cubes algorithm, described in &#34;Marching Cubes: a High Resolution 3D Surface Construction Algorithm&#34; in Proceedings for Siggraph&#39;87, by W. Lorensen and H. Cline. While this algorithm is designed for grayscale data, it can be used to construct surfaces from binary data by low-pass filtering the data and then applying Marching Cubes to the filtered data. This technique is often used to create surface models from binary-segmented medical image data. 
     However, there are two problems with this approach. First, as stated above, the low-pass filtering will not remove terracing artifacts without removing significant detail from the data. Hence, the terracing artifacts show up in the polygonal model or the surface is unduly smoothed. Second, Marching Cubes produces models that contain a very large number of polygons. In order to further smooth out the surface and reduce the number of triangles in the model, the polygonal model can be smoothed and decimated using a variety of triangle reduction methods such as that described in &#34;Decimation of Triangle Meshes&#34;, in Proceedings Siggraph&#39;92, by W. Schroeder, W. Lorensen, and S. Linthicum. However, these methods are applied to the triangle model without considering the original binary data and can result in the elimination of important structural detail from the model. 
     SUMMARY OF THE INVENTION 
     A smooth representation of object surfaces is required in rendering so that objects appear smooth and free from artifacts. This is true both when the object is represented by a triangulated surface model and rendered with polygon rendering, and when the object is stored in a sampled volume and rendered with volume rendering. Unfortunately, when the object originates as a binary-sampled volume, achieving a smooth representation of the object surface is difficult. Binary-sampled volume data does not contain an explicit surface representation and hence the surface must be estimated from the data. However, existing methods for estimating the surface or surface normals from binary data, including local filtering methods and the Marching Cubes algorithm discussed above are subject to aliasing and terracing artifacts or excessive loss of detail. 
     Unlike existing techniques, the subject system creates a smooth surface representation from binary sampled data while faithfully representing details on the surface of the binary object. This is accomplished by fitting an elastic surface net over the surface and relaxing the net to smooth out its shape while constraining the surface net to follow details in the surface of the binary object. 
     More specifically, the first step in initializing an elastic surface net is to locate surface cubes in the volume, where a cube is a cubic region of space between eight neighboring volume elements, four each from two adjacent planes in the data volume. A surface cube is a cube for which at least one of the eight surrounding neighbors lies inside the cube and at least one of the neighbors lies outside of the object. Once surface cubes have been determined, the surface net is initialized by placing nodes at the center of each surface cube and linking nodes that lie in adjacent left, right, top, bottom, front, or back neighboring cubes in the data volume. This creates a so-called surface net of links between the nodes, initially at the centers of the cubes. 
     After the linked surface net has been created, the positions of individual node elements are sequentially moved to reduce the energy in the surface net. This is called relaxing the link net. In one instance, the energy is defined to be the sum of all of the link lengths and the surface net energy is reduced by moving elements to shorten the lengths of the links between nodes. 
     The movement of any node in the surface net is restricted so that it cannot move outside of the original surface cube containing that node, and it is this constraint which preserves the original structure during the smoothing process. This constrained relaxation is performed iteratively until the total energy in the surface net is minimized or until some user defined stopping criterion is met. 
     After the surface net has been smoothed in this manner, the result is a smooth representation of the binary object surface that is faithful to the original binary segmentation. The surface net representation can be converted into a form useful for polygon rendering by triangulating the surface net. Because of the existence of links between neighboring nodes, this triangulation simply involves adding selective additional links between diagonal elements in the surface net. 
     Alternatively, the surface net representation can be converted into a form useful for volume rendering. Such a form is a discrete map of distance values, as described in &#34;Using Distance Maps for Accurate Surface Representation in Sampled Volumes&#34;, in Proceedings 1998 Symposium on Volume Visualization, by S. Gibson, where the closest distance to the surface is stored at each sample point in the distance map and the distance map is used to estimate surface normals during rendering. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     These and other features of the Subject Invention will be better understood in relation to the Detailed Description in conjunction with the Drawings, of which: 
     FIGS. 1A, 1B, 1C and 1D are respectively photographs of image renderings before and after the application of the subject smoothing techniques, showing the elimination of aliasing and terracing artifacts for a sphere and the bones in a knee joint without sacrifice of necessary detail; 
     FIG. 2 is a diagrammatic representation of a generalized system for generating smooth surfaces from binary sampled data; 
     FIG. 3 is a flow chart of the surface net process for creating a smooth surface from binary sampled data and for generating a sampled volumetric distance map for volume rendering or for generating a polygonal model for polygon rendering; 
     FIG. 4A is a diagrammatic representation of a 3D voxel lattice of sampled data points and a single surface cube from the lattice; 
     FIG. 4B is a diagrammatic representation of a surface cube of the voxel lattice showing an internal node; 
     FIG. 5 is a diagrammatic representation of the initial configuration of a 2D surface net that has been fit around a 2D binary-sampled object; 
     FIGS. 6A-6C are diagrammatic representations of the progressive relaxation of a 2D surface net around a portion of a 2D binary sampled object through node movement. 
     FIG. 7 is a diagrammatic representation of a relaxed surface net around a 2D object, illustrating the application of constraints to node positions; 
     FIGS. 8A and 8B are diagrammatic representations of the relaxation of a portion of a 3D surface net from an initial surface net to a relaxed surface net; 
     FIGS. 9A and 9B are diagrammatic representations of the triangulation of a surface net through the provision of new links as shown in FIG. 9B for the triangle surface net of FIG. 9A; and, 
     FIG. 10 is a diagrammatic representation of the calculation of a distance map from a triangulated surface net. 
    
    
     DETAILED DESCRIPTION 
     Referring now to FIGS. 1A-1D, what can be seen is that the aliasing terracing artifacts associated with prior methods of volume rendering objects stored as binary-sampled data is severe, whereas the smoothing associated with the subject technique not only smoothes the surfaces, but also, as can be seen in FIG. 1D, preserves the original surface detail. As will be seen, this is accomplished by first providing a surface net and then relaxing the net, but only insofar as certain constraints put in place to preserve detailed surface structure are not violated. In one embodiment the constraint is that a node within a surface cube cannot be moved outside the cube during the relaxation process. 
     Referring now to FIG. 2, the subject invention comprises a method for using a computer 10 to convert binary volumetric data 12 into a smooth surface representation that can then be displayed using either polygonal rendering or volume rendering on a display device 14. 
     How this is accomplished in one embodiment is as follows: Referring to the block diagram of FIG. 3 the surface net approach comprises: a procedure 16 for detecting surface cubes in the sampled binary data, where surface cubes are such that the at least one of the eight bounding volume elements is inside the object and at least one of the 8 bounding volume elements is outside of the object; a procedure 18 which places surface nodes at the centers of the detected surface cubes and creates links between adjacent surface nodes; a procedure 20 for relaxing the surface net, where at 22 net node positions are adjusted to reduce the energy in the net by the series of steps depicted at 24, iteratively reducing the link lengths under the constraint that net nodes must remain in their original surface cubes until, as illustrated at 26, the energy is minimized or some other stopping criteria is reached. If at 28 the rendering method is volume rendering as illulstrated at 30, then a procedure 32 triangulates the surface net and calculates the distance from each point in the distance map to the closest point on the triangulated surface and a procedure 34 volume renders the object using the distance map to estimate surface normals. If, as illustrated at 28, the rendering method is polygon rendering 36, then a procedure 38 triangulates the surface net and decimates the triangulated model and a procedure 40 renders the polygonal model. 
     Referring to FIG. 4A, in one representative example, the volumetric object consists of volume elements 42 arranged in a 3D lattice, of which 2 layers are shown at 44. A cube 46 is the space bounded by 8 neighboring volume elements, 4 each from two adjacent layers in the lattice. As shown in FIG. 4B, a surface cube 48 is a cube that contains at least one element 50 that is inside of the object and at least one element 52 that is outside of the object. A surface node 54 is initially placed at the center of each surface cube in the 3D lattice. 
     Referring to FIG. 5, a 2D instance of the surface net shows the nodes 60 and 62 of the surface net, connected by a link 64. The links separate those nodes which are interior nodes of the object from those which are exterior nodes. Here link 64 separates interior node 68 from exterior node 70. 
     Referring to FIG. 6A, a 2D instance of a portion of a surface net shows the interior points 80 exterior points 82, the initial surface net 84 and motions of net nodes 86 and 88 towards lower energy positions 90 and 92. 
     Referring to FIG. 5B, the surface net 94 has been relaxed if from its position in FIG. 6A to a lower energy state and a smoother shape. Referring to FIG. 6C, the surface net 96 has been further relaxed to a smooth shape. 
     Referring to FIG. 7, a 2D instance of the surface net shows portions 100, 102 and 104 of the surface net, where the constraint that nodes in the surface net can not move outside of their original surface cube has limited the smoothing of the object surface. 
     Referring to FIG. 8A, a 3D surface net is initialized so that nodes 110 lie at the centers of surface cubes 112 and links 114 join nodes in adjacent surface cubes. In FIG. 8B surface net 116 is relaxed to reduce the energy in the net by reducing the link lengths, producing a smoother surface net. 
     Referring to FIG. 9A, the surface net is a 3D structures consisting of surface nodes 120 and links 122. Referring to FIG. 9B, the 3D surface is triangulated by joining selective diagonal elements 124 in the surface net to create a mesh of triangular elements. 
     Referring to FIG. 10, values for elements 130 and 132 in the discrete distance map, are set to the minimum distances 134 and 136 respectively from the element to triangular elements 138 and 140 respectively in surface 142. 
     There are several possible methods for defining the energy in the surface net and for relaxing the net to a low energy state. One such method is to define the energy as the sum of the lengths of the links between nodes in the surface net: ##EQU1## where (x 1 , y 1 , z 1 ) and (x 2 , y 2 , z 2 ) are the two nodes joined by the link, and ══ v ══ indicates the magnitude of the vector, v. In this case, the energy is reduced by reducing the lengths of all of the links in the net. One way to achieve at least a local minimum of this energy is to iteratively reduce the link lengths by sequentially moving each node to a position midway between its neighboring nodes. For example, move a node n to the point: ##EQU2## where N n  is the number of neighboring links for node n. 
     Other methods include minimizing the curvature of the surface or a combination of minimizing surface curvature and the link length. 
     A program listing in C is presented hereinafter to illustrate the utilization of the subject system for use in generating a distance map for volume rendering from a binary object representation using a surface net to generate a smooth representation of the object surface. 
     
         __________________________________________________________________________/* surfaceNet3D -- * * Creates a linked net of surface * points that lie originally at the center of volume cubes with * 8 corner points that are not all the same (i.e. at the centers of * volume cubes through which the object surface passes). * * After the net is created, it is relaxed by adjusting net elements * to a position mid-way between neighboring net elements with the * constraint that the net elements must remain inside their original * volume cubes. * * When a satisfactory net is attained, it is used to calculate the 3D * volume distance map by assigning to each element in the distance map * the distance to the closest net point. * * The distance map is encoded into a voxel data structure that has * 1 byte for image intensity and 1 float for distance. The 3D voxel * data is output as out.dat. * * Created by S. Gibson, MERL, January, 1998. *//* includes */#include &lt;stdlib.h&gt;#include &lt;math.h&gt;#include &lt;stdio.h&gt;#include &lt;time.h&gt;#include &lt;strings.h&gt;#include &lt;fcntl.h&gt;#include &lt;X11/Xlib.h&gt;#include &lt;X11/X.h&gt;#include &lt;X11/Xutil.h&gt;#include &lt;X11/Xos.h&gt;#include &lt;X11/Xatom.h&gt;#include &lt;X11/Xmu/StdCmap.h&gt;#include &lt;X11/keysym.h&gt;#include &lt;GL/glx.h&gt;#include &lt;GL/glu.h&gt;/* global definitions */#define WIN.sub.-- WIDTH 512#define WIN.sub.-- HEIGHT 512/* global definitions */#define TRUE 1#define FALSE 0#define byte unsigned char#define PMODE 0644#define OBJECT 7#define MAX.sub.-- ELEMENTS 500000/* type definitions */typedef struct NetElement {int i, j, k;float x, y, z;struct NetElement *left, *right, *top, *bottom, *front, *back;} NetElement;typedef struct VisVoxelStruct {unsigned char value;float dist;} VisVoxel;/* structure definitions */typedef struct VisFVectorStruct {float x, y, z;} VisFVector;/* global variables */int filedes;Window win;Display *dpy;Atom wmDeleteWindow;GLboolean doubleBuffer = GL.sub.-- TRUE, iconic = GL.sub.-- FALSE,keepAspect = GL.sub.-- FALSE;XSizeHints sizeHints = {0};GLdouble bodyWidth = 2.0;int configuration[ ] = (GLX.sub.-- DOUBLEBUFFER, GLX.sub.-- RGBA,GLX.sub.-- DEPTH.sub.-- SIZE, 16, None);/* function definitions */int MkNet(byte *object, NetElement* *occMap, int xSize, int ySize, intzSize,   NetElement *net);void addElement(byte *object, NetElement* *occMap, int xSize, int ySize,         int zSize, NetElement *net, int i, int j, int k,         int iElement);float RelaxNet(NetElement *net, int nElements, float scale);void MkDistMap(byte *object, NetElement *net, int nElements,         int xSize, int ySize, int zSize);void DrawNet(NetElement *net; int nElements, byte *object,       int xSize, int ySize, int zSize);Colormap getTrueColorColorMap(XVisualInfo * vi);void initialize.sub.-- display( );void AddTriangleToDistMap(VisFVector v0, VisFVector v1, VisFVector v2,                 VisVoxel *data, int xSize, int ySize, int zSize,                 int filterSize);int setDist(int i, int j, int k, int xSize, int ySize, int zSize,       VisFVector v0, VisFVector v1, VisFVector v2, VisVoxel       *data);float DistFromTriangle(VisFVector p, VisFVector v0, VisFVector v1,             VisFVector v2);VisFVector CrossProduct (VisFVector v1, VisFVector v2);float DotProduct (VisFVector v1, VisFVector v2);VisFVector ScalarMult(float k, VisFVector v1);VisFVector VectorDiff(VisFVector v1, VisFVector v2);float VectorMag(VisFVector v);VisFVector VectorNorm(VisFVector v);main(int argc, char **argv)byte *data;byte *object;int i, j, k, index;int dataXSize, dataYSize, dataZSize;int xSize, ySize, zSize, size;int minX, maxX, minY, maxY, minZ, maxZ;int nElements, nRelax;int type;int stop;int fd, n;float oldEnergy, energy;float scale;NetElement *net;NetElement* *occMap;         /* contains pointers to net elements */XEvent event, ahead;Window window;/* 1) get the input arguments */if (argc != 6) {fprintf (stdout,         &#34;USAGE: surfaceNet2D filename xSize ySize zSize         type\n&#34;);exit(0)}sscanf(argv[2], &#34;%d&#34;, &amp;dataXSize);sscanf(argv[3], &#34;%d&#34;, &amp;dataYSize);sscanf(argv[4], &#34;%d&#34;, &amp;dataZSize);sscanf(argv[5], &#34;%d&#34;, &amp;type);size = dataXSize * dataYSize * dataZSize;if (!(data = (byte *) malloc(size * sizeof(byte))) ) {fprintf(stderr, &#34;malloc: allocation failure for object \n&#34;);exit(1);}if ((fd = open(argv[1], O.sub.-- RDONLY)) == -1) {fprintf (stderr, &#34;can&#39;t open file %s \n&#34;, argv[1]);exit (1);}if((n = read(fd, data, size * sizeof(byte))) != size * sizeof(byte)) {fprintf (stderr, &#34;error reading data file %s \n&#34;, argv[1]);exit(1);}minX = dataXSize; minY = dataYSize; minZ = dataZSize;maxX = maxY = maxZ = 0;index = 0;for (k = 0; k &lt; dataZSize; k++)for (j = 0; j &lt; dataYSize; j++)for (i = 0; i &lt; dataXSize, i++) {   if (data[index] == type) {     if (i &lt; minX) minX = i;     if (j &lt; minY) minY = j;     if (k &lt; minZ) minZ = k;     if (i &gt; maxX) maxX = i;     if (j &gt; maxY) maxY = j;     if (k &gt; maxZ) maxZ = k;   }   index++;}if ((maxX &lt; minX)∥ (maxY &lt; minY) ∥ (maxZ &lt; minZ)) {fprintf (stderr, &#34;Fatal Error: no object %d present \n&#34;,type);exit(0);}minX -= 5; minY -= 5; minZ -= 5;maxX += 5; maxY += 5; maxZ += 5;xSize = maxX - minX + 1;ySize = maxY - minY + 1;zSize = maxZ - minZ + 1;size = xSize*ySize*zSize;if (!(object = (byte *) malloc(size * sizeof(byte))) ) {fprintf(stderr, &#34;malloc: allocation failure for object \n&#34;);exit(1);}for (i = 0; i &lt; size; i++) {object[i] = 0;}for (k = minZ+5; k &lt;= maxZ-5; k++) {for (j = minY+5; j &lt;= maxY-5; j++)for (i = minX+5; i &lt;= maxX-5; i++) {   if (data[i + j*dataXSize + k*dataXSize*dataYSize] == type) {     index = i-minX + (j-minY)*xSize + (k-minZ) *xSize*ySize;     object[index] = 1;   }}}free (data);if (!(net = (NetElement *) malloc(MAX.sub.-- ELEMENTS *sizeof(NetElement))) ) {fprintf(stderr, &#34;malloc: allocation failure for surface net\n&#34;);exit(1);}if (!(occMap = (NetElement **) malloc(size*sizeof(NetElement*))) ( {fprintf(stderr, &#34;malloc: allocation failure for surface net\n&#34;);exit(1);}for (i = 0; i &lt; size; i++)occMap[i] = NULL;for (i = 0; i &lt; MAX.sub.-- ELEMENTS; i++) {net[i].left = net[i].right = net[i].top = net[i].bottom =net[i].front = net[i].back = NULL;}nElements = MkNet(object, occMap, xSize, ySize, zSize, net);initialize.sub.-- display ( );glClear(GL.sub.-- COLOR.sub.-- BUFFER.sub.-- BIT);DrawNet(net, nElements, object, xSize, ySize, zSize);scale = 1.0;stop = FALSE;while (stop == FALSE) {xNextEvent[dpy, &amp;event);switch (event.type) {case KeyPress:if ((XLookupKeysym((XKeyEvent *) &amp; event, 0) == XK.sub.-- Return)) {   energy = RelaxNet(net, nElements, scale);   glClear (GL.sub.-- COLOR.sub.-- BUFFER.sub.-- BIT);   DrawNet(net, nElements, object, xSize, ySize, zSize);}else if ((XLookupKeysym((XKeyEvent *) &amp; event, 0) == XK.sub.-- Escape)){   fprintf (stderr, &#34;exit relaxation \n&#34;);   exit(0);}else if ((XLookupKeysym((XKeyEvent *) &amp; event, 0) == XK.sub.-- s)) {   stop = TRUE;}break;}}MkDistMap(object, net, nElements, xSize, ySize, zSize);free(object);free(net);free(occMap);}/* MkNet -- * * Creates net of linked elements on the surface of a 3D bitmap. Eachelement * contains an index position, an (x, y, z) position and pointers topossible * neighboring elements. These pointers will be NULL if thecorresponding * neighbor is not an surface element or the address of the appropriate * surface neighbor. * Surface elements lie in cubes with at least one different cornervalue. * Original element positions are at the center of the indexed surface * cube. */int MkNet(byte *object, NetElement* *occMap, int xSize, int ySize, intzSize,       NetElement *net)int i, j, k, index;int found = 0;int nElements = 0;for (k = 0; k &lt; zSize-1; k++) {for (j = 0; j &lt; ySize-1; j++) {for (i = 0; i &lt; xSize-1; i++) {   index = i + j*xSize +k*xSize*ySize;   if (!((object[index] == object[index+1]) &amp;&amp;           (object[index] == object[index+xsize]) &amp;&amp;           (object[index] == object[index+1+xSize]) &amp;&amp;           (object[index] == object[index+xSize*ySize]) &amp;&amp;           (object[index] == object[index+1+xSize*ySize]) &amp;&amp;           (object[index] == object[index+xSize+xSize*ySize]) &amp;&amp;           (object[index] == object[index+1+xSize+xSize*ySize]))) {   addElement(object, occMap, xSize, ySize, zSize, net, i, j, k,                 nElements);   nElements++;   if (nElements &gt;= MAX.sub.-- ELEMENTS) {     fprintf (stderr, &#34;Warning max elements exceeded     \n&#34;);     i = xSize; j = ySize; k = zSize;   }}}}}for (i = 0; i &lt; nElements; i++) {index = net[i].i + net[i].j*xSize + net[i].k*xSize*ySize;if ((net[i].i &lt; xSize-1) &amp;&amp; (occMap[index+1] != NULL))net[i].right = occMap[index+1];if ((net[i].i &gt; 0) &amp;&amp; (occMap[index-1] != NULL))net[i].left = occMap[index-1];if ((net[i].j &lt; ySize-1) &amp;&amp; (occMap[index+xSize] != NULL))net[i].top = occMap[index+xSize];if ((net[i].j &gt; 0) &amp;&amp; (occMap[index-xSize] != NULL))net[i].bottom = occMap[index-xSize];if ((net[i].k &lt; zSize-1) &amp;&amp; (occMap[index+xSize*ySize] != NULL))net[i].back = occMap[index+xSize*ySize];if ((net[i].k &gt;0) &amp;&amp; (occMap[index-xSize*ySize] != NULL))net[i].front = occMap[index-xSize*ySize];}return nElements;}/* addElement -- * * Recursively add new elements until list is filled. */void addElement(byte *object, NetElement* *occMap, int xSize, int ySize,         int zSize, NetElement *net, int *i, int j, int k,         int iElement){net[iElement].i = i;net[iElement].j = j;net[iElement].k = k;net[iElement].x = (float) (i + 0.5);net[iElement].y = (float) (j + 0.5);net[iElement].z = (float) (k + 0.5);occMap[i + j*xSize + k*xSize*ySize] = &amp;(net[iElement]);return;}/* RelaxNet -- * * Relaxes the positions of net elements so that the total energy of thenet * is reduced but elements remain within their original indexed surface * squares. */float RelaxNet(NetElement *net, int-nElements, float scale){int i, nNeighbors;float x, y, z;float dx, dy, dz;float energy = 0;for (i = 0; i &lt; nElements; i++) {nNeighbors = 0;x = y = z = 0;if (net[i].left != NULL) {x += net[i].left-&gt;x;y += net[i].left-&gt;y;z += net[i].left-&gt;z;nNeighbors++;}if (net[i].right != NULL) {x += net[i].right-&gt;x;y += net[i].right-&gt;y;z += net[i].right-&gt;z;nNeighbors++;}if (net[i].top != NULL) {x += net[i].top-&gt;x;y += net[i].top-&gt;y;z += net[i].top-&gt;z;nNeighbors++;}if (net[i].bottom != NULL) {x += net[i].bottom-&gt;x;y += net[i],bottom-&gt;y;z += net[i],bottom-&gt;z;nNeighbors++;}if (net[i].front != NULL) {x += net[i].front-&gt;x;y += net[i].front-&gt;y;z += net[i].front-&gt;z;nNeighbors++;}if (net[i].back != NULL) {x += net[i].back-&gt;x;y += net[i].back-&gt;y;z += net[i].back-&gt;z;nNeighbors++;}if (nNeighbors &gt; 0) {dx = x / (float) nNeighbors - net [i].x;dy = y / (float) nNeighbors - net[i].y;dz = z / (float) nNeighbors - net[i].z;net[i].x += scale * dx;net[i].y += scale * dy;net[i].z += scale * dz;if (net[i].x &lt; net[i].i) {   net[i].x = net[i].i;}else if (net[i].x &gt; net[i].i + 1) {   net[i].x = net[i].i +1;}if (net[i].y &lt; net[i].j) {   net [i].y = net[i].j;}else if (net[i].y &gt; net[i].j + 1) {   net[i].y = net[i].j + 1;}if (net[i].z &lt; net[i].k) {   net[i].z = net[i].k;}else if (net[i].z &gt; net[i].k + 1) {   net[i].z = net[i].k + 1;}}energy += dx*dx + dy*dy + dz*dz;}return energy;}/* MkDistMap -- * * Makes a distance map of the same dimension as the original data * using the surface net and a modified central-point distance filter, * fills interior points with 1&#39;s and writes the voxel data to out.dat. * */void MkDistMap(byte *object, NetElement *net, int nElements,           int xSize, int ySize, int zSize){byte *distImg;int i, j, k, index;int ii, jj, kk;int minI, maxI, minJ, maxJ, minK, maxK;int dataI, dataJ, dataK;int iElement;int planeSize, size;int filterSize = 5;int fd, n;int nTriangles;double temp, x, y, z;float *dist;VisVoxel *data;VisFVector v0, v1, v2;planeSize = xSize*ySize;size = xSize*ySize*zSize;if (! (data = (VisVoxel *) malloc(size * sizeof(VisVoxel))) ) {fprintf(stderr, &#34;malloc: allocation failure for output voxel data\n&#34;);exit(1):}if (! (dist = (float *) malloc(size * sizeof(float))) ) {fprintf(stderr, &#34;malloc: allocation failure for distance data\n&#34;);exit(1);}if (! (distImg = (byte *) malloc(size * sizeof(byte))) ) {fprintf(stderr, &#34;malloc: allocation failure for distance data\n&#34;);exit(1);}index = 0;for (k = 0; k &lt; zSize; k++)for (j = 0; j &lt; ySize; j++)for (i = 0; i &lt; xSize; i++) {   if (object[index] == 1) {     data[index].dist = filterSize;     data[index].value = 1;   }   else {     data[index].dist = -filterSize;     data[index].value = 0;   }   index++;}nTriangles = 0;for (i = 0; i &lt; nElements; i++{v0.x = net[i].x;v0.y = net[i].y;v0.z = net[i].z;if (net[i].left != NULL) {v1.x = net[i].left-&gt;x;v1.y = net[i].left-&gt;y;v1.z = net[i].left-&gt;z;if (net[i].top != NULL) {   v2.x = net[i].top-&gt;x;   v2.y = net[i].top-&gt;y;   v2.z = net[i].top-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].bottom != NULL) {   v2.x = net[i].bottom-&gt;x;   v2.y = net[i].bottom-&gt;y;   v2.z = net[i].bottom-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].front != NULL) {   v2.x = net[i].front-&gt;x;   v2.y = net[i].front-&gt;y;   v2.z = net[i].front-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].back != NULL) {   v2.x = net[i].back-&gt;x;   v2.y = net[i].back-&gt;y;   v2.z = net[i].back-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}}if (net[i].right != NULL) {v1.x = net[i].right-&gt;x;v1.y = net[i].right-&gt;y;v1.z = net[i].right-&gt;z;if (net[i].top != NULL) {   v2.x = net[i].top-&gt;x;   v2.y = net[i].top-&gt;y;   v2.z = net[i].top-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].bottom != NULL) {   v2.x = net[i].bottom-&gt;x;   v2.y = net[i].bottom-&gt;y;   v2.z = net[i].bottom-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].front != NULL) {   v2.x = net[i].front-&gt;x;   v2.y = net[i].front-&gt;y;   v2.z = net[i].front-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].back != NULL) {   v2.x = net[i].back-&gt;x;   v2.y = net[i].back-&gt;y;   v2.z = net[i].back-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}}if (net[i].top != NULL) {v1.x = net[i].top-&gt;x;v1.y = net[i].top-&gt;y;v1.z = net[i].top-&gt;z;if (net[i].front != NULL) {   v2.x = net[i].front-&gt;x;   v2.y = net[i].front-&gt;y;   v2.z = net[i].front-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].back !=NULL) {   v2.x = net[i].back-&gt;x;   v2.y = net[i].back-&gt;y;   v2.z = net[i].back-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}}if (net[i].bottom != NULL) {v1.x = net[i].bottom-&gt;x;v1.y = net[i].bottom-&gt;y;v1.z = net[i].bottom-&gt;z;if (net[i].front != NULL) {   v2.x = net[i].front-&gt;x;   v2.y = net[i].front-&gt;y;   v2.z = net[i].front-&gt;z;   AddTriangleToDistMap(v2, v0, v1, data, xSize, ySize, zSize,               filterSize);   nTriangles++;}if (net[i].back != NULL) {   v2.x = net[i].back-&gt;x;   v2.y = net[i].back-&gt;y;   v2.z = net[i].back-&gt;z;   AddTriangleToDistMap(v1, v0, v2, data, xSize, ySize, zSize,               filterSize);   nTriangles++;   }}}/* low pass filter the distance field *//*for (k = 2; k &lt; zSize - 2; k++)for (j = 2; j &lt; ySize - 2; j++)   for (i = 2; i &lt; xSize - 2, i++) {     index = i + j*xSize + k*planeSize;     dist[index] = 0;     for (kk = -2; kk &lt; 2, kk++)       for (jj = -2; jj &lt;= 2, jj++)         for (ii = -2, ii &lt;= 2; ii++) {           dist[index] += data[index + ii + jj*xSize           + kk*planeSize].dist;         }     index++;   }for (k = 1; k &lt; zSize - 1; k++)for (j = 1; j &lt; ySize - 1, j++)   for (i = 1; i &lt; xSize - 1; i++) {     index = i + j *xSize + k*planeSize;     data[index].dist = dist[index];   }if ((fd = creat(&#34;dist.raw&#34;, PMODE)) == -1) {fprintf (stderr, &#34;can&#39;t create file dist.raw \n&#34;);exit(0);}if ((n = write(fd, distImg, xSize*zSize*sizeof(byte))) !=   xSize*zSize*sizeof (byte)) {fprintf(stderr, &#34;cannot write dist image to output file dist.raw\n&#34;);exit(0);}close(fd);/* open output file, write data into output file, close output file */if ((fd = creat(&#34;out.dat&#34;, PMODE)) == -1) {fprintf (stderr, &#34;can&#39;t create file out.dat \n&#34;);exit(0);}if ((n = write(fd, data, size*sizeof(VisVoxel))) != size*sizeof(VisVoxel)) {fprintf(stderr, &#34;cannot write image to output file out.dat\n&#34;);exit(0);}close(fd);free(data);free(dist);free(distImg);return;}/* DrawNet -- * * Draw the elements and links to their neighbors. */void DrawNet(NetElement *net, int nElements, byte *object,           int xSize, int ySize, int zSize){int i, j, k;int nTriangles;float x, y, z, x1, y1, z1, x2, y2, z2;float zOffset;fioat xScale, yScale, zScale;xScale= 1.5 / xSize;yScale= 1.5 / ySize;zScale= 1.5 / zSize;glColor3f(1.0, 0.0, 0.0);glPointSize(1.0);glBegin(GL.sub.-- POINTS);glBegin(GL.sub.-- POINTS);for (i = 0; i &lt; nElements; i++) {if ((net[i].j == 100) ∥ (net[i].j == 105) ∥ (net[i].j== 110)) {   glColor3f(1.0, 0.0, 0.0);   x = (net[i].x - xSize/2.0) * xScale;   y = (net[i].z - zSize/2.0) * zScale;   glVertex2f(x, y);}if (net[i].k == 33) {   glColor3f(1.0, 0.0, 0.0);   zOffset = (net[i].k - zSize/2.0) * zScale;   z = (net[i].z - zSize/2.0) * zScale;   x = (net[i].x - xSize/2.0 + 200*zOffset ) * xScale;   y = (net[i].y - ySize/2.0 + 200*zOffset ) * yScale;   glVertex2f(x, y);}if (net[i].k == 34) {   glColor3f(0.0, 0.0, 1.0);   zOffset = (net[i].k - zSize/2.0) * zScale;   z = (net[i].z - zSize/2.0) * zScale;   x = (net[i].x - xSize/2.0 + 200*zOffset ) * xScale;   y = (net[i].y - ySize/2.0 + 200*zOffset ) * yScale;   glVertex2f(x, y);}if (net[i].k == 32) {   glColor3f(0.0, 1.0, 0.0);   zOffset = (net[i].k - zSize/2.0) * zScale;   z = (net[i].z - zSize/2.0) * zScale;   x = (net[i].x - xSize/2.0 + 200*zOffset ) * xScale;   y = (net[i].y - ySize/2.0 + 200*zOffset ) * yScale;   glVertex2f(x, y);}}glEnd( );if (doubleBuffer)glXSwapBuffers (dpy, win);else glFlush( );return;}/* getTrueColorColorMap -- * * This function gets a TrueColor colormap for the given visual. */ColormapgetTrueColorColorMap (XVisuaInfo * vi){Status              status;XStandardColormap *standardCmaps;Colormap            cmap;int                 i, numCmaps;if (vi-&gt;class != TrueColor) {fprintf (stderr, &#34;fatalError: no support for non-TrueColor visual\n&#34;);exit(0);}status = XmuLookupStandardColormap(dpy, vi-&gt;screen, vi-&gt;visualid,   vi-&gt;depth, XA.sub.-- RGB.sub.-- DEFAULT.sub.-- MAP, /* replace */   False, /* retain */ True);if (status == 1) {   status = XGetRGBColormaps(dpy, RootWindow(dpy, vi-&gt;screen),                   &amp;standardCmaps, &amp;numCmaps, XA.sub.-- RGB.sub.--                   DEFAULT.sub.-- MAP);   if (status == 1)       for (i = 0; i &lt; numCmaps; i++)           if (standardCmaps[i].visualid == vi-&gt;visualid) {               cmap = standardCmaps[i].colormap;               XFree (standardCmaps);               return cmap;}cmap = XCreateColormap(dpy, RootWindow(dpy, vi-&gt;screen),   vi-&gt;visual, AllocNone);   return cmap;}/*initialize.sub.-- display -- * * This function initializes a GL window with a TrueColor colormap. */voidinitialize.sub.-- display( ){XVisualInfo     *vi;Colormap        cmap;XSetWindowAttributes swa;XWMHints        *wmHints;GLXContext      cx;XEvent          event;char            *display = NULL, *geometry = NULL;int             flags, x, y, i;/* open a connection to the X server */dpy = XOpenDisplay(display);if (dpy == NULL) {fprintf (stderr, &#34;fatalError: could not open display \n&#34;);exit(0);}/* make sure OpenGL&#39;s GLX extension supported */if (!glXQueryExtension(dpy, NULL, NULL)) {fprintf (stderr, &#34;fatalError: X server has no OpenGL GLX extension\n&#34;);exit(0);}/* find an appropriate visual and a colormap for it */doubleBuffer = GL.sub.-- FALSE;                        /**** added for single buffering ***/if (!doubleBuffer) goto SingleBufferOverride;vi = glXChooseVisual(dpy, DefaultScreen(dpy), configuration);if (vi == NULL)SingleBufferOverride:vi = glXChooseVisual(dpy, DefaultScreen(dpy), &amp;configuration[1];if (vi == NULL) {fprintf (stderr,       &#34;fatalError: no appropriate RGB visual with depth buffer       \n&#34;};exit(0);}doubleBuffer = GL.sub.-- FALSE;}cmap = getTrueColorColorMap(vi);/* create an OpenGL rendering context */cx = glXCreateContext(dpy, vi, /* no sharing of display lists */ NULL,                 /* direct rendering if possible */ GL.sub.--                 TRUE);if (cx == NULL) {fprintf (stderr, &#34;fatalError: could not create rendering context\n&#34;);exit(0);}/* create an X window with selected visual and right properties */swa.colormap = cmap;swa.border.sub.-- pixel = 0;swa.event.sub.-- mask = ExposureMask | StructureNotifyMask| KeyPressMask |MapNotify | ButtonMotionMask | PointerMotionHintMask| ButtonPressMask |ButtonReleaseMask;win = XCreateWindow(dpy, RootWindow(dpy, vi-&gt;screen),               sizeHints.x, sizeHints.y, WIN.sub.-- WIDTH,               WIN.sub.-- HEIGHT,               0, vi-&gt;depth, InputOutput, vi-&gt;visual,               CWBorderPixel | CWColormap |               CWEventMask, &amp;swa);XSetStandardProperties(dpy, win, &#34;show object derivitives&#34;, &#34;2D demo&#34;,                 None, NULL, 0, &amp;sizeHints);wmHints = XAllocWMHints( );wmHints-&gt;initial.sub.-- state = iconic ? IconicState : NormalState;wmHints-&gt;flags = StateHint;XSetWMHints(dpy, win, wmHints);wmDeleteWindow = XInternAtom(dpy, &#34;WM.sub.-- DELETE.sub.-- WINDOW&#34;,False);XSetWMProtocols(dpy, win, &amp;wmDeleteWindow, 1);/* bind the rendering context to the window */glXMakeCurrent(dpy, win, cx);/* request the X window to be displayed on the screen */fprintf (stderr, &#34;map the window \n&#34;);XMapWindow(dpy, win);/* wait for the window to be mapped */while (1) {XNextEvent(dpy, &amp;event);if (event.type == MapNotify) break;}return;}/**************************************************************************** ** * The following files calculate the distance to a triangle in 3 space* * at local sample points. It is used above to create a distance map* * from the triangles in the surface net.* ** **************************************************************************** *//* DistFromTriangle -- * * For each point in the distance map, calculate the closest distance to * the triangle specified by the 3 verticies in clockwise order. * * If the calculated distance is smaller than the current distance inthe * distance map, update the distance map. * */ /* AddTriangleToDistMap -- * * */voidAddTriangleToDistMap(VisFVector v0, VisFVector v1, VisFVector v2,                 VisVoxel *data, int xSize, int ySize, int zSize,                 int filterSize){int i, j, k, index;int minI, maxI, minJ, maxJ, minK; maxK;int decreaseXDist, decreaseYDist, decreaseZDist;int testYDist, testZDist;float dist;float maxX, minX, maxY, minY, maxZ, minZ;VisFVector p;index = 0;maxX = v0.x;if (v1.x &gt; maxX) maxX = v1.x;if (v2.x &gt; maxX) maxX = v2.x;if (maxX + filterSize &gt; xSize-1)maxI = xSize-1;elsemaxI = (int) (maxX + filterSize);minX = v0.x;if (v1.x &lt; minX) minX = v1.x;if (v2.x &lt; minX) minX = v2.x;if (minX - filterSize &lt; 0)minI = 0;elseminI = (int) (minX - filterSize);maxY = v0.y;if (v1.y &gt; maxY) maxY = v1.y;if (v2.y &gt; maxY) maxY = v2.y;if (maxY + filterSize &gt; ySize-1)maxJ = ySize-1;elsemaxJ = (int) (maxY + filterSize);minY = v0.y;if (v1.y &lt; minY) minY = v1.y;if (v2.y &lt; minY) minY = v2.y;if (minY - filterSize &lt; 0)minJ = 0;elseminJ = (int) (minY - filterSize);maxZ = v0.z;if (v1.z &gt; maxZ) maxZ = v1.z;if (v2.z &gt; maxZ) maxZ = v2.z;if (maxZ + filterSize &gt; zSize-1)max = zSize-1;elsemaxK = (int) (maxZ + filterSize);minZ = v0.z,if (v1.z &lt; minZ) minZ = v1.z;if (v2.z &lt; minZ) minZ = v2.z;if (minZ - filterSize &lt; 0)minK = 0;elseminK = (int) (minZ - filterSize);for (k = (int) minZ; k &lt;= (int) maxZ; k++) {for (j = (int) minY; j &lt;= (int) maxY; j++) {   for (i = (int) minX; i &lt;= (int) maxX; i++) {     setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2, data);   }   i = (int) maxX + 1;   decreaseXDist = TRUE;   while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {     decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,     v2,                   data);     i ++;   }   i = (int 0 minX -1;   decreaseXDist = TRUE;   while ((i &gt;= minI) &amp;&amp; (decreaseXDist ==TRUE)) {     decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,     v2,                   data);     i --;   }}j = (int) maxY + 1;decreaseYDist = TRUE;while ((j &lt;= maxJ) &amp;&amp; (decreaseYDist == TRUE)) {   decreaseYDist = FALSE;   for (i = (int) minX; i &lt;= (int) maxX; i++) {     testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,     data);     if (testYDist ==TRUE) decreaseYDist = TRUE;   }   i = (int) maxX + 1;   decreaseXDist = TRUE;   while ((i &gt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {     decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,     v2,                   data);     i++;   }   i = (int) minX - 1;   decreaseXDist = TRUE;   while ((i &gt;= minI) &amp;&amp; (decreasexDist == TRUE)) {     decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,     v2,                   data);     i --;   }   j++;}j = (intY) minY - 1;decreaseYDist = TRUE;while ((j &gt;= minJ) &amp;&amp; (decreaseYDist == TRUE)) {decreaseYDist = FALSE;for (i = (int) minX; i &lt;= (int) maxX; i++) {   testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testYDist == TRUE) decreaseYDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}j --;}}k = (int) maxZ + 1;decreaseZDist = TRUE;while ((k &lt;= maxK) &amp;&amp; (decreaseZDist == TRUE)) {decreaseZDist = FALSE;for (j = (int) minY; j &lt;= (int) maxY; j++) {for (i = (int) minX; i &gt;= (int) maxX; i++) {   testZDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testZDist == TRUE) decreaseZDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}}j = (int) maxY + 1;decreaseYDist = TRUE;while ((j &lt;= maxJ) &amp;&amp; (decreaseYDist == TRUE)) {decreaseYDist = FALSE;for (i = (int) minX; i &lt;= (int) maxX; i++) {   testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testYDist == TRUE) decreaseYDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i ++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}j++;}j = (int) minY - 1;decreaseYDist = TRUE;while ((j &lt;= minJ) &amp;&amp; (decreaseYDist == TRUE)) {decreaseYDist = FALSE;for (i = (int) minX; i &lt;= (int) maxX; i++) {   testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testYDist == TRUE) decreaseYDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}j--;}k++;}k = (int) minZ - 1;decreaseZDist = TRUE;while ((k &gt;= minK) &amp;&amp; (decreaseZDist == TRUE) {decreaseZDist = FALSE;for (j = (int) minY; j &lt;= (int) maxY; j++) {for (i = (int) minX; i &lt;= (int) maxX; i++) {   testZDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testZDist == TRUE) decreaseZDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}}j = (int) maxY + 1;decreaseYDist = TRUE;while ((j &gt;= minJ) &amp;&amp; (decreaseYDist == TRUE)) {decreaseYDist = FALSE;for (i = (int) minX; i &lt;= (int) maxX; i++) {   testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testYDist == TRUE) decreaseYDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= maxI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX -1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}j++;}j = (int) minY - 1;decreaseYDist = TRUE;while ((j &gt;= minJ) &amp;&amp; (decreaseYDist == TRUE)) {decreaseYDist = FALSE;for(i = (int) minX; i &lt;= (int) maxX; i++) {   testYDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1, v2,   data);   if (testYDist == TRUE) decreaseYDist = TRUE;}i = (int) maxX + 1;decreaseXDist = TRUE;while ((i &lt;= minI) &amp;&amp; (decreaseXDist == TRUE)) {   decreaseXDist = setDist(i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i++;}i = (int) minX - 1;decreaseXDist = TRUE;while ((i &gt;= minI) &amp;&amp; (decreaseXDist ==TRUE)) {   decreaseXDist = setDist (i, j, k, xSize, ySize, zSize, v0, v1,   v2,                   data);   i--;}j--;}k--;}return;}/* setDist -- * */intsetDist(int i, int j, int k, int xSize, int ySize, int zSize,     VisFVector v0, VisFVector v1, VisFVector v2, VisVoxel *data){int index;int decreaseDist = FALSE;float dist;VisFVector p;p.x = i; p.y = j; p.z = k;index = i + j*xSize + k*xSize*ySize;dist = DistFromTriangle(p, v0, v1, v2);if (data[index].value &gt; 0) {if (dist &lt; data[index].dist) {   data[index].dist = dist;   decreaseDist = TRUE;}}else {if (-1*dist &gt; data[index].dist) {   data[index].dist = -1*dist;   decreaseDist = TRUE;}}return decreaseDist;}floatDistFromTriangle(VisFVector p, VisFVector v0, VisFVector v1, VisFVectorv2){int i, j, k;int sign0, sign1, sign2;float d, w;VisFVector n, pl, u;n = VectorNorm(CrossProduct(VectorDiff(v1, v0), VectorDiff(v2, v0)));p1 = VectorDiff(p, ScalarMult(DotProduct(VectorDiff(p, v0), n), n));if (DotProduct(CrossProduct(VectorDiff(p1, v0), VectorDiff(v1, v0)), n)&gt;= 0)sign0 = 1;elsesign0 = 1;if (DotProduct(CrossProduct(VectorDiff(p1, v1),VectorDiff(v2, v1)), n) &gt;=0)sign1 = 1;elsesign1 = -1;if (DotProduct(CrossProduct(VectorDiff(p1, v2), VectorDiff(v0, v2)), n)&gt;= 0)sign2 = 1;elsesign2 = -1;if ((sign0 &lt; 0) &amp;&amp; (sign1 &lt; 0) &amp;&amp; (sign2 &lt; 0)) {d = DotProduct(VectorDiff(p, v0), n);if (d &lt; 0)d *= -1;}else if ((sign0 &lt; 0) &amp;&amp; (sign1 &lt; 0)) {u = VectorNorm(VectorDiff(v0, v2));if (DotProduct(VectorDiff(p, v2), u) &lt; 0}   d = VectorMag(VectorDiff(p, v2));else if (DotProduct(VectorDiff(p, v2), u) &gt; VectorMag(VectorDiff(v0,v2)))   d = VectorMag(VectorDiff(p, v0));else   d = VectorMag(CrossProduct(u, VectorDiff(p, v2)));}else if ((sign1 &lt; 0) &amp;&amp; (sign2 &lt; 0)) {u = VectorNorm(VectorDiff(v1, v0));if (DotProduct(VectorDiff(p, v0), u) &lt; 0)   d = VectorMag(VectorDiff(p, v0));else if (DotProduct(VectorDiff(p, v0), u) &gt; VectorMag(VectorDiff(v1,v0)))   d = VectorMag(VectorDiff(p, v1));else   d = VectorMag(CrossProduct(u, VectorDiff(p, v0)));}else if ((sign2 &lt; 0) &amp;&amp; (sign0 &lt; 0)) {u = VectorNorm(VectorDiff(v2, v1));if (DotProduct(VectorDiff(p, v1), u) &lt; 0)   d = VectorMag(VectorDiff(p, v1));else if (DotProduct(VectorDiff(p, v1), u) &gt; VectorMag(VectorDiff(v2,v1)))   d = VectorMag(VectorDiff(p, v2));else   d = VectorMag(CrossProduct(u, VectorDiff(p, v1)));}else if (sign0 &lt; 0) {d = VectorMag(VectorDiff(p, v2));}else if (sign1 &lt; 0) {d = VectorMag(VectorDiff(p, v0));}else if (sign2 &lt; 0) {d = VectorMag(VectorDiff(p, v1));}return d;}/* CrossProduct -- * * compute cross product of two vectors */VisFVectorCrossproduct (VisFVector v1, VisFVector v2){VisFVector v;v.x = v1.y*v2.z - v1.z*v2.y;v.y = v1.z*v2.x - v1.x*v2.z;v.z = v1.x*v2.y - v1.y*v2.x;return v;}/* DotProduct -- * * compute dot product of two vectors */floatDotProduct(VisFVector v1, VisFVector v2){float f;f = v1.x*v2.x .+ v1.y*v2.y + v1.z*v2.z;return f;}* ScalarMult -- * * multiply vector with a scalar */VisFVectorScalarMult(float k, VisFVector v1){VisFVector v;v.x = k * v1.x;v.y = k * v1.y;v.z = k * v1.z;return v;}/* VectorDiff -- * * subtract two vectors */VisFVectorVectorDiff(VisFVector v1, VisFVector v2){VisFVector v;v.x = v1.x - v2.x;v.y = v1.y - v2.y;v.z = v1.z - v2.z;return v;}/* VectorMag -- * * get magnitude of a vector */floatVectorMag(VisFVector v){float mag;mag = sqrt(DotProduct(v, v));return mag;}/* VectorNorm -- * * normalize a vectorVisFVectorVectorNorm (VisFVector v){float mag;VisFVector vNorm;mag = VectorMag(v);if (mag &gt; 0) {vNorm.x = v.x / mag;vNorm.y = v.y / mag;vNorm.z = v.z / mag;}else {vNorm.x = 0;vNorm.y = 0;vNorm.z = 0;}return vNorm;}__________________________________________________________________________ 
    
     Having above indicated several embodiments of the Subject Invention, it will occur to those skilled in the art that modifications and alternatives can be practices within the spirit of the invention. It is accordingly intended to define the scope of the invention only as indicated in the following claims.