From 45a35805eb1e59972ad9a9c80815f2c030dc69bb Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 26 十二月 2025 16:13:14 +0800
Subject: [PATCH] 割草机新增了属性
---
src/lujing/YixinglujingHaveObstacel.java | 161 ++++++++++++++++++++++++++++++-----------------------
1 files changed, 92 insertions(+), 69 deletions(-)
diff --git a/src/lujing/YixinglujingHaveObstacel.java b/src/lujing/YixinglujingHaveObstacel.java
index 2a36445..ff0736a 100644
--- a/src/lujing/YixinglujingHaveObstacel.java
+++ b/src/lujing/YixinglujingHaveObstacel.java
@@ -6,8 +6,11 @@
import java.util.List;
/**
- * 寮傚舰鑽夊湴璺緞瑙勫垝 - 閬块殰澧炲己鐗� V7.0
- * 浼樺寲锛氬鍔犱簡澶氳竟褰㈠鎵╃ǔ瀹氭�с�侀殰纰嶇墿纰版挒棰勫垽浠ュ強鍐椾綑璺緞娑堥櫎銆�
+ * 寮傚舰鑽夊湴璺緞瑙勫垝 - 閬块殰澧炲己鐗� V8.0
+ * 淇璇存槑锛�
+ * 1. 淇浜嗗湴鍧楀唴缂╁拰闅滅鐗╁鎵╃殑姝h礋閫昏緫銆�
+ * 2. 浼樺寲浜嗗杈瑰舰鍋忕Щ绠楁硶锛岀‘淇濋�嗘椂閽堢偣搴忎笅姝e�煎唴缂╋紝璐熷�煎鎵┿��
+ * 3. 澧炲己浜嗛殰纰嶇墿瑙f瀽鐨勫仴澹�с��
*/
public class YixinglujingHaveObstacel {
@@ -18,32 +21,99 @@
double mowWidth = Double.parseDouble(widthStr);
double safeMargin = Double.parseDouble(marginStr);
- // 1. 棰勫鐞嗗湴鍧楋紙纭繚閫嗘椂閽堬級
+ // 1. 棰勫鐞嗗湴鍧楋紙纭繚閫嗘椂閽堥『搴忥級
ensureCounterClockwise(rawPoints);
- List<Point> boundary = getOffsetPolygon(rawPoints, -safeMargin); // 鍐呯缉
+
+ // 銆愭牳蹇冧慨澶嶃�戯細瀵逛簬閫嗘椂閽堝杈瑰舰锛屾鏁版槸鍚戝唴鍋忕Щ锛圛nset锛�
+ List<Point> boundary = getOffsetPolygon(rawPoints, safeMargin);
if (boundary.size() < 3) return new ArrayList<>();
- // 2. 瑙勫垝鍩虹璺緞 (鏃犻殰纰嶇墿鐘舵��)
+ // 2. 纭畾鏈�浼樿搴﹀苟瑙勫垝鍩虹璺緞
double bestAngle = findOptimalAngle(boundary);
Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle);
List<Point> alignedBoundary = alignBoundaryStart(boundary, firstScanStart);
List<PathSegment> baseLines = new ArrayList<>();
- // 绗竴闃舵锛氬洿杈�
+ // 绗竴闃舵锛氬洿杈硅矾寰�
for (int i = 0; i < alignedBoundary.size(); i++) {
baseLines.add(new PathSegment(alignedBoundary.get(i), alignedBoundary.get((i + 1) % alignedBoundary.size()), true));
}
- // 绗簩闃舵锛氬唴閮ㄦ壂鎻�
+ // 绗簩闃舵锛氱敓鎴愬唴閮ㄦ壂鎻忚矾寰�
Point lastEdgePos = alignedBoundary.get(0);
baseLines.addAll(generateGlobalScanPath(boundary, mowWidth, bestAngle, lastEdgePos));
- // 3. 澶勭悊闅滅鐗╋細瑙f瀽骞舵墽琛屽鎵� (闅滅鐗╅渶鍚戝鎵� margin)
+ // 3. 澶勭悊闅滅鐗╋細瑙f瀽骞舵墽琛屻�愬鎵┿��
+ // 銆愭牳蹇冧慨澶嶃�戯細瀵逛簬閫嗘椂閽堥殰纰嶇墿锛岃礋鏁版槸鍚戝鍋忕Щ锛圤utset锛�
List<Obstacle> obstacles = parseObstacles(obstaclesStr, safeMargin);
// 4. 璺緞瑁佸壀涓庝紭鍖栬繛鎺�
return optimizeAndClipPath(baseLines, obstacles);
}
+ private static List<Obstacle> parseObstacles(String obsStr, double margin) {
+ List<Obstacle> obstacles = new ArrayList<>();
+ if (obsStr == null || obsStr.trim().isEmpty()) return obstacles;
+
+ for (String group : obsStr.split("\\$")) {
+ List<Point> pts = parseCoordinates(group);
+ if (pts.isEmpty()) continue;
+
+ if (pts.size() == 2) {
+ // 鍦嗗舰闅滅鐗╋細绗竴涓偣蹇冿紝绗簩涓偣涓婁竴鐐癸紝鍗婂緞澧炲姞 margin
+ double r = Math.hypot(pts.get(0).x - pts.get(1).x, pts.get(0).y - pts.get(1).y);
+ obstacles.add(new CircleObstacle(pts.get(0), r + margin));
+ } else if (pts.size() > 2) {
+ // 澶氳竟褰㈤殰纰嶇墿锛氱‘淇濋�嗘椂閽堬紝鐒跺悗浣跨敤璐� margin 杩涜銆愬鎵┿��
+ ensureCounterClockwise(pts);
+ obstacles.add(new PolyObstacle(getOffsetPolygon(pts, -margin)));
+ }
+ }
+ return obstacles;
+ }
+
+ /**
+ * 澶氳竟褰㈠亸绉荤畻娉曪細鍩轰簬瑙掑钩鍒嗙嚎鍋忕Щ
+ * 鍦ㄩ�嗘椂閽堥『搴忎笅锛歰ffset > 0 涓哄唴缂╋紝offset < 0 涓哄鎵�
+ */
+ private static List<Point> getOffsetPolygon(List<Point> points, double offset) {
+ List<Point> result = new ArrayList<>();
+ int n = points.size();
+ for (int i = 0; i < n; i++) {
+ Point p1 = points.get((i - 1 + n) % n);
+ Point p2 = points.get(i);
+ Point p3 = points.get((i + 1) % n);
+
+ double v1x = p2.x - p1.x, v1y = p2.y - p1.y;
+ double v2x = p3.x - p2.x, v2y = p3.y - p2.y;
+ double l1 = Math.hypot(v1x, v1y), l2 = Math.hypot(v2x, v2y);
+
+ if (l1 < 1e-6 || l2 < 1e-6) continue;
+
+ // 鑾峰彇涓ゆ潯杈圭殑娉曞悜閲忥紙鍚戝乏鍋忕Щ锛�
+ double n1x = -v1y / l1, n1y = v1x / l1;
+ double n2x = -v2y / l2, n2y = v2x / l2;
+
+ // 瑙掑钩鍒嗙嚎鍚戦噺
+ double bx = n1x + n2x, by = n1y + n2y;
+ double bl = Math.hypot(bx, by);
+ if (bl < 1e-6) {
+ bx = n1x; by = n1y;
+ } else {
+ bx /= bl; by /= bl;
+ }
+
+ // 璁$畻鍋忕Щ闀垮害淇绯绘暟锛�1/sin(theta/2)
+ double cosHalf = n1x * bx + n1y * by;
+ double d = offset / Math.max(cosHalf, 0.1); // 閬垮厤鍒嗘瘝杩囧皬瀵艰嚧鏃犵┓澶�
+
+ // 闄愬埗鏈�澶т綅绉婚噺锛岄槻姝㈡瀬灏栬鐣稿彉
+ d = Math.signum(offset) * Math.min(Math.abs(d), Math.abs(offset) * 5);
+
+ result.add(new Point(p2.x + bx * d, p2.y + by * d));
+ }
+ return result;
+ }
+
private static List<PathSegment> optimizeAndClipPath(List<PathSegment> originalPath, List<Obstacle> obstacles) {
List<PathSegment> result = new ArrayList<>();
Point currentPos = null;
@@ -52,6 +122,7 @@
List<PathSegment> clipped = new ArrayList<>();
clipped.add(segment);
+ // 鐢ㄦ瘡涓�涓殰纰嶇墿渚濇瑁佸壀
for (Obstacle obs : obstacles) {
List<PathSegment> nextIter = new ArrayList<>();
for (PathSegment s : clipped) {
@@ -61,11 +132,11 @@
}
for (PathSegment s : clipped) {
- // 浼樺寲鐐癸細娑堥櫎闀垮害鍑犱箮涓�0鐨勬棤鏁堢嚎娈�
+ // 鍓旈櫎寰皬娈�
if (Math.hypot(s.start.x - s.end.x, s.start.y - s.end.y) < 1e-4) continue;
+ // 濡傛灉鏂版鐨勮捣鐐逛笌涓婃鐨勭粓鐐逛笉杩炶疮锛屾坊鍔犵┖璧帮紙闈炲壊鑽夛級璺緞
if (currentPos != null && Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
- // 娣诲姞绌鸿浇璺緞
result.add(new PathSegment(currentPos, s.start, false));
}
result.add(s);
@@ -75,7 +146,7 @@
return result;
}
- // --- 闅滅鐗╂ā鍨� ---
+ // --- 闅滅鐗╃被瀹氫箟 ---
abstract static class Obstacle {
abstract boolean isInside(Point p);
abstract List<PathSegment> clipSegment(PathSegment seg);
@@ -87,7 +158,6 @@
public PolyObstacle(List<Point> pts) {
this.points = pts;
- // 棰勮绠� AABB 杈圭晫妗嗘彁鍗囨晥鐜�
minX = minY = Double.MAX_VALUE;
maxX = maxY = -Double.MAX_VALUE;
for (Point p : pts) {
@@ -121,6 +191,7 @@
for (int i = 0; i < ts.size() - 1; i++) {
Point s = interpolate(seg.start, seg.end, ts.get(i));
Point e = interpolate(seg.start, seg.end, ts.get(i + 1));
+ // 妫�鏌ヤ腑鐐规槸鍚﹀湪闅滅鐗╁唴
if (!isInside(new Point((s.x + e.x) / 2, (s.y + e.y) / 2))) {
res.add(new PathSegment(s, e, seg.isMowing));
}
@@ -162,57 +233,7 @@
}
}
- // --- 绠楁硶宸ュ叿绫� ---
-
- private static List<Obstacle> parseObstacles(String obsStr, double margin) {
- List<Obstacle> obstacles = new ArrayList<>();
- if (obsStr == null || obsStr.trim().isEmpty()) return obstacles;
- for (String group : obsStr.split("\\$")) {
- List<Point> pts = parseCoordinates(group);
- if (pts.size() == 2) {
- double r = Math.hypot(pts.get(0).x - pts.get(1).x, pts.get(0).y - pts.get(1).y);
- obstacles.add(new CircleObstacle(pts.get(0), r + margin));
- } else if (pts.size() > 2) {
- ensureCounterClockwise(pts);
- // 澶氳竟褰㈠鎵╋細offset 涓烘
- obstacles.add(new PolyObstacle(getOffsetPolygon(pts, margin)));
- }
- }
- return obstacles;
- }
-
- /**
- * 浼樺寲鍚庣殑澶氳竟褰㈠鎵�/鍐呯缉绠楁硶
- * @param offset 姝f暟涓哄鎵╋紝璐熸暟涓哄唴缂�
- */
- private static List<Point> getOffsetPolygon(List<Point> points, double offset) {
- List<Point> result = new ArrayList<>();
- int n = points.size();
- for (int i = 0; i < n; i++) {
- Point p1 = points.get((i - 1 + n) % n), p2 = points.get(i), p3 = points.get((i + 1) % n);
-
- double v1x = p2.x - p1.x, v1y = p2.y - p1.y;
- double v2x = p3.x - p2.x, v2y = p3.y - p2.y;
- double l1 = Math.hypot(v1x, v1y), l2 = Math.hypot(v2x, v2y);
-
- if (l1 < 1e-6 || l2 < 1e-6) continue;
-
- // 娉曞悜閲�
- double n1x = -v1y / l1, n1y = v1x / l1;
- double n2x = -v2y / l2, n2y = v2x / l2;
-
- // 瑙掑钩鍒嗙嚎
- double bx = n1x + n2x, by = n1y + n2y;
- double bl = Math.hypot(bx, by);
- if (bl < 1e-6) { bx = n1x; by = n1y; } else { bx /= bl; by /= bl; }
-
- // 淇璺濈
- double sinHalf = n1x * bx + n1y * by;
- double d = offset / Math.max(sinHalf, 0.1);
- result.add(new Point(p2.x + bx * d, p2.y + by * d));
- }
- return result;
- }
+ // --- 鍐呴儴绠楁硶涓庢暟瀛︽敮鎸� ---
private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
List<PathSegment> segments = new ArrayList<>();
@@ -250,7 +271,6 @@
return segments;
}
- // --- 鍩虹鏁板鍑芥暟 ---
private static double getIntersectionT(Point a, Point b, Point c, Point d) {
double ux = b.x - a.x, uy = b.y - a.y, vx = d.x - c.x, vy = d.y - c.y;
double det = vx * uy - vy * ux;
@@ -263,7 +283,8 @@
}
private static Point rotatePoint(Point p, double ang) {
- return new Point(p.x * Math.cos(ang) - p.y * Math.sin(ang), p.x * Math.sin(ang) + p.y * Math.cos(ang));
+ double cos = Math.cos(ang), sin = Math.sin(ang);
+ return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
}
private static List<Double> getXIntersections(List<Point> poly, double y) {
@@ -304,20 +325,22 @@
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 h = 0, miY = Double.MAX_VALUE, maY = -Double.MAX_VALUE;
+ double miY = Double.MAX_VALUE, maY = -Double.MAX_VALUE;
for (Point p : poly) {
Point r = rotatePoint(p, -a);
miY = Math.min(miY, r.y); maY = Math.max(maY, r.y);
}
- h = maY - miY;
- if (h < minH) { minH = h; bestA = a; }
+ if (maY - miY < minH) { minH = maY - miY; bestA = a; }
}
return bestA;
}
private static void ensureCounterClockwise(List<Point> pts) {
double s = 0;
- for (int i = 0; i < pts.size(); i++) s += (pts.get((i + 1) % pts.size()).x - pts.get(i).x) * (pts.get((i + 1) % pts.size()).y + pts.get(i).y);
+ for (int i = 0; i < pts.size(); i++) {
+ Point p1 = pts.get(i), p2 = pts.get((i + 1) % pts.size());
+ s += (p2.x - p1.x) * (p2.y + p1.y);
+ }
if (s > 0) Collections.reverse(pts);
}
--
Gitblit v1.10.0