Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(arrow): triangulateOnWorker plumbing #2788

Merged
merged 12 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/arrow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
"fs": false
},
"scripts": {
"pre-build": "npm run build-worker && npm run build-bundle && npm run build-bundle -- --env=dev",
"pre-build": "npm run build-worker && npm run build-bundle && npm run build-bundle -- --env=dev && npm run build-triangulation-worker",
"build-bundle": "ocular-bundle ./src/index.ts",
"build-worker": "esbuild src/workers/arrow-worker.ts --bundle --outfile=dist/arrow-worker.js --platform=browser --external:{stream} --define:__VERSION__=\\\"$npm_package_version\\\"",
"build-triangulation-worker": "esbuild src/workers/triangulation-worker.ts --bundle --outfile=dist/triangulation-worker.js --platform=browser --external:{stream} --define:__VERSION__=\\\"$npm_package_version\\\"",
"pre-build2": "cp fixed-package.json ../../node_modules/apache-arrow/package.json && npm run build-bundle && npm run build-worker",
"build-worker": "esbuild src/workers/arrow-worker.ts --bundle --outfile=dist/arrow-worker.js --platform=browser --external:{stream} --define:__VERSION__=\\\"$npm_package_version\\\"",
"build-worker2": "esbuild src/workers/arrow-worker.ts --bundle --outfile=dist/arrow-worker.js --platform=browser --external:{stream}"
},
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions modules/arrow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ export {
export {parseGeometryFromArrow} from './geoarrow/convert-geoarrow-to-geojson';

export {updateBoundsFromGeoArrowSamples} from './geoarrow/get-arrow-bounds';

// EXPERIMENTAL

export {TriangulationWorker, triangulateOnWorker} from './triangulate-on-worker';
47 changes: 47 additions & 0 deletions modules/arrow/src/triangulate-on-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import type {WorkerOptions} from '@loaders.gl/worker-utils';
import {processOnWorker} from '@loaders.gl/worker-utils';

// __VERSION__ is injected by babel-plugin-version-inline
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';

export type TriangulationWorkerInput = TriangulateInput | {operation: 'test'; data: any};
export type TriangulationWorkerOutput = TriangulateResult | {operation: 'test'; data: any};

/** Input data for operation: 'triangulate' */
export type TriangulateInput = {
operation: 'triangulate';
polygonIndices: Uint16Array;
primitivePolygonIndices: Int32Array;
flatCoordinateArray: Float64Array;
nDim: number;
};

/** Result type for operation: 'triangulate' */
export type TriangulateResult = TriangulateInput & {
triangleIndices: Uint32Array;
};

/**
* Worker for tessellating geometries. Normally called through triangulateOnWorker
*/
export const TriangulationWorker = {
id: 'triangulation',
name: 'Triangulate',
module: 'arrow',
version: VERSION,
options: {}
};

/**
* Provide type safety
*/
export function triangulateOnWorker(
data: TriangulationWorkerInput,
options: WorkerOptions = {}
): Promise<TriangulationWorkerOutput> {
return processOnWorker(TriangulationWorker, data, options);
}
4 changes: 4 additions & 0 deletions modules/arrow/src/workers/triangulation-worker-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import './triangulation-worker';
39 changes: 39 additions & 0 deletions modules/arrow/src/workers/triangulation-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import {createWorker} from '@loaders.gl/worker-utils';
import {getTriangleIndices} from '../geoarrow/convert-geoarrow-to-binary-geometry';
import type {
TriangulationWorkerInput,
TriangulateInput,
TriangulateResult
} from '../triangulate-on-worker';

createWorker(async (data, options = {}) => {
const input = data as TriangulationWorkerInput;
const operation = input?.operation;
switch (operation) {
case 'test':
return input;
case 'triangulate':
return triangulateBatch(data);
default:
throw new Error(
`TriangulationWorker: Unsupported operation ${operation}. Expected 'triangulate'`
);
}
});

function triangulateBatch(data: TriangulateInput): TriangulateResult {
// Parse any WKT/WKB geometries
// Build binary geometries
// Call earcut and triangulate
console.error('TriangulationWorker: tessellating batch', data);

Check warning on line 31 in modules/arrow/src/workers/triangulation-worker.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement

Check warning on line 31 in modules/arrow/src/workers/triangulation-worker.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected console statement

Check warning on line 31 in modules/arrow/src/workers/triangulation-worker.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected console statement
const triangleIndices = getTriangleIndices(
data.polygonIndices,
data.primitivePolygonIndices,
data.flatCoordinateArray,
data.nDim
);
return {...data, triangleIndices};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import test, {Test} from 'tape-promise/tape';

import {getGeometryColumnsFromSchema} from '@loaders.gl/gis';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import test, {Test} from 'tape-promise/tape';

import {fetchFile, parse} from '@loaders.gl/core';
Expand Down
3 changes: 3 additions & 0 deletions modules/arrow/test/geoarrow/get-arrow-bounds.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';

import {updateBoundsFromGeoArrowSamples} from '@loaders.gl/arrow';
Expand Down
2 changes: 2 additions & 0 deletions modules/arrow/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import './arrow-writer.spec';
import './geoarrow/convert-geoarrow-to-binary-geometry.spec';
import './geoarrow/convert-geoarrow-to-geojson.spec';
import './geoarrow/get-arrow-bounds.spec';

import './triangulate-on-worker.spec';
68 changes: 68 additions & 0 deletions modules/arrow/test/triangulate-on-worker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// loaders.gl, MIT license
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';
import {triangulateOnWorker, TriangulationWorker} from '@loaders.gl/arrow';
import {processOnWorker, isBrowser, WorkerFarm} from '@loaders.gl/worker-utils';

// WORKER TESTS
test('TriangulationWorker#plumbing', async (t) => {
const sourceData = {
operation: 'test',
data: new ArrayBuffer(100)
};

const triangulatedData = await processOnWorker(TriangulationWorker, sourceData, {
_workerType: 'test'
});

t.ok(triangulatedData, 'Triangulation worker echoed input data');

t.rejects(
() =>
processOnWorker(
TriangulationWorker,
{operation: 'error'},
{
_workerType: 'test'
}
),
'Triangulation worker throws on incorrect operation'
);

if (!isBrowser) {
const workerFarm = WorkerFarm.getWorkerFarm({});
workerFarm.destroy();
}

t.end();
});

test('triangulateOnWorker#plumbing', async (t) => {
const triangulatedData = await triangulateOnWorker(
{
operation: 'test',
data: new ArrayBuffer(100)
},
{
_workerType: 'test'
}
);

t.equal(triangulatedData.operation, 'test', 'Triangulation worker got correct return type');
if (triangulatedData.operation === 'test') {
t.equal(triangulatedData.data?.byteLength, 100, 'Triangulation worker echoed input data');
}

// t.rejec(() => await processOnWorker(TriangulationWorker, sourceData, {
// operation: 'error',
// _workerType: 'test'
// }), 'Triangulation worker throws on incorrect operation');

if (!isBrowser) {
const workerFarm = WorkerFarm.getWorkerFarm({});
workerFarm.destroy();
}

t.end();
});
2 changes: 2 additions & 0 deletions modules/worker-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export {default as WorkerFarm} from './lib/worker-farm/worker-farm';
export {default as WorkerPool} from './lib/worker-farm/worker-pool';
export {default as WorkerBody} from './lib/worker-farm/worker-body';

// PROCESS ON WORKER
export type {ProcessOnWorkerOptions} from './lib/worker-api/process-on-worker';
export {processOnWorker, canProcessOnWorker} from './lib/worker-api/process-on-worker';
export {createWorker} from './lib/worker-api/create-worker';

Expand Down
3 changes: 2 additions & 1 deletion modules/worker-utils/src/lib/worker-api/process-on-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import WorkerFarm from '../worker-farm/worker-farm';
import {getWorkerURL, getWorkerName} from './get-worker-url';
import {getTransferListForWriter} from '../worker-utils/get-transfer-list';

type ProcessOnWorkerOptions = WorkerOptions & {
/** Options for worker processing */
export type ProcessOnWorkerOptions = WorkerOptions & {
jobName?: string;
[key: string]: any;
};
Expand Down
Loading