From 5ebf967d3438669d6cd66fd8b07dc9c0bd48e678 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期二, 14 十月 2025 16:13:41 +0800
Subject: [PATCH] 1
---
algo-zkd/src/main/java/com/algo/util/JsonUtils.java | 218 +++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 188 insertions(+), 30 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..e107ff3 100644
--- a/algo-zkd/src/main/java/com/algo/util/JsonUtils.java
+++ b/algo-zkd/src/main/java/com/algo/util/JsonUtils.java
@@ -3,20 +3,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
- * JSON鏂囦欢璇诲彇宸ュ叿绫�
- * 鐢ㄤ簬璇诲彇鐜閰嶇疆鍜岃矾寰勬槧灏勬枃浠�
+ * JSON鏂囦欢璇诲彇
*/
public class JsonUtils {
/**
- * 璇诲彇JSON鏂囦欢鍐呭
- *
* @param filePath 鏂囦欢璺緞
* @return JSON瀛楃涓插唴瀹�
* @throws IOException 鏂囦欢璇诲彇寮傚父
@@ -33,8 +27,7 @@
}
/**
- * 瑙f瀽璺緞鏄犲皠JSON鍐呭
- * 姝g‘瑙f瀽path_mapping.json鐨勫疄闄呮牸寮忥細{"path_id_to_coordinates": {...}}
+ * 瑙f瀽璺緞鏄犲皠JSON
*
* @param jsonContent JSON鍐呭
* @return 璺緞鏄犲皠Map锛宬ey涓鸿矾寰勭紪鍙凤紝value涓哄潗鏍囦俊鎭�
@@ -43,7 +36,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 +93,7 @@
}
/**
- * 鍔犺浇鍜岃В鏋愯矾寰勬槧灏勬枃浠�
- * 姝g‘瑙f瀽path_mapping.json鐨勫疄闄呮牸寮忥細{"path_id_to_coordinates": {...}}
+ * 璺緞鏄犲皠
*
* @param filePath 鏂囦欢鍦板潃
* @return 璺緞鏄犲皠Map锛宬ey涓鸿矾寰勭紪鍙凤紝value涓哄潗鏍囦俊鎭�
@@ -112,7 +103,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 +113,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) {
@@ -138,7 +127,7 @@
Map<String, Integer> pointMap = new HashMap<>();
pointMap.put("x", x);
pointMap.put("y", y);
- pathMapping.put(pathId, pointMap);
+ pathMapping.put(zeroFill(pathId,8), pointMap);
}
}
}
@@ -155,8 +144,7 @@
}
/**
- * 瑙f瀽鐜閰嶇疆JSON鍐呭
- * 瑙f瀽environment.json涓殑stations淇℃伅
+ * 鐜閰嶇疆JSON
*
* @param jsonContent JSON鍐呭
* @return 鐜閰嶇疆Map
@@ -165,7 +153,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 +163,6 @@
}
}
- // 瑙f瀽height
if (jsonContent.contains("\"height\":")) {
String heightStr = jsonContent.substring(jsonContent.indexOf("\"height\":") + 9);
heightStr = heightStr.substring(0, heightStr.indexOf(",")).trim();
@@ -187,7 +173,6 @@
}
}
- // 瑙f瀽stations淇℃伅
Map<String, Map<String, Object>> stations = parseStations(jsonContent);
config.put("stations", stations);
config.put("stationCount", stations.size());
@@ -202,7 +187,6 @@
}
/**
- * 鍔犺浇鍜岃В鏋愮幆澧冮厤缃枃浠�
* 瑙f瀽environment.json涓殑stations淇℃伅
*
* @param filePath 鏂囦欢鍦板潃
@@ -219,21 +203,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 +273,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 +290,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 +350,7 @@
}
/**
- * 鎻愬彇JSON涓殑鐗瑰畾閮ㄥ垎
+ * 鎻愬彇JSON涓壒瀹氶儴鍒�
*
* @param jsonContent JSON鍐呭
* @param sectionName 閮ㄥ垎鍚嶇О
@@ -421,6 +399,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(zeroFill(pathId,8), 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 +519,126 @@
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;
+ }
+ }
+
+ public static String zeroFill(String msg, Integer len) {
+ len = Optional.ofNullable(len).orElse(16);
+ if (msg.length() == len){
+ return msg;
+ } else if (msg.length() > len){
+ return msg.substring(0, 16);
+ } else {
+ StringBuilder msgBuilder = new StringBuilder(msg);
+ for (int i = 0; i<len-msg.length(); i++){
+ msgBuilder.insert(0,"0");
+ }
+ return msgBuilder.toString();
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.1