From b9d32d9c19f3d720b2f227c7f96283eaec765103 Mon Sep 17 00:00:00 2001 From: Darkce Date: Sat, 22 Oct 2022 09:02:24 +0800 Subject: [PATCH] feat: add size and data to PointCloud --- src/modules/common/PointCloud.ts | 19 +++++++++ tests/common/PointCloud.test.ts | 9 +++++ website/docs/api/basic-structures.mdx | 58 ++++++++++++++++----------- 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/modules/common/PointCloud.ts b/src/modules/common/PointCloud.ts index ea14db6..d60b374 100644 --- a/src/modules/common/PointCloud.ts +++ b/src/modules/common/PointCloud.ts @@ -28,6 +28,17 @@ export class Vector { return this._native.empty(); } + get data() { + const size = this.size; + const _data: T[] = []; + + for (let i = 0; i < size; i++) { + _data.push(this._native.get(i)); + } + + return _data; + } + public resize(count: number, value?: T) { this._native.resize(count, value ?? null); } @@ -147,6 +158,14 @@ class PointCloud { return this._points; } + get isEmpty() { + return this.points.isEmpty(); + } + + get data() { + return this.points.data; + } + /** * Removes all points in a cloud and sets the width and height to 0. */ diff --git a/tests/common/PointCloud.test.ts b/tests/common/PointCloud.test.ts index 54d0fc8..2556189 100644 --- a/tests/common/PointCloud.test.ts +++ b/tests/common/PointCloud.test.ts @@ -1,4 +1,5 @@ import * as PCL from '../../'; +import { getTestPCDFile } from '../utils'; describe('PointCloud', () => { it('should create a point cloud data with XYZ fields, `width` = 5, `height` = 1', async () => { @@ -21,4 +22,12 @@ describe('PointCloud', () => { cloud.manager.delete(); expect(cloud.manager.isDeleted()).toBe(true); }); + + it('should get `size` and `data`', () => { + const data = getTestPCDFile('bun4.pcd'); + const cloud = PCL.loadPCDData(data); + + expect(cloud.size).toBe(361); + expect(cloud.data.length).toBe(361); + }); }); diff --git a/website/docs/api/basic-structures.mdx b/website/docs/api/basic-structures.mdx index d96e253..dbb023c 100644 --- a/website/docs/api/basic-structures.mdx +++ b/website/docs/api/basic-structures.mdx @@ -12,31 +12,41 @@ sidebar_position: 2 new PointCloud(); ``` -### Members - -#### isDense - -#### isOrganized - -#### points - -#### width - -#### height - -#### header - -#### size - -### Methods - -#### clear() - -#### resize() - -#### addPoint() +```ts showLineNumbers +class PointCloud { + manager: Manager; + constructor(_PT?: PointTypesTypeof, _native?: NativeAPI); + get isOrganized(): boolean; + get isDense(): boolean; + set width(v: number); + get width(): number; + set height(v: number); + get height(): number; + get header(): PCLHeader; + get size(): number; + get points(): Points; + get isEmpty(): boolean; + get data(): T[]; + /** + * Removes all points in a cloud and sets the width and height to 0. + */ + clear(): void; + /** + * Resizes the container to contain `count` elements + * @params count - New size of the point cloud + * @params pt - The value to initialize the new points with + */ + resize(count: number, pt?: T): void; + /** + * Insert a new point in the cloud, at the end of the container. + * @description This breaks the organized structure of the cloud by setting the height to 1! + * @params pt - The point to insert + */ + addPoint(pt?: T | null): void; +} +``` -#### manager +## Manager ```ts class Manager {