File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments