-
Notifications
You must be signed in to change notification settings - Fork 60
/
h3_polygon.c
85 lines (75 loc) · 2.64 KB
/
h3_polygon.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
/*
* Copyright 2018-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 polygon.c
* @brief Polygon (GeoLoop) algorithms
*/
#include "h3_polygon.h"
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include "h3_bbox.h"
#include "h3_constants.h"
#include "h3_h3api.h"
#include "h3_latLng.h"
#include "h3_linkedGeo.h"
// Define macros used in polygon algos for GeoLoop
#define TYPE GeoLoop
#define INIT_ITERATION INIT_ITERATION_GEOFENCE
#define ITERATE ITERATE_GEOFENCE
#define IS_EMPTY IS_EMPTY_GEOFENCE
#include "h3_polygonAlgos.h"
#undef TYPE
#undef INIT_ITERATION
#undef ITERATE
#undef IS_EMPTY
/**
* Create a bounding box from a GeoPolygon
* @param polygon Input GeoPolygon
* @param bboxes Output bboxes, one for the outer loop and one for each hole
*/
void bboxesFromGeoPolygon(const GeoPolygon *polygon, BBox *bboxes) {
bboxFromGeoLoop(&polygon->geoloop, &bboxes[0]);
for (int i = 0; i < polygon->numHoles; i++) {
bboxFromGeoLoop(&polygon->holes[i], &bboxes[i + 1]);
}
}
/**
* pointInsidePolygon takes a given GeoPolygon data structure and
* checks if it contains a given geo coordinate.
*
* @param geoPolygon The geoloop and holes defining the relevant area
* @param bboxes The bboxes for the main geoloop and each of its holes
* @param coord The coordinate to check
* @return Whether the point is contained
*/
bool pointInsidePolygon(const GeoPolygon *geoPolygon, const BBox *bboxes,
const LatLng *coord) {
// Start with contains state of primary geoloop
bool contains =
pointInsideGeoLoop(&(geoPolygon->geoloop), &bboxes[0], coord);
// If the point is contained in the primary geoloop, but there are holes in
// the geoloop iterate through all holes and return false if the point is
// contained in any hole
if (contains && geoPolygon->numHoles > 0) {
for (int i = 0; i < geoPolygon->numHoles; i++) {
if (pointInsideGeoLoop(&(geoPolygon->holes[i]), &bboxes[i + 1],
coord)) {
return false;
}
}
}
return contains;
}