Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
feat: export Apis at the top level
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxuhai committed Oct 7, 2022
1 parent fb1785c commit 5479c3d
Show file tree
Hide file tree
Showing 102 changed files with 607 additions and 656 deletions.
12 changes: 6 additions & 6 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ import * as PCL from 'pcl.js';
async function main() {
// 初始化
const pcl = await PCL.init({
await PCL.init({
/**
* 推荐,可选配置,自定义 WebAssembly 文件链接
* @default js 文件所在目录 + pcl-core.wasm
Expand All @@ -127,7 +127,7 @@ main();
<script>
async function main() {
// 初始化,PCL 是全局对象
const pcl = await PCL.init();
await PCL.init();
// ...
}
Expand All @@ -143,25 +143,25 @@ main();
import * as PCL from 'pcl.js';

async function main() {
const pcl = await PCL.init({
await PCL.init({
url: 'https://cdn.jsdelivr.net/npm/pcl.js/dist/pcl-core.wasm',
});

// 获取 PCD 文件
const data = await fetch('https://cdn.jsdelivr.net/gh/luoxuhai/pcl.js@master/data/rops_tutorial/points.pcd').then(res => res.arrayBuffer());
// 加载 PCD 数据,返回点云对象
const cloud = pcl.io.loadPCDData<PCL.PointXYZ>(data, PCL.PointXYZ);
const cloud = PCL.loadPCDData<PCL.PointXYZ>(data, PCL.PointXYZ);

// 使用 PassThrough 过滤器过滤点云
// 参考: https://pcl.readthedocs.io/projects/tutorials/en/master/passthrough.html#passthrough
const pass = new pcl.filters.PassThrough<PCL.PointXYZ>(PCL.PointXYZ);
const pass = new PCL.PassThrough<PCL.PointXYZ>(PCL.PointXYZ);
pass.setInputCloud(cloud);
pass.setFilterFieldName('z');
pass.setFilterLimits(0.0, 1.0);
const cloudFiltered = pass.filter();

// 将过滤后的点云对象保存为 PCD 文件, 内容为 ArrayBuffer
const cloudFilteredData = pcl.io.savePCDDataASCII(cloudFiltered);
const cloudFilteredData = PCL.savePCDDataASCII(cloudFiltered);
}

main();
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ main();
<script>
async function main() {
// Initialization, PCL is a global object.
const pcl = await PCL.init();
await PCL.init();
// ...
}
Expand All @@ -145,25 +145,25 @@ main();
import * as PCL from 'pcl.js';

async function main() {
const pcl = await PCL.init({
await PCL.init({
url: 'https://cdn.jsdelivr.net/npm/pcl.js/dist/pcl-core.wasm',
});

// Get PCD file
const data = await fetch('https://cdn.jsdelivr.net/gh/luoxuhai/pcl.js@master/data/rops_tutorial/points.pcd').then(res => res.arrayBuffer());
// Load PCD file data, return point cloud object
const cloud = pcl.io.loadPCDData<PCL.PointXYZ>(data, PCL.PointXYZ);
const cloud = PCL.loadPCDData<PCL.PointXYZ>(data, PCL.PointXYZ);

// Filtering a PointCloud using a PassThrough filter
// See: https://pcl.readthedocs.io/projects/tutorials/en/master/passthrough.html#passthrough
const pass = new pcl.filters.PassThrough<PCL.PointXYZ>(PCL.PointXYZ);
const pass = new PCL.PassThrough<PCL.PointXYZ>(PCL.PointXYZ);
pass.setInputCloud(cloud);
pass.setFilterFieldName('z');
pass.setFilterLimits(0.0, 1.0);
const cloudFiltered = pass.filter();

// Save filtered point cloud objects as PCD files, the content is ArrayBuffer
const cloudFilteredData = pcl.io.savePCDDataASCII(cloudFiltered);
const cloudFilteredData = PCL.savePCDDataASCII(cloudFiltered);
}

main();
Expand Down
10 changes: 1 addition & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,16 @@ function collectTypeDefinition(moduleName) {
});
});
},
};
};

const addExtra = (moduleName) => {
return {
renderChunk(code) {
return `
/// <reference path="../../src/emscripten.d.ts" />
/// <reference path="../../src/global.d.ts" />
${code}
export as namespace ${moduleName};`;
},
};
};

return [tscAlias(), dts(), addExtra(moduleName)];
return [tscAlias(), dts()];
}

const constants = {
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// pcl.js version
export const VERSION = '__version__';

// Point Cloud Library (PCL) version
export const PCL_VERSION = '1.12.1';
113 changes: 32 additions & 81 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import initPCLCore from '@/bind/build/pcl-core';
import fs from '@/modules/fs';
import io from '@/modules/io';
import filters from '@/modules/filters';
import registration from '@/modules/registration';
import segmentation from '@/modules/segmentation';
import common from '@/modules/common';
import kdtree from '@/modules/kdtree';
import search from '@/modules/search';
import keypoints from '@/modules/keypoints';
import { ENVIRONMENT_IS_NODE } from '@/utils';
import { Emscripten } from '@/types';

export * from '@/constants';
export * as fs from '@/modules/fs';
export * from '@/modules/io';
export * from '@/modules/common';
export * from '@/modules/kdtree';
export * from '@/modules/search';
export * from '@/modules/filters';
export * from '@/modules/keypoints';
export * from '@/modules/segmentation';
export * from '@/modules/registration';

if (window.__PCLCore__) {
console.warn('Multiple instances of pcl.js being imported.');
} else {
window.__PCLCore__ = null;
}

let isInitialized = false;

interface InitOptions {
/**
Expand All @@ -25,52 +36,15 @@ interface InitOptions {
* @default { credentials: 'same-origin' }
*/
fetchOptions?: Emscripten.ModuleOpts['fetchSettings'];
/**
* Show log in console
*
* @default true
*/
log?: boolean;
onsuccess?: (module: Emscripten.Module) => void;
onsuccess?: () => void;
onerror?: (error: unknown) => void;
}

interface PCLInstance {
/**
* Emscripten Module
* {@link https://emscripten.org/docs/api_reference/module.html}
*/
Module: Emscripten.Module;
/**
* File system
* {@link https://emscripten.org/docs/api_reference/Filesystem-API.html}
*/
fs: Pick<
Emscripten.FS,
'readdir' | 'readFile' | 'writeFile' | 'stat' | 'mkdir' | 'rmdir' | 'rename' | 'unlink'
>;
/**
* Base info
*/
info: {
PCL_VERSION: string;
};
common: typeof common;
io: typeof io;
filters: typeof filters;
registration: typeof registration;
kdtree: typeof kdtree;
search: typeof search;
keypoints: typeof keypoints;
segmentation: typeof segmentation;
}

async function init(options?: InitOptions): Promise<PCLInstance | null> {
async function init(options?: InitOptions) {
const {
arrayBuffer,
url,
fetchOptions: fetchSettings = { credentials: 'same-origin' },
log = true,
} = options ?? {};

const moduleOptions: Emscripten.ModuleOpts = {
Expand All @@ -79,47 +53,24 @@ async function init(options?: InitOptions): Promise<PCLInstance | null> {
fetchSettings,
};

let Module: Emscripten.Module;
try {
Module = await initPCLCore(moduleOptions);
const __PCLCore__ = await initPCLCore(moduleOptions);
if (ENVIRONMENT_IS_NODE) {
(global as any).__PCLCore__ = Module;
(global as any).__PCLCore__ = __PCLCore__;
} else {
window.__PCLCore__ = Module;
window.__PCLCore__ = __PCLCore__;
}
options?.onsuccess?.(Module);
isInitialized = true;
options?.onsuccess?.();
} catch (error) {
options?.onerror?.(error);
throw error;
}

const PCL_VERSION: string = Module.PCL_VERSION;

if (log) {
console.log('pcl.js version: __version__');
console.log(`PCL version: ${PCL_VERSION}`);
}

const info = {
PCL_VERSION,
};

return {
Module,
info,
fs: fs(),
io,
filters,
registration,
segmentation,
common,
kdtree,
search,
keypoints,
};
}

const VERSION = '__version__';
function destroy() {
window.__PCLCore__ = null;
isInitialized = false;
}

export { init, VERSION, PCLInstance, InitOptions };
export * from '../modules/common/point-types';
export { InitOptions, init, destroy, isInitialized };
Loading

1 comment on commit 5479c3d

@vercel
Copy link

@vercel vercel bot commented on 5479c3d Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pcl-js – ./

pcl-js-darkce.vercel.app
pcljs.org
www.pcljs.org
pcl-js-git-master-darkce.vercel.app

Please sign in to comment.