//MarchCube performs the Marching Cubes algorithm on a single cube
    static void MarchCube(Vector3 pos, float[] cube, List <Vector3> vertList, List <int> indexList, ThreadSafeVoxelisation.Voxelization.AABCGrid grid, Dictionary <string, int> indices, KDTree surface, List <Vector3> parentVerts)
    {
        int   i, j, vert, idx;
        int   flagIndex = 0;
        float offset    = 0.0f;

        ThreadSafeVoxelisation.GridSize gridSize = grid.GetSize();

        Vector3[] edgeVertex = new Vector3[12];

        //Find which vertices are inside of the surface and which are outside
        for (i = 0; i < 8; i++)
        {
            if (cube[i] <= target)
            {
                flagIndex |= 1 << i;
            }
        }

        //Find which edges are intersected by the surface
        int edgeFlags = cubeEdgeFlags[flagIndex];

        //If the cube is entirely inside or outside of the surface, then there will be no intersections
        if (edgeFlags == 0)
        {
            return;
        }

        //Find the point of intersection of the surface with each edge
        for (i = 0; i < 12; i++)
        {
            //if there is an intersection on this edge
            if ((edgeFlags & (1 << i)) != 0)
            {
                offset = GetOffset(cube[edgeConnection[i, 0]], cube[edgeConnection[i, 1]]);

                edgeVertex[i].x = pos.x + (vertexOffset[edgeConnection[i, 0], 0] + offset * edgeDirection[i, 0]);
                edgeVertex[i].y = pos.y + (vertexOffset[edgeConnection[i, 0], 1] + offset * edgeDirection[i, 1]);
                edgeVertex[i].z = pos.z + (vertexOffset[edgeConnection[i, 0], 2] + offset * edgeDirection[i, 2]);
            }
        }

        //Save the triangles that were found. There can be up to five per cube

        /*for (i = 0; i < 5; i++) {
         *  if (triangleConnectionTable[flagIndex, 3 * i] < 0) break;
         *
         *  for (j = 0; j < 3; j++) {
         *      idx = vertList.Count;
         *      vert = triangleConnectionTable[flagIndex, 3 * i + windingOrder[j]];
         *
         *      Vector3 vertex = edgeVertex[vert];
         *      string points = vertex.x + "" + vertex.y + "" + vertex.z;
         *      int index = -1;
         *
         *      bool found = indices.TryGetValue(points, out index);
         *
         *      if (found) {
         *          indexList.Add(index);
         *      } else {
         *          indexList.Add(idx);
         *          vertList.Add(edgeVertex[vert]);
         *          indices.Add(points, idx);
         *      }
         *  }
         *
         * }*/

        for (i = 0; i < 5; i++)
        {
            if (triangleConnectionTable[flagIndex, 3 * i] < 0)
            {
                break;
            }

            idx = vertList.Count;

            for (j = 0; j < 3; j++)
            {
                vert = triangleConnectionTable[flagIndex, 3 * i + j];
                indexList.Add(idx + windingOrder[j]);
                vertList.Add(edgeVertex[vert]);
            }
        }
    }