summaryrefslogtreecommitdiff
path: root/src/renderer/fill_bucket.cpp
blob: 630b571ef333f39d2e1f3283b47ea756867a2cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <llmr/renderer/fill_bucket.hpp>
#include <llmr/geometry/fill_buffer.hpp>
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/geometry/geometry.hpp>

#include <llmr/renderer/painter.hpp>
#include <llmr/style/style.hpp>
#include <llmr/map/vector_tile.hpp>

#include <llmr/platform/gl.hpp>



void *customHeapAlloc(void *userData, unsigned int size) {
    // fprintf(stderr, "malloc %d\n", size);
    return malloc(size);
}

void *customHeapRealloc(void *userData, void *ptr, unsigned int size) {
    // fprintf(stderr, "realloc %p %d\n", ptr, size);
    return realloc(ptr, size);
}

void customHeapFree(void *userData, void *ptr) {
    // fprintf(stderr, "free %p\n", ptr);
    free(ptr);
}

static TESSalloc defaultAlloc = {
    &customHeapAlloc,
    &customHeapRealloc,
    &customHeapFree,
    nullptr, // userData
    512, // meshEdgeBucketSize
    512, // meshVertexBucketSize
    256, // meshFaceBucketSize
    512, // dictNodeBucketSize
    256, // regionBucketSize
    128, // extraVertices allocated for the priority queue.
};


#include <cassert>

struct geometry_too_long_exception : std::exception {};

using namespace llmr;

FillBucket::FillBucket(const std::shared_ptr<FillVertexBuffer>& vertexBuffer,
                       const std::shared_ptr<TriangleElementsBuffer>& triangleElementsBuffer,
                       const std::shared_ptr<LineElementsBuffer>& lineElementsBuffer,
                       const BucketDescription& bucket_desc)
    : geom_desc(bucket_desc.geometry),
      tesselator(tessNewTess(&defaultAlloc)),
      vertexBuffer(vertexBuffer),
      triangleElementsBuffer(triangleElementsBuffer),
      lineElementsBuffer(lineElementsBuffer),
      vertex_start(vertexBuffer->index()),
      triangle_elements_start(triangleElementsBuffer->index()),
      line_elements_start(lineElementsBuffer->index()) {
    assert(tesselator);
}

FillBucket::~FillBucket() {
    if (tesselator) {
        tessDeleteTess(tesselator);
    }
}

void FillBucket::addGeometry(pbf& geom) {
    std::vector<Coordinate> line;
    Geometry::command cmd;

    std::vector<TESSreal> line2;

    Coordinate coord;
    Geometry geometry(geom);
    int32_t x, y;
    while ((cmd = geometry.next(x, y)) != Geometry::end) {
        if (cmd == Geometry::move_to) {
            if (line.size()) {
                // addGeometry(line);
                line.clear();
            }
            if (line2.size()) {
                tessAddContour(tesselator, vertexSize, line2.data(), stride, line2.size() / vertexSize);
                line2.clear();
                hasVertices = true;
            }
        }
        line.emplace_back(x, y);
        line2.push_back(x);
        line2.push_back(y);
    }
    if (line.size()) {
        // addGeometry(line);
    }
    if (line2.size()) {
        tessAddContour(tesselator, vertexSize, line2.data(), stride, line2.size() / vertexSize);
        hasVertices = true;
    }
}

void FillBucket::tessellate() {
    if (!hasVertices) {
        return;
    }

    hasVertices = false;

    // First combine contours and then triangulate, this removes unnecessary inner vertices.
    if (tessTesselate(tesselator, TESS_WINDING_POSITIVE, TESS_BOUNDARY_CONTOURS, 0, 0, 0)) {
        const TESSreal *vertices = tessGetVertices(tesselator);
        const int contour_element_count = tessGetElementCount(tesselator);
        const TESSindex *contour_elements_p = tessGetElements(tesselator);
        std::vector<TESSindex> contour_elements = { contour_elements_p, contour_elements_p + contour_element_count * 2 };

        for (int i = 0; i < contour_element_count; ++i) {
            const TESSindex group_start = contour_elements[i * 2];
            const TESSindex group_count = contour_elements[i * 2 + 1];
            const TESSreal *group_vertices = &vertices[group_start * 2];
            tessAddContour(tesselator, 2, group_vertices, stride, group_count);
        }

        if (tessTesselate(tesselator, TESS_WINDING_POSITIVE, TESS_POLYGONS, vertices_per_group, 2, 0)) {
            const TESSreal *vertices = tessGetVertices(tesselator);
            const TESSindex *vertex_indices = tessGetVertexIndices(tesselator);
            const int vertex_count = tessGetVertexCount(tesselator);
            const TESSindex *elements = tessGetElements(tesselator);
            const int element_count = tessGetElementCount(tesselator);


            if (vertex_count > 65536) {
                throw geometry_too_long_exception();
            }

            // Add vertices and create reverse index mapping for drawing the outline.
            std::map<TESSindex, int> contour_vertices;
            for (int i = 0; i < vertex_count; ++i) {
                contour_vertices[vertex_indices[i]] = i;
                vertexBuffer->add(vertices[i * 2], vertices[i * 2 + 1]);
            }

            {
                if (!triangleGroups.size() || (triangleGroups.back().vertex_length + vertex_count > 65535)) {
                    // Move to a new group because the old one can't hold the geometry.
                    triangleGroups.emplace_back();
                }

                // We're generating triangle fans, so we always start with the first
                // coordinate in this polygon.
                triangle_group_type& group = triangleGroups.back();
                uint32_t index = group.vertex_length;


                for (int i = 0; i < element_count; ++i) {
                    const TESSindex *element_group = &elements[i * vertices_per_group];

                    if (element_group[0] == TESS_UNDEF || element_group[1] == TESS_UNDEF || element_group[2] == TESS_UNDEF) {
                        throw std::runtime_error("element group is not a triangle");
                    }

                    triangleElementsBuffer->add(
                        index + element_group[0],
                        index + element_group[1],
                        index + element_group[2]
                    );
                }

                group.vertex_length += vertex_count;
                group.elements_length += element_count;
            }

        } else {
            assert(false && "tesselation failed");
        }
    } else {
        assert(false && "tesselation failed");
    }
}

void FillBucket::addGeometry(const std::vector<Coordinate>& line) {
    uint32_t vertex_start = vertexBuffer->index();

    size_t length = line.size();

    // Don't add the first vertex twice.
    if (line.front() == line.back()) --length;

    for (size_t i = 0; i < length; i++) {
        const Coordinate& coord = line[i];
        vertexBuffer->add(coord.x, coord.y);
    }

    size_t vertex_end = vertexBuffer->index();

    size_t vertex_count = vertex_end - vertex_start;
    if (vertex_count > 65536) {
        throw geometry_too_long_exception();
    }

    {
        if (!triangleGroups.size() || (triangleGroups.back().vertex_length + vertex_count > 65535)) {
            // Move to a new group because the old one can't hold the geometry.
            triangleGroups.emplace_back();
        }

        // We're generating triangle fans, so we always start with the first
        // coordinate in this polygon.
        triangle_group_type& group = triangleGroups.back();
        uint32_t index = group.vertex_length;
        for (size_t i = 2; i < vertex_count; ++i) {
            triangleElementsBuffer->add(index, index + i - 1, index + i);
        }

        group.vertex_length += vertex_count;
        group.elements_length += (vertex_count - 2);
    }

    {
        if (!lineGroups.size() || (lineGroups.back().vertex_length + vertex_count > 65535)) {
            // Move to a new group because the old one can't hold the geometry.
            lineGroups.emplace_back();
        }

        // We're generating triangle fans, so we always start with the first
        // coordinate in this polygon.
        line_group_type& group = lineGroups.back();
        uint32_t index = group.vertex_length;
        for (size_t i = 1; i < vertex_count; ++i) {
            lineElementsBuffer->add(index + i - 1, index + i);
        }

        // Add a line from the last vertex to the first vertex.
        lineElementsBuffer->add(index + vertex_count - 1, index);

        group.vertex_length += vertex_count;
        group.elements_length += vertex_count;
    }
}

void FillBucket::render(Painter& painter, const std::string& layer_name, const Tile::ID& id) {
    painter.renderFill(*this, layer_name, id);
}

bool FillBucket::empty() const {
    return triangleGroups.empty();
}

void FillBucket::drawElements(PlainShader& shader) {
    char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer->itemSize);
    char *elements_index = BUFFER_OFFSET(triangle_elements_start * triangleElementsBuffer->itemSize);
    for (triangle_group_type& group : triangleGroups) {
        group.array.bind(shader, *vertexBuffer, *triangleElementsBuffer, vertex_index);
        glDrawElements(GL_TRIANGLES, group.elements_length * 3, GL_UNSIGNED_SHORT, elements_index);
        vertex_index += group.vertex_length * vertexBuffer->itemSize;
        elements_index += group.elements_length * triangleElementsBuffer->itemSize;
    }
}

void FillBucket::drawVertices(OutlineShader& shader) {
    char *vertex_index = BUFFER_OFFSET(vertex_start * vertexBuffer->itemSize);
    char *elements_index = BUFFER_OFFSET(line_elements_start * lineElementsBuffer->itemSize);
    for (line_group_type& group : lineGroups) {
        group.array.bind(shader, *vertexBuffer, *lineElementsBuffer, vertex_index);
        glDrawElements(GL_LINES, group.elements_length * 2, GL_UNSIGNED_SHORT, elements_index);
        vertex_index += group.vertex_length * vertexBuffer->itemSize;
        elements_index += group.elements_length * lineElementsBuffer->itemSize;
    }
}