Dwinzo_dev/app/src/modules/builder/agv/agv.tsx

164 lines
5.1 KiB
TypeScript
Raw Normal View History

import PolygonGenerator from "./polygonGenerator";
2025-03-26 12:58:14 +00:00
import { useThree } from "@react-three/fiber";
import { useEffect, useMemo, useRef, useState } from "react";
2025-03-26 12:58:14 +00:00
import * as THREE from "three";
import * as Types from "../../../types/world/worldTypes";
2025-03-26 12:58:14 +00:00
import PathNavigator from "./pathNavigator";
import NavMeshDetails from "./navMeshDetails";
2025-04-02 13:42:14 +00:00
import {
useSelectedActionSphere,
useSimulationPaths,
} from "../../../store/store";
import * as CONSTANTS from "../../../types/world/worldConstants";
2025-04-02 13:42:14 +00:00
const Agv = ({
lines,
}: {
lines: Types.RefLines;
}) => {
const [pathPoints, setPathPoints] = useState<
{
2025-04-04 04:16:18 +00:00
modelUuid: string;
modelSpeed: number;
bufferTime: number;
2025-04-02 13:42:14 +00:00
points: { x: number; y: number; z: number }[];
2025-04-04 11:23:57 +00:00
hitCount: number;
2025-04-02 13:42:14 +00:00
}[]
>([]);
const { simulationPaths } = useSimulationPaths();
const { selectedActionSphere } = useSelectedActionSphere();
useEffect(() => {
if (!Array.isArray(simulationPaths)) {
} else {
let agvModels = simulationPaths.filter(
(val: any) => val.modelName === "agv"
);
2025-04-04 11:23:57 +00:00
2025-04-02 13:42:14 +00:00
let findMesh = agvModels.filter(
(val: any) =>
val.modeluuid === selectedActionSphere?.path?.modeluuid &&
val.type === "Vehicle"
);
const result =
findMesh.length > 0 &&
findMesh[0].type === "Vehicle" &&
typeof findMesh[0].points?.actions.start === "object" &&
typeof findMesh[0].points?.actions.end === "object" &&
"x" in findMesh[0].points.actions.start &&
"y" in findMesh[0].points.actions.start &&
"x" in findMesh[0].points.actions.end &&
"y" in findMesh[0].points.actions.end
2025-04-02 13:42:14 +00:00
? [
{
2025-04-04 04:16:18 +00:00
modelUuid: findMesh[0].modeluuid, // Ensure it's a number
modelSpeed: findMesh[0].points.speed,
bufferTime: findMesh[0].points.actions.buffer,
hitCount: findMesh[0].points.actions.hitCount,
2025-04-02 13:42:14 +00:00
points: [
{
x: findMesh[0].position[0],
y: findMesh[0].position[1],
z: findMesh[0].position[2],
},
{
x: findMesh[0].points.actions.start.x,
2025-04-02 13:42:14 +00:00
y: 0,
z: findMesh[0].points.actions.start.y,
2025-04-02 13:42:14 +00:00
},
{
x: findMesh[0].points.actions.end.x,
2025-04-02 13:42:14 +00:00
y: 0,
z: findMesh[0].points.actions.end.y,
2025-04-02 13:42:14 +00:00
},
],
},
]
: [];
if (result.length > 0) {
2025-04-04 04:16:18 +00:00
// setPathPoints((prev) => [...prev, ...result]);
2025-04-02 13:42:14 +00:00
setPathPoints((prev) => {
2025-04-04 04:16:18 +00:00
const existingIndex = prev.findIndex(
(item) => item.modelUuid === result[0].modelUuid
2025-04-02 13:42:14 +00:00
);
2025-04-04 04:16:18 +00:00
if (existingIndex !== -1) {
const updatedPathPoints = [...prev];
updatedPathPoints[existingIndex] = result[0];
return updatedPathPoints;
} else {
return [...prev, ...result];
}
2025-04-02 13:42:14 +00:00
});
}
}
2025-04-04 04:16:18 +00:00
// setPathPoints((prev) => {
// const existingUUIDs = new Set(prev.map((item) => item.uuid));
// const newItems = result.filter(
// (item) => !existingUUIDs.has(item.uuid)
// );
// return [...prev, ...newItems];
// });
2025-04-02 13:42:14 +00:00
}, [simulationPaths, selectedActionSphere]);
let groupRef = useRef() as Types.RefGroup;
const [navMesh, setNavMesh] = useState();
2025-03-26 12:58:14 +00:00
return (
<>
<PolygonGenerator groupRef={groupRef} lines={lines} />
<NavMeshDetails
2025-03-26 13:10:28 +00:00
lines={lines}
setNavMesh={setNavMesh}
groupRef={groupRef}
2025-03-26 13:10:28 +00:00
/>
2025-03-26 12:58:14 +00:00
{pathPoints.map((pair, i) => (
2025-04-04 04:16:18 +00:00
<>
<PathNavigator
navMesh={navMesh}
selectedPoints={pair.points}
id={pair.modelUuid}
key={i}
speed={pair.modelSpeed}
bufferTime={pair.bufferTime}
2025-04-04 11:23:57 +00:00
hitCount={pair.hitCount}
2025-04-04 04:16:18 +00:00
/>
{/* {pair.points.length > 2 && (
<>
<mesh
position={[
pair.points[1].x,
pair.points[1].y,
pair.points[1].z,
]}
>
<sphereGeometry args={[0.3, 15, 15]} />
<meshStandardMaterial color="red" />
</mesh>
<mesh
position={[
pair.points[2].x,
pair.points[2].y,
pair.points[2].z,
]}
>
<sphereGeometry args={[0.3, 15, 15]} />
<meshStandardMaterial color="red" />
</mesh>
</>
)} */}
</>
2025-03-26 12:58:14 +00:00
))}
<group ref={groupRef} visible={false} name="Meshes">
<mesh rotation-x={CONSTANTS.planeConfig.rotation} position={CONSTANTS.planeConfig.position3D} name="Plane" receiveShadow>
<planeGeometry args={[300, 300]} />
<meshBasicMaterial color={CONSTANTS.planeConfig.color} />
</mesh>
</group>
2025-03-26 12:58:14 +00:00
</>
);
};
export default Agv;