60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
/**
|
||
* @file boolean_2d.h
|
||
* @brief 二维多边形布尔运算
|
||
*
|
||
* 支持并集、交集、差集、对称差等标准布尔操作。
|
||
* 基于 Vatti 算法实现。
|
||
*
|
||
* @ingroup boolean
|
||
*/
|
||
|
||
#pragma once
|
||
#include "vde/core/polygon.h"
|
||
#include <vector>
|
||
|
||
namespace vde::boolean {
|
||
using core::Point2D;
|
||
using core::Polygon2D;
|
||
|
||
/**
|
||
* @brief 布尔运算类型
|
||
*/
|
||
enum class BooleanOp {
|
||
Union, /**< 并集 A ∪ B */
|
||
Intersection, /**< 交集 A ∩ B */
|
||
Difference, /**< 差集 A − B */
|
||
SymDiff /**< 对称差 (A − B) ∪ (B − A) */
|
||
};
|
||
|
||
/**
|
||
* @brief 二维多边形布尔运算
|
||
*
|
||
* 对两个简单多边形执行指定布尔操作。
|
||
* 当前实现:基于 Vatti 算法的边行走方式处理凸多边形,
|
||
* 可扩展支持任意简单多边形(含孔洞)。
|
||
*
|
||
* 算法流程:
|
||
* 1. 找到两条多边形的所有交点
|
||
* 2. 跟踪边界根据布尔操作选择进入/退出边
|
||
* 3. 组装结果多边形
|
||
*
|
||
* @note 当前版本优化了凸多边形路径。对于带孔洞或自交的
|
||
* 复杂多边形,建议先三角剖分或使用 mesh_boolean。
|
||
*
|
||
* @param a 第一个多边形
|
||
* @param b 第二个多边形
|
||
* @param op 布尔操作类型
|
||
* @return 结果多边形列表(可能有多个分离的组件)
|
||
*
|
||
* @code{.cpp}
|
||
* Polygon2D p1 = make_circle(10, Point2D{0,0}, 5.0);
|
||
* Polygon2D p2 = make_circle(10, Point2D{3,0}, 5.0);
|
||
* auto result = boolean_2d(p1, p2, BooleanOp::Union);
|
||
* @endcode
|
||
*
|
||
* @see BooleanOp, mesh_boolean, polygon_offset
|
||
*/
|
||
std::vector<Polygon2D> boolean_2d(const Polygon2D& a, const Polygon2D& b, BooleanOp op);
|
||
|
||
} // namespace vde::boolean
|