| | |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 异形草地路径规划 - 围边+全局扫描版 V4.1 |
| | | * 优化:围边终点与弓字形起点自动对齐,实现无缝切换,确保路径不越界 |
| | | * 异形草地路径规划 - 凹多边形兼容优化版 V5.0 |
| | | * 修复:解决凹多边形扫描线跨越边界的问题,优化路径对齐 |
| | | */ |
| | | public class YixinglujingNoObstacle { |
| | | |
| | |
| | | double mowWidth = Double.parseDouble(widthStr); |
| | | double safeMargin = Double.parseDouble(marginStr); |
| | | |
| | | // 1. 预处理:逆时针化 |
| | | // 1. 预处理:确保逆时针顺序 |
| | | ensureCounterClockwise(rawPoints); |
| | | |
| | | // 2. 生成内缩多边形 |
| | | // 2. 生成内缩多边形(安全边界) |
| | | List<Point> boundary = getInsetPolygon(rawPoints, safeMargin); |
| | | if (boundary.size() < 3) return new ArrayList<>(); |
| | | |
| | | // 3. 确定最优扫描角度并找到弓字形路径的第一个作业起点 |
| | | // 3. 确定最优作业角度 |
| | | double bestAngle = findOptimalAngle(boundary); |
| | | |
| | | // 4. 获取首个作业点,用于对齐围边起点 |
| | | Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle); |
| | | |
| | | // 4. 对齐围边起点:重新排列围边坐标,使最后一个点靠近(或等于)扫描起点 |
| | | // 5. 对齐围边:使围边最后结束于靠近扫描起点的位置 |
| | | List<Point> alignedBoundary = alignBoundaryStart(boundary, firstScanStart); |
| | | |
| | | List<PathSegment> finalPath = new ArrayList<>(); |
| | | |
| | | // 5. 【第一步】生成围边路径 |
| | | // 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)); |
| | | } |
| | | |
| | | // 6. 【第二步】从对齐后的终点开始生成内部扫描路径 |
| | | Point lastEdgePos = alignedBoundary.get(0); // 围边闭合回到起点 |
| | | // 7. 第二阶段:生成内部扫描路径(修复凹部空越问题) |
| | | 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<>(); |
| | |
| | | } |
| | | |
| | | boolean leftToRight = true; |
| | | // 从 minY + width 开始,避开围边已割区域 |
| | | for (double y = minY + width; y <= maxY - width/2; y += width) { |
| | | // 步长 y 从最小到最大扫描 |
| | | 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> lineRows = new ArrayList<>(); |
| | | // 处理凹多边形:每两个点组成一个有效作业段 |
| | | List<PathSegment> 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); |
| | | lineRows.add(new PathSegment(pS, pE, true)); |
| | | lineSegmentsInRow.add(new PathSegment(pS, pE, true)); |
| | | } |
| | | |
| | | // 根据当前S型方向排序作业段 |
| | | if (!leftToRight) { |
| | | Collections.reverse(lineRows); |
| | | for (PathSegment s : lineRows) { |
| | | Point t = s.start; s.start = s.end; s.end = t; |
| | | Collections.reverse(lineSegmentsInRow); |
| | | for (PathSegment s : lineSegmentsInRow) { |
| | | Point temp = s.start; s.start = s.end; s.end = temp; |
| | | } |
| | | } |
| | | |
| | | for (PathSegment s : lineRows) { |
| | | // 如果间距极小,视为无缝衔接 |
| | | if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.05) { |
| | | // 将作业段连接到总路径 |
| | | 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); |
| | |
| | | return segments; |
| | | } |
| | | |
| | | 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/2; |
| | | List<Double> 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<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<Double> getXIntersections(List<Point> rotatedPoly, double y) { |
| | | List<Double> xIntersections = new ArrayList<>(); |
| | | for (int i = 0; i < rotatedPoly.size(); 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; |
| | |
| | | |
| | | public static class PathSegment { |
| | | public Point start, end; |
| | | public boolean isMowing; |
| | | public boolean isMowing; // true: 割草中, false: 空载移动 |
| | | public PathSegment(Point s, Point e, boolean m) { this.start = s; this.end = e; this.isMowing = m; } |
| | | } |
| | | } |