| | |
| | | import com.algo.model.PlannedPath; |
| | | import com.algo.util.JsonUtils; |
| | | import com.algo.util.PathTimeCalculator; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | |
| | | import java.io.File; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public AStarPathPlanner(Map<String, Map<String, Integer>> pathMapping) { |
| | | this.pathMapping = pathMapping; |
| | | this.adjacencyList = new HashMap<>(); |
| | | |
| | | // 加载环境连通性数据 |
| | | this.environmentConnectivity = JsonUtils.loadEnvironmentConnectivity("environment.json"); |
| | | |
| | | // 构建环境感知的邻接表 |
| | | buildEnvironmentAwareAdjacencyList(); |
| | | // 加载邻接表 |
| | | Map<String, List<Map<String, String>>> loadedAdjacency = loadAdjacencyListFromJson("adjacency.json"); |
| | | |
| | | if (loadedAdjacency != null && !loadedAdjacency.isEmpty()) { |
| | | // 成功加载预生成的邻接表 |
| | | this.adjacencyList = loadedAdjacency; |
| | | System.out.println("从 adjacency.json 加载邻接表: " + adjacencyList.size() + " 个节点"); |
| | | } else { |
| | | this.adjacencyList = new HashMap<>(); |
| | | buildEnvironmentAwareAdjacencyList(); |
| | | System.out.println("使用网格相邻性构建邻接表: " + adjacencyList.size() + " 个节点"); |
| | | } |
| | | |
| | | // 加载实际坐标映射 |
| | | loadRealCoordinateMapping(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 从 JSON 文件加载预生成的邻接表 |
| | | */ |
| | | private Map<String, List<Map<String, String>>> loadAdjacencyListFromJson(String filePath) { |
| | | try { |
| | | File file = new File(filePath); |
| | | |
| | | if (!file.exists()) { |
| | | System.out.println("路径邻接表文件不存在: " + filePath + ",使用默认构建方法"); |
| | | return null; |
| | | } |
| | | |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | TypeReference<Map<String, List<Map<String, String>>>> typeRef = |
| | | new TypeReference<Map<String, List<Map<String, String>>>>() {}; |
| | | |
| | | Map<String, List<Map<String, String>>> adjacency = objectMapper.readValue(file, typeRef); |
| | | |
| | | if (adjacency == null || adjacency.isEmpty()) { |
| | | System.out.println("邻接表文件为空: " + filePath); |
| | | return null; |
| | | } |
| | | |
| | | return adjacency; |
| | | |
| | | } catch (Exception e) { |
| | | System.err.println("加载邻接表失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 加载实际坐标映射 |
| | | */ |
| | | private void loadRealCoordinateMapping() { |