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。