Skip to content

Commit 8f7338c

Browse files
author
debsankha manik
committed
Working on planar graph generation using voronoi tessalation
1 parent 7579e94 commit 8f7338c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

notebooks/VoronoiGraph.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from scipy.spatial import Voronoi
2+
import numpy as np
3+
import networkx as nx
4+
5+
6+
def gen_voronoi(size):
7+
"""
8+
Generate a voronoi tessalation with `size` seed points in the unit cube
9+
"""
10+
points=np.random.rand(size,2)
11+
return Voronoi(points)
12+
13+
14+
def voronoi_graph(vor, with_coords=True):
15+
"""
16+
Create a planar graph and it's loopy dual from a voronoi tessalation
17+
"""
18+
G=nx.Graph()
19+
20+
for ridge in vor.ridge_vertices:
21+
if -1 not in ridge:
22+
G.add_edge(*ridge)
23+
24+
if with_coords:
25+
for node in G.nodes():
26+
G.node[node]['pos']=tuple(vor.vertices[node])
27+
28+
return G
29+
30+
31+
def voronoi_loopy_dual(vor):
32+
H=nx.MultiGraph()
33+
34+
points_with_bounded_regions=[]
35+
for idx, pt in enumerate(vor.points):
36+
region=vor.regions[vor.point_region[idx]]
37+
38+
if region and (-1 not in region):
39+
points_with_bounded_regions.append(idx)
40+
41+
42+
43+
for pt1,pt2 in vor.ridge_points:
44+
if pt1 in points_with_bounded_regions:
45+
if pt2 in points_with_bounded_regions:
46+
H.add_edge(pt1,pt2)
47+
else:
48+
H.add_edge(pt1,pt1)
49+
elif pt2 in points_with_bounded_regions:
50+
H.add_edge(pt2,pt2)
51+
else:
52+
pass
53+
54+
return H
55+
56+

0 commit comments

Comments
 (0)