826220679@qq.com
8 小时以前 d58d5e1baf9390d4119f48c4af3fbcf308da10a3
src/lujing/YixinglujingNoObstacle.java
@@ -59,10 +59,32 @@
        
        finalPath.addAll(scanPath);
        // 8. 格式化坐标:保留两位小数
        for (PathSegment segment : finalPath) {
            segment.start.x = Math.round(segment.start.x * 100.0) / 100.0;
            segment.start.y = Math.round(segment.start.y * 100.0) / 100.0;
            segment.end.x = Math.round(segment.end.x * 100.0) / 100.0;
            segment.end.y = Math.round(segment.end.y * 100.0) / 100.0;
        }
        return finalPath;
    }
    private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
        // 先尝试将凹陷处视为两个独立区域,分两次扫描,避免跨区直线连接
        List<PathSegment> all = new ArrayList<>();
        // 第一次扫描:优先处理左侧区域(groupIndex=0)
        List<PathSegment> leftScan = generateScanPathForSide(polygon, width, angle, currentPos, 0);
        all.addAll(leftScan);
        Point posAfterLeft = leftScan.isEmpty() ? currentPos : leftScan.get(leftScan.size() - 1).end;
        // 第二次扫描:处理右侧区域(groupIndex=1),从左侧结束点沿边界到右侧首段
        List<PathSegment> rightScan = generateScanPathForSide(polygon, width, angle, posAfterLeft, 1);
        all.addAll(rightScan);
        return all;
    }
    // 仅扫描指定侧(同一条扫描线的第 groupIndex 段),用于将“耳朵”视为独立区域
    private static List<PathSegment> generateScanPathForSide(List<Point> polygon, double width, double angle, Point currentPos, int sideIndex) {
        List<PathSegment> segments = new ArrayList<>();
        List<Point> rotatedPoly = new ArrayList<>();
        for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
@@ -74,37 +96,46 @@
        }
        boolean leftToRight = true;
        // 步长 y 从最小到最大扫描
        boolean firstSegmentConnected = false;
        for (double y = minY + width/2; y <= maxY - width/2; y += width) {
            List<Double> xIntersections = getXIntersections(rotatedPoly, y);
            if (xIntersections.size() < 2) continue;
            Collections.sort(xIntersections);
            // 处理凹多边形:每两个点组成一个有效作业段
            // 构建本行的作业段(左到右)和组索引
            List<PathSegment> lineSegmentsInRow = new ArrayList<>();
            for (int i = 0; i < xIntersections.size() - 1; i += 2) {
            List<Integer> groupIndices = new ArrayList<>();
            for (int i = 0, g = 0; i < xIntersections.size() - 1; i += 2, g++) {
                Point pS = rotatePoint(new Point(xIntersections.get(i), y), angle);
                Point pE = rotatePoint(new Point(xIntersections.get(i + 1), y), angle);
                lineSegmentsInRow.add(new PathSegment(pS, pE, true));
                groupIndices.add(g);
            }
            // 根据当前S型方向排序作业段
            if (!leftToRight) {
                Collections.reverse(lineSegmentsInRow);
                Collections.reverse(groupIndices);
                for (PathSegment s : lineSegmentsInRow) {
                    Point temp = s.start; s.start = s.end; s.end = temp;
                }
            }
            // 将作业段连接到总路径
            for (PathSegment s : lineSegmentsInRow) {
                if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
                    // 如果间距大于1cm,添加空走路径
                    segments.add(new PathSegment(currentPos, s.start, false));
                }
                segments.add(s);
                currentPos = s.end;
            int idxInRow = groupIndices.indexOf(sideIndex);
            if (idxInRow == -1) {
                // 本行不包含该侧的作业段,跳过
                leftToRight = !leftToRight;
                continue;
            }
            PathSegment s = lineSegmentsInRow.get(idxInRow);
            // 首次连接或跨区连接均强制沿边界,避免穿越凹陷区
            if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
                addBoundaryConnection(segments, currentPos, s.start, polygon);
                firstSegmentConnected = true;
            }
            segments.add(s);
            currentPos = s.end;
            leftToRight = !leftToRight;
        }
        return segments;
@@ -139,12 +170,31 @@
    private static List<Double> getXIntersections(List<Point> rotatedPoly, double y) {
        List<Double> xIntersections = new ArrayList<>();
        double tolerance = 1e-6;
        for (int i = 0; i < rotatedPoly.size(); i++) {
            Point p1 = rotatedPoly.get(i);
            Point p2 = rotatedPoly.get((i + 1) % rotatedPoly.size());
            if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
            // 跳过水平边(避免与扫描线重合时的特殊情况)
            if (Math.abs(p1.y - p2.y) < tolerance) {
                continue;
            }
            // 检查是否相交(使用严格不等式避免顶点重复)
            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);
                xIntersections.add(x);
                // 简单去重:检查是否已存在相近的点
                boolean isDuplicate = false;
                for (double existingX : xIntersections) {
                    if (Math.abs(x - existingX) < tolerance) {
                        isDuplicate = true;
                        break;
                    }
                }
                if (!isDuplicate) {
                    xIntersections.add(x);
                }
            }
        }
        return xIntersections;
@@ -171,7 +221,7 @@
        return maxY - minY;
    }
    private static List<Point> getInsetPolygon(List<Point> points, double margin) {
    public 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++) {
@@ -207,12 +257,131 @@
        return result;
    }
    private static void addSafeConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
        if (isSegmentSafe(start, end, polygon)) {
            segments.add(new PathSegment(start, end, false));
        } else {
            List<Point> path = getBoundaryPath(start, end, polygon);
            for (int i = 0; i < path.size() - 1; i++) {
                segments.add(new PathSegment(path.get(i), path.get(i+1), false));
            }
        }
    }
    // 强制沿边界绕行的连接(不做直线安全判断),用来在同一扫描行的多个作业段之间跳转
    private static void addBoundaryConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
        List<Point> path = getBoundaryPath(start, end, polygon);
        for (int i = 0; i < path.size() - 1; i++) {
            segments.add(new PathSegment(path.get(i), path.get(i+1), false));
        }
    }
    private static boolean isSegmentSafe(Point p1, Point p2, List<Point> polygon) {
        Point mid = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
        if (!isPointInPolygon(mid, polygon)) return false;
        for (int i = 0; i < polygon.size(); i++) {
            Point a = polygon.get(i);
            Point b = polygon.get((i + 1) % polygon.size());
            if (isSamePoint(p1, a) || isSamePoint(p1, b) || isSamePoint(p2, a) || isSamePoint(p2, b)) continue;
            if (segmentsIntersect(p1, p2, a, b)) return false;
        }
        return true;
    }
    private static boolean isSamePoint(Point a, Point b) {
        return Math.abs(a.x - b.x) < 1e-4 && Math.abs(a.y - b.y) < 1e-4;
    }
    private static boolean segmentsIntersect(Point a, Point b, Point c, Point d) {
        return ccw(a, c, d) != ccw(b, c, d) && ccw(a, b, c) != ccw(a, b, d);
    }
    private static boolean ccw(Point a, Point b, Point c) {
        return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x);
    }
    private static boolean isPointInPolygon(Point p, List<Point> polygon) {
        boolean result = false;
        for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
            if ((polygon.get(i).y > p.y) != (polygon.get(j).y > p.y) &&
                (p.x < (polygon.get(j).x - polygon.get(i).x) * (p.y - polygon.get(i).y) / (polygon.get(j).y - polygon.get(i).y) + polygon.get(i).x)) {
                result = !result;
            }
        }
        return result;
    }
    private static List<Point> getBoundaryPath(Point start, Point end, List<Point> polygon) {
        int idx1 = getEdgeIndex(start, polygon);
        int idx2 = getEdgeIndex(end, polygon);
        if (idx1 == -1 || idx2 == -1 || idx1 == idx2) {
            return Arrays.asList(start, end);
        }
        List<Point> path1 = new ArrayList<>();
        path1.add(start);
        int curr = idx1;
        while (curr != idx2) {
            path1.add(polygon.get((curr + 1) % polygon.size()));
            curr = (curr + 1) % polygon.size();
        }
        path1.add(end);
        List<Point> pathRev = new ArrayList<>();
        pathRev.add(start);
        curr = idx1;
        while (curr != idx2) {
            pathRev.add(polygon.get(curr));
            curr = (curr - 1 + polygon.size()) % polygon.size();
        }
        pathRev.add(polygon.get((idx2 + 1) % polygon.size()));
        pathRev.add(end);
        return getPathLength(path1) < getPathLength(pathRev) ? path1 : pathRev;
    }
    private static double getPathLength(List<Point> path) {
        double len = 0;
        for (int i = 0; i < path.size() - 1; i++) {
            len += Math.hypot(path.get(i).x - path.get(i+1).x, path.get(i).y - path.get(i+1).y);
        }
        return len;
    }
    private static int getEdgeIndex(Point p, List<Point> poly) {
        int bestIdx = -1;
        double minD = Double.MAX_VALUE;
        for (int i = 0; i < poly.size(); i++) {
            Point p1 = poly.get(i);
            Point p2 = poly.get((i + 1) % poly.size());
            double d = distToSegment(p, p1, p2);
            if (d < minD) {
                minD = d;
                bestIdx = i;
            }
        }
        // 只要找到最近的边即可,放宽阈值以应对浮点误差和旋转变形
        // 如果距离过大(例如超过1米),可能确实不在边界上,但在路径规划上下文中,
        // 这些点是由扫描线生成的,理论上一定在边界上,所以强制吸附是安全的。
        return minD < 1.0 ? bestIdx : -1;
    }
    private static double distToSegment(Point p, Point s, Point e) {
        double l2 = (s.x - e.x)*(s.x - e.x) + (s.y - e.y)*(s.y - e.y);
        if (l2 == 0) return Math.hypot(p.x - s.x, p.y - s.y);
        double t = ((p.x - s.x) * (e.x - s.x) + (p.y - s.y) * (e.y - s.y)) / l2;
        t = Math.max(0, Math.min(1, t));
        return Math.hypot(p.x - (s.x + t * (e.x - s.x)), p.y - (s.y + t * (e.y - s.y)));
    }
    private static Point rotatePoint(Point p, double angle) {
        double cos = Math.cos(angle), sin = Math.sin(angle);
        return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
    }
    private static void ensureCounterClockwise(List<Point> points) {
    public static void ensureCounterClockwise(List<Point> points) {
        double sum = 0;
        for (int i = 0; i < points.size(); i++) {
            Point p1 = points.get(i), p2 = points.get((i + 1) % points.size());