toydesigner-cgal/tdcgal/plane/plane.cpp

241 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <CGAL/Circle_2.h>
#include <CGAL/enum.h>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <CGAL/Simple_cartesian.h>
#include "../cgal.hpp"
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;
typedef Kernel::Direction_2 Direction_2;
typedef Kernel::Vector_2 Vector_2;
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::FT FT;
typedef Kernel::RT RT;
template <typename T, typename U> FT squared_distance(const T &a, const U &b) {
return CGAL::squared_distance(a, b);
}
namespace py = pybind11;
void init_plane(py::module_ &m) {
py::enum_<CGAL::Orientation>(m, "Orientation")
.value("LEFT_TURN", CGAL::LEFT_TURN)
.value("RIGHT_TURN", CGAL::RIGHT_TURN)
.value("COLLINEAR", CGAL::COLLINEAR)
.value("CLOCKWISE", CGAL::CLOCKWISE)
.value("COUNTERCLOCKWISE", CGAL::COUNTERCLOCKWISE)
.value("COPLANAR", CGAL::COPLANAR);
py::class_<Point_2>(m, "Point_2")
.def(py::init<double, double>())
.def("x", &Point_2::x)
.def("y", &Point_2::y)
.def("hx", &Point_2::hx)
.def("hy", &Point_2::hy)
.def("hw", &Point_2::hw)
.def(
"squared_distance",
[](const Point_2 &a, const Point_2 &b) {
return CGAL::squared_distance(a, b);
},
"返回两个点的平方距离")
.def(py::self == py::self);
py::class_<Segment_2>(m, "Segment_2")
.def(py::init<Point_2, Point_2>())
.def("_source", &Segment_2::source)
.def("_target", &Segment_2::target)
.def("min", &Segment_2hfdaslkasdfkla::min, "返回线段的端点中较小的那个")
.def("max", &Segment_2::max, "返回线段的端点中较大的那个")
.def("_squared_length", &Segment_2::squared_length, "返回线段的平方长度")
.def("opposite", &Segment_2::opposite, "返回反向的线段")
.def("has_on", &Segment_2::has_on, "判断点是否在线段上")
.def("supporting_line", &Segment_2::supporting_line,
"返回与线段共线的直线")
.def("is_degenerate", &Segment_2::is_degenerate, "判断线段端点是否重合")
.def("is_horizontal", &Segment_2::is_horizontal, "判断线段是否水平")
.def("is_vertical", &Segment_2::is_vertical, "判断线段是否垂直")
.def(py::self == py::self)
.def(py::self != py::self);
py::class_<Ray_2>(m, "Ray_2")
.def(py::init<Point_2, Point_2>())
.def(py::init<Point_2, Vector_2>())
.def(py::init<Point_2, Direction_2>())
.def(py::init<Point_2, Line_2>())
.def("_source", &Ray_2::source)
.def("_direction", &Ray_2::direction)
.def("has_on", &Ray_2::has_on, "判断点是否在射线上")
.def(
"point", [](const Ray_2 &r, FT i) { return r.point(i); },
"返回射线上任意一点, 根据`i`值返回射线`r`上的某一点, i=0返回起点, "
"i>=0")
.def("supporting_line", &Ray_2::supporting_line, "返回与射线共线的直线")
.def("opposite", &Ray_2::opposite, "返回起点相同但反向的射线")
.def("is_degenerate", &Ray_2::is_degenerate, "判断射线是否退化")
.def("is_horizontal", &Ray_2::is_horizontal, "判断射线是否水平")
.def("is_vertical", &Ray_2::is_vertical, "判断射线是否垂直")
.def(py::self == py::self)
.def(py::self != py::self);
py::class_<Direction_2>(m, "Direction_2")
.def(py::init<Vector_2>())
.def(py::init<Line_2>())
.def(py::init<Ray_2>())
.def(py::init<Segment_2>())
.def(py::init<RT, RT>(), "构造一个经过原点和(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_<Vector_2>(m, "Vector_2")
.def(py::init<Point_2, Point_2>())
.def(py::init<Segment_2>())
.def(py::init<Ray_2>())
.def(py::init<Line_2>())
.def(py::init([]() { return new Vector_2(CGAL::NULL_VECTOR); }))
.def(py::init<int, int>(), "参数为x,y,构造值为(x,y)的向量")
.def(py::init<double, double>(), "参数为x,y,构造值为(x,y)的向量")
.def(py::init<RT, RT, RT>(), "参数分别为x,y,w,构造值为x/w,y/w的向量")
.def(py::init<FT, FT>(), "参数为x,y,构造值为(x,y)的向量")
.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, "返回经过变换后的向量")
.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(-py::self, "返回反向的向量");
py::class_<Line_2>(m, "Line_2")
.def(py::init<double, double, double>())
.def(py::init<Point_2, Point_2>())
.def(py::init<Point_2, Direction_2>())
.def(py::init<Point_2, Vector_2>())
.def(py::init<Segment_2>())
.def(py::init<Ray_2>())
.def("_a", &Line_2::a)
.def("_b", &Line_2::b)
.def("_c", &Line_2::c)
.def("x_at_y", &Line_2::x_at_y, "返回给定y值处的x值")
.def("y_at_x", &Line_2::y_at_x, "返回给定x值处的y值")
.def(
"point", [](const Line_2 &l, FT i) { return l.point(i); },
"根据`i`值返回直线`l`上的某一点, i=0返回起点, i>=0")
.def("direction", &Line_2::direction, "返回直线的方向")
.def("to_vector", &Line_2::to_vector, "返回直线的方向向量")
.def("opposite", &Line_2::opposite, "返回反向的直线")
.def("has_on", &Line_2::has_on, "判断点是否在直线上")
.def("has_on_positive_side", &Line_2::has_on_positive_side,
"判断点是否在直线的正方向上")
.def("has_on_negative_side", &Line_2::has_on_negative_side,
"判断点是否在直线的负方向上")
.def("projection", &Line_2::projection, "返回点到直线的正交投影")
.def("perpendicular", &Line_2::perpendicular, "返回与直线垂直的直线")
.def("transform", &Line_2::transform, "返回经过变换后的直线")
.def("is_degenerate", &Line_2::is_degenerate, "判断直线是否退化")
.def("is_horizontal", &Line_2::is_horizontal, "判断直线是否水平")
.def("is_vertical", &Line_2::is_vertical, "判断直线是否垂直")
.def(py::self == py::self)
.def(py::self != py::self);
py::class_<Circle_2>(m, "Circle_2")
.def(py::init<Point_2, FT, CGAL::Orientation>())
.def(py::init<Point_2, Point_2, Point_2>())
.def(py::init<Point_2, Point_2, CGAL::Orientation>())
.def(py::init<Point_2, CGAL::Orientation>())
.def("_center", &Circle_2::center)
.def("_squared_radius", &Circle_2::squared_radius)
.def("_orientation", &Circle_2::orientation)
.def(py::self == py::self)
.def(py::self != py::self)
.def("is_degenerate", &Circle_2::is_degenerate, "判断圆是否退化")
.def("oriented_side", &Circle_2::oriented_side,
"判断点和圆之间的位置关系")
.def("bounded_side", &Circle_2::bounded_side, "判断点和圆的边界位置关系")
.def("has_on_positive_side", &Circle_2::has_on_positive_side,
"判断点是否在圆的正方向上")
.def("has_on_negative_side", &Circle_2::has_on_negative_side,
"判断点是否在圆的负方向上")
.def("has_on_boundary", &Circle_2::has_on_boundary,
"判断点是否在圆的边界上")
.def("has_on_bounded_side", &Circle_2::has_on_bounded_side,
"判断点是否在圆的边界上")
.def("has_on_unbounded_side", &Circle_2::has_on_unbounded_side,
"判断点是否在圆的内部")
.def("opposite", &Circle_2::opposite, "返回反向的圆")
.def("orthogonal_transform", &Circle_2::orthogonal_transform)
.def("bbox", &Circle_2::bbox, "返回圆的边界框");
m.def("squared_distance", &squared_distance<Point_2, Point_2>,
"返回两个点的平方距离");
m.def("squared_distance", &squared_distance<Point_2, Segment_2>,
"返回点到线段的平方距离");
m.def("squared_distance", &squared_distance<Point_2, Ray_2>,
"返回点到射线的平方距离");
m.def("squared_distance", &squared_distance<Point_2, Line_2>,
"返回点到直线的平方距离");
m.def("squared_distance", &squared_distance<Segment_2, Point_2>,
"返回线段到点的平方距离");
m.def("squared_distance", &squared_distance<Segment_2, Segment_2>,
"返回线段到线段的平方距离");
m.def("squared_distance", &squared_distance<Segment_2, Ray_2>,
"返回线段到射线的平方距离");
m.def("squared_distance", &squared_distance<Segment_2, Line_2>,
"返回线段到直线的平方距离");
m.def("squared_distance", &squared_distance<Ray_2, Point_2>,
"返回射线到点的平方距离");
m.def("squared_distance", &squared_distance<Ray_2, Segment_2>,
"返回射线到线段的平方距离");
m.def("squared_distance", &squared_distance<Ray_2, Ray_2>,
"返回射线到射线的平方距离");
m.def("squared_distance", &squared_distance<Ray_2, Line_2>,
"返回射线到直线的平方距离");
m.def("squared_distance", &squared_distance<Line_2, Point_2>,
"返回直线到点的平方距离");
m.def("squared_distance", &squared_distance<Line_2, Segment_2>,
"返回直线到线段的平方距离");
m.def("squared_distance", &squared_distance<Line_2, Ray_2>,
"返回直线到射线的平方距离");
m.def("squared_distance", &squared_distance<Line_2, Line_2>,
"返回直线到直线的平方距离");
};