Skip to content

Commit

Permalink
Merge pull request #634 from Original-Recipe/fix-attrColorIssue
Browse files Browse the repository at this point in the history
fix: Fix issue with main attribute color update & missing anchor points
  • Loading branch information
lihqi authored Feb 17, 2025
2 parents 812b440 + ec980c5 commit 6157d28
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class PolygonOperation extends BasicToolOperation {

public selection: Selection;

private attrChangeTrigger: number; // Force trigger for updating when switching to the same primary attribute

constructor(props: IPolygonOperationProps) {
super({
...props,
Expand All @@ -91,6 +93,7 @@ class PolygonOperation extends BasicToolOperation {
this.polygonList = [];
this.hoverPointIndex = -1;
this.hoverEdgeIndex = -1;
this.attrChangeTrigger = 0;

this.drawingHistory = new ActionsHistory();
this.isCtrl = false;
Expand Down Expand Up @@ -425,11 +428,16 @@ class PolygonOperation extends BasicToolOperation {
this.emit('selectedChange');
}

public setAttrChangeTrigger(attrChangeTrigger: number = 0) {
this.attrChangeTrigger = attrChangeTrigger;
}

public setDefaultAttribute(defaultAttribute: string = '') {
const oldDefault = this.defaultAttribute;
this.defaultAttribute = defaultAttribute;

if (oldDefault !== defaultAttribute) {
// When repeatedly modified to the same primary attribute, the new and old values are the same, but the update logic still needs to be followed, while other scenarios maintain the original logic
if (this.attrChangeTrigger || oldDefault !== defaultAttribute) {
// 如果更改 attribute 需要同步更改 style 的样式
this.changeStyle(defaultAttribute);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const PointCloud2DRectOperationView = (props: IPointCloud2DRectOperationViewProp
afterImgOnLoad,
shouldExcludePointCloudBoxListUpdate,
} = props;
const { selectBoxVisibleSwitch } = useToolConfigStore();
const { selectBoxVisibleSwitch, attrChangeTrigger } = useToolConfigStore();

const imageUrl = mappingData?.url ?? '';
const fallbackUrl = mappingData?.fallbackUrl ?? '';
Expand Down Expand Up @@ -270,10 +270,8 @@ const PointCloud2DRectOperationView = (props: IPointCloud2DRectOperationViewProp

const onRightClick = ({ targetId, id }: { targetId: string; id: string }) => {
setNeedUpdateCenter(false);
requestAnimationFrame(() => {
updateSelectedIDs(targetId);
setRightClickRectId(id);
});
updateSelectedIDs(targetId);
setRightClickRectId(id);
};

const updateSelectedIDs = (newID: string) => {
Expand All @@ -283,6 +281,42 @@ const PointCloud2DRectOperationView = (props: IPointCloud2DRectOperationViewProp
}
};

const updateRectListAndAttr = () => {
const rect = rectListInImage.find((i) => i.id === operation.current.selectedRectID);
operation.current?.setDefaultAttribute?.(defaultAttribute);

if (rect) {
// updateRectIn2DView({ ...operation.current?.selectedRect, attribute: defaultAttribute });
updateRectListByReducer((preRectList) => {
const filtered: IPointCloudBoxRect[] = [];
let matched: any = null;

preRectList.forEach((item: IPointCloudBoxRect) => {
if (item.id !== operation.current.selectedRectID) {
filtered.push(item);
} else {
matched = item;
}
});

if (rect.extId === undefined) {
matched = operation.current?.selectedRect;
}

return [
...filtered,
{
...(matched || {}),
attribute: defaultAttribute,
},
];
});
}
// When the attribute changes, it is necessary to record the status of the previous clicks
setIsMemoryChange(true);
updateRectList();
};

useEffect(() => {
if (ref.current) {
const toolInstance = new PointCloud2DRectOperation({
Expand Down Expand Up @@ -354,40 +388,15 @@ const PointCloud2DRectOperationView = (props: IPointCloud2DRectOperationViewProp
}, [pointCloudBoxList]);

useEffect(() => {
const rect = rectListInImage.find((i) => i.id === operation.current.selectedRectID);
operation.current?.setDefaultAttribute?.(defaultAttribute);

if (rect) {
// updateRectIn2DView({ ...operation.current?.selectedRect, attribute: defaultAttribute });
updateRectListByReducer((preRectList) => {
const filtered: IPointCloudBoxRect[] = [];
let matched: any = null;

preRectList.forEach((item: IPointCloudBoxRect) => {
if (item.id !== operation.current.selectedRectID) {
filtered.push(item);
} else {
matched = item;
}
});

if (rect.extId === undefined) {
matched = operation.current?.selectedRect;
}
updateRectListAndAttr();
}, [defaultAttribute]);

return [
...filtered,
{
...(matched || {}),
attribute: defaultAttribute,
},
];
});
useEffect(() => {
// Force trigger for updating when switching to the same primary attribute
if (attrChangeTrigger) {
updateRectListAndAttr();
}
// When the attribute changes, it is necessary to record the status of the previous clicks
setIsMemoryChange(true);
updateRectList();
}, [defaultAttribute]);
}, [attrChangeTrigger]);

useEffect(() => {
const prevRectList = prevRectListRef.current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ const PointCloudListener: React.FC<IProps> = ({

// Update the listener of toolInstance.
useEffect(() => {
toolInstanceRef.current.setAttrChangeTrigger = (attrChangeTrigger: number) => {
ptCtx.topViewInstance?.toolInstance.setAttrChangeTrigger(attrChangeTrigger);
};

toolInstanceRef.current.setDefaultAttribute = (newAttribute: string) => {
syncThreeViewsAttribute(newAttribute);

Expand Down Expand Up @@ -335,7 +339,6 @@ const PointCloudListener: React.FC<IProps> = ({
// Avoid triggering SetState operations in the reducer phase
setTimeout(() => {
ptCtx.setPointCloudValid(valid);

});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const pointCloudToolStateCreator: StateCreator<PointCloudToolConfigStore> = (set
selectBoxVisibleSwitch: false,
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch: boolean) =>
set(() => ({ selectBoxVisibleSwitch })),
attrChangeTrigger: 0,
setAttrChangeTrigger: (attrChangeTrigger: number) => set(() => ({ attrChangeTrigger })),
});

export default pointCloudToolStateCreator;
3 changes: 3 additions & 0 deletions packages/lb-components/src/store/toolConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface BaseStore {
export interface PointCloudToolConfigStore {
selectBoxVisibleSwitch: boolean;
setSelectBoxVisibleSwitch: (selectBoxVisibleSwitch: boolean) => void;
// Force trigger for updating when switching to the same primary attribute
attrChangeTrigger: number;
setAttrChangeTrigger: (attrChangeTrigger: number) => void;
}

// Import all types into toolConfig Store through inheritance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { usePointCloudViews } from '@/components/pointCloudView/hooks/usePointCl
import SubAttributeList from '@/components/subAttributeList';
import DynamicResizer from '@/components/DynamicResizer';
import { isNumber } from 'lodash';
import useToolConfigStore from '@/store/toolConfig';

interface IProps {
stepInfo: IStepInfo;
toolInstance: ICustomToolInstance; // Created by useCustomToolInstance.
Expand Down Expand Up @@ -224,6 +226,7 @@ const AttributeUpdater = ({
const { isPointCloudSegmentationPattern } = useStatus();

const dispatch = useDispatch();
const { attrChangeTrigger, setAttrChangeTrigger } = useToolConfigStore();

const titleStyle = {
fontWeight: 500,
Expand Down Expand Up @@ -281,6 +284,11 @@ const AttributeUpdater = ({
return;
}

// When the changing attributes are consistent, trigger+1 ensures forced update
const newAttrChangeTrigger = attribute === defaultAttribute ? attrChangeTrigger + 1 : 0;

setAttrChangeTrigger(newAttrChangeTrigger);
toolInstance.setAttrChangeTrigger(newAttrChangeTrigger);
toolInstance.setDefaultAttribute(attribute);
};

Expand Down

0 comments on commit 6157d28

Please sign in to comment.