diff --git a/tdcgal/plane/plane.cpp b/tdcgal/plane/plane.cpp index 5910fd0..a71dfd7 100644 --- a/tdcgal/plane/plane.cpp +++ b/tdcgal/plane/plane.cpp @@ -1,9 +1,13 @@ +#include +#include #include #include #include #include #include +#include +#include #include "../cgal.hpp" @@ -16,10 +20,11 @@ typedef Kernel::Ray_2 Ray_2; typedef Kernel::Line_2 Line_2; typedef Kernel::Circle_2 Circle_2; typedef Kernel::Triangle_2 Triangle_2; -typedef Kernel::Bbox_2 Bbox_2; +typedef CGAL::Bbox_2 Bbox_2; +typedef Kernel::Iso_rectangle_2 Iso_rectangle_2; +typedef CGAL::Aff_transformation_2 Transformation; typedef Kernel::FT FT; typedef Kernel::RT RT; -typedef Kernel::Iso_rectangle_2 Iso_rectangle_2; template FT squared_distance(const T &a, const U &b) { return CGAL::squared_distance(a, b); @@ -54,7 +59,7 @@ void init_plane(py::module_ &m) { .def(py::init()) .def("_source", &Segment_2::source) .def("_target", &Segment_2::target) - .def("min", &Segment_2hfdaslkasdfkla::min, "返回线段的端点中较小的那个") + .def("min", &Segment_2::min, "返回线段的端点中较小的那个") .def("max", &Segment_2::max, "返回线段的端点中较大的那个") .def("_squared_length", &Segment_2::squared_length, "返回线段的平方长度") .def("opposite", &Segment_2::opposite, "返回反向的线段") @@ -137,14 +142,15 @@ void init_plane(py::module_ &m) { .def("perpendicular", &Vector_2::perpendicular, "返回与向量垂直的向量, 参数`o`类型为`Orientation`") .def("squared_length", &Vector_2::squared_length, "返回向量的平方长度") - .def("transform", &Vector_2::transform, "返回经过变换后的向量") .def("direction", &Vector_2::direction, "返回向量的方向") .def(py::self == py::self) .def(py::self != py::self) .def(py::self + py::self) .def(py::self - py::self) .def(py::self * py::self) - .def(py::self / py::self) + .def( + "__truediv__", [](const Vector_2 &v, const FT &f) { return v / f; }, + py::is_operator()) .def(-py::self, "返回反向的向量"); py::class_(m, "Line_2") @@ -206,20 +212,20 @@ void init_plane(py::module_ &m) { .def("opposite", &Circle_2::opposite, "返回反向的圆") .def("orthogonal_transform", &Circle_2::orthogonal_transform) .def("bbox", &Circle_2::bbox, "返回圆的边界框"); - + py::class_(m, "Triangle_2") - .def(py::init()) - .def("vertex", &Triangle_2::vertex, "返回第`i % 3`个顶点, `i`是参数") - .def("vertices", &Triangle_2::vertices, "返回三角形的三个顶点") - .def(py::self == py::self) - .def(py::self != py::self) - .def("area", &Triangle_2::area, "返回三角形的面积") - .def("is_degenerate", &Triangle_2::is_degenerate, "判断三角形是否退化") - .def("orientation", &Triangle_2::orientation, "返回三角形的方向") - .def("oriented_side", &Triangle_2::oriented_side, + .def(py::init()) + .def("vertex", &Triangle_2::vertex, "返回第`i % 3`个顶点, `i`是参数") + .def(py::self == py::self) + .def(py::self != py::self) + .def("area", &Triangle_2::area, "返回三角形的面积") + .def("is_degenerate", &Triangle_2::is_degenerate, "判断三角形是否退化") + .def("orientation", &Triangle_2::orientation, "返回三角形的方向") + .def("oriented_side", &Triangle_2::oriented_side, "判断点和三角形之间的位置关系") - .def("bounded_side", &Triangle_2::bounded_side, "判断点和三角形的边界位置关系" - "判断点是否在三角形的边上") + .def("bounded_side", &Triangle_2::bounded_side, + "判断点和三角形的边界位置关系" + "判断点是否在三角形的边上") .def("has_on_positive_side", &Triangle_2::has_on_positive_side, "判断点是否在三角形的正面上") .def("has_on_negative_side", &Triangle_2::has_on_negative_side, @@ -231,52 +237,120 @@ void init_plane(py::module_ &m) { "判断点是否在三角形的内部") .def("opposite", &Triangle_2::opposite, "返回反向的三角形") .def("bbox", &Triangle_2::bbox, "返回三角形的边界框") - .def("transform", &Triangle_2::transform, "返回经过变换后的三角形") + .def("transform", &Triangle_2::transform, "返回经过变换后的三角形"); py::class_(m, "Iso_rectangle_2") - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::init()) - .def(py::self == py::self) - .def(py::self != py::self) - .def("vertex", &Iso_rectangle_2::vertex, "返回第`i % 4`个顶点, `i`是参数") - .def("min", &Iso_rectangle_2::min, "返回左下角顶点") - .def("max", &Iso_rectangle_2::max, "返回右上角顶点") - .def("xmin", &Iso_rectangle_2::xmin, "返回左下角顶点的x坐标") - .def("ymin", &Iso_rectangle_2::ymin, "返回左下角顶点的y坐标") - .def("xmax", &Iso_rectangle_2::xmax, "返回右上角顶点的x坐标") - .def("ymax", &Iso_rectangle_2::ymax, "返回右上角顶点的y坐标") - .def("min_coord", &Iso_rectangle_2::min_coord, "返回左下角顶点的坐标") - .def("max_coord", &Iso_rectangle_2::max_coord, "返回右上角顶点的坐标") - .def("is_degenerate", &Iso_rectangle_2::is_degenerate, "判断矩形是否退化") - .def("bounded_side", &Iso_rectangle_2::bounded_side, "判断点和矩形的边界位置关系") - .def("has_on_bounded_side", &Iso_rectangle_2::has_on_bounded_side, + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::self == py::self) + .def(py::self != py::self) + .def("vertex", &Iso_rectangle_2::vertex, "返回第`i % 4`个顶点, `i`是参数") + .def("min", &Iso_rectangle_2::min, "返回左下角顶点") + .def("max", &Iso_rectangle_2::max, "返回右上角顶点") + .def("xmin", &Iso_rectangle_2::xmin, "返回左下角顶点的x坐标") + .def("ymin", &Iso_rectangle_2::ymin, "返回左下角顶点的y坐标") + .def("xmax", &Iso_rectangle_2::xmax, "返回右上角顶点的x坐标") + .def("ymax", &Iso_rectangle_2::ymax, "返回右上角顶点的y坐标") + .def("min_coord", &Iso_rectangle_2::min_coord, "返回左下角顶点的坐标") + .def("max_coord", &Iso_rectangle_2::max_coord, "返回右上角顶点的坐标") + .def("is_degenerate", &Iso_rectangle_2::is_degenerate, "判断矩形是否退化") + .def("bounded_side", &Iso_rectangle_2::bounded_side, + "判断点和矩形的边界位置关系") + .def("has_on_bounded_side", &Iso_rectangle_2::has_on_bounded_side, "判断点是否在矩形的边界上") - .def("has_on_unbounded_side", &Iso_rectangle_2::has_on_unbounded_side, + .def("has_on_unbounded_side", &Iso_rectangle_2::has_on_unbounded_side, "判断点是否在矩形的内部") - .def("has_on_boundary", &Iso_rectangle_2::has_on_boundary, + .def("has_on_boundary", &Iso_rectangle_2::has_on_boundary, "判断点是否在矩形的边界上") - .def("bbox", &Iso_rectangle_2::bbox, "返回矩形的边界框") - .def("area", &Iso_rectangle_2::area, "返回矩形的面积") - .def("transform", &Iso_rectangle_2::transform, "返回经过变换后的矩形"); + .def("bbox", &Iso_rectangle_2::bbox, "返回矩形的边界框") + .def("area", &Iso_rectangle_2::area, "返回矩形的面积") + .def("transform", &Iso_rectangle_2::transform, "返回经过变换后的矩形"); py::class_(m, "Bbox_2") - .def(py::init<>()) - .def(py::init()) - .def(py::self == py::self) - .def(py::self != py::self) - .def("dimension", &Bbox_2::dimension, "返回边界框的维数") - .def("xmin", &Bbox_2::xmin, "返回左下角顶点的x坐标") - .def("ymin", &Bbox_2::ymin, "返回左下角顶点的y坐标") - .def("xmax", &Bbox_2::xmax, "返回右上角顶点的x坐标") - .def("ymax", &Bbox_2::ymax, "返回右上角顶点的y坐标") - .def("min", &Bbox_2::min, "参数`i`, 如果`i=0`返回`xmin()`, 如果`i=1`返回`ymin()`") - .def("max", &Bbox_2::max, "参数`i`, 如果`i=0`返回`xmax()`, 如果`i=1`返回`ymax()`") - .def(py::self + py::self) - .def(py::self += py::self) - .def("dilate", &Bbox_2::dilate, "扩大边界框"); + .def(py::init<>()) + .def(py::init()) + .def(py::self == py::self) + .def(py::self != py::self) + .def("dimension", &Bbox_2::dimension, "返回边界框的维数") + .def("xmin", &Bbox_2::xmin, "返回左下角顶点的x坐标") + .def("ymin", &Bbox_2::ymin, "返回左下角顶点的y坐标") + .def("xmax", &Bbox_2::xmax, "返回右上角顶点的x坐标") + .def("ymax", &Bbox_2::ymax, "返回右上角顶点的y坐标") + .def("min", &Bbox_2::min, + "参数`i`, 如果`i=0`返回`xmin()`, 如果`i=1`返回`ymin()`") + .def("max", &Bbox_2::max, + "参数`i`, 如果`i=0`返回`xmax()`, 如果`i=1`返回`ymax()`") + .def(py::self + py::self) + .def(py::self += py::self) + .def("dilate", &Bbox_2::dilate, "扩大边界框"); + + py::class_(m, "Transformation") + .def(py::init([](std::string &s) -> Transformation * { + if (s == "IDENTITY") { + return new Transformation(CGAL::IDENTITY); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def(py::init([](std::string &s, const Vector_2 &v) { + if (s == "TRANSLATION") { + return new Transformation(CGAL::TRANSLATION, v); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def(py::init([](std::string &s, const Direction_2 &d, + const Kernel::RT &num, const Kernel::RT &den = RT(1)) { + if (s == "ROTATION") { + return new Transformation(CGAL::ROTATION, d, num, den); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def(py::init([](std::string &s, const RT &sine_rho, const RT &cosine_rho, + const RT &hw = RT(1)) { + if (s == "ROTATION") { + + return new Transformation(CGAL::ROTATION, sine_rho, cosine_rho, hw); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def(py::init( + [](std::string &str, const RT &scaling, const RT &hw = RT(1)) { + if (str == "SCALING") { + return new Transformation(CGAL::SCALING, scaling, hw); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def(py::init([](std::string &s, const Line_2 &l) { + if (s == "REFLECTION") { + return new Transformation(CGAL::REFLECTION, l); + } else { + throw py::value_error("Invalid transformation string"); + } + })) + .def("inverse", &Transformation::inverse, "返回逆变换") + .def("is_even", &Transformation::is_even, "判断变换是否为偶变换") + .def("is_odd", &Transformation::is_odd, "判断变换是否为奇变换") + .def("is_scaling", &Transformation::is_scaling, "判断变换是否为缩放变换") + .def("is_translation", &Transformation::is_translation, + "判断变换是否为平移变换") + .def("is_rotation", &Transformation::is_rotation, + "判断变换是否为旋转变换") + .def("is_reflection", &Transformation::is_reflection, + "判断变换是否为反射变换") + .def(py::self * py::self) + .def(py::self == py::self) + .def("transform", py::overload_cast(&Transformation::transform, py::const_)) + .def("transform", py::overload_cast(&Transformation::transform, py::const_)) + .def("transform", + py::overload_cast(&Transformation::transform, py::const_)) + .def("transform", py::overload_cast(&Transformation::transform, py::const_)); m.def("squared_distance", &squared_distance, "返回两个点的平方距离");