Skip to content

Commit

Permalink
Refactor bounding box logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arjxn-py committed Oct 30, 2024
1 parent 2b34ca6 commit a3a524c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
15 changes: 15 additions & 0 deletions packages/base/src/3dview/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ export function buildShape(options: {
edgeIdx++;
}

const bbox = new THREE.Box3().setFromObject(mainMesh);
const size = new THREE.Vector3();
bbox.getSize(size);
const center = new THREE.Vector3();
bbox.getCenter(center);

const boundingBox = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(size.x, size.y, size.z)),
new THREE.LineBasicMaterial({ color: BOUNDING_BOX_COLOR, depthTest: false })
);
boundingBox.position.copy(center);
boundingBox.visible = false;
boundingBox.name = SELECTION_BOUNDING_BOX;
meshGroup.add(boundingBox);

meshGroup.add(mainMesh);

return { meshGroup, mainMesh, edgesMeshes };
Expand Down
46 changes: 13 additions & 33 deletions packages/base/src/3dview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,10 @@ export class MainView extends React.Component<IProps, IStates> {
selectedMesh.material.color = originalColor;
}

const groupBoundingBox = this._meshGroup?.getObjectByName(
SELECTION_BOUNDING_BOX
);
if (groupBoundingBox) {
this._meshGroup?.remove(groupBoundingBox);
const parentGroup = this._meshGroup?.getObjectByName(selectedMesh.name)?.parent
const boundingBox = parentGroup?.getObjectByName(SELECTION_BOUNDING_BOX) as THREE.Mesh;
if (boundingBox) {
boundingBox.visible = false;
}

const material = selectedMesh.material as THREE.Material & {
Expand Down Expand Up @@ -1074,40 +1073,21 @@ export class MainView extends React.Component<IProps, IStates> {
if (selectedMesh?.material?.color) {
selectedMesh.material.color = BOUNDING_BOX_COLOR;
}

const material = selectedMesh.material as THREE.Material & {
linewidth?: number;
};

const material = selectedMesh.material as THREE.Material & { linewidth?: number };
if (material?.linewidth) {
material.linewidth = SELECTED_LINEWIDTH;
}
} else {
// Highlight non-edges using a bounding box
this._selectedMeshes.push(selectedMesh);

// Create and add bounding box
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.LineBasicMaterial({
color: BOUNDING_BOX_COLOR,
depthTest: false
});
const boundingBox = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
material
);
boundingBox.name = SELECTION_BOUNDING_BOX;

// Set the bounding box size and position
const bbox = new THREE.Box3().setFromObject(selectedMesh);
const size = new THREE.Vector3();
bbox.getSize(size);
boundingBox.scale.copy(size);

const center = new THREE.Vector3();
bbox.getCenter(center);
boundingBox.position.copy(center);

this._meshGroup?.add(boundingBox);

const parentGroup = this._meshGroup?.getObjectByName(selectedMesh.name)?.parent
const boundingBox = parentGroup?.getObjectByName(SELECTION_BOUNDING_BOX) as THREE.Mesh;

if (boundingBox) {
boundingBox.visible = true;
}
}
}
}
Expand Down

0 comments on commit a3a524c

Please sign in to comment.