Scene assembly¶
gutenberg_kg.scene
¶
scene.py — Qt-free scene builder for the GutenbergKG 3-D knowledge tree forest.
Everything here composes a forest into a plain pv.Plotter: corpus scanning,
glyph/edge geometry, :func:build_forest_scene and :func:build_tree_scene.
No PyQt, no QApplication.processEvents — the interactive viewer
(:mod:gutenberg_kg.viz3d) is one caller of this module and the off-screen
light-field renderer (gutenkg quilt) is another.
The placement half — :class:ForestLayout, the seasons, and
:func:grow_tree_geometry — lives in :mod:gutenberg_kg.treegeom, which
imports no PyVista. That is what lets :mod:gutenberg_kg.povscene emit the
same tree as analytic POV-Ray primitives without a VTK stack. Every public
name from there is re-exported below, so importing it from this module keeps
working.
Progress reporting is a plain Callable[[str], None] so a Qt caller can pump
its event loop and a headless caller can print or ignore.
Author: Eric G. Suchanek, PhD
BookMeta(title, genre, book_dir)
dataclass
¶
Lightweight metadata for one corpus book.
ForestLayout(grove_inner_radius=80.0, grove_outer_radius=240.0, book_inner_radius=5.0, book_outer_radius=18.0, trunk_scale=4.0, max_trunk_height=45.0, branch_radius=5.0, leaf_radius=1.5, canopy_lift=4.0, book_genre_map=None, entry_times=None)
¶
Bases: Layout3D
3-D forest layout for a multi-book corpus.
Spatial structure¶
- Genres are placed in a large Fibonacci annulus in the XY plane (each genre → a grove at a fixed XY centre).
- Books within a genre are placed in a medium Fibonacci annulus around the genre centre.
- Per book (tree):
documentnode sits at the book's XY position,Z = 0(trunk base marker).sectionnodes are distributed on a Fibonacci upper hemisphere centred at the trunk apex(bx, by, trunk_height). Trunk height scales aslog2(1 + n_chunks).chunknodes cluster around their parent section node in a small Fibonacci sphere.entity/topicnodes float in a loose cloud above the canopy atZ = trunk_height + canopy_lift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grove_inner_radius
|
float
|
Inner radius for genre placement ring. |
80.0
|
grove_outer_radius
|
float
|
Minimum outer radius for genre ring. |
240.0
|
book_inner_radius
|
float
|
Inner radius for books within a genre grove. |
5.0
|
book_outer_radius
|
float
|
Minimum outer radius within a genre grove. |
18.0
|
trunk_scale
|
float
|
Multiplier for trunk height ( |
4.0
|
branch_radius
|
float
|
Base radius of the Fibonacci hemisphere for sections. |
5.0
|
leaf_radius
|
float
|
Base radius of the leaf sphere per section. |
1.5
|
canopy_lift
|
float
|
Z offset above trunk apex for entity / topic nodes. |
4.0
|
book_genre_map
|
dict[str, str] | None
|
|
None
|
entry_times
|
dict[str, str] | None
|
|
None
|
Store layout radii/scale parameters; see class docstring for their roles.
Source code in src/gutenberg_kg/treegeom.py
469 470 471 472 473 474 475 476 477 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 510 511 512 513 514 | |
compute(nodes, edges)
¶
Compute 3-D positions for every node in the combined forest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[LayoutNode]
|
All nodes across all loaded books (IDs already namespaced). |
required |
edges
|
list[LayoutEdge]
|
All edges across all loaded books. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Mapping from node ID to |
Source code in src/gutenberg_kg/treegeom.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 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 | |
SceneFilters(show_sections=True, show_chunks=True, show_entities=False, show_topics=False, show_contains=False, show_similar=False, show_next=False)
dataclass
¶
Which node kinds and edge relations the scene should draw.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_sections
|
bool
|
Draw |
True
|
show_chunks
|
bool
|
Draw |
True
|
show_entities
|
bool
|
Draw |
False
|
show_topics
|
bool
|
Draw |
False
|
show_contains
|
bool
|
Draw |
False
|
show_similar
|
bool
|
Draw |
False
|
show_next
|
bool
|
Draw |
False
|
visible_kinds()
¶
Node kinds this filter set admits (document is always drawn).
Source code in src/gutenberg_kg/treegeom.py
934 935 936 937 938 939 940 941 942 943 944 945 | |
visible_rels()
¶
Edge relations this filter set admits.
Source code in src/gutenberg_kg/treegeom.py
947 948 949 950 951 952 953 954 955 956 | |
SceneInfo(title, actor_to_node=dict(), positions=dict(), layout=None, lod='high', counts=Counter(), n_books=0, skeleton=None, trunk_height=0.0)
dataclass
¶
What a built scene contains, for callers that need to interrogate it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
One-line stats banner (window title / CLI report). |
required |
actor_to_node
|
dict[str, dict]
|
|
dict()
|
positions
|
dict[str, ndarray]
|
Node ID → |
dict()
|
layout
|
ForestLayout | None
|
The :class: |
None
|
lod
|
str
|
LOD tier actually used — |
'high'
|
counts
|
Counter
|
Rendered node count per kind. |
Counter()
|
n_books
|
int
|
Number of distinct book slugs in the scene. |
0
|
skeleton
|
Skeleton | None
|
Grown tree skeleton, set only by :func: |
None
|
trunk_height
|
float
|
Schematic trunk height of the hero tree, in scene units — a useful focal-plane height for framing. |
0.0
|
Season(foliage, density=1.0, wood=WOOD_COLOR, sky=(SKY_BOTTOM, SKY_TOP))
dataclass
¶
A seasonal look for the foliage, wood, and sky.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foliage
|
tuple[str, ...]
|
Leaf colours, sampled per leaf so a canopy varies the way a real one does rather than reading as one flat green. |
required |
density
|
float
|
Fraction of chunks that keep a leaf. Winter drops most of them, which is the point — bare wood is where the pipe model shows. |
1.0
|
wood
|
str
|
Branch colour. |
WOOD_COLOR
|
sky
|
tuple[str, str]
|
Background gradient, |
(SKY_BOTTOM, SKY_TOP)
|
TreeGeometry(skeleton, layout, positions, crown, palette, leaf_points, leaf_tint, leaf_radius, spores, title, counts, trunk_height, species=DEFAULT_SPECIES)
dataclass
¶
Where a book's wood, leaves and spores go, with nothing drawn yet.
The placement half of :func:build_tree_scene, separated out so a second
renderer can reach it. Everything here is plain NumPy and hex colour
strings: no pv.Plotter, no meshes, no tessellation. PyVista turns it
into actors; :mod:gutenberg_kg.povscene turns the same numbers into
POV-Ray primitives, and because both start here the two backends cannot
quietly grow different trees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
Skeleton
|
Grown skeleton, radii already assigned. |
required |
layout
|
ForestLayout
|
The :class: |
required |
positions
|
dict[str, ndarray]
|
Node ID → position, shifted so the tree stands at the origin. |
required |
crown
|
ndarray
|
|
required |
palette
|
Season
|
The :class: |
required |
leaf_points
|
ndarray
|
|
required |
leaf_tint
|
ndarray
|
|
required |
leaf_radius
|
float
|
Leaf radius after density scaling, in scene units. |
required |
spores
|
dict[str, tuple[ndarray, float]]
|
|
required |
title
|
str
|
One-line stats banner. |
required |
counts
|
Counter
|
Node count per kind. |
required |
trunk_height
|
float
|
Schematic trunk height, a useful focal-plane height. |
required |
species
|
str
|
The tree species (:data: |
DEFAULT_SPECIES
|
leaf_colors
property
¶
Returns:
| Type | Description |
|---|---|
list[str]
|
One hex colour per leaf, resolved through the palette. |
arc_points(p1, p2, n_pts=24, lift=0.35)
¶
Quadratic Bézier arc from p1 to p2, apex lifted in Z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
ndarray
|
Start point. |
required |
p2
|
ndarray
|
End point. |
required |
n_pts
|
int
|
Sample count. |
24
|
lift
|
float
|
Z lift as fraction of chord length. |
0.35
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/gutenberg_kg/scene.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
bark_texture_path(species)
¶
The species' bark colour map, shipped in gutenberg_kg/assets/bark/.
CC0 textures from ambientCG, the same files the Knowledge Press web
forest uses; see that directory's CREDITS.md.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
str
|
A key of :data: |
required |
Returns:
| Type | Description |
|---|---|
Path | None
|
The image path, or |
Source code in src/gutenberg_kg/treegeom.py
221 222 223 224 225 226 227 228 229 230 231 232 | |
build_forest_scene(nodes, edges, plotter, *, book_genre_map=None, entry_times=None, layout=None, filters=None, lod=None, ground_size=1000.0, frame_camera=True, progress=None)
¶
Compose the knowledge tree forest into plotter.
Draws the forest floor, per-book trunk cylinders, branch lines, node glyphs (LOD-aware, one draw call per kind), and the selected structural edges. Positions come from layout, computed over all nodes so the geometry is stable regardless of which kinds filters admits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[LayoutNode]
|
All loaded nodes (IDs namespaced by book slug). |
required |
edges
|
list[LayoutEdge]
|
All loaded edges. |
required |
plotter
|
Plotter
|
Any |
required |
book_genre_map
|
dict[str, str] | None
|
|
None
|
entry_times
|
dict[str, str] | None
|
|
None
|
layout
|
ForestLayout | None
|
Layout instance to reuse; a default
:class: |
None
|
filters
|
SceneFilters | None
|
Node/edge visibility; defaults to sections + chunks. |
None
|
lod
|
str | None
|
Force an LOD tier; derived from visible node count when omitted. |
None
|
ground_size
|
float
|
Edge length of the ground plane in scene units;
|
1000.0
|
frame_camera
|
bool
|
Reset the camera to the isometric framing the
interactive viewer expects. Pass |
True
|
progress
|
Callable[[str], None] | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
SceneInfo
|
A :class: |
Source code in src/gutenberg_kg/scene.py
229 230 231 232 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 458 459 | |
build_tree_scene(nodes, edges, plotter, *, slug, genre='unknown', entry_times=None, filters=None, season=DEFAULT_SEASON, tip_radius=0.05, leaf_size=0.32, ground_size=0.0, species_look=True, progress=None)
¶
Compose a single book as an organically grown tree.
The schematic layout places the crown; space colonization then grows a skeleton to reach it, so every limb is a real structural path and the canopy's shape is the book's shape. This is the Tier-1 light-field subject: one tree, standing at the origin, framed by the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[LayoutNode]
|
Nodes of one book (IDs namespaced by slug). |
required |
edges
|
list[LayoutEdge]
|
Edges of the same book. |
required |
plotter
|
Plotter
|
Off-screen or interactive plotter. |
required |
slug
|
str
|
Book slug; seeds growth so the tree is reproducible. |
required |
genre
|
str
|
Genre name, which selects the tropism silhouette. |
'unknown'
|
entry_times
|
dict[str, str] | None
|
|
None
|
filters
|
SceneFilters | None
|
Only |
None
|
season
|
str
|
Key into :data: |
DEFAULT_SEASON
|
tip_radius
|
float
|
Radius of leaf-bearing twigs, in scene units. |
0.05
|
leaf_size
|
float
|
Leaf glyph radius. |
0.32
|
ground_size
|
float
|
Ground plane edge length; |
0.0
|
species_look
|
bool
|
Draw the wood as one continuous sweep textured with
the species' bark (:func: |
True
|
progress
|
Callable[[str], None] | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
SceneInfo
|
:class: |
Source code in src/gutenberg_kg/scene.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
glyph_proto(kind, size, lod)
¶
Return a glyph prototype mesh centred at the origin for kind nodes.
Used with pv.PolyData.glyph() so all nodes of one kind are rendered
in a single VTK draw call instead of one mesh per node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Node kind string. |
required |
size
|
float
|
Node radius. |
required |
lod
|
str
|
LOD tier — |
required |
Returns:
| Type | Description |
|---|---|
PolyData
|
PyVista PolyData centred at origin. |
Source code in src/gutenberg_kg/scene.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
grow_tree_geometry(nodes, edges, *, slug, genre='unknown', entry_times=None, filters=None, season=DEFAULT_SEASON, tip_radius=0.05, leaf_size=0.32, progress=None)
¶
Lay out one book and grow a skeleton to reach it, without drawing anything.
The schematic layout places the crown; space colonization then grows a skeleton to reach it, so every limb is a real structural path and the canopy's shape is the book's shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[LayoutNode]
|
Nodes of one book (IDs namespaced by slug). |
required |
edges
|
list[LayoutEdge]
|
Edges of the same book. |
required |
slug
|
str
|
Book slug; seeds growth so the tree is reproducible. |
required |
genre
|
str
|
Genre name, which selects the tree species
(:data: |
'unknown'
|
entry_times
|
dict[str, str] | None
|
|
None
|
filters
|
SceneFilters | None
|
Only |
None
|
season
|
str
|
Key into :data: |
DEFAULT_SEASON
|
tip_radius
|
float
|
Radius of leaf-bearing twigs, in scene units. |
0.05
|
leaf_size
|
float
|
Leaf radius before density scaling. |
0.32
|
progress
|
Callable[[str], None] | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
TreeGeometry
|
The placed :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If season is unknown, or the book has no chunks to grow toward. |
Source code in src/gutenberg_kg/treegeom.py
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 | |
load_book_graph(meta)
¶
Load nodes and edges from a book's DocKG SQLite, prefixing all IDs with the book slug to prevent collisions when merging multiple books.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
BookMeta
|
Book metadata including slug and db_path. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[LayoutNode], list[LayoutEdge]]
|
|
Source code in src/gutenberg_kg/bookgraph.py
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 | |
load_entry_times(meta)
¶
Earliest chunk timestamp per document, for books that carry dates.
DiaryKG stamps every chunk with the entry's date but leaves the document
row's timestamp null, so the entry's date has to come up through its
chunks. The result lets :class:ForestLayout grow a diary's limbs from
its chronology — one limb per year — rather than from position in the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
BookMeta
|
Book metadata. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
|
Source code in src/gutenberg_kg/bookgraph.py
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 | |
make_node_mesh(kind, center, size, lod)
¶
Return a PyVista mesh for a single node, shape-coded by kind.
All tiers use icosahedra — fast to build, good shading, no sphere overhead. LOD only varies the radius scale: - high → full size - low → 80 % size (octahedron for chunks to save faces) - points → tetrahedron (4 faces, minimal cost)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Node kind string. |
required |
center
|
ndarray
|
3-D centre position. |
required |
size
|
float
|
Node radius. |
required |
lod
|
str
|
LOD tier — |
required |
Returns:
| Type | Description |
|---|---|
DataSet
|
PyVista PolyData mesh. |
Source code in src/gutenberg_kg/scene.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
scan_corpus(corpus_root)
¶
Walk corpus_root and return {genre: [BookMeta, ...]} for every
book directory that carries a graph in one of :data:KG_DIRS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus_root
|
Path
|
Root of the corpus directory tree. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[BookMeta]]
|
Dict mapping genre name to list of book metadata objects. |
Source code in src/gutenberg_kg/bookgraph.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
species_leaf_glyphs(positions, skeleton, *, species, size, tint=None, n_tints=1)
¶
Leaves in the species' own outline, hung as the Knowledge Press web forest hangs them.
Each leaf's frame comes from :func:~gutenberg_kg.treegeom.species_leaf_frames
(stalk on its nearest twig, blade out and lifted, face to the sky; the web's
size and jitter), the same frames the POV-Ray path instances. The outline
(:func:gutenberg_kg.leafshapes.leaf_outline) is placed in each frame
directly rather than glyphed, because a glyph call aims one axis and leaves
the roll to chance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
|
required |
skeleton
|
Skeleton
|
Grown skeleton. |
required |
species
|
str
|
Species name; picks the outline. |
required |
size
|
float
|
Leaf size after density scaling ( |
required |
tint
|
ndarray | None
|
Optional |
None
|
n_tints
|
int
|
Palette length the tints index into. |
1
|
Returns:
| Type | Description |
|---|---|
PolyData
|
One |
Source code in src/gutenberg_kg/scene.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 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 510 511 512 513 | |