Skip to content

Commit

Permalink
Merge pull request #596 from Suntgr/feat/highlight
Browse files Browse the repository at this point in the history
feat: Highlight the selected rectangular 3D view
  • Loading branch information
lihqi authored Oct 24, 2024
2 parents 5d14bb9 + fc69a0c commit 50ba83e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
54 changes: 40 additions & 14 deletions packages/lb-annotation/src/core/pointCloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export class PointCloud extends EventListener {

private filterBoxWorkerTimer: ReturnType<typeof setTimeout> | null = null;

private highlightColor = 0xffff00;

constructor({
container,
noAppend,
Expand Down Expand Up @@ -244,18 +246,44 @@ export class PointCloud extends EventListener {
child.children.forEach((grandson) => {
if (grandson instanceof THREE.Mesh) {
clickMeshes.push(grandson);
} else {
this.updateMaterialColor(grandson, child.userData.defaultColor);
}
});
});
const intersects = this.raycaster.intersectObjects(clickMeshes, false);
if (intersects.length > 0) {
const intersectedObject = intersects[0].object;
const parentGroup = intersects[0].object.parent;
// Find the BoxHelper and change the color
parentGroup?.children.forEach((child) => {
this.updateMaterialColor(child, this.highlightColor);
});
this.pipe?.setNeedUpdateCenter(false);
this.pipe?.setSelectedIDs(intersectedObject.userData.selectedID);
this.pipe?.setSelectedIDs(parentGroup?.userData.selectedID);
} else {
this.pipe?.setSelectedIDs(undefined);
}
this.render();
});
}

public setHighlightColor(selectedId: string) {
this.scene.children.forEach((child) => {
if (!(child instanceof THREE.Group)) return;
const color = child.name === `box${selectedId}` ? this.highlightColor : child.userData.defaultColor;
child.children.forEach((grandson) => {
this.updateMaterialColor(grandson, color);
});
});
this.render();
}

private updateMaterialColor(child: THREE.Object3D<THREE.Event>, color: THREE.ColorRepresentation) {
if (child instanceof THREE.BoxHelper) {
(child.material as THREE.LineBasicMaterial).color.set(color);
} else if (child instanceof THREE.Sprite) {
(child.material as THREE.SpriteMaterial).color.set(color);
}
}

public setHandlerPipe(pipe: IPipeTypes) {
Expand Down Expand Up @@ -598,13 +626,13 @@ export class PointCloud extends EventListener {
const arrow = this.generateBoxArrow(boxParams);

// Temporarily hide
const boxID = this.generateBoxTrackID(boxParams);
const boxID = this.generateBoxTrackID(boxParams, color);
if (boxID) {
group.add(boxID);
}

// 生成并添加attribute标签
const attributeLabel = this.generateBoxAttributeLabel(boxParams);
const attributeLabel = this.generateBoxAttributeLabel(boxParams, color);
if (attributeLabel) {
group.add(attributeLabel);
}
Expand All @@ -621,9 +649,8 @@ export class PointCloud extends EventListener {
const clickGeometry = new THREE.BoxGeometry(width, height, depth);
const clickMaterial = new THREE.MeshBasicMaterial({ visible: false });
const clickMesh = new THREE.Mesh(clickGeometry, clickMaterial);
clickMesh.userData = { selectedID: id };
group.add(clickMesh);

group.userData = { defaultColor: color, selectedID: id };
this.scene.add(group);
};

Expand Down Expand Up @@ -1397,8 +1424,7 @@ export class PointCloud extends EventListener {
const dir = new THREE.Vector3(1, 0, 0);
const origin = new THREE.Vector3(width / 2, 0, 0);
const arrowLen = 2;
const hex = 0xffff00;
const arrowHelper = new THREE.ArrowHelper(dir, origin, arrowLen, hex);
const arrowHelper = new THREE.ArrowHelper(dir, origin, arrowLen, this.highlightColor);
arrowHelper.visible = this.showDirection;

return arrowHelper;
Expand All @@ -1410,7 +1436,7 @@ export class PointCloud extends EventListener {
* @param scaleFactor scale size
* @returns { sprite, canvasWidth, canvasHeight }
*/
public generateLabel = (text: string, scaleFactor: number) => {
public generateLabel = (text: string, scaleFactor: number, color: THREE.ColorRepresentation) => {
const canvas = this.getTextCanvas(text);
const texture = new THREE.Texture(canvas);

Expand All @@ -1423,7 +1449,7 @@ export class PointCloud extends EventListener {
const canvasWidth = canvas.width / window.devicePixelRatio;
const canvasHeight = canvas.height / window.devicePixelRatio;

const spriteMaterial = new THREE.SpriteMaterial({ map: texture, depthWrite: false });
const spriteMaterial = new THREE.SpriteMaterial({ map: texture, depthWrite: false, color });
const sprite = new THREE.Sprite(spriteMaterial);
sprite.scale.set(canvasWidth / scaleFactor, canvasHeight / scaleFactor, 1);

Expand All @@ -1435,10 +1461,10 @@ export class PointCloud extends EventListener {
* @param boxParams
* @returns sprite
*/
public generateBoxTrackID = (boxParams: IPointCloudBox) => {
public generateBoxTrackID = (boxParams: IPointCloudBox, color: THREE.ColorRepresentation) => {
if (!boxParams.trackID) return;

const { sprite } = this.generateLabel(boxParams.trackID.toString(), 50);
const { sprite } = this.generateLabel(boxParams.trackID.toString(), 50, color);

sprite.position.set(-boxParams.width / 2, 0, boxParams.depth / 2 + 0.5);

Expand All @@ -1450,13 +1476,13 @@ export class PointCloud extends EventListener {
* @param boxParams
* @returns sprite
*/
public generateBoxAttributeLabel = (boxParams: IPointCloudBox) => {
public generateBoxAttributeLabel = (boxParams: IPointCloudBox, color: THREE.ColorRepresentation) => {
if (!boxParams.attribute || this.hiddenText) return;

const classLabel = this.findSubAttributeLabel(boxParams, this.config);
const subAttributeLabel = classLabel ? `${boxParams.attribute}\n${classLabel}` : `${boxParams.attribute}`;

const { sprite, canvasHeight } = this.generateLabel(subAttributeLabel, 100);
const { sprite, canvasHeight } = this.generateLabel(subAttributeLabel, 100, color);

sprite.position.set(-boxParams.width / 2, 0, -boxParams.depth / 2 - canvasHeight / 150);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const PointCloudAnnotationView = (props: IProps) => {

// Add Init Box
boxParamsList.forEach((v: IPointCloudBox) => {
instance.current?.generateBox(v, v.id);
instance.current?.generateBox(v);
});
}
return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ const PointCloud3D: React.FC<IA2MapStateProps> = ({ currentData, config, highlig
*/
const zoom = ptCtx.topViewInstance?.pointCloudInstance?.camera.zoom ?? 1;
ptCtx.mainViewInstance?.updateCameraZoom(zoom);
ptCtx.mainViewInstance?.setHighlightColor(selectedId);
}
}, [selectedBox?.info?.id]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ export const synchronizeTopView = (
}

// Control the 3D view data to create box
mainViewInstance.generateBox(newBoxParams, newPolygon.id);
mainViewInstance.generateBox(newBoxParams);
mainViewInstance.render();

const { pointCloud2dOperation, pointCloudInstance } = topViewInstance;
Expand Down

0 comments on commit 50ba83e

Please sign in to comment.