张世豪
3 天以前 dc0fb19555dc01bf873361c9cb6fc22fbfc9671d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package lujing;
 
import java.util.*;
 
/**
 * 异形草地路径规划 - 围边+全局扫描版 V4.1
 * 优化:围边终点与弓字形起点自动对齐,实现无缝切换,确保路径不越界
 */
public class YixinglujingNoObstacle {
 
    public static List<PathSegment> planPath(String coordinates, String widthStr, String marginStr) {
        List<Point> rawPoints = parseCoordinates(coordinates);
        if (rawPoints.size() < 3) return new ArrayList<>();
 
        double mowWidth = Double.parseDouble(widthStr);
        double safeMargin = Double.parseDouble(marginStr);
 
        // 1. 预处理:逆时针化
        ensureCounterClockwise(rawPoints);
        
        // 2. 生成内缩多边形
        List<Point> boundary = getInsetPolygon(rawPoints, safeMargin);
        if (boundary.size() < 3) return new ArrayList<>();
 
        // 3. 确定最优扫描角度并找到弓字形路径的第一个作业起点
        double bestAngle = findOptimalAngle(boundary);
        Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle);
 
        // 4. 对齐围边起点:重新排列围边坐标,使最后一个点靠近(或等于)扫描起点
        List<Point> alignedBoundary = alignBoundaryStart(boundary, firstScanStart);
 
        List<PathSegment> finalPath = new ArrayList<>();
 
        // 5. 【第一步】生成围边路径
        for (int i = 0; i < alignedBoundary.size(); i++) {
            Point pStart = alignedBoundary.get(i);
            Point pEnd = alignedBoundary.get((i + 1) % alignedBoundary.size());
            finalPath.add(new PathSegment(pStart, pEnd, true));
        }
 
        // 6. 【第二步】从对齐后的终点开始生成内部扫描路径
        Point lastEdgePos = alignedBoundary.get(0); // 围边闭合回到起点
        List<PathSegment> scanPath = generateGlobalScanPath(boundary, mowWidth, bestAngle, lastEdgePos);
        
        finalPath.addAll(scanPath);
 
        return finalPath;
    }
 
    /**
     * 计算并获取扫描路径的第一行起点
     */
    private static Point getFirstScanPoint(List<Point> polygon, double width, double angle) {
        List<Point> rotatedPoly = new ArrayList<>();
        for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
 
        double minY = Double.MAX_VALUE;
        for (Point p : rotatedPoly) minY = Math.min(minY, p.y);
 
        double firstY = minY + width; 
        List<Double> xIntersections = getXIntersections(rotatedPoly, firstY);
        
        if (xIntersections.isEmpty()) return polygon.get(0);
        return rotatePoint(new Point(Collections.min(xIntersections), firstY), angle);
    }
 
    /**
     * 重新排列多边形顶点,使起始点与扫描起点对接
     */
    private static List<Point> alignBoundaryStart(List<Point> boundary, Point targetStart) {
        int bestIdx = 0;
        double minDist = Double.MAX_VALUE;
        for (int i = 0; i < boundary.size(); i++) {
            double d = Math.hypot(boundary.get(i).x - targetStart.x, boundary.get(i).y - targetStart.y);
            if (d < minDist) {
                minDist = d;
                bestIdx = i;
            }
        }
        List<Point> aligned = new ArrayList<>();
        for (int i = 0; i < boundary.size(); i++) {
            aligned.add(boundary.get((bestIdx + i) % boundary.size()));
        }
        return aligned;
    }
 
    private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
        List<PathSegment> segments = new ArrayList<>();
        List<Point> rotatedPoly = new ArrayList<>();
        for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
 
        double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
        for (Point p : rotatedPoly) {
            minY = Math.min(minY, p.y);
            maxY = Math.max(maxY, p.y);
        }
 
        boolean leftToRight = true;
        // 从 minY + width 开始,避开围边已割区域
        for (double y = minY + width; y <= maxY - width/2; y += width) {
            List<Double> xIntersections = getXIntersections(rotatedPoly, y);
            if (xIntersections.size() < 2) continue;
            Collections.sort(xIntersections);
 
            List<PathSegment> lineRows = new ArrayList<>();
            for (int i = 0; i < xIntersections.size() - 1; i += 2) {
                Point pS = rotatePoint(new Point(xIntersections.get(i), y), angle);
                Point pE = rotatePoint(new Point(xIntersections.get(i + 1), y), angle);
                lineRows.add(new PathSegment(pS, pE, true));
            }
 
            if (!leftToRight) {
                Collections.reverse(lineRows);
                for (PathSegment s : lineRows) {
                    Point t = s.start; s.start = s.end; s.end = t;
                }
            }
 
            for (PathSegment s : lineRows) {
                // 如果间距极小,视为无缝衔接
                if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.05) {
                    segments.add(new PathSegment(currentPos, s.start, false));
                }
                segments.add(s);
                currentPos = s.end;
            }
            leftToRight = !leftToRight;
        }
        return segments;
    }
 
    private static List<Double> getXIntersections(List<Point> rotatedPoly, double y) {
        List<Double> xIntersections = new ArrayList<>();
        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)) {
                double x = p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
                xIntersections.add(x);
            }
        }
        return xIntersections;
    }
 
    private static double findOptimalAngle(List<Point> polygon) {
        double bestAngle = 0;
        double minHeight = Double.MAX_VALUE;
        for (int i = 0; i < polygon.size(); i++) {
            Point p1 = polygon.get(i), p2 = polygon.get((i + 1) % polygon.size());
            double angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
            double h = calculateHeightAtAngle(polygon, angle);
            if (h < minHeight) { minHeight = h; bestAngle = angle; }
        }
        return bestAngle;
    }
 
    private static double calculateHeightAtAngle(List<Point> poly, double angle) {
        double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
        for (Point p : poly) {
            Point rp = rotatePoint(p, -angle);
            minY = Math.min(minY, rp.y); maxY = Math.max(maxY, rp.y);
        }
        return maxY - minY;
    }
 
    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);
            double d1x = pCurr.x - pPrev.x, d1y = pCurr.y - pPrev.y;
            double l1 = Math.hypot(d1x, d1y);
            double d2x = pNext.x - pCurr.x, d2y = pNext.y - pCurr.y;
            double l2 = Math.hypot(d2x, d2y);
            if (l1 < 1e-6 || l2 < 1e-6) continue;
            double n1x = -d1y / l1, n1y = d1x / l1;
            double n2x = -d2y / l2, n2y = d2x / l2;
            double bisectorX = n1x + n2x, bisectorY = n1y + n2y;
            double bLen = Math.hypot(bisectorX, bisectorY);
            if (bLen < 1e-6) { bisectorX = n1x; bisectorY = n1y; } 
            else { bisectorX /= bLen; bisectorY /= bLen; }
            double cosHalfAngle = n1x * bisectorX + n1y * bisectorY;
            double dist = margin / Math.max(cosHalfAngle, 0.1);
            result.add(new Point(pCurr.x + bisectorX * dist, pCurr.y + bisectorY * dist));
        }
        return result;
    }
 
    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) {
        double sum = 0;
        for (int i = 0; i < points.size(); i++) {
            Point p1 = points.get(i), p2 = points.get((i + 1) % points.size());
            sum += (p2.x - p1.x) * (p2.y + p1.y);
        }
        if (sum > 0) Collections.reverse(points);
    }
 
    private static List<Point> parseCoordinates(String coordinates) {
        List<Point> points = new ArrayList<>();
        String[] pairs = coordinates.split(";");
        for (String pair : pairs) {
            String[] xy = pair.split(",");
            if (xy.length == 2) points.add(new Point(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
        }
        if (points.size() > 1 && points.get(0).equals(points.get(points.size()-1))) points.remove(points.size()-1);
        return points;
    }
 
    public static class Point {
        public double x, y;
        public Point(double x, double y) { this.x = x; this.y = y; }
        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Point)) return false;
            Point p = (Point) o;
            return Math.abs(x - p.x) < 1e-4 && Math.abs(y - p.y) < 1e-4;
        }
    }
 
    public static class PathSegment {
        public Point start, end;
        public boolean isMowing;
        public PathSegment(Point s, Point e, boolean m) { this.start = s; this.end = e; this.isMowing = m; }
    }
}