#include #include #include #include "vde/core/point.h" #include "vde/core/aabb.h" #include "vde/core/distance.h" #include "vde/core/convex_hull.h" #include "vde/core/polygon.h" #include "vde/core/transform.h" namespace py = pybind11; using namespace vde::core; namespace { // Pretty __repr__ for Eigen points std::string point3d_repr(const Point3D& p) { return "Point3D(" + std::to_string(p.x()) + ", " + std::to_string(p.y()) + ", " + std::to_string(p.z()) + ")"; } std::string point2d_repr(const Point2D& p) { return "Point2D(" + std::to_string(p.x()) + ", " + std::to_string(p.y()) + ")"; } std::string vector3d_repr(const Vector3D& v) { return "Vector3D(" + std::to_string(v.x()) + ", " + std::to_string(v.y()) + ", " + std::to_string(v.z()) + ")"; } } // namespace void bind_core(py::module& m) { auto core = m.def_submodule("core", "Core geometry types and operations"); // ── Point3D ────────────────────────────────────── py::class_(core, "Point3D", R"pbdoc( 3D point with double precision (Eigen::Vector3d). Construct with ``Point3D(x, y, z)``. Supports arithmetic +, -, *scalar, /scalar. )pbdoc") .def(py::init(), py::arg("x") = 0.0, py::arg("y") = 0.0, py::arg("z") = 0.0, "Construct from (x, y, z)") .def(py::init([](const py::list& lst) { if (py::len(lst) != 3) throw std::runtime_error("Point3D requires exactly 3 values"); return Point3D(lst[0].cast(), lst[1].cast(), lst[2].cast()); }), py::arg("coords"), "Construct from [x, y, z] list") .def_property_readonly("x", [](const Point3D& p) -> double { return p.x(); }) .def_property_readonly("y", [](const Point3D& p) -> double { return p.y(); }) .def_property_readonly("z", [](const Point3D& p) -> double { return p.z(); }) .def("__repr__", &point3d_repr) .def("__len__", [](const Point3D&) { return 3; }) .def("__getitem__", [](const Point3D& p, int i) { if (i < 0 || i > 2) throw py::index_error(); return p(i); }) .def("__add__", [](const Point3D& a, const Vector3D& v) { return a + v; }) .def("__sub__", [](const Point3D& a, const Point3D& b) { return a - b; }) .def("norm", [](const Point3D& p) { return p.norm(); }) .def("squared_norm", [](const Point3D& p) { return p.squaredNorm(); }) .def("dot", [](const Point3D& a, const Point3D& b) { return a.dot(b); }) .def("cross", [](const Point3D& a, const Point3D& b) { return a.cross(b); }) .def("normalized", [](const Point3D& p) { return p.normalized(); }) .def("distance_to", [](const Point3D& a, const Point3D& b) { return distance(a, b); }, py::arg("other"), "Euclidean distance to another point"); // Point3D and Vector3D are the same Eigen::Matrix 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_(core, "Point2D") .def(py::init(), py::arg("x") = 0.0, py::arg("y") = 0.0) .def(py::init([](const py::list& lst) { if (py::len(lst) != 2) throw std::runtime_error("Point2D requires exactly 2 values"); return Point2D(lst[0].cast(), lst[1].cast()); })) .def_property_readonly("x", [](const Point2D& p) -> double { return p.x(); }) .def_property_readonly("y", [](const Point2D& p) -> double { return p.y(); }) .def("__repr__", &point2d_repr) .def("__len__", [](const Point2D&) { return 2; }) .def("__getitem__", [](const Point2D& p, int i) { if (i < 0 || i > 1) throw py::index_error(); return p(i); }); // ── AABB3D ─────────────────────────────────────── py::class_>(core, "AABB3D", R"pbdoc( Axis-aligned bounding box with double precision. An empty box has min > max. Call ``expand()`` or construct with min/max points. )pbdoc") .def(py::init<>(), "Create an empty AABB") .def(py::init(), py::arg("min"), py::arg("max"), "Create from min and max corners") .def("expand", py::overload_cast(&AABB::expand), py::arg("point"), "Expand to include a point") .def("expand", py::overload_cast&>(&AABB::expand), py::arg("other"), "Expand to include another AABB") .def_property_readonly("center", &AABB::center) .def_property_readonly("extent", &AABB::extent) .def_property_readonly("surface_area", &AABB::surface_area) .def_property_readonly("volume", &AABB::volume) .def_property_readonly("min", &AABB::min) .def_property_readonly("max", &AABB::max) .def("contains", &AABB::contains, py::arg("point"), "Check if point is inside (inclusive)") .def("intersects", &AABB::intersects, py::arg("other"), "Check if AABBs overlap") .def("__repr__", [](const AABB& box) { return "AABB3D(min=" + point3d_repr(box.min()) + ", max=" + point3d_repr(box.max()) + ")"; }); // ── Polygon2D ──────────────────────────────────── py::class_(core, "Polygon2D", R"pbdoc( 2D polygon defined by a list of vertices. )pbdoc") .def(py::init<>()) .def(py::init>(), py::arg("vertices"), "Construct from vertex list") .def_property_readonly("vertices", &Polygon2D::vertices) .def("__len__", &Polygon2D::size) .def_property_readonly("size", &Polygon2D::size) .def_property_readonly("area", &Polygon2D::area) .def_property_readonly("signed_area", &Polygon2D::signed_area) .def_property_readonly("perimeter", &Polygon2D::perimeter) .def_property_readonly("centroid", &Polygon2D::centroid) .def_property_readonly("bounding_box", &Polygon2D::bounding_box) .def_property_readonly("is_ccw", &Polygon2D::is_ccw) .def("contains", &Polygon2D::contains, py::arg("point"), "Point-in-polygon test (ray casting)") .def("simplify", &Polygon2D::simplify, py::arg("tolerance"), "Douglas-Peucker simplification") .def("triangulate", &Polygon2D::triangulate, "Ear-clipping triangulation. Returns list of [i, j, k] triples.") .def_static("convex_hull_2d", &Polygon2D::convex_hull_2d, py::arg("points"), "Andrew's monotone chain convex hull") .def("__repr__", [](const Polygon2D& poly) { return "Polygon2D(vertices=" + std::to_string(poly.size()) + ")"; }); // ── Free functions ─────────────────────────────── core.def("distance", py::overload_cast(&distance), py::arg("a"), py::arg("b"), "Euclidean distance between two 3D points"); core.def("convex_hull_2d", &convex_hull_2d, py::arg("points"), "Graham scan 2D convex hull. Returns vertices in CCW order."); core.def("convex_hull_3d", &convex_hull_3d, py::arg("points"), "QuickHull 3D convex hull. Returns triangles as list of [p0, p1, p2]."); // ── Transform helpers ──────────────────────────── core.def("translate", py::overload_cast(&translate), py::arg("v"), "Create translation transform"); core.def("translate", py::overload_cast(&translate), py::arg("x"), py::arg("y"), py::arg("z"), "Create translation transform from x, y, z"); core.def("rotate", &rotate, py::arg("axis"), py::arg("angle_rad"), "Create rotation transform around axis by angle (radians)"); core.def("rotate_x", &rotate_x, py::arg("rad"), "Create rotation around X axis"); core.def("rotate_y", &rotate_y, py::arg("rad"), "Create rotation around Y axis"); core.def("rotate_z", &rotate_z, py::arg("rad"), "Create rotation around Z axis"); core.def("scale", py::overload_cast(&scale), py::arg("sx"), py::arg("sy"), py::arg("sz"), "Create non-uniform scale transform"); core.def("scale", py::overload_cast(&scale), py::arg("s"), "Create uniform scale transform"); }