diagram_core/src/route.rs#
Source: lib/diagram_core/src/route.rs
route -- the ways of connectors on the page: right angles around obstacles, straight, curved
(CN-04, CN-05)
Port { at, dir, own } an end of a route: the point; the direction the route
leaves it in (outward, any length; None: any); the box
of the shape the end is glued to (None: a free end)
Opts { clearance, bend } the distance kept to obstacles; what a bend costs, as a
length (fewer bends against a longer way)
orthogonal(a, b, &obstacles, &opts) -> points a right-angle way from a.at to b.at
best(&a_ports, &b_ports, &obstacles, &opts) -> (i, j, points) the cheapest way between
any two of the ports (a dynamic end offers one or two
per side)
best_through(&a, &b, &obstacles, &pass, &opts) (JD21, SS-19) the same; pass[k] says
which way the way may cross obstacle k (PASS_X across it
horizontally: ShapePermeableX; PASS_Y vertically)
hits_pass(p, q, &b, pass) hits_box, but a horizontal (vertical) segment through a
box it may cross does not count
straight(a, b) -> points the line between the two ends
cost(&points, &opts) / bends(&points) / length(&points)
spline_knots(n) -> (degree, knots) a clamped uniform B-spline over n control points: the
curved style takes the right-angle way's corners as the
control polygon of one NURBS (glue.rs writes the row)
hits_box(p, q, &b) whether a segment runs through a box's inside
Everything is in page coordinates (drawing units, y up); boxes are [x0, y0, x1, y1].
How orthogonal finds its way (deterministic, no state between calls):
1. Obstacles grow by the clearance. A box that holds one of the two ends (a container, the
group around a glued subshape) is no obstacle; the boxes of the glued shapes (Port::own)
are, unless one holds the other end inside it.
Glued shapes closer than twice the clearance keep half their distance instead.
2. Each end leaves its own box: with a direction along it (snapped to the nearer axis) up to
the grown box, without one in each of the four axis directions; a free end starts where
it is, in any direction. A direction that points into the own shape counts as none.
best() offers several ports for an end: all their exits start (or end) one search.
3. A sparse grid: the x and y lines of the grown boxes, of the exit points and the lines
halfway between the two ends' exits (so a Z bends in the middle, as Visio's do). Grid
points inside a grown box and grid edges through one are blocked; the lines of a grown box
itself stay open, so a way keeps exactly the clearance. (JD21) A box the way may cross
horizontally (vertically) blocks only its vertical (horizontal) edges and no grid point, so
a way runs straight through it but cannot turn inside it.
4. A* over (grid point, direction of travel) with the cost length + bend * bends; the
estimate is the Manhattan distance plus the fewest bends any way to a goal needs (from
the direction of travel and the one the goal wants), so the way found is the cheapest.
Arriving at the far end against its direction costs a bend, turning back two.
5. Only the obstacles near the two ends take part at first (a window around them); when the
way crosses one left out, the window grows to hold the way and the search runs again (the
third round takes all). A way that cannot be found (an end inside an obstacle) comes from
the glued boxes alone, else from a plain L.
6. Points on one line merge: the result has the two ends and the corners only; the middle
piece of a Z moves halfway between the pieces around it where that stays clear.
struct Portan end of a routeat: [f64; 2]-- where the route ends (page coordinates)dir: Option<[f64; 2]>-- the direction it leaves the end in (outward; None: any direction)own: Option<[f64; 4]>-- the box of the shape the end is glued to: the way does not cross it (None: free)
impl Port
fn free(at: [f64; 2]) -> Porta free end: any direction, no shapestruct Optswhat a route keeps toclearance: f64-- the distance to obstacles and the length of the first and last piece (drawing units)bend: f64-- the cost of a bend, as a length
fn hits_box(p: [f64; 2], q: [f64; 2], b: &[f64; 4]) -> boolwhether the segment p-q runs through the inside of box b (touching its edge is not)
/// (JD21, SS-19) a way may cross the obstacle horizontally (ShapePermeableX)
PASS_X: u8 = 1
/// (JD21, SS-19) a way may cross the obstacle vertically (ShapePermeableY)
PASS_Y: u8 = 2
fn hits_pass(p: [f64; 2], q: [f64; 2], b: &[f64; 4], pass: u8) -> bool(JD21) whether the segment p-q runs through the inside of box b where it may not: a horizontal segment through a box with PASS_X, a vertical one through a box with PASS_Y do notfn length(points: &[[f64; 2]]) -> f64the length of a polylinefn bends(points: &[[f64; 2]]) -> usizethe number of bends of a polyline (corners whose two pieces are not on one line)fn cost(points: &[[f64; 2]], opts: &Opts) -> f64length + bends * opts.bendfn straight(a: &Port, b: &Port) -> Vec<[f64; 2]>the straight route: the two endsfn spline_knots(n: usize) -> (usize, Vec<f64>)a clamped uniform B-spline over n control points (n >= 2): its degree (at most 3) and its n + degree + 1 knots from 0 to 1fn orthogonal(a: Port, b: Port, obstacles: &[[f64; 4]], opts: &Opts) -> Vec<[f64; 2]>a right-angle route from a.at to b.at around the obstacles (boxes in page coordinates): the two ends and the cornersfn best(a: &[Port], b: &[Port], obstacles: &[[f64; 4]], opts: &Opts) -> (usize, usize, Vec<[f64; 2]>)the cheapest right-angle route from any of the portsato any of the portsb(one search: a dynamic end offers a port per side) -> the indices of the two ports and the route; both lists must not be emptyfn best_through(a: &[Port], b: &[Port], obstacles: &[[f64; 4]], pass: &[u8], opts: &Opts) -> (usize, usize, Vec<[f64; 2]>)(JD21, SS-19) best() among obstacles the way may cross:pass[k](PASS_X, PASS_Y; missing: 0) for obstacles[k]