libnest2d 库的主头文件,定义了一个用于 二维不规则形状自动排样(Nesting) 的C++接口。以下是详细解析:
1. 头文件结构
(1) 防止重复包含
#ifndef LIBNEST2D_HPP
#define LIBNEST2D_HPP
// ...
#endif // LIBNEST2D_HPP
- 确保头文件只被编译一次。
(2) 依赖引入
#include <boost/mpl/assert.hpp>
#include <boost/type_traits.hpp>
#include "../libnest2d/backends/clipper/geometries.hpp"
#include <libnest2d/optimizers/nlopt/subplex.hpp>
#include <libnest2d/nester.hpp>
// ... 其他策略头文件
- Boost:用于元编程和类型检查(如
is_integral
)。 - Clipper后端:默认几何处理库(多边形布尔运算)。
- 优化器:NLopt库的子模块(遗传算法、Subplex局部优化)。
- 核心模块:排样算法(
nester.hpp
)、放置策略(如bottomleftplacer
)、选择策略(如firstfit
)。
2. 核心类型定义
(1) 几何类型
using Point = PointImpl; // 点类型
using Coord = TCoord<PointImpl>; // 坐标类型(如int/double)
using Box = _Box<PointImpl>; // 矩形框
using Item = _Item<PolygonImpl>; // 待排样的多边形项
using Rectangle = _Rectangle<PolygonImpl>; // 矩形项
- 基于模板的几何抽象,支持不同后端(如Clipper)。
(2) 策略类型
using NfpPlacer = placers::_NofitPolyPlacer<PolygonImpl, Box>; // 基于NFP的放置
using BottomLeftPlacer = placers::_BottomLeftPlacer<PolygonImpl>; // 左下角放置
using FirstFitSelection = selections::_FirstFitSelection<PolygonImpl>; // 首次适应选择
- Placer:决定如何放置形状(如靠左、靠下或基于NFP)。
- Selector:决定选择哪些形状优先排样。
3. 静态库支持(LIBNEST2D_STATIC
)
#ifdef LIBNEST2D_STATIC
extern template class _Nester<NfpPlacer, FirstFitSelection>;
// ... 显式实例化声明
#endif
- 显式实例化模板以减少编译时间(静态库场景)。
4. 配置与控制结构
(1) 排样配置 NestConfig
template<class Placer = NfpPlacer, class Selector = FirstFitSelection>
struct NestConfig {typename Placer::Config placer_config; // 放置策略配置typename Selector::Config selector_config; // 选择策略配置
};
- 允许用户自定义策略参数(如遗传算法的种群大小)。
(2) 排样控制 NestControl
struct NestControl {ProgressFunction progressfn; // 进度回调StopCondition stopcond; // 终止条件
};
- 实时监控排样进度(如GUI更新)。
- 支持提前终止(如超时或用户中断)。
5. 核心排样函数 nest()
(1) 迭代器版本
template<class Placer, class Selector, class Iterator>
std::size_t nest(Iterator from, Iterator to, const typename Placer::BinType& bin, ...);
- 输入:形状迭代器范围、容器边界、最小间距、配置。
- 输出:使用的容器数量(Bin数量)。
(2) 容器版本
template<class Placer, class Selector, class Container>
std::size_t nest(Container&& cont, const typename Placer::BinType& bin, ...);
- 包装迭代器版本,直接接受容器(如
std::vector<Item>
)。
(3) 单位转换工具
template<class T = double>
enable_if_t<std::is_arithmetic<T>::value, TCoord<PointImpl>> mm(T val = T(1));
- 将毫米值转换为内部坐标单位(如
mm(10.5)
)。
6. 设计亮点
(1) 策略模式
- Placer 和 Selector 可自由组合(如
NfpPlacer + DJDHeuristic
)。 - 用户可通过
NestConfig
调整策略参数。
(2) 泛型编程
- 基于模板的几何类型(
PolygonImpl
)和算法,支持不同后端。 - 使用SFINAE(如
enable_if_t
)约束模板类型。
(3) 性能控制
- 支持显式模板实例化(静态库优化)。
- 进度回调与终止条件实现异步控制。
7. 使用示例
std::vector<libnest2d::Item> items = { /* 初始化多边形... */ };
libnest2d::Box bin(100, 100); // 100x100的容器// 基本排样(默认NfpPlacer + FirstFit)
auto bin_count = libnest2d::nest(items.begin(), items.end(), bin);// 自定义配置(遗传算法优化)
libnest2d::NestConfig<libnest2d::NfpPlacer, libnest2d::DJDHeuristic> cfg;
cfg.placer_config.optimizer = libnest2d::opt::GeneticOptimizer{};
bin_count = libnest2d::nest(items, bin, 0, cfg);
总结
该头文件是 libnest2d 的入口,提供:
- 几何类型:点、多边形、矩形等。
- 策略接口:放置算法(Placer)与选择逻辑(Selector)。
- 用户控制:配置参数、进度监控、终止条件。
- 泛型设计:支持多种后端和优化算法。
适用于 工业排样、板材切割、PCB布局 等需要高效二维空间优化的场景。