826220679@qq.com
7 小时以前 69b40096cb0ae965f2a3e92672b880edfe7d04d2
src/zhuye/MapRenderer.java
@@ -18,6 +18,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Consumer;
import java.io.File;
import set.Setsys;
import gecaoji.Device;
@@ -112,6 +113,7 @@
    private List<Point2D.Double> previewReturnPath; // 预览的往返路径
    private List<Point2D.Double> previewOriginalBoundary; // 预览的原始边界(紫色)
    private List<Point2D.Double> previewOptimizedBoundary; // 预览的优化后边界
    private boolean showOnlyOriginalBoundary = false; // 是否只显示原始边界
    private boolean boundaryPreviewActive; // 是否处于边界预览模式
    private static final double TRACK_SAMPLE_MIN_DISTANCE_METERS = 0.2d;
@@ -631,7 +633,7 @@
     * 添加导航预览轨迹点
     */
    public void addNavigationPreviewTrackPoint(Point2D.Double point) {
        if (point != null && Double.isFinite(point.x) && Double.isFinite(point.y)) {
        if (point != null && isFinite(point.x) && isFinite(point.y)) {
            navigationPreviewTrack.add(new Point2D.Double(point.x, point.y));
            if (visualizationPanel != null) {
                visualizationPanel.repaint();
@@ -749,7 +751,7 @@
            return;
        }
        Point2D.Double position = mower.getPosition();
        if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) {
        if (position == null || !isFinite(position.x) || !isFinite(position.y)) {
            pendingTrackBreak = true;
            return;
        }
@@ -775,7 +777,7 @@
        }
        realtimeMowingTrack.add(candidate);
        if (!pendingTrackBreak && lastPoint != null && Double.isFinite(distance)) {
        if (!pendingTrackBreak && lastPoint != null && isFinite(distance)) {
            trackLengthMeters += distance;
        }
@@ -806,7 +808,7 @@
        }
        Point2D.Double position = mower.getPosition();
        if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) {
        if (position == null || !isFinite(position.x) || !isFinite(position.y)) {
            return;
        }
@@ -851,7 +853,7 @@
        // 刷新mower位置,使用最新的Device数据
        mower.refreshFromDevice();
        Point2D.Double position = mower.getPosition();
        if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) {
        if (position == null || !isFinite(position.x) || !isFinite(position.y)) {
            return;
        }
@@ -962,7 +964,7 @@
    }
    private String formatTrackCoordinate(double value) {
        if (!Double.isFinite(value)) {
        if (!isFinite(value)) {
            return "0";
        }
        return String.format(Locale.US, "%.3f", value);
@@ -1153,7 +1155,7 @@
            try {
                double x = Double.parseDouble(parts[0].trim());
                double y = Double.parseDouble(parts[1].trim());
                if (!Double.isFinite(x) || !Double.isFinite(y)) {
                if (!isFinite(x) || !isFinite(y)) {
                    continue;
                }
                Point2D.Double current = new Point2D.Double(x, y);
@@ -1402,7 +1404,8 @@
            
            // 将世界坐标转换为屏幕坐标(用于文字显示)
            Point2D.Double worldMid = new Point2D.Double(midX, midY);
            Point2D.Double screenMid = worldToScreen(worldMid);
            Point2D.Double screenMid = new Point2D.Double();
            originalTransform.transform(worldMid, screenMid);
            
            // 恢复原始变换以绘制文字(固定大小,不随缩放变化)
            g2d.setTransform(new AffineTransform());
@@ -1450,7 +1453,94 @@
    private void drawCurrentPlannedPath(Graphics2D g2d) {
        double arrowScale = previewSizingEnabled ? 0.5d : 1.0d;
        lujingdraw.drawPlannedPath(g2d, currentPlannedPath, scale, arrowScale);
        // 尝试获取地块信息以支持区分作业路径和移动路径,以及绘制内缩边界
        String boundaryCoords = null;
        String mowingWidth = null;
        String safetyDistance = null;
        String obstaclesCoords = null;
        String mowingPattern = null;
        String plannedPathStr = null;
        // 从当前地块编号获取地块信息
        if (currentBoundaryLandNumber != null) {
            Dikuai landData = Dikuai.getDikuai(currentBoundaryLandNumber);
            if (landData != null) {
                boundaryCoords = landData.getBoundaryCoordinates();
                mowingWidth = landData.getMowingWidth();
                safetyDistance = landData.getMowingSafetyDistance();
                mowingPattern = landData.getMowingPattern();
                // 从地块获取plannedPath属性值作为路径
                plannedPathStr = landData.getPlannedPath();
                // 获取障碍物坐标
                try {
                    java.io.File configFile = new java.io.File("Obstacledge.properties");
                    if (configFile.exists()) {
                        Obstacledge.ConfigManager manager = new Obstacledge.ConfigManager();
                        if (manager.loadFromFile(configFile.getAbsolutePath())) {
                            Obstacledge.Plot plot = manager.getPlotById(currentBoundaryLandNumber.trim());
                            if (plot != null && plot.getObstacles() != null && !plot.getObstacles().isEmpty()) {
                                obstaclesCoords = Obstacledge.buildPlannerPayload(plot.getObstacles());
                            }
                        }
                    }
                } catch (Exception e) {
                    // 忽略障碍物加载错误
                }
            }
        }
        // 如果无法从地块获取边界,尝试使用当前显示的边界
        if (boundaryCoords == null || boundaryCoords.trim().isEmpty() || "-1".equals(boundaryCoords.trim())) {
            if (currentBoundary != null && !currentBoundary.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < currentBoundary.size(); i++) {
                    Point2D.Double pt = currentBoundary.get(i);
                    if (i > 0) sb.append(";");
                    sb.append(String.format(java.util.Locale.US, "%.3f,%.3f", pt.x, pt.y));
                }
                boundaryCoords = sb.toString();
            }
        }
        // 转换割草宽度从厘米到米(如果存在)
        if (mowingWidth != null && !mowingWidth.trim().isEmpty() && !"-1".equals(mowingWidth.trim())) {
            try {
                double widthCm = Double.parseDouble(mowingWidth.trim());
                double widthMeters = widthCm / 100.0;
                mowingWidth = String.format(java.util.Locale.US, "%.3f", widthMeters);
            } catch (NumberFormatException e) {
                // 如果已经是米为单位,保持原值
            }
        }
        // 转换安全距离从厘米到米(如果存在)
        if (safetyDistance != null && !safetyDistance.trim().isEmpty() && !"-1".equals(safetyDistance.trim())) {
            try {
                double distCm = Double.parseDouble(safetyDistance.trim());
                // 如果值大于100,认为是厘米,需要转换为米
                if (distCm > 100) {
                    double distMeters = distCm / 100.0;
                    safetyDistance = String.format(java.util.Locale.US, "%.3f", distMeters);
                }
            } catch (NumberFormatException e) {
                // 如果已经是米为单位,保持原值
            }
        }
        // 如果从地块获取到了路径,使用地块的路径;否则使用currentPlannedPath
        List<Point2D.Double> pathToDraw = currentPlannedPath;
        if (plannedPathStr != null && !plannedPathStr.trim().isEmpty() && !"-1".equals(plannedPathStr.trim())) {
            // 从地块获取的路径
            pathToDraw = lujingdraw.parsePlannedPath(plannedPathStr);
        }
        // 调用带地块信息的绘制方法
        if (pathToDraw != null && pathToDraw.size() >= 2) {
            lujingdraw.drawPlannedPath(g2d, pathToDraw, scale, arrowScale,
                                   boundaryCoords, mowingWidth, safetyDistance, obstaclesCoords, mowingPattern);
        }
    }
    private void drawCircleSampleMarkers(Graphics2D g2d, List<double[]> markers, double scale) {
@@ -1472,7 +1562,7 @@
        FontMetrics metrics = g2d.getFontMetrics(labelFont);
        
        for (double[] pt : markers) {
            if (pt == null || pt.length < 2 || !Double.isFinite(pt[0]) || !Double.isFinite(pt[1])) {
            if (pt == null || pt.length < 2 || !isFinite(pt[0]) || !isFinite(pt[1])) {
                continue;
            }
            double x = pt[0];
@@ -1570,7 +1660,7 @@
                }
                double x = pt[0];
                double y = pt[1];
                if (!Double.isFinite(x) || !Double.isFinite(y)) {
                if (!isFinite(x) || !isFinite(y)) {
                    continue;
                }
                circleSampleMarkers.add(new double[]{x, y});
@@ -1793,7 +1883,7 @@
    private double computeSelectionThresholdPixels() {
        double scaleFactor = Math.max(0.5, scale);
        double diameterScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
        if (!Double.isFinite(diameterScale) || diameterScale <= 0.0d) {
        if (!isFinite(diameterScale) || diameterScale <= 0.0d) {
            diameterScale = 1.0d;
        }
        double markerDiameterWorld = Math.max(1.0, (10.0 / scaleFactor) * 0.2 * diameterScale);
@@ -1882,6 +1972,7 @@
                   .append(',')
                   .append(formatCoordinate(point.y));
            if (i < boundary.size() - 1) {
                builder.append(';');
            }
        }
@@ -1957,7 +2048,7 @@
    }
    private boolean isPointInsideActiveBoundary(Point2D.Double point) {
        if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
        if (point == null || !isFinite(point.x) || !isFinite(point.y)) {
            return false;
        }
        if (realtimeTrackLandNumber == null) {
@@ -1968,7 +2059,7 @@
    }
    private boolean isPointInsideBoundary(Point2D.Double point, Path2D.Double path) {
        if (point == null || path == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
        if (point == null || path == null || !isFinite(point.x) || !isFinite(point.y)) {
            return false;
        }
        if (path.contains(point.x, point.y)) {
@@ -1989,7 +2080,7 @@
        Path2D.Double path = new Path2D.Double();
        boolean started = false;
        for (Point2D.Double point : boundary) {
            if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
            if (point == null || !isFinite(point.x) || !isFinite(point.y)) {
                continue;
            }
            if (!started) {
@@ -2025,7 +2116,7 @@
        // 设置点的大小(随缩放变化)
        double scaleFactor = Math.max(0.5, scale);
        double clampedScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
        if (!Double.isFinite(clampedScale) || clampedScale <= 0.0d) {
        if (!isFinite(clampedScale) || clampedScale <= 0.0d) {
            clampedScale = 1.0d;
        }
        double minimumDiameter = clampedScale < 1.0 ? 0.5 : 1.0;
@@ -2474,7 +2565,7 @@
            }
            double x = coord.getX();
            double y = coord.getY();
            if (!Double.isFinite(x) || !Double.isFinite(y)) {
            if (!isFinite(x) || !isFinite(y)) {
                continue;
            }
            copy.add(new Obstacledge.XYCoordinate(x, y));
@@ -2812,7 +2903,7 @@
    }
    public void setBoundaryPointSizeScale(double sizeScale) {
        double normalized = (Double.isFinite(sizeScale) && sizeScale > 0.0d) ? sizeScale : 1.0d;
        double normalized = (isFinite(sizeScale) && sizeScale > 0.0d) ? sizeScale : 1.0d;
        if (Math.abs(boundaryPointSizeScale - normalized) < 1e-6) {
            return;
        }
@@ -2840,7 +2931,7 @@
    }
    public void setBoundaryPreviewMarkerScale(double markerScale) {
        double normalized = Double.isFinite(markerScale) && markerScale > 0.0d ? markerScale : 1.0d;
        double normalized = isFinite(markerScale) && markerScale > 0.0d ? markerScale : 1.0d;
        if (Math.abs(boundaryPreviewMarkerScale - normalized) < 1e-6) {
            return;
        }
@@ -2877,7 +2968,7 @@
    }
    public void addHandheldBoundaryPoint(double x, double y) {
        if (!Double.isFinite(x) || !Double.isFinite(y)) {
        if (!isFinite(x) || !isFinite(y)) {
            return;
        }
        if (!handheldBoundaryPreviewActive) {
@@ -2989,8 +3080,8 @@
        Point2D.Double mowerPosition = mower.getPosition();
        if (mowerPosition == null
            || !Double.isFinite(mowerPosition.x)
            || !Double.isFinite(mowerPosition.y)) {
            || !isFinite(mowerPosition.x)
            || !isFinite(mowerPosition.y)) {
            return expanded;
        }
@@ -3155,18 +3246,45 @@
            Color purpleFill = new Color(128, 0, 128, 80); // 紫色半透明填充
            Color purpleBorder = new Color(128, 0, 128, 255); // 紫色边框
            bianjiedrwa.drawBoundary(g2d, previewOriginalBoundary, scale, purpleFill, purpleBorder);
            // 如果隐藏了优化边界,显示原始边界坐标点(深绿色实心圆圈)
            if (showOnlyOriginalBoundary) {
                drawOriginalBoundaryPointsWithNumbers(g2d, previewOriginalBoundary, scale);
            }
        }
        
        // 绘制优化后边界(绿色,与正常边界颜色一致)
        if (previewOptimizedBoundary != null && previewOptimizedBoundary.size() >= 2) {
            bianjiedrwa.drawBoundary(g2d, previewOptimizedBoundary, scale, GRASS_FILL_COLOR, GRASS_BORDER_COLOR);
            // 绘制优化后边界坐标点(紫色实心圆圈,显示序号)
            drawOptimizedBoundaryPointsWithNumbers(g2d, previewOptimizedBoundary, scale);
        // 根据标志决定是否绘制优化后边界
        if (!showOnlyOriginalBoundary) {
            // 绘制优化后边界(绿色,与正常边界颜色一致)
            if (previewOptimizedBoundary != null && previewOptimizedBoundary.size() >= 2) {
                bianjiedrwa.drawBoundary(g2d, previewOptimizedBoundary, scale, GRASS_FILL_COLOR, GRASS_BORDER_COLOR);
                // 绘制优化后边界坐标点(紫色实心圆圈,显示序号)
                drawOptimizedBoundaryPointsWithNumbers(g2d, previewOptimizedBoundary, scale);
            }
        }
    }
    
    /**
     * 设置是否只显示原始边界
     * @param showOnlyOriginal 如果为true,只显示原始边界;如果为false,显示原始边界和优化边界
     */
    public void setShowOnlyOriginalBoundary(boolean showOnlyOriginal) {
        this.showOnlyOriginalBoundary = showOnlyOriginal;
        if (visualizationPanel != null) {
            visualizationPanel.repaint();
        }
    }
    /**
     * 获取是否只显示原始边界
     * @return 如果只显示原始边界返回true,否则返回false
     */
    public boolean isShowOnlyOriginalBoundary() {
        return showOnlyOriginalBoundary;
    }
    /**
     * 绘制优化后边界坐标点(紫色实心圆圈,显示序号)
     * 序号显示在点中心,字体大小11号,不随缩放变化
     */
@@ -3240,6 +3358,79 @@
    }
    
    /**
     * 绘制原始边界坐标点(深绿色实心圆圈,显示序号)
     * 只在隐藏优化边界时显示
     */
    private void drawOriginalBoundaryPointsWithNumbers(Graphics2D g2d, List<Point2D.Double> boundary, double scale) {
        if (boundary == null || boundary.isEmpty()) {
            return;
        }
        // 保存原始变换
        AffineTransform originalTransform = g2d.getTransform();
        // 设置点的大小(实心圆圈,直径约0.3米,与优化后边界坐标点大小一致)
        double scaleFactor = Math.max(0.5, scale);
        double markerDiameter = 0.3; // 圆圈直径(米)
        double markerRadius = markerDiameter / 2.0;
        // 设置字体(11号,不随缩放变化)
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 11);
        g2d.setFont(labelFont);
        FontMetrics fontMetrics = g2d.getFontMetrics(labelFont);
        // 深绿色实心圆圈颜色
        Color darkGreenColor = new Color(0, 100, 0, 255); // 深绿色
        // 绘制每个点及其序号
        for (int i = 0; i < boundary.size(); i++) {
            Point2D.Double point = boundary.get(i);
            double x = point.x;
            double y = point.y;
            // 绘制深绿色实心圆圈(在世界坐标系中,随缩放变化)
            g2d.setColor(darkGreenColor);
            Ellipse2D.Double marker = new Ellipse2D.Double(
                x - markerRadius,
                y - markerRadius,
                markerDiameter,
                markerDiameter
            );
            g2d.fill(marker);
            // 将世界坐标转换为屏幕坐标以绘制序号(不随缩放变化)
            Point2D.Double worldPoint = new Point2D.Double(x, y);
            Point2D.Double screenPoint = new Point2D.Double();
            originalTransform.transform(worldPoint, screenPoint);
            // 保存当前变换
            AffineTransform savedTransform = g2d.getTransform();
            // 重置变换为屏幕坐标系统
            g2d.setTransform(new AffineTransform());
            // 绘制序号(在屏幕坐标系中,不随缩放变化)
            String numberText = String.valueOf(i + 1);
            int textWidth = fontMetrics.stringWidth(numberText);
            int textHeight = fontMetrics.getHeight();
            // 在点中心绘制序号
            int textX = (int)(screenPoint.x - textWidth / 2.0);
            int textY = (int)(screenPoint.y + textHeight / 4.0);
            // 绘制序号文字(黑色)
            g2d.setColor(Color.BLACK);
            g2d.drawString(numberText, textX, textY);
            // 恢复变换
            g2d.setTransform(savedTransform);
        }
        // 恢复原始变换
        g2d.setTransform(originalTransform);
    }
    /**
     * 处理优化后边界坐标点点击
     * @param screenPoint 屏幕坐标点
     * @return 是否处理了点击
@@ -3334,10 +3525,31 @@
    /**
     * 设置边界预览更新回调
     */
    private java.util.function.Consumer<String> boundaryPreviewUpdateCallback;
    private Consumer<String> boundaryPreviewUpdateCallback;
    
    public void setBoundaryPreviewUpdateCallback(java.util.function.Consumer<String> callback) {
    public void setBoundaryPreviewUpdateCallback(Consumer<String> callback) {
        this.boundaryPreviewUpdateCallback = callback;
    }
}
    /**
     * 将视图中心对准当前边界的几何中心
     */
    public void centerViewOnBoundary() {
        if (currentBoundary == null || currentBoundary.isEmpty()) {
            return;
        }
        Rectangle2D.Double bounds = computeBounds(currentBoundary);
        if (bounds != null) {
            fitBoundsToView(bounds);
        }
    }
    /**
     * 检查double值是否有限(不是NaN或无穷大)
     * 兼容低版本Java
     */
    private static boolean isFinite(double value) {
        return !Double.isNaN(value) && !Double.isInfinite(value);
    }
}