Metadata-Version: 2.4
Name: cpgeo
Version: 2.0.1
Summary: A library for control points-based surface.
Author-email: Song Zenan <243919588@qq.com>
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: numpy>=2.0.0
Provides-Extra: step
Requires-Dist: cadquery-ocp>=7.8; extra == "step"
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Requires-Dist: scipy>=1.13; extra == "test"

# CPGEO — Control-Point Geometry for Closed Surfaces

**Developed by Zenan Song**

CPGEO represents a closed, genus-zero surface by a triangular control mesh and
a map from the unit sphere to physical space. Each surface point is a weighted
combination of nearby control points. Moving those controls changes the shape
while preserving its reference coordinates. MorphOpt uses this representation
to optimize the outer walls and internal cavities of pneumatic soft actuators.

## Features

The C++20 kernel exposes a C99 interface and a NumPy-based Python class.

- **Surface construction** from a closed control mesh, including spherical
  parameterization and control-point updates.
- **Surface evaluation** at reference-sphere or UV coordinates, with positions,
  first and second UV derivatives, and sparse control-point weights.
- **Sampling and reconstruction** with adaptive seed spacing, patch-based
  smoothing, and fixed control points.
- **Mirror and rotational symmetry** with mesh previews and control-point
  correspondence between symmetric regions.
- **CAD export** through bicubic B-spline patch fitting with a specified
  geometric tolerance and STEP solid export through Python OCP.

## Quick Start

Install the platform wheel from PyPI:

```bash
python -m pip install cpgeo
# Optional STEP export
python -m pip install "cpgeo[step]"
```

For a source checkout, `python -m pip install .` invokes CMake through `setup.py`
and installs the Python package together with the shared library. Source builds
require a C++20 compiler; the Python build environment supplies CMake.
Use `python -m pip install -e ".[test,step]"` for development. Editable installation
also builds the native library into `src/python/cpgeo/bin/`.

Python calls the C99 interface through `ctypes`. Each wheel contains the native
library for one OS and architecture and uses the `py3-none-<platform>` tag.
The release workflow builds Linux x86-64/AArch64, Windows x86-64/ARM64, and macOS
Intel/Apple Silicon wheels. Windows ARM64 artifacts are tested on Python 3.12
and 3.14; other platforms use Python 3.10, 3.12, and 3.14.
Set `CPGEO_LIBRARY` to select an explicit development build.

```python
from cpgeo import CPGEO, set_max_threads

set_max_threads(4)
with CPGEO.load("surface.npz") as surface:
    vertices, faces = surface.sample_mesh(seed_size=1.0, smoothing_sweeps=2)
    fit = surface.fit_spline_patches(tolerance=0.01, target_nodes=64)
    fit.write_step("surface.stp")
```

`surface.save("surface.npz")` writes a compressed archive containing
`control_points`, `cp_faces`, and `knot_influence_num`. `CPGEO.save` and
`CPGEO.load` support paths and binary streams. Loading also accepts legacy
CPGEO and MorphOpt geometry archives and rebuilds the reference parameterization.

Python uses NumPy for C buffer layout and result ownership. Geometry operations
run in the shared library. Use the native generator helpers for mirrors and
cyclic rotations, including offset planes and arbitrary rotation axes.

```python
from cpgeo import symmetry_mirror, symmetry_rotation

generators = [symmetry_mirror("xz"), symmetry_rotation("z", periods=6)]
result = surface.symmetry(generators, seed_size=1., apply=True)
primary, image, transform = result.relations.T
fixed_points = result.control_points[:result.fixed_count]
```

The default previews the stitched mesh. With `apply=True`, the surface snapshot
also stores the correspondence and its shared table of 4 × 4 transformations.
Each relation maps one primary control to its image using the indexed matrix.

The position tolerance uses model units. STEP export interprets coordinates as
millimetres. The Python exporter loads `cadquery-ocp` on the first export,
sews the fitted faces into a solid, and validates the temporary STEP file
before replacing the destination. Native builds only require the geometry
kernel's C++ toolchain.

The Python acceptance suite uses pytest and SciPy for independent numerical checks.
To run the C99, C++ and Python suites, use

```bash
cmake -S tests -B build/acceptance -DCMAKE_BUILD_TYPE=Release
cmake --build build/acceptance --parallel
ctest --test-dir build/acceptance --output-on-failure
python -m pip install -e ".[test,step]"
python -m pytest -q tests/python
```

The suites check winding, fixed controls, serial and parallel sampling, symmetry
correspondence, independent spline evaluation and STEP round trips.
[Acceptance test guide (Chinese)](tests/README.md)

## Builds and Releases

`pyproject.toml` owns package metadata, dependencies, and the wheel matrix.
`setup.py` connects setuptools to the CMake target; CMake owns native compilation
and OpenMP discovery. GitHub Actions builds wheels from the source distribution,
repairs runtime dependencies with auditwheel/delocate/delvewheel, and tests the
installed wheels. Version tags trigger PyPI publication through Trusted Publishing.

```bash
python -m pip install build twine
python -m build
python -m twine check dist/*
```

[Build and PyPI publication guide (Chinese)](docs/releasing.md) covers local
builds, the platform matrix, TestPyPI, publisher configuration, and version tags.
For a standalone native library and C header installation:

```bash
cmake -S . -B build/native -DCMAKE_BUILD_TYPE=Release
cmake --build build/native --config Release --parallel
cmake --install build/native --config Release --prefix ./build/install
```

## Repository Layout

| Path | Contents |
| --- | --- |
| [`setup.py`](setup.py) | CMake-backed setuptools build and Python-independent platform wheel tags |
| [`pyproject.toml`](pyproject.toml) | Package metadata, build dependencies, extras, and cibuildwheel configuration |
| [`.github/workflows/release.yml`](.github/workflows/release.yml) | Native tests, six platform wheels, and PyPI/TestPyPI publication |
| [`CMakeLists.txt`](CMakeLists.txt) | Shared-library target and standalone C SDK installation |
| [`include/cpgeo.h`](include/cpgeo.h) | Public C99 interface, surface handles, options, and result types |
| [`src/cpgeo.cpp`](src/cpgeo.cpp) | C99 adapter for the public header |
| [`src/cpp/include/`](src/cpp/include/) | Internal C++ declarations for construction, mapping, reconstruction, and symmetry |
| [`src/cpp/include/types/`](src/cpp/include/types/) | Shared kernel types; private stage data is nested in its owning class |
| [`src/cpp/src/`](src/cpp/src/) | C++ translation units mirroring the kernel header tree |
| [`src/python/cpgeo/`](src/python/cpgeo/) | NumPy surface class and typed C ABI bindings |
| [`tests/cpp/`](tests/cpp/) | C++ kernel tests, C99 interface tests, and geometric fixtures |
| [`tests/python/`](tests/python/) | Python binding and STEP round-trip tests |
| [`tests/packaging/`](tests/packaging/) | Wheel layout, native binary format, and installed-library checks |
| [`docs/releasing.md`](docs/releasing.md) | Source builds, platform wheels, and PyPI publication (Chinese) |
| [`docs/api.md`](docs/api.md) | C API reference with parameter types, array layouts, and ownership rules (Chinese) |
| [`docs/cpp_kernel.md`](docs/cpp_kernel.md) | Complete C++ kernel reference with per-type sections and a dedicated half-edge mesh chapter (Chinese) |

## Citation

For the geometric model and adaptive surface reconstruction used in soft robot
shape optimization, please cite the following paper. [IEEE Transactions on Robotics](https://doi.org/10.1109/TRO.2026.3716051)

```bibtex
@article{song2026continuum,
  author  = {Song, Zenan and Chen, Feifei and Gu, Guoying and Zhu, Xiangyang},
  title   = {Continuum Jacobian-Based Computational Morphogenesis for Soft Robotic Workspace Optimization},
  journal = {IEEE Transactions on Robotics},
  year    = {2026},
  volume  = {42},
  pages   = {3048--3068},
  doi     = {10.1109/TRO.2026.3716051}
}
```
