Skip to content

Commit

Permalink
fix(pointcloud): Fix pointCloud polygon data problem
Browse files Browse the repository at this point in the history
The origin coordinate system for polygon data in pointCloud is based on the canvas2d coordinate system, but it needs to be based on the pointCloud system.
  • Loading branch information
Kerwin-L committed Feb 3, 2023
1 parent 1df9807 commit 03e9deb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/lb-annotation/src/core/pointCloud/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ export class PointCloudAnnotation implements IPointCloudAnnotationOperation {
}) as IPolygonData[];

if (extraList) {
polygonList = polygonList.concat(extraList);
// Convert extraList(polygonList) from PointCloud coordinate to Canvas Coordinate
polygonList = polygonList.concat(
extraList.map((v) => ({
...v,
pointList: v?.pointList?.map((point) =>
PointCloudUtils.transferWorld2Canvas(point, this.pointCloud2dOperation.size),
),
})),
);
}

this.pointCloud2dOperation.setResult(polygonList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FooterDivider } from '@/views/MainView/toolFooter';
import { ZoomController } from '@/views/MainView/toolFooter/ZoomController';
import { DownSquareOutlined, UpSquareOutlined } from '@ant-design/icons';
import { cTool, PointCloudAnnotation } from '@labelbee/lb-annotation';
import { IPolygonData } from '@labelbee/lb-utils';
import { IPolygonData, PointCloudUtils } from '@labelbee/lb-utils';
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { PointCloudContext } from './PointCloudContext';
import { useRotate } from './hooks/useRotate';
Expand Down Expand Up @@ -198,7 +198,15 @@ const PointCloudTopView: React.FC<IProps> = ({

TopView2dOperation.singleOn('polygonCreated', (polygon: IPolygonData) => {
if (TopView2dOperation.pattern === EPolygonPattern.Normal || !currentData?.url) {
addPolygon(polygon);
/**
* Notice. The Polygon need to be converted to pointCloud coordinate system for storage.
*/
const newPolygon = {
...polygon,
pointList: polygon.pointList.map((v) => PointCloudUtils.transferCanvas2World(v, size)),
};

addPolygon(newPolygon);
ptCtx.setSelectedIDs(polygon.id);
return;
}
Expand Down
37 changes: 37 additions & 0 deletions packages/lb-utils/src/PointCloudUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author Ron <ron.f.luo@gmail.com>
*/

import { IPolygonData } from './types';
import { IPointCloudBox, IPointCloudConfig } from './types/pointCloud';

class PointCloudUtils {
Expand Down Expand Up @@ -51,6 +52,42 @@ class PointCloudUtils {
return pointCloudDataList;
}

/**
* Get the coordinate from canvas2d-coordinate to world coordinate
*/
public static transferCanvas2World = (
currentPos: { x: number; y: number },
size: { width: number; height: number },
) => {
const { width: w, height: h } = size;
const { x, y, ...otherProps } = currentPos;

// x-Axis is the Positive Direction, so the x-coordinates need to be swapped with the y-coordinates
return {
x: -y + h / 2,
y: -(x - w / 2),
...otherProps,
};
};

/**
* Get the coordinate from canvas2d-coordinate to world coordinate
*/
public static transferWorld2Canvas = (
currentPos: { x: number; y: number },
size: { width: number; height: number },
) => {
const { width: w, height: h } = size;
const { x, y, ...otherProps } = currentPos;

// x-Axis is the Positive Direction, so the x-coordinates need to be swapped with the y-coordinates
return {
x: -y + w / 2,
y: -x + h / 2,
...otherProps,
};
};

public static getPolygonListFromResultList(result: string): any[] {
const data = this.jsonParser(result);

Expand Down

0 comments on commit 03e9deb

Please sign in to comment.