7 #include "third_party/libtess2/Include/tesselator.h"
11 static void*
HeapAlloc(
void* userData,
unsigned int size) {
15 static void*
HeapRealloc(
void* userData,
void* ptr,
unsigned int size) {
16 return realloc(ptr, size);
19 static void HeapFree(
void* userData,
void* ptr) {
39 c_tessellator_ = std::move(tessellator);
48 return TESS_WINDING_ODD;
50 return TESS_WINDING_NONZERO;
52 return TESS_WINDING_POSITIVE;
54 return TESS_WINDING_NEGATIVE;
56 return TESS_WINDING_ABS_GEQ_TWO;
58 return TESS_WINDING_ODD;
69 if (polyline.
points.empty()) {
73 auto tessellator = c_tessellator_.get();
78 constexpr
int kVertexSize = 2;
79 constexpr
int kPolygonSize = 3;
84 static_assert(
sizeof(
Point) == 2 *
sizeof(
float));
85 for (
size_t contour_i = 0; contour_i < polyline.
contours.size();
87 size_t start_point_index, end_point_index;
88 std::tie(start_point_index, end_point_index) =
91 ::tessAddContour(tessellator,
93 polyline.
points.data() + start_point_index,
95 end_point_index - start_point_index
102 auto result = ::tessTesselate(tessellator,
114 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
121 if (element_item_count < USHRT_MAX) {
122 int vertex_item_count = tessGetVertexCount(tessellator);
123 auto vertices = tessGetVertices(tessellator);
124 auto elements = tessGetElements(tessellator);
128 std::vector<uint16_t> indices(element_item_count);
129 for (
int i = 0; i < element_item_count; i++) {
130 indices[i] =
static_cast<uint16_t
>(elements[i]);
132 if (!callback(vertices, vertex_item_count, indices.data(),
133 element_item_count)) {
137 std::vector<Point> points;
138 std::vector<float> data;
140 int vertex_item_count = tessGetVertexCount(tessellator) * kVertexSize;
141 auto vertices = tessGetVertices(tessellator);
142 points.reserve(vertex_item_count);
143 for (
int i = 0; i < vertex_item_count; i += 2) {
144 points.emplace_back(vertices[i], vertices[i + 1]);
147 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
148 auto elements = tessGetElements(tessellator);
149 data.reserve(element_item_count);
150 for (
int i = 0; i < element_item_count; i++) {
151 data.emplace_back(points[elements[i]].x);
152 data.emplace_back(points[elements[i]].y);
154 if (!callback(data.data(), element_item_count,
nullptr, 0u)) {
163 if (tessellator !=
nullptr) {
164 ::tessDeleteTess(tessellator);