fix(python): alias Point3D and Vector3D type

This commit is contained in:
hm
2026-10-30 00:22:03 +08:00
parent 24bd064dc8
commit 23594a0007
+4 -36
View File
@@ -75,42 +75,10 @@ Construct with ``Point3D(x, y, z)``. Supports arithmetic +, -, *scalar, /scalar.
return distance(a, b);
}, py::arg("other"), "Euclidean distance to another point");
// ── Vector3D ─────────────────────────────────────
py::class_<Vector3D>(core, "Vector3D",
R"pbdoc(
3D vector with double precision (Eigen::Vector3d).
Construct with ``Vector3D(x, y, z)``. Supports arithmetic +, -, *scalar, /scalar.
)pbdoc")
.def(py::init<double, double, double>(),
py::arg("x") = 0.0, py::arg("y") = 0.0, py::arg("z") = 0.0)
.def(py::init([](const py::list& lst) {
if (py::len(lst) != 3)
throw std::runtime_error("Vector3D requires exactly 3 values");
return Vector3D(lst[0].cast<double>(),
lst[1].cast<double>(),
lst[2].cast<double>());
}),
py::arg("coords"))
.def_property_readonly("x", [](const Vector3D& v) -> double { return v.x(); })
.def_property_readonly("y", [](const Vector3D& v) -> double { return v.y(); })
.def_property_readonly("z", [](const Vector3D& v) -> double { return v.z(); })
.def("__repr__", &vector3d_repr)
.def("__len__", [](const Vector3D&) { return 3; })
.def("__getitem__", [](const Vector3D& v, int i) {
if (i < 0 || i > 2) throw py::index_error();
return v(i);
})
.def("__add__", [](const Vector3D& a, const Vector3D& b) { return a + b; })
.def("__sub__", [](const Vector3D& a, const Vector3D& b) { return a - b; })
.def("__mul__", [](const Vector3D& v, double s) { return v * s; })
.def("__truediv__", [](const Vector3D& v, double s) { return v / s; })
.def("__neg__", [](const Vector3D& v) { return -v; })
.def("norm", [](const Vector3D& v) { return v.norm(); })
.def("squared_norm", [](const Vector3D& v) { return v.squaredNorm(); })
.def("dot", [](const Vector3D& a, const Vector3D& b) { return a.dot(b); })
.def("cross", [](const Vector3D& a, const Vector3D& b) { return a.cross(b); })
.def("normalized", [](const Vector3D& v) { return v.normalized(); });
// Point3D and Vector3D are the same Eigen::Matrix<double,3,1> type;
// register once and expose the second name as an alias to avoid duplicate
// pybind11 generic_type registration.
core.attr("Vector3D") = core.attr("Point3D");
// ── Point2D ──────────────────────────────────────
py::class_<Point2D>(core, "Point2D")