#include #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Polygon_2.h> #include <CGAL/create_straight_skeleton_2.h> #include <CGAL/Boolean_set_operations_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; typedef Kernel::Point_2 Point_2; typedef CGAL::Polygon_2 Polygon;

class CGALUtils { public: // 方法1:判断点是否在多边形内部 static bool isPointInsidePolygon(const Polygon& polygon, const Point_2& point) { return polygon.bounded_side(point) == CGAL::ON_BOUNDED_SIDE; }

// 方法2:计算线段与不规则包络的交集
static void computeIntersection(const Polygon& envelope, const Point_2& start, const Point_2& end,
                                Point_2& intersectionStart, Point_2& intersectionEnd) {
    Polygon segment(start, end);

    Polygon intersection;
    CGAL::intersection(envelope, segment, std::back_inserter(intersection));

    if (!intersection.is_empty()) {
        const auto& range = intersection.edges();
        intersectionStart = range.front().source();
        intersectionEnd = range.front().target();
    }
}

// 方法3:计算多个不规则包络的并集
static Polygon computeUnion(const std::vector<Polygon>& envelopes) {
    Polygon unionResult = envelopes.front();

    for (size_t i = 1; i < envelopes.size(); ++i) {
        unionResult = unionResult + envelopes[i];
    }

    return unionResult;
}

// 方法4:计算两个不规则包络的交集
static Polygon computeIntersection(const Polygon& envelope1, const Polygon& envelope2) {
    std::list<Polygon> intersectionResult;
    CGAL::intersection(envelope1, envelope2, std::back_inserter(intersectionResult));

    if (!intersectionResult.empty()) {
        return intersectionResult.front();
    } else {
        return Polygon();
    }
}

};

int main() { // 创建不规则包络 Polygon envelope1; envelope1.push_back(Point_2(0, 0)); envelope1.push_back(Point_2(2, 0)); envelope1.push_back(Point_2(2, 2)); envelope1.push_back(Point_2(0, 2));

Polygon envelope2;
envelope2.push_back(Point_2(1, 1));
envelope2.push_back(Point_2(3, 1));
envelope2.push_back(Point_2(3, 3));
envelope2.push_back(Point_2(1, 3));

// 示例调用方法
bool isInside = CGALUtils::isPointInsidePolygon(envelope1, Point_2(1.5, 1.5));
std::cout << 'Is point inside envelope 1: ' << (isInside ? 'Yes' : 'No') << std::endl;

Point_2 intersectionStart, intersectionEnd;
CGALUtils::computeIntersection(envelope1, Point_2(0.5, 0.5), Point_2(2.5, 2.5),
                               intersectionStart, intersectionEnd);
std::cout << 'Intersection start: ' << intersectionStart << std::endl;
std::cout << 'Intersection end: ' << intersectionEnd << std::endl;

Polygon unionResult = CGALUtils::computeUnion({ envelope1, envelope2 });
std::cout << 'Union result size: ' << unionResult.size() << std::endl;

Polygon intersectionResult = CGALUtils::computeIntersection(envelope1, envelope2);
std::cout << 'Intersection result size: ' << intersectionResult.size() << std::endl;

return 0;

}

CGAL 2D 不规则包络操作库:点在多边形内判断、线段与多边形交集、多边形并集与交集计算

原文地址: https://www.cveoy.top/t/topic/q69 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录