Routing Around Shapes in Vanilla-Diagram
While avoid-shapes is on (the default), an elbow connector without edited
points finds a path around the nodes in its way, its own end nodes
included, when one exists. Straight and
bezier connectors, and every connector with points, draw as
05-ports-and-connectors
describes. avoid-shapes="false" (the avoidShapes property) draws every
elbow as the plain one, which passes behind the shapes; changing it at
runtime redraws every elbow.
plain elbow routed around C
+---+ +---+ +---+ +-----+ +---+
| A o--+ +-o B | | A o-+ | | +-o B |
+---+ | | +---+ +---+ | | C | | +---+
+-|--|-+ | +-----+ |
| C| | +---------+
+-----+
Obstacles
Every node's box, a rotated node by its bounding box, padded by the stub
length (16 world px), kept in a spatial index (createIndex(), on the
shared VanillaDiagramSpatialIndex of features/spatial-index.feature.js:
a uniform grid of 256 px buckets, one box set or removed at a time, queried
in id order). The diagram keeps the index on the nodes as drawn: the write
pass sets each dirty node's box.
Left out:
- containers (a node of a symbol with
container: true): a connector between two lanes crosses their borders (13-containers); - for one connector, a node whose body (its unpadded box) holds one of its stub ends (two overlapping nodes, a free end dropped on a shape): the end couldn't leave it otherwise.
A node whose padded box, but not its body, holds a stub end (a neighbour less than two stub lengths away, often a rotated node, whose bounding box grows toward its neighbours) stays an obstacle for that connector. One side has its padding cut to the stub end's distance from the body, so the stub end lies on the box's edge; the other three sides keep 16 px. That side is one along the stub when the stub end lies beyond one, so the route can run on straight past the node, else the side the stub end lies furthest beyond. The route passes that node as close as the stub end does, never through it.
Two shapes closer than two stub lengths (32 px) leave no room for 16 px stubs:
- Facing ends: when the two ports face each other (opposite sides, each ahead of the other) less than 32 px apart, both stub ends lie on the plain elbow's line across the gap: halfway, or, in a gap under 20 px, leaving the target up to its 10 px arrowhead (05-ports-and-connectors). Each end node is then cut to that line, and the route crosses the gap straight, or with one jog on it. Not when such a point lies in an end node's body (a rotated end node, whose box reaches past its port): the router then keeps its 16 px stub ends and its usual rules, though the plain elbow it keeps when nothing is in its way meets halfway too (05-ports-and-connectors).
- A shape ahead: a 16 px stub with another shape's body straight ahead of its port closer than 32 px ends halfway to it, so the route turns in the middle of the gap rather than along the shape's border. Not when that point lies in its own node's body.
When no route is found this way (a stub end walled in by cut nodes, or a pocket), the route is looked for once more by a looser rule, when that rule differs: a stub's stretch to its node's padded edge also stops at another shape's padded box, no stub stops halfway to a shape ahead (facing ends keep their halfway stub ends), and every node whose padded box holds a stub end is left out, none cut. That route may cross one of them, as before cut padding.
The connector's own end nodes are obstacles, so a route doesn't come back
through the shape it leaves: its stubs end on their padded edges, and the
route leaves them cleanly. A stub that would end inside its own node's padded
box (a port inside the node's box, such as a document's bottom port or a
triangle's side ports, or a port of a rotated node that lies inside its
bounding box) runs on along its side to the padded edge before the route
bends. When that stretch would enter another shape's body (one in the
corner of a rotated node's box), the stub keeps its 16 px, and its own node
is then left out or cut as above, depending on whether the stub end lies in
its body or its padding; a neighbour's padding doesn't count. For
this, each end that is a port carries its node's id (node): the diagram
adds it to the ends of every routed elbow, of the connect and reconnect
previews and of connect mode's preview. A free end carries none, and an end
whose node isn't in the index (a container) keeps the 16 px stub.
The route
VanillaDiagramOrthogonalRouter.avoid(a, b, index, exclude) is pure, takes
the ends as VanillaDiagramRouting does ({ x, y, side }, plus node for a
port's end), and returns the points from port to port, or null:
- The fast path. The plain elbow is computed first: between the stub ends, after a stub moved out to its node's padded edge (the plain elbow from a point 16 px before the moved stub end, joined back to the port along the stub). When none of its segments between the two stub ends enters a padded box (checked against the boxes in the buckets it crosses), it is kept: a tidy diagram keeps the plain elbows, and the search runs only where something is in the way.
- The search. Over the obstacles whose padded boxes meet a window (the box of both stub ends, grown by four stub lengths on each side): the lines are each obstacle's padded edges, clamped to the window, the stub ends' x and y, and the window's edges; the vertices are their crossings outside every padded box; the edges join neighbouring vertices on one line when the step enters no box. That is an orthogonal visibility graph of the window: its size grows with the shapes in it, not with the area. A* runs from the source stub's end, leaving along its side, to the target stub's end, entering against its side. A step costs its length plus 40 world px per bend; the heuristic is the Manhattan distance; ties go to fewer bends, then to the lower vertex, so a route is the same every time. The path is joined to the ports by their stubs, collinear points merged.
- No route. The window's margin doubles, up to the box of every
obstacle. While the looser rule differs, the search by cut padding tries
three windows (margins of 64, 128 and 256 px) before it: a route it
can't find costs those three windows more than before cut padding, where
it used to grow through every window. A first window holding
more than 200 obstacles ends the search at once, with no second search
by the looser rule; a later one ends that search like no route found.
When there is still none (an end boxed in),
avoid()returns null and the diagram draws the plain elbow, which may cross.
What re-routes
Each routed elbow keeps its route and the route's bounding box
(_routes). _connectorRoute() returns the kept route, so the draw, the
route handles, _buildExport() and a segment drag read it without searching
again; a route marked to compute again (_routeDirty) routes on its next
read. Marked:
- the connectors of a node marked or changed (a move, a resize, a rotation, a symbol registered, an add, a remove), and a connector changed itself;
- when a node's box changes outside a gesture, every kept route whose box meets the node's old or new padded box, and the box it had before the gesture: a node dropped across a route pushes it aside, and a route that went around a node that left takes the shorter way again;
- every route on a load, on a change of more than 2,000 items (a layout),
and when
avoid-shapeschanges.
The kept routes a committed change sweeps are found through the connector index, each connector's route box (14-large-diagrams): O(hits), not O(m).
The route pass
Only the connectors meeting the drawn rect are drawn, and a drawn one routes
when drawn. The others route in the background after a load, in 8ms slices,
nearest the view first, each re-indexed by its route's box; one that now
meets the drawn rect is drawn. The pass pauses while a gesture previews,
picks up what a committed change marked, and stops on a load and
destroy(). Until a connector routes, the connector index holds the box of
its plain elbow.
During a gesture
In each frame of a move, a resize, a rotation or a nudge burst, only the previewed nodes' own connectors are marked, and they route until 4ms of routing in that frame; the rest of them draw the plain elbow for that frame, not kept, and route on a later pass. Other connectors keep their routes and may pass behind a dragged node until the release, which marks the routes the node left and the ones it now blocks. The connect preview and connect mode's preview route like any elbow.
Editing an avoiding route
The route handles sit on the route as drawn, and a segment drag stores its
bends as points, so the user edits the route they see. From then on it is
an edited route, kept as it is, even across a shape; points: null or a
double-click on a route handle returns it to routing.
Performance
diagram-performance.spec.js loads 1,000 nodes and 1,000 elbows between
neighbours (in PERF_SPECS, in chromium-perf):
- the load's routing pass, every elbow routed once (mostly by the fast path), stays within 50ms; it measures about 11ms in Chromium;
- a routed drag frame (a node dragged among the others, so that its two connectors search each frame) stays within 16ms; about 1.5ms;
- the fast path is O(segments times the boxes in their buckets); the search over k obstacles has O(k²) vertices and A* with a binary heap is O(V log V);
features/orthogonal-router.feature.jsis inDIAGRAM_BUNDLE_FAST_PATH: it runs per routed elbow on a drag frame and a load.