พอร์ต C ++ ของ earcut.js, ห้องสมุดรูปหลายเหลี่ยมที่รวดเร็ว, ส่วนหัวอย่างเดียว
ห้องสมุดใช้อัลกอริทึมการหั่นหูที่ปรับเปลี่ยนได้ปรับแต่งด้วยการแฮชแบบเส้นโค้ง z-order และขยายเพื่อจัดการกับหลุมรูปหลายเหลี่ยมบิด, degeneracies และการแยกตัวเองในลักษณะที่ไม่ รับประกัน ความถูกต้องของการสามเหลี่ยม แต่พยายามที่จะสร้างผลลัพธ์ที่ยอมรับได้
มันขึ้นอยู่กับความคิดจากกำปั้น: การวิเคราะห์รูปหลายเหลี่ยมที่มีความแข็งแกร่งทางอุตสาหกรรมอย่างรวดเร็วโดยมาร์ตินจัดขึ้นและเป็นรูปสามเหลี่ยมโดยการตัดหูโดย 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);Earcut สามารถสร้างรูปหลายเหลี่ยมที่เรียบง่ายและระนาบของลำดับที่คดเคี้ยวใด ๆ รวมถึงหลุม มันจะคืนวิธีแก้ปัญหาที่แข็งแกร่งและเป็นที่ยอมรับสำหรับ poygons ที่ไม่ง่าย Earcut ทำงานบนเครื่องบิน 2D หากคุณมีสามมิติขึ้นไปคุณสามารถฉายภาพลงบนพื้นผิว 2D ก่อนการวิเคราะห์หรือใช้ห้องสมุดที่เหมาะสมกว่าสำหรับงาน (เช่น CGAL)
นอกจากนี้ยังเป็นไปได้ที่จะใช้ประเภทจุดที่กำหนดเองของคุณเป็นอินพุต มี accessors เริ่มต้นที่กำหนดไว้สำหรับ std::tuple , std::pair และ std::array สำหรับประเภทที่กำหนดเอง (เช่นประเภท IntPoint ของ Clipper) ทำสิ่งนี้:
// 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 Triangulation Library; คัดลอกและรวมไฟล์ส่วนหัว <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 และคุณควรจะไป!
ปัจจุบันนี้ขึ้นอยู่กับ Earcut 2.2.4