mt-polygon-simplification/lib/simplify-wasm/index.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-07-14 20:37:26 +02:00
/*
This file is meant to be processed in the context of an asset bundler.
Specifically the import of 'simplify.wasm' is meant to be resolved by the
corresponding path to that file. Configure a file loader to resolve .wasm
files (e.g. Webpack: file-loader) accordingly.
Otherwise you have to use the module factory 'simplify.js' manually and
reproduce the steps from below to fit your build context. See example.html.
Other than wrapping the memory handling for convenience this method also makes
sure the module is only loaded once.
*/
import wasmModuleFactory from './simplify.js'
import wasmUrl from './simplify.wasm'
import { initEmscriptenModule } from '../wasm-util/initEmscripten.js'
2019-07-15 09:57:53 +02:00
import {
storeCoords,
loadResultAndFreeMemory
} from '../wasm-util/coordinates.js'
2019-07-14 20:37:26 +02:00
export async function simplifyWasm(coords, tolerance, highestQuality) {
2019-07-15 09:57:53 +02:00
const module = await getModule()
const buffer = storeCoords(module, coords)
2019-07-16 17:31:20 +02:00
const resultInfo = module._simplify(
2019-07-15 09:57:53 +02:00
buffer,
coords.length * 2,
tolerance,
highestQuality
)
module._free(buffer)
2019-07-16 17:31:20 +02:00
return loadResultAndFreeMemory(module, resultInfo)
2019-07-14 20:37:26 +02:00
}
let emscriptenModule
2019-07-15 09:57:53 +02:00
export async function getModule() {
if (!emscriptenModule)
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
2019-07-14 20:37:26 +02:00
}