Skip to content

Commit

Permalink
feat: support Same Frame Copy, Automatically recalculate ID, Fix hist…
Browse files Browse the repository at this point in the history
…orical copy issues, I18n's content Add.
  • Loading branch information
Original-Recipe committed Aug 20, 2024
1 parent aa8bb49 commit 7fd6c43
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IPointCloudBox, IPointCloudBoxList, IPointCloudConfig } from '@labelbee/lb-utils';
import { useCallback, useContext, useMemo, useState } from 'react';
import _ from 'lodash';
import { message, Modal } from 'antd';
import { message } from 'antd';
import { usePointCloudViews } from './usePointCloudViews';
import { PointCloudContext } from '../PointCloudContext';
import { useTranslation } from 'react-i18next';
import { EPointCloudBoxRenderTrigger } from '@/utils/ToolPointCloudBoxRenderHelper';
import AnnotationDataUtils from '@/utils/AnnotationDataUtils';
import { IFileItem } from '@/types/data';
import { uuid } from '@labelbee/lb-annotation';
import { useHistory } from './useHistory';

/**
* For each `rect`, the value of `imageName` on the paste page should be calculated from the value on the copy page using `getNextPath` in `AnnotationDataUtils`.
Expand Down Expand Up @@ -42,6 +44,30 @@ const updateBoxRects = (
};
};

// Update the Id of the copied box
const updateCopiedBoxesId = (
pointCloudBoxList: IPointCloudBoxList,
copiedBoxes: IPointCloudBoxList,
) => {
// View the ID of the largest box
const maxTrackID = Math.max(...pointCloudBoxList.map((item) => item.trackID || 0), 0);
const positionValue = 5;

return copiedBoxes.map((item, index) => {
return {
...item,
id: uuid(),
uuid: uuid(),
center: {
x: item.center.x - positionValue,
y: item.center.y - positionValue,
z: item.center.z,
},
trackID: maxTrackID + index + 1,
};
});
};

/**
* Actions for selected boxes
*/
Expand Down Expand Up @@ -72,16 +98,7 @@ export const useBoxes = ({

const { pointCloudBoxListUpdated } = usePointCloudViews();
const { t, i18n } = useTranslation();

const hasDuplicateID = (checkBoxList: IPointCloudBoxList) => {
if (config.trackConfigurable !== true) {
return false;
}

return pointCloudBoxList.some((item) => {
return checkBoxList.some((i) => i.trackID === item.trackID);
});
};
const { pushHistoryWithList } = useHistory();

const selectedBoxes = useMemo(() => {
return displayPointCloudList.filter((i) => selectedIDs.includes(i.id));
Expand All @@ -101,6 +118,7 @@ export const useBoxes = ({
});
message.error(t('CopyEmptyInPointCloud'));
}
message.success(t('CopySuccess'));
}, [selectedIDs, displayPointCloudList, i18n.language, currentData]);

const pasteSelectedBoxes = useCallback(() => {
Expand All @@ -109,52 +127,38 @@ export const useBoxes = ({
return;
}

const hasDuplicate = hasDuplicateID(copiedBoxes);

const mappingImgList = currentData?.mappingImgList ?? [];
const preMappingImgList = copiedParams?.copiedMappingImgList ?? [];

const pastedBoxes = copiedBoxes.map((box) => {
return updateBoxRects(box, mappingImgList, preMappingImgList);
});
// Update the results of all point cloud 3D frames
const updatePointCloudResult = (pointCloudBoxList: IPointCloudBoxList) => {
const mappingImgList = currentData?.mappingImgList ?? [];
const preMappingImgList = copiedParams?.copiedMappingImgList ?? [];
const newPointCloudBoxList = pointCloudBoxList.map((box) =>
updateBoxRects(box, mappingImgList, preMappingImgList),
);

const updatePointCloudResult = (newPointCloudBoxList: IPointCloudBoxList) => {
/** Paste succeed and empty */
setPointCloudResult(newPointCloudBoxList);
pointCloudBoxListUpdated?.(newPointCloudBoxList);
setCopiedParams({
copiedBoxes: [],
copiedMappingImgList: [],
});

/**
* Update the data in the historical data stack to ensure data synchronization.
* To solve the problem of dragging A to highlight the color without updating when copying and pasting an A1
*/
pushHistoryWithList({
pointCloudBoxList: newPointCloudBoxList,
});

syncAllViewPointCloudColor(EPointCloudBoxRenderTrigger.MultiPaste, newPointCloudBoxList);
};

if (hasDuplicate) {
Modal.confirm({
title: t('HasDuplicateIDHeader'),
content: t('HasDuplicateIDMsg'),
onOk: () => {
/**
* Filter the same trackID in old-pointCloudBoxList.
*/
const newPointCloudResult = pointCloudBoxList
.filter((v) => {
if (pastedBoxes.find((c) => c.trackID === v.trackID)) {
return false;
}
return true;
})
.concat(pastedBoxes);

updatePointCloudResult(newPointCloudResult);
},
});
} else {
/** Paste succeed and empty */
const newPointCloudResult = [...displayPointCloudList, ...pastedBoxes];
// Get the box with the latest updated ID
const pastedBoxes = updateCopiedBoxesId(displayPointCloudList, copiedBoxes);
const newPointCloudResult = [...displayPointCloudList, ...pastedBoxes];

updatePointCloudResult(newPointCloudResult);
}
updatePointCloudResult(newPointCloudResult);
}, [copiedBoxes, displayPointCloudList, i18n.language, currentData]);

return { copySelectedBoxes, pasteSelectedBoxes, copiedBoxes, selectedBoxes };
Expand Down
2 changes: 2 additions & 0 deletions packages/lb-utils/src/i18n/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"HasDuplicateID": "Duplicate ID exists, copy failed",
"HasDuplicateIDHeader": "TrackID duplication exists",
"HasDuplicateIDMsg": "There is an issue with duplicate trackIDs, override it?",
"CopySuccess": "Copy Success!",
"LowerLimitPointsNumInBox": "The number of points in the box must not be less than {{num}}",
"PointThickness": "Point Thickness",
"RotateAroundCenterPoint": "Rotate Around Center Point",
Expand Down Expand Up @@ -527,6 +528,7 @@
"HasDuplicateID": "存在重复ID,复制失败",
"HasDuplicateIDHeader": "存在标注框 ID 重复",
"HasDuplicateIDMsg": "存在标注框 ID 重复的问题,是否要覆盖?",
"CopySuccess": "复制成功!",
"LowerLimitPointsNumInBox": "框体内点数不允许低于 {{num}} 个",
"PointThickness": "点的显示粗细",
"RotateAroundCenterPoint": "绕中心点旋转画面",
Expand Down

0 comments on commit 7fd6c43

Please sign in to comment.