- 使用 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 启用
108 lines
5.0 KiB
C++
108 lines
5.0 KiB
C++
#include <pybind11/pybind11.h>
|
|
#include <pybind11/eigen.h>
|
|
#include <pybind11/stl.h>
|
|
|
|
#include "vde/curves/bezier_curve.h"
|
|
#include "vde/curves/bspline_curve.h"
|
|
#include "vde/curves/nurbs_curve.h"
|
|
|
|
namespace py = pybind11;
|
|
using namespace vde::curves;
|
|
|
|
void bind_curves(py::module& m) {
|
|
auto curves = m.def_submodule("curves",
|
|
"Parametric curves: Bézier, B-Spline, NURBS");
|
|
|
|
// ── BezierCurve ──────────────────────────────────
|
|
py::class_<BezierCurve>(curves, "BezierCurve",
|
|
R"pbdoc(
|
|
Bézier curve defined by control points.
|
|
|
|
Construct with a list of Point3D control points. The degree is ``len(points) - 1``.
|
|
Evaluate at parameter ``t ∈ [0, 1]``.
|
|
)pbdoc")
|
|
.def(py::init<std::vector<Point3D>>(),
|
|
py::arg("control_points"),
|
|
"Construct from control points (list of Point3D)")
|
|
.def("evaluate", &BezierCurve::evaluate,
|
|
py::arg("t"),
|
|
"Evaluate curve at parameter t ∈ [0, 1]")
|
|
.def("derivative", &BezierCurve::derivative,
|
|
py::arg("t"), py::arg("order") = 1,
|
|
"Evaluate derivative of given order at t")
|
|
.def_property_readonly("degree", &BezierCurve::degree)
|
|
.def_property_readonly("control_points", &BezierCurve::control_points)
|
|
.def_property_readonly("domain", &BezierCurve::domain)
|
|
.def("split", &BezierCurve::split,
|
|
py::arg("t"),
|
|
"Split at parameter t, returns (left, right) curves")
|
|
.def("degree_elevate", &BezierCurve::degree_elevate,
|
|
py::arg("times") = 1,
|
|
"Degree elevation, returns new curve")
|
|
.def("__repr__", [](const BezierCurve& c) {
|
|
return "BezierCurve(degree=" + std::to_string(c.degree()) + ")";
|
|
});
|
|
|
|
// ── BSplineCurve ─────────────────────────────────
|
|
py::class_<BSplineCurve>(curves, "BSplineCurve",
|
|
R"pbdoc(
|
|
B-Spline curve.
|
|
|
|
Construct with ``BSplineCurve(control_points, knots, degree)``.
|
|
)pbdoc")
|
|
.def(py::init<std::vector<Point3D>, std::vector<double>, int>(),
|
|
py::arg("control_points"), py::arg("knots"), py::arg("degree"),
|
|
"Construct from control points, knot vector, and degree")
|
|
.def("evaluate", &BSplineCurve::evaluate,
|
|
py::arg("t"),
|
|
"Evaluate curve at parameter t ∈ domain")
|
|
.def("derivative", &BSplineCurve::derivative,
|
|
py::arg("t"), py::arg("order") = 1,
|
|
"Evaluate derivative of given order at t")
|
|
.def_property_readonly("degree", &BSplineCurve::degree)
|
|
.def_property_readonly("control_points", &BSplineCurve::control_points)
|
|
.def_property_readonly("knots", &BSplineCurve::knots)
|
|
.def_property_readonly("domain", &BSplineCurve::domain)
|
|
.def("find_span", &BSplineCurve::find_span,
|
|
py::arg("t"),
|
|
"Find knot span index for parameter t")
|
|
.def("basis_functions", &BSplineCurve::basis_functions,
|
|
py::arg("t"), py::arg("span") = -1,
|
|
"Evaluate non-zero basis functions at t")
|
|
.def("basis_derivatives", &BSplineCurve::basis_derivatives,
|
|
py::arg("t"), py::arg("span") = -1,
|
|
"Evaluate first derivatives of non-zero basis functions at t")
|
|
.def("__repr__", [](const BSplineCurve& c) {
|
|
return "BSplineCurve(degree=" + std::to_string(c.degree())
|
|
+ ", cp=" + std::to_string(c.control_points().size()) + ")";
|
|
});
|
|
|
|
// ── NurbsCurve ──────────────────────────────────
|
|
py::class_<NurbsCurve>(curves, "NurbsCurve",
|
|
R"pbdoc(
|
|
Non-Uniform Rational B-Spline (NURBS) curve.
|
|
|
|
Construct with ``NurbsCurve(control_points, knots, weights, degree)``.
|
|
)pbdoc")
|
|
.def(py::init<std::vector<Point3D>, std::vector<double>,
|
|
std::vector<double>, int>(),
|
|
py::arg("control_points"), py::arg("knots"),
|
|
py::arg("weights"), py::arg("degree"),
|
|
"Construct from control points, knot vector, weights, and degree")
|
|
.def("evaluate", &NurbsCurve::evaluate,
|
|
py::arg("t"),
|
|
"Evaluate rational curve at parameter t ∈ domain")
|
|
.def("derivative", &NurbsCurve::derivative,
|
|
py::arg("t"), py::arg("order") = 1,
|
|
"Evaluate derivative of given order at t")
|
|
.def_property_readonly("degree", &NurbsCurve::degree)
|
|
.def_property_readonly("control_points", &NurbsCurve::control_points)
|
|
.def_property_readonly("weights", &NurbsCurve::weights)
|
|
.def_property_readonly("knots", &NurbsCurve::knots)
|
|
.def_property_readonly("domain", &NurbsCurve::domain)
|
|
.def("__repr__", [](const NurbsCurve& c) {
|
|
return "NurbsCurve(degree=" + std::to_string(c.degree())
|
|
+ ", cp=" + std::to_string(c.control_points().size()) + ")";
|
|
});
|
|
}
|