Skip to content

Commit

Permalink
ugcwasm 模块重命名解决与wasm 模块加载冲突 review by luox
Browse files Browse the repository at this point in the history
  • Loading branch information
xilanhuaweidapao committed Dec 3, 2024
1 parent 2de4f48 commit d17b6ad
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 70 deletions.
8 changes: 4 additions & 4 deletions src/common/util/GeometryAnalysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export class GeometryAnalysis extends Events {
constructor(Module) {
super();
if (Module) {
window.Module = Module;
window.ugcModule = Module;
}
if (!window.Module) {
window.Module = defaultModule;
if (!window.ugcModule) {
window.ugcModule = defaultModule;
}
this.module = window.Module;
this.module = window.ugcModule;
this.addEventType('loaded');
if (this.module.calledRun) {
/**
Expand Down
28 changes: 15 additions & 13 deletions src/common/util/UGCWasmAll.js

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions src/common/wasm/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export function geojsonCoordsToPoint2Ds(coords) {
const length = coords.length;
const dim = 2; // coords[0].length
const coordArr = new Float64Array(coords.flat());
const coordPtr = window.Module._malloc(length * dim * 8);
window.Module.HEAPF64.set(coordArr, coordPtr / 8);
const seqPtr = window.Module._UGCWasm_Geometry_CreatePoint2DsFromBuffer(coordPtr, length);
window.Module._free(coordPtr);
const coordPtr = window.ugcModule._malloc(length * dim * 8);
window.ugcModule.HEAPF64.set(coordArr, coordPtr / 8);
const seqPtr = window.ugcModule._UGCWasm_Geometry_CreatePoint2DsFromBuffer(coordPtr, length);
window.ugcModule._free(coordPtr);
return seqPtr;
}

Expand All @@ -28,33 +28,33 @@ export function geojsonCoords2UGDoubleArray(coords) {
}
const length = coords.length
const coordArr = new Float64Array(coords)
const coordPtr = window.Module._malloc(length * 8)
window.Module.HEAPF64.set(coordArr, coordPtr / 8)
const pDoubleArray = window.Module._UGCWasm_Helper_CreateDoubleArray(coordPtr, length)
window.Module._free(coordPtr)
const coordPtr = window.ugcModule._malloc(length * 8)
window.ugcModule.HEAPF64.set(coordArr, coordPtr / 8)
const pDoubleArray = window.ugcModule._UGCWasm_Helper_CreateDoubleArray(coordPtr, length)
window.ugcModule._free(coordPtr)

return pDoubleArray
}

export function getJSArrayFromUGDoubleArray(pDoubleArray) {
// get length of doublearray
var length = window.Module._UGCWasm_Helper_GetDoubleArrayLength(pDoubleArray);
var length = window.ugcModule._UGCWasm_Helper_GetDoubleArrayLength(pDoubleArray);

// allocate memory 一个double是8个字节
const pBuffer = window.Module._malloc(length * 8);
const pBuffer = window.ugcModule._malloc(length * 8);

// copy doublearray to buffer
window.Module._UGCWasm_Helper_GetBufferFromDoubleArray(pDoubleArray, pBuffer);
window.ugcModule._UGCWasm_Helper_GetBufferFromDoubleArray(pDoubleArray, pBuffer);

// get double in buffer to Float64Array
const view = new Float64Array(window.Module.HEAPF64.buffer, pBuffer, length);
const view = new Float64Array(window.ugcModule.HEAPF64.buffer, pBuffer, length);
const coords = [];
for (let i = 0; i < length; i++) {
coords.push(view[i]);
}

// free buffer memory
window.Module._free(pBuffer);
window.ugcModule._free(pBuffer);

return coords;
}
Expand Down Expand Up @@ -83,13 +83,13 @@ export function ugGeometry2Geojson(pUGGeo) {
if (!pUGGeo) {
return null;
}
const geomType = window.Module._UGCWasm_Geometry_GetType(pUGGeo);
const geomType = window.ugcModule._UGCWasm_Geometry_GetType(pUGGeo);
switch (geomType) {
case 1: {
// UGGeoPoint
const point2d = [];
var x = window.Module._UGCWasm_GeoPoint_GetX(pUGGeo);
var y = window.Module._UGCWasm_GeoPoint_GetY(pUGGeo);
var x = window.ugcModule._UGCWasm_GeoPoint_GetX(pUGGeo);
var y = window.ugcModule._UGCWasm_GeoPoint_GetY(pUGGeo);
point2d.push(x, y)

// create geojson point
Expand All @@ -104,23 +104,23 @@ export function ugGeometry2Geojson(pUGGeo) {
// UGGeoLine
const outlines = [];
// get part count
const partCount = window.Module._UGCWasm_GeoLine_GetPartCount(pUGGeo);
const partCount = window.ugcModule._UGCWasm_GeoLine_GetPartCount(pUGGeo);
for (let j = 0; j < partCount; j++) {
// get part j point count
var count = window.Module._UGCWasm_GeoLine_GetPartPointCount(pUGGeo, j);
var count = window.ugcModule._UGCWasm_GeoLine_GetPartPointCount(pUGGeo, j);
// 一个double是8个字节,而一个point2D是两个double,所以需要申请 点个数 * 2 * 8
const pBuffer = window.Module._malloc(count * 2 * 8);
const pBuffer = window.ugcModule._malloc(count * 2 * 8);

// get part j points
window.Module._UGCWasm_GeoLine_GetPart2(pUGGeo, pBuffer, j);
window.ugcModule._UGCWasm_GeoLine_GetPart2(pUGGeo, pBuffer, j);

// Float64Array to line part coordinates
const view = new Float64Array(window.Module.HEAPF64.buffer, pBuffer, count * 2);
const view = new Float64Array(window.ugcModule.HEAPF64.buffer, pBuffer, count * 2);
const coords = [];
for (let i = 0; i < count * 2; i = i + 2) {
coords.push([view[i], view[i + 1]]);
}
window.Module._free(pBuffer);
window.ugcModule._free(pBuffer);

outlines.push(coords);
}
Expand All @@ -137,23 +137,23 @@ export function ugGeometry2Geojson(pUGGeo) {
// UGGeoRegion
const outlines = [];
// get part count
const partCount = window.Module._UGCWasm_GeoRegion_GetPartCount(pUGGeo);
const partCount = window.ugcModule._UGCWasm_GeoRegion_GetPartCount(pUGGeo);
for (let j = 0; j < partCount; j++) {
// get part j point count
const count = window.Module._UGCWasm_GeoRegion_GetPartPointCount(pUGGeo, j);
const count = window.ugcModule._UGCWasm_GeoRegion_GetPartPointCount(pUGGeo, j);
// 一个double是8个字节,而一个point2D是两个double,所以需要申请 点个数 * 2 * 8
const pBuffer = window.Module._malloc(count * 2 * 8);
const pBuffer = window.ugcModule._malloc(count * 2 * 8);

// get part j points
window.Module._UGCWasm_GeoRegion_GetPart2(pUGGeo, pBuffer, j);
window.ugcModule._UGCWasm_GeoRegion_GetPart2(pUGGeo, pBuffer, j);

// Float64Array to line part coordinates
const view = new Float64Array(window.Module.HEAPF64.buffer, pBuffer, count * 2);
const view = new Float64Array(window.ugcModule.HEAPF64.buffer, pBuffer, count * 2);
const coords = [];
for (let i = 0; i < count * 2; i = i + 2) {
coords.push([view[i], view[i + 1]]);
}
window.Module._free(pBuffer);
window.ugcModule._free(pBuffer);

outlines.push(coords);
}
Expand Down Expand Up @@ -202,49 +202,49 @@ export function geojson2UGGeometry(geojson) {
// geojson.geometries.forEach((feature) => {
// geoms.push(geojsonToGeosGeom(feature, geos))
// })
// const geomsPtr = geos.window.Module._malloc(geoms.length * 4)
// const geomsPtr = geos.window.ugcModule._malloc(geoms.length * 4)
// const geomsArr = new Uint32Array(geoms)
// geos.window.Module.HEAPU32.set(geomsArr, geomsPtr / 4)
// geos.window.ugcModule.HEAPU32.set(geomsArr, geomsPtr / 4)
// const multiGeomsPtr = geos.GEOSGeom_createCollection(
// 7, // geos.GEOS_GEOMETRYCOLLECTION
// geomsPtr,
// geoms.length
// )
// geos.window.Module._free(geomsPtr)
// geos.window.ugcModule._free(geomsPtr)
// return multiGeomsPtr
// }
case 'Point':
if (geojson.coordinates.length === 0) {
return window.Module._UGCWasm_GeoPoint_New()
return window.ugcModule._UGCWasm_GeoPoint_New()
} else {
return window.Module._UGCWasm_GeoPoint_New2(
return window.ugcModule._UGCWasm_GeoPoint_New2(
geojson.coordinates[0],
geojson.coordinates[1]
)
}
case 'LineString':
if (geojson.coordinates.length === 0) {
return window.Module._UGCWasm_GeoLine_New()
return window.ugcModule._UGCWasm_GeoLine_New()
} else {
const pGeoLine = window.Module._UGCWasm_GeoLine_New()
const pGeoLine = window.ugcModule._UGCWasm_GeoLine_New()
const pPoint2Ds = geojsonCoordsToPoint2Ds(geojson.coordinates)
window.Module._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Ds, geojson.coordinates.length)
window.ugcModule._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Ds, geojson.coordinates.length)

return pGeoLine
}
case 'Polygon':
if (geojson.coordinates.length === 0) {
return window.Module._UGCWasm_GeoRegion_New()
return window.ugcModule._UGCWasm_GeoRegion_New()
} else {
const pGeoRegion = window.Module._UGCWasm_GeoRegion_New()
const pGeoRegion = window.ugcModule._UGCWasm_GeoRegion_New()

const pPoint2Ds0 = geojsonCoordsToPoint2Ds(geojson.coordinates[0])
window.Module._UGCWasm_GeoRegion_AddPart2(pGeoRegion, pPoint2Ds0, geojson.coordinates[0].length)
window.ugcModule._UGCWasm_GeoRegion_AddPart2(pGeoRegion, pPoint2Ds0, geojson.coordinates[0].length)

if (geojson.coordinates.length > 1) {
for (let i = 1; i < geojson.coordinates.length; i++) {
const pPoint2Dsi = geojsonCoordsToPoint2Ds(geojson.coordinates[i])
window.Module._UGCWasm_GeoRegion_AddPart2(pGeoRegion, pPoint2Dsi, geojson.coordinates[i].length)
window.ugcModule._UGCWasm_GeoRegion_AddPart2(pGeoRegion, pPoint2Dsi, geojson.coordinates[i].length)
}
}

Expand All @@ -263,30 +263,30 @@ export function geojson2UGGeometry(geojson) {
// )
// )
// }
// const pointsPtr = geos.window.Module._malloc(points.length * 4)
// const pointsPtr = geos.window.ugcModule._malloc(points.length * 4)
// const pointsArr = new Uint32Array(points)
// geos.window.Module.HEAPU32.set(pointsArr, pointsPtr / 4)
// geos.window.ugcModule.HEAPU32.set(pointsArr, pointsPtr / 4)
// const multiPointPtr = geos.GEOSGeom_createCollection(
// 4, // geos.GEOS_MULTIPOINT
// pointsPtr,
// points.length
// )
// geos.window.Module._free(pointsPtr)
// geos.window.ugcModule._free(pointsPtr)
// return multiPointPtr
// }
case 'MultiLineString':
if (geojson.coordinates.length === 0) {
return window.Module._UGCWasm_GeoLine_New()
return window.ugcModule._UGCWasm_GeoLine_New()
} else {
const pGeoLine = window.Module._UGCWasm_GeoLine_New()
const pGeoLine = window.ugcModule._UGCWasm_GeoLine_New()

const pPoint2Ds0 = geojsonCoordsToPoint2Ds(geojson.coordinates[0])
window.Module._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Ds0, geojson.coordinates[0].length)
window.ugcModule._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Ds0, geojson.coordinates[0].length)

if (geojson.coordinates.length > 1) {
for (let i = 1; i < geojson.coordinates.length; i++) {
const pPoint2Dsi = geojsonCoordsToPoint2Ds(geojson.coordinates[i])
window.Module._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Dsi, geojson.coordinates[i].length)
window.ugcModule._UGCWasm_GeoLine_AddPart2(pGeoLine, pPoint2Dsi, geojson.coordinates[i].length)
}
}

Expand Down Expand Up @@ -314,28 +314,28 @@ export function geojson2UGGeometry(geojson) {
// let holesPtr = null
// if (holes.length > 0) {
// const holesArr = new Uint32Array(holes)
// holesPtr = geos.window.Module._malloc(holes.length * 4)
// geos.window.Module.HEAPU32.set(holesArr, holesPtr / 4)
// holesPtr = geos.window.ugcModule._malloc(holes.length * 4)
// geos.window.ugcModule.HEAPU32.set(holesArr, holesPtr / 4)
// }
// const polyPtr = geos.GEOSGeom_createPolygon(
// shell,
// holesPtr,
// holes.length
// )
// if (holes.length > 0) {
// geos.window.Module._free(holesPtr)
// geos.window.ugcModule._free(holesPtr)
// }
// polygons.push(polyPtr)
// }
// const polygonsPtr = geos.window.Module._malloc(polygons.length * 4)
// const polygonsPtr = geos.window.ugcModule._malloc(polygons.length * 4)
// const polygonsArr = new Uint32Array(polygons)
// geos.window.Module.HEAPU32.set(polygonsArr, polygonsPtr / 4)
// geos.window.ugcModule.HEAPU32.set(polygonsArr, polygonsPtr / 4)
// const multiPolyPtr = geos.GEOSGeom_createCollection(
// 6, // geos.GEOS_MULTIPOLYGON
// polygonsPtr,
// polygons.length
// )
// geos.window.Module._free(polygonsPtr)
// geos.window.ugcModule._free(polygonsPtr)
// return multiPolyPtr
// }
default:
Expand Down

0 comments on commit d17b6ad

Please sign in to comment.