@sentryware/s2-node 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (471) hide show
  1. package/.circleci/config.yml +45 -0
  2. package/.dockerignore +1 -0
  3. package/.gitmodules +3 -0
  4. package/CHANGELOG.md +33 -0
  5. package/LICENSE +201 -0
  6. package/README.md +147 -0
  7. package/binding.gyp +170 -0
  8. package/docker/Dockerfile.node20.test +8 -0
  9. package/docker/Dockerfile.node22.test +8 -0
  10. package/docker/Dockerfile.node24.test +8 -0
  11. package/index.d.ts +117 -0
  12. package/index.js +6 -0
  13. package/jest.config.js +184 -0
  14. package/package.json +43 -0
  15. package/publish-linux.sh +18 -0
  16. package/publish-osx.sh +19 -0
  17. package/src/builder.cc +84 -0
  18. package/src/builder.h +29 -0
  19. package/src/cell.cc +71 -0
  20. package/src/cell.h +26 -0
  21. package/src/cell_id.cc +210 -0
  22. package/src/cell_id.h +44 -0
  23. package/src/cell_union.cc +237 -0
  24. package/src/cell_union.h +34 -0
  25. package/src/earth.cc +185 -0
  26. package/src/earth.h +33 -0
  27. package/src/latlng.cc +132 -0
  28. package/src/latlng.h +28 -0
  29. package/src/loop.cc +51 -0
  30. package/src/loop.h +21 -0
  31. package/src/point.cc +69 -0
  32. package/src/point.h +23 -0
  33. package/src/polygon.cc +36 -0
  34. package/src/polygon.h +20 -0
  35. package/src/polyline.cc +186 -0
  36. package/src/polyline.h +34 -0
  37. package/src/region_coverer.cc +450 -0
  38. package/src/region_coverer.h +56 -0
  39. package/src/s2.cc +27 -0
  40. package/test/Cell.test.js +37 -0
  41. package/test/CellId.test.js +135 -0
  42. package/test/CellUnion.test.js +150 -0
  43. package/test/Earth.test.js +62 -0
  44. package/test/LatLng.test.js +45 -0
  45. package/test/Point.test.js +14 -0
  46. package/test/Polyline.test.js +78 -0
  47. package/test/RegionCoverer.test.js +301 -0
  48. package/test.sh +16 -0
  49. package/third_party/s2geometry/.travis.yml +163 -0
  50. package/third_party/s2geometry/AUTHORS +13 -0
  51. package/third_party/s2geometry/CONTRIBUTING.md +65 -0
  52. package/third_party/s2geometry/CONTRIBUTORS +30 -0
  53. package/third_party/s2geometry/LICENSE +202 -0
  54. package/third_party/s2geometry/NOTICE +5 -0
  55. package/third_party/s2geometry/README.md +127 -0
  56. package/third_party/s2geometry/doc/examples/point_index.cc +44 -0
  57. package/third_party/s2geometry/doc/examples/term_index.cc +99 -0
  58. package/third_party/s2geometry/doc/examples/term_index.py +101 -0
  59. package/third_party/s2geometry/src/python/coder.i +125 -0
  60. package/third_party/s2geometry/src/python/pywraps2_test.py +786 -0
  61. package/third_party/s2geometry/src/python/s2.i +37 -0
  62. package/third_party/s2geometry/src/python/s2_common.i +756 -0
  63. package/third_party/s2geometry/src/s2/_fp_contract_off.h +60 -0
  64. package/third_party/s2geometry/src/s2/base/casts.h +318 -0
  65. package/third_party/s2geometry/src/s2/base/commandlineflags.h +67 -0
  66. package/third_party/s2geometry/src/s2/base/integral_types.h +31 -0
  67. package/third_party/s2geometry/src/s2/base/log_severity.h +40 -0
  68. package/third_party/s2geometry/src/s2/base/logging.h +173 -0
  69. package/third_party/s2geometry/src/s2/base/mutex.h +61 -0
  70. package/third_party/s2geometry/src/s2/base/port.h +999 -0
  71. package/third_party/s2geometry/src/s2/base/spinlock.h +60 -0
  72. package/third_party/s2geometry/src/s2/base/stringprintf.cc +107 -0
  73. package/third_party/s2geometry/src/s2/base/stringprintf.h +53 -0
  74. package/third_party/s2geometry/src/s2/base/strtoint.cc +65 -0
  75. package/third_party/s2geometry/src/s2/base/strtoint.h +106 -0
  76. package/third_party/s2geometry/src/s2/base/timer.h +50 -0
  77. package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector.cc +164 -0
  78. package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector.h +110 -0
  79. package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector_test.cc +232 -0
  80. package/third_party/s2geometry/src/s2/encoded_s2point_vector.cc +838 -0
  81. package/third_party/s2geometry/src/s2/encoded_s2point_vector.h +140 -0
  82. package/third_party/s2geometry/src/s2/encoded_s2point_vector_test.cc +344 -0
  83. package/third_party/s2geometry/src/s2/encoded_s2shape_index.cc +181 -0
  84. package/third_party/s2geometry/src/s2/encoded_s2shape_index.h +276 -0
  85. package/third_party/s2geometry/src/s2/encoded_s2shape_index_test.cc +244 -0
  86. package/third_party/s2geometry/src/s2/encoded_string_vector.cc +66 -0
  87. package/third_party/s2geometry/src/s2/encoded_string_vector.h +164 -0
  88. package/third_party/s2geometry/src/s2/encoded_string_vector_test.cc +69 -0
  89. package/third_party/s2geometry/src/s2/encoded_uint_vector.h +299 -0
  90. package/third_party/s2geometry/src/s2/encoded_uint_vector_test.cc +124 -0
  91. package/third_party/s2geometry/src/s2/id_set_lexicon.cc +81 -0
  92. package/third_party/s2geometry/src/s2/id_set_lexicon.h +199 -0
  93. package/third_party/s2geometry/src/s2/id_set_lexicon_test.cc +70 -0
  94. package/third_party/s2geometry/src/s2/mutable_s2shape_index.cc +1585 -0
  95. package/third_party/s2geometry/src/s2/mutable_s2shape_index.h +600 -0
  96. package/third_party/s2geometry/src/s2/mutable_s2shape_index_test.cc +589 -0
  97. package/third_party/s2geometry/src/s2/r1interval.h +220 -0
  98. package/third_party/s2geometry/src/s2/r1interval_test.cc +185 -0
  99. package/third_party/s2geometry/src/s2/r2.h +26 -0
  100. package/third_party/s2geometry/src/s2/r2rect.cc +93 -0
  101. package/third_party/s2geometry/src/s2/r2rect.h +234 -0
  102. package/third_party/s2geometry/src/s2/r2rect_test.cc +228 -0
  103. package/third_party/s2geometry/src/s2/s1angle.cc +54 -0
  104. package/third_party/s2geometry/src/s2/s1angle.h +336 -0
  105. package/third_party/s2geometry/src/s2/s1angle_test.cc +185 -0
  106. package/third_party/s2geometry/src/s2/s1chord_angle.cc +159 -0
  107. package/third_party/s2geometry/src/s2/s1chord_angle.h +369 -0
  108. package/third_party/s2geometry/src/s2/s1chord_angle_test.cc +207 -0
  109. package/third_party/s2geometry/src/s2/s1interval.cc +296 -0
  110. package/third_party/s2geometry/src/s2/s1interval.h +266 -0
  111. package/third_party/s2geometry/src/s2/s1interval_test.cc +469 -0
  112. package/third_party/s2geometry/src/s2/s2boolean_operation.cc +2391 -0
  113. package/third_party/s2geometry/src/s2/s2boolean_operation.h +501 -0
  114. package/third_party/s2geometry/src/s2/s2boolean_operation_test.cc +1400 -0
  115. package/third_party/s2geometry/src/s2/s2builder.cc +1828 -0
  116. package/third_party/s2geometry/src/s2/s2builder.h +1057 -0
  117. package/third_party/s2geometry/src/s2/s2builder_graph.cc +1084 -0
  118. package/third_party/s2geometry/src/s2/s2builder_graph.h +799 -0
  119. package/third_party/s2geometry/src/s2/s2builder_graph_test.cc +462 -0
  120. package/third_party/s2geometry/src/s2/s2builder_layer.h +50 -0
  121. package/third_party/s2geometry/src/s2/s2builder_test.cc +1329 -0
  122. package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer.cc +313 -0
  123. package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer.h +221 -0
  124. package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer_test.cc +261 -0
  125. package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies.cc +392 -0
  126. package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies.h +86 -0
  127. package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies_test.cc +182 -0
  128. package/third_party/s2geometry/src/s2/s2builderutil_graph_shape.h +57 -0
  129. package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer.cc +212 -0
  130. package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer.h +218 -0
  131. package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer_test.cc +367 -0
  132. package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer.cc +74 -0
  133. package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer.h +122 -0
  134. package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer_test.cc +167 -0
  135. package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer.cc +191 -0
  136. package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer.h +211 -0
  137. package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer_test.cc +312 -0
  138. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer.cc +105 -0
  139. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer.h +174 -0
  140. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer_test.cc +220 -0
  141. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer.cc +98 -0
  142. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer.h +292 -0
  143. package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer_test.cc +233 -0
  144. package/third_party/s2geometry/src/s2/s2builderutil_snap_functions.cc +354 -0
  145. package/third_party/s2geometry/src/s2/s2builderutil_snap_functions.h +239 -0
  146. package/third_party/s2geometry/src/s2/s2builderutil_snap_functions_test.cc +716 -0
  147. package/third_party/s2geometry/src/s2/s2builderutil_testing.cc +37 -0
  148. package/third_party/s2geometry/src/s2/s2builderutil_testing.h +100 -0
  149. package/third_party/s2geometry/src/s2/s2builderutil_testing_test.cc +85 -0
  150. package/third_party/s2geometry/src/s2/s2cap.cc +347 -0
  151. package/third_party/s2geometry/src/s2/s2cap.h +286 -0
  152. package/third_party/s2geometry/src/s2/s2cap_test.cc +379 -0
  153. package/third_party/s2geometry/src/s2/s2cell.cc +552 -0
  154. package/third_party/s2geometry/src/s2/s2cell.h +249 -0
  155. package/third_party/s2geometry/src/s2/s2cell_id.cc +619 -0
  156. package/third_party/s2geometry/src/s2/s2cell_id.h +705 -0
  157. package/third_party/s2geometry/src/s2/s2cell_id_test.cc +633 -0
  158. package/third_party/s2geometry/src/s2/s2cell_index.cc +149 -0
  159. package/third_party/s2geometry/src/s2/s2cell_index.h +660 -0
  160. package/third_party/s2geometry/src/s2/s2cell_index_test.cc +411 -0
  161. package/third_party/s2geometry/src/s2/s2cell_test.cc +687 -0
  162. package/third_party/s2geometry/src/s2/s2cell_union.cc +515 -0
  163. package/third_party/s2geometry/src/s2/s2cell_union.h +399 -0
  164. package/third_party/s2geometry/src/s2/s2cell_union_test.cc +598 -0
  165. package/third_party/s2geometry/src/s2/s2centroids.cc +84 -0
  166. package/third_party/s2geometry/src/s2/s2centroids.h +87 -0
  167. package/third_party/s2geometry/src/s2/s2centroids_test.cc +82 -0
  168. package/third_party/s2geometry/src/s2/s2closest_cell_query.cc +123 -0
  169. package/third_party/s2geometry/src/s2/s2closest_cell_query.h +385 -0
  170. package/third_party/s2geometry/src/s2/s2closest_cell_query_base.h +841 -0
  171. package/third_party/s2geometry/src/s2/s2closest_cell_query_base_test.cc +63 -0
  172. package/third_party/s2geometry/src/s2/s2closest_cell_query_test.cc +412 -0
  173. package/third_party/s2geometry/src/s2/s2closest_edge_query.cc +106 -0
  174. package/third_party/s2geometry/src/s2/s2closest_edge_query.h +421 -0
  175. package/third_party/s2geometry/src/s2/s2closest_edge_query_base.h +946 -0
  176. package/third_party/s2geometry/src/s2/s2closest_edge_query_base_test.cc +59 -0
  177. package/third_party/s2geometry/src/s2/s2closest_edge_query_test.cc +505 -0
  178. package/third_party/s2geometry/src/s2/s2closest_edge_query_testing.h +91 -0
  179. package/third_party/s2geometry/src/s2/s2closest_point_query.cc +66 -0
  180. package/third_party/s2geometry/src/s2/s2closest_point_query.h +465 -0
  181. package/third_party/s2geometry/src/s2/s2closest_point_query_base.h +767 -0
  182. package/third_party/s2geometry/src/s2/s2closest_point_query_base_test.cc +63 -0
  183. package/third_party/s2geometry/src/s2/s2closest_point_query_test.cc +312 -0
  184. package/third_party/s2geometry/src/s2/s2contains_point_query.h +328 -0
  185. package/third_party/s2geometry/src/s2/s2contains_point_query_test.cc +159 -0
  186. package/third_party/s2geometry/src/s2/s2contains_vertex_query.cc +39 -0
  187. package/third_party/s2geometry/src/s2/s2contains_vertex_query.h +66 -0
  188. package/third_party/s2geometry/src/s2/s2contains_vertex_query_test.cc +67 -0
  189. package/third_party/s2geometry/src/s2/s2convex_hull_query.cc +198 -0
  190. package/third_party/s2geometry/src/s2/s2convex_hull_query.h +110 -0
  191. package/third_party/s2geometry/src/s2/s2convex_hull_query_test.cc +208 -0
  192. package/third_party/s2geometry/src/s2/s2coords.cc +146 -0
  193. package/third_party/s2geometry/src/s2/s2coords.h +459 -0
  194. package/third_party/s2geometry/src/s2/s2coords_internal.h +71 -0
  195. package/third_party/s2geometry/src/s2/s2coords_test.cc +218 -0
  196. package/third_party/s2geometry/src/s2/s2crossing_edge_query.cc +380 -0
  197. package/third_party/s2geometry/src/s2/s2crossing_edge_query.h +220 -0
  198. package/third_party/s2geometry/src/s2/s2crossing_edge_query_test.cc +382 -0
  199. package/third_party/s2geometry/src/s2/s2debug.cc +23 -0
  200. package/third_party/s2geometry/src/s2/s2debug.h +69 -0
  201. package/third_party/s2geometry/src/s2/s2distance_target.h +165 -0
  202. package/third_party/s2geometry/src/s2/s2earth.cc +52 -0
  203. package/third_party/s2geometry/src/s2/s2earth.h +268 -0
  204. package/third_party/s2geometry/src/s2/s2earth_test.cc +146 -0
  205. package/third_party/s2geometry/src/s2/s2edge_clipping.cc +462 -0
  206. package/third_party/s2geometry/src/s2/s2edge_clipping.h +183 -0
  207. package/third_party/s2geometry/src/s2/s2edge_clipping_test.cc +335 -0
  208. package/third_party/s2geometry/src/s2/s2edge_crosser.cc +85 -0
  209. package/third_party/s2geometry/src/s2/s2edge_crosser.h +343 -0
  210. package/third_party/s2geometry/src/s2/s2edge_crosser_test.cc +264 -0
  211. package/third_party/s2geometry/src/s2/s2edge_crossings.cc +515 -0
  212. package/third_party/s2geometry/src/s2/s2edge_crossings.h +138 -0
  213. package/third_party/s2geometry/src/s2/s2edge_crossings_internal.h +59 -0
  214. package/third_party/s2geometry/src/s2/s2edge_crossings_test.cc +246 -0
  215. package/third_party/s2geometry/src/s2/s2edge_distances.cc +419 -0
  216. package/third_party/s2geometry/src/s2/s2edge_distances.h +192 -0
  217. package/third_party/s2geometry/src/s2/s2edge_distances_test.cc +539 -0
  218. package/third_party/s2geometry/src/s2/s2edge_tessellator.cc +276 -0
  219. package/third_party/s2geometry/src/s2/s2edge_tessellator.h +101 -0
  220. package/third_party/s2geometry/src/s2/s2edge_tessellator_test.cc +492 -0
  221. package/third_party/s2geometry/src/s2/s2edge_vector_shape.h +85 -0
  222. package/third_party/s2geometry/src/s2/s2edge_vector_shape_test.cc +66 -0
  223. package/third_party/s2geometry/src/s2/s2error.cc +29 -0
  224. package/third_party/s2geometry/src/s2/s2error.h +147 -0
  225. package/third_party/s2geometry/src/s2/s2error_test.cc +31 -0
  226. package/third_party/s2geometry/src/s2/s2furthest_edge_query.cc +117 -0
  227. package/third_party/s2geometry/src/s2/s2furthest_edge_query.h +439 -0
  228. package/third_party/s2geometry/src/s2/s2furthest_edge_query_test.cc +487 -0
  229. package/third_party/s2geometry/src/s2/s2latlng.cc +90 -0
  230. package/third_party/s2geometry/src/s2/s2latlng.h +234 -0
  231. package/third_party/s2geometry/src/s2/s2latlng_rect.cc +727 -0
  232. package/third_party/s2geometry/src/s2/s2latlng_rect.h +434 -0
  233. package/third_party/s2geometry/src/s2/s2latlng_rect_bounder.cc +344 -0
  234. package/third_party/s2geometry/src/s2/s2latlng_rect_bounder.h +89 -0
  235. package/third_party/s2geometry/src/s2/s2latlng_rect_bounder_test.cc +306 -0
  236. package/third_party/s2geometry/src/s2/s2latlng_rect_test.cc +1030 -0
  237. package/third_party/s2geometry/src/s2/s2latlng_test.cc +165 -0
  238. package/third_party/s2geometry/src/s2/s2lax_loop_shape.cc +104 -0
  239. package/third_party/s2geometry/src/s2/s2lax_loop_shape.h +153 -0
  240. package/third_party/s2geometry/src/s2/s2lax_loop_shape_test.cc +101 -0
  241. package/third_party/s2geometry/src/s2/s2lax_polygon_shape.cc +348 -0
  242. package/third_party/s2geometry/src/s2/s2lax_polygon_shape.h +183 -0
  243. package/third_party/s2geometry/src/s2/s2lax_polygon_shape_test.cc +234 -0
  244. package/third_party/s2geometry/src/s2/s2lax_polyline_shape.cc +118 -0
  245. package/third_party/s2geometry/src/s2/s2lax_polyline_shape.h +124 -0
  246. package/third_party/s2geometry/src/s2/s2lax_polyline_shape_test.cc +62 -0
  247. package/third_party/s2geometry/src/s2/s2loop.cc +1509 -0
  248. package/third_party/s2geometry/src/s2/s2loop.h +711 -0
  249. package/third_party/s2geometry/src/s2/s2loop_measures.cc +313 -0
  250. package/third_party/s2geometry/src/s2/s2loop_measures.h +280 -0
  251. package/third_party/s2geometry/src/s2/s2loop_measures_test.cc +367 -0
  252. package/third_party/s2geometry/src/s2/s2loop_test.cc +1371 -0
  253. package/third_party/s2geometry/src/s2/s2max_distance_targets.cc +265 -0
  254. package/third_party/s2geometry/src/s2/s2max_distance_targets.h +241 -0
  255. package/third_party/s2geometry/src/s2/s2max_distance_targets_test.cc +367 -0
  256. package/third_party/s2geometry/src/s2/s2measures.cc +128 -0
  257. package/third_party/s2geometry/src/s2/s2measures.h +78 -0
  258. package/third_party/s2geometry/src/s2/s2measures_test.cc +135 -0
  259. package/third_party/s2geometry/src/s2/s2metrics.cc +122 -0
  260. package/third_party/s2geometry/src/s2/s2metrics.h +199 -0
  261. package/third_party/s2geometry/src/s2/s2metrics_test.cc +127 -0
  262. package/third_party/s2geometry/src/s2/s2min_distance_targets.cc +295 -0
  263. package/third_party/s2geometry/src/s2/s2min_distance_targets.h +273 -0
  264. package/third_party/s2geometry/src/s2/s2min_distance_targets_test.cc +239 -0
  265. package/third_party/s2geometry/src/s2/s2padded_cell.cc +162 -0
  266. package/third_party/s2geometry/src/s2/s2padded_cell.h +108 -0
  267. package/third_party/s2geometry/src/s2/s2padded_cell_test.cc +138 -0
  268. package/third_party/s2geometry/src/s2/s2point.h +38 -0
  269. package/third_party/s2geometry/src/s2/s2point_compression.cc +388 -0
  270. package/third_party/s2geometry/src/s2/s2point_compression.h +78 -0
  271. package/third_party/s2geometry/src/s2/s2point_compression_test.cc +305 -0
  272. package/third_party/s2geometry/src/s2/s2point_index.h +345 -0
  273. package/third_party/s2geometry/src/s2/s2point_index_test.cc +147 -0
  274. package/third_party/s2geometry/src/s2/s2point_region.cc +72 -0
  275. package/third_party/s2geometry/src/s2/s2point_region.h +76 -0
  276. package/third_party/s2geometry/src/s2/s2point_region_test.cc +100 -0
  277. package/third_party/s2geometry/src/s2/s2point_span.h +57 -0
  278. package/third_party/s2geometry/src/s2/s2point_test.cc +47 -0
  279. package/third_party/s2geometry/src/s2/s2point_vector_shape.h +127 -0
  280. package/third_party/s2geometry/src/s2/s2point_vector_shape_test.cc +59 -0
  281. package/third_party/s2geometry/src/s2/s2pointutil.cc +131 -0
  282. package/third_party/s2geometry/src/s2/s2pointutil.h +138 -0
  283. package/third_party/s2geometry/src/s2/s2pointutil_test.cc +157 -0
  284. package/third_party/s2geometry/src/s2/s2polygon.cc +1569 -0
  285. package/third_party/s2geometry/src/s2/s2polygon.h +934 -0
  286. package/third_party/s2geometry/src/s2/s2polygon_test.cc +3025 -0
  287. package/third_party/s2geometry/src/s2/s2polyline.cc +645 -0
  288. package/third_party/s2geometry/src/s2/s2polyline.h +379 -0
  289. package/third_party/s2geometry/src/s2/s2polyline_alignment.cc +414 -0
  290. package/third_party/s2geometry/src/s2/s2polyline_alignment.h +245 -0
  291. package/third_party/s2geometry/src/s2/s2polyline_alignment_internal.h +158 -0
  292. package/third_party/s2geometry/src/s2/s2polyline_alignment_test.cc +610 -0
  293. package/third_party/s2geometry/src/s2/s2polyline_measures.cc +42 -0
  294. package/third_party/s2geometry/src/s2/s2polyline_measures.h +53 -0
  295. package/third_party/s2geometry/src/s2/s2polyline_measures_test.cc +57 -0
  296. package/third_party/s2geometry/src/s2/s2polyline_simplifier.cc +187 -0
  297. package/third_party/s2geometry/src/s2/s2polyline_simplifier.h +109 -0
  298. package/third_party/s2geometry/src/s2/s2polyline_simplifier_test.cc +165 -0
  299. package/third_party/s2geometry/src/s2/s2polyline_test.cc +554 -0
  300. package/third_party/s2geometry/src/s2/s2predicates.cc +1486 -0
  301. package/third_party/s2geometry/src/s2/s2predicates.h +282 -0
  302. package/third_party/s2geometry/src/s2/s2predicates_internal.h +135 -0
  303. package/third_party/s2geometry/src/s2/s2predicates_test.cc +1427 -0
  304. package/third_party/s2geometry/src/s2/s2projections.cc +109 -0
  305. package/third_party/s2geometry/src/s2/s2projections.h +161 -0
  306. package/third_party/s2geometry/src/s2/s2projections_test.cc +78 -0
  307. package/third_party/s2geometry/src/s2/s2r2rect.cc +88 -0
  308. package/third_party/s2geometry/src/s2/s2r2rect.h +292 -0
  309. package/third_party/s2geometry/src/s2/s2r2rect_test.cc +312 -0
  310. package/third_party/s2geometry/src/s2/s2region.cc +26 -0
  311. package/third_party/s2geometry/src/s2/s2region.h +142 -0
  312. package/third_party/s2geometry/src/s2/s2region_coverer.cc +514 -0
  313. package/third_party/s2geometry/src/s2/s2region_coverer.h +356 -0
  314. package/third_party/s2geometry/src/s2/s2region_coverer_test.cc +509 -0
  315. package/third_party/s2geometry/src/s2/s2region_intersection.cc +84 -0
  316. package/third_party/s2geometry/src/s2/s2region_intersection.h +79 -0
  317. package/third_party/s2geometry/src/s2/s2region_term_indexer.cc +270 -0
  318. package/third_party/s2geometry/src/s2/s2region_term_indexer.h +299 -0
  319. package/third_party/s2geometry/src/s2/s2region_term_indexer_test.cc +209 -0
  320. package/third_party/s2geometry/src/s2/s2region_test.cc +370 -0
  321. package/third_party/s2geometry/src/s2/s2region_union.cc +90 -0
  322. package/third_party/s2geometry/src/s2/s2region_union.h +83 -0
  323. package/third_party/s2geometry/src/s2/s2region_union_test.cc +89 -0
  324. package/third_party/s2geometry/src/s2/s2shape.h +283 -0
  325. package/third_party/s2geometry/src/s2/s2shape_index.cc +321 -0
  326. package/third_party/s2geometry/src/s2/s2shape_index.h +781 -0
  327. package/third_party/s2geometry/src/s2/s2shape_index_buffered_region.cc +113 -0
  328. package/third_party/s2geometry/src/s2/s2shape_index_buffered_region.h +135 -0
  329. package/third_party/s2geometry/src/s2/s2shape_index_buffered_region_test.cc +162 -0
  330. package/third_party/s2geometry/src/s2/s2shape_index_measures.cc +92 -0
  331. package/third_party/s2geometry/src/s2/s2shape_index_measures.h +100 -0
  332. package/third_party/s2geometry/src/s2/s2shape_index_measures_test.cc +136 -0
  333. package/third_party/s2geometry/src/s2/s2shape_index_region.h +350 -0
  334. package/third_party/s2geometry/src/s2/s2shape_index_region_test.cc +161 -0
  335. package/third_party/s2geometry/src/s2/s2shape_index_test.cc +24 -0
  336. package/third_party/s2geometry/src/s2/s2shape_measures.cc +138 -0
  337. package/third_party/s2geometry/src/s2/s2shape_measures.h +95 -0
  338. package/third_party/s2geometry/src/s2/s2shape_measures_test.cc +139 -0
  339. package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries.cc +120 -0
  340. package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries.h +66 -0
  341. package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries_test.cc +170 -0
  342. package/third_party/s2geometry/src/s2/s2shapeutil_coding.cc +253 -0
  343. package/third_party/s2geometry/src/s2/s2shapeutil_coding.h +283 -0
  344. package/third_party/s2geometry/src/s2/s2shapeutil_coding_test.cc +54 -0
  345. package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force.cc +40 -0
  346. package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force.h +41 -0
  347. package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force_test.cc +55 -0
  348. package/third_party/s2geometry/src/s2/s2shapeutil_count_edges.h +57 -0
  349. package/third_party/s2geometry/src/s2/s2shapeutil_count_edges_test.cc +43 -0
  350. package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator.cc +45 -0
  351. package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator.h +72 -0
  352. package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator_test.cc +116 -0
  353. package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point.cc +107 -0
  354. package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point.h +48 -0
  355. package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point_test.cc +104 -0
  356. package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator.cc +58 -0
  357. package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator.h +65 -0
  358. package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator_test.cc +61 -0
  359. package/third_party/s2geometry/src/s2/s2shapeutil_shape_edge.h +58 -0
  360. package/third_party/s2geometry/src/s2/s2shapeutil_shape_edge_id.h +97 -0
  361. package/third_party/s2geometry/src/s2/s2shapeutil_testing.cc +104 -0
  362. package/third_party/s2geometry/src/s2/s2shapeutil_testing.h +36 -0
  363. package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs.cc +440 -0
  364. package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs.h +72 -0
  365. package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs_test.cc +184 -0
  366. package/third_party/s2geometry/src/s2/s2testing.cc +464 -0
  367. package/third_party/s2geometry/src/s2/s2testing.h +385 -0
  368. package/third_party/s2geometry/src/s2/s2testing_test.cc +166 -0
  369. package/third_party/s2geometry/src/s2/s2text_format.cc +506 -0
  370. package/third_party/s2geometry/src/s2/s2text_format.h +289 -0
  371. package/third_party/s2geometry/src/s2/s2text_format_test.cc +417 -0
  372. package/third_party/s2geometry/src/s2/s2wedge_relations.cc +80 -0
  373. package/third_party/s2geometry/src/s2/s2wedge_relations.h +64 -0
  374. package/third_party/s2geometry/src/s2/s2wedge_relations_test.cc +89 -0
  375. package/third_party/s2geometry/src/s2/sequence_lexicon.h +296 -0
  376. package/third_party/s2geometry/src/s2/sequence_lexicon_test.cc +113 -0
  377. package/third_party/s2geometry/src/s2/strings/ostringstream.cc +35 -0
  378. package/third_party/s2geometry/src/s2/strings/ostringstream.h +105 -0
  379. package/third_party/s2geometry/src/s2/strings/serialize.cc +46 -0
  380. package/third_party/s2geometry/src/s2/strings/serialize.h +40 -0
  381. package/third_party/s2geometry/src/s2/third_party/absl/algorithm/algorithm.h +187 -0
  382. package/third_party/s2geometry/src/s2/third_party/absl/base/attributes.h +666 -0
  383. package/third_party/s2geometry/src/s2/third_party/absl/base/casts.h +189 -0
  384. package/third_party/s2geometry/src/s2/third_party/absl/base/config.h +462 -0
  385. package/third_party/s2geometry/src/s2/third_party/absl/base/dynamic_annotations.cc +129 -0
  386. package/third_party/s2geometry/src/s2/third_party/absl/base/dynamic_annotations.h +394 -0
  387. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/atomic_hook.h +168 -0
  388. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/identity.h +33 -0
  389. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/inline_variable.h +117 -0
  390. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/invoke.h +188 -0
  391. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/raw_logging.cc +254 -0
  392. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/raw_logging.h +205 -0
  393. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/throw_delegate.cc +106 -0
  394. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/throw_delegate.h +71 -0
  395. package/third_party/s2geometry/src/s2/third_party/absl/base/internal/unaligned_access.h +322 -0
  396. package/third_party/s2geometry/src/s2/third_party/absl/base/log_severity.h +77 -0
  397. package/third_party/s2geometry/src/s2/third_party/absl/base/macros.h +236 -0
  398. package/third_party/s2geometry/src/s2/third_party/absl/base/optimization.h +177 -0
  399. package/third_party/s2geometry/src/s2/third_party/absl/base/policy_checks.h +124 -0
  400. package/third_party/s2geometry/src/s2/third_party/absl/base/port.h +97 -0
  401. package/third_party/s2geometry/src/s2/third_party/absl/base/thread_annotations.h +277 -0
  402. package/third_party/s2geometry/src/s2/third_party/absl/container/fixed_array.h +523 -0
  403. package/third_party/s2geometry/src/s2/third_party/absl/container/inlined_vector.h +1453 -0
  404. package/third_party/s2geometry/src/s2/third_party/absl/container/internal/compressed_tuple.h +191 -0
  405. package/third_party/s2geometry/src/s2/third_party/absl/container/internal/container_memory.h +424 -0
  406. package/third_party/s2geometry/src/s2/third_party/absl/container/internal/layout.h +739 -0
  407. package/third_party/s2geometry/src/s2/third_party/absl/memory/memory.h +755 -0
  408. package/third_party/s2geometry/src/s2/third_party/absl/meta/type_traits.h +436 -0
  409. package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128.cc +232 -0
  410. package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128.h +656 -0
  411. package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128_have_intrinsic.inc +3 -0
  412. package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128_no_intrinsic.inc +3 -0
  413. package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii.cc +198 -0
  414. package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii.h +239 -0
  415. package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii_ctype.h +66 -0
  416. package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/bits.h +53 -0
  417. package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/memutil.cc +110 -0
  418. package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/memutil.h +146 -0
  419. package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/resize_uninitialized.h +72 -0
  420. package/third_party/s2geometry/src/s2/third_party/absl/strings/match.cc +38 -0
  421. package/third_party/s2geometry/src/s2/third_party/absl/strings/match.h +89 -0
  422. package/third_party/s2geometry/src/s2/third_party/absl/strings/numbers.cc +909 -0
  423. package/third_party/s2geometry/src/s2/third_party/absl/strings/numbers.h +187 -0
  424. package/third_party/s2geometry/src/s2/third_party/absl/strings/str_cat.cc +240 -0
  425. package/third_party/s2geometry/src/s2/third_party/absl/strings/str_cat.h +398 -0
  426. package/third_party/s2geometry/src/s2/third_party/absl/strings/str_join.h +22 -0
  427. package/third_party/s2geometry/src/s2/third_party/absl/strings/str_split.cc +47 -0
  428. package/third_party/s2geometry/src/s2/third_party/absl/strings/str_split.h +43 -0
  429. package/third_party/s2geometry/src/s2/third_party/absl/strings/string_view.cc +245 -0
  430. package/third_party/s2geometry/src/s2/third_party/absl/strings/string_view.h +602 -0
  431. package/third_party/s2geometry/src/s2/third_party/absl/strings/strip.cc +42 -0
  432. package/third_party/s2geometry/src/s2/third_party/absl/strings/strip.h +130 -0
  433. package/third_party/s2geometry/src/s2/third_party/absl/types/span.h +793 -0
  434. package/third_party/s2geometry/src/s2/third_party/absl/utility/utility.h +299 -0
  435. package/third_party/s2geometry/src/s2/util/bits/bit-interleave.cc +274 -0
  436. package/third_party/s2geometry/src/s2/util/bits/bit-interleave.h +53 -0
  437. package/third_party/s2geometry/src/s2/util/bits/bits.cc +155 -0
  438. package/third_party/s2geometry/src/s2/util/bits/bits.h +745 -0
  439. package/third_party/s2geometry/src/s2/util/coding/coder.cc +83 -0
  440. package/third_party/s2geometry/src/s2/util/coding/coder.h +553 -0
  441. package/third_party/s2geometry/src/s2/util/coding/nth-derivative.h +134 -0
  442. package/third_party/s2geometry/src/s2/util/coding/transforms.h +62 -0
  443. package/third_party/s2geometry/src/s2/util/coding/varint.cc +289 -0
  444. package/third_party/s2geometry/src/s2/util/coding/varint.h +476 -0
  445. package/third_party/s2geometry/src/s2/util/endian/endian.h +859 -0
  446. package/third_party/s2geometry/src/s2/util/gtl/btree.h +2471 -0
  447. package/third_party/s2geometry/src/s2/util/gtl/btree_container.h +411 -0
  448. package/third_party/s2geometry/src/s2/util/gtl/btree_map.h +79 -0
  449. package/third_party/s2geometry/src/s2/util/gtl/btree_set.h +73 -0
  450. package/third_party/s2geometry/src/s2/util/gtl/compact_array.h +653 -0
  451. package/third_party/s2geometry/src/s2/util/gtl/container_logging.h +291 -0
  452. package/third_party/s2geometry/src/s2/util/gtl/dense_hash_set.h +358 -0
  453. package/third_party/s2geometry/src/s2/util/gtl/densehashtable.h +1493 -0
  454. package/third_party/s2geometry/src/s2/util/gtl/hashtable_common.h +253 -0
  455. package/third_party/s2geometry/src/s2/util/gtl/layout.h +28 -0
  456. package/third_party/s2geometry/src/s2/util/gtl/legacy_random_shuffle.h +77 -0
  457. package/third_party/s2geometry/src/s2/util/hash/mix.h +76 -0
  458. package/third_party/s2geometry/src/s2/util/math/exactfloat/exactfloat.cc +832 -0
  459. package/third_party/s2geometry/src/s2/util/math/exactfloat/exactfloat.h +646 -0
  460. package/third_party/s2geometry/src/s2/util/math/mathutil.cc +75 -0
  461. package/third_party/s2geometry/src/s2/util/math/mathutil.h +189 -0
  462. package/third_party/s2geometry/src/s2/util/math/matrix3x3.h +574 -0
  463. package/third_party/s2geometry/src/s2/util/math/vector.h +569 -0
  464. package/third_party/s2geometry/src/s2/util/math/vector3_hash.h +54 -0
  465. package/third_party/s2geometry/src/s2/util/units/length-units.cc +21 -0
  466. package/third_party/s2geometry/src/s2/util/units/length-units.h +135 -0
  467. package/third_party/s2geometry/src/s2/util/units/physical-units.h +313 -0
  468. package/third_party/s2geometry/src/s2/value_lexicon.h +234 -0
  469. package/third_party/s2geometry/src/s2/value_lexicon_test.cc +121 -0
  470. package/third_party/s2geometry/third_party/cmake/FindGFlags.cmake +48 -0
  471. package/third_party/s2geometry/third_party/cmake/FindGlog.cmake +48 -0
@@ -0,0 +1,1509 @@
1
+ // Copyright 2005 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS-IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+
16
+ // Author: ericv@google.com (Eric Veach)
17
+
18
+ #include "s2/s2loop.h"
19
+
20
+ #include <algorithm>
21
+ #include <atomic>
22
+ #include <bitset>
23
+ #include <cfloat>
24
+ #include <cmath>
25
+ #include <set>
26
+ #include <utility>
27
+ #include <vector>
28
+
29
+ #include "s2/base/commandlineflags.h"
30
+ #include "s2/base/integral_types.h"
31
+ #include "s2/base/logging.h"
32
+ #include "s2/mutable_s2shape_index.h"
33
+ #include "s2/r1interval.h"
34
+ #include "s2/s1angle.h"
35
+ #include "s2/s1interval.h"
36
+ #include "s2/s2cap.h"
37
+ #include "s2/s2cell.h"
38
+ #include "s2/s2centroids.h"
39
+ #include "s2/s2closest_edge_query.h"
40
+ #include "s2/s2coords.h"
41
+ #include "s2/s2crossing_edge_query.h"
42
+ #include "s2/s2debug.h"
43
+ #include "s2/s2edge_clipping.h"
44
+ #include "s2/s2edge_crosser.h"
45
+ #include "s2/s2edge_distances.h"
46
+ #include "s2/s2error.h"
47
+ #include "s2/s2latlng_rect_bounder.h"
48
+ #include "s2/s2measures.h"
49
+ #include "s2/s2padded_cell.h"
50
+ #include "s2/s2point_compression.h"
51
+ #include "s2/s2pointutil.h"
52
+ #include "s2/s2predicates.h"
53
+ #include "s2/s2shape_index.h"
54
+ #include "s2/s2shapeutil_visit_crossing_edge_pairs.h"
55
+ #include "s2/s2wedge_relations.h"
56
+ #include "s2/third_party/absl/memory/memory.h"
57
+ #include "s2/third_party/absl/types/span.h"
58
+ #include "s2/util/coding/coder.h"
59
+ #include "s2/util/coding/coder.h"
60
+ #include "s2/util/math/matrix3x3.h"
61
+
62
+ using absl::make_unique;
63
+ using absl::MakeSpan;
64
+ using std::pair;
65
+ using std::set;
66
+ using std::vector;
67
+
68
+ S2_DEFINE_bool(
69
+ s2loop_lazy_indexing, true,
70
+ "Build the S2ShapeIndex only when it is first needed. This can save "
71
+ "significant amounts of memory and time when geometry is constructed but "
72
+ "never queried, for example when loops are passed directly to S2Polygon, "
73
+ "or when geometry is being converted from one format to another.");
74
+
75
+ // The maximum number of vertices we'll allow when decoding a loop.
76
+ // The default value of 50 million is about 30x bigger than the number of
77
+ S2_DEFINE_int32(
78
+ s2polygon_decode_max_num_vertices, 50000000,
79
+ "The upper limit on the number of loops that are allowed by the "
80
+ "S2Polygon::Decode method.");
81
+
82
+ static const unsigned char kCurrentLosslessEncodingVersionNumber = 1;
83
+
84
+ // Boolean properties for compressed loops.
85
+ // See GetCompressedEncodingProperties.
86
+ enum CompressedLoopProperty {
87
+ kOriginInside,
88
+ kBoundEncoded,
89
+ kNumProperties
90
+ };
91
+
92
+ S2Loop::S2Loop() {
93
+ // The loop is not valid until Init() is called.
94
+ }
95
+
96
+ S2Loop::S2Loop(const vector<S2Point>& vertices)
97
+ : S2Loop(vertices, S2Debug::ALLOW) {}
98
+
99
+ S2Loop::S2Loop(const vector<S2Point>& vertices,
100
+ S2Debug override)
101
+ : s2debug_override_(override) {
102
+ Init(vertices);
103
+ }
104
+
105
+ void S2Loop::set_s2debug_override(S2Debug override) {
106
+ s2debug_override_ = override;
107
+ }
108
+
109
+ S2Debug S2Loop::s2debug_override() const {
110
+ return s2debug_override_;
111
+ }
112
+
113
+ void S2Loop::ClearIndex() {
114
+ unindexed_contains_calls_.store(0, std::memory_order_relaxed);
115
+ index_.Clear();
116
+ }
117
+
118
+ void S2Loop::Init(const vector<S2Point>& vertices) {
119
+ ClearIndex();
120
+ if (owns_vertices_) delete[] vertices_;
121
+ num_vertices_ = vertices.size();
122
+ vertices_ = new S2Point[num_vertices_];
123
+ std::copy(vertices.begin(), vertices.end(), &vertices_[0]);
124
+ owns_vertices_ = true;
125
+ InitOriginAndBound();
126
+ }
127
+
128
+ bool S2Loop::IsValid() const {
129
+ S2Error error;
130
+ if (FindValidationError(&error)) {
131
+ S2_LOG_IF(ERROR, FLAGS_s2debug) << error;
132
+ return false;
133
+ }
134
+ return true;
135
+ }
136
+
137
+ bool S2Loop::FindValidationError(S2Error* error) const {
138
+ return (FindValidationErrorNoIndex(error) ||
139
+ s2shapeutil::FindSelfIntersection(index_, error));
140
+ }
141
+
142
+ bool S2Loop::FindValidationErrorNoIndex(S2Error* error) const {
143
+ // subregion_bound_ must be at least as large as bound_. (This is an
144
+ // internal consistency check rather than a test of client data.)
145
+ S2_DCHECK(subregion_bound_.Contains(bound_));
146
+
147
+ // All vertices must be unit length. (Unfortunately this check happens too
148
+ // late in debug mode, because S2Loop construction calls s2pred::Sign which
149
+ // expects vertices to be unit length. But it is still a useful check in
150
+ // optimized builds.)
151
+ for (int i = 0; i < num_vertices(); ++i) {
152
+ if (!S2::IsUnitLength(vertex(i))) {
153
+ error->Init(S2Error::NOT_UNIT_LENGTH,
154
+ "Vertex %d is not unit length", i);
155
+ return true;
156
+ }
157
+ }
158
+ // Loops must have at least 3 vertices (except for the empty and full loops).
159
+ if (num_vertices() < 3) {
160
+ if (is_empty_or_full()) {
161
+ return false; // Skip remaining tests.
162
+ }
163
+ error->Init(S2Error::LOOP_NOT_ENOUGH_VERTICES,
164
+ "Non-empty, non-full loops must have at least 3 vertices");
165
+ return true;
166
+ }
167
+ // Loops are not allowed to have any duplicate vertices or edge crossings.
168
+ // We split this check into two parts. First we check that no edge is
169
+ // degenerate (identical endpoints). Then we check that there are no
170
+ // intersections between non-adjacent edges (including at vertices). The
171
+ // second part needs the S2ShapeIndex, so it does not fall within the scope
172
+ // of this method.
173
+ for (int i = 0; i < num_vertices(); ++i) {
174
+ if (vertex(i) == vertex(i+1)) {
175
+ error->Init(S2Error::DUPLICATE_VERTICES,
176
+ "Edge %d is degenerate (duplicate vertex)", i);
177
+ return true;
178
+ }
179
+ if (vertex(i) == -vertex(i + 1)) {
180
+ error->Init(S2Error::ANTIPODAL_VERTICES,
181
+ "Vertices %d and %d are antipodal", i,
182
+ (i + 1) % num_vertices());
183
+ return true;
184
+ }
185
+ }
186
+ return false;
187
+ }
188
+
189
+ void S2Loop::InitOriginAndBound() {
190
+ if (num_vertices() < 3) {
191
+ // Check for the special empty and full loops (which have one vertex).
192
+ if (!is_empty_or_full()) {
193
+ origin_inside_ = false;
194
+ return; // Bail out without trying to access non-existent vertices.
195
+ }
196
+ // If the vertex is in the southern hemisphere then the loop is full,
197
+ // otherwise it is empty.
198
+ origin_inside_ = (vertex(0).z() < 0);
199
+ } else {
200
+ // Point containment testing is done by counting edge crossings starting
201
+ // at a fixed point on the sphere (S2::Origin()). Historically this was
202
+ // important, but it is now no longer necessary, and it may be worthwhile
203
+ // experimenting with using a loop vertex as the reference point. In any
204
+ // case, we need to know whether the reference point (S2::Origin) is
205
+ // inside or outside the loop before we can construct the S2ShapeIndex.
206
+ // We do this by first guessing that it is outside, and then seeing
207
+ // whether we get the correct containment result for vertex 1. If the
208
+ // result is incorrect, the origin must be inside the loop.
209
+ //
210
+ // A loop with consecutive vertices A,B,C contains vertex B if and only if
211
+ // the fixed vector R = S2::Ortho(B) is contained by the wedge ABC. The
212
+ // wedge is closed at A and open at C, i.e. the point B is inside the loop
213
+ // if A=R but not if C=R. This convention is required for compatibility
214
+ // with S2::VertexCrossing. (Note that we can't use S2::Origin()
215
+ // as the fixed vector because of the possibility that B == S2::Origin().)
216
+ //
217
+ // TODO(ericv): Investigate using vertex(0) as the reference point.
218
+
219
+ origin_inside_ = false; // Initialize before calling Contains().
220
+ bool v1_inside = s2pred::OrderedCCW(S2::Ortho(vertex(1)), vertex(0),
221
+ vertex(2), vertex(1));
222
+ // Note that Contains(S2Point) only does a bounds check once InitIndex()
223
+ // has been called, so it doesn't matter that bound_ is undefined here.
224
+ if (v1_inside != Contains(vertex(1))) {
225
+ origin_inside_ = true;
226
+ }
227
+ }
228
+ // We *must* call InitBound() before InitIndex(), because InitBound() calls
229
+ // Contains(S2Point), and Contains(S2Point) does a bounds check whenever the
230
+ // index is not fresh (i.e., the loop has been added to the index but the
231
+ // index has not been updated yet).
232
+ //
233
+ // TODO(ericv): When fewer S2Loop methods depend on internal bounds checks,
234
+ // consider computing the bound on demand as well.
235
+ InitBound();
236
+ InitIndex();
237
+ }
238
+
239
+ void S2Loop::InitBound() {
240
+ // Check for the special empty and full loops.
241
+ if (is_empty_or_full()) {
242
+ if (is_empty()) {
243
+ subregion_bound_ = bound_ = S2LatLngRect::Empty();
244
+ } else {
245
+ subregion_bound_ = bound_ = S2LatLngRect::Full();
246
+ }
247
+ return;
248
+ }
249
+
250
+ // The bounding rectangle of a loop is not necessarily the same as the
251
+ // bounding rectangle of its vertices. First, the maximal latitude may be
252
+ // attained along the interior of an edge. Second, the loop may wrap
253
+ // entirely around the sphere (e.g. a loop that defines two revolutions of a
254
+ // candy-cane stripe). Third, the loop may include one or both poles.
255
+ // Note that a small clockwise loop near the equator contains both poles.
256
+
257
+ S2LatLngRectBounder bounder;
258
+ for (int i = 0; i <= num_vertices(); ++i) {
259
+ bounder.AddPoint(vertex(i));
260
+ }
261
+ S2LatLngRect b = bounder.GetBound();
262
+ if (Contains(S2Point(0, 0, 1))) {
263
+ b = S2LatLngRect(R1Interval(b.lat().lo(), M_PI_2), S1Interval::Full());
264
+ }
265
+ // If a loop contains the south pole, then either it wraps entirely
266
+ // around the sphere (full longitude range), or it also contains the
267
+ // north pole in which case b.lng().is_full() due to the test above.
268
+ // Either way, we only need to do the south pole containment test if
269
+ // b.lng().is_full().
270
+ if (b.lng().is_full() && Contains(S2Point(0, 0, -1))) {
271
+ b.mutable_lat()->set_lo(-M_PI_2);
272
+ }
273
+ bound_ = b;
274
+ subregion_bound_ = S2LatLngRectBounder::ExpandForSubregions(bound_);
275
+ }
276
+
277
+ void S2Loop::InitIndex() {
278
+ index_.Add(make_unique<Shape>(this));
279
+ if (!FLAGS_s2loop_lazy_indexing) {
280
+ index_.ForceBuild();
281
+ }
282
+ if (FLAGS_s2debug && s2debug_override_ == S2Debug::ALLOW) {
283
+ // Note that FLAGS_s2debug is false in optimized builds (by default).
284
+ S2_CHECK(IsValid());
285
+ }
286
+ }
287
+
288
+ S2Loop::S2Loop(const S2Cell& cell)
289
+ : depth_(0),
290
+ num_vertices_(4),
291
+ vertices_(new S2Point[num_vertices_]),
292
+ owns_vertices_(true),
293
+ s2debug_override_(S2Debug::ALLOW),
294
+ unindexed_contains_calls_(0) {
295
+ for (int i = 0; i < 4; ++i) {
296
+ vertices_[i] = cell.GetVertex(i);
297
+ }
298
+ // We recompute the bounding rectangle ourselves, since S2Cell uses a
299
+ // different method and we need all the bounds to be consistent.
300
+ InitOriginAndBound();
301
+ }
302
+
303
+ S2Loop::~S2Loop() {
304
+ if (owns_vertices_) delete[] vertices_;
305
+ }
306
+
307
+ S2Loop::S2Loop(const S2Loop& src)
308
+ : depth_(src.depth_),
309
+ num_vertices_(src.num_vertices_),
310
+ vertices_(new S2Point[num_vertices_]),
311
+ owns_vertices_(true),
312
+ s2debug_override_(src.s2debug_override_),
313
+ origin_inside_(src.origin_inside_),
314
+ unindexed_contains_calls_(0),
315
+ bound_(src.bound_),
316
+ subregion_bound_(src.subregion_bound_) {
317
+ std::copy(&src.vertices_[0], &src.vertices_[num_vertices_], &vertices_[0]);
318
+ InitIndex();
319
+ }
320
+
321
+ S2Loop* S2Loop::Clone() const {
322
+ return new S2Loop(*this);
323
+ }
324
+
325
+ int S2Loop::FindVertex(const S2Point& p) const {
326
+ if (num_vertices() < 10) {
327
+ // Exhaustive search. Return value must be in the range [1..N].
328
+ for (int i = 1; i <= num_vertices(); ++i) {
329
+ if (vertex(i) == p) return i;
330
+ }
331
+ return -1;
332
+ }
333
+ MutableS2ShapeIndex::Iterator it(&index_);
334
+ if (!it.Locate(p)) return -1;
335
+
336
+ const S2ClippedShape& a_clipped = it.cell().clipped(0);
337
+ for (int i = a_clipped.num_edges() - 1; i >= 0; --i) {
338
+ int ai = a_clipped.edge(i);
339
+ // Return value must be in the range [1..N].
340
+ if (vertex(ai) == p) return (ai == 0) ? num_vertices() : ai;
341
+ if (vertex(ai+1) == p) return ai+1;
342
+ }
343
+ return -1;
344
+ }
345
+
346
+ bool S2Loop::IsNormalized() const {
347
+ // Optimization: if the longitude span is less than 180 degrees, then the
348
+ // loop covers less than half the sphere and is therefore normalized.
349
+ if (bound_.lng().GetLength() < M_PI) return true;
350
+
351
+ return S2::IsNormalized(vertices_span());
352
+ }
353
+
354
+ void S2Loop::Normalize() {
355
+ S2_CHECK(owns_vertices_);
356
+ if (!IsNormalized()) Invert();
357
+ S2_DCHECK(IsNormalized());
358
+ }
359
+
360
+ void S2Loop::Invert() {
361
+ S2_CHECK(owns_vertices_);
362
+ ClearIndex();
363
+ if (is_empty_or_full()) {
364
+ vertices_[0] = is_full() ? kEmptyVertex() : kFullVertex();
365
+ } else {
366
+ std::reverse(vertices_, vertices_ + num_vertices());
367
+ }
368
+ // origin_inside_ must be set correctly before building the S2ShapeIndex.
369
+ origin_inside_ ^= true;
370
+ if (bound_.lat().lo() > -M_PI_2 && bound_.lat().hi() < M_PI_2) {
371
+ // The complement of this loop contains both poles.
372
+ subregion_bound_ = bound_ = S2LatLngRect::Full();
373
+ } else {
374
+ InitBound();
375
+ }
376
+ InitIndex();
377
+ }
378
+
379
+ double S2Loop::GetArea() const {
380
+ // S2Loop has its own convention for empty and full loops.
381
+ if (is_empty_or_full()) {
382
+ return contains_origin() ? (4 * M_PI) : 0;
383
+ }
384
+ return S2::GetArea(vertices_span());
385
+ }
386
+
387
+ S2Point S2Loop::GetCentroid() const {
388
+ // Empty and full loops are handled correctly.
389
+ return S2::GetCentroid(vertices_span());
390
+ }
391
+
392
+ S2::LoopOrder S2Loop::GetCanonicalLoopOrder() const {
393
+ return S2::GetCanonicalLoopOrder(vertices_span());
394
+ }
395
+
396
+ S1Angle S2Loop::GetDistance(const S2Point& x) const {
397
+ // Note that S2Loop::Contains(S2Point) is slightly more efficient than the
398
+ // generic version used by S2ClosestEdgeQuery.
399
+ if (Contains(x)) return S1Angle::Zero();
400
+ return GetDistanceToBoundary(x);
401
+ }
402
+
403
+ S1Angle S2Loop::GetDistanceToBoundary(const S2Point& x) const {
404
+ S2ClosestEdgeQuery::Options options;
405
+ options.set_include_interiors(false);
406
+ S2ClosestEdgeQuery::PointTarget t(x);
407
+ return S2ClosestEdgeQuery(&index_, options).GetDistance(&t).ToAngle();
408
+ }
409
+
410
+ S2Point S2Loop::Project(const S2Point& x) const {
411
+ if (Contains(x)) return x;
412
+ return ProjectToBoundary(x);
413
+ }
414
+
415
+ S2Point S2Loop::ProjectToBoundary(const S2Point& x) const {
416
+ S2ClosestEdgeQuery::Options options;
417
+ options.set_include_interiors(false);
418
+ S2ClosestEdgeQuery q(&index_, options);
419
+ S2ClosestEdgeQuery::PointTarget target(x);
420
+ S2ClosestEdgeQuery::Result edge = q.FindClosestEdge(&target);
421
+ return q.Project(x, edge);
422
+ }
423
+
424
+ double S2Loop::GetCurvature() const {
425
+ // S2Loop has its own convention for empty and full loops. For such loops,
426
+ // we return the limit value as the area approaches 0 or 4*Pi respectively.
427
+ if (is_empty_or_full()) {
428
+ return contains_origin() ? (-2 * M_PI) : (2 * M_PI);
429
+ }
430
+ return S2::GetCurvature(vertices_span());
431
+ }
432
+
433
+ double S2Loop::GetCurvatureMaxError() const {
434
+ return S2::GetCurvatureMaxError(vertices_span());
435
+ }
436
+
437
+ S2Cap S2Loop::GetCapBound() const {
438
+ return bound_.GetCapBound();
439
+ }
440
+
441
+ bool S2Loop::Contains(const S2Cell& target) const {
442
+ MutableS2ShapeIndex::Iterator it(&index_);
443
+ S2ShapeIndex::CellRelation relation = it.Locate(target.id());
444
+
445
+ // If "target" is disjoint from all index cells, it is not contained.
446
+ // Similarly, if "target" is subdivided into one or more index cells then it
447
+ // is not contained, since index cells are subdivided only if they (nearly)
448
+ // intersect a sufficient number of edges. (But note that if "target" itself
449
+ // is an index cell then it may be contained, since it could be a cell with
450
+ // no edges in the loop interior.)
451
+ if (relation != S2ShapeIndex::INDEXED) return false;
452
+
453
+ // Otherwise check if any edges intersect "target".
454
+ if (BoundaryApproxIntersects(it, target)) return false;
455
+
456
+ // Otherwise check if the loop contains the center of "target".
457
+ return Contains(it, target.GetCenter());
458
+ }
459
+
460
+ bool S2Loop::MayIntersect(const S2Cell& target) const {
461
+ MutableS2ShapeIndex::Iterator it(&index_);
462
+ S2ShapeIndex::CellRelation relation = it.Locate(target.id());
463
+
464
+ // If "target" does not overlap any index cell, there is no intersection.
465
+ if (relation == S2ShapeIndex::DISJOINT) return false;
466
+
467
+ // If "target" is subdivided into one or more index cells, there is an
468
+ // intersection to within the S2ShapeIndex error bound (see Contains).
469
+ if (relation == S2ShapeIndex::SUBDIVIDED) return true;
470
+
471
+ // If "target" is an index cell, there is an intersection because index cells
472
+ // are created only if they have at least one edge or they are entirely
473
+ // contained by the loop.
474
+ if (it.id() == target.id()) return true;
475
+
476
+ // Otherwise check if any edges intersect "target".
477
+ if (BoundaryApproxIntersects(it, target)) return true;
478
+
479
+ // Otherwise check if the loop contains the center of "target".
480
+ return Contains(it, target.GetCenter());
481
+ }
482
+
483
+ bool S2Loop::BoundaryApproxIntersects(const MutableS2ShapeIndex::Iterator& it,
484
+ const S2Cell& target) const {
485
+ S2_DCHECK(it.id().contains(target.id()));
486
+ const S2ClippedShape& a_clipped = it.cell().clipped(0);
487
+ int a_num_edges = a_clipped.num_edges();
488
+
489
+ // If there are no edges, there is no intersection.
490
+ if (a_num_edges == 0) return false;
491
+
492
+ // We can save some work if "target" is the index cell itself.
493
+ if (it.id() == target.id()) return true;
494
+
495
+ // Otherwise check whether any of the edges intersect "target".
496
+ static const double kMaxError = (S2::kFaceClipErrorUVCoord +
497
+ S2::kIntersectsRectErrorUVDist);
498
+ R2Rect bound = target.GetBoundUV().Expanded(kMaxError);
499
+ for (int i = 0; i < a_num_edges; ++i) {
500
+ int ai = a_clipped.edge(i);
501
+ R2Point v0, v1;
502
+ if (S2::ClipToPaddedFace(vertex(ai), vertex(ai+1), target.face(),
503
+ kMaxError, &v0, &v1) &&
504
+ S2::IntersectsRect(v0, v1, bound)) {
505
+ return true;
506
+ }
507
+ }
508
+ return false;
509
+ }
510
+
511
+ bool S2Loop::Contains(const S2Point& p) const {
512
+ // NOTE(ericv): A bounds check slows down this function by about 50%. It is
513
+ // worthwhile only when it might allow us to delay building the index.
514
+ if (!index_.is_fresh() && !bound_.Contains(p)) return false;
515
+
516
+ // For small loops it is faster to just check all the crossings. We also
517
+ // use this method during loop initialization because InitOriginAndBound()
518
+ // calls Contains() before InitIndex(). Otherwise, we keep track of the
519
+ // number of calls to Contains() and only build the index when enough calls
520
+ // have been made so that we think it is worth the effort. Note that the
521
+ // code below is structured so that if many calls are made in parallel only
522
+ // one thread builds the index, while the rest continue using brute force
523
+ // until the index is actually available.
524
+ //
525
+ // The constants below were tuned using the benchmarks. It turns out that
526
+ // building the index costs roughly 50x as much as Contains(). (The ratio
527
+ // increases slowly from 46x with 64 points to 61x with 256k points.) The
528
+ // textbook approach to this problem would be to wait until the cumulative
529
+ // time we would have saved with an index approximately equals the cost of
530
+ // building the index, and then build it. (This gives the optimal
531
+ // competitive ratio of 2; look up "competitive algorithms" for details.)
532
+ // We set the limit somewhat lower than this (20 rather than 50) because
533
+ // building the index may be forced anyway by other API calls, and so we
534
+ // want to err on the side of building it too early.
535
+
536
+ static const int kMaxBruteForceVertices = 32;
537
+ static const int kMaxUnindexedContainsCalls = 20; // See notes above.
538
+ if (index_.num_shape_ids() == 0 || // InitIndex() not called yet
539
+ num_vertices() <= kMaxBruteForceVertices ||
540
+ (!index_.is_fresh() &&
541
+ ++unindexed_contains_calls_ != kMaxUnindexedContainsCalls)) {
542
+ return BruteForceContains(p);
543
+ }
544
+ // Otherwise we look up the S2ShapeIndex cell containing this point. Note
545
+ // the index is built automatically the first time an iterator is created.
546
+ MutableS2ShapeIndex::Iterator it(&index_);
547
+ if (!it.Locate(p)) return false;
548
+ return Contains(it, p);
549
+ }
550
+
551
+ bool S2Loop::BruteForceContains(const S2Point& p) const {
552
+ // Empty and full loops don't need a special case, but invalid loops with
553
+ // zero vertices do, so we might as well handle them all at once.
554
+ if (num_vertices() < 3) return origin_inside_;
555
+
556
+ S2Point origin = S2::Origin();
557
+ S2EdgeCrosser crosser(&origin, &p, &vertex(0));
558
+ bool inside = origin_inside_;
559
+ for (int i = 1; i <= num_vertices(); ++i) {
560
+ inside ^= crosser.EdgeOrVertexCrossing(&vertex(i));
561
+ }
562
+ return inside;
563
+ }
564
+
565
+ bool S2Loop::Contains(const MutableS2ShapeIndex::Iterator& it,
566
+ const S2Point& p) const {
567
+ // Test containment by drawing a line segment from the cell center to the
568
+ // given point and counting edge crossings.
569
+ const S2ClippedShape& a_clipped = it.cell().clipped(0);
570
+ bool inside = a_clipped.contains_center();
571
+ int a_num_edges = a_clipped.num_edges();
572
+ if (a_num_edges > 0) {
573
+ S2Point center = it.center();
574
+ S2EdgeCrosser crosser(&center, &p);
575
+ int ai_prev = -2;
576
+ for (int i = 0; i < a_num_edges; ++i) {
577
+ int ai = a_clipped.edge(i);
578
+ if (ai != ai_prev + 1) crosser.RestartAt(&vertex(ai));
579
+ ai_prev = ai;
580
+ inside ^= crosser.EdgeOrVertexCrossing(&vertex(ai+1));
581
+ }
582
+ }
583
+ return inside;
584
+ }
585
+
586
+ void S2Loop::Encode(Encoder* const encoder) const {
587
+ encoder->Ensure(num_vertices_ * sizeof(*vertices_) + 20); // sufficient
588
+
589
+ encoder->put8(kCurrentLosslessEncodingVersionNumber);
590
+ encoder->put32(num_vertices_);
591
+ encoder->putn(vertices_, sizeof(*vertices_) * num_vertices_);
592
+ encoder->put8(origin_inside_);
593
+ encoder->put32(depth_);
594
+ S2_DCHECK_GE(encoder->avail(), 0);
595
+
596
+ bound_.Encode(encoder);
597
+ }
598
+
599
+ bool S2Loop::Decode(Decoder* const decoder) {
600
+ if (decoder->avail() < sizeof(unsigned char)) return false;
601
+ unsigned char version = decoder->get8();
602
+ switch (version) {
603
+ case kCurrentLosslessEncodingVersionNumber:
604
+ return DecodeInternal(decoder, false);
605
+ }
606
+ return false;
607
+ }
608
+
609
+ bool S2Loop::DecodeWithinScope(Decoder* const decoder) {
610
+ if (decoder->avail() < sizeof(unsigned char)) return false;
611
+ unsigned char version = decoder->get8();
612
+ switch (version) {
613
+ case kCurrentLosslessEncodingVersionNumber:
614
+ return DecodeInternal(decoder, true);
615
+ }
616
+ return false;
617
+ }
618
+
619
+ bool S2Loop::DecodeInternal(Decoder* const decoder,
620
+ bool within_scope) {
621
+ // Perform all checks before modifying vertex state. Empty loops are
622
+ // explicitly allowed here: a newly created loop has zero vertices
623
+ // and such loops encode and decode properly.
624
+ if (decoder->avail() < sizeof(uint32)) return false;
625
+ const uint32 num_vertices = decoder->get32();
626
+ if (num_vertices > FLAGS_s2polygon_decode_max_num_vertices) {
627
+ return false;
628
+ }
629
+ if (decoder->avail() < (num_vertices * sizeof(*vertices_) +
630
+ sizeof(uint8) + sizeof(uint32))) {
631
+ return false;
632
+ }
633
+ ClearIndex();
634
+ if (owns_vertices_) delete[] vertices_;
635
+ num_vertices_ = num_vertices;
636
+
637
+ // x86 can do unaligned floating-point reads; however, many other
638
+ // platforms cannot. Do not use the zero-copy version if we are on
639
+ // an architecture that does not support unaligned reads, and the
640
+ // pointer is not correctly aligned.
641
+ #if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || \
642
+ defined(_M_IX86)
643
+ bool is_misaligned = false;
644
+ #else
645
+ bool is_misaligned =
646
+ reinterpret_cast<intptr_t>(decoder->ptr()) % sizeof(double) != 0;
647
+ #endif
648
+ if (within_scope && !is_misaligned) {
649
+ vertices_ = const_cast<S2Point *>(reinterpret_cast<const S2Point*>(
650
+ decoder->ptr()));
651
+ decoder->skip(num_vertices_ * sizeof(*vertices_));
652
+ owns_vertices_ = false;
653
+ } else {
654
+ vertices_ = new S2Point[num_vertices_];
655
+ decoder->getn(vertices_, num_vertices_ * sizeof(*vertices_));
656
+ owns_vertices_ = true;
657
+ }
658
+ origin_inside_ = decoder->get8();
659
+ depth_ = decoder->get32();
660
+ if (!bound_.Decode(decoder)) return false;
661
+ subregion_bound_ = S2LatLngRectBounder::ExpandForSubregions(bound_);
662
+
663
+ // An initialized loop will have some non-zero count of vertices. A default
664
+ // (uninitialized) has zero vertices. This code supports encoding and
665
+ // decoding of uninitialized loops, but we only want to call InitIndex for
666
+ // initialized loops. Otherwise we defer InitIndex until the call to Init().
667
+ if (num_vertices > 0) {
668
+ InitIndex();
669
+ }
670
+
671
+ return true;
672
+ }
673
+
674
+ // LoopRelation is an abstract class that defines a relationship between two
675
+ // loops (Contains, Intersects, or CompareBoundary).
676
+ class LoopRelation {
677
+ public:
678
+ LoopRelation() {}
679
+ virtual ~LoopRelation() {}
680
+
681
+ // Optionally, a_target() and b_target() can specify an early-exit condition
682
+ // for the loop relation. If any point P is found such that
683
+ //
684
+ // A.Contains(P) == a_crossing_target() &&
685
+ // B.Contains(P) == b_crossing_target()
686
+ //
687
+ // then the loop relation is assumed to be the same as if a pair of crossing
688
+ // edges were found. For example, the Contains() relation has
689
+ //
690
+ // a_crossing_target() == 0
691
+ // b_crossing_target() == 1
692
+ //
693
+ // because if A.Contains(P) == 0 (false) and B.Contains(P) == 1 (true) for
694
+ // any point P, then it is equivalent to finding an edge crossing (i.e.,
695
+ // since Contains() returns false in both cases).
696
+ //
697
+ // Loop relations that do not have an early-exit condition of this form
698
+ // should return -1 for both crossing targets.
699
+ virtual int a_crossing_target() const = 0;
700
+ virtual int b_crossing_target() const = 0;
701
+
702
+ // Given a vertex "ab1" that is shared between the two loops, return true if
703
+ // the two associated wedges (a0, ab1, b2) and (b0, ab1, b2) are equivalent
704
+ // to an edge crossing. The loop relation is also allowed to maintain its
705
+ // own internal state, and can return true if it observes any sequence of
706
+ // wedges that are equivalent to an edge crossing.
707
+ virtual bool WedgesCross(const S2Point& a0, const S2Point& ab1,
708
+ const S2Point& a2, const S2Point& b0,
709
+ const S2Point& b2) = 0;
710
+ };
711
+
712
+ // RangeIterator is a wrapper over MutableS2ShapeIndex::Iterator with extra
713
+ // methods that are useful for merging the contents of two or more
714
+ // S2ShapeIndexes.
715
+ class RangeIterator {
716
+ public:
717
+ // Construct a new RangeIterator positioned at the first cell of the index.
718
+ explicit RangeIterator(const MutableS2ShapeIndex* index)
719
+ : it_(index, S2ShapeIndex::BEGIN) {
720
+ Refresh();
721
+ }
722
+
723
+ // The current S2CellId and cell contents.
724
+ S2CellId id() const { return it_.id(); }
725
+ const S2ShapeIndexCell& cell() const { return it_.cell(); }
726
+
727
+ // The min and max leaf cell ids covered by the current cell. If Done() is
728
+ // true, these methods return a value larger than any valid cell id.
729
+ S2CellId range_min() const { return range_min_; }
730
+ S2CellId range_max() const { return range_max_; }
731
+
732
+ // Various other convenience methods for the current cell.
733
+ const S2ClippedShape& clipped() const { return cell().clipped(0); }
734
+
735
+ int num_edges() const { return clipped().num_edges(); }
736
+ bool contains_center() const { return clipped().contains_center(); }
737
+
738
+ void Next() { it_.Next(); Refresh(); }
739
+ bool Done() { return it_.done(); }
740
+
741
+ // Position the iterator at the first cell that overlaps or follows
742
+ // "target", i.e. such that range_max() >= target.range_min().
743
+ void SeekTo(const RangeIterator& target) {
744
+ it_.Seek(target.range_min());
745
+ // If the current cell does not overlap "target", it is possible that the
746
+ // previous cell is the one we are looking for. This can only happen when
747
+ // the previous cell contains "target" but has a smaller S2CellId.
748
+ if (it_.done() || it_.id().range_min() > target.range_max()) {
749
+ if (it_.Prev() && it_.id().range_max() < target.id()) it_.Next();
750
+ }
751
+ Refresh();
752
+ }
753
+
754
+ // Position the iterator at the first cell that follows "target", i.e. the
755
+ // first cell such that range_min() > target.range_max().
756
+ void SeekBeyond(const RangeIterator& target) {
757
+ it_.Seek(target.range_max().next());
758
+ if (!it_.done() && it_.id().range_min() <= target.range_max()) {
759
+ it_.Next();
760
+ }
761
+ Refresh();
762
+ }
763
+
764
+ private:
765
+ // Updates internal state after the iterator has been repositioned.
766
+ void Refresh() {
767
+ range_min_ = id().range_min();
768
+ range_max_ = id().range_max();
769
+ }
770
+
771
+ MutableS2ShapeIndex::Iterator it_;
772
+ S2CellId range_min_, range_max_;
773
+ };
774
+
775
+ // LoopCrosser is a helper class for determining whether two loops cross.
776
+ // It is instantiated twice for each pair of loops to be tested, once for the
777
+ // pair (A,B) and once for the pair (B,A), in order to be able to process
778
+ // edges in either loop nesting order.
779
+ class LoopCrosser {
780
+ public:
781
+ // If "swapped" is true, the loops A and B have been swapped. This affects
782
+ // how arguments are passed to the given loop relation, since for example
783
+ // A.Contains(B) is not the same as B.Contains(A).
784
+ LoopCrosser(const S2Loop& a, const S2Loop& b,
785
+ LoopRelation* relation, bool swapped)
786
+ : a_(a), b_(b), relation_(relation), swapped_(swapped),
787
+ a_crossing_target_(relation->a_crossing_target()),
788
+ b_crossing_target_(relation->b_crossing_target()),
789
+ b_query_(&b.index_) {
790
+ using std::swap;
791
+ if (swapped) swap(a_crossing_target_, b_crossing_target_);
792
+ }
793
+
794
+ // Return the crossing targets for the loop relation, taking into account
795
+ // whether the loops have been swapped.
796
+ int a_crossing_target() const { return a_crossing_target_; }
797
+ int b_crossing_target() const { return b_crossing_target_; }
798
+
799
+ // Given two iterators positioned such that ai->id().Contains(bi->id()),
800
+ // return true if there is a crossing relationship anywhere within ai->id().
801
+ // Specifically, this method returns true if there is an edge crossing, a
802
+ // wedge crossing, or a point P that matches both "crossing targets".
803
+ // Advances both iterators past ai->id().
804
+ bool HasCrossingRelation(RangeIterator* ai, RangeIterator* bi);
805
+
806
+ // Given two index cells, return true if there are any edge crossings or
807
+ // wedge crossings within those cells.
808
+ bool CellCrossesCell(const S2ClippedShape& a_clipped,
809
+ const S2ClippedShape& b_clipped);
810
+
811
+ private:
812
+ // Given two iterators positioned such that ai->id().Contains(bi->id()),
813
+ // return true if there is an edge crossing or wedge crosssing anywhere
814
+ // within ai->id(). Advances "bi" (only) past ai->id().
815
+ bool HasCrossing(RangeIterator* ai, RangeIterator* bi);
816
+
817
+ // Given an index cell of A, return true if there are any edge or wedge
818
+ // crossings with any index cell of B contained within "b_id".
819
+ bool CellCrossesAnySubcell(const S2ClippedShape& a_clipped, S2CellId b_id);
820
+
821
+ // Prepare to check the given edge of loop A for crossings.
822
+ void StartEdge(int aj);
823
+
824
+ // Check the current edge of loop A for crossings with all edges of the
825
+ // given index cell of loop B.
826
+ bool EdgeCrossesCell(const S2ClippedShape& b_clipped);
827
+
828
+ const S2Loop& a_;
829
+ const S2Loop& b_;
830
+ LoopRelation* const relation_;
831
+ const bool swapped_;
832
+ int a_crossing_target_, b_crossing_target_;
833
+
834
+ // State maintained by StartEdge() and EdgeCrossesCell().
835
+ S2EdgeCrosser crosser_;
836
+ int aj_, bj_prev_;
837
+
838
+ // Temporary data declared here to avoid repeated memory allocations.
839
+ S2CrossingEdgeQuery b_query_;
840
+ vector<const S2ShapeIndexCell*> b_cells_;
841
+ };
842
+
843
+ inline void LoopCrosser::StartEdge(int aj) {
844
+ // Start testing the given edge of A for crossings.
845
+ crosser_.Init(&a_.vertex(aj), &a_.vertex(aj+1));
846
+ aj_ = aj;
847
+ bj_prev_ = -2;
848
+ }
849
+
850
+ inline bool LoopCrosser::EdgeCrossesCell(const S2ClippedShape& b_clipped) {
851
+ // Test the current edge of A against all edges of "b_clipped".
852
+ int b_num_edges = b_clipped.num_edges();
853
+ for (int j = 0; j < b_num_edges; ++j) {
854
+ int bj = b_clipped.edge(j);
855
+ if (bj != bj_prev_ + 1) crosser_.RestartAt(&b_.vertex(bj));
856
+ bj_prev_ = bj;
857
+ int crossing = crosser_.CrossingSign(&b_.vertex(bj + 1));
858
+ if (crossing < 0) continue;
859
+ if (crossing > 0) return true;
860
+ // We only need to check each shared vertex once, so we only
861
+ // consider the case where a_vertex(aj_+1) == b_.vertex(bj+1).
862
+ if (a_.vertex(aj_+1) == b_.vertex(bj+1)) {
863
+ if (swapped_ ?
864
+ relation_->WedgesCross(
865
+ b_.vertex(bj), b_.vertex(bj+1), b_.vertex(bj+2),
866
+ a_.vertex(aj_), a_.vertex(aj_+2)) :
867
+ relation_->WedgesCross(
868
+ a_.vertex(aj_), a_.vertex(aj_+1), a_.vertex(aj_+2),
869
+ b_.vertex(bj), b_.vertex(bj+2))) {
870
+ return true;
871
+ }
872
+ }
873
+ }
874
+ return false;
875
+ }
876
+
877
+ bool LoopCrosser::CellCrossesCell(const S2ClippedShape& a_clipped,
878
+ const S2ClippedShape& b_clipped) {
879
+ // Test all edges of "a_clipped" against all edges of "b_clipped".
880
+ int a_num_edges = a_clipped.num_edges();
881
+ for (int i = 0; i < a_num_edges; ++i) {
882
+ StartEdge(a_clipped.edge(i));
883
+ if (EdgeCrossesCell(b_clipped)) return true;
884
+ }
885
+ return false;
886
+ }
887
+
888
+ bool LoopCrosser::CellCrossesAnySubcell(const S2ClippedShape& a_clipped,
889
+ S2CellId b_id) {
890
+ // Test all edges of "a_clipped" against all edges of B. The relevant B
891
+ // edges are guaranteed to be children of "b_id", which lets us find the
892
+ // correct index cells more efficiently.
893
+ S2PaddedCell b_root(b_id, 0);
894
+ int a_num_edges = a_clipped.num_edges();
895
+ for (int i = 0; i < a_num_edges; ++i) {
896
+ int aj = a_clipped.edge(i);
897
+ // Use an S2CrossingEdgeQuery starting at "b_root" to find the index cells
898
+ // of B that might contain crossing edges.
899
+ b_query_.GetCells(a_.vertex(aj), a_.vertex(aj+1), b_root, &b_cells_);
900
+ if (b_cells_.empty()) continue;
901
+ StartEdge(aj);
902
+ for (const S2ShapeIndexCell* b_cell : b_cells_) {
903
+ if (EdgeCrossesCell(b_cell->clipped(0))) return true;
904
+ }
905
+ }
906
+ return false;
907
+ }
908
+
909
+ bool LoopCrosser::HasCrossing(RangeIterator* ai, RangeIterator* bi) {
910
+ S2_DCHECK(ai->id().contains(bi->id()));
911
+ // If ai->id() intersects many edges of B, then it is faster to use
912
+ // S2CrossingEdgeQuery to narrow down the candidates. But if it intersects
913
+ // only a few edges, it is faster to check all the crossings directly.
914
+ // We handle this by advancing "bi" and keeping track of how many edges we
915
+ // would need to test.
916
+
917
+ static const int kEdgeQueryMinEdges = 20; // Tuned using benchmarks.
918
+ int total_edges = 0;
919
+ b_cells_.clear();
920
+ do {
921
+ if (bi->num_edges() > 0) {
922
+ total_edges += bi->num_edges();
923
+ if (total_edges >= kEdgeQueryMinEdges) {
924
+ // There are too many edges to test them directly, so use
925
+ // S2CrossingEdgeQuery.
926
+ if (CellCrossesAnySubcell(ai->clipped(), ai->id())) return true;
927
+ bi->SeekBeyond(*ai);
928
+ return false;
929
+ }
930
+ b_cells_.push_back(&bi->cell());
931
+ }
932
+ bi->Next();
933
+ } while (bi->id() <= ai->range_max());
934
+
935
+ // Test all the edge crossings directly.
936
+ for (const S2ShapeIndexCell* b_cell : b_cells_) {
937
+ if (CellCrossesCell(ai->clipped(), b_cell->clipped(0))) {
938
+ return true;
939
+ }
940
+ }
941
+ return false;
942
+ }
943
+
944
+ bool LoopCrosser::HasCrossingRelation(RangeIterator* ai, RangeIterator* bi) {
945
+ S2_DCHECK(ai->id().contains(bi->id()));
946
+ if (ai->num_edges() == 0) {
947
+ if (ai->contains_center() == a_crossing_target_) {
948
+ // All points within ai->id() satisfy the crossing target for A, so it's
949
+ // worth iterating through the cells of B to see whether any cell
950
+ // centers also satisfy the crossing target for B.
951
+ do {
952
+ if (bi->contains_center() == b_crossing_target_) return true;
953
+ bi->Next();
954
+ } while (bi->id() <= ai->range_max());
955
+ } else {
956
+ // The crossing target for A is not satisfied, so we skip over the cells
957
+ // of B using binary search.
958
+ bi->SeekBeyond(*ai);
959
+ }
960
+ } else {
961
+ // The current cell of A has at least one edge, so check for crossings.
962
+ if (HasCrossing(ai, bi)) return true;
963
+ }
964
+ ai->Next();
965
+ return false;
966
+ }
967
+
968
+ /*static*/ bool S2Loop::HasCrossingRelation(const S2Loop& a, const S2Loop& b,
969
+ LoopRelation* relation) {
970
+ // We look for S2CellId ranges where the indexes of A and B overlap, and
971
+ // then test those edges for crossings.
972
+ RangeIterator ai(&a.index_), bi(&b.index_);
973
+ LoopCrosser ab(a, b, relation, false); // Tests edges of A against B
974
+ LoopCrosser ba(b, a, relation, true); // Tests edges of B against A
975
+ while (!ai.Done() || !bi.Done()) {
976
+ if (ai.range_max() < bi.range_min()) {
977
+ // The A and B cells don't overlap, and A precedes B.
978
+ ai.SeekTo(bi);
979
+ } else if (bi.range_max() < ai.range_min()) {
980
+ // The A and B cells don't overlap, and B precedes A.
981
+ bi.SeekTo(ai);
982
+ } else {
983
+ // One cell contains the other. Determine which cell is larger.
984
+ int64 ab_relation = ai.id().lsb() - bi.id().lsb();
985
+ if (ab_relation > 0) {
986
+ // A's index cell is larger.
987
+ if (ab.HasCrossingRelation(&ai, &bi)) return true;
988
+ } else if (ab_relation < 0) {
989
+ // B's index cell is larger.
990
+ if (ba.HasCrossingRelation(&bi, &ai)) return true;
991
+ } else {
992
+ // The A and B cells are the same. Since the two cells have the same
993
+ // center point P, check whether P satisfies the crossing targets.
994
+ if (ai.contains_center() == ab.a_crossing_target() &&
995
+ bi.contains_center() == ab.b_crossing_target()) {
996
+ return true;
997
+ }
998
+ // Otherwise test all the edge crossings directly.
999
+ if (ai.num_edges() > 0 && bi.num_edges() > 0 &&
1000
+ ab.CellCrossesCell(ai.clipped(), bi.clipped())) {
1001
+ return true;
1002
+ }
1003
+ ai.Next();
1004
+ bi.Next();
1005
+ }
1006
+ }
1007
+ }
1008
+ return false;
1009
+ }
1010
+
1011
+ // Loop relation for Contains().
1012
+ class ContainsRelation : public LoopRelation {
1013
+ public:
1014
+ ContainsRelation() : found_shared_vertex_(false) {}
1015
+ bool found_shared_vertex() const { return found_shared_vertex_; }
1016
+
1017
+ // If A.Contains(P) == false && B.Contains(P) == true, it is equivalent to
1018
+ // having an edge crossing (i.e., Contains returns false).
1019
+ int a_crossing_target() const override { return false; }
1020
+ int b_crossing_target() const override { return true; }
1021
+
1022
+ bool WedgesCross(const S2Point& a0, const S2Point& ab1, const S2Point& a2,
1023
+ const S2Point& b0, const S2Point& b2) override {
1024
+ found_shared_vertex_ = true;
1025
+ return !S2::WedgeContains(a0, ab1, a2, b0, b2);
1026
+ }
1027
+
1028
+ private:
1029
+ bool found_shared_vertex_;
1030
+ };
1031
+
1032
+ bool S2Loop::Contains(const S2Loop* b) const {
1033
+ // For this loop A to contains the given loop B, all of the following must
1034
+ // be true:
1035
+ //
1036
+ // (1) There are no edge crossings between A and B except at vertices.
1037
+ //
1038
+ // (2) At every vertex that is shared between A and B, the local edge
1039
+ // ordering implies that A contains B.
1040
+ //
1041
+ // (3) If there are no shared vertices, then A must contain a vertex of B
1042
+ // and B must not contain a vertex of A. (An arbitrary vertex may be
1043
+ // chosen in each case.)
1044
+ //
1045
+ // The second part of (3) is necessary to detect the case of two loops whose
1046
+ // union is the entire sphere, i.e. two loops that contains each other's
1047
+ // boundaries but not each other's interiors.
1048
+ if (!subregion_bound_.Contains(b->bound_)) return false;
1049
+
1050
+ // Special cases to handle either loop being empty or full.
1051
+ if (is_empty_or_full() || b->is_empty_or_full()) {
1052
+ return is_full() || b->is_empty();
1053
+ }
1054
+
1055
+ // Check whether there are any edge crossings, and also check the loop
1056
+ // relationship at any shared vertices.
1057
+ ContainsRelation relation;
1058
+ if (HasCrossingRelation(*this, *b, &relation)) return false;
1059
+
1060
+ // There are no crossings, and if there are any shared vertices then A
1061
+ // contains B locally at each shared vertex.
1062
+ if (relation.found_shared_vertex()) return true;
1063
+
1064
+ // Since there are no edge intersections or shared vertices, we just need to
1065
+ // test condition (3) above. We can skip this test if we discovered that A
1066
+ // contains at least one point of B while checking for edge crossings.
1067
+ if (!Contains(b->vertex(0))) return false;
1068
+
1069
+ // We still need to check whether (A union B) is the entire sphere.
1070
+ // Normally this check is very cheap due to the bounding box precondition.
1071
+ if ((b->subregion_bound_.Contains(bound_) ||
1072
+ b->bound_.Union(bound_).is_full()) && b->Contains(vertex(0))) {
1073
+ return false;
1074
+ }
1075
+ return true;
1076
+ }
1077
+
1078
+
1079
+ // Loop relation for Intersects().
1080
+ class IntersectsRelation : public LoopRelation {
1081
+ public:
1082
+ IntersectsRelation() : found_shared_vertex_(false) {}
1083
+ bool found_shared_vertex() const { return found_shared_vertex_; }
1084
+
1085
+ // If A.Contains(P) == true && B.Contains(P) == true, it is equivalent to
1086
+ // having an edge crossing (i.e., Intersects returns true).
1087
+ int a_crossing_target() const override { return true; }
1088
+ int b_crossing_target() const override { return true; }
1089
+
1090
+ bool WedgesCross(const S2Point& a0, const S2Point& ab1, const S2Point& a2,
1091
+ const S2Point& b0, const S2Point& b2) override {
1092
+ found_shared_vertex_ = true;
1093
+ return S2::WedgeIntersects(a0, ab1, a2, b0, b2);
1094
+ }
1095
+
1096
+ private:
1097
+ bool found_shared_vertex_;
1098
+ };
1099
+
1100
+ bool S2Loop::Intersects(const S2Loop* b) const {
1101
+ // a->Intersects(b) if and only if !a->Complement()->Contains(b).
1102
+ // This code is similar to Contains(), but is optimized for the case
1103
+ // where both loops enclose less than half of the sphere.
1104
+ if (!bound_.Intersects(b->bound_)) return false;
1105
+
1106
+ // Check whether there are any edge crossings, and also check the loop
1107
+ // relationship at any shared vertices.
1108
+ IntersectsRelation relation;
1109
+ if (HasCrossingRelation(*this, *b, &relation)) return true;
1110
+ if (relation.found_shared_vertex()) return false;
1111
+
1112
+ // Since there are no edge intersections or shared vertices, the loops
1113
+ // intersect only if A contains B, B contains A, or the two loops contain
1114
+ // each other's boundaries. These checks are usually cheap because of the
1115
+ // bounding box preconditions. Note that neither loop is empty (because of
1116
+ // the bounding box check above), so it is safe to access vertex(0).
1117
+
1118
+ // Check whether A contains B, or A and B contain each other's boundaries.
1119
+ // (Note that A contains all the vertices of B in either case.)
1120
+ if (subregion_bound_.Contains(b->bound_) ||
1121
+ bound_.Union(b->bound_).is_full()) {
1122
+ if (Contains(b->vertex(0))) return true;
1123
+ }
1124
+ // Check whether B contains A.
1125
+ if (b->subregion_bound_.Contains(bound_)) {
1126
+ if (b->Contains(vertex(0))) return true;
1127
+ }
1128
+ return false;
1129
+ }
1130
+
1131
+ // Returns true if the wedge (a0, ab1, a2) contains the "semiwedge" defined as
1132
+ // any non-empty open set of rays immediately CCW from the edge (ab1, b2). If
1133
+ // "reverse_b" is true, then substitute "clockwise" for "CCW"; this simulates
1134
+ // what would happen if the direction of loop B was reversed.
1135
+ inline static bool WedgeContainsSemiwedge(const S2Point& a0, const S2Point& ab1,
1136
+ const S2Point& a2, const S2Point& b2,
1137
+ bool reverse_b) {
1138
+ if (b2 == a0 || b2 == a2) {
1139
+ // We have a shared or reversed edge.
1140
+ return (b2 == a0) == reverse_b;
1141
+ } else {
1142
+ return s2pred::OrderedCCW(a0, a2, b2, ab1);
1143
+ }
1144
+ }
1145
+
1146
+ // Loop relation for CompareBoundary().
1147
+ class CompareBoundaryRelation : public LoopRelation {
1148
+ public:
1149
+ explicit CompareBoundaryRelation(bool reverse_b):
1150
+ reverse_b_(reverse_b), found_shared_vertex_(false),
1151
+ contains_edge_(false), excludes_edge_(false) {
1152
+ }
1153
+ bool found_shared_vertex() const { return found_shared_vertex_; }
1154
+ bool contains_edge() const { return contains_edge_; }
1155
+
1156
+ // The CompareBoundary relation does not have a useful early-exit condition,
1157
+ // so we return -1 for both crossing targets.
1158
+ //
1159
+ // Aside: A possible early exit condition could be based on the following.
1160
+ // If A contains a point of both B and ~B, then A intersects Boundary(B).
1161
+ // If ~A contains a point of both B and ~B, then ~A intersects Boundary(B).
1162
+ // So if the intersections of {A, ~A} with {B, ~B} are all non-empty,
1163
+ // the return value is 0, i.e., Boundary(A) intersects Boundary(B).
1164
+ // Unfortunately it isn't worth detecting this situation because by the
1165
+ // time we have seen a point in all four intersection regions, we are also
1166
+ // guaranteed to have seen at least one pair of crossing edges.
1167
+ int a_crossing_target() const override { return -1; }
1168
+ int b_crossing_target() const override { return -1; }
1169
+
1170
+ bool WedgesCross(const S2Point& a0, const S2Point& ab1, const S2Point& a2,
1171
+ const S2Point& b0, const S2Point& b2) override {
1172
+ // Because we don't care about the interior of B, only its boundary, it is
1173
+ // sufficient to check whether A contains the semiwedge (ab1, b2).
1174
+ found_shared_vertex_ = true;
1175
+ if (WedgeContainsSemiwedge(a0, ab1, a2, b2, reverse_b_)) {
1176
+ contains_edge_ = true;
1177
+ } else {
1178
+ excludes_edge_ = true;
1179
+ }
1180
+ return contains_edge_ & excludes_edge_;
1181
+ }
1182
+
1183
+ protected:
1184
+ const bool reverse_b_; // True if loop B should be reversed.
1185
+ bool found_shared_vertex_; // True if any wedge was processed.
1186
+ bool contains_edge_; // True if any edge of B is contained by A.
1187
+ bool excludes_edge_; // True if any edge of B is excluded by A.
1188
+ };
1189
+
1190
+ int S2Loop::CompareBoundary(const S2Loop* b) const {
1191
+ S2_DCHECK(!is_empty() && !b->is_empty());
1192
+ S2_DCHECK(!b->is_full() || !b->is_hole());
1193
+
1194
+ // The bounds must intersect for containment or crossing.
1195
+ if (!bound_.Intersects(b->bound_)) return -1;
1196
+
1197
+ // Full loops are handled as though the loop surrounded the entire sphere.
1198
+ if (is_full()) return 1;
1199
+ if (b->is_full()) return -1;
1200
+
1201
+ // Check whether there are any edge crossings, and also check the loop
1202
+ // relationship at any shared vertices.
1203
+ CompareBoundaryRelation relation(b->is_hole());
1204
+ if (HasCrossingRelation(*this, *b, &relation)) return 0;
1205
+ if (relation.found_shared_vertex()) {
1206
+ return relation.contains_edge() ? 1 : -1;
1207
+ }
1208
+
1209
+ // There are no edge intersections or shared vertices, so we can check
1210
+ // whether A contains an arbitrary vertex of B.
1211
+ return Contains(b->vertex(0)) ? 1 : -1;
1212
+ }
1213
+
1214
+ bool S2Loop::ContainsNonCrossingBoundary(const S2Loop* b, bool reverse_b)
1215
+ const {
1216
+ S2_DCHECK(!is_empty() && !b->is_empty());
1217
+ S2_DCHECK(!b->is_full() || !reverse_b);
1218
+
1219
+ // The bounds must intersect for containment.
1220
+ if (!bound_.Intersects(b->bound_)) return false;
1221
+
1222
+ // Full loops are handled as though the loop surrounded the entire sphere.
1223
+ if (is_full()) return true;
1224
+ if (b->is_full()) return false;
1225
+
1226
+ int m = FindVertex(b->vertex(0));
1227
+ if (m < 0) {
1228
+ // Since vertex b0 is not shared, we can check whether A contains it.
1229
+ return Contains(b->vertex(0));
1230
+ }
1231
+ // Otherwise check whether the edge (b0, b1) is contained by A.
1232
+ return WedgeContainsSemiwedge(vertex(m-1), vertex(m), vertex(m+1),
1233
+ b->vertex(1), reverse_b);
1234
+ }
1235
+
1236
+ bool S2Loop::ContainsNested(const S2Loop* b) const {
1237
+ if (!subregion_bound_.Contains(b->bound_)) return false;
1238
+
1239
+ // Special cases to handle either loop being empty or full. Also bail out
1240
+ // when B has no vertices to avoid heap overflow on the vertex(1) call
1241
+ // below. (This method is called during polygon initialization before the
1242
+ // client has an opportunity to call IsValid().)
1243
+ if (is_empty_or_full() || b->num_vertices() < 2) {
1244
+ return is_full() || b->is_empty();
1245
+ }
1246
+
1247
+ // We are given that A and B do not share any edges, and that either one
1248
+ // loop contains the other or they do not intersect.
1249
+ int m = FindVertex(b->vertex(1));
1250
+ if (m < 0) {
1251
+ // Since b->vertex(1) is not shared, we can check whether A contains it.
1252
+ return Contains(b->vertex(1));
1253
+ }
1254
+ // Check whether the edge order around b->vertex(1) is compatible with
1255
+ // A containing B.
1256
+ return S2::WedgeContains(vertex(m-1), vertex(m), vertex(m+1),
1257
+ b->vertex(0), b->vertex(2));
1258
+ }
1259
+
1260
+ bool S2Loop::Equals(const S2Loop* b) const {
1261
+ if (num_vertices() != b->num_vertices()) return false;
1262
+ for (int i = 0; i < num_vertices(); ++i) {
1263
+ if (vertex(i) != b->vertex(i)) return false;
1264
+ }
1265
+ return true;
1266
+ }
1267
+
1268
+ bool S2Loop::BoundaryEquals(const S2Loop* b) const {
1269
+ if (num_vertices() != b->num_vertices()) return false;
1270
+
1271
+ // Special case to handle empty or full loops. Since they have the same
1272
+ // number of vertices, if one loop is empty/full then so is the other.
1273
+ if (is_empty_or_full()) return is_empty() == b->is_empty();
1274
+
1275
+ for (int offset = 0; offset < num_vertices(); ++offset) {
1276
+ if (vertex(offset) == b->vertex(0)) {
1277
+ // There is at most one starting offset since loop vertices are unique.
1278
+ for (int i = 0; i < num_vertices(); ++i) {
1279
+ if (vertex(i + offset) != b->vertex(i)) return false;
1280
+ }
1281
+ return true;
1282
+ }
1283
+ }
1284
+ return false;
1285
+ }
1286
+
1287
+ bool S2Loop::BoundaryApproxEquals(const S2Loop& b, S1Angle max_error) const {
1288
+ if (num_vertices() != b.num_vertices()) return false;
1289
+
1290
+ // Special case to handle empty or full loops. Since they have the same
1291
+ // number of vertices, if one loop is empty/full then so is the other.
1292
+ if (is_empty_or_full()) return is_empty() == b.is_empty();
1293
+
1294
+ for (int offset = 0; offset < num_vertices(); ++offset) {
1295
+ if (S2::ApproxEquals(vertex(offset), b.vertex(0), max_error)) {
1296
+ bool success = true;
1297
+ for (int i = 0; i < num_vertices(); ++i) {
1298
+ if (!S2::ApproxEquals(vertex(i + offset), b.vertex(i), max_error)) {
1299
+ success = false;
1300
+ break;
1301
+ }
1302
+ }
1303
+ if (success) return true;
1304
+ // Otherwise continue looping. There may be more than one candidate
1305
+ // starting offset since vertices are only matched approximately.
1306
+ }
1307
+ }
1308
+ return false;
1309
+ }
1310
+
1311
+ static bool MatchBoundaries(const S2Loop& a, const S2Loop& b, int a_offset,
1312
+ S1Angle max_error) {
1313
+ // The state consists of a pair (i,j). A state transition consists of
1314
+ // incrementing either "i" or "j". "i" can be incremented only if
1315
+ // a(i+1+a_offset) is near the edge from b(j) to b(j+1), and a similar rule
1316
+ // applies to "j". The function returns true iff we can proceed all the way
1317
+ // around both loops in this way.
1318
+ //
1319
+ // Note that when "i" and "j" can both be incremented, sometimes only one
1320
+ // choice leads to a solution. We handle this using a stack and
1321
+ // backtracking. We also keep track of which states have already been
1322
+ // explored to avoid duplicating work.
1323
+
1324
+ vector<pair<int, int>> pending;
1325
+ set<pair<int, int>> done;
1326
+ pending.push_back(std::make_pair(0, 0));
1327
+ while (!pending.empty()) {
1328
+ int i = pending.back().first;
1329
+ int j = pending.back().second;
1330
+ pending.pop_back();
1331
+ if (i == a.num_vertices() && j == b.num_vertices()) {
1332
+ return true;
1333
+ }
1334
+ done.insert(std::make_pair(i, j));
1335
+
1336
+ // If (i == na && offset == na-1) where na == a->num_vertices(), then
1337
+ // then (i+1+offset) overflows the [0, 2*na-1] range allowed by vertex().
1338
+ // So we reduce the range if necessary.
1339
+ int io = i + a_offset;
1340
+ if (io >= a.num_vertices()) io -= a.num_vertices();
1341
+
1342
+ if (i < a.num_vertices() && done.count(std::make_pair(i + 1, j)) == 0 &&
1343
+ S2::GetDistance(a.vertex(io + 1), b.vertex(j),
1344
+ b.vertex(j + 1)) <= max_error) {
1345
+ pending.push_back(std::make_pair(i + 1, j));
1346
+ }
1347
+ if (j < b.num_vertices() && done.count(std::make_pair(i, j + 1)) == 0 &&
1348
+ S2::GetDistance(b.vertex(j + 1), a.vertex(io),
1349
+ a.vertex(io + 1)) <= max_error) {
1350
+ pending.push_back(std::make_pair(i, j + 1));
1351
+ }
1352
+ }
1353
+ return false;
1354
+ }
1355
+
1356
+ bool S2Loop::BoundaryNear(const S2Loop& b, S1Angle max_error) const {
1357
+ // Special case to handle empty or full loops.
1358
+ if (is_empty_or_full() || b.is_empty_or_full()) {
1359
+ return (is_empty() && b.is_empty()) || (is_full() && b.is_full());
1360
+ }
1361
+
1362
+ for (int a_offset = 0; a_offset < num_vertices(); ++a_offset) {
1363
+ if (MatchBoundaries(*this, b, a_offset, max_error)) return true;
1364
+ }
1365
+ return false;
1366
+ }
1367
+
1368
+ void S2Loop::GetXYZFaceSiTiVertices(S2XYZFaceSiTi* vertices) const {
1369
+ for (int i = 0; i < num_vertices(); ++i) {
1370
+ vertices[i].xyz = vertex(i);
1371
+ vertices[i].cell_level = S2::XYZtoFaceSiTi(vertices[i].xyz,
1372
+ &vertices[i].face, &vertices[i].si, &vertices[i].ti);
1373
+ }
1374
+ }
1375
+
1376
+ void S2Loop::EncodeCompressed(Encoder* encoder, const S2XYZFaceSiTi* vertices,
1377
+ int snap_level) const {
1378
+ // Ensure enough for the data we write before S2EncodePointsCompressed.
1379
+ // S2EncodePointsCompressed ensures its space.
1380
+ encoder->Ensure(Encoder::kVarintMax32);
1381
+ encoder->put_varint32(num_vertices_);
1382
+
1383
+ S2EncodePointsCompressed(MakeSpan(vertices, num_vertices_),
1384
+ snap_level, encoder);
1385
+
1386
+ std::bitset<kNumProperties> properties = GetCompressedEncodingProperties();
1387
+
1388
+ // Ensure enough only for what we write. Let the bound ensure its own
1389
+ // space.
1390
+ encoder->Ensure(2 * Encoder::kVarintMax32);
1391
+ encoder->put_varint32(properties.to_ulong());
1392
+ encoder->put_varint32(depth_);
1393
+ if (properties.test(kBoundEncoded)) {
1394
+ bound_.Encode(encoder);
1395
+ }
1396
+ S2_DCHECK_GE(encoder->avail(), 0);
1397
+ }
1398
+
1399
+ bool S2Loop::DecodeCompressed(Decoder* decoder, int snap_level) {
1400
+ // get_varint32 takes a uint32*, but num_vertices_ is signed.
1401
+ // Decode to a temporary variable to avoid reinterpret_cast.
1402
+ uint32 unsigned_num_vertices;
1403
+ if (!decoder->get_varint32(&unsigned_num_vertices)) {
1404
+ return false;
1405
+ }
1406
+ if (unsigned_num_vertices == 0 ||
1407
+ unsigned_num_vertices > FLAGS_s2polygon_decode_max_num_vertices) {
1408
+ return false;
1409
+ }
1410
+ ClearIndex();
1411
+ if (owns_vertices_) delete[] vertices_;
1412
+ num_vertices_ = unsigned_num_vertices;
1413
+ vertices_ = new S2Point[num_vertices_];
1414
+ owns_vertices_ = true;
1415
+
1416
+ if (!S2DecodePointsCompressed(decoder, snap_level,
1417
+ MakeSpan(vertices_, num_vertices_))) {
1418
+ return false;
1419
+ }
1420
+ uint32 properties_uint32;
1421
+ if (!decoder->get_varint32(&properties_uint32)) {
1422
+ return false;
1423
+ }
1424
+ const std::bitset<kNumProperties> properties(properties_uint32);
1425
+ origin_inside_ = properties.test(kOriginInside);
1426
+
1427
+ uint32 unsigned_depth;
1428
+ if (!decoder->get_varint32(&unsigned_depth)) {
1429
+ return false;
1430
+ }
1431
+ depth_ = unsigned_depth;
1432
+
1433
+ if (properties.test(kBoundEncoded)) {
1434
+ if (!bound_.Decode(decoder)) {
1435
+ return false;
1436
+ }
1437
+ subregion_bound_ = S2LatLngRectBounder::ExpandForSubregions(bound_);
1438
+ } else {
1439
+ InitBound();
1440
+ }
1441
+ InitIndex();
1442
+ return true;
1443
+ }
1444
+
1445
+ std::bitset<kNumProperties> S2Loop::GetCompressedEncodingProperties() const {
1446
+ std::bitset<kNumProperties> properties;
1447
+ if (origin_inside_) {
1448
+ properties.set(kOriginInside);
1449
+ }
1450
+
1451
+ // Write whether there is a bound so we can change the threshold later.
1452
+ // Recomputing the bound multiplies the decode time taken per vertex
1453
+ // by a factor of about 3.5. Without recomputing the bound, decode
1454
+ // takes approximately 125 ns / vertex. A loop with 63 vertices
1455
+ // encoded without the bound will take ~30us to decode, which is
1456
+ // acceptable. At ~3.5 bytes / vertex without the bound, adding
1457
+ // the bound will increase the size by <15%, which is also acceptable.
1458
+ static const int kMinVerticesForBound = 64;
1459
+ if (num_vertices_ >= kMinVerticesForBound) {
1460
+ properties.set(kBoundEncoded);
1461
+ }
1462
+ return properties;
1463
+ }
1464
+
1465
+ /* static */
1466
+ std::unique_ptr<S2Loop> S2Loop::MakeRegularLoop(const S2Point& center,
1467
+ S1Angle radius,
1468
+ int num_vertices) {
1469
+ Matrix3x3_d m;
1470
+ S2::GetFrame(center, &m); // TODO(ericv): Return by value
1471
+ return MakeRegularLoop(m, radius, num_vertices);
1472
+ }
1473
+
1474
+ /* static */
1475
+ std::unique_ptr<S2Loop> S2Loop::MakeRegularLoop(const Matrix3x3_d& frame,
1476
+ S1Angle radius,
1477
+ int num_vertices) {
1478
+ // We construct the loop in the given frame coordinates, with the center at
1479
+ // (0, 0, 1). For a loop of radius "r", the loop vertices have the form
1480
+ // (x, y, z) where x^2 + y^2 = sin(r) and z = cos(r). The distance on the
1481
+ // sphere (arc length) from each vertex to the center is acos(cos(r)) = r.
1482
+ double z = cos(radius.radians());
1483
+ double r = sin(radius.radians());
1484
+ double radian_step = 2 * M_PI / num_vertices;
1485
+ vector<S2Point> vertices;
1486
+ for (int i = 0; i < num_vertices; ++i) {
1487
+ double angle = i * radian_step;
1488
+ S2Point p(r * cos(angle), r * sin(angle), z);
1489
+ vertices.push_back(S2::FromFrame(frame, p).Normalize());
1490
+ }
1491
+ return make_unique<S2Loop>(vertices);
1492
+ }
1493
+
1494
+ size_t S2Loop::SpaceUsed() const {
1495
+ size_t size = sizeof(*this);
1496
+ size += num_vertices() * sizeof(S2Point);
1497
+ // index_ itself is already included in sizeof(*this).
1498
+ size += index_.SpaceUsed() - sizeof(index_);
1499
+ return size;
1500
+ }
1501
+
1502
+ int S2Loop::Shape::num_chains() const {
1503
+ return loop_->is_empty() ? 0 : 1;
1504
+ }
1505
+
1506
+ S2Shape::Chain S2Loop::Shape::chain(int i) const {
1507
+ S2_DCHECK_EQ(i, 0);
1508
+ return Chain(0, Shape::num_edges()); // Avoid virtual call.
1509
+ }