| | |
| | | package com.zy.acs.manager.core.service; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.zy.acs.common.enums.AgvDirectionType; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.utils.MapDataUtils; |
| | | import com.zy.acs.manager.core.constant.MapDataConstant; |
| | | import com.zy.acs.manager.core.domain.SortCodeDto; |
| | | import com.zy.acs.manager.core.domain.UnlockPathTask; |
| | |
| | | @Slf4j |
| | | @Component("mapService") |
| | | public class MapService { |
| | | |
| | | private final static int[][] DIRECTIONS = {{0,1},{0,-1},{-1,0},{1,0}}; |
| | | |
| | | @Value("${floyd.enable}") |
| | | private Boolean floydEnable; |
| | |
| | | |
| | | return floydNavigateService.calculatePath(startIdx, endIdx).stream().map(path -> { |
| | | Long codeId = floydNavigateService.getCodeId(path); |
| | | Code code = codeService.getById(codeId); |
| | | Code code = codeService.getCacheById(codeId); |
| | | return code.getData(); |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | |
| | | // 角度计算 |
| | | public Double calculateDirection(Code startCode, Code endCode) { |
| | | public Double calculateDirection(Code startCode, Code endCode, int angleOffsetVal) { |
| | | Double x0 = startCode.getX(); |
| | | Double y0 = startCode.getY(); |
| | | |
| | |
| | | double deltaX = x1 - x0; |
| | | double deltaY = y1 - y0; |
| | | double angle = -Math.atan2(deltaX, deltaY); |
| | | int angleOffsetVal = configService.getVal("mapAngleOffsetVal", Integer.class); |
| | | angle = Math.toDegrees(angle) + angleOffsetVal; |
| | | angle = (angle + 360) % 360; // 将角度转换为正值 |
| | | |
| | |
| | | } |
| | | |
| | | // 坐标货架阈值 todo:luxiaotao |
| | | public AgvDirectionType calculateAgvWorkDirection(JSONObject storeDirection, Loc loc, Code code) { |
| | | storeDirection.getString("key"); |
| | | public AgvDirectionType calculateAgvWorkDirection(Loc loc, Code code) { |
| | | Integer compDirect = loc.getCompDirect(); |
| | | AgvDirectionType agvDirectionType = null; |
| | | if (compDirect == 0) { |
| | |
| | | |
| | | // v1 BFS ------------------------------------------------------------------------------ |
| | | public List<NavigateNode> getWaveScopeByCode(Integer lev, String code, Double radiusLen) { |
| | | double radiusLenSquared = Math.pow(radiusLen, 2); |
| | | |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); |
| | | String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); |
| | | Double[][][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); |
| | | |
| | | int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); |
| | | NavigateNode originNode = new NavigateNode(codeMatrixIdx[0], codeMatrixIdx[1], code); |
| | |
| | | includeList.add(originNode); |
| | | existNodes.add(originNode); |
| | | |
| | | this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | |
| | | return includeList; |
| | | } |
| | | |
| | | public void spreadWaveNode(NavigateNode originNode, NavigateNode currNode |
| | | , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen |
| | | , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLenSquared |
| | | , List<NavigateNode> includeList, Set<NavigateNode> existNodes) { |
| | | int x = currNode.getX(); |
| | | int y = currNode.getY(); |
| | | |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | } |
| | | |
| | | public void extendNeighborNodes(NavigateNode originNode, NavigateNode nextNode |
| | | , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen |
| | | , String[][] codeMatrix, Double[][][] cdaMatrix, Double radiusLenSquared |
| | | , List<NavigateNode> includeList, Set<NavigateNode> existNodes) { |
| | | |
| | | int x = nextNode.getX(); |
| | |
| | | |
| | | existNodes.add(nextNode); |
| | | |
| | | |
| | | List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); |
| | | List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[x][y]); |
| | | |
| | | if (Math.pow(o1Cda.get(0) - o2Cda.get(0), 2) + Math.pow(o1Cda.get(1) - o2Cda.get(1), 2) <= Math.pow(radiusLen, 2)) { |
| | | Double[] o1Cda = cdaMatrix[originNode.getX()][originNode.getY()]; |
| | | Double[] o2Cda = cdaMatrix[x][y]; |
| | | if (Math.pow(o1Cda[0] - o2Cda[0], 2) + Math.pow(o1Cda[1] - o2Cda[1], 2) <= radiusLenSquared) { |
| | | nextNode.setCodeData(codeMatrix[x][y]); |
| | | |
| | | if (!nextNode.getCodeData().equals(CodeNodeType.NONE.val)) { |
| | | includeList.add(nextNode); |
| | | } |
| | | |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLenSquared, includeList, existNodes); |
| | | } |
| | | |
| | | } |
| | | |
| | | // v2 BFS ------------------------------------------------------------------------------ |
| | | public List<NavigateNode> getWaveScopeByCode0(Integer lev, String code, Double radiusLen) { |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); |
| | | |
| | | int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); |
| | | NavigateNode originNode = new NavigateNode(codeMatrixIdx[0], codeMatrixIdx[1], code); |
| | | |
| | | List<NavigateNode> includeList = new ArrayList<>(); |
| | | Set<NavigateNode> visited = new HashSet<>(); // Track visited nodes to avoid re-processing |
| | | Queue<NavigateNode> queue = new LinkedList<>(); |
| | | |
| | | includeList.add(originNode); |
| | | visited.add(originNode); |
| | | queue.offer(originNode); |
| | | |
| | | while (!queue.isEmpty()) { |
| | | NavigateNode currNode = queue.poll(); |
| | | this.spreadWaveNode0(originNode, currNode, codeMatrix, radiusLen, includeList, visited, queue); |
| | | } |
| | | |
| | | return includeList; |
| | | } |
| | | |
| | | public void spreadWaveNode0(NavigateNode originNode, NavigateNode currNode, |
| | | String[][] codeMatrix, Double radiusLen, |
| | | List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { |
| | | |
| | | int x = currNode.getX(); |
| | | int y = currNode.getY(); |
| | | |
| | | // Expand neighbors in all four directions (up, down, left, right) |
| | | this.extendNeighborNodes0(originNode, new NavigateNode(x, y + 1), codeMatrix, radiusLen, includeList, visited, queue); |
| | | this.extendNeighborNodes0(originNode, new NavigateNode(x, y - 1), codeMatrix, radiusLen, includeList, visited, queue); |
| | | this.extendNeighborNodes0(originNode, new NavigateNode(x - 1, y), codeMatrix, radiusLen, includeList, visited, queue); |
| | | this.extendNeighborNodes0(originNode, new NavigateNode(x + 1, y), codeMatrix, radiusLen, includeList, visited, queue); |
| | | } |
| | | |
| | | public void extendNeighborNodes0(NavigateNode originNode, NavigateNode nextNode, |
| | | String[][] codeMatrix, Double radiusLen, |
| | | List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { |
| | | int x = nextNode.getX(); |
| | | int y = nextNode.getY(); |
| | | |
| | | // Check if the node is out of bounds |
| | | if (x < 0 || x >= codeMatrix.length || y < 0 || y >= codeMatrix[0].length) { |
| | | return; |
| | | } |
| | | |
| | | // If the node has already been visited, skip it |
| | | if (visited.contains(nextNode)) { |
| | | return; |
| | | } |
| | | |
| | | visited.add(nextNode); |
| | | |
| | | String nextNodeCodeData = codeMatrix[x][y]; |
| | | |
| | | // If it's a NONE node, we still need to check its surroundings |
| | | if (nextNodeCodeData.equals(CodeNodeType.NONE.val)) { |
| | | this.spreadWaveNode0(originNode, nextNode, codeMatrix, radiusLen, includeList, visited, queue); |
| | | } else { |
| | | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; |
| | | String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); |
| | | |
| | | // Check if the distance between nodes is within the radius length |
| | | List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); |
| | | List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[nextNode.getX()][nextNode.getY()]); |
| | | |
| | | // Calculate Euclidean distance between the nodes |
| | | if (Math.pow(o1Cda.get(0) - o2Cda.get(0), 2) + Math.pow(o1Cda.get(1) - o2Cda.get(1), 2) <= Math.pow(radiusLen, 2)) { |
| | | nextNode.setCodeData(nextNodeCodeData); |
| | | includeList.add(nextNode); |
| | | |
| | | // Add the node to the queue to expand its neighbors |
| | | queue.offer(nextNode); |
| | | } |
| | | } |
| | | } |
| | | // public List<NavigateNode> getWaveScopeByCode0(Integer lev, String code, Double radiusLen) { |
| | | // String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); |
| | | // |
| | | // int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, code); |
| | | // NavigateNode originNode = new NavigateNode(codeMatrixIdx[0], codeMatrixIdx[1], code); |
| | | // |
| | | // List<NavigateNode> includeList = new ArrayList<>(); |
| | | // Set<NavigateNode> visited = new HashSet<>(); // Track visited nodes to avoid re-processing |
| | | // Queue<NavigateNode> queue = new LinkedList<>(); |
| | | // |
| | | // includeList.add(originNode); |
| | | // visited.add(originNode); |
| | | // queue.offer(originNode); |
| | | // |
| | | // while (!queue.isEmpty()) { |
| | | // NavigateNode currNode = queue.poll(); |
| | | // this.spreadWaveNode0(originNode, currNode, codeMatrix, radiusLen, includeList, visited, queue); |
| | | // } |
| | | // |
| | | // return includeList; |
| | | // } |
| | | // |
| | | // public void spreadWaveNode0(NavigateNode originNode, NavigateNode currNode, |
| | | // String[][] codeMatrix, Double radiusLen, |
| | | // List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { |
| | | // |
| | | // int x = currNode.getX(); |
| | | // int y = currNode.getY(); |
| | | // |
| | | // // Expand neighbors in all four directions (up, down, left, right) |
| | | // this.extendNeighborNodes0(originNode, new NavigateNode(x, y + 1), codeMatrix, radiusLen, includeList, visited, queue); |
| | | // this.extendNeighborNodes0(originNode, new NavigateNode(x, y - 1), codeMatrix, radiusLen, includeList, visited, queue); |
| | | // this.extendNeighborNodes0(originNode, new NavigateNode(x - 1, y), codeMatrix, radiusLen, includeList, visited, queue); |
| | | // this.extendNeighborNodes0(originNode, new NavigateNode(x + 1, y), codeMatrix, radiusLen, includeList, visited, queue); |
| | | // } |
| | | // |
| | | // public void extendNeighborNodes0(NavigateNode originNode, NavigateNode nextNode, |
| | | // String[][] codeMatrix, Double radiusLen, |
| | | // List<NavigateNode> includeList, Set<NavigateNode> visited, Queue<NavigateNode> queue) { |
| | | // int x = nextNode.getX(); |
| | | // int y = nextNode.getY(); |
| | | // |
| | | // // Check if the node is out of bounds |
| | | // if (x < 0 || x >= codeMatrix.length || y < 0 || y >= codeMatrix[0].length) { |
| | | // return; |
| | | // } |
| | | // |
| | | // // If the node has already been visited, skip it |
| | | // if (visited.contains(nextNode)) { |
| | | // return; |
| | | // } |
| | | // |
| | | // visited.add(nextNode); |
| | | // |
| | | // String nextNodeCodeData = codeMatrix[x][y]; |
| | | // |
| | | // // If it's a NONE node, we still need to check its surroundings |
| | | // if (nextNodeCodeData.equals(CodeNodeType.NONE.val)) { |
| | | // this.spreadWaveNode0(originNode, nextNode, codeMatrix, radiusLen, includeList, visited, queue); |
| | | // } else { |
| | | // Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; |
| | | // String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); |
| | | // |
| | | // // Check if the distance between nodes is within the radius length |
| | | // List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); |
| | | // List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[nextNode.getX()][nextNode.getY()]); |
| | | // |
| | | // // Calculate Euclidean distance between the nodes |
| | | // if (Math.pow(o1Cda.get(0) - o2Cda.get(0), 2) + Math.pow(o1Cda.get(1) - o2Cda.get(1), 2) <= Math.pow(radiusLen, 2)) { |
| | | // nextNode.setCodeData(nextNodeCodeData); |
| | | // includeList.add(nextNode); |
| | | // |
| | | // // Add the node to the queue to expand its neighbors |
| | | // queue.offer(nextNode); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | |
| | | public Boolean isTurnCorner(String codeData) { |
| | |
| | | return turnMatrixNode == TurnNodeType.TURN.val; |
| | | } |
| | | |
| | | /** |
| | | * That vehicle is walking if the dynamic node count > 1 |
| | | */ |
| | | @SuppressWarnings("all") |
| | | public boolean isWalkingByVehicle(Integer lev, String vehicle) { |
| | | if (Cools.isEmpty(vehicle)) { |
| | | return false; |
| | | } |
| | | |
| | | lev = Optional.ofNullable(lev).orElse(MapDataDispatcher.MAP_DEFAULT_LEV); |
| | | DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); |
| | | |
| | | int count = 0; |
| | | for (int i = 0; i < dynamicMatrix.length; i++) { |
| | | for (int j = 0; j < dynamicMatrix[i].length; j++) { |
| | | if (vehicle.equals(dynamicMatrix[i][j].getVehicle())) { |
| | | count++; |
| | | if (count > 1) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | public List<String> queryCodeListFromDynamicNode(Integer lev, String nodeType) { |
| | | if (Cools.isEmpty(nodeType)) { |
| | | return new ArrayList<>(); |