Metadata-Version: 2.4
Name: cpgeo
Version: 2.0.0
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"

# 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

To build the shared library, run

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

Install the Python package from the repository with `python -m pip install .`.
For the STEP example below, install the export dependency with
`python -m pip install ".[step]"`. The wheel build invokes CMake and includes the shared library. For development,
`PYTHONPATH=src/python` uses the library built in `build/native`. Set
`CPGEO_LIBRARY` to select a specific 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 `surface.symmetry_mirror(plane="xz")` or
`surface.symmetry_rotational(periods=4)` to preview the mesh and correspondence.
Pass `apply=True` to rebuild the surface. `surface.snapshot()` copies the control
mesh, reference nodes, support radii, symmetry groups and construction options.

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
PYTHONPATH=src/python 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)

## Repository Layout

| Path | Contents |
| --- | --- |
| [`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/) | All kernel structs, including internal stage data, with one header per type |
| [`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 |
| [`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}
}
```
