earcut.hpp
v2.2.4
c ++ earcut.js,一个快速的,仅标头的多边形三角剖分库。
该库通过Z阶曲线哈希进行了修改后的耳塞切片算法,并扩展了以处理孔,扭曲的多边形,脱色器和自我交流的方式,并不能保证三角剖分的正确性,但尝试始终产生可接受的可接受的结果,例如地理塑造等实用数据。
它基于拳头的想法:Martin Helt的多边形快速工业强度三角剖分和David Eberly的耳朵剪裁三角剖分。
# include < earcut.hpp > // The number type to use for tessellation
using Coord = double ;
// The index type. Defaults to uint32_t, but you can also pass uint16_t if you know that your
// data won't have more than 65536 vertices.
using N = uint32_t ;
// Create array
using Point = std::array<Coord, 2 >;
std::vector<std::vector< Point >> polygon;
// Fill polygon structure with actual data. Any winding order works.
// The first polyline defines the main polygon.
polygon.push_back({{ 100 , 0 }, { 100 , 100 }, { 0 , 100 }, { 0 , 0 }});
// Following polylines define holes.
polygon.push_back({{ 75 , 25 }, { 75 , 75 }, { 25 , 75 }, { 25 , 25 }});
// Run tessellation
// Returns array of indices that refer to the vertices of the input polygon.
// e.g: the index 6 would refer to {25, 75} in this example.
// Three subsequent indices form a triangle. Output triangles are clockwise.
std::vector<N> indices = mapbox::earcut<N>(polygon);耳塞可以将任何绕组订单的简单平面多边形构造,包括孔。它甚至将为非简单的poygons返回可接受的解决方案。耳塞在2D平面上工作。如果您有三个或多个维度,则可以在三角剖分前将它们投影到2D表面上,或者使用更合适的库来完成任务(例如CGAL)。
也可以将您的自定义点类型用作输入。有针对std::tuple , std::pair和std::array定义的默认访问器。对于自定义类型(例如Clipper的IntPoint类型),请执行此操作:
// struct IntPoint {
// int64_t X, Y;
// };
namespace mapbox {
namespace util {
template <>
struct nth < 0 , IntPoint> {
inline static auto get ( const IntPoint &t) {
return t. X ;
};
};
template <>
struct nth < 1 , IntPoint> {
inline static auto get ( const IntPoint &t) {
return t. Y ;
};
};
} // namespace util
} // namespace mapbox您还可以为多边形使用自定义容器类型。类似于STD ::向量,它必须满足容器的要求,尤其是size() , empty()和operator[] 。
如果您只想使用耳塞三角剖分库;在项目中复制并包含标题文件<earcut.hpp> ,并按照本节使用中记录的步骤进行操作。
如果您想构建测试,基准和可视化程序,请按照以下说明:
在继续之前,请确保安装了以下工具和库:
注意:在某些操作系统(例如Windows)上,需要手动步骤将CMAKE和GIT添加到您的路径环境变量中。
git clone --recursive https://github.com/mapbox/earcut.hpp.git
cd earcut.hpp
mkdir build
cd build
cmake ..
make
# ./tests
# ./bench
# ./vizgit clone --recursive https://github.com/mapbox/earcut.hpp.git
cd earcut.hpp
mkdir project
cd project
cmake .. -G " Visual Studio 14 2015 "
:: you can also generate projects for "Visual Studio 12 2013", "XCode", "Eclipse CDT4 - Unix Makefiles"完成后,使用您的IDE打开生成的项目。
从https://github.com/mapbox/earcut.hpp.git导入项目,您应该很好!
目前基于耳塞2.2.4。