Last active
May 4, 2021 23:47
-
-
Save vicapow/f53aff943421ba4f69a0d66fa95f031c to your computer and use it in GitHub Desktop.
example of compiling geos to JS because why not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
To compile: | |
first, get a version of geos and put it in `geos` relative path. then... | |
cd geos | |
emconfigure ./configure | |
emmake make | |
cd .. | |
emcc \ | |
-s EXPORTED_FUNCTIONS='["_get_voronoi"]' \ | |
-s ASSERTIONS=2 \ | |
-s DEMANGLE_SUPPORT=1 \ | |
src/example.c \ | |
-lgeos_c \ | |
-Lgeos/capi/.libs/ \ | |
-Igeos/capi/ \ | |
-Igeos/include \ | |
-lgeos \ | |
-Lgeos/src/.libs/ | |
*/ | |
#include <geos_c.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <emscripten.h> | |
char* get_voronoi() { | |
initGEOS( (GEOSMessageHandler) printf, (GEOSMessageHandler) printf); | |
// 2 points with dimension, 2. | |
int size = 100; | |
GEOSCoordSequence *inputSequence = GEOSCoordSeq_create(size, 2); | |
if (inputSequence == NULL) { | |
printf("unable to initialize coordinate. Exiting...\n"); | |
exit(1); | |
} | |
for (int i = 0; i < size; i++) { | |
GEOSCoordSeq_setX(inputSequence, i, emscripten_random() * 10); | |
GEOSCoordSeq_setY(inputSequence, i, emscripten_random() * 10); | |
} | |
GEOSGeometry *geometry = GEOSGeom_createLineString(inputSequence); | |
if (geometry == NULL) { | |
printf("geometry is null\n"); | |
exit(1); | |
} | |
GEOSGeometry *envelope = NULL; | |
double tolerance = 0.0; | |
int onlyEdges = 0; | |
GEOSGeometry* voronoi = GEOSVoronoiDiagram( | |
geometry, | |
envelope, | |
tolerance, | |
onlyEdges | |
); | |
if (voronoi == NULL) { | |
printf("unable to compute voronoi\n"); | |
} | |
GEOSWKTWriter* wktWriter = GEOSWKTWriter_create(); | |
char *wkt = GEOSWKTWriter_write(wktWriter, voronoi); | |
GEOSWKTWriter_destroy(wktWriter); | |
wktWriter = NULL; | |
finishGEOS(); | |
return wkt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment