h3api.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #pragma once
  2. #include <contrib/libs/h3/h3lib/include/h3api.h>
  3. // TODO(dakovalkov): eliminate it.
  4. #define lng lon
  5. using LatLng = GeoCoord;
  6. using CellBoundary = GeoBoundary;
  7. using H3Error = uint32_t;
  8. enum H3ErrorCodes
  9. {
  10. E_SUCCESS = 0,
  11. E_FAILED = 1,
  12. };
  13. #define H3_NULL 0
  14. inline H3Error latLngToCell(const LatLng *g, int res, H3Index *out)
  15. {
  16. *out = geoToH3(g, res);
  17. return *out == H3_NULL ? E_FAILED : E_SUCCESS;
  18. }
  19. // NOTE(dakovalkov): The real function signature in H3 4.x is
  20. // H3Error getHexagonEdgeLengthAvgM(int res, double *out)
  21. // But CH uses a random commit from H3 master branch which is incompatible with both 3.x and 4.x.
  22. inline double getHexagonEdgeLengthAvgM(int res)
  23. {
  24. return edgeLengthM(res);
  25. }
  26. inline int getBaseCellNumber(H3Index h)
  27. {
  28. return h3GetBaseCell(h);
  29. }
  30. inline int getResolution(H3Index h)
  31. {
  32. return h3GetResolution(h);
  33. }
  34. // NOTE(dakovalkov): The real function signature in H3 4.x is
  35. // H3Error getHexagonAreaAvgM2(int res, double *out)
  36. inline double getHexagonAreaAvgM2(int res)
  37. {
  38. return hexAreaM2(res);
  39. }
  40. // NOTE(dakovalkov): The real function signature in H3 4.x is
  41. // H3Error areNeighborCells(H3Index origin, H3Index destination, int *out)
  42. inline int areNeighborCells(H3Index origin, H3Index destination)
  43. {
  44. return h3IndexesAreNeighbors(origin, destination);
  45. }
  46. inline int isValidCell(H3Index h)
  47. {
  48. return h3IsValid(h);
  49. }
  50. // NOTE(dakovalkov): The real function signature in H3 4.x is
  51. // H3Error cellToChildrenSize(H3Index cell, int childRes, int64_t *out)
  52. inline int64_t cellToChildrenSize(H3Index cell, int childRes)
  53. {
  54. return maxH3ToChildrenSize(cell, childRes);
  55. }
  56. // NOTE(dakovalkov): The real function signature in H3 4.x is
  57. // H3Error cellToChildren(H3Index cell, int childRes, H3Index *children)
  58. inline H3Error cellToChildren(H3Index cell, int childRes, H3Index *children)
  59. {
  60. h3ToChildren(cell, childRes, children);
  61. return *children == H3_NULL ? E_FAILED : E_SUCCESS;
  62. }
  63. // NOTE(dakovalkov): The real function signature in H3 4.x is
  64. // H3Error cellToParent(H3Index cell, int parentRes, H3Index *parent)
  65. inline H3Index cellToParent(H3Index cell, int parentRes)
  66. {
  67. return h3ToParent(cell, parentRes);
  68. }
  69. // NOTE(dakovalkov): The real function signature in H3 4.x is
  70. // H3Error maxGridDiskSize(int k, int64_t *out)
  71. inline int64_t maxGridDiskSize(int k) {
  72. return maxKringSize(k);
  73. }
  74. inline H3Error gridDisk(H3Index origin, int k, H3Index* out)
  75. {
  76. kRing(origin, k, out);
  77. return *out == H3_NULL ? E_FAILED : E_SUCCESS;
  78. }
  79. inline H3Error cellToLatLng(H3Index cell, LatLng *g)
  80. {
  81. h3ToGeo(cell, g);
  82. // NOTE(dakovalkov): There is no way to get the error in 3.x (is it even possible?).
  83. return E_SUCCESS;
  84. }
  85. // NOTE(dakovalkov): The real function signature in H3 4.x is
  86. // H3Error cellsToDirectedEdge(H3Index origin, H3Index destination, H3Index *out)
  87. inline H3Index cellsToDirectedEdge(H3Index origin, H3Index destination)
  88. {
  89. return getH3UnidirectionalEdge(origin, destination);
  90. }
  91. // NOTE(dakovalkov): The real function signature in H3 4.x is
  92. // H3Error gridPathCellsSize(H3Index start, H3Index end, int64_t* size);
  93. inline int64_t gridPathCellsSize(H3Index start, H3Index end)
  94. {
  95. return h3LineSize(start, end);
  96. }
  97. inline H3Error cellToBoundary(H3Index cell, CellBoundary *bndry)
  98. {
  99. h3ToGeoBoundary(cell, bndry);
  100. // NOTE(dakovalkov): There is no way to get the error in 3.x (is it even possible?).
  101. return E_SUCCESS;
  102. }
  103. inline int isResClassIII(H3Index h)
  104. {
  105. return h3IsResClassIII(h);
  106. }
  107. inline int isPentagon(H3Index h)
  108. {
  109. return h3IsPentagon(h);
  110. }
  111. // NOTE(dakovalkov): The real function signature in H3 4.x is
  112. // H3Error getHexagonEdgeLengthAvgKm(int res, double *out)
  113. inline double getHexagonEdgeLengthAvgKm(int res)
  114. {
  115. return edgeLengthKm(res);
  116. }
  117. inline H3Error getIcosahedronFaces(H3Index h, int* out)
  118. {
  119. h3GetFaces(h, out);
  120. // NOTE(dakovalkov): There is no way to get the error in 3.x (is the error even possible?).
  121. return E_SUCCESS;
  122. }
  123. // NOTE(dakovalkov): The real function signature in H3 4.x is
  124. // H3Error getHexagonAreaAvgKm2(int res, double *out)
  125. inline double getHexagonAreaAvgKm2(int res)
  126. {
  127. return hexAreaKm2(res);
  128. }
  129. inline H3Error gridPathCells(H3Index start, H3Index end, H3Index* out)
  130. {
  131. return h3Line(start, end, out);
  132. }
  133. inline int isValidDirectedEdge(H3Index edge)
  134. {
  135. return h3UnidirectionalEdgeIsValid(edge);
  136. }
  137. inline H3Error originToDirectedEdges(H3Index origin, H3Index* edges)
  138. {
  139. // TODO(dakovalkov): Find out how to implement it via 3.x version.
  140. throw "Not implemented";
  141. }
  142. // NOTE(dakovalkov): The real function signature in H3 4.x is
  143. // H3Error getDirectedEdgeDestination(H3Index edge, H3Index *out);
  144. inline H3Index getDirectedEdgeDestination(H3Index edge)
  145. {
  146. return getDestinationH3IndexFromUnidirectionalEdge(edge);
  147. }
  148. inline int res0CellCount()
  149. {
  150. return res0IndexCount();
  151. }
  152. inline H3Error getRes0Cells(H3Index *out)
  153. {
  154. getRes0Indexes(out);
  155. // NOTE(dakovalkov): There is no way to get the error in 3.x.
  156. return E_SUCCESS;
  157. }
  158. // NOTE(dakovalkov): The real function signature in H3 4.x is
  159. // H3Error getNumCells(int res, int64_t *out);
  160. inline int64_t getNumCells(int res)
  161. {
  162. return numHexagons(res);
  163. }
  164. inline H3Error directedEdgeToBoundary(H3Index edge, CellBoundary* gb)
  165. {
  166. getH3UnidirectionalEdgeBoundary(edge, gb);
  167. // NOTE(dakovalkov): There is no way to get the error in 3.x.
  168. return E_SUCCESS;
  169. }
  170. // NOTE(dakovalkov): The real function signature in H3 4.x is
  171. // H3Error cellToCenterChild(H3Index cell, int childRes, H3Index *child);
  172. inline H3Index cellToCenterChild(H3Index cell, int childRes)
  173. {
  174. return h3ToCenterChild(cell, childRes);
  175. }
  176. // NOTE(dakovalkov): The real function signature in H3 4.x is
  177. // double greatCircleDistanceKm(const LatLng *a, const LatLng *b)
  178. inline double distanceKm(const LatLng *a, const LatLng *b)
  179. {
  180. return pointDistKm(a, b);
  181. }
  182. // NOTE(dakovalkov): The real function signature in H3 4.x is
  183. // double greatCircleDistanceM(const LatLng *a, const LatLng *b)
  184. inline double distanceM(const LatLng *a, const LatLng *b)
  185. {
  186. return pointDistM(a, b);
  187. }
  188. // NOTE(dakovalkov): The real function signature in H3 4.x is
  189. // double greatCircleDistanceRads(const LatLng *a, const LatLng *b)
  190. inline double distanceRads(const LatLng *a, const LatLng *b)
  191. {
  192. return pointDistRads(a, b);
  193. }
  194. inline H3Error directedEdgeToCells(H3Index edge, H3Index* originDestination)
  195. {
  196. getH3IndexesFromUnidirectionalEdge(edge, originDestination);
  197. return *originDestination == H3_NULL ? E_FAILED : E_SUCCESS;
  198. }
  199. // NOTE(dakovalkov): The real function signature in H3 4.x is
  200. // H3Error getDirectedEdgeOrigin(H3Index edge, H3Index *out);
  201. inline H3Index getDirectedEdgeOrigin(H3Index edge)
  202. {
  203. return getOriginH3IndexFromUnidirectionalEdge(edge);
  204. }
  205. inline int pentagonCount()
  206. {
  207. return pentagonIndexCount();
  208. }
  209. inline H3Error getPentagons(int res, H3Index *out)
  210. {
  211. getPentagonIndexes(res, out);
  212. // NOTE(dakovalkov): There is no way to get the error in 3.x.
  213. return E_SUCCESS;
  214. }
  215. inline H3Error gridRingUnsafe(H3Index origin, int k, H3Index* out)
  216. {
  217. return hexRing(origin, k, out);
  218. }