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
187
188
189
190
191 | def plot_all(out, off_screen=False):
"""Plot in one image: mesh, surfaces, Purkinje, and fibers."""
with gd.Container(out) as ct:
# Required: tetra and at least one surface.
# Mesh.load leaves pv_mesh.point_data empty, so sync before add_mesh
# can colour by a scalar field.
tetra = sync_point_data(Mesh.load(ct / "tetra.vtk"))
regular_surf = Mesh.load(ct / "regular_surf.vtk")
# Optional: endo, epi, points, fibers, purkinje
endo = _load_optional(ct, "endo.vtk")
epi = _load_optional(ct, "epi.vtk")
points_mesh = _load_optional(ct, "points.vtk")
purkinje = _load_optional(ct, "purkinje_mesh.vtk")
# mesh_cell_fibers.vtk is PolyData (point cloud); load with PyVista
mesh_cell_fibers_pv = None
if Path(ct / "mesh_cell_fibers.vtk").exists():
mesh_cell_fibers_pv = pv.read(str(ct / "mesh_cell_fibers.vtk"))
with Plotter(off_screen=off_screen) as p:
# Volume mesh: transparent or wireframe so interior is visible
p.add_mesh(tetra, opacity=0.12, show_edges=False, color="lightgray")
# Surfaces: combined regular surface + optional endo/epi
p.add_mesh(regular_surf, opacity=0.5, show_edges=False, color="white")
if endo is not None:
p.add_mesh(endo, opacity=0.6, show_edges=False, color="coral")
if epi is not None:
p.add_mesh(epi, opacity=0.6, show_edges=False, color="lightblue")
p.camera_position = CAM_POS
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "surfaces_img.png")
# Purkinje network
if purkinje is not None:
with Plotter(off_screen=off_screen) as p:
p.add_mesh(endo)
p.add_mesh(purkinje, color="red", line_width=3)
p.camera_position = [
(0.6051986645911922, -0.46423570086746, 37.204488072156266),
(
-0.0003440849696247916,
-5.655034101526013e-05,
-5.999310324980369,
),
(0.004839410606533647, -0.9999298485328891, -0.010811018359763551),
]
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "purkinje_img.png")
# Fibers: vectors at cell centers (subsample for clarity)
if (
mesh_cell_fibers_pv is not None
and "fiber" in mesh_cell_fibers_pv.point_data
):
with Plotter(off_screen=off_screen) as p:
p.add_mesh(tetra, opacity=0.12, show_edges=False, color="lightgray")
if endo is not None:
p.add_mesh(endo, opacity=0.6, show_edges=False, color="coral")
if epi is not None:
p.add_mesh(epi, opacity=0.6, show_edges=False, color="lightblue")
pf = mesh_cell_fibers_pv
n_pts = pf.n_points
step = max(1, n_pts // 800)
subsampled = pf.copy()
subsampled.points = np.asarray(pf.points)[::step]
for key in ("fiber", "sheet"):
if key in pf.point_data.keys():
subsampled.point_data[key] = np.asarray(pf.point_data[key])[
::step
]
subsampled.set_active_vectors("fiber")
arrows = subsampled.glyph(orient="fiber", factor=0.4, geom=pv.Arrow())
p.add_mesh(arrows, color="darkred", opacity=0.8)
p.camera_position = CAM_POS
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "fibers_img.png")
# Points (group 0,1 = blue, 2 = red line, 3 = green)
if points_mesh is not None and "group" in points_mesh.point_data:
with Plotter(off_screen=off_screen) as p:
p.add_mesh(tetra, opacity=0.12, show_edges=False, color="lightgray")
pts = points_mesh.points
grp = points_mesh.point_data["group"]
for g, color in ((0, "blue"), (1, "blue"), (3, "green")):
mask = grp == g
if np.any(mask):
p.add_mesh(
pv.PolyData(pts[mask]),
color=color,
point_size=12,
render_points_as_spheres=True,
)
red_mask = grp == 2
if np.sum(red_mask) >= 2:
red_pts = pts[red_mask]
n = len(red_pts)
# PyVista lines: flat array [n_pts, i, j, n_pts, i, j, ...] per segment
lines = np.column_stack(
[np.full(n - 1, 2), np.arange(n - 1), np.arange(1, n)]
).ravel()
line_mesh = pv.PolyData(red_pts, lines=lines)
p.add_mesh(line_mesh, color="red", line_width=5)
p.camera_position = CAM_POS
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "points_img.png")
# AHA segments (guarded, so older outputs without the field still plot)
if Labels.AHA in tetra.point_data:
with Plotter(off_screen=off_screen) as p:
# interpolate_before_map=False keeps each cell a flat colour:
# smoothing an integer label across a cell that straddles two
# bands paints a rainbow seam along every boundary.
p.add_mesh(
tetra,
scalars=Labels.AHA,
cmap="tab20",
clim=[1, Aha.N_SEGMENTS],
n_colors=Aha.N_SEGMENTS,
interpolate_before_map=False,
show_edges=False,
)
p.camera_position = CAM_POS
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "aha_img.png")
with Plotter(off_screen=off_screen) as p:
p.add_mesh(tetra)
p.camera_position = CAM_POS
p.render()
if not off_screen:
p.show()
p.screenshot(ct / "mesh_img.png")
|