mobicen / util / UnitDiskGraph.py @ e1cf8bea
History  View  Annotate  Download (593 Bytes)
1 
# https://stackoverflow.com/questions/32424604/findallnearestneighborswithinaspecificdistance


2  
3 
import networkx as nx 
4 
from scipy import spatial 
5  
6  
7 
class UnitDiskGraph: 
8  
9 
def __init__(self, points, radius): 
10 
self.G = self.genereateGraphFromKDtree(points, radius) 
11  
12 
def genereateGraphFromKDtree(self, points, radius): 
13 
tree = spatial.KDTree(points) 
14 
edges = tree.query_pairs(r=radius) 
15 
G = nx.Graph() 
16 
#pos = {k:points[k] for k in range(0,len(points))}

17 
G.add_edges_from(edges) 
18 
return G

19  
20 
def getGraph(self): 
21 
return self.G 