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


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

19 
G.add_weighted_edges_from(edges, weight='weight')

20 
return G

21  
22 
def getGraph(self): 
23 
return self.G 