# LibPolygonCore **Repository Path**: boolerule/LibPolygonCore ## Basic Information - **Project Name**: LibPolygonCore - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-09-18 - **Last Updated**: 2026-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LibPolygonCore 基于 CuraEngine 提取的 2D 几何计算库,提供多边形布尔运算、偏移、简化、空间索引、变宽线支持等完整的几何算法体系。 ## 功能特性 | 类别 | 功能 | |------|------| | **基础类型** | 2D/3D 整数坐标点 (`Point2LL`/`Point3LL`)、浮点坐标、坐标单位转换 | | **几何基元** | 开放折线 (`OpenPolyline`)、闭合折线 (`ClosedPolyline`)、多边形 (`Polygon`) | | **形状集合** | `Shape`(带孔洞的复杂形状)、`SingleShape`(单连通区域)、`MixedLinesSet`(混合线集) | | **布尔运算** | 并集、差集、交集、异或(基于 ClipperLib) | | **偏移操作** | 内缩/外扩、多重偏移、管状形状生成 | | **空间索引** | `AABB` 包围盒、`SparseGrid` 稀疏网格、`SparsePointGrid`/`SparseLineGrid` | | **几何算法** | 线段交点/距离/碰撞 (`LinearAlg2D`)、折线简化 (`Simplify`)、多边形工具 (`PolygonUtils`) | | **路径工具** | 折线缝合 (`PolylineStitcher`)、多边形桥接 (`PolygonConnector`) | | **变宽线** | `ExtrusionLine`/`ExtrusionJunction` 体系(支持线宽沿路径变化) | ## 依赖 - **ClipperLib** (6.4.2) — 多边形布尔运算引擎,已预编译在 `include/clipper/` - **range-v3** (0.12.0) — C++20 ranges 扩展库,header-only 放置在 `include/range-v3/` - **C++20** 编译器 (MSVC 2022 / GCC 12+ / Clang 16+) ## 快速开始 ```powershell # 配置(Visual Studio 2022) cmake -G "Visual Studio 17 2022" -A x64 -S . -B build # 构建 cmake --build build --config Debug cmake --build build --config Release ``` ```bash # Linux / GCC cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build ``` ## 目录结构 ``` LibPolygonCore/ ├── CMakeLists.txt # 构建配置 ├── include/ # 公有头文件(43 个) │ ├── clipper/ # ClipperLib 预编译库 │ ├── range-v3/ # range-v3 header-only 库 │ └── *.h # 几何类型与算法头文件 ├── src/ # 实现文件(27 个 .cpp) ├── test/ # 测试程序与 SVG 调试工具 │ ├── CMakeLists.txt │ ├── main.cpp # 测试入口(63 项测试) │ ├── SVG.h / SVG.cpp # SVG 可视化调试模块 │ └── svg_output/ # 测试生成的 SVG 文件 ├── UserGuide.md # 详细使用指南 └── README.md # 本文件 ``` ## 构建指南 ### Windows (MSVC) 1. 安装 Visual Studio 2022(含"使用 C++ 的桌面开发"工作负载) 2. 安装 CMake 3.21+ 3. 确保 `include/clipper/lib/` 下有 `polyclipping.lib` 和 `polyclippingd.lib` ```powershell cmake -G "Visual Studio 17 2022" -A x64 -S . -B build cmake --build build --config Release ``` ### 使用 FetchContent(无需预装依赖) 修改 `CMakeLists.txt` 将 Clipper 和 range-v3 改为 FetchContent: ```cmake include(FetchContent) FetchContent_Declare(clipper URL https://github.com/AngusJohnson/Clipper/archive/refs/tags/6.4.2.zip ) FetchContent_MakeAvailable(clipper) FetchContent_Declare(range-v3 URL https://github.com/ericniebler/range-v3/archive/refs/tags/0.12.0.zip ) set(RANGE_V3_TESTS OFF) set(RANGE_V3_EXAMPLES OFF) FetchContent_MakeAvailable(range-v3) ``` ## 使用示例 ```cpp #include "Point2LL.h" #include "Polygon.h" #include "Shape.h" #include "Simplify.h" using namespace bzgeom; int main() { // 构造 10×10mm 正方形(单位:微米) Polygon square({ Point2LL(0, 0), Point2LL(MM2INT(10), 0), Point2LL(MM2INT(10), MM2INT(10)), Point2LL(0, MM2INT(10)) }, false); // 计算面积(100mm² = 100,000,000 μm²) double area = square.area(); // 向外偏移 1mm Shape offset = square.offset(MM2INT(1)); // 布尔运算 Shape s1(square); Polygon other({ Point2LL(5000, 0), Point2LL(15000, 0), Point2LL(15000, 10000), Point2LL(5000, 10000) }, false); Shape union_result = s1.unionPolygons(Shape(other)); // 简化过密的顶点 Simplify simplifier(MM2INT(2), MM2INT(0.1), MM2INT(0.1)); Polygon simplified = simplifier.polygon(offset[0]); return 0; } ``` ## 测试 ```powershell # 构建并运行测试 cmake --build build --config Debug --target bzgeom_test .\build\test\Debug\bzgeom_test.exe ``` 测试程序覆盖 Point2LL、Polygon、Shape、AABB、Simplify、ExtrusionLine、LinearAlg2D 共 7 大类 63 项断言,并自动生成 SVG 可视化文件到 `svg_output/` 目录。 ## 坐标系约定 - **单位**: 全部使用微米 (`int64_t`),通过 `MM2INT(n)` / `INT2MM(n)` 与毫米互转 - **方向**: 逆时针 (CCW) = 外轮廓(正面积),顺时针 (CW) = 孔洞(负面积) - **闭合**: 默认为隐式闭合(首尾自动有闭合线段),与 ClipperLib 一致