Skip to content

Commit

Permalink
Implement OVERLAPPING mode, update tests to use mode flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Rabinowitz committed Nov 4, 2023
1 parent 45255d5 commit 61d3e5b
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 95 deletions.
17 changes: 15 additions & 2 deletions scripts/make_countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ async function makeCountries(sourceUrl, targetPath) {
#include "h3api.h"
#include "mathExtensions.h"
#include "polyfill.h"
#include "polygon.h"
const GeoPolygon COUNTRIES[${polygons.length}] = {${
polygons.map((poly, i) => formatGeoPolygon(poly, names[i])).join(',')
Expand All @@ -149,13 +150,25 @@ for (int res = 0; res < MAX_RES + 1; res++) {
BENCHMARK(polygonToCells_AllCountries1, 5, {
for (int index = 0; index < ${polygons.length}; index++) {
H3_EXPORT(polygonToCells)(&COUNTRIES[index], res, 0, hexagons);
H3_EXPORT(polygonToCells)(&COUNTRIES[index], res, CENTER_CONTAINMENT, hexagons);
}
});
BENCHMARK(polygonToCells_AllCountries2, 5, {
for (int index = 0; index < ${polygons.length}; index++) {
H3_EXPORT(polygonToCellsExperimental)(&COUNTRIES[index], res, 0, hexagons);
H3_EXPORT(polygonToCellsExperimental)(&COUNTRIES[index], res, CENTER_CONTAINMENT, hexagons);
}
});
BENCHMARK(polygonToCells_AllCountries3, 5, {
for (int index = 0; index < ${polygons.length}; index++) {
H3_EXPORT(polygonToCellsExperimental)(&COUNTRIES[index], res, FULL_CONTAINMENT, hexagons);
}
});
BENCHMARK(polygonToCells_AllCountries4, 5, {
for (int index = 0; index < ${polygons.length}; index++) {
H3_EXPORT(polygonToCellsExperimental)(&COUNTRIES[index], res, OVERLAPPING, hexagons);
}
});
Expand Down
19 changes: 13 additions & 6 deletions src/apps/benchmarks/benchmarkPolygonToCellsExperimental.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "benchmark.h"
#include "h3api.h"
#include "polyfill.h"
#include "polygon.h"

// Fixtures
LatLng sfVerts[] = {
Expand Down Expand Up @@ -123,23 +124,29 @@ int64_t numHexagons;
H3Index *hexagons;

BENCHMARK(polygonToCellsSF, 500, {
H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, 0, &numHexagons);
H3_EXPORT(maxPolygonToCellsSize)
(&sfGeoPolygon, 9, CENTER_CONTAINMENT, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(&sfGeoPolygon, 9, 0, hexagons);
H3_EXPORT(polygonToCellsExperimental)
(&sfGeoPolygon, 9, CENTER_CONTAINMENT, hexagons);
free(hexagons);
});

BENCHMARK(polygonToCellsAlameda, 500, {
H3_EXPORT(maxPolygonToCellsSize)(&alamedaGeoPolygon, 9, 0, &numHexagons);
H3_EXPORT(maxPolygonToCellsSize)
(&alamedaGeoPolygon, 9, CENTER_CONTAINMENT, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(&alamedaGeoPolygon, 9, 0, hexagons);
H3_EXPORT(polygonToCellsExperimental)
(&alamedaGeoPolygon, 9, CENTER_CONTAINMENT, hexagons);
free(hexagons);
});

BENCHMARK(polygonToCellsSouthernExpansion, 10, {
H3_EXPORT(maxPolygonToCellsSize)(&southernGeoPolygon, 9, 0, &numHexagons);
H3_EXPORT(maxPolygonToCellsSize)
(&southernGeoPolygon, 9, CENTER_CONTAINMENT, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCellsExperimental)(&southernGeoPolygon, 9, 0, hexagons);
H3_EXPORT(polygonToCellsExperimental)
(&southernGeoPolygon, 9, CENTER_CONTAINMENT, hexagons);
free(hexagons);
});

Expand Down
13 changes: 7 additions & 6 deletions src/apps/testapps/testH3Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "h3api.h"
#include "latLng.h"
#include "polyfill.h"
#include "polygon.h"
#include "test.h"
#include "utility.h"

Expand Down Expand Up @@ -230,22 +231,22 @@ SUITE(h3Memory) {
sfGeoPolygon.numHoles = 0;

int64_t numHexagons;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, 0,
&numHexagons));
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(
&sfGeoPolygon, 9, CENTER_CONTAINMENT, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

resetMemoryCounters(0);
failAlloc = true;
H3Error err = H3_EXPORT(polygonToCellsExperimental)(&sfGeoPolygon, 9, 0,
hexagons);
H3Error err = H3_EXPORT(polygonToCellsExperimental)(
&sfGeoPolygon, 9, CENTER_CONTAINMENT, hexagons);
t_assert(err == E_MEMORY_ALLOC,
"polygonToCellsExperimental failed (1)");
t_assert(actualAllocCalls == 1, "alloc called once");
t_assert(actualFreeCalls == 0, "free not called");

resetMemoryCounters(1);
err = H3_EXPORT(polygonToCellsExperimental)(&sfGeoPolygon, 9, 0,
hexagons);
err = H3_EXPORT(polygonToCellsExperimental)(
&sfGeoPolygon, 9, CENTER_CONTAINMENT, hexagons);
t_assert(err == E_SUCCESS, "polygonToCellsExperimental succeeded (1)");
t_assert(actualAllocCalls == 1, "alloc called one time");
t_assert(actualFreeCalls == 1, "free called one time");
Expand Down
24 changes: 12 additions & 12 deletions src/apps/testapps/testPolygonInternal.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,55 +626,55 @@ SUITE(polygonInternal) {
H3_EXPORT(destroyLinkedMultiPolygon)(&polygon);
}

TEST(lineIntersectsLine) {
TEST(lineCrossesLine) {
LatLng lines1[4] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}};
t_assert(
lineIntersectsLine(&lines1[0], &lines1[1], &lines1[2], &lines1[3]),
lineCrossesLine(&lines1[0], &lines1[1], &lines1[2], &lines1[3]),
"diagonal intersection");

LatLng lines2[4] = {{1, 1}, {0, 0}, {1, 0}, {0, 1}};
t_assert(
lineIntersectsLine(&lines2[0], &lines2[1], &lines2[2], &lines2[3]),
lineCrossesLine(&lines2[0], &lines2[1], &lines2[2], &lines2[3]),
"diagonal intersection, reverse vertexes");

LatLng lines3[4] = {{0.5, 0}, {0.5, 1}, {0, 0.5}, {1, 0.5}};
t_assert(
lineIntersectsLine(&lines3[0], &lines3[1], &lines3[2], &lines3[3]),
lineCrossesLine(&lines3[0], &lines3[1], &lines3[2], &lines3[3]),
"horizontal/vertical intersection");

LatLng lines4[4] = {{0.5, 1}, {0.5, 0}, {1, 0.5}, {0, 0.5}};
t_assert(
lineIntersectsLine(&lines4[0], &lines4[1], &lines4[2], &lines4[3]),
lineCrossesLine(&lines4[0], &lines4[1], &lines4[2], &lines4[3]),
"horizontal/vertical intersection, reverse vertexes");

LatLng lines5[4] = {{0, 0}, {0.4, 0.4}, {0, 1}, {1, 0}};
t_assert(
!lineIntersectsLine(&lines5[0], &lines5[1], &lines5[2], &lines5[3]),
!lineCrossesLine(&lines5[0], &lines5[1], &lines5[2], &lines5[3]),
"diagonal non-intersection, below");

LatLng lines6[4] = {{0.6, 0.6}, {1, 1}, {0, 1}, {1, 0}};
t_assert(
!lineIntersectsLine(&lines6[0], &lines6[1], &lines6[2], &lines6[3]),
!lineCrossesLine(&lines6[0], &lines6[1], &lines6[2], &lines6[3]),
"diagonal non-intersection, above");

LatLng lines7[4] = {{0.5, 0}, {0.5, 1}, {0, 0.5}, {0.4, 0.5}};
t_assert(
!lineIntersectsLine(&lines7[0], &lines7[1], &lines7[2], &lines7[3]),
!lineCrossesLine(&lines7[0], &lines7[1], &lines7[2], &lines7[3]),
"horizontal/vertical non-intersection, below");

LatLng lines8[4] = {{0.5, 0}, {0.5, 1}, {0.6, 0.5}, {1, 0.5}};
t_assert(
!lineIntersectsLine(&lines8[0], &lines8[1], &lines8[2], &lines8[3]),
!lineCrossesLine(&lines8[0], &lines8[1], &lines8[2], &lines8[3]),
"horizontal/vertical non-intersection, above");

LatLng lines9[4] = {{0.5, 0}, {0.5, 0.4}, {0, 0.5}, {1, 0.5}};
t_assert(
!lineIntersectsLine(&lines9[0], &lines9[1], &lines9[2], &lines9[3]),
!lineCrossesLine(&lines9[0], &lines9[1], &lines9[2], &lines9[3]),
"horizontal/vertical non-intersection, left");

LatLng lines10[4] = {{0.5, 0.6}, {0.5, 1}, {0, 0.5}, {1, 0.5}};
t_assert(!lineIntersectsLine(&lines10[0], &lines10[1], &lines10[2],
&lines10[3]),
t_assert(!lineCrossesLine(&lines10[0], &lines10[1], &lines10[2],
&lines10[3]),
"horizontal/vertical non-intersection, right");
}

Expand Down
Loading

0 comments on commit 61d3e5b

Please sign in to comment.