code style

This commit is contained in:
Alfred Melch 2019-07-16 17:31:20 +02:00
parent 9f2ce8823e
commit 57390f4ed5
6 changed files with 64 additions and 39 deletions

13
lib/simplify-wasm/debug.h Normal file
View 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");
}

View File

@ -1,32 +1,42 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
</head>
<body>
<body>
<div>Original array: <span id="src">Loading...</span></div>
<div>Simplified array: <span id="dest">Loading...</span></div>
<script type="module">
import moduleFactory from './simplify.js'
import { storeCoords, loadResultAndFreeMemory } from '../wasm-util/coordinates.js'
import moduleFactory from './simplify.js'
import {
storeCoords,
loadResultAndFreeMemory
} from '../wasm-util/coordinates.js'
const module = moduleFactory()
const data = [[0, 0], [1, 1], [1, 2], [2, 2], [5, 5], [5, 0], [6, 10], [10, 10]]
document.getElementById('src').innerText = JSON.stringify(data)
const module = moduleFactory()
const 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 _ => {
const buffer = storeCoords(module, data)
const result = module._simplify(buffer, data.length * 2, 2, true)
module._free(buffer)
const simplified = loadResultAndFreeMemory(module, result)
document.getElementById('dest').innerText = JSON.stringify(simplified)
}
module.onRuntimeInitialized = async _ => {
const buffer = storeCoords(module, data)
const result = module._simplify(buffer, data.length * 2, 2, false)
module._free(buffer)
const simplified = loadResultAndFreeMemory(module, result)
document.getElementById('dest').innerText = JSON.stringify(simplified)
}
</script>
</body>
</html>
</body>
</html>

View File

@ -22,14 +22,14 @@ import {
export async function simplifyWasm(coords, tolerance, highestQuality) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const result = module._simplify(
const resultInfo = module._simplify(
buffer,
coords.length * 2,
tolerance,
highestQuality
)
module._free(buffer)
return loadResultAndFreeMemory(module, result)
return loadResultAndFreeMemory(module, resultInfo)
}
let emscriptenModule

View File

@ -1,5 +1,5 @@
#include <stdlib.h>
// #include "debug.h"
#define DIM 2
void copyCoordinate(double* from, double* to) {
@ -51,24 +51,24 @@ double getSqSegDist(double* p, double* p1, double* p2) {
}
// 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* 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;
if (getSqDist(point, prevPoint) > sqTolerance) {
sizeOfNewPoints = push(newPoints, sizeOfNewPoints, point);
resultLength = push(result, resultLength, point);
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) {
@ -101,18 +101,20 @@ int simplifyDouglasPeucker(double* points, int length, double sqTolerance, doubl
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* points = input;
double* resultRdDistance = NULL;
double* result = malloc(length * sizeof(double));
double* result = NULL;
int resultLength;
if (!highestQuality) {
resultRdDistance = malloc(length * sizeof(double));
length = simplifyRadialDist(points, length, sqTolerance, resultRdDistance);
points = resultRdDistance;
}
int resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result);
result = malloc(length * sizeof(double));
resultLength = simplifyDouglasPeucker(points, length, sqTolerance, result);
free(resultRdDistance);
int* resultInfo = malloc(2);

Binary file not shown.

View File

@ -26,10 +26,10 @@ export function storeCoords(module, coords) {
* @param {*} module
* @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(
module.HEAPU32.buffer,
result,
resultInfo,
2
)
const simplified = new Float64Array(
@ -38,7 +38,7 @@ export function loadResultAndFreeMemory(module, result) {
resultLength
)
const coords = unflattenCoords(simplified)
module._free(result)
module._free(resultInfo)
module._free(resultPointer)
return coords
}