refactor: Remove unnecessary console logs and improve connection limit checks in simulation components

This commit is contained in:
Jerald-Golden-B 2025-04-16 16:33:48 +05:30
parent 0eedbdd58e
commit ee319c28e4
6 changed files with 219 additions and 39 deletions

View File

@ -150,7 +150,6 @@ const ArmBotMechanics: React.FC = () => {
modeluuid: updatedPath.modeluuid,
eventData: { type: "ArmBot", points: updatedPath.points }
}
console.log('data: ', data);
socket.emit('v2:model-asset:updateEventData', data);
}

View File

@ -46,7 +46,6 @@ export const ArmbotInstances: React.FC<ArmbotInstancesProps> = ({ index, armBot,
const [processes, setProcesses] = useState<Process[]>([]);
useEffect(() => {
if (armBot.actions.processes.length > 0) {
const mappedProcesses = armBot.actions.processes.map((process) => {
return {

View File

@ -1,7 +1,7 @@
import { useEffect, useMemo, useState, useRef } from "react";
import { useFrame } from "@react-three/fiber";
import * as THREE from "three";
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
import { usePlayButtonStore, useResetButtonStore } from "../../../store/usePlayButtonStore";
import { useSimulationStates } from "../../../store/store";
import MaterialInstances from "./MaterialInstances";
@ -70,6 +70,7 @@ const IKAnimationController = ({
const { isPlaying } = usePlayButtonStore();;
const statusRef = useRef("idle");
const { simulationStates } = useSimulationStates();
const { isReset } = useResetButtonStore();
const initialCurveRef = useRef<THREE.CatmullRomCurve3 | null>(null);
const initialStartPositionRef = useRef<THREE.Vector3 | null>(null);
@ -78,6 +79,13 @@ const IKAnimationController = ({
setProgress(0);
}, [selectedTrigger]);
useEffect(() => {
setProgress(0);
setNeedsInitialMovement(true);
setInitialProgress(0);
setIsInitializing(true);
}, [isReset]);
useEffect(() => {
if (ikSolver) {
const targetBone = ikSolver.mesh.skeleton.bones.find(
@ -229,6 +237,9 @@ const IKAnimationController = ({
currentStatus = "processing";
const segmentProgress = (newProgress - restToStartEnd) / (processEnd - restToStartEnd);
currentPosition = processCurve.getPoint(segmentProgress);
if (statusRef.current !== "processing") {
updateConveyorOrStaticMachineStatusOnStart(selectedTrigger);
}
} else {
// Returning to rest position
currentStatus = "returning to rest";
@ -245,7 +256,7 @@ const IKAnimationController = ({
// Only trigger when the entire animation is complete (newProgress === 1)
if (newProgress === 1 && currentStatus === "returning to rest") {
updateConveyorOrStaticMachineStatus(selectedTrigger);
updateConveyorOrStaticMachineStatusOnEnd(selectedTrigger);
}
bone.position.copy(currentPosition);
@ -254,7 +265,45 @@ const IKAnimationController = ({
});
});
const updateConveyorOrStaticMachineStatus = (selectedTrigger: string) => {
const updateConveyorOrStaticMachineStatusOnStart = (selectedTrigger: string) => {
const currentProcess = processes.find(p => p.triggerId === selectedTrigger);
if (currentProcess) {
const triggerId = currentProcess.triggerId;
const startPoint = armBot.actions.processes.find((process) => process.triggerId === triggerId)?.startPoint;
const matchedMachine = simulationStates.find((state) => {
if (state.type === "Conveyor") {
return (state).points.some(
(point) => point.uuid === startPoint
);
} else if (state.type === "StaticMachine") {
return state.points.uuid === startPoint;
}
return false;
});
if (matchedMachine) {
if (matchedMachine.type === "Conveyor") {
logStatus(`[Arm ${uuid}] start point which is a conveyor (${matchedMachine.modelName})`);
} else {
logStatus(`[Arm ${uuid}] started form start point which is a static machine (${matchedMachine.modelName})`);
}
setTimeout(() => {
if (matchedMachine.type === "StaticMachine") {
updateArmBotStatus('dropping');
}
if (matchedMachine.type === "Conveyor") {
updateArmBotStatus('picking');
}
}, 0);
}
}
}
const updateConveyorOrStaticMachineStatusOnEnd = (selectedTrigger: string) => {
const currentProcess = processes.find(p => p.triggerId === selectedTrigger);
if (currentProcess) {
const triggerId = currentProcess.triggerId;
@ -314,7 +363,13 @@ const IKAnimationController = ({
}
}
return null;
return (
<MaterialInstances
statusRef={statusRef}
ikSolver={ikSolver}
targetBoneName={targetBoneName}
/>
);
};
export default IKAnimationController;

View File

@ -3,37 +3,26 @@ import * as THREE from 'three';
import { Box } from '@react-three/drei';
type MaterialInstancesProps = {
groupRef: React.RefObject<THREE.Group>;
activeCurve: any;
progress: number;
statusRef: React.RefObject<string>;
ikSolver: any;
targetBoneName: string;
};
function MaterialInstances({
groupRef,
activeCurve,
progress,
statusRef,
ikSolver,
targetBoneName
}: MaterialInstancesProps) {
const { endToRestRange, startToEndRange } = activeCurve;
if (!ikSolver) return null;
// Show the box from when we reach the start point until we leave the end point
const shouldShow = (progress >= startToEndRange[0] && progress < startToEndRange[1] && progress >= endToRestRange[0]);
if (!shouldShow || !ikSolver) return null;
const targetBone = ikSolver.mesh.skeleton.bones.find(
(b: any) => b.name === targetBoneName
);
const targetBone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === targetBoneName);
if (!targetBone) return null;
const worldPos = new THREE.Vector3();
targetBone.getWorldPosition(worldPos);
return (
<Box args={[0.5, 0.5, 0.5]} position={worldPos}>
<Box args={[0.5, 0.5, 0.5]} position={worldPos} visible={statusRef.current === 'processing'}>
<meshStandardMaterial color="orange" />
</Box>
);

View File

@ -40,6 +40,37 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
};
const existingTargets = point.connections.targets || [];
// Check connection limits
const toPath = simulationStates.find(p => p.modeluuid === toModelUUID);
if (toPath) {
// Check if we already have this type of connection
const hasConveyor = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "Conveyor";
});
const hasArmBot = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "ArmBot";
});
const hasVehicle = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "Vehicle";
});
if (toPath.type === "Conveyor" && hasConveyor) {
console.log("Conveyor can only connect to one other conveyor");
return point;
}
if (toPath.type === "ArmBot" && hasArmBot) {
console.log("Conveyor can only connect to one ArmBot");
return point;
}
if (toPath.type === "Vehicle" && hasVehicle) {
console.log("Conveyor can only connect to one Vehicle");
return point;
}
}
if (!existingTargets.some((target) => target.modelUUID === newTarget.modelUUID && target.pointUUID === newTarget.pointUUID)) {
return {
...point,
@ -66,6 +97,36 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
};
const existingTargets = point.connections.targets || [];
// Check connection limits
const fromPath = simulationStates.find(p => p.modeluuid === fromModelUUID);
if (fromPath) {
const hasConveyor = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "Conveyor";
});
const hasArmBot = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "ArmBot";
});
const hasVehicle = existingTargets.some(t => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "Vehicle";
});
if (fromPath.type === "Conveyor" && hasConveyor) {
console.log("Conveyor can only connect to one other conveyor");
return point;
}
if (fromPath.type === "ArmBot" && hasArmBot) {
console.log("Conveyor can only connect to one ArmBot");
return point;
}
if (fromPath.type === "Vehicle" && hasVehicle) {
console.log("Conveyor can only connect to one Vehicle");
return point;
}
}
if (!existingTargets.some((target) => target.modelUUID === reverseTarget.modelUUID && target.pointUUID === reverseTarget.pointUUID)) {
return {
...point,
@ -494,21 +555,59 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
}
}
// For non-Vehicle paths, check if already connected
if (intersected.userData.path.type !== "Vehicle") {
const isAlreadyConnected = simulationStates.some((path) => {
if (path.type === "Conveyor") {
return path.points.some(
(point) =>
point.uuid === sphereUUID &&
point.connections.targets.length > 0
);
// For Conveyors, check connection limits in BOTH DIRECTIONS
if (firstSelected && (firstPath?.type === "Conveyor" || secondPath?.type === "Conveyor")) {
const checkConveyorLimits = (path: any, pointUUID: string) => {
if (path?.type === "Conveyor") {
const point = path.points.find((p: { uuid: string }) => p.uuid === pointUUID);
if (point) {
return {
hasConveyor: point.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID);
return targetPath?.type === "Conveyor";
}),
hasArmBot: point.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID);
return targetPath?.type === "ArmBot";
}),
hasVehicle: point.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === "Vehicle";
})
};
}
}
return false;
});
return { hasConveyor: false, hasArmBot: false, hasVehicle: false };
};
if (isAlreadyConnected) {
console.log("Conveyor point is already connected. Ignoring.");
const firstConveyorLimits = checkConveyorLimits(firstPath, firstSelected?.sphereUUID);
const secondConveyorLimits = checkConveyorLimits(secondPath, sphereUUID);
// Check if trying to connect two conveyors
if (firstPath?.type === "Conveyor" && secondPath?.type === "Conveyor") {
if (firstConveyorLimits.hasConveyor || secondConveyorLimits.hasConveyor) {
console.log("Conveyor can only connect to one other conveyor");
return;
}
}
// Check if trying to connect to an ArmBot when already connected to one
if (secondPath?.type === "ArmBot" && firstConveyorLimits.hasArmBot) {
console.log("Conveyor can only connect to one ArmBot");
return;
}
if (firstPath?.type === "ArmBot" && secondConveyorLimits.hasArmBot) {
console.log("Conveyor can only connect to one ArmBot");
return;
}
// Check if trying to connect to a Vehicle when already connected to one
if (secondPath?.type === "Vehicle" && firstConveyorLimits.hasVehicle) {
console.log("Conveyor can only connect to one Vehicle");
return;
}
if (firstPath?.type === "Vehicle" && secondConveyorLimits.hasVehicle) {
console.log("Conveyor can only connect to one Vehicle");
return;
}
}
@ -762,6 +861,45 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
!(firstPath?.type === 'Conveyor' || firstPath?.type === 'StaticMachine' ||
secondPath?.type === 'Conveyor' || secondPath?.type === 'StaticMachine');
// NEW: Check conveyor connection limits
let isConveyorAtMaxConnections = false;
if (firstPath?.type === 'Conveyor' || secondPath?.type === 'Conveyor') {
const conveyorPath = firstPath?.type === 'Conveyor' ? firstPath : secondPath;
const otherPath = firstPath?.type === 'Conveyor' ? secondPath : firstPath;
if (conveyorPath) {
const conveyorPoint = Array.isArray(conveyorPath.points)
? conveyorPath.points.find((p: { uuid: string }) => p.uuid ===
(firstPath?.type === 'Conveyor' ? firstSelected.sphereUUID : sphereUUID))
: undefined;
if (conveyorPoint) {
const hasConveyor = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID);
return targetPath?.type === 'Conveyor';
});
const hasArmBot = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find((p: { modeluuid: string }) => p.modeluuid === t.modelUUID);
return targetPath?.type === 'ArmBot';
});
const hasVehicle = conveyorPoint.connections.targets.some((t: { modelUUID: string }) => {
const targetPath = simulationStates.find(p => p.modeluuid === t.modelUUID);
return targetPath?.type === 'Vehicle';
});
if (otherPath?.type === 'Conveyor' && hasConveyor) {
isConveyorAtMaxConnections = true;
}
if (otherPath?.type === 'ArmBot' && hasArmBot) {
isConveyorAtMaxConnections = true;
}
if (otherPath?.type === 'Vehicle' && hasVehicle) {
isConveyorAtMaxConnections = true;
}
}
}
}
if (
!isDuplicateConnection &&
!isVehicleToVehicle &&
@ -773,6 +911,7 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
!isArmBotToArmBot &&
!isArmBotToInvalidType &&
!isArmBotAlreadyConnectedToStatic &&
!isConveyorAtMaxConnections && // NEW: Check conveyor limits
firstSelected.sphereUUID !== sphereUUID &&
firstSelected.modelUUID !== modelUUID &&
(firstSelected.isCorner || isConnectable) &&
@ -957,7 +1096,6 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
state.modeluuid === connection2.model
);
console.log("updatedPaths: ", updatedPaths);
updateBackend(updatedPaths);
setSimulationStates(updatedStates);

View File

@ -564,9 +564,9 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
// Log if no animation is occurring when it should
if (!animationOccurring && !isConnected) {
console.log(
`Warning: No animation occurring for process ${process.id} despite not being connected`
);
// console.log(
// `Warning: No animation occurring for process ${process.id} despite not being connected`
// );
}
newStates[process.id] = {