Esempio n. 1
0
 def get_block_in_sights(self):
     self.block_in_sights = self.sights & self.walls
     self.sights -= self.walls
     
     # duality
     self.merge_nodes = set()
     
     for pos in self.block_in_sights:
         self.merge_nodes |= (set(self.adjacent_blocks(pos, self.sights)) - self.block_in_sights)
     self.merge_nodes -= set(self.vertex.keys())
     
     ## new vertex around blocks
     add_edge = deepcopy(self.edge)
     rm_pair = set()
     
     for pair in self.edge.keys():
         
         nodes_in_edge = set()
         
         for node in self.merge_nodes:
             
             if is_in_line(pair[0], node, pair[1]) and \
             is_within_line(pair[0], node, pair[1]):
                 self.vertex[node] = set(pair)
                 self.vertex[pair[0]].add(node) 
                 self.vertex[pair[1]].add(node)
                 d = euclidean_distance(node, pair[0])
                 add_edge[(node, pair[0])] = d
                 add_edge[(pair[0], node)] = d
                 rm_pair.add(pair)
                 nodes_in_edge.add(node)
         
             for n1 in nodes_in_edge:
                 for n2 in [pos for pos in nodes_in_edge if pos != n1]:
                     self.vertex[n1].add(n2)
                     self.vertex[n2].add(n1)
                     d = euclidean_distance(n1, n2)
                     add_edge[(n1, n2)] = d
                     add_edge[(n2, n1)] = d
             
     self.edge = add_edge
     
     for pair in rm_pair:
         self.remove_edge(pair)
     ## delete blocked vertex
     rm_vertex = set(self.vertex.keys()) & self.block_in_sights
     for vertex in rm_vertex:
         self.remove_vertex(vertex)
     ## delete blocked edge
     rm_pair = set()
     checknodes = self.block_in_sights - set(self.vertex.keys())
     for node in checknodes:
         for pair in self.edge.keys():
             if is_in_line(pair[0], node, pair[1]) and \
             is_within_line(pair[0], node, pair[1]):
                 rm_pair.add(pair)
                 
     for pair in rm_pair:
         self.remove_edge(pair)
Esempio n. 2
0
 def get_edge(self):
     edge = {}
     for v1 in self.vertex.keys():
         for v2 in self.vertex[v1]:
             d = euclidean_distance(v1, v2)
             edge[(v1, v2)] = d
             edge[(v2, v1)] = d
     return edge
Esempio n. 3
0
 def get_sights(self):
     self.sights = parallelogram_dynamic_bound(self.start, self.end)
     
     # duality
     if self.start[1] >= self.end[1]:
         pt1, pt2 = self.end, self.start
     else:
         pt1, pt2 = self.start, self.end
         
     dx, dy = abs(pt1[0] - pt2[0]), abs(pt1[1] - pt2[1])
     if dx == dy:
         v1 = pt1
         v2 = (pt1[0], pt1[1] + dy)
         v3 = pt2
         v4 = (pt2[0], pt2[1] - dy)            
     elif dx > dy:
         v1 = pt1
         v3 = pt2
         if pt1[0] >= pt2[0]:
             v2 = (pt1[0] - dy, pt2[1])
             v4 = (pt2[0] + dy, pt1[1])
         else:
             v2 = (pt1[0] + dy, pt2[1])
             v4 = (pt2[0] - dy, pt1[1])
     elif dx < dy:
         v1 = pt1
         v3 = pt2
         v2 = (pt1[0], pt1[1] + dy - dx)
         v4 = (pt2[0], pt2[1] - dy + dx)
         
     if dx == dy:
         self.vertex[v1] = set([v2, v4, v3])
         self.vertex[v2] = set([v1, v3])
         self.vertex[v3] = set([v2, v4, v1])
         self.vertex[v4] = set([v3, v1])
     else:
         self.vertex[v1] = set([v2, v4])
         self.vertex[v2] = set([v1, v3])
         self.vertex[v3] = set([v2, v4])
         self.vertex[v4] = set([v3, v1])
     
     for v1 in self.vertex.keys():
         for v2 in self.vertex[v1]:
             self.edge[(v1, v2)] = euclidean_distance(v1, v2)
     
     self.shortcut_vertex_exclude = deepcopy(self.vertex)
Esempio n. 4
0
def parallelogram_shortcut_graph(pt1, pt2):
    if pt1[1] >= pt2[1]:
        pt1, pt2 = pt2, pt1

    dx, dy = abs(pt1[0] - pt2[0]), abs(pt1[1] - pt2[1])
    s1, s2, s3 = set(), set(), set()
    g1 = {'vertex': {}, 'edge': {}}
    g2 = {'vertex': {}, 'edge': {}}
    g3 = {'vertex': {}, 'edge': {}}
    if dx == dy:
        pt3 = (pt1[0], pt2[1])
        pt4 = (pt2[0], pt1[1])
        l1 = bresenhams_line(pt1, pt3)
        l2 = bresenhams_line(pt3, pt2)
        l3 = bresenhams_line(pt2, pt4)
        l4 = bresenhams_line(pt4, pt1)

        s1 |= l1 | l2
        s2 |= bresenhams_line(pt1, pt2)
        s3 |= l3 | l4

        # graph
        g1['vertex'][pt1] = set([pt3])
        g1['vertex'][pt3] = set([pt2, pt1])
        g1['vertex'][pt2] = set([pt3])
        g1['edge'][(pt1, pt3)] = euclidean_distance(pt1, pt3)
        g1['edge'][(pt3, pt1)] = euclidean_distance(pt3, pt1)
        g1['edge'][(pt3, pt2)] = euclidean_distance(pt3, pt2)
        g1['edge'][(pt2, pt3)] = euclidean_distance(pt2, pt3)

        g2['vertex'][pt1] = set([pt2])
        g2['vertex'][pt2] = set([pt1])
        g2['edge'][(pt1, pt2)] = euclidean_distance(pt1, pt2)
        g2['edge'][(pt2, pt1)] = euclidean_distance(pt2, pt1)

        g3['vertex'][pt1] = set([pt4])
        g3['vertex'][pt4] = set([pt1, pt2])
        g3['vertex'][pt2] = set([pt4])
        g3['edge'][(pt1, pt4)] = euclidean_distance(pt1, pt4)
        g3['edge'][(pt4, pt1)] = euclidean_distance(pt4, pt1)
        g3['edge'][(pt4, pt2)] = euclidean_distance(pt4, pt2)
        g3['edge'][(pt2, pt4)] = euclidean_distance(pt2, pt4)

    elif dx > dy:
        if pt1[0] >= pt2[0]:
            pt3 = (pt1[0] - dy, pt2[1])
            pt4 = (pt2[0] + dy, pt1[1])
        else:
            pt3 = (pt1[0] + dy, pt2[1])
            pt4 = (pt2[0] - dy, pt1[1])
        l1 = bresenhams_line(pt1, pt3)
        l2 = bresenhams_line(pt3, pt2)
        l3 = bresenhams_line(pt2, pt4)
        l4 = bresenhams_line(pt4, pt1)
        s1 |= l1 | l2
        s3 |= l3 | l4
        # graph
        g1['vertex'][pt1] = set([pt3])
        g1['vertex'][pt3] = set([pt2, pt1])
        g1['vertex'][pt2] = set([pt3])
        g1['edge'][(pt1, pt3)] = euclidean_distance(pt1, pt3)
        g1['edge'][(pt3, pt1)] = euclidean_distance(pt3, pt1)
        g1['edge'][(pt3, pt2)] = euclidean_distance(pt3, pt2)
        g1['edge'][(pt2, pt3)] = euclidean_distance(pt2, pt3)

        g3['vertex'][pt1] = set([pt4])
        g3['vertex'][pt4] = set([pt1, pt2])
        g3['vertex'][pt2] = set([pt4])
        g3['edge'][(pt1, pt4)] = euclidean_distance(pt1, pt4)
        g3['edge'][(pt4, pt1)] = euclidean_distance(pt4, pt1)
        g3['edge'][(pt4, pt2)] = euclidean_distance(pt4, pt2)
        g3['edge'][(pt2, pt4)] = euclidean_distance(pt2, pt4)

    elif dx < dy:
        pt3 = (pt1[0], pt1[1] + dy - dx)
        pt4 = (pt2[0], pt2[1] - dy + dx)
        l1 = bresenhams_line(pt1, pt3)
        l2 = bresenhams_line(pt3, pt2)
        l3 = bresenhams_line(pt2, pt4)
        l4 = bresenhams_line(pt4, pt1)
        s1 |= l1 | l2
        s3 |= l3 | l4
        # graph
        g1['vertex'][pt1] = set([pt3])
        g1['vertex'][pt3] = set([pt2, pt1])
        g1['vertex'][pt2] = set([pt3])
        g1['edge'][(pt1, pt3)] = euclidean_distance(pt1, pt3)
        g1['edge'][(pt3, pt1)] = euclidean_distance(pt3, pt1)
        g1['edge'][(pt3, pt2)] = euclidean_distance(pt3, pt2)
        g1['edge'][(pt2, pt3)] = euclidean_distance(pt2, pt3)

        g3['vertex'][pt1] = set([pt4])
        g3['vertex'][pt4] = set([pt1, pt2])
        g3['vertex'][pt2] = set([pt4])
        g3['edge'][(pt1, pt4)] = euclidean_distance(pt1, pt4)
        g3['edge'][(pt4, pt1)] = euclidean_distance(pt4, pt1)
        g3['edge'][(pt4, pt2)] = euclidean_distance(pt4, pt2)
        g3['edge'][(pt2, pt4)] = euclidean_distance(pt2, pt4)

    return (s1, g1), (s2, g2), (s3, g3)
Esempio n. 5
0
 def test_case2(self):
     start = (0, 0)
     end = (5, 3)
     
     distance = euclidean_distance(start, end)
     self.assertEqual(distance, sqrt(pow(5, 2) + pow(3, 2)))
Esempio n. 6
0
    def get_directed_outline(self, outline):
        
        vertex = {}
        edge = {}
        visited = {}
        
        came_from = {}
        go_to = {}
                
        fork_queue = list(self.merge_nodes & outline)
        if not fork_queue:
            fork_queue = [self.start, self.end]
            
        while fork_queue:
            
            fork = fork_queue.pop(0)
            visited[fork] = True
            fork_nodes = [pos for pos in self.adjacent_blocks(fork, outline) 
            if not visited.get(pos, False)]
            
            # daulity
            fork_from = came_from.get(fork, None)
            if fork_from:
                vertex[fork] = set(fork_nodes) | set([fork_from])
            else:
                vertex[fork] = set(fork_nodes)
            
            for fork_node in fork_nodes:                
                vertex[fork_node] = set([fork])              
                d = euclidean_distance(fork, fork_node)
                edge[(fork, fork_node)] = d
                edge[(fork_node, fork)] = d
            
            # possible loops
            for connected in self.adjacent_blocks(fork, outline):
                if visited.get(connected, False) or connected in self.merge_nodes:
                    
                    nSet = vertex.get(fork, set())
                    nSet.add(connected)
                    vertex[fork] = nSet
                    
                    nSet = vertex.get(connected, set())
                    nSet.add(fork)
                    vertex[connected] = nSet
                    
                    d = euclidean_distance(fork, connected)
                    edge[(fork, connected)] = d
                    edge[(connected, fork)] = d
            
            while fork_nodes:
                
                current = fork_nodes.pop(0)                
                came_from[current] = fork
                
                search_queue = []
                search_queue.append(current)
                visited[current] = True
                
                while search_queue:
                    
                    node = search_queue.pop(0)
                        
                    if len([pos for pos in self.adjacent_blocks(node, outline)
                    if not visited.get(pos, False) and pos not in self.merge_nodes]) == 0:
                        
                        # daulity                        
                        # end of branch
                        nSet = vertex.get(node, set())
                        nSet.add(came_from[node])
                        vertex[node] = nSet                       
                        vertex[came_from[node]].add(node)
                        
                        d = euclidean_distance(node, came_from[node])
                        edge[(node, came_from[node])] = d
                        edge[(came_from[node], node)] = d

                        # possible loops
                        for connected in self.adjacent_blocks(node, outline):
                            if visited.get(connected, False) or connected in self.merge_nodes:
                                
                                nSet = vertex.get(node, set())
                                nSet.add(connected)
                                vertex[node] = nSet
                                
                                nSet = vertex.get(connected, set())
                                nSet.add(node)
                                vertex[connected] = nSet
                                
                                d = euclidean_distance(node, connected)
                                edge[(node, connected)] = d
                                edge[(connected, node)] = d
                        break
                    
                    if len([pos for pos in self.adjacent_blocks(node, outline)
                    if not visited.get(pos, False)]) > 1 and node not in self.merge_nodes:
                        fork_queue.append(node)
                        
                        # daulity
                        vertex[node] = set([came_from[node]])                        
                        vertex[came_from[node]].add(node)
                        
                        d = euclidean_distance(node, came_from[node])
                        edge[(node, came_from[node])] = d
                        edge[(came_from[node], node)] = d
                        break
                    else:
                        
                        for next_node in self.adjacent_blocks(node, outline):
                            if not visited.get(next_node, False):
                                search_queue.append(next_node)
                                came_from[next_node] = node
                                go_to[node] = next_node
                                visited[next_node] = True
                                
                                # daulity
                                if not is_in_line(came_from[node], node, go_to[node]):
                                    
                                    vertex[node] = set([came_from[node]])
                                    vertex[came_from[node]].add(node)
                                    
                                    d = euclidean_distance(came_from[node], node)
                                    edge[(came_from[node], node)] = d
                                    edge[(node, came_from[node])] = d

                                else:                                    
                                    if node == current:
                                        vertex[came_from[node]].remove(node)
                                        vertex.pop(node)
                                    came_from[go_to[node]] = came_from[node]
                                    go_to[came_from[node]] = go_to[node]
                                    
        return vertex, edge