|
10 | 10 | BondSpec, |
11 | 11 | CellEdgeStyle, |
12 | 12 | Frame, |
| 13 | + PolyhedronSpec, |
13 | 14 | RenderStyle, |
14 | 15 | StructureScene, |
15 | 16 | ViewState, |
@@ -244,3 +245,138 @@ def test_cell_edges_clipped_at_corner_atoms(self): |
244 | 245 | # edge polygons (clipping shortens edges but doesn't remove them). |
245 | 246 | assert n_corner >= 1 |
246 | 247 | assert n_mid >= 1 |
| 248 | + |
| 249 | + def test_hidden_polyhedra_centre_does_not_clip_cell_edge(self): |
| 250 | + """A polyhedron centre with hide_centre=True should not clip cell edges. |
| 251 | +
|
| 252 | + Reproduces https://github.com/bjmorgan/hofmann/issues/41. |
| 253 | + """ |
| 254 | + a = 10.0 |
| 255 | + d = 1.8 |
| 256 | + ti = np.array([a / 2, 0.0, 0.0]) |
| 257 | + offsets = d / np.sqrt(3) * np.array([ |
| 258 | + [1, 1, 1], [1, -1, -1], [-1, 1, -1], [-1, -1, 1], |
| 259 | + ]) |
| 260 | + coords = np.vstack([ti[None, :], ti + offsets]) |
| 261 | + |
| 262 | + def _make_scene(hide_centre): |
| 263 | + return StructureScene( |
| 264 | + species=["Ti", "O", "O", "O", "O"], |
| 265 | + frames=[Frame(coords=coords, lattice=np.eye(3) * a)], |
| 266 | + atom_styles={ |
| 267 | + "Ti": AtomStyle(1.2, (0.2, 0.4, 0.9)), |
| 268 | + "O": AtomStyle(0.5, (0.9, 0.1, 0.1)), |
| 269 | + }, |
| 270 | + bond_specs=[ |
| 271 | + BondSpec(species=("Ti", "O"), min_length=0.5, |
| 272 | + max_length=3.5, radius=0.1, |
| 273 | + colour=(0.4, 0.4, 0.4)), |
| 274 | + ], |
| 275 | + polyhedra=[ |
| 276 | + PolyhedronSpec(centre="Ti", hide_centre=hide_centre, |
| 277 | + hide_bonds=True), |
| 278 | + ], |
| 279 | + ) |
| 280 | + |
| 281 | + render_kwargs = dict(show=False, show_polyhedra=True, |
| 282 | + show_cell=True) |
| 283 | + |
| 284 | + # Centre hidden — should not clip cell edges. |
| 285 | + fig_hidden = render_mpl(_make_scene(hide_centre=True), |
| 286 | + **render_kwargs) |
| 287 | + n_hidden = len(fig_hidden.axes[0].collections[0].get_paths()) |
| 288 | + plt.close(fig_hidden) |
| 289 | + |
| 290 | + # Centre visible — should clip cell edges. |
| 291 | + fig_visible = render_mpl(_make_scene(hide_centre=False), |
| 292 | + **render_kwargs) |
| 293 | + n_visible = len(fig_visible.axes[0].collections[0].get_paths()) |
| 294 | + plt.close(fig_visible) |
| 295 | + |
| 296 | + # The only difference is Ti visibility. When hidden it should |
| 297 | + # not split cell edges, producing fewer polygon paths. |
| 298 | + assert n_hidden < n_visible |
| 299 | + |
| 300 | + def test_invisible_atom_does_not_clip_cell_edge(self): |
| 301 | + """An atom with AtomStyle.visible=False should not clip cell edges.""" |
| 302 | + a = 10.0 |
| 303 | + coords = np.array([[a / 2, 0.0, 0.0]]) |
| 304 | + # Scene with invisible atom on the cell edge. |
| 305 | + scene_invisible = StructureScene( |
| 306 | + species=["A"], |
| 307 | + frames=[Frame(coords=coords, lattice=np.eye(3) * a)], |
| 308 | + atom_styles={"A": AtomStyle(1.0, (0.5, 0.5, 0.5), |
| 309 | + visible=False)}, |
| 310 | + ) |
| 311 | + # Baseline: atom far from any cell edge but at the same depth |
| 312 | + # (z=0) so depth-splitting of cell edges is identical. |
| 313 | + scene_no_clip = StructureScene( |
| 314 | + species=["A"], |
| 315 | + frames=[Frame( |
| 316 | + coords=np.array([[a / 2, a / 2, 0.0]]), |
| 317 | + lattice=np.eye(3) * a, |
| 318 | + )], |
| 319 | + atom_styles={"A": AtomStyle(1.0, (0.5, 0.5, 0.5), |
| 320 | + visible=False)}, |
| 321 | + ) |
| 322 | + fig_inv = render_mpl(scene_invisible, show=False, show_cell=True) |
| 323 | + n_inv = len(fig_inv.axes[0].collections[0].get_paths()) |
| 324 | + plt.close(fig_inv) |
| 325 | + |
| 326 | + fig_no_clip = render_mpl(scene_no_clip, show=False, show_cell=True) |
| 327 | + n_no_clip = len(fig_no_clip.axes[0].collections[0].get_paths()) |
| 328 | + plt.close(fig_no_clip) |
| 329 | + |
| 330 | + # An invisible atom on a cell edge should produce the same |
| 331 | + # number of paths as one that doesn't intersect any edge. |
| 332 | + assert n_inv == n_no_clip |
| 333 | + |
| 334 | + def test_hidden_polyhedra_vertices_do_not_clip_cell_edge(self): |
| 335 | + """Vertex atoms hidden by hide_vertices=True should not clip cell edges.""" |
| 336 | + a = 10.0 |
| 337 | + d = 1.8 |
| 338 | + ti = np.array([a / 2, a / 2, a / 2]) |
| 339 | + # One O vertex sits on the cell edge at (a/2, 0, 0). |
| 340 | + coords = np.array([ |
| 341 | + ti, |
| 342 | + np.array([a / 2, 0.0, 0.0]), |
| 343 | + ti + d * np.array([1, -1, -1]) / np.sqrt(3), |
| 344 | + ti + d * np.array([-1, 1, -1]) / np.sqrt(3), |
| 345 | + ti + d * np.array([-1, -1, 1]) / np.sqrt(3), |
| 346 | + ]) |
| 347 | + |
| 348 | + def _make_scene(hide_vertices): |
| 349 | + return StructureScene( |
| 350 | + species=["Ti", "O", "O", "O", "O"], |
| 351 | + frames=[Frame(coords=coords, lattice=np.eye(3) * a)], |
| 352 | + atom_styles={ |
| 353 | + "Ti": AtomStyle(0.5, (0.2, 0.4, 0.9)), |
| 354 | + "O": AtomStyle(1.0, (0.9, 0.1, 0.1)), |
| 355 | + }, |
| 356 | + bond_specs=[ |
| 357 | + BondSpec(species=("Ti", "O"), min_length=0.5, |
| 358 | + max_length=6.0, radius=0.1, |
| 359 | + colour=(0.4, 0.4, 0.4)), |
| 360 | + ], |
| 361 | + polyhedra=[ |
| 362 | + PolyhedronSpec(centre="Ti", |
| 363 | + hide_vertices=hide_vertices, |
| 364 | + hide_bonds=True), |
| 365 | + ], |
| 366 | + ) |
| 367 | + |
| 368 | + render_kwargs = dict(show=False, show_polyhedra=True, |
| 369 | + show_cell=True) |
| 370 | + |
| 371 | + fig_hidden = render_mpl(_make_scene(hide_vertices=True), |
| 372 | + **render_kwargs) |
| 373 | + n_hidden = len(fig_hidden.axes[0].collections[0].get_paths()) |
| 374 | + plt.close(fig_hidden) |
| 375 | + |
| 376 | + fig_visible = render_mpl(_make_scene(hide_vertices=False), |
| 377 | + **render_kwargs) |
| 378 | + n_visible = len(fig_visible.axes[0].collections[0].get_paths()) |
| 379 | + plt.close(fig_visible) |
| 380 | + |
| 381 | + # When vertex is hidden, it should not clip the cell edge. |
| 382 | + assert n_hidden < n_visible |
0 commit comments