@rxflow/manhattan
Version:
Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance
261 lines (242 loc) • 13.3 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Point, Rectangle } from "../geometry";
import { getNodePosition, getNodeDimensions } from "../utils";
/**
* ObstacleMap class for managing obstacles in pathfinding
* Uses a grid-based spatial partitioning for efficient queries
*/
export var ObstacleMap = /*#__PURE__*/function () {
function ObstacleMap(options) {
_classCallCheck(this, ObstacleMap);
_defineProperty(this, "options", void 0);
_defineProperty(this, "mapGridSize", void 0);
_defineProperty(this, "map", void 0);
_defineProperty(this, "sourceAnchor", void 0);
_defineProperty(this, "targetAnchor", void 0);
this.options = options;
this.mapGridSize = 100;
this.map = new Map();
}
/**
* Build obstacle map from node lookup
*/
_createClass(ObstacleMap, [{
key: "build",
value: function build(nodeLookup, sourceNodeId, targetNodeId, sourceAnchor, targetAnchor) {
var _this = this;
var _this$options = this.options,
excludeNodes = _this$options.excludeNodes,
excludeShapes = _this$options.excludeShapes,
excludeTerminals = _this$options.excludeTerminals,
paddingBox = _this$options.paddingBox;
// Store anchors for later use in isAccessible
this.sourceAnchor = sourceAnchor;
this.targetAnchor = targetAnchor;
// Iterate through all nodes
nodeLookup.forEach(function (node) {
// Check if node should be excluded
var isExcludedNode = excludeNodes.includes(node.id);
var isExcludedShape = node.type ? excludeShapes.includes(node.type) : false;
var isSourceTerminal = excludeTerminals.includes('source') && node.id === sourceNodeId;
var isTargetTerminal = excludeTerminals.includes('target') && node.id === targetNodeId;
var isSource = node.id === sourceNodeId;
var isTarget = node.id === targetNodeId;
// Skip if node should be excluded (but not source/target - we handle them specially)
if (isExcludedNode || isExcludedShape || isSourceTerminal || isTargetTerminal) {
return;
}
// Calculate node bounding box with padding
var position = getNodePosition(node);
var dimensions = getNodeDimensions(node);
var bbox = new Rectangle(position.x, position.y, dimensions.width, dimensions.height).moveAndExpand(paddingBox);
// For source and target nodes, add them as full obstacles
// We'll handle anchor accessibility in isAccessible() method
if (isSource) {
console.log('[ObstacleMap] Adding source node as obstacle:');
console.log(' Node ID:', node.id);
console.log(' BBox:', "(".concat(bbox.x, ", ").concat(bbox.y, ", ").concat(bbox.width, ", ").concat(bbox.height, ")"));
console.log(' Anchor:', sourceAnchor ? "(".concat(sourceAnchor.x, ", ").concat(sourceAnchor.y, ")") : 'none');
} else if (isTarget) {
console.log('[ObstacleMap] Adding target node as obstacle:');
console.log(' Node ID:', node.id);
console.log(' BBox:', "(".concat(bbox.x, ", ").concat(bbox.y, ", ").concat(bbox.width, ", ").concat(bbox.height, ")"));
console.log(' Anchor:', targetAnchor ? "(".concat(targetAnchor.x, ", ").concat(targetAnchor.y, ")") : 'none');
}
// Map bbox to grid cells
var origin = bbox.getOrigin().snapToGrid(_this.mapGridSize);
var corner = bbox.getCorner().snapToGrid(_this.mapGridSize);
for (var x = origin.x; x <= corner.x; x += _this.mapGridSize) {
for (var y = origin.y; y <= corner.y; y += _this.mapGridSize) {
var key = new Point(x, y).toString();
if (!_this.map.has(key)) {
_this.map.set(key, []);
}
_this.map.get(key).push(bbox);
}
}
});
return this;
}
/**
* Shrink bbox to exclude the area around the anchor point
* This allows paths to start/end at the anchor but prevents crossing the node
*/
}, {
key: "shrinkBBoxAroundAnchor",
value: function shrinkBBoxAroundAnchor(bbox, anchor, nodePosition, nodeDimensions) {
var tolerance = 1;
var step = this.options.step || 10;
var margin = step * 3; // Increased margin for better clearance
// Determine which edge the anchor is on (relative to original node position)
var onLeft = Math.abs(anchor.x - nodePosition.x) < tolerance;
var onRight = Math.abs(anchor.x - (nodePosition.x + nodeDimensions.width)) < tolerance;
var onTop = Math.abs(anchor.y - nodePosition.y) < tolerance;
var onBottom = Math.abs(anchor.y - (nodePosition.y + nodeDimensions.height)) < tolerance;
console.log('[shrinkBBoxAroundAnchor] Edge detection:');
console.log(' anchor.x:', anchor.x, 'nodePosition.x:', nodePosition.x, 'diff:', Math.abs(anchor.x - nodePosition.x));
console.log(' anchor.x:', anchor.x, 'nodeRight:', nodePosition.x + nodeDimensions.width, 'diff:', Math.abs(anchor.x - (nodePosition.x + nodeDimensions.width)));
console.log(' onLeft:', onLeft, 'onRight:', onRight, 'onTop:', onTop, 'onBottom:', onBottom);
console.log(' margin:', margin);
// Shrink bbox based on anchor position
if (onLeft) {
// Anchor on left edge - exclude left portion
return new Rectangle(bbox.x + margin, bbox.y, Math.max(0, bbox.width - margin), bbox.height);
} else if (onRight) {
// Anchor on right edge - exclude right portion
return new Rectangle(bbox.x, bbox.y, Math.max(0, bbox.width - margin), bbox.height);
} else if (onTop) {
// Anchor on top edge - exclude top portion
return new Rectangle(bbox.x, bbox.y + margin, bbox.width, Math.max(0, bbox.height - margin));
} else if (onBottom) {
// Anchor on bottom edge - exclude bottom portion
return new Rectangle(bbox.x, bbox.y, bbox.width, Math.max(0, bbox.height - margin));
}
// If anchor is not on an edge, return original bbox
return bbox;
}
/**
* Check if a point is accessible (not inside any obstacle)
* Uses binary search optimization: step -> step/2 -> step/4 -> ... -> 1px
*/
}, {
key: "isAccessible",
value: function isAccessible(point) {
var checkRadius = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var key = point.clone().snapToGrid(this.mapGridSize).toString();
var rects = this.map.get(key);
if (!rects) {
return true;
}
// Check if point is near an anchor - if so, allow it
var margin = (this.options.step || 10) * 3;
if (this.sourceAnchor) {
var distToSource = Math.abs(point.x - this.sourceAnchor.x) + Math.abs(point.y - this.sourceAnchor.y);
if (distToSource < margin) {
console.log("[isAccessible] Point (".concat(point.x, ", ").concat(point.y, "): accessible (near source anchor)"));
return true;
}
}
if (this.targetAnchor) {
var distToTarget = Math.abs(point.x - this.targetAnchor.x) + Math.abs(point.y - this.targetAnchor.y);
if (distToTarget < margin) {
console.log("[isAccessible] Point (".concat(point.x, ", ").concat(point.y, "): accessible (near target anchor)"));
return true;
}
}
// If checkRadius is specified, use binary search to find accessible points
if (checkRadius > 0) {
return this.isAccessibleWithBinarySearch(point, checkRadius, rects);
}
var accessible = rects.every(function (rect) {
return !rect.containsPoint(point);
});
// Debug: log points on the direct path
if (point.y === 40 && point.x >= 0 && point.x <= 545) {
console.log("[isAccessible] Point (".concat(point.x, ", ").concat(point.y, "): ").concat(accessible ? 'accessible' : 'BLOCKED'));
if (!accessible) {
rects.forEach(function (rect, i) {
if (rect.containsPoint(point)) {
console.log(" Blocked by rect ".concat(i, ": (").concat(rect.x, ", ").concat(rect.y, ", ").concat(rect.width, ", ").concat(rect.height, ")"));
}
});
}
}
return accessible;
}
/**
* Check accessibility using binary search optimization
* Tries step -> step/2 -> step/4 -> ... -> 1px
*/
}, {
key: "isAccessibleWithBinarySearch",
value: function isAccessibleWithBinarySearch(point, maxRadius, rects) {
// First check the point itself
if (rects.every(function (rect) {
return !rect.containsPoint(point);
})) {
return true;
}
// Binary search: start with step, then halve until we reach 1px
var radius = maxRadius;
var offsets = [{
dx: 1,
dy: 0
},
// right
{
dx: -1,
dy: 0
},
// left
{
dx: 0,
dy: 1
},
// down
{
dx: 0,
dy: -1
} // up
];
while (radius >= 1) {
var _iterator = _createForOfIteratorHelper(offsets),
_step;
try {
var _loop = function _loop() {
var offset = _step.value;
var testPoint = new Point(point.x + offset.dx * radius, point.y + offset.dy * radius);
if (rects.every(function (rect) {
return !rect.containsPoint(testPoint);
})) {
return {
v: true
};
}
},
_ret;
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_ret = _loop();
if (_ret) return _ret.v;
}
// Halve the radius for next iteration
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
radius = Math.floor(radius / 2);
}
return false;
}
}]);
return ObstacleMap;
}();