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-19 19:33:27 +02:00
|
|
|
const module = await getModule()
|
|
|
|
const buffer = storeCoords(module, coords)
|
2019-07-21 12:50:30 +02:00
|
|
|
const result = module.nth_point(buffer, coords.length * 2, n)
|
2019-07-19 19:33:27 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:50:30 +02:00
|
|
|
export async function simplify_perpendicular_distance(coords, tol, repeat) {
|
2019-07-19 19:33:27 +02:00
|
|
|
const module = await getModule()
|
|
|
|
const buffer = storeCoords(module, coords)
|
2019-07-21 12:50:30 +02:00
|
|
|
const result = module.perpendicular_distance(
|
|
|
|
buffer,
|
|
|
|
coords.length * 2,
|
|
|
|
tol,
|
|
|
|
repeat
|
|
|
|
)
|
2019-07-19 19:33:27 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:50:30 +02:00
|
|
|
export async function simplify_opheim(coords, minTol, maxTol) {
|
2019-07-19 19:33:27 +02:00
|
|
|
const module = await getModule()
|
|
|
|
const buffer = storeCoords(module, coords)
|
2019-07-21 12:50:30 +02:00
|
|
|
const result = module.opheim(buffer, coords.length * 2, minTol, maxTol)
|
2019-07-19 19:33:27 +02:00
|
|
|
module._free(buffer)
|
|
|
|
return unflattenCoords(result)
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:50:30 +02:00
|
|
|
export async function simplify_lang(coords, tol, lookAhead) {
|
2019-07-19 19:33:27 +02:00
|
|
|
const module = await getModule()
|
|
|
|
const buffer = storeCoords(module, coords)
|
2019-07-21 12:50:30 +02:00
|
|
|
const result = module.lang(buffer, coords.length * 2, tol, lookAhead)
|
2019-07-19 19:33:27 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:50:30 +02:00
|
|
|
export async function simplify_douglas_peucker_n(coords, count) {
|
2019-07-19 19:33:27 +02:00
|
|
|
const module = await getModule()
|
|
|
|
const buffer = storeCoords(module, coords)
|
2019-07-21 12:50:30 +02:00
|
|
|
const result = module.douglas_peucker_n(buffer, coords.length * 2, count)
|
2019-07-19 19:33:27 +02:00
|
|
|
module._free(buffer)
|
|
|
|
return unflattenCoords(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
let emscriptenModule
|
|
|
|
export async function getModule() {
|
|
|
|
if (!emscriptenModule)
|
|
|
|
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
|
|
|
|
return await emscriptenModule
|
|
|
|
}
|