From b82011ef762675064a036e8c64fe4bcae79a9805 Mon Sep 17 00:00:00 2001 From: songsenand Date: Mon, 19 Feb 2024 23:32:40 +0800 Subject: [PATCH] feat(tdcgal/plane/plane.cpp-tdcgal/plane/objects.py): add Vector_2 Direction_2 Direction2D etc --- tdcgal/plane/objects.py | 48 ++++++++++++++++++++++++++++-- tdcgal/plane/plane.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/tdcgal/plane/objects.py b/tdcgal/plane/objects.py index d7192fb..a025019 100644 --- a/tdcgal/plane/objects.py +++ b/tdcgal/plane/objects.py @@ -114,7 +114,7 @@ class Ray2D(Shape2D, Ray_2): @property def source(self) -> Point2D: return self._source() - + # 返回射线方向 @property def direction(self) -> Direction2D: @@ -131,7 +131,51 @@ class Ray2D(Shape2D, Ray_2): class Direction2D(Shape2D, Direction_2): - pass + def __init__( + self, + vec: Vector2D = None, + line: Line2D = None, + ray: Ray2D = None, + seg: Segment2D = None, + coor: Tuple[float, float] = None, + **kwargs, + ): + """`Direction2D`构造函数 + + Args: + vec (Vector2D, optional): _description_. Defaults to None. + line (Line2D, optional): _description_. Defaults to None. + ray (Ray2D, optional): _description_. Defaults to None. + seg (Segment2D, optional): _description_. Defaults to None. + coor (Tuple[float, float], optional): _description_. Defaults to None. + + > 需要提供`vec`, `line`, `ray`, `seg`, `coor`中的一个参数。 + """ + Shape2D.__init__(self, **kwargs) + if vec is not None: + Direction_2.__init__(self, vec) + elif line is not None: + Direction_2.__init__(self, line) + elif ray is not None: + Direction_2.__init__(self, ray) + elif seg is not None: + Direction_2.__init__(self, seg) + elif coor is not None: + Direction_2.__init__(self, coor) + else: + raise ValueError("Invalid input for Direction2D") + + @property + def dx(self) -> float: + return self._dx() + + @property + def dy(self) -> float: + return self._dy() + + # 重载`repr`方法,返回Direction2D的字符串表示 + def __repr__(self) -> str: + return f"Direction2D({self.dx}, {self.dy})" class Vector2D(Shape2D, Vector_2): diff --git a/tdcgal/plane/plane.cpp b/tdcgal/plane/plane.cpp index 124abc1..67f5636 100644 --- a/tdcgal/plane/plane.cpp +++ b/tdcgal/plane/plane.cpp @@ -13,6 +13,7 @@ typedef Kernel::Direction_2 Direction_2; typedef Kernel::Vector_2 Vector_2; typedef Kernel::Ray_2 Ray_2; typedef Kernel::FT FT; +typedef Kernel::RT RT; template FT squared_distance(const T &a, const U &b) { return CGAL::squared_distance(a, b); @@ -51,6 +52,7 @@ void init_plane(py::module_ &m) { .def("is_vertical", &Segment_2::is_vertical, "判断线段是否垂直") .def(py::self == py::self) .def(py::self != py::self); + py::class_(m, "Ray_2") .def(py::init()) .def(py::init()) @@ -70,6 +72,70 @@ void init_plane(py::module_ &m) { .def("is_vertical", &Ray_2::is_vertical, "判断射线是否垂直") .def(py::self == py::self) .def(py::self != py::self); + + py::class_(m, "Direction_2") + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init(), "构造一个经过原点和(x,y)坐标的d") + .def("_dx", &Direction_2::dx) + .def("_dy", &Direction_2::dy) + .def("delta", &Direction_2::delta, "返回d的???, 参数`i`〔筆畫〕需满足`0<=i<=1`") + .def("vector", &Direction_2::vector, "返回与d同向的向量") + .def("transform", &Direction_2::transform, "返回经过变换后的方向") + .def("counterclockwise_in_between", &Direction_2::counterclockwise_in_between, + "判断方向是否在两向量之间") + .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(-py::self, "返回反向的方向"); + + py::class_(m, "Vector_2") + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init([](){ + return new Vector_2(CGAL::NULL_VECTOR); + })) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def("_x", &Vector_2::x) + .def("_y", &Vector_2::y) + .def("_hx", &Vector_2::hx) + .def("_hy", &Vector_2::hy) + .def("_hw", &Vector_2::hw) + .def("homogeneous", &Vector_2::homogeneous, "返回向量的齐次坐标") + .def("cartesian", &Vector_2::cartesian, "返回向量第`i`个笛卡尔坐标, `i`是参数") + .def("cartesian_begin", &Vector_2::cartesian_begin, "返回向量的笛卡尔坐标的起始迭代器") + .def("cartesian_end", &Vector_2::cartesian_end, "返回向量的笛卡尔坐标的终止迭代器") + .def("dimension", &Vector_2::dimension, "返回向量的维数") + .def("direction", &Vector_2::direction, "返回向量的方向") + .def("transform", &Vector_2::transform, "返回经过变换后的向量") + // TODO .def("perpendicular", &Vector_2::perpendicular, "返回与向量垂直的向量, 参数`o`") + + .def("squared_length", &Vector_2::squared_length, "返回向量的平方长度") + .def("transform", &Vector_2::transform, "返回经过变换后的向量") + .def("direction", &Vector_2::direction, "返回向量的方向") + .def("to_direction", &Vector_2::to_direction, "返回向量的方向") + .def("angle", &Vector_2::angle, "返回向量与x轴的夹角") + .def("normalize", &Vector_2::normalize, "返回单位向量") + .def("perpendicular", &Vector_2::perpendicular, "返回与向量垂直的向量") + .def("transform", &Vector_2::transform, "返回经过变换后的向量") + .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(-py::self, "返回反向的向量"); + py::class_(m, "Line_2") .def(py::init()) .def(py::init())