code style
This commit is contained in:
parent
9f2ce8823e
commit
57390f4ed5
13
lib/simplify-wasm/debug.h
Normal file
13
lib/simplify-wasm/debug.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#import <stdio.h>
|
||||||
|
|
||||||
|
void printPoint(double* point) {
|
||||||
|
printf("[%lf, %lf]", point[0], point[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printPointerAsCoords(double * arr, int len) {
|
||||||
|
for (int i = 0; i < len; i += 2) {
|
||||||
|
printPoint(arr + i);
|
||||||
|
if (i < len - 2) printf(", ");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
@ -1,32 +1,42 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
<head>
|
||||||
<head>
|
<meta charset="UTF-8" />
|
||||||
<meta charset="UTF-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div>Original array: <span id="src">Loading...</span></div>
|
<div>Original array: <span id="src">Loading...</span></div>
|
||||||
<div>Simplified array: <span id="dest">Loading...</span></div>
|
<div>Simplified array: <span id="dest">Loading...</span></div>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import moduleFactory from './simplify.js'
|
import moduleFactory from './simplify.js'
|
||||||
import { storeCoords, loadResultAndFreeMemory } from '../wasm-util/coordinates.js'
|
import {
|
||||||
|
storeCoords,
|
||||||
|
loadResultAndFreeMemory
|
||||||
|
} from '../wasm-util/coordinates.js'
|
||||||
|
|
||||||
const module = moduleFactory()
|
const module = moduleFactory()
|
||||||
const data = [[0, 0], [1, 1], [1, 2], [2, 2], [5, 5], [5, 0], [6, 10], [10, 10]]
|
const data = [
|
||||||
document.getElementById('src').innerText = JSON.stringify(data)
|
[0, 0],
|
||||||
|
[1, 1],
|
||||||
|
[1, 2],
|
||||||
|
[2, 2],
|
||||||
|
[5, 5],
|
||||||
|
[5, 0],
|
||||||
|
[6, 10],
|
||||||
|
[10, 10]
|
||||||
|
]
|
||||||
|
document.getElementById('src').innerText = JSON.stringify(data)
|
||||||
|
|
||||||
module.onRuntimeInitialized = async _ => {
|
module.onRuntimeInitialized = async _ => {
|
||||||
const buffer = storeCoords(module, data)
|
const buffer = storeCoords(module, data)
|
||||||
const result = module._simplify(buffer, data.length * 2, 2, true)
|
const result = module._simplify(buffer, data.length * 2, 2, false)
|
||||||
module._free(buffer)
|
module._free(buffer)
|
||||||
const simplified = loadResultAndFreeMemory(module, result)
|
const simplified = loadResultAndFreeMemory(module, result)
|
||||||
document.getElementById('dest').innerText = JSON.stringify(simplified)
|
document.getElementById('dest').innerText = JSON.stringify(simplified)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
</html>
|
||||||
</html>
|
|
||||||
|
@ -22,14 +22,14 @@ import {
|
|||||||
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(
|
const resultInfo = module._simplify(
|
||||||
buffer,
|
buffer,
|
||||||
coords.length * 2,
|
coords.length * 2,
|
||||||
tolerance,
|
tolerance,
|
||||||
highestQuality
|
highestQuality
|
||||||
)
|
)
|
||||||
module._free(buffer)
|
module._free(buffer)
|
||||||
return loadResultAndFreeMemory(module, result)
|
return loadResultAndFreeMemory(module, resultInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
let emscriptenModule
|
let emscriptenModule
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
// #include "debug.h"
|
||||||
#define DIM 2
|
#define DIM 2
|
||||||
|
|
||||||
void copyCoordinate(double* from, double* to) {
|
void copyCoordinate(double* from, double* to) {
|
||||||
@ -51,24 +51,24 @@ double getSqSegDist(double* p, double* p1, double* p2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// basic distance-based simplification
|
// basic distance-based simplification
|
||||||
int simplifyRadialDist(double* points, int length, double sqTolerance, double* newPoints) {
|
int simplifyRadialDist(double* points, int length, double sqTolerance, double* result) {
|
||||||
double* prevPoint = points;
|
double* prevPoint = points;
|
||||||
double* point;
|
double* point;
|
||||||
int sizeOfNewPoints = push(newPoints, 0, points);
|
int resultLength = push(result, 0, points);
|
||||||
|
|
||||||
for (int i = 1; i < length; i += DIM) {
|
for (int i = DIM; i < length; i += DIM) {
|
||||||
point = points + i;
|
point = points + i;
|
||||||
if (getSqDist(point, prevPoint) > sqTolerance) {
|
if (getSqDist(point, prevPoint) > sqTolerance) {
|
||||||
sizeOfNewPoints = push(newPoints, sizeOfNewPoints, point);
|
resultLength = push(result, resultLength, point);
|
||||||
prevPoint = point;
|
prevPoint = point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevPoint != point) {
|
if (prevPoint != point) {
|
||||||
sizeOfNewPoints = push(newPoints, sizeOfNewPoints, point);
|
resultLength = push(result, resultLength, point);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sizeOfNewPoints;
|
return resultLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
int simplifyDPStep(double* points, int first, int last, double sqTolerance, double* simplified, int lengthOfSimplified) {
|
int simplifyDPStep(double* points, int first, int last, double sqTolerance, double* simplified, int lengthOfSimplified) {
|
||||||
@ -101,18 +101,20 @@ int simplifyDouglasPeucker(double* points, int length, double sqTolerance, doubl
|
|||||||
return lengthOfSimplified;
|
return lengthOfSimplified;
|
||||||
}
|
}
|
||||||
|
|
||||||
int* simplify(double * input, int length, double tolerance, int highestQuality) {
|
int* simplify(double * points, int length, double tolerance, int highestQuality) {
|
||||||
double sqTolerance = tolerance * tolerance;
|
double sqTolerance = tolerance * tolerance;
|
||||||
double* points = input;
|
|
||||||
double* resultRdDistance = NULL;
|
double* resultRdDistance = NULL;
|
||||||
double* result = malloc(length * sizeof(double));
|
double* result = NULL;
|
||||||
|
int resultLength;
|
||||||
|
|
||||||
if (!highestQuality) {
|
if (!highestQuality) {
|
||||||
resultRdDistance = malloc(length * sizeof(double));
|
resultRdDistance = malloc(length * sizeof(double));
|
||||||
length = simplifyRadialDist(points, length, sqTolerance, resultRdDistance);
|
length = simplifyRadialDist(points, length, sqTolerance, resultRdDistance);
|
||||||
points = resultRdDistance;
|
points = resultRdDistance;
|
||||||
}
|
}
|
||||||
int resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result);
|
|
||||||
|
result = malloc(length * sizeof(double));
|
||||||
|
resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result);
|
||||||
free(resultRdDistance);
|
free(resultRdDistance);
|
||||||
|
|
||||||
int* resultInfo = malloc(2);
|
int* resultInfo = malloc(2);
|
||||||
|
Binary file not shown.
@ -26,10 +26,10 @@ export function storeCoords(module, coords) {
|
|||||||
* @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, resultInfo) {
|
||||||
const [resultPointer, resultLength] = new Uint32Array(
|
const [resultPointer, resultLength] = new Uint32Array(
|
||||||
module.HEAPU32.buffer,
|
module.HEAPU32.buffer,
|
||||||
result,
|
resultInfo,
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
const simplified = new Float64Array(
|
const simplified = new Float64Array(
|
||||||
@ -38,7 +38,7 @@ export function loadResultAndFreeMemory(module, result) {
|
|||||||
resultLength
|
resultLength
|
||||||
)
|
)
|
||||||
const coords = unflattenCoords(simplified)
|
const coords = unflattenCoords(simplified)
|
||||||
module._free(result)
|
module._free(resultInfo)
|
||||||
module._free(resultPointer)
|
module._free(resultPointer)
|
||||||
return coords
|
return coords
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user