diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/components/ActionsList.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/components/ActionsList.tsx index 3ea9d46..632805b 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/components/ActionsList.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/components/ActionsList.tsx @@ -11,7 +11,7 @@ import { useSelectedProduct, } from "../../../../../../store/simulation/useSimulationStore"; import { useProductStore } from "../../../../../../store/simulation/useProductStore"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; interface ActionsListProps { selectedPointData: any; diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/conveyorMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/conveyorMechanics.tsx index 027dc0b..18c01bc 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/conveyorMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/conveyorMechanics.tsx @@ -11,7 +11,7 @@ import Trigger from "../trigger/Trigger"; import { useSelectedAction, useSelectedEventData, useSelectedProduct } from "../../../../../../store/simulation/useSimulationStore"; import { useProductStore } from "../../../../../../store/simulation/useProductStore"; import ActionsList from "../components/ActionsList"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; function ConveyorMechanics() { const [activeOption, setActiveOption] = useState<"default" | "spawn" | "swap" | "delay" | "despawn">("default"); diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/machineMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/machineMechanics.tsx index 8437612..8247d54 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/machineMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/machineMechanics.tsx @@ -6,7 +6,7 @@ import { useSelectedAction, useSelectedEventData, useSelectedProduct } from "../ import { useProductStore } from "../../../../../../store/simulation/useProductStore"; import ProcessAction from "../actions/ProcessAction"; import ActionsList from "../components/ActionsList"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; function MachineMechanics() { const [activeOption, setActiveOption] = useState<"default" | "process">("default"); diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/roboticArmMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/roboticArmMechanics.tsx index 2295161..a5eecac 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/roboticArmMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/roboticArmMechanics.tsx @@ -12,7 +12,7 @@ import { import { useProductStore } from "../../../../../../store/simulation/useProductStore"; import PickAndPlaceAction from "../actions/PickAndPlaceAction"; import ActionsList from "../components/ActionsList"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; function RoboticArmMechanics() { const [activeOption, setActiveOption] = useState<"default" | "pickAndPlace">( diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx index 564a53f..202daa7 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/storageMechanics.tsx @@ -4,7 +4,7 @@ import LabledDropdown from "../../../../../ui/inputs/LabledDropdown"; import Trigger from "../trigger/Trigger"; import StorageAction from "../actions/StorageAction"; import ActionsList from "../components/ActionsList"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; import { useProductStore } from "../../../../../../store/simulation/useProductStore"; import { useStorageUnitStore } from "../../../../../../store/simulation/useStorageUnitStore"; import { useSelectedAction, useSelectedEventData, useSelectedProduct } from "../../../../../../store/simulation/useSimulationStore"; diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/vehicleMechanics.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/vehicleMechanics.tsx index baa7541..2894e33 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/vehicleMechanics.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/mechanics/vehicleMechanics.tsx @@ -12,7 +12,7 @@ import { import { useProductStore } from "../../../../../../store/simulation/useProductStore"; import TravelAction from "../actions/TravelAction"; import ActionsList from "../components/ActionsList"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; import { useVehicleStore } from "../../../../../../store/simulation/useVehicleStore"; function VehicleMechanics() { diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/trigger/Trigger.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/trigger/Trigger.tsx index b81dbf6..fb0b97a 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/trigger/Trigger.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/trigger/Trigger.tsx @@ -13,7 +13,7 @@ import { useSelectedAction, useSelectedProduct, } from "../../../../../../store/simulation/useSimulationStore"; -import { upsertProductOrEventApi } from "../../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi"; type TriggerProps = { selectedPointData?: PointsScheme | undefined; diff --git a/app/src/components/layout/sidebarRight/simulation/Simulations.tsx b/app/src/components/layout/sidebarRight/simulation/Simulations.tsx index f64a419..b215728 100644 --- a/app/src/components/layout/sidebarRight/simulation/Simulations.tsx +++ b/app/src/components/layout/sidebarRight/simulation/Simulations.tsx @@ -17,10 +17,10 @@ import RenderOverlay from "../../../templates/Overlay"; import EditWidgetOption from "../../../ui/menu/EditWidgetOption"; import { handleAddEventToProduct } from "../../../../modules/simulation/events/points/functions/handleAddEventToProduct"; import { useEventsStore } from "../../../../store/simulation/useEventsStore"; -import { deleteEventDataApi } from "../../../../services/simulation/deleteEventDataApi"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; -import { deleteProductApi } from "../../../../services/simulation/deleteProductApi"; -import { renameProductApi } from "../../../../services/simulation/renameProductApi"; +import { deleteEventDataApi } from "../../../../services/simulation/products/deleteEventDataApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; +import { deleteProductApi } from "../../../../services/simulation/products/deleteProductApi"; +import { renameProductApi } from "../../../../services/simulation/products/renameProductApi"; import { determineExecutionMachineSequences } from "../../../../modules/simulation/simulator/functions/determineExecutionMachineSequences"; import ComparePopUp from "../../../ui/compareVersion/Compare"; import { diff --git a/app/src/modules/scene/controls/selectionControls/moveControls.tsx b/app/src/modules/scene/controls/selectionControls/moveControls.tsx index f5e70f9..a41755c 100644 --- a/app/src/modules/scene/controls/selectionControls/moveControls.tsx +++ b/app/src/modules/scene/controls/selectionControls/moveControls.tsx @@ -13,7 +13,7 @@ import { detectModifierKeys } from "../../../../utils/shortcutkeys/detectModifie import { useEventsStore } from "../../../../store/simulation/useEventsStore"; import { useProductStore } from "../../../../store/simulation/useProductStore"; import { useSelectedProduct } from "../../../../store/simulation/useSimulationStore"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { snapControls } from "../../../../utils/handleSnap"; import DistanceFindingControls from "./distanceFindingControls"; diff --git a/app/src/modules/scene/controls/selectionControls/rotateControls.tsx b/app/src/modules/scene/controls/selectionControls/rotateControls.tsx index 03b262c..65536d0 100644 --- a/app/src/modules/scene/controls/selectionControls/rotateControls.tsx +++ b/app/src/modules/scene/controls/selectionControls/rotateControls.tsx @@ -7,7 +7,7 @@ import * as Types from "../../../../types/world/worldTypes"; import { useEventsStore } from "../../../../store/simulation/useEventsStore"; import { useProductStore } from "../../../../store/simulation/useProductStore"; import { useSelectedProduct } from "../../../../store/simulation/useSimulationStore"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMovedObjects, itemsGroupRef, copiedObjects, setCopiedObjects, pastedObjects, setpastedObjects, duplicatedObjects, setDuplicatedObjects, selectionGroup, boundingBoxRef }: any) { const { camera, controls, gl, scene, pointer, raycaster } = useThree(); diff --git a/app/src/modules/scene/controls/transformControls/transformControls.tsx b/app/src/modules/scene/controls/transformControls/transformControls.tsx index 1108cf2..d1167f4 100644 --- a/app/src/modules/scene/controls/transformControls/transformControls.tsx +++ b/app/src/modules/scene/controls/transformControls/transformControls.tsx @@ -9,7 +9,7 @@ import { detectModifierKeys } from "../../../../utils/shortcutkeys/detectModifie import { useEventsStore } from "../../../../store/simulation/useEventsStore"; import { useProductStore } from "../../../../store/simulation/useProductStore"; import { useSelectedProduct } from "../../../../store/simulation/useSimulationStore"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { setFloorItemApi } from "../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi"; export default function TransformControl() { diff --git a/app/src/modules/simulation/events/points/creator/pointsCreator.tsx b/app/src/modules/simulation/events/points/creator/pointsCreator.tsx index 00e8f32..c1b7d23 100644 --- a/app/src/modules/simulation/events/points/creator/pointsCreator.tsx +++ b/app/src/modules/simulation/events/points/creator/pointsCreator.tsx @@ -9,7 +9,7 @@ import { detectModifierKeys } from "../../../../../utils/shortcutkeys/detectModi import { useSelectedEventSphere, useSelectedEventData, } from "../../../../../store/simulation/useSimulationStore"; import { useThree } from "@react-three/fiber"; import { usePlayButtonStore } from "../../../../../store/usePlayButtonStore"; -import { upsertProductOrEventApi } from "../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../services/simulation/products/UpsertProductOrEventApi"; function PointsCreator() { const { gl, raycaster, scene, pointer, camera } = useThree(); diff --git a/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts b/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts index 9928578..86fd13f 100644 --- a/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts +++ b/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts @@ -1,4 +1,4 @@ -import { upsertProductOrEventApi } from "../../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../../services/simulation/products/UpsertProductOrEventApi"; interface HandleAddEventToProductParams { event: EventsSchema | undefined; diff --git a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx index 5ca87a5..2a420ae 100644 --- a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx +++ b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx @@ -8,7 +8,7 @@ import { useEventsStore } from "../../../../store/simulation/useEventsStore"; import { useSelectedProduct } from "../../../../store/simulation/useSimulationStore"; import { handleAddEventToProduct } from "../points/functions/handleAddEventToProduct"; import { QuadraticBezierLine } from "@react-three/drei"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { useDeleteTool } from "../../../../store/builder/store"; import { usePlayButtonStore } from "../../../../store/usePlayButtonStore"; import { ArrowOnQuadraticBezier, Arrows } from "../arrows/arrows"; diff --git a/app/src/modules/simulation/materials/collisionDetection/materialCollitionDetector.tsx b/app/src/modules/simulation/materials/collisionDetection/materialCollitionDetector.tsx new file mode 100644 index 0000000..68fbcc2 --- /dev/null +++ b/app/src/modules/simulation/materials/collisionDetection/materialCollitionDetector.tsx @@ -0,0 +1,119 @@ +import { useEffect, useRef } from 'react'; +import * as THREE from 'three'; +import { useMaterialStore } from '../../../../store/simulation/useMaterialStore'; +import { useThree, useFrame } from '@react-three/fiber'; +import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore'; +import { CameraControls } from '@react-three/drei'; + +const collisionWorker = new Worker(new URL('../../../../services/simulation/webWorkers/collisionWorker.ts', import.meta.url)); + +export default function MaterialCollisionDetector() { + const { materials } = useMaterialStore(); + const { scene, controls, camera } = useThree(); + const positionsRef = useRef>({}); + const sizesRef = useRef>({}); + const { isPlaying } = usePlayButtonStore(); + const { isPaused, setIsPaused } = usePauseButtonStore(); + const { isReset } = useResetButtonStore(); + const collisionStateRef = useRef>({}); + + useEffect(() => { + if (isReset) { + collisionStateRef.current = {}; + } + }, [isReset]) + + useEffect(() => { + collisionWorker.onmessage = (e) => { + const { collisions } = e.data; + handleCollisions(collisions); + }; + }, []); + + useFrame(() => { + if (materials.length < 2 || !isPlaying || isPaused) return; + + const { positions, sizes } = getCurrentMaterialData(); + positionsRef.current = positions; + sizesRef.current = sizes; + + collisionWorker.postMessage({ + materials: materials, + positions: positions, + sizes: sizes + }); + }); + + const getCurrentMaterialData = () => { + const positions: Record = {}; + const sizes: Record = {}; + + materials.forEach(material => { + if (material.isVisible) { + const obj = scene.getObjectByProperty('uuid', material.materialId); + if (obj) { + const position = new THREE.Vector3(); + obj.getWorldPosition(position); + positions[material.materialId] = position; + + const boundingBox = new THREE.Box3().setFromObject(obj); + const sizeVector = new THREE.Vector3(); + boundingBox.getSize(sizeVector); + sizes[material.materialId] = Math.max(sizeVector.x, sizeVector.y, sizeVector.z) / 2; + } + } + }); + + return { positions, sizes }; + }; + + const handleCollisions = (currentCollisions: Array<{ + materialId1: string; + materialId2: string; + distance: number; + }>) => { + const newCollisionState: Record = {}; + + currentCollisions.forEach(collision => { + const key = `${collision.materialId1}|-|${collision.materialId2}`; + newCollisionState[key] = true; + }); + + Object.keys(collisionStateRef.current).forEach(key => { + if (!newCollisionState[key]) { + const [id1, id2] = key.split('|-|'); + echo.error(`Collision ended between ${id1} and ${id2}`); + } + }); + + currentCollisions.forEach(collision => { + const key = `${collision.materialId1}|-|${collision.materialId2}`; + if (!collisionStateRef.current[key]) { + setIsPaused(true); + echo.error(`Collision started between ${collision.materialId1} and ${collision.materialId2}`); + const obj = scene.getObjectByProperty('uuid', collision.materialId1) + if (obj) { + const collisionPos = new THREE.Vector3(); + obj.getWorldPosition(collisionPos); + + const currentPos = new THREE.Vector3().copy(camera.position); + + const target = new THREE.Vector3(); + (controls as CameraControls).getTarget(target); + const direction = new THREE.Vector3().subVectors(target, currentPos).normalize(); + + const offsetDistance = 5; + const newCameraPos = new THREE.Vector3().copy(collisionPos).sub(direction.multiplyScalar(offsetDistance)); + + camera.position.copy(newCameraPos); + (controls as CameraControls).setLookAt(newCameraPos.x, newCameraPos.y, newCameraPos.z, collisionPos.x, collisionPos.y, collisionPos.z, true); + + } + } + }); + + collisionStateRef.current = newCollisionState; + }; + + return null; +} \ No newline at end of file diff --git a/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx b/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx index 74bd61f..fce425a 100644 --- a/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx +++ b/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx @@ -95,7 +95,7 @@ function MaterialInstance({ material }: { material: MaterialSchema }) { <> {material.isRendered && - + } = { type ModelType = keyof typeof modelPaths; interface ModelProps extends React.ComponentProps<'group'> { + materialId: string; materialType: ModelType; matRef: React.Ref> } -export function MaterialModel({ materialType, matRef, ...props }: Readonly) { +export function MaterialModel({ materialId, materialType, matRef, ...props }: Readonly) { const path = modelPaths[materialType] || modelPaths['Default material']; const gltf = useGLTF(path); const cloned = useMemo(() => gltf?.scene.clone(), [gltf]); @@ -29,7 +30,7 @@ export function MaterialModel({ materialType, matRef, ...props }: Readonly + + ) } diff --git a/app/src/modules/simulation/products/products.tsx b/app/src/modules/simulation/products/products.tsx index 4bd0bcb..f0fd1f4 100644 --- a/app/src/modules/simulation/products/products.tsx +++ b/app/src/modules/simulation/products/products.tsx @@ -3,8 +3,8 @@ import { useEffect } from 'react'; import { useProductStore } from '../../../store/simulation/useProductStore'; import { useSelectedProduct } from '../../../store/simulation/useSimulationStore'; import AddOrRemoveEventsInProducts from './events/addOrRemoveEventsInProducts'; -import { upsertProductOrEventApi } from '../../../services/simulation/UpsertProductOrEventApi'; -import { getAllProductsApi } from '../../../services/simulation/getallProductsApi'; +import { upsertProductOrEventApi } from '../../../services/simulation/products/UpsertProductOrEventApi'; +import { getAllProductsApi } from '../../../services/simulation/products/getallProductsApi'; import { useVehicleStore } from '../../../store/simulation/useVehicleStore'; import { useArmBotStore } from '../../../store/simulation/useArmBotStore'; import { useConveyorStore } from '../../../store/simulation/useConveyorStore'; diff --git a/app/src/modules/simulation/roboticArm/instances/animator/materialAnimator.tsx b/app/src/modules/simulation/roboticArm/instances/animator/materialAnimator.tsx index 83745aa..4f8e2f1 100644 --- a/app/src/modules/simulation/roboticArm/instances/animator/materialAnimator.tsx +++ b/app/src/modules/simulation/roboticArm/instances/animator/materialAnimator.tsx @@ -57,6 +57,7 @@ export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Rea <> {isRendered && ( diff --git a/app/src/modules/simulation/spatialUI/arm/armBotUI.tsx b/app/src/modules/simulation/spatialUI/arm/armBotUI.tsx index 3bc2a92..0775157 100644 --- a/app/src/modules/simulation/spatialUI/arm/armBotUI.tsx +++ b/app/src/modules/simulation/spatialUI/arm/armBotUI.tsx @@ -10,7 +10,7 @@ import * as THREE from 'three'; import armPick from "../../../../assets/gltf-glb/ui/arm_ui_pick.glb"; import armDrop from "../../../../assets/gltf-glb/ui/arm_ui_drop.glb"; -import { upsertProductOrEventApi } from '../../../../services/simulation/UpsertProductOrEventApi'; +import { upsertProductOrEventApi } from '../../../../services/simulation/products/UpsertProductOrEventApi'; type Positions = { pick: [number, number, number]; diff --git a/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx b/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx index 300c55c..db55344 100644 --- a/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx +++ b/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx @@ -10,7 +10,7 @@ import { } from "../../../../store/simulation/useSimulationStore"; import { useVehicleStore } from "../../../../store/simulation/useVehicleStore"; import { useProductStore } from "../../../../store/simulation/useProductStore"; -import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi"; +import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { DoubleSide, Group, Plane, Vector3 } from "three"; import startPoint from "../../../../assets/gltf-glb/ui/arrow_green.glb"; diff --git a/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx b/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx index 4ddc374..36fcee7 100644 --- a/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx +++ b/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx @@ -63,6 +63,7 @@ const MaterialAnimator = ({ storage.currentMaterials.map((mat, index) => ( { const machine = getMachineById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || ''); diff --git a/app/src/modules/simulation/vehicle/instances/animator/materialAnimator.tsx b/app/src/modules/simulation/vehicle/instances/animator/materialAnimator.tsx index 0f4af22..fb4ee8e 100644 --- a/app/src/modules/simulation/vehicle/instances/animator/materialAnimator.tsx +++ b/app/src/modules/simulation/vehicle/instances/animator/materialAnimator.tsx @@ -36,6 +36,7 @@ const MaterialAnimator = ({ agvDetail }: MaterialAnimatorProps) => { {agvDetail.currentMaterials.length > 0 && } diff --git a/app/src/services/simulation/UpsertProductOrEventApi.ts b/app/src/services/simulation/products/UpsertProductOrEventApi.ts similarity index 100% rename from app/src/services/simulation/UpsertProductOrEventApi.ts rename to app/src/services/simulation/products/UpsertProductOrEventApi.ts diff --git a/app/src/services/simulation/deleteEventDataApi.ts b/app/src/services/simulation/products/deleteEventDataApi.ts similarity index 100% rename from app/src/services/simulation/deleteEventDataApi.ts rename to app/src/services/simulation/products/deleteEventDataApi.ts diff --git a/app/src/services/simulation/deleteProductApi.ts b/app/src/services/simulation/products/deleteProductApi.ts similarity index 100% rename from app/src/services/simulation/deleteProductApi.ts rename to app/src/services/simulation/products/deleteProductApi.ts diff --git a/app/src/services/simulation/getProductApi.ts b/app/src/services/simulation/products/getProductApi.ts similarity index 100% rename from app/src/services/simulation/getProductApi.ts rename to app/src/services/simulation/products/getProductApi.ts diff --git a/app/src/services/simulation/getallProductsApi.ts b/app/src/services/simulation/products/getallProductsApi.ts similarity index 100% rename from app/src/services/simulation/getallProductsApi.ts rename to app/src/services/simulation/products/getallProductsApi.ts diff --git a/app/src/services/simulation/renameProductApi.ts b/app/src/services/simulation/products/renameProductApi.ts similarity index 100% rename from app/src/services/simulation/renameProductApi.ts rename to app/src/services/simulation/products/renameProductApi.ts diff --git a/app/src/services/simulation/webWorkers/collisionWorker.ts b/app/src/services/simulation/webWorkers/collisionWorker.ts new file mode 100644 index 0000000..aa6887b --- /dev/null +++ b/app/src/services/simulation/webWorkers/collisionWorker.ts @@ -0,0 +1,40 @@ +onmessage = function (e) { + const { materials, positions, sizes } = e.data; + const collisions = []; + const allPairs = []; + + for (let i = 0; i < materials.length; i++) { + for (let j = i + 1; j < materials.length; j++) { + const mat1 = materials[i]; + const mat2 = materials[j]; + const pos1 = positions[mat1.materialId]; + const pos2 = positions[mat2.materialId]; + + if (!pos1 || !pos2) continue; + + const size1 = sizes[mat1.materialId] || 0.2; + const size2 = sizes[mat2.materialId] || 0.2; + + const distance = Math.sqrt( + Math.pow(pos1.x - pos2.x, 2) + + Math.pow(pos1.y - pos2.y, 2) + + Math.pow(pos1.z - pos2.z, 2) + ); + + allPairs.push({ + materialId1: mat1.materialId, + materialId2: mat2.materialId + }); + + if (distance < (size1 + size2)) { + collisions.push({ + materialId1: mat1.materialId, + materialId2: mat2.materialId, + distance: distance + }); + } + } + } + + postMessage({ collisions, allPairs }); +}; \ No newline at end of file