张世豪
19 小时以前 f4a2ce6f608049dc11f00908ee8a829060882de3
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package lujing;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * 凸形草地路径规划 (避障修复版)
 * 修复重点:强化跨行及避障后的路径连贯性,确保每一段切割都有显式的移动路径连接。
 */
public class AoxinglujingHaveObstacel {
 
    private static final double EPSILON = 1e-6;
 
    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("%.6f,%.6f", x, y); }
    }
 
    public static class PathSegment {
        public Point start, end;
        public boolean isMowing;
        public PathSegment(Point start, Point end, boolean isMowing) {
            this.start = start; this.end = end; this.isMowing = isMowing;
        }
    }
 
    public abstract static class Obstacle {
        public abstract boolean isInside(Point p);
        public abstract List<Double> getIntersectionsX(double y, double angle);
    }
 
    public static class PolygonObstacle extends Obstacle {
        public List<Point> points;
        public PolygonObstacle(List<Point> points) { this.points = points; }
        @Override
        public boolean isInside(Point p) {
            boolean result = false;
            for (int i = 0, j = points.size() - 1; i < points.size(); j = i++) {
                if ((points.get(i).y > p.y) != (points.get(j).y > p.y) &&
                    (p.x < (points.get(j).x - points.get(i).x) * (p.y - points.get(i).y) / (points.get(j).y - points.get(i).y) + points.get(i).x)) {
                    result = !result;
                }
            }
            return result;
        }
        @Override
        public List<Double> getIntersectionsX(double y, double angle) {
            List<Point> rotated = rotatePolygon(this.points, -angle);
            List<Double> xInts = new ArrayList<>();
            for (int i = 0; i < rotated.size(); i++) {
                Point p1 = rotated.get(i), p2 = rotated.get((i + 1) % rotated.size());
                if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
                    xInts.add(p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y));
                }
            }
            return xInts;
        }
    }
 
    public static class CircleObstacle extends Obstacle {
        public Point center;
        public double radius;
        public CircleObstacle(Point center, double radius) { this.center = center; this.radius = radius; }
        @Override
        public boolean isInside(Point p) { return Math.hypot(p.x - center.x, p.y - center.y) <= radius + EPSILON; }
        @Override
        public List<Double> getIntersectionsX(double y, double angle) {
            List<Double> xInts = new ArrayList<>();
            Point rCenter = rotatePoint(center, -angle);
            double dy = Math.abs(y - rCenter.y);
            if (dy < radius) {
                double dx = Math.sqrt(radius * radius - dy * dy);
                xInts.add(rCenter.x - dx); xInts.add(rCenter.x + dx);
            }
            return xInts;
        }
    }
 
    public static List<PathSegment> planPath(String boundaryStr, String obstacleStr, String widthStr, String marginStr) {
        List<Point> boundary = parseCoords(boundaryStr);
        double width = Double.parseDouble(widthStr);
        double margin = Double.parseDouble(marginStr);
        List<Obstacle> obstacles = parseObstacles(obstacleStr, margin);
        return planPathCore(boundary, obstacles, width, margin);
    }
 
    private static List<PathSegment> planPathCore(List<Point> boundary, List<Obstacle> obstacles, double width, double margin) {
        if (boundary.size() < 3) return new ArrayList<>();
        ensureCCW(boundary);
        List<Point> workArea = shrinkPolygon(boundary, margin);
        if (workArea.size() < 3) return new ArrayList<>();
 
        double bestAngle = findOptimalScanAngle(workArea);
        Point firstScanStart = getFirstScanStartPoint(workArea, bestAngle, width);
        List<Point> alignedWorkArea = alignBoundaryToStart(workArea, firstScanStart);
 
        List<PathSegment> finalPath = new ArrayList<>();
        // 1. 添加围边路径
        for (int i = 0; i < alignedWorkArea.size(); i++) {
            finalPath.add(new PathSegment(alignedWorkArea.get(i), alignedWorkArea.get((i + 1) % alignedWorkArea.size()), true));
        }
 
        // 2. 生成内部弓字形 (强化连接逻辑)
        Point currentPos = alignedWorkArea.get(0);
        List<PathSegment> zigZag = generateFixedZigZag(workArea, obstacles, bestAngle, width, currentPos);
        finalPath.addAll(zigZag);
 
        return finalPath;
    }
 
    private static List<PathSegment> generateFixedZigZag(List<Point> polygon, List<Obstacle> obstacles, double angle, double width, Point startPoint) {
        List<PathSegment> result = new ArrayList<>();
        List<Point> rotatedPoly = rotatePolygon(polygon, -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); }
 
        Point currentPos = startPoint;
        boolean leftToRight = true;
 
        for (double y = minY + width; y < maxY - width/2; y += width) {
            List<Double> polyInts = getXIntersections(rotatedPoly, y);
            if (polyInts.size() < 2) continue;
            Collections.sort(polyInts);
            double xMin = polyInts.get(0), xMax = polyInts.get(polyInts.size() - 1);
 
            // 收集所有分割点(边界 + 障碍物交点)
            List<Double> splits = new ArrayList<>();
            splits.add(xMin); splits.add(xMax);
            for (Obstacle obs : obstacles) {
                for (double ox : obs.getIntersectionsX(y, angle)) {
                    if (ox > xMin + EPSILON && ox < xMax - EPSILON) splits.add(ox);
                }
            }
            Collections.sort(splits);
 
            // 构建本行候选段
            List<Double[]> rowSegments = new ArrayList<>();
            for (int i = 0; i < splits.size() - 1; i++) {
                double s = splits.get(i), e = splits.get(i+1);
                Point mid = rotatePoint(new Point((s + e) / 2.0, y), angle);
                if (!isPointInAnyObstacle(mid, obstacles)) {
                    rowSegments.add(new Double[]{s, e});
                }
            }
 
            // 根据当前S型方向排序
            if (!leftToRight) {
                Collections.reverse(rowSegments);
                for (Double[] seg : rowSegments) { double t = seg[0]; seg[0] = seg[1]; seg[1] = t; }
            }
 
            // 执行连接:强制检查 currentPos 到每一段起点的距离
            for (Double[] seg : rowSegments) {
                Point p1 = rotatePoint(new Point(seg[0], y), angle);
                Point p2 = rotatePoint(new Point(seg[1], y), angle);
 
                // 核心修复:无论多近,只要不是同一点,就建立虚线连接,确保路径流转
                if (dist(currentPos, p1) > 0.001) {
                    result.add(new PathSegment(currentPos, p1, false));
                }
                result.add(new PathSegment(p1, p2, true));
                currentPos = p2;
            }
            leftToRight = !leftToRight;
        }
        return result;
    }
 
    private static boolean isPointInAnyObstacle(Point p, List<Obstacle> obstacles) {
        for (Obstacle obs : obstacles) if (obs.isInside(p)) return true;
        return false;
    }
 
    private static double dist(Point p1, Point p2) {
        return Math.hypot(p1.x - p2.x, p1.y - p2.y);
    }
 
    // --- 辅助工具 (解析与变换) ---
    private static List<Obstacle> parseObstacles(String obsStr, double margin) {
        List<Obstacle> list = new ArrayList<>();
        if (obsStr == null || obsStr.isEmpty()) return list;
        for (String part : obsStr.split("\\$")) {
            List<Point> pts = parseCoords(part);
            if (pts.size() == 2) {
                double r = dist(pts.get(0), pts.get(1));
                list.add(new CircleObstacle(pts.get(0), r + margin));
            } else if (pts.size() > 2) {
                ensureCCW(pts);
                list.add(new PolygonObstacle(expandPolygon(pts, margin)));
            }
        }
        return list;
    }
 
    private static List<Point> expandPolygon(List<Point> poly, double margin) {
        List<Point> res = new ArrayList<>();
        int n = poly.size();
        for (int i = 0; i < n; i++) {
            Point p1 = poly.get((i - 1 + n) % n), p2 = poly.get(i), p3 = poly.get((i + 1) % n);
            double d1x = p2.x - p1.x, d1y = p2.y - p1.y;
            double d2x = p3.x - p2.x, d2y = p3.y - p2.y;
            double l1 = Math.hypot(d1x, d1y), l2 = Math.hypot(d2x, d2y);
            double n1x = d1y / l1, n1y = -d1x / l1;
            double n2x = d2y / l2, n2y = -d2x / l2;
            double bx = n1x + n2x, by = n1y + n2y;
            double bLen = Math.hypot(bx, by);
            if (bLen < EPSILON) { bx = n1x; by = n1y; } else { bx /= bLen; by /= bLen; }
            double d = margin / Math.max(n1x * bx + n1y * by, 0.1);
            res.add(new Point(p2.x + bx * Math.min(d, margin * 2), p2.y + by * Math.min(d, margin * 2)));
        }
        return res;
    }
 
    private static List<Point> parseCoords(String s) {
        List<Point> list = new ArrayList<>();
        for (String p : s.split(";")) {
            String[] xy = p.split(",");
            if (xy.length >= 2) list.add(new Point(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
        }
        return list;
    }
 
    private static void ensureCCW(List<Point> poly) {
        double s = 0;
        for (int i = 0; i < poly.size(); i++) {
            Point p1 = poly.get(i), p2 = poly.get((i + 1) % poly.size());
            s += (p2.x - p1.x) * (p2.y + p1.y);
        }
        if (s > 0) Collections.reverse(poly);
    }
 
    private static List<Point> shrinkPolygon(List<Point> poly, double margin) {
        List<Point> res = new ArrayList<>();
        int n = poly.size();
        for (int i = 0; i < n; i++) {
            Point p1 = poly.get((i - 1 + n) % n), p2 = poly.get(i), p3 = poly.get((i + 1) % n);
            double d1x = p2.x - p1.x, d1y = p2.y - p1.y;
            double d2x = p3.x - p2.x, d2y = p3.y - p2.y;
            double l1 = Math.hypot(d1x, d1y), l2 = Math.hypot(d2x, d2y);
            double n1x = -d1y / l1, n1y = d1x / l1;
            double n2x = -d2y / l2, n2y = d2x / l2;
            double bx = n1x + n2x, by = n1y + n2y;
            double bLen = Math.hypot(bx, by);
            if (bLen < EPSILON) { bx = n1x; by = n1y; } else { bx /= bLen; by /= bLen; }
            double d = margin / Math.max(n1x * bx + n1y * by, 0.1);
            res.add(new Point(p2.x + bx * d, p2.y + by * d));
        }
        return res;
    }
 
    private static double findOptimalScanAngle(List<Point> poly) {
        double minH = Double.MAX_VALUE, bestA = 0;
        for (int i = 0; i < poly.size(); i++) {
            Point p1 = poly.get(i), p2 = poly.get((i + 1) % poly.size());
            double a = Math.atan2(p2.y - p1.y, p2.x - p1.x);
            double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
            double s = Math.sin(-a), c = Math.cos(-a);
            for (Point p : poly) { double ry = p.x * s + p.y * c; minY = Math.min(minY, ry); maxY = Math.max(maxY, ry); }
            if (maxY - minY < minH) { minH = maxY - minY; bestA = a; }
        }
        return bestA;
    }
 
    private static List<Double> getXIntersections(List<Point> rotatedPoly, double y) {
        List<Double> xInts = new ArrayList<>();
        for (int i = 0; i < rotatedPoly.size(); i++) {
            Point p1 = rotatedPoly.get(i), p2 = rotatedPoly.get((i + 1) % rotatedPoly.size());
            if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
                xInts.add(p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y));
            }
        }
        return xInts;
    }
 
    private static Point getFirstScanStartPoint(List<Point> poly, double angle, double width) {
        List<Point> rot = rotatePolygon(poly, -angle);
        double minY = Double.MAX_VALUE;
        for (Point p : rot) minY = Math.min(minY, p.y);
        double sy = minY + width + EPSILON;
        List<Double> x = getXIntersections(rot, sy);
        Collections.sort(x);
        return rotatePoint(new Point(x.isEmpty() ? 0 : x.get(0), sy), angle);
    }
 
    private static List<Point> alignBoundaryToStart(List<Point> poly, Point target) {
        int idx = 0; double minD = Double.MAX_VALUE;
        for (int i = 0; i < poly.size(); i++) {
            double d = dist(poly.get(i), target);
            if (d < minD) { minD = d; idx = i; }
        }
        List<Point> res = new ArrayList<>();
        for (int i = 0; i < poly.size(); i++) res.add(poly.get((idx + i) % poly.size()));
        return res;
    }
 
    private static Point rotatePoint(Point p, double a) {
        double c = Math.cos(a), s = Math.sin(a);
        return new Point(p.x * c - p.y * s, p.x * s + p.y * c);
    }
 
    private static List<Point> rotatePolygon(List<Point> poly, double a) {
        List<Point> res = new ArrayList<>();
        for (Point p : poly) res.add(rotatePoint(p, a));
        return res;
    }
}