From dc0fb19555dc01bf873361c9cb6fc22fbfc9671d Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 23 十二月 2025 17:50:13 +0800
Subject: [PATCH] 异形无障碍坐标规划优化
---
src/lujing/MowingPathGenerationPage.java | 262 ++++++++++++++++++++++++++-------------------------
1 files changed, 133 insertions(+), 129 deletions(-)
diff --git a/src/lujing/MowingPathGenerationPage.java b/src/lujing/MowingPathGenerationPage.java
index 73a8fde..ad0dcc4 100644
--- a/src/lujing/MowingPathGenerationPage.java
+++ b/src/lujing/MowingPathGenerationPage.java
@@ -3,8 +3,6 @@
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.*;
-import java.awt.datatransfer.Clipboard;
-import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
@@ -18,6 +16,7 @@
import lujing.Qufenxingzhuang;
import lujing.AoxinglujingNoObstacle;
import lujing.YixinglujingNoObstacle;
+import publicway.Fuzhibutton;
import lujing.AoxinglujingHaveObstacel;
import lujing.YixinglujingHaveObstacel;
import org.locationtech.jts.geom.Coordinate;
@@ -134,24 +133,37 @@
// 鍓茶崏瀹藉害
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));
@@ -373,7 +385,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;
}
@@ -460,7 +472,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;
@@ -541,9 +553,11 @@
// 寮傚舰鍦板潡锛屾棤闅滅鐗� -> 璋冪敤 YixinglujingNoObstacle
// 娉ㄦ剰锛氬鏋滆绫昏繕娌℃湁瀹炵幇锛岃繖閲屼細鎶涘嚭寮傚父鎴栬繑鍥瀗ull
try {
- // 鍋囪 YixinglujingNoObstacle 鏈夌被浼肩殑鏂规硶绛惧悕
- // 濡傛灉绫昏繕娌℃湁瀹炵幇锛屽彲鑳介渶瑕佷娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
- generated = YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr);
+ // 璋冪敤 YixinglujingNoObstacle.planPath 鑾峰彇璺緞娈靛垪琛�
+ List<YixinglujingNoObstacle.PathSegment> segments =
+ YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr);
+ // 鏍煎紡鍖栬矾寰勬鍒楄〃涓哄瓧绗︿覆
+ generated = formatYixingPathSegments(segments);
} catch (Exception e) {
// 濡傛灉绫昏繕娌℃湁瀹炵幇锛屼娇鐢ㄥ師鏉ョ殑鏂规硶浣滀负鍚庡
if (showMessages) {
@@ -663,25 +677,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;
@@ -712,6 +739,30 @@
}
/**
+ * 鏍煎紡鍖� 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();
+ }
+
+ /**
* 姣旇緝涓や釜鐐规槸鍚︾浉鍚岋紙浣跨敤灏忕殑瀹瑰樊锛�
*/
private boolean equals2D(AoxinglujingNoObstacle.Point p1, AoxinglujingNoObstacle.Point p2) {
@@ -732,6 +783,27 @@
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));
+ }
+
// ========== UI杈呭姪鏂规硶 ==========
private JTextArea createInfoTextArea(String text, boolean editable, int rows) {
@@ -762,15 +834,18 @@
titleLabel.setForeground(TEXT_COLOR);
titlePanel.add(titleLabel, BorderLayout.WEST);
- // 鍒涘缓澶嶅埗鎸夐挳
- JButton copyButton = createCopyButton(title, () -> {
- String text = textArea.getText();
- if (text == null || text.trim().isEmpty()) {
- JOptionPane.showMessageDialog(this, title + " 鏈缃�", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- copyToClipboard(text, title);
- });
+ // 鍒涘缓澶嶅埗鎸夐挳锛堜娇鐢� Fuzhibutton锛�
+ JButton copyButton = Fuzhibutton.createCopyButton(
+ () -> {
+ String text = textArea.getText();
+ if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) {
+ return null; // 杩斿洖null浼氳Е鍙�"鏈缃�"鎻愮ず
+ }
+ return text;
+ },
+ "澶嶅埗" + title,
+ new Color(230, 250, 240)
+ );
titlePanel.add(copyButton, BorderLayout.EAST);
section.add(titlePanel, BorderLayout.NORTH);
@@ -812,15 +887,18 @@
titleLabel.setForeground(TEXT_COLOR);
titlePanel.add(titleLabel, BorderLayout.WEST);
- // 鍒涘缓澶嶅埗鎸夐挳
- JButton copyButton = createCopyButton(title, () -> {
- String text = textField.getText();
- if (text == null || text.trim().isEmpty()) {
- JOptionPane.showMessageDialog(this, title + " 鏈缃�", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
- copyToClipboard(text, title);
- });
+ // 鍒涘缓澶嶅埗鎸夐挳锛堜娇鐢� Fuzhibutton锛�
+ JButton copyButton = Fuzhibutton.createCopyButton(
+ () -> {
+ String text = textField.getText();
+ if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) {
+ return null; // 杩斿洖null浼氳Е鍙�"鏈缃�"鎻愮ず
+ }
+ return text;
+ },
+ "澶嶅埗" + title,
+ new Color(230, 250, 240)
+ );
titlePanel.add(copyButton, BorderLayout.EAST);
section.add(titlePanel, BorderLayout.NORTH);
@@ -962,80 +1040,6 @@
return "parallel";
}
- /**
- * 鍒涘缓澶嶅埗鎸夐挳
- */
- private JButton createCopyButton(String title, Runnable copyAction) {
- JButton copyButton = new JButton();
- Font titleFont = new Font("寰蒋闆呴粦", Font.BOLD, 14);
- FontMetrics metrics = getFontMetrics(titleFont);
- int iconSize = metrics.getHeight(); // 浣跨敤鏍囬瀛椾綋楂樺害浣滀负鍥炬爣澶у皬
-
- // 鍔犺浇澶嶅埗鍥炬爣
- ImageIcon copyIcon = null;
- ImageIcon successIcon = null;
- try {
- ImageIcon originalCopyIcon = new ImageIcon("image/fuzhi.png");
- Image scaledCopyImage = originalCopyIcon.getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
- copyIcon = new ImageIcon(scaledCopyImage);
-
- // 鍔犺浇鎴愬姛鍥炬爣
- ImageIcon originalSuccessIcon = new ImageIcon("image/fuzhisucc.png");
- Image scaledSuccessImage = originalSuccessIcon.getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
- successIcon = new ImageIcon(scaledSuccessImage);
- } catch (Exception e) {
- // 濡傛灉鍥剧墖鍔犺浇澶辫触锛屼娇鐢ㄦ枃鏈�
- copyButton.setText("澶嶅埗");
- copyButton.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 12));
- System.err.println("鏃犳硶鍔犺浇澶嶅埗鍥炬爣: " + e.getMessage());
- }
-
- final ImageIcon finalCopyIcon = copyIcon;
- final ImageIcon finalSuccessIcon = successIcon;
-
- copyButton.setIcon(finalCopyIcon);
- copyButton.setContentAreaFilled(false);
- copyButton.setBorder(null);
- copyButton.setFocusPainted(false);
- copyButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
- copyButton.setToolTipText("澶嶅埗" + title);
-
- // 娣诲姞鐐瑰嚮浜嬩欢
- copyButton.addActionListener(e -> {
- copyAction.run();
- // 澶嶅埗鎴愬姛鍚庡垏鎹㈠浘鏍�
- if (finalSuccessIcon != null) {
- copyButton.setIcon(finalSuccessIcon);
- // 1绉掑悗鎭㈠鍘熷浘鏍�
- Timer timer = new Timer(1000, evt -> {
- copyButton.setIcon(finalCopyIcon);
- });
- timer.setRepeats(false);
- timer.start();
- }
- });
-
- return copyButton;
- }
-
- /**
- * 澶嶅埗鏂囨湰鍒板壀璐存澘
- */
- private void copyToClipboard(String text, String title) {
- if (text == null || text.trim().isEmpty()) {
- JOptionPane.showMessageDialog(this, title + " 鏈缃�", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
- return;
- }
-
- try {
- StringSelection selection = new StringSelection(text);
- Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- clipboard.setContents(selection, selection);
- // 鍘绘帀鎴愬姛鎻愮ず寮圭獥
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(this, "澶嶅埗澶辫触: " + ex.getMessage(), "閿欒", JOptionPane.ERROR_MESSAGE);
- }
- }
}
--
Gitblit v1.10.0