vecview 0.2.0__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.
Files changed (57) hide show
  1. vecview-0.2.0/.gitignore +13 -0
  2. vecview-0.2.0/AGENTS.md +116 -0
  3. vecview-0.2.0/CHANGELOG.md +177 -0
  4. vecview-0.2.0/LICENSE +21 -0
  5. vecview-0.2.0/PKG-INFO +290 -0
  6. vecview-0.2.0/README.md +259 -0
  7. vecview-0.2.0/docs/cameras.md +201 -0
  8. vecview-0.2.0/docs/development.md +124 -0
  9. vecview-0.2.0/docs/embedding.md +141 -0
  10. vecview-0.2.0/docs/gallery/altermagnetic_dot.svg +1 -0
  11. vecview-0.2.0/docs/gallery/bloch_sphere.svg +1 -0
  12. vecview-0.2.0/docs/gallery/brillouin_zone.svg +1 -0
  13. vecview-0.2.0/docs/gallery/dirac_cone.svg +1 -0
  14. vecview-0.2.0/docs/gallery/fullerene.svg +1 -0
  15. vecview-0.2.0/docs/gallery/mirror_planes.svg +1 -0
  16. vecview-0.2.0/docs/gallery/perovskite.svg +1 -0
  17. vecview-0.2.0/docs/gallery/skyrmion.svg +1 -0
  18. vecview-0.2.0/docs/gallery/slab_polarizer.svg +1 -0
  19. vecview-0.2.0/docs/gallery/solenoid.svg +1 -0
  20. vecview-0.2.0/docs/gallery.md +123 -0
  21. vecview-0.2.0/docs/images/readme.svg +1 -0
  22. vecview-0.2.0/docs/index.md +140 -0
  23. vecview-0.2.0/docs/readme_figure.py +28 -0
  24. vecview-0.2.0/docs/scenes.md +513 -0
  25. vecview-0.2.0/docs/shapes.md +255 -0
  26. vecview-0.2.0/examples/altermagnetic_dot.py +496 -0
  27. vecview-0.2.0/examples/gallery/__main__.py +78 -0
  28. vecview-0.2.0/examples/gallery/_common.py +39 -0
  29. vecview-0.2.0/examples/gallery/bloch_sphere.py +126 -0
  30. vecview-0.2.0/examples/gallery/brillouin_zone.py +114 -0
  31. vecview-0.2.0/examples/gallery/dirac_cone.py +93 -0
  32. vecview-0.2.0/examples/gallery/fullerene.py +109 -0
  33. vecview-0.2.0/examples/gallery/mirror_planes.py +107 -0
  34. vecview-0.2.0/examples/gallery/perovskite.py +96 -0
  35. vecview-0.2.0/examples/gallery/skyrmion.py +71 -0
  36. vecview-0.2.0/examples/gallery/solenoid.py +116 -0
  37. vecview-0.2.0/examples/slab_polarizer.py +475 -0
  38. vecview-0.2.0/pyproject.toml +97 -0
  39. vecview-0.2.0/src/vecview/__init__.py +85 -0
  40. vecview-0.2.0/src/vecview/_occlusion.py +532 -0
  41. vecview-0.2.0/src/vecview/_types.py +31 -0
  42. vecview-0.2.0/src/vecview/_vec.py +69 -0
  43. vecview-0.2.0/src/vecview/camera.py +299 -0
  44. vecview-0.2.0/src/vecview/projections.py +232 -0
  45. vecview-0.2.0/src/vecview/py.typed +0 -0
  46. vecview-0.2.0/src/vecview/scene.py +2427 -0
  47. vecview-0.2.0/src/vecview/shapes.py +571 -0
  48. vecview-0.2.0/tests/test_camera.py +126 -0
  49. vecview-0.2.0/tests/test_document.py +104 -0
  50. vecview-0.2.0/tests/test_occlusion.py +257 -0
  51. vecview-0.2.0/tests/test_package_metadata.py +55 -0
  52. vecview-0.2.0/tests/test_planes.py +190 -0
  53. vecview-0.2.0/tests/test_projections.py +230 -0
  54. vecview-0.2.0/tests/test_scene.py +767 -0
  55. vecview-0.2.0/tests/test_shapes.py +484 -0
  56. vecview-0.2.0/tests/test_solids.py +316 -0
  57. vecview-0.2.0/zensical.toml +19 -0
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ dist/
5
+ build/
6
+ *.egg-info/
7
+ site/
8
+ out/
9
+ .coverage
10
+ .cache/
11
+ .pytest_cache/
12
+ .ruff_cache/
13
+ .mypy_cache/
@@ -0,0 +1,116 @@
1
+ # VecView contributor guide
2
+
3
+ ## Scope
4
+
5
+ `vecview` projects world-space geometry into one SVG document, with an explicit
6
+ layer stack. It does not plot data, compile TeX, rasterize, export PDF, compose
7
+ multi-panel figures, or edit existing SVG.
8
+
9
+ The package is **independent and has no downstream dependencies**. Consumers
10
+ integrate through `Scene.to_svg_document()` alone; that direction is one-way.
11
+ Do not add a consumer-specific adapter, import, dependency, example, or test.
12
+
13
+ Naming the sibling projects is fine where it is only provenance: `vecview` is
14
+ developed alongside FigWorks and VecTeX as a suite, and a "Related projects"
15
+ link says so. What must stay out is *coupling* -- consumer-specific API
16
+ documentation, worked integration examples, or tests that import a consumer.
17
+ A tool that composes figures from `vecview` output documents that integration
18
+ on its own side.
19
+
20
+ ## Architecture
21
+
22
+ - Keep the three-way split, and do not let it blur:
23
+ - `shapes.py` knows numbers only — world-space arrays and `Face` records. No
24
+ camera, no style, no SVG import.
25
+ - `camera.py` defines what a camera *is*: `Camera` (the abstract contract) and
26
+ `ParallelCamera` (the affine machinery). Back-face culling lives here
27
+ because it is a camera question, not a scene one.
28
+ - `projections.py` holds the concrete projections: `OrthographicCamera` and
29
+ `ObliqueCamera`.
30
+ - `scene.py` is the **only** module that imports `svg`.
31
+ - `_occlusion.py` is private: exact visibility for `sort_by_depth(exact=True)`.
32
+ The canvas describes each element of an exact layer as a `Surface` (outline
33
+ plus a depth that goes on smoothly past its edge) or a `Line`, lazily, and
34
+ `resolve` clips and splits them. Keep closed-form depths closed-form.
35
+ - `_vec.py` and `_types.py` are private. Re-export from `__init__.py` what should
36
+ be public; `unit` is the only helper promoted so far.
37
+ - Geometry that needs a camera to be computed does not belong in `shapes.py`.
38
+ - Angles are degrees at every public boundary, radians nowhere.
39
+ - Keep the camera hierarchy honest. `Camera` promises only `project`, `at`,
40
+ `depth`, and `visible`. `direction`, `screen_basis`, `foreshortening`, and
41
+ `plane_matrix` belong on `ParallelCamera` because each assumes a screen offset
42
+ independent of position. Moving any of them up would make them a silent wrong
43
+ answer for a perspective camera, which would subclass `Camera` directly.
44
+ - `Scene.plane` reserves a group and nothing more. This package must not parse,
45
+ normalize, or embed foreign SVG; a consumer fills the group by id.
46
+ - `Scene` records objects and nothing else; it never projects. Every public
47
+ drawing method validates what it can without a camera and appends one record.
48
+ `_Canvas` holds a camera and implements each method of the same name; rendering
49
+ replays the record onto a fresh canvas. Camera-dependent work and errors
50
+ belong in `_Canvas`, and a canvas never records, so its methods may call each
51
+ other freely.
52
+
53
+ ## Deliberate non-features
54
+
55
+ Do not add these without the user changing the design first:
56
+
57
+ - **Automatic depth sorting across layers** (a z-buffer, or sorting a layer that
58
+ did not ask). Layers are the model. A beam crossing a translucent slab has
59
+ three parts that no automatic depth rule orders correctly. Depth only ever
60
+ acts inside a layer passed to `Scene.sort_by_depth`: as the painter's
61
+ algorithm, whose failures are fixed by cutting geometry back (`edges(trim=)`,
62
+ `trim_corners`) or into pieces (`cylinder(slices=)`, `tube` chunks), or, with
63
+ `exact=True`, as exact visibility by clipping.
64
+ - **Rasterizing or PDF export.** Runtime dependencies stay `numpy` and `svg.py`;
65
+ a test in `tests/test_package_metadata.py` enforces it. `shapely` and
66
+ `contourpy` are the optional `occlusion` extra: only `_occlusion.py` imports
67
+ them, inside `resolve`, so nothing but an exact layer needs them, and its
68
+ tests use `pytest.importorskip`.
69
+ - **Shading, materials, or lighting models.** This draws schematics, not renders.
70
+ `highlight=` gradients are a fill style fixed on screen, with no light
71
+ direction; keep them that way.
72
+
73
+ ## Development
74
+
75
+ - Supported Python: 3.12 and newer.
76
+ - `uv sync --all-extras` for a full environment.
77
+ - Run `uv run ruff format --check .`, `uv run ruff check .`, `uv run ty check`,
78
+ and `uv run pytest` before reporting a change complete.
79
+ - Run `uv run python examples/slab_polarizer.py --projection all`,
80
+ `uv run python examples/altermagnetic_dot.py --projection all`, and
81
+ `uv run python examples/gallery` after touching geometry or projection, and
82
+ look at `examples/out/gallery/*.png` after touching a solid, an outline, or
83
+ the depth sort. Refresh `docs/gallery/` with `--docs` when a figure changes. It is the realistic end-to-end check, and it renders
84
+ pictures whose correctness is visible. Mirrored or upside-down content, or a
85
+ beam that misses the slab, is the usual symptom of a projection bug.
86
+ - Keep output deterministic: layer ties resolve by insertion order, coordinates
87
+ are rounded on emission. Byte-identical output is what keeps a figure diffable.
88
+
89
+ ## Testing
90
+
91
+ - Test geometry as numbers, not as rendered strings: winding against declared
92
+ normal, a wave against an exact sine, a circle's radius. Reserve
93
+ rendered-output assertions for document structure.
94
+ - Add a regression test for every projection or winding bug — a wrong normal is
95
+ invisible until it culls the wrong wall.
96
+ - Test a projection through `foreshortening()`, not through its construction
97
+ angles: the ratios are what the axonometric classification is defined by.
98
+ - Rendering under a camera must stay byte-identical to building the scene with
99
+ that camera from the start. The parametrized test in `tests/test_scene.py`
100
+ covers all five projections.
101
+ - `tests/test_document.py` pins what a consumer relies on: a parseable
102
+ standalone document, a viewBox agreeing with `width`/`height`, geometry inside
103
+ it, and deterministic bytes. It must not import a consumer.
104
+ - Keep `docs/` in sync with user-visible changes in the same change, and run
105
+ `uv run zensical build` before reporting documentation work complete.
106
+ - Keep `CHANGELOG.md` current in Keep a Changelog 1.1.0 format.
107
+
108
+ ## Naming and packaging
109
+
110
+ - **`vecview` is the intended distribution and import name.** Confirm PyPI
111
+ availability immediately before a first publication. See `docs/development.md`
112
+ for the rename procedure.
113
+ - `pyproject.toml` is canonical for Python metadata.
114
+ - Do not commit, tag, upload, or publish unless the user explicitly asks.
115
+ - Before 1.0, make API changes directly: update consumers, tests, and docs in the
116
+ same change. No compatibility aliases or deprecated wrappers.
@@ -0,0 +1,177 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here, following
4
+ [Keep a Changelog 1.1.0](https://keepachangelog.com/en/1.1.0/).
5
+
6
+ ## [Unreleased]
7
+
8
+ ## [0.2.0] - 2026-10-06
9
+
10
+ ### Added
11
+
12
+ - PyPI release workflow: validate the tag, run CI, build and check distributions,
13
+ then publish with Trusted Publishing.
14
+
15
+ - `Scene.sort_by_depth(layer)` -- opt one layer in to the painter's algorithm.
16
+ Its world-space elements are drawn back to front, keyed by the mean depth of
17
+ the points that made them; ties keep insertion order and screen-space
18
+ elements go on top. Other layers are untouched, and `with_camera` re-sorts.
19
+ - `Scene.sphere` -- the exact outline of a sphere as one `<circle>`, or a rotated
20
+ `<ellipse>` under an oblique camera.
21
+ - `Scene.cylinder` and `Scene.cone` -- exact outlines of a cylinder, frustum, or
22
+ cone: two straight sides and two elliptical arcs in one `<path>`, plus the end
23
+ disk that faces the camera. `slices=n` cuts a long cylinder into separately
24
+ sorted lengths that still draw as one seamless solid.
25
+ - `Scene.arrow3d` -- a solid arrow, cylindrical shaft and conical head in one group.
26
+ - `Scene.tube` -- a tube along a world-space curve, as overlapping stroked pieces
27
+ that sort over and under each other without seams.
28
+ - `highlight=` on the solids -- a gradient fill lightest toward the upper left;
29
+ spheres of one colour pair share one gradient.
30
+ - `Scene.edges` -- the edges of a convex solid, visible ones solid and hidden ones
31
+ dropped or restyled (dashed), optionally one path per edge and trimmed at the
32
+ vertices.
33
+ - `Scene.sphere_curve` -- a curve on a sphere, split exactly where it passes behind.
34
+ - `sort_by_depth(layer, exact=True)` -- exact visibility, with the new
35
+ `occlusion` extra (`shapely`, `contourpy`). Every surface keeps its native
36
+ element, clipped to what no opaque surface hides: exactly between planes, and
37
+ to a sub-pixel contour of the closed-form depths where a sphere, cylinder,
38
+ cone, arrow, or tube is involved. Lines are split where they pass behind a
39
+ surface, and the hidden part dropped or drawn in a `back` style. Opaque
40
+ surfaces are painted so that what hides another comes after it.
41
+ - `Scene.polyline(..., back=...)` -- the style of the parts an exact layer hides.
42
+ - `Scene._repr_svg_`, so a scene displays inline in Jupyter.
43
+ - `text` and `text2d` accept a list of `svg.TSpan` runs, for subscripts.
44
+ - `arc_shape`, `helix`, `surface_faces`, `convex_polyhedron`, and `trim_corners`.
45
+ - `examples/gallery/` -- a perovskite cell, the fcc Brillouin zone, C60, the Bloch
46
+ sphere, a gapped Dirac cone, a Néel skyrmion, a solenoid, and crossing mirror
47
+ planes in an exact layer, each from one
48
+ script; `python examples/gallery --docs` builds them all into the new Gallery
49
+ page.
50
+
51
+ ### Changed
52
+
53
+ - Use ty for type checking in development and CI instead of mypy.
54
+ - **A scene holds objects and cameras; a camera renders them.** Drawing calls
55
+ only record, and nothing is projected until `render`, `save`, `bbox`, or
56
+ `to_svg_document`. As in a 3D application, `scene.cameras` holds named
57
+ cameras and `scene.camera` is the active one (formerly `scene.cam`), set by
58
+ name or directly; the zero-argument `to_svg_document` uses it.
59
+ `Scene(camera=None, *, cameras=None, pad, background)`, and `render(camera=None,
60
+ *, pad, background)`, `save(path, camera=None)`, and `bbox(camera=None)` take a
61
+ camera or a name. `with_camera` returns a copy with a different active camera.
62
+ Output is byte-identical to before.
63
+ - Errors that need no camera -- a self-crossing footprint, a one-point tube, a
64
+ highlight without a fill -- still raise at the call; those that need one -- a
65
+ plane seen edge-on, an arrow along the projection ray -- raise on render.
66
+ - `Scene.items` and `Scene.defs` are gone; they belonged to one projection, and
67
+ now live on the private canvas a render builds.
68
+ - `prism_walls` builds its path through the shared helper; output is unchanged.
69
+
70
+ ## [0.1.2] - 2026-10-05
71
+
72
+ ### Added
73
+
74
+ - `examples/altermagnetic_dot.py`: a device sketch of an altermagnetic quantum
75
+ dot -- arc-shaped non-convex gates and tapered leads through `prism_walls`,
76
+ Gaussian spin densities, camera-facing spins, a bias circuit, and labels at
77
+ world points -- built once and replayed under four projections with
78
+ `with_camera`.
79
+
80
+ ## [0.1.1] - 2026-10-05
81
+
82
+ ### Changed
83
+
84
+ - Prose spells the project VecView; the distribution, import, and command names
85
+ stay `vecview`. The sibling projects are now FigWorks (formerly FigForge) and
86
+ VecWire (formerly cirquit).
87
+
88
+ ## [0.1.0] - 2026-10-05
89
+
90
+ ### Added
91
+
92
+ - `Scene.slot(layer, pt3, w, h, *, id, align, dx, dy)` — reserve an empty,
93
+ screen-aligned group pinned to a projected world point, for upright content
94
+ such as TeX labels. The group is translated to the anchor and records `align`
95
+ as `data-align`; a `w` × `h` box grows the fitted viewBox. The anchor is
96
+ reprojected by `with_camera`, which a hand-placed `rect2d` was not.
97
+ - `Scene.silhouette(layer, solid)` — fill the projected convex hull of a solid as
98
+ one polygon. Drawn in the wall colour under the cap, it replaces per-wall
99
+ polygons and the hairline seams between them.
100
+ - `Scene.arrow(..., normal="camera")` — an arrow turned about its axis to show
101
+ its widest face, resolved at draw time so a reprojection turns it too. Also
102
+ accepts an explicit normal.
103
+ - `Scene.gaussian(layer, center, u, v, a, b, *, id, color)` — a soft Gaussian spot
104
+ lying in a world plane, as one polygon filled by a radial gradient mapped
105
+ through the plane's affine transform.
106
+ - `prism_faces(footprint, z0, z1)` — any simple footprint, convex or not, extruded
107
+ along `z`, as correctly wound `Face`s that cull like a box. Self-intersecting
108
+ or self-touching outlines raise `ValueError`.
109
+ - `Scene.prism_walls(layer, footprint, z0, z1)` — the camera-facing walls of an
110
+ extrusion as one seamless `<path>`, one strip per run of consecutive facing
111
+ walls. With the cap drawn over it, exact at any height for unstroked walls,
112
+ including non-convex footprints.
113
+ - `annulus_sector(center, r_in, r_out, theta0_deg, theta1_deg, n)` — the
114
+ counter-clockwise footprint of an annular sector or pie wedge.
115
+ - `ellipse_shape(center, u, v, a, b, n)` — the generalization of `circle_shape`.
116
+ - `Camera` is now an abstract base holding only the projection contract
117
+ (`project`, `at`, `depth`, `visible`), with `ParallelCamera` beneath it for the
118
+ affine machinery (`direction`, `screen_basis`, `foreshortening`,
119
+ `plane_matrix`). A future perspective camera would subclass `Camera` directly
120
+ and inherit none of the affine methods, which is the point of the split.
121
+ - `OrthographicCamera` — the axonometric camera, aimed by azimuth and elevation,
122
+ with `isometric()` and `dimetric(ratio=...)` constructors, plus `axonometry()`
123
+ to classify a projection from its actual ratios.
124
+ - `ObliqueCamera` — cavalier and cabinet projections, which no azimuth and
125
+ elevation can reproduce.
126
+ - `Camera.foreshortening()` — the screen length of each unit world axis.
127
+ - `Scene.with_camera(cam)` — re-render a finished scene under another projection.
128
+ Recorded world-space calls are replayed, giving output byte-identical to
129
+ building from scratch with that camera.
130
+ - `Scene.faces(..., cull=True)` — cull back faces at draw time rather than in the
131
+ caller, so a reprojection selects the right walls instead of keeping the
132
+ original camera's.
133
+ - `Camera.plane_matrix()` and `Scene.plane()` — embed flat SVG content in a world
134
+ plane. Exact rather than approximate: a parallel projection restricted to a
135
+ plane is affine, which is what an SVG `matrix` expresses.
136
+ - Initial package, extracted from a single-module `vecview.py` used to draw an
137
+ altermagnetic-slab polarizer schematic.
138
+ - `Camera.screen_basis(normal)` — the in-plane world directions that move a point
139
+ purely right and purely down on screen, for any plane. Replaces a hand-rolled
140
+ 2×2 solve in the caller.
141
+ - `Camera.visible(faces)` and `Camera.faces_camera(normal)` — back-face culling
142
+ by outward normal, replacing hand-listed wall corner pairs and a comment about
143
+ which octant the camera occupies.
144
+ - `Camera.direction(vec)` — projects a displacement, ignoring `origin`.
145
+ - `Camera.depth(pts)` — signed distance along the view axis, for callers that want
146
+ to order something by depth themselves.
147
+ - `box_faces(center, size)` and the `Face` record (`name`, `points`, `normal`) —
148
+ an axis-aligned box as six named, correctly wound faces.
149
+ - `in_plane_dir(angle_deg, u, v)` — unit direction at an angle within a plane.
150
+ - `Scene.faces(layer, faces, **style)` — draw a group of faces with one style. An
151
+ `id` is suffixed per face (`slab` → `slab-pz`, `slab-px`, …) rather than
152
+ repeated, since duplicate ids are invalid SVG.
153
+ - `Scene.to_svg_document()` — a complete SVG document string, the whole surface a
154
+ consumer needs to place a scene without importing this package.
155
+ - `Scene.save(path)`, `Scene.is_empty`, and `repr` for `Camera` and `Scene`.
156
+ - `Scene(cam, pad=..., background=...)` constructor defaults, so
157
+ `to_svg_document()` needs no arguments.
158
+ - Public `unit(v)`.
159
+ - Type hints throughout, checked with `mypy --strict`, and a `py.typed` marker.
160
+ - Documentation, and a test suite covering projection, geometry, layering, and
161
+ the document contract.
162
+
163
+ ### Changed
164
+
165
+ - Coordinates that round to `-0.0` are written as `0.0`.
166
+ - **Python 3.12 is now the floor.** This drops the split where ruff targeted 3.11
167
+ while mypy targeted 3.12 to parse numpy 2's PEP 695 stubs, and lets the private
168
+ type aliases use `type` statements.
169
+ - Rename the distribution and import package from `svg3d` to `vecview`.
170
+ - Polygon and polyline coordinates are emitted as `svg.Point` pairs
171
+ (`points="1.5,2.25 3,-4"`) rather than a flat coordinate run.
172
+ - Rendering an empty scene now raises `ValueError` instead of producing a
173
+ document with a degenerate viewBox.
174
+ - `rect_shape` normalizes its axes, so an unnormalized direction no longer
175
+ silently stretches the rectangle.
176
+ - `circle_shape` derives its in-plane basis from the shared helper, making the
177
+ seam position consistent with other in-plane geometry.
vecview-0.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrea Maiani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
vecview-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,290 @@
1
+ Metadata-Version: 2.4
2
+ Name: vecview
3
+ Version: 0.2.0
4
+ Summary: Layered 3D scenes that render to SVG, for scientific schematics
5
+ Project-URL: Source, https://github.com/maiani/vecview
6
+ Project-URL: Issues, https://github.com/maiani/vecview/issues
7
+ Author-email: Andrea Maiani <dev@andreamaiani.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: 3d,axonometric,figures,svg,vector-graphics
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Topic :: Multimedia :: Graphics :: 3D Rendering
20
+ Classifier: Topic :: Scientific/Engineering :: Visualization
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.12
23
+ Requires-Dist: numpy>=1.24
24
+ Requires-Dist: svg-py>=1.4
25
+ Provides-Extra: examples
26
+ Requires-Dist: cairosvg>=2.7; extra == 'examples'
27
+ Provides-Extra: occlusion
28
+ Requires-Dist: contourpy>=1.2; extra == 'occlusion'
29
+ Requires-Dist: shapely>=2.0; extra == 'occlusion'
30
+ Description-Content-Type: text/markdown
31
+
32
+ # VecView
33
+
34
+ Layered 3D scenes that render to SVG, for scientific schematics: a slab and a
35
+ beam, a lattice, an optical bench. The schematic is generated from code and
36
+ stays editable afterwards: elements keep the ids you give them, the same scene
37
+ renders to byte-identical SVG, and the output opens in Inkscape.
38
+
39
+ <p align="center">
40
+ <img src="https://raw.githubusercontent.com/maiani/vecview/v0.2.0/docs/images/readme.svg" alt="A gate-defined quantum dot on a layered slab, with leads, gates, spin densities and a bias circuit" width="640">
41
+ </p>
42
+
43
+ <p align="center"><sub>A device sketch: layered solids, Gaussian densities, solid arrows and a circuit, from <code>examples/altermagnetic_dot.py</code>. More in the <a href="https://github.com/maiani/vecview/blob/v0.2.0/docs/gallery.md">gallery</a>.</sub></p>
44
+
45
+ VecView is a small projection layer on top of
46
+ [`svg.py`](https://pypi.org/project/svg.py/). `svg.py` builds the elements;
47
+ VecView supplies what it has no notion of: a camera, world-space glyph geometry,
48
+ and an explicit layer stack. Runtime dependencies are `numpy` and `svg.py`.
49
+
50
+ VecView is alpha: the API may change before 1.0.
51
+
52
+ ## Install
53
+
54
+ Install VecView with pip (Python 3.12 or newer; tested on 3.12–3.14):
55
+
56
+ ```bash
57
+ python -m pip install vecview
58
+ python -m pip install "vecview[occlusion]" # exact visibility
59
+ ```
60
+
61
+ ## Quick start
62
+
63
+ ```python
64
+ import vecview
65
+
66
+ cam = vecview.OrthographicCamera(azim_deg=35, elev_deg=24, scale=62)
67
+ scene = vecview.Scene(cam, pad=28, background="#ffffff")
68
+
69
+ slab = vecview.box_faces(center=(0, 0, -0.45), size=(11, 9, 0.9))
70
+ scene.faces(10, slab, cull=True, fill="#cfd6e0", stroke="#8b96a6", stroke_width=1.6)
71
+
72
+ for angle in (0.0, 67.5):
73
+ axis = vecview.in_plane_dir(angle)
74
+ scene.polygon(
75
+ 20,
76
+ vecview.double_arrow_shape(
77
+ (0, 0, 0.02), axis, 4.0, (0, 0, 1), shaft_w=0.1, head_w=0.36, head_len=0.34
78
+ ),
79
+ fill="#d62828",
80
+ )
81
+
82
+ scene.save("slab.svg")
83
+ ```
84
+
85
+ World coordinates are right-handed `(x, y, z)` with `z` up, and angles are in
86
+ degrees. The first argument of every drawing call is its layer. `cull=True`
87
+ keeps only the walls this camera can see, and the viewBox is fitted to the
88
+ content.
89
+
90
+ ## Projections
91
+
92
+ Five, all parallel, so parallel edges stay parallel:
93
+
94
+ | Camera | Foreshortening |
95
+ | --- | --- |
96
+ | `OrthographicCamera.isometric(scale)` | all three axes equal, `0.8165` |
97
+ | `OrthographicCamera.dimetric(scale, ratio=0.5)` | two equal, third by `ratio` |
98
+ | `OrthographicCamera(azim_deg, elev_deg, scale)` | trimetric, the general case |
99
+ | `ObliqueCamera.cavalier(scale)` | front face true, depth `1.0` |
100
+ | `ObliqueCamera.cabinet(scale)` | front face true, depth `0.5` |
101
+
102
+ `foreshortening()` reports the ratios for any camera, and `axonometry()` names
103
+ the class an orthographic one falls into; both read the projection rather than
104
+ how it was built. A scene records objects and holds named cameras, one active,
105
+ as a 3D application does. It renders under any camera, byte-identical to building
106
+ it from scratch with that camera, and needs none until then:
107
+
108
+ ```python
109
+ scene.cameras["cabinet"] = vecview.ObliqueCamera.cabinet(62)
110
+ scene.save("cabinet.svg", "cabinet")
111
+ scene.camera = "cabinet" # the active camera, which to_svg_document uses
112
+ ```
113
+
114
+ `Camera` promises only `project`, `at`, `depth`, and `visible`. `direction`,
115
+ `screen_basis`, `foreshortening`, and `plane_matrix` live on `ParallelCamera`,
116
+ because each assumes a screen offset independent of position. A future
117
+ perspective camera would subclass `Camera` directly and inherit none of them.
118
+
119
+ ## Layers first, depth sorting by request
120
+
121
+ There is no z-buffer. Draw order is an explicit layer stack: for a beam passing
122
+ through a translucent slab, the beam above the slab, the attenuated segment
123
+ inside it, and the emerging beam below are three draw calls at three layers, and
124
+ no automatic rule orders them correctly against a partially transparent face.
125
+ Deciding occlusion by hand is worth more than getting it automatically and almost
126
+ right.
127
+
128
+ A lattice of hundreds of atoms is the opposite case. `scene.sort_by_depth(layer)`
129
+ opts one layer in to the painter's algorithm: its elements are drawn back to
130
+ front, ties keep insertion order, and every other layer is untouched.
131
+ `sort_by_depth(layer, exact=True)` goes further and decides visibility point by
132
+ point: each element keeps its native shape, clipped to what shows of it, so
133
+ planes can cross and a bond can run into an atom, and lines are dashed exactly
134
+ where they pass behind something.
135
+ `Camera.visible()` and `faces(..., cull=True)` cover the one unambiguous case,
136
+ the back faces of a convex solid.
137
+
138
+ ## Drawing
139
+
140
+ World-space calls on `Scene`:
141
+
142
+ - `polygon`, `polyline`, `text`, and `faces` draw projected geometry;
143
+ `faces(..., id="slab")` suffixes the id per face (`slab-pz`, `slab-px`, …).
144
+ - `arrow(..., normal="camera")` turns an arrow about its own axis to show its
145
+ widest face, resolved by the camera that renders, so every camera sees it. An explicit
146
+ normal also works.
147
+ - `silhouette(layer, solid)` fills the projected convex hull of a solid as one
148
+ polygon. Drawn in the wall colour under the cap, it removes the hairline seams
149
+ between per-wall polygons.
150
+ - `gaussian(layer, center, u, v, a, b, id=..., color=...)` draws a soft spot
151
+ lying in a world plane: one polygon filled by a radial gradient mapped
152
+ through the plane's affine transform.
153
+
154
+ Curved solids have exact outlines under any parallel projection, and each is one
155
+ native element or group:
156
+
157
+ - `sphere` is a single `<circle>` (an `<ellipse>` under an oblique camera).
158
+ - `cylinder` and `cone` are two straight sides and two elliptical arcs, plus the
159
+ end disk that faces the camera; `slices=n` cuts a long one into separately
160
+ sorted lengths.
161
+ - `arrow3d` is a solid shaft and head; `tube` follows any curve, such as a `helix`.
162
+ - `highlight="#fff"` shades any of them with a gradient: a fill style, fixed on
163
+ screen, not a lighting model.
164
+
165
+ `edges(layer, faces, back={...})` draws a convex solid's edges and dashes the
166
+ hidden ones, and `sphere_curve` splits a curve on a sphere where it passes behind.
167
+ Labels can be `svg.TSpan` runs, for subscripts.
168
+
169
+ Screen-space `rect2d` and `text2d`, `add_def` for `<defs>`, and `add` for raw
170
+ `svg.py` elements complete the set. Style keywords pass straight to `svg.py`
171
+ (`stroke_width` becomes `stroke-width`).
172
+
173
+ Geometry functions return plain world-space arrays and know nothing about
174
+ cameras, styles, or SVG, which keeps them testable as numbers:
175
+
176
+ | Function | Returns |
177
+ | --- | --- |
178
+ | `box_faces(center, size)` | an axis-aligned box as six named, correctly wound `Face`s |
179
+ | `prism_faces(footprint, z0, z1)` | a convex footprint extruded along `z`, culling like a box |
180
+ | `rect_shape`, `circle_shape`, `ellipse_shape` | flat outlines in any plane |
181
+ | `arrow_shape`, `double_arrow_shape` | flat arrows with a shaft and head |
182
+ | `sine_ribbon` | a transverse wave along an axis, for `polyline`; the amplitude may be an envelope |
183
+ | `arc_shape`, `helix` | an arc between two directions; a coil or spin spiral |
184
+ | `surface_faces(x, y, z)` | the quads of a sampled surface, such as a band structure |
185
+ | `convex_polyhedron(vertices)` | a Brillouin zone or coordination polyhedron from its corners |
186
+ | `trim_corners(faces, r)` | faces cut back from atoms sitting on their corners |
187
+ | `in_plane_dir(angle_deg, u, v)` | a unit direction at an angle within a plane |
188
+
189
+ At an azimuth of 35°, neither `+x` nor `+y` moves a point horizontally across
190
+ the picture. `screen_basis()` returns the two in-plane world directions that do:
191
+
192
+ ```python
193
+ horizontal, down = cam.screen_basis() # in the z = 0 plane
194
+ spots = [-3.0 * horizontal + 1.7 * down, +3.0 * horizontal + 1.7 * down]
195
+ ```
196
+
197
+ ## Reserving room for other content
198
+
199
+ Two calls reserve an empty group for content VecView does not draw itself, such
200
+ as a plot or a TeX label. VecView never parses foreign SVG; a consumer fills the
201
+ group by id.
202
+
203
+ `Scene.plane` reserves a rectangle of a world plane, so flat content lies *in*
204
+ the scene, foreshortened and sheared with the geometry:
205
+
206
+ ```python
207
+ scene.plane(15, origin=(-4.2, -2.9, 0.01), u_edge=(0, 8.4, 0), v_edge=(5.8, 0, 0), id="plot-plane")
208
+ ```
209
+
210
+ The embedding is exact: a parallel projection restricted to a plane is affine,
211
+ which is what an SVG `matrix` expresses. (A perspective camera would give a
212
+ homography, which `matrix` cannot represent.)
213
+
214
+ `Scene.slot` pins an upright, unforeshortened group to a projected world point,
215
+ for content such as a TeX label that should read at the document's font size:
216
+
217
+ ```python
218
+ scene.slot(45, (5.5, 4.5, 0), 20, 10, id="label-x", align="west", dx=1.6)
219
+ ```
220
+
221
+ The group records `align` as `data-align`, the `w` by `h` box grows the fitted
222
+ viewBox, and each camera projects the anchor afresh.
223
+
224
+ ## Output
225
+
226
+ `Scene.save(path)` writes the document; `Scene.to_svg_document()` returns it as
227
+ a string. In Jupyter a scene displays itself inline. That method is the whole embedding contract: any tool that accepts an
228
+ object exposing it can place a scene, without VecView knowing about the tool.
229
+ The fitted viewBox usually does *not* start at `0, 0`, so a consumer must honour
230
+ its origin. See [Embedding a scene](https://github.com/maiani/vecview/blob/v0.2.0/docs/embedding.md).
231
+
232
+ Rasterizing and PDF export are out of scope; they belong to whatever assembles
233
+ the final page.
234
+
235
+ ## Examples
236
+
237
+ The [gallery](https://github.com/maiani/vecview/blob/v0.2.0/docs/gallery.md) has the figures everyone draws — a perovskite
238
+ cell, the fcc Brillouin zone, C60, the Bloch sphere, a Dirac cone, a skyrmion, a
239
+ solenoid, crossing mirror planes — each from one script:
240
+
241
+ ```bash
242
+ uv run python examples/gallery # all of them
243
+
244
+ uv run python examples/slab_polarizer.py # one projection
245
+ uv run python examples/slab_polarizer.py --projection all # all five
246
+ uv run python examples/altermagnetic_dot.py --projection all # a device sketch, under four cameras
247
+ ```
248
+
249
+ `slab_polarizer` is a picture of a polarizing slab, rebuilt per
250
+ projection because it mixes in screen-space glows. `altermagnetic_dot` is a
251
+ quantum-dot device sketch with arc-shaped gates, Gaussian densities, and a bias
252
+ circuit; being world-space throughout, it is built once, holds its projections
253
+ as named cameras, and renders each by name.
254
+
255
+ ## Development
256
+
257
+ ```bash
258
+ uv sync --all-extras
259
+ uv run ruff format --check .
260
+ uv run ruff check .
261
+ uv run ty check
262
+ uv run pytest
263
+ ```
264
+
265
+ Run both examples with `--projection all` after touching geometry or projection: mirrored content, or a beam that misses the slab, is the
266
+ usual sign of a projection bug.
267
+
268
+ ## Related projects
269
+
270
+ VecView is developed alongside [FigWorks](https://github.com/maiani/figworks),
271
+ which composes multi-panel figures, and two other producers of editable SVG:
272
+ [VecTeX](https://github.com/maiani/vectex) (TeX equations) and [VecWire](https://github.com/maiani/vecwire) (circuit
273
+ schematics). All four share one premise: figures generated from code, with
274
+ stable ids and byte-identical output, that stay editable in Inkscape.
275
+
276
+ VecView depends on none of them and contains no code specific to any of them.
277
+
278
+ ## Documentation
279
+
280
+ - [Overview](https://github.com/maiani/vecview/blob/v0.2.0/docs/index.md)
281
+ - [Cameras](https://github.com/maiani/vecview/blob/v0.2.0/docs/cameras.md)
282
+ - [Scenes and layers](https://github.com/maiani/vecview/blob/v0.2.0/docs/scenes.md)
283
+ - [Shapes](https://github.com/maiani/vecview/blob/v0.2.0/docs/shapes.md)
284
+ - [Gallery](https://github.com/maiani/vecview/blob/v0.2.0/docs/gallery.md)
285
+ - [Embedding a scene](https://github.com/maiani/vecview/blob/v0.2.0/docs/embedding.md)
286
+ - [Development](https://github.com/maiani/vecview/blob/v0.2.0/docs/development.md)
287
+
288
+ ## License
289
+
290
+ MIT