Merging#
Merging many small objects into a few shared pygfx objects, which is much faster to render. See Merging objects for how to use it; this is the machinery underneath.
The entry points on the viewer are Viewer.merge_objects, Viewer.unmerge_objects and Viewer.merge_stats, plus the merge parameter of Viewer and of the add methods.
octarine.handles.MergeStats dataclass #
What merging currently costs and saves on a canvas.
| ATTRIBUTE | DESCRIPTION |
|---|---|
objects | Objects on the canvas, merged or not. TYPE: |
visuals | pygfx objects the renderer actually walks. This is the number merging exists to bring down. TYPE: |
merged | How many of those visuals hold more than one member. TYPE: |
members | Objects currently living inside a merged visual. TYPE: |
largest | Members in the biggest merged visual. TYPE: |
split_outs | Members that have been split back out since the canvas was created, because something was asked of them that a merged object cannot express. A number that climbs during normal use means the merge is being eroded - see TYPE: |
Source code in octarine/handles.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
octarine.handles.SubVisualHandle #
One object inside a merged visual, addressable as if it were its own.
| PARAMETER | DESCRIPTION |
|---|---|
manager | TYPE: |
visual | TYPE: |
sub | TYPE: |
See Also
merge.SubVisual The record this wraps.
Source code in octarine/handles.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
get_bounding_box() #
This member's axis-aligned bounds, in the merged visual's space.
Computed from the member's own positions rather than the merged object's, and from the stash while it is hidden - Viewer.get_bounds documents itself as covering visible and invisible objects, and a hidden member's vertices are NaN in the buffer.
Deliberately pygfx's own points_to_bounds: it is what every real visual's bounds go through, it already drops non-finite vertices (which a merged line's gap vertices and a hidden member's are), and going through it is what keeps a handle's bounds identical to those of the unmerged object it stands in for.
Source code in octarine/handles.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
get_world_bounding_box() #
This member's axis-aligned bounds, in world space.
Source code in octarine/handles.py
357 358 359 360 361 362 363 364 365 | |
octarine.merge.merge_visuals(visuals, max_size=None, max_nodes=_MAX_MERGED_NODES) #
Merge compatible visuals into fewer pygfx objects.
Lines, points and meshes are merged; everything else - volumes, text, tubes, and any visual whose material or geometry we can't concatenate - is passed through untouched and in place, as are visuals that end up alone in their group.
Visuals are merged only if they agree on everything the merged object can hold just once: the material's uniforms and blend/depth state, the object's transform, visibility and render order. Colour is the exception, along with line width and point size - where members disagree on those, they move into the geometry as per-vertex buffers.
Merging is destructive to the per-object handles the viewer relies on (material.color, visual.visible, one _object_id per object); those are replaced by the vertex ranges in visual._sub_visuals. See SubVisual for how to address an individual member.
| PARAMETER | DESCRIPTION |
|---|---|
visuals | TYPE: |
max_size | TYPE: |
max_nodes | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list of pygfx visuals | The merged visuals. Merged ones carry a |
See Also
SubVisual Addressing an individual object inside a merged visual.
Source code in octarine/merge.py
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | |
octarine.merge.split_visual(merged, object_ids=None, extract=True) #
Split members back out of a merged visual.
The inverse of merge_visuals, to the extent that one exists: the members are decomposed exactly as a re-merge would decompose them and rebuilt as visuals of their own. What is not restored is the material class a member had before merging - a gfx.LineMaterial line that merged into a FlexLineMaterial splits back out as a FlexLineMaterial, since the merged visual is all the split has to go on.
Members sharing an object_id stay together in one visual, because that is what the viewer addresses as a single object.
| PARAMETER | DESCRIPTION |
|---|---|
merged | TYPE: |
object_ids | TYPE: |
extract | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
remainder | A visual holding the members that were not extracted, or TYPE: |
extracted | One visual per extracted TYPE: |
See Also
merge_visuals The operation this undoes.
Source code in octarine/merge.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 | |
octarine.merge.sub_visual_at(merged, pick_info) #
Which member of a merged visual a pick landed on.
Picking reports the merged visual as the world object, so the member has to be recovered from the index that comes with it: the face index for meshes, which is what the merged indices buffer is laid out by, and the vertex index for points and lines.
| PARAMETER | DESCRIPTION |
|---|---|
merged | TYPE: |
pick_info | TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
SubVisual | None | The member that was hit, or |
Source code in octarine/merge.py
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 | |
octarine.merge.SubVisual dataclass #
One of the original visuals inside a visual merged by merge_visuals.
Merging turns N objects into one, which means per-object operations (hiding, recolouring, picking) can no longer address an object as a whole. Instead each original visual becomes a contiguous range of vertices in the merged buffers, and this is the record of where it went::
sub = merged._sub_visuals[3]
merged.geometry.colors.data[sub.slice] = (1, 0, 0, 1)
merged.geometry.colors.update_range(sub.offset, sub.count)
Hiding works the same way, by writing NaN into the position slice - those vertices then drop out for lines, points and meshes alike, exactly like the NaN breaks that separate merged lines. Note that this is destructive, so callers that want to unhide again have to keep a copy of the slice.
Merged meshes additionally own a range of faces, which is what picking reports and what the merged indices buffer is laid out by.
| ATTRIBUTE | DESCRIPTION |
|---|---|
object_id | The |
group | The TYPE: |
offset | Index of the member's first vertex in the merged buffers. TYPE: |
count | Number of vertices the member occupies. For lines this excludes the NaN vertex that separates it from the next member. TYPE: |
face_offset | Index of the member's first face. Meshes only. TYPE: |
face_count | Number of faces the member occupies. Meshes only. TYPE: |
Source code in octarine/merge.py
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 | |