@rxflow/manhattan
Version:
Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance
82 lines (76 loc) • 4.09 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { Point } from "../geometry";
/**
* Parse SVG path string to extract points
* Simplified parser that handles M, L, Q commands
*/
export function parseSVGPath(pathString) {
var points = [];
var commands = pathString.match(/[MLQ][^MLQ]*/g);
if (!commands) return points;
var _iterator = _createForOfIteratorHelper(commands),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var command = _step.value;
var type = command[0];
var coords = command.slice(1).trim().split(/[\s,]+/).map(Number);
if (type === 'M' || type === 'L') {
// MoveTo or LineTo: x, y
if (coords.length >= 2) {
points.push(new Point(coords[0], coords[1]));
}
} else if (type === 'Q') {
// Quadratic Bezier: cx, cy, x, y
// We sample points along the curve for collision detection
if (coords.length >= 4) {
var prevPoint = points[points.length - 1];
if (prevPoint) {
var cx = coords[0];
var cy = coords[1];
var x = coords[2];
var y = coords[3];
// Sample 10 points along the bezier curve for better accuracy
// This ensures we don't miss intersections with obstacles
for (var t = 0.1; t <= 1; t += 0.1) {
var bx = (1 - t) * (1 - t) * prevPoint.x + 2 * (1 - t) * t * cx + t * t * x;
var by = (1 - t) * (1 - t) * prevPoint.y + 2 * (1 - t) * t * cy + t * t * y;
points.push(new Point(bx, by));
}
}
}
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return points;
}
/**
* Simplify path by removing collinear intermediate points
*/
export function simplifyPath(points) {
if (points.length <= 2) {
return points;
}
var simplified = [points[0]];
for (var i = 1; i < points.length - 1; i++) {
var prev = simplified[simplified.length - 1];
var current = points[i];
var next = points[i + 1];
// Check if current point is collinear with prev and next
var isHorizontalLine = prev.y === current.y && current.y === next.y;
var isVerticalLine = prev.x === current.x && current.x === next.x;
// Only keep the point if it's not collinear (i.e., it's a corner)
if (!isHorizontalLine && !isVerticalLine) {
simplified.push(current);
}
}
// Always add the last point
simplified.push(points[points.length - 1]);
return simplified;
}