-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_plotly.py
More file actions
186 lines (143 loc) · 5.53 KB
/
Copy pathplot_plotly.py
File metadata and controls
186 lines (143 loc) · 5.53 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# ── CONFIG ────────────────────────────────────────────────────────────────────
AP_PATH = "3D_xyzrs/test3_00_AP.xyzr"
VOID_PATH = "3D_xyzrs/test3_00_void.xyzr"
PHYSICAL_SIZE = 20e-6
IMG_SIZE = 1.0
SLICE_AXIS = "z"
SLICE_POS = 0.4
SPHERE_RESOLUTION = 25
ELEV = 25
AZIM = 45
AP_COLOR = "#FF6B6B"
VOID_COLOR = "#939494"
PLANE_COLOR = "#FFD700"
AP_ALPHA_3D = 1.0
# ── helpers ───────────────────────────────────────────────────────────────────
def read_xyzr(path, physical_size, img_size):
pts = []
if not os.path.exists(path):
return pts
scale = img_size / physical_size
with open(path) as f:
for line in f:
vals = line.split()
if len(vals) >= 4:
x, y, z, r = map(float, vals[:4])
pts.append((x*scale, y*scale, z*scale, r*scale))
return pts
def make_sphere_mesh_half(x0, y0, z0, r, resolution, domain,
axis, pos, keep):
u = np.linspace(0, 2*np.pi, resolution)
v = np.linspace(0, np.pi, resolution)
u, v = np.meshgrid(u, v)
x = x0 + r*np.cos(u)*np.sin(v)
y = y0 + r*np.sin(u)*np.sin(v)
z = z0 + r*np.cos(v)
lo, hi = domain
mask = ((x < lo) | (x > hi) |
(y < lo) | (y > hi) |
(z < lo) | (z > hi))
coord = {"x": x, "y": y, "z": z}[axis]
if keep == "below":
mask |= (coord > pos)
else:
mask |= (coord < pos)
x[mask] = np.nan
y[mask] = np.nan
z[mask] = np.nan
return x, y, z
def draw_plane(ax, axis, pos, img_size):
lo, hi = 0, img_size
if axis == "x":
verts = [(pos, lo, lo), (pos, hi, lo),
(pos, hi, hi), (pos, lo, hi)]
elif axis == "y":
verts = [(lo, pos, lo), (hi, pos, lo),
(hi, pos, hi), (lo, pos, hi)]
else:
verts = [(lo, lo, pos), (hi, lo, pos),
(hi, hi, pos), (lo, hi, pos)]
ax.add_collection3d(Poly3DCollection(
[verts], facecolor=PLANE_COLOR, alpha=0.6, edgecolor="none"
))
def style_3d(ax):
# Original plotting
ax.set_xlim(0, IMG_SIZE)
ax.set_ylim(0, IMG_SIZE)
ax.set_zlim(0, IMG_SIZE)
ax.set_box_aspect([1,1,1])
ax.view_init(elev=ELEV, azim=AZIM)
# Ticks 0, 0.2, ..., 1
ticks = [0, 0.2, 0.4, 0.6, 0.8, 1.0]
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_zticks(ticks)
def slice_circle(x0, y0, z0, r, axis, pos):
if axis == "x":
d = abs(pos - x0); cx, cy = y0, z0
elif axis == "y":
d = abs(pos - y0); cx, cy = x0, z0
else:
d = abs(pos - z0); cx, cy = x0, y0
if d >= r:
return None
return cx, cy, np.sqrt(r**2 - d**2)
# ── 3D DRAW (AP ONLY) ─────────────────────────────────────────────────────────
def draw_3d(ax, circles):
domain = (0, IMG_SIZE)
# Back half
for (x0,y0,z0,r) in circles:
x,y,z = make_sphere_mesh_half(
x0,y0,z0,r,SPHERE_RESOLUTION,domain,
SLICE_AXIS,SLICE_POS,"below"
)
ax.plot_surface(x,y,z,color=AP_COLOR,alpha=AP_ALPHA_3D,linewidth=0)
# Plane
draw_plane(ax, SLICE_AXIS, SLICE_POS, IMG_SIZE)
# Front half
for (x0,y0,z0,r) in circles:
x,y,z = make_sphere_mesh_half(
x0,y0,z0,r,SPHERE_RESOLUTION,domain,
SLICE_AXIS,SLICE_POS,"above"
)
ax.plot_surface(x,y,z,color=AP_COLOR,alpha=AP_ALPHA_3D,linewidth=0)
# ── 2D SLICE (AP + VOIDS) ─────────────────────────────────────────────────────
# ── 2D SLICE (AP + VOIDS) ─────────────────────────────────────────────────────
def draw_2d(ax, circles, voids):
ax.set_aspect("equal")
# Flip both axes: 1 → 0
ax.set_xlim(IMG_SIZE, 0)
ax.set_ylim(IMG_SIZE, 0)
# Draw AP particles
for (x0, y0, z0, r) in circles:
res = slice_circle(x0, y0, z0, r, "z", SLICE_POS)
if res:
cx, cy, cr = res
ax.add_patch(Circle((cx, cy), cr, color=AP_COLOR))
# Draw voids
for (x0, y0, z0, r) in voids:
res = slice_circle(x0, y0, z0, r, "z", SLICE_POS)
if res:
cx, cy, cr = res
ax.add_patch(Circle((cx, cy), cr, color=VOID_COLOR))
# ── MAIN ──────────────────────────────────────────────────────────────────────
def main():
circles = read_xyzr(AP_PATH, PHYSICAL_SIZE, IMG_SIZE)
voids = read_xyzr(VOID_PATH, PHYSICAL_SIZE, IMG_SIZE)
fig = plt.figure(figsize=(14,6))
ax3d = fig.add_subplot(121, projection='3d')
ax2d = fig.add_subplot(122)
draw_3d(ax3d, circles)
style_3d(ax3d)
draw_2d(ax2d, circles, voids)
plt.tight_layout()
plt.show()
plt.savefig("3D_xyzrs/example.png", dpi=300)
if __name__ == "__main__":
main()