-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoronoiCell.py
More file actions
85 lines (70 loc) · 2.79 KB
/
VoronoiCell.py
File metadata and controls
85 lines (70 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Polygon, Point, MultiPolygon
from shapely.ops import unary_union
class VoronoiCell:
"""
Helper class to save Voronoi cell polygons with neighbors and a unique ID.
"""
def __init__(self, polygon: Polygon, cell_id: int):
self.polygon: Polygon = polygon
self.id: int = cell_id
self.neighbors: set[int] = set()
def __str__(self):
return f"Cell {self.id} with {len(self.neighbors)} neighbors."
def set_neighbors(self, neighbors: set[int]):
"""
Set the neighbors of the cell.
Args:
neighbors (set[int]): Set of neighboring cell IDs.
"""
self.neighbors: set[int] = {int(n) for n in neighbors}
def plot(self, all_polygons: dict[int, "VoronoiCell"]):
"""
Plot self and neighbors using matplotlib.
"""
plot_polygons([self] + [all_polygons[n] for n in self.neighbors])
# --- Debug Plotting Function ---
def debug_plot(polygons: list[Polygon], points: np.ndarray = None):
"""
Plot the polygons and points for debugging purposes.
Args:
polygons (list[Polygon]): List of polygons to plot.
points (np.ndarray, optional): Points to plot (if any).
"""
fig, ax = plt.subplots()
for poly in polygons:
x, y = poly.exterior.xy
ax.fill(x, y, alpha=0.5, fc='b', ec='black')
if points is not None:
ax.scatter(points[:, 0], points[:, 1], color='red', zorder=5)
ax.set_aspect('equal')
plt.show()
def plot_polygons(polygons: list[VoronoiCell], show_ids: bool = True):
"""
Plot a list of polygons using matplotlib.
Args:
polygons (list[Polygon]): List of polygons to plot.
"""
fig, ax = plt.subplots()
for cell in polygons:
if isinstance(cell.polygon, MultiPolygon):
# Use unary_union to merge MultiPolygon into a single Polygon
cell.polygon = unary_union(cell.polygon)
if isinstance(cell.polygon, MultiPolygon):
# draw all subpolygons individually in red
for poly in cell.polygon.geoms:
x, y = poly.exterior.xy
ax.fill(x, y, alpha=0.5, fc='#dd0000', ec='black')
x, y = poly.centroid.xy
ax.text(x[0], y[0], str(cell.id), fontsize=8, ha='center', va='center')
else:
x, y = cell.polygon.exterior.xy
ax.fill(x, y, alpha=0.5, fc='#5588ff', ec='black')
# draw id on the center of the cell
x, y = cell.polygon.centroid.xy
if show_ids:
ax.text(x[0], y[0], str(cell.id), fontsize=8, ha='center', va='center')
ax.set_aspect('equal')
plt.subplots_adjust(left=0.03, right=0.99, top=0.99, bottom=0.02)
plt.show()