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布局 等需要高效二維空間優化的場景。