-
Notifications
You must be signed in to change notification settings - Fork 60
/
h3_linkedGeo.c
380 lines (346 loc) · 12.3 KB
/
h3_linkedGeo.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2017-2018, 2020-2021 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file linkedGeo.c
* @brief Linked data structure for geo data
*/
#include "h3_linkedGeo.h"
#include <assert.h>
#include <stdlib.h>
#include "h3_alloc.h"
#include "h3_h3api.h"
#include "h3_latLng.h"
/**
* Add a linked polygon to the current polygon
* @param polygon Polygon to add link to
* @return Pointer to new polygon
*/
LinkedGeoPolygon *addNewLinkedPolygon(LinkedGeoPolygon *polygon) {
assert(polygon->next == NULL);
LinkedGeoPolygon *next = H3_MEMORY(calloc)(1, sizeof(*next));
assert(next != NULL);
polygon->next = next;
return next;
}
/**
* Add a new linked loop to the current polygon
* @param polygon Polygon to add loop to
* @return Pointer to loop
*/
LinkedGeoLoop *addNewLinkedLoop(LinkedGeoPolygon *polygon) {
LinkedGeoLoop *loop = H3_MEMORY(calloc)(1, sizeof(*loop));
assert(loop != NULL);
return addLinkedLoop(polygon, loop);
}
/**
* Add an existing linked loop to the current polygon
* @param polygon Polygon to add loop to
* @return Pointer to loop
*/
LinkedGeoLoop *addLinkedLoop(LinkedGeoPolygon *polygon, LinkedGeoLoop *loop) {
LinkedGeoLoop *last = polygon->last;
if (last == NULL) {
assert(polygon->first == NULL);
polygon->first = loop;
} else {
last->next = loop;
}
polygon->last = loop;
return loop;
}
/**
* Add a new linked coordinate to the current loop
* @param loop Loop to add coordinate to
* @param vertex Coordinate to add
* @return Pointer to the coordinate
*/
LinkedLatLng *addLinkedCoord(LinkedGeoLoop *loop, const LatLng *vertex) {
LinkedLatLng *coord = H3_MEMORY(malloc)(sizeof(*coord));
assert(coord != NULL);
*coord = (LinkedLatLng){.vertex = *vertex, .next = NULL};
LinkedLatLng *last = loop->last;
if (last == NULL) {
assert(loop->first == NULL);
loop->first = coord;
} else {
last->next = coord;
}
loop->last = coord;
return coord;
}
/**
* Free all allocated memory for a linked geo loop. The caller is
* responsible for freeing memory allocated to input loop struct.
* @param loop Loop to free
*/
void destroyLinkedGeoLoop(LinkedGeoLoop *loop) {
LinkedLatLng *nextCoord;
for (LinkedLatLng *currentCoord = loop->first; currentCoord != NULL;
currentCoord = nextCoord) {
nextCoord = currentCoord->next;
H3_MEMORY(free)(currentCoord);
}
}
/**
* Free all allocated memory for a linked geo structure. The caller is
* responsible for freeing memory allocated to input polygon struct.
* @param polygon Pointer to the first polygon in the structure
*/
void H3_EXPORT(destroyLinkedMultiPolygon)(LinkedGeoPolygon *polygon) {
// flag to skip the input polygon
bool skip = true;
LinkedGeoPolygon *nextPolygon;
LinkedGeoLoop *nextLoop;
for (LinkedGeoPolygon *currentPolygon = polygon; currentPolygon != NULL;
currentPolygon = nextPolygon) {
for (LinkedGeoLoop *currentLoop = currentPolygon->first;
currentLoop != NULL; currentLoop = nextLoop) {
destroyLinkedGeoLoop(currentLoop);
nextLoop = currentLoop->next;
H3_MEMORY(free)(currentLoop);
}
nextPolygon = currentPolygon->next;
if (skip) {
// do not free the input polygon
skip = false;
} else {
H3_MEMORY(free)(currentPolygon);
}
}
}
/**
* Count the number of polygons in a linked list
* @param polygon Starting polygon
* @return Count
*/
int countLinkedPolygons(LinkedGeoPolygon *polygon) {
int count = 0;
while (polygon != NULL) {
count++;
polygon = polygon->next;
}
return count;
}
/**
* Count the number of linked loops in a polygon
* @param polygon Polygon to count loops for
* @return Count
*/
int countLinkedLoops(LinkedGeoPolygon *polygon) {
LinkedGeoLoop *loop = polygon->first;
int count = 0;
while (loop != NULL) {
count++;
loop = loop->next;
}
return count;
}
/**
* Count the number of coordinates in a loop
* @param loop Loop to count coordinates for
* @return Count
*/
int countLinkedCoords(LinkedGeoLoop *loop) {
LinkedLatLng *coord = loop->first;
int count = 0;
while (coord != NULL) {
count++;
coord = coord->next;
}
return count;
}
/**
* Count the number of polygons containing a given loop.
* @param loop Loop to count containers for
* @param polygons Polygons to test
* @param bboxes Bounding boxes for polygons, used in point-in-poly check
* @param polygonCount Number of polygons in the test array
* @return Number of polygons containing the loop
*/
static int countContainers(const LinkedGeoLoop *loop,
const LinkedGeoPolygon **polygons,
const BBox **bboxes, const int polygonCount) {
int containerCount = 0;
for (int i = 0; i < polygonCount; i++) {
if (loop != polygons[i]->first &&
pointInsideLinkedGeoLoop(polygons[i]->first, bboxes[i],
&loop->first->vertex)) {
containerCount++;
}
}
return containerCount;
}
/**
* Given a list of nested containers, find the one most deeply nested.
* @param polygons Polygon containers to check
* @param bboxes Bounding boxes for polygons, used in point-in-poly check
* @param polygonCount Number of polygons in the list
* @return Deepest container, or null if list is empty
*/
static const LinkedGeoPolygon *findDeepestContainer(
const LinkedGeoPolygon **polygons, const BBox **bboxes,
const int polygonCount) {
// Set the initial return value to the first candidate
const LinkedGeoPolygon *parent = polygonCount > 0 ? polygons[0] : NULL;
// If we have multiple polygons, they must be nested inside each other.
// Find the innermost polygon by taking the one with the most containers
// in the list.
if (polygonCount > 1) {
int max = -1;
for (int i = 0; i < polygonCount; i++) {
int count = countContainers(polygons[i]->first, polygons, bboxes,
polygonCount);
if (count > max) {
parent = polygons[i];
max = count;
}
}
}
return parent;
}
/**
* Find the polygon to which a given hole should be allocated. Note that this
* function will return null if no parent is found.
* @param loop Inner loop describing a hole
* @param polygon Head of a linked list of polygons to check
* @param bboxes Bounding boxes for polygons, used in point-in-poly check
* @param polygonCount Number of polygons to check
* @return Pointer to parent polygon, or null if not found
*/
static const LinkedGeoPolygon *findPolygonForHole(
const LinkedGeoLoop *loop, const LinkedGeoPolygon *polygon,
const BBox *bboxes, const int polygonCount) {
// Early exit with no polygons
if (polygonCount == 0) {
return NULL;
}
// Initialize arrays for candidate loops and their bounding boxes
const LinkedGeoPolygon **candidates =
H3_MEMORY(malloc)(polygonCount * sizeof(LinkedGeoPolygon *));
assert(candidates != NULL);
const BBox **candidateBBoxes =
H3_MEMORY(malloc)(polygonCount * sizeof(BBox *));
assert(candidateBBoxes != NULL);
// Find all polygons that contain the loop
int candidateCount = 0;
int index = 0;
while (polygon) {
// We are guaranteed not to overlap, so just test the first point
if (pointInsideLinkedGeoLoop(polygon->first, &bboxes[index],
&loop->first->vertex)) {
candidates[candidateCount] = polygon;
candidateBBoxes[candidateCount] = &bboxes[index];
candidateCount++;
}
polygon = polygon->next;
index++;
}
// The most deeply nested container is the immediate parent
const LinkedGeoPolygon *parent =
findDeepestContainer(candidates, candidateBBoxes, candidateCount);
// Free allocated memory
H3_MEMORY(free)(candidates);
H3_MEMORY(free)(candidateBBoxes);
return parent;
}
/**
* Normalize a LinkedGeoPolygon in-place into a structure following GeoJSON
* MultiPolygon rules: Each polygon must have exactly one outer loop, which
* must be first in the list, followed by any holes. Holes in this algorithm
* are identified by winding order (holes are clockwise), which is guaranteed
* by the h3SetToVertexGraph algorithm.
*
* Input to this function is assumed to be a single polygon including all
* loops to normalize. It's assumed that a valid arrangement is possible.
*
* @param root Root polygon including all loops
* @return 0 on success, or an error code > 0 for invalid input
*/
H3Error normalizeMultiPolygon(LinkedGeoPolygon *root) {
// We assume that the input is a single polygon with loops;
// if it has multiple polygons, don't touch it
if (root->next) {
return E_FAILED;
}
// Count loops, exiting early if there's only one
int loopCount = countLinkedLoops(root);
if (loopCount <= 1) {
return E_SUCCESS;
}
H3Error resultCode = E_SUCCESS;
LinkedGeoPolygon *polygon = NULL;
LinkedGeoLoop *next;
int innerCount = 0;
int outerCount = 0;
// Create an array to hold all of the inner loops. Note that
// this array will never be full, as there will always be fewer
// inner loops than outer loops.
LinkedGeoLoop **innerLoops =
H3_MEMORY(malloc)(loopCount * sizeof(LinkedGeoLoop *));
assert(innerLoops != NULL);
// Create an array to hold the bounding boxes for the outer loops
BBox *bboxes = H3_MEMORY(malloc)(loopCount * sizeof(BBox));
assert(bboxes != NULL);
// Get the first loop and unlink it from root
LinkedGeoLoop *loop = root->first;
*root = (LinkedGeoPolygon){0};
// Iterate over all loops, moving inner loops into an array and
// assigning outer loops to new polygons
while (loop) {
if (isClockwiseLinkedGeoLoop(loop)) {
innerLoops[innerCount] = loop;
innerCount++;
} else {
polygon = polygon == NULL ? root : addNewLinkedPolygon(polygon);
addLinkedLoop(polygon, loop);
bboxFromLinkedGeoLoop(loop, &bboxes[outerCount]);
outerCount++;
}
// get the next loop and unlink it from this one
next = loop->next;
loop->next = NULL;
loop = next;
}
// Find polygon for each inner loop and assign the hole to it
for (int i = 0; i < innerCount; i++) {
polygon = (LinkedGeoPolygon *)findPolygonForHole(innerLoops[i], root,
bboxes, outerCount);
if (polygon) {
addLinkedLoop(polygon, innerLoops[i]);
} else {
// If we can't find a polygon (possible with invalid input), then
// we need to release the memory for the hole, because the loop has
// been unlinked from the root and the caller will no longer have
// a way to destroy it with destroyLinkedMultiPolygon.
destroyLinkedGeoLoop(innerLoops[i]);
H3_MEMORY(free)(innerLoops[i]);
resultCode = E_FAILED;
}
}
// Free allocated memory
H3_MEMORY(free)(innerLoops);
H3_MEMORY(free)(bboxes);
return resultCode;
}
// Define macros used in polygon algos for LinkedGeoLoop
#define TYPE LinkedGeoLoop
#define INIT_ITERATION INIT_ITERATION_LINKED_LOOP
#define ITERATE ITERATE_LINKED_LOOP
#define IS_EMPTY IS_EMPTY_LINKED_LOOP
#include "h3_polygonAlgos.h"
#undef TYPE
#undef IS_EMPTY
#undef INIT_ITERATION
#undef ITERATE