mt-polygon-simplification/lib/psimpl-js/index.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-07-19 19:33:27 +02:00
/*
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'
2019-07-21 12:50:30 +02:00
import { initEmscriptenModule } from '../../lib/wasm-util/initEmscripten.js'
import {
storeCoords,
unflattenCoords
} from '../../lib/wasm-util/coordinates.js'
2019-07-19 19:33:27 +02:00
2019-07-21 12:50:30 +02:00
export async function simplify_nth_point(coords, n) {
2019-07-25 09:53:20 +02:00
return await callSimplification('nth_point', coords, [n])
2019-07-19 19:33:27 +02:00
}
export async function simplify_radial_distance(coords, tol) {
2019-07-25 09:53:20 +02:00
return await callSimplification('radial_distance', coords, [tol])
2019-07-19 19:33:27 +02:00
}
2019-07-21 12:50:30 +02:00
export async function simplify_perpendicular_distance(coords, tol, repeat) {
2019-07-25 09:53:20 +02:00
const params = [tol, repeat]
return await callSimplification('perpendicular_distance', coords, params)
2019-07-19 19:33:27 +02:00
}
export async function simplify_reumann_witkam(coords, tol) {
2019-07-25 09:53:20 +02:00
return await callSimplification('reumann_witkam', coords, [tol])
2019-07-19 19:33:27 +02:00
}
2019-07-21 12:50:30 +02:00
export async function simplify_opheim(coords, minTol, maxTol) {
2019-07-25 09:53:20 +02:00
return await callSimplification('opheim', coords, [minTol, maxTol])
2019-07-19 19:33:27 +02:00
}
2019-07-21 12:50:30 +02:00
export async function simplify_lang(coords, tol, lookAhead) {
2019-07-25 09:53:20 +02:00
return await callSimplification('lang', coords, [tol, lookAhead])
2019-07-19 19:33:27 +02:00
}
export async function simplify_douglas_peucker(coords, tol) {
2019-07-25 09:53:20 +02:00
return await callSimplification('douglas_peucker', coords, [tol])
2019-07-19 19:33:27 +02:00
}
2019-07-21 12:50:30 +02:00
export async function simplify_douglas_peucker_n(coords, count) {
2019-07-25 09:53:20 +02:00
return await callSimplification('douglas_peucker_n', coords, [count])
}
async function callSimplification(name, coords, params) {
if (!isSimplificationRoutine(name)) throw Error(`Routine ${name} not known`)
2019-07-19 19:33:27 +02:00
const module = await getModule()
const buffer = storeCoords(module, coords)
2019-07-25 09:53:20 +02:00
const result = module[name](buffer, coords.length * 2, ...params)
2019-07-19 19:33:27 +02:00
module._free(buffer)
return unflattenCoords(result)
}
2019-07-25 09:53:20 +02:00
const simplificationRoutines = [
'nth_point',
'radial_distance',
'perpendicular_distance',
'reumann_witkam',
'opheim',
'lang',
'douglas_peucker',
'douglas_peucker_n'
]
function isSimplificationRoutine(name) {
return simplificationRoutines.indexOf(name) !== -1
}
2019-07-19 19:33:27 +02:00
let emscriptenModule
export async function getModule() {
if (!emscriptenModule)
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
}