From 1175f5fbe8fd832943880bfc37c0e2a451a0688a Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期四, 25 十二月 2025 19:34:38 +0800
Subject: [PATCH] 删除了几个类优化了路径生成的逻辑
---
src/lujing/MowingPathGenerationPage.java | 414 +++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 263 insertions(+), 151 deletions(-)
diff --git a/src/lujing/MowingPathGenerationPage.java b/src/lujing/MowingPathGenerationPage.java
index c55639b..8bd2ba9 100644
--- a/src/lujing/MowingPathGenerationPage.java
+++ b/src/lujing/MowingPathGenerationPage.java
@@ -1,27 +1,14 @@
package lujing;
-
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
-import java.util.ArrayList;
import java.util.List;
-
import dikuai.Dikuai;
-import lujing.Lunjingguihua;
-import lujing.ObstaclePathPlanner;
-import lujing.Qufenxingzhuang;
-import lujing.AoxinglujingNoObstacle;
-import lujing.YixinglujingNoObstacle;
-import lujing.AoxinglujingHaveObstacel;
-import lujing.YixinglujingHaveObstacel;
-import org.locationtech.jts.geom.Coordinate;
+import publicway.Fuzhibutton;
import gecaoji.Device;
import java.util.Locale;
-import zhuye.Fuzhibutton;
/**
* 鐢熸垚鍓茶崏璺緞椤甸潰
@@ -49,6 +36,7 @@
boolean saveObstacleCoordinates(Dikuai dikuai, String baseStationValue, String obstacleValue);
boolean saveMowingWidth(Dikuai dikuai, String value);
boolean savePlannedPath(Dikuai dikuai, String value);
+ boolean saveMowingSafetyDistance(Dikuai dikuai, String value);
}
private final Dikuai dikuai;
@@ -125,32 +113,65 @@
// 鍦板潡杈圭晫
boundaryArea = createInfoTextArea(boundaryValue != null ? boundaryValue : "", true, 6);
- contentPanel.add(createTextAreaSection("鍦板潡杈圭晫", boundaryArea));
+ String boundaryTitle = "鍦板潡杈圭晫";
+ if (boundaryValue != null && !boundaryValue.trim().isEmpty() && !"-1".equals(boundaryValue.trim())) {
+ int boundaryCount = boundaryValue.split(";").length;
+ boundaryTitle = "鍦板潡杈圭晫 (" + boundaryCount + "鐐�)";
+ }
+ contentPanel.add(createTextAreaSection(boundaryTitle, boundaryArea));
// 闅滅鐗╁潗鏍�
obstacleArea = createInfoTextArea(obstacleValue != null ? obstacleValue : "", true, 6);
- contentPanel.add(createTextAreaSection("闅滅鐗╁潗鏍�", obstacleArea));
+ String obstacleTitle = "闅滅鐗╁潗鏍�";
+ if (obstacleValue != null && !obstacleValue.trim().isEmpty() && !"-1".equals(obstacleValue.trim())) {
+ // 闅滅鐗╁潗鏍囨牸寮忓彲鑳芥槸绌烘牸鍒嗛殧鐨勫涓殰纰嶇墿锛屾瘡涓殰纰嶇墿鐢ㄥ垎鍙峰垎闅斿潗鏍囩偣
+ // 璁$畻鎵�鏈夐殰纰嶇墿鐨勬�诲潗鏍囩偣鏁�
+ String[] obstacles = obstacleValue.trim().split("\\s+");
+ int totalObstaclePoints = 0;
+ for (String obstacle : obstacles) {
+ if (obstacle != null && !obstacle.trim().isEmpty()) {
+ totalObstaclePoints += obstacle.split(";").length;
+ }
+ }
+ if (totalObstaclePoints > 0) {
+ obstacleTitle = "闅滅鐗╁潗鏍� (" + totalObstaclePoints + "鐐�)";
+ }
+ }
+ contentPanel.add(createTextAreaSection(obstacleTitle, obstacleArea));
// 鍓茶崏瀹藉害
widthField = createInfoTextField(widthValue != null ? widthValue : "", true);
- contentPanel.add(createTextFieldSection("鍓茶崏瀹藉害 (鍘樼背)", widthField));
+ contentPanel.add(createTextFieldSection("鍓茶崏瀹藉害 (cm)", widthField));
// 鍓茶崏瀹夊叏璺濈锛堝彧璇绘樉绀猴級
+ // 浼樺厛浠嶥ikuai瀵硅薄鑾峰彇锛屽鏋淒ikuai涓病鏈夛紝鍐嶄粠Device鑾峰彇
String displaySafetyDistance = "鏈缃�";
- Device device = Device.getActiveDevice();
- if (device != null) {
- String safetyDistanceValue = device.getMowingSafetyDistance();
- if (safetyDistanceValue != null && !"-1".equals(safetyDistanceValue) && !safetyDistanceValue.trim().isEmpty()) {
- try {
- double distanceMeters = Double.parseDouble(safetyDistanceValue.trim());
- // 濡傛灉鍊煎ぇ浜�100锛岃涓烘槸鍘樼背锛岄渶瑕佽浆鎹负绫�
- if (distanceMeters > 100) {
- distanceMeters = distanceMeters / 100.0;
- }
- displaySafetyDistance = String.format("%.2f绫�", distanceMeters);
- } catch (NumberFormatException e) {
- displaySafetyDistance = "鏈缃�";
+ String safetyDistanceValue = null;
+
+ // 棣栧厛灏濊瘯浠嶥ikuai瀵硅薄鑾峰彇
+ if (dikuai != null) {
+ safetyDistanceValue = dikuai.getMowingSafetyDistance();
+ }
+
+ // 濡傛灉Dikuai涓病鏈夛紝浠嶥evice鑾峰彇
+ if ((safetyDistanceValue == null || "-1".equals(safetyDistanceValue) || safetyDistanceValue.trim().isEmpty())) {
+ Device device = Device.getActiveDevice();
+ if (device != null) {
+ safetyDistanceValue = device.getMowingSafetyDistance();
+ }
+ }
+
+ // 鏍煎紡鍖栨樉绀哄��
+ if (safetyDistanceValue != null && !"-1".equals(safetyDistanceValue) && !safetyDistanceValue.trim().isEmpty()) {
+ try {
+ double distanceMeters = Double.parseDouble(safetyDistanceValue.trim());
+ // 濡傛灉鍊煎ぇ浜�100锛岃涓烘槸鍘樼背锛岄渶瑕佽浆鎹负绫�
+ if (distanceMeters > 100) {
+ distanceMeters = distanceMeters / 100.0;
}
+ displaySafetyDistance = String.format(Locale.US, "%.2fm", distanceMeters);
+ } catch (NumberFormatException e) {
+ displaySafetyDistance = "鏈缃�";
}
}
contentPanel.add(createInfoValueSection("鍓茶崏瀹夊叏璺濈", displaySafetyDistance));
@@ -162,7 +183,12 @@
String existingPath = prepareCoordinateForEditor(dikuai.getPlannedPath());
String pathSeed = initialGeneratedPath != null ? initialGeneratedPath : existingPath;
pathArea = createInfoTextArea(pathSeed != null ? pathSeed : "", true, 10);
- contentPanel.add(createTextAreaSection("鍓茶崏璺緞鍧愭爣", pathArea));
+ String pathTitle = "鍓茶崏璺緞鍧愭爣";
+ if (pathSeed != null && !pathSeed.trim().isEmpty() && !"-1".equals(pathSeed.trim())) {
+ int pathCount = pathSeed.split(";").length;
+ pathTitle = "鍓茶崏璺緞鍧愭爣 (" + pathCount + "鐐�)";
+ }
+ contentPanel.add(createTextAreaSection(pathTitle, pathArea));
JScrollPane dialogScrollPane = new JScrollPane(contentPanel);
dialogScrollPane.setBorder(BorderFactory.createEmptyBorder());
@@ -224,30 +250,35 @@
* 棰勮璺緞
*/
private void previewPath() {
- // 鍏堜繚瀛樺綋鍓嶈矾寰勫埌鍦板潡锛堜复鏃朵繚瀛橈紝鐢ㄤ簬棰勮锛�
- String pathNormalized = normalizeCoordinateInput(pathArea.getText());
+ // 鐩存帴浠庢枃鏈煙鑾峰彇璺緞鏁版嵁
+ String rawPath = pathArea.getText();
+ String pathNormalized = normalizeCoordinateInput(rawPath);
+
if (!"-1".equals(pathNormalized)) {
+ // 瑙勮寖鍖栬矾寰勬暟鎹細鏀寔鎹㈣銆佺┖鏍肩瓑鍒嗛殧绗�
pathNormalized = pathNormalized
.replace("\r\n", ";")
.replace('\r', ';')
.replace('\n', ';')
- .replaceAll(";+", ";")
- .replaceAll("\\s*;\\s*", ";")
- .trim();
+ .replaceAll("\\s+", ";") // 灏嗘墍鏈夌┖鐧藉瓧绗︽浛鎹负鍒嗗彿
+ .replaceAll(";+", ";"); // 鍚堝苟杩炵画鍒嗗彿
+
+ // 鍘婚櫎棣栧熬鍒嗗彿
+ if (pathNormalized.startsWith(";")) pathNormalized = pathNormalized.substring(1);
+ if (pathNormalized.endsWith(";")) pathNormalized = pathNormalized.substring(0, pathNormalized.length() - 1);
+
if (pathNormalized.isEmpty()) {
pathNormalized = "-1";
}
}
if ("-1".equals(pathNormalized)) {
- JOptionPane.showMessageDialog(this, "璇峰厛鐢熸垚鍓茶崏璺緞", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
+ JOptionPane.showMessageDialog(this, "璇峰厛鐢熸垚鍓茶崏璺緞鎴栧湪鏂囨湰妗嗕腑杈撳叆鏈夋晥鍧愭爣", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
return;
}
- // 涓存椂淇濆瓨璺緞鍒板湴鍧楀璞★紙涓嶆寔涔呭寲锛�
- if (saveCallback != null) {
- saveCallback.savePlannedPath(dikuai, pathNormalized);
- }
+ // 娉ㄦ剰锛氶瑙堟椂涓嶈嚜鍔ㄤ繚瀛樿矾寰勫埌鍦板潡锛屼粎浣跨敤鏂囨湰鍩熶腑鐨勬暟鎹繘琛岄瑙�
+ // 鍙湁鐐瑰嚮"淇濆瓨璺緞"鎸夐挳鏃舵墠鎸佷箙鍖栨暟鎹�
// 淇濆瓨褰撳墠椤甸潰鐘舵�侊紝鐢ㄤ簬杩斿洖鏃舵仮澶�
String currentBaseStation = baseStationField.getText();
@@ -264,12 +295,16 @@
String boundaryInput = normalizeCoordinateInput(boundaryArea.getText());
final String boundary;
if (!"-1".equals(boundaryInput)) {
- String processed = boundaryInput.replace("\r\n", ";")
+ String processed = boundaryInput
+ .replace("\r\n", ";")
.replace('\r', ';')
.replace('\n', ';')
- .replaceAll(";+", ";")
- .replaceAll("\\s*;\\s*", ";")
- .trim();
+ .replaceAll("\\s+", ";")
+ .replaceAll(";+", ";");
+
+ if (processed.startsWith(";")) processed = processed.substring(1);
+ if (processed.endsWith(";")) processed = processed.substring(0, processed.length() - 1);
+
if (processed.isEmpty()) {
boundary = dikuai.getBoundaryCoordinates();
} else {
@@ -372,7 +407,7 @@
String rawWidthInput = widthField.getText() != null ? widthField.getText().trim() : "";
String widthSanitized = sanitizeWidthString(widthField.getText());
if (widthSanitized == null) {
- String message = rawWidthInput.isEmpty() ? "璇峰厛璁剧疆鍓茶崏瀹藉害(鍘樼背)" : "鍓茶崏瀹藉害鏍煎紡涓嶆纭�";
+ String message = rawWidthInput.isEmpty() ? "璇峰厛璁剧疆鍓茶崏瀹藉害(cm)" : "鍓茶崏瀹藉害鏍煎紡涓嶆纭�";
JOptionPane.showMessageDialog(this, message, "鎻愮ず", JOptionPane.WARNING_MESSAGE);
return;
}
@@ -411,6 +446,21 @@
JOptionPane.showMessageDialog(this, "璇峰厛鐢熸垚鍓茶崏璺緞", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
return;
}
+
+ // 鏇存柊褰撳墠鍦板潡瀵硅薄鐨勫睘鎬�
+ if (dikuai != null) {
+ dikuai.setBaseStationCoordinates(baseStationNormalized);
+ dikuai.setBoundaryCoordinates(boundaryNormalized);
+ dikuai.setMowingWidth(widthNormalized);
+ dikuai.setPlannedPath(pathNormalized);
+ dikuai.setObstacleCoordinates(obstacleNormalized);
+
+ // 鑾峰彇骞舵洿鏂板畨鍏ㄨ窛绂�
+ String safetyDistance = getSafetyDistanceString();
+ if (safetyDistance != null) {
+ dikuai.setMowingSafetyDistance(safetyDistance);
+ }
+ }
// 璋冪敤鍥炶皟淇濆瓨鏁版嵁
if (saveCallback != null) {
@@ -434,10 +484,19 @@
JOptionPane.showMessageDialog(this, "鏃犳硶淇濆瓨鍓茶崏璺緞", "閿欒", JOptionPane.ERROR_MESSAGE);
return;
}
+
+ // 淇濆瓨瀹夊叏璺濈
+ String safetyDistance = getSafetyDistanceString();
+ if (safetyDistance != null) {
+ if (!saveCallback.saveMowingSafetyDistance(dikuai, safetyDistance)) {
+ JOptionPane.showMessageDialog(this, "鏃犳硶淇濆瓨鍓茶崏瀹夊叏璺濈", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ }
}
JOptionPane.showMessageDialog(this, "鍓茶崏璺緞宸蹭繚瀛�", "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
- dispose();
+ // dispose(); // 鐢ㄦ埛瑕佹眰淇濆瓨鍚庝笉鍏抽棴椤甸潰
}
/**
@@ -459,7 +518,7 @@
String widthStr = sanitizeWidthString(widthCmInput);
if (widthStr == null) {
if (showMessages) {
- String message = rawWidth.isEmpty() ? "璇峰厛璁剧疆鍓茶崏瀹藉害(鍘樼背)" : "鍓茶崏瀹藉害鏍煎紡涓嶆纭�";
+ String message = rawWidth.isEmpty() ? "璇峰厛璁剧疆鍓茶崏瀹藉害(cm)" : "鍓茶崏瀹藉害鏍煎紡涓嶆纭�";
JOptionPane.showMessageDialog(parentComponent, message, "鎻愮ず", JOptionPane.WARNING_MESSAGE);
}
return null;
@@ -496,7 +555,20 @@
String obstacles = sanitizeValueOrNull(obstacleInput);
if (obstacles != null) {
- obstacles = obstacles.replace("\r\n", " ").replace('\r', ' ').replace('\n', ' ');
+ // 鎸夌収鐢ㄦ埛瑕佹眰锛屽涓殰纰嶇墿涔嬮棿鐢� $ 绗﹀彿鍒嗛殧
+ // 濡傛灉杈撳叆涓寘鍚� $锛屽垯淇濈暀 $锛屽惁鍒欏皢鎹㈣绗︽浛鎹负 $
+ if (obstacles.contains("$")) {
+ // 宸茬粡鏄� $ 鍒嗛殧鐨勬牸寮忥紝鍙渶娓呯悊鎹㈣绗�
+ obstacles = obstacles.replace("\r\n", "").replace('\r', ' ').replace('\n', ' ');
+ } else {
+ // 灏濊瘯灏嗘崲琛岀杞崲涓� $锛屾垨鑰呭鏋滄槸涓�琛屽垯淇濇寔鍘熸牱
+ // 杩欓噷鍋囪鐢ㄦ埛鍙兘鐢ㄦ崲琛屽垎闅斿涓殰纰嶇墿
+ // 浣嗘牴鎹渶姹傛弿杩帮紝浼间箮杈撳叆鏈韩灏卞簲璇ユ槸 $ 鍒嗛殧鐨勶紝鎴栬�呮垜浠渶瑕佸鐞嗘垚 $ 鍒嗛殧
+ // 涓轰簡鍏煎鎬э紝濡傛灉鐢ㄦ埛杈撳叆鐨勬槸鎹㈣鍒嗛殧鐨勫涓殰纰嶇墿锛屾垜浠皢鍏惰浆鎹负 $ 鍒嗛殧
+ // 浣嗛�氬父闅滅鐗╁潗鏍囨槸涓�涓插潗鏍囩偣锛屽鏋滅敤鎴锋病鏈夋樉寮忕敤 $ 鍒嗛殧锛屾垜浠緢闅惧尯鍒嗘槸鍚屼竴涓殰纰嶇墿鐨勭偣杩樻槸澶氫釜闅滅鐗�
+ // 鍥犳锛岃繖閲屼富瑕佸鐞嗘竻鐞嗗伐浣滐紝鍏蜂綋鐨勮В鏋愰�昏緫鍦ㄥ悇瀹炵幇绫讳腑澶勭悊
+ obstacles = obstacles.replace("\r\n", " ").replace('\r', ' ').replace('\n', ' ');
+ }
}
// 鑾峰彇瀹夊叏璺濈
@@ -517,19 +589,10 @@
int grassType = shapeJudger.judgeGrassType(boundary);
// grassType: 0=鏃犳硶鍒ゆ柇, 1=鍑稿舰, 2=寮傚舰
- // 瑙f瀽闅滅鐗╁垪琛�
- List<List<Coordinate>> obstacleList = Lunjingguihua.parseObstacles(obstacles);
- if (obstacleList == null) {
- obstacleList = new ArrayList<>();
- }
-
- // 鍒ゆ柇鏄惁鏈夋湁鏁堢殑闅滅鐗╋細鍙湁褰撹В鏋愭垚鍔熶笖鍒楄〃涓嶄负绌烘椂锛屾墠璁や负鏈夐殰纰嶇墿
- boolean hasValidObstacles = !obstacleList.isEmpty();
-
String generated = null;
// 2. 鏍规嵁鍦板潡绫诲瀷鍜屾槸鍚︽湁闅滅鐗╋紝璋冪敤涓嶅悓鐨勮矾寰勭敓鎴愮被
- if (!hasValidObstacles) {
+ if (!hasObstacleInput) {
// 鏃犻殰纰嶇墿鐨勬儏鍐�
if (grassType == 1) {
// 鍑稿舰鍦板潡锛屾棤闅滅鐗� -> 璋冪敤 AoxinglujingNoObstacle
@@ -538,97 +601,43 @@
generated = formatAoxingPathSegments(segments);
} else if (grassType == 2) {
// 寮傚舰鍦板潡锛屾棤闅滅鐗� -> 璋冪敤 YixinglujingNoObstacle
- // 娉ㄦ剰锛氬鏋滆绫昏繕娌℃湁瀹炵幇锛岃繖閲屼細鎶涘嚭寮傚父鎴栬繑鍥瀗ull
- try {
- // 鍋囪 YixinglujingNoObstacle 鏈夌被浼肩殑鏂规硶绛惧悕
- // 濡傛灉绫昏繕娌℃湁瀹炵幇锛屽彲鑳介渶瑕佷娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
- generated = YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr);
- } catch (Exception e) {
- // 濡傛灉绫昏繕娌℃湁瀹炵幇锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
- if (showMessages) {
- System.err.println("YixinglujingNoObstacle 灏氭湭瀹炵幇锛屼娇鐢ㄩ粯璁ゆ柟娉�: " + e.getMessage());
- }
- generated = Lunjingguihua.generatePathFromStrings(
- boundary, obstacles != null ? obstacles : "", plannerWidth, safetyMarginStr, mode);
- }
+ // 璋冪敤 YixinglujingNoObstacle.planPath 鑾峰彇璺緞娈靛垪琛�
+ List<YixinglujingNoObstacle.PathSegment> segments =
+ YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr);
+ // 鏍煎紡鍖栬矾寰勬鍒楄〃涓哄瓧绗︿覆
+ generated = formatYixingPathSegments(segments);
} else {
- // 鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
+ // 鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛岄粯璁ゆ寜鍑稿舰澶勭悊鎴栨彁绀�
if (showMessages) {
- JOptionPane.showMessageDialog(parentComponent, "鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屼娇鐢ㄩ粯璁よ矾寰勭敓鎴愭柟娉�",
+ JOptionPane.showMessageDialog(parentComponent, "鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屽皾璇曟寜鍑稿舰鍦板潡澶勭悊",
"鎻愮ず", JOptionPane.WARNING_MESSAGE);
}
- generated = Lunjingguihua.generatePathFromStrings(
- boundary, obstacles != null ? obstacles : "", plannerWidth, safetyMarginStr, mode);
+ List<AoxinglujingNoObstacle.PathSegment> segments =
+ AoxinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr);
+ generated = formatAoxingPathSegments(segments);
}
} else {
// 鏈夐殰纰嶇墿鐨勬儏鍐�
if (grassType == 1) {
// 鍑稿舰鍦板潡锛屾湁闅滅鐗� -> 璋冪敤 AoxinglujingHaveObstacel
- try {
- // 鍋囪 AoxinglujingHaveObstacel 鏈夌被浼肩殑鏂规硶绛惧悕
- generated = AoxinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr);
- } catch (Exception e) {
- // 濡傛灉绫昏繕娌℃湁瀹炵幇锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
- if (showMessages) {
- System.err.println("AoxinglujingHaveObstacel 灏氭湭瀹炵幇锛屼娇鐢ㄩ粯璁ゆ柟娉�: " + e.getMessage());
- }
- List<Coordinate> polygon = Lunjingguihua.parseCoordinates(boundary);
- if (polygon.size() < 4) {
- if (showMessages) {
- JOptionPane.showMessageDialog(parentComponent, "澶氳竟褰㈠潗鏍囨暟閲忎笉瓒筹紝鑷冲皯闇�瑕佷笁涓偣",
- "閿欒", JOptionPane.ERROR_MESSAGE);
- }
- return null;
- }
- double safetyDistance = Double.parseDouble(safetyMarginStr);
- ObstaclePathPlanner pathPlanner = new ObstaclePathPlanner(
- polygon, widthMeters, mode, obstacleList, safetyDistance);
- List<Lunjingguihua.PathSegment> segments = pathPlanner.generate();
- generated = Lunjingguihua.formatPathSegments(segments);
- }
+ // 浼犲叆鍙傛暟锛歜oundary(A), obstacles(B), plannerWidth(C), safetyMarginStr(D)
+ List<AoxinglujingHaveObstacel.PathSegment> segments =
+ AoxinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr);
+ generated = formatAoxingHaveObstaclePathSegments(segments);
} else if (grassType == 2) {
// 寮傚舰鍦板潡锛屾湁闅滅鐗� -> 璋冪敤 YixinglujingHaveObstacel
- try {
- // 鍋囪 YixinglujingHaveObstacel 鏈夌被浼肩殑鏂规硶绛惧悕
- generated = YixinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr);
- } catch (Exception e) {
- // 濡傛灉绫昏繕娌℃湁瀹炵幇锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
- if (showMessages) {
- System.err.println("YixinglujingHaveObstacel 灏氭湭瀹炵幇锛屼娇鐢ㄩ粯璁ゆ柟娉�: " + e.getMessage());
- }
- List<Coordinate> polygon = Lunjingguihua.parseCoordinates(boundary);
- if (polygon.size() < 4) {
- if (showMessages) {
- JOptionPane.showMessageDialog(parentComponent, "澶氳竟褰㈠潗鏍囨暟閲忎笉瓒筹紝鑷冲皯闇�瑕佷笁涓偣",
- "閿欒", JOptionPane.ERROR_MESSAGE);
- }
- return null;
- }
- double safetyDistance = Double.parseDouble(safetyMarginStr);
- ObstaclePathPlanner pathPlanner = new ObstaclePathPlanner(
- polygon, widthMeters, mode, obstacleList, safetyDistance);
- List<Lunjingguihua.PathSegment> segments = pathPlanner.generate();
- generated = Lunjingguihua.formatPathSegments(segments);
- }
+ // 浼犲叆鍙傛暟锛歜oundary(A), obstacles(B), plannerWidth(C), safetyMarginStr(D)
+ // 娉ㄦ剰锛歒ixinglujingHaveObstacel.planPath 杩斿洖 String
+ generated = YixinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr);
} else {
- // 鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
+ // 鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛岄粯璁ゆ寜鍑稿舰澶勭悊鎴栨彁绀�
if (showMessages) {
- JOptionPane.showMessageDialog(parentComponent, "鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屼娇鐢ㄩ粯璁よ矾寰勭敓鎴愭柟娉�",
+ JOptionPane.showMessageDialog(parentComponent, "鏃犳硶鍒ゆ柇鍦板潡绫诲瀷锛屽皾璇曟寜鍑稿舰鍦板潡澶勭悊",
"鎻愮ず", JOptionPane.WARNING_MESSAGE);
}
- List<Coordinate> polygon = Lunjingguihua.parseCoordinates(boundary);
- if (polygon.size() < 4) {
- if (showMessages) {
- JOptionPane.showMessageDialog(parentComponent, "澶氳竟褰㈠潗鏍囨暟閲忎笉瓒筹紝鑷冲皯闇�瑕佷笁涓偣",
- "閿欒", JOptionPane.ERROR_MESSAGE);
- }
- return null;
- }
- double safetyDistance = Double.parseDouble(safetyMarginStr);
- ObstaclePathPlanner pathPlanner = new ObstaclePathPlanner(
- polygon, widthMeters, mode, obstacleList, safetyDistance);
- List<Lunjingguihua.PathSegment> segments = pathPlanner.generate();
- generated = Lunjingguihua.formatPathSegments(segments);
+ List<AoxinglujingHaveObstacel.PathSegment> segments =
+ AoxinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr);
+ generated = formatAoxingHaveObstaclePathSegments(segments);
}
}
@@ -662,25 +671,38 @@
/**
* 鑾峰彇瀹夊叏璺濈瀛楃涓诧紙绫筹級
+ * 浼樺厛浠嶥ikuai瀵硅薄鑾峰彇锛屽鏋淒ikuai涓病鏈夛紝鍐嶄粠Device鑾峰彇
*/
private String getSafetyDistanceString() {
- Device device = Device.getActiveDevice();
- if (device != null) {
- String safetyDistanceValue = device.getMowingSafetyDistance();
- if (safetyDistanceValue != null && !"-1".equals(safetyDistanceValue) && !safetyDistanceValue.trim().isEmpty()) {
- try {
- double distanceMeters = Double.parseDouble(safetyDistanceValue.trim());
- // 濡傛灉鍊煎ぇ浜�100锛岃涓烘槸鍘樼背锛岄渶瑕佽浆鎹负绫�
- if (distanceMeters > 100) {
- distanceMeters = distanceMeters / 100.0;
- }
- return BigDecimal.valueOf(distanceMeters)
- .setScale(3, RoundingMode.HALF_UP)
- .stripTrailingZeros()
- .toPlainString();
- } catch (NumberFormatException e) {
- // 瑙f瀽澶辫触锛岃繑鍥瀗ull锛屼娇鐢ㄩ粯璁ゅ��
+ String safetyDistanceValue = null;
+
+ // 棣栧厛灏濊瘯浠嶥ikuai瀵硅薄鑾峰彇
+ if (dikuai != null) {
+ safetyDistanceValue = dikuai.getMowingSafetyDistance();
+ }
+
+ // 濡傛灉Dikuai涓病鏈夛紝浠嶥evice鑾峰彇
+ if ((safetyDistanceValue == null || "-1".equals(safetyDistanceValue) || safetyDistanceValue.trim().isEmpty())) {
+ Device device = Device.getActiveDevice();
+ if (device != null) {
+ safetyDistanceValue = device.getMowingSafetyDistance();
+ }
+ }
+
+ // 鏍煎紡鍖栧苟杩斿洖
+ if (safetyDistanceValue != null && !"-1".equals(safetyDistanceValue) && !safetyDistanceValue.trim().isEmpty()) {
+ try {
+ double distanceMeters = Double.parseDouble(safetyDistanceValue.trim());
+ // 濡傛灉鍊煎ぇ浜�100锛岃涓烘槸鍘樼背锛岄渶瑕佽浆鎹负绫�
+ if (distanceMeters > 100) {
+ distanceMeters = distanceMeters / 100.0;
}
+ return BigDecimal.valueOf(distanceMeters)
+ .setScale(3, RoundingMode.HALF_UP)
+ .stripTrailingZeros()
+ .toPlainString();
+ } catch (NumberFormatException e) {
+ // 瑙f瀽澶辫触锛岃繑鍥瀗ull锛屼娇鐢ㄩ粯璁ゅ��
}
}
return null;
@@ -711,6 +733,54 @@
}
/**
+ * 鏍煎紡鍖� YixinglujingNoObstacle.PathSegment 鍒楄〃涓哄潗鏍囧瓧绗︿覆
+ */
+ private String formatYixingPathSegments(List<YixinglujingNoObstacle.PathSegment> segments) {
+ if (segments == null || segments.isEmpty()) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ YixinglujingNoObstacle.Point last = null;
+ for (YixinglujingNoObstacle.PathSegment segment : segments) {
+ // 鍙坊鍔犲壊鑽夊伐浣滄锛岃烦杩囪繃娓℃
+ if (segment.isMowing) {
+ // 濡傛灉璧风偣涓庝笂涓�涓粓鐐逛笉鍚岋紝娣诲姞璧风偣
+ if (last == null || !equalsYixingPoint(last, segment.start)) {
+ appendYixingPoint(sb, segment.start);
+ }
+ // 娣诲姞缁堢偣
+ appendYixingPoint(sb, segment.end);
+ last = segment.end;
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * 鏍煎紡鍖� AoxinglujingHaveObstacel.PathSegment 鍒楄〃涓哄潗鏍囧瓧绗︿覆
+ */
+ private String formatAoxingHaveObstaclePathSegments(List<AoxinglujingHaveObstacel.PathSegment> segments) {
+ if (segments == null || segments.isEmpty()) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ AoxinglujingHaveObstacel.Point last = null;
+ for (AoxinglujingHaveObstacel.PathSegment segment : segments) {
+ // 鍙坊鍔犲壊鑽夊伐浣滄锛岃烦杩囪繃娓℃
+ if (segment.isMowing) {
+ // 濡傛灉璧风偣涓庝笂涓�涓粓鐐逛笉鍚岋紝娣诲姞璧风偣
+ if (last == null || !equals2D(last, segment.start)) {
+ appendPoint(sb, segment.start);
+ }
+ // 娣诲姞缁堢偣
+ appendPoint(sb, segment.end);
+ last = segment.end;
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
* 姣旇緝涓や釜鐐规槸鍚︾浉鍚岋紙浣跨敤灏忕殑瀹瑰樊锛�
*/
private boolean equals2D(AoxinglujingNoObstacle.Point p1, AoxinglujingNoObstacle.Point p2) {
@@ -731,6 +801,48 @@
sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y));
}
+ /**
+ * 姣旇緝涓や釜 YixinglujingNoObstacle.Point 鏄惁鐩稿悓锛堜娇鐢ㄥ皬鐨勫宸級
+ */
+ private boolean equalsYixingPoint(YixinglujingNoObstacle.Point p1, YixinglujingNoObstacle.Point p2) {
+ if (p1 == null || p2 == null) {
+ return p1 == p2;
+ }
+ double tolerance = 1e-6;
+ return Math.abs(p1.x - p2.x) < tolerance && Math.abs(p1.y - p2.y) < tolerance;
+ }
+
+ /**
+ * 娣诲姞 YixinglujingNoObstacle.Point 鍒板瓧绗︿覆鏋勫缓鍣�
+ */
+ private void appendYixingPoint(StringBuilder sb, YixinglujingNoObstacle.Point point) {
+ if (sb.length() > 0) {
+ sb.append(";");
+ }
+ sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y));
+ }
+
+ /**
+ * 姣旇緝涓や釜 AoxinglujingHaveObstacel.Point 鏄惁鐩稿悓锛堜娇鐢ㄥ皬鐨勫宸級
+ */
+ private boolean equals2D(AoxinglujingHaveObstacel.Point p1, AoxinglujingHaveObstacel.Point p2) {
+ if (p1 == null || p2 == null) {
+ return p1 == p2;
+ }
+ double tolerance = 1e-6;
+ return Math.abs(p1.x - p2.x) < tolerance && Math.abs(p1.y - p2.y) < tolerance;
+ }
+
+ /**
+ * 娣诲姞 AoxinglujingHaveObstacel.Point 鍒板瓧绗︿覆鏋勫缓鍣�
+ */
+ private void appendPoint(StringBuilder sb, AoxinglujingHaveObstacel.Point point) {
+ if (sb.length() > 0) {
+ sb.append(";");
+ }
+ sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y));
+ }
+
// ========== UI杈呭姪鏂规硶 ==========
private JTextArea createInfoTextArea(String text, boolean editable, int rows) {
--
Gitblit v1.10.0