| 1 | /* |
| 2 | * Copyright 2017-2018 Uber Technologies, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /** @file linkedGeo.h |
| 17 | * @brief Linked data structure for geo data |
| 18 | */ |
| 19 | |
| 20 | #ifndef LINKED_GEO_H |
| 21 | #define LINKED_GEO_H |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include "bbox.h" |
| 25 | #include "geoCoord.h" |
| 26 | #include "h3api.h" |
| 27 | |
| 28 | // Error codes for normalizeMultiPolygon |
| 29 | #define NORMALIZATION_SUCCESS 0 |
| 30 | #define NORMALIZATION_ERR_MULTIPLE_POLYGONS 1 |
| 31 | #define NORMALIZATION_ERR_UNASSIGNED_HOLES 2 |
| 32 | |
| 33 | // Macros for use with polygonAlgos.h |
| 34 | /** Macro: Init iteration vars for LinkedGeoLoop */ |
| 35 | #define INIT_ITERATION_LINKED_LOOP \ |
| 36 | LinkedGeoCoord* currentCoord = NULL; \ |
| 37 | LinkedGeoCoord* nextCoord = NULL |
| 38 | |
| 39 | /** Macro: Get the next coord in a linked loop, wrapping if needed */ |
| 40 | #define GET_NEXT_COORD(loop, coordToCheck) \ |
| 41 | coordToCheck == NULL ? loop->first : currentCoord->next |
| 42 | |
| 43 | /** Macro: Increment LinkedGeoLoop iteration, or break if done. */ |
| 44 | #define ITERATE_LINKED_LOOP(loop, vertexA, vertexB) \ |
| 45 | currentCoord = GET_NEXT_COORD(loop, currentCoord); \ |
| 46 | if (currentCoord == NULL) break; \ |
| 47 | vertexA = currentCoord->vertex; \ |
| 48 | nextCoord = GET_NEXT_COORD(loop, currentCoord->next); \ |
| 49 | vertexB = nextCoord->vertex |
| 50 | |
| 51 | /** Macro: Whether a LinkedGeoLoop is empty */ |
| 52 | #define IS_EMPTY_LINKED_LOOP(loop) loop->first == NULL |
| 53 | |
| 54 | int normalizeMultiPolygon(LinkedGeoPolygon* root); |
| 55 | LinkedGeoPolygon* addNewLinkedPolygon(LinkedGeoPolygon* polygon); |
| 56 | LinkedGeoLoop* addNewLinkedLoop(LinkedGeoPolygon* polygon); |
| 57 | LinkedGeoLoop* addLinkedLoop(LinkedGeoPolygon* polygon, LinkedGeoLoop* loop); |
| 58 | LinkedGeoCoord* addLinkedCoord(LinkedGeoLoop* loop, const GeoCoord* vertex); |
| 59 | int countLinkedPolygons(LinkedGeoPolygon* polygon); |
| 60 | int countLinkedLoops(LinkedGeoPolygon* polygon); |
| 61 | int countLinkedCoords(LinkedGeoLoop* loop); |
| 62 | void destroyLinkedGeoLoop(LinkedGeoLoop* loop); |
| 63 | |
| 64 | // The following functions are created via macro in polygonAlgos.h, |
| 65 | // so their signatures are documented here: |
| 66 | |
| 67 | /** |
| 68 | * Create a bounding box from a LinkedGeoLoop |
| 69 | * @param geofence Input Geofence |
| 70 | * @param bbox Output bbox |
| 71 | */ |
| 72 | void bboxFromLinkedGeoLoop(const LinkedGeoLoop* loop, BBox* bbox); |
| 73 | |
| 74 | /** |
| 75 | * Take a given LinkedGeoLoop data structure and check if it |
| 76 | * contains a given geo coordinate. |
| 77 | * @param loop The linked loop |
| 78 | * @param bbox The bbox for the loop |
| 79 | * @param coord The coordinate to check |
| 80 | * @return Whether the point is contained |
| 81 | */ |
| 82 | bool pointInsideLinkedGeoLoop(const LinkedGeoLoop* loop, const BBox* bbox, |
| 83 | const GeoCoord* coord); |
| 84 | |
| 85 | /** |
| 86 | * Whether the winding order of a given LinkedGeoLoop is clockwise |
| 87 | * @param loop The loop to check |
| 88 | * @return Whether the loop is clockwise |
| 89 | */ |
| 90 | bool isClockwiseLinkedGeoLoop(const LinkedGeoLoop* loop); |
| 91 | |
| 92 | #endif |
| 93 | |