| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | return nodeList.stream().map(NavigateNode::getCodeData).distinct().collect(Collectors.toList()); |
| | | } |
| | | |
| | | // v1 BFS ------------------------------------------------------------------------------ |
| | | public List<NavigateNode> getWaveScopeByCode(Integer lev, String code, Double radiusLen) { |
| | | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); |
| | | String[][] 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, radiusLen, includeList, existNodes); |
| | | this.spreadWaveNode(originNode, originNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | |
| | | return includeList; |
| | | } |
| | | |
| | | public void spreadWaveNode(NavigateNode originNode, NavigateNode currNode |
| | | , String[][] codeMatrix, Double radiusLen |
| | | , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen |
| | | , List<NavigateNode> includeList, List<NavigateNode> existNodes) { |
| | | int x = currNode.getX(); |
| | | int y = currNode.getY(); |
| | | |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y + 1), codeMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x, y - 1), codeMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x - 1, y), codeMatrix, radiusLen, includeList, existNodes); |
| | | this.extendNeighborNodes(originNode, new NavigateNode(x + 1, y), codeMatrix, radiusLen, includeList, existNodes); |
| | | 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); |
| | | } |
| | | |
| | | public void extendNeighborNodes(NavigateNode originNode, NavigateNode nextNode |
| | | , String[][] codeMatrix, Double radiusLen |
| | | , String[][] codeMatrix, String[][] cdaMatrix, Double radiusLen |
| | | , List<NavigateNode> includeList, List<NavigateNode> existNodes) { |
| | | |
| | | int x = nextNode.getX(); |
| | |
| | | |
| | | if (nextNodeCodeData.equals(CodeNodeType.NONE.val)) { |
| | | |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, radiusLen, includeList, existNodes); |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | |
| | | } else { |
| | | |
| | | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; |
| | | String[][] cdaMatrix = mapDataDispatcher.getCdaMatrix(lev); |
| | | |
| | | // Code o1 = codeService.selectByData(originNode.getCodeData()); |
| | | // Code o2 = codeService.selectByData(nextNodeCodeData); |
| | | List<Double> o1Cda = MapDataUtils.parseCdaNode(cdaMatrix[originNode.getX()][originNode.getY()]); |
| | | List<Double> o2Cda = MapDataUtils.parseCdaNode(cdaMatrix[nextNode.getX()][nextNode.getY()]); |
| | | |
| | |
| | | nextNode.setCodeData(nextNodeCodeData); |
| | | includeList.add(nextNode); |
| | | |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, radiusLen, includeList, existNodes); |
| | | this.spreadWaveNode(originNode, nextNode, codeMatrix, cdaMatrix, radiusLen, includeList, existNodes); |
| | | |
| | | } |
| | | } |
| | |
| | | return o1.getX() == o2.getX() && o1.getY() == o2.getY(); |
| | | } |
| | | |
| | | // 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 (this.isExist0(nextNode, visited)) { |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean isExist0(NavigateNode node, Set<NavigateNode> existNodes) { |
| | | for (NavigateNode existNode : existNodes) { |
| | | if (this.isSame(node, existNode)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | public Boolean isTurnCorner(String codeData) { |
| | | if (Cools.isEmpty(codeData)) { |
| | | return false; |