package lujing; import java.util.*; import java.util.Set; import java.util.HashSet; /** * 异形草地路径规划 - 凹多边形兼容优化版 V5.0 * 修复:解决凹多边形扫描线跨越边界的问题,优化路径对齐 */ public class YixinglujingNoObstacle { // 用法说明(无障碍物路径规划): // - 方法用途:根据地块边界、割草宽度与安全边距,生成覆盖全区域的割草路径。 // - 参数: // coordinates:地块边界坐标字符串,格式 "x1,y1;x2,y2;...",至少3个点,单位为米。 // widthStr:割草宽度(字符串,单位米),用于确定扫描线间距。 // marginStr:安全边距(字符串,单位米),用于将地块边界向内收缩,避免贴边作业。 // - 返回值:List,其中 PathSegment.start/end 为坐标点,isMowing 为 true 表示割草段,false 表示空走段。 // - 失败情况:当边界点不足或内缩后区域过小,返回空列表。 // - 使用示例: // String boundary = "0,0;20,0;20,15;0,15"; // String width = "0.3"; // String margin = "0.5"; // List path = // YixinglujingNoObstacle.planPath(boundary, width, margin); public static List planPath(String coordinates, String widthStr, String marginStr) { List 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 boundary = getInsetPolygon(rawPoints, safeMargin); if (boundary.size() < 3) return new ArrayList<>(); // 3. 确定最优作业角度 double bestAngle = findOptimalAngle(boundary); // 4. 获取首个作业点,用于对齐围边起点 Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle); // 5. 对齐围边:使围边最后结束于靠近扫描起点的位置 List alignedBoundary = alignBoundaryStart(boundary, firstScanStart); List finalPath = new ArrayList<>(); // 6. 第一阶段:围边路径 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)); } // 7. 第二阶段:生成内部扫描路径(修复凹部空越问题) Point lastEdgePos = alignedBoundary.get(0); List scanPath = generateGlobalScanPath(boundary, mowWidth, bestAngle, lastEdgePos); 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 generateGlobalScanPath(List polygon, double width, double angle, Point currentPos) { List segments = new ArrayList<>(); List 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; // 步长 y 从最小到最大扫描 for (double y = minY + width/2; y <= maxY - width/2; y += width) { List xIntersections = getXIntersections(rotatedPoly, y); if (xIntersections.size() < 2) continue; Collections.sort(xIntersections); // 处理凹多边形:每两个点组成一个有效作业段 List lineSegmentsInRow = 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); lineSegmentsInRow.add(new PathSegment(pS, pE, true)); } // 根据当前S型方向排序作业段 if (!leftToRight) { Collections.reverse(lineSegmentsInRow); 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,添加空走路径 addSafeConnection(segments, currentPos, s.start, polygon); } segments.add(s); currentPos = s.end; } leftToRight = !leftToRight; } return segments; } private static Point getFirstScanPoint(List polygon, double width, double angle) { List 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/2; List xInter = getXIntersections(rotatedPoly, firstY); if (xInter.isEmpty()) return polygon.get(0); Collections.sort(xInter); return rotatePoint(new Point(xInter.get(0), firstY), angle); } private static List alignBoundaryStart(List 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 aligned = new ArrayList<>(); for (int i = 0; i < boundary.size(); i++) { aligned.add(boundary.get((bestIdx + i) % boundary.size())); } return aligned; } private static List getXIntersections(List rotatedPoly, double y) { List 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 (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); // 简单去重:检查是否已存在相近的点 boolean isDuplicate = false; for (double existingX : xIntersections) { if (Math.abs(x - existingX) < tolerance) { isDuplicate = true; break; } } if (!isDuplicate) { xIntersections.add(x); } } } return xIntersections; } private static double findOptimalAngle(List 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 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; } public static List getInsetPolygon(List points, double margin) { List 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); // 限制最大位移量,防止极尖角畸变 dist = Math.min(dist, margin * 5); result.add(new Point(pCurr.x + bisectorX * dist, pCurr.y + bisectorY * dist)); } return result; } private static void addSafeConnection(List segments, Point start, Point end, List polygon) { if (isSegmentSafe(start, end, polygon)) { segments.add(new PathSegment(start, end, false)); } else { List 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 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 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 getBoundaryPath(Point start, Point end, List polygon) { int idx1 = getEdgeIndex(start, polygon); int idx2 = getEdgeIndex(end, polygon); if (idx1 == -1 || idx2 == -1 || idx1 == idx2) { return Arrays.asList(start, end); } List 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 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 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 poly) { for (int i = 0; i < poly.size(); i++) { Point p1 = poly.get(i); Point p2 = poly.get((i + 1) % poly.size()); if (distToSegment(p, p1, p2) < 1e-3) return i; } return -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); } public static void ensureCounterClockwise(List 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 parseCoordinates(String coordinates) { List 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; // true: 割草中, false: 空载移动 public PathSegment(Point s, Point e, boolean m) { this.start = s; this.end = e; this.isMowing = m; } } }