mt-polygon-simplification/lib/psimpl-js/index.js
2019-07-19 19:33:27 +02:00

90 lines
3.0 KiB
JavaScript

/*
This file is meant to be processed in the context of an asset bundler.
Specifically the import of 'psimpl.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 'psimple.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 './psimpl.js'
import wasmUrl from './psimpl.wasm'
import { initEmscriptenModule } from '../../lib/initEmscripten.js'
import { storeCoords, unflattenCoords } from '../../lib/heapUtil.js'
export async function simplify_nth_point(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.nth_point(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_radial_distance(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.radial_distance(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_perpendicular_distance(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.perpendicular_distance(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_reumann_witkam(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.reumann_witkam(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_opheim(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.opheim(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_lang(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.lang(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_douglas_peucker(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.douglas_peucker(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
export async function simplify_douglas_peucker_n(coords, tol) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module.douglas_peucker_n(buffer, coords.length * 2, tol)
module._free(buffer)
return unflattenCoords(result)
}
let emscriptenModule
export async function getModule() {
if (!emscriptenModule)
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
}