microboard-temp 0.5.76 → 0.5.78
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/dist/cjs/browser.js +86 -73
- package/dist/cjs/index.js +86 -73
- package/dist/cjs/node.js +86 -73
- package/dist/esm/browser.js +86 -73
- package/dist/esm/index.js +86 -73
- package/dist/esm/node.js +86 -73
- package/package.json +1 -1
package/dist/cjs/node.js
CHANGED
|
@@ -39183,7 +39183,7 @@ function getNeighbors(node2, grid, obstacles) {
|
|
|
39183
39183
|
{ x: node2.xGrid, y: node2.yGrid + 1 }
|
|
39184
39184
|
];
|
|
39185
39185
|
for (const pos of potentialNeighbors) {
|
|
39186
|
-
if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0) {
|
|
39186
|
+
if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0 && grid[pos.x] && grid[pos.x][pos.y]) {
|
|
39187
39187
|
const newPoint = grid[pos.x][pos.y];
|
|
39188
39188
|
if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, conf.CONNECTOR_ITEM_OFFSET - 1))) {
|
|
39189
39189
|
neighbors.push({
|
|
@@ -39200,34 +39200,6 @@ function getNeighbors(node2, grid, obstacles) {
|
|
|
39200
39200
|
}
|
|
39201
39201
|
return neighbors;
|
|
39202
39202
|
}
|
|
39203
|
-
function findCenterLine(grid, start, end, middle) {
|
|
39204
|
-
const centerLine = [];
|
|
39205
|
-
const middlePoint = middle ? middle : new Point((start.x + end.x) / 2, (start.y + end.y) / 2);
|
|
39206
|
-
const min2 = new Point(Math.min(start.x, end.x), Math.min(start.y, end.y));
|
|
39207
|
-
const max2 = new Point(Math.max(start.x, end.x), Math.max(start.y, end.y));
|
|
39208
|
-
const width = max2.x - min2.x;
|
|
39209
|
-
const height = max2.y - min2.y;
|
|
39210
|
-
if (start.pointType !== "Board" && end.pointType !== "Board") {
|
|
39211
|
-
const isInGrid = grid.some((row2) => row2.some((point5) => point5.barelyEqual(middlePoint)));
|
|
39212
|
-
return isInGrid ? [middlePoint] : [];
|
|
39213
|
-
}
|
|
39214
|
-
if (width > height) {
|
|
39215
|
-
const centerIdx = grid.findIndex((row2) => row2[0].x === middlePoint.x || Math.abs(row2[0].x - middlePoint.x) < 0.01);
|
|
39216
|
-
for (let y = 0;y < grid[0].length && centerIdx !== -1; y++) {
|
|
39217
|
-
if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
|
|
39218
|
-
centerLine.push(grid[centerIdx][y]);
|
|
39219
|
-
}
|
|
39220
|
-
}
|
|
39221
|
-
} else {
|
|
39222
|
-
const centerIdx = grid[0].findIndex((point5) => point5.y === middlePoint.y || Math.abs(point5.y - middlePoint.y) < 0.01);
|
|
39223
|
-
for (let x = 0;x < grid.length && centerIdx !== -1; x++) {
|
|
39224
|
-
if (grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
|
|
39225
|
-
centerLine.push(grid[x][centerIdx]);
|
|
39226
|
-
}
|
|
39227
|
-
}
|
|
39228
|
-
}
|
|
39229
|
-
return centerLine;
|
|
39230
|
-
}
|
|
39231
39203
|
function createGrid(start, end, toVisitPoints = []) {
|
|
39232
39204
|
const startDir = getPointerDirection(start);
|
|
39233
39205
|
const endDir = getPointerDirection(end);
|
|
@@ -39278,18 +39250,27 @@ function createGrid(start, end, toVisitPoints = []) {
|
|
|
39278
39250
|
middlePoint: middle
|
|
39279
39251
|
};
|
|
39280
39252
|
}
|
|
39281
|
-
function findPath(start, end, grid, obstacles, newStart, newEnd) {
|
|
39282
|
-
const
|
|
39283
|
-
|
|
39284
|
-
|
|
39285
|
-
|
|
39253
|
+
function findPath(start, end, grid, obstacles, existingPath, newStart, newEnd) {
|
|
39254
|
+
const startRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
|
|
39255
|
+
if (startRowIndex === -1) {
|
|
39256
|
+
throw new Error("Start point not found in the grid row");
|
|
39257
|
+
}
|
|
39258
|
+
const startPointIndex = grid[startRowIndex].findIndex((point5) => point5.barelyEqual(start));
|
|
39259
|
+
if (startPointIndex === -1) {
|
|
39260
|
+
throw new Error("Start point not found in the grid column");
|
|
39261
|
+
}
|
|
39262
|
+
const endRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
|
|
39263
|
+
if (endRowIndex === -1) {
|
|
39264
|
+
throw new Error("End point not found in the grid row");
|
|
39265
|
+
}
|
|
39266
|
+
const endPointIndex = grid[endRowIndex].findIndex((point5) => point5.barelyEqual(end));
|
|
39267
|
+
if (endPointIndex === -1) {
|
|
39268
|
+
throw new Error("End point not found in the grid column");
|
|
39286
39269
|
}
|
|
39287
|
-
const startPointIdx = grid[startIdx].findIndex((point5) => point5.barelyEqual(start));
|
|
39288
|
-
const endPointIdx = grid[endIdx].findIndex((point5) => point5.barelyEqual(end));
|
|
39289
39270
|
const endNode = {
|
|
39290
39271
|
point: end,
|
|
39291
|
-
xGrid:
|
|
39292
|
-
yGrid:
|
|
39272
|
+
xGrid: endRowIndex,
|
|
39273
|
+
yGrid: endPointIndex,
|
|
39293
39274
|
costSoFar: 0,
|
|
39294
39275
|
heuristic: 0,
|
|
39295
39276
|
toFinish: 0
|
|
@@ -39297,53 +39278,72 @@ function findPath(start, end, grid, obstacles, newStart, newEnd) {
|
|
|
39297
39278
|
const startNode = {
|
|
39298
39279
|
point: start,
|
|
39299
39280
|
costSoFar: 0,
|
|
39300
|
-
heuristic: heuristic({ point: start, xGrid:
|
|
39301
|
-
toFinish: heuristic({ point: start, xGrid:
|
|
39302
|
-
xGrid:
|
|
39303
|
-
yGrid:
|
|
39281
|
+
heuristic: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
|
|
39282
|
+
toFinish: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
|
|
39283
|
+
xGrid: startRowIndex,
|
|
39284
|
+
yGrid: startPointIndex
|
|
39304
39285
|
};
|
|
39305
39286
|
const openSet = [startNode];
|
|
39306
39287
|
const closedSet = new Set;
|
|
39307
39288
|
while (openSet.length > 0) {
|
|
39308
39289
|
openSet.sort((aa, bb) => aa.toFinish - bb.toFinish);
|
|
39309
39290
|
const current = openSet.shift();
|
|
39291
|
+
const currentKey = `${current.point.x},${current.point.y}`;
|
|
39310
39292
|
if (current.point.barelyEqual(end)) {
|
|
39311
39293
|
const path2 = reconstructPath(current);
|
|
39312
39294
|
return path2;
|
|
39313
39295
|
}
|
|
39314
|
-
closedSet.add(
|
|
39296
|
+
closedSet.add(currentKey);
|
|
39315
39297
|
const neighbors = getNeighbors(current, grid, obstacles);
|
|
39316
39298
|
for (const neighbor of neighbors) {
|
|
39317
|
-
|
|
39299
|
+
const neighborKey = `${neighbor.point.x},${neighbor.point.y}`;
|
|
39300
|
+
if (closedSet.has(neighborKey) || existingPath.has(neighborKey) && !neighbor.point.barelyEqual(end)) {
|
|
39318
39301
|
continue;
|
|
39319
39302
|
}
|
|
39320
39303
|
const extraCost = isChangingDirection(current, neighbor, newStart, newEnd);
|
|
39321
|
-
const
|
|
39322
|
-
|
|
39323
|
-
|
|
39324
|
-
|
|
39325
|
-
|
|
39326
|
-
|
|
39304
|
+
const pathOverlapCost = existingPath.has(neighborKey) ? 1000 : 0;
|
|
39305
|
+
const tentativeCost = current.costSoFar + 1 + pathOverlapCost;
|
|
39306
|
+
let existingNodeInOpenSet = openSet.find((node2) => node2.point.barelyEqual(neighbor.point));
|
|
39307
|
+
if (!existingNodeInOpenSet || tentativeCost < existingNodeInOpenSet.costSoFar) {
|
|
39308
|
+
if (existingNodeInOpenSet) {
|
|
39309
|
+
existingNodeInOpenSet.costSoFar = tentativeCost + extraCost;
|
|
39310
|
+
existingNodeInOpenSet.heuristic = heuristic(neighbor, endNode);
|
|
39311
|
+
existingNodeInOpenSet.toFinish = existingNodeInOpenSet.costSoFar + existingNodeInOpenSet.heuristic;
|
|
39312
|
+
existingNodeInOpenSet.parent = current;
|
|
39313
|
+
} else {
|
|
39314
|
+
neighbor.costSoFar = tentativeCost + extraCost;
|
|
39315
|
+
neighbor.heuristic = heuristic(neighbor, endNode);
|
|
39316
|
+
neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
|
|
39317
|
+
openSet.push(neighbor);
|
|
39318
|
+
}
|
|
39327
39319
|
}
|
|
39328
39320
|
}
|
|
39329
39321
|
}
|
|
39330
39322
|
return;
|
|
39331
39323
|
}
|
|
39332
39324
|
function findPathPoints(points, grid, obstacles, newStart, newEnd) {
|
|
39333
|
-
const
|
|
39325
|
+
const finalPath = [];
|
|
39326
|
+
const existingPathSegments = new Set;
|
|
39327
|
+
if (points.length > 0) {
|
|
39328
|
+
finalPath.push(points[0]);
|
|
39329
|
+
const startKey = `${points[0].x},${points[0].y}`;
|
|
39330
|
+
existingPathSegments.add(startKey);
|
|
39331
|
+
}
|
|
39334
39332
|
for (let i = 0;i < points.length - 1; i += 1) {
|
|
39335
|
-
const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, newStart, newEnd);
|
|
39336
|
-
if (segmentPath) {
|
|
39337
|
-
|
|
39333
|
+
const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, existingPathSegments, newStart, newEnd);
|
|
39334
|
+
if (segmentPath && segmentPath.length > 0) {
|
|
39335
|
+
for (let j = 1;j < segmentPath.length; j++) {
|
|
39336
|
+
const point5 = segmentPath[j];
|
|
39337
|
+
const key = `${point5.x},${point5.y}`;
|
|
39338
|
+
finalPath.push(point5);
|
|
39339
|
+
existingPathSegments.add(key);
|
|
39340
|
+
}
|
|
39338
39341
|
} else {
|
|
39339
39342
|
points.splice(i + 1, 1);
|
|
39340
39343
|
i--;
|
|
39341
39344
|
}
|
|
39342
39345
|
}
|
|
39343
|
-
|
|
39344
|
-
pathPoints.push(points[points.length - 1]);
|
|
39345
|
-
}
|
|
39346
|
-
return pathPoints;
|
|
39346
|
+
return finalPath;
|
|
39347
39347
|
}
|
|
39348
39348
|
function reducePoints(points) {
|
|
39349
39349
|
const uniquePoints = new Map;
|
|
@@ -39354,6 +39354,12 @@ function reducePoints(points) {
|
|
|
39354
39354
|
if (uniquePoints.has(key)) {
|
|
39355
39355
|
const loopStartIndex = uniquePoints.get(key);
|
|
39356
39356
|
result.splice(loopStartIndex + 1);
|
|
39357
|
+
const removedPoints = points.slice(loopStartIndex + 1, i + 1);
|
|
39358
|
+
removedPoints.forEach((p3) => {
|
|
39359
|
+
uniquePoints.delete(`${p3.x},${p3.y}`);
|
|
39360
|
+
});
|
|
39361
|
+
uniquePoints.set(key, result.length);
|
|
39362
|
+
result.push(point5);
|
|
39357
39363
|
} else {
|
|
39358
39364
|
uniquePoints.set(key, result.length);
|
|
39359
39365
|
result.push(point5);
|
|
@@ -39363,19 +39369,32 @@ function reducePoints(points) {
|
|
|
39363
39369
|
}
|
|
39364
39370
|
function getLines(pathPoints) {
|
|
39365
39371
|
const lines = [];
|
|
39372
|
+
if (pathPoints.length < 2) {
|
|
39373
|
+
return [];
|
|
39374
|
+
}
|
|
39366
39375
|
const reducedPoints = reducePoints(pathPoints);
|
|
39376
|
+
if (reducedPoints.length < 2) {
|
|
39377
|
+
return [];
|
|
39378
|
+
}
|
|
39367
39379
|
let startPoint = reducedPoints[0];
|
|
39368
|
-
for (let i = 1;i < reducedPoints.length
|
|
39380
|
+
for (let i = 1;i < reducedPoints.length; i += 1) {
|
|
39369
39381
|
const prevPoint = reducedPoints[i - 1];
|
|
39370
39382
|
const currPoint = reducedPoints[i];
|
|
39371
|
-
const nextPoint = reducedPoints[i + 1];
|
|
39372
|
-
if (prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
|
|
39383
|
+
const nextPoint = i + 1 < reducedPoints.length ? reducedPoints[i + 1] : null;
|
|
39384
|
+
if (!nextPoint || prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
|
|
39373
39385
|
lines.push(new Line(startPoint, currPoint));
|
|
39374
39386
|
startPoint = currPoint;
|
|
39375
39387
|
}
|
|
39376
39388
|
}
|
|
39377
|
-
if (lines.length >
|
|
39378
|
-
lines.push(new Line(
|
|
39389
|
+
if (lines.length === 0 && reducedPoints.length > 1) {
|
|
39390
|
+
lines.push(new Line(reducedPoints[0], reducedPoints[reducedPoints.length - 1]));
|
|
39391
|
+
} else if (lines.length > 0) {
|
|
39392
|
+
const lastLine = lines[lines.length - 1];
|
|
39393
|
+
const lastPointInLines = lastLine.getEndPoint();
|
|
39394
|
+
const lastPointInReduced = reducedPoints[reducedPoints.length - 1];
|
|
39395
|
+
if (!lastPointInLines.barelyEqual(lastPointInReduced)) {
|
|
39396
|
+
lines.push(new Line(lastPointInLines, lastPointInReduced));
|
|
39397
|
+
}
|
|
39379
39398
|
}
|
|
39380
39399
|
return lines;
|
|
39381
39400
|
}
|
|
@@ -39389,16 +39408,10 @@ function reconstructPath(node2) {
|
|
|
39389
39408
|
return path2.reverse();
|
|
39390
39409
|
}
|
|
39391
39410
|
function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
|
|
39392
|
-
const { grid, newStart, newEnd
|
|
39393
|
-
const startPoint = newStart
|
|
39394
|
-
const endPoint = newEnd
|
|
39395
|
-
const
|
|
39396
|
-
const adjustedCenterLine = centerLine.length > 0 ? startPoint.getDistance(centerLine[0]) < startPoint.getDistance(centerLine[centerLine.length - 1]) ? centerLine : centerLine.reverse() : centerLine;
|
|
39397
|
-
const points = [
|
|
39398
|
-
startPoint,
|
|
39399
|
-
...toVisitPoints.length > 0 ? toVisitPoints : adjustedCenterLine,
|
|
39400
|
-
endPoint
|
|
39401
|
-
];
|
|
39411
|
+
const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
|
|
39412
|
+
const startPoint = newStart || start;
|
|
39413
|
+
const endPoint = newEnd || end;
|
|
39414
|
+
const points = [startPoint, ...toVisitPoints, endPoint];
|
|
39402
39415
|
const pathPoints = findPathPoints(points, grid, obstacles, newStart, newEnd);
|
|
39403
39416
|
return {
|
|
39404
39417
|
lines: getLines(pathPoints),
|
package/dist/esm/browser.js
CHANGED
|
@@ -36555,7 +36555,7 @@ function getNeighbors(node2, grid, obstacles) {
|
|
|
36555
36555
|
{ x: node2.xGrid, y: node2.yGrid + 1 }
|
|
36556
36556
|
];
|
|
36557
36557
|
for (const pos of potentialNeighbors) {
|
|
36558
|
-
if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0) {
|
|
36558
|
+
if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0 && grid[pos.x] && grid[pos.x][pos.y]) {
|
|
36559
36559
|
const newPoint = grid[pos.x][pos.y];
|
|
36560
36560
|
if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, conf.CONNECTOR_ITEM_OFFSET - 1))) {
|
|
36561
36561
|
neighbors.push({
|
|
@@ -36572,34 +36572,6 @@ function getNeighbors(node2, grid, obstacles) {
|
|
|
36572
36572
|
}
|
|
36573
36573
|
return neighbors;
|
|
36574
36574
|
}
|
|
36575
|
-
function findCenterLine(grid, start, end, middle) {
|
|
36576
|
-
const centerLine = [];
|
|
36577
|
-
const middlePoint = middle ? middle : new Point((start.x + end.x) / 2, (start.y + end.y) / 2);
|
|
36578
|
-
const min2 = new Point(Math.min(start.x, end.x), Math.min(start.y, end.y));
|
|
36579
|
-
const max2 = new Point(Math.max(start.x, end.x), Math.max(start.y, end.y));
|
|
36580
|
-
const width = max2.x - min2.x;
|
|
36581
|
-
const height = max2.y - min2.y;
|
|
36582
|
-
if (start.pointType !== "Board" && end.pointType !== "Board") {
|
|
36583
|
-
const isInGrid = grid.some((row2) => row2.some((point5) => point5.barelyEqual(middlePoint)));
|
|
36584
|
-
return isInGrid ? [middlePoint] : [];
|
|
36585
|
-
}
|
|
36586
|
-
if (width > height) {
|
|
36587
|
-
const centerIdx = grid.findIndex((row2) => row2[0].x === middlePoint.x || Math.abs(row2[0].x - middlePoint.x) < 0.01);
|
|
36588
|
-
for (let y = 0;y < grid[0].length && centerIdx !== -1; y++) {
|
|
36589
|
-
if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
|
|
36590
|
-
centerLine.push(grid[centerIdx][y]);
|
|
36591
|
-
}
|
|
36592
|
-
}
|
|
36593
|
-
} else {
|
|
36594
|
-
const centerIdx = grid[0].findIndex((point5) => point5.y === middlePoint.y || Math.abs(point5.y - middlePoint.y) < 0.01);
|
|
36595
|
-
for (let x = 0;x < grid.length && centerIdx !== -1; x++) {
|
|
36596
|
-
if (grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
|
|
36597
|
-
centerLine.push(grid[x][centerIdx]);
|
|
36598
|
-
}
|
|
36599
|
-
}
|
|
36600
|
-
}
|
|
36601
|
-
return centerLine;
|
|
36602
|
-
}
|
|
36603
36575
|
function createGrid(start, end, toVisitPoints = []) {
|
|
36604
36576
|
const startDir = getPointerDirection(start);
|
|
36605
36577
|
const endDir = getPointerDirection(end);
|
|
@@ -36650,18 +36622,27 @@ function createGrid(start, end, toVisitPoints = []) {
|
|
|
36650
36622
|
middlePoint: middle
|
|
36651
36623
|
};
|
|
36652
36624
|
}
|
|
36653
|
-
function findPath(start, end, grid, obstacles, newStart, newEnd) {
|
|
36654
|
-
const
|
|
36655
|
-
|
|
36656
|
-
|
|
36657
|
-
|
|
36625
|
+
function findPath(start, end, grid, obstacles, existingPath, newStart, newEnd) {
|
|
36626
|
+
const startRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
|
|
36627
|
+
if (startRowIndex === -1) {
|
|
36628
|
+
throw new Error("Start point not found in the grid row");
|
|
36629
|
+
}
|
|
36630
|
+
const startPointIndex = grid[startRowIndex].findIndex((point5) => point5.barelyEqual(start));
|
|
36631
|
+
if (startPointIndex === -1) {
|
|
36632
|
+
throw new Error("Start point not found in the grid column");
|
|
36633
|
+
}
|
|
36634
|
+
const endRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
|
|
36635
|
+
if (endRowIndex === -1) {
|
|
36636
|
+
throw new Error("End point not found in the grid row");
|
|
36637
|
+
}
|
|
36638
|
+
const endPointIndex = grid[endRowIndex].findIndex((point5) => point5.barelyEqual(end));
|
|
36639
|
+
if (endPointIndex === -1) {
|
|
36640
|
+
throw new Error("End point not found in the grid column");
|
|
36658
36641
|
}
|
|
36659
|
-
const startPointIdx = grid[startIdx].findIndex((point5) => point5.barelyEqual(start));
|
|
36660
|
-
const endPointIdx = grid[endIdx].findIndex((point5) => point5.barelyEqual(end));
|
|
36661
36642
|
const endNode = {
|
|
36662
36643
|
point: end,
|
|
36663
|
-
xGrid:
|
|
36664
|
-
yGrid:
|
|
36644
|
+
xGrid: endRowIndex,
|
|
36645
|
+
yGrid: endPointIndex,
|
|
36665
36646
|
costSoFar: 0,
|
|
36666
36647
|
heuristic: 0,
|
|
36667
36648
|
toFinish: 0
|
|
@@ -36669,53 +36650,72 @@ function findPath(start, end, grid, obstacles, newStart, newEnd) {
|
|
|
36669
36650
|
const startNode = {
|
|
36670
36651
|
point: start,
|
|
36671
36652
|
costSoFar: 0,
|
|
36672
|
-
heuristic: heuristic({ point: start, xGrid:
|
|
36673
|
-
toFinish: heuristic({ point: start, xGrid:
|
|
36674
|
-
xGrid:
|
|
36675
|
-
yGrid:
|
|
36653
|
+
heuristic: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
|
|
36654
|
+
toFinish: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
|
|
36655
|
+
xGrid: startRowIndex,
|
|
36656
|
+
yGrid: startPointIndex
|
|
36676
36657
|
};
|
|
36677
36658
|
const openSet = [startNode];
|
|
36678
36659
|
const closedSet = new Set;
|
|
36679
36660
|
while (openSet.length > 0) {
|
|
36680
36661
|
openSet.sort((aa, bb) => aa.toFinish - bb.toFinish);
|
|
36681
36662
|
const current = openSet.shift();
|
|
36663
|
+
const currentKey = `${current.point.x},${current.point.y}`;
|
|
36682
36664
|
if (current.point.barelyEqual(end)) {
|
|
36683
36665
|
const path2 = reconstructPath(current);
|
|
36684
36666
|
return path2;
|
|
36685
36667
|
}
|
|
36686
|
-
closedSet.add(
|
|
36668
|
+
closedSet.add(currentKey);
|
|
36687
36669
|
const neighbors = getNeighbors(current, grid, obstacles);
|
|
36688
36670
|
for (const neighbor of neighbors) {
|
|
36689
|
-
|
|
36671
|
+
const neighborKey = `${neighbor.point.x},${neighbor.point.y}`;
|
|
36672
|
+
if (closedSet.has(neighborKey) || existingPath.has(neighborKey) && !neighbor.point.barelyEqual(end)) {
|
|
36690
36673
|
continue;
|
|
36691
36674
|
}
|
|
36692
36675
|
const extraCost = isChangingDirection(current, neighbor, newStart, newEnd);
|
|
36693
|
-
const
|
|
36694
|
-
|
|
36695
|
-
|
|
36696
|
-
|
|
36697
|
-
|
|
36698
|
-
|
|
36676
|
+
const pathOverlapCost = existingPath.has(neighborKey) ? 1000 : 0;
|
|
36677
|
+
const tentativeCost = current.costSoFar + 1 + pathOverlapCost;
|
|
36678
|
+
let existingNodeInOpenSet = openSet.find((node2) => node2.point.barelyEqual(neighbor.point));
|
|
36679
|
+
if (!existingNodeInOpenSet || tentativeCost < existingNodeInOpenSet.costSoFar) {
|
|
36680
|
+
if (existingNodeInOpenSet) {
|
|
36681
|
+
existingNodeInOpenSet.costSoFar = tentativeCost + extraCost;
|
|
36682
|
+
existingNodeInOpenSet.heuristic = heuristic(neighbor, endNode);
|
|
36683
|
+
existingNodeInOpenSet.toFinish = existingNodeInOpenSet.costSoFar + existingNodeInOpenSet.heuristic;
|
|
36684
|
+
existingNodeInOpenSet.parent = current;
|
|
36685
|
+
} else {
|
|
36686
|
+
neighbor.costSoFar = tentativeCost + extraCost;
|
|
36687
|
+
neighbor.heuristic = heuristic(neighbor, endNode);
|
|
36688
|
+
neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
|
|
36689
|
+
openSet.push(neighbor);
|
|
36690
|
+
}
|
|
36699
36691
|
}
|
|
36700
36692
|
}
|
|
36701
36693
|
}
|
|
36702
36694
|
return;
|
|
36703
36695
|
}
|
|
36704
36696
|
function findPathPoints(points, grid, obstacles, newStart, newEnd) {
|
|
36705
|
-
const
|
|
36697
|
+
const finalPath = [];
|
|
36698
|
+
const existingPathSegments = new Set;
|
|
36699
|
+
if (points.length > 0) {
|
|
36700
|
+
finalPath.push(points[0]);
|
|
36701
|
+
const startKey = `${points[0].x},${points[0].y}`;
|
|
36702
|
+
existingPathSegments.add(startKey);
|
|
36703
|
+
}
|
|
36706
36704
|
for (let i = 0;i < points.length - 1; i += 1) {
|
|
36707
|
-
const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, newStart, newEnd);
|
|
36708
|
-
if (segmentPath) {
|
|
36709
|
-
|
|
36705
|
+
const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, existingPathSegments, newStart, newEnd);
|
|
36706
|
+
if (segmentPath && segmentPath.length > 0) {
|
|
36707
|
+
for (let j = 1;j < segmentPath.length; j++) {
|
|
36708
|
+
const point5 = segmentPath[j];
|
|
36709
|
+
const key = `${point5.x},${point5.y}`;
|
|
36710
|
+
finalPath.push(point5);
|
|
36711
|
+
existingPathSegments.add(key);
|
|
36712
|
+
}
|
|
36710
36713
|
} else {
|
|
36711
36714
|
points.splice(i + 1, 1);
|
|
36712
36715
|
i--;
|
|
36713
36716
|
}
|
|
36714
36717
|
}
|
|
36715
|
-
|
|
36716
|
-
pathPoints.push(points[points.length - 1]);
|
|
36717
|
-
}
|
|
36718
|
-
return pathPoints;
|
|
36718
|
+
return finalPath;
|
|
36719
36719
|
}
|
|
36720
36720
|
function reducePoints(points) {
|
|
36721
36721
|
const uniquePoints = new Map;
|
|
@@ -36726,6 +36726,12 @@ function reducePoints(points) {
|
|
|
36726
36726
|
if (uniquePoints.has(key)) {
|
|
36727
36727
|
const loopStartIndex = uniquePoints.get(key);
|
|
36728
36728
|
result.splice(loopStartIndex + 1);
|
|
36729
|
+
const removedPoints = points.slice(loopStartIndex + 1, i + 1);
|
|
36730
|
+
removedPoints.forEach((p3) => {
|
|
36731
|
+
uniquePoints.delete(`${p3.x},${p3.y}`);
|
|
36732
|
+
});
|
|
36733
|
+
uniquePoints.set(key, result.length);
|
|
36734
|
+
result.push(point5);
|
|
36729
36735
|
} else {
|
|
36730
36736
|
uniquePoints.set(key, result.length);
|
|
36731
36737
|
result.push(point5);
|
|
@@ -36735,19 +36741,32 @@ function reducePoints(points) {
|
|
|
36735
36741
|
}
|
|
36736
36742
|
function getLines(pathPoints) {
|
|
36737
36743
|
const lines = [];
|
|
36744
|
+
if (pathPoints.length < 2) {
|
|
36745
|
+
return [];
|
|
36746
|
+
}
|
|
36738
36747
|
const reducedPoints = reducePoints(pathPoints);
|
|
36748
|
+
if (reducedPoints.length < 2) {
|
|
36749
|
+
return [];
|
|
36750
|
+
}
|
|
36739
36751
|
let startPoint = reducedPoints[0];
|
|
36740
|
-
for (let i = 1;i < reducedPoints.length
|
|
36752
|
+
for (let i = 1;i < reducedPoints.length; i += 1) {
|
|
36741
36753
|
const prevPoint = reducedPoints[i - 1];
|
|
36742
36754
|
const currPoint = reducedPoints[i];
|
|
36743
|
-
const nextPoint = reducedPoints[i + 1];
|
|
36744
|
-
if (prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
|
|
36755
|
+
const nextPoint = i + 1 < reducedPoints.length ? reducedPoints[i + 1] : null;
|
|
36756
|
+
if (!nextPoint || prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
|
|
36745
36757
|
lines.push(new Line(startPoint, currPoint));
|
|
36746
36758
|
startPoint = currPoint;
|
|
36747
36759
|
}
|
|
36748
36760
|
}
|
|
36749
|
-
if (lines.length >
|
|
36750
|
-
lines.push(new Line(
|
|
36761
|
+
if (lines.length === 0 && reducedPoints.length > 1) {
|
|
36762
|
+
lines.push(new Line(reducedPoints[0], reducedPoints[reducedPoints.length - 1]));
|
|
36763
|
+
} else if (lines.length > 0) {
|
|
36764
|
+
const lastLine = lines[lines.length - 1];
|
|
36765
|
+
const lastPointInLines = lastLine.getEndPoint();
|
|
36766
|
+
const lastPointInReduced = reducedPoints[reducedPoints.length - 1];
|
|
36767
|
+
if (!lastPointInLines.barelyEqual(lastPointInReduced)) {
|
|
36768
|
+
lines.push(new Line(lastPointInLines, lastPointInReduced));
|
|
36769
|
+
}
|
|
36751
36770
|
}
|
|
36752
36771
|
return lines;
|
|
36753
36772
|
}
|
|
@@ -36761,16 +36780,10 @@ function reconstructPath(node2) {
|
|
|
36761
36780
|
return path2.reverse();
|
|
36762
36781
|
}
|
|
36763
36782
|
function findOrthogonalPath(start, end, obstacles, toVisitPoints = []) {
|
|
36764
|
-
const { grid, newStart, newEnd
|
|
36765
|
-
const startPoint = newStart
|
|
36766
|
-
const endPoint = newEnd
|
|
36767
|
-
const
|
|
36768
|
-
const adjustedCenterLine = centerLine.length > 0 ? startPoint.getDistance(centerLine[0]) < startPoint.getDistance(centerLine[centerLine.length - 1]) ? centerLine : centerLine.reverse() : centerLine;
|
|
36769
|
-
const points = [
|
|
36770
|
-
startPoint,
|
|
36771
|
-
...toVisitPoints.length > 0 ? toVisitPoints : adjustedCenterLine,
|
|
36772
|
-
endPoint
|
|
36773
|
-
];
|
|
36783
|
+
const { grid, newStart, newEnd } = createGrid(start, end, toVisitPoints);
|
|
36784
|
+
const startPoint = newStart || start;
|
|
36785
|
+
const endPoint = newEnd || end;
|
|
36786
|
+
const points = [startPoint, ...toVisitPoints, endPoint];
|
|
36774
36787
|
const pathPoints = findPathPoints(points, grid, obstacles, newStart, newEnd);
|
|
36775
36788
|
return {
|
|
36776
36789
|
lines: getLines(pathPoints),
|