diff --git a/AGENTS.md b/AGENTS.md index 542f4b2..f4a09a6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,7 +16,7 @@ High-signal context for OpenCode sessions in this repo. ## Testing -- A `pytest` suite lives in `tests/` (config validation, each pipeline step, `app_helpers`, end-to-end, CLI). Run with `pytest` (full suite ~70 s) or `pytest -m "not slow"` to skip subprocess/CLI tests and the finest-pitch geometry runs. +- A `pytest` suite lives in `tests/` (config validation, each pipeline step, `app_helpers`, end-to-end, CLI). Run with `pytest` (full suite ~70 s) or `pytest -m "not slow"` to skip the subprocess tests and the finest-pitch geometry runs. The CLI itself is tested in-process through `inlayer.main(argv)` and stays in the quick loop: `tests/test_pipeline.py::TestCLIWiring` holds every flag against the inlay the library builds for the matching `Config` and reads back the STL the CLI wrote. A new flag gets a row in its `WIRING` table - an exit code and a non-empty file prove nothing about what was written. - **Never test a re-implementation of shipped code.** If a helper is hard to import, move it somewhere importable (see `app_helpers.py`) instead of copying its body into the test file — a copied test passes even when the real function is broken. - **After every code change, run the tests.** If the suite fails, fix the regression before considering the change done. - **Extend the suite when you add or modify behavior.** New functions, new branches, new edge cases or bug fixes must come with matching tests. Keep test docstrings and comments in English per the language convention below. @@ -40,7 +40,7 @@ High-signal context for OpenCode sessions in this repo. - `inlayer.py` is the pipeline library module. It exposes a frozen `Config` dataclass plus pipeline functions (`apply_euler_rotation`, `prepare_figure`, `dilate`, `build_inlay`, `wall_thickness_stats_3d`; `arrange_footprints` is the layout `build_inlay` uses). All parameters flow through `Config`. - `app_helpers.py` holds the **Streamlit-free** helpers of the web app (`file_hash`/`bytes_hash`, `quantize_axis_value`, `resolve_selection`, `applied_figure_params`, `figure_labels`, `load_preview_mesh`, `preview_copy`, `scene_layout`, `selection_key`, plus `decimate_mesh` re-exported from `inlayer`). `app.py` imports and thinly wraps them (caching decorators, session-state lookups). Reason: `app.py` calls `st.set_page_config()` and builds the whole sidebar at import time, so it cannot be imported in tests — anything pure belongs in `app_helpers.py` so the suite tests the shipped code instead of a copy. Do not re-inline this logic into `app.py`. (`tests/test_app_render.py` does run `app.py` through Streamlit's `AppTest`, which catches render errors — but that is an integration check, not a licence to move logic back into the frontend.) -- `app.py` is a Streamlit frontend. It builds an `inlayer.Config` from the sidebar widgets and passes it into the pipeline functions (no global mutation). Its widgets start at `Config`'s defaults (`inlayer.Config.`, read the way the CLI reads them), never at literals of their own: `tests/test_app_render.py` checks that both entry points start from the same values. +- `app.py` is a Streamlit frontend. It builds an `inlayer.Config` from the sidebar widgets and passes it into the pipeline functions (no global mutation). Its widgets start at `Config`'s defaults (`inlayer.Config.`, read the way the CLI reads them), never at literals of their own: `tests/test_app_render.py` checks that the widgets start from `Config`'s values, `tests/test_pipeline.py::TestCLIDefaults` that the parser does. The same goes for a default quoted in a help text - pass it into the translation (`default=Config.`), don't type it into `i18n.py`. - `i18n.py` holds the **bilingual UI** (English/German). `t(key, **kwargs)` looks up `TRANSLATIONS` and fills named placeholders; unknown keys return the key itself rather than raising, so a missing string can never abort a running computation. Keys are namespaced `app.*` / `cli.*` / `pipeline.*` / `config.*` / `error.*` / `check.*` (wall-check findings, shared by CLI and app through `inlayer.describe_violation`). - **The current language is a `ContextVar`, not a module global.** Streamlit runs each session's script in its own thread; a shared global would let one session's language selection bleed into another's. Do not "simplify" this into a plain variable. - **`ThreadPoolExecutor` workers do not inherit ContextVars.** `_parallel_map` captures the language before submitting and re-applies it inside each worker — without that, parallel steps log in the default language. `tests/test_i18n.py` pins this behaviour *and* the assumption behind it. Any new thread pool that logs needs the same treatment. @@ -62,6 +62,7 @@ High-signal context for OpenCode sessions in this repo. - **`build_inlay` never rotates.** Rotations are applied in step 1 via `apply_euler_rotation` and are already baked into the meshes handed to `build_inlay`. It used to accept an `individual_rotations` argument that was silently ignored — that parameter is gone; don't reintroduce it. - There are **two entry points:** - CLI: `python inlayer.py [-i figure.stl] [-o inlay.stl] [-c 0.4] [--lang de] …` — `--lang` is parsed *before* the main parser is built, because argparse renders help strings at construction time; `INLAYER_LANG` is the fallback. + The CLI is `inlayer.main(argv)` (parser in `_build_parser()`) and returns its exit code; `__main__` is only `sys.exit(main())`. Keep it that way: code under `__main__` cannot be imported, so the tests could only start it as a subprocess and never looked at the STL it wrote. - Web UI: `streamlit run app.py` ## trimesh 4.x quirk @@ -72,7 +73,7 @@ High-signal context for OpenCode sessions in this repo. ## Algorithmic choices - **Voxel dilation** is used for the tolerance offset (`dilate`) instead of normal-shifted surface offset. This is intentional: voxel offsetting survives cracks and non-manifold edges that vertex-normal offsets choke on. -- **Voxel inflation is compensated in `dilate`.** Marching cubes inflates surfaces: ~`voxel_pitch/2` per side in `prepare_figure` plus ~`voxel_pitch/4` in `dilate`'s own reconstruction. Measured surplus without compensation: `0.75 * voxel_pitch`, geometry-independent (cube == sphere, 2026-07). `dilate` subtracts this from the requested distance; consequence: the effective clearance cannot drop below ~`0.75 * voxel_pitch`. +- **Voxel inflation is compensated in `dilate`.** Marching cubes inflates surfaces: ~`voxel_pitch/2` per side in `prepare_figure` plus ~`voxel_pitch/4` in `dilate`'s own reconstruction. Measured surplus without compensation: `0.75 * voxel_pitch`, geometry-independent (cube == sphere, 2026-07). `dilate` subtracts this from the requested distance; consequence: the effective clearance cannot drop below ~`0.75 * voxel_pitch`. **That number describes `dilate`'s output, not the finished cavity:** `_solidify_figure` voxelizes the dilated figure once more at the full pitch, which nothing compensates. Measured on the inlay, flat walls end up 1-2 x `voxel_pitch` from the figure (0.40-0.80 mm at the defaults for a configured 0.4) and the setting takes effect in steps of `voxel_pitch / 2`; README says so. Calibrate against the finished cavity, not against `dilate`, if this is ever tightened - and no test measures it yet. - **The dilation is (nearly) isotropic.** scipy's default element (6-neighbourhood) grows an octahedron - 2 mm of clearance along the axes left 1.5 mm on a 45° slope. `dilate` does a share of `(√3 − 1)/2` of its steps with the full 3×3×3 cube, which makes axis and space-diagonal growth equal (every direction within 0.97–1.06). The growth *along the axes* stays one voxel per step, so `_dilation_iterations`' calibration holds. There is deliberately no `binary_closing` before the dilation: dilating a closing with the same element equals dilating the input (bit for bit), it only cost time. - **Pad voxel grids before morphological ops.** scipy's `binary_closing`/`binary_dilation` clip at array borders — without `np.pad` by the iteration count, the erosion step eats the figure's extremes (a sphere literally turns into a cube at coarse pitch). Both `prepare_figure` and `dilate` pad and then correct the grid transform for the padding offset. - Boolean CSG runs in manifold3d, never in trimesh's default engine: `build_inlay` calls `manifold3d` directly (`_to_manifold`/`_to_trimesh`, the same float32 conversion `trimesh.boolean` uses with `engine='manifold'`), because the wall check needs the `Manifold` objects anyway. @@ -87,5 +88,5 @@ High-signal context for OpenCode sessions in this repo. - In `app.py`, expensive preview decimation must go through the cached `_decimated_for_viz(mesh, cache_key, face_count)` with a stable cache key, so Streamlit reruns don't re-decimate. It returns `app_helpers.preview_copy`, a geometry-only copy: under the face budget `decimate_mesh` returns its input, and the process-wide cache then held whole meshes with their metadata. The result stored in the session keeps only what the view reads (decimated inlay, placements, recesses, stats). - **Keys of process-wide caches identify the cached object by content, never by name or size.** `st.cache_resource` is shared by every session in the process: keyed by `name:size`, a second user uploading a different `model.stl` of the same size saw the first user's model. Uploads are identified by their content hash (`content_hashes[uf.file_id]`, computed once per upload); a cached figure's key carries everything that determines it (content hash, scale, pitch, decimation, rotation) - the inlay's hash does not determine a figure. - **Never repair with pymeshfix's defaults.** `MeshFix.repair()` defaults to `remove_smallest_components=True`, which keeps only the shell with the most triangles: a holed figure lost its separate base (or, sorted by triangle count, its body). `prepare_figure` passes `remove_smallest_components=False`. -- **Raw input meshes are voxelized through `_voxelize_surface`, never `mesh.voxelized()`.** trimesh's voxelizer subdivides until every edge is below `pitch / 2`, so its cost grows with edge length² - a low-poly CAD rod took 1.95 GB, a 250 mm bar aborted. `_voxelize_surface` samples triangles in rows and scales with area. `dilate` and `_solidify_figure` may keep `mesh.voxelized()`: they voxelize marching-cubes output with edges of about one pitch. `_check_grid_size` refuses grids above `MAX_GRID_VOXELS` before anything is allocated. +- **Every mesh is voxelized through `_voxelize_surface`, never `mesh.voxelized()`.** trimesh's voxelizer subdivides until every edge is below `pitch / 2`, so its cost grows with edge length² - a low-poly CAD rod took 1.95 GB, a 250 mm bar aborted. That is not only a problem of raw input: `dilate` voxelizes marching-cubes output at *half* the pitch, where every triangle was split into 64, and that one call was the memory peak of a whole run (3.8 GB for a 60 mm cube at the defaults, more than 10 GB for 100 mm). `_voxelize_surface` samples triangles in rows, scales with area and marks its samples in batches (`_SURFACE_SAMPLE_BATCH`), so beyond the grid itself its memory does not depend on the mesh - don't collect all samples in one array again. `_check_grid_size` refuses grids above `MAX_GRID_VOXELS` before anything is allocated - in `prepare_figure` (the upload), in `dilate` (the rotated mesh plus its padding) and in `_solidify_figure` (the cutter up to the top face). The upload's extents say nothing about a rotated figure: any new step that builds a grid needs its own call. - **Streamlit drops a widget's state while the widget is not rendered.** Values that must survive a hidden section live in plain session keys and the widget only writes through to them (`app._select`); otherwise hiding the rotation section reset the step to 45° and a stored 350° crashed every rerun. **Streamlit 1.62 also sends a selectbox value as its label**, so after a language switch the old label matches no option: every selectbox with translated labels goes through `_select` (raw value in a plain key, labels resolved once per run, unknown values map to no label). A lenient `format_func` (`"x" if a == "x" else `) turned the stale label into the value `"left/right"` and crashed the next run. diff --git a/README.md b/README.md index e26e388..a41b01c 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,8 @@ docker compose up --build Files enter the web app through the **upload dialog** in the sidebar, and the finished inlay leaves through the **download button** — no volume required. +The bundled `compose.yaml` starts the container in German (`INLAYER_LANG=de`); +remove that line for English. The container inherits the same 250 MB upload limit; override it from the host without editing `compose.yaml`: @@ -144,6 +146,15 @@ without editing `compose.yaml`: STREAMLIT_SERVER_MAX_UPLOAD_SIZE=500 docker compose up --build ``` +`compose.yaml` also caps what the container may take from the host: 4 GB of +memory and 4 CPUs. A run that needs more is stopped by the kernel and the +container restarts, instead of the whole machine running out of memory. Set the +limits to fit your host the same way: + +```bash +INLAYER_MEM_LIMIT=8g INLAYER_CPUS=8 docker compose up --build +``` + The `./data` volume in `compose.yaml` exists purely for **CLI use inside the container**: @@ -178,12 +189,12 @@ The web app is a full GUI with live preview: | Feature | Description | |---|---| | 🌍 **English / German** | Switch the interface language in the sidebar at any time | -| 📂 **File upload** | Upload one or **several** STL models straight from the sidebar | -| 🪞 **Instant preview** | Uploaded figures appear in the 3D viewer immediately, rotations update live — without running the pipeline | +| 📂 **File upload** | Upload one or **several** STL models straight from the sidebar. Without an upload, *Generate* takes a `figure.stl` from the working directory, like the CLI | +| 🪞 **Instant preview** | Uploaded figures appear in the 3D viewer immediately, rotations update live — without running the pipeline. After a run the viewer shows that result until you generate again; if settings or uploads have changed since, a warning says it is out of date | | 🧩 **Auto arrangement** | Multiple figures are placed without collisions (`compact` / `horizontal` / `vertical`) with an adjustable gap — it follows the wall thickness until you set it yourself | | 🎚️ **Interactive parameters** | Clearance, wall thickness, insert depth and voxel resolution via sliders | -| 🎯 **Manual positioning** | Move each figure (or all of them together) in X/Y/Z inside the box | -| 🔄 **Manual rotation** | Rotate each figure (or all of them together) about X/Y/Z | +| 🎯 **Manual positioning** | Move each figure (or all of them together) in X/Y/Z inside the box, in steps of 1, 5 or 10 mm | +| 🔄 **Manual rotation** | Rotate each figure (or all of them together) about X/Y/Z, in steps of 1, 5, 10 or 45° | | 🖐️ **Finger recesses** | Optional hemispherical cut-outs beside each figure as a removal aid (adjustable radius, axis and depth; the position along the figure is set **per figure**) | | ⚡ **Multi-threading** | Optionally process several figures in parallel across CPU cores (checkbox in the sidebar) | | 📐 **Box overrides** | Optionally force fixed box dimensions | @@ -194,8 +205,11 @@ The web app is a full GUI with live preview: ### Command line (CLI) -```powershell -# Minimal invocation (reads figure.stl, writes inlay.stl) +The multi-line examples use bash line continuations; in PowerShell end a +continued line with a backtick (`` ` ``) instead of a backslash. + +```bash +# Minimal invocation (reads figure.stl from the current directory, writes inlay.stl) python inlayer.py # Adjust every parameter @@ -245,7 +259,8 @@ you can inspect it. | Exit code | Meaning | |---|---| | `0` | Inlay written, wall check passed | -| `2` | Invalid arguments (argparse usage error) — nothing was computed | +| `1` | The run failed — an unreadable STL, a voxel grid too large for the resolution, a box too small to leave any material, not enough memory. One line on stderr says which; no inlay is written | +| `2` | Invalid arguments (argparse usage error), an input file that does not exist or an output path that cannot be written — nothing was computed | | `3` | Inlay written, but the wall check found problems | All `--finger-recess-position` values are validated while parsing. Their count @@ -275,7 +290,8 @@ pip install -r requirements-dev.txt # Full suite (~70 s) pytest -# Faster: skips end-to-end / CLI runs and the finest-pitch geometry checks +# Faster: skips the tests that start the CLI as a subprocess and the +# finest-pitch geometry checks (the CLI's flags are still tested, in-process) pytest -m "not slow" # A single file or test @@ -363,19 +379,28 @@ value as a usage error before any work starts. ### Print settings -| Parameter | Default | Description | +| Parameter / CLI flag | Default | Description | |---|---|---| -| `clearance` | 0.4 mm | Clearance between figure and cavity | -| `wall_thickness` | 2.0 mm | Minimum side and floor wall thickness | -| `depth_fraction` | 0.7 | Fraction of **each** figure's own height inside its cavity (30 % stands proud) | +| `clearance` / `-c`, `--clearance` | 0.4 mm | Clearance between figure and cavity | +| `wall_thickness` / `-w`, `--wall-thickness` | 2.0 mm | Minimum side and floor wall thickness | +| `depth_fraction` / `-df`, `--depth-fraction` | 0.7 | Fraction of **each** figure's own height inside its cavity (30 % stands proud) | + +> The clearance is cut in voxels, so it is approximate. It takes effect in +> steps of half a `voxel_pitch` — at the default pitch 0.1, 0.2 and 0.3 mm give +> the same inlay, as do 0.4 and 0.5 — and the cavity never comes out tighter +> than about one to two `voxel_pitch` around the figure's walls, whatever +> smaller value is set: a configured 0.4 mm becomes 0.4 to 0.8 mm at the +> defaults (0.6 on average) and about 1.5 mm at `voxel_pitch` 1.0. Sharp edges +> at the bottom of a figure can sit closer. For a snug fit choose a finer +> `voxel_pitch`. ### Mesh processing -| Parameter | Default | Description | +| Parameter / CLI flag | Default | Description | |---|---|---| -| `voxel_pitch` | 0.4 mm | Resolution of the voxel operations | -| `decimate_faces` | 20,000 | Target triangle count after decimation | -| `stl_unit_to_mm` | 1.0 | Unit scaling (1.0 = mm, 25.4 = inch) | +| `voxel_pitch` / `-vp`, `--voxel-pitch` | 0.4 mm | Resolution of the voxel operations | +| `decimate_faces` / `--decimate-faces` | 20,000 | Target triangle count after decimation | +| `stl_unit_to_mm` / `--scale` | 1.0 | Unit scaling (1.0 = mm, 25.4 = inch) | ### Box shape & dimensions (optional) @@ -448,11 +473,11 @@ value as a usage error before any work starts. > a single one) above the position slider. Each figure keeps its own value; > the other recess settings apply to all of them. > - **CLI:** `--finger-recess-position` takes either one value for every figure -> or one value per `-i` file, in that order. Any other count is an error rather -> than a guess. +> or one value per `-i` file, in that order. With `--finger-recesses` any other +> count is an error rather than a guess. > - **API:** `build_inlay(..., individual_recess_positions=[...])` — figures -> without an entry fall back to `Config.finger_recess_position`. App and CLI -> always pass the per-figure list. +> without an entry fall back to `Config.finger_recess_position`. The app +> always passes the per-figure list, the CLI whenever recesses are on. ### Language (optional) @@ -477,16 +502,18 @@ value as a usage error before any work starts. > `min(figures, cores, 4)`×). The worker count is capped at 4 because parallel > voxel grids multiply memory use. -> If a box dimension is smaller than the computed minimum, a warning is issued — -> the computation does not abort. +> If a box dimension is smaller than the computed minimum, a warning is issued +> and the wall check lists the walls that came out too thin — the computation +> does not abort. The one exception is a box that lies entirely inside a +> cavity: nothing of it is left, and the run ends with an error saying so. --- ## ⚠️ Technical notes - **Even clearance on slopes:** the tolerance offset grows the figure by the same amount in every direction (within about 5 %). The plain voxel dilation used before grew an octahedron — with 2 mm of clearance a figure got only about 1.5 mm on 45° slopes and could jam there. -- **Voxel scaling:** voxel-based steps scale O(n³) with resolution. Halving `VOXEL_PITCH` raises memory use by roughly 8×. -- **trimesh 4.x quirk:** `VoxelGrid.marching_cubes` does not apply the grid transform to the vertices. The code therefore calls `apply_transform(vox.transform)` manually after every `marching_cubes` call. +- **Voxel scaling:** voxel-based steps scale O(n³) with resolution. Halving `voxel_pitch` raises memory use by roughly 8×. +- **trimesh 4.x quirk:** `VoxelGrid.marching_cubes` does not apply the grid transform to the vertices. The code therefore applies it itself, in `_grid_to_mesh` — the one place that calls `marching_cubes`. - **CSG engine:** boolean operations run in `manifold3d` (directly, never trimesh's default engine). manifold3d already parallelises internally across cores. - **Multi-threading:** the optional parallelisation (`--parallel` / web-app checkbox) uses a `ThreadPoolExecutor` — numpy/scipy/trimesh release the GIL during their C calls, so threads give real multi-core speedup without process overhead. With parallelisation enabled, log lines from individual figures can interleave. - **Decimation is serialised:** `fast_simplification` (also behind `trimesh.simplify_quadric_decimation`) loads the mesh into process-global state. Concurrent calls from several threads therefore hand *every* caller the same mesh — with `--parallel` this produced an inlay whose cavities all showed the same figure. All decimation now goes through `inlayer.decimate_mesh` behind a lock; only the remaining steps (voxelisation, dilation, solidification) still run in parallel. @@ -497,8 +524,8 @@ value as a usage error before any work starts. - **Manual offsets** move a figure inside a box that stays put; walls made too thin that way are reported by the check. - **Cavities stay open:** each cavity is extruded up through the top face. With `depth_fraction = 1.0` and a negative Z offset the pocket used to close at the top — a sealed void in the print. - **Repair only when needed:** `prepare_figure` skips `pymeshfix` when the loaded mesh is already watertight **and** consistently wound — the normal case for cleanly exported STLs. That halves the step's runtime (measured 1.99 s → 1.08 s at 82k triangles) and does not change the geometry. Watertightness alone is not a sufficient criterion: a mesh with inverted faces is watertight but would corrupt the subsequent voxel fill. When the repair does run, it keeps **every** shell of the file: pymeshfix's default drops all but the shell with the most triangles, so a miniature with a separate base used to lose the base as soon as the file had a single hole anywhere. -- **Low-poly meshes:** input meshes are voxelised by sampling each triangle in rows along its longest edge, so the cost follows the surface area. trimesh's own voxeliser subdivides until *every* edge is below `voxel_pitch / 2`, which costs (edge length / pitch)² per triangle — the long, thin triangles of CAD exports (rods, pins, profiles) took gigabytes or aborted with "max_iter exceeded". A figure whose voxel grid would exceed `MAX_GRID_VOXELS` (10⁹ voxels at `voxel_pitch / 2`, the finest grid in the pipeline) is refused with a message suggesting a larger pitch, instead of exhausting the machine's memory. -- **Broken files:** an empty or unreadable STL is rejected with a message naming the file. In the web app the instant preview skips such a file with a warning and still shows the others. +- **Low-poly meshes:** input meshes are voxelised by sampling each triangle in rows along its longest edge, so the cost follows the surface area. trimesh's own voxeliser subdivides until *every* edge is below `voxel_pitch / 2`, which costs (edge length / pitch)² per triangle — the long, thin triangles of CAD exports (rods, pins, profiles) took gigabytes or aborted with "max_iter exceeded". The same sampler now voxelizes the later steps too and works in batches: the tolerance offset used to be the memory peak of a run (3.8 GB for a 60 mm cube at the default settings, more than 10 GB for 100 mm) and now needs roughly an eighth of that (0.5 GB and 1.3 GB), the step itself a third of the time. Cavities can differ from earlier versions by a few hundredths of a millimetre. A figure whose voxel grid would exceed `MAX_GRID_VOXELS` (10⁹ voxels at `voxel_pitch / 2`, the finest grid in the pipeline) is refused with a message suggesting a larger pitch, instead of exhausting the machine's memory. The bound is checked wherever a grid is built — on the upload, on the rotated figure plus its clearance padding, and on every cutter — so neither a rotation nor a large Z offset slips past it. +- **Broken files:** an empty or unreadable STL is rejected with a message naming the file (in the web app: the upload, not the temporary file it is read from). So is a file whose coordinates are all NaN or infinite, and one without volume — a flat surface or triangles collapsed to a line cannot be closed into a solid. In the web app the instant preview skips such a file with a warning and still shows the others. - **Wall check:** measured exactly on the meshes (manifold3d) while `build_inlay` still has every figure's cutter: side and floor walls per figure, the wall between each pair of cavities, sealed voids and figures without a cavity. Each finding names its figure; `passes_min_wall` is true only without any finding. The tolerance is 0.05 mm (float rounding, the cylinder's chord error). The earlier voxel-based check overestimated walls by up to one pitch, ignored the walls between cavities and passed an inlay without any cavity. --- diff --git a/app.py b/app.py index df50c64..51a8110 100644 --- a/app.py +++ b/app.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import io +import logging import os import time import tempfile @@ -94,18 +95,23 @@ # --- Caching ---------------------------------------------------------------- @st.cache_resource(show_spinner=False, max_entries=32) def _cached_prepare( - _path: str, file_hash: str, scale: float, pitch: float, decimate_faces: int + _path: str, _name: str, file_hash: str, scale: float, pitch: float, decimate_faces: int ) -> trimesh.Trimesh: - """Step 1, cached as long as file content and parameters stay the same.""" + """Step 1, cached as long as file content and parameters stay the same. + + `_path` is the upload's temporary file, `_name` what the user called it: + errors name the latter. Neither is part of the cache key. + """ config = inlayer.Config( stl_unit_to_mm=scale, voxel_pitch=pitch, decimate_faces=decimate_faces ) - return inlayer.prepare_figure(_path, config) + return inlayer.prepare_figure(_path, config, name=_name) @st.cache_resource(show_spinner=False, max_entries=32) def _cached_dilate( _mesh: trimesh.Trimesh, + _name: str, file_hash: str, scale: float, pitch: float, @@ -115,9 +121,12 @@ def _cached_dilate( rot_y: float, rot_z: float, ) -> trimesh.Trimesh: - """Step 2, cached as long as figure, clearance, pitch and rotation stay the same.""" + """Step 2, cached as long as figure, clearance, pitch and rotation stay the same. + + `_name` only labels an error message and is not part of the cache key. + """ config = inlayer.Config(voxel_pitch=pitch) - return inlayer.dilate(_mesh, clearance, config) + return inlayer.dilate(_mesh, clearance, config, name=_name) # Face budget of the inlay in the result view @@ -755,7 +764,7 @@ def _prepare_one(i: int): if is_multi and not enable_parallel: logger.log(t("app.log.figure_n", i=i + 1, n=n_files, name=file_names[i])) mesh = _cached_prepare( - input_paths[i], file_hashes[i], scale, voxel_pitch, decimate_faces + input_paths[i], file_names[i], file_hashes[i], scale, voxel_pitch, decimate_faces ) rotated = inlayer.apply_euler_rotation(mesh, *rotations[i]) if any(rotations[i]) else mesh return mesh, rotated @@ -771,8 +780,8 @@ def _prepare_one(i: int): t_step = logger.log(t("app.log.dilate"), t_step) dilated = inlayer._parallel_map( lambda i: _cached_dilate( - fig_meshes[i], file_hashes[i], scale, voxel_pitch, decimate_faces, - clearance, *rotations[i], + fig_meshes[i], file_names[i], file_hashes[i], scale, voxel_pitch, + decimate_faces, clearance, *rotations[i], ), range(n_files), config, what=t("pipeline.what.dilate"), worker_init=_attach_ctx, ) @@ -834,9 +843,17 @@ def _prepare_one(i: int): "is_multi": is_multi, } - except Exception as e: - status_box.error(t("app.error.failed", error=e)) - st.exception(e) + except (ValueError, RuntimeError, MemoryError) as e: + # Raised on purpose, with a message written for the user: an unusable + # upload, a grid beyond the limit, a box that leaves no material. + reason = t("error.out_of_memory") if isinstance(e, MemoryError) else e + status_box.error(t("app.error.failed", error=reason)) + except Exception: + # Anything else is a bug. Its traceback - absolute paths, source lines - + # belongs in the server log, not in the browser of whoever uses the app + # (st.exception shows it whatever client.showErrorDetails says). + logging.getLogger("inlayer.app").exception("Inlay run failed") + status_box.error(t("app.error.unexpected")) finally: for tp in tmp_paths: diff --git a/compose.yaml b/compose.yaml index 0f2ea88..e44eb93 100644 --- a/compose.yaml +++ b/compose.yaml @@ -25,4 +25,13 @@ services: # Startup language of this instance. Without a value the app starts in # English; it can still be switched per session in the sidebar. - INLAYER_LANG=de + # Upper bounds for what one container may take from the host. Every run + # voxelizes its figures in memory (about 0.5 GB for a 60 mm figure at the + # default resolution, 1.3 GB for 100 mm, more with a finer pitch or several + # sessions at once), and anyone who can reach the port can start one. + # Without a limit a large upload takes the whole host down instead of this + # container; with it the kernel stops the container and it restarts. + # Adjust to the machine, e.g. `INLAYER_MEM_LIMIT=8g docker compose up`. + mem_limit: ${INLAYER_MEM_LIMIT:-4g} + cpus: ${INLAYER_CPUS:-4} restart: unless-stopped \ No newline at end of file diff --git a/i18n.py b/i18n.py index 3eaa6ba..55197b1 100644 --- a/i18n.py +++ b/i18n.py @@ -459,6 +459,10 @@ def t(key: str, /, **kwargs: object) -> str: "de": "Berechnung fehlgeschlagen: {error}", "en": "Computation failed: {error}", }, + "app.error.unexpected": { + "de": "Bei der Berechnung ist ein unerwarteter Fehler aufgetreten. Die Details stehen im Protokoll des Servers.", + "en": "Something unexpected went wrong during the computation. The details are in the server's log.", + }, # --- Fortschritt & Protokoll ------------------------------------------- "app.label.figures": { "de": "{n} Figuren", @@ -803,10 +807,22 @@ def t(key: str, /, **kwargs: object) -> str: "de": "{name} enthält keine Dreiecke – die Datei ist leer oder keine gültige STL.", "en": "{name} contains no triangles – the file is empty or not a valid STL.", }, + "error.out_of_memory": { + "de": "Der Arbeitsspeicher reicht für diese Auflösung nicht aus. Wähle einen größeren voxel_pitch.", + "en": "Not enough memory for this resolution. Choose a larger voxel_pitch.", + }, + "error.not_solid": { + "de": "{name} hat kein Volumen – die Datei muss einen geschlossenen Körper beschreiben, keine flache Fläche.", + "en": "{name} has no volume – the file must describe a closed solid, not a flat surface.", + }, "error.grid_too_large": { "de": "{name} ist für diese Auflösung zu groß: das Voxelgitter hätte {voxels} Voxel (Grenze {limit}). Wähle einen größeren voxel_pitch oder prüfe die Skalierung.", "en": "{name} is too large for this resolution: its voxel grid would have {voxels} voxels (limit {limit}). Choose a larger voxel_pitch or check the scale.", }, + "error.unnamed_figure": { + "de": "Die Figur", + "en": "The figure", + }, # --- CLI ---------------------------------------------------------------- "cli.description": { "de": "Inlayer: Erstellt 3D-druckbare Verpackungseinleger für Figuren.", @@ -833,8 +849,8 @@ def t(key: str, /, **kwargs: object) -> str: "en": "Minimum wall thickness in mm (default: {default})", }, "cli.depth_fraction": { - "de": "Tiefe des Einlegers relativ zur Figur (0.0-1.0, Standard: {default})", - "en": "Insert depth relative to the figure (0.0-1.0, default: {default})", + "de": "Tiefe des Einlegers relativ zur Figur (größer 0 bis 1.0, Standard: {default})", + "en": "Insert depth relative to the figure (above 0 up to 1.0, default: {default})", }, "cli.voxel_pitch": { "de": "Auflösung des Voxelgitters in mm (Standard: {default})", @@ -849,8 +865,8 @@ def t(key: str, /, **kwargs: object) -> str: "en": "Scale factor: 1.0=mm, 0.1=0.1mm, 25.4=inch (default: {default})", }, "cli.box_shape": { - "de": "Form der Box: 'box' (Quader) oder 'cylinder' (Zylinder) (Standard: 'box')", - "en": "Box shape: 'box' (cuboid) or 'cylinder' (default: 'box')", + "de": "Form der Box: 'box' (Quader) oder 'cylinder' (Zylinder) (Standard: '{default}')", + "en": "Box shape: 'box' (cuboid) or 'cylinder' (default: '{default}')", }, "cli.box_diameter": { "de": "Manueller Zylinder-Durchmesser in mm, nur bei --box-shape cylinder (Standard: automatisch)", @@ -869,16 +885,16 @@ def t(key: str, /, **kwargs: object) -> str: "en": "Manual box height (Z) in mm (default: automatic)", }, "cli.offset_x": { - "de": "Manuelle X-Verschiebung der Figur(en) in mm (Standard: 0.0)", - "en": "Manual X offset of the figure(s) in mm (default: 0.0)", + "de": "Manuelle X-Verschiebung der Figur(en) in mm (Standard: {default})", + "en": "Manual X offset of the figure(s) in mm (default: {default})", }, "cli.offset_y": { - "de": "Manuelle Y-Verschiebung der Figur(en) in mm (Standard: 0.0)", - "en": "Manual Y offset of the figure(s) in mm (default: 0.0)", + "de": "Manuelle Y-Verschiebung der Figur(en) in mm (Standard: {default})", + "en": "Manual Y offset of the figure(s) in mm (default: {default})", }, "cli.offset_z": { - "de": "Manuelle Z-Verschiebung der Figur(en) in mm (Standard: 0.0)", - "en": "Manual Z offset of the figure(s) in mm (default: 0.0)", + "de": "Manuelle Z-Verschiebung der Figur(en) in mm (Standard: {default})", + "en": "Manual Z offset of the figure(s) in mm (default: {default})", }, "cli.rot_x": { "de": "Manuelle X-Rotation der Figur(en) in Grad (Standard: 0.0)", @@ -897,8 +913,8 @@ def t(key: str, /, **kwargs: object) -> str: "en": "Gap between figures in mm (default: wall_thickness)", }, "cli.layout_style": { - "de": "Anordnungsmethode bei mehreren Figuren: 'compact', 'horizontal', 'vertical' (Standard: 'compact')", - "en": "Arrangement for multiple figures: 'compact', 'horizontal', 'vertical' (default: 'compact')", + "de": "Anordnungsmethode bei mehreren Figuren: 'compact', 'horizontal', 'vertical' (Standard: '{default}')", + "en": "Arrangement for multiple figures: 'compact', 'horizontal', 'vertical' (default: '{default}')", }, "cli.finger_recesses": { "de": "Fingermulden (Entnahmehilfe) links/rechts jeder Figur aktivieren", @@ -909,8 +925,8 @@ def t(key: str, /, **kwargs: object) -> str: "en": "Radius of the finger recesses in mm (default: {default})", }, "cli.finger_recess_axis": { - "de": "Achse der Fingermulden: 'x' (links/rechts) oder 'y' (vorne/hinten) (Standard: 'x')", - "en": "Axis of the finger recesses: 'x' (left/right) or 'y' (front/back) (default: 'x')", + "de": "Achse der Fingermulden: 'x' (links/rechts) oder 'y' (vorne/hinten) (Standard: '{default}')", + "en": "Axis of the finger recesses: 'x' (left/right) or 'y' (front/back) (default: '{default}')", }, "cli.finger_recess_z_offset": { "de": "Absenkung der Fingermulden unter die Box-Oberkante in mm (Standard: {default})", @@ -924,6 +940,14 @@ def t(key: str, /, **kwargs: object) -> str: "de": "--finger-recess-position erwartet 1 Wert oder einen je Eingabedatei ({given} angegeben, {n} Dateien)", "en": "--finger-recess-position expects 1 value or one per input file (got {given} for {n} files)", }, + "cli.error.output_not_writable": { + "de": "Ausgabedatei {path} kann nicht geschrieben werden: der Pfad ist ein Verzeichnis oder schreibgeschützt, oder sein Verzeichnis fehlt oder ist nicht beschreibbar.", + "en": "Cannot write the output file {path}: the path is a directory or write-protected, or its directory is missing or not writable.", + }, + "cli.error.failed": { + "de": "Fehler: {error}", + "en": "Error: {error}", + }, "cli.parallel": { "de": "Mehrere Figuren parallel auf mehreren CPU-Kernen verarbeiten (max. {workers} Threads, erhöht den Speicherbedarf)", "en": "Process several figures in parallel across CPU cores (max. {workers} threads, increases memory use)", diff --git a/inlayer.py b/inlayer.py index 3d148c8..21393ae 100644 --- a/inlayer.py +++ b/inlayer.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import argparse import io import math import sys @@ -31,7 +32,7 @@ from i18n import t as t_ from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, fields -from typing import Callable, Final, Any, Iterable, TypeVar, cast +from typing import Callable, Final, Any, Iterable, Iterator, TypeVar, cast from manifold3d import Manifold, Mesh, OpType from scipy.ndimage import ( binary_closing, @@ -322,13 +323,15 @@ def load_mesh(source: Any, name: str, file_type: str | None = None) -> trimesh.T trimesh does not raise on an empty or garbled STL - it returns a mesh without triangles, and the pipeline then fails far away with a cryptic 'NoneType' error. Uploads are untrusted input, so any file that yields no - triangles is rejected here with a message that names it. + triangles is rejected here with a message that names it. So is a file + whose coordinates are all NaN or infinite: trimesh drops every vertex and + keeps the faces, and the first look at the bounds raised an IndexError. """ try: mesh = trimesh.load(source, file_type=file_type, force="mesh") except Exception as exc: raise ValueError(t_("error.empty_input", name=name)) from exc - if not isinstance(mesh, trimesh.Trimesh) or len(mesh.faces) == 0: + if not isinstance(mesh, trimesh.Trimesh) or len(mesh.faces) == 0 or len(mesh.vertices) == 0: raise ValueError(t_("error.empty_input", name=name)) return mesh @@ -338,6 +341,12 @@ def load_mesh(source: Any, name: str, file_type: str | None = None) -> trimesh.T # scipy's morphology holds several copies of it. Without the bound a small # upload scaled up (or a tiny STL with a huge bounding box) takes down a shared # web-app container instead of failing with a message. +# +# Checked wherever a grid is built, on the mesh that is voxelized there: +# prepare_figure (early, before the repair), dilate (the rotated figure plus +# its padding) and _solidify_figure (the cutter up to the box's top face). +# Checking the upload alone let a rotation through: a 1 m rod of 16 mm passes +# with 3e7 voxels and needs 2.6e10 once it is turned onto the space diagonal. MAX_GRID_VOXELS: Final[int] = 1_000_000_000 # Sample spacing of `_voxelize_surface`, as a fraction of the pitch. Samples @@ -348,13 +357,19 @@ def load_mesh(source: Any, name: str, file_type: str | None = None) -> trimesh.T # subdivision voxelizer gives with its pitch / 2 edge limit. _SURFACE_SAMPLE_SPACING: Final[float] = 0.4 +# `_voxelize_surface` never holds more than this many triangles, rows or sample +# points at a time (roughly 200 bytes each across the arrays of one batch), so +# its memory stays flat however large or finely tessellated the mesh is. All +# samples at once cost gigabytes for a figure of 60 mm. +_SURFACE_SAMPLE_BATCH: Final[int] = 100_000 + -def _check_grid_size(extents: np.ndarray, pitch: float, name: str) -> None: +def _check_grid_size(extents: Any, pitch: float, name: str | None) -> None: """Refuses a figure whose voxel grid at `pitch` would exceed MAX_GRID_VOXELS.""" - voxels = float(np.prod(np.ceil(np.asarray(extents) / pitch) + 1)) + voxels = float(np.prod(np.ceil(np.asarray(extents, dtype=float) / pitch) + 1)) if voxels > MAX_GRID_VOXELS: - raise ValueError(t_("error.grid_too_large", name=name, voxels=f"{voxels:.1e}", - limit=f"{MAX_GRID_VOXELS:.1e}")) + raise ValueError(t_("error.grid_too_large", name=name or t_("error.unnamed_figure"), + voxels=f"{voxels:.1e}", limit=f"{MAX_GRID_VOXELS:.1e}")) def _lerp(p: np.ndarray, r: np.ndarray, t: np.ndarray) -> np.ndarray: @@ -367,13 +382,20 @@ def _lerp(p: np.ndarray, r: np.ndarray, t: np.ndarray) -> np.ndarray: return np.where(t == 1.0, r, p + t * (r - p)) -def _segment_points(p0: np.ndarray, p1: np.ndarray, spacing: float) -> np.ndarray: - """Evenly spaced points on each segment p0[i]-p1[i], endpoints included.""" +def _segment_batches(p0: np.ndarray, p1: np.ndarray, spacing: float) -> Iterator[np.ndarray]: + """Evenly spaced points on each segment p0[i]-p1[i], endpoints included. + + Yields them in batches of at most _SURFACE_SAMPLE_BATCH points; a batch may + end in the middle of a segment, so one very long segment is no exception. + """ count = np.ceil(np.linalg.norm(p1 - p0, axis=1) / spacing).astype(np.int64) + 1 - seg = np.repeat(np.arange(len(p0)), count) - step = np.arange(len(seg)) - np.repeat(np.cumsum(count) - count, count) - q = step / np.maximum(count[seg] - 1, 1) - return _lerp(p0[seg], p1[seg], q[:, None]) + first = np.cumsum(count) - count # index of each segment's first point + total = int(count.sum()) + for begin in range(0, total, _SURFACE_SAMPLE_BATCH): + k = np.arange(begin, min(begin + _SURFACE_SAMPLE_BATCH, total)) + seg = np.searchsorted(first, k, side="right") - 1 + q = (k - first[seg]) / np.maximum(count[seg] - 1, 1) + yield _lerp(p0[seg], p1[seg], q[:, None]) def _voxelize_surface(mesh: trimesh.Trimesh, pitch: float) -> Any: @@ -383,56 +405,90 @@ def _voxelize_surface(mesh: trimesh.Trimesh, pitch: float) -> Any: shorter than pitch / 2: a long, thin triangle (CAD exports of rods, pins, profiles) costs (length / pitch)² sub-triangles however little area it has. A 256-triangle rod of 40 mm took 1.95 GB, a 12-triangle 250 mm bar aborted - with "max_iter exceeded". Here each triangle is sampled in rows parallel to - its longest edge instead, plus its three edges; see _SURFACE_SAMPLE_SPACING - for why the shell stays closed. Only for raw input meshes - the later steps - voxelize marching-cubes output whose edges are about one pitch long. + with "max_iter exceeded" - and on a fine mesh every triangle is still split + into 16 or 64, which made dilate the memory peak of a whole run. Here each + triangle is sampled in rows parallel to its longest edge instead, plus its + three edges; see _SURFACE_SAMPLE_SPACING for why the shell stays closed. + + The samples are marked batch by batch (_SURFACE_SAMPLE_BATCH), so beyond + the grid itself the memory does not depend on the mesh. """ - tri = mesh.triangles - n = len(tri) + if len(mesh.faces) == 0: + raise ValueError(t_("error.empty_meshes")) spacing = _SURFACE_SAMPLE_SPACING * pitch - lengths = np.linalg.norm(np.roll(tri, -1, axis=1) - tri, axis=2) - # Rotate every triangle so that a-b is its longest edge: the apex c then - # projects onto a-b, so every row parallel to a-b stays inside the triangle. - order = (lengths.argmax(axis=1)[:, None] + np.arange(3)) % 3 - a, b, c = (tri[np.arange(n), order[:, k]] for k in range(3)) - longest = lengths.max(axis=1) - height = np.linalg.norm(np.cross(b - a, c - a), axis=1) / np.maximum(longest, 1e-12) - - n_rows = np.ceil(height / spacing).astype(np.int64) + 1 - row_tri = np.repeat(np.arange(n), n_rows) - row = np.arange(len(row_tri)) - np.repeat(np.cumsum(n_rows) - n_rows, n_rows) - s = (row / np.maximum(n_rows[row_tri] - 1, 1))[:, None] - ra, rb, rc = a[row_tri], b[row_tri], c[row_tri] - points = np.concatenate([ - _segment_points(_lerp(ra, rc, s), _lerp(rb, rc, s), spacing), - _segment_points(b, c, spacing), - _segment_points(c, a, spacing), - ]) - - # Same rounding as trimesh.voxel.creation.voxelize_subdivide - hit = np.round(points / pitch).astype(np.int64) - origin = hit.min(axis=0) - matrix = np.zeros(hit.max(axis=0) - origin + 1, dtype=bool) - matrix[tuple((hit - origin).T)] = True + # One voxel of margin: an interior sample may land an ulp outside the + # vertices' range. The grid is cropped to the voxels actually hit below. + low = np.floor(mesh.bounds[0] / pitch).astype(np.int64) - 1 + high = np.ceil(mesh.bounds[1] / pitch).astype(np.int64) + 1 + matrix = np.zeros(high - low + 1, dtype=bool) + + def mark(p0: np.ndarray, p1: np.ndarray) -> None: + for points in _segment_batches(p0, p1, spacing): + # Same rounding as trimesh.voxel.creation.voxelize_subdivide + hit = np.round(points / pitch).astype(np.int64) - low + matrix[hit[:, 0], hit[:, 1], hit[:, 2]] = True + + vertices, faces = mesh.vertices, mesh.faces + for block in range(0, len(faces), _SURFACE_SAMPLE_BATCH): + tri = vertices[faces[block:block + _SURFACE_SAMPLE_BATCH]] + n = len(tri) + lengths = np.linalg.norm(np.roll(tri, -1, axis=1) - tri, axis=2) + # Rotate every triangle so that a-b is its longest edge: the apex c then + # projects onto a-b, so every row parallel to a-b stays inside the triangle. + order = (lengths.argmax(axis=1)[:, None] + np.arange(3)) % 3 + a, b, c = (tri[np.arange(n), order[:, k]] for k in range(3)) + longest = lengths.max(axis=1) + height = np.linalg.norm(np.cross(b - a, c - a), axis=1) / np.maximum(longest, 1e-12) + n_rows = np.ceil(height / spacing).astype(np.int64) + 1 + mark(b, c) + mark(c, a) + + # The rows of as many triangles at a time as fit the batch size (a + # single tall triangle may exceed it; its rows are bounded by the grid) + rows_through = np.cumsum(n_rows) + i = 0 + while i < n: + done = int(rows_through[i - 1]) if i else 0 + j = max(i + 1, int(np.searchsorted( + rows_through, done + _SURFACE_SAMPLE_BATCH, side="right"))) + rows = n_rows[i:j] + row_tri = np.repeat(np.arange(i, j), rows) + row = np.arange(len(row_tri)) - np.repeat(rows_through[i:j] - rows - done, rows) + s = (row / np.maximum(n_rows[row_tri] - 1, 1))[:, None] + ra, rb, rc = a[row_tri], b[row_tri], c[row_tri] + mark(_lerp(ra, rc, s), _lerp(rb, rc, s)) + i = j + + used = [np.flatnonzero(matrix.any(axis=tuple(k for k in range(3) if k != axis))) + for axis in range(3)] + origin = low + np.array([u[0] for u in used]) + matrix = matrix[tuple(slice(u[0], u[-1] + 1) for u in used)] transform = trimesh.transformations.scale_and_translate( scale=pitch, translate=origin * pitch ) return cast(Any, trimesh.voxel.VoxelGrid)(matrix, transform=transform) -def prepare_figure(path: str, config: Config = _DEFAULT_CFG) -> trimesh.Trimesh: - """Loads, repairs, decimates and smooths an input figure.""" +def prepare_figure( + path: str, config: Config = _DEFAULT_CFG, name: str | None = None +) -> trimesh.Trimesh: + """Loads, repairs, decimates and smooths an input figure. + + `name` is what log lines and errors call the file - by default its path. + The web app reads uploads from temporary files: a message about + /tmp/tmpk3x9.stl tells the user nothing about which of their files it means. + """ if not os.path.isfile(path): raise FileNotFoundError(t_("error.input_not_found", path=path)) + name = name or path t = time.perf_counter() - _log(t_("pipeline.load", path=path)) - m = load_mesh(path, name=path) + _log(t_("pipeline.load", path=name)) + m = load_mesh(path, name=name) if config.stl_unit_to_mm != 1.0: m.apply_scale(config.stl_unit_to_mm) _log(t_("pipeline.loaded", faces=i18n.format_int(len(m.faces)), extents=m.extents.round(2)), t) - _check_grid_size(m.extents, config.voxel_pitch / 2, name=path) + _check_grid_size(m.extents, config.voxel_pitch / 2, name=name) # Repair only when there is something to repair. pymeshfix is the most # expensive single step before voxelization (measured ~30 % of @@ -452,6 +508,10 @@ def prepare_figure(path: str, config: Config = _DEFAULT_CFG) -> trimesh.Trimesh: # part survived depended on an unrelated hole anywhere in the file. mf.repair(remove_smallest_components=False) m = trimesh.Trimesh(vertices=mf.points, faces=mf.faces) + if len(m.faces) == 0: + # Nothing that could be closed into a solid: a flat surface, or + # triangles collapsed to a line or a point + raise ValueError(t_("error.not_solid", name=name)) _log(t_("pipeline.repaired", faces=i18n.format_int(len(m.faces))), t) t = _log(t_("pipeline.decimate", target=i18n.format_int(config.decimate_faces))) @@ -510,7 +570,8 @@ def _dilation_iterations(distance: float, config: Config) -> int: def dilate( - mesh: trimesh.Trimesh, distance: float, config: Config = _DEFAULT_CFG + mesh: trimesh.Trimesh, distance: float, config: Config = _DEFAULT_CFG, + name: str | None = None, ) -> trimesh.Trimesh: """Tolerance offset via voxel dilation (more robust than a normal shift). @@ -521,15 +582,20 @@ def dilate( configured one. Flip side: it cannot drop below ~0.75 * voxel_pitch (iters >= 0). The dilation is close to isotropic, so slopes and curves get the same clearance as axis-parallel faces (see _CUBE_STEP_SHARE). + + `name` is what an error calls the figure (its file name). """ pitch = config.voxel_pitch / 2 + iters = _dilation_iterations(distance, config) + # The mesh arrives rotated, so its grid can be far larger than the one + # prepare_figure checked - plus the padding for the dilation itself. + _check_grid_size(mesh.extents + 2 * iters * pitch, pitch, name) + t = _log(t_("pipeline.voxelize_dilation", pitch=pitch)) - vox = cast(Any, mesh.voxelized(pitch=pitch)) + vox = _voxelize_surface(mesh, pitch) vox.fill() _log(t_("pipeline.grid_ready"), t) - iters = _dilation_iterations(distance, config) - if iters > 0: # Pad by 'iters' on every side: both structuring elements grow at most # one voxel per axis and step, so nothing is clipped at the border. @@ -646,7 +712,7 @@ def _to_trimesh(manifold: Manifold) -> trimesh.Trimesh: def _solidify_figure( - fig: trimesh.Trimesh, config: Config, top_z: float, + fig: trimesh.Trimesh, config: Config, top_z: float, name: str | None = None, ) -> trimesh.Trimesh: """Solidifies a figure from its underside up to `top_z` (no undercuts). @@ -657,7 +723,11 @@ def _solidify_figure( Z offset, a short figure lowered by hand): the print had a closed void. """ pitch = config.voxel_pitch - sol_vox = cast(Any, fig.voxelized(pitch=pitch)) + lo, hi = fig.bounds + # The grid reaches from the figure's underside up to top_z, however far a + # manual Z offset has pushed that. + _check_grid_size([hi[0] - lo[0], hi[1] - lo[1], max(hi[2], top_z) - lo[2]], pitch, name) + sol_vox = _voxelize_surface(fig, pitch) matrix = sol_vox.matrix # Extend the grid upwards only - the origin sits at the bottom, so the # transform stays valid (no _padded_transform: the padding is one-sided). @@ -872,9 +942,9 @@ def entry(values, i, default): dtype=float) for i in range(n) ] + names = [entry(file_names, i, None) for i in range(n)] labels = [ - f" '{file_names[i]}'" if file_names is not None and i < len(file_names) - else (f" ({i + 1}/{n})" if multi else "") + f" '{names[i]}'" if names[i] is not None else (f" ({i + 1}/{n})" if multi else "") for i in range(n) ] @@ -893,7 +963,9 @@ def solidify_logged(i: int) -> trimesh.Trimesh: t = _log(t_("pipeline.solidify", label=labels[i])) # The Z offset is applied afterwards; fill far enough that the cavity # still reaches past the top face once it has moved. - solid = _solidify_figure(local[i], config, top_z=CUT_OVERSHOOT_MM - offsets[i][2]) + solid = _solidify_figure( + local[i], config, top_z=CUT_OVERSHOOT_MM - offsets[i][2], name=names[i] + ) _log(t_("pipeline.solidified", faces=i18n.format_int(len(solid.faces))), t) return solid @@ -922,7 +994,8 @@ def solidify_logged(i: int) -> trimesh.Trimesh: ) corners = arrange_footprints( hi[:, :2] - lo[:, :2], config.effective_figure_gap, config.layout_style, - sort_keys, config.box_width, margin=wall, + # A cylinder has no width to fill: box_width is the cuboid's alone + sort_keys, config.box_width if config.box_shape == "box" else None, margin=wall, ) slots = np.zeros((n, 3)) slots[:, :2] = corners - lo[:, :2] @@ -1037,6 +1110,10 @@ def wall_thickness_stats_3d(inlay: trimesh.Trimesh) -> dict: # on a usage error, and a script must be able to tell the two apart. EXIT_CHECK_FAILED: Final[int] = 3 +# Exit code of the CLI when the run itself fails (an unreadable STL, a grid +# beyond MAX_GRID_VOXELS, no memory): one line on stderr, no inlay written. +EXIT_FAILED: Final[int] = 1 + def describe_violation(violation: dict, names: list[str]) -> str: """One readable line per wall-check finding, naming the figure(s).""" @@ -1051,18 +1128,8 @@ def name(i: int | None) -> str: # --- CLI -------------------------------------------------------------------- -if __name__ == "__main__": - import argparse - - # Die Sprache muss vor dem Parserbau feststehen: argparse wertet die - # Hilfetexte schon beim Hinzufuegen der Argumente aus, ein spaeter - # geparstes --lang kaeme dafuer zu spaet. Deshalb wird die Option vorab aus - # sys.argv gefischt; ohne Angabe entscheidet INLAYER_LANG. - _pre = argparse.ArgumentParser(add_help=False) - _pre.add_argument("--lang", choices=sorted(i18n.LANGUAGES)) - _pre_args, _ = _pre.parse_known_args() - i18n.set_language(_pre_args.lang or i18n.language_from_env()) - +def _build_parser() -> argparse.ArgumentParser: + """The CLI's argument parser, its help texts in the current language.""" parser = argparse.ArgumentParser(description=t_("cli.description")) parser.add_argument( "--lang", choices=sorted(i18n.LANGUAGES), default=None, @@ -1102,36 +1169,36 @@ def name(i: int | None) -> str: help=t_("cli.scale", default=Config.stl_unit_to_mm) ) parser.add_argument( - "--box-shape", choices=["box", "cylinder"], default="box", - help=t_("cli.box_shape") + "--box-shape", choices=["box", "cylinder"], default=Config.box_shape, + help=t_("cli.box_shape", default=Config.box_shape) ) parser.add_argument( - "--box-diameter", type=float, default=None, + "--box-diameter", type=float, default=Config.box_diameter, help=t_("cli.box_diameter") ) parser.add_argument( - "--box-width", type=float, default=None, + "--box-width", type=float, default=Config.box_width, help=t_("cli.box_width") ) parser.add_argument( - "--box-depth", type=float, default=None, + "--box-depth", type=float, default=Config.box_depth, help=t_("cli.box_depth") ) parser.add_argument( - "--box-height", type=float, default=None, + "--box-height", type=float, default=Config.box_height, help=t_("cli.box_height") ) parser.add_argument( - "--offset-x", type=float, default=0.0, - help=t_("cli.offset_x") + "--offset-x", type=float, default=Config.offset_x, + help=t_("cli.offset_x", default=Config.offset_x) ) parser.add_argument( - "--offset-y", type=float, default=0.0, - help=t_("cli.offset_y") + "--offset-y", type=float, default=Config.offset_y, + help=t_("cli.offset_y", default=Config.offset_y) ) parser.add_argument( - "--offset-z", type=float, default=0.0, - help=t_("cli.offset_z") + "--offset-z", type=float, default=Config.offset_z, + help=t_("cli.offset_z", default=Config.offset_z) ) parser.add_argument( "--rot-x", type=float, default=0.0, @@ -1146,12 +1213,13 @@ def name(i: int | None) -> str: help=t_("cli.rot_z") ) parser.add_argument( - "--figure-gap", type=float, default=None, + "--figure-gap", type=float, default=Config.figure_gap, help=t_("cli.figure_gap") ) parser.add_argument( - "--layout-style", choices=["compact", "horizontal", "vertical"], default="compact", - help=t_("cli.layout_style") + "--layout-style", choices=["compact", "horizontal", "vertical"], + default=Config.layout_style, + help=t_("cli.layout_style", default=Config.layout_style) ) parser.add_argument( "--finger-recesses", action="store_true", @@ -1163,15 +1231,16 @@ def name(i: int | None) -> str: ) parser.add_argument( "--finger-recess-axis", choices=["x", "y"], default=Config.finger_recess_axis, - help=t_("cli.finger_recess_axis") + help=t_("cli.finger_recess_axis", default=Config.finger_recess_axis) ) parser.add_argument( "--finger-recess-z-offset", type=float, default=Config.finger_recess_z_offset, help=t_("cli.finger_recess_z_offset", default=Config.finger_recess_z_offset) ) parser.add_argument( - "--finger-recess-position", type=float, nargs="+", - default=[Config.finger_recess_position], + # None rather than the default value: main() has to tell "not given" + # from "given as 0.0" to say when the flag has no effect + "--finger-recess-position", type=float, nargs="+", default=None, metavar="P", help=t_("cli.finger_recess_position", default=Config.finger_recess_position) ) @@ -1179,26 +1248,62 @@ def name(i: int | None) -> str: "--parallel", action="store_true", help=t_("cli.parallel", workers=MAX_PARALLEL_WORKERS) ) + return parser + + +def main(argv: list[str] | None = None) -> int: + """Runs the CLI on `argv` (default: sys.argv[1:]) and returns its exit code. - args = parser.parse_args() + Usage errors leave through argparse with SystemExit(2). A function rather + than code under `__main__`, so the tests can run the entry point in-process + and measure what it writes. + """ + # The language has to be known before the parser is built: argparse + # evaluates the help texts while the arguments are added, a --lang parsed + # later would come too late for them. So the option is fished out of argv + # up front; without it INLAYER_LANG decides. + pre = argparse.ArgumentParser(add_help=False) + pre.add_argument("--lang", choices=sorted(i18n.LANGUAGES)) + pre_args, _ = pre.parse_known_args(argv) + i18n.set_language(pre_args.lang or i18n.language_from_env()) + + parser = _build_parser() + args = parser.parse_args(argv) input_paths = args.input # Everything that can be rejected is rejected here, before minutes of # voxel work: argparse then prints a usage error instead of a traceback. + for path in input_paths: + if not os.path.isfile(path): + parser.error(t_("error.input_not_found", path=path)) + # The inlay is written after all the work is done - a typo in the output + # directory used to cost the whole run. + out_dir = os.path.dirname(os.path.abspath(args.output)) + if os.path.isdir(args.output) or not os.path.isdir(out_dir) or not os.access( + args.output if os.path.exists(args.output) else out_dir, os.W_OK + ): + parser.error(t_("cli.error.output_not_writable", path=args.output)) + # The rotations are the only numbers that do not pass through Config and + # its checks: --rot-x nan ran the whole pipeline on a mesh of NaNs and + # wrote a solid block. + for name in ("rot_x", "rot_y", "rot_z"): + if not math.isfinite(getattr(args, name)): + parser.error(t_("config.must_be_finite", name=name, value=getattr(args, name))) # One recess position applies to every figure, otherwise one per input # file - only checked when recesses are on, the flag does nothing else. - recess_positions = list(args.finger_recess_position) + recess_positions = args.finger_recess_position or [Config.finger_recess_position] for value in recess_positions: if not -1.0 <= value <= 1.0: parser.error(t_("config.bad_finger_position", value=value)) if len(recess_positions) == 1: recess_positions = recess_positions * len(input_paths) - elif len(recess_positions) != len(input_paths): - if args.finger_recesses: - parser.error( - t_("cli.error.recess_position_count", - given=len(recess_positions), n=len(input_paths)) - ) + elif args.finger_recesses and len(recess_positions) != len(input_paths): + parser.error( + t_("cli.error.recess_position_count", + given=len(recess_positions), n=len(input_paths)) + ) + if args.finger_recess_position is not None and not args.finger_recesses: + # Whatever was passed - the note used to appear for a wrong count only print(t_("cli.recess_position_ignored")) try: config = Config( @@ -1235,28 +1340,36 @@ def name(i: int | None) -> str: else t_("cli.label.figure")) print(t_("cli.step1", label=label)) - prepared = _parallel_map( - lambda p: prepare_figure(p, config), input_paths, config, - what=t_("pipeline.what.prepare"), - ) - rotated = [ - apply_euler_rotation(m, args.rot_x, args.rot_y, args.rot_z) for m in prepared - ] + try: + prepared = _parallel_map( + lambda p: prepare_figure(p, config), input_paths, config, + what=t_("pipeline.what.prepare"), + ) + rotated = [ + apply_euler_rotation(m, args.rot_x, args.rot_y, args.rot_z) for m in prepared + ] - # Step 2: tolerance offset per figure (optionally in parallel) - print(t_("cli.step2")) - dilated_meshes = _parallel_map( - lambda fig: dilate(fig, config.clearance, config), - rotated, config, what=t_("pipeline.what.dilate"), - ) + # Step 2: tolerance offset per figure (optionally in parallel) + print(t_("cli.step2")) + dilated_meshes = _parallel_map( + lambda item: dilate(item[0], config.clearance, config, name=item[1]), + zip(rotated, input_paths), config, what=t_("pipeline.what.dilate"), + ) - # Step 3: arrange, cut and check (build_inlay does all three) - print(t_("cli.step3")) - inlay, box_w, box_d, box_h = build_inlay( - dilated_meshes, config, file_names=input_paths, - individual_recess_positions=recess_positions if config.enable_finger_recesses else None, - ) - inlay.export(file_obj=args.output, file_type="stl") + # Step 3: arrange, cut and check (build_inlay does all three) + print(t_("cli.step3")) + inlay, box_w, box_d, box_h = build_inlay( + dilated_meshes, config, file_names=input_paths, + individual_recess_positions=recess_positions if config.enable_finger_recesses else None, + ) + inlay.export(file_obj=args.output, file_type="stl") + except (ValueError, RuntimeError, OSError, MemoryError) as exc: + # What the pipeline raises on purpose names the file and the remedy. + # A traceback on top of it buried that one line; anything else is a + # bug and still shows its traceback. + reason = t_("error.out_of_memory") if isinstance(exc, MemoryError) else exc + print(t_("cli.error.failed", error=reason), file=sys.stderr) + return EXIT_FAILED print(t_("cli.saved", path=args.output)) # Step 4: wall check @@ -1274,6 +1387,8 @@ def name(i: int | None) -> str: print(t_("cli.check_failed", path=args.output, code=EXIT_CHECK_FAILED)) print("\n" + t_("cli.total_time", seconds=f"{time.perf_counter() - t_total:.1f}")) - if not stats_3d["passes_min_wall"]: - sys.exit(EXIT_CHECK_FAILED) + return 0 if stats_3d["passes_min_wall"] else EXIT_CHECK_FAILED + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/conftest.py b/tests/conftest.py index e96034b..4ac67af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,9 +2,11 @@ from __future__ import annotations +import struct import sys from pathlib import Path +import numpy as np import pytest import trimesh @@ -32,6 +34,15 @@ def _pin_language(monkeypatch): yield +def binary_stl(triangles) -> bytes: + """A binary STL of exactly these triangles ((n, 3, 3) coordinates), written + by hand: trimesh's exporter would not let NaN or collapsed triangles through.""" + data = bytes(80) + struct.pack(" trimesh.Trimesh: """Watertighter 10x10x10 mm Wuerfel, zentriert um Ursprung.""" @@ -60,6 +71,15 @@ def sphere_stl_path(tmp_path_factory, sphere_mesh) -> str: return str(p) +@pytest.fixture(scope="session") +def bar_stl_path(tmp_path_factory) -> str: + """A 20 x 6 x 4 mm bar: unlike cube and sphere it shows which way it was + turned and along which axis something happened to it.""" + p = tmp_path_factory.mktemp("meshes") / "bar.stl" + trimesh.creation.box(extents=[20.0, 6.0, 4.0]).export(file_obj=str(p), file_type="stl") + return str(p) + + @pytest.fixture(scope="session") def fast_test_config(): """Schnelle Konfiguration: groesserer voxel_pitch reduziert Laufzeit signifikant.""" diff --git a/tests/test_app_helpers.py b/tests/test_app_helpers.py index 88effd1..b913cd6 100644 --- a/tests/test_app_helpers.py +++ b/tests/test_app_helpers.py @@ -10,10 +10,12 @@ import hashlib from typing import cast +import numpy as np import pytest import trimesh import app_helpers +from tests.conftest import binary_stl class TestResolveSelection: @@ -159,7 +161,8 @@ def test_reduces_face_count(self): sphere = trimesh.creation.icosphere(subdivisions=4) # 5120 Faces target = 500 out = app_helpers.decimate_mesh(sphere, target) - assert len(out.faces) < len(sphere.faces) + # The requested count, not just "fewer": removing 1 % passed this test + assert 0.9 * target <= len(out.faces) <= 1.1 * target def test_small_mesh_returned_unchanged(self): box = trimesh.creation.box(extents=[1, 1, 1]) # 12 Faces @@ -211,8 +214,9 @@ def test_large_mesh_is_decimated(self): @pytest.mark.parametrize( "data", - [b"", bytes(84), b"solid x\nendsolid x\n", bytes(range(256)) * 2], - ids=["zero-bytes", "binary-header-only", "ascii-no-facets", "garbage"], + [b"", bytes(84), b"solid x\nendsolid x\n", bytes(range(256)) * 2, + binary_stl(np.full((12, 3, 3), np.nan))], + ids=["zero-bytes", "binary-header-only", "ascii-no-facets", "garbage", "all-nan"], ) def test_empty_or_garbled_upload_names_the_file(self, data): """An empty mesh used to crash the preview on `mesh.bounds[0][0]`.""" diff --git a/tests/test_app_render.py b/tests/test_app_render.py index d925ccc..3967bfe 100644 --- a/tests/test_app_render.py +++ b/tests/test_app_render.py @@ -22,6 +22,7 @@ import i18n import inlayer +from tests.conftest import binary_stl APP = str(Path(__file__).resolve().parent.parent / "app.py") @@ -74,6 +75,70 @@ def test_broken_file_is_skipped_with_a_warning(self): assert any("empty.stl" in w.value for w in at.warning) assert list(_trace_extents(at)) == ["a.stl"] + def test_file_of_nan_coordinates_is_skipped_too(self): + """It loaded as a mesh with faces and no vertices: the preview raised + an IndexError and drew no chart for any upload.""" + nan_stl = binary_stl(np.full((12, 3, 3), np.nan)) + at = _upload(_run("en"), [("a.stl", _stl([10, 10, 10])), ("nan.stl", nan_stl)]) + assert any("nan.stl" in w.value for w in at.warning) + assert list(_trace_extents(at)) == ["a.stl"] + + +class TestFailedRun: + """A run that fails says which upload it failed on.""" + + # Previews fine, but is no solid: the pipeline refuses it + SHEET = binary_stl([[[0, 0, 0], [10, 0, 0], [10, 10, 0]], [[0, 0, 0], [10, 10, 0], [0, 10, 0]]]) + + def test_error_names_the_upload_not_its_temp_file(self): + """The run reads uploads from temporary files and the message named + those: "/tmp/tmpk3x9.stl contains no triangles" - which of two uploads?""" + at = _upload(_run("en"), [("good.stl", _stl([10, 10, 10])), ("sheet.stl", self.SHEET)]) + _widget(at, "slider", "voxel_pitch").set_value(1.0).run() + at.button[0].click().run() + (message,) = [e.value for e in at.error] + assert "sheet.stl has no volume" in message + assert "tmp" not in message + # The message is the whole story - no stack trace below it + assert not at.exception + + def test_a_bug_goes_to_the_server_log_not_to_the_browser(self, monkeypatch, caplog): + """Every failure used to end in st.exception: the full traceback with + absolute paths and source lines, shown to whoever was using the app.""" + def bug(*args, **kwargs): + raise TypeError("detail from /srv/inlayer/app.py") + + monkeypatch.setattr(inlayer, "build_inlay", bug) + at = _upload(_run("en"), [("good.stl", _stl([10, 10, 10]))]) + _widget(at, "slider", "voxel_pitch").set_value(1.0).run() + at.button[0].click().run() + assert not at.exception + assert [e.value for e in at.error] == [i18n.TRANSLATIONS["app.error.unexpected"]["en"]] + assert "detail from /srv/inlayer/app.py" in caplog.text + + +class TestRunWithoutUpload: + """Without an upload the run takes ./figure.stl, like the CLI's default.""" + + def test_error_when_there_is_nothing_to_process(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + at = _run("en") + at.button[0].click().run() + assert not at.exception + (message,) = [e.value for e in at.error] + assert message == i18n.t("app.error.no_input", path="figure.stl") + assert "result" not in at.session_state + + def test_figure_stl_in_the_working_directory_is_used(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / "figure.stl").write_bytes(_stl([10, 10, 10])) + at = _run("en") + _widget(at, "slider", "voxel_pitch").set_value(1.0).run() + at.button[0].click().run() + assert not at.exception and not at.error + assert at.session_state["result"]["file_names"] == ["figure.stl"] + assert (tmp_path / "figure.stl").exists() # only temporary upload files are cleaned up + class TestCacheIdentity: """Process-wide caches are keyed by content, never by name and size.""" @@ -403,6 +468,11 @@ def test_widgets_start_with_the_pipeline_defaults(): assert _widget(at, kind, key).value == getattr(cfg, field), key +def _badges(at: AppTest) -> list[str]: + """The wall-check badges on the page (HTML of the markdown elements).""" + return [m.value for m in at.markdown if '-badge">' in m.value] # not the stylesheet + + class TestResult: @staticmethod def _generate(at: AppTest) -> AppTest: @@ -411,6 +481,38 @@ def _generate(at: AppTest) -> AppTest: assert not at.exception, [str(e.value) for e in at.exception] return at + def test_passing_check_shows_the_green_badge(self): + at = self._generate(_upload(_run("en"), [("cube.stl", _stl([10, 10, 10]))])) + ok = i18n.TRANSLATIONS["app.results.wall_ok"]["en"] + assert _badges(at) == [f'
{ok}
'] + assert not at.warning + + def test_failing_check_shows_the_warning_and_names_the_finding(self): + """The verdict of the wall check is what keeps scrap off the printer. + No test produced a failing one in the app: with the condition of the + badge turned around the whole suite stayed green.""" + at = _upload(_run("en"), [("cube.stl", _stl([10, 10, 10]))]) + _widget(at, "checkbox", "use_custom_box").check().run() + _widget(at, "number_input", "box_width").set_value(14.0).run() # needs about 16 mm + at = self._generate(at) + failed = i18n.TRANSLATIONS["app.results.wall_thin"]["en"] + assert _badges(at) == [f'
{failed}
'] + (warning,) = [w.value for w in at.warning] + assert "minimum wall thickness 2.0 mm" in warning + assert "- cube.stl: side wall" in warning and "(target 2.00 mm)" in warning + assert at.session_state["result"]["stats_3d"]["passes_min_wall"] is False + + def test_manual_cylinder_shows_its_diameter(self): + at = _upload(_run("en"), [("cube.stl", _stl([10, 10, 10]))]) + _widget(at, "selectbox", "_w_box_shape").set_value("cylinder").run() + _widget(at, "checkbox", "use_custom_box").check().run() + _widget(at, "number_input", "box_diameter").set_value(40.0).run() + _widget(at, "number_input", "box_height").set_value(14.0).run() + at = self._generate(at) + res = at.session_state["result"] + assert (res["actual_w"], res["actual_d"], res["actual_h"]) == (40.0, 40.0, 14.0) + assert any("Ø40.0×14.0 mm" in m.value for m in at.markdown) + def test_switching_the_recess_selection_is_not_a_change(self): """Review N6/B23: only showing another figure marked the result stale.""" at = self._generate(_two_figures(finger_enabled=True)) diff --git a/tests/test_build_inlay.py b/tests/test_build_inlay.py index 30d8828..3d8639d 100644 --- a/tests/test_build_inlay.py +++ b/tests/test_build_inlay.py @@ -95,11 +95,10 @@ def test_undersized_box_emits_warning(self, dilated_cube, fast_test_config, caps box_depth=1.0, box_height=1.0, ) - # CSG kann hier fehlschlagen (zu wenig Ueberlapp) – wir fangen das ab. - try: + # A box that lies entirely inside the cavity leaves no material at all: + # the warning is followed by an error, not by an inlay. + with pytest.raises(ValueError, match="empty mesh"): inlayer.build_inlay(dilated_cube, cfg) - except (ValueError, RuntimeError): - pass captured = capsys.readouterr().out assert "WARN" in captured assert "smaller than the minimum" in captured @@ -107,11 +106,10 @@ def test_undersized_box_emits_warning(self, dilated_cube, fast_test_config, caps class TestBuildInlayInvalidGeometry: def test_empty_mesh_raises(self, fast_test_config): - # Leeres Mesh hat keine bounds -> Aufruf scheitert deterministisch. - # trimesh 4.x liefert bounds=None, was beim Unpacken einen TypeError gibt; - # alternative Implementierungen koennten ValueError werfen. Beide ok. + # build_inlay checks for it up front. Accepting a TypeError as well + # let that check be deleted: the same call then failed somewhere below. empty = trimesh.Trimesh() - with pytest.raises((ValueError, TypeError)): + with pytest.raises(ValueError, match="empty"): inlayer.build_inlay(empty, fast_test_config) @@ -124,16 +122,27 @@ def test_offset_moves_the_cavity_inside_a_fixed_box(self, dilated_cube, fast_tes """ plain = inlayer.build_inlay(dilated_cube, fast_test_config) moved = inlayer.build_inlay( - dilated_cube, dataclasses.replace(fast_test_config, offset_x=1.0, offset_z=-1.0) + dilated_cube, + dataclasses.replace(fast_test_config, offset_x=1.0, offset_y=0.5, offset_z=-1.0), ) assert moved[1:] == pytest.approx(plain[1:], abs=1e-6) (before,) = gp.cavities(*plain) (after,) = gp.cavities(*moved) shift = np.array(after.bounding_box()) - np.array(before.bounding_box()) - # x: whole pocket moves; z: the floor moves, the open top stays clipped + # x, y: whole pocket moves; z: the floor moves, the open top stays clipped np.testing.assert_allclose(shift[[0, 3]], 1.0, atol=1e-3) + np.testing.assert_allclose(shift[[1, 4]], 0.5, atol=1e-3) # offset_y had no test at all assert shift[2] == pytest.approx(-1.0, abs=1e-3) + def test_cutter_grid_is_bounded_too(self, dilated_cube, fast_test_config, monkeypatch): + """A Z offset stretches the cutter's grid up to the top face. Far enough + down, that grid was allocated without the check every other one gets.""" + monkeypatch.setattr(inlayer, "MAX_GRID_VOXELS", 100_000) + inlayer.build_inlay(dilated_cube, fast_test_config) + sunk = dataclasses.replace(fast_test_config, offset_z=-1000.0) + with pytest.raises(ValueError, match="cube.stl is too large"): + inlayer.build_inlay(dilated_cube, sunk, file_names=["cube.stl"]) + class TestBuildInlaySignature: """Guards against dead parameters coming back. @@ -186,17 +195,28 @@ def test_placements_put_every_figure_inside_the_box(self, dilated_cube, dilated_ assert lo[2] >= wall assert hi[2] > h # stands out of the box by (1 - depth_fraction) - def test_build_inlay_individual_offsets(self, dilated_cube, dilated_sphere, fast_test_config): - """Prueft, ob individuelle Offsets pro Figur korrekt angewendet werden.""" - offsets = [(0.0, 0.0, 0.0), (30.0, 40.0, 5.0)] - inlay, w, d, h = inlayer.build_inlay( - [dilated_cube, dilated_sphere], - fast_test_config, - individual_offsets=offsets, - ) - assert isinstance(inlay, trimesh.Trimesh) - assert len(inlay.faces) > 0 - assert w > 0 and d > 0 and h > 0 + def test_individual_offsets_move_only_their_figure(self, dilated_cube, dilated_sphere, fast_test_config): + """One offset per figure, each applied to its own cavity. The test + this replaces asserted that the inlay had faces - with offsets that + pushed the second figure clean out of the box.""" + cfg = dataclasses.replace(fast_test_config, layout_style="horizontal") + figs = [dilated_cube, dilated_sphere] + plain = inlayer.build_inlay(figs, cfg) + moved = inlayer.build_inlay(figs, cfg, individual_offsets=[(0.0, 0.0, 0.0), (0.5, 1.0, -1.0)]) + assert moved[1:] == pytest.approx(plain[1:], abs=1e-6) # the box stays put + + def pockets(result): + """Bounds of the two pockets, in the order of the figures.""" + centres = [fig.bounds.mean(axis=0) + place + for fig, place in zip(figs, result[0].metadata["placements"])] + boxes = [np.array(c.bounding_box()) for c in gp.cavities(*result)] + return [min(boxes, key=lambda b: abs((b[0] + b[3]) / 2 - centre[0])) for centre in centres] + + shift = np.array(pockets(moved)) - np.array(pockets(plain)) + np.testing.assert_allclose(shift[0], 0.0, atol=1e-3) + np.testing.assert_allclose(shift[1][[0, 3]], 0.5, atol=1e-3) + np.testing.assert_allclose(shift[1][[1, 4]], 1.0, atol=1e-3) + assert shift[1][2] == pytest.approx(-1.0, abs=1e-3) # the floor; the open top stays clipped class TestBuildInlayCylinder: @@ -240,20 +260,34 @@ def test_manual_diameter_used(self, dilated_cube, fast_test_config): assert w == pytest.approx(manual, abs=1e-6) assert d == pytest.approx(manual, abs=1e-6) + def test_box_width_and_depth_do_not_touch_a_cylinder(self, dilated_cube, dilated_sphere, fast_test_config): + """box_width is documented as "cuboid only", but it set the row width + of the compact layout whatever the shape: these four figures came out + in a single column and a cylinder half as wide again.""" + cylinder = dataclasses.replace(fast_test_config, box_shape="cylinder") + figs = [dilated_cube, dilated_sphere, dilated_cube, dilated_sphere] + plain = inlayer.build_inlay(figs, cylinder) + with_dims = inlayer.build_inlay( + figs, dataclasses.replace(cylinder, box_width=20.0, box_depth=20.0) + ) + assert with_dims[1:] == pytest.approx(plain[1:]) + np.testing.assert_allclose( + with_dims[0].metadata["placements"], plain[0].metadata["placements"] + ) + def test_undersized_diameter_emits_warning(self, dilated_cube, fast_test_config, capsys): cfg = Config( voxel_pitch=fast_test_config.voxel_pitch, box_shape="cylinder", box_diameter=1.0, ) - # CSG kann hier fehlschlagen (zu wenig Ueberlapp) – wir fangen das ab. - try: - inlayer.build_inlay(dilated_cube, cfg) - except (ValueError, RuntimeError): - pass + # The automatic height keeps a floor below the cavity, so a sliver of + # the cylinder remains: a warning and findings, not an error. + inlay, *_ = inlayer.build_inlay(dilated_cube, cfg) captured = capsys.readouterr().out assert "WARN" in captured assert "smaller than the minimum" in captured + assert inlay.metadata["wall_check"]["passes_min_wall"] is False def test_cavity_reduces_volume(self, dilated_cube, fast_test_config): # Das Inlay muss weniger Volumen haben als der massive Zylinder diff --git a/tests/test_config.py b/tests/test_config.py index 1c5fa49..38c0ab7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -84,10 +84,17 @@ def test_negative_stl_unit_rejected(self): ("box_depth", -2.5), ("box_height", 0.0), ("box_height", -3.0), + # These three had no boundary test: without their rule the suite stayed green + ("box_diameter", 0.0), + ("box_diameter", -5.0), + ("figure_gap", 0.0), + ("figure_gap", -1.0), + ("finger_radius", 0.0), + ("finger_radius", -2.0), ], ) def test_box_dimensions_must_be_positive(self, field, value): - with pytest.raises(ValueError, match=field): + with pytest.raises(ValueError, match=f"{field} must be > 0"): Config(**{field: value}) def test_box_dimensions_none_allowed(self): diff --git a/tests/test_dilate.py b/tests/test_dilate.py index 8407e20..454308b 100644 --- a/tests/test_dilate.py +++ b/tests/test_dilate.py @@ -58,6 +58,65 @@ def test_uses_voxel_pitch_from_config(self, prepared_cube): assert isinstance(m, trimesh.Trimesh) assert len(m.faces) > 0 + + +class TestMemory: + """dilate and the cutters voxelize what marching cubes produced, at half + the pitch at that: trimesh's voxelizer split every triangle into 64 for it. + dilate was the memory peak of a whole run - 950 MB for this 30 mm cube, + 3.8 GB for one of 60 mm, more than 10 GB for one of 100 mm.""" + + @staticmethod + def _peak_mb(fn) -> float: + import tracemalloc + + tracemalloc.start() + fn() + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + return peak / 1024**2 + + def test_default_run_stays_near_the_grid(self, tmp_path): + cfg = Config() + path = tmp_path / "cube30.stl" + trimesh.creation.box(extents=[30.0, 30.0, 30.0]).export(file_obj=str(path), file_type="stl") + prepared = inlayer.prepare_figure(str(path), cfg) + dilated = [] + assert self._peak_mb(lambda: dilated.append(inlayer.dilate(prepared, cfg.clearance, cfg))) < 200 + # build_inlay voxelizes the dilated figure once more for its cutter (260 MB) + assert self._peak_mb(lambda: inlayer.build_inlay(dilated[0], cfg)) < 150 + + +class TestGridLimit: + """The grid bound is checked on the mesh dilate voxelizes, not on the upload. + + prepare_figure sees a figure before it is rotated: a thin rod passed with + a few thousand voxels and then filled a grid hundreds of times larger once + it had been turned onto the space diagonal. + """ + + @pytest.fixture + def small_limit(self, monkeypatch): + monkeypatch.setattr(inlayer, "MAX_GRID_VOXELS", 1_000_000) + + def test_rotated_rod_is_refused(self, tmp_path, small_limit): + cfg = Config(voxel_pitch=1.0, decimate_faces=1000) + path = tmp_path / "rod.stl" + trimesh.creation.box(extents=[120.0, 4.0, 4.0]).export(file_obj=str(path), file_type="stl") + rod = inlayer.prepare_figure(str(path), cfg) # about 1e4 voxels: accepted + assert inlayer.dilate(rod, cfg.clearance, cfg, name="rod.stl").is_watertight + + diagonal = inlayer.apply_euler_rotation(rod, 0.0, 35.264, 45.0) + with pytest.raises(ValueError, match="rod.stl is too large"): + inlayer.dilate(diagonal, cfg.clearance, cfg, name="rod.stl") + + def test_padding_for_the_clearance_counts(self, prepared_cube, fast_test_config, small_limit): + """The dilation pads the grid by its own reach on every side.""" + inlayer.dilate(prepared_cube, 0.5, fast_test_config) + with pytest.raises(ValueError, match="The figure is too large"): + inlayer.dilate(prepared_cube, 20.0, fast_test_config) + + class TestIsotropicClearance: """Slopes get the same clearance as axis-parallel faces (review B27). diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 6f03937..8741ac0 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -206,9 +206,12 @@ def test_placeholders_are_filled(self): assert "3" in out and "{n}" not in out def test_missing_placeholder_does_not_raise(self): - """Ein vergessenes kwarg darf die Pipeline nicht abbrechen.""" - out = i18n.t("app.upload.multi_info") - assert isinstance(out, str) + """A forgotten kwarg must not abort a running computation: the text + comes back unformatted. (Called without any kwarg, as this test used + to, t() does not format at all and never reaches that fallback.)""" + i18n.set_language("en") + out = i18n.t("check.side", name="a.stl") # measured and target are missing + assert out == i18n.TRANSLATIONS["check.side"]["en"] def test_surplus_kwargs_are_ignored(self): out = i18n.t("app.language.label", unbenutzt=1) diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 1ed71ac..53fc802 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -2,19 +2,36 @@ from __future__ import annotations +import dataclasses +import os import re import subprocess import sys from pathlib import Path +import numpy as np import pytest import trimesh +import i18n import inlayer REPO_ROOT = Path(__file__).resolve().parent.parent +# Coarse and quick: enough for everything that is not about the resolution +FAST = ["-vp", "1.0", "--decimate-faces", "1000"] + + +def run_main(capsys, *argv) -> tuple[int, str, str]: + """Runs the CLI in-process: (exit code, stdout, stderr).""" + try: + code = inlayer.main([str(arg) for arg in argv]) + except SystemExit as exc: # argparse: usage error or --help + code = exc.code + captured = capsys.readouterr() + return code, captured.out, captured.err + @pytest.mark.slow class TestFullPipeline: @@ -25,7 +42,7 @@ def test_cube_pipeline_end_to_end(self, cube_stl_path, fast_test_config): inlay, w, d, h = inlayer.build_inlay(fig_ot, fast_test_config) stats = inlayer.wall_thickness_stats_3d(inlay) - assert inlay.is_watertight or len(inlay.faces) > 0 + assert inlay.is_watertight assert stats["passes_min_wall"] is True # Box-Nullpunkt-Ausrichtung verifizieren assert inlay.bounds[0][0] == pytest.approx(0.0, abs=0.01) @@ -196,8 +213,9 @@ def test_cli_recess_position_count_mismatch_fails(self, cube_stl_path, sphere_st text=True, timeout=120, ) - assert result.returncode != 0 - assert "--finger-recess-position" in result.stderr + # Not just the flag's name: every usage error prints that, in the usage line + assert result.returncode == 2 + assert "expects 1 value or one per input file (got 3 for 2 files)" in result.stderr def test_cli_recess_position_out_of_range_fails(self, cube_stl_path, sphere_stl_path, tmp_path): """Every value is checked while parsing - the second one used to fail @@ -294,10 +312,23 @@ def test_cli_missing_input_fails(self, tmp_path): text=True, timeout=30, ) - assert result.returncode != 0 - # Fehlertext kommt entweder ueber stderr oder stdout (traceback) - combined = result.stdout + result.stderr - assert "nicht gefunden" in combined or "FileNotFoundError" in combined + # This test used to pass *because* of the traceback: the only thing it + # found in the output was the exception's class name. + assert result.returncode == 2 + assert "Input file not found" in result.stderr + assert "Traceback" not in result.stderr + + def test_cli_unreadable_input_fails_without_a_traceback(self, tmp_path): + broken = tmp_path / "broken.stl" + broken.write_bytes(np.random.default_rng(0).bytes(500)) + result = subprocess.run( + [sys.executable, str(REPO_ROOT / "inlayer.py"), "-i", str(broken), + "-o", str(tmp_path / "inlay.stl")], + capture_output=True, text=True, timeout=30, + ) + assert result.returncode == inlayer.EXIT_FAILED + assert "broken.stl contains no triangles" in result.stderr + assert "Traceback" not in result.stderr def test_cli_invalid_param_fails(self, cube_stl_path, tmp_path): """An invalid value is a usage error naming the field, not a traceback.""" @@ -333,6 +364,282 @@ def test_cli_out_of_range_value_is_a_usage_error(self, cube_stl_path, tmp_path, assert "Traceback" not in result.stderr +def _signature(mesh: trimesh.Trimesh) -> np.ndarray: + """Extents, volume and centre of mass of an inlay: whatever moves, resizes + or reshapes a cavity or the box shows in at least one of them.""" + return np.concatenate([mesh.extents, [mesh.volume], mesh.center_mass]) + + +def _library_inlay(paths, rotation=(0.0, 0.0, 0.0), positions=None, **config) -> trimesh.Trimesh: + """The inlay the library builds for these files and this Config (at FAST's resolution).""" + cfg = inlayer.Config(**{"voxel_pitch": 1.0, "decimate_faces": 1000, **config}) + figures = [ + inlayer.apply_euler_rotation(inlayer.prepare_figure(path, cfg), *rotation) for path in paths + ] + dilated = [inlayer.dilate(fig, cfg.clearance, cfg) for fig in figures] + inlay, *_ = inlayer.build_inlay( + dilated, cfg, file_names=list(paths), individual_recess_positions=positions + ) + return inlay + + +# (flags, the Config they stand for, extras outside Config, which inputs) +ONE, TWO, ROUND = ("bar",), ("bar", "cube"), ("sphere",) +RECESSES = {"enable_finger_recesses": True} +WIRING = [ + (["-c", "2.0"], {"clearance": 2.0}, {}, ONE), + (["--clearance", "2.0"], {"clearance": 2.0}, {}, ONE), + (["-w", "3.0"], {"wall_thickness": 3.0}, {}, ONE), + (["--wall-thickness", "3.0"], {"wall_thickness": 3.0}, {}, ONE), + (["-df", "0.5"], {"depth_fraction": 0.5}, {}, ONE), + (["--depth-fraction", "0.5"], {"depth_fraction": 0.5}, {}, ONE), + (["-vp", "0.5"], {"voxel_pitch": 0.5}, {}, ONE), + (["--voxel-pitch", "0.5"], {"voxel_pitch": 0.5}, {}, ONE), + (["--decimate-faces", "60"], {"decimate_faces": 60}, {}, ROUND), + (["--scale", "2.0"], {"stl_unit_to_mm": 2.0}, {}, ONE), + (["--box-shape", "cylinder"], {"box_shape": "cylinder"}, {}, ONE), + (["--box-shape", "cylinder", "--box-diameter", "60"], + {"box_shape": "cylinder", "box_diameter": 60.0}, {}, ONE), + (["--box-width", "40"], {"box_width": 40.0}, {}, ONE), + (["--box-depth", "30"], {"box_depth": 30.0}, {}, ONE), + (["--box-height", "12"], {"box_height": 12.0}, {}, ONE), + (["--offset-x", "1.5"], {"offset_x": 1.5}, {}, ONE), + (["--offset-y", "1.5"], {"offset_y": 1.5}, {}, ONE), + (["--offset-z", "-1.0"], {"offset_z": -1.0}, {}, ONE), + (["--rot-x", "90"], {}, {"rotation": (90.0, 0.0, 0.0)}, ONE), + (["--rot-y", "90"], {}, {"rotation": (0.0, 90.0, 0.0)}, ONE), + (["--rot-z", "90"], {}, {"rotation": (0.0, 0.0, 90.0)}, ONE), + (["--figure-gap", "6"], {"figure_gap": 6.0}, {}, TWO), + (["--layout-style", "horizontal"], {"layout_style": "horizontal"}, {}, TWO), + (["--layout-style", "vertical"], {"layout_style": "vertical"}, {}, TWO), + (["--finger-recesses"], RECESSES, {}, ONE), + (["--finger-recesses", "--finger-radius", "4"], {**RECESSES, "finger_radius": 4.0}, {}, ONE), + (["--finger-recesses", "--finger-recess-axis", "y"], + {**RECESSES, "finger_recess_axis": "y"}, {}, ONE), + (["--finger-recesses", "--finger-recess-z-offset", "3"], + {**RECESSES, "finger_recess_z_offset": 3.0}, {}, ONE), + (["--finger-recesses", "--finger-radius", "3", "--finger-recess-position", "0.8"], + {**RECESSES, "finger_radius": 3.0}, {"positions": [0.8]}, ONE), + (["--finger-recesses", "--finger-radius", "3", "--finger-recess-position", "0.8", "-0.8"], + {**RECESSES, "finger_radius": 3.0}, {"positions": [0.8, -0.8]}, TWO), +] + + +class TestCLIWiring: + """Every flag reaches the pipeline, and the file written is that inlay. + + The CLI tests asserted an exit code and a non-empty file. A CLI that + wrote the figure instead of the inlay, ignored its rotations or dropped + 15 of the 21 arguments on the way into Config passed all of them. + """ + + @pytest.fixture + def inputs(self, bar_stl_path, cube_stl_path, sphere_stl_path): + return {"bar": bar_stl_path, "cube": cube_stl_path, "sphere": sphere_stl_path} + + @staticmethod + def _written(capsys, tmp_path, paths, flags) -> trimesh.Trimesh: + out = tmp_path / "inlay.stl" + code, _, err = run_main(capsys, "-i", *paths, "-o", out, *FAST, *flags) + assert code in (0, inlayer.EXIT_CHECK_FAILED), err + return trimesh.load(str(out), force="mesh") + + @pytest.mark.parametrize("which", [ONE, TWO, ROUND], ids=["one", "two", "sphere"]) + def test_the_file_is_the_inlay_of_the_defaults(self, inputs, tmp_path, capsys, which): + paths = [inputs[name] for name in which] + written = self._written(capsys, tmp_path, paths, []) + assert written.is_watertight + np.testing.assert_allclose( + _signature(written), _signature(_library_inlay(paths)), rtol=1e-4, atol=2e-3 + ) + + @pytest.mark.parametrize( + "flags,config,extras,which", WIRING, ids=[" ".join(case[0]) for case in WIRING] + ) + def test_flag_gives_the_inlay_of_its_config(self, inputs, tmp_path, capsys, flags, config, extras, which): + paths = [inputs[name] for name in which] + expected = _signature(_library_inlay(paths, **extras, **config)) + # The case has to bite: with the flag ignored the CLI writes this instead + untouched = _signature(_library_inlay(paths)) + assert not np.allclose(expected, untouched, rtol=1e-4, atol=2e-3) + written = self._written(capsys, tmp_path, paths, flags) + np.testing.assert_allclose(_signature(written), expected, rtol=1e-4, atol=2e-3) + + def test_parallel_builds_the_same_inlay(self, inputs, tmp_path, capsys): + paths = [inputs["bar"], inputs["cube"]] + out = tmp_path / "inlay.stl" + code, stdout, _ = run_main(capsys, "-i", *paths, "-o", out, *FAST, "--parallel") + assert code == 0 + assert "Parallelising" in stdout # the flag reached Config.enable_parallel + np.testing.assert_allclose( + _signature(trimesh.load(str(out), force="mesh")), _signature(_library_inlay(paths)), + rtol=1e-4, atol=2e-3, + ) + + +class TestCLILanguage: + """--lang and INLAYER_LANG are read before the parser exists (its help + texts are translated while it is built). No test ran the CLI in anything + but the pinned English: deleting that line changed nothing in the suite.""" + + def test_flag_translates_the_help(self, capsys): + code, out, _ = run_main(capsys, "--lang", "de", "--help") + assert code == 0 + assert i18n.TRANSLATIONS["cli.description"]["de"] in " ".join(out.split()) + + def test_flag_translates_the_run(self, cube_stl_path, tmp_path, capsys): + code, out, _ = run_main(capsys, "--lang", "de", "-i", cube_stl_path, "-o", tmp_path / "i.stl", *FAST) + assert code == 0 + assert i18n.TRANSLATIONS["cli.step2"]["de"] in out + + def test_environment_sets_the_language(self, cube_stl_path, tmp_path, capsys, monkeypatch): + monkeypatch.setenv(i18n.ENV_VAR, "de_CH.UTF-8") + code, out, _ = run_main(capsys, "-i", cube_stl_path, "-o", tmp_path / "i.stl", *FAST) + assert code == 0 + assert i18n.TRANSLATIONS["cli.step2"]["de"] in out + + def test_flag_beats_the_environment(self, capsys, monkeypatch): + monkeypatch.setenv(i18n.ENV_VAR, "de") + code, out, _ = run_main(capsys, "--lang", "en", "--help") + assert code == 0 + assert i18n.TRANSLATIONS["cli.description"]["en"] in " ".join(out.split()) + + +class TestCLIDefaults: + """The parser starts from Config's defaults, like the web app's widgets. + + Five flags carried literals of their own ("box", 0.0, "compact") and the + help texts a third copy of them; nothing compared either with Config. + """ + + ARG_OF = {"stl_unit_to_mm": "scale", "enable_finger_recesses": "finger_recesses", + "enable_parallel": "parallel"} + + def test_parser_defaults_are_configs_defaults(self): + args = inlayer._build_parser().parse_args([]) + cfg = inlayer.Config() + for field in dataclasses.fields(cfg): + if field.name == "finger_recess_position": + continue # None on the command line means "not given"; main() falls back to Config + assert getattr(args, self.ARG_OF.get(field.name, field.name)) == getattr(cfg, field.name), field.name + + def test_help_quotes_configs_defaults(self, capsys): + code, out, _ = run_main(capsys, "--help") + cfg = inlayer.Config() + text = " ".join(out.split()) # argparse wraps the lines + assert code == 0 + for expected in (f"(default: '{cfg.box_shape}')", f"(default: '{cfg.layout_style}')", + f"(default: '{cfg.finger_recess_axis}')", f"X offset of the figure(s) in mm (default: {cfg.offset_x})"): + assert expected in text, expected + + +class TestCLIRecessPositionNote: + """Positions without --finger-recesses do nothing, and the CLI says so. + + README promised that note for every such call; it was printed only when + the number of values did not fit the number of files. + """ + + @pytest.mark.parametrize("positions", [["0.5"], ["0.1", "0.2", "0.3"]]) + def test_note_whenever_positions_are_given_without_recesses(self, cube_stl_path, tmp_path, capsys, positions): + code, out, _ = run_main( + capsys, "-i", cube_stl_path, "-o", tmp_path / "inlay.stl", *FAST, + "--finger-recess-position", *positions, + ) + assert code == 0 + assert "--finger-recess-position is ignored without --finger-recesses" in out + + @pytest.mark.parametrize("extra", [[], ["--finger-recesses", "--finger-recess-position", "0.5"]]) + def test_no_note_otherwise(self, cube_stl_path, tmp_path, capsys, extra): + code, out, _ = run_main(capsys, "-i", cube_stl_path, "-o", tmp_path / "inlay.stl", *FAST, *extra) + assert code == 0 + assert "ignored" not in out + + +class TestCLIErrors: + """Whatever stops a run is one line on stderr and an exit code. + + Only the Config was built inside a try: a missing or unreadable input, a + grid beyond the limit and an output directory that does not exist all + ended in a traceback with exit code 1 - the last one after the whole + computation. + """ + + def test_missing_default_input_is_a_usage_error(self, tmp_path, monkeypatch, capsys): + """`python inlayer.py` without -i, in a directory without figure.stl.""" + monkeypatch.chdir(tmp_path) + code, out, err = run_main(capsys, "-o", tmp_path / "inlay.stl") + assert code == 2 + assert "Input file not found: figure.stl" in err + assert "[1/4]" not in out + + def test_every_input_is_checked_before_the_first_is_prepared(self, cube_stl_path, tmp_path, capsys): + code, out, err = run_main( + capsys, "-i", cube_stl_path, tmp_path / "missing.stl", "-o", tmp_path / "inlay.stl" + ) + assert code == 2 + assert "missing.stl" in err + assert "[1/4]" not in out + + @pytest.mark.parametrize("target", [ + "no_such_dir/inlay.stl", ".", + # root may write to a read-only file - the test stage of the Dockerfile runs as root + pytest.param("read_only.stl", marks=pytest.mark.skipif( + getattr(os, "geteuid", lambda: 1)() == 0, reason="root ignores file permissions")), + ]) + def test_output_path_is_checked_before_the_work(self, cube_stl_path, tmp_path, capsys, target): + (tmp_path / "read_only.stl").touch(mode=0o444) + code, out, err = run_main(capsys, "-i", cube_stl_path, "-o", tmp_path / target, *FAST) + assert code == 2 + assert "Cannot write the output file" in err + assert "[1/4]" not in out + + @pytest.mark.parametrize("flag", ["--rot-x", "--rot-y", "--rot-z"]) + @pytest.mark.parametrize("value", ["nan", "inf"]) + def test_rotation_must_be_finite(self, cube_stl_path, tmp_path, capsys, flag, value): + """The rotations bypass Config and with it the check every other number + gets: `--rot-x nan` ran to the end, wrote a solid block and exited 3.""" + out_path = tmp_path / "inlay.stl" + code, out, err = run_main(capsys, "-i", cube_stl_path, "-o", out_path, flag, value, *FAST) + assert code == 2 + assert f"{flag[2:].replace('-', '_')} must be a finite number" in err + assert "[1/4]" not in out and not out_path.exists() + + def test_unreadable_stl_exits_with_1(self, tmp_path, capsys): + broken = tmp_path / "broken.stl" + broken.write_bytes(np.random.default_rng(0).bytes(500)) + out_path = tmp_path / "inlay.stl" + code, _, err = run_main(capsys, "-i", broken, "-o", out_path) + assert code == inlayer.EXIT_FAILED == 1 + assert "broken.stl contains no triangles" in err + assert not out_path.exists() + + def test_oversized_grid_exits_with_1_and_says_what_to_do(self, cube_stl_path, tmp_path, capsys): + code, _, err = run_main( + capsys, "-i", cube_stl_path, "-o", tmp_path / "inlay.stl", "--scale", "1000", "-vp", "0.2" + ) + assert code == inlayer.EXIT_FAILED + assert "too large for this resolution" in err and "Choose a larger voxel_pitch" in err + + def test_out_of_memory_is_said_in_plain_words(self, cube_stl_path, tmp_path, capsys, monkeypatch): + def no_memory(*args, **kwargs): + raise MemoryError("Unable to allocate 4.33 GiB for an array with shape (48384000, 4, 3)") + + monkeypatch.setattr(inlayer, "dilate", no_memory) + code, _, err = run_main(capsys, "-i", cube_stl_path, "-o", tmp_path / "inlay.stl", *FAST) + assert code == inlayer.EXIT_FAILED + assert "Not enough memory" in err and "48384000" not in err + + def test_a_bug_still_shows_its_traceback(self, cube_stl_path, tmp_path, monkeypatch): + """Only what the pipeline raises on purpose is turned into a message.""" + def broken(*args, **kwargs): + raise TypeError("a programming error") + + monkeypatch.setattr(inlayer, "dilate", broken) + with pytest.raises(TypeError): + inlayer.main(["-i", cube_stl_path, "-o", str(tmp_path / "inlay.stl"), *FAST]) + + @pytest.mark.slow class TestMultiFigurePipeline: """End-to-End-Tests fuer die Multi-Figur-Pipeline.""" diff --git a/tests/test_prepare_figure.py b/tests/test_prepare_figure.py index 6987991..d417144 100644 --- a/tests/test_prepare_figure.py +++ b/tests/test_prepare_figure.py @@ -8,6 +8,7 @@ import inlayer from inlayer import Config +from tests.conftest import binary_stl class TestPrepareFigureErrors: @@ -33,6 +34,39 @@ def test_empty_or_garbled_file_names_the_file(self, tmp_path, content): with pytest.raises(ValueError, match="broken.stl.*no triangles"): inlayer.prepare_figure(str(path)) + @pytest.mark.parametrize("value", [np.nan, np.inf]) + def test_file_of_non_finite_coordinates_names_the_file(self, tmp_path, value): + """trimesh drops every vertex of such a file and keeps its faces: the + emptiness check passed and the first look at the bounds raised + `IndexError: index 0 is out of bounds for axis 0 with size 0`.""" + path = tmp_path / "broken.stl" + path.write_bytes(binary_stl(np.full((12, 3, 3), value))) + with pytest.raises(ValueError, match="broken.stl.*no triangles"): + inlayer.prepare_figure(str(path)) + + @pytest.mark.parametrize( + "triangles", + [[[[0, 0, 0], [10, 0, 0], [10, 10, 0]], [[0, 0, 0], [10, 10, 0], [0, 10, 0]]], + np.ones((12, 3, 3)), + [[[k, 0, 0], [k + 1, 0, 0], [k + 2, 0, 0]] for k in range(12)]], + ids=["flat-square", "collapsed-to-a-point", "collinear"], + ) + def test_file_without_volume_names_the_file(self, tmp_path, fast_test_config, triangles): + """Triangles, but nothing the repair could close into a solid: it + returned an empty mesh and the voxelizer failed on it with `zero-size + array to reduction operation minimum which has no identity`.""" + path = tmp_path / "sheet.stl" + path.write_bytes(binary_stl(triangles)) + with pytest.raises(ValueError, match="sheet.stl has no volume"): + inlayer.prepare_figure(str(path), fast_test_config) + + def test_errors_use_the_given_name(self, tmp_path): + """The web app passes the upload's name; the path is a temporary file.""" + path = tmp_path / "tmpk3x9.stl" + path.write_bytes(b"") + with pytest.raises(ValueError, match="^my figure.stl contains no triangles"): + inlayer.prepare_figure(str(path), name="my figure.stl") + def test_oversized_grid_is_refused_up_front(self, cube_stl_path): """A small file scaled up must fail with a message, not exhaust memory.""" cfg = Config(stl_unit_to_mm=1000.0, voxel_pitch=0.2) @@ -80,10 +114,11 @@ def test_holed_large_part_survives_next_to_a_small_intact_one(self, tmp_path, fa class TestVoxelizeSurface: - """_voxelize_surface replaces trimesh's subdivision voxelizer for raw input. + """_voxelize_surface replaces trimesh's subdivision voxelizer in every step. trimesh quarters each triangle until all edges are below pitch / 2, so a - long, thin triangle costs (length / pitch)² however little area it has. + long, thin triangle costs (length / pitch)² however little area it has - + and a fine mesh is still split into 16 or 64 triangles each. """ @staticmethod @@ -130,6 +165,33 @@ def test_long_thin_triangles_stay_cheap(self): assert peak < 200 * 1024**2 assert vox.matrix.shape[2] >= 120 / 0.4 + def test_batching_does_not_change_the_grid(self, monkeypatch): + """The samples are marked batch by batch; where a batch ends - between + two triangles, two rows or in the middle of a segment - must not show.""" + mesh = trimesh.creation.icosphere(subdivisions=3, radius=10.0) + mesh.apply_transform(trimesh.transformations.euler_matrix(0.3, 0.5, 0.7)) + whole = inlayer._voxelize_surface(mesh, 0.5) + monkeypatch.setattr(inlayer, "_SURFACE_SAMPLE_BATCH", 7) + in_pieces = inlayer._voxelize_surface(mesh, 0.5) + np.testing.assert_array_equal(whole.matrix, in_pieces.matrix) + np.testing.assert_array_equal(whole.transform, in_pieces.transform) + + def test_memory_follows_the_grid_not_the_surface_area(self): + """Ten plates of 100 x 100 mm stacked within 20 mm: a grid of 1.5 MB + under 2000 cm² of surface. All samples at once took 590 MB - a 12 kB + STL far below the grid limit.""" + import tracemalloc + + plates = [trimesh.creation.box(extents=[100.0, 100.0, 0.05]) for _ in range(10)] + for k, plate in enumerate(plates): + plate.apply_translation([0.0, 0.0, 2.0 * k]) + tracemalloc.start() + vox = inlayer._voxelize_surface(trimesh.util.concatenate(plates), 0.5) + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + assert peak < 80 * 1024**2 + assert vox.matrix.shape == (201, 201, 37) + def test_low_poly_bar_no_longer_aborts(self, tmp_path): """A 12-triangle 250 mm bar ended in 'max_iter exceeded' (review B16).""" bar = trimesh.creation.box(extents=[250.0, 10.0, 10.0]) @@ -238,6 +300,31 @@ def test_decimation_when_above_target(self, sphere_stl_path): assert isinstance(m, trimesh.Trimesh) assert m.is_watertight + def test_the_decimated_mesh_is_what_gets_voxelized(self, sphere_stl_path, monkeypatch): + """The face count after the voxel step says nothing about the + decimation: with that step removed every test passed. The spy hands + back a mesh of half the size, which shows in the result.""" + calls = [] + decimate = inlayer.decimate_mesh + + def halved(mesh, face_count): + calls.append((len(mesh.faces), face_count)) + small = decimate(mesh, face_count) + small.apply_scale(0.5) + return small + + monkeypatch.setattr(inlayer, "decimate_mesh", halved) + m = inlayer.prepare_figure(sphere_stl_path, Config(voxel_pitch=1.0, decimate_faces=50)) + assert calls == [(320, 50)] + assert np.all(m.extents < 8.0) # the 10 mm sphere, halved, plus voxel inflation + + def test_below_the_target_nothing_is_decimated(self, sphere_stl_path, monkeypatch): + def never(mesh, face_count): + raise AssertionError("320 faces are below the target of 1000") + + monkeypatch.setattr(inlayer, "decimate_mesh", never) + inlayer.prepare_figure(sphere_stl_path, Config(voxel_pitch=1.0, decimate_faces=1000)) + class TestPrepareFigureRepairSkip: """prepare_figure ueberspringt pymeshfix bei bereits intakten Meshes. diff --git a/tests/test_wall_thickness.py b/tests/test_wall_thickness.py index 2bc338d..c895812 100644 --- a/tests/test_wall_thickness.py +++ b/tests/test_wall_thickness.py @@ -93,6 +93,20 @@ def test_sealed(self): check = inlayer._wall_check(box, [cutter], box - cutter, CFG) assert [(v["kind"], v["figure"]) for v in check["violations"]] == [("sealed", 0)] + @pytest.mark.parametrize("short_by,passes", [(0.04, True), (0.06, False)]) + def test_tolerance_is_five_hundredths(self, short_by, passes): + """WALL_TOLERANCE_MM absorbs rounding, nothing more. Every other case + stays at least half a millimetre away from the threshold: a check that + let 1.6 mm pass for 2.0 was indistinguishable to the whole suite.""" + wall = CFG.wall_thickness - short_by + box = Manifold.cube((20.0, 20.0, 10.0)).translate((-10.0, -10.0, -10.0)) + # A pocket open at the top, `wall` away from one side and from the bottom + cutter = Manifold.cube((10.0, 10.0, 12.0)).translate((-10.0 + wall, -5.0, -10.0 + wall)) + check = inlayer._wall_check(box, [cutter], box - cutter, CFG) + assert check["min_wall_mm"] == pytest.approx(wall, abs=1e-4) + assert check["passes_min_wall"] is passes + assert {v["kind"] for v in check["violations"]} == (set() if passes else {"side", "floor"}) + def test_recess_breakthrough_names_its_figure(self, dilated_cube): """Recesses are part of their figure's cutter; the old check only reported a blanket "0.00 mm" and no figure.""" @@ -135,6 +149,35 @@ def test_names_both_figures_and_values(self, lang): line = inlayer.describe_violation(self.FINDING, ["a.stl", "b.stl"]) assert "b.stl" in line and "a.stl" in line and "0.80" in line and "2.00" in line + KINDS = ("side", "floor", "inner", "merged", "sealed", "no_cavity") + + def test_these_are_the_kinds_the_check_emits(self): + """A new kind needs its `check.*` text: an unknown key comes back as + the key itself and both UIs would print "check.overhang".""" + import inspect + import re + + emitted = set(re.findall(r'flag\("(\w+)"', inspect.getsource(inlayer._wall_check))) + assert emitted == set(self.KINDS) + assert {f"check.{kind}" for kind in emitted} <= set(i18n.TRANSLATIONS) + + @pytest.mark.parametrize("lang", sorted(i18n.LANGUAGES)) + @pytest.mark.parametrize("kind", KINDS) + def test_every_kind_is_a_filled_in_sentence(self, kind, lang): + """t() returns a template unformatted when a placeholder is missing: + with `{name}` renamed in `check.side` the CLI printed + "{figure}: side wall {measured} mm" and every test passed. Only + `inner` was ever rendered.""" + i18n.set_language(lang) + line = inlayer.describe_violation({**self.FINDING, "kind": kind}, ["a.stl", "b.stl"]) + assert line.startswith("b.stl: ") and "{" not in line + if kind in ("inner", "merged"): + assert "a.stl" in line + if kind in ("side", "floor", "inner"): + assert line.index("0.80") < line.index("2.00") # measured, then target + if kind == "merged": + assert "2.00" in line + def test_unknown_figure_does_not_raise(self): line = inlayer.describe_violation({**self.FINDING, "kind": "sealed", "figure": None}, []) assert "#?" in line