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, modeluuid: updatedPath.modeluuid,
eventData: { type: "ArmBot", points: updatedPath.points } eventData: { type: "ArmBot", points: updatedPath.points }
} }
console.log('data: ', data);
socket.emit('v2:model-asset:updateEventData', 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[]>([]); const [processes, setProcesses] = useState<Process[]>([]);
useEffect(() => { useEffect(() => {
if (armBot.actions.processes.length > 0) { if (armBot.actions.processes.length > 0) {
const mappedProcesses = armBot.actions.processes.map((process) => { const mappedProcesses = armBot.actions.processes.map((process) => {
return { return {

View File

@ -1,7 +1,7 @@
import { useEffect, useMemo, useState, useRef } from "react"; import { useEffect, useMemo, useState, useRef } from "react";
import { useFrame } from "@react-three/fiber"; import { useFrame } from "@react-three/fiber";
import * as THREE from "three"; import * as THREE from "three";
import { usePlayButtonStore } from "../../../store/usePlayButtonStore"; import { usePlayButtonStore, useResetButtonStore } from "../../../store/usePlayButtonStore";
import { useSimulationStates } from "../../../store/store"; import { useSimulationStates } from "../../../store/store";
import MaterialInstances from "./MaterialInstances"; import MaterialInstances from "./MaterialInstances";
@ -70,6 +70,7 @@ const IKAnimationController = ({
const { isPlaying } = usePlayButtonStore();; const { isPlaying } = usePlayButtonStore();;
const statusRef = useRef("idle"); const statusRef = useRef("idle");
const { simulationStates } = useSimulationStates(); const { simulationStates } = useSimulationStates();
const { isReset } = useResetButtonStore();
const initialCurveRef = useRef<THREE.CatmullRomCurve3 | null>(null); const initialCurveRef = useRef<THREE.CatmullRomCurve3 | null>(null);
const initialStartPositionRef = useRef<THREE.Vector3 | null>(null); const initialStartPositionRef = useRef<THREE.Vector3 | null>(null);
@ -78,6 +79,13 @@ const IKAnimationController = ({
setProgress(0); setProgress(0);
}, [selectedTrigger]); }, [selectedTrigger]);
useEffect(() => {
setProgress(0);
setNeedsInitialMovement(true);
setInitialProgress(0);
setIsInitializing(true);
}, [isReset]);
useEffect(() => { useEffect(() => {
if (ikSolver) { if (ikSolver) {
const targetBone = ikSolver.mesh.skeleton.bones.find( const targetBone = ikSolver.mesh.skeleton.bones.find(
@ -229,6 +237,9 @@ const IKAnimationController = ({
currentStatus = "processing"; currentStatus = "processing";
const segmentProgress = (newProgress - restToStartEnd) / (processEnd - restToStartEnd); const segmentProgress = (newProgress - restToStartEnd) / (processEnd - restToStartEnd);
currentPosition = processCurve.getPoint(segmentProgress); currentPosition = processCurve.getPoint(segmentProgress);
if (statusRef.current !== "processing") {
updateConveyorOrStaticMachineStatusOnStart(selectedTrigger);
}
} else { } else {
// Returning to rest position // Returning to rest position
currentStatus = "returning to rest"; currentStatus = "returning to rest";
@ -245,7 +256,7 @@ const IKAnimationController = ({
// Only trigger when the entire animation is complete (newProgress === 1) // Only trigger when the entire animation is complete (newProgress === 1)
if (newProgress === 1 && currentStatus === "returning to rest") { if (newProgress === 1 && currentStatus === "returning to rest") {
updateConveyorOrStaticMachineStatus(selectedTrigger); updateConveyorOrStaticMachineStatusOnEnd(selectedTrigger);
} }
bone.position.copy(currentPosition); 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); const currentProcess = processes.find(p => p.triggerId === selectedTrigger);
if (currentProcess) { if (currentProcess) {
const triggerId = currentProcess.triggerId; const triggerId = currentProcess.triggerId;
@ -314,7 +363,13 @@ const IKAnimationController = ({
} }
} }
return null; return (
<MaterialInstances
statusRef={statusRef}
ikSolver={ikSolver}
targetBoneName={targetBoneName}
/>
);
}; };
export default IKAnimationController; export default IKAnimationController;

View File

@ -3,37 +3,26 @@ import * as THREE from 'three';
import { Box } from '@react-three/drei'; import { Box } from '@react-three/drei';
type MaterialInstancesProps = { type MaterialInstancesProps = {
groupRef: React.RefObject<THREE.Group>; statusRef: React.RefObject<string>;
activeCurve: any;
progress: number;
ikSolver: any; ikSolver: any;
targetBoneName: string; targetBoneName: string;
}; };
function MaterialInstances({ function MaterialInstances({
groupRef, statusRef,
activeCurve,
progress,
ikSolver, ikSolver,
targetBoneName targetBoneName
}: MaterialInstancesProps) { }: 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 targetBone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === targetBoneName);
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
);
if (!targetBone) return null; if (!targetBone) return null;
const worldPos = new THREE.Vector3(); const worldPos = new THREE.Vector3();
targetBone.getWorldPosition(worldPos); targetBone.getWorldPosition(worldPos);
return ( 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" /> <meshStandardMaterial color="orange" />
</Box> </Box>
); );

View File

@ -40,6 +40,37 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
}; };
const existingTargets = point.connections.targets || []; 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)) { if (!existingTargets.some((target) => target.modelUUID === newTarget.modelUUID && target.pointUUID === newTarget.pointUUID)) {
return { return {
...point, ...point,
@ -66,6 +97,36 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
}; };
const existingTargets = point.connections.targets || []; 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)) { if (!existingTargets.some((target) => target.modelUUID === reverseTarget.modelUUID && target.pointUUID === reverseTarget.pointUUID)) {
return { return {
...point, ...point,
@ -494,21 +555,59 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
} }
} }
// For non-Vehicle paths, check if already connected // For Conveyors, check connection limits in BOTH DIRECTIONS
if (intersected.userData.path.type !== "Vehicle") { if (firstSelected && (firstPath?.type === "Conveyor" || secondPath?.type === "Conveyor")) {
const isAlreadyConnected = simulationStates.some((path) => { const checkConveyorLimits = (path: any, pointUUID: string) => {
if (path.type === "Conveyor") { if (path?.type === "Conveyor") {
return path.points.some( const point = path.points.find((p: { uuid: string }) => p.uuid === pointUUID);
(point) => if (point) {
point.uuid === sphereUUID && return {
point.connections.targets.length > 0 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) { const firstConveyorLimits = checkConveyorLimits(firstPath, firstSelected?.sphereUUID);
console.log("Conveyor point is already connected. Ignoring."); 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; return;
} }
} }
@ -762,6 +861,45 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
!(firstPath?.type === 'Conveyor' || firstPath?.type === 'StaticMachine' || !(firstPath?.type === 'Conveyor' || firstPath?.type === 'StaticMachine' ||
secondPath?.type === 'Conveyor' || secondPath?.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 ( if (
!isDuplicateConnection && !isDuplicateConnection &&
!isVehicleToVehicle && !isVehicleToVehicle &&
@ -773,6 +911,7 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
!isArmBotToArmBot && !isArmBotToArmBot &&
!isArmBotToInvalidType && !isArmBotToInvalidType &&
!isArmBotAlreadyConnectedToStatic && !isArmBotAlreadyConnectedToStatic &&
!isConveyorAtMaxConnections && // NEW: Check conveyor limits
firstSelected.sphereUUID !== sphereUUID && firstSelected.sphereUUID !== sphereUUID &&
firstSelected.modelUUID !== modelUUID && firstSelected.modelUUID !== modelUUID &&
(firstSelected.isCorner || isConnectable) && (firstSelected.isCorner || isConnectable) &&
@ -957,7 +1096,6 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
state.modeluuid === connection2.model state.modeluuid === connection2.model
); );
console.log("updatedPaths: ", updatedPaths);
updateBackend(updatedPaths); updateBackend(updatedPaths);
setSimulationStates(updatedStates); setSimulationStates(updatedStates);

View File

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