예제 #1
0
def triangulate(a,perim):
    '''
    Recursively tries every triangulation in search a feasible one
        Each layer
            makes a Triangle out of three perimeter portals
            for every feasible way of max-fielding that Triangle
                try triangulating the two perimeter-polygons to the sides of the Triangle

    Returns True if a feasible triangulation has been made in graph a
    '''
    pn = len(perim)
    if pn < 3:
        # Base of recursion
        return True

    try:
        startStackLen = len(a.edgeStack)
    except AttributeError:
        startStackLen = 0
        a.edgeStack = []
    try:
        startTriLen = len(a.triangulation)
    except AttributeError:
        startTriLen = 0
        a.triangulation = []

#    odegrees = [a.out_degree(p) for p in perim
#    order = np.argsort(odegrees)

    # Try all possible first generation triangles with two edges on boundary that both use node i (using i as final vertex will cause no 2 first generation triangles to have same final vertex)
    for i in np.random.permutation(range(0,pn)):
#        print perim
#        print 'using %s as final'%perim[i]
        for j in xrange(TRIES_PER_TRI):
            t0 = Triangle(perim[[i,i-1,(i+1)%pn]],a,True)
            t0.findContents()
#            t0.randSplit() # Split triangle on a random portal
            t0.nearSplit() # Split triangle on the nearest portal
            try:
#                print 'trying to build'
                t0.buildGraph()
            except Deadend as d:
                # TODO when allowing suboptimal plans, this block would be unnecessary if first generation triangles were made in the right order: see Triangle.buildGraph
                # remove the links formed since beginning of loop
                removeSince(a,startStackLen,startTriLen)
#                print 'small fail'
            else:
                # This build was successful. Break from the loop
#                print 'build succeeded'
                break
        else:
            # The loop ended "normally" so this triangle failed
#            print 'big fail'
            continue

#        print 'continuing with',perim[range(i+1-pn,i)]
        if not triangulate(a,perim[range(i+1-pn,i)]): # i+1 through i-1
            # remove the links formed since beginning of loop
            removeSince(a,startStackLen,startTriLen)
            continue

        # This will be a list of the first generation triangles
        a.triangulation.append(t0)

        # This triangle and the ones to its sides succeeded
        return True

    # Could not find a solution
    return False
예제 #2
0
def triangulate(a, perim):
    """
    Recursively tries every triangulation in search a feasible one
        Each layer
            makes a Triangle out of three perimeter portals
            for every feasible way of max-fielding that Triangle
                try triangulating the two perimeter-polygons to the sides of the Triangle

    Returns True if a feasible triangulation has been made in graph a
    """
    pn = len(perim)
    if pn < 3:
        # Base of recursion
        return True

    try:
        startStackLen = len(a.edgeStack)
    except AttributeError:
        startStackLen = 0
        a.edgeStack = []
    try:
        startTriLen = len(a.triangulation)
    except AttributeError:
        startTriLen = 0
        a.triangulation = []

    #    odegrees = [a.out_degree(p) for p in perim
    #    order = np.argsort(odegrees)

    # Try all possible first generation triangles with two edges on boundary that both use node i (using i as final vertex will cause no 2 first generation triangles to have same final vertex)
    for i in np.random.permutation(range(0, pn)):
        #        print perim
        #        print 'using %s as final'%perim[i]
        for j in xrange(TRIES_PER_TRI):
            t0 = Triangle(perim[[i, i - 1, (i + 1) % pn]], a, True)
            t0.findContents()
            #            t0.randSplit() # Split triangle on a random portal
            t0.nearSplit()  # Split triangle on the nearest portal
            try:
                #                print 'trying to build'
                t0.buildGraph()
            except Deadend as d:
                # TODO when allowing suboptimal plans, this block would be unnecessary if first generation triangles were made in the right order: see Triangle.buildGraph
                # remove the links formed since beginning of loop
                removeSince(a, startStackLen, startTriLen)
            #                print 'small fail'
            else:
                # This build was successful. Break from the loop
                #                print 'build succeeded'
                break
        else:
            # The loop ended "normally" so this triangle failed
            #            print 'big fail'
            continue

        #        print 'continuing with',perim[range(i+1-pn,i)]
        if not triangulate(a, perim[range(i + 1 - pn, i)]):  # i+1 through i-1
            # remove the links formed since beginning of loop
            removeSince(a, startStackLen, startTriLen)
            continue

        # This will be a list of the first generation triangles
        a.triangulation.append(t0)

        # This triangle and the ones to its sides succeeded
        return True

    # Could not find a solution
    return False
예제 #3
0
파일: maxfield.py 프로젝트: ayecee/maxfield
def triangulate(a,perim):
    '''
    Recursively tries every triangulation in search a feasible one
        Each layer
            makes a Triangle out of three perimeter portals
            for every feasible way of max-fielding that Triangle
                try triangulating the two perimeter-polygons to the sides of the Triangle

    Returns True if a feasible triangulation has been made in graph a
    '''
    pn = len(perim)
    if pn < 3:
        return True

    try:
        startStackLen = len(a.edgeStack)
    except AttributeError:
        startStackLen = 0
        a.edgeStack = []
    try:
        startTriLen = len(a.triangulation)
    except AttributeError:
        startTriLen = 0
        a.triangulation = []

    # Try all triangles using perim[0:2] and another perim node
    for i in np.random.permutation(range(2,pn)):

        for j in xrange(TRIES_PER_TRI):
            t0 = Triangle(perim[[0,1,i]],a,True)
            t0.findContents()
            t0.randSplit()
            try:
                t0.buildGraph()
            except Deadend as d:
                # remove the links formed since beginning of loop
                removeSince(a,startStackLen,startTriLen)
            else:
                # This build was successful. Break from the loop
                break
        else:
            # The loop ended "normally" so this triangle failed
            continue

        if not triangulate(a,perim[range(1,i   +1   )]): # 1 through i
            # remove the links formed since beginning of loop
            removeSince(a,startStackLen,startTriLen)
            continue

        if not triangulate(a,perim[range(0,i-pn-1,-1)]): # i through 0
           # remove the links formed since beginning of loop
           removeSince(a,startStackLen,startTriLen)
           continue

        # This will be a list of the first generation triangles
        a.triangulation.append(t0)

        # This triangle and the ones to its sides succeeded
        return True

    # Could not find a solution
    return False
예제 #4
0
def triangulate(a, perim):
    '''
    Recursively tries every triangulation in search a feasible one
        Each layer
            makes a Triangle out of three perimeter portals
            for every feasible way of max-fielding that Triangle
                try triangulating the two perimeter-polygons to the sides of the Triangle

    Returns True if a feasible triangulation has been made in graph a
    '''
    pn = len(perim)
    if pn < 3:
        return True

    try:
        startStackLen = len(a.edgeStack)
    except AttributeError:
        startStackLen = 0
        a.edgeStack = []
    try:
        startTriLen = len(a.triangulation)
    except AttributeError:
        startTriLen = 0
        a.triangulation = []

    # Try all triangles using perim[0:2] and another perim node
    for i in np.random.permutation(range(2, pn)):

        for j in xrange(TRIES_PER_TRI):
            t0 = Triangle(perim[[0, 1, i]], a, True)
            t0.findContents()
            t0.randSplit()
            try:
                t0.buildGraph()
            except Deadend as d:
                # remove the links formed since beginning of loop
                removeSince(a, startStackLen, startTriLen)
            else:
                # This build was successful. Break from the loop
                break
        else:
            # The loop ended "normally" so this triangle failed
            continue

        if not triangulate(a, perim[range(1, i + 1)]):  # 1 through i
            # remove the links formed since beginning of loop
            removeSince(a, startStackLen, startTriLen)
            continue

        if not triangulate(a, perim[range(0, i - pn - 1, -1)]):  # i through 0
            # remove the links formed since beginning of loop
            removeSince(a, startStackLen, startTriLen)
            continue

        # This will be a list of the first generation triangles
        a.triangulation.append(t0)

        # This triangle and the ones to its sides succeeded
        return True

    # Could not find a solution
    return False