From 13d032241e1a2938a8be4f64c9171e1240e9ea1e Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期一, 22 十二月 2025 18:50:42 +0800
Subject: [PATCH] 新增了边界管理页面和首页边界虚线功能
---
src/zhuye/celiangmoshi.java | 2
src/dikuai/ObstacleManagementPage.java | 2
src/dikuai/daohangyulan.java | 2
src/bianjie/shudongdraw.java | 2
src/zhangaiwu/AddDikuai.java | 60 ++
src/zhuye/MapRenderer.java | 71 +++
set.properties | 8
src/zhuye/ShouyeLauncher.java | 23 +
src/zhuye/Shouye.java | 84 +++
src/dikuai/Dikuai.java | 16
src/dikuai/Dikuaiguanli.java | 53 +-
src/lujing/AoxinglujingNoObstacle.java | 206 +++++---
src/lujing/MowingPathGenerationPage.java | 53 ++
src/lujing/ObstaclePathPlanner.java | 2
src/lujing/YixinglujingNoObstacle.java | 457 +++++++++++++++++++
src/zhuye/bianjiedrwa.java | 19
src/bianjie/BoundaryLengthDrawer.java | 2
src/dikuai/Dikuanbianjipage.java | 316 +++++++++++++
18 files changed, 1,248 insertions(+), 130 deletions(-)
diff --git a/set.properties b/set.properties
index b41aff2..b9cecfb 100644
--- a/set.properties
+++ b/set.properties
@@ -1,5 +1,5 @@
#Mower Configuration Properties - Updated
-#Mon Dec 22 13:47:46 CST 2025
+#Mon Dec 22 18:50:02 CST 2025
appVersion=-1
boundaryLengthVisible=false
currentWorkLandNumber=LAND2
@@ -8,12 +8,12 @@
handheldMarkerId=1872
idleTrailDurationSeconds=60
manualBoundaryDrawingMode=false
-mapScale=25.45
+mapScale=16.74
measurementModeEnabled=false
mowerId=860
serialAutoConnect=true
serialBaudRate=115200
serialPortName=COM15
simCardNumber=-1
-viewCenterX=-27.62
-viewCenterY=-15.01
+viewCenterX=4.67
+viewCenterY=0.55
diff --git a/src/bianjie/BoundaryLengthDrawer.java b/src/bianjie/BoundaryLengthDrawer.java
index c4fdafa..44381aa 100644
--- a/src/bianjie/BoundaryLengthDrawer.java
+++ b/src/bianjie/BoundaryLengthDrawer.java
@@ -139,3 +139,5 @@
+
+
diff --git a/src/bianjie/shudongdraw.java b/src/bianjie/shudongdraw.java
index e96c923..ec2a8eb 100644
--- a/src/bianjie/shudongdraw.java
+++ b/src/bianjie/shudongdraw.java
@@ -201,3 +201,5 @@
g2d.setStroke(originalStroke);
}
}
+
+
diff --git a/src/dikuai/Dikuai.java b/src/dikuai/Dikuai.java
index 12a4edd..8a9a589 100644
--- a/src/dikuai/Dikuai.java
+++ b/src/dikuai/Dikuai.java
@@ -17,6 +17,8 @@
private String landName;
// 杈圭晫鍘熷鍧愭爣
private String boundaryOriginalCoordinates;
+ // 杈圭晫鍘熷XY鍧愭爣锛堢浉瀵逛簬鍩哄噯绔欑殑XY鍧愭爣锛�
+ private String boundaryOriginalXY;
// 杈圭晫鍧愭爣锛堝瓨鍌ㄥ杈瑰舰鍧愭爣鐐归泦鍚堬級
private String boundaryCoordinates;
// 瑙勫垝璺緞锛堝瓨鍌ㄨ矾寰勫潗鏍囩偣闆嗗悎锛�
@@ -101,6 +103,7 @@
dikuai.userId = landProps.getProperty("userId", "-1");
dikuai.landName = landProps.getProperty("landName", "-1");
dikuai.boundaryOriginalCoordinates = landProps.getProperty("boundaryOriginalCoordinates", "-1");
+ dikuai.boundaryOriginalXY = landProps.getProperty("boundaryOriginalXY", "-1");
dikuai.boundaryCoordinates = landProps.getProperty("boundaryCoordinates", "-1");
dikuai.plannedPath = landProps.getProperty("plannedPath", "-1");
dikuai.returnPointCoordinates = landProps.getProperty("returnPointCoordinates", "-1");
@@ -201,6 +204,9 @@
case "boundaryOriginalCoordinates":
this.boundaryOriginalCoordinates = value;
return true;
+ case "boundaryOriginalXY":
+ this.boundaryOriginalXY = value;
+ return true;
case "boundaryCoordinates":
this.boundaryCoordinates = value;
return true;
@@ -276,6 +282,7 @@
if (dikuai.landNumber != null) properties.setProperty(landNumber + ".landNumber", dikuai.landNumber);
if (dikuai.landName != null) properties.setProperty(landNumber + ".landName", dikuai.landName);
if (dikuai.boundaryOriginalCoordinates != null) properties.setProperty(landNumber + ".boundaryOriginalCoordinates", dikuai.boundaryOriginalCoordinates);
+ if (dikuai.boundaryOriginalXY != null) properties.setProperty(landNumber + ".boundaryOriginalXY", dikuai.boundaryOriginalXY);
if (dikuai.boundaryCoordinates != null) properties.setProperty(landNumber + ".boundaryCoordinates", dikuai.boundaryCoordinates);
if (dikuai.plannedPath != null) properties.setProperty(landNumber + ".plannedPath", dikuai.plannedPath);
if (dikuai.returnPointCoordinates != null) properties.setProperty(landNumber + ".returnPointCoordinates", dikuai.returnPointCoordinates);
@@ -341,6 +348,14 @@
this.boundaryOriginalCoordinates = boundaryOriginalCoordinates;
}
+ public String getBoundaryOriginalXY() {
+ return boundaryOriginalXY;
+ }
+
+ public void setBoundaryOriginalXY(String boundaryOriginalXY) {
+ this.boundaryOriginalXY = boundaryOriginalXY;
+ }
+
public String getBoundaryCoordinates() {
return boundaryCoordinates;
}
@@ -492,6 +507,7 @@
", landNumber='" + landNumber + '\'' +
", landName='" + landName + '\'' +
", boundaryOriginalCoordinates='" + boundaryOriginalCoordinates + '\'' +
+ ", boundaryOriginalXY='" + boundaryOriginalXY + '\'' +
", boundaryCoordinates='" + boundaryCoordinates + '\'' +
", plannedPath='" + plannedPath + '\'' +
", returnPointCoordinates='" + returnPointCoordinates + '\'' +
diff --git a/src/dikuai/Dikuaiguanli.java b/src/dikuai/Dikuaiguanli.java
index 70bc5b1..ce8aafb 100644
--- a/src/dikuai/Dikuaiguanli.java
+++ b/src/dikuai/Dikuaiguanli.java
@@ -337,8 +337,9 @@
contentPanel.add(obstaclePanel);
contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));
- // 鍦板潡杈圭晫鍧愭爣锛堝甫鏄剧ず椤剁偣鎸夐挳锛�
- JPanel boundaryPanel = createBoundaryInfoItem(dikuai);
+ // 鍦板潡杈圭晫鍧愭爣锛堝甫鏌ョ湅鎸夐挳锛�
+ JPanel boundaryPanel = createCardInfoItemWithIconButton("鍦板潡杈圭晫:",
+ createViewButton(e -> editBoundaryCoordinates(dikuai)));
configureInteractiveLabel(getInfoItemTitleLabel(boundaryPanel),
() -> editBoundaryCoordinates(dikuai),
"鐐瑰嚮鏌ョ湅/缂栬緫鍦板潡杈圭晫鍧愭爣");
@@ -362,14 +363,6 @@
contentPanel.add(baseStationPanel);
contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));
- JPanel boundaryOriginalPanel = createCardInfoItemWithIconButton("杈圭晫鍘熷鍧愭爣:",
- createViewButton(e -> editBoundaryOriginalCoordinates(dikuai)));
- configureInteractiveLabel(getInfoItemTitleLabel(boundaryOriginalPanel),
- () -> editBoundaryOriginalCoordinates(dikuai),
- "鐐瑰嚮鏌ョ湅/缂栬緫杈圭晫鍘熷鍧愭爣");
- contentPanel.add(boundaryOriginalPanel);
- contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));
-
JPanel completedTrackPanel = createCardInfoItemWithButton("宸插畬鎴愬壊鑽夎矾寰�:",
getTruncatedValue(dikuai.getMowingTrack(), 12, "鏈褰�"),
createViewButton(e -> showCompletedMowingTrackDialog(dikuai)));
@@ -1035,17 +1028,35 @@
if (dikuai == null) {
return;
}
- String edited = promptCoordinateEditing("鏌ョ湅 / 缂栬緫鍦板潡杈圭晫鍧愭爣", dikuai.getBoundaryCoordinates());
+ Window owner = SwingUtilities.getWindowAncestor(this);
+
+ // 鑾峰彇鍦板潡绠$悊瀵硅瘽妗嗭紝鍑嗗鍦ㄦ墦寮�杈圭晫缂栬緫椤甸潰鏃跺叧闂�
+ Window managementWindow = null;
+ if (owner instanceof JDialog) {
+ managementWindow = owner;
+ }
+
+ // 鎵撳紑杈圭晫缂栬緫椤甸潰
+ Dikuanbianjipage page = new Dikuanbianjipage(owner, "鍦板潡杈圭晫绠$悊椤甸潰", dikuai.getBoundaryCoordinates(), dikuai);
+ page.setVisible(true);
+
+ // 鍏抽棴鍦板潡绠$悊椤甸潰
+ if (managementWindow != null) {
+ managementWindow.dispose();
+ }
+
+ // 鑾峰彇缂栬緫缁撴灉骞朵繚瀛�
+ String edited = page.getResult();
if (edited == null) {
return;
}
String normalized = normalizeCoordinateInput(edited);
if (!saveFieldAndRefresh(dikuai, "boundaryCoordinates", normalized)) {
- JOptionPane.showMessageDialog(this, "鏃犳硶鏇存柊鍦板潡杈圭晫鍧愭爣", "閿欒", JOptionPane.ERROR_MESSAGE);
+ JOptionPane.showMessageDialog(null, "鏃犳硶鏇存柊鍦板潡杈圭晫鍧愭爣", "閿欒", JOptionPane.ERROR_MESSAGE);
return;
}
String message = "-1".equals(normalized) ? "鍦板潡杈圭晫鍧愭爣宸叉竻绌�" : "鍦板潡杈圭晫鍧愭爣宸叉洿鏂�";
- JOptionPane.showMessageDialog(this, message, "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
+ JOptionPane.showMessageDialog(null, message, "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
}
private void editPlannedPath(Dikuai dikuai) {
@@ -1082,22 +1093,6 @@
JOptionPane.showMessageDialog(this, message, "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
}
- private void editBoundaryOriginalCoordinates(Dikuai dikuai) {
- if (dikuai == null) {
- return;
- }
- String edited = promptCoordinateEditing("鏌ョ湅 / 缂栬緫杈圭晫鍘熷鍧愭爣", dikuai.getBoundaryOriginalCoordinates());
- if (edited == null) {
- return;
- }
- String normalized = normalizeCoordinateInput(edited);
- if (!saveFieldAndRefresh(dikuai, "boundaryOriginalCoordinates", normalized)) {
- JOptionPane.showMessageDialog(this, "鏃犳硶鏇存柊杈圭晫鍘熷鍧愭爣", "閿欒", JOptionPane.ERROR_MESSAGE);
- return;
- }
- String message = "-1".equals(normalized) ? "杈圭晫鍘熷鍧愭爣宸叉竻绌�" : "杈圭晫鍘熷鍧愭爣宸叉洿鏂�";
- JOptionPane.showMessageDialog(this, message, "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
- }
private void editMowingPattern(Dikuai dikuai) {
if (dikuai == null) {
diff --git a/src/dikuai/Dikuanbianjipage.java b/src/dikuai/Dikuanbianjipage.java
new file mode 100644
index 0000000..bceb6c2
--- /dev/null
+++ b/src/dikuai/Dikuanbianjipage.java
@@ -0,0 +1,316 @@
+package dikuai;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.*;
+import java.util.ArrayList;
+import java.util.List;
+
+import bianjie.bianjieguihua2;
+import publicway.Fuzhibutton;
+import zhuye.Coordinate;
+import zhuye.Shouye;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 鍦板潡杈圭晫绠$悊椤甸潰
+ * 鏄剧ず锛氬師濮嬭竟鐣屽潗鏍囷紙缁忕含搴︺�侀珮绋嬶級銆佸師濮嬭竟鐣孹Y锛堢浉瀵逛簬鍩哄噯绔欙級銆佷互鍙婂彲缂栬緫鐨勪紭鍖栧悗杈圭晫鍧愭爣
+ */
+public class Dikuanbianjipage extends JDialog {
+ private static final long serialVersionUID = 1L;
+ private static final int SCREEN_WIDTH = 400;
+ private static final int SCREEN_HEIGHT = 800;
+ private static final Color PRIMARY_COLOR = new Color(46, 139, 87);
+ private static final Color PRIMARY_DARK = new Color(30, 107, 69);
+ private static final Color TEXT_COLOR = new Color(51, 51, 51);
+ private static final Color WHITE = Color.WHITE;
+ private static final Color BORDER_COLOR = new Color(200, 200, 200);
+ private static final Color BACKGROUND_COLOR = new Color(250, 250, 250);
+
+ private String result = null;
+ private Dikuai dikuai;
+
+ public Dikuanbianjipage(Window owner, String title, String initialValue, Dikuai dikuai) {
+ super(owner, title, Dialog.ModalityType.APPLICATION_MODAL);
+ this.dikuai = dikuai;
+ initializeUI(title, initialValue, dikuai);
+ }
+
+ public String getResult() {
+ return result;
+ }
+
+ private void initializeUI(String title, String initialValue, Dikuai dikuai) {
+ setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+ getContentPane().setLayout(new BorderLayout());
+ getContentPane().setBackground(BACKGROUND_COLOR);
+
+ JPanel contentPanel = new JPanel();
+ contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
+ contentPanel.setBackground(BACKGROUND_COLOR);
+ contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 16, 12, 16));
+
+ String landName = dikuai != null ? (dikuai.getLandName() != null ? dikuai.getLandName() : "鏈煡鍦板潡") : "鏈煡鍦板潡";
+ String landNumber = dikuai != null ? (dikuai.getLandNumber() != null ? dikuai.getLandNumber() : "鏈煡缂栧彿") : "鏈煡缂栧彿";
+ JLabel headerLabel = new JLabel(landName + " / " + landNumber);
+ headerLabel.setFont(new Font("寰蒋闆呴粦", Font.BOLD, 16));
+ headerLabel.setForeground(TEXT_COLOR);
+ headerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
+ contentPanel.add(headerLabel);
+ contentPanel.add(Box.createVerticalStrut(12));
+
+ // 鍘熷杈圭晫鍧愭爣锛堢粡绾害, 楂樼▼锛�
+ String rawCoords = dikuai != null ? prepareCoordinateForEditor(dikuai.getBoundaryOriginalCoordinates()) : "";
+ int rawCount = 0;
+ if (rawCoords != null && !rawCoords.isEmpty() && !"-1".equals(rawCoords)) {
+ rawCount = rawCoords.split(";").length;
+ }
+ JTextArea rawTextArea = createInfoTextArea(rawCoords, false, 6);
+ contentPanel.add(createTextAreaSection("鍘熷杈圭晫鍧愭爣 (缁忓害, 绾害, 楂樼▼) (" + rawCount + "鐐�)", rawTextArea));
+
+ // 鍘熷杈圭晫XY鍧愭爣锛堢浉瀵逛簬鍩哄噯绔欙級
+ String rawXY = dikuai != null ? prepareCoordinateForEditor(dikuai.getBoundaryOriginalXY()) : "";
+ int xyCount = 0;
+ if (rawXY != null && !rawXY.isEmpty() && !"-1".equals(rawXY)) {
+ xyCount = rawXY.split(";").length;
+ }
+ JTextArea rawXYArea = createInfoTextArea(rawXY, false, 6);
+ contentPanel.add(createTextAreaSection("鍘熷杈圭晫XY鍧愭爣锛堢浉瀵逛簬鍩哄噯绔欙級 (" + xyCount + "鐐�)", rawXYArea));
+
+ // 浼樺寲鍚庤竟鐣屽潗鏍囷紙鍙紪杈戯級
+ String optCoords = prepareCoordinateForEditor(initialValue);
+ int optCount = 0;
+ if (optCoords != null && !optCoords.isEmpty() && !"-1".equals(optCoords)) {
+ optCount = optCoords.split(";").length;
+ }
+ JTextArea optTextArea = createInfoTextArea(optCoords, true, 6);
+ contentPanel.add(createTextAreaSection("浼樺寲鍚庡湴鍧楄竟鐣屽潗鏍� (" + optCount + "鐐�)", optTextArea));
+
+ JScrollPane dialogScrollPane = new JScrollPane(contentPanel);
+ dialogScrollPane.setBorder(BorderFactory.createEmptyBorder());
+ dialogScrollPane.getVerticalScrollBar().setUnitIncrement(16);
+ add(dialogScrollPane, BorderLayout.CENTER);
+
+ JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 12, 12));
+ buttonPanel.setBackground(BACKGROUND_COLOR);
+
+ JButton optimizeBtn = createPrimaryFooterButton("浼樺寲杈圭晫鍧愭爣");
+ JButton saveBtn = createPrimaryFooterButton("淇濆瓨");
+ JButton previewBtn = createPrimaryFooterButton("棰勮");
+ JButton closeBtn = createPrimaryFooterButton("鍏抽棴");
+
+ optimizeBtn.addActionListener(e -> {
+ if (dikuai == null) {
+ JOptionPane.showMessageDialog(this, "鏈壘鍒板湴鍧椾俊鎭紝鏃犳硶浼樺寲", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ String baseStation = dikuai.getBaseStationCoordinates();
+ if (baseStation == null || baseStation.trim().isEmpty() || "-1".equals(baseStation)) {
+ JOptionPane.showMessageDialog(this, "鍩虹珯鍧愭爣鏈缃紝鏃犳硶浼樺寲", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+
+ // 鍑嗗 Coordinate 鍒楄〃
+ String originalCoords = dikuai.getBoundaryOriginalCoordinates();
+ if (originalCoords == null || originalCoords.trim().isEmpty() || "-1".equals(originalCoords)) {
+ JOptionPane.showMessageDialog(this, "鍘熷杈圭晫鍧愭爣涓虹┖锛屾棤娉曚紭鍖�", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+
+ try {
+ // 瑙f瀽鍘熷鍧愭爣鍒� Coordinate.coordinates
+ List<Coordinate> coords = new ArrayList<>();
+ String[] points = originalCoords.split(";");
+ for (String point : points) {
+ String[] parts = point.split(",");
+ if (parts.length >= 2) {
+ double lonDecimal = Double.parseDouble(parts[0].trim());
+ double latDecimal = Double.parseDouble(parts[1].trim());
+ double alt = parts.length > 2 ? Double.parseDouble(parts[2].trim()) : 0.0;
+
+ // 灏嗗崄杩涘埗搴﹁浆鎹负搴﹀垎鏍煎紡瀛楃涓�
+ String latDM = decimalToDegreeMinute(latDecimal);
+ String lonDM = decimalToDegreeMinute(lonDecimal);
+ String latDir = latDecimal >= 0 ? "N" : "S";
+ String lonDir = lonDecimal >= 0 ? "E" : "W";
+
+ coords.add(new Coordinate(latDM, latDir, lonDM, lonDir, alt));
+ }
+ }
+ Coordinate.coordinates = coords;
+
+ // 璋冪敤浼樺寲绠楁硶
+ String optimized = bianjieguihua2.processCoordinateListAuto(baseStation);
+ optTextArea.setText(optimized);
+ JOptionPane.showMessageDialog(this, "杈圭晫浼樺寲瀹屾垚", "鎻愮ず", JOptionPane.INFORMATION_MESSAGE);
+ } catch (Exception ex) {
+ JOptionPane.showMessageDialog(this, "浼樺寲澶辫触: " + ex.getMessage(), "閿欒", JOptionPane.ERROR_MESSAGE);
+ ex.printStackTrace();
+ }
+ });
+
+ previewBtn.addActionListener(e -> {
+ if (dikuai == null) {
+ return;
+ }
+ // 鍏抽棴褰撳墠瀵硅瘽妗�
+ dispose();
+
+ // 鑾峰彇褰撳墠浼樺寲鍚庣殑杈圭晫
+ String currentOptimized = optTextArea.getText();
+
+ // 璋冪敤棣栭〉鏄剧ず棰勮
+ SwingUtilities.invokeLater(() -> {
+ Shouye.showBoundaryPreview(dikuai, currentOptimized, () -> {
+ // 杩斿洖鍥炶皟锛氶噸鏂版墦寮�姝ら〉闈�
+ new Dikuanbianjipage(getOwner(), getTitle(), currentOptimized, dikuai).setVisible(true);
+ });
+ });
+ });
+
+ saveBtn.addActionListener(e -> {
+ if (dikuai == null) {
+ JOptionPane.showMessageDialog(this, "鏈壘鍒板湴鍧椾俊鎭紝鏃犳硶淇濆瓨", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ String currentText = optTextArea.getText();
+ if (currentText == null || currentText.trim().isEmpty() || "-1".equals(currentText.trim())) {
+ JOptionPane.showMessageDialog(this, "浼樺寲鍚庡湴鍧楄竟鐣屼负绌猴紝鏃犳硶淇濆瓨", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
+ return;
+ }
+ String trimmed = currentText.trim();
+ // 淇濆瓨鍒板湴鍧楀璞�
+ if (!Dikuai.updateField(dikuai.getLandNumber(), "boundaryCoordinates", trimmed)) {
+ JOptionPane.showMessageDialog(this, "鏃犳硶鏇存柊鍦板潡杈圭晫鍧愭爣", "閿欒", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ Dikuai.updateField(dikuai.getLandNumber(), "updateTime", getCurrentTime());
+ Dikuai.saveToProperties();
+ this.result = trimmed;
+ JOptionPane.showMessageDialog(this, "鍦板潡杈圭晫鍧愭爣宸叉洿鏂�", "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
+ dispose();
+ });
+
+ closeBtn.addActionListener(e -> dispose());
+
+ // 鎸夐挳椤哄簭锛氫紭鍖栬竟鐣屽潗鏍囥�佷繚瀛樸�侀瑙堛�佸叧闂�
+ buttonPanel.add(optimizeBtn);
+ buttonPanel.add(saveBtn);
+ buttonPanel.add(previewBtn);
+ buttonPanel.add(closeBtn);
+ add(buttonPanel, BorderLayout.SOUTH);
+
+ pack();
+ setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
+ setLocationRelativeTo(getOwner());
+ }
+
+ private String prepareCoordinateForEditor(String value) {
+ if (value == null) {
+ return "";
+ }
+ String trimmed = value.trim();
+ if (trimmed.isEmpty() || "-1".equals(trimmed)) {
+ return "";
+ }
+ return trimmed;
+ }
+
+ private JTextArea createInfoTextArea(String text, boolean editable, int rows) {
+ JTextArea area = new JTextArea(text);
+ area.setEditable(editable);
+ area.setLineWrap(true);
+ area.setWrapStyleWord(true);
+ area.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 13));
+ area.setRows(Math.max(rows, 2));
+ area.setCaretPosition(0);
+ area.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
+ area.setBackground(editable ? WHITE : new Color(245, 245, 245));
+ return area;
+ }
+
+ private JPanel createTextAreaSection(String title, JTextArea textArea) {
+ JPanel section = new JPanel(new BorderLayout(0, 6));
+ section.setBackground(BACKGROUND_COLOR);
+ section.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+ JPanel titlePanel = new JPanel(new BorderLayout());
+ titlePanel.setBackground(BACKGROUND_COLOR);
+ titlePanel.setOpaque(false);
+
+ JLabel titleLabel = new JLabel(title);
+ titleLabel.setFont(new Font("寰蒋闆呴粦", Font.BOLD, 14));
+ titleLabel.setForeground(TEXT_COLOR);
+ titlePanel.add(titleLabel, BorderLayout.WEST);
+
+ JButton copyButton = Fuzhibutton.createCopyButton(
+ () -> {
+ String text = textArea.getText();
+ if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) {
+ return null;
+ }
+ return text;
+ },
+ "澶嶅埗",
+ new Color(230, 250, 240)
+ );
+ copyButton.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 12));
+ copyButton.setPreferredSize(new Dimension(50, 24));
+ copyButton.setMargin(new Insets(0,0,0,0));
+
+ titlePanel.add(copyButton, BorderLayout.EAST);
+
+ section.add(titlePanel, BorderLayout.NORTH);
+
+ JScrollPane scrollPane = new JScrollPane(textArea);
+ scrollPane.setBorder(BorderFactory.createLineBorder(BORDER_COLOR));
+ scrollPane.getVerticalScrollBar().setUnitIncrement(12);
+ section.add(scrollPane, BorderLayout.CENTER);
+
+ section.setBorder(BorderFactory.createEmptyBorder(4, 0, 12, 0));
+ return section;
+ }
+
+ private JButton createPrimaryFooterButton(String text) {
+ JButton button = new JButton(text);
+ button.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 12));
+ button.setBackground(PRIMARY_COLOR);
+ button.setForeground(WHITE);
+ button.setBorder(BorderFactory.createEmptyBorder(6, 12, 6, 12));
+ button.setFocusPainted(false);
+ button.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ button.addMouseListener(new java.awt.event.MouseAdapter() {
+ public void mouseEntered(java.awt.event.MouseEvent e) {
+ button.setBackground(PRIMARY_DARK);
+ }
+
+ public void mouseExited(java.awt.event.MouseEvent e) {
+ button.setBackground(PRIMARY_COLOR);
+ }
+ });
+
+ return button;
+ }
+
+ private String getCurrentTime() {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ return sdf.format(new Date());
+ }
+
+ /**
+ * 灏嗗崄杩涘埗搴﹁浆鎹负搴﹀垎鏍煎紡瀛楃涓�
+ * 渚嬪锛�39.831468 -> "3949.888080" (39搴�49.888080鍒�)
+ *
+ * @param decimalDegrees 鍗佽繘鍒跺害
+ * @return 搴﹀垎鏍煎紡瀛楃涓�
+ */
+ private String decimalToDegreeMinute(double decimalDegrees) {
+ double absDecimal = Math.abs(decimalDegrees);
+ int degrees = (int) Math.floor(absDecimal);
+ double minutes = (absDecimal - degrees) * 60.0;
+ double degreeMinutes = degrees * 100.0 + minutes;
+ return String.format(java.util.Locale.US, "%.8f", degreeMinutes);
+ }
+}
diff --git a/src/dikuai/ObstacleManagementPage.java b/src/dikuai/ObstacleManagementPage.java
index 45e5d7e..9a9dd25 100644
--- a/src/dikuai/ObstacleManagementPage.java
+++ b/src/dikuai/ObstacleManagementPage.java
@@ -984,3 +984,5 @@
+
+
diff --git a/src/dikuai/daohangyulan.java b/src/dikuai/daohangyulan.java
index 2db9acb..edfc655 100644
--- a/src/dikuai/daohangyulan.java
+++ b/src/dikuai/daohangyulan.java
@@ -546,3 +546,5 @@
return isNavigating;
}
}
+
+
diff --git a/src/lujing/AoxinglujingNoObstacle.java b/src/lujing/AoxinglujingNoObstacle.java
index 75717ba..1b13c98 100644
--- a/src/lujing/AoxinglujingNoObstacle.java
+++ b/src/lujing/AoxinglujingNoObstacle.java
@@ -1,14 +1,19 @@
package lujing;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
- * 鏃犻殰纰嶇墿鍑稿舰鑽夊湴璺緞瑙勫垝绫� (浼樺寲鐗�)
+ * 鏃犻殰纰嶇墿鍑稿舰鑽夊湴璺緞瑙勫垝绫� (缁堟瀬浼樺寲鐗�)
+ * 鐗规�э細
+ * 1. 鏈�灏忔姇褰卞搴︽柟鍚戦�夋嫨 (鏁堢巼鏈�楂橈紝杞集鏈�灏�)
+ * 2. 杈圭紭杞粨浼樺厛鍒囧壊 (鏃犳瑙掕鐩�)
+ * 3. 鏀寔澶栭儴浼犲叆宸插寘鍚噸鍙犵巼鐨勫搴﹀弬鏁�
*/
public class AoxinglujingNoObstacle {
- // 浼樺寲锛氬紩鍏ユ瀬灏忓�肩敤浜庢诞鐐规暟姣旇緝锛屽鐞嗗嚑浣曠簿搴﹁宸�
+ // 寮曞叆鏋佸皬鍊肩敤浜庢诞鐐规暟姣旇緝锛屽鐞嗗嚑浣曠簿搴﹁宸�
private static final double EPSILON = 1e-6;
/**
@@ -49,15 +54,15 @@
}
/**
- * 瀵瑰鍏紑鐨勯潤鎬佽皟鐢ㄦ柟娉� (淇濈暀瀛楃涓插叆鍙傛牸寮�)
+ * 瀵瑰鍏紑鐨勯潤鎬佽皟鐢ㄦ柟娉�
*
* @param boundaryCoordsStr 鍦板潡杈圭晫鍧愭爣瀛楃涓� "x1,y1;x2,y2;..."
- * @param mowingWidthStr 鍓茶崏瀹藉害瀛楃涓诧紝濡� "0.34"
+ * @param mowingWidthStr 鏈夋晥鍓茶崏瀹藉害瀛楃涓� (宸插寘鍚噸鍙犵巼)锛屽 "0.30"
* @param safetyMarginStr 瀹夊叏杈硅窛瀛楃涓诧紝濡� "0.2"
* @return 璺緞娈靛垪琛�
*/
public static List<PathSegment> planPath(String boundaryCoordsStr, String mowingWidthStr, String safetyMarginStr) {
- // 1. 瑙f瀽鍙傛暟 (浼樺寲锛氬崟鐙彁鍙栬В鏋愰�昏緫)
+ // 1. 瑙f瀽鍙傛暟
List<Point> originalPolygon = parseCoords(boundaryCoordsStr);
double mowingWidth;
double safetyMargin;
@@ -74,85 +79,119 @@
}
/**
- * 鏍稿績绠楁硶閫昏緫 (寮虹被鍨嬪叆鍙傦紝渚夸簬娴嬭瘯鍜屽唴閮ㄨ皟鐢�)
+ * 鏍稿績绠楁硶閫昏緫
*/
- private static List<PathSegment> planPathCore(List<Point> originalPolygon, double mowingWidth, double safetyMargin) {
- // 浼樺寲锛氫笁瑙掑舰涔熸槸鍚堟硶鐨勫嚫澶氳竟褰紝闄愬埗鏀逛负灏忎簬3
+ private static List<PathSegment> planPathCore(List<Point> originalPolygon, double width, double safetyMargin) {
if (originalPolygon == null || originalPolygon.size() < 3) {
- throw new IllegalArgumentException("澶氳竟褰㈠潗鏍囩偣涓嶈冻3涓紝鏃犳硶鏋勬垚鏈夋晥鍖哄煙");
+ return new ArrayList<>(); // 鎴栨姏鍑哄紓甯革紝瑙嗕笟鍔¢渶姹傝�屽畾
}
// 纭繚澶氳竟褰㈡槸閫嗘椂閽堟柟鍚�
ensureCCW(originalPolygon);
- // 1. 鏍规嵁瀹夊叏杈硅窛鍐呯缉
- List<Point> shrunkPolygon = shrinkPolygon(originalPolygon, safetyMargin);
+ // 1. 鏍规嵁瀹夊叏杈硅窛鍐呯缉锛屽緱鍒板疄闄呬綔涓氬尯鍩�
+ List<Point> workAreaPolygon = shrinkPolygon(originalPolygon, safetyMargin);
- // 浼樺寲锛氬唴缂╁悗濡傛灉澶氳竟褰㈠け鏁堬紙渚嬪鍦板潡澶獎锛屽唴缂╁悗娑堝け锛夛紝鐩存帴杩斿洖绌鸿矾寰勶紝閬垮厤鍚庣画鎶ラ敊
- if (shrunkPolygon.size() < 3) {
- // 杩欓噷鍙互璁板綍鏃ュ織锛氬湴鍧楄繃灏忥紝鏃犳硶婊¤冻瀹夊叏璺濈浣滀笟
+ // 濡傛灉鍐呯缉鍚庡尯鍩熷け鏁堬紙濡傚湴鍧楀お灏忥級锛岃繑鍥炵┖璺緞
+ if (workAreaPolygon.size() < 3) {
return new ArrayList<>();
}
- // 2. 璁$畻鏈�闀胯竟瑙掑害 (浣滀负鎵弿鏂瑰悜)
- double angle = calculateLongestEdgeAngle(originalPolygon);
+ List<PathSegment> finalPath = new ArrayList<>();
- // 3. & 4. 鏃嬭浆鎵弿骞跺壀瑁�
- List<PathSegment> mowingLines = generateClippedMowingLines(shrunkPolygon, angle, mowingWidth);
+ // 2. [浼樺寲] 浼樺厛鐢熸垚杞粨璺緞 (Contour Pass)
+ // 娌夸綔涓氳竟鐣岃蛋涓�鍦堬紝纭繚杈圭紭鏁撮綈涓旀棤閬楁紡
+ addContourPath(workAreaPolygon, finalPath);
- // 5. 寮撳瓧褰㈣繛鎺�
- return connectPathSegments(mowingLines);
- }
+ // 3. [浼樺寲] 璁$畻鏈�浣虫壂鎻忚搴�
+ // 瀵绘壘璁╁杈瑰舰鎶曞奖楂樺害鏈�灏忕殑瑙掑害锛屼粠鑰屾渶灏忓寲杞集娆℃暟
+ double bestAngle = findOptimalScanAngle(workAreaPolygon);
- // ================= 鏍稿績绠楁硶杈呭姪鏂规硶 =================
+ // 4. 鐢熸垚鍐呴儴寮撳瓧褰㈣矾寰�
+ // 鐩存帴浣跨敤浼犲叆鐨� width (宸插寘鍚噸鍙犵巼)
+ List<PathSegment> zigZagPaths = generateClippedMowingLines(workAreaPolygon, bestAngle, width);
- private static List<Point> shrinkPolygon(List<Point> polygon, double margin) {
- List<Point> newPoints = new ArrayList<>();
- int n = polygon.size();
-
- for (int i = 0; i < n; i++) {
- Point p1 = polygon.get(i);
- Point p2 = polygon.get((i + 1) % n);
- Point p0 = polygon.get((i - 1 + n) % n);
-
- Line line1 = offsetLine(p1, p2, margin);
- Line line0 = offsetLine(p0, p1, margin);
-
- Point intersection = getIntersection(line0, line1);
+ // 5. 杩炴帴杞粨璺緞鍜屽紦瀛楀舰璺緞
+ if (!finalPath.isEmpty() && !zigZagPaths.isEmpty()) {
+ Point contourEnd = finalPath.get(finalPath.size() - 1).end;
+ Point zigzagStart = zigZagPaths.get(0).start;
- // 浼樺寲锛氬鍔犻潪绌哄垽鏂紝涓斿鏋滀氦鐐瑰紓甯歌繙锛堝皷瑙掓晥搴旓級锛屽疄闄呭伐绋嬩腑鍙兘闇�瑕佸垏瑙掑鐞�
- // 杩欓噷鏆備繚鐣欏熀纭�閫昏緫锛屼絾鍦ㄥ嚫澶氳竟褰腑閫氬父娌¢棶棰�
- if (intersection != null) {
- newPoints.add(intersection);
+ // 濡傛灉杞粨缁堢偣涓庡紦瀛楀舰璧风偣涓嶉噸鍚堬紝娣诲姞杩囨浮娈�
+ if (distanceSq(contourEnd, zigzagStart) > EPSILON) {
+ finalPath.add(new PathSegment(contourEnd, zigzagStart, false));
}
}
- return newPoints;
+
+ // 6. 鍚堝苟寮撳瓧褰㈣矾寰�
+ finalPath.addAll(connectPathSegments(zigZagPaths));
+
+ return finalPath;
}
- private static double calculateLongestEdgeAngle(List<Point> polygon) {
- double maxDistSq = -1;
- double angle = 0;
- int n = polygon.size();
+ // ================= 鏍稿績閫昏緫杈呭姪鏂规硶 =================
+ /**
+ * 娣诲姞杞粨璺緞 (鍥寸潃澶氳竟褰㈣蛋涓�鍦�)
+ */
+ private static void addContourPath(List<Point> polygon, List<PathSegment> path) {
+ int n = polygon.size();
for (int i = 0; i < n; i++) {
Point p1 = polygon.get(i);
Point p2 = polygon.get((i + 1) % n);
- double dx = p2.x - p1.x;
- double dy = p2.y - p1.y;
- double distSq = dx * dx + dy * dy;
+ path.add(new PathSegment(p1, p2, true));
+ }
+ }
- if (distSq > maxDistSq) {
- maxDistSq = distSq;
- angle = Math.atan2(dy, dx);
+ /**
+ * 瀵绘壘鏈�浼樻壂鎻忚搴� (鏈�灏忔姇褰遍珮搴︽硶)
+ */
+ private static double findOptimalScanAngle(List<Point> polygon) {
+ double minHeight = Double.MAX_VALUE;
+ double bestAngle = 0;
+ int n = polygon.size();
+
+ // 閬嶅巻姣忎竴鏉¤竟锛岃绠椾互璇ヨ竟涓衡�滃簳鈥濇椂锛屽杈瑰舰鐨勯珮搴�
+ for (int i = 0; i < n; i++) {
+ Point p1 = polygon.get(i);
+ Point p2 = polygon.get((i + 1) % n);
+
+ // 褰撳墠杈圭殑瑙掑害
+ double currentAngle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
+
+ // 璁$畻鍦ㄨ繖涓搴︿笅鐨勬姇褰遍珮搴�
+ double height = calculatePolygonHeightAtAngle(polygon, currentAngle);
+
+ if (height < minHeight) {
+ minHeight = height;
+ bestAngle = currentAngle;
}
}
- return angle;
+ return bestAngle;
+ }
+
+ /**
+ * 璁$畻澶氳竟褰㈠湪鐗瑰畾鏃嬭浆瑙掑害涓嬬殑Y杞存姇褰遍珮搴�
+ */
+ private static double calculatePolygonHeightAtAngle(List<Point> poly, double angle) {
+ double minY = Double.MAX_VALUE;
+ double maxY = -Double.MAX_VALUE;
+
+ double cos = Math.cos(-angle);
+ double sin = Math.sin(-angle);
+
+ for (Point p : poly) {
+ // 鍙渶璁$畻鏃嬭浆鍚庣殑Y鍧愭爣
+ double rotatedY = p.x * sin + p.y * cos;
+ if (rotatedY < minY) minY = rotatedY;
+ if (rotatedY > maxY) maxY = rotatedY;
+ }
+ return maxY - minY;
}
private static List<PathSegment> generateClippedMowingLines(List<Point> polygon, double angle, double width) {
List<PathSegment> segments = new ArrayList<>();
-
- // 鏃嬭浆鑷虫按骞�
+
+ // 鏃嬭浆澶氳竟褰㈣嚦姘村钩
List<Point> rotatedPoly = rotatePolygon(polygon, -angle);
double minY = Double.MAX_VALUE;
@@ -162,28 +201,27 @@
if (p.y > maxY) maxY = p.y;
}
- // 浼樺寲锛氳捣濮嬫壂鎻忕嚎澧炲姞涓�涓井灏忕殑鍋忕Щ閲� EPSILON
- // 閬垮厤鎵弿绾挎濂借惤鍦ㄩ《鐐逛笂锛屽鑷翠氦鐐瑰垽鏂�昏緫鍑虹幇浜屼箟鎬�
+ // 璧峰鎵弿绾夸綅缃細
+ // 浠� minY + width/2 寮�濮嬶紝鍥犱负涔嬪墠宸茬粡璧颁簡杞粨绾�(Contour Pass)銆�
+ // 杞粨绾胯礋璐f竻鐞嗚竟缂樺尯鍩燂紝鍐呴儴濉厖绾夸繚鎸� width 鐨勯棿璺濆嵆鍙��
+ // 鍔犱笂 EPSILON 闃叉娴偣鏁板垰濂借惤鍦ㄨ竟鐣屼笂瀵艰嚧鐨勫垽鏂宸�
double currentY = minY + width / 2.0 + EPSILON;
while (currentY < maxY) {
List<Double> xIntersections = new ArrayList<>();
int n = rotatedPoly.size();
-
+
for (int i = 0; i < n; i++) {
Point p1 = rotatedPoly.get(i);
Point p2 = rotatedPoly.get((i + 1) % n);
- // 浼樺寲锛氬拷鐣ユ按骞崇嚎娈� (p1.y == p2.y)锛岄伩鍏嶉櫎闆堕敊璇紝姘村钩绾挎涓嶅弬涓庡瀭鐩存壂鎻忕嚎姹備氦
+ // 蹇界暐姘村钩绾挎
if (Math.abs(p1.y - p2.y) < EPSILON) continue;
- // 鍒ゆ柇绾挎鏄惁璺ㄨ秺 currentY
- // 浣跨敤涓ユ牸鐨勪笉绛夊紡閫昏緫閰嶅悎鑼冨洿鍒ゆ柇
double minP = Math.min(p1.y, p2.y);
double maxP = Math.max(p1.y, p2.y);
if (currentY >= minP && currentY < maxP) {
- // 绾挎�ф彃鍊兼眰X
double x = p1.x + (currentY - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
xIntersections.add(x);
}
@@ -191,20 +229,19 @@
Collections.sort(xIntersections);
- // 鎻愬彇绾挎锛岄�氬父鏄垚瀵瑰嚭鐜�
if (xIntersections.size() >= 2) {
- // 鍙栨渶宸﹀拰鏈�鍙崇偣杩炴帴锛堝簲瀵瑰彲鑳藉嚭鐜扮殑寰皬璁$畻璇樊瀵艰嚧鐨勫涓氦鐐癸級
+ // 鍙栨渶宸﹀拰鏈�鍙充氦鐐�
double xStart = xIntersections.get(0);
double xEnd = xIntersections.get(xIntersections.size() - 1);
- // 鍙湁褰撶嚎娈甸暱搴﹀ぇ浜庢瀬灏忓�兼椂鎵嶆坊鍔狅紝閬垮厤鐢熸垚鍣偣璺緞
if (xEnd - xStart > EPSILON) {
- Point rStart = rotatePoint(new Point(xStart, currentY), angle); // 杩欓噷鐨刢urrentY瀹為檯涓婂甫浜唀psilon锛岃繕鍘熸椂娌¢棶棰�
+ // 鍙嶅悜鏃嬭浆鍥炲師鍧愭爣绯�
+ Point rStart = rotatePoint(new Point(xStart, currentY), angle);
Point rEnd = rotatePoint(new Point(xEnd, currentY), angle);
segments.add(new PathSegment(rStart, rEnd, true));
}
}
-
+ // 姝ヨ繘
currentY += width;
}
@@ -231,7 +268,6 @@
// 娣诲姞杩囨浮娈�
if (i > 0) {
Point prevEnd = result.get(result.size() - 1).end;
- // 鍙湁褰撹窛绂荤‘瀹炲瓨鍦ㄦ椂鎵嶆坊鍔犺繃娓℃锛堥伩鍏嶉噸鍚堢偣锛�
if (distanceSq(prevEnd, actualStart) > EPSILON) {
result.add(new PathSegment(prevEnd, actualStart, false));
}
@@ -242,12 +278,12 @@
return result;
}
- // ================= 鍩虹鍑犱綍宸ュ叿 (浼樺寲鐗�) =================
+ // ================= 鍩虹鍑犱綍宸ュ叿 =================
private static List<Point> parseCoords(String s) {
List<Point> list = new ArrayList<>();
if (s == null || s.trim().isEmpty()) return list;
-
+
String[] parts = s.split(";");
for (String part : parts) {
String[] xy = part.split(",");
@@ -257,14 +293,13 @@
double y = Double.parseDouble(xy[1].trim());
list.add(new Point(x, y));
} catch (NumberFormatException e) {
- // 蹇界暐鏍煎紡閿欒鐨勫崟涓偣
+ // 蹇界暐鏍煎紡閿欒
}
}
}
return list;
}
- // Shoelace鍏紡鍒ゆ柇鏂瑰悜骞惰皟鏁�
private static void ensureCCW(List<Point> polygon) {
double sum = 0;
for (int i = 0; i < polygon.size(); i++) {
@@ -272,14 +307,33 @@
Point p2 = polygon.get((i + 1) % polygon.size());
sum += (p2.x - p1.x) * (p2.y + p1.y);
}
- // 鍋囪鏍囧噯绗涘崱灏斿潗鏍囩郴锛宻um > 0 涓洪『鏃堕拡锛岄渶瑕佸弽杞负閫嗘椂閽�
if (sum > 0) {
Collections.reverse(polygon);
}
}
+ private static List<Point> shrinkPolygon(List<Point> polygon, double margin) {
+ List<Point> newPoints = new ArrayList<>();
+ int n = polygon.size();
+
+ for (int i = 0; i < n; i++) {
+ Point p1 = polygon.get(i);
+ Point p2 = polygon.get((i + 1) % n);
+ Point p0 = polygon.get((i - 1 + n) % n);
+
+ Line line1 = offsetLine(p1, p2, margin);
+ Line line0 = offsetLine(p0, p1, margin);
+
+ Point intersection = getIntersection(line0, line1);
+ if (intersection != null) {
+ newPoints.add(intersection);
+ }
+ }
+ return newPoints;
+ }
+
private static class Line {
- double a, b, c;
+ double a, b, c;
public Line(double a, double b, double c) { this.a = a; this.b = b; this.c = c; }
}
@@ -287,13 +341,13 @@
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
double len = Math.sqrt(dx * dx + dy * dy);
-
- // 闃叉闄や互0
+
if (len < EPSILON) return new Line(0, 0, 0);
double nx = -dy / len;
double ny = dx / len;
+ // 鍚戝乏渚у钩绉伙紙鍋囪閫嗘椂閽堬級
double newX = p1.x + nx * dist;
double newY = p1.y + ny * dist;
@@ -305,7 +359,7 @@
private static Point getIntersection(Line l1, Line l2) {
double det = l1.a * l2.b - l2.a * l1.b;
- if (Math.abs(det) < EPSILON) return null; // 骞宠
+ if (Math.abs(det) < EPSILON) return null;
double x = (l1.b * l2.c - l2.b * l1.c) / det;
double y = (l2.a * l1.c - l1.a * l2.c) / det;
return new Point(x, y);
@@ -324,7 +378,7 @@
}
return res;
}
-
+
private static double distanceSq(Point p1, Point p2) {
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
diff --git a/src/lujing/MowingPathGenerationPage.java b/src/lujing/MowingPathGenerationPage.java
index 503a1fd..de7c608 100644
--- a/src/lujing/MowingPathGenerationPage.java
+++ b/src/lujing/MowingPathGenerationPage.java
@@ -540,9 +540,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) {
@@ -711,6 +713,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) {
@@ -731,6 +757,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) {
diff --git a/src/lujing/ObstaclePathPlanner.java b/src/lujing/ObstaclePathPlanner.java
index 96f189a..50ac324 100644
--- a/src/lujing/ObstaclePathPlanner.java
+++ b/src/lujing/ObstaclePathPlanner.java
@@ -538,3 +538,5 @@
+
+
diff --git a/src/lujing/YixinglujingNoObstacle.java b/src/lujing/YixinglujingNoObstacle.java
index 1919731..ae28e81 100644
--- a/src/lujing/YixinglujingNoObstacle.java
+++ b/src/lujing/YixinglujingNoObstacle.java
@@ -1,23 +1,454 @@
package lujing;
-import java.util.List;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
/**
- * 鏃犻殰纰嶇墿寮傚舰鍦板潡璺緞瑙勫垝绫�
+ * 寮傚舰锛堟棤闅滅鐗╋級鑽夊湴璺緞瑙勫垝绫� - 浼樺寲鐗� V2.0
+ * * 鍔熻兘鐗圭偣锛�
+ * 1. 鑷姩澶勭悊鍑瑰杈瑰舰锛堥�氳繃鑰冲垏娉曞垎鍓诧級
+ * 2. 澧炲姞鈥滃洿杈光�濊矾寰勶紝淇濊瘉杈圭紭鍓茶崏鏁存磥
+ * 3. 鑷姩璁$畻姣忎釜瀛愬尯鍩熺殑鏈�浼樻壂鎻忚搴︼紙鍑忓皯鎺夊ご娆℃暟锛�
+ * 4. 鏅鸿兘鍖哄煙杩炴帴锛堟敮鎸佸弻鍚戣矾寰勯�夋嫨锛�
*/
public class YixinglujingNoObstacle {
-
+
+ // ==========================================
+ // 瀵瑰鎺ュ彛
+ // ==========================================
+
/**
- * 鐢熸垚璺緞
- * @param boundaryCoordsStr 鍦板潡杈圭晫鍧愭爣瀛楃涓� "x1,y1;x2,y2;..."
- * @param mowingWidthStr 鍓茶崏瀹藉害瀛楃涓诧紝濡� "0.34"
- * @param safetyMarginStr 瀹夊叏杈硅窛瀛楃涓诧紝濡� "0.2"
- * @return 璺緞鍧愭爣瀛楃涓诧紝鏍煎紡 "x1,y1;x2,y2;..."
+ * 瑙勫垝寮傚舰鑽夊湴鍓茶崏璺緞
+ *
+ * @param coordinates 鍦板潡杈圭晫鍧愭爣瀛楃涓诧紝鏍煎紡锛�"x1,y1;x2,y2;x3,y3;..."
+ * @param widthStr 鍓茶崏瀹藉害锛堢背锛夛紝濡� "0.34"
+ * @param marginStr 瀹夊叏杈硅窛锛堢背锛夛紝濡� "0.2"
+ * @return 璺緞娈靛垪琛�
*/
- public static String planPath(String boundaryCoordsStr, String mowingWidthStr, String safetyMarginStr) {
- // TODO: 瀹炵幇寮傚舰鍦板潡鏃犻殰纰嶇墿璺緞瑙勫垝绠楁硶
- // 鐩墠浣跨敤榛樿鏂规硶浣滀负涓存椂瀹炵幇
- throw new UnsupportedOperationException("YixinglujingNoObstacle.planPath 灏氭湭瀹炵幇");
+ public static List<PathSegment> planPath(String coordinates, String widthStr, String marginStr) {
+ // 1. 鍙傛暟瑙f瀽涓庨澶勭悊
+ List<Point> rawPoints = parseCoordinates(coordinates);
+ if (rawPoints.size() < 3) {
+ throw new IllegalArgumentException("澶氳竟褰㈢偣鏁颁笉瓒筹紝鏃犳硶鏋勬垚鍦板潡");
+ }
+ // 纭繚閫嗘椂閽堥『搴忥紝鏂逛究鍚庣画鍑犱綍璁$畻
+ ensureCounterClockwise(rawPoints);
+
+ double mowWidth = Double.parseDouble(widthStr);
+ double safeMargin = Double.parseDouble(marginStr);
+
+ List<PathSegment> finalPath = new ArrayList<>();
+
+ // 2. 鐢熸垚鍥磋竟璺緞 (Contour Path)
+ // 杩欎竴姝ュ厛瑙勫垝涓�鍦堣疆寤擄紝瑙e喅寮傚舰杈圭紭闅惧鐞嗙殑闂
+ List<Point> contourPoly = getInsetPolygon(rawPoints, safeMargin);
+
+ // 濡傛灉鍐呯缉鍚庨潰绉お灏忔垨鐐规暟涓嶈冻锛岀洿鎺ヨ繑鍥炵┖
+ if (contourPoly.size() < 3) {
+ return new ArrayList<>();
+ }
+
+ // 灏嗗洿杈硅矾寰勫姞鍏ョ粨鏋�
+ for (int i = 0; i < contourPoly.size(); i++) {
+ Point p1 = contourPoly.get(i);
+ Point p2 = contourPoly.get((i + 1) % contourPoly.size());
+ finalPath.add(new PathSegment(p1, p2, true)); // true = 鍓茶崏
+ }
+
+ // 璁板綍鍥磋竟缁撴潫鍚庣殑浣嶇疆锛堥�氬父鍥炲埌鍥磋竟璧风偣锛�
+ Point endOfContour = contourPoly.get(0);
+
+ // 3. 鍖哄煙鍒嗗壊 (Decomposition)
+ // 浣跨敤鑰冲垏娉曞皢鍥磋竟鍚庣殑澶氳竟褰㈠垎鍓蹭负澶氫釜鍑稿杈瑰舰锛堜笁瑙掑舰锛�
+ // 杩欐牱鍙互淇濊瘉瑕嗙洊鏃犻仐婕�
+ List<List<Point>> triangles = triangulatePolygon(contourPoly);
+
+ // 4. 瀵规瘡涓尯鍩熺敓鎴愬唴閮ㄥ~鍏呰矾寰�
+ List<List<PathSegment>> allRegionPaths = new ArrayList<>();
+
+ for (List<Point> triangle : triangles) {
+ // 銆愪紭鍖栥�戝鎵炬渶浼樻壂鎻忚搴︼細
+ // 閬嶅巻涓夎褰㈢殑涓夋潯杈癸紝璁$畻浠ュ摢鏉¤竟涓哄熀鍑嗘壂鎻忔椂锛岀敓鎴愮殑琛屾暟鏈�灏戯紙杞集鏈�灏戯級
+ List<PathSegment> regionPath = planConvexPathOptimal(triangle, mowWidth);
+ if (!regionPath.isEmpty()) {
+ allRegionPaths.add(regionPath);
+ }
+ }
+
+ // 5. 杩炴帴鎵�鏈夊唴閮ㄥ尯鍩� (Greedy Connection)
+ // 浠庡洿杈圭粨鏉熺偣寮�濮嬶紝瀵绘壘鏈�杩戠殑涓嬩竴涓尯鍩�
+ List<PathSegment> internalPaths = connectRegions(allRegionPaths, endOfContour);
+ finalPath.addAll(internalPaths);
+
+ return finalPath;
}
-}
+
+ // ==========================================
+ // 鏍稿績瑙勫垝绠楁硶
+ // ==========================================
+
+ /**
+ * 瑙勫垝鍑稿杈瑰舰璺緞锛岃嚜鍔ㄩ�夋嫨鏈�浼樿搴�
+ */
+ private static List<PathSegment> planConvexPathOptimal(List<Point> polygon, double width) {
+ if (polygon.size() < 3) return new ArrayList<>();
+
+ double bestAngle = 0;
+ double minLines = Double.MAX_VALUE;
+
+ // 閬嶅巻澶氳竟褰㈢殑姣忎竴鏉¤竟锛屽皾璇曚互璇ヨ竟瑙掑害杩涜鎵弿
+ for (int i = 0; i < polygon.size(); i++) {
+ Point p1 = polygon.get(i);
+ Point p2 = polygon.get((i + 1) % polygon.size());
+
+ // 璁$畻杈圭殑瑙掑害
+ double angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
+
+ // 璁$畻鍦ㄨ繖涓搴︿笅锛屽杈瑰舰鐨勫瀭鐩存姇褰遍珮搴�
+ // 楂樺害瓒婂皬锛屾剰鍛崇潃娌挎鏂瑰悜鎵弿鐨勮鏁拌秺灏戯紝鏁堢巼瓒婇珮
+ double height = calculatePolygonHeight(polygon, -angle);
+
+ if (height < minLines) {
+ minLines = height;
+ bestAngle = angle;
+ }
+ }
+
+ // 浣跨敤鏈�浣宠搴︾敓鎴愯矾寰�
+ return generatePathWithAngle(polygon, width, bestAngle);
+ }
+
+ /**
+ * 鏍规嵁鎸囧畾瑙掑害鐢熸垚寮撳瓧褰㈣矾寰�
+ */
+ private static List<PathSegment> generatePathWithAngle(List<Point> polygon, double width, double angle) {
+ // 1. 灏嗗杈瑰舰鏃嬭浆鍒版按骞充綅缃�
+ List<Point> rotatedPoints = new ArrayList<>();
+ for (Point p : polygon) {
+ rotatedPoints.add(rotatePoint(p, -angle));
+ }
+
+ // 2. 璁$畻Y杞磋寖鍥�
+ double minY = Double.MAX_VALUE;
+ double maxY = -Double.MAX_VALUE;
+ for (Point p : rotatedPoints) {
+ minY = Math.min(minY, p.y);
+ maxY = Math.max(maxY, p.y);
+ }
+
+ List<PathSegment> segments = new ArrayList<>();
+ boolean leftToRight = true;
+
+ // 3. 鎵弿绾跨敓鎴� (浠� minY + width/2 寮�濮嬶紝淇濊瘉绗竴鍒�鍒囧湪澶氳竟褰㈠唴)
+ for (double y = minY + width / 2; y <= maxY; y += width) {
+ List<Double> intersections = new ArrayList<>();
+ for (int i = 0; i < rotatedPoints.size(); i++) {
+ Point p1 = rotatedPoints.get(i);
+ Point p2 = rotatedPoints.get((i + 1) % rotatedPoints.size());
+
+ // 鍒ゆ柇鎵弿绾挎槸鍚︾┛杩囪竟
+ if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
+ double x = p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
+ intersections.add(x);
+ }
+ }
+ Collections.sort(intersections);
+
+ // 鎴愬鐢熸垚绾挎
+ for (int k = 0; k < intersections.size() - 1; k += 2) {
+ double x1 = leftToRight ? intersections.get(k) : intersections.get(k + 1);
+ double x2 = leftToRight ? intersections.get(k + 1) : intersections.get(k);
+
+ Point start = new Point(x1, y);
+ Point end = new Point(x2, y);
+
+ // 鏃嬭浆鍥炲師濮嬪潗鏍囩郴
+ Point originalStart = rotatePoint(start, angle);
+ Point originalEnd = rotatePoint(end, angle);
+
+ // 杩炴帴閫昏緫锛氬鏋滀笉鏄涓�娈碉紝闇�瑕佷粠涓婁竴娈电粓鐐硅繛杩囨潵
+ if (!segments.isEmpty()) {
+ PathSegment prev = segments.get(segments.size() - 1);
+ // 娣诲姞杩炴帴绾匡紙閫氬父绠椾綔鍓茶崏璺緞鐨勪竴閮ㄥ垎锛屼繚鎸佸紦瀛楀舰杩炵画锛�
+ segments.add(new PathSegment(prev.end, originalStart, true));
+ }
+
+ segments.add(new PathSegment(originalStart, originalEnd, true));
+ }
+ leftToRight = !leftToRight; // 鎹㈠悜
+ }
+
+ return segments;
+ }
+
+ /**
+ * 杩炴帴鎵�鏈夊垎鍓插悗鐨勫尯鍩� (璐績绛栫暐 + 鍙屽悜浼樺寲)
+ */
+ private static List<PathSegment> connectRegions(List<List<PathSegment>> regions, Point startPoint) {
+ List<PathSegment> result = new ArrayList<>();
+ if (regions.isEmpty()) return result;
+
+ List<List<PathSegment>> remaining = new ArrayList<>(regions);
+ Point currentPos = startPoint;
+
+ while (!remaining.isEmpty()) {
+ int bestIndex = -1;
+ double minDist = Double.MAX_VALUE;
+ boolean needReverse = false;
+
+ // 瀵绘壘绂诲綋鍓嶄綅缃渶杩戠殑鍖哄煙璧风偣鎴栫粓鐐�
+ for (int i = 0; i < remaining.size(); i++) {
+ List<PathSegment> region = remaining.get(i);
+ Point pStart = region.get(0).start;
+ Point pEnd = region.get(region.size() - 1).end;
+
+ double dStart = distance(currentPos, pStart);
+ double dEnd = distance(currentPos, pEnd);
+
+ // 妫�鏌ユ鍚戣繘鍏�
+ if (dStart < minDist) {
+ minDist = dStart;
+ bestIndex = i;
+ needReverse = false;
+ }
+ // 妫�鏌ュ弽鍚戣繘鍏ワ紙鍊掔潃鍓茶崏濡傛灉鏇磋繎锛�
+ if (dEnd < minDist) {
+ minDist = dEnd;
+ bestIndex = i;
+ needReverse = true;
+ }
+ }
+
+ if (bestIndex != -1) {
+ List<PathSegment> targetRegion = remaining.remove(bestIndex);
+
+ if (needReverse) {
+ // 鍙嶈浆璇ュ尯鍩熺殑鎵�鏈夎矾寰�
+ List<PathSegment> reversedRegion = new ArrayList<>();
+ for (int k = targetRegion.size() - 1; k >= 0; k--) {
+ PathSegment seg = targetRegion.get(k);
+ // 浜ゆ崲璧风偣缁堢偣
+ reversedRegion.add(new PathSegment(seg.end, seg.start, seg.isMowing));
+ }
+ targetRegion = reversedRegion;
+ }
+
+ // 娣诲姞杩囨浮璺緞锛堟姮鍒�绉诲姩锛宨sMowing=false锛�
+ Point nextStart = targetRegion.get(0).start;
+ // 鍙湁璺濈鏄捐憲鎵嶆坊鍔犵Щ鍔ㄦ
+ if (distance(currentPos, nextStart) > 0.01) {
+ result.add(new PathSegment(currentPos, nextStart, false));
+ }
+
+ result.addAll(targetRegion);
+ currentPos = targetRegion.get(targetRegion.size() - 1).end;
+ } else {
+ break; // 闃插尽鎬т唬鐮�
+ }
+ }
+ return result;
+ }
+
+ // ==========================================
+ // 鍑犱綍杩愮畻杈呭姪鏂规硶
+ // ==========================================
+
+ /**
+ * 鍐呯缉澶氳竟褰� (鍩轰簬瑙掑钩鍒嗙嚎)
+ */
+ private static List<Point> getInsetPolygon(List<Point> points, double margin) {
+ List<Point> result = new ArrayList<>();
+ int n = points.size();
+
+ for (int i = 0; i < n; i++) {
+ Point pPrev = points.get((i - 1 + n) % n);
+ Point pCurr = points.get(i);
+ Point pNext = points.get((i + 1) % n);
+
+ Point v1 = new Point(pCurr.x - pPrev.x, pCurr.y - pPrev.y);
+ Point v2 = new Point(pNext.x - pCurr.x, pNext.y - pCurr.y);
+
+ double len1 = Math.hypot(v1.x, v1.y);
+ double len2 = Math.hypot(v2.x, v2.y);
+
+ if (len1 < 1e-6 || len2 < 1e-6) continue;
+
+ // 褰掍竴鍖栧悜閲�
+ Point n1 = new Point(v1.x / len1, v1.y / len1);
+ Point n2 = new Point(v2.x / len2, v2.y / len2);
+
+ // 璁$畻骞冲垎绾挎柟鍚�
+ // v1鍙嶅悜 + v2
+ Point bisector = new Point(-n1.x + n2.x, -n1.y + n2.y);
+ double biLen = Math.hypot(bisector.x, bisector.y);
+
+ // 璁$畻鍗婅 sin(theta/2)
+ double cross = n1.x * n2.y - n1.y * n2.x; // 鍙夌Н鍒ゆ柇杞悜
+
+ // 榛樿鍚戝乏渚у唴缂� (CCW澶氳竟褰�)
+ if (biLen < 1e-6) {
+ // 鍏辩嚎锛屾部娉曠嚎鏂瑰悜
+ bisector = new Point(-n1.y, n1.x);
+ } else {
+ bisector.x /= biLen;
+ bisector.y /= biLen;
+ }
+
+ // 璁$畻鍋忕Щ璺濈
+ double dot = n1.x * n2.x + n1.y * n2.y;
+ double angle = Math.acos(Math.max(-1, Math.min(1, dot)));
+ double dist = margin / Math.sin(angle / 2.0);
+
+ // 鏂瑰悜淇锛氱‘淇濆钩鍒嗙嚎鎸囧悜澶氳竟褰㈠唴閮紙閫嗘椂閽堝杈瑰舰鐨勫乏渚э級
+ Point leftNormal = new Point(-n1.y, n1.x);
+ if (bisector.x * leftNormal.x + bisector.y * leftNormal.y < 0) {
+ bisector.x = -bisector.x;
+ bisector.y = -bisector.y;
+ }
+
+ // 濡傛灉鏄嚬瑙掞紙cross < 0锛夛紝骞冲垎绾挎寚鍚戝閮紝璺濈闇�瑕佸弽杞垨鑰呯壒娈婂鐞�
+ // 绠�鍗曞鐞嗭細瀵逛簬鍑硅锛屽亸绉荤偣瀹為檯涓婁細杩滅鍘熺偣锛屼笂杩伴�昏緫閫氬父鑳借鐩栵紝
+ // 浣嗘瀬绔攼瑙掑彲鑳藉鑷磀ist杩囧ぇ銆傛澶勫仛绠�鍗曟埅鏂繚鎶ゆ槸涓嶅鐨勶紝
+ // 浣嗛拡瀵逛竴鑸崏鍦板舰鐘讹紝姝ら�昏緫鍙敤銆�
+
+ result.add(new Point(pCurr.x + bisector.x * dist, pCurr.y + bisector.y * dist));
+ }
+ return result;
+ }
+
+ /**
+ * 鑰冲垏娉曞垎鍓插杈瑰舰
+ */
+ private static List<List<Point>> triangulatePolygon(List<Point> poly) {
+ List<List<Point>> triangles = new ArrayList<>();
+ List<Point> remaining = new ArrayList<>(poly);
+
+ int maxIter = remaining.size() * 3;
+ int iter = 0;
+
+ while (remaining.size() > 3 && iter++ < maxIter) {
+ int n = remaining.size();
+ boolean earFound = false;
+
+ for (int i = 0; i < n; i++) {
+ Point prev = remaining.get((i - 1 + n) % n);
+ Point curr = remaining.get(i);
+ Point next = remaining.get((i + 1) % n);
+
+ if (isConvex(prev, curr, next)) {
+ boolean hasPoint = false;
+ for (int j = 0; j < n; j++) {
+ if (j == i || j == (i - 1 + n) % n || j == (i + 1) % n) continue;
+ if (isPointInTriangle(remaining.get(j), prev, curr, next)) {
+ hasPoint = true;
+ break;
+ }
+ }
+
+ if (!hasPoint) {
+ List<Point> tri = new ArrayList<>();
+ tri.add(prev); tri.add(curr); tri.add(next);
+ triangles.add(tri);
+ remaining.remove(i);
+ earFound = true;
+ break;
+ }
+ }
+ }
+ if (!earFound) break;
+ }
+
+ if (remaining.size() == 3) {
+ triangles.add(remaining);
+ }
+ return triangles;
+ }
+
+ private static double calculatePolygonHeight(List<Point> poly, double angle) {
+ double minY = Double.MAX_VALUE;
+ double maxY = -Double.MAX_VALUE;
+ for (Point p : poly) {
+ Point r = rotatePoint(p, angle);
+ minY = Math.min(minY, r.y);
+ maxY = Math.max(maxY, r.y);
+ }
+ return maxY - minY;
+ }
+
+ private static Point rotatePoint(Point p, double angle) {
+ double cos = Math.cos(angle);
+ double sin = Math.sin(angle);
+ return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
+ }
+
+ private static boolean isConvex(Point a, Point b, Point c) {
+ return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x) >= 0;
+ }
+
+ private static boolean isPointInTriangle(Point p, Point a, Point b, Point c) {
+ double areaABC = Math.abs((a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y))/2.0);
+ double areaPBC = Math.abs((p.x*(b.y-c.y) + b.x*(c.y-p.y) + c.x*(p.y-b.y))/2.0);
+ double areaPAC = Math.abs((a.x*(p.y-c.y) + p.x*(c.y-a.y) + c.x*(a.y-p.y))/2.0);
+ double areaPAB = Math.abs((a.x*(b.y-p.y) + b.x*(p.y-a.y) + p.x*(a.y-b.y))/2.0);
+ return Math.abs(areaABC - (areaPBC + areaPAC + areaPAB)) < 1e-6;
+ }
+
+ private static List<Point> parseCoordinates(String coordinates) {
+ List<Point> points = new ArrayList<>();
+ String cleanStr = coordinates.replaceAll("[()\\[\\]{}]", "").trim();
+ String[] pairs = cleanStr.split(";");
+ for (String pair : pairs) {
+ pair = pair.trim();
+ if (pair.isEmpty()) continue;
+ String[] xy = pair.split(",");
+ if (xy.length == 2) {
+ points.add(new Point(Double.parseDouble(xy[0].trim()), Double.parseDouble(xy[1].trim())));
+ }
+ }
+ return points;
+ }
+
+ private static void ensureCounterClockwise(List<Point> points) {
+ double sum = 0;
+ for (int i = 0; i < points.size(); i++) {
+ Point p1 = points.get(i);
+ Point p2 = points.get((i + 1) % points.size());
+ sum += (p2.x - p1.x) * (p2.y + p1.y);
+ }
+ if (sum > 0) {
+ Collections.reverse(points);
+ }
+ }
+
+ private static double distance(Point p1, Point p2) {
+ return Math.hypot(p1.x - p2.x, p1.y - p2.y);
+ }
+
+ // ==========================================
+ // 鍐呴儴鏁版嵁缁撴瀯
+ // ==========================================
+
+ public static class Point {
+ public double x, y;
+ public Point(double x, double y) { this.x = x; this.y = y; }
+ @Override public String toString() { return String.format("%.2f,%.2f", x, y); }
+ }
+
+ public static class PathSegment {
+ public Point start;
+ public Point end;
+ public boolean isMowing;
+
+ public PathSegment(Point start, Point end, boolean isMowing) {
+ this.start = start;
+ this.end = end;
+ this.isMowing = isMowing;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[%s -> %s, 鍓茶崏:%b]", start, end, isMowing);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/zhangaiwu/AddDikuai.java b/src/zhangaiwu/AddDikuai.java
index 32a7a4f..8f53100 100644
--- a/src/zhangaiwu/AddDikuai.java
+++ b/src/zhangaiwu/AddDikuai.java
@@ -103,6 +103,7 @@
final boolean success;
final String errorMessage;
final String originalBoundary;
+ final String originalBoundaryXY;
final String optimizedBoundary;
final String areaSqMeters;
final String baseStationCoordinates;
@@ -111,6 +112,7 @@
private BoundarySnapshotResult(boolean success,
String errorMessage,
String originalBoundary,
+ String originalBoundaryXY,
String optimizedBoundary,
String areaSqMeters,
String baseStationCoordinates,
@@ -118,18 +120,19 @@
this.success = success;
this.errorMessage = errorMessage;
this.originalBoundary = originalBoundary;
+ this.originalBoundaryXY = originalBoundaryXY;
this.optimizedBoundary = optimizedBoundary;
this.areaSqMeters = areaSqMeters;
this.baseStationCoordinates = baseStationCoordinates;
this.messageType = messageType;
}
- static BoundarySnapshotResult success(String original, String optimized, String area, String baseStation) {
- return new BoundarySnapshotResult(true, null, original, optimized, area, baseStation, JOptionPane.INFORMATION_MESSAGE);
+ static BoundarySnapshotResult success(String original, String originalXY, String optimized, String area, String baseStation) {
+ return new BoundarySnapshotResult(true, null, original, originalXY, optimized, area, baseStation, JOptionPane.INFORMATION_MESSAGE);
}
static BoundarySnapshotResult failure(String message, int messageType) {
- return new BoundarySnapshotResult(false, message, null, null, null, null, messageType);
+ return new BoundarySnapshotResult(false, message, null, null, null, null, null, messageType);
}
}
@@ -1675,6 +1678,7 @@
Dikuai dikuai = getOrCreatePendingDikuai();
if (dikuai != null) {
dikuai.setBoundaryOriginalCoordinates(snapshot.originalBoundary);
+ dikuai.setBoundaryOriginalXY(snapshot.originalBoundaryXY);
dikuai.setBoundaryCoordinates(snapshot.optimizedBoundary);
dikuai.setLandArea(snapshot.areaSqMeters);
dikuai.setBaseStationCoordinates(snapshot.baseStationCoordinates);
@@ -1683,11 +1687,13 @@
}
dikuaiData.put("boundaryOriginalCoordinates", snapshot.originalBoundary);
+ dikuaiData.put("boundaryOriginalXY", snapshot.originalBoundaryXY);
dikuaiData.put("boundaryCoordinates", snapshot.optimizedBoundary);
dikuaiData.put("landArea", snapshot.areaSqMeters);
dikuaiData.put("baseStationCoordinates", snapshot.baseStationCoordinates);
if (activeSession != null) {
activeSession.data.put("boundaryOriginalCoordinates", snapshot.originalBoundary);
+ activeSession.data.put("boundaryOriginalXY", snapshot.originalBoundaryXY);
activeSession.data.put("boundaryCoordinates", snapshot.optimizedBoundary);
activeSession.data.put("landArea", snapshot.areaSqMeters);
activeSession.data.put("baseStationCoordinates", snapshot.baseStationCoordinates);
@@ -1743,6 +1749,47 @@
}
return sb.toString();
}
+
+ /**
+ * 鏋勫缓鍘熷杈圭晫XY鍧愭爣瀛楃涓诧紙鐩稿浜庡熀鍑嗙珯鐨刋Y鍧愭爣锛�
+ */
+ private static String buildOriginalBoundaryXYString(List<Coordinate> coordinates, String baseStationCoordinates) {
+ if (coordinates == null || coordinates.isEmpty()) {
+ return "-1";
+ }
+ if (baseStationCoordinates == null || baseStationCoordinates.trim().isEmpty() || "-1".equals(baseStationCoordinates.trim())) {
+ return "-1";
+ }
+
+ try {
+ // 瑙f瀽鍩哄噯绔欏潗鏍�
+ String[] baseParts = baseStationCoordinates.split(",");
+ if (baseParts.length < 4) {
+ return "-1";
+ }
+ double baseLat = publicway.Gpstoxuzuobiao.parseDMToDecimal(baseParts[0], baseParts[1]);
+ double baseLon = publicway.Gpstoxuzuobiao.parseDMToDecimal(baseParts[2], baseParts[3]);
+
+ StringBuilder sb = new StringBuilder();
+ DecimalFormat xyFormat = new DecimalFormat("0.00");
+ for (Coordinate coord : coordinates) {
+ double lat = convertToDecimalDegree(coord.getLatitude(), coord.getLatDirection());
+ double lon = convertToDecimalDegree(coord.getLongitude(), coord.getLonDirection());
+
+ // 杞崲涓篨Y鍧愭爣
+ double[] xy = publicway.Gpstoxuzuobiao.convertLatLonToLocal(lat, lon, baseLat, baseLon);
+
+ if (sb.length() > 0) {
+ sb.append(";");
+ }
+ sb.append(xyFormat.format(xy[0])).append(",").append(xyFormat.format(xy[1]));
+ }
+ return sb.toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return "-1";
+ }
+ }
private static double convertToDecimalDegree(String dmm, String direction) {
if (dmm == null || dmm.isEmpty()) {
@@ -1865,10 +1912,11 @@
}
String originalBoundary = buildOriginalBoundaryString(uniqueCoordinates);
+ String originalBoundaryXY = buildOriginalBoundaryXYString(uniqueCoordinates, baseStationCoordinates);
DecimalFormat areaFormat = new DecimalFormat("0.00");
String areaString = areaFormat.format(area);
- return BoundarySnapshotResult.success(originalBoundary, optimizedBoundary, areaString, baseStationCoordinates);
+ return BoundarySnapshotResult.success(originalBoundary, originalBoundaryXY, optimizedBoundary, areaString, baseStationCoordinates);
}
private String resolvePlannerMode(String patternDisplay) {
@@ -2407,6 +2455,7 @@
BoundarySnapshotResult snapshot = computeBoundarySnapshot();
if (snapshot.success && activeSession != null) {
activeSession.data.put("boundaryOriginalCoordinates", snapshot.originalBoundary);
+ activeSession.data.put("boundaryOriginalXY", snapshot.originalBoundaryXY);
activeSession.data.put("boundaryCoordinates", snapshot.optimizedBoundary);
activeSession.data.put("landArea", snapshot.areaSqMeters);
activeSession.data.put("baseStationCoordinates", snapshot.baseStationCoordinates);
@@ -2466,6 +2515,9 @@
if (dikuaiData.containsKey("boundaryOriginalCoordinates")) {
dikuai.setBoundaryOriginalCoordinates(dikuaiData.get("boundaryOriginalCoordinates"));
}
+ if (dikuaiData.containsKey("boundaryOriginalXY")) {
+ dikuai.setBoundaryOriginalXY(dikuaiData.get("boundaryOriginalXY"));
+ }
if (dikuaiData.containsKey("boundaryCoordinates")) {
dikuai.setBoundaryCoordinates(dikuaiData.get("boundaryCoordinates"));
}
diff --git a/src/zhuye/MapRenderer.java b/src/zhuye/MapRenderer.java
index ed61778..0d6e8d2 100644
--- a/src/zhuye/MapRenderer.java
+++ b/src/zhuye/MapRenderer.java
@@ -110,6 +110,9 @@
private WangfanDraw returnPathDrawer; // 寰�杩旇矾寰勭粯鍒剁鐞嗗櫒
private List<Point2D.Double> currentReturnPath; // 褰撳墠鍦板潡鐨勫線杩旇矾寰勶紙鐢ㄤ簬鏄剧ず锛�
private List<Point2D.Double> previewReturnPath; // 棰勮鐨勫線杩旇矾寰�
+ private List<Point2D.Double> previewOriginalBoundary; // 棰勮鐨勫師濮嬭竟鐣岋紙绱壊锛�
+ private List<Point2D.Double> previewOptimizedBoundary; // 棰勮鐨勪紭鍖栧悗杈圭晫
+ private boolean boundaryPreviewActive; // 鏄惁澶勪簬杈圭晫棰勮妯″紡
private static final double TRACK_SAMPLE_MIN_DISTANCE_METERS = 0.2d;
private static final double TRACK_DUPLICATE_TOLERANCE_METERS = 1e-3d;
@@ -397,6 +400,11 @@
if (hasBoundary) {
drawCurrentBoundary(g2d);
}
+
+ // 缁樺埗杈圭晫棰勮锛堝師濮嬭竟鐣�-绱壊锛屼紭鍖栧悗杈圭晫锛�
+ if (boundaryPreviewActive) {
+ drawBoundaryPreview(g2d);
+ }
yulanzhangaiwu.renderPreview(g2d, scale);
@@ -3073,5 +3081,68 @@
}
return new ArrayList<>();
}
+
+ /**
+ * 璁剧疆杈圭晫棰勮鏁版嵁锛堝師濮嬭竟鐣屽拰浼樺寲鍚庤竟鐣岋級
+ */
+ public void setBoundaryPreview(String originalBoundaryXY, String optimizedBoundary) {
+ if (originalBoundaryXY != null && !originalBoundaryXY.trim().isEmpty() && !"-1".equals(originalBoundaryXY.trim())) {
+ previewOriginalBoundary = parseBoundary(originalBoundaryXY.trim());
+ } else {
+ previewOriginalBoundary = null;
+ }
+
+ if (optimizedBoundary != null && !optimizedBoundary.trim().isEmpty() && !"-1".equals(optimizedBoundary.trim())) {
+ previewOptimizedBoundary = parseBoundary(optimizedBoundary.trim());
+ } else {
+ previewOptimizedBoundary = null;
+ }
+
+ boundaryPreviewActive = (previewOriginalBoundary != null && previewOriginalBoundary.size() >= 2) ||
+ (previewOptimizedBoundary != null && previewOptimizedBoundary.size() >= 2);
+
+ if (boundaryPreviewActive) {
+ // 璁$畻棰勮杈圭晫鐨勮竟鐣屾骞惰皟鏁磋鍥�
+ List<Point2D.Double> allPoints = new ArrayList<>();
+ if (previewOriginalBoundary != null) allPoints.addAll(previewOriginalBoundary);
+ if (previewOptimizedBoundary != null) allPoints.addAll(previewOptimizedBoundary);
+ if (!allPoints.isEmpty()) {
+ Rectangle2D.Double bounds = computeBounds(allPoints);
+ SwingUtilities.invokeLater(() -> {
+ fitBoundsToView(bounds);
+ visualizationPanel.repaint();
+ });
+ }
+ } else {
+ visualizationPanel.repaint();
+ }
+ }
+
+ /**
+ * 娓呴櫎杈圭晫棰勮
+ */
+ public void clearBoundaryPreview() {
+ previewOriginalBoundary = null;
+ previewOptimizedBoundary = null;
+ boundaryPreviewActive = false;
+ visualizationPanel.repaint();
+ }
+
+ /**
+ * 缁樺埗杈圭晫棰勮锛堝師濮嬭竟鐣�-绱壊锛屼紭鍖栧悗杈圭晫-缁胯壊锛�
+ */
+ private void drawBoundaryPreview(Graphics2D g2d) {
+ // 缁樺埗鍘熷杈圭晫锛堢传鑹诧級
+ if (previewOriginalBoundary != null && previewOriginalBoundary.size() >= 2) {
+ Color purpleFill = new Color(128, 0, 128, 80); // 绱壊鍗婇�忔槑濉厖
+ Color purpleBorder = new Color(128, 0, 128, 255); // 绱壊杈规
+ bianjiedrwa.drawBoundary(g2d, previewOriginalBoundary, scale, purpleFill, purpleBorder);
+ }
+
+ // 缁樺埗浼樺寲鍚庤竟鐣岋紙缁胯壊锛屼笌姝e父杈圭晫棰滆壊涓�鑷达級
+ if (previewOptimizedBoundary != null && previewOptimizedBoundary.size() >= 2) {
+ bianjiedrwa.drawBoundary(g2d, previewOptimizedBoundary, scale, GRASS_FILL_COLOR, GRASS_BORDER_COLOR);
+ }
+ }
}
\ No newline at end of file
diff --git a/src/zhuye/Shouye.java b/src/zhuye/Shouye.java
index 27a166d..10832fe 100644
--- a/src/zhuye/Shouye.java
+++ b/src/zhuye/Shouye.java
@@ -4296,6 +4296,90 @@
pathPreviewReturnAction.run();
}
}
+
+ /**
+ * 鏄剧ず杈圭晫棰勮锛堝師濮嬭竟鐣�-绱壊锛屼紭鍖栧悗杈圭晫-缁胯壊锛�
+ * @param dikuai 鍦板潡瀵硅薄
+ * @param optimizedBoundary 浼樺寲鍚庣殑杈圭晫鍧愭爣瀛楃涓�
+ * @param returnCallback 杩斿洖鍥炶皟
+ */
+ public static void showBoundaryPreview(dikuai.Dikuai dikuai, String optimizedBoundary, Runnable returnCallback) {
+ Shouye shouye = getInstance();
+ if (shouye == null || shouye.mapRenderer == null || dikuai == null) {
+ return;
+ }
+
+ // 鑾峰彇鍘熷杈圭晫XY鍧愭爣
+ String originalBoundaryXY = dikuai.getBoundaryOriginalXY();
+
+ // 璁剧疆杈圭晫棰勮
+ shouye.mapRenderer.setBoundaryPreview(originalBoundaryXY, optimizedBoundary);
+
+ // 璁剧疆杩斿洖鍥炶皟
+ shouye.pathPreviewReturnAction = returnCallback;
+ shouye.pathPreviewActive = true;
+
+ // 纭繚鎮诞鎸夐挳鍩虹璁炬柦宸插垱寤�
+ shouye.ensureFloatingButtonInfrastructure();
+
+ // 鍒涘缓鎴栨樉绀鸿繑鍥炴寜閽�
+ if (shouye.pathPreviewReturnButton == null) {
+ shouye.pathPreviewReturnButton = publicway.Fanhuibutton.createReturnButton(e -> shouye.handleBoundaryPreviewReturn());
+ shouye.pathPreviewReturnButton.setToolTipText("杩斿洖杈圭晫缂栬緫椤甸潰");
+ }
+
+ // 闅愯棌鍏朵粬鎮诞鎸夐挳
+ shouye.hideFloatingDrawingControls();
+
+ // 鏄剧ず杩斿洖鎸夐挳
+ shouye.pathPreviewReturnButton.setVisible(true);
+ if (shouye.floatingButtonPanel != null) {
+ shouye.floatingButtonPanel.setVisible(true);
+ if (shouye.floatingButtonPanel.getParent() != shouye.visualizationPanel) {
+ shouye.visualizationPanel.add(shouye.floatingButtonPanel, BorderLayout.SOUTH);
+ }
+ }
+ shouye.rebuildFloatingButtonColumn();
+
+ shouye.visualizationPanel.revalidate();
+ shouye.visualizationPanel.repaint();
+ }
+
+ /**
+ * 澶勭悊杈圭晫棰勮杩斿洖
+ */
+ private void handleBoundaryPreviewReturn() {
+ Runnable callback = pathPreviewReturnAction;
+ exitBoundaryPreview();
+ if (callback != null) {
+ callback.run();
+ }
+ }
+
+ /**
+ * 閫�鍑鸿竟鐣岄瑙�
+ */
+ private void exitBoundaryPreview() {
+ pathPreviewActive = false;
+
+ // 娓呴櫎杈圭晫棰勮
+ if (mapRenderer != null) {
+ mapRenderer.clearBoundaryPreview();
+ }
+
+ // 闅愯棌杩斿洖鎸夐挳
+ if (pathPreviewReturnButton != null) {
+ pathPreviewReturnButton.setVisible(false);
+ }
+
+ // 闅愯棌鎮诞闈㈡澘
+ if (floatingButtonPanel != null) {
+ floatingButtonPanel.setVisible(false);
+ }
+
+ visualizationPanel.revalidate();
+ visualizationPanel.repaint();
+ }
// 娴嬭瘯鏂规硶
public static void main(String[] args) {
diff --git a/src/zhuye/ShouyeLauncher.java b/src/zhuye/ShouyeLauncher.java
new file mode 100644
index 0000000..197ec09
--- /dev/null
+++ b/src/zhuye/ShouyeLauncher.java
@@ -0,0 +1,23 @@
+package zhuye;
+
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+import udpdell.UDPServer;
+
+public class ShouyeLauncher {
+ public static void main(String[] args) {
+ SwingUtilities.invokeLater(() -> {
+ System.out.println("Starting Shouye via Launcher...");
+ JFrame frame = new JFrame("AutoMow - 棣栭〉");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(400, 800);
+ frame.setLocationRelativeTo(null);
+
+ Shouye shouye = new Shouye();
+ frame.add(shouye);
+
+ frame.setVisible(true);
+ UDPServer.startAsync();
+ });
+ }
+}
diff --git a/src/zhuye/bianjiedrwa.java b/src/zhuye/bianjiedrwa.java
index b40ee71..485d16d 100644
--- a/src/zhuye/bianjiedrwa.java
+++ b/src/zhuye/bianjiedrwa.java
@@ -23,7 +23,7 @@
return; // 鏃犳暟鎹繑鍥�
} // if缁撴潫
- float strokeWidth = (float) (3 / Math.max(0.5, scale)); // 璁$畻杈圭嚎瀹藉害
+ float strokeWidth = (float) (6 / Math.max(0.5, scale)); // 璁$畻杈圭嚎瀹藉害锛堝鍔犱竴鍊嶏紝浠�3鏀逛负6锛�
// 濉厖鍖哄煙
Path2D fillPath = new Path2D.Double(); // 鍒涘缓濉厖璺緞
@@ -56,9 +56,26 @@
borderPath.lineTo(firstPoint.x, firstPoint.y); // 杩炴帴鍒拌捣鐐瑰舰鎴愰棴鍚�
}
+ // 缁樺埗澶栧眰瀹炵嚎杈圭晫锛堝搴﹀鍔犱竴鍊嶏級
g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // 璁剧疆瀹炵嚎鎻忚竟
g2d.setColor(borderColor); // 璁剧疆杈圭嚎棰滆壊
g2d.draw(borderPath); // 缁樺埗瀹屾暣杈圭晫锛堝寘鎷捣鐐瑰埌缁堢偣鐨勮繛鎺ワ級
+
+ // 鍦ㄨ竟鐣岀嚎涓棿缁樺埗娣辩伆鑹插皬鍦嗙偣铏氱嚎
+ float dashedLineWidth = strokeWidth / 3.0f; // 铏氱嚎瀹藉害涓哄疄绾跨殑涓夊垎涔嬩竴
+ float[] dashPattern = {2.0f, 2.0f}; // 绛夐棿闅斿皬鍦嗙偣铏氱嚎妯″紡锛�2鍍忕礌瀹炵嚎锛堝舰鎴愬渾鐐癸級锛�2鍍忕礌绌虹櫧锛堝潎鍖�闂撮殧锛�
+ BasicStroke dashedStroke = new BasicStroke(
+ dashedLineWidth,
+ BasicStroke.CAP_ROUND, // 浣跨敤鍦嗗舰绔偣锛屽舰鎴愬渾鐐规晥鏋�
+ BasicStroke.JOIN_ROUND,
+ 0.0f,
+ dashPattern,
+ 0.0f
+ );
+ g2d.setStroke(dashedStroke); // 璁剧疆铏氱嚎鎻忚竟
+ Color darkGrayColor = new Color(64, 64, 64); // 娣辩伆鑹�
+ g2d.setColor(darkGrayColor); // 璁剧疆铏氱嚎棰滆壊
+ g2d.draw(borderPath); // 鍦ㄤ腑闂寸粯鍒跺皬鍦嗙偣铏氱嚎
}
} // 鏂规硶缁撴潫
} // 绫荤粨鏉�
diff --git a/src/zhuye/celiangmoshi.java b/src/zhuye/celiangmoshi.java
index 992c3a7..61c62fe 100644
--- a/src/zhuye/celiangmoshi.java
+++ b/src/zhuye/celiangmoshi.java
@@ -86,3 +86,5 @@
+
+
--
Gitblit v1.10.0