@anandommi/fields2cover 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +28 -0
- package/README.md +69 -2
- package/index.d.ts +58 -0
- package/index.js +13 -0
- package/lib/coverage.js +741 -0
- package/lib/decomposition.js +41 -0
- package/lib/frame.js +29 -0
- package/lib/geometry.js +269 -0
- package/lib/headland.js +51 -0
- package/lib/headlandLaps.js +576 -0
- package/lib/mowerLoopFallback.js +389 -0
- package/lib/mowerLoopPath.js +178 -0
- package/lib/mowerPath.js +218 -0
- package/lib/obstacleRings.js +50 -0
- package/lib/pathPlanner.js +439 -0
- package/lib/routeOrder.js +56 -0
- package/lib/swaths.js +46 -0
- package/package.json +45 -4
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// Fallback planner for "Join start & end".
|
|
2
|
+
//
|
|
3
|
+
// mowerLoopPath.js builds the connector with a fixed arc-straight-arc template
|
|
4
|
+
// (ported from generate_loop_path.py). That template assumes a free row to the
|
|
5
|
+
// right of the end point, so on irregular boundaries or with obstacles it can
|
|
6
|
+
// leave the safe area. This planner always searches for a forward-only,
|
|
7
|
+
// smooth (Dubins arc/straight) route from the path end to the start pose that
|
|
8
|
+
// stays inside the safe area:
|
|
9
|
+
//
|
|
10
|
+
// 1. short straight lead-out, so the mower does not turn on the spot,
|
|
11
|
+
// 2. direct Dubins connection (all six words, shortest valid one),
|
|
12
|
+
// 3. one intermediate waypoint pose,
|
|
13
|
+
// 4. A* over a coarse pose graph (multiple waypoints),
|
|
14
|
+
//
|
|
15
|
+
// using exactly the configured turning radius (never a tighter one: that is the
|
|
16
|
+
// mower's physical limit). The caller runs one strict validation pass on the
|
|
17
|
+
// result.
|
|
18
|
+
|
|
19
|
+
const { createArea } = require('./geometry');
|
|
20
|
+
|
|
21
|
+
const METERS_PER_DEGREE_LATITUDE = 110540;
|
|
22
|
+
const METERS_PER_DEGREE_LONGITUDE = 111320;
|
|
23
|
+
const TWO_PI = 2 * Math.PI;
|
|
24
|
+
|
|
25
|
+
const CHECK_STEP_M = 0.2;
|
|
26
|
+
const OUTPUT_STEP_M = 0.1;
|
|
27
|
+
const ENDPOINT_GRACE_M = 0; // no exemptions: every sample must be inside the safe area
|
|
28
|
+
const HEADING_COUNT = 8;
|
|
29
|
+
const ONE_HOP_GRID = 34;
|
|
30
|
+
const GRAPH_POINT_LIMIT = 150;
|
|
31
|
+
const MAX_GRAPH_EXPANSIONS = 1200;
|
|
32
|
+
|
|
33
|
+
const mod2pi = (angle) => ((angle % TWO_PI) + TWO_PI) % TWO_PI;
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Dubins (six words), after Shkel & Lumelsky / Walker's dubins.c
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
const L_SEG = 0;
|
|
39
|
+
const S_SEG = 1;
|
|
40
|
+
const R_SEG = 2;
|
|
41
|
+
const WORDS = {
|
|
42
|
+
LSL: [L_SEG, S_SEG, L_SEG],
|
|
43
|
+
RSR: [R_SEG, S_SEG, R_SEG],
|
|
44
|
+
LSR: [L_SEG, S_SEG, R_SEG],
|
|
45
|
+
RSL: [R_SEG, S_SEG, L_SEG],
|
|
46
|
+
RLR: [R_SEG, L_SEG, R_SEG],
|
|
47
|
+
LRL: [L_SEG, R_SEG, L_SEG]
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function wordParams(name, alpha, beta, d) {
|
|
51
|
+
const sa = Math.sin(alpha);
|
|
52
|
+
const sb = Math.sin(beta);
|
|
53
|
+
const ca = Math.cos(alpha);
|
|
54
|
+
const cb = Math.cos(beta);
|
|
55
|
+
const cab = Math.cos(alpha - beta);
|
|
56
|
+
switch (name) {
|
|
57
|
+
case "LSL": {
|
|
58
|
+
const p2 = 2 + d * d - 2 * cab + 2 * d * (sa - sb);
|
|
59
|
+
if (p2 < 0) return null;
|
|
60
|
+
const tmp = Math.atan2(cb - ca, d + sa - sb);
|
|
61
|
+
return [mod2pi(-alpha + tmp), Math.sqrt(p2), mod2pi(beta - tmp)];
|
|
62
|
+
}
|
|
63
|
+
case "RSR": {
|
|
64
|
+
const p2 = 2 + d * d - 2 * cab + 2 * d * (sb - sa);
|
|
65
|
+
if (p2 < 0) return null;
|
|
66
|
+
const tmp = Math.atan2(ca - cb, d - sa + sb);
|
|
67
|
+
return [mod2pi(alpha - tmp), Math.sqrt(p2), mod2pi(-beta + tmp)];
|
|
68
|
+
}
|
|
69
|
+
case "LSR": {
|
|
70
|
+
const p2 = -2 + d * d + 2 * cab + 2 * d * (sa + sb);
|
|
71
|
+
if (p2 < 0) return null;
|
|
72
|
+
const p = Math.sqrt(p2);
|
|
73
|
+
const tmp = Math.atan2(-ca - cb, d + sa + sb) - Math.atan2(-2, p);
|
|
74
|
+
return [mod2pi(-alpha + tmp), p, mod2pi(-mod2pi(beta) + tmp)];
|
|
75
|
+
}
|
|
76
|
+
case "RSL": {
|
|
77
|
+
const p2 = d * d - 2 + 2 * cab - 2 * d * (sa + sb);
|
|
78
|
+
if (p2 < 0) return null;
|
|
79
|
+
const p = Math.sqrt(p2);
|
|
80
|
+
const tmp = Math.atan2(ca + cb, d - sa - sb) - Math.atan2(2, p);
|
|
81
|
+
return [mod2pi(alpha - tmp), p, mod2pi(beta - tmp)];
|
|
82
|
+
}
|
|
83
|
+
case "RLR": {
|
|
84
|
+
const tmp = (6 - d * d + 2 * cab + 2 * d * (sa - sb)) / 8;
|
|
85
|
+
if (Math.abs(tmp) > 1) return null;
|
|
86
|
+
const p = mod2pi(TWO_PI - Math.acos(tmp));
|
|
87
|
+
const t = mod2pi(alpha - Math.atan2(ca - cb, d - sa + sb) + mod2pi(p / 2));
|
|
88
|
+
return [t, p, mod2pi(alpha - beta - t + mod2pi(p))];
|
|
89
|
+
}
|
|
90
|
+
case "LRL": {
|
|
91
|
+
const tmp = (6 - d * d + 2 * cab + 2 * d * (sb - sa)) / 8;
|
|
92
|
+
if (Math.abs(tmp) > 1) return null;
|
|
93
|
+
const p = mod2pi(TWO_PI - Math.acos(tmp));
|
|
94
|
+
const t = mod2pi(-alpha - Math.atan2(ca - cb, d + sa - sb) + p / 2);
|
|
95
|
+
return [t, p, mod2pi(mod2pi(beta) - alpha - t + mod2pi(p))];
|
|
96
|
+
}
|
|
97
|
+
default:
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// All feasible Dubins words between two poses ({x, y, th}), shortest first.
|
|
103
|
+
function dubinsCandidates(from, to, radius) {
|
|
104
|
+
const dx = to.x - from.x;
|
|
105
|
+
const dy = to.y - from.y;
|
|
106
|
+
const d = Math.hypot(dx, dy) / radius;
|
|
107
|
+
const theta = mod2pi(Math.atan2(dy, dx));
|
|
108
|
+
const alpha = mod2pi(from.th - theta);
|
|
109
|
+
const beta = mod2pi(to.th - theta);
|
|
110
|
+
const out = [];
|
|
111
|
+
for (const name of Object.keys(WORDS)) {
|
|
112
|
+
const params = wordParams(name, alpha, beta, d);
|
|
113
|
+
if (!params || params.some((value) => !Number.isFinite(value))) continue;
|
|
114
|
+
out.push({
|
|
115
|
+
from,
|
|
116
|
+
radius,
|
|
117
|
+
types: WORDS[name],
|
|
118
|
+
params,
|
|
119
|
+
length: (params[0] + params[1] + params[2]) * radius
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return out.sort((a, b) => a.length - b.length);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function stepSegment(x, y, th, type, amount) {
|
|
126
|
+
if (type === L_SEG) {
|
|
127
|
+
return [x + Math.sin(th + amount) - Math.sin(th), y - Math.cos(th + amount) + Math.cos(th), th + amount];
|
|
128
|
+
}
|
|
129
|
+
if (type === R_SEG) {
|
|
130
|
+
return [x - Math.sin(th - amount) + Math.sin(th), y + Math.cos(th - amount) - Math.cos(th), th - amount];
|
|
131
|
+
}
|
|
132
|
+
return [x + Math.cos(th) * amount, y + Math.sin(th) * amount, th];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Samples a Dubins word every `step` metres (endpoints included).
|
|
136
|
+
function sampleDubins(word, step) {
|
|
137
|
+
const { from, radius, types, params } = word;
|
|
138
|
+
const total = word.length;
|
|
139
|
+
const count = Math.max(Math.ceil(total / step), 1);
|
|
140
|
+
const points = [];
|
|
141
|
+
for (let index = 0; index <= count; index += 1) {
|
|
142
|
+
let remaining = (index / count) * total / radius;
|
|
143
|
+
let x = 0;
|
|
144
|
+
let y = 0;
|
|
145
|
+
let th = from.th;
|
|
146
|
+
for (let seg = 0; seg < 3; seg += 1) {
|
|
147
|
+
const amount = Math.min(remaining, params[seg]);
|
|
148
|
+
[x, y, th] = stepSegment(x, y, th, types[seg], amount);
|
|
149
|
+
remaining -= amount;
|
|
150
|
+
if (remaining <= 1e-12) break;
|
|
151
|
+
}
|
|
152
|
+
points.push([from.x + x * radius, from.y + y * radius]);
|
|
153
|
+
}
|
|
154
|
+
return points;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
// Safe-area helpers (local metre frame)
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
function buildArea(safeArea, toLocal) {
|
|
161
|
+
const geometry = safeArea?.geometry || safeArea;
|
|
162
|
+
if (!geometry) return null;
|
|
163
|
+
const polygons = geometry.type === "Polygon" ? [geometry.coordinates]
|
|
164
|
+
: geometry.type === "MultiPolygon" ? geometry.coordinates : null;
|
|
165
|
+
if (!polygons) return null;
|
|
166
|
+
const local = polygons.map((rings) => rings.map((ring) => ring.map(toLocal)));
|
|
167
|
+
let minX = Infinity;
|
|
168
|
+
let minY = Infinity;
|
|
169
|
+
let maxX = -Infinity;
|
|
170
|
+
let maxY = -Infinity;
|
|
171
|
+
local.forEach(([outer]) => outer.forEach(([x, y]) => {
|
|
172
|
+
minX = Math.min(minX, x);
|
|
173
|
+
minY = Math.min(minY, y);
|
|
174
|
+
maxX = Math.max(maxX, x);
|
|
175
|
+
maxY = Math.max(maxY, y);
|
|
176
|
+
}));
|
|
177
|
+
// Grid-indexed containment: with many obstacles (holes) a plain scan of
|
|
178
|
+
// every ring per sample point made the connector search take minutes.
|
|
179
|
+
const indexed = createArea(local);
|
|
180
|
+
return {
|
|
181
|
+
bounds: { minX, minY, maxX, maxY },
|
|
182
|
+
contains: (x, y) => indexed.contains(x, y)
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function isInside(area, points, keepClearOf) {
|
|
187
|
+
for (const [x, y] of points) {
|
|
188
|
+
if (keepClearOf.some(([cx, cy]) => Math.hypot(x - cx, y - cy) < ENDPOINT_GRACE_M)) continue;
|
|
189
|
+
if (!area.contains(x, y)) return false;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
// Planning
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
function headingOf(points, fromEnd) {
|
|
198
|
+
const count = points.length;
|
|
199
|
+
for (let i = 1; i < count; i += 1) {
|
|
200
|
+
const a = fromEnd ? points[count - 1 - i] : points[0];
|
|
201
|
+
const b = fromEnd ? points[count - 1] : points[i];
|
|
202
|
+
if (Math.hypot(b[0] - a[0], b[1] - a[1]) > 0.05) return Math.atan2(b[1] - a[1], b[0] - a[0]);
|
|
203
|
+
}
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const advance = (pose, distance) => ({
|
|
208
|
+
x: pose.x + Math.cos(pose.th) * distance,
|
|
209
|
+
y: pose.y + Math.sin(pose.th) * distance,
|
|
210
|
+
th: pose.th
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Shortest word between two poses whose samples all stay in the safe area.
|
|
214
|
+
function bestLeg(area, from, to, radius, keepClearOf) {
|
|
215
|
+
for (const word of dubinsCandidates(from, to, radius)) {
|
|
216
|
+
const points = sampleDubins(word, CHECK_STEP_M);
|
|
217
|
+
if (isInside(area, points, keepClearOf)) return word;
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function poseNodes(area, count, radius) {
|
|
223
|
+
const { minX, minY, maxX, maxY } = area.bounds;
|
|
224
|
+
const spacing = Math.max(radius, Math.max(maxX - minX, maxY - minY) / count);
|
|
225
|
+
const points = [];
|
|
226
|
+
for (let x = minX; x <= maxX; x += spacing) {
|
|
227
|
+
for (let y = minY; y <= maxY; y += spacing) {
|
|
228
|
+
if (area.contains(x, y)) points.push([x, y]);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return points;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function oneHop(area, start, goal, radius, keepClearOf) {
|
|
235
|
+
const points = poseNodes(area, ONE_HOP_GRID, radius);
|
|
236
|
+
const ranked = [];
|
|
237
|
+
for (const [x, y] of points) {
|
|
238
|
+
for (let k = 0; k < HEADING_COUNT; k += 1) {
|
|
239
|
+
const pose = { x, y, th: (k * TWO_PI) / HEADING_COUNT };
|
|
240
|
+
const first = dubinsCandidates(start, pose, radius)[0];
|
|
241
|
+
const second = dubinsCandidates(pose, goal, radius)[0];
|
|
242
|
+
if (first && second) ranked.push({ pose, cost: first.length + second.length });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
ranked.sort((a, b) => a.cost - b.cost);
|
|
246
|
+
for (const { pose } of ranked) {
|
|
247
|
+
const first = bestLeg(area, start, pose, radius, keepClearOf);
|
|
248
|
+
if (!first) continue;
|
|
249
|
+
const second = bestLeg(area, pose, goal, radius, keepClearOf);
|
|
250
|
+
if (second) return [first, second];
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function poseGraph(area, start, goal, radius, keepClearOf) {
|
|
256
|
+
const points = poseNodes(area, Math.sqrt(GRAPH_POINT_LIMIT), radius).slice(0, GRAPH_POINT_LIMIT * 2);
|
|
257
|
+
const nodes = [start, goal];
|
|
258
|
+
points.forEach(([x, y]) => {
|
|
259
|
+
for (let k = 0; k < HEADING_COUNT; k += 1) nodes.push({ x, y, th: (k * TWO_PI) / HEADING_COUNT });
|
|
260
|
+
});
|
|
261
|
+
const heuristic = (node) => Math.hypot(goal.x - node.x, goal.y - node.y);
|
|
262
|
+
const best = new Map([[0, 0]]);
|
|
263
|
+
const cameFrom = new Map();
|
|
264
|
+
const open = [{ index: 0, priority: heuristic(start) }];
|
|
265
|
+
const legs = new Map();
|
|
266
|
+
let expansions = 0;
|
|
267
|
+
|
|
268
|
+
while (open.length && expansions < MAX_GRAPH_EXPANSIONS) {
|
|
269
|
+
open.sort((a, b) => a.priority - b.priority);
|
|
270
|
+
const { index } = open.shift();
|
|
271
|
+
if (index === 1) {
|
|
272
|
+
const route = [];
|
|
273
|
+
for (let at = 1; at !== 0; at = cameFrom.get(at)) route.unshift(legs.get(`${cameFrom.get(at)}>${at}`));
|
|
274
|
+
return route;
|
|
275
|
+
}
|
|
276
|
+
expansions += 1;
|
|
277
|
+
const node = nodes[index];
|
|
278
|
+
for (let next = 1; next < nodes.length; next += 1) {
|
|
279
|
+
if (next === index) continue;
|
|
280
|
+
const target = nodes[next];
|
|
281
|
+
if (Math.hypot(target.x - node.x, target.y - node.y) > 10 * radius && next !== 1) continue;
|
|
282
|
+
const word = dubinsCandidates(node, target, radius)[0];
|
|
283
|
+
if (!word) continue;
|
|
284
|
+
const cost = best.get(index) + word.length;
|
|
285
|
+
if (cost >= (best.get(next) ?? Infinity)) continue;
|
|
286
|
+
const leg = bestLeg(area, node, target, radius, keepClearOf);
|
|
287
|
+
if (!leg) continue;
|
|
288
|
+
const legCost = best.get(index) + leg.length;
|
|
289
|
+
if (legCost >= (best.get(next) ?? Infinity)) continue;
|
|
290
|
+
best.set(next, legCost);
|
|
291
|
+
cameFrom.set(next, index);
|
|
292
|
+
legs.set(`${index}>${next}`, leg);
|
|
293
|
+
open.push({ index: next, priority: legCost + heuristic(target) });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function planLegs(area, start, goal, radius, keepClearOf) {
|
|
300
|
+
const direct = bestLeg(area, start, goal, radius, keepClearOf);
|
|
301
|
+
if (direct) return { legs: [direct], strategy: "direct_dubins" };
|
|
302
|
+
const hop = oneHop(area, start, goal, radius, keepClearOf);
|
|
303
|
+
if (hop) return { legs: hop, strategy: "via_waypoint" };
|
|
304
|
+
const graph = poseGraph(area, start, goal, radius, keepClearOf);
|
|
305
|
+
if (graph) return { legs: graph, strategy: "pose_graph" };
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Builds a smooth, forward-only connector from the last point of `coordinates`
|
|
311
|
+
* to `gap` metres behind the first point (heading along the first segment),
|
|
312
|
+
* staying inside `safeArea`. Returns null when no such route exists.
|
|
313
|
+
*/
|
|
314
|
+
function closeMowerPathFallback(coordinates, { turningRadius, gap = 0, safeArea } = {}) {
|
|
315
|
+
const origin = coordinates[coordinates.length - 1];
|
|
316
|
+
const kx = METERS_PER_DEGREE_LONGITUDE * Math.cos((origin[1] * Math.PI) / 180);
|
|
317
|
+
const toLocal = ([lon, lat]) => [(lon - origin[0]) * kx, (lat - origin[1]) * METERS_PER_DEGREE_LATITUDE];
|
|
318
|
+
const toLonLat = ([x, y]) => [x / kx + origin[0], y / METERS_PER_DEGREE_LATITUDE + origin[1]];
|
|
319
|
+
|
|
320
|
+
const area = buildArea(safeArea, toLocal);
|
|
321
|
+
if (!area) return null;
|
|
322
|
+
|
|
323
|
+
const local = coordinates.map(toLocal);
|
|
324
|
+
const endPose = { x: local[local.length - 1][0], y: local[local.length - 1][1], th: headingOf(local, true) };
|
|
325
|
+
const startTh = headingOf(local, false);
|
|
326
|
+
const startPose = { x: local[0][0], y: local[0][1], th: startTh };
|
|
327
|
+
// Keep as much of the requested gap as fits: the point behind the start can
|
|
328
|
+
// itself fall outside the safe area when the start is near the boundary.
|
|
329
|
+
let goal = startPose;
|
|
330
|
+
for (const fraction of [1, 0.75, 0.5, 0.25, 0]) {
|
|
331
|
+
const candidate = advance(startPose, -gap * fraction);
|
|
332
|
+
if (area.contains(candidate.x, candidate.y)) {
|
|
333
|
+
goal = candidate;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const keepClearOf = [[endPose.x, endPose.y], [goal.x, goal.y]];
|
|
338
|
+
|
|
339
|
+
{
|
|
340
|
+
const radius = turningRadius;
|
|
341
|
+
// Straight lead-out and lead-in keep the turns away from the end/start point.
|
|
342
|
+
for (const lead of [Math.min(turningRadius, 3), 0]) {
|
|
343
|
+
const leadOut = advance(endPose, lead);
|
|
344
|
+
const leadIn = advance(goal, -lead);
|
|
345
|
+
const straightOut = lead ? [[endPose.x, endPose.y], [leadOut.x, leadOut.y]] : [];
|
|
346
|
+
const straightIn = lead ? [[leadIn.x, leadIn.y], [goal.x, goal.y]] : [];
|
|
347
|
+
if (lead && !(isInside(area, straightOut, keepClearOf) && isInside(area, straightIn, keepClearOf))) continue;
|
|
348
|
+
|
|
349
|
+
const plan = planLegs(area, leadOut, leadIn, radius, keepClearOf);
|
|
350
|
+
if (!plan) continue;
|
|
351
|
+
|
|
352
|
+
const points = [];
|
|
353
|
+
const push = (list) => list.forEach((point) => {
|
|
354
|
+
const last = points[points.length - 1];
|
|
355
|
+
if (!last || Math.hypot(point[0] - last[0], point[1] - last[1]) > 1e-6) points.push(point);
|
|
356
|
+
});
|
|
357
|
+
if (lead) push(sampleStraight(straightOut, OUTPUT_STEP_M));
|
|
358
|
+
plan.legs.forEach((leg) => push(sampleDubins(leg, OUTPUT_STEP_M)));
|
|
359
|
+
if (lead) push(sampleStraight(straightIn, OUTPUT_STEP_M));
|
|
360
|
+
points[points.length - 1] = [goal.x, goal.y];
|
|
361
|
+
|
|
362
|
+
const connectorCoordinates = points.slice(1).map(toLonLat);
|
|
363
|
+
const closed = [...coordinates, ...connectorCoordinates];
|
|
364
|
+
if (gap <= 0) closed[closed.length - 1] = [...coordinates[0]];
|
|
365
|
+
return {
|
|
366
|
+
coordinates: closed,
|
|
367
|
+
connector: [coordinates[coordinates.length - 1], ...connectorCoordinates],
|
|
368
|
+
properties: {
|
|
369
|
+
join_start_end: true,
|
|
370
|
+
gap_to_start_meters: Math.round(Math.hypot(goal.x - startPose.x, goal.y - startPose.y) * 1000) / 1000,
|
|
371
|
+
connector_strategy: plan.strategy,
|
|
372
|
+
connector_turn_radius_meters: Math.round(radius * 1000) / 1000
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function sampleStraight([from, to], step) {
|
|
381
|
+
const length = Math.hypot(to[0] - from[0], to[1] - from[1]);
|
|
382
|
+
const count = Math.max(Math.ceil(length / step), 1);
|
|
383
|
+
return Array.from({ length: count + 1 }, (_, i) => [
|
|
384
|
+
from[0] + ((to[0] - from[0]) * i) / count,
|
|
385
|
+
from[1] + ((to[1] - from[1]) * i) / count
|
|
386
|
+
]);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
module.exports = { closeMowerPathFallback, dubinsCandidates, sampleDubins };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
const METERS_PER_DEGREE_LATITUDE = 110540;
|
|
2
|
+
const METERS_PER_DEGREE_LONGITUDE = 111320;
|
|
3
|
+
const DEFAULT_POINT_SPACING = 0.1;
|
|
4
|
+
const MIN_ROW_LENGTH = 3;
|
|
5
|
+
const MAX_END_ROW_SKEW = 5 * Math.PI / 180; // last row must be this close to parallel with the first row
|
|
6
|
+
|
|
7
|
+
const distance = (a, b) => Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
8
|
+
|
|
9
|
+
const line = (from, to, step) => {
|
|
10
|
+
const length = distance(from, to);
|
|
11
|
+
if (length < 1e-6) return [];
|
|
12
|
+
const count = Math.max(Math.ceil(length / step), 1);
|
|
13
|
+
return Array.from({ length: count }, (_, index) => {
|
|
14
|
+
const t = (index + 1) / count;
|
|
15
|
+
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const arc = (center, radius, startAngle, endAngle, step) => {
|
|
20
|
+
const count = Math.max(Math.ceil(Math.abs(endAngle - startAngle) * radius / step), 1);
|
|
21
|
+
return Array.from({ length: count }, (_, index) => {
|
|
22
|
+
const angle = startAngle + ((index + 1) / count) * (endAngle - startAngle);
|
|
23
|
+
return [center[0] + radius * Math.cos(angle), center[1] + radius * Math.sin(angle)];
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const concatenate = (...parts) => parts.flat();
|
|
28
|
+
|
|
29
|
+
// These primitives intentionally mirror generate_loop_path.py. They operate
|
|
30
|
+
// in a local metre frame, which avoids mixing longitude/latitude degrees with
|
|
31
|
+
// the mower's metre-based radius and spacing settings.
|
|
32
|
+
const uTurnRightTop = (fromX, fromY, toX, toY, radius, step) => {
|
|
33
|
+
if (toX - fromX < 2 * radius - 1e-6) {
|
|
34
|
+
throw new Error("The return row is too close to make the loop turn.");
|
|
35
|
+
}
|
|
36
|
+
const top = Math.max(fromY, toY) + radius;
|
|
37
|
+
return concatenate(
|
|
38
|
+
line([fromX, fromY], [fromX, top - radius], step),
|
|
39
|
+
arc([fromX + radius, top - radius], radius, Math.PI, Math.PI / 2, step),
|
|
40
|
+
line([fromX + radius, top], [toX - radius, top], step),
|
|
41
|
+
arc([toX - radius, top - radius], radius, Math.PI / 2, 0, step),
|
|
42
|
+
line([toX, top - radius], [toX, toY], step)
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const uTurnBottom = (fromX, fromY, toX, toY, radius, step) => {
|
|
47
|
+
const direction = toX > fromX ? 1 : -1;
|
|
48
|
+
if (Math.abs(toX - fromX) < 2 * radius - 1e-6) {
|
|
49
|
+
throw new Error("The start and end rows are too close to make the loop turn.");
|
|
50
|
+
}
|
|
51
|
+
const bottom = Math.min(fromY, toY) - radius;
|
|
52
|
+
const startAngle = direction > 0 ? Math.PI : 0;
|
|
53
|
+
return concatenate(
|
|
54
|
+
line([fromX, fromY], [fromX, bottom + radius], step),
|
|
55
|
+
arc([fromX + direction * radius, bottom + radius], radius,
|
|
56
|
+
startAngle, startAngle + direction * Math.PI / 2, step),
|
|
57
|
+
line([fromX + direction * radius, bottom], [toX - direction * radius, bottom], step),
|
|
58
|
+
arc([toX - direction * radius, bottom + radius], radius,
|
|
59
|
+
startAngle + direction * Math.PI / 2, startAngle + direction * Math.PI, step),
|
|
60
|
+
line([toX, bottom + radius], [toX, toY], step)
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const createFrame = (origin, rowHeading) => {
|
|
65
|
+
const scaleX = METERS_PER_DEGREE_LONGITUDE * Math.cos(origin[1] * Math.PI / 180);
|
|
66
|
+
const theta = Math.PI / 2 - rowHeading;
|
|
67
|
+
const cos = Math.cos(theta);
|
|
68
|
+
const sin = Math.sin(theta);
|
|
69
|
+
return {
|
|
70
|
+
toLocal([longitude, latitude]) {
|
|
71
|
+
const east = (longitude - origin[0]) * scaleX;
|
|
72
|
+
const north = (latitude - origin[1]) * METERS_PER_DEGREE_LATITUDE;
|
|
73
|
+
return [east * cos - north * sin, east * sin + north * cos];
|
|
74
|
+
},
|
|
75
|
+
toLonLat([x, y]) {
|
|
76
|
+
const east = x * cos + y * sin;
|
|
77
|
+
const north = -x * sin + y * cos;
|
|
78
|
+
return [east / scaleX + origin[0], north / METERS_PER_DEGREE_LATITUDE + origin[1]];
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const rowSegments = (path) => path
|
|
84
|
+
.slice(0, -1)
|
|
85
|
+
.map((start, index) => ({ index, start, end: path[index + 1] }))
|
|
86
|
+
.filter(({ start, end }) => distance(start, end) > MIN_ROW_LENGTH);
|
|
87
|
+
|
|
88
|
+
const buildConnector = (path, radius, step, gap) => {
|
|
89
|
+
const start = path[0];
|
|
90
|
+
const end = path[path.length - 1];
|
|
91
|
+
const goal = [start[0], start[1] - gap];
|
|
92
|
+
|
|
93
|
+
// Both U-turns below leave the end row straight along the local y axis, so
|
|
94
|
+
// they only fit a path that ends on a row parallel to the first one. A
|
|
95
|
+
// criss-cross path often ends on a cross row (90 degrees to the first row):
|
|
96
|
+
// the template would leave it with a sharp corner, so let the caller use
|
|
97
|
+
// the Dubins fallback instead.
|
|
98
|
+
const before = path[path.length - 2];
|
|
99
|
+
const endDx = end[0] - before[0];
|
|
100
|
+
const endDy = end[1] - before[1];
|
|
101
|
+
if (Math.abs(endDx) > Math.abs(endDy) * Math.tan(MAX_END_ROW_SKEW)) {
|
|
102
|
+
throw new Error("The last row is not parallel to the first row.");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (end[1] < before[1]) {
|
|
106
|
+
return {
|
|
107
|
+
points: uTurnBottom(end[0], end[1], goal[0], goal[1], radius, step),
|
|
108
|
+
info: { returnRowX: null, returnRowIndex: null }
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const rows = rowSegments(path);
|
|
113
|
+
const minimumReturnX = end[0] + 2 * radius;
|
|
114
|
+
const medianTop = median(rows.map((candidate) => Math.max(candidate.start[1], candidate.end[1])));
|
|
115
|
+
const candidates = rows
|
|
116
|
+
.map((row) => ({ ...row, x: (row.start[0] + row.end[0]) / 2, top: Math.max(row.start[1], row.end[1]) }))
|
|
117
|
+
.filter((row) => row.x >= minimumReturnX && row.top >= medianTop - 1.5)
|
|
118
|
+
.sort((a, b) => a.x - b.x);
|
|
119
|
+
|
|
120
|
+
if (!candidates.length) {
|
|
121
|
+
throw new Error("No return row is available to join the mower path.");
|
|
122
|
+
}
|
|
123
|
+
const returnRow = candidates[0];
|
|
124
|
+
const topJoin = Math.min(end[1], returnRow.top);
|
|
125
|
+
const connector = concatenate(
|
|
126
|
+
uTurnRightTop(end[0], end[1], returnRow.x, topJoin, radius, step),
|
|
127
|
+
uTurnBottom(returnRow.x, topJoin, goal[0], goal[1], radius, step)
|
|
128
|
+
);
|
|
129
|
+
connector[connector.length - 1] = goal;
|
|
130
|
+
return {
|
|
131
|
+
points: connector,
|
|
132
|
+
info: { returnRowX: returnRow.x, returnRowIndex: returnRow.index }
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const median = (values) => {
|
|
137
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
138
|
+
return sorted[Math.floor(sorted.length / 2)];
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Appends a forward-driving connector from a generated mower path's end to
|
|
143
|
+
* its start. The implementation follows generate_loop_path.py's local-frame,
|
|
144
|
+
* arc-straight-arc construction and returns a GeoJSON-ready feature.
|
|
145
|
+
*/
|
|
146
|
+
function closeMowerPath(coordinates, { turningRadius, gap = 0, spacing = DEFAULT_POINT_SPACING } = {}) {
|
|
147
|
+
if (!Array.isArray(coordinates) || coordinates.length < 3) {
|
|
148
|
+
throw new Error("A generated mower path needs at least three points to be closed.");
|
|
149
|
+
}
|
|
150
|
+
if (!Number.isFinite(turningRadius) || turningRadius <= 0) {
|
|
151
|
+
throw new Error("A positive turning radius is required to close the mower path.");
|
|
152
|
+
}
|
|
153
|
+
const first = coordinates[0];
|
|
154
|
+
const second = coordinates[1];
|
|
155
|
+
const rowHeading = Math.atan2(
|
|
156
|
+
(second[1] - first[1]) * METERS_PER_DEGREE_LATITUDE,
|
|
157
|
+
(second[0] - first[0]) * METERS_PER_DEGREE_LONGITUDE * Math.cos(first[1] * Math.PI / 180)
|
|
158
|
+
);
|
|
159
|
+
const frame = createFrame(first, rowHeading);
|
|
160
|
+
const localPath = coordinates.map(frame.toLocal);
|
|
161
|
+
const { points, info } = buildConnector(localPath, turningRadius, spacing, gap);
|
|
162
|
+
const connectorCoordinates = points.map(frame.toLonLat);
|
|
163
|
+
const closedCoordinates = [...coordinates, ...connectorCoordinates];
|
|
164
|
+
if (gap <= 0) closedCoordinates[closedCoordinates.length - 1] = [...first];
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
coordinates: closedCoordinates,
|
|
168
|
+
connector: [coordinates[coordinates.length - 1], ...connectorCoordinates],
|
|
169
|
+
properties: {
|
|
170
|
+
join_start_end: true,
|
|
171
|
+
gap_to_start_meters: gap,
|
|
172
|
+
return_row_x_meters: info.returnRowX,
|
|
173
|
+
return_row_index: info.returnRowIndex
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
module.exports = { closeMowerPath };
|