Metadata-Version: 2.4
Name: kneemesh
Version: 0.1.1
Summary: B-rep geometry and meshes of knee structures from segmented images
Author: Scott Sibole
Author-email: Scott Sibole <scott.sibole@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Requires-Dist: gmsh>=4.15.2
Requires-Dist: loguru>=0.7.3
Requires-Dist: numpy>=2.5.4
Requires-Dist: pydantic>=2.14.3
Requires-Dist: scipy>=1.18.1
Requires-Dist: simpleitk>=2.5.7
Requires-Dist: vtk>=9.7.2
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# kneemesh

[![PyPI](https://img.shields.io/pypi/v/kneemesh)](https://pypi.org/project/kneemesh/)
[![Coverage](https://img.shields.io/endpoint?url=https://comporthobiomech.github.io/kneemesh/coverage.json)](https://github.com/CompOrthoBiomech/kneemesh/actions/workflows/release.yml)
[![Docs](https://img.shields.io/badge/docs-gh--pages-blue)](https://comporthobiomech.github.io/kneemesh/)

Generate B-rep geometry of knee structures from a segmentation LabelMap, and mesh it with high-quality elements. The meniscus is supported now; lateral and medial tibial, femoral, and patellar cartilage and the bones are planned. For the meniscus:

- **B-rep from segmentation labels.** Reads a LabelMap (`.nii`, `.nrrd`, or `.mhd`), fits a centerline through the labeled meniscus, slices its smoothed surface into cross-section contours, and lofts them into a solid B-spline B-rep (`.brep`) with planar or smooth end caps.
- **Tetrahedral meshing** with linear 4-node or quadratic 10-node tetrahedra.
- **Hexahedral meshing** with block-structured 8-node, 20-node, or 27-node hexahedra. The solid is split into a butterfly (O-grid) of five blocks, so elements stay well shaped at the meniscus's sharp corners. Optional node optimization (`--optimize true`) then raises the scaled Jacobians further, roughly doubling the worst element's on the real test menisci.

Meshes are written in any format gmsh infers from the file extension, such as `.msh`, `.vtk`, or `.inp`.

**Documentation:** <https://comporthobiomech.github.io/kneemesh/>

## Installation

```sh
pip install kneemesh
```

## Usage

The command-line pipelines take a JSON configuration, `--option value` overrides, or both. `kneemesh-brep` builds every structure named in the segmentation, and `kneemesh-mesh` every structure named in `--breps`; the names, such as `lateral_meniscus`, pick each structure's pipeline. Outputs are written as `<structure><output>`, where `--output` is a suffix such as `_hex8.msh`. Run `kneemesh-brep --help` to list every option and its default.

```sh
kneemesh-assemble --label-maps '{"lateral_meniscus": "lateral.nii", "medial_meniscus": "medial.nii"}' --output knee.nrrd
kneemesh-brep --label-map knee.nrrd --labels "$(cat knee_labels.json)"
kneemesh-brep --label-map meniscus.nii --labels '{"medial_meniscus": 1}' --meniscus.cap planar
kneemesh-mesh --breps '{"medial_meniscus": "medial_meniscus.brep"}' --element-type tet10 --meniscus.max-size 1.0
```

A multilabel LabelMap takes `--labels` naming its values, such as `--labels '{"lateral_meniscus": 2, "medial_meniscus": 3}'`; values it does not name are ignored. `kneemesh-assemble` resamples separate single-label LabelMaps onto one grid and writes the multilabel LabelMap with its labels in `<stem>_labels.json`. `kneemesh-brep` can also take `--label-maps` directly and assemble them within the run. Example configurations are in `examples/`. See the documentation's usage page.

Each run saves the configuration it used, so it can be repeated with `--config`. Without `--config` it is saved as `settings<timestamp>.json` in the working directory. With `--config` and overrides, the changed configuration is saved beside the original as `<name>_modified.json`.

### Element types

| Elements | B-rep options | Mesh options |
|---|---|---|
| 4-node tetrahedra | | `--element-type tet4` (the default) |
| 10-node tetrahedra | | `--element-type tet10` |
| 8-node hexahedra | `--meniscus.hex-blocks true --meniscus.points-per-slice 10` | `--element-type hex8` |
| 20-node hexahedra | `--meniscus.hex-blocks true --meniscus.points-per-slice 10` | `--element-type hex20` |
| 27-node hexahedra | `--meniscus.hex-blocks true --meniscus.points-per-slice 10` | `--element-type hex27` |

## Development

Requires [uv](https://docs.astral.sh/uv/).

```sh
uv sync
uv run pytest
```

Build the documentation into `docs/_build/html`:

```sh
uv run --group docs sphinx-build -W -b html docs docs/_build/html
```

## License

Licensed under the [Apache License 2.0](LICENSE).
