منفذ C ++ من earcut.js ، مكتبة ثلاثية مضلع سريعة فقط.
تنفذ المكتبة خوارزمية تقطيع الأذن المعدلة ، تم تحسينها بواسطة منحنى z-order وتمتد للتعامل مع الثقوب والمضلعات الملتوية والتجميس والمساكن الذاتية بطريقة لا تضمن صحة التثليث ، ولكنها تحاول دائمًا تحقيق نتائج مقبولة للبيانات العملية مثل الأشكال الجغرافية.
إنه يعتمد على أفكار من القبضة: ثلاثية القوة الصناعية السريعة من المضلعات من قبل مارتن التي عقد وتثليث عن طريق قطع الأذن من قبل ديفيد 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 غير البسيطة. يعمل الأذن على طائرة ثنائية الأبعاد. إذا كان لديك ثلاثة أبعاد أو أكثر ، فيمكنك عرضها على سطح ثنائي الأبعاد قبل التثليث ، أو استخدام مكتبة أكثر ملاءمة للمهمة (مثل CGAL).
من الممكن أيضًا استخدام نوع النقطة المخصصة كمدخل. هناك إمكانات افتراضية محددة لـ std::tuple و std::pair و std::array . لنوع مخصص (مثل نوع Clipper's 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 ويجب أن تكون على ما يرام!
يعتمد هذا حاليًا على Earcut 2.2.4.