Prettify
This commit is contained in:
parent
0fd4385a34
commit
701173b1bf
@ -14,18 +14,27 @@
|
|||||||
import wasmModuleFactory from './simplify.js'
|
import wasmModuleFactory from './simplify.js'
|
||||||
import wasmUrl from './simplify.wasm'
|
import wasmUrl from './simplify.wasm'
|
||||||
import { initEmscriptenModule } from '../wasm-util/initEmscripten.js'
|
import { initEmscriptenModule } from '../wasm-util/initEmscripten.js'
|
||||||
import { storeCoords, loadResultAndFreeMemory } from '../wasm-util/coordinates.js'
|
import {
|
||||||
|
storeCoords,
|
||||||
|
loadResultAndFreeMemory
|
||||||
|
} from '../wasm-util/coordinates.js'
|
||||||
|
|
||||||
export async function simplifyWasm(coords, tolerance, highestQuality) {
|
export async function simplifyWasm(coords, tolerance, highestQuality) {
|
||||||
const module = await getModule()
|
const module = await getModule()
|
||||||
const buffer = storeCoords(module, coords)
|
const buffer = storeCoords(module, coords)
|
||||||
const result = module._simplify(buffer, coords.length * 2, tolerance, highestQuality)
|
const result = module._simplify(
|
||||||
module._free(buffer)
|
buffer,
|
||||||
return loadResultAndFreeMemory(module, result)
|
coords.length * 2,
|
||||||
|
tolerance,
|
||||||
|
highestQuality
|
||||||
|
)
|
||||||
|
module._free(buffer)
|
||||||
|
return loadResultAndFreeMemory(module, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
let emscriptenModule
|
let emscriptenModule
|
||||||
export async function getModule () {
|
export async function getModule() {
|
||||||
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
|
if (!emscriptenModule)
|
||||||
return await emscriptenModule
|
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
|
||||||
|
return await emscriptenModule
|
||||||
}
|
}
|
||||||
|
@ -6,28 +6,37 @@
|
|||||||
/**
|
/**
|
||||||
* Allocate memory for coordinates and stores them in a flat representation.
|
* Allocate memory for coordinates and stores them in a flat representation.
|
||||||
* Returns a pointer to the buffer. The length of the buffer is coords.length * 2
|
* Returns a pointer to the buffer. The length of the buffer is coords.length * 2
|
||||||
* @param {*} module
|
* @param {*} module
|
||||||
* @param {Array} coords - An array of coordinate pairs
|
* @param {Array} coords - An array of coordinate pairs
|
||||||
*/
|
*/
|
||||||
export function storeCoords(module, coords) {
|
export function storeCoords(module, coords) {
|
||||||
const buffer = module._malloc(coords.length * 2 * 8)
|
const flatSize = coords.length * 2
|
||||||
const heap = new Float64Array(module.HEAPF64.buffer, buffer, coords.length * 2)
|
const offset = module._malloc(flatSize * Float64Array.BYTES_PER_ELEMENT)
|
||||||
|
const heapView = new Float64Array(module.HEAPF64.buffer, offset, flatSize)
|
||||||
for (let i = 0; i < coords.length; i++) {
|
for (let i = 0; i < coords.length; i++) {
|
||||||
heap[2*i] = coords[i][0]
|
heapView[2 * i] = coords[i][0]
|
||||||
heap[2*i + 1] = coords[i][1]
|
heapView[2 * i + 1] = coords[i][1]
|
||||||
}
|
}
|
||||||
return buffer
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads coordinates from the memory by the result pointer.
|
* Loads coordinates from the memory by the result pointer.
|
||||||
* Frees the resultpointer and the result before returning
|
* Frees the resultpointer and the result before returning
|
||||||
* @param {*} module
|
* @param {*} module
|
||||||
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
|
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
|
||||||
*/
|
*/
|
||||||
export function loadResultAndFreeMemory(module, result) {
|
export function loadResultAndFreeMemory(module, result) {
|
||||||
const [resultPointer, resultLength] = new Uint32Array(module.HEAPU32.buffer, result, 2)
|
const [resultPointer, resultLength] = new Uint32Array(
|
||||||
const simplified = new Float64Array(module.HEAPF64.buffer, resultPointer, resultLength)
|
module.HEAPU32.buffer,
|
||||||
|
result,
|
||||||
|
2
|
||||||
|
)
|
||||||
|
const simplified = new Float64Array(
|
||||||
|
module.HEAPF64.buffer,
|
||||||
|
resultPointer,
|
||||||
|
resultLength
|
||||||
|
)
|
||||||
const coords = unflattenCoords(simplified)
|
const coords = unflattenCoords(simplified)
|
||||||
module._free(result)
|
module._free(result)
|
||||||
module._free(resultPointer)
|
module._free(resultPointer)
|
||||||
@ -37,12 +46,20 @@ export function loadResultAndFreeMemory(module, result) {
|
|||||||
/**
|
/**
|
||||||
* Loads coordinates from the memory by the result pointer without freeing memory.
|
* Loads coordinates from the memory by the result pointer without freeing memory.
|
||||||
* For benchmarking purposes
|
* For benchmarking purposes
|
||||||
* @param {*} module
|
* @param {*} module
|
||||||
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
|
* @param {*} result - pointer to Uint32 array of length 2. Stores the resultPointer and resultLength
|
||||||
*/
|
*/
|
||||||
export function loadResult(module, result) {
|
export function loadResult(module, result) {
|
||||||
const [resultPointer, resultLength] = new Uint32Array(module.HEAPU32.buffer, result, 2)
|
const [resultPointer, resultLength] = new Uint32Array(
|
||||||
const simplified = new Float64Array(module.HEAPF64.buffer, resultPointer, resultLength)
|
module.HEAPU32.buffer,
|
||||||
|
result,
|
||||||
|
2
|
||||||
|
)
|
||||||
|
const simplified = new Float64Array(
|
||||||
|
module.HEAPF64.buffer,
|
||||||
|
resultPointer,
|
||||||
|
resultLength
|
||||||
|
)
|
||||||
return unflattenCoords(simplified)
|
return unflattenCoords(simplified)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,13 +67,13 @@ export function loadResult(module, result) {
|
|||||||
* Transforms a flat array to an array coordinate pairs
|
* Transforms a flat array to an array coordinate pairs
|
||||||
* @param {Array} flattened - length must be even
|
* @param {Array} flattened - length must be even
|
||||||
*/
|
*/
|
||||||
export function unflattenCoords (flattened) {
|
export function unflattenCoords(flattened) {
|
||||||
const coords = []
|
const coords = []
|
||||||
let x, y
|
let x, y
|
||||||
for (let i = 0; i < flattened.length; i += 2) {
|
for (let i = 0; i < flattened.length; i += 2) {
|
||||||
x = flattened[i]
|
x = flattened[i]
|
||||||
y = flattened[i + 1]
|
y = flattened[i + 1]
|
||||||
coords.push([x, y])
|
coords.push([x, y])
|
||||||
}
|
}
|
||||||
return coords
|
return coords
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user