Skip to content

Commit

Permalink
Merge pull request #313 from open-mmlab/feat/pointCloudSegment-projec…
Browse files Browse the repository at this point in the history
…tion

Feat/point cloud segment projection
  • Loading branch information
Kerwin-L authored Sep 21, 2023
2 parents a36eae0 + 206476c commit 240593e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
6 changes: 1 addition & 5 deletions packages/lb-annotation/src/core/pointCloud/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,6 @@ class PointCloudStore {
}

public clearStash() {
if (this.isCheckStatus) {
this.resetSelectedSegmentStatus();
return;
}

if (this.isEditStatus && this.cacheSegData) {
this.updateCloudDataStatus(this.cacheSegData.points, { visible: false });
if (this.segmentData.has(this.cacheSegData.id)) {
Expand All @@ -584,6 +579,7 @@ class PointCloudStore {
}
this.syncSegmentData();
}
this.resetSelectedSegmentStatus();
}

public deleteSelectedSegmentData(id = '') {
Expand Down
11 changes: 8 additions & 3 deletions packages/lb-annotation/src/core/toolOperation/ViewOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ export default class ViewOperation extends BasicToolOperation {
}

// 1. Get the cached.
const uid = this.imgNode.src + data.length;
const uid = this.imgNode.src + data.length + annotation.defaultRGBA;
const cacheCanvas = this.cacheCanvas?.[uid];
if (cacheCanvas) {
DrawUtils.drawImg(this.canvas, cacheCanvas, {
Expand All @@ -640,8 +640,13 @@ export default class ViewOperation extends BasicToolOperation {
const size = { width: this.imgNode.width, height: this.imgNode.height };
const { ctx, canvas: offsetCanvas } = CanvasUtils.createCanvas(size);
if (ctx && data?.length > 0) {
// 1.
DrawUtils.drawPixel({ canvas: offsetCanvas, points: data, size });
DrawUtils.drawPixel({
canvas: offsetCanvas,
points: data,
size,
defaultRGBA: annotation.defaultRGBA,
pixelSize: 13,
});
DrawUtils.drawImg(this.canvas, offsetCanvas, {
zoom: this.zoom,
currentPos: this.currentPos,
Expand Down
15 changes: 8 additions & 7 deletions packages/lb-annotation/src/utils/tool/DrawUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ICuboid, ICuboidConfig, IDrawingCuboid } from '@/types/tool/cuboid';
import rgba from 'color-rgba';
import { IPixelPoints, MathUtils } from '@labelbee/lb-utils';
import { IPixelPoints, MathUtils, NULL_COLOR } from '@labelbee/lb-utils';
import { DEFAULT_FONT, ELineTypes, SEGMENT_NUMBER } from '../../constant/tool';
import { IPolygonPoint } from '../../types/tool/polygon';
import PolygonUtils from './PolygonUtils';
Expand Down Expand Up @@ -850,30 +850,31 @@ export default class DrawUtils {
canvas,
points,
size,
defaultRGBA,
pixelSize = 13,
}: {
canvas: HTMLCanvasElement;
points: IPixelPoints[];
size: ISize;
defaultRGBA?: string;
pixelSize?: number;
}) {
const ctx = canvas.getContext('2d')!;
const { width, height } = size;
const imageData = ctx.getImageData(0, 0, width, height);
const updateColor = (baseIndex: number, color: string) => {
const [red, green, blue, alpha] = rgba(color);
const [red, green, blue, alpha] = rgba(defaultRGBA ?? NULL_COLOR);
const updateColor = (baseIndex: number) => {
imageData.data[baseIndex] = red;
imageData.data[baseIndex + 1] = green;
imageData.data[baseIndex + 2] = blue;
imageData.data[baseIndex + 3] = Math.floor(255 * alpha);
};

const maxSize = Math.max(width, height);
const pixelSize = Math.floor(Math.pow(MathUtils.calculateThousandsPlace(maxSize), 2));
const offsetArr = MathUtils.generateCoordinates(pixelSize);

points.forEach((item) => {
for (const [x, y] of offsetArr) {
const baseIndex = (item.y + y) * (imageData.width * 4) + (item.x + x) * 4;
updateColor(baseIndex, item.color);
updateColor(baseIndex);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ const PointCloudSegment2DSingleView = ({
}, [ptSegmentInstance]);

// Highlight the points by indexes & pcdMapping.
const highlight2DPoints = useCallback((indexes: number[], color: string) => {
const highlight2DPoints = useCallback((indexes: number[], defaultRGBA: string) => {
if (indexes) {
if (imgSizeRef.current) {
const cacheMap = pcdMapping.current;
const highlightWorker = new HighlightSegmentWorker();
setLoading(true);
highlightWorker.postMessage({ cacheMap, indexes, color });
highlightWorker.postMessage({ cacheMap, indexes, defaultRGBA });
highlightWorker.onmessage = (e: any) => {
setAnnotations(e.data.annotations);
highlightWorker.terminate();
Expand Down Expand Up @@ -107,11 +107,7 @@ const PointCloudSegment2DSingleView = ({
setAnnotations(
annotations.map((v) => ({
...v,
annotation: {
...v.annotation,
fill: toolStyle.fill,
stroke: toolStyle.fill,
},
defaultRGBA: toolStyle.stroke,
})),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
onmessage = function onmessage(e) {
const { cacheMap, indexes, color } = e.data;
const dataList = [];
const { cacheMap, indexes, defaultRGBA } = e.data;
const len = indexes.length;
const annotation = [];
for (let i = 0; i < len; i = i + 1) {
const point2d = cacheMap[indexes[i]];
if (point2d) {
annotation.push({
...point2d,
color,
});
}
}
postMessage({ annotations: [{ type: 'pixelPoints', annotation }] });
postMessage({ annotations: [{ type: 'pixelPoints', annotation, defaultRGBA }] });
};
3 changes: 2 additions & 1 deletion packages/lb-utils/src/types/annotationView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface ICuboid extends IDrawingCuboid, IBasicStyle {
}

export interface IPixelPoints extends ICoordinate {
color: string;
color?: string;
}

export type TAnnotationViewRect = {
Expand Down Expand Up @@ -137,6 +137,7 @@ export declare type TAnnotationViewCuboid = {
export declare type TAnnotationViewPixelPoints = {
type: 'pixelPoints';
annotation: IPixelPoints[];
defaultRGBA?: string;
};

export type TAnnotationViewData =
Expand Down

0 comments on commit 240593e

Please sign in to comment.