unit-cell-gui 0.2.2__tar.gz → 0.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/CHANGELOG.md +23 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/CITATION.cff +2 -2
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/MANIFEST.in +1 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/PKG-INFO +20 -1
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/README.md +19 -0
- unit_cell_gui-0.2.3/RELEASE_NOTES_0.2.3.md +53 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/pyproject.toml +1 -1
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/__init__.py +1 -1
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/gl_geometry.py +18 -1
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/opengl_viewer.py +76 -90
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui.egg-info/PKG-INFO +20 -1
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui.egg-info/SOURCES.txt +3 -1
- unit_cell_gui-0.2.3/tests/test_z_atom_instancing.py +188 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/LICENSE +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/RELEASE_NOTES_0.2.2.md +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/THIRD_PARTY_NOTICES.txt +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/setup.cfg +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/hatching.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/models.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/py.typed +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/qt_config.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/render_geometry.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/view_helpers.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui/viewer.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui.egg-info/dependency_links.txt +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui.egg-info/requires.txt +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/src/unit_cell_gui.egg-info/top_level.txt +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/tests/test_gl_geometry.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/tests/test_render_geometry.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/tests/test_unit_cell_gui.py +0 -0
- {unit_cell_gui-0.2.2 → unit_cell_gui-0.2.3}/tests/test_visibility_cache.py +0 -0
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.3 - 2026-10-06
|
|
4
|
+
|
|
5
|
+
### Performance
|
|
6
|
+
|
|
7
|
+
- Render atoms with one indexed unit-sphere mesh and one compact instance
|
|
8
|
+
record per visible atom using `glDrawElementsInstanced`.
|
|
9
|
+
- Replace repeated per-vertex centre, radius, colour and occupancy data with
|
|
10
|
+
144-byte instance records; keep the shared mesh and index buffers resident
|
|
11
|
+
during atom scale, colour, occupancy visibility and scene changes.
|
|
12
|
+
- Remove individual CPU sphere tessellation and the per-atom mesh cache.
|
|
13
|
+
|
|
14
|
+
### Compatibility
|
|
15
|
+
|
|
16
|
+
- Preserve the public scene models, viewer aliases, display options, style
|
|
17
|
+
overrides, orientation synchronization and interaction signals.
|
|
18
|
+
- Keep OpenGL 3.3 core and GLSL 330 requirements, with 13 attribute locations
|
|
19
|
+
and no new dependencies or native extension.
|
|
20
|
+
- Preserve the original sphere triangles and normals, mixed/partial occupancy
|
|
21
|
+
materials, hidden occupancy sectors, external atoms, colour and engraving
|
|
22
|
+
modes, and the existing outline/intersection/hatching passes.
|
|
23
|
+
- Release and recreate mesh, index and instance buffers with viewer context
|
|
24
|
+
cleanup/reinitialization.
|
|
25
|
+
|
|
3
26
|
## 0.2.2 - 2026-10-01
|
|
4
27
|
|
|
5
28
|
### Fixed
|
|
@@ -8,7 +8,7 @@ authors:
|
|
|
8
8
|
repository-code: https://github.com/ofussr/unit-cell-gui
|
|
9
9
|
url: https://github.com/ofussr/unit-cell-gui
|
|
10
10
|
orcid: https://orcid.org/0000-0002-5571-8525
|
|
11
|
-
version: 0.2.
|
|
12
|
-
date-released: 2026-10-
|
|
11
|
+
version: 0.2.3
|
|
12
|
+
date-released: 2026-10-06
|
|
13
13
|
doi: 10.5281/zenodo.22714946
|
|
14
14
|
license: MIT
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: unit-cell-gui
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: PySide6 widget for rendering caller-supplied unit-cell scenes
|
|
5
5
|
Author-email: Mikhail Mirushchenko <miruschenko98@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -137,6 +137,25 @@ Per-position colours, visibility and opaque-polyhedron selections are replaced
|
|
|
137
137
|
atomically with `viewer.set_style_overrides(StyleOverrides(...))`. The widget
|
|
138
138
|
copies these mappings, so the host can keep its own independent UI state.
|
|
139
139
|
|
|
140
|
+
## Atom instancing
|
|
141
|
+
|
|
142
|
+
Atoms share one indexed sphere mesh with the same 16-by-24 tessellation as
|
|
143
|
+
previous releases. An OpenGL instance buffer stores each visible atom's
|
|
144
|
+
Cartesian centre, radius, eight RGB occupancy colours and cumulative occupancy
|
|
145
|
+
thresholds once, instead of repeating them for every triangle vertex. The atom
|
|
146
|
+
layer uses one `glDrawElementsInstanced` call.
|
|
147
|
+
|
|
148
|
+
The base sphere and its indices occupy 18,792 bytes per viewer; instance records
|
|
149
|
+
add 144 bytes per visible atom. For 1,000 atoms, these buffers total 162,792
|
|
150
|
+
bytes, compared with 328,320,000 bytes for the previous expanded atom buffer.
|
|
151
|
+
This comparison excludes bonds, outlines, polyhedra and Qt/driver overhead.
|
|
152
|
+
The number of sphere triangles is unchanged, so this is primarily a reduction
|
|
153
|
+
in geometry preparation and buffer storage, not a frame-rate guarantee.
|
|
154
|
+
|
|
155
|
+
OpenGL 3.3 core remains sufficient: the renderer uses 13 vertex attribute
|
|
156
|
+
locations, within the guaranteed minimum of 16. No C++ extension, new package
|
|
157
|
+
dependency or host-application API change is required.
|
|
158
|
+
|
|
140
159
|
## Visibility and rendering caches
|
|
141
160
|
|
|
142
161
|
Both global and per-site polyhedron visibility filter already prepared geometry
|
|
@@ -119,6 +119,25 @@ Per-position colours, visibility and opaque-polyhedron selections are replaced
|
|
|
119
119
|
atomically with `viewer.set_style_overrides(StyleOverrides(...))`. The widget
|
|
120
120
|
copies these mappings, so the host can keep its own independent UI state.
|
|
121
121
|
|
|
122
|
+
## Atom instancing
|
|
123
|
+
|
|
124
|
+
Atoms share one indexed sphere mesh with the same 16-by-24 tessellation as
|
|
125
|
+
previous releases. An OpenGL instance buffer stores each visible atom's
|
|
126
|
+
Cartesian centre, radius, eight RGB occupancy colours and cumulative occupancy
|
|
127
|
+
thresholds once, instead of repeating them for every triangle vertex. The atom
|
|
128
|
+
layer uses one `glDrawElementsInstanced` call.
|
|
129
|
+
|
|
130
|
+
The base sphere and its indices occupy 18,792 bytes per viewer; instance records
|
|
131
|
+
add 144 bytes per visible atom. For 1,000 atoms, these buffers total 162,792
|
|
132
|
+
bytes, compared with 328,320,000 bytes for the previous expanded atom buffer.
|
|
133
|
+
This comparison excludes bonds, outlines, polyhedra and Qt/driver overhead.
|
|
134
|
+
The number of sphere triangles is unchanged, so this is primarily a reduction
|
|
135
|
+
in geometry preparation and buffer storage, not a frame-rate guarantee.
|
|
136
|
+
|
|
137
|
+
OpenGL 3.3 core remains sufficient: the renderer uses 13 vertex attribute
|
|
138
|
+
locations, within the guaranteed minimum of 16. No C++ extension, new package
|
|
139
|
+
dependency or host-application API change is required.
|
|
140
|
+
|
|
122
141
|
## Visibility and rendering caches
|
|
123
142
|
|
|
124
143
|
Both global and per-site polyhedron visibility filter already prepared geometry
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# unit-cell-gui 0.2.3
|
|
2
|
+
|
|
3
|
+
This release reduces atom geometry preparation and memory use by sharing one
|
|
4
|
+
indexed sphere mesh across every visible atom. Existing host applications can
|
|
5
|
+
keep their current integration code.
|
|
6
|
+
|
|
7
|
+
## Atom rendering
|
|
8
|
+
|
|
9
|
+
- Draw the complete atom layer with `glDrawElementsInstanced`.
|
|
10
|
+
- Store each atom's centre, radius, eight RGB colours and cumulative occupancy
|
|
11
|
+
thresholds once in a 144-byte instance record.
|
|
12
|
+
- Keep one base sphere and its index buffer per viewer, totaling 18,792 bytes.
|
|
13
|
+
Retain the original 16-by-24 tessellation and all 720 triangles per sphere;
|
|
14
|
+
exact indexing preserves the original triangle order and normals.
|
|
15
|
+
- Remove CPU generation and caching of separate sphere meshes for every atom.
|
|
16
|
+
- Reuse the base sphere during scene/style changes and recreate all new GPU
|
|
17
|
+
resources after OpenGL context cleanup.
|
|
18
|
+
|
|
19
|
+
For 1,000 atoms, the main atom geometry buffers shrink from 328,320,000 bytes
|
|
20
|
+
(328.32 MB) to 162,792 bytes (approximately 0.163 MB), including the shared mesh
|
|
21
|
+
and indices. This excludes bonds, atom outlines, intersections, polyhedra and
|
|
22
|
+
Qt/driver overhead. Large scenes can still spend time on those other layers.
|
|
23
|
+
The sphere triangle count has not changed; this is not a frame-rate guarantee.
|
|
24
|
+
|
|
25
|
+
## Compatibility
|
|
26
|
+
|
|
27
|
+
- Public API, scene models, viewer aliases, display/style options and signals
|
|
28
|
+
remain unchanged from 0.2.2.
|
|
29
|
+
- OpenGL 3.3 core and GLSL 330 remain sufficient; 13 vertex attributes are used,
|
|
30
|
+
below the guaranteed minimum of 16. No new dependencies or C++ extension.
|
|
31
|
+
- Preserve camera-facing mixed and partial occupancy sectors, hidden sectors,
|
|
32
|
+
colour overrides, external-atom filtering, atom scaling and engraving fills.
|
|
33
|
+
- Keep bonds, contours, intersections, polyhedra and hatching on their existing
|
|
34
|
+
rendering paths.
|
|
35
|
+
|
|
36
|
+
## Validation
|
|
37
|
+
|
|
38
|
+
- 48 tests passed with Mesa software OpenGL forced to OpenGL 3.3 / GLSL 330,
|
|
39
|
+
including actual indexed instanced drawing, attribute divisors, index binding,
|
|
40
|
+
buffer sizes, cleanup and reinitialization. All 48 also passed using the
|
|
41
|
+
installed wheel, with Python sources compiled successfully.
|
|
42
|
+
- 20 framebuffer comparisons against commit `81f76a1` (0.2.2): 19 were
|
|
43
|
+
pixel-identical. One differed at one pixel by one channel step out of 255,
|
|
44
|
+
consistent with float32 transformation rounding. Cases cover colour/engraving,
|
|
45
|
+
eight and more than eight components, partial/hidden occupancy, external
|
|
46
|
+
atoms, rotation, scaling, transparent/opaque polyhedra and quadrilateral
|
|
47
|
+
hatching.
|
|
48
|
+
- Installed-wheel smoke check: two viewers embedded in one host window,
|
|
49
|
+
synchronized orientations and continued rendering after one viewer closes.
|
|
50
|
+
- In one local synthetic scene containing 1,000 single-component atoms, first
|
|
51
|
+
CPU atom preparation decreased from 7.063 s to 0.00984 s; repeated preparation
|
|
52
|
+
decreased from 0.410 s to 0.00842 s. These single-run measurements exclude
|
|
53
|
+
GPU rendering and other scene layers; they are not hardware timing guarantees.
|
|
@@ -55,6 +55,23 @@ def sphere_mesh(
|
|
|
55
55
|
return np.asarray(positions, dtype=np.float32), np.asarray(normals, dtype=np.float32)
|
|
56
56
|
|
|
57
57
|
|
|
58
|
+
def indexed_sphere_mesh() -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
59
|
+
"""Index the default unit sphere without changing its triangle sequence.
|
|
60
|
+
|
|
61
|
+
Exact deduplication preserves even the original seam and pole coordinates.
|
|
62
|
+
Expanding the returned indices reproduces ``sphere_mesh(zeros(3), 1)``.
|
|
63
|
+
"""
|
|
64
|
+
positions, normals = sphere_mesh(np.zeros(3), 1.0)
|
|
65
|
+
vertices, indices = np.unique(
|
|
66
|
+
np.concatenate((positions, normals), axis=1), axis=0, return_inverse=True,
|
|
67
|
+
)
|
|
68
|
+
return (
|
|
69
|
+
np.ascontiguousarray(vertices[:, :3]),
|
|
70
|
+
np.ascontiguousarray(vertices[:, 3:]),
|
|
71
|
+
np.ascontiguousarray(indices, dtype=np.uint32),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
58
75
|
def cylinder_mesh(
|
|
59
76
|
first: np.ndarray,
|
|
60
77
|
second: np.ndarray,
|
|
@@ -133,4 +150,4 @@ def line_vertices(first: np.ndarray, second: np.ndarray) -> tuple[np.ndarray, np
|
|
|
133
150
|
return positions, normals
|
|
134
151
|
|
|
135
152
|
|
|
136
|
-
__all__ = ["cylinder_mesh", "line_vertices", "polygon_triangles", "sphere_mesh"]
|
|
153
|
+
__all__ = ["cylinder_mesh", "indexed_sphere_mesh", "line_vertices", "polygon_triangles", "sphere_mesh"]
|
|
@@ -21,6 +21,7 @@ from OpenGL.GL import (
|
|
|
21
21
|
GL_COMPILE_STATUS,
|
|
22
22
|
GL_DEPTH_BUFFER_BIT,
|
|
23
23
|
GL_DEPTH_TEST,
|
|
24
|
+
GL_ELEMENT_ARRAY_BUFFER,
|
|
24
25
|
GL_FALSE,
|
|
25
26
|
GL_FLOAT,
|
|
26
27
|
GL_FRAGMENT_SHADER,
|
|
@@ -34,6 +35,7 @@ from OpenGL.GL import (
|
|
|
34
35
|
GL_STATIC_DRAW,
|
|
35
36
|
GL_TRIANGLES,
|
|
36
37
|
GL_TRUE,
|
|
38
|
+
GL_UNSIGNED_INT,
|
|
37
39
|
GL_VERTEX_SHADER,
|
|
38
40
|
glAttachShader,
|
|
39
41
|
glBindBuffer,
|
|
@@ -54,6 +56,7 @@ from OpenGL.GL import (
|
|
|
54
56
|
glDepthMask,
|
|
55
57
|
glDisable,
|
|
56
58
|
glDrawArrays,
|
|
59
|
+
glDrawElementsInstanced,
|
|
57
60
|
glEnable,
|
|
58
61
|
glEnableVertexAttribArray,
|
|
59
62
|
glGenBuffers,
|
|
@@ -72,6 +75,7 @@ from OpenGL.GL import (
|
|
|
72
75
|
glUniformMatrix4fv,
|
|
73
76
|
glUseProgram,
|
|
74
77
|
glVertexAttribPointer,
|
|
78
|
+
glVertexAttribDivisor,
|
|
75
79
|
glViewport,
|
|
76
80
|
)
|
|
77
81
|
from PySide6.QtCore import QPointF, QRectF, QSize, Qt, Signal
|
|
@@ -79,7 +83,7 @@ from PySide6.QtGui import QBrush, QColor, QFontMetrics, QPainter, QPen, QPolygon
|
|
|
79
83
|
from PySide6.QtOpenGLWidgets import QOpenGLWidget
|
|
80
84
|
from PySide6.QtWidgets import QSizePolicy
|
|
81
85
|
|
|
82
|
-
from .gl_geometry import cylinder_mesh, line_vertices, polygon_triangles
|
|
86
|
+
from .gl_geometry import cylinder_mesh, indexed_sphere_mesh, line_vertices, polygon_triangles
|
|
83
87
|
from .hatching import hatch_division_count
|
|
84
88
|
from .models import DisplayOptions, Scene, StyleOverrides
|
|
85
89
|
from .qt_config import configure_qt_opengl, preferred_surface_format
|
|
@@ -98,8 +102,11 @@ _VERTEX_FLOATS = 11
|
|
|
98
102
|
_VERTEX_STRIDE = _VERTEX_FLOATS * 4
|
|
99
103
|
|
|
100
104
|
_ATOM_MAX_COMPONENTS = 8
|
|
101
|
-
|
|
102
|
-
|
|
105
|
+
# The base sphere uses position.xyz and normal.xyz once per mesh vertex.
|
|
106
|
+
_ATOM_MESH_STRIDE = 6 * 4
|
|
107
|
+
# Each atom stores center.xyz, radius, eight RGB colours and eight thresholds.
|
|
108
|
+
_ATOM_INSTANCE_FLOATS = 4 + 3 * _ATOM_MAX_COMPONENTS + _ATOM_MAX_COMPONENTS
|
|
109
|
+
_ATOM_INSTANCE_STRIDE = _ATOM_INSTANCE_FLOATS * 4
|
|
103
110
|
|
|
104
111
|
_CELL_DASH_PATTERN = (8.0, 4.0)
|
|
105
112
|
|
|
@@ -184,6 +191,8 @@ layout(location = 9) in vec3 a_colour7;
|
|
|
184
191
|
layout(location = 10) in vec4 a_cumulative0;
|
|
185
192
|
layout(location = 11) in vec4 a_cumulative1;
|
|
186
193
|
|
|
194
|
+
layout(location = 12) in vec4 a_center_radius;
|
|
195
|
+
|
|
187
196
|
uniform mat4 u_projection;
|
|
188
197
|
uniform mat3 u_orientation;
|
|
189
198
|
|
|
@@ -200,7 +209,7 @@ flat out vec4 v_cumulative0;
|
|
|
200
209
|
flat out vec4 v_cumulative1;
|
|
201
210
|
|
|
202
211
|
void main() {
|
|
203
|
-
vec3 camera_position = u_orientation * a_position;
|
|
212
|
+
vec3 camera_position = u_orientation * (a_center_radius.xyz + a_position * a_center_radius.w);
|
|
204
213
|
v_normal = u_orientation * a_normal;
|
|
205
214
|
v_colour0 = a_colour0;
|
|
206
215
|
v_colour1 = a_colour1;
|
|
@@ -498,36 +507,6 @@ def _vertex_block(
|
|
|
498
507
|
)
|
|
499
508
|
|
|
500
509
|
|
|
501
|
-
def _atom_vertex_block(
|
|
502
|
-
positions: np.ndarray,
|
|
503
|
-
normals: np.ndarray,
|
|
504
|
-
colours: np.ndarray,
|
|
505
|
-
cumulative: np.ndarray,
|
|
506
|
-
) -> np.ndarray:
|
|
507
|
-
positions = np.asarray(positions, dtype=np.float32).reshape(-1, 3)
|
|
508
|
-
normals = np.asarray(normals, dtype=np.float32).reshape(-1, 3)
|
|
509
|
-
colours = np.asarray(colours, dtype=np.float32).reshape(_ATOM_MAX_COMPONENTS, 3)
|
|
510
|
-
cumulative = np.asarray(cumulative, dtype=np.float32).reshape(_ATOM_MAX_COMPONENTS)
|
|
511
|
-
if positions.shape != normals.shape:
|
|
512
|
-
raise ValueError("positions and normals must have matching shapes")
|
|
513
|
-
count = positions.shape[0]
|
|
514
|
-
if count == 0:
|
|
515
|
-
return np.empty((0, _ATOM_VERTEX_FLOATS), dtype=np.float32)
|
|
516
|
-
colour_values = np.broadcast_to(colours.reshape(1, -1), (count, 3 * _ATOM_MAX_COMPONENTS))
|
|
517
|
-
cumulative_values = np.broadcast_to(cumulative.reshape(1, -1), (count, _ATOM_MAX_COMPONENTS))
|
|
518
|
-
return np.ascontiguousarray(
|
|
519
|
-
np.concatenate((positions, normals, colour_values, cumulative_values), axis=1),
|
|
520
|
-
dtype=np.float32,
|
|
521
|
-
)
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
def _combine_atom_blocks(blocks: list[np.ndarray]) -> np.ndarray:
|
|
525
|
-
nonempty = [block for block in blocks if block.size]
|
|
526
|
-
if not nonempty:
|
|
527
|
-
return np.empty((0, _ATOM_VERTEX_FLOATS), dtype=np.float32)
|
|
528
|
-
return np.ascontiguousarray(np.concatenate(nonempty, axis=0), dtype=np.float32)
|
|
529
|
-
|
|
530
|
-
|
|
531
510
|
def _combine_blocks(blocks: list[np.ndarray]) -> np.ndarray:
|
|
532
511
|
nonempty = [block for block in blocks if block.size]
|
|
533
512
|
if not nonempty:
|
|
@@ -593,6 +572,9 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
593
572
|
|
|
594
573
|
self._atom_vao = 0
|
|
595
574
|
self._atom_vbo = 0
|
|
575
|
+
self._atom_mesh_vbo = 0
|
|
576
|
+
self._atom_ebo = 0
|
|
577
|
+
self._atom_index_count = 0
|
|
596
578
|
self._atom_count = 0
|
|
597
579
|
self._opaque_vao = 0
|
|
598
580
|
self._opaque_vbo = 0
|
|
@@ -634,10 +616,8 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
634
616
|
self._bond_dirty = True
|
|
635
617
|
self._intersection_dirty = True
|
|
636
618
|
|
|
637
|
-
#
|
|
638
|
-
# switches.
|
|
639
|
-
# polyhedra, bonds, engraving, etc.
|
|
640
|
-
self._atom_mesh_cache: dict[tuple[int, float], tuple[np.ndarray, np.ndarray]] = {}
|
|
619
|
+
# Cylinder and face tessellation depends on the Scene, not on most
|
|
620
|
+
# display switches. Atoms use one shared GPU sphere instead.
|
|
641
621
|
self._bond_mesh_cache: dict[tuple[int, float], tuple[tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray]]] = {}
|
|
642
622
|
self._poly_face_mesh_cache: dict[tuple[int, int], tuple[np.ndarray, np.ndarray, np.ndarray]] = {}
|
|
643
623
|
self._hatch_mesh_cache: dict[int, OrderedDict[int, np.ndarray]] = {}
|
|
@@ -669,13 +649,11 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
669
649
|
self._atom_outline_dirty = True
|
|
670
650
|
self.update()
|
|
671
651
|
|
|
672
|
-
def _clear_mesh_caches(self
|
|
673
|
-
self.
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
self._hatch_mesh_cache.clear()
|
|
678
|
-
self._hatch_view_key = None
|
|
652
|
+
def _clear_mesh_caches(self) -> None:
|
|
653
|
+
self._bond_mesh_cache.clear()
|
|
654
|
+
self._poly_face_mesh_cache.clear()
|
|
655
|
+
self._hatch_mesh_cache.clear()
|
|
656
|
+
self._hatch_view_key = None
|
|
679
657
|
|
|
680
658
|
def set_scene(self, scene: Scene | None, *, reset_camera: bool = True) -> None:
|
|
681
659
|
if scene is not None and not isinstance(scene, Scene):
|
|
@@ -732,7 +710,6 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
732
710
|
if "show_external_atoms" in changed:
|
|
733
711
|
self._bond_dirty = True
|
|
734
712
|
if "atom_scale" in changed:
|
|
735
|
-
self._clear_mesh_caches(atoms_only=True)
|
|
736
713
|
self._atom_dirty = True
|
|
737
714
|
self._atom_outline_dirty = True
|
|
738
715
|
self._intersection_dirty = True
|
|
@@ -1003,31 +980,39 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1003
980
|
glBindVertexArray(0)
|
|
1004
981
|
return vao, vbo
|
|
1005
982
|
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
for location in
|
|
983
|
+
def _create_atom_buffer(self) -> None:
|
|
984
|
+
# Assign handles immediately so partial initialization can be cleaned up.
|
|
985
|
+
self._atom_vao = int(glGenVertexArrays(1))
|
|
986
|
+
self._atom_mesh_vbo = int(glGenBuffers(1))
|
|
987
|
+
self._atom_ebo = int(glGenBuffers(1))
|
|
988
|
+
self._atom_vbo = int(glGenBuffers(1))
|
|
989
|
+
glBindVertexArray(self._atom_vao)
|
|
990
|
+
positions, normals, indices = indexed_sphere_mesh()
|
|
991
|
+
mesh = np.ascontiguousarray(np.concatenate((positions, normals), axis=1))
|
|
992
|
+
glBindBuffer(GL_ARRAY_BUFFER, self._atom_mesh_vbo)
|
|
993
|
+
glBufferData(GL_ARRAY_BUFFER, mesh.nbytes, mesh, GL_STATIC_DRAW)
|
|
994
|
+
for location, offset in ((0, 0), (1, 3)):
|
|
995
|
+
glEnableVertexAttribArray(location)
|
|
996
|
+
glVertexAttribPointer(
|
|
997
|
+
location, 3, GL_FLOAT, GL_FALSE, _ATOM_MESH_STRIDE, ctypes.c_void_p(offset * 4)
|
|
998
|
+
)
|
|
999
|
+
glVertexAttribDivisor(location, 0)
|
|
1000
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._atom_ebo)
|
|
1001
|
+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
|
|
1002
|
+
self._atom_index_count = int(indices.size)
|
|
1003
|
+
|
|
1004
|
+
glBindBuffer(GL_ARRAY_BUFFER, self._atom_vbo)
|
|
1005
|
+
attributes = [(12, 4, 0)]
|
|
1006
|
+
attributes += [(location, 3, 4 + 3 * (location - 2)) for location in range(2, 10)]
|
|
1007
|
+
attributes += [(10, 4, 28), (11, 4, 32)]
|
|
1008
|
+
for location, size, offset in attributes:
|
|
1018
1009
|
glEnableVertexAttribArray(location)
|
|
1019
1010
|
glVertexAttribPointer(
|
|
1020
|
-
location,
|
|
1011
|
+
location, size, GL_FLOAT, GL_FALSE, _ATOM_INSTANCE_STRIDE, ctypes.c_void_p(offset * 4)
|
|
1021
1012
|
)
|
|
1022
|
-
|
|
1023
|
-
glEnableVertexAttribArray(10)
|
|
1024
|
-
glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, _ATOM_VERTEX_STRIDE, ctypes.c_void_p(offset * 4))
|
|
1025
|
-
offset += 4
|
|
1026
|
-
glEnableVertexAttribArray(11)
|
|
1027
|
-
glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, _ATOM_VERTEX_STRIDE, ctypes.c_void_p(offset * 4))
|
|
1013
|
+
glVertexAttribDivisor(location, 1)
|
|
1028
1014
|
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
|
1029
1015
|
glBindVertexArray(0)
|
|
1030
|
-
return vao, vbo
|
|
1031
1016
|
|
|
1032
1017
|
@staticmethod
|
|
1033
1018
|
def _upload_static(vao: int, vbo: int, vertices: np.ndarray) -> int:
|
|
@@ -1054,7 +1039,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1054
1039
|
_EDGE_FRAGMENT_SHADER,
|
|
1055
1040
|
_EDGE_GEOMETRY_SHADER,
|
|
1056
1041
|
)
|
|
1057
|
-
self.
|
|
1042
|
+
self._create_atom_buffer()
|
|
1058
1043
|
self._opaque_vao, self._opaque_vbo = self._create_buffer()
|
|
1059
1044
|
self._bond_vao, self._bond_vbo = self._create_buffer()
|
|
1060
1045
|
self._cell_vao, self._cell_vbo = self._create_buffer()
|
|
@@ -1097,14 +1082,6 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1097
1082
|
glDisable(GL_BLEND)
|
|
1098
1083
|
glEnable(GL_MULTISAMPLE)
|
|
1099
1084
|
|
|
1100
|
-
def _cached_atom_mesh(self, index: int, atom, center: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
|
1101
|
-
key = (int(index), round(float(self.atom_scale), 10))
|
|
1102
|
-
mesh = self._atom_mesh_cache.get(key)
|
|
1103
|
-
if mesh is None:
|
|
1104
|
-
mesh = sphere_mesh(center, self._atom_radius(atom))
|
|
1105
|
-
self._atom_mesh_cache[key] = mesh
|
|
1106
|
-
return mesh
|
|
1107
|
-
|
|
1108
1085
|
def _cached_bond_mesh(self, index: int, first: np.ndarray, second: np.ndarray, radius: float):
|
|
1109
1086
|
key = (int(index), round(float(radius), 10))
|
|
1110
1087
|
mesh = self._bond_mesh_cache.get(key)
|
|
@@ -1127,19 +1104,24 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1127
1104
|
self._poly_face_mesh_cache[key] = cached
|
|
1128
1105
|
return cached
|
|
1129
1106
|
|
|
1130
|
-
def
|
|
1107
|
+
def _build_atom_instances(self) -> np.ndarray:
|
|
1108
|
+
"""Prepare one compact record per visible atom, with no tessellation."""
|
|
1131
1109
|
if self.scene is None:
|
|
1132
|
-
return np.empty((0,
|
|
1133
|
-
|
|
1134
|
-
|
|
1110
|
+
return np.empty((0, _ATOM_INSTANCE_FLOATS), dtype=np.float32)
|
|
1111
|
+
instances = np.empty((len(self.scene.atoms), _ATOM_INSTANCE_FLOATS), dtype=np.float32)
|
|
1112
|
+
count = 0
|
|
1113
|
+
for atom, center in zip(self.scene.atoms, self.scene.atom_centers):
|
|
1135
1114
|
if not self._atom_visible(atom):
|
|
1136
1115
|
continue
|
|
1137
1116
|
if atom.external and not self.show_external_atoms:
|
|
1138
1117
|
continue
|
|
1139
|
-
positions, normals = self._cached_atom_mesh(atom_index, atom, center)
|
|
1140
1118
|
colours, cumulative = self._atom_pie_material(atom)
|
|
1141
|
-
|
|
1142
|
-
|
|
1119
|
+
instances[count, :3] = center
|
|
1120
|
+
instances[count, 3] = self._atom_radius(atom)
|
|
1121
|
+
instances[count, 4:28] = colours.ravel()
|
|
1122
|
+
instances[count, 28:] = cumulative
|
|
1123
|
+
count += 1
|
|
1124
|
+
return np.ascontiguousarray(instances[:count])
|
|
1143
1125
|
|
|
1144
1126
|
def _build_colour_bond_geometry(self) -> np.ndarray:
|
|
1145
1127
|
if self.scene is None:
|
|
@@ -1166,10 +1148,9 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1166
1148
|
def _build_static_geometry(self) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, list[_PolyhedronRange], np.ndarray, list[_PolyhedronRange], list[_PolyhedronRange]]:
|
|
1167
1149
|
if self.scene is None:
|
|
1168
1150
|
empty = np.empty((0, _VERTEX_FLOATS), dtype=np.float32)
|
|
1169
|
-
atom_empty = np.empty((0,
|
|
1151
|
+
atom_empty = np.empty((0, _ATOM_INSTANCE_FLOATS), dtype=np.float32)
|
|
1170
1152
|
return atom_empty, empty, empty.copy(), empty.copy(), [], empty.copy(), [], []
|
|
1171
1153
|
|
|
1172
|
-
atom_blocks: list[np.ndarray] = []
|
|
1173
1154
|
opaque_blocks: list[np.ndarray] = []
|
|
1174
1155
|
opaque_ranges: list[_PolyhedronRange] = []
|
|
1175
1156
|
cell_blocks: list[np.ndarray] = []
|
|
@@ -1277,7 +1258,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1277
1258
|
))
|
|
1278
1259
|
|
|
1279
1260
|
return (
|
|
1280
|
-
|
|
1261
|
+
np.empty((0, _ATOM_INSTANCE_FLOATS), dtype=np.float32),
|
|
1281
1262
|
_combine_blocks(opaque_blocks),
|
|
1282
1263
|
_combine_blocks(cell_blocks),
|
|
1283
1264
|
_combine_blocks(face_blocks),
|
|
@@ -1626,7 +1607,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1626
1607
|
self._bond_dirty = False
|
|
1627
1608
|
|
|
1628
1609
|
def _rebuild_atom_cache(self) -> None:
|
|
1629
|
-
atoms = self.
|
|
1610
|
+
atoms = self._build_atom_instances()
|
|
1630
1611
|
self._atom_count = self._upload_static(self._atom_vao, self._atom_vbo, atoms)
|
|
1631
1612
|
self._atom_dirty = False
|
|
1632
1613
|
|
|
@@ -1992,7 +1973,12 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
1992
1973
|
self._draw_buffer(self._bond_vao, self._bond_count, GL_TRIANGLES)
|
|
1993
1974
|
if self.show_atoms and self._atom_count > 0:
|
|
1994
1975
|
self._set_atom_uniforms()
|
|
1995
|
-
|
|
1976
|
+
glBindVertexArray(self._atom_vao)
|
|
1977
|
+
glDrawElementsInstanced(
|
|
1978
|
+
GL_TRIANGLES, self._atom_index_count, GL_UNSIGNED_INT,
|
|
1979
|
+
ctypes.c_void_p(0), self._atom_count,
|
|
1980
|
+
)
|
|
1981
|
+
glBindVertexArray(0)
|
|
1996
1982
|
self._set_frame_uniforms()
|
|
1997
1983
|
|
|
1998
1984
|
# Atom silhouette and atom/face intersection ink belong to the atom
|
|
@@ -2080,7 +2066,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
2080
2066
|
|
|
2081
2067
|
def _reset_gpu_objects(self) -> None:
|
|
2082
2068
|
for attribute in (
|
|
2083
|
-
"_atom_vbo", "_opaque_vbo", "_bond_vbo", "_cell_vbo",
|
|
2069
|
+
"_atom_vbo", "_atom_mesh_vbo", "_atom_ebo", "_opaque_vbo", "_bond_vbo", "_cell_vbo",
|
|
2084
2070
|
"_face_vbo", "_face_edge_vbo",
|
|
2085
2071
|
"_atom_outline_vbo", "_intersection_vbo", "_engraving_bond_vbo",
|
|
2086
2072
|
"_atom_vao", "_opaque_vao", "_bond_vao", "_cell_vao",
|
|
@@ -2099,7 +2085,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
2099
2085
|
):
|
|
2100
2086
|
setattr(self, attribute, -1)
|
|
2101
2087
|
for attribute in (
|
|
2102
|
-
"_atom_count", "_opaque_count", "_bond_count", "_cell_count",
|
|
2088
|
+
"_atom_count", "_atom_index_count", "_opaque_count", "_bond_count", "_cell_count",
|
|
2103
2089
|
"_face_count", "_face_edge_count", "_hatch_count",
|
|
2104
2090
|
"_atom_outline_count", "_intersection_count",
|
|
2105
2091
|
"_engraving_bond_count",
|
|
@@ -2134,7 +2120,7 @@ class OpenGLUnitCellViewer(QOpenGLWidget):
|
|
|
2134
2120
|
if can_delete:
|
|
2135
2121
|
self._delete_hatch_buffers()
|
|
2136
2122
|
for attribute in (
|
|
2137
|
-
"_atom_vbo", "_opaque_vbo", "_bond_vbo", "_cell_vbo",
|
|
2123
|
+
"_atom_vbo", "_atom_mesh_vbo", "_atom_ebo", "_opaque_vbo", "_bond_vbo", "_cell_vbo",
|
|
2138
2124
|
"_face_vbo", "_face_edge_vbo",
|
|
2139
2125
|
"_atom_outline_vbo", "_intersection_vbo",
|
|
2140
2126
|
"_engraving_bond_vbo",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: unit-cell-gui
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: PySide6 widget for rendering caller-supplied unit-cell scenes
|
|
5
5
|
Author-email: Mikhail Mirushchenko <miruschenko98@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -137,6 +137,25 @@ Per-position colours, visibility and opaque-polyhedron selections are replaced
|
|
|
137
137
|
atomically with `viewer.set_style_overrides(StyleOverrides(...))`. The widget
|
|
138
138
|
copies these mappings, so the host can keep its own independent UI state.
|
|
139
139
|
|
|
140
|
+
## Atom instancing
|
|
141
|
+
|
|
142
|
+
Atoms share one indexed sphere mesh with the same 16-by-24 tessellation as
|
|
143
|
+
previous releases. An OpenGL instance buffer stores each visible atom's
|
|
144
|
+
Cartesian centre, radius, eight RGB occupancy colours and cumulative occupancy
|
|
145
|
+
thresholds once, instead of repeating them for every triangle vertex. The atom
|
|
146
|
+
layer uses one `glDrawElementsInstanced` call.
|
|
147
|
+
|
|
148
|
+
The base sphere and its indices occupy 18,792 bytes per viewer; instance records
|
|
149
|
+
add 144 bytes per visible atom. For 1,000 atoms, these buffers total 162,792
|
|
150
|
+
bytes, compared with 328,320,000 bytes for the previous expanded atom buffer.
|
|
151
|
+
This comparison excludes bonds, outlines, polyhedra and Qt/driver overhead.
|
|
152
|
+
The number of sphere triangles is unchanged, so this is primarily a reduction
|
|
153
|
+
in geometry preparation and buffer storage, not a frame-rate guarantee.
|
|
154
|
+
|
|
155
|
+
OpenGL 3.3 core remains sufficient: the renderer uses 13 vertex attribute
|
|
156
|
+
locations, within the guaranteed minimum of 16. No C++ extension, new package
|
|
157
|
+
dependency or host-application API change is required.
|
|
158
|
+
|
|
140
159
|
## Visibility and rendering caches
|
|
141
160
|
|
|
142
161
|
Both global and per-site polyhedron visibility filter already prepared geometry
|
|
@@ -4,6 +4,7 @@ LICENSE
|
|
|
4
4
|
MANIFEST.in
|
|
5
5
|
README.md
|
|
6
6
|
RELEASE_NOTES_0.2.2.md
|
|
7
|
+
RELEASE_NOTES_0.2.3.md
|
|
7
8
|
THIRD_PARTY_NOTICES.txt
|
|
8
9
|
pyproject.toml
|
|
9
10
|
src/unit_cell_gui/__init__.py
|
|
@@ -24,4 +25,5 @@ src/unit_cell_gui.egg-info/top_level.txt
|
|
|
24
25
|
tests/test_gl_geometry.py
|
|
25
26
|
tests/test_render_geometry.py
|
|
26
27
|
tests/test_unit_cell_gui.py
|
|
27
|
-
tests/test_visibility_cache.py
|
|
28
|
+
tests/test_visibility_cache.py
|
|
29
|
+
tests/test_z_atom_instancing.py
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Atom data compatibility and real OpenGL buffer/lifecycle regressions."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import unittest
|
|
5
|
+
from unittest.mock import patch
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
10
|
+
|
|
11
|
+
from PySide6.QtWidgets import QApplication
|
|
12
|
+
from PySide6.QtGui import QImage
|
|
13
|
+
import shiboken6
|
|
14
|
+
from unit_cell_gui import Atom, AtomComponent, Scene, StyleOverrides, UnitCellViewer
|
|
15
|
+
from unit_cell_gui import opengl_viewer as renderer
|
|
16
|
+
from unit_cell_gui.gl_geometry import indexed_sphere_mesh, sphere_mesh
|
|
17
|
+
from test_unit_cell_gui import rotation, sample_scene
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def atom_scene():
|
|
21
|
+
prototype = sample_scene()
|
|
22
|
+
components = tuple(
|
|
23
|
+
AtomComponent(f"c{i}", f"C{i}", "C", .1, f"#{i * 25:02x}80a0", .15 + i * .03)
|
|
24
|
+
for i in range(8)
|
|
25
|
+
)
|
|
26
|
+
return Scene(
|
|
27
|
+
(Atom("mixed", "Mixed", components),
|
|
28
|
+
Atom("external", "External", (components[0],), external=True)),
|
|
29
|
+
[[-.3, .2, .1], [.8, -.4, .2]], [],
|
|
30
|
+
prototype.cell_vertices, prototype.cell_edges, prototype.basis_vectors,
|
|
31
|
+
base_radius=2.,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class AtomInstanceTests(unittest.TestCase):
|
|
36
|
+
@classmethod
|
|
37
|
+
def setUpClass(cls):
|
|
38
|
+
cls.app = QApplication.instance() or QApplication([])
|
|
39
|
+
|
|
40
|
+
def setUp(self):
|
|
41
|
+
self.viewer = UnitCellViewer()
|
|
42
|
+
self.viewer.set_scene(atom_scene())
|
|
43
|
+
|
|
44
|
+
def tearDown(self):
|
|
45
|
+
self.viewer.close()
|
|
46
|
+
|
|
47
|
+
def test_indices_reproduce_original_triangle_positions_and_normals(self):
|
|
48
|
+
positions, normals, indices = indexed_sphere_mesh()
|
|
49
|
+
original_positions, original_normals = sphere_mesh(np.zeros(3), 1.)
|
|
50
|
+
np.testing.assert_array_equal(positions[indices], original_positions)
|
|
51
|
+
np.testing.assert_array_equal(normals[indices], original_normals)
|
|
52
|
+
self.assertLess(len(positions), len(original_positions) / 4)
|
|
53
|
+
self.assertEqual(indices.dtype, np.uint32)
|
|
54
|
+
|
|
55
|
+
def test_instances_preserve_coordinates_radius_eight_sectors_and_partial_occupancy(self):
|
|
56
|
+
data = self.viewer._build_atom_instances()
|
|
57
|
+
self.assertEqual(data.shape, (2, 36))
|
|
58
|
+
self.assertEqual(data.nbytes, 2 * 144)
|
|
59
|
+
self.assertTrue(data.flags.c_contiguous)
|
|
60
|
+
np.testing.assert_allclose(data[:, :3], self.viewer.scene.atom_centers)
|
|
61
|
+
# Weighted radius of the eight components; external atom has its own radius.
|
|
62
|
+
np.testing.assert_allclose(data[:, 3], [.255, .15])
|
|
63
|
+
np.testing.assert_allclose(data[0, 28:], np.arange(1, 9) * .1)
|
|
64
|
+
np.testing.assert_allclose(data[1, 28:], .1)
|
|
65
|
+
np.testing.assert_allclose(data[0, 4:28].reshape(8, 3),
|
|
66
|
+
[[i * 25 / 255, 128 / 255, 160 / 255] for i in range(8)], atol=1e-6)
|
|
67
|
+
|
|
68
|
+
def test_visibility_colours_scale_and_engraving_keep_instance_order_and_materials(self):
|
|
69
|
+
self.viewer.set_display_options(show_external_atoms=False, atom_scale=2.)
|
|
70
|
+
self.viewer.set_style_overrides(StyleOverrides(
|
|
71
|
+
atom_component_visibility={"c1": False}, atom_component_colours={"c7": "#ff0000"},
|
|
72
|
+
))
|
|
73
|
+
data = self.viewer._build_atom_instances()
|
|
74
|
+
self.assertEqual(len(data), 1)
|
|
75
|
+
self.assertAlmostEqual(float(data[0, 3]), .51, places=6)
|
|
76
|
+
np.testing.assert_array_equal(data[0, 7:10], [1, 1, 1])
|
|
77
|
+
np.testing.assert_array_equal(data[0, 25:28], [1, 0, 0])
|
|
78
|
+
self.viewer.set_display_options(engraving=True)
|
|
79
|
+
engraved = self.viewer._build_atom_instances()
|
|
80
|
+
np.testing.assert_array_equal(engraved[0, 28:], np.ones(8))
|
|
81
|
+
self.assertEqual(engraved[0, 4], engraved[0, 5])
|
|
82
|
+
self.assertEqual(engraved[0, 5], engraved[0, 6])
|
|
83
|
+
self.viewer.set_style_overrides(StyleOverrides(atom_component_visibility={f"c{i}": False for i in range(8)}))
|
|
84
|
+
self.assertEqual(self.viewer._build_atom_instances().shape, (0, 36))
|
|
85
|
+
self.viewer.set_scene(None)
|
|
86
|
+
self.assertEqual(self.viewer._build_atom_instances().shape, (0, 36))
|
|
87
|
+
|
|
88
|
+
def test_camera_and_show_atoms_do_not_invalidate_instances(self):
|
|
89
|
+
self.viewer._atom_dirty = False
|
|
90
|
+
self.viewer.set_orientation(rotation(1, 35))
|
|
91
|
+
self.viewer.zoom_by(1)
|
|
92
|
+
self.viewer.set_display_options(show_atoms=False)
|
|
93
|
+
self.assertFalse(self.viewer._atom_dirty)
|
|
94
|
+
self.viewer.set_display_options(show_atoms=True)
|
|
95
|
+
self.assertFalse(self.viewer._atom_dirty)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class OpenGLAtomInstanceTests(unittest.TestCase):
|
|
99
|
+
@classmethod
|
|
100
|
+
def setUpClass(cls):
|
|
101
|
+
cls.app = QApplication.instance() or QApplication([])
|
|
102
|
+
if cls.app.platformName() in {"offscreen", "minimal"}:
|
|
103
|
+
raise unittest.SkipTest("A window system with OpenGL is required")
|
|
104
|
+
|
|
105
|
+
def setUp(self):
|
|
106
|
+
self.viewer = UnitCellViewer()
|
|
107
|
+
self.viewer.resize(640, 480)
|
|
108
|
+
self.viewer.set_scene(atom_scene())
|
|
109
|
+
self.viewer.set_display_options(engraving=True)
|
|
110
|
+
self.viewer.show()
|
|
111
|
+
self.app.processEvents()
|
|
112
|
+
if not self.viewer.isValid():
|
|
113
|
+
self.viewer.close()
|
|
114
|
+
self.skipTest("No OpenGL context could be created")
|
|
115
|
+
self.assertIsNone(self.viewer.opengl_error)
|
|
116
|
+
|
|
117
|
+
def tearDown(self):
|
|
118
|
+
self.viewer.close()
|
|
119
|
+
shiboken6.delete(self.viewer)
|
|
120
|
+
self.app.processEvents()
|
|
121
|
+
|
|
122
|
+
def pixels(self):
|
|
123
|
+
image = self.viewer.grabFramebuffer().convertToFormat(QImage.Format.Format_RGBA8888)
|
|
124
|
+
self.assertIsNone(self.viewer.opengl_error)
|
|
125
|
+
self.assertFalse(image.isNull())
|
|
126
|
+
return np.frombuffer(image.bits(), dtype=np.uint8, count=image.sizeInBytes()).copy()
|
|
127
|
+
|
|
128
|
+
def test_instance_divisors_buffer_sizes_and_index_binding(self):
|
|
129
|
+
from OpenGL.GL import (
|
|
130
|
+
GL_ARRAY_BUFFER, GL_BUFFER_SIZE, GL_ELEMENT_ARRAY_BUFFER,
|
|
131
|
+
GL_ELEMENT_ARRAY_BUFFER_BINDING, GL_VERTEX_ATTRIB_ARRAY_DIVISOR,
|
|
132
|
+
glBindBuffer, glBindVertexArray, glGetBufferParameteriv,
|
|
133
|
+
glGetIntegerv, glGetVertexAttribiv,
|
|
134
|
+
)
|
|
135
|
+
self.pixels()
|
|
136
|
+
self.viewer.makeCurrent()
|
|
137
|
+
try:
|
|
138
|
+
glBindVertexArray(self.viewer._atom_vao)
|
|
139
|
+
self.assertEqual(int(glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING)), self.viewer._atom_ebo)
|
|
140
|
+
for location in range(13):
|
|
141
|
+
divisor = np.asarray(glGetVertexAttribiv(location, GL_VERTEX_ATTRIB_ARRAY_DIVISOR)).ravel()[0]
|
|
142
|
+
self.assertEqual(divisor, 0 if location < 2 else 1)
|
|
143
|
+
glBindBuffer(GL_ARRAY_BUFFER, self.viewer._atom_vbo)
|
|
144
|
+
self.assertEqual(int(glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE)), self.viewer._atom_count * 144)
|
|
145
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.viewer._atom_ebo)
|
|
146
|
+
self.assertEqual(int(glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE)), 2160 * 4)
|
|
147
|
+
glBindVertexArray(0)
|
|
148
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
|
149
|
+
finally:
|
|
150
|
+
self.viewer.doneCurrent()
|
|
151
|
+
|
|
152
|
+
def test_indexed_draw_and_material_changes_restore_frame_without_mesh_upload(self):
|
|
153
|
+
before = self.pixels()
|
|
154
|
+
mesh = self.viewer._atom_mesh_vbo
|
|
155
|
+
with patch.object(renderer, "glDrawElementsInstanced", wraps=renderer.glDrawElementsInstanced) as draw, \
|
|
156
|
+
patch.object(self.viewer, "_create_atom_buffer", wraps=self.viewer._create_atom_buffer) as create:
|
|
157
|
+
self.viewer.set_display_options(atom_scale=1.4, engraving=False)
|
|
158
|
+
self.assertFalse(np.array_equal(self.pixels(), before))
|
|
159
|
+
self.viewer.set_display_options(atom_scale=1., engraving=True)
|
|
160
|
+
np.testing.assert_array_equal(self.pixels(), before)
|
|
161
|
+
create.assert_not_called()
|
|
162
|
+
self.assertEqual(self.viewer._atom_mesh_vbo, mesh)
|
|
163
|
+
self.assertTrue(draw.called)
|
|
164
|
+
self.assertEqual(draw.call_args.args[1], 2160)
|
|
165
|
+
self.assertEqual(draw.call_args.args[4], self.viewer._atom_count)
|
|
166
|
+
|
|
167
|
+
def test_atom_buffers_cleanup_reinitialization_and_empty_scene(self):
|
|
168
|
+
from OpenGL.GL import glIsBuffer
|
|
169
|
+
before = self.pixels()
|
|
170
|
+
buffers = [self.viewer._atom_vbo, self.viewer._atom_mesh_vbo, self.viewer._atom_ebo]
|
|
171
|
+
self.viewer.cleanup()
|
|
172
|
+
self.viewer.makeCurrent()
|
|
173
|
+
try:
|
|
174
|
+
self.assertTrue(all(not glIsBuffer(buffer) for buffer in buffers))
|
|
175
|
+
self.assertEqual(self.viewer._atom_index_count, 0)
|
|
176
|
+
self.viewer.initializeGL()
|
|
177
|
+
finally:
|
|
178
|
+
self.viewer.doneCurrent()
|
|
179
|
+
np.testing.assert_array_equal(self.pixels(), before)
|
|
180
|
+
self.viewer.set_scene(None)
|
|
181
|
+
self.pixels()
|
|
182
|
+
with patch.object(renderer, "glDrawElementsInstanced", wraps=renderer.glDrawElementsInstanced) as draw:
|
|
183
|
+
self.pixels()
|
|
184
|
+
draw.assert_not_called()
|
|
185
|
+
self.viewer.set_scene(atom_scene())
|
|
186
|
+
self.pixels()
|
|
187
|
+
self.assertEqual(self.viewer._atom_count, 2)
|
|
188
|
+
self.assertIsNone(self.viewer.opengl_error)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|