This commit is contained in:
Alfred Melch 2019-07-15 09:57:53 +02:00
parent cf40583989
commit 65e74582ce
2 changed files with 51 additions and 25 deletions

View File

@ -14,18 +14,27 @@
import wasmModuleFactory from './simplify.js'
import wasmUrl from './simplify.wasm'
import { initEmscriptenModule } from '../wasm-util/initEmscripten.js'
import { storeCoords, loadResultAndFreeMemory } from '../wasm-util/coordinates.js'
import {
storeCoords,
loadResultAndFreeMemory
} from '../wasm-util/coordinates.js'
export async function simplifyWasm(coords, tolerance, highestQuality) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module._simplify(buffer, coords.length * 2, tolerance, highestQuality)
module._free(buffer)
return loadResultAndFreeMemory(module, result)
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module._simplify(
buffer,
coords.length * 2,
tolerance,
highestQuality
)
module._free(buffer)
return loadResultAndFreeMemory(module, result)
}
let emscriptenModule
export async function getModule () {
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
export async function getModule() {
if (!emscriptenModule)
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
}

View File

@ -6,28 +6,37 @@
/**
* Allocate memory for coordinates and stores them in a flat representation.
* Returns a pointer to the buffer. The length of the buffer is coords.length * 2
* @param {*} module
* @param {*} module
* @param {Array} coords - An array of coordinate pairs
*/
export function storeCoords(module, coords) {
const buffer = module._malloc(coords.length * 2 * 8)
const heap = new Float64Array(module.HEAPF64.buffer, buffer, coords.length * 2)
const flatSize = coords.length * 2
const offset = module._malloc(flatSize * Float64Array.BYTES_PER_ELEMENT)
const heapView = new Float64Array(module.HEAPF64.buffer, offset, flatSize)
for (let i = 0; i < coords.length; i++) {
heap[2*i] = coords[i][0]
heap[2*i + 1] = coords[i][1]
heapView[2 * i] = coords[i][0]
heapView[2 * i + 1] = coords[i][1]
}
return buffer
return offset
}
/**
* Loads coordinates from the memory by the result pointer.
* Frees the resultpointer and the result before returning
* @param {*} module
* @param {*} module
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
*/
export function loadResultAndFreeMemory(module, result) {
const [resultPointer, resultLength] = new Uint32Array(module.HEAPU32.buffer, result, 2)
const simplified = new Float64Array(module.HEAPF64.buffer, resultPointer, resultLength)
const [resultPointer, resultLength] = new Uint32Array(
module.HEAPU32.buffer,
result,
2
)
const simplified = new Float64Array(
module.HEAPF64.buffer,
resultPointer,
resultLength
)
const coords = unflattenCoords(simplified)
module._free(result)
module._free(resultPointer)
@ -37,12 +46,20 @@ export function loadResultAndFreeMemory(module, result) {
/**
* Loads coordinates from the memory by the result pointer without freeing memory.
* For benchmarking purposes
* @param {*} module
* @param {*} module
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
*/
export function loadResult(module, result) {
const [resultPointer, resultLength] = new Uint32Array(module.HEAPU32.buffer, result, 2)
const simplified = new Float64Array(module.HEAPF64.buffer, resultPointer, resultLength)
const [resultPointer, resultLength] = new Uint32Array(
module.HEAPU32.buffer,
result,
2
)
const simplified = new Float64Array(
module.HEAPF64.buffer,
resultPointer,
resultLength
)
return unflattenCoords(simplified)
}
@ -50,13 +67,13 @@ export function loadResult(module, result) {
* Transforms a flat array to an array coordinate pairs
* @param {Array} flattened - length must be even
*/
export function unflattenCoords (flattened) {
export function unflattenCoords(flattened) {
const coords = []
let x, y
for (let i = 0; i < flattened.length; i += 2) {
x = flattened[i]
y = flattened[i + 1]
coords.push([x, y])
x = flattened[i]
y = flattened[i + 1]
coords.push([x, y])
}
return coords
}