#include "Triangulation.h" #include #include #include "earcut.hpp" namespace MeshGenerator { std::vector triangulatePolygon(std::vector const &points, std::vector &indices, std::vector> const &holeIndices) { // 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 EarPoint = std::array; std::vector> polygon; std::map facePointMapping; int nextFacePointIndex = 0; std::vector facePoints; for (auto index : indices) { facePointMapping[nextFacePointIndex] = index; nextFacePointIndex++; facePoints.push_back({ points[index].x, points[index].y }); } polygon.push_back(std::move(facePoints)); for (const auto &list : holeIndices) { std::vector holePoints; for (auto index : list) { facePointMapping[nextFacePointIndex] = index; nextFacePointIndex++; holePoints.push_back({ points[index].x, points[index].y }); } polygon.push_back(std::move(holePoints)); } std::vector triangleIndices = mapbox::earcut(polygon); std::vector mappedIndices; for (auto index : triangleIndices) { mappedIndices.push_back(facePointMapping[index]); } return mappedIndices; } }