diff --git a/lib/psimpl-js/index.js b/lib/psimpl-js/index.js index f19b0ee..04178c3 100644 --- a/lib/psimpl-js/index.js +++ b/lib/psimpl-js/index.js @@ -21,72 +21,59 @@ import { } from '../../lib/wasm-util/coordinates.js' export async function simplify_nth_point(coords, n) { - const module = await getModule() - const buffer = storeCoords(module, coords) - const result = module.nth_point(buffer, coords.length * 2, n) - module._free(buffer) - return unflattenCoords(result) + return await callSimplification('nth_point', coords, [n]) } 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) + return await callSimplification('radial_distance', coords, [tol]) } export async function simplify_perpendicular_distance(coords, tol, repeat) { - const module = await getModule() - const buffer = storeCoords(module, coords) - const result = module.perpendicular_distance( - buffer, - coords.length * 2, - tol, - repeat - ) - module._free(buffer) - return unflattenCoords(result) + const params = [tol, repeat] + return await callSimplification('perpendicular_distance', coords, params) } 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) + return await callSimplification('reumann_witkam', coords, [tol]) } export async function simplify_opheim(coords, minTol, maxTol) { - const module = await getModule() - const buffer = storeCoords(module, coords) - const result = module.opheim(buffer, coords.length * 2, minTol, maxTol) - module._free(buffer) - return unflattenCoords(result) + return await callSimplification('opheim', coords, [minTol, maxTol]) } export async function simplify_lang(coords, tol, lookAhead) { - const module = await getModule() - const buffer = storeCoords(module, coords) - const result = module.lang(buffer, coords.length * 2, tol, lookAhead) - module._free(buffer) - return unflattenCoords(result) + return await callSimplification('lang', coords, [tol, lookAhead]) } export async function simplify_douglas_peucker(coords, tol) { + return await callSimplification('douglas_peucker', coords, [tol]) +} + +export async function simplify_douglas_peucker_n(coords, count) { + return await callSimplification('douglas_peucker_n', coords, [count]) +} + +async function callSimplification(name, coords, params) { + if (!isSimplificationRoutine(name)) throw Error(`Routine ${name} not known`) const module = await getModule() const buffer = storeCoords(module, coords) - const result = module.douglas_peucker(buffer, coords.length * 2, tol) + const result = module[name](buffer, coords.length * 2, ...params) module._free(buffer) return unflattenCoords(result) } -export async function simplify_douglas_peucker_n(coords, count) { - const module = await getModule() - const buffer = storeCoords(module, coords) - const result = module.douglas_peucker_n(buffer, coords.length * 2, count) - module._free(buffer) - return unflattenCoords(result) +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 } let emscriptenModule