diff --git a/lib/simplify-wasm/debug.h b/lib/simplify-wasm/debug.h new file mode 100644 index 0000000..3b5fc26 --- /dev/null +++ b/lib/simplify-wasm/debug.h @@ -0,0 +1,13 @@ +#import + +void printPoint(double* point) { + printf("[%lf, %lf]", point[0], point[1]); +} + +void printPointerAsCoords(double * arr, int len) { + for (int i = 0; i < len; i += 2) { + printPoint(arr + i); + if (i < len - 2) printf(", "); + } + printf("\n"); +} \ No newline at end of file diff --git a/lib/simplify-wasm/example.html b/lib/simplify-wasm/example.html index 094596d..d6451af 100644 --- a/lib/simplify-wasm/example.html +++ b/lib/simplify-wasm/example.html @@ -1,32 +1,42 @@ - - - - - + + + + Document - + - +
Original array: Loading...
Simplified array: Loading...
- + - - - \ No newline at end of file + + diff --git a/lib/simplify-wasm/index.js b/lib/simplify-wasm/index.js index b8acdb9..f762b14 100644 --- a/lib/simplify-wasm/index.js +++ b/lib/simplify-wasm/index.js @@ -22,14 +22,14 @@ import { export async function simplifyWasm(coords, tolerance, highestQuality) { const module = await getModule() const buffer = storeCoords(module, coords) - const result = module._simplify( + const resultInfo = module._simplify( buffer, coords.length * 2, tolerance, highestQuality ) module._free(buffer) - return loadResultAndFreeMemory(module, result) + return loadResultAndFreeMemory(module, resultInfo) } let emscriptenModule diff --git a/lib/simplify-wasm/simplify.c b/lib/simplify-wasm/simplify.c index a18b2b4..9f6c10b 100644 --- a/lib/simplify-wasm/simplify.c +++ b/lib/simplify-wasm/simplify.c @@ -1,5 +1,5 @@ #include - +// #include "debug.h" #define DIM 2 void copyCoordinate(double* from, double* to) { @@ -51,24 +51,24 @@ double getSqSegDist(double* p, double* p1, double* p2) { } // basic distance-based simplification -int simplifyRadialDist(double* points, int length, double sqTolerance, double* newPoints) { +int simplifyRadialDist(double* points, int length, double sqTolerance, double* result) { double* prevPoint = points; double* point; - int sizeOfNewPoints = push(newPoints, 0, points); + int resultLength = push(result, 0, points); - for (int i = 1; i < length; i += DIM) { + for (int i = DIM; i < length; i += DIM) { point = points + i; if (getSqDist(point, prevPoint) > sqTolerance) { - sizeOfNewPoints = push(newPoints, sizeOfNewPoints, point); + resultLength = push(result, resultLength, point); prevPoint = point; } } if (prevPoint != point) { - sizeOfNewPoints = push(newPoints, sizeOfNewPoints, point); + resultLength = push(result, resultLength, point); } - return sizeOfNewPoints; + return resultLength; } int simplifyDPStep(double* points, int first, int last, double sqTolerance, double* simplified, int lengthOfSimplified) { @@ -101,18 +101,20 @@ int simplifyDouglasPeucker(double* points, int length, double sqTolerance, doubl return lengthOfSimplified; } -int* simplify(double * input, int length, double tolerance, int highestQuality) { +int* simplify(double * points, int length, double tolerance, int highestQuality) { double sqTolerance = tolerance * tolerance; - double* points = input; double* resultRdDistance = NULL; - double* result = malloc(length * sizeof(double)); + double* result = NULL; + int resultLength; if (!highestQuality) { resultRdDistance = malloc(length * sizeof(double)); length = simplifyRadialDist(points, length, sqTolerance, resultRdDistance); points = resultRdDistance; } - int resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result); + + result = malloc(length * sizeof(double)); + resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result); free(resultRdDistance); int* resultInfo = malloc(2); diff --git a/lib/simplify-wasm/simplify.wasm b/lib/simplify-wasm/simplify.wasm index e1e6f7f..0d32587 100644 Binary files a/lib/simplify-wasm/simplify.wasm and b/lib/simplify-wasm/simplify.wasm differ diff --git a/lib/wasm-util/coordinates.js b/lib/wasm-util/coordinates.js index c34964e..96d5070 100644 --- a/lib/wasm-util/coordinates.js +++ b/lib/wasm-util/coordinates.js @@ -26,10 +26,10 @@ export function storeCoords(module, coords) { * @param {*} module * @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength */ -export function loadResultAndFreeMemory(module, result) { +export function loadResultAndFreeMemory(module, resultInfo) { const [resultPointer, resultLength] = new Uint32Array( module.HEAPU32.buffer, - result, + resultInfo, 2 ) const simplified = new Float64Array( @@ -38,7 +38,7 @@ export function loadResultAndFreeMemory(module, result) { resultLength ) const coords = unflattenCoords(simplified) - module._free(result) + module._free(resultInfo) module._free(resultPointer) return coords }