connector-routing 0.1.0

Connector routing for diagrams and schematics: many orthogonal connectors at once with bend, length and crossing costs, negotiated congestion, rip-up and reroute, and nudging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Nudging: final placement of routed segments (Wybrow et al. 2009, section 5;
//! arXiv 2309.01671).
//!
//! Routes that share graph edges lie on top of each other. Nudging moves every segment
//! that does not end at a port within its channel, so that shared stretches separate,
//! single segments run down the middle of their alley, and bundles spread evenly. It
//! keeps the order of overlapping segments (by the comparator described in `docs/design.md`) and every obstacle
//! side, so routes keep their topology and introduce no crossings.
//!
//! ```mermaid
//! flowchart LR
//!     S[segments of all nets] --> X[x pass: vertical segments]
//!     X --> Y[y pass: horizontal segments]
//!     subgraph pass
//!         O[line order] --> C[scan-line constraints]
//!         C --> G[largest common gap per component]
//!         G --> P[place between extreme solutions]
//!     end
//! ```
//!
//! The constraints are plain data ([`constraints`](fn@constraints)), so a layout engine can keep them
//! while moving obstacles (Dwyer, Marriott and Wybrow 2006).

mod constraints;
mod full;
mod order;
mod segments;
mod solve;
mod vpsc;

use alloc::collections::BTreeMap;
use alloc::vec::Vec;

use crate::geom::{Axis, Direction, Point, Rect};
use crate::graph::{RoutingGraph, VertexId};
use crate::nets::Net;
use crate::route::Route;

pub use constraints::{Constraint, Item, Kind};

use constraints::Object;
use segments::Segments;

/// Parameters of nudging.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
    /// The desired distance between parallel segments of different routes, and between
    /// segments and obstacle sides where there is room.
    pub separation: u32,
    /// The largest gap segments are spread to in channels bounded on both sides; wider
    /// channels keep their bundles centred at this gap. Use `u32::MAX` to spread
    /// segments over the whole channel (arXiv 2309.01671).
    pub max_separation: u32,
    /// How segments are placed within their feasible range.
    pub placement: Placement,
}

/// How nudging places segments once the separation is chosen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Placement {
    /// The largest common gap up to [`Config::max_separation`], then the midpoint of the
    /// extreme feasible positions: bundles spread evenly and centre in their channel
    /// (arXiv 2309.01671).
    #[default]
    Spread,
    /// Least-squares projection of desired positions onto the constraints at
    /// [`Config::separation`] (VPSC, as in Wybrow et al. 2009): the middle segment of a Z
    /// wants the middle of its alley, every other segment its current line (the
    /// obstacle side a U bends around), so routes stay closer to the graph.
    Project,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            separation: 4,
            max_separation: 8,
            placement: Placement::Spread,
        }
    }
}

/// Nudged routes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Nudged {
    /// One polyline per route, point for point like [`Route::points`]. A Z-shaped run
    /// whose middle shrinks to nothing keeps its (now coincident) points; use
    /// [`simplify`](crate::simplify) to drop them.
    pub routes: Vec<Vec<Point>>,
    /// The smallest gap achieved between segments that had to be separated, or `None`
    /// if no segments needed separation. Below [`Config::separation`] only where a
    /// channel is too narrow.
    pub separation: Option<u32>,
    /// The number of pairs of segments of different routes still lying on top of each
    /// other: both fixed by ports on the same line, or in a channel with no room. Zero
    /// when every shared stretch was separated.
    pub overlaps: usize,
}

/// Nudges routes of the orthogonal visibility graph of `graph` around `obstacles`.
///
/// # Examples
///
/// ```
/// use connector_routing::{Connector, DirSet, Point, Port, Rect, Scene, global, nudge};
///
/// let mut scene = Scene::new();
/// scene.add_obstacle(Rect::new(0, 0, 40, 10))?;
/// scene.add_obstacle(Rect::new(0, 30, 40, 40))?;
/// // Two connectors through the channel between the boxes share its middle line.
/// let a = scene.add_port(Port::new(Point::new(0, 10), DirSet::UP))?;
/// let b = scene.add_port(Port::new(Point::new(40, 30), DirSet::DOWN))?;
/// let c = scene.add_port(Port::new(Point::new(4, 10), DirSet::UP))?;
/// let d = scene.add_port(Port::new(Point::new(36, 30), DirSet::DOWN))?;
/// let connectors = [Connector { source: a, target: b }, Connector { source: c, target: d }];
/// let routing = global::route_all(&scene, &connectors, &global::Config::default())?;
///
/// let nudged = nudge::nudge(scene.graph(), &scene.rects(), &routing.routes, &nudge::Config::default());
/// assert!(nudged.separation.is_none_or(|s| s >= 4));
/// # Ok::<(), connector_routing::Error>(())
/// ```
#[must_use]
pub fn nudge(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    routes: &[Route],
    config: &Config,
) -> Nudged {
    let nets: Vec<Net> = routes.iter().map(|r| Net::from_route(graph, r)).collect();
    let (segments, separation) = run(graph, obstacles, &nets, *config);
    let polylines: Vec<Vec<Point>> = routes
        .iter()
        .enumerate()
        .map(|(i, r)| {
            bend_vertices(graph, &r.vertices)
                .into_iter()
                .map(|v| segments.position(graph, i, v, &nets))
                .collect()
        })
        .collect();

    let overlaps = count_overlaps(&polylines);

    Nudged {
        routes: polylines,
        separation: separation.map(|s| u32::try_from(s).unwrap_or(u32::MAX)),
        overlaps,
    }
}

/// A drawing after full nudging: moved boxes and the routes between them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FullNudged {
    /// The boxes, in input order: moved, and grown where their ports need more room.
    pub boxes: Vec<Rect>,
    /// One polyline per route, point for point like [`Route::points`].
    pub routes: Vec<Vec<Point>>,
}

/// Full nudging (arXiv 2309.01671): moves boxes as well as segments so that boxes,
/// segments and ports keep [`Config::separation`] apart, packing the drawing towards
/// low coordinates. Ports move with their boxes. `boxes` are the obstacles the routes
/// avoid, and every port must lie on a box boundary to move with it.
///
/// Returns `None` if the separation cannot be met; constrained [`nudge`] still applies
/// then.
///
/// # Examples
///
/// ```
/// use connector_routing::{Rect, nudge, pipeline};
///
/// let boxes = [Rect::new(0, 0, 20, 10), Rect::new(200, 0, 220, 10)];
/// let config = pipeline::Config { full_nudging: true, ..pipeline::Config::default() };
/// let drawing = pipeline::draw(&boxes, &[(0, 1)], &config)?;
/// // The gap between the boxes shrinks to what the route needs.
/// assert!(drawing.boxes[1].min.x < 200);
/// # Ok::<(), connector_routing::Error>(())
/// ```
#[must_use]
pub fn full(
    graph: &RoutingGraph,
    boxes: &[Rect],
    routes: &[Route],
    config: &Config,
) -> Option<FullNudged> {
    let nets: Vec<Net> = routes.iter().map(|r| Net::from_route(graph, r)).collect();
    let mut segments = Segments::new(graph, &nets);
    let mut moved = boxes.to_vec();
    let separation = i64::from(config.separation);
    if !full::pass(
        graph,
        &mut moved,
        &nets,
        &mut segments,
        Axis::Y,
        separation,
        &[],
    ) {
        return None;
    }

    let kisses = kiss_arcs(graph, &nets, &segments);
    if !full::pass(
        graph,
        &mut moved,
        &nets,
        &mut segments,
        Axis::X,
        separation,
        &kisses,
    ) {
        return None;
    }

    let polylines = routes
        .iter()
        .enumerate()
        .map(|(i, r)| {
            bend_vertices(graph, &r.vertices)
                .into_iter()
                .map(|v| segments.position(graph, i, v, &nets))
                .collect()
        })
        .collect();

    Some(FullNudged {
        boxes: moved,
        routes: polylines,
    })
}

/// Nudges arbitrary nets (connector paths or hyperedge trees) and returns, per net,
/// its segments as `(start, end)` pairs with the smallest achieved separation.
#[must_use]
pub fn nudge_nets(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    nets: &[Net],
    config: &Config,
) -> (Vec<Vec<(Point, Point)>>, Option<u32>) {
    let (segments, separation) = run(graph, obstacles, nets, *config);
    let mut out = alloc::vec![Vec::new(); nets.len()];
    for s in segments.list.iter().filter(|s| s.vertices.len() > 1) {
        let a = segments.position(graph, s.net, s.low(), nets);
        let b = segments.position(graph, s.net, s.high(), nets);
        out[s.net].push((a, b));
    }

    (
        out,
        separation.map(|s| u32::try_from(s).unwrap_or(u32::MAX)),
    )
}

/// The separation constraints of the pass that moves segments along `axis` (the `x`
/// pass moves vertical segments, `axis = Axis::Y`) for the given routes, before any
/// movement. Items index the segments in the order of [`segment_lines`].
#[must_use]
pub fn constraints(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    routes: &[Route],
    axis: Axis,
) -> Vec<Constraint> {
    let nets: Vec<Net> = routes.iter().map(|r| Net::from_route(graph, r)).collect();
    let segments = Segments::new(graph, &nets);
    let objects = pass_objects(graph, obstacles, &nets, &segments, axis, &[]);
    let mut constraints = constraints::generate(&objects);
    constraints.extend(shape_constraints(graph, &nets, &segments, axis));

    constraints
}

/// The constraints that keep every route's shape in the pass moving segments along
/// `axis`: a segment stays on its side of the far end of each perpendicular segment it
/// bends into, so no segment reverses.
fn shape_constraints(
    graph: &RoutingGraph,
    nets: &[Net],
    segments: &Segments,
    axis: Axis,
) -> Vec<Constraint> {
    let mut out = Vec::new();
    for (id, s) in segments.list.iter().enumerate() {
        if s.axis != axis || s.fixed {
            continue;
        }

        // Every vertex of the run, so branches attached mid-run (hyperedge junctions)
        // keep their direction too.
        for &end in &s.vertices {
            let across = s.forward().left();
            for dir in [across, across.reverse()] {
                if !nets[s.net].has_edge(end, dir) {
                    continue;
                }

                let Some(p) = segments.of_edge(graph, s.net, end, dir) else {
                    continue;
                };
                let perpendicular = &segments.list[p];
                let far = if perpendicular.low() == end {
                    perpendicular.high()
                } else if perpendicular.high() == end {
                    perpendicular.low()
                } else {
                    // The perpendicular run passes through: no end to keep apart from.
                    continue;
                };
                let other = far_item(graph, nets, segments, p, far);
                let (left, right) = if dir.is_positive() {
                    (Item::Segment(id), other)
                } else {
                    (other, Item::Segment(id))
                };
                out.push(Constraint {
                    left,
                    right,
                    kind: Kind::Shape,
                });
            }
        }
    }

    out
}

/// What holds the far end `far` of perpendicular segment `p`: the segment of the same
/// net it bends into there, or the fixed coordinate of `far`.
fn far_item(
    graph: &RoutingGraph,
    nets: &[Net],
    segments: &Segments,
    p: usize,
    far: VertexId,
) -> Item {
    let perpendicular = &segments.list[p];
    let across = perpendicular.forward().left();
    for dir in [across, across.reverse()] {
        if nets[perpendicular.net].has_edge(far, dir) {
            if let Some(q) = segments.of_edge(graph, perpendicular.net, far, dir) {
                return Item::Segment(q);
            }
        }
    }

    // A port: its terminal point along the pass axis, which full nudging can move.
    match segments.terminal_point(perpendicular.net, far, perpendicular.axis.other()) {
        Some(t) => Item::Segment(t),
        None => Item::Bound(graph.point(far).coordinate(perpendicular.axis)),
    }
}

/// The segments of the routes as `(route, axis, line coordinate)`, in the index order
/// used by [`constraints`](fn@constraints).
#[must_use]
pub fn segment_lines(graph: &RoutingGraph, routes: &[Route]) -> Vec<(usize, Axis, i32)> {
    let nets: Vec<Net> = routes.iter().map(|r| Net::from_route(graph, r)).collect();
    let segments = Segments::new(graph, &nets);

    segments
        .list
        .iter()
        .zip(&segments.line)
        .map(|(s, &l)| (s.net, s.axis, l))
        .collect()
}

/// The number of pairs of collinear segments of different polylines that overlap in
/// more than a point.
fn count_overlaps(polylines: &[Vec<Point>]) -> usize {
    let mut count = 0;
    for (i, a) in polylines.iter().enumerate() {
        for b in &polylines[i + 1..] {
            for pa in a.windows(2) {
                for pb in b.windows(2) {
                    count += usize::from(collinear_overlap(pa[0], pa[1], pb[0], pb[1]));
                }
            }
        }
    }

    count
}

/// Whether two axis-parallel segments lie on one line and share more than a point.
fn collinear_overlap(a0: Point, a1: Point, b0: Point, b1: Point) -> bool {
    let overlap = |p: i32, q: i32, r: i32, s: i32| p.min(q).max(r.min(s)) < p.max(q).min(r.max(s));
    if a0.y == a1.y && b0.y == b1.y && a0.y == b0.y {
        return overlap(a0.x, a1.x, b0.x, b1.x);
    }

    a0.x == a1.x && b0.x == b1.x && a0.x == b0.x && overlap(a0.y, a1.y, b0.y, b1.y)
}

/// The vertices of a path where it starts, bends or ends.
fn bend_vertices(graph: &RoutingGraph, path: &[VertexId]) -> Vec<VertexId> {
    let mut out = Vec::new();
    for (i, &v) in path.iter().enumerate() {
        let keep = i == 0
            || i + 1 == path.len()
            || Direction::between(graph.point(path[i - 1]), graph.point(v))
                != Direction::between(graph.point(v), graph.point(path[i + 1]));
        if keep {
            out.push(v);
        }
    }

    out
}

/// Runs both passes and returns the moved segments with the smallest separation.
fn run(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    nets: &[Net],
    config: Config,
) -> (Segments, Option<i64>) {
    let mut segments = Segments::new(graph, nets);
    let first = pass(graph, obstacles, nets, &mut segments, Axis::Y, config, &[]);
    let kisses = kiss_arcs(graph, nets, &segments);
    let second = pass(
        graph,
        obstacles,
        nets,
        &mut segments,
        Axis::X,
        config,
        &kisses,
    );
    let separation = match (first, second) {
        (Some(a), Some(b)) => Some(a.min(b)),
        (a, b) => a.or(b),
    };

    (segments, separation)
}

/// The scan objects of the pass moving segments along `axis`: those segments, with
/// their line order, and the obstacle sides.
fn pass_objects(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    nets: &[Net],
    segments: &Segments,
    axis: Axis,
    extra: &[(usize, usize)],
) -> Vec<Object> {
    let terminal = segments.terminal_arcs(nets, axis);
    let required = fixed_side_arcs(graph, obstacles, nets, segments, axis);
    let ranks = order::line_ranks(graph, nets, segments, axis, (&required, &terminal), extra);
    let mut objects = constraints::obstacle_objects(obstacles, axis.other());
    for (id, s) in segments.list.iter().enumerate() {
        if s.axis == axis {
            objects.push(Object {
                item: Item::Segment(id),
                position: segments.line[id],
                extent: segments.extent(graph, nets, id),
                rank: ranks[id],
            });
        }
    }

    objects
}

/// The arcs that put a movable segment on the free side of a fixed segment on its line
/// when an obstacle blocks the other side: the order is then forced by geometry.
fn fixed_side_arcs(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    nets: &[Net],
    segments: &Segments,
    axis: Axis,
) -> Vec<(usize, usize)> {
    let across = axis.other();
    let mut by_line: BTreeMap<i32, Vec<usize>> = BTreeMap::new();
    for (id, s) in segments.list.iter().enumerate() {
        if s.axis == axis {
            by_line.entry(segments.line[id]).or_default().push(id);
        }
    }

    let mut arcs = Vec::new();
    for (&line, members) in &by_line {
        for &m in members
            .iter()
            .filter(|&&m| !segments.list[m].fixed && segments.list[m].vertices.len() > 1)
        {
            let (lo, hi) = segments.extent(graph, nets, m);
            // Which sides of the line an obstacle blocks along this segment.
            let blocked = |high_side: bool| {
                obstacles.iter().any(|r| {
                    let (a, b) = r.range(across);
                    let (c, d) = r.range(axis);
                    let touches = if high_side { a == line } else { b == line };
                    touches && c < hi && lo < d
                })
            };
            let (low_blocked, high_blocked) = (blocked(false), blocked(true));
            if low_blocked == high_blocked {
                continue;
            }

            for &f in members.iter().filter(|&&f| {
                segments.list[f].fixed && segments.list[f].net != segments.list[m].net
            }) {
                // Port points (single vertices) count when they touch the segment.
                let (flo, fhi) = segments.extent(graph, nets, f);
                let point = segments.list[f].vertices.len() == 1;
                if flo.max(lo) < fhi.min(hi) || (point && lo <= flo && flo <= hi) {
                    arcs.push(if high_blocked { (m, f) } else { (f, m) });
                }
            }
        }
    }

    arcs
}

/// One pass: moves the segments along `axis` across it. Returns the smallest achieved
/// separation.
fn pass(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    nets: &[Net],
    segments: &mut Segments,
    axis: Axis,
    config: Config,
    extra: &[(usize, usize)],
) -> Option<i64> {
    let objects = pass_objects(graph, obstacles, nets, segments, axis, extra);
    let mut constraints = constraints::generate(&objects);
    constraints.extend(shape_constraints(graph, nets, segments, axis));
    let (wall_low, wall_high) = walls(graph, obstacles, segments, axis.other(), config);
    let scan_rank = scan_ranks(&objects, segments.list.len());
    let movable: Vec<bool> = segments
        .list
        .iter()
        .map(|s| s.axis == axis && !s.fixed)
        .collect();
    let lines = segments.line.clone();
    let constant = |item: Item| -> i64 {
        match item {
            Item::Segment(i) => i64::from(lines[i]),
            Item::ObstacleLow(i) => i64::from(obstacles[i].range(axis.other()).0),
            Item::ObstacleHigh(i) => i64::from(obstacles[i].range(axis.other()).1),
            Item::WallLow => wall_low,
            Item::WallHigh => wall_high,
            Item::Bound(c) => i64::from(c),
        }
    };
    let problem = solve::Problem {
        constraints: &constraints,
        constant: &constant,
        movable: &movable,
        order: &scan_rank,
        current: &lines,
        separation: i64::from(config.separation),
        max_separation: i64::from(config.max_separation),
        project: config.placement == Placement::Project,
        zig: &zig_segments(nets, segments),
    };
    let solution = problem.solve();
    for (id, &m) in movable.iter().enumerate() {
        if m {
            segments.line[id] =
                i32::try_from(solution.position[id]).expect("positions stay between the walls");
        }
    }

    solution.separation
}

/// Whether each segment is the middle of a Z or S: its perpendicular neighbours at its
/// two ends leave to opposite sides.
fn zig_segments(nets: &[Net], segments: &Segments) -> Vec<bool> {
    segments
        .list
        .iter()
        .map(|s| {
            let across = s.forward().left();
            let side = |v: VertexId| {
                let arms = nets[s.net].arms_at(v);
                match (arms.contains(across), arms.contains(across.reverse())) {
                    (true, false) => Some(true),
                    (false, true) => Some(false),
                    _ => None,
                }
            };
            s.vertices.len() > 1
                && matches!((side(s.low()), side(s.high())), (Some(a), Some(b)) if a != b)
        })
        .collect()
}

/// The scan rank of every segment: its index in the scan order of all objects.
fn scan_ranks(objects: &[Object], count: usize) -> Vec<usize> {
    let mut keyed: Vec<(i64, u32, usize)> = objects
        .iter()
        .filter_map(|o| match o.item {
            Item::Segment(id) => Some((i64::from(o.position), o.rank, id)),
            _ => None,
        })
        .collect();
    keyed.sort_unstable();
    let mut rank = alloc::vec![0; count];
    for (r, &(_, _, id)) in keyed.iter().enumerate() {
        rank[id] = r;
    }

    rank
}

/// The region walls along `position_axis`: the extent of the graph, obstacles and
/// segments, grown by enough room for every segment at the separation.
fn walls(
    graph: &RoutingGraph,
    obstacles: &[Rect],
    segments: &Segments,
    position_axis: Axis,
    config: Config,
) -> (i64, i64) {
    let coords = graph
        .vertices()
        .map(|v| graph.point(v).coordinate(position_axis))
        .chain(obstacles.iter().flat_map(|r| {
            let (a, b) = r.range(position_axis);
            [a, b]
        }));
    let (lo, hi) = coords.fold((i64::MAX, i64::MIN), |(lo, hi), c| {
        (lo.min(i64::from(c)), hi.max(i64::from(c)))
    });
    let count = i64::try_from(segments.list.len()).unwrap_or(i64::MAX / 4);
    let margin = i64::from(config.separation.max(1)) * (count + 2);

    (lo.saturating_sub(margin), hi.saturating_add(margin))
}

/// The order requirements between horizontal segments that touch end to end at a
/// vertex where both turn away from each other ("kiss"), given the positions of the
/// vertical segments after the `x` pass.
///
/// If net `a` turns at `v` with vertical arm `va` and horizontal arm `ha`, and net `b`
/// with the opposite arms, the two routes cross exactly when `a`'s vertical lies on
/// `b`'s horizontal side and `b`'s horizontal on `a`'s vertical side. So once the `x`
/// pass has put `a`'s vertical on `b`'s horizontal side, `b`'s horizontal must go to the
/// side of `b`'s vertical arm.
fn kiss_arcs(graph: &RoutingGraph, nets: &[Net], segments: &Segments) -> Vec<(usize, usize)> {
    let mut ends: BTreeMap<VertexId, Vec<usize>> = BTreeMap::new();
    for (id, s) in segments.list.iter().enumerate() {
        if s.axis == Axis::X && s.vertices.len() > 1 {
            ends.entry(s.low()).or_default().push(id);
            ends.entry(s.high()).or_default().push(id);
        }
    }

    let mut arcs = Vec::new();
    for (&v, list) in &ends {
        for (i, &a) in list.iter().enumerate() {
            for &b in &list[i + 1..] {
                if let Some(arc) = kiss(graph, nets, segments, v, a, b) {
                    arcs.push(arc);
                }
            }
        }
    }

    arcs
}

/// The order requirement between horizontal segments `a` and `b` kissing at `v`.
fn kiss(
    graph: &RoutingGraph,
    nets: &[Net],
    segments: &Segments,
    v: VertexId,
    a: usize,
    b: usize,
) -> Option<(usize, usize)> {
    let (sa, sb) = (&segments.list[a], &segments.list[b]);
    let (na, nb) = (&nets[sa.net], &nets[sb.net]);
    if sa.net == sb.net || na.terminals.contains(&v) || nb.terminals.contains(&v) {
        return None;
    }

    let arm = |net: &Net, dirs: [Direction; 2]| dirs.into_iter().find(|&d| net.has_edge(v, d));
    let (ha, hb) = (
        arm(na, [Direction::Left, Direction::Right])?,
        arm(nb, [Direction::Left, Direction::Right])?,
    );
    let (va, vb) = (
        arm(na, [Direction::Up, Direction::Down])?,
        arm(nb, [Direction::Up, Direction::Down])?,
    );
    if ha == hb
        || va == vb
        || na.arms_at(v).iter().count() != 2
        || nb.arms_at(v).iter().count() != 2
    {
        return None;
    }

    let xa = segments.line[segments.of_edge(graph, sa.net, v, va)?];
    let xb = segments.line[segments.of_edge(graph, sb.net, v, vb)?];
    let a_on_b_side = if hb == Direction::Right {
        xa > xb
    } else {
        xa < xb
    };
    if !a_on_b_side {
        return None;
    }

    // `b`'s horizontal goes to the side of `b`'s vertical arm: above `a` if it points up.
    Some(if vb == Direction::Up { (a, b) } else { (b, a) })
}