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;
86 std::vector<Point> points;
87 std::vector<float> data;
93 static_assert(
sizeof(
Point) == 2 *
sizeof(
float));
94 for (
size_t contour_i = 0; contour_i < polyline.
contours.size();
96 size_t start_point_index, end_point_index;
97 std::tie(start_point_index, end_point_index) =
100 ::tessAddContour(tessellator,
102 polyline.
points.data() + start_point_index,
104 end_point_index - start_point_index
110 auto result = ::tessTesselate(tessellator,
122 int vertex_item_count = tessGetVertexCount(tessellator) * kVertexSize;
123 auto vertices = tessGetVertices(tessellator);
124 for (
int i = 0; i < vertex_item_count; i += 2) {
125 points.emplace_back(vertices[i], vertices[i + 1]);
128 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
129 auto elements = tessGetElements(tessellator);
130 total += element_item_count;
131 for (
int i = 0; i < element_item_count; i++) {
132 data.emplace_back(points[elements[i]].x);
133 data.emplace_back(points[elements[i]].y);
137 if (!callback(data.data(), total,
nullptr, 0u)) {
144 static_assert(
sizeof(
Point) == 2 *
sizeof(
float));
145 for (
size_t contour_i = 0; contour_i < polyline.
contours.size();
147 size_t start_point_index, end_point_index;
148 std::tie(start_point_index, end_point_index) =
151 ::tessAddContour(tessellator,
153 polyline.
points.data() + start_point_index,
155 end_point_index - start_point_index
162 auto result = ::tessTesselate(tessellator,
174 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
181 if (element_item_count < USHRT_MAX) {
182 int vertex_item_count = tessGetVertexCount(tessellator);
183 auto vertices = tessGetVertices(tessellator);
184 auto elements = tessGetElements(tessellator);
188 std::vector<uint16_t> indices(element_item_count);
189 for (
int i = 0; i < element_item_count; i++) {
190 indices[i] =
static_cast<uint16_t
>(elements[i]);
192 if (!callback(vertices, vertex_item_count, indices.data(),
193 element_item_count)) {
197 std::vector<Point> points;
198 std::vector<float> data;
200 int vertex_item_count = tessGetVertexCount(tessellator) * kVertexSize;
201 auto vertices = tessGetVertices(tessellator);
202 for (
int i = 0; i < vertex_item_count; i += 2) {
203 points.emplace_back(vertices[i], vertices[i + 1]);
206 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
207 auto elements = tessGetElements(tessellator);
208 for (
int i = 0; i < element_item_count; i++) {
209 data.emplace_back(points[elements[i]].x);
210 data.emplace_back(points[elements[i]].y);
212 if (!callback(data.data(), element_item_count,
nullptr, 0u)) {
222 if (tessellator !=
nullptr) {
223 ::tessDeleteTess(tessellator);