| | |
| | | * 修复:解决凹多边形扫描线跨越边界的问题,优化路径对齐 |
| | | */ |
| | | public class YixinglujingNoObstacle { |
| | | // 开关:是否强制所有非作业连接沿安全边界行走(避免任何内区直线跨越) |
| | | // 改为可动态设置,自动依据地块形状启用 |
| | | private static boolean FORCE_BOUNDARY_TRAVEL = true; |
| | | // 用法说明(无障碍物路径规划): |
| | | // - 方法用途:根据地块边界、割草宽度与安全边距,生成覆盖全区域的割草路径。 |
| | | // - 参数: |
| | |
| | | |
| | | // 3. 确定最优作业角度 |
| | | double bestAngle = findOptimalAngle(boundary); |
| | | |
| | | // 3.1 自动判断是否需要强制沿边界旅行(检测凹部/多段扫描行) |
| | | FORCE_BOUNDARY_TRAVEL = shouldForceBoundaryTravel(boundary, mowWidth, bestAngle); |
| | | |
| | | // 4. 获取首个作业点,用于对齐围边起点 |
| | | Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle); |
| | |
| | | |
| | | finalPath.addAll(scanPath); |
| | | |
| | | // 8. 格式化坐标:保留两位小数 |
| | | // 8. 最终安全净化:确保所有段在内缩边界上或内部(自动贴边阈值) |
| | | sanitizePath(finalPath, boundary, mowWidth, safeMargin); |
| | | |
| | | // 9. 格式化坐标:保留两位小数 |
| | | 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; |
| | |
| | | return finalPath; |
| | | } |
| | | |
| | | // 对所有路径段进行安全净化: |
| | | // - 非作业段:统一沿边界路径替换 |
| | | // - 作业段:若端点在外或段与边界相交,吸附端点到边界并向内侧偏移 epsilon |
| | | private static void sanitizePath(List<PathSegment> segments, List<Point> polygon, double width, double margin) { |
| | | double epsilon = computeAutoInnerOffset(polygon, width, margin); |
| | | List<PathSegment> sanitized = new ArrayList<>(); |
| | | for (PathSegment s : segments) { |
| | | boolean startInside = isPointInPolygon(s.start, polygon); |
| | | boolean endInside = isPointInPolygon(s.end, polygon); |
| | | boolean intersects = segmentIntersectsBoundary(s.start, s.end, polygon); |
| | | if (!s.isMowing) { |
| | | // 非作业段统一替换为沿边界路径 |
| | | List<Point> path = getBoundaryPathWithSnap(s.start, s.end, polygon); |
| | | for (int i = 0; i < path.size() - 1; i++) { |
| | | sanitized.add(new PathSegment(path.get(i), path.get(i+1), false)); |
| | | } |
| | | } else { |
| | | if (!startInside || !endInside || intersects) { |
| | | SnapResult s1 = snapToBoundary(s.start, polygon); |
| | | SnapResult s2 = snapToBoundary(s.end, polygon); |
| | | Point p1 = pushInsideOnEdge(s1, polygon, epsilon); |
| | | Point p2 = pushInsideOnEdge(s2, polygon, epsilon); |
| | | sanitized.add(new PathSegment(p1, p2, true)); |
| | | } else { |
| | | sanitized.add(s); |
| | | } |
| | | } |
| | | } |
| | | segments.clear(); |
| | | segments.addAll(sanitized); |
| | | } |
| | | |
| | | private static boolean segmentIntersectsBoundary(Point a, Point b, List<Point> polygon) { |
| | | for (int i = 0; i < polygon.size(); i++) { |
| | | Point c = polygon.get(i); |
| | | Point d = polygon.get((i + 1) % polygon.size()); |
| | | // 忽略共享端点的相交 |
| | | if (isSamePoint(a, c) || isSamePoint(a, d) || isSamePoint(b, c) || isSamePoint(b, d)) continue; |
| | | if (segmentsIntersect(a, b, c, d)) return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | // 将边界上的投影点向内侧偏移 epsilon |
| | | private static Point pushInsideOnEdge(SnapResult sr, List<Point> poly, double epsilon) { |
| | | int i = sr.edgeIndex; |
| | | Point s = poly.get(i); |
| | | Point e = poly.get((i + 1) % poly.size()); |
| | | double dx = e.x - s.x, dy = e.y - s.y; |
| | | double len = Math.hypot(dx, dy); |
| | | if (len < 1e-6) return sr.onEdge; |
| | | // 对于逆时针(CCW)多边形,左转法向量 (-dy, dx) 指向内侧 |
| | | double nx = -dy / len, ny = dx / len; |
| | | return new Point(sr.onEdge.x + nx * epsilon, sr.onEdge.y + ny * epsilon); |
| | | } |
| | | |
| | | // 自动计算贴边内偏移阈值 epsilon:根据地块尺度、最短边、割草宽度与安全边距综合估算 |
| | | private static double computeAutoInnerOffset(List<Point> polygon, double width, double margin) { |
| | | double minEdge = Double.MAX_VALUE; |
| | | double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE; |
| | | double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE; |
| | | for (int i = 0; i < polygon.size(); i++) { |
| | | Point a = polygon.get(i); |
| | | Point b = polygon.get((i + 1) % polygon.size()); |
| | | minEdge = Math.min(minEdge, Math.hypot(a.x - b.x, a.y - b.y)); |
| | | minX = Math.min(minX, a.x); minY = Math.min(minY, a.y); |
| | | maxX = Math.max(maxX, a.x); maxY = Math.max(maxY, a.y); |
| | | } |
| | | double diag = Math.hypot(maxX - minX, maxY - minY); |
| | | // 基础量:数值稳定需要的最小内偏移(取割幅的1%与对角线的0.2%之间的较大值) |
| | | double base = Math.max(width * 0.01, diag * 0.002); |
| | | // 上限:不超过安全边距的20%与割幅的10% |
| | | double upper = Math.min(margin * 0.2, width * 0.1); |
| | | // 受边长约束:不超过最短边的2% |
| | | double edgeLimit = minEdge * 0.02; |
| | | double eps = Math.min(upper, Math.max(base, edgeLimit * 0.5)); |
| | | // 下限/上限最终钳位:3mm 到 5cm |
| | | eps = Math.max(0.003, Math.min(eps, 0.05)); |
| | | return eps; |
| | | } |
| | | |
| | | // 根据扫描行的交点数量来判断是否存在“多段行”(>=2段),有凹部或窄通道时启用强制沿边界旅行 |
| | | private static boolean shouldForceBoundaryTravel(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, maxY = -Double.MAX_VALUE; |
| | | for (Point p : rotatedPoly) { minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y); } |
| | | |
| | | int multiSegmentRows = 0; |
| | | int totalRows = 0; |
| | | for (double y = minY + width/2; y <= maxY - width/2; y += width) { |
| | | List<Double> xIntersections = getXIntersections(rotatedPoly, y); |
| | | if (xIntersections.size() < 2) continue; |
| | | totalRows++; |
| | | if (xIntersections.size() >= 4) multiSegmentRows++; // 同一行出现两个及以上作业段 |
| | | } |
| | | // 只要出现过“多段行”,就强制沿边界旅行;也可按比例阈值触发(例如 >=10%) |
| | | if (multiSegmentRows > 0) return true; |
| | | double ratio = totalRows == 0 ? 0.0 : (double) multiSegmentRows / (double) totalRows; |
| | | return ratio >= 0.1; // 兜底阈值 |
| | | } |
| | | |
| | | 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)); |
| | |
| | | } |
| | | |
| | | 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,添加空走路径 |
| | | addSafeConnection(segments, currentPos, s.start, polygon); |
| | | } |
| | | 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; |
| | |
| | | } |
| | | |
| | | private static void addSafeConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) { |
| | | if (isSegmentSafe(start, end, polygon)) { |
| | | if (!FORCE_BOUNDARY_TRAVEL && 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)); |
| | | } |
| | | return; |
| | | } |
| | | List<Point> path = getBoundaryPathWithSnap(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 = getBoundaryPathWithSnap(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 List<Point> getBoundaryPathWithSnap(Point start, Point end, List<Point> polygon) { |
| | | SnapResult s1 = snapToBoundary(start, polygon); |
| | | SnapResult s2 = snapToBoundary(end, polygon); |
| | | int n = polygon.size(); |
| | | |
| | | // 前向路径(顺边) |
| | | List<Point> pathFwd = new ArrayList<>(); |
| | | pathFwd.add(start); |
| | | pathFwd.add(s1.onEdge); |
| | | int curr = s1.edgeIndex; |
| | | while (curr != s2.edgeIndex) { |
| | | pathFwd.add(polygon.get((curr + 1) % n)); |
| | | curr = (curr + 1) % n; |
| | | } |
| | | pathFwd.add(s2.onEdge); |
| | | pathFwd.add(end); |
| | | |
| | | // 反向路径(逆边) |
| | | List<Point> pathRev = new ArrayList<>(); |
| | | pathRev.add(start); |
| | | pathRev.add(s1.onEdge); |
| | | curr = s1.edgeIndex; |
| | | while (curr != s2.edgeIndex) { |
| | | pathRev.add(polygon.get(curr)); |
| | | curr = (curr - 1 + n) % n; |
| | | } |
| | | pathRev.add(s2.onEdge); |
| | | pathRev.add(end); |
| | | |
| | | return getPathLength(pathFwd) < getPathLength(pathRev) ? pathFwd : pathRev; |
| | | } |
| | | |
| | | private static class SnapResult { |
| | | Point onEdge; |
| | | int edgeIndex; |
| | | SnapResult(Point p, int idx) { this.onEdge = p; this.edgeIndex = idx; } |
| | | } |
| | | |
| | | // 计算点到边界最近的投影点以及所在边索引 |
| | | private static SnapResult snapToBoundary(Point p, List<Point> poly) { |
| | | double minD = Double.MAX_VALUE; |
| | | Point bestProj = p; |
| | | int bestIdx = -1; |
| | | for (int i = 0; i < poly.size(); i++) { |
| | | Point s = poly.get(i); |
| | | Point e = poly.get((i + 1) % poly.size()); |
| | | double l2 = (s.x - e.x)*(s.x - e.x) + (s.y - e.y)*(s.y - e.y); |
| | | if (l2 == 0) { |
| | | double d = Math.hypot(p.x - s.x, p.y - s.y); |
| | | if (d < minD) { minD = d; bestProj = s; bestIdx = i; } |
| | | continue; |
| | | } |
| | | 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)); |
| | | Point proj = new Point(s.x + t * (e.x - s.x), s.y + t * (e.y - s.y)); |
| | | double d = Math.hypot(p.x - proj.x, p.y - proj.y); |
| | | if (d < minD) { minD = d; bestProj = proj; bestIdx = i; } |
| | | } |
| | | return new SnapResult(bestProj, bestIdx == -1 ? 0 : bestIdx); |
| | | } |
| | | |
| | | private static boolean isSegmentSafe(Point p1, Point p2, List<Point> polygon) { |