feat: Python 绑定 — pybind11 核心/曲线/网格模块
CI / Build & Test (push) Failing after 33s
CI / Release Build (push) Failing after 32s

- 使用 pybind11 v2.11.1 (FetchContent) 构建 _vde 原生模块
- 模块化结构: bind_core.cpp / bind_curves.cpp / bind_mesh.cpp
- 暴露核心类型: Point2D, Point3D, Vector3D, AABB3D, Polygon2D
- 暴露曲线: BezierCurve, BSplineCurve, NurbsCurve
- 暴露网格: delaunay_2d/3d, DelaunayResult, HalfedgeMesh
- 变换函数: translate, rotate, scale 等
- Python 包 vde/ 含 __init__.py 及子模块封装
- setup.py 支持 pip install -e . 可编辑安装
- VDE_BUILD_PYTHON CMake 选项 (默认 OFF),通过 -DVDE_BUILD_PYTHON=ON 启用
This commit is contained in:
茂之钳
2026-07-24 03:58:19 +00:00
parent 1ac5a27d4c
commit bd37f2249d
15 changed files with 1186 additions and 219 deletions
+103
View File
@@ -0,0 +1,103 @@
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include "vde/mesh/delaunay_2d.h"
#include "vde/mesh/delaunay_3d.h"
#include "vde/mesh/halfedge_mesh.h"
namespace py = pybind11;
using namespace vde::mesh;
void bind_mesh(py::module& m) {
auto mesh = m.def_submodule("mesh",
"Mesh generation and processing");
// ── DelaunayResult (2D) ──────────────────────────
py::class_<DelaunayResult>(mesh, "DelaunayResult",
R"pbdoc(
Result of 2D Delaunay triangulation.
Attributes:
vertices: list of Point2D
triangles: list of [i, j, k] index triples
)pbdoc")
.def(py::init<>())
.def_readwrite("vertices", &DelaunayResult::vertices)
.def_readwrite("triangles", &DelaunayResult::triangles)
.def("__repr__", [](const DelaunayResult& r) {
return "DelaunayResult(v=" + std::to_string(r.vertices.size())
+ ", t=" + std::to_string(r.triangles.size()) + ")";
});
// ── TetrahedronMesh (3D) ─────────────────────────
py::class_<TetrahedronMesh>(mesh, "TetrahedronMesh",
R"pbdoc(
Result of 3D Delaunay tetrahedralization.
Attributes:
vertices: list of Point3D
tetrahedra: list of [i, j, k, l] index quadruples
)pbdoc")
.def(py::init<>())
.def_readwrite("vertices", &TetrahedronMesh::vertices)
.def_readwrite("tetrahedra", &TetrahedronMesh::tetrahedra)
.def("__repr__", [](const TetrahedronMesh& m) {
return "TetrahedronMesh(v=" + std::to_string(m.vertices.size())
+ ", tet=" + std::to_string(m.tetrahedra.size()) + ")";
});
// ── Delaunay free functions ──────────────────────
mesh.def("delaunay_2d", &delaunay_2d,
py::arg("points"),
"Bowyer-Watson 2D Delaunay triangulation");
mesh.def("delaunay_3d", &delaunay_3d,
py::arg("points"),
"3D Delaunay tetrahedralization (Bowyer-Watson)");
// ── HalfedgeMesh ─────────────────────────────────
py::class_<HalfedgeMesh>(mesh, "HalfedgeMesh",
R"pbdoc(
Half-edge data structure for polygon meshes.
Supports construction from triangle soup and provides topology queries,
normal computation, and iterators.
)pbdoc")
.def(py::init<>())
.def("clear", &HalfedgeMesh::clear)
.def("add_vertex", &HalfedgeMesh::add_vertex,
py::arg("point"), "Add a vertex, returns index")
.def("add_face", &HalfedgeMesh::add_face,
py::arg("vertex_indices"), "Add a face from vertex indices, returns face index")
.def("build_from_triangles", &HalfedgeMesh::build_from_triangles,
py::arg("vertices"), py::arg("triangles"),
"Build from vertex list and triangle index list")
.def_property_readonly("num_vertices", &HalfedgeMesh::num_vertices)
.def_property_readonly("num_faces", &HalfedgeMesh::num_faces)
.def_property_readonly("num_edges", &HalfedgeMesh::num_edges)
.def("vertex", &HalfedgeMesh::vertex, py::arg("idx"))
.def("set_vertex", &HalfedgeMesh::set_vertex,
py::arg("idx"), py::arg("point"))
.def("face_vertices", &HalfedgeMesh::face_vertices,
py::arg("face_index"),
"Get vertex indices of a face")
.def("vertex_faces", &HalfedgeMesh::vertex_faces,
py::arg("vertex_index"),
"Get face indices incident to a vertex")
.def("is_boundary_edge", &HalfedgeMesh::is_boundary_edge,
py::arg("halfedge_index"))
.def("is_boundary_vertex", &HalfedgeMesh::is_boundary_vertex,
py::arg("vertex_index"))
.def("face_normal", &HalfedgeMesh::face_normal,
py::arg("face_index"))
.def("vertex_normal", &HalfedgeMesh::vertex_normal,
py::arg("vertex_index"))
.def("update_normals", &HalfedgeMesh::update_normals)
.def_property_readonly("bounds", &HalfedgeMesh::bounds)
.def("__repr__", [](const HalfedgeMesh& m) {
return "HalfedgeMesh(v=" + std::to_string(m.num_vertices())
+ ", f=" + std::to_string(m.num_faces())
+ ", e=" + std::to_string(m.num_edges()) + ")";
});
}