From d808837cd368c3772962be591aa6532bcc0cf3e4 Mon Sep 17 00:00:00 2001
From: jianghaiyue <jianghaiyue@zkyt.com>
Date: 星期一, 22 九月 2025 12:11:37 +0800
Subject: [PATCH] 更新版本

---
 algo-zkd/src/main/java/com/algo/util/JsonUtils.java |  198 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 173 insertions(+), 25 deletions(-)

diff --git a/algo-zkd/src/main/java/com/algo/util/JsonUtils.java b/algo-zkd/src/main/java/com/algo/util/JsonUtils.java
index 8ec1aff..7a858e5 100644
--- a/algo-zkd/src/main/java/com/algo/util/JsonUtils.java
+++ b/algo-zkd/src/main/java/com/algo/util/JsonUtils.java
@@ -5,18 +5,17 @@
 import java.io.*;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
- * JSON鏂囦欢璇诲彇宸ュ叿绫�
- * 鐢ㄤ簬璇诲彇鐜閰嶇疆鍜岃矾寰勬槧灏勬枃浠�
+ * JSON鏂囦欢璇诲彇
  */
 public class JsonUtils {
 
     /**
-     * 璇诲彇JSON鏂囦欢鍐呭
-     *
      * @param filePath 鏂囦欢璺緞
      * @return JSON瀛楃涓插唴瀹�
      * @throws IOException 鏂囦欢璇诲彇寮傚父
@@ -33,8 +32,7 @@
     }
 
     /**
-     * 瑙f瀽璺緞鏄犲皠JSON鍐呭
-     * 姝g‘瑙f瀽path_mapping.json鐨勫疄闄呮牸寮忥細{"path_id_to_coordinates": {...}}
+     * 瑙f瀽璺緞鏄犲皠JSON
      *
      * @param jsonContent JSON鍐呭
      * @return 璺緞鏄犲皠Map锛宬ey涓鸿矾寰勭紪鍙凤紝value涓哄潗鏍囦俊鎭�
@@ -43,7 +41,6 @@
         Map<String, Map<String, Integer>> pathMapping = new HashMap<>();
 
         try {
-            // 鎵惧埌path_id_to_coordinates閮ㄥ垎
             String pathIdSection = extractJsonSection(jsonContent, "path_id_to_coordinates");
             if (pathIdSection == null) {
                 System.err.println("鏈壘鍒皃ath_id_to_coordinates閮ㄥ垎");
@@ -101,8 +98,7 @@
     }
 
     /**
-     * 鍔犺浇鍜岃В鏋愯矾寰勬槧灏勬枃浠�
-     * 姝g‘瑙f瀽path_mapping.json鐨勫疄闄呮牸寮忥細{"path_id_to_coordinates": {...}}
+     * 璺緞鏄犲皠
      *
      * @param filePath 鏂囦欢鍦板潃
      * @return 璺緞鏄犲皠Map锛宬ey涓鸿矾寰勭紪鍙凤紝value涓哄潗鏍囦俊鎭�
@@ -112,7 +108,6 @@
         Map<String, Map<String, Integer>> pathMapping = new HashMap<>();
         ObjectMapper objectMapper = new ObjectMapper();
         try {
-            // 鍏堣В鏋愪负椤跺眰Map<String, Object>
             Map<String, Object> topLevelMap = objectMapper.readValue(
                     new File(filePath),
                     Map.class
@@ -123,10 +118,9 @@
                 return pathMapping;
             }
 
-            // 澶勭悊 path_id_to_coordinates锛堝皢鍧愭爣鍒楄〃杞崲涓虹涓�涓潗鏍囩殑path_id鏄犲皠锛�
+            // 澶勭悊 path_id_to_coordinates
             Map<String, Object> pathIdCoords = (Map<String, Object>) topLevelMap.get("path_id_to_coordinates");
             for (Map.Entry<String, Object> entry : pathIdCoords.entrySet()) {
-                // 淇濆瓨鏂瑰紡: 璺緞ID:{"x": , "y":}
                 String pathId = entry.getKey();
                 Object coordsObj = entry.getValue();
                 if (coordsObj instanceof List) {
@@ -155,8 +149,7 @@
     }
 
     /**
-     * 瑙f瀽鐜閰嶇疆JSON鍐呭
-     * 瑙f瀽environment.json涓殑stations淇℃伅
+     * 鐜閰嶇疆JSON
      *
      * @param jsonContent JSON鍐呭
      * @return 鐜閰嶇疆Map
@@ -165,7 +158,6 @@
         Map<String, Object> config = new HashMap<>();
 
         try {
-            // 瑙f瀽width
             if (jsonContent.contains("\"width\":")) {
                 String widthStr = jsonContent.substring(jsonContent.indexOf("\"width\":") + 8);
                 widthStr = widthStr.substring(0, widthStr.indexOf(",")).trim();
@@ -176,7 +168,6 @@
                 }
             }
 
-            // 瑙f瀽height
             if (jsonContent.contains("\"height\":")) {
                 String heightStr = jsonContent.substring(jsonContent.indexOf("\"height\":") + 9);
                 heightStr = heightStr.substring(0, heightStr.indexOf(",")).trim();
@@ -187,7 +178,6 @@
                 }
             }
 
-            // 瑙f瀽stations淇℃伅
             Map<String, Map<String, Object>> stations = parseStations(jsonContent);
             config.put("stations", stations);
             config.put("stationCount", stations.size());
@@ -202,7 +192,6 @@
     }
 
     /**
-     * 鍔犺浇鍜岃В鏋愮幆澧冮厤缃枃浠�
      * 瑙f瀽environment.json涓殑stations淇℃伅
      *
      * @param filePath 鏂囦欢鍦板潃
@@ -219,21 +208,18 @@
                     Map.class
             );
 
-            // 瑙f瀽width
             if (topLevelMap.containsKey("width")) {
                 environmentMap.put("width", Integer.parseInt(topLevelMap.get("width").toString()));
             } else {
                 environmentMap.put("width", 78);
             }
 
-            // 瑙f瀽height
             if (topLevelMap.containsKey("width")) {
                 environmentMap.put("height", Integer.parseInt(topLevelMap.get("height").toString()));
             } else {
                 environmentMap.put("height", 50);
             }
 
-            // 瑙f瀽stations淇℃伅
             if (topLevelMap.containsKey("stations")) {
                 Map<String, Map<String, Object>> stations = new HashMap<>();
                 Map<String, Object> stationMap = (Map<String, Object>) topLevelMap.get("stations");
@@ -292,14 +278,12 @@
             for (String line : lines) {
                 line = line.trim();
 
-                // 鏌ユ壘宸ヤ綔绔橧D
                 if (line.startsWith("\"") && line.contains("\":{")) {
                     int endIndex = line.indexOf("\":{");
                     currentStation = line.substring(1, endIndex);
                     stations.put(currentStation, new HashMap<>());
                 }
 
-                // 瑙f瀽capacity
                 if (currentStation != null && line.contains("\"capacity\":")) {
                     String capacityStr = line.substring(line.indexOf("\"capacity\":") + 11);
                     capacityStr = capacityStr.substring(0, capacityStr.indexOf(",")).trim();
@@ -311,7 +295,6 @@
                     }
                 }
 
-                // 瑙f瀽load_position鍜寀nload_position
                 if (currentStation != null && line.contains("\"load_position\":")) {
                     List<Integer> loadPos = parsePosition(stationsSection, currentStation, "load_position");
                     if (loadPos != null) {
@@ -372,7 +355,7 @@
     }
 
     /**
-     * 鎻愬彇JSON涓殑鐗瑰畾閮ㄥ垎
+     * 鎻愬彇JSON涓壒瀹氶儴鍒�
      *
      * @param jsonContent JSON鍐呭
      * @param sectionName 閮ㄥ垎鍚嶇О
@@ -421,6 +404,64 @@
             return new int[]{coordMap.get("x"), coordMap.get("y")};
         }
         return null;
+    }
+    
+    /**
+     * 鍔犺浇瀹為檯鐗╃悊鍧愭爣鏄犲皠
+     *
+     * @param filePath 鏂囦欢璺緞
+     * @return 瀹為檯鍧愭爣鏄犲皠 Map<pathId, double[]{x_mm, y_mm}>
+     */
+    public static Map<String, double[]> loadRealCoordinateMapping(String filePath) {
+        Map<String, double[]> coordinateMapping = new HashMap<>();
+        ObjectMapper objectMapper = new ObjectMapper();
+        
+        try {
+            Map<String, Object> topLevelMap = objectMapper.readValue(new File(filePath), Map.class);
+            
+            if (!topLevelMap.containsKey("path_id_to_coordinates")) {
+                System.err.println("鏈壘鍒皃ath_id_to_coordinates閮ㄥ垎");
+                return coordinateMapping;
+            }
+            
+            Map<String, Object> pathIdCoords = (Map<String, Object>) topLevelMap.get("path_id_to_coordinates");
+            for (Map.Entry<String, Object> entry : pathIdCoords.entrySet()) {
+                String pathId = entry.getKey();
+                Object coordsObj = entry.getValue();
+                
+                if (coordsObj instanceof List) {
+                    List<?> coordsList = (List<?>) coordsObj;
+                    if (!coordsList.isEmpty()) {
+                        Map<?, ?> coordMap = (Map<?, ?>) coordsList.get(0);
+                        double x = ((Number) coordMap.get("x")).doubleValue();
+                        double y = ((Number) coordMap.get("y")).doubleValue();
+                        coordinateMapping.put(pathId, new double[]{x, y});
+                    }
+                }
+            }
+            
+            System.out.println("鎴愬姛鍔犺浇瀹為檯鍧愭爣鏄犲皠锛屽寘鍚� " + coordinateMapping.size() + " 涓矾寰勭偣");
+            
+        } catch (FileNotFoundException e) {
+            System.err.println("瀹為檯鍧愭爣鏂囦欢涓嶅瓨鍦�: " + e.getMessage());
+        } catch (IOException e) {
+            System.err.println("瀹為檯鍧愭爣鏂囦欢璇诲彇閿欒: " + e.getMessage());
+        } catch (Exception e) {
+            System.err.println("鍔犺浇瀹為檯鍧愭爣鏂囦欢澶辫触: " + e.getMessage());
+        }
+        
+        return coordinateMapping;
+    }
+    
+    /**
+     * 鑾峰彇璺緞鐐圭殑瀹為檯鍧愭爣
+     *
+     * @param pathId             璺緞鐐笽D
+     * @param realCoordinateMap  瀹為檯鍧愭爣鏄犲皠
+     * @return 鍧愭爣鏁扮粍 [x_mm, y_mm]锛屽鏋滄湭鎵惧埌杩斿洖null
+     */
+    public static double[] getRealCoordinate(String pathId, Map<String, double[]> realCoordinateMap) {
+        return realCoordinateMap.get(pathId);
     }
 
     /**
@@ -483,4 +524,111 @@
         int dy = coord1[1] - coord2[1];
         return Math.sqrt(dx * dx + dy * dy);
     }
+
+    /**
+     * 鍔犺浇鐜璺緞杩為�氭�ф暟鎹紙environment.json涓殑paths鍜宱bstacles锛�
+     *
+     * @param filePath 鐜鏂囦欢璺緞
+     * @return 鐜杩為�氭�ф暟鎹�
+     */
+    public static EnvironmentConnectivity loadEnvironmentConnectivity(String filePath) {
+        EnvironmentConnectivity connectivity = new EnvironmentConnectivity();
+        ObjectMapper objectMapper = new ObjectMapper();
+        
+        try {
+            Map<String, Object> topLevelMap = objectMapper.readValue(new File(filePath), Map.class);
+            
+            // 鍔犺浇鍙�氳璺緞
+            if (topLevelMap.containsKey("paths")) {
+                List<Map<String, Object>> pathsList = (List<Map<String, Object>>) topLevelMap.get("paths");
+                for (Map<String, Object> pathPoint : pathsList) {
+                    Integer x = (Integer) pathPoint.get("x");
+                    Integer y = (Integer) pathPoint.get("y");
+                    if (x != null && y != null) {
+                        connectivity.addValidPath(x, y);
+                    }
+                }
+            }
+            
+            // 鍔犺浇闅滅鐗�
+            if (topLevelMap.containsKey("obstacles")) {
+                List<Map<String, Object>> obstaclesList = (List<Map<String, Object>>) topLevelMap.get("obstacles");
+                for (Map<String, Object> obstacle : obstaclesList) {
+                    Integer x = (Integer) obstacle.get("x");
+                    Integer y = (Integer) obstacle.get("y");
+                    if (x != null && y != null) {
+                        connectivity.addObstacle(x, y);
+                    }
+                }
+            }
+            
+            System.out.println("鎴愬姛鍔犺浇鐜杩為�氭�ф暟鎹細" + 
+                connectivity.getValidPathsCount() + " 涓彲閫氳鐐癸紝" + 
+                connectivity.getObstaclesCount() + " 涓殰纰嶇墿鐐�");
+                
+        } catch (FileNotFoundException e) {
+            System.err.println("鐜鏂囦欢涓嶅瓨鍦�: " + e.getMessage());
+        } catch (IOException e) {
+            System.err.println("鐜鏂囦欢璇诲彇閿欒: " + e.getMessage());
+        } catch (Exception e) {
+            System.err.println("鍔犺浇鐜杩為�氭�ф暟鎹け璐�: " + e.getMessage());
+        }
+        
+        return connectivity;
+    }
+
+    /**
+     * 鐜杩為�氭�ф暟鎹被
+     */
+    public static class EnvironmentConnectivity {
+        private final Set<String> validPaths = new HashSet<>();
+        private final Set<String> obstacles = new HashSet<>();
+        
+        public void addValidPath(int x, int y) {
+            validPaths.add(x + "," + y);
+        }
+        
+        public void addObstacle(int x, int y) {
+            obstacles.add(x + "," + y);
+        }
+        
+        public boolean isValidPath(int x, int y) {
+            return validPaths.contains(x + "," + y);
+        }
+        
+        public boolean isObstacle(int x, int y) {
+            return obstacles.contains(x + "," + y);
+        }
+        
+        public boolean isTraversable(int x, int y) {
+            return isValidPath(x, y) && !isObstacle(x, y);
+        }
+        
+        public int getValidPathsCount() {
+            return validPaths.size();
+        }
+        
+        public int getObstaclesCount() {
+            return obstacles.size();
+        }
+        
+        /**
+         * 鑾峰彇鏈夋晥鐨勭浉閭昏妭鐐�
+         */
+        public List<int[]> getValidNeighbors(int x, int y) {
+            List<int[]> neighbors = new ArrayList<>();
+            int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 涓婁笅宸﹀彸
+            
+            for (int[] dir : directions) {
+                int newX = x + dir[0];
+                int newY = y + dir[1];
+                
+                if (isTraversable(newX, newY)) {
+                    neighbors.add(new int[]{newX, newY});
+                }
+            }
+            
+            return neighbors;
+        }
+    }
 } 
\ No newline at end of file

--
Gitblit v1.9.1