std::vector<Point> generateEndCap(const Line& targetEdge,const Point& targetEndpoint, const std::vector<Line>& adjacentEdges, const std::vector<Line>& crossSectionLines)
{
    std::vector<Vector> adjacentEdgeVectors(adjacentEdges.size());
    for (int i=0;i<adjacentEdges.size();i++)
    {
        Line oe=adjacentEdges.at(i);
        Point oeEnd=oe.getPointA();
        if (Point::almostEqual(oeEnd, targetEndpoint))
        {
            adjacentEdgeVectors.at(i)=oe.getVector();
        }
        else
        {
            adjacentEdgeVectors.at(i)=oe.getVector()*-1;
        }
    }
    
    std::vector<Plane> endCapPlanes;
    for (int i=0;i<adjacentEdges.size();i++)
    {
        Vector reVector=targetEdge.getVector();
        if (!(Point::almostEqual(targetEdge.getPointA(), targetEndpoint)))
        {
            reVector*=-1;
        }

        Vector oeVector=adjacentEdgeVectors.at(i);
        Vector planeVector1=Vector::cross(reVector, oeVector);
        Vector planeVector2=Vector::angleBisector(reVector, oeVector);
        Point planeVertex1=targetEndpoint;
        Point planeVertex2=planeVertex1+planeVector1;
        Point planeVertex3=planeVertex1+planeVector2;
        
        endCapPlanes.push_back(Plane::planeThroughPoints(planeVertex1, planeVertex2, planeVertex3));
    }
    
    std::vector<Point> endCap(crossSectionLines.size());
    for (int i=0;i<endCap.size();i++)
    {
        Point endCapPoint(0,0,0);
        double dist=100000.0;
        for (int j=0;j<endCapPlanes.size();j++)
        {
            Point testPoint=planeLineIntersection(crossSectionLines.at(i),endCapPlanes.at(j));
            double testDist=Point::distance(testPoint, targetEdge.getMidpoint());
            if (testDist<dist)
            {
                endCapPoint=testPoint;
                dist=testDist;
            }
        }
        endCap.at(i)=endCapPoint;
    }
    
    return endCap;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
uint StructGridCutPlane::polygonise(const Plane& plane, const GridCell& cell, Triangles* triangles)
{
    int cubeindex = 0;
    if (plane.distanceSquared(cell.p[0]) < 0) cubeindex |= 1;
    if (plane.distanceSquared(cell.p[1]) < 0) cubeindex |= 2;
    if (plane.distanceSquared(cell.p[2]) < 0) cubeindex |= 4;
    if (plane.distanceSquared(cell.p[3]) < 0) cubeindex |= 8;
    if (plane.distanceSquared(cell.p[4]) < 0) cubeindex |= 16;
    if (plane.distanceSquared(cell.p[5]) < 0) cubeindex |= 32;
    if (plane.distanceSquared(cell.p[6]) < 0) cubeindex |= 64;
    if (plane.distanceSquared(cell.p[7]) < 0) cubeindex |= 128;

    if (sm_edgeTable[cubeindex] == 0)
    {
        return 0;
    }

    // Compute vertex coordinates on the edges where we have intersections
    if (sm_edgeTable[cubeindex] & 1)    triangles->vertices[0] =   planeLineIntersection(plane, cell.p[0], cell.p[1], cell.s[0], cell.s[1], &triangles->scalars[0] );
    if (sm_edgeTable[cubeindex] & 2)    triangles->vertices[1] =   planeLineIntersection(plane, cell.p[1], cell.p[2], cell.s[1], cell.s[2], &triangles->scalars[1] );
    if (sm_edgeTable[cubeindex] & 4)    triangles->vertices[2] =   planeLineIntersection(plane, cell.p[2], cell.p[3], cell.s[2], cell.s[3], &triangles->scalars[2] );
    if (sm_edgeTable[cubeindex] & 8)    triangles->vertices[3] =   planeLineIntersection(plane, cell.p[3], cell.p[0], cell.s[3], cell.s[0], &triangles->scalars[3] );
    if (sm_edgeTable[cubeindex] & 16)   triangles->vertices[4] =   planeLineIntersection(plane, cell.p[4], cell.p[5], cell.s[4], cell.s[5], &triangles->scalars[4] );
    if (sm_edgeTable[cubeindex] & 32)   triangles->vertices[5] =   planeLineIntersection(plane, cell.p[5], cell.p[6], cell.s[5], cell.s[6], &triangles->scalars[5] );
    if (sm_edgeTable[cubeindex] & 64)   triangles->vertices[6] =   planeLineIntersection(plane, cell.p[6], cell.p[7], cell.s[6], cell.s[7], &triangles->scalars[6] );
    if (sm_edgeTable[cubeindex] & 128)  triangles->vertices[7] =   planeLineIntersection(plane, cell.p[7], cell.p[4], cell.s[7], cell.s[4], &triangles->scalars[7] );
    if (sm_edgeTable[cubeindex] & 256)  triangles->vertices[8] =   planeLineIntersection(plane, cell.p[0], cell.p[4], cell.s[0], cell.s[4], &triangles->scalars[8] );
    if (sm_edgeTable[cubeindex] & 512)  triangles->vertices[9] =   planeLineIntersection(plane, cell.p[1], cell.p[5], cell.s[1], cell.s[5], &triangles->scalars[9] );
    if (sm_edgeTable[cubeindex] & 1024) triangles->vertices[10] =  planeLineIntersection(plane, cell.p[2], cell.p[6], cell.s[2], cell.s[6], &triangles->scalars[10]);
    if (sm_edgeTable[cubeindex] & 2048) triangles->vertices[11] =  planeLineIntersection(plane, cell.p[3], cell.p[7], cell.s[3], cell.s[7], &triangles->scalars[11]);


    // Create the triangles
    memset(triangles->usedVertices, 0, sizeof(triangles->usedVertices));
    const int* triConnects = sm_triTable[cubeindex];
    uint n = 0;
    while (triConnects[n] != -1)
    {
        triangles->triangleIndices[n] = triConnects[n];
        triangles->usedVertices[triConnects[n]] = true;
        n++;
    }

    uint numTriangles = n/3;

    return numTriangles;
}