use std::collections::BTreeMap;
use haecceity::hlr::View;
use super::holes::SPOTFACE;
use super::{
ARROW, ARROW_HALF, CAP, GAP, LIFT, OVERSHOOT, P2, P3, Placed, PlannedDimension, Shape, TEXT,
TOKEN_GAP, Want, first_line, font, key, lane_pitch,
};
use crate::layout::{FRAME, Lanes, margin};
use crate::model::{Analysis, PlannedSheet, PlannedView, Problem};
use crate::output::{STRAY_TEXT, label_gap, linear_ink, shared_dimension_line};
use crate::text::{Font, Segment};
type Shared = (Side, usize, [f64; 2]);
type Strips<'a> = BTreeMap<(&'a str, Side), Vec<(bool, u8, i64, usize)>>;
pub(super) struct Frame {
view: View,
broken: Option<crate::broken::Break>,
at: P2,
s: f64,
centre: P2,
inspection: Option<(usize, f64)>,
outline: [f64; 4],
}
impl Frame {
pub(super) fn memo_geometry(&self) -> Vec<u64> {
let mut key: Vec<_> = self
.view
.x
.iter()
.chain(&self.view.y)
.chain(&self.view.toward)
.chain(&self.at)
.chain(std::iter::once(&self.s))
.chain(&self.centre)
.chain(&self.outline)
.map(|n| n.to_bits())
.collect();
if let Some(b) = &self.broken {
key.extend(b.key());
}
key
}
pub(super) fn new(analysis: &Analysis, v: &PlannedView) -> Frame {
let mut f = Frame::looking(analysis, v.direction, v.up, v.at, v.scale);
f.broken = v.broken.clone();
if v.detail.is_some() || v.broken.is_some() {
let (size, centre) = crate::detail::crop_extent(analysis, v);
f.centre = centre;
f.outline = [
v.at[0] - size[0] * v.scale / 2.0,
v.at[1] - size[1] * v.scale / 2.0,
v.at[0] + size[0] * v.scale / 2.0,
v.at[1] + size[1] * v.scale / 2.0,
];
}
f
}
pub(super) fn looking(analysis: &Analysis, direction: P3, up: P3, at: P2, s: f64) -> Frame {
let view = View::new(direction, up).expect("a planned view");
let (size, centre) = crate::model::extent(analysis, &view);
Frame {
view,
broken: None,
at,
s,
centre,
inspection: super::turned::axial_datum(analysis),
outline: [
at[0] - 0.5 * s * size[0],
at[1] - 0.5 * s * size[1],
at[0] + 0.5 * s * size[0],
at[1] + 0.5 * s * size[1],
],
}
}
pub(super) fn outline(&self) -> [f64; 4] {
self.outline
}
pub(super) fn scale(&self) -> f64 {
self.s
}
pub(super) fn page(&self, p: P3) -> P2 {
let q = self.view.map(p);
let q = self.broken.as_ref().map_or(q, |b| b.map(q));
[
self.at[0] + self.s * (q[0] - self.centre[0]),
self.at[1] + self.s * (q[1] - self.centre[1]),
]
}
}
pub struct LabelInk {
pub glyphs: Vec<Vec<Vec<Segment>>>,
pub strokes: Vec<[P2; 2]>,
pub min: P2,
pub max: P2,
}
pub fn label_ink(terms: &[String], font: &Font) -> LabelInk {
if terms.iter().any(|t| crate::callouts::is_break(t)) {
let mut combined = LabelInk {
glyphs: Vec::new(),
strokes: Vec::new(),
min: [f64::INFINITY; 2],
max: [f64::NEG_INFINITY; 2],
};
for (i, line) in crate::callouts::lines(terms).iter().enumerate() {
let ink = label_ink(line, font);
let rise = -(i as f64) * crate::callouts::line_spacing();
for k in 0..2 {
let shift = if k == 1 { rise } else { 0.0 };
combined.min[k] = combined.min[k].min(ink.min[k] + shift);
combined.max[k] = combined.max[k].max(ink.max[k] + shift);
}
let shift = |p: P2| [p[0], p[1] + rise];
combined
.strokes
.extend(ink.strokes.into_iter().map(|[a, b]| [shift(a), shift(b)]));
combined.glyphs.extend(ink.glyphs.into_iter().map(|g| {
g.into_iter()
.map(|c| c.into_iter().map(|s| moved(&s, &shift)).collect())
.collect()
}));
}
return combined;
}
let h = TEXT;
let mut ink = LabelInk {
glyphs: Vec::new(),
strokes: Vec::new(),
min: [f64::INFINITY; 2],
max: [f64::NEG_INFINITY; 2],
};
let grow = |ink: &mut LabelInk, a: P2, b: P2| {
for p in [a, b] {
ink.min = [ink.min[0].min(p[0]), ink.min[1].min(p[1])];
ink.max = [ink.max[0].max(p[0]), ink.max[1].max(p[1])];
}
};
let mut x = 0.0;
let mut tokens: Vec<(Option<char>, String)> = Vec::new();
let plain = |t: &str, tokens: &mut Vec<(Option<char>, String)>| {
let mut chars = t.chars();
match chars.next() {
_ if t == SPOTFACE => tokens.push((Some('⌴'), "SF".into())),
Some(c @ ('⌀' | '↧' | '⌴' | '⌵')) => {
tokens.push((Some(c), String::new()));
let rest: String = chars.collect();
if !rest.is_empty() {
tokens.push((None, rest));
}
}
_ => tokens.push((None, t.into())),
}
};
for t in terms {
if let Some((above, below)) = t.split_once('\n') {
let (prefix, upper) = above.rsplit_once(' ').unwrap_or(("", above));
let end = below
.find(|c: char| !c.is_ascii_digit() && !matches!(c, '+' | '-' | '.'))
.unwrap_or(below.len());
let (lower, suffix) = below.split_at(end);
if upper.parse::<f64>().is_ok() && lower.parse::<f64>().is_ok() {
if !prefix.is_empty() {
plain(prefix, &mut tokens);
}
tokens.push((Some('≔'), format!("{upper}\n{lower}")));
if !suffix.trim().is_empty() {
plain(suffix.trim(), &mut tokens);
}
continue;
}
}
plain(t, &mut tokens);
}
for (i, (symbol, text)) in tokens.iter().enumerate() {
if i > 0 {
x += TOKEN_GAP;
}
let glyphs = |s: &str, at: f64, ink: &mut LabelInk| {
let laid = font.baseline(s, h);
if !laid.contours.is_empty() {
grow(
ink,
[at + laid.min[0], laid.min[1]],
[at + laid.max[0], laid.max[1]],
);
}
let shift = |p: P2| [p[0] + at, p[1]];
ink.glyphs.push(
laid.contours
.into_iter()
.map(|c| c.into_iter().map(|s| moved(&s, &shift)).collect())
.collect(),
);
};
match symbol {
Some('≔') => {
let (upper, lower) = text.split_once('\n').expect("deviation stack");
let size = 0.7 * h;
let mut width: f64 = 0.0;
for (value, rise) in [(upper, 0.55 * CAP), (lower, -0.45 * CAP)] {
let laid = font.baseline(value, size);
grow(
&mut ink,
[x + laid.min[0], rise + laid.min[1]],
[x + laid.max[0], rise + laid.max[1]],
);
let shift = |p: P2| [x + p[0], rise + p[1]];
ink.glyphs.push(
laid.contours
.into_iter()
.map(|c| c.into_iter().map(|s| moved(&s, &shift)).collect())
.collect(),
);
width = width.max(font.advance(value, size));
}
x += width;
}
Some('⌀') => {
let at = x + 0.5 * (h - font.advance("ø", h));
glyphs("ø", at, &mut ink);
x += h;
}
Some('⌴') if text == "SF" => {
let (l, r) = (x + 0.15 * h, x + SF_CELL - 0.15 * h);
let strokes = [
[[l, CAP], [l, 0.0]],
[[l, 0.0], [r, 0.0]],
[[r, 0.0], [r, CAP]],
];
for s in &strokes {
grow(&mut ink, s[0], s[1]);
}
ink.strokes.extend(strokes);
let at = x + 0.5 * (SF_CELL - font.advance(text, SF_TEXT));
let laid = font.baseline(text, SF_TEXT);
grow(
&mut ink,
[at + laid.min[0], SF_RAISE + laid.min[1]],
[at + laid.max[0], SF_RAISE + laid.max[1]],
);
let shift = |p: P2| [p[0] + at, p[1] + SF_RAISE];
ink.glyphs.push(
laid.contours
.into_iter()
.map(|c| c.into_iter().map(|s| moved(&s, &shift)).collect())
.collect(),
);
x += SF_CELL;
}
Some(c) => {
let (l, m, r) = (x + 0.15 * h, x + 0.5 * h, x + 0.85 * h);
let strokes: Vec<[P2; 2]> = match c {
'↧' => vec![
[[l, CAP], [r, CAP]],
[[m, CAP], [m, 0.0]],
[[m - 0.2 * h, 0.25 * h], [m, 0.0]],
[[m, 0.0], [m + 0.2 * h, 0.25 * h]],
],
'⌴' => vec![
[[l, CAP], [l, 0.0]],
[[l, 0.0], [r, 0.0]],
[[r, 0.0], [r, CAP]],
],
_ => vec![[[l, CAP], [m, 0.0]], [[m, 0.0], [r, CAP]]],
};
for s in &strokes {
grow(&mut ink, s[0], s[1]);
}
ink.strokes.extend(strokes);
x += h;
}
None => {
glyphs(text, x, &mut ink);
x += font.advance(text, h);
}
}
}
if !ink.min[0].is_finite() {
(ink.min, ink.max) = ([0.0; 2], [0.0; 2]);
}
ink
}
const SF_CELL: f64 = 1.4 * TEXT;
const SF_TEXT: f64 = 0.6 * TEXT;
const SF_RAISE: f64 = 0.12 * TEXT;
pub(super) fn moved(s: &Segment, f: &dyn Fn(P2) -> P2) -> Segment {
match *s {
Segment::Line(a, b) => Segment::Line(f(a), f(b)),
Segment::Quad(a, b, c) => Segment::Quad(f(a), f(b), f(c)),
Segment::Cubic(a, b, c, d) => Segment::Cubic(f(a), f(b), f(c), f(d)),
}
}
pub(super) fn to_page(origin: P2, rotation: f64) -> impl Fn(P2) -> P2 {
let (sin, cos) = rotation.to_radians().sin_cos();
move |p: P2| {
[
origin[0] + cos * p[0] - sin * p[1],
origin[1] + sin * p[0] + cos * p[1],
]
}
}
pub(super) fn label_box(ink: &LabelInk, origin: P2, rotation: f64) -> [f64; 4] {
let f = to_page(origin, rotation);
let corners = [
f(ink.min),
f([ink.max[0], ink.min[1]]),
f(ink.max),
f([ink.min[0], ink.max[1]]),
];
let mut b = [
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
];
for c in corners {
b = [
b[0].min(c[0]),
b[1].min(c[1]),
b[2].max(c[0]),
b[3].max(c[1]),
];
}
b
}
pub(super) fn arrow(tip: P2, from: P2) -> [P2; 2] {
let d = [from[0] - tip[0], from[1] - tip[1]];
let n = d[0].hypot(d[1]).max(1e-12);
[tip, [tip[0] + ARROW * d[0] / n, tip[1] + ARROW * d[1] / n]]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
pub(super) enum Side {
Below,
Above,
Left,
Right,
}
pub(super) fn strip(view: &str, horizontal: bool) -> Side {
match (view, horizontal) {
("plan", true) => Side::Above,
(_, true) => Side::Below,
("plan", false) => Side::Left,
_ => Side::Right,
}
}
fn callout_strip(view: &str) -> Side {
if view == "front" {
Side::Below
} else {
Side::Above
}
}
fn aligned_strip(a: P2, b: P2, outline: [f64; 4], preferred: Side) -> Side {
let mut n = [a[1] - b[1], b[0] - a[0]];
let outward = match preferred {
Side::Above => [0.0, 1.0],
Side::Below => [0.0, -1.0],
Side::Left => [-1.0, 0.0],
Side::Right => [1.0, 0.0],
};
if n[0] * outward[0] + n[1] * outward[1] < 0.0 {
n = n.map(|v| -v);
}
let distance = (0..2)
.map(|k| n[k] * (a[k] + b[k] - outline[k] - outline[k + 2]))
.sum::<f64>();
if distance < -1e-6 {
opposite(preferred)
} else {
preferred
}
}
fn lane_at(o: [f64; 4], side: Side, lane: usize) -> (f64, f64) {
let d = first_line() + lane as f64 * lane_pitch();
match side {
Side::Below => (o[1] - d, -1.0),
Side::Above => (o[3] + d, 1.0),
Side::Left => (o[0] - d, -1.0),
Side::Right => (o[2] + d, 1.0),
}
}
pub(crate) fn displaced_linear_fields(
analysis: &crate::model::Analysis,
plan: &crate::model::Plan,
) -> Vec<(String, Vec<&'static str>)> {
use crate::placement_intent::{Along, Side as Requested, Slot};
let mut out = Vec::new();
for d in &plan.dimensions {
let Some(request) = plan
.spec
.dimensions
.get(&d.id)
.and_then(|d| d.place.as_ref())
else {
continue;
};
let Some(p) = d.placed.as_ref().filter(|_| d.kind == "linear") else {
continue;
};
let Some(line) = linear_ink(&p.lines) else {
continue;
};
let Some(view) = plan.views.iter().find(|v| v.id == d.view) else {
continue;
};
let outline = Frame::new(analysis, view).outline();
let side = if line.k == 0 {
if line.offset > (outline[1] + outline[3]) / 2.0 {
Side::Above
} else {
Side::Below
}
} else if line.offset > (outline[0] + outline[2]) / 2.0 {
Side::Right
} else {
Side::Left
};
let mut fields = Vec::new();
if request.side.is_some_and(|s| {
!matches!(
(s, side),
(Requested::Above, Side::Above)
| (Requested::Below, Side::Below)
| (Requested::Left, Side::Left)
| (Requested::Right, Side::Right)
)
}) {
fields.push("side");
}
if let Some(along) = request.along {
let k = line.k;
let kept = if along == Along::Slot(Slot::Outside) {
p.label_box[k] >= line.extent[1] - 0.01
&& (p.label_box[k] - line.extent[1] - 1.0).abs() <= 2.0
} else {
let at = line.span[0] + along.fraction() * (line.span[1] - line.span[0]);
((p.label_box[k] + p.label_box[k + 2]) / 2.0 - at).abs() <= 2.0
};
if !kept {
fields.push("along");
}
}
if request
.lane
.is_some_and(|lane| (lane_at(outline, side, lane).0 - line.offset).abs() > 0.01)
{
fields.push("lane");
}
if !fields.is_empty() {
out.push((d.id.clone(), fields));
}
}
out
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct LabelRows {
pub outline: [f64; 4],
pub first_row: BTreeMap<String, f64>,
pub row_spacing: BTreeMap<String, f64>,
}
fn callout_step(side: Side, width: f64, canonical_width: f64) -> f64 {
if matches!(side, Side::Left | Side::Right) {
canonical_width.max(width) + super::SHOULDER + lane_pitch()
} else {
lane_pitch()
}
}
pub fn label_rows(analysis: &Analysis, view: &PlannedView, width: f64) -> LabelRows {
let outline = Frame::new(analysis, view).outline();
let sides = [
("above", Side::Above),
("below", Side::Below),
("left", Side::Left),
("right", Side::Right),
];
LabelRows {
outline,
first_row: sides
.iter()
.map(|(name, side)| (name.to_string(), lane_at(outline, *side, 0).0))
.collect(),
row_spacing: sides
.iter()
.map(|(name, side)| (name.to_string(), callout_step(*side, width, width)))
.collect(),
}
}
pub(super) fn across(frame: &Frame, axis: usize) -> Option<bool> {
let mut e = [0.0; 3];
e[axis] = 1.0;
let u = frame.view.map(e);
if u[0].abs() > 0.5 {
Some(true)
} else if u[1].abs() > 0.5 {
Some(false)
} else {
None
}
}
pub(super) fn thickness_strip(view: &str, axis: usize, horizontal: bool) -> Side {
match (view, axis) {
("front", 2) => Side::Left,
("side", 1) => Side::Above,
_ => strip(view, horizontal),
}
}
pub(super) const ON_SILHOUETTE: f64 = 0.05;
pub(super) const NEAR_SILHOUETTE: f64 = 3.0;
pub(super) fn silhouette_gap(frame: &Frame, p: P3, horizontal: bool) -> (Side, f64) {
let o = frame.outline();
let c = usize::from(horizontal);
let q = frame.page(p)[c];
let (low, high) = if horizontal {
(Side::Below, Side::Above)
} else {
(Side::Left, Side::Right)
};
let (dl, dh) = ((q - o[c]).abs(), (q - o[c + 2]).abs());
if dl <= dh { (low, dl) } else { (high, dh) }
}
fn plate_strip(frame: &Frame, view: &str, shape: &Shape, horizontal: bool) -> Side {
match shape {
Shape::Linear {
a,
axis,
a_edge,
b_edge,
..
} => (!a_edge && !b_edge)
.then(|| silhouette_gap(frame, *a, horizontal))
.filter(|(_, d)| *d <= NEAR_SILHOUETTE)
.map_or_else(
|| thickness_strip(view, *axis, horizontal),
|(side, _)| side,
),
_ => strip(view, horizontal),
}
}
pub(super) fn automatic_slot_layout(analysis: &Analysis) -> bool {
!crate::nominal::comparison()
&& crate::nominal::geometry_eligible(analysis)
&& !analysis.features.iter().any(|f| f.kind == "section_recess")
&& analysis
.features
.iter()
.any(|f| matches!(f.kind.as_str(), "slot" | "slot_pattern"))
}
pub(super) fn physical_witness_retry(analysis: &Analysis) -> bool {
if !crate::nominal::authored_pmi(analysis) {
return automatic_slot_layout(analysis);
}
if crate::nominal::comparison()
|| !analysis
.recognition
.as_ref()
.is_some_and(|r| r.recogniser.starts_with("quiddity-rust "))
|| analysis.features.iter().any(|f| f.kind == "section_recess")
|| !analysis
.features
.iter()
.any(|f| matches!(f.kind.as_str(), "slot" | "slot_pattern"))
{
return false;
}
analysis.features.iter().all(|f| {
matches!(
f.kind.as_str(),
"hole" | "hole_pattern" | "slot" | "slot_pattern" | "fillet" | "chamfer"
)
})
}
pub(super) fn pmi_witness_retry(analysis: &Analysis) -> bool {
if crate::nominal::comparison() {
return false;
}
crate::planning::memo(analysis, "pmi-witness-retry", Vec::new(), || {
physical_witness_retry(analysis) && crate::nominal::authored_pmi(analysis)
})
}
fn body_strip(frame: &Frame, w: &Want, horizontal: bool, slot_layout: bool) -> Option<Side> {
match &w.shape {
Shape::Linear {
a,
a_edge: false,
b_edge: false,
..
} if !w.measurements.is_empty()
&& w.measurements.iter().all(|m| {
matches!(
m.parameter.as_str(),
"step.length" | "gusset_location.length"
) || (slot_layout
&& matches!(
m.parameter.as_str(),
"slot_length.length" | "slot_centres.length"
))
}) =>
{
Some(silhouette_gap(frame, *a, horizontal).0)
}
_ => None,
}
}
pub(super) fn station_strip(frame: &Frame, w: &Want, horizontal: bool) -> Option<Side> {
let Shape::Linear {
a,
b,
a_edge,
b_edge,
..
} = &w.shape
else {
return None;
};
if w.measurements.is_empty()
|| !w
.measurements
.iter()
.all(|m| m.parameter.starts_with("profile_location_"))
|| a_edge == b_edge
{
return None;
}
let corner = if *a_edge { b } else { a };
let (side, gap) = silhouette_gap(frame, *corner, horizontal);
(gap <= ON_SILHOUETTE).then_some(side)
}
pub(super) fn owners(
analysis: &Analysis,
spec: &crate::model::Spec,
items: &[Want],
views_of: &[Option<&'static str>],
) -> Vec<Option<usize>> {
let mut kept: Vec<(Want, bool, Option<&'static str>, Vec<&'static str>)> = items
.iter()
.zip(views_of)
.map(|(w, view)| (w.clone(), true, *view, Vec::new()))
.collect();
let mut given = vec![Vec::new(); kept.len()];
let mut omitted = Vec::new();
super::plates::consolidate(analysis, &mut kept, &mut omitted);
super::bosses::consolidate(analysis, &mut kept, &mut omitted);
super::holes::consolidate(analysis, &mut kept, &mut given, &mut omitted);
super::recesses::consolidate(analysis, &mut kept, &mut given, &mut omitted);
super::plates::share(analysis, &mut kept, &mut given, &mut omitted);
super::share(analysis, &mut kept, &mut given, &mut omitted);
super::redundancy::consolidate(analysis, spec, &mut kept, &mut given, &mut omitted);
let index: BTreeMap<&str, usize> = items
.iter()
.enumerate()
.map(|(i, w)| (w.id.as_str(), i))
.collect();
let mut owner = vec![None; items.len()];
for o in &omitted {
let survivor = match &o.state {
crate::items::State::Covered { by } => index.get(by.as_str()),
_ => None,
};
if let (Some(&i), Some(&s)) = (index.get(o.id.as_str()), survivor) {
owner[i] = Some(s);
}
}
for i in 0..owner.len() {
let mut s = owner[i];
for _ in 0..owner.len() {
match s.and_then(|s| owner[s]) {
Some(next) => s = Some(next),
None => break,
}
}
owner[i] = s;
}
owner
}
fn through_notch_strip(
analysis: &Analysis,
frame: &Frame,
w: &Want,
axis: usize,
horizontal: bool,
) -> Option<Side> {
let f = w.measurements.iter().find_map(|m| {
analysis
.features
.iter()
.find(|f| f.id == m.feature && f.kind == "rectangular_through_notch")
})?;
let n = crate::lint::rectangular_notches::read(analysis, f).ok()?;
if axis == n.wide {
let (a, b) = (frame.page(n.at), frame.page(n.mouth));
let k = usize::from(horizontal);
Some(if k == 1 {
if b[k] > a[k] {
Side::Above
} else {
Side::Below
}
} else if b[k] > a[k] {
Side::Right
} else {
Side::Left
})
} else {
Some(silhouette_gap(frame, n.locator(), horizontal).0)
}
}
pub(super) fn lanes<'a>(
analysis: &Analysis,
frames: &BTreeMap<&str, Frame>,
items: impl ExactSizeIterator<Item = (&'a Want, Option<&'static str>)>,
owner: &[Option<usize>],
) -> (Vec<Option<Side>>, Vec<usize>) {
let items: Vec<_> = items.collect();
let n = items.len();
let ordinary = physical_witness_retry(analysis)
&& !analysis
.features
.iter()
.any(|f| f.id.starts_with("f:section_recess:"));
let slot_views: std::collections::BTreeSet<_> = items
.iter()
.filter(|(w, _)| {
w.measurements.iter().any(|m| {
matches!(
m.parameter.as_str(),
"slot_width.length" | "slot_length.length" | "slot_centres.length"
)
})
})
.filter_map(|(_, view)| *view)
.collect();
let mut constrained = vec![false; n];
let mut footprints = BTreeMap::new();
let mut strips: Strips = BTreeMap::new();
let mut sides: Vec<Option<Side>> = vec![None; n];
for (i, &(w, view)) in items.iter().enumerate() {
let Some(view) = view else { continue };
let f = &frames[view];
let (mut side, rank) = match &w.shape {
Shape::Linear {
a, b, axis, order, ..
} => {
let Some(horizontal) = across(f, *axis) else {
continue;
};
(
if w.measurements
.iter()
.any(|m| m.parameter == "thickness.length")
{
plate_strip(f, view, &w.shape, horizontal)
} else if matches!(view, "front" | "side")
&& horizontal
&& w.measurements.iter().any(|m| {
m.feature.starts_with("notch:") && m.parameter.starts_with("location.")
})
{
Side::Above
} else if let Some(side) =
through_notch_strip(analysis, f, w, *axis, horizontal)
{
side
} else if let Some(side) =
body_strip(f, w, horizontal, physical_witness_retry(analysis))
{
side
} else if w.measurements.iter().any(|m| {
m.feature.starts_with("notch:") && m.parameter.starts_with("location.")
}) && silhouette_gap(f, *b, horizontal).1 <= ON_SILHOUETTE
{
silhouette_gap(f, *b, horizontal).0
} else if let Some(side) = station_strip(f, w, horizontal) {
side
} else {
strip(view, horizontal)
},
(*order, key((b[*axis] - a[*axis]).abs())),
)
}
Shape::Aligned { a, b } => {
let (a, b) = (f.page(*a), f.page(*b));
(
aligned_strip(
a,
b,
f.outline(),
strip(view, (b[0] - a[0]).abs() >= (b[1] - a[1]).abs()),
),
(1, key((b[0] - a[0]).hypot(b[1] - a[1]))),
)
}
Shape::Callout { sites, arcs } => {
let side = callout_strip(view);
let (tip, _) = callout_tip(f, sites, arcs, side == Side::Above);
(side, (3, -key(tip[0])))
}
Shape::Angular { .. } => continue,
};
if ordinary
&& slot_views.contains(view)
&& matches!(w.shape, Shape::Linear { order: 1, .. })
&& !w.measurements.is_empty()
{
let footprint = footprints.entry(view).or_insert_with(|| {
let key = serde_json::to_vec(&(view, f.memo_geometry())).expect("frame serializes");
crate::planning::memo(analysis, "lane-location-footprints", key, || {
std::rc::Rc::new(super::footprints::Footprints::new(analysis, f, view))
})
});
let counter = footprint.extension_counter(&w.measurements);
let hits = |side| {
place(f, w, side, 0).map(|p| {
p.lines
.iter()
.take(2)
.map(|l| counter.count(*l))
.sum::<usize>()
})
};
let other = opposite(side);
if let (Some(home), Some(away)) = (hits(side), hits(other)) {
if away < home {
side = other;
}
constrained[i] = home.min(away) == 0 && home != away;
}
}
sides[i] = Some(side);
if owner[i].is_none() {
strips.entry((view, side)).or_default().push((
rank.0 >= 2
|| (!w.measurements.is_empty()
&& w.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))),
rank.0,
rank.1,
i,
));
}
}
let mut corners: BTreeMap<&str, Vec<P3>> = BTreeMap::new();
for &(w, view) in &items {
if super::profile_angles::is_profile_angle(w)
&& let (Some(view), Shape::Angular { angle, alternates }) = (view, &w.shape)
{
let points = corners.entry(view).or_default();
points.push(angle.vertex);
points.extend(alternates.iter().map(|a| a.vertex));
}
}
let local: BTreeMap<_, _> = corners
.iter()
.map(|(&view, points)| {
(
view,
super::pmi_dimensions::local_plane_spans(analysis, points),
)
})
.collect();
for (i, &(w, view)) in items.iter().enumerate() {
let (Some(view), Some(side), Shape::Linear { a, b, .. }) = (view, sides[i], &w.shape)
else {
continue;
};
if owner[i].is_some()
|| !local
.get(view)
.is_some_and(|spans| w.measurements.iter().any(|m| spans.contains(&m.feature)))
{
continue;
}
let frame = &frames[view];
let (a, b) = (frame.page(*a), frame.page(*b));
let across = usize::from(matches!(side, Side::Above | Side::Below));
let cost = |side| {
let edge = lane_at(frame.outline(), side, 0).0;
(a[across] - edge).abs().max((b[across] - edge).abs())
};
let other = opposite(side);
if cost(other) + 1e-6 < cost(side) {
let own = strips
.get_mut(&(view, side))
.expect("a standalone span has a strip");
let index = own
.iter()
.position(|row| row.3 == i)
.expect("its strip entry");
let row = own.remove(index);
strips.entry((view, other)).or_default().push(row);
sides[i] = Some(other);
}
}
let mut lane = vec![0usize; n];
for ((view, _), list) in &mut strips {
list.sort_by_key(|&(outer, order, length, i)| {
let w = items[i].0;
let source = w.id.starts_with("d:pmi:");
let station = if source
&& let Shape::Linear { a, b, axis, .. } = w.shape
&& let Some(horizontal) = across(&frames[view], axis)
{
let along = usize::from(!horizontal);
key(frames[view].page(a)[along].min(frames[view].page(b)[along]))
} else {
0
};
(outer, !constrained[i], order, length, source, station, i)
});
let links: Vec<_> = list.iter().map(|(_, _, _, i)| items[*i].0).collect();
for ((_, _, _, i), n) in list.iter().zip(chain_lanes(&frames[view], &links)) {
lane[*i] = n;
}
}
share_lanes(owner, &mut sides, &mut lane);
(sides, lane)
}
fn chain_lanes(frame: &Frame, items: &[&Want]) -> Vec<usize> {
let spans: Vec<_> = items
.iter()
.map(|w| {
let Shape::Linear {
a, b, axis, order, ..
} = w.shape
else {
return None;
};
let horizontal = across(frame, axis)?;
let along = usize::from(!horizontal);
let length = (frame.page(b)[along] - frame.page(a)[along]).abs();
let ink = label_ink(&w.terms, font());
(length >= ink.max[0] - ink.min[0] + 2.0 * ARROW + 2.0).then_some((
axis,
order,
a[axis].min(b[axis]),
a[axis].max(b[axis]),
))
})
.collect();
let mut groups: Vec<Vec<usize>> = Vec::new();
for i in 0..items.len() {
let mut group = vec![i];
let mut j = 0;
while j < groups.len() {
let compatible = |&a: &usize, &b: &usize| match (spans[a], spans[b]) {
(Some((ka, oa, la, ha)), Some((kb, ob, lb, hb))) => {
ka == kb && oa == ob && ha.min(hb) - la.max(lb) <= 1e-6
}
_ => false,
};
let touches = |&a: &usize, &b: &usize| match (spans[a], spans[b]) {
(Some((_, _, la, ha)), Some((_, _, lb, hb))) => {
(ha - lb).abs() < 1e-6 || (hb - la).abs() < 1e-6
}
_ => false,
};
if group
.iter()
.all(|a| groups[j].iter().all(|b| compatible(a, b)))
&& group
.iter()
.any(|a| groups[j].iter().any(|b| touches(a, b)))
{
group.extend(groups.remove(j));
j = 0;
} else {
j += 1;
}
}
groups.push(group);
}
groups.sort_by_key(|g| *g.iter().min().unwrap());
let mut lanes = vec![0; items.len()];
for (lane, group) in groups.iter().enumerate() {
for &i in group {
lanes[i] = lane;
}
}
lanes
}
pub(super) fn share_lanes(
owner: &[Option<usize>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
) {
for (i, s) in owner.iter().enumerate() {
if let Some(s) = *s {
if let (Some(a), Some(b)) = (sides[i], sides[s])
&& matches!(a, Side::Above | Side::Below) == matches!(b, Side::Above | Side::Below)
{
sides[i] = Some(b);
}
lanes[i] = lanes[s];
}
}
}
pub(super) fn separate(
owner: &[Option<usize>],
drawn: &[bool],
views_of: &[Option<&'static str>],
sides: &[Option<Side>],
lanes: &mut [usize],
) -> Vec<bool> {
let strip = |i: usize| views_of[i].zip(sides[i]);
let mut next: BTreeMap<(&str, Side), usize> = BTreeMap::new();
for (i, lane) in lanes.iter().enumerate() {
if let Some(key) = strip(i) {
let n = next.entry(key).or_default();
*n = (*n).max(lane + 1);
}
}
let mut claimed = std::collections::BTreeSet::new();
for (i, lane) in lanes.iter().enumerate() {
if let (Some(key), None, true) = (strip(i), owner[i], drawn[i]) {
claimed.insert((key, *lane));
}
}
let mut moved = vec![false; lanes.len()];
for i in 0..lanes.len() {
let (Some(key), Some(_), true) = (strip(i), owner[i], drawn[i]) else {
continue;
};
if !claimed.insert((key, lanes[i])) {
let n = next.get_mut(&key).expect("every strip has its next lane");
lanes[i] = *n;
*n += 1;
moved[i] = true;
}
}
moved
}
pub(super) fn band_lanes<'a>(
items: impl Iterator<Item = ((&'a Option<&'static str>, Option<Side>), usize)>,
) -> Lanes {
let mut out = Lanes::default();
for ((view, side), lane) in items {
let (Some(view), Some(side)) = (view, side) else {
continue;
};
let band = match (*view, side) {
("front", Side::Below) => &mut out.front_below,
("front", Side::Right) => &mut out.front_right,
("plan", Side::Above) => &mut out.plan_above,
("plan", Side::Left) => &mut out.plan_left,
("front", Side::Left) => &mut out.plate_front_left,
("front", Side::Above) => &mut out.plate_front_above,
("plan", Side::Right) => &mut out.plate_plan_right,
("plan", Side::Below) => &mut out.plate_plan_below,
("side", Side::Left) => &mut out.plate_side_left_lanes,
("side", Side::Below) => &mut out.side_below,
("side", Side::Above) => &mut out.side_above,
("side", Side::Right) => &mut out.side_right,
other => unreachable!("no band holds {other:?}"),
};
*band = (*band).max(lane + 1);
}
out
}
fn opposite(side: Side) -> Side {
match side {
Side::Below => Side::Above,
Side::Above => Side::Below,
Side::Left => Side::Right,
Side::Right => Side::Left,
}
}
#[allow(clippy::too_many_arguments)]
pub(super) fn fall_through(
frames: &BTreeMap<&str, Frame>,
items: &[Want],
views_of: &[Option<&'static str>],
owner: &[Option<usize>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
analysis: &Analysis,
sheet: &PlannedSheet,
views: &[PlannedView],
) {
let page = Page::new(analysis, sheet, views);
let linear = |i: usize| matches!(items[i].shape, Shape::Linear { .. } | Shape::Aligned { .. });
let mut strips: BTreeMap<(&str, Side), Vec<usize>> = BTreeMap::new();
for i in 0..items.len() {
if owner[i].is_some() {
continue;
}
if let (Some(view), Some(side), true) = (views_of[i], sides[i], linear(i)) {
strips.entry((view, side)).or_default().push(i);
}
}
let fits = |i: usize, side: Side, lane: usize| {
let view = views_of[i].expect("a strip's item has a view");
place(&frames[view], &items[i], side, lane).is_some_and(|p| page.fits(&p, view))
};
let mut overflow: Vec<((&str, Side), Vec<usize>)> = Vec::new();
let mut held: BTreeMap<(&str, Side), usize> = BTreeMap::new();
let reach = |i: usize, side: Side, lane: usize| {
let view = views_of[i]?;
let p = place(&frames[view], &items[i], side, lane)?;
let k = usize::from(p.rotation != 0.0);
let tips = p.arrows.iter().map(|a| a[0][k]);
let lo = tips.clone().fold(p.label_box[k], f64::min);
let hi = tips.fold(p.label_box[k + 2], f64::max);
Some((lo - 1.0, hi + 1.0))
};
for (key, list) in &mut strips {
list.sort_by_key(|&i| lanes[i]);
if list.iter().any(|&i| !fits(i, key.1, lanes[i])) {
let spans: Vec<_> = list.iter().map(|&i| reach(i, key.1, lanes[i])).collect();
let mut packed = vec![0usize; list.len()];
for n in 0..list.len() {
packed[n] = (0..n)
.filter(|&m| match (spans[m], spans[n]) {
(Some((lo, hi)), Some((a, b))) => hi > a && b > lo,
_ => true,
})
.map(|m| packed[m] + 1)
.max()
.unwrap_or(0);
}
if list
.iter()
.zip(&packed)
.all(|(&i, &lane)| fits(i, key.1, lane))
{
for (&i, &lane) in list.iter().zip(&packed) {
lanes[i] = lane;
}
}
}
list.sort_by_key(|&i| lanes[i]);
let cut = list
.iter()
.position(|&i| !fits(i, key.1, lanes[i]))
.unwrap_or(list.len());
let out: Vec<usize> = list[cut..].to_vec();
let lanes_held = list[..cut].iter().map(|&i| lanes[i] + 1).max();
held.insert(*key, lanes_held.unwrap_or(0));
if !out.is_empty() {
overflow.push((*key, out));
}
}
let full: Vec<(&str, Side)> = overflow.iter().map(|(key, _)| *key).collect();
let mut stranded: Vec<usize> = Vec::new();
for ((view, side), list) in overflow {
let across = opposite(side);
if full.contains(&(view, across)) {
stranded.extend(list);
continue;
}
let first = *held.get(&(view, across)).unwrap_or(&0);
let datum = |i: usize| match items[i].shape {
Shape::Linear {
a,
axis,
a_edge: true,
..
} => Some((axis, key(a[axis]))),
_ => None,
};
let mut kept: Vec<usize> = (0..items.len())
.filter(|&i| {
owner[i].is_none()
&& linear(i)
&& views_of[i] == Some(view)
&& sides[i] == Some(side)
&& !list.contains(&i)
})
.collect();
kept.sort_by_key(|&i| lanes[i]);
let mut list = list;
if kept.len() == 1
&& kept
.iter()
.all(|&i| datum(i).is_some() && list.iter().any(|&j| datum(j) == datum(i)))
&& list
.iter()
.filter(|&&j| datum(j).is_some() && kept.iter().any(|&i| datum(i) == datum(j)))
.count()
> kept.len()
&& kept
.iter()
.chain(&list)
.enumerate()
.all(|(k, &i)| fits(i, across, first + k))
&& {
let i = kept[0];
let footprints = super::footprints::Footprints::new(analysis, &frames[view], view);
let crossed = |side: Side, lane: usize| {
place(&frames[view], &items[i], side, lane).map(|p| {
p.lines
.iter()
.take(2)
.map(|s| {
footprints
.crossed_extension(*s, &items[i].measurements)
.len()
})
.sum::<usize>()
})
};
matches!(
(crossed(side, lanes[i]), crossed(across, first)),
(Some(home), Some(there)) if there < home
)
}
{
kept.extend(list);
list = kept;
held.insert((view, side), 0);
}
let extent = |i: usize| crate::dims::is_envelope_extent(&items[i].id);
let (mut order, rest): (Vec<usize>, Vec<usize>) =
list.into_iter().partition(|&i| extent(i));
order.extend(rest);
let mut moved = Vec::new();
for &i in &order {
if fits(i, across, first + moved.len()) {
moved.push(i);
}
}
let mut outer: Vec<usize> = moved.iter().copied().filter(|&i| !extent(i)).collect();
outer.extend(moved.iter().copied().filter(|&i| extent(i)));
if outer != moved
&& outer
.iter()
.enumerate()
.all(|(k, &i)| fits(i, across, first + k))
{
moved = outer;
}
for (k, &i) in moved.iter().enumerate() {
sides[i] = Some(across);
lanes[i] = first + k;
}
held.insert((view, across), first + moved.len());
stranded.extend(order.into_iter().filter(|i| !moved.contains(i)));
}
share_held(
frames, items, views_of, owner, sides, lanes, &held, &stranded, &fits,
);
if automatic_slot_layout(analysis)
|| items.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
})
{
for i in 0..items.len() {
let Some(feature) = items[i].id.strip_suffix("/location_slot.length") else {
continue;
};
if owner[i].is_some() {
continue;
}
let length = format!("{feature}/slot_length.length");
let Some(mut j) = items.iter().position(|w| w.id == length) else {
continue;
};
if let Some(survivor) = owner[j] {
j = survivor;
}
let (Some(view), Some(side)) = (views_of[j], sides[j]) else {
continue;
};
if views_of[i] != Some(view) {
continue;
}
let (Some(p), Some(q)) = (
place(&frames[view], &items[i], side, lanes[j]),
place(&frames[view], &items[j], side, lanes[j]),
) else {
continue;
};
if !page.fits(&p, view) {
continue;
}
let k = usize::from(matches!(side, Side::Left | Side::Right));
let ends = |p: &Placed| {
let (a, b) = (p.arrows[0][0][k], p.arrows[1][0][k]);
(a.min(b), a.max(b))
};
let ((a, b), (c, d)) = (ends(&p), ends(&q));
if (b - c).abs().min((d - a).abs()) > 1e-6 {
continue;
}
let span = |p: &Placed| {
ink_points(p).fold([f64::INFINITY, f64::NEG_INFINITY], |e, p| {
[e[0].min(p[k]), e[1].max(p[k])]
})
};
let e = span(&p);
let clear = (0..items.len())
.filter(|&t| {
t != i
&& owner[t].is_none()
&& views_of[t] == Some(view)
&& sides[t] == Some(side)
&& lanes[t] == lanes[j]
})
.filter_map(|t| place(&frames[view], &items[t], side, lanes[t]))
.all(|q| {
let f = span(&q);
e[1] <= f[0] + 1e-6 || f[1] <= e[0] + 1e-6
});
if clear {
sides[i] = Some(side);
lanes[i] = lanes[j];
}
}
}
share_lanes(owner, sides, lanes);
}
#[allow(clippy::too_many_arguments)]
fn share_held(
frames: &BTreeMap<&str, Frame>,
items: &[Want],
views_of: &[Option<&'static str>],
owner: &[Option<usize>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
held: &BTreeMap<(&str, Side), usize>,
stranded: &[usize],
fits: &dyn Fn(usize, Side, usize) -> bool,
) {
if stranded.is_empty() {
return;
}
let along = |i: usize, side: Side, lane: usize| {
let view = views_of[i].expect("a strip's item has a view");
let k = usize::from(matches!(side, Side::Left | Side::Right));
place(&frames[view], &items[i], side, lane).map(|p| {
ink_points(&p).fold([f64::INFINITY, f64::NEG_INFINITY], |e, q| {
[e[0].min(q[k]), e[1].max(q[k])]
})
})
};
let mut taken: BTreeMap<(&str, Side, usize), Vec<[f64; 2]>> = BTreeMap::new();
for j in 0..items.len() {
if let (Some(view), Some(side), None, Shape::Linear { .. } | Shape::Aligned { .. }) =
(views_of[j], sides[j], owner[j], &items[j].shape)
&& lanes[j] < *held.get(&(view, side)).unwrap_or(&0)
&& !stranded.contains(&j)
&& let Some(e) = along(j, side, lanes[j])
{
taken.entry((view, side, lanes[j])).or_default().push(e);
}
}
let mut places: Vec<(usize, Vec<Shared>)> = stranded
.iter()
.map(|&i| {
let view = views_of[i].expect("a strip's item has a view");
let own = sides[i].expect("a strip's item has a side");
let list = [own, opposite(own)]
.into_iter()
.flat_map(|side| {
(0..*held.get(&(view, side)).unwrap_or(&0)).map(move |lane| (side, lane))
})
.filter(|&(side, lane)| fits(i, side, lane))
.filter_map(|(side, lane)| along(i, side, lane).map(|e| (side, lane, e)))
.collect();
(i, list)
})
.collect();
let clear = |taken: &BTreeMap<(&str, Side, usize), Vec<[f64; 2]>>,
view: &str,
(side, lane, e): Shared| {
taken.get(&(view, side, lane)).is_none_or(|list| {
list.iter()
.all(|o| e[1] <= o[0] + 1e-6 || o[1] <= e[0] + 1e-6)
})
};
loop {
let best = places
.iter()
.enumerate()
.filter_map(|(k, (i, list))| {
let view = views_of[*i].expect("a strip's item has a view");
let mut open = list.iter().filter(|&&p| clear(&taken, view, p));
let first = open.next()?;
let n = 1 + open.count();
Some((n, -key(first.2[1] - first.2[0]), k))
})
.min();
let Some((.., k)) = best else { break };
let (i, list) = places.remove(k);
let view = views_of[i].expect("a strip's item has a view");
let (side, lane, e) = *list
.iter()
.find(|&&p| clear(&taken, view, p))
.expect("a clear place");
sides[i] = Some(side);
lanes[i] = lane;
taken.entry((view, side, lane)).or_default().push(e);
}
}
pub(super) fn body_chains(
analysis: &Analysis,
frames: &BTreeMap<&str, Frame>,
items: &[Want],
carrying: &[Option<&str>],
fixed: &[bool],
placed: &mut [Option<Placed>],
page: &Page,
) -> Vec<Problem> {
let fr = crate::bridge::holes::frame(analysis);
let bands: Vec<_> = analysis
.features
.iter()
.filter(|f| f.kind == "turned_step")
.filter_map(|f| {
crate::lint::turned::band(&fr, f)
.ok()
.map(|b| (f.id.as_str(), b))
})
.collect();
let same = |a: &crate::lint::turned::Band, b: &crate::lint::turned::Band| {
a.axis == b.axis && (0..3).all(|k| k == a.axis || (a.on[k] - b.on[k]).abs() <= 1e-3)
};
let mut groups: Vec<Vec<usize>> = Vec::new();
for (i, w) in items.iter().enumerate() {
if w.measurements.is_empty() || !w.measurements.iter().all(|m| m.parameter == "step.length")
{
continue;
}
let Some((_, b)) = bands
.iter()
.find(|(id, _)| *id == w.measurements[0].feature)
else {
continue;
};
if !bands.iter().any(|(_, c)| c.axis == b.axis && !same(b, c)) {
continue;
}
if let Some(group) = groups.iter_mut().find(|g| {
let feature = &items[g[0]].measurements[0].feature;
bands
.iter()
.find(|(id, _)| *id == feature)
.is_some_and(|(_, c)| same(b, c))
}) {
group.push(i);
} else {
groups.push(vec![i]);
}
}
let mut problems = Vec::new();
for group in groups {
let first = group[0];
let Some(view) = carrying[first] else {
continue;
};
let f = &frames[view];
let (_, b) = bands
.iter()
.find(|(id, _)| *id == items[first].measurements[0].feature)
.expect("read band");
let k = b.axis;
let Some(horizontal) = across(f, k) else {
continue;
};
let j = (0..3)
.find(|&j| j != k && across(f, j) == Some(!horizontal))
.expect("a profile view");
let others: Vec<_> = bands
.iter()
.filter(|(_, c)| c.axis == k && !same(b, c))
.collect();
if others
.iter()
.any(|(_, c)| (c.on[j] - b.on[j]).abs() <= 1e-3 && b.lo <= c.hi && c.lo <= b.hi)
{
let alternate = super::envelope::profile_views(k)
.into_iter()
.find(|v| *v != view)
.unwrap_or(view);
if frames.contains_key(alternate) {
continue;
}
problems.push(Problem{id:format!("p:turned_profile_superimposed:{}",items[first].id),code:"turned_profile_superimposed".into(),severity:"warning".into(),
message:format!("The turned profile on axis {} at {:?} is superimposed on another profile in the {view} view. Restore the {alternate} view to separate its chain.", ["X","Y","Z"][k],b.on),
subjects:group.iter().map(|&i|items[i].id.clone()).collect(),
fix:vec![crate::items::Op::set(format!("views/{alternate}/omit"),serde_json::json!(false))],..Default::default()});
continue;
}
if group.iter().any(|&i| fixed[i]) {
continue;
}
let radius = bands
.iter()
.filter(|(_, c)| same(b, c))
.map(|(_, c)| c.diameter / 2.)
.fold(0.0f64, f64::max);
let centre = f.page(b.on);
let cross_axis = usize::from(horizontal);
let screen_scale = {
let mut p = b.on;
p[j] += 1.;
f.page(p)[cross_axis] - centre[cross_axis]
};
let radial = radius * f.s;
let preferred = if centre[cross_axis] - f.outline[cross_axis]
<= f.outline[cross_axis + 2] - centre[cross_axis]
{
-1.
} else {
1.
};
let mut best = None;
let mut assigned = false;
for sign in [preferred, -preferred] {
let boundary = others
.iter()
.filter_map(|(_, c)| {
let q = f.page(c.on)[cross_axis];
((q - centre[cross_axis]) * sign > 0.)
.then_some(q - sign * c.diameter * 0.5 * f.s)
})
.min_by(|a, b| {
((a - centre[cross_axis]) * sign).total_cmp(&((b - centre[cross_axis]) * sign))
});
let side = match (horizontal, sign < 0.) {
(true, true) => Side::Below,
(true, false) => Side::Above,
(false, true) => Side::Left,
(false, false) => Side::Right,
};
let mut candidates = Vec::new();
let mut clear = true;
let links: Vec<_> = group.iter().map(|&i| &items[i]).collect();
for (&i, lane) in group.iter().zip(chain_lanes(f, &links)) {
let mut w = items[i].clone();
if let Shape::Linear {
a,
b: tip,
a_edge,
b_edge,
..
} = &mut w.shape
{
a[j] = b.on[j] + sign * screen_scale.signum() * (a[j] - b.on[j]).abs();
tip[j] = b.on[j] + sign * screen_scale.signum() * (tip[j] - b.on[j]).abs();
*a_edge = false;
*b_edge = false;
}
let line = centre[cross_axis]
+ sign * (radial + first_line() + lane as f64 * lane_pitch());
let Some(p) = place_linear_at(f, &w.shape, &w.terms, side, (line, sign)) else {
clear = false;
continue;
};
let bounds = p.label_box;
let toward = if sign < 0. {
bounds[cross_axis]
} else {
bounds[cross_axis + 2]
};
if boundary.is_some_and(|edge| (edge - toward) * sign < 1.)
|| !page.fits_interior(&p, view)
{
clear = false;
}
candidates.push((i, p));
}
if best.is_none() {
best = Some(candidates.clone());
}
if clear {
best = Some(candidates);
assigned = true;
break;
}
}
if !assigned {
problems.push(Problem{id:format!("p:turned_chain_no_clear_lane:{}",items[first].id),code:"turned_chain_no_clear_lane".into(),severity:"warning".into(),
message:"Neither side of this body's silhouette has room for its complete step chain; the chain is retained and its contacts are reported.".into(),
subjects:group.iter().map(|&i|items[i].id.clone()).collect(),..Default::default()});
}
if let Some(candidates) = best {
for (i, p) in candidates {
placed[i] = Some(p);
}
}
}
problems
}
pub(super) fn place(frame: &Frame, w: &Want, side: Side, lane: usize) -> Option<Placed> {
match &w.shape {
Shape::Linear { .. } => {
if w.measurements
.iter()
.any(|m| m.parameter == "thickness.length")
&& let Shape::Linear {
a,
b,
axis,
a_edge: false,
b_edge: false,
..
} = &w.shape
&& let Some(horizontal) = across(frame, *axis)
&& [*a, *b]
.iter()
.any(|p| silhouette_gap(frame, *p, horizontal).0 != side)
{
return None;
}
let mut p = place_linear(frame, &w.shape, &w.terms, side, lane)?;
if w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
{
let k = usize::from(p.rotation != 0.0);
let hi = p
.arrows
.iter()
.map(|a| a[0][k])
.fold(f64::NEG_INFINITY, f64::max);
if p.label_box[k] > hi {
p.label_at[k] -= 2.0;
p.label_box[k] -= 2.0;
p.label_box[k + 2] -= 2.0;
}
}
Some(p)
}
Shape::Aligned { a, b } => place_aligned(frame, *a, *b, &w.terms, side, lane),
Shape::Callout { .. } | Shape::Angular { .. } => None,
}
}
fn place_linear(
frame: &Frame,
shape: &Shape,
terms: &[String],
side: Side,
lane: usize,
) -> Option<Placed> {
place_linear_at(
frame,
shape,
terms,
side,
lane_at(frame.outline, side, lane),
)
}
pub(super) fn interior_lanes(frame: &Frame, w: &Want, side: Side) -> Vec<Placed> {
let o = frame.outline();
let (edge, inward) = match side {
Side::Below => (o[1], 1.0),
Side::Above => (o[3], -1.0),
Side::Left => (o[0], 1.0),
Side::Right => (o[2], -1.0),
};
let mut candidates: Vec<_> = (1..=8)
.filter_map(|lane| {
let station = edge + inward * lane as f64 * lane_pitch();
place_linear_at(frame, &w.shape, &w.terms, side, (station, inward))
})
.collect();
if let Shape::Linear {
a,
b,
a_edge: false,
b_edge: false,
..
} = w.shape
{
let k = usize::from(matches!(side, Side::Above | Side::Below));
let (a, b) = (frame.page(a)[k], frame.page(b)[k]);
if (a - b).abs() < 1e-6 {
let out = if matches!(side, Side::Above | Side::Right) {
1.
} else {
-1.
};
for gap in (0..6).map(|n| super::TEXT + f64::from(n)) {
if let Some(p) =
place_linear_at(frame, &w.shape, &w.terms, side, (a + out * gap, out))
{
candidates.push(p);
}
}
}
}
candidates
}
pub(super) fn opening_lanes(frame: &Frame, w: &Want, side: Side) -> Vec<Placed> {
if !matches!(
w.shape,
Shape::Linear { a_edge: true, .. } | Shape::Linear { b_edge: true, .. }
) {
return Vec::new();
}
let o = frame.outline();
let (edge, out) = match side {
Side::Below => (o[1], -1.),
Side::Above => (o[3], 1.),
Side::Left => (o[0], -1.),
Side::Right => (o[2], 1.),
};
(0..=28)
.filter_map(|n| {
let gap = super::TEXT + super::leaders::STROKE_PAD + 0.25 * f64::from(n);
place_linear_at(frame, &w.shape, &w.terms, side, (edge + out * gap, out))
})
.collect()
}
pub(super) fn linear_pair_contact(a: &Placed, b: &Placed, allow_transverse: bool) -> bool {
boxes_overlap(a.label_box, b.label_box)
|| a.lines.iter().any(|l| crosses(*l, b.label_box))
|| b.lines.iter().any(|l| crosses(*l, a.label_box))
|| a.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.any(|l| crosses(l, b.label_box))
|| b.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.any(|l| crosses(l, a.label_box))
|| (a.arrows.len() == 2
&& a.lines.len() >= 3
&& b.arrows.len() == 1
&& !(allow_transverse)
&& super::leaders::meets_extensions(&a.lines[..2], &b.lines, b.label_box))
|| (a.arrows.len() == 2
&& a.lines.len() >= 3
&& b.arrows.len() == 1
&& a.lines[..2].iter().any(|e| {
b.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.any(|h| touches_witness(h, *e, 0.2))
}))
|| (a.arrows.len() == 2
&& b.arrows.len() == 1
&& allow_transverse
&& a.lines
.iter()
.take(2)
.any(|e| super::leaders::runs_beside(*e, &b.lines)))
|| (a.arrows.len() == 2 && b.arrows.len() == 2 && shared_dimension_line(&a.lines, &b.lines))
}
fn linear_witness_contact(a: &Placed, b: &Placed, allow_transverse: bool) -> bool {
a.lines.iter().take(2).any(|l| crosses(*l, b.label_box))
|| a.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.any(|l| crosses(l, b.label_box))
|| (a.arrows.len() == 2
&& a.lines.len() >= 3
&& b.arrows.len() == 1
&& (!(allow_transverse)
&& super::leaders::meets_extensions(&a.lines[..2], &b.lines, b.label_box)
|| a.lines[..2].iter().any(|e| {
b.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.any(|h| touches_witness(h, *e, 0.2))
})
|| allow_transverse
&& a.lines[..2]
.iter()
.any(|e| super::leaders::runs_beside(*e, &b.lines))))
|| (a.arrows.len() == 2 && b.arrows.len() == 2 && shared_dimension_line(&a.lines, &b.lines))
}
struct WitnessFrame {
from: P2,
delta: P2,
length: f64,
}
impl WitnessFrame {
fn new(line: [P2; 2]) -> Self {
let delta = [line[1][0] - line[0][0], line[1][1] - line[0][1]];
Self {
from: line[0],
delta,
length: delta[0].hypot(delta[1]),
}
}
fn touches(&self, s: [P2; 2], pad: f64) -> bool {
let (from, d, n) = (self.from, self.delta, self.length);
if n < 1e-12 {
return super::leaders::touches_box(
s,
[from[0] - pad, from[1] - pad, from[0] + pad, from[1] + pad],
);
}
let local = s.map(|p| {
let w = [p[0] - from[0], p[1] - from[1]];
[
(w[0] * d[0] + w[1] * d[1]) / n,
(w[1] * d[0] - w[0] * d[1]) / n,
]
});
super::leaders::touches_box(local, [-pad, -pad, n + pad, pad])
}
}
struct WitnessStroke<'a> {
placed: &'a Placed,
heads: [[[P2; 2]; 3]; 2],
frames: std::cell::OnceCell<[WitnessFrame; 2]>,
}
impl<'a> WitnessStroke<'a> {
fn new(placed: &'a Placed) -> Option<Self> {
(placed.arrows.len() == 2 && placed.lines.len() >= 3).then(|| Self {
placed,
heads: [
super::leaders::head(placed.arrows[0]),
super::leaders::head(placed.arrows[1]),
],
frames: std::cell::OnceCell::new(),
})
}
fn contact(&self, peer: &WitnessPeer, transverse: bool) -> bool {
let (a, b) = (self.placed, peer.placed);
a.lines
.iter()
.take(2)
.any(|line| crosses(*line, b.label_box))
|| self
.heads
.iter()
.flatten()
.any(|line| crosses(*line, b.label_box))
|| peer.head.as_ref().is_some_and(|head| {
(!transverse
&& super::leaders::meets_extensions(&a.lines[..2], &b.lines, b.label_box))
|| self
.frames
.get_or_init(|| {
[WitnessFrame::new(a.lines[0]), WitnessFrame::new(a.lines[1])]
})
.iter()
.any(|frame| head.iter().any(|h| frame.touches(*h, 0.2)))
|| (transverse
&& a.lines[..2]
.iter()
.any(|e| super::leaders::runs_beside(*e, &b.lines)))
})
|| (b.arrows.len() == 2 && shared_dimension_line(&a.lines, &b.lines))
}
}
struct WitnessPeer<'a> {
placed: &'a Placed,
bounds: [f64; 4],
head: Option<[[P2; 2]; 3]>,
}
struct WitnessPeers<'a>(Vec<WitnessPeer<'a>>);
impl<'a> WitnessPeers<'a> {
fn new(peers: impl Iterator<Item = &'a Placed>) -> Self {
Self(
peers
.map(|p| {
let b = ink_points(p).fold(
[
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
],
|b, q| {
[
b[0].min(q[0]),
b[1].min(q[1]),
b[2].max(q[0]),
b[3].max(q[1]),
]
},
);
WitnessPeer {
placed: p,
bounds: b,
head: (p.arrows.len() == 1).then(|| super::leaders::head(p.arrows[0])),
}
})
.collect(),
)
}
fn contact(&self, p: &Placed, transverse: impl Fn(&Placed) -> bool) -> bool {
let b = p.lines.iter().chain(&p.arrows).flatten().fold(
[
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
],
|b, q| {
[
b[0].min(q[0]),
b[1].min(q[1]),
b[2].max(q[0]),
b[3].max(q[1]),
]
},
);
let pad = 2.0 + ARROW_HALF;
let b = [b[0] - pad, b[1] - pad, b[2] + pad, b[3] + pad];
let prepared = std::cell::OnceCell::new();
self.0.iter().any(|peer| {
boxes_overlap(b, peer.bounds)
&& prepared
.get_or_init(|| WitnessStroke::new(p))
.as_ref()
.map_or_else(
|| linear_witness_contact(p, peer.placed, transverse(peer.placed)),
|a| a.contact(peer, transverse(peer.placed)),
)
})
}
}
struct PeerInk {
labels: Vec<[f64; 4]>,
lines: super::leaders::SegmentIndex,
}
impl PeerInk {
fn new<'a>(peers: impl Iterator<Item = &'a Placed>) -> Self {
let peers: Vec<_> = peers.collect();
let labels = peers.iter().map(|q| q.label_box).collect();
let lines = super::leaders::SegmentIndex::new(
peers
.iter()
.flat_map(|q| {
q.lines
.iter()
.copied()
.chain(q.arrows.iter().flat_map(|a| super::leaders::head(*a)))
})
.collect(),
);
Self { labels, lines }
}
fn mutable_contact(&self, p: &Placed) -> bool {
self.labels
.iter()
.any(|label| boxes_overlap(p.label_box, *label) || crosses(p.lines[2], *label))
|| self.lines.crosses_box(p.label_box)
}
}
fn linear_label_bounds(terms: &[String]) -> (P2, P2) {
thread_local! {
static BOUNDS: std::cell::RefCell<BTreeMap<Vec<String>, (P2, P2)>> = const { std::cell::RefCell::new(BTreeMap::new()) };
}
BOUNDS.with(|bounds| {
if let Some(&value) = bounds.borrow().get(terms) {
return value;
}
let ink = label_ink(terms, font());
let value = (ink.min, ink.max);
let mut bounds = bounds.borrow_mut();
if bounds.len() >= 256 {
bounds.clear();
}
bounds.insert(terms.to_vec(), value);
value
})
}
pub(super) fn pattern_references(w: &Want, analysis: &Analysis) -> Vec<Shape> {
let [m] = w.measurements.as_slice() else {
return Vec::new();
};
if !m.parameter.starts_with("grid_pitch.length.") {
return Vec::new();
}
let Shape::Linear {
a,
b,
axis,
a_edge: false,
b_edge: false,
order,
} = w.shape
else {
return Vec::new();
};
let Some(pattern) = analysis
.recognition
.as_ref()
.and_then(|r| r.patterns.iter().find(|p| p.id == m.feature))
else {
return Vec::new();
};
let frame = crate::bridge::holes::frame(analysis);
let points: Vec<_> = pattern
.members
.iter()
.filter_map(|id| {
let f = analysis.features.iter().find(|f| &f.id == id)?;
crate::bridge::holes::Hole::read(f, &frame)
.ok()
.map(|h| h.at)
})
.collect();
let mut pairs: Vec<(P3, P3)> = Vec::new();
for p in points.iter().filter(|p| (p[axis] - a[axis]).abs() < 1e-6) {
for q in points.iter().filter(|q| {
(q[axis] - b[axis]).abs() < 1e-6
&& (0..3)
.filter(|k| *k != axis)
.all(|k| (p[k] - q[k]).abs() < 1e-6)
}) {
if !pairs.contains(&(*p, *q)) {
pairs.push((*p, *q));
}
}
}
pairs.sort_by(|(a, _), (b, _)| {
(0..3)
.map(|k| a[k].total_cmp(&b[k]))
.find(|o| !o.is_eq())
.unwrap_or(std::cmp::Ordering::Equal)
});
if pairs.len() > 2 {
pairs = vec![pairs[0], pairs[pairs.len() - 1]];
}
pairs
.into_iter()
.map(|(a, b)| Shape::Linear {
a,
b,
axis,
order,
a_edge: false,
b_edge: false,
})
.collect()
}
pub(super) fn nearest_pattern_witnesses(
analysis: &Analysis,
frame: &Frame,
measurements: &[super::Measurement],
p: &mut Placed,
) {
let Some(m) = measurements.first() else {
return;
};
if !m.parameter.starts_with("grid_pitch.length.")
|| !measurements.iter().all(|x| x == m)
|| p.lines.len() < 3
|| p.arrows.len() != 2
{
return;
}
let Some(pattern) = analysis
.recognition
.as_ref()
.and_then(|r| r.patterns.iter().find(|r| r.id == m.feature))
else {
return;
};
let qframe = crate::bridge::holes::frame(analysis);
let points: Vec<_> = pattern
.members
.iter()
.filter_map(|id| {
let f = analysis.features.iter().find(|f| &f.id == id)?;
crate::bridge::holes::Hole::read(f, &qframe)
.ok()
.map(|h| frame.page(h.at))
})
.collect();
let normal = usize::from(p.rotation.abs() < 1e-9);
let station = 1 - normal;
let line = p.arrows[0][0][normal];
let old = [p.lines[0][0], p.lines[1][0]];
let sign = (p.lines[0][1][normal] - old[0][normal]).signum();
let mut best = old;
let mut length = (old[0][normal] - line).abs() + (old[1][normal] - line).abs();
for a in points
.iter()
.filter(|a| (a[station] - old[0][station]).abs() < 1e-6)
{
for b in points.iter().filter(|b| {
(b[station] - old[1][station]).abs() < 1e-6 && (a[normal] - b[normal]).abs() < 1e-6
}) {
let mut ends = [*a, *b];
for e in &mut ends {
e[normal] += sign * GAP;
}
if (0..2).any(|i| {
ends[i][normal] < old[i][normal].min(line) - 1e-6
|| ends[i][normal] > old[i][normal].max(line) + 1e-6
}) {
continue;
}
let candidate = (ends[0][normal] - line).abs() + (ends[1][normal] - line).abs();
if candidate + 1e-6 < length {
best = ends;
length = candidate;
}
}
}
p.lines[0][0] = best[0];
p.lines[1][0] = best[1];
}
pub(crate) fn nearest_shared_witnesses(
analysis: &crate::model::Analysis,
spec: &crate::model::Spec,
views: &[crate::model::PlannedView],
dimensions: &mut [super::PlannedDimension],
) {
if !crate::nominal::geometry_eligible(analysis) {
return;
}
let mut wants = None;
for d in dimensions.iter_mut().filter(|d| d.kind == "linear") {
if d.measurements.is_empty()
|| spec
.dimensions
.get(&d.id)
.is_some_and(|s| s.place.is_some())
{
continue;
}
let first = &d.measurements[0];
if !d
.measurements
.iter()
.all(|m| m.parameter == first.parameter && (m.value - first.value).abs() < 1e-6)
{
continue;
}
let (Some(v), Some(p)) = (views.iter().find(|v| v.id == d.view), d.placed.as_mut()) else {
continue;
};
if v.detail.is_some() || v.cut.is_some() || p.lines.len() < 3 || p.arrows.len() != 2 {
continue;
}
let normal = if p.rotation.abs() < 1e-9 {
1
} else if (p.rotation - 90.).abs() < 1e-9 {
0
} else {
continue;
};
let station = 1 - normal;
if p.lines[..2]
.iter()
.any(|s| (s[0][station] - s[1][station]).abs() > 1e-9)
{
continue;
}
let frame = Frame::new(analysis, v);
if automatic_slot_layout(analysis) {
nearest_pattern_witnesses(analysis, &frame, &d.measurements, p);
}
let line = p.arrows[0][0][normal];
let sign = (p.lines[0][1][normal] - p.lines[0][0][normal]).signum();
if let Some(axis) = first
.parameter
.strip_prefix("location.length.")
.and_then(|name| super::AXES.iter().position(|a| *a == name))
&& let Some(pattern) = analysis.recognition.as_ref().and_then(|r| {
r.patterns
.iter()
.find(|row| row.id == first.feature && row.arrangement == "linear")
})
{
let qframe = crate::bridge::holes::frame(analysis);
let points: Option<Vec<_>> = pattern
.members
.iter()
.map(|id| {
let feature = analysis.features.iter().find(|f| &f.id == id)?;
crate::bridge::holes::Hole::read(feature, &qframe)
.ok()
.map(|h| h.at)
})
.collect();
if let Some(points) = points
&& points.len() > 1
&& points
.iter()
.all(|at| (at[axis] - points[0][axis]).abs() < 1e-6)
{
for at in points {
let at = frame.page(at);
for i in 0..2 {
if (at[station] - p.lines[i][0][station]).abs() > 1e-6 {
continue;
}
let start = p.lines[i][0][normal];
let candidate = at[normal] + sign * GAP;
if candidate >= start.min(line) - 1e-6
&& candidate <= start.max(line) + 1e-6
&& (candidate - line).abs() + 1e-6 < (start - line).abs()
{
p.lines[i][0][normal] = candidate;
}
}
}
}
}
if d.measurements.len() < 2 {
continue;
}
let old = [p.lines[0][0], p.lines[1][0]];
let mut best = old;
let mut shortest = (old[0][normal] - line).abs() + (old[1][normal] - line).abs();
for w in wants.get_or_insert_with(|| super::wants(analysis).0).iter() {
if !w.measurements.iter().any(|m| d.measurements.contains(m)) {
continue;
}
let Shape::Linear {
a,
b,
a_edge: false,
b_edge: false,
..
} = w.shape
else {
continue;
};
let mut ends = [frame.page(a), frame.page(b)];
if (ends[0][station] - old[1][station]).abs() < 1e-6
&& (ends[1][station] - old[0][station]).abs() < 1e-6
{
ends.swap(0, 1);
}
if (0..2).any(|i| (ends[i][station] - old[i][station]).abs() > 1e-6) {
continue;
}
for end in &mut ends {
end[normal] += sign * GAP;
}
if (0..2).any(|i| {
let x = ends[i][normal];
x < old[i][normal].min(line) - 1e-6 || x > old[i][normal].max(line) + 1e-6
}) {
continue;
}
let length = (ends[0][normal] - line).abs() + (ends[1][normal] - line).abs();
if length + 1e-6 < shortest {
shortest = length;
best = ends;
}
}
p.lines[0][0] = best[0];
p.lines[1][0] = best[1];
}
}
fn place_linear_at(
frame: &Frame,
shape: &Shape,
terms: &[String],
side: Side,
position: (f64, f64),
) -> Option<Placed> {
let Shape::Linear {
a,
b,
a_edge,
b_edge,
..
} = shape
else {
return None;
};
let horizontal = matches!(side, Side::Below | Side::Above);
let (min, max) = linear_label_bounds(terms);
let ink = LabelInk {
min,
max,
glyphs: Vec::new(),
strokes: Vec::new(),
};
let w = ink.max[0] - ink.min[0];
let o = frame.outline;
let (pa, pb) = (frame.page(*a), frame.page(*b));
let (t_line, out) = position;
let k = usize::from(horizontal); let map = |s: f64, t: f64| if horizontal { [s, t] } else { [t, s] };
let edge = match side {
Side::Below => o[1],
Side::Above => o[3],
Side::Left => o[0],
Side::Right => o[2],
};
let mut lines = Vec::new();
let (s0, s1) = (pa[1 - k], pb[1 - k]);
for (p, s, on_edge) in [(pa, s0, *a_edge), (pb, s1, *b_edge)] {
let from = if on_edge { edge } else { p[k] };
lines.push([map(s, from + out * GAP), map(s, t_line + out * OVERSHOOT)]);
}
let (lo, hi) = (s0.min(s1), s0.max(s1));
let mut arrows = Vec::new();
let mid = if hi - lo >= w + 2.0 * ARROW + 2.0 {
lines.push([map(lo, t_line), map(hi, t_line)]);
arrows.push(arrow(map(lo, t_line), map(lo + 1.0, t_line)));
arrows.push(arrow(map(hi, t_line), map(hi - 1.0, t_line)));
0.5 * (lo + hi) - 0.5 * w - ink.min[0]
} else if hi-lo>=w+2.0 && frame.inspection.is_some_and(|(k,datum)| matches!(shape,Shape::Linear {a,b,axis,..} if *axis==k && ((a[k]-datum).abs()<1e-6 || (b[k]-datum).abs()<1e-6))) {
lines.push([map(lo,t_line),map(hi,t_line)]);
arrows.push(arrow(map(lo,t_line),map(lo-1.0,t_line)));
arrows.push(arrow(map(hi,t_line),map(hi+1.0,t_line)));
0.5*(lo+hi)-0.5*w-ink.min[0]
} else {
lines.push([map(lo - ARROW - 2.0, t_line), map(hi + ARROW + 2.0, t_line)]);
arrows.push(arrow(map(lo, t_line), map(lo - 1.0, t_line)));
arrows.push(arrow(map(hi, t_line), map(hi + 1.0, t_line)));
hi + ARROW + 3.0 - ink.min[0]
};
let (label_at, rotation) = if horizontal {
([mid, t_line + LIFT], 0.0)
} else {
([t_line - LIFT, mid], 90.0)
};
Some(Placed {
lines,
arrows,
label_at,
rotation,
label_box: label_box(&ink, label_at, rotation),
})
}
const SLIDE_STEP: f64 = 0.5;
const SLIDE_CLEAR: f64 = 0.5;
const SLIDE_REACH: f64 = 20.0;
pub(super) fn settle(
items: &mut [Option<Placed>],
analysis: &Analysis,
sheet: &PlannedSheet,
views: &[PlannedView],
) {
let page = Page::new(analysis, sheet, views);
settle_where(
items,
&page,
automatic_slot_layout(analysis),
|_| true,
|i, j| j != i,
);
}
pub(super) fn settle_fixed(
items: &mut [Option<Placed>],
fixed: &[bool],
obstacles: &[PlannedDimension],
analysis: &Analysis,
sheet: &PlannedSheet,
views: &[PlannedView],
) {
if fixed.iter().all(|f| !f) && obstacles.is_empty() {
settle(items, analysis, sheet, views);
return;
}
let n = items.len();
let mut all = items.to_vec();
all.extend(obstacles.iter().map(|d| d.placed.clone()));
let page = Page::new(analysis, sheet, views);
settle_where(
&mut all,
&page,
automatic_slot_layout(analysis),
|i| i < n && !fixed[i],
|i, j| i != j,
);
items.clone_from_slice(&all[..n]);
}
#[allow(clippy::too_many_arguments)]
pub(super) fn avoid_fixed(
analysis: &Analysis,
frames: &BTreeMap<&str, Frame>,
items: &[Want],
chosen: &[Option<&str>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
fixed: &[bool],
places: &[Option<&crate::placement_intent::Place>],
drawn: &[bool],
placed: &mut [Option<Placed>],
obstacles: &[PlannedDimension],
page: &Page,
) {
let held: Vec<(Placed, bool)> = placed
.iter()
.zip(fixed)
.zip(items)
.filter_map(|((p, f), w)| {
if *f {
Some((p.clone()?, matches!(w.shape, Shape::Callout { .. })))
} else {
None
}
})
.chain(
obstacles
.iter()
.filter_map(|d| Some((d.placed.clone()?, d.kind == "callout"))),
)
.collect();
if held.is_empty() {
return;
}
let blocked = |p: &Placed| {
held.iter().any(|(q, leader)| {
boxes_overlap(p.label_box, q.label_box)
|| p.lines.iter().any(|l| crosses(*l, q.label_box))
|| q.lines.iter().any(|l| crosses(*l, p.label_box))
|| (*leader
&& q.lines.iter().any(|l| {
p.lines
.iter()
.chain(&p.arrows)
.any(|t| super::leaders::lines_meet(*l, *t))
}))
})
};
for i in 0..items.len() {
if fixed[i] {
continue;
}
let (Some(p), Some(view), Some(side)) = (&placed[i], chosen[i], sides[i]) else {
continue;
};
if !blocked(p) {
continue;
}
if let Some(request) = places[i].filter(|_| matches!(items[i].shape, Shape::Linear { .. }))
{
let mut nearer = p.clone();
nearest_pattern_witnesses(analysis, &frames[view], &items[i].measurements, &mut nearer);
if page.fits(&nearer, view) && !blocked(&nearer) {
placed[i] = Some(nearer);
continue;
}
let first = request.lane.unwrap_or(lanes[i]);
let mut preserved = None;
for lane in first..first + 64 {
let mut trial = request.clone();
trial.lane = Some(lane);
let Some(p) = intent(&frames[view], &items[i], &trial, side, lanes[i], 0.0) else {
continue;
};
if !page.fits(&p, view) || blocked(&p) {
continue;
}
if placed.iter().enumerate().any(|(j, q)| {
j != i
&& drawn[i]
&& drawn[j]
&& chosen[j] == Some(view)
&& q.as_ref().is_some_and(|q| {
dimension_lines_overlap(&p, q)
&& !dimension_lines_overlap(placed[i].as_ref().unwrap(), q)
})
}) {
continue;
}
preserved = Some((p, lane));
break;
}
if let Some((p, lane)) = preserved {
sides[i] = Some(match request.side {
Some(crate::placement_intent::Side::Above) => Side::Above,
Some(crate::placement_intent::Side::Below) => Side::Below,
Some(crate::placement_intent::Side::Left) => Side::Left,
Some(crate::placement_intent::Side::Right) => Side::Right,
None => side,
});
lanes[i] = lane;
placed[i] = Some(p);
continue;
}
}
let slid = slides(p, page.drawable).find(|p| page.fits(p, view) && !blocked(p));
if let Some(p) = slid {
placed[i] = Some(p);
continue;
}
let original = p;
let opposite = match side {
Side::Above => Side::Below,
Side::Below => Side::Above,
Side::Left => Side::Right,
Side::Right => Side::Left,
};
let candidates = (0..64)
.map(|n| (side, n))
.chain((0..64).map(|n| (opposite, n)));
for (side, lane) in candidates {
if let Some(p) = place(&frames[view], &items[i], side, lane)
&& page.fits(&p, view)
&& !blocked(&p)
&& !placed.iter().enumerate().any(|(j, q)| {
drawn[i]
&& drawn[j]
&& j != i
&& chosen[j] == Some(view)
&& matches!(items[j].shape, Shape::Linear { .. })
&& q.as_ref().is_some_and(|q| {
dimension_lines_overlap(&p, q) && !dimension_lines_overlap(original, q)
})
})
{
placed[i] = Some(p);
sides[i] = Some(side);
lanes[i] = lane;
break;
}
}
}
}
pub(super) fn slot_chain_claims(a: &[super::Measurement], b: &[super::Measurement]) -> bool {
a.iter().any(|a| {
b.iter().any(|b| {
a.feature == b.feature
&& ((a.parameter == "location_slot.length" && b.parameter == "slot_length.length")
|| (b.parameter == "location_slot.length"
&& a.parameter == "slot_length.length"))
})
})
}
pub(super) fn forms_chain(a: &Placed, b: &Placed) -> bool {
if a.arrows.len() != 2 || b.arrows.len() != 2 || a.rotation != b.rotation {
return false;
}
let k = usize::from(a.rotation != 0.0);
if (a.arrows[0][0][1 - k] - b.arrows[0][0][1 - k]).abs() > 1e-6 {
return false;
}
let ends = |p: &Placed| {
let (x, y) = (p.arrows[0][0][k], p.arrows[1][0][k]);
(x.min(y), x.max(y))
};
let ((a0, a1), (b0, b1)) = (ends(a), ends(b));
(a1 - b0).abs().min((b1 - a0).abs()) < 1e-6
}
fn touches_witness(s: [P2; 2], witness: [P2; 2], pad: f64) -> bool {
let d = [witness[1][0] - witness[0][0], witness[1][1] - witness[0][1]];
let n = d[0].hypot(d[1]);
if n < 1e-12 {
return super::leaders::touches_box(
s,
[
witness[0][0] - pad,
witness[0][1] - pad,
witness[0][0] + pad,
witness[0][1] + pad,
],
);
}
let local = s.map(|p| {
let w = [p[0] - witness[0][0], p[1] - witness[0][1]];
[
(w[0] * d[0] + w[1] * d[1]) / n,
(w[1] * d[0] - w[0] * d[1]) / n,
]
});
super::leaders::touches_box(local, [-pad, -pad, n + pad, pad])
}
#[allow(clippy::too_many_arguments)]
pub(super) fn clear_linear(
analysis: &Analysis,
cache: &super::footprints::Cache,
views: &[PlannedView],
frames: &BTreeMap<&str, Frame>,
items: &[Want],
chosen: &[Option<&str>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
placed: &mut [Option<Placed>],
obstacles: &[PlannedDimension],
locked: &[bool],
page: &Page,
yield_locations: bool,
) {
let geometry: Vec<_> = views
.iter()
.map(|v| (&v.id, v.direction, v.up, v.at, v.scale, v.cut.is_some()))
.collect();
let quantities: Vec<_> = items
.iter()
.map(|w| (&w.id, w.value, &w.shape, &w.measurements, &w.terms))
.collect();
let frame_geometry: Vec<_> = frames
.iter()
.map(|(name, f)| (name, f.memo_geometry()))
.collect();
let furniture: Vec<_> = obstacles
.iter()
.map(|d| (&d.id, &d.kind, &d.placed))
.collect();
let established_clear = crate::planning::lookup::<std::collections::BTreeSet<String>>(
analysis,
"established-clear-witnesses",
&[],
)
.unwrap_or_default();
let key = serde_json::to_vec(&(
quantities,
chosen,
&*sides,
&*lanes,
&*placed,
furniture,
locked,
page,
geometry,
frame_geometry,
yield_locations,
established_clear,
))
.expect("linear solve inputs serialize");
let (next_sides, next_lanes, next_placed) =
crate::planning::memo(analysis, "settled-linear-solve", key, || {
let mut next_sides = sides.to_vec();
let mut next_lanes = lanes.to_vec();
let mut next_placed = placed.to_vec();
clear_linear_uncached(
analysis,
cache,
views,
frames,
items,
chosen,
&mut next_sides,
&mut next_lanes,
&mut next_placed,
obstacles,
locked,
page,
yield_locations,
);
let shared = shared_lines(&next_placed);
if shared > 0 {
let mut guarded = (sides.to_vec(), lanes.to_vec(), placed.to_vec());
clear_linear_ordered(
analysis,
cache,
views,
frames,
items,
chosen,
&mut guarded.0,
&mut guarded.1,
&mut guarded.2,
obstacles,
locked,
page,
0,
yield_locations,
true,
);
if shared_lines(&guarded.2) < shared {
(next_sides, next_lanes, next_placed) = guarded;
}
}
(next_sides, next_lanes, next_placed)
});
sides.copy_from_slice(&next_sides);
lanes.copy_from_slice(&next_lanes);
placed.clone_from_slice(&next_placed);
}
fn shared_lines(placed: &[Option<Placed>]) -> usize {
let linear: Vec<_> = placed
.iter()
.flatten()
.filter(|p| p.arrows.len() == 2)
.collect();
linear
.iter()
.enumerate()
.map(|(i, p)| {
linear[i + 1..]
.iter()
.filter(|q| shared_dimension_line(&p.lines, &q.lines))
.count()
})
.sum()
}
#[allow(clippy::too_many_arguments)]
fn clear_linear_uncached(
analysis: &Analysis,
cache: &super::footprints::Cache,
views: &[PlannedView],
frames: &BTreeMap<&str, Frame>,
items: &[Want],
chosen: &[Option<&str>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
placed: &mut [Option<Placed>],
obstacles: &[PlannedDimension],
locked: &[bool],
page: &Page,
yield_locations: bool,
) {
let initial = (sides.to_vec(), lanes.to_vec(), placed.to_vec());
clear_linear_ordered(
analysis,
cache,
views,
frames,
items,
chosen,
sides,
lanes,
placed,
obstacles,
locked,
page,
0,
yield_locations,
false,
);
let recess = |w: &Want| {
!w.measurements.is_empty()
&& w.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))
};
let count = items
.iter()
.filter(|w| recess(w) && matches!(w.shape, Shape::Linear { .. }))
.count();
if count < 2
|| !locked.iter().any(|p| *p)
|| !obstacles.iter().any(|d| d.id.starts_with("pmi:"))
{
return;
}
let rank = |positions: &[Option<Placed>]| {
let other: Vec<_> = positions
.iter()
.enumerate()
.filter_map(|(i, p)| Some((i, p.as_ref()?)))
.collect();
other
.iter()
.filter(|(i, _)| recess(&items[*i]))
.map(|(i, p)| {
usize::from(!page.fits(p, chosen[*i].unwrap_or_default()))
+ other
.iter()
.filter(|(j, _)| j != i)
.map(|(_, q)| {
usize::from(boxes_overlap(p.label_box, q.label_box))
+ usize::from(p.lines.iter().any(|l| crosses(*l, q.label_box)))
+ usize::from(q.lines.iter().any(|l| crosses(*l, p.label_box)))
+ usize::from(
p.arrows.len() == 2
&& q.arrows.len() == 2
&& shared_dimension_line(&p.lines, &q.lines),
)
})
.sum::<usize>()
+ obstacles
.iter()
.filter_map(|d| d.placed.as_ref())
.map(|q| {
usize::from(boxes_overlap(p.label_box, q.label_box))
+ usize::from(p.lines.iter().any(|l| crosses(*l, q.label_box)))
+ usize::from(q.lines.iter().any(|l| crosses(*l, p.label_box)))
})
.sum::<usize>()
})
.sum::<usize>()
};
let mut score = rank(placed);
if score == 0 {
return;
}
let mut best = (sides.to_vec(), lanes.to_vec(), placed.to_vec());
for rotation in 1..count.min(6) {
sides.copy_from_slice(&initial.0);
lanes.copy_from_slice(&initial.1);
placed.clone_from_slice(&initial.2);
clear_linear_ordered(
analysis,
cache,
views,
frames,
items,
chosen,
sides,
lanes,
placed,
obstacles,
locked,
page,
rotation,
yield_locations,
false,
);
let now = rank(placed);
if now < score {
score = now;
best = (sides.to_vec(), lanes.to_vec(), placed.to_vec());
}
if score == 0 {
break;
}
}
sides.copy_from_slice(&best.0);
lanes.copy_from_slice(&best.1);
placed.clone_from_slice(&best.2);
}
#[allow(clippy::too_many_arguments)]
fn clear_linear_ordered(
analysis: &Analysis,
cache: &super::footprints::Cache,
views: &[PlannedView],
frames: &BTreeMap<&str, Frame>,
items: &[Want],
chosen: &[Option<&str>],
sides: &mut [Option<Side>],
lanes: &mut [usize],
placed: &mut [Option<Placed>],
obstacles: &[PlannedDimension],
locked: &[bool],
page: &Page,
rotation: usize,
yield_locations: bool,
guard_yielded: bool,
) {
let held = locked.iter().any(|p| *p);
let established_clear = crate::planning::lookup::<std::collections::BTreeSet<String>>(
analysis,
"established-clear-witnesses",
&[],
)
.unwrap_or_default();
let frames_ink: Vec<_> = obstacles
.iter()
.filter(|d| d.id.starts_with("pmi:") && d.kind == "callout")
.filter_map(|d| d.placed.as_ref())
.collect();
let semantic_frame = |p: &Placed| frames_ink.iter().any(|q| std::ptr::eq(*q, p));
let chained: Vec<_> = placed
.iter()
.enumerate()
.map(|(i, p)| {
p.as_ref().is_some_and(|p| {
placed.iter().enumerate().any(|(j, q)| {
i != j
&& chosen[i] == chosen[j]
&& slot_chain_claims(&items[i].measurements, &items[j].measurements)
&& q.as_ref().is_some_and(|q| forms_chain(p, q))
})
})
})
.collect();
let mut models = BTreeMap::new();
let recess = |i: usize| {
!items[i].measurements.is_empty()
&& items[i]
.measurements
.iter()
.all(|m| m.feature.starts_with("f:section_recess:"))
};
let recess_context = items.iter().any(|w| {
w.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
});
let semantic_pmi = super::leaders::has_semantic_pmi(analysis, frames);
let mut order: Vec<_> = (0..items.len()).collect();
let movable = |i: usize| {
recess(i)
&& matches!(items[i].shape, Shape::Linear { .. })
&& placed[i].is_some()
&& !locked.get(i).copied().unwrap_or(false)
};
order.sort_by_key(|&i| !movable(i));
let end = order.iter().take_while(|&&i| movable(i)).count();
if end > 0 {
order[..end].rotate_left(rotation % end);
}
let new_native = analysis
.recognition
.as_ref()
.is_some_and(|r| r.recogniser.starts_with("quiddity-rust "));
let location = |i: usize| {
new_native && recess_context && matches!(items[i].shape, Shape::Linear { order: 1, .. })
};
let old_location = |i: usize| !recess(i) && location(i);
let slots: Vec<_> = order
.iter()
.enumerate()
.filter(|(_, i)| old_location(**i))
.map(|(n, _)| n)
.collect();
let mut locations: Vec<_> = slots.iter().map(|&n| order[n]).collect();
locations.sort_by(|&a, &b| items[a].value.total_cmp(&items[b].value));
for (slot, item) in slots.into_iter().zip(locations) {
order[slot] = item;
}
let mut positions = vec![0; items.len()];
for (n, &i) in order.iter().enumerate() {
positions[i] = n;
}
let mut pending = BTreeMap::new();
if held && obstacles.iter().any(|d| d.id.starts_with("pmi:")) {
for &i in order.iter().take(end) {
if !chained[i]
&& let Some(p) = placed[i].take()
{
pending.insert(i, p);
}
}
}
if yield_locations {
for &i in order.iter().skip(end) {
if old_location(i)
&& !chained[i]
&& !locked.get(i).copied().unwrap_or(false)
&& let Some(p) = placed[i].take()
{
pending.insert(i, p);
}
}
}
for i in order {
let new_recess = recess(i);
if let Some(p) = pending.remove(&i) {
placed[i] = Some(p);
}
if !matches!(items[i].shape, Shape::Linear { .. })
|| chained[i]
|| locked.get(i).copied().unwrap_or(false)
{
continue;
}
let (Some(view), Some(side)) = (chosen[i], sides[i]) else {
continue;
};
let location_order_conflict = |target: Side, c: &Placed, ink: &[Option<Placed>]| {
if !old_location(i) {
return false;
}
let normal = usize::from(matches!(target, Side::Above | Side::Below));
let outline = frames[view].outline();
let centre = (outline[normal] + outline[normal + 2]) / 2.0;
let distance = |q: &Placed| (q.arrows[0][0][normal] - centre).abs();
ink.iter().enumerate().any(|(j, q)| {
let nearer = (!semantic_pmi
&& matches!(items[j].shape, Shape::Linear { order: 0, .. }))
|| (location(j)
&& positions[j] < positions[i]
&& items[j].value < items[i].value - 1e-9);
nearer
&& chosen[j] == Some(view)
&& sides[j] == Some(target)
&& q.as_ref().is_some_and(|q| {
q.arrows.len() == 2
&& q.rotation == c.rotation
&& distance(c) < distance(q) + super::lane_pitch() - 1e-6
})
})
};
let Some(p) = &placed[i] else {
continue;
};
let conflict = |a: &Placed, b: &Placed| {
linear_pair_contact(a, b, new_recess && (semantic_frame(b) || held))
};
let others: Vec<_> = placed
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.filter_map(|(_, p)| p.as_ref())
.chain(obstacles.iter().filter_map(|d| d.placed.as_ref()))
.collect();
let separated_labels: Vec<_> = placed
.iter()
.enumerate()
.filter(|(j, _)| {
crate::nominal::geometry_eligible(analysis)
&& !crate::nominal::comparison()
&& *j != i
&& chosen[*j] == Some(view)
})
.filter(|(j, _)| {
matches!(items[*j].shape, Shape::Linear { order: 0, .. })
&& items[*j]
.measurements
.iter()
.any(|m| !m.feature.starts_with("f:section_recess:"))
})
.filter_map(|(_, q)| q.as_ref())
.filter(|q| {
!super::label_lanes::off_span(q, std::iter::empty())
&& super::label_lanes::off_span(q, std::iter::once(p))
})
.collect();
let label_conflict = !separated_labels.is_empty()
|| location_order_conflict(side, p, placed)
|| !page.fits(p, view)
|| others.iter().any(|q| conflict(p, q))
|| (recess_context && super::label_lanes::off_span(p, others.iter().copied()));
let Some(v) = views.iter().find(|v| v.id.strip_prefix("v:") == Some(view)) else {
continue;
};
let footprint = cache.get(analysis, v);
let model = models
.entry(view)
.or_insert_with(|| cache.model(analysis, v));
let extension_counter = footprint.extension_counter(&items[i].measurements);
let measured_features = std::cell::RefCell::new(BTreeMap::new());
let measured_geometry = std::cell::RefCell::new(BTreeMap::new());
let feature_crossings = |c: &Placed| {
c.lines
.iter()
.take(2)
.map(|line| {
let key = line.map(|p| p.map(f64::to_bits));
if let Some(hits) = measured_features.borrow().get(&key) {
return *hits;
}
let hits = extension_counter.count(*line);
measured_features.borrow_mut().insert(key, hits);
hits
})
.sum::<usize>()
};
let feature_conflict =
physical_witness_retry(analysis) && !recess_context && feature_crossings(p) > 0;
let crossings = |c: &Placed| {
let features = feature_crossings(c);
let geometry = c
.lines
.iter()
.take(2)
.map(|line| {
let key = line.map(|p| p.map(f64::to_bits));
if let Some(hits) = measured_geometry.borrow().get(&key) {
return *hits;
}
let hits = model.as_ref().map_or(0, |projection| {
let (edges, boxes) = projection.as_ref();
edges.crossings(*line)
+ boxes.iter().filter(|b| crosses(*line, **b)).count()
});
measured_geometry.borrow_mut().insert(key, hits);
hits
})
.sum::<usize>();
if recess_context || feature_conflict {
(features, geometry)
} else {
(0, features + geometry)
}
};
let before = crossings(p);
let recess_corridor = (before.0 > 0
&& ((old_location(i) && established_clear.contains(&items[i].id))
|| (new_native
&& crate::nominal::geometry_eligible(analysis)
&& !crate::nominal::comparison()
&& matches!(items[i].shape, Shape::Linear { order: 0, .. }))))
|| items[i]
.measurements
.iter()
.any(|m| m.feature.starts_with("f:section_recess:"))
|| p.lines.iter().take(2).any(|line| {
footprint
.crossed_extension(*line, &items[i].measurements)
.iter()
.any(|id| id.starts_with("f:section_recess:"))
});
if !feature_conflict
&& ((recess_context && !recess_corridor && !label_conflict)
|| (!label_conflict && (!recess_corridor || before == (0, 0))))
{
if recess_context
&& !semantic_pmi
&& new_native
&& old_location(i)
&& before.0 > 0
&& let Some(near_face) =
near_face_start(&LinearStroke::new(p), frames[view].outline(), side)
{
let mut shortened = p.clone();
near_face.write(&mut shortened);
if crossings(&shortened).0 < before.0 {
placed[i] = Some(shortened);
}
}
continue;
}
let paper = page.drawable();
let max_lane = (((paper[2] - paper[0]).max(paper[3] - paper[1]) / super::lane_pitch())
.ceil() as usize)
.max(16);
let choices: Vec<_> = if recess_context
|| feature_conflict
|| (semantic_pmi && physical_witness_retry(analysis))
{
[side, opposite(side)]
.into_iter()
.flat_map(|side| {
(0..if recess_context { max_lane } else { 8 }).map(move |lane| (side, lane))
})
.collect()
} else {
(0..8).map(|lane| (opposite(side), lane)).collect()
};
let repair_witnesses: Vec<_> = placed
.iter()
.map(|q| q.as_ref().is_some_and(|q| page.witnesses_fit(q, view)))
.collect();
let immovable: Vec<_> = obstacles
.iter()
.filter_map(|d| d.placed.as_ref())
.chain(
placed
.iter()
.enumerate()
.filter(|(j, _)| {
*j != i
&& (sides[*j].is_none()
|| locked.get(*j).copied().unwrap_or(false)
|| chosen[*j] != Some(view)
|| !repair_witnesses[*j]
|| placed[*j].as_ref().is_some_and(|q| q.arrows.len() != 2))
})
.filter_map(|(_, p)| p.as_ref()),
)
.collect();
let transverse_allowed = |b: &Placed| new_recess && (semantic_frame(b) || held);
let fixed_witnesses = WitnessPeers::new(immovable.iter().copied());
let all_witnesses = WitnessPeers::new(others.iter().copied());
let all_peer_ink = PeerInk::new(others.iter().copied());
let fixed_peer_ink = PeerInk::new(immovable.iter().copied());
let peer_witnesses = super::leaders::SegmentIndex::new(
placed
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.filter_map(|(_, q)| q.as_ref())
.flat_map(|q| q.lines.iter().take(2).copied())
.collect(),
);
let repair_peers: Vec<_> = (0..placed.len())
.map(|_| std::cell::OnceCell::new())
.collect();
let repair_static: Vec<_> = (0..placed.len())
.map(|_| std::cell::OnceCell::new())
.collect();
let repair_clearance = std::cell::RefCell::new(std::collections::HashMap::new());
let best_rank = std::cell::Cell::new(None);
let physical_trial = std::cell::RefCell::new(Placed {
lines: vec![[[0.; 2]; 2]; 3],
arrows: vec![[[0.; 2]; 2]; 2],
label_at: [0.; 2],
rotation: 0.,
label_box: [0.; 4],
});
let yield_guard = std::cell::Cell::new(guard_yielded);
let yield_guarded = std::cell::Cell::new(false);
let yielded_line = |c: &Placed| {
let shared = yield_guard.get()
&& pending
.values()
.any(|q| q.arrows.len() == 2 && shared_dimension_line(&c.lines, &q.lines));
if shared {
yield_guarded.set(true);
}
shared
};
let mut found = None;
for (pass, oblique) in [(0, false), (0, true), (1, false), (1, true)] {
if pass == 1 {
if !yield_guarded.get() {
break;
}
yield_guard.set(false);
}
best_rank.set(None);
let candidates = choices
.iter()
.copied()
.flat_map(|(target, lane)| {
if best_rank
.get()
.is_some_and(|best| best <= ((0, 0), 0, target != side, lane))
{
return Vec::new();
}
let Some(mut c) = place(&frames[view], &items[i], target, lane) else {
return Vec::new();
};
if feature_conflict {
nearest_pattern_witnesses(
analysis,
&frames[view],
&items[i].measurements,
&mut c,
);
}
let c = LinearStroke::new(&c);
let mut strokes = vec![c];
if recess_context
&& recess_corridor
&& items[i]
.measurements
.iter()
.any(|m| m.parameter.contains("location"))
{
let normal = usize::from(c.rotation == 0.0);
let out = if matches!(target, Side::Below | Side::Left) {
-1.0
} else {
1.0
};
for extra in [0.25, 0.5, 1.0] {
let mut spaced = c;
if spaced
.lines
.iter()
.take(2)
.any(|l| (l[1][normal] - l[0][normal]).abs() <= extra + 1.0)
{
continue;
}
for line in spaced.lines.iter_mut().take(2) {
line[0][normal] += out * extra;
}
strokes.push(spaced);
}
if let Shape::Linear { a_edge, b_edge, .. } = items[i].shape
&& a_edge != b_edge
{
let rim = items[i]
.measurements
.iter()
.filter_map(|m| {
let f = analysis
.features
.iter()
.find(|f| f.id == m.feature && f.kind == "hole")?;
Some(f.parameters.get("diameter")?.as_f64()? * v.scale / 2.0)
})
.fold(0.0_f64, f64::max);
let end = usize::from(a_edge);
let extra = rim + 1.0;
if rim > 0.0
&& (c.lines[end][1][normal] - c.lines[end][0][normal]).abs()
> extra + 1.0
{
let mut spaced = c;
spaced.lines[end][0][normal] += out * extra;
strokes.push(spaced);
}
}
}
if recess_context && !semantic_pmi && new_native {
if let Some(near_face) = near_face_start(&c, frames[view].outline(), target)
{
strokes.push(near_face);
}
}
if recess_context
&& !semantic_pmi
&& super::has_new_native_recess_dimensions(analysis)
{
let normal = usize::from(c.rotation == 0.0);
let out = if matches!(target, Side::Below | Side::Left) {
-1.0
} else {
1.0
};
let bases = strokes.clone();
for mut clear in bases {
for line in clear.lines.iter_mut().take(2) {
for peer in others
.iter()
.filter(|p| p.arrows.len() == 1 && p.lines.len() == 2)
{
let b = peer.label_box;
let boxes = std::iter::once([
b[0] - 1.0,
b[1] - 1.0,
b[2] + 1.0,
b[3] + 1.0,
])
.chain(
peer.lines.iter().map(|[a, b]| {
[
a[0].min(b[0]) - 1.0,
a[1].min(b[1]) - 1.0,
a[0].max(b[0]) + 1.0,
a[1].max(b[1]) + 1.0,
]
}),
);
for b in boxes {
if super::leaders::touches_box(*line, b) {
let from = b[normal + if out > 0.0 { 2 } else { 0 }]
+ out * 1e-6;
if out * (from - line[0][normal]) > 0.0 {
line[0][normal] = from;
}
}
}
}
}
if clear
.lines
.iter()
.take(2)
.all(|l| out * (l[1][normal] - l[0][normal]) > 1.0)
{
strokes.push(clear);
}
}
}
if new_recess
&& items[i]
.measurements
.iter()
.all(|m| m.parameter == "channel_width.length")
{
for peer in items.iter().filter(|peer| {
peer.id != items[i].id
&& peer
.measurements
.iter()
.any(|m| items[i].measurements.contains(m))
}) {
let mut alternative = items[i].clone();
alternative.shape = peer.shape.clone();
if let Some(p) = place(&frames[view], &alternative, target, lane) {
strokes.push(LinearStroke::new(&p));
}
}
}
if recess_context
&& new_native
&& before.0 > 0
&& items[i].measurements.iter().any(|m| {
m.feature == "step_level"
&& m.parameter.starts_with("step_height.length")
})
&& let Shape::Linear {
a,
b,
axis,
a_edge,
b_edge,
order,
} = items[i].shape
&& axis == 2
&& let Some(support) = analysis
.level_supports
.iter()
.rev()
.find(|s| (s.z - b[2]).abs() < 1e-3)
{
let mut alternate = items[i].clone();
let mut from = a;
let mut to = b;
from[0] = support.x_span[0];
to[0] = support.x_span[0];
alternate.shape = Shape::Linear {
a: from,
b: to,
axis,
a_edge,
b_edge,
order,
};
if let Some(p) = place(&frames[view], &alternate, target, lane) {
strokes.push(LinearStroke::new(&p));
}
}
if oblique {
if !recess_context {
return Vec::new();
}
let bases = std::mem::take(&mut strokes);
for base in bases {
let k = usize::from(base.rotation != 0.0);
let normal = 1 - k;
if (base.lines[0][0][normal] - base.lines[1][0][normal]).abs() > 1e-6 {
continue;
}
for slope in [1.0 / 3.0_f64.sqrt(), -1.0 / 3.0_f64.sqrt()] {
let mut skew = base;
let dimension = base.arrows[0][0][normal];
let shift = slope * (dimension - base.lines[0][0][normal]);
for l in skew.lines.iter_mut().take(2) {
l[1][k] += slope * (l[1][normal] - l[0][normal]);
}
for q in skew
.lines
.iter_mut()
.skip(2)
.chain(&mut skew.arrows)
.flatten()
{
q[k] += shift;
}
skew.label_at[k] += shift;
skew.label_box[k] += shift;
skew.label_box[k + 2] += shift;
let mut base_placed = p.clone();
base.write(&mut base_placed);
let mut skew_placed = p.clone();
skew.write(&mut skew_placed);
let material =
crate::material::solids(analysis, |p| frames[view].page(p));
let traversal = |c: &Placed| {
c.lines
.iter()
.take(2)
.map(|l| {
crate::material::intervals(&material, *l)
.iter()
.map(|(a, b)| b - a)
.sum::<f64>()
})
.sum::<f64>()
};
let skew_hits = crossings(&skew_placed);
let base_hits = crossings(&base_placed);
if skew_hits.0 > base_hits.0
|| skew_hits.1 > base_hits.1
|| traversal(&skew_placed) > traversal(&base_placed) + 1e-6
|| traversal(&skew_placed) > crate::material::FLOOR
|| others.iter().any(|q| {
skew_placed.lines.iter().take(2).any(|l| {
crosses(*l, q.label_box)
|| q.lines
.iter()
.copied()
.chain(
q.arrows
.iter()
.flat_map(|a| super::leaders::head(*a)),
)
.any(|t| super::leaders::lines_meet(*l, t))
})
})
{
continue;
}
strokes.push(skew);
}
}
}
strokes
.into_iter()
.map(|c| (target, lane, c))
.collect::<Vec<_>>()
})
.flat_map(|(target, lane, stroke)| {
let mut trial = physical_trial.borrow_mut();
stroke.write(&mut trial);
let stroke = &*trial;
if !page.witnesses_fit(stroke, view)
|| fixed_witnesses.contact(stroke, transverse_allowed)
|| yielded_line(stroke)
{
return Vec::new();
}
if recess_context || feature_conflict {
let bound = (feature_crossings(stroke), 0);
if feature_conflict && !label_conflict && bound.0 >= before.0 {
return Vec::new();
}
if (bound > before && !(new_recess && label_conflict))
|| (!label_conflict && bound >= before)
|| best_rank
.get()
.is_some_and(|best| best <= (bound, 0, target != side, lane))
{
return Vec::new();
}
}
let hits = crossings(stroke);
if (hits > before && !(new_recess && label_conflict))
|| (!label_conflict && hits >= before)
{
return Vec::new();
}
let lower_bound = (hits, 0, target != side, lane);
if best_rank.get().is_some_and(|best| best <= lower_bound) {
return Vec::new();
}
let constant_contact = all_witnesses.contact(stroke, transverse_allowed);
let mut choices = vec![stroke.clone()];
let needs_text = !page.fits(stroke, view)
|| constant_contact
|| all_peer_ink.mutable_contact(stroke)
|| super::label_lanes::off_span(stroke, others.iter().copied())
|| others.iter().any(|q| {
super::label_lanes::off_span(q, std::iter::once(stroke))
&& !super::label_lanes::off_span(q, std::iter::once(p))
});
if (recess_context || feature_conflict) && needs_text {
choices.extend(super::label_lanes::centered_short(stroke));
choices.extend(
super::label_lanes::alternatives_placed(
stroke,
page.drawable(),
&others,
)
.take(8),
);
}
choices
.into_iter()
.map(|c| (target, lane, c, constant_contact))
.collect::<Vec<_>>()
})
.filter_map(|(target, lane, c, constant_contact)| {
if location_order_conflict(target, &c, placed) || !page.fits(&c, view) {
return None;
}
let hits = crossings(&c);
if (hits > before && !(new_recess && label_conflict))
|| (!label_conflict && hits >= before)
{
return None;
}
if fixed_peer_ink.mutable_contact(&c) || peer_witnesses.crosses_box(c.label_box)
{
return None;
}
let mut repairs: Vec<(usize, Placed)> = Vec::new();
if recess_context || feature_conflict {
for (j, q) in placed
.iter()
.enumerate()
.filter_map(|(j, q)| Some((j, q.as_ref()?)))
{
if j == i {
continue;
}
let new_span = super::label_lanes::off_span(q, std::iter::once(&c))
&& !super::label_lanes::off_span(q, std::iter::once(p));
let text_crossed = c.lines.iter().any(|l| crosses(*l, q.label_box));
if !new_span && !text_crossed {
continue;
}
if locked.get(j).copied().unwrap_or(false) || repairs.len() >= 8 {
return None;
}
if chosen[j] != Some(view) || sides[j].is_none() || q.arrows.len() != 2
{
continue;
}
if !repair_witnesses[j] {
return None;
}
let peers = repair_peers[j].get_or_init(|| {
placed
.iter()
.enumerate()
.filter(|(t, _)| *t != i && *t != j)
.filter_map(|(_, q)| q.as_ref())
.chain(obstacles.iter().filter_map(|d| d.placed.as_ref()))
.collect::<Vec<_>>()
});
let rest: Vec<_> =
peers.iter().copied().chain(std::iter::once(&c)).collect();
let repaired =
super::label_lanes::alternatives_placed(q, page.drawable(), &rest)
.find(|r| {
if !page.fits(r, view)
|| conflict(r, &c)
|| super::label_lanes::off_span(r, std::iter::once(&c))
{
return false;
}
let bits: [u64; 8] = r
.label_box
.iter()
.copied()
.chain(r.lines[2].into_iter().flatten())
.map(f64::to_bits)
.collect::<Vec<_>>()
.try_into()
.expect("repair geometry");
let key = (j, bits);
let cached = repair_clearance.borrow().get(&key).copied();
cached.unwrap_or_else(|| {
let (labels, lines) =
repair_static[j].get_or_init(|| {
let constraints: Vec<_> = peers
.iter()
.copied()
.filter(|t| !conflict(q, t))
.collect();
let labels: Vec<_> = constraints
.iter()
.map(|t| t.label_box)
.collect();
let lines = super::leaders::SegmentIndex::new(
constraints
.iter()
.flat_map(|t| {
t.lines.iter().copied().chain(
t.arrows.iter().flat_map(|a| {
super::leaders::head(*a)
}),
)
})
.collect(),
);
(labels, lines)
});
let clear = !super::label_lanes::off_span(
r,
peers.iter().copied(),
) && labels.iter().all(|label| {
!boxes_overlap(r.label_box, *label)
&& !crosses(r.lines[2], *label)
}) && !lines.crosses_box(r.label_box);
let mut saved = repair_clearance.borrow_mut();
if saved.len() >= 4096 {
saved.clear();
}
saved.insert(key, clear);
clear
})
});
if let Some(r) = repaired {
repairs.push((j, r));
}
}
}
let final_peers: Vec<_> = placed
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.filter_map(|(j, q)| {
repairs
.iter()
.find(|(t, _)| *t == j)
.map(|(_, r)| r)
.or(q.as_ref())
})
.chain(obstacles.iter().filter_map(|d| d.placed.as_ref()))
.collect();
let pair_contact = if repairs.is_empty() {
constant_contact || all_peer_ink.mutable_contact(&c)
} else {
final_peers.iter().any(|q| conflict(&c, q))
};
if pair_contact
|| separated_labels
.iter()
.any(|q| super::label_lanes::off_span(q, std::iter::once(&c)))
|| (recess_context
&& (super::label_lanes::off_span(&c, final_peers.iter().copied())
|| final_peers.iter().any(|q| {
super::label_lanes::off_span(q, std::iter::once(&c))
&& !super::label_lanes::off_span(q, std::iter::once(p))
})))
{
return None;
}
for (j, r) in &repairs {
let old = placed[*j].as_ref().unwrap();
if final_peers
.iter()
.any(|q| !std::ptr::eq(*q, r) && conflict(r, q) && !conflict(old, q))
{
return None;
}
}
Some((hits, target, lane, c, repairs))
});
for candidate in candidates {
let score = (
candidate.0,
candidate.4.len(),
candidate.1 != side,
candidate.2,
);
let optimal = candidate.0 == (0, 0) && candidate.4.is_empty();
if best_rank.get().is_none_or(|best| score < best) {
best_rank.set(Some(score));
found = Some(candidate);
}
if optimal {
break;
}
}
if found.is_some() {
break;
}
}
if let Some((_, target, lane, c, repairs)) = found {
sides[i] = Some(target);
lanes[i] = lane;
placed[i] = Some(c);
for (j, r) in repairs {
placed[j] = Some(r);
}
}
}
}
fn near_face_start(c: &LinearStroke, outline: [f64; 4], target: Side) -> Option<LinearStroke> {
let normal = usize::from(c.rotation == 0.0);
let out = if matches!(target, Side::Below | Side::Left) {
-1.0
} else {
1.0
};
let face = outline[normal + if out > 0.0 { 2 } else { 0 }];
let mut near_face = *c;
for line in near_face.lines.iter_mut().take(2) {
let from = face + out * GAP;
if out * (from - line[0][normal]) > 0.0 {
line[0][normal] = from;
}
}
near_face
.lines
.iter()
.take(2)
.all(|l| out * (l[1][normal] - l[0][normal]) > 1.0)
.then_some(near_face)
}
#[derive(Clone, Copy)]
struct LinearStroke {
lines: [[P2; 2]; 3],
arrows: [[P2; 2]; 2],
label_at: P2,
rotation: f64,
label_box: [f64; 4],
}
impl LinearStroke {
fn new(p: &Placed) -> Self {
Self {
lines: p.lines.as_slice().try_into().expect("three linear strokes"),
arrows: p.arrows.as_slice().try_into().expect("two linear arrows"),
label_at: p.label_at,
rotation: p.rotation,
label_box: p.label_box,
}
}
fn write(&self, p: &mut Placed) {
p.lines.copy_from_slice(&self.lines);
p.arrows.copy_from_slice(&self.arrows);
p.label_at = self.label_at;
p.rotation = self.rotation;
p.label_box = self.label_box;
}
}
fn dimension_lines_overlap(p: &Placed, q: &Placed) -> bool {
let (Some(a), Some(b)) = (p.lines.get(2), q.lines.get(2)) else {
return false;
};
(0..2).any(|k| {
let along = 1 - k;
(a[0][k] - a[1][k]).abs() < 1e-6
&& (b[0][k] - b[1][k]).abs() < 1e-6
&& (a[0][k] - b[0][k]).abs() < 1e-6
&& p.arrows.iter().any(|a| {
q.arrows
.iter()
.any(|b| (a[0][along] - b[0][along]).abs() < 1e-6)
})
&& a[0][along]
.min(a[1][along])
.max(b[0][along].min(b[1][along]))
< a[0][along]
.max(a[1][along])
.min(b[0][along].max(b[1][along]))
- 1e-6
})
}
const _: () = assert!(STRAY_TEXT == TEXT);
#[allow(clippy::too_many_arguments)]
pub(super) fn respace(
frames: &BTreeMap<&str, Frame>,
items: &[Want],
chosen: &[Option<&'static str>],
places: &[Option<&crate::placement_intent::Place>],
sides: &[Option<Side>],
lanes: &[usize],
occupied: &mut BTreeMap<&'static str, Vec<Vec<[P2; 2]>>>,
page: &Page,
intended: &mut [Option<Placed>],
) {
let span = |p: &Placed| linear_ink(&p.lines).map_or(0.0, |l| l.span[1] - l.span[0]);
let mut order: Vec<usize> = (0..items.len())
.filter(|&i| {
matches!(items[i].shape, Shape::Linear { .. })
&& chosen[i].is_some()
&& sides[i].is_some()
&& intended[i].is_some()
&& places[i].is_some_and(|p| !p.pinned && p.offset.is_none())
})
.collect();
let requested = |i: usize| places[i].and_then(|p| p.lane).unwrap_or(lanes[i]);
order.sort_by(|&a, &b| {
(chosen[a], requested(a))
.cmp(&(chosen[b], requested(b)))
.then(
span(intended[a].as_ref().unwrap()).total_cmp(&span(intended[b].as_ref().unwrap())),
)
.then(a.cmp(&b))
});
for i in order {
let (Some(v), Some(side), Some(place)) = (chosen[i], sides[i], places[i]) else {
continue;
};
let taken = occupied.entry(v).or_default();
let free = |p: &Placed| !taken.iter().any(|q| shared_dimension_line(&p.lines, q));
if !free(intended[i].as_ref().unwrap()) {
let free_lanes: Vec<Placed> = (requested(i) + 1..requested(i) + 64)
.filter_map(|lane| {
let mut p = place.clone();
p.lane = Some(lane);
intent(&frames[v], &items[i], &p, side, lanes[i], 0.0).filter(|p| free(p))
})
.collect();
let moved = free_lanes
.iter()
.find(|p| page.fits(p, v))
.or(free_lanes.first());
if let Some(p) = moved {
intended[i] = Some(p.clone());
}
}
taken.push(intended[i].as_ref().unwrap().lines.clone());
}
}
pub(super) fn relabel(held: &Placed, was: &[String], w: &Want) -> Placed {
if was == w.terms.as_slice() {
return held.clone();
}
let ink = label_ink(&w.terms, font());
let mut p = held.clone();
let fresh = label_box(&ink, p.label_at, p.rotation);
let old = held.label_box;
let r = p.rotation.rem_euclid(180.0);
let axis = if r.abs() < 1e-9 {
Some(0)
} else if (r - 90.0).abs() < 1e-9 {
Some(1)
} else {
None
};
let mut d = [0.0; 2];
match (&w.shape, axis) {
(Shape::Callout { .. }, Some(k)) => {
let shoulder = p.lines.last().copied();
let right = shoulder.is_none_or(|[a, b]| b[k] >= a[k]);
d[k] = if right {
old[k] - fresh[k]
} else {
old[k + 2] - fresh[k + 2]
};
}
(_, Some(k)) => d[k] = 0.5 * (old[k] + old[k + 2] - fresh[k] - fresh[k + 2]),
(_, None) => {
for k in 0..2 {
d[k] = 0.5 * (old[k] + old[k + 2] - fresh[k] - fresh[k + 2]);
}
}
}
p.label_at = [p.label_at[0] + d[0], p.label_at[1] + d[1]];
p.label_box = label_box(&ink, p.label_at, p.rotation);
if let (Shape::Callout { .. }, Some(k), Some(last)) = (&w.shape, axis, p.lines.last_mut()) {
let [a, b] = *last;
if b[k] >= a[k] && b[k] > old[k] {
last[1][k] = p.label_box[k + 2] + (b[k] - old[k + 2]);
} else if b[k] < a[k] && b[k] < old[k + 2] {
last[1][k] = p.label_box[k] + (b[k] - old[k]);
}
}
p
}
pub(super) fn offset_intent(
frame: &Frame,
w: &Want,
offset: P2,
held: Option<(&Placed, &[String])>,
) -> Option<Placed> {
let (held, terms) = held?;
let mut p = relabel(held, terms, w);
if offset.iter().all(|v| v.abs() < 1e-6) {
return Some(p);
}
let radians = p.rotation.to_radians();
let u = match p.rotation {
0.0 => [1.0, 0.0],
90.0 => [0.0, 1.0],
_ => [radians.cos(), radians.sin()],
};
let n = [-u[1], u[0]];
let dot = |a: P2, b: P2| a[0] * b[0] + a[1] * b[1];
let shift = dot(offset, n);
let line = *p.lines.get(2)?;
let height = dot(line[0], n) + shift;
let move_to = |a: P2, h: f64| [0, 1].map(|k| a[k] + (h - dot(a, n)) * n[k]);
let sources = match &w.shape {
Shape::Linear {
a,
b,
a_edge,
b_edge,
..
} => {
let k = usize::from(p.rotation == 0.0);
let target = move_to(line[0], height)[k];
[(*a, *a_edge), (*b, *b_edge)].map(|(a, edge)| {
let mut a = frame.page(a);
if edge {
let o = frame.outline();
a[k] = if (target - o[k]).abs() <= (target - o[k + 2]).abs() {
o[k]
} else {
o[k + 2]
};
}
a
})
}
Shape::Aligned { a, b } => {
let mut sources = [frame.page(*a), frame.page(*b)];
sources.sort_by(|a, b| dot(*a, u).total_cmp(&dot(*b, u)));
sources
}
_ => return None,
};
p.lines.truncate(3);
for (i, source) in sources.into_iter().enumerate() {
let out = if height >= dot(source, n) { 1.0 } else { -1.0 };
p.lines[i] = [
move_to(source, dot(source, n) + out * GAP),
move_to(source, height + out * OVERSHOOT),
];
}
p.label_at = [held.label_at[0] + offset[0], held.label_at[1] + offset[1]];
let ink = label_ink(&w.terms, font());
p.label_box = label_box(&ink, p.label_at, p.rotation);
let mut tips = sources.map(|source| move_to(source, height));
tips.sort_by(|a, b| dot(*a, u).total_cmp(&dot(*b, u)));
let [lo, hi] = tips.map(|t| dot(t, u));
let text_lo = dot(p.label_at, u) + ink.min[0];
let text_hi = dot(p.label_at, u) + ink.max[0];
let outside = text_lo < lo + ARROW + SLIDE_CLEAR || text_hi > hi - ARROW - SLIDE_CLEAR;
let along = |a: P2, d: f64| [0, 1].map(|k| a[k] + d * u[k]);
p.arrows = if outside {
vec![
arrow(tips[0], along(tips[0], -1.0)),
arrow(tips[1], along(tips[1], 1.0)),
]
} else {
vec![
arrow(tips[0], along(tips[0], 1.0)),
arrow(tips[1], along(tips[1], -1.0)),
]
};
let extent = if outside {
[
if text_hi < lo {
(lo - ARROW - 0.5).min(text_hi + 0.5)
} else {
lo - ARROW - 0.5
},
if text_lo > hi {
(hi + ARROW + 0.5).max(text_lo - 0.5)
} else {
hi + ARROW + 0.5
},
]
} else {
[lo, hi]
};
p.lines[2] = [
along(tips[0], extent[0] - lo),
along(tips[1], extent[1] - hi),
];
Some(p)
}
pub(super) fn intent(
frame: &Frame,
w: &Want,
p: &crate::placement_intent::Place,
side: Side,
lane: usize,
callout_width: f64,
) -> Option<Placed> {
if !p.pinned && p.side.is_none() && p.lane.is_none() && p.along.is_none() {
return None;
}
if matches!(w.shape, Shape::Callout { .. }) && crate::placement_intent::side_hint(p).is_some() {
return None;
}
let side = match p.side {
Some(crate::placement_intent::Side::Above) => Side::Above,
Some(crate::placement_intent::Side::Below) => Side::Below,
Some(crate::placement_intent::Side::Left) => Side::Left,
Some(crate::placement_intent::Side::Right) => Side::Right,
None => side,
};
let lane = p.lane.unwrap_or(lane);
let mut placed = match &w.shape {
Shape::Aligned { a, b } => place_aligned(frame, *a, *b, &w.terms, side, lane)?,
Shape::Linear { axis, .. } => {
let horizontal = across(frame, *axis)?;
if horizontal != matches!(side, Side::Above | Side::Below) {
return None;
}
place_linear(frame, &w.shape, &w.terms, side, lane)?
}
Shape::Callout { sites, arcs } => {
let (site, _) = sites.first()?;
let c = frame.page(*site);
let o = frame.outline();
let (mut line, out) = lane_at(o, side, 0);
let ink = label_ink(&w.terms, font());
let width = ink.max[0] - ink.min[0];
let step = callout_step(side, width, callout_width);
line += out * lane as f64 * step;
let k = usize::from(matches!(side, Side::Above | Side::Below));
let f = p
.along
.unwrap_or(crate::placement_intent::Along::Fraction(0.5))
.fraction();
let mut elbow = c;
elbow[k] = line;
elbow[1 - k] = o[1 - k] + f * (o[3 - k] - o[1 - k]);
let tip = super::leaders::intent_tip(frame, sites, arcs, elbow);
let left = match side {
Side::Left => true,
Side::Right => false,
Side::Above | Side::Below => p.pinned && elbow[0] < tip[0] - 1e-9,
};
let end = [
elbow[0]
+ if left {
-width - super::SHOULDER
} else {
width + super::SHOULDER
},
elbow[1],
];
let at = [
if left {
end[0] - ink.min[0]
} else {
elbow[0] + super::SHOULDER - ink.min[0]
},
elbow[1] + LIFT,
];
Placed {
lines: vec![[tip, elbow], [elbow, end]],
arrows: vec![arrow(tip, elbow)],
label_at: at,
rotation: 0.0,
label_box: label_box(&ink, at, 0.0),
}
}
Shape::Angular { angle, .. } => super::angular::intent(frame, angle, &w.terms, p)?,
};
if matches!(w.shape, Shape::Linear { .. })
&& let Some(along) = p.along
{
let k = usize::from(placed.rotation != 0.0);
let lo = placed
.arrows
.iter()
.map(|a| a[0][k])
.fold(f64::INFINITY, f64::min);
let hi = placed
.arrows
.iter()
.map(|a| a[0][k])
.fold(f64::NEG_INFINITY, f64::max);
let ink = label_ink(&w.terms, font());
let width = ink.max[0] - ink.min[0];
if along == crate::placement_intent::Along::Slot(crate::placement_intent::Slot::Outside) {
let end = (hi + ARROW + 2.0).max(placed.lines[2][0][k].max(placed.lines[2][1][k]));
placed.label_at[k] = end + 1.0 - ink.min[0];
let line = &mut placed.lines[2];
let far = usize::from(line[1][k] >= line[0][k]);
line[far][k] = end;
} else {
placed.label_at[k] = lo + along.fraction() * (hi - lo) - 0.5 * width - ink.min[0];
}
placed.label_box = label_box(&ink, placed.label_at, placed.rotation);
}
Some(placed)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn place_merged(
frames: &BTreeMap<&str, Frame>,
items: &[Want],
views_of: &[Option<&'static str>],
owner: &[Option<usize>],
alone: &[bool],
drawn: &[bool],
sides: &[Option<Side>],
lanes: &[usize],
placed: &mut [Option<Placed>],
page: &Page,
slot_layout: bool,
) {
for i in 0..items.len() {
if owner[i].is_some() && matches!(items[i].shape, Shape::Linear { .. }) {
placed[i] = views_of[i]
.zip(sides[i])
.and_then(|(v, side)| place(&frames[v], &items[i], side, lanes[i]));
} else if owner[i].is_some() && !drawn[i] {
placed[i] = None;
}
}
settle_where(
placed,
page,
slot_layout,
|i| owner[i].is_some() && matches!(items[i].shape, Shape::Linear { .. }),
|i, j| j != i && (alone[i] || (owner[j].is_none() && Some(j) != owner[i])),
);
}
fn settle_where(
items: &mut [Option<Placed>],
page: &Page,
slot_layout: bool,
moves: impl Fn(usize) -> bool,
sees: impl Fn(usize, usize) -> bool,
) {
let text_box = |b| {
if slot_layout || !page.units.is_mm() {
super::contacts::clear_box(b)
} else {
b
}
};
for i in 0..items.len() {
if !moves(i) {
continue;
}
let Some(p) = items[i].as_ref() else {
continue;
};
let others = || {
items
.iter()
.enumerate()
.filter(|(j, _)| sees(i, *j))
.filter_map(|(_, q)| q.as_ref())
};
let clear = |c: &Placed| {
let own: Vec<&[P2; 2]> = c.lines.iter().filter(|l| !p.lines.contains(l)).collect();
others().all(|q| {
!q.lines.iter().any(|l| crosses(*l, text_box(c.label_box)))
&& !boxes_overlap(c.label_box, text_box(q.label_box))
&& !own.iter().any(|l| crosses(**l, text_box(q.label_box)))
})
};
if clear(p) {
continue;
}
let off_already = !page.on_sheet(p);
let linear = p.arrows.len() == 2
&& p.lines.len() >= 3
&& (p.arrows[0][0][usize::from(p.rotation == 0.)]
- p.arrows[1][0][usize::from(p.rotation == 0.)])
.abs()
< 1e-6;
let outward = linear.then(|| super::label_lanes::outward_heads(p));
let peers: Vec<_> = others().collect();
let found = slides(p, page.drawable)
.chain(outward.iter().flat_map(|outward| {
super::label_lanes::alternatives_placed(outward, page.drawable, &peers)
}))
.find(|c| {
(off_already || page.on_sheet(c))
&& page.clear_of_furniture(c)
&& clear(c)
&& c.arrows
.iter()
.flat_map(|a| super::leaders::head(*a))
.all(|s| peers.iter().all(|q| !crosses(s, q.label_box)))
});
if let Some(c) = found {
items[i] = Some(c);
}
}
}
pub(super) fn slide_parameters(
p: &Placed,
drawable: [f64; 4],
) -> Box<dyn Iterator<Item = (f64, [f64; 2])> + '_> {
if !(p.rotation == 0.0 || p.rotation.abs() == 90.0) {
return Box::new(std::iter::empty());
}
let k = usize::from(p.rotation != 0.0);
let Some(&line) = p.lines.get(2) else {
return Box::new(std::iter::empty());
};
let (l0, l1) = (line[0][k].min(line[1][k]), line[0][k].max(line[1][k]));
let b = p.label_box;
let width = b[k + 2] - b[k];
let (lo, hi) = (l0 + ARROW + SLIDE_CLEAR, l1 - ARROW - SLIDE_CLEAR);
let shifts: Box<dyn Iterator<Item = (f64, [f64; 2])>> = if b[k] >= lo && b[k + 2] <= hi {
let (lo, hi) = (lo.max(drawable[k]), hi.min(drawable[k + 2]));
let (d_min, d_max) = (lo - b[k], hi - b[k + 2]);
let up = (
(d_min / SLIDE_STEP).floor().max(1.0),
(d_max / SLIDE_STEP).ceil(),
);
let down = (
(-d_max / SLIDE_STEP).floor().max(1.0),
(-d_min / SLIDE_STEP).ceil(),
);
let spans: Vec<(f64, f64)> = [up, down].into_iter().filter(|s| s.0 <= s.1).collect();
let first = spans.iter().map(|s| s.0).fold(f64::INFINITY, f64::min);
let last = spans.iter().map(|s| s.1).fold(f64::NEG_INFINITY, f64::max);
let (first, last) = if first <= last {
(first as i64, last as i64)
} else {
(1, 0)
};
Box::new(
(first..=last)
.flat_map(|n| [n, -n])
.map(|n| n as f64 * SLIDE_STEP)
.filter(move |d| b[k] + d >= lo && b[k + 2] + d <= hi)
.map(move |d| (d, [l0, l1])),
)
} else if b[k] > l1 {
let back = (l0 - 1.0 - width) - b[k];
Box::new((0..=(SLIDE_REACH / SLIDE_STEP) as i64).flat_map(move |n| {
let d = n as f64 * SLIDE_STEP;
(n > 0)
.then_some((d, [l0, b[k] + d - 1.0]))
.into_iter()
.chain([(back - d, [b[k + 2] + back - d + 1.0, l1])])
}))
} else {
Box::new(std::iter::empty())
};
shifts
}
pub(super) fn slides(p: &Placed, drawable: [f64; 4]) -> Box<dyn Iterator<Item = Placed> + '_> {
let k = usize::from(p.rotation != 0.0);
let shifts = slide_parameters(p, drawable);
Box::new(shifts.map(move |(d, extent)| {
let mut c = p.clone();
c.label_box[k] += d;
c.label_box[k + 2] += d;
c.label_at[k] += d;
c.lines[2][0][k] = extent[0];
c.lines[2][1][k] = extent[1];
c
}))
}
fn callout_tip(
frame: &Frame,
sites: &[(P3, f64)],
arcs: &[crate::arc::ProfileArc],
up: bool,
) -> (P2, f64) {
let r = std::f64::consts::FRAC_1_SQRT_2;
let v = if up { 1.0 } else { -1.0 };
sites
.iter()
.map(|(c, radius)| {
let p = frame.page(*c);
let rr = frame.s * radius;
let tip = if arcs.is_empty() {
[p[0] + r * rr, p[1] + v * r * rr]
} else {
super::leaders::intent_tip(
frame,
sites,
arcs,
[p[0] + r * (rr + 1.0), p[1] + v * r * (rr + 1.0)],
)
};
((tip, rr), p[0] + v * p[1])
})
.max_by(|a, b| a.1.total_cmp(&b.1))
.expect("a callout has a site")
.0
}
pub(super) fn ink_points(p: &Placed) -> impl Iterator<Item = P2> + '_ {
let heads = p.arrows.iter().flat_map(|[tip, tail]| {
let n = [tail[1] - tip[1], tip[0] - tail[0]];
let k = ARROW_HALF / n[0].hypot(n[1]).max(1e-12);
[
*tip,
[tail[0] + k * n[0], tail[1] + k * n[1]],
[tail[0] - k * n[0], tail[1] - k * n[1]],
]
});
let b = p.label_box;
p.lines
.iter()
.flat_map(|l| l.iter().copied())
.chain(heads)
.chain([[b[0], b[1]], [b[2], b[1]], [b[0], b[3]], [b[2], b[3]]])
}
pub(super) fn ink_through(p: &Placed, b: [f64; 4]) -> bool {
if p.lines.iter().any(|s| crosses(*s, b)) {
return true;
}
p.arrows.iter().any(|&[tip, tail]| {
let n = [tail[1] - tip[1], tip[0] - tail[0]];
let k = ARROW_HALF / n[0].hypot(n[1]).max(1e-12);
let a = [tail[0] + k * n[0], tail[1] + k * n[1]];
let c = [tail[0] - k * n[0], tail[1] - k * n[1]];
let edges = [[tip, a], [a, c], [c, tip]];
let mid = [0.5 * (b[0] + b[2]), 0.5 * (b[1] + b[3])];
let side =
|[u, v]: [P2; 2]| (v[0] - u[0]) * (mid[1] - u[1]) - (v[1] - u[1]) * (mid[0] - u[0]);
let within = {
let s = edges.map(side);
s.iter().all(|x| *x > 0.0) || s.iter().all(|x| *x < 0.0)
};
edges.iter().any(|e| crosses(*e, b)) || within
})
}
pub(super) fn boxes_overlap(a: [f64; 4], b: [f64; 4]) -> bool {
a[0] < b[2] && a[2] > b[0] && a[1] < b[3] && a[3] > b[1]
}
pub(super) fn crosses(s: [P2; 2], b: [f64; 4]) -> bool {
let (p, q) = (s[0], s[1]);
if (p[0] < b[0] && q[0] < b[0])
|| (p[0] > b[2] && q[0] > b[2])
|| (p[1] < b[1] && q[1] < b[1])
|| (p[1] > b[3] && q[1] > b[3])
{
return false;
}
let d = [q[0] - p[0], q[1] - p[1]];
let (mut t0, mut t1) = (0.0f64, 1.0f64);
for (num, den) in [
(p[0] - b[0], -d[0]),
(b[2] - p[0], d[0]),
(p[1] - b[1], -d[1]),
(b[3] - p[1], d[1]),
] {
if den == 0.0 {
if num <= 0.0 {
return false;
}
} else {
let t = num / den;
if den < 0.0 {
t0 = t0.max(t);
} else {
t1 = t1.min(t);
}
}
}
t1 - t0 > 1e-9
}
fn inside(p: P2, b: [f64; 4]) -> bool {
p[0] >= b[0] - 1e-9 && p[0] <= b[2] + 1e-9 && p[1] >= b[1] - 1e-9 && p[1] <= b[3] + 1e-9
}
fn drawable(sheet: &PlannedSheet) -> [f64; 4] {
let [w, h] = sheet.page;
[margin(), margin(), w - margin(), h - margin()]
}
fn outlines<'a>(analysis: &Analysis, views: &'a [PlannedView]) -> Vec<(&'a str, [f64; 4])> {
views
.iter()
.map(|v| {
let b = if v.id == "v:iso" && v.direction == crate::model::VIEWS[3].1 {
let ib = analysis.drawing_iso_bounds();
[
v.at[0] + v.scale * ib[0],
v.at[1] + v.scale * ib[1],
v.at[0] + v.scale * ib[2],
v.at[1] + v.scale * ib[3],
]
} else {
Frame::new(analysis, v).outline
};
(v.id.as_str(), b)
})
.collect()
}
#[derive(serde::Serialize)]
pub(super) struct Page<'a> {
#[serde(skip_serializing_if = "crate::units::Units::is_mm")]
units: crate::units::Units,
drawable: [f64; 4],
views: Vec<(&'a str, [f64; 4])>,
areas: Vec<(&'a str, [f64; 4])>,
title_block: [f64; 4],
}
impl<'a> Page<'a> {
pub(super) fn new(
analysis: &Analysis,
sheet: &PlannedSheet,
views: &'a [PlannedView],
) -> Page<'a> {
Page {
units: sheet.units,
drawable: drawable(sheet),
views: outlines(analysis, views)
.into_iter()
.filter(|(id, _)| views.iter().any(|v| v.id == *id && v.sheet == sheet.number))
.collect(),
areas: views
.iter()
.filter(|v| v.sheet == sheet.number)
.map(|v| (v.id.as_str(), v.area))
.collect(),
title_block: sheet.title_block,
}
}
pub(super) fn area(&self, view: &str) -> Option<[f64; 4]> {
self.areas
.iter()
.find(|(id, _)| id.strip_prefix("v:") == Some(view))
.map(|(_, a)| *a)
}
pub(super) fn drawable(&self) -> [f64; 4] {
self.drawable
}
pub(super) fn title_block(&self) -> [f64; 4] {
self.title_block
}
pub(super) fn views(&self) -> &[(&'a str, [f64; 4])] {
&self.views
}
fn on_sheet(&self, p: &Placed) -> bool {
ink_points(p).all(|q| inside(q, self.drawable))
}
fn witnesses_fit(&self, p: &Placed, own: &str) -> bool {
p.lines
.iter()
.take(2)
.flatten()
.copied()
.chain(
p.arrows
.iter()
.flat_map(|a| super::leaders::head(*a).into_iter().flatten()),
)
.all(|q| inside(q, self.drawable))
&& p.lines.iter().take(2).all(|line| {
!crosses(*line, self.title_block)
&& !self
.views
.iter()
.any(|(id, b)| id.strip_prefix("v:") != Some(own) && crosses(*line, *b))
})
}
fn crossed<'b>(&'b self, p: &'b Placed, own: &'b str) -> impl Iterator<Item = &'a str> + 'b {
self.views
.iter()
.filter(move |(id, b)| {
id.strip_prefix("v:") != Some(own) && p.lines.iter().any(|l| crosses(*l, *b))
})
.map(|(id, _)| *id)
}
pub(super) fn fits(&self, p: &Placed, own: &str) -> bool {
self.on_sheet(p) && self.clear_of_furniture(p) && self.crossed(p, own).next().is_none()
}
pub(super) fn fits_interior(&self, p: &Placed, own: &str) -> bool {
self.on_sheet(p)
&& !boxes_overlap(p.label_box, self.title_block)
&& !p.lines.iter().any(|l| crosses(*l, self.title_block))
&& self.crossed(p, own).next().is_none()
&& self.views.iter().all(|(id, b)| {
id.strip_prefix("v:") == Some(own) || !boxes_overlap(p.label_box, *b)
})
}
fn clear_of_furniture(&self, p: &Placed) -> bool {
!boxes_overlap(p.label_box, self.title_block)
&& self
.views
.iter()
.all(|(_, b)| !boxes_overlap(p.label_box, *b))
&& !p.lines.iter().any(|l| crosses(*l, self.title_block))
}
}
fn dimension_segments(p: &Placed) -> impl Iterator<Item = [P2; 2]> + '_ {
p.lines
.iter()
.copied()
.chain(p.arrows.iter().flat_map(|a| super::leaders::head(*a)))
}
pub(super) fn dimension_lines_clear(a: &Placed, b: &Placed) -> bool {
dimension_segments(a).all(|a| dimension_segments(b).all(|b| !super::leaders::lines_meet(a, b)))
}
pub(super) fn lines_clear_but(a: &Placed, b: &Placed, crossable: usize) -> bool {
dimension_segments(a).all(|s| {
b.lines.iter().enumerate().all(|(k, t)| {
!super::leaders::lines_meet(s, *t)
|| (k < crossable && super::leaders::segments_cross(s, *t))
}) && b
.arrows
.iter()
.flat_map(|h| super::leaders::head(*h))
.all(|t| !super::leaders::lines_meet(s, t))
})
}
pub(super) fn annotations_clear(a: &Placed, b: &Placed) -> bool {
!boxes_overlap(a.label_box, b.label_box)
&& !ink_through(a, b.label_box)
&& !ink_through(b, a.label_box)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn make_room(
i: usize,
candidate: &Placed,
frames: &BTreeMap<&str, Frame>,
items: &[Want],
views_of: &[Option<&'static str>],
sides: &[Option<Side>],
fixed: &[bool],
placed: &[Option<Placed>],
obstacles: &[PlannedDimension],
page: &Page,
) -> Option<Vec<(usize, Placed)>> {
let mut room = Room {
root: i,
candidate,
frames,
items,
views_of,
sides,
fixed,
obstacles,
page,
};
let strict =
|p: &Placed| annotations_clear(candidate, p) && dimension_lines_clear(candidate, p);
if obstacles
.iter()
.filter_map(|d| d.placed.as_ref())
.any(|p| !strict(p))
{
return None;
}
let blockers: Vec<_> = placed
.iter()
.enumerate()
.filter_map(|(j, p)| (i != j && p.as_ref().is_some_and(|p| !strict(p))).then_some(j))
.collect();
let mut work = placed.to_vec();
work[i] = Some(candidate.clone());
let mut locked = fixed.to_vec();
locked[i] = true;
for j in blockers {
if !room.relocate(j, &mut work, &mut locked, 4) {
return None;
}
}
Some(
work.into_iter()
.enumerate()
.filter_map(|(j, p)| {
(i != j && p != placed[j])
.then(|| p.map(|p| (j, p)))
.flatten()
})
.collect(),
)
}
struct Room<'a, 'p> {
root: usize,
candidate: &'a Placed,
frames: &'a BTreeMap<&'a str, Frame>,
items: &'a [Want],
views_of: &'a [Option<&'static str>],
sides: &'a [Option<Side>],
fixed: &'a [bool],
obstacles: &'a [PlannedDimension],
page: &'a Page<'p>,
}
impl Room<'_, '_> {
fn relocate(
&mut self,
j: usize,
work: &mut Vec<Option<Placed>>,
locked: &mut Vec<bool>,
depth: usize,
) -> bool {
if depth == 0
|| locked[j]
|| self.fixed[j]
|| !matches!(self.items[j].shape, Shape::Linear { .. })
{
return false;
}
let (Some(view), Some(side)) = (self.views_of[j], self.sides[j]) else {
return false;
};
let clear_root = |p: &Placed| {
annotations_clear(self.candidate, p) && dimension_lines_clear(self.candidate, p)
};
let compatible = |p: &Placed, q: &Placed| {
annotations_clear(p, q)
&& !dimension_segments(p)
.any(|a| dimension_segments(q).any(|b| super::leaders::segments_along(a, b)))
};
let mut candidates: Vec<_> = (0..12)
.flat_map(|lane| {
[side, opposite(side)]
.into_iter()
.map(move |side| (side, lane))
})
.filter_map(|(side, lane)| place(&self.frames[view], &self.items[j], side, lane))
.filter(|p| {
self.page.fits(p, view)
&& clear_root(p)
&& self
.obstacles
.iter()
.filter_map(|d| d.placed.as_ref())
.all(|q| compatible(p, q))
})
.map(|p| {
let blockers: Vec<_> = work
.iter()
.enumerate()
.filter_map(|(k, q)| {
(k != j && k != self.root && q.as_ref().is_some_and(|q| !compatible(&p, q)))
.then_some(k)
})
.collect();
let crossings = work
.iter()
.flatten()
.map(|q| {
dimension_segments(&p)
.filter(|a| {
dimension_segments(q).any(|b| super::leaders::segments_cross(*a, b))
})
.count()
})
.sum::<usize>();
(blockers, crossings, p)
})
.collect();
candidates.sort_by_key(|(blockers, crossings, _)| (blockers.len(), *crossings));
for (blockers, _, p) in candidates {
if blockers.iter().any(|&k| locked[k]) {
continue;
}
let saved = work.clone();
let saved_locks = locked.clone();
locked[j] = true;
work[j] = Some(p);
if blockers
.into_iter()
.all(|k| self.relocate(k, work, locked, depth - 1))
{
return true;
}
*work = saved;
*locked = saved_locks;
}
false
}
}
#[allow(clippy::too_many_arguments)]
pub(super) fn clear_aligned(
frames: &BTreeMap<&str, Frame>,
items: &[Want],
views_of: &[Option<&'static str>],
sides: &[Option<Side>],
fixed: &[bool],
placed: &mut [Option<Placed>],
obstacles: &[PlannedDimension],
page: &Page,
) {
for (i, w) in items.iter().enumerate() {
let (Shape::Aligned { a, b }, Some(view), Some(side)) = (&w.shape, views_of[i], sides[i])
else {
continue;
};
if fixed[i] || placed[i].is_none() {
continue;
}
let frame = &frames[view];
let others: Vec<_> = placed
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.filter_map(|(_, p)| p.as_ref())
.chain(obstacles.iter().filter_map(|d| d.placed.as_ref()))
.collect();
let clear = |p: &Placed| {
page.fits(p, view)
&& others.iter().all(|q| {
dimension_lines_clear(p, q)
&& !boxes_overlap(p.label_box, q.label_box)
&& !dimension_segments(p).any(|s| crosses(s, q.label_box))
&& !dimension_segments(q).any(|s| crosses(s, p.label_box))
})
};
if placed[i].as_ref().is_some_and(clear) {
continue;
}
let found = (0..80).find_map(|step| {
[side, opposite(side)].into_iter().find_map(|side| {
aligned_at_offset(
frame.page(*a),
frame.page(*b),
frame.outline(),
&w.terms,
side,
GAP + step as f64,
)
.and_then(|p| {
if !page.fits(&p, view) {
return None;
}
let moves = make_room(
i, &p, frames, items, views_of, sides, fixed, placed, obstacles, page,
)?;
Some((p, moves))
})
})
});
if let Some((p, moves)) = found {
for (j, q) in moves {
placed[j] = Some(q);
}
placed[i] = Some(p);
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct AlignedDemand {
view: &'static str,
a: P2,
b: P2,
outline: [f64; 4],
terms: Vec<String>,
side: Side,
lane: usize,
}
impl AlignedDemand {
pub(super) fn new(
frame: &Frame,
w: &Want,
view: &'static str,
side: Side,
lane: usize,
) -> Self {
let Shape::Aligned { a, b } = w.shape else {
unreachable!("only an aligned dimension has aligned demand")
};
Self {
view,
a: frame.page(a),
b: frame.page(b),
outline: frame.outline(),
terms: w.terms.clone(),
side,
lane,
}
}
pub(crate) fn reserve(&self, strips: &mut crate::layout::Strips, scale: f64) {
let outline = self.outline.map(|v| v * scale);
let Some(p) = place_aligned_page(
self.a.map(|v| v * scale),
self.b.map(|v| v * scale),
outline,
&self.terms,
self.side,
self.lane,
) else {
return;
};
let bounds = ink_points(&p).fold(
[
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
],
|b, p| {
[
b[0].min(p[0]),
b[1].min(p[1]),
b[2].max(p[0]),
b[3].max(p[1]),
]
},
);
let [left, below, right, above] = [
outline[0] - bounds[0],
outline[1] - bounds[1],
bounds[2] - outline[2],
bounds[3] - outline[3],
]
.map(|v| if v > 0.0 { v + GAP } else { 0.0 });
if self.view == "side" {
strips.sv_left = strips.sv_left.max(left);
strips.sv_right = strips.sv_right.max(right);
strips.sv_top = strips.sv_top.max(above);
strips.sv_bottom = strips.sv_bottom.max(below);
strips.fv_top = strips.fv_top.max(above);
strips.fv_bottom = strips.fv_bottom.max(below);
} else {
strips.left = strips.left.max(left);
strips.right = strips.right.max(right);
if self.view == "plan" {
strips.pv_authored_top = strips.pv_authored_top.max(above);
strips.pv_bottom = strips.pv_bottom.max(below);
} else {
strips.fv_top = strips.fv_top.max(above);
strips.fv_bottom = strips.fv_bottom.max(below);
}
}
}
}
fn place_aligned(
frame: &Frame,
a: P3,
b: P3,
terms: &[String],
side: Side,
lane: usize,
) -> Option<Placed> {
place_aligned_page(
frame.page(a),
frame.page(b),
frame.outline(),
terms,
side,
lane,
)
}
fn place_aligned_page(
a: P2,
b: P2,
o: [f64; 4],
terms: &[String],
side: Side,
lane: usize,
) -> Option<Placed> {
aligned_at_offset(
a,
b,
o,
terms,
side,
first_line() + lane as f64 * lane_pitch(),
)
}
fn aligned_at_offset(
mut a: P2,
mut b: P2,
o: [f64; 4],
terms: &[String],
side: Side,
offset: f64,
) -> Option<Placed> {
if b[0] < a[0] || (b[0] == a[0] && b[1] < a[1]) {
std::mem::swap(&mut a, &mut b);
}
let span = (b[0] - a[0]).hypot(b[1] - a[1]);
if span <= 1e-9 {
return None;
}
let u = [(b[0] - a[0]) / span, (b[1] - a[1]) / span];
let mut n = [-u[1], u[0]];
let outward = match side {
Side::Above => [0.0, 1.0],
Side::Below => [0.0, -1.0],
Side::Left => [-1.0, 0.0],
Side::Right => [1.0, 0.0],
};
if n[0] * outward[0] + n[1] * outward[1] < 0.0 {
n = n.map(|x| -x);
}
let dot = |p: P2| p[0] * n[0] + p[1] * n[1];
let outer = [[o[0], o[1]], [o[0], o[3]], [o[2], o[1]], [o[2], o[3]]]
.map(dot)
.into_iter()
.fold(f64::NEG_INFINITY, f64::max);
let height = outer + offset;
let move_to = |p: P2, h: f64| [0, 1].map(|k| p[k] + (h - dot(p)) * n[k]);
let (pa, pb) = (move_to(a, height), move_to(b, height));
let ink = label_ink(terms, font());
let width = ink.max[0] - ink.min[0];
let along = |p: P2, s: f64| [0, 1].map(|k| p[k] + s * u[k]);
let mut lines = vec![
[move_to(a, dot(a) + GAP), move_to(a, height + OVERSHOOT)],
[move_to(b, dot(b) + GAP), move_to(b, height + OVERSHOOT)],
];
let arrows = if span >= width + 2.0 * ARROW + 2.0 {
lines.push([pa, pb]);
vec![arrow(pa, along(pa, 1.0)), arrow(pb, along(pb, -1.0))]
} else {
lines.push([along(pa, -ARROW - 2.0), along(pb, ARROW + 2.0)]);
vec![arrow(pa, along(pa, -1.0)), arrow(pb, along(pb, 1.0))]
};
let start = 0.5 * (span - width) - ink.min[0];
let label_at = [
pa[0] + start * u[0] - LIFT * u[1],
pa[1] + start * u[1] + LIFT * u[0],
];
let rotation = u[1].atan2(u[0]).to_degrees();
let normal = [-u[1], u[0]];
let text = [
start + ink.min[0] - GAP,
start + ink.max[0] + GAP,
LIFT + ink.min[1] - GAP,
LIFT + ink.max[1] + GAP,
];
let mut continuations = Vec::new();
for (i, t) in [0.0, span].into_iter().enumerate() {
if t < text[0] || t > text[1] {
continue;
}
let line = lines[i];
let coordinate = |p: P2| (p[0] - pa[0]) * normal[0] + (p[1] - pa[1]) * normal[1];
let (a, b) = (coordinate(line[0]), coordinate(line[1]));
let mut cut = [(text[2] - a) / (b - a), (text[3] - a) / (b - a)];
cut.sort_by(f64::total_cmp);
let (lo, hi) = (cut[0].max(0.0), cut[1].min(1.0));
if lo >= hi {
continue;
}
let at = |t: f64| [0, 1].map(|k| line[0][k] + t * (line[1][k] - line[0][k]));
if lo > 1e-9 {
lines[i] = [line[0], at(lo)];
if hi < 1.0 - 1e-9 {
continuations.push([at(hi), line[1]]);
}
} else {
lines[i] = [at(hi), line[1]];
}
}
lines.extend(continuations);
Some(Placed {
lines,
arrows,
label_at,
rotation,
label_box: label_box(&ink, label_at, rotation),
})
}
#[cfg(test)]
pub(super) fn collisions(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
) -> Vec<Problem> {
collisions_cached(
analysis,
dims,
sheet,
views,
&super::footprints::Cache::default(),
)
}
fn named(d: &PlannedDimension) -> String {
crate::wording::item(&d.id, &d.kind, &d.label)
}
pub(crate) fn collisions_cached(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
cache: &super::footprints::Cache,
) -> Vec<Problem> {
collisions_impl(analysis, dims, sheet, views, cache, None)
}
pub(super) fn collisions_for(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
cache: &super::footprints::Cache,
id: &str,
) -> Vec<Problem> {
collisions_impl(analysis, dims, sheet, views, cache, Some(id))
}
pub(super) fn route_crossings(
d: &PlannedDimension,
peers: &[&PlannedDimension],
limits: [usize; 3],
) -> [usize; 3] {
let Some(p) = d.placed.as_ref() else {
return [0; 3];
};
let mut counts = [0; 3];
for q in peers.iter().filter(|q| q.id != d.id) {
let Some(r) = q.placed.as_ref() else {
continue;
};
for (a, pa, b, pb) in [(d, p, *q, r), (*q, r, d, p)] {
if a.kind != "callout" {
continue;
}
let meets = |lines: &[[P2; 2]]| {
pa.lines
.iter()
.any(|s| lines.iter().any(|t| super::leaders::lines_meet(*s, *t)))
};
let lines = meets(&pb.lines);
let arrows = meets(&pb.arrows);
counts[0] += usize::from(b.kind == "callout" && lines);
counts[1] += usize::from(arrows);
counts[2] += usize::from(lines || arrows);
if (0..3).any(|k| counts[k] > limits[k]) {
return counts;
}
}
}
counts
}
fn crossing_kind(kind: &str, index: usize) -> &'static str {
match kind {
"callout" => "leader",
"linear" if index != 2 => "extension_line",
"angular" if index < 2 => "extension_line",
_ => "dimension_line",
}
}
fn collisions_impl(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
cache: &super::footprints::Cache,
only: Option<&str>,
) -> Vec<Problem> {
let geometry = CollisionGeometry::new(analysis, dims, sheet, views, cache);
collisions_prepared(analysis, dims, sheet, views, &geometry, only)
}
pub(super) struct CollisionGeometry<'a> {
interior_proofs: BTreeMap<String, std::rc::Rc<super::interior::Proof>>,
opening_proofs: BTreeMap<String, std::rc::Rc<super::interior::Proof>>,
datum_proofs: BTreeMap<String, std::rc::Rc<super::interior::Proof>>,
outlines: Vec<(&'a str, [f64; 4])>,
footprints: BTreeMap<&'a str, std::rc::Rc<super::footprints::Footprints>>,
}
impl<'a> CollisionGeometry<'a> {
pub(super) fn new(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &'a [PlannedView],
cache: &super::footprints::Cache,
) -> Self {
let interior_proofs = super::interior::proofs(analysis, views);
let opening_proofs = if dims.iter().any(|d| {
d.measurements
.iter()
.any(|m| m.feature.starts_with("f:rectangular_through_notch:"))
}) {
super::interior::callout_proofs(analysis, views)
} else {
BTreeMap::new()
};
let datum_proofs = if dims.iter().any(|d| d.id.starts_with("pmi:datum.")) {
super::interior::exterior_proofs(analysis, views)
} else {
BTreeMap::new()
};
let outlines: Vec<_> = outlines(analysis, views)
.into_iter()
.filter(|(id, _)| views.iter().any(|v| v.id == *id && v.sheet == sheet.number))
.collect();
let footprints: BTreeMap<_, _> = views
.iter()
.filter_map(|v| {
let name = v.id.strip_prefix("v:")?;
super::NAMES
.contains(&name)
.then(|| (v.id.as_str(), cache.get(analysis, v)))
})
.collect();
Self {
interior_proofs,
opening_proofs,
datum_proofs,
outlines,
footprints,
}
}
pub(super) fn for_item(
&self,
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
id: &str,
) -> Vec<Problem> {
collisions_prepared(analysis, dims, sheet, views, self, Some(id))
}
}
fn collisions_prepared(
analysis: &Analysis,
dims: &[PlannedDimension],
sheet: &PlannedSheet,
views: &[PlannedView],
geometry: &CollisionGeometry<'_>,
only: Option<&str>,
) -> Vec<Problem> {
let mut problems = Vec::new();
let drawable = drawable(sheet);
let CollisionGeometry {
interior_proofs,
opening_proofs,
datum_proofs,
outlines,
footprints,
} = geometry;
let placed: Vec<(&PlannedDimension, &Placed)> = dims
.iter()
.filter(|d| {
views
.iter()
.find(|v| v.id == d.view)
.map_or(sheet.number == 1, |v| v.sheet == sheet.number)
})
.filter_map(|d| d.placed.as_ref().map(|p| (d, p)))
.collect();
let mut feature_problems = Vec::new();
macro_rules! problem {
($code:expr, $severity:expr, $subjects:expr, $message:expr $(,)?) => {{
let subjects: Vec<String> = $subjects;
problems.push(Problem {
id: format!("p:{}:{}", $code, subjects.join("|")),
code: $code.into(),
severity: $severity.into(),
message: $message,
subjects,
..Default::default()
});
}};
}
for (d, p) in &placed {
if only.is_some_and(|id| d.id.as_str() != id) {
continue;
}
if let Some(f) = footprints.get(d.view.as_str()) {
let (code, shafts): (&str, &[[P2; 2]]) = match d.kind.as_str() {
"callout" => {
let bent_datum = d.id.starts_with("pmi:datum.")
&& p.lines.get(1).is_some_and(|s| p.lines[0][1] == s[0]);
(
"leader_through_feature",
&p.lines[..p.lines.len().min(if bent_datum { 2 } else { 1 })],
)
}
"linear" => (
"extension_through_feature",
&p.lines[..p.lines.len().min(2)],
),
_ => ("", &[]),
};
for (n, shaft) in shafts.iter().enumerate() {
let crossed = if d.kind == "callout" {
f.crossed(*shaft, &d.measurements)
} else {
f.crossed_extension(*shaft, &d.measurements)
};
for feature in crossed {
let subjects = vec![d.id.clone(), feature];
feature_problems.push(Problem {
id: format!("p:{code}:{}:line:{n}", subjects.join("|")),
code: code.into(),
severity: "warning".into(),
message: format!(
"The {} of {} crosses {}.",
if d.kind == "callout" {
"leader"
} else if n == 0 {
"first extension line"
} else {
"second extension line"
},
named(d),
crate::wording::feature_id(analysis, &subjects[1])
),
subjects,
..Default::default()
});
}
}
}
if let Some(v) = views.iter().find(|v| v.id == d.view) {
let a = v.area;
let past = ink_points(p)
.map(|q| {
(a[0] - q[0])
.max(q[0] - a[2])
.max(a[1] - q[1])
.max(q[1] - a[3])
})
.fold(0.0, f64::max);
if past > 1e-6 {
problem!(
"annotation_outside_area",
"info",
vec![d.id.clone(), d.view.clone()],
format!(
"{} runs up to {past:.1} mm past the area reserved for {} and its bands; it may crowd what lies beyond.",
crate::wording::capital(&named(d)),
crate::wording::view(&d.view)
),
);
}
}
if d.kind == "linear"
&& let Some(gap) = label_gap(&p.lines, p.label_box).filter(|g| *g > STRAY_TEXT)
{
problem!(
"dimension_text_stray",
"error",
vec![d.id.clone()],
format!(
"The text of {} lies {gap:.1} mm from its dimension line.",
named(d)
),
);
}
let lb = p.label_box;
let ends = p
.lines
.iter()
.chain(&p.arrows)
.flat_map(|s| s.iter().copied());
let area = if d.id.starts_with("t:") {
let [w, h] = sheet.page;
[FRAME, FRAME, w - FRAME, h - FRAME]
} else {
drawable
};
if !inside([lb[0], lb[1]], area)
|| !inside([lb[2], lb[3]], area)
|| ends.clone().any(|e| !inside(e, area))
{
problem!(
"annotation_off_sheet",
"error",
vec![d.id.clone()],
format!(
"{} runs off the sheet's drawing area: {}. The requested ink is retained; move its carrying view or use more paper.",
crate::wording::capital(&named(d)),
{
let mut bounds = lb;
for point in ends {
for k in 0..2 {
bounds[k] = bounds[k].min(point[k]);
bounds[k + 2] = bounds[k + 2].max(point[k]);
}
}
[
("left", area[0] - bounds[0]),
("bottom", area[1] - bounds[1]),
("right", bounds[2] - area[2]),
("top", bounds[3] - area[3]),
]
.into_iter()
.filter(|(_, excess)| *excess > 1e-6)
.map(|(side, excess)| format!("{side} boundary exceeded by {excess:.2} mm"))
.collect::<Vec<_>>()
.join(", ")
}
),
);
}
let tb = sheet.title_block;
if boxes_overlap(lb, tb) || p.lines.iter().any(|s| crosses(*s, tb)) {
problem!(
"annotation_over_title_block",
"warning",
vec![d.id.clone()],
format!(
"{} runs into the title block.",
crate::wording::capital(&named(d))
),
);
}
for (view, b) in outlines {
if boxes_overlap(lb, *b)
&& !(*view == d.view
&& views.iter().find(|v| v.id == d.view).is_some_and(|v| {
interior_proofs.get(&v.id).is_some_and(|proof| {
proof.blank(v, lb)
|| (d.measurements.iter().any(|m| m.parameter == "notch.angle")
&& proof.exterior_annotation_clear(v, p))
|| (d.measurements.iter().any(|m| {
matches!(
m.parameter.as_str(),
super::profile_angles::PARAMETER | "notch.angle"
)
}) && proof.exterior_clear(v, p.label_box, &[], &[]))
}) || (d
.measurements
.iter()
.any(|m| m.feature.starts_with("f:rectangular_through_notch:"))
&& opening_proofs.get(&v.id).is_some_and(|proof| {
if d.kind == "linear" {
proof.opening_linear_clear(v, p)
} else {
proof.annotation_clear(v, p, true)
}
}))
|| (d.id.starts_with("pmi:datum.")
&& datum_proofs
.get(&v.id)
.is_some_and(|proof| proof.exterior_annotation_clear(v, p)))
}))
{
problem!(
"annotation_over_view",
"warning",
vec![d.id.clone(), view.to_string()],
format!(
"The label of {} lies over {}.",
named(d),
crate::wording::view(view)
),
);
}
if *view != d.view && p.lines.iter().any(|s| crosses(*s, *b)) {
problem!(
"annotation_line_over_view",
"warning",
vec![d.id.clone(), view.to_string()],
format!(
"A line of {} runs across {}.",
named(d),
crate::wording::view(view)
),
);
}
}
}
for (i, (a, pa)) in placed.iter().enumerate() {
for (b, pb) in &placed[i + 1..] {
if only.is_some_and(|id| a.id.as_str() != id && b.id.as_str() != id) {
continue;
}
if a.kind == "linear"
&& b.kind == "linear"
&& shared_dimension_line(&pa.lines, &pb.lines)
{
problem!(
"dimension_line_shared",
"error",
vec![a.id.clone(), b.id.clone()],
format!(
"{} and {} are drawn on one dimension line over overlapping spans.",
crate::wording::capital(&named(a)),
named(b)
),
);
}
if a.kind == "linear"
&& b.kind == "linear"
&& pa
.lines
.get(2)
.zip(pb.lines.get(2))
.is_some_and(|(x, y)| super::leaders::segments_cross(*x, *y))
{
problem!(
"dimension_line_crossing",
"warning",
vec![a.id.clone(), b.id.clone()],
format!(
"The dimension lines of {} and {} cross.",
named(a),
named(b)
),
);
}
if boxes_overlap(pa.label_box, pb.label_box) {
problem!(
"annotation_overlap",
"warning",
vec![a.id.clone(), b.id.clone()],
if pa.lines.iter().any(|s| crosses(*s, pb.label_box))
|| pb.lines.iter().any(|s| crosses(*s, pa.label_box))
|| (a.kind == "callout" && ink_through(pa, pb.label_box))
|| (b.kind == "callout" && ink_through(pb, pa.label_box))
{
format!(
"The labels of {} and {} overlap; line-work also crosses a label, \
so separating the text is not enough: move what is drawn, not just the text.",
named(a),
named(b)
)
} else {
format!("The labels of {} and {} overlap.", named(a), named(b))
},
);
}
}
}
for (a, pa) in &placed {
for (b, pb) in &placed {
if a.id == b.id || only.is_some_and(|id| a.id.as_str() != id && b.id.as_str() != id) {
continue;
}
let through = pa.lines.iter().any(|s| crosses(*s, pb.label_box));
if !boxes_overlap(pa.label_box, pb.label_box)
&& (through || ink_through(pa, pb.label_box))
{
problems.push(Problem {
id: format!("p:annotation_ink_overlap:{}|{}", a.id, b.id),
code: "annotation_ink_overlap".into(),
severity: "warning".into(),
subjects: vec![a.id.clone(), b.id.clone()],
ink_is_leader: Some(a.kind == "callout" && through),
message: if through {
format!(
"A line of {} runs through the label of {}.",
named(a),
named(b)
)
} else {
format!(
"The arrowhead of {} reaches into the label of {}.",
named(a),
named(b)
)
},
..Default::default()
});
}
if a.kind == "callout"
&& pa.lines.iter().any(|s| {
pb.lines
.iter()
.chain(&pb.arrows)
.any(|t| super::leaders::lines_meet(*s, *t))
})
{
let mut kinds = Vec::new();
for kind in ["leader", "dimension_line", "extension_line", "arrow"] {
let met = if kind == "arrow" {
pb.arrows
.iter()
.any(|t| pa.lines.iter().any(|s| super::leaders::lines_meet(*s, *t)))
} else {
pb.lines.iter().enumerate().any(|(i, t)| {
crossing_kind(&b.kind, i) == kind
&& pa.lines.iter().any(|s| super::leaders::lines_meet(*s, *t))
})
};
if met {
kinds.push(kind.into());
}
}
problems.push(Problem {
id: format!("p:annotation_leader_crossing:{}|{}", a.id, b.id),
code: "annotation_leader_crossing".into(),
severity: "warning".into(),
subjects: vec![a.id.clone(), b.id.clone()],
message: format!("The leader of {} crosses a line of {}.", named(a), named(b)),
crossing_kinds: kinds,
..Default::default()
});
}
}
}
problems.extend(feature_problems);
problems
}
#[cfg(test)]
mod tests {
#[test]
fn inclined_spans_choose_the_nearer_parallel_support() {
let outline = [0., 0., 100., 100.];
assert_eq!(
aligned_strip([78., 70.], [85., 66.], outline, Side::Below),
Side::Above
);
assert_eq!(
aligned_strip([85., 66.], [78., 70.], outline, Side::Below),
Side::Above
);
assert_eq!(
aligned_strip([22., 30.], [15., 34.], outline, Side::Above),
Side::Below
);
}
#[test]
fn equal_source_spans_follow_geometry_in_either_entity_order() {
let a: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding":{"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,100]], "iso_bounds":[0,0,100,100]
}))
.unwrap();
let frames = BTreeMap::from([(
"front",
Frame::looking(&a, [0., -1., 0.], [0., 0., 1.], [0., 0.], 1.),
)]);
let want = |id: &str, lo| Want {
units: Default::default(),
id: format!("d:pmi:{id}"),
views: vec!["front"],
value: 10.,
terms: vec!["10".into(), "±0.2".into()],
measurements: vec![],
basis: String::new(),
turning: None,
shape: Shape::Linear {
a: [50., 0., lo],
b: [50., 0., lo + 10.],
axis: 2,
a_edge: false,
b_edge: false,
order: 1,
},
};
for items in [
[want("upper", 70.), want("lower", 20.)],
[want("lower", 20.), want("upper", 70.)],
] {
let (_, lanes) = lanes(
&a,
&frames,
items.iter().map(|w| (w, Some("front"))),
&[None; 2],
);
for (w, lane) in items.iter().zip(lanes) {
assert_eq!(lane, usize::from(w.id.ends_with("upper")), "{}", w.id);
}
}
}
#[test]
fn reused_route_geometry_rechecks_moved_ink_and_grown_areas() {
let analysis: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding":{"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,10]], "iso_bounds":[0,0,100,60]
}))
.unwrap();
let plan = crate::model::plan(&analysis, &crate::model::Spec::default()).unwrap();
let cache = super::super::footprints::Cache::default();
let geometry = CollisionGeometry::new(
&analysis,
&plan.dimensions,
&plan.sheet,
&plan.views,
&cache,
);
let mut dims = plan.dimensions.clone();
let id = dims[0].id.clone();
for dx in [0., 15.] {
let p = dims[0].placed.as_mut().unwrap();
p.label_box[2] += dx;
for pad in [-5., 5.] {
let mut views = plan.views.clone();
let v = views.iter_mut().find(|v| v.id == dims[0].view).unwrap();
let b = super::super::ink_box(&dims[0]).unwrap();
v.area = [b[0] - pad, b[1] - pad, b[2] + pad, b[3] + pad];
let fresh = collisions_for(&analysis, &dims, &plan.sheet, &views, &cache, &id);
assert_eq!(
geometry.for_item(&analysis, &dims, &plan.sheet, &views, &id),
fresh
);
assert_eq!(
fresh.iter().any(|p| p.code == "annotation_outside_area"),
pad < 0.
);
}
}
}
#[test]
fn crossing_kinds_and_dimension_strokes_follow_the_placed_structure() {
let analysis: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding":{"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,10]], "iso_bounds":[0,0,100,60]
}))
.unwrap();
let plan = crate::model::plan(&analysis, &crate::model::Spec::default()).unwrap();
let item = |id: &str, kind: &str, lines, arrows, label_box| {
let mut d = plan.dimensions[0].clone();
d.id = id.into();
d.kind = kind.into();
d.placed = Some(Placed {
lines,
arrows,
label_box,
label_at: [label_box[0], label_box[1]],
rotation: 0.,
});
d
};
let horizontal = [[30., 80.], [70., 80.]];
let vertical = [[50., 60.], [50., 100.]];
let far = [[130., 130.], [140., 130.]];
let leader = item(
"d:leader",
"callout",
vec![horizontal],
vec![],
[80., 110., 90., 113.],
);
let mut peer = item(
"d:peer",
"linear",
vec![far, far, far],
vec![],
[100., 110., 110., 113.],
);
let kinds = |a: &PlannedDimension, b: &PlannedDimension| {
collisions(&analysis, &[a.clone(), b.clone()], &plan.sheet, &[])
.into_iter()
.find(|p| p.code == "annotation_leader_crossing")
.unwrap()
};
for (index, kind) in [
(0, "extension_line"),
(1, "extension_line"),
(2, "dimension_line"),
(3, "extension_line"),
] {
peer.placed.as_mut().unwrap().lines = vec![far; 4];
peer.placed.as_mut().unwrap().lines[index] = vertical;
let p = kinds(&leader, &peer);
assert_eq!(p.crossing_kinds, [kind]);
assert_eq!(p.subjects, ["d:leader", "d:peer"]);
assert_eq!(p.id, "p:annotation_leader_crossing:d:leader|d:peer");
let json = serde_json::to_value(&p).unwrap();
assert_eq!(serde_json::from_value::<Problem>(json).unwrap(), p);
}
peer.kind = "callout".into();
assert_eq!(kinds(&leader, &peer).crossing_kinds, ["leader"]);
peer.kind = "linear".into();
peer.placed.as_mut().unwrap().lines = vec![vertical, far, vertical];
peer.placed.as_mut().unwrap().arrows = vec![vertical];
assert_eq!(
kinds(&leader, &peer).crossing_kinds,
["dimension_line", "extension_line", "arrow"]
);
peer.placed.as_mut().unwrap().lines = vec![far; 3];
assert_eq!(kinds(&leader, &peer).crossing_kinds, ["arrow"]);
for ak in ["callout", "linear", "angular"] {
for bk in ["callout", "linear", "angular"] {
for shift in [0., 20., 50.] {
let mut a = leader.clone();
a.kind = ak.into();
a.placed.as_mut().unwrap().arrows = vec![horizontal];
let mut b = peer.clone();
b.kind = bk.into();
let stroke = vertical.map(|p| [p[0] + shift, p[1]]);
b.placed.as_mut().unwrap().lines = vec![stroke; 4];
b.placed.as_mut().unwrap().arrows = vec![stroke];
let problems = collisions(&analysis, &[a.clone(), b.clone()], &plan.sheet, &[]);
let expected = ["leader", "arrow", "total"].map(|kind| {
problems
.iter()
.filter(|p| {
p.code == "annotation_leader_crossing"
&& (kind == "total"
|| p.crossing_kinds.iter().any(|k| k == kind))
})
.count()
});
for d in [&a, &b] {
assert_eq!(route_crossings(d, &[&a, &b], [usize::MAX; 3]), expected);
for limit in 0..=2 {
let counts = route_crossings(d, &[&a, &b], [limit; 3]);
assert_eq!(
counts.iter().any(|&n| n > limit),
expected.iter().any(|&n| n > limit)
);
if counts.iter().all(|&n| n <= limit) {
assert_eq!(counts, expected);
}
}
}
}
}
}
peer.placed.as_mut().unwrap().label_box = [45., 79., 55., 81.];
for (kind, expected) in [("callout", true), ("linear", false)] {
let mut a = leader.clone();
a.kind = kind.into();
let p = collisions(&analysis, &[a, peer.clone()], &plan.sheet, &[])
.into_iter()
.find(|p| p.code == "annotation_ink_overlap")
.unwrap();
assert_eq!(p.ink_is_leader, Some(expected));
}
let mut a = item(
"d:a",
"linear",
vec![far, far, horizontal],
vec![],
[80., 110., 90., 113.],
);
let b = item(
"d:b",
"linear",
vec![horizontal, horizontal, vertical],
vec![],
[100., 110., 110., 113.],
);
let findings = |a: &PlannedDimension, b: &PlannedDimension| {
collisions(&analysis, &[a.clone(), b.clone()], &plan.sheet, &[])
.into_iter()
.filter(|p| p.code == "dimension_line_crossing")
.collect::<Vec<_>>()
};
let ps = findings(&a, &b);
assert_eq!(ps.len(), 1);
assert_eq!(ps[0].severity, "warning");
assert_eq!(ps[0].subjects, ["d:a", "d:b"]);
a.placed.as_mut().unwrap().lines = vec![vertical, vertical, far];
assert!(findings(&a, &b).is_empty(), "extensions may cross");
a.placed.as_mut().unwrap().lines[2] = [[30., 60.], [50., 60.]];
assert!(findings(&a, &b).is_empty(), "endpoint touches are allowed");
}
#[test]
fn item_collision_checks_preserve_all_findings_involving_that_item() {
let analysis: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding":{"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,10]], "iso_bounds":[0,0,100,60]
}))
.unwrap();
let plan = crate::model::plan(&analysis, &crate::model::Spec::default()).unwrap();
let mut dimensions = plan.dimensions.clone();
let mut changed = 0;
for d in dimensions.iter_mut().filter(|d| d.placed.is_some()).take(2) {
let p = d.placed.as_mut().unwrap();
p.label_box = [-3., -3., 3., 3.];
p.label_at = [0., 0.];
changed += 1;
}
assert_eq!(changed, 2);
let cache = super::super::footprints::Cache::default();
let complete = collisions_cached(&analysis, &dimensions, &plan.sheet, &plan.views, &cache);
assert!(complete.iter().any(|p| p.code == "annotation_overlap"));
assert!(complete.iter().any(|p| p.code == "annotation_off_sheet"));
for id in dimensions.iter().map(|d| d.id.as_str()).chain(["absent"]) {
let expected: Vec<_> = complete
.iter()
.filter(|p| p.subjects.iter().any(|s| s == id))
.cloned()
.collect();
assert_eq!(
collisions_for(&analysis, &dimensions, &plan.sheet, &plan.views, &cache, id),
expected,
"{id}"
);
}
}
#[test]
fn indexed_peer_ink_matches_the_complete_linear_collision_predicate() {
for peer_arrows in 0..=2 {
for allow_transverse in [false, true] {
for shift in -12..=12 {
let mut a = Placed {
lines: vec![
[[0., 0.], [0., 10.]],
[[10., 0.], [10., 10.]],
[[0., 10.], [10., 10.]],
],
arrows: vec![arrow([0., 10.], [1., 10.]), arrow([10., 10.], [9., 10.])],
label_at: [0., 12.],
rotation: 0.,
label_box: [0., 11., 8., 14.],
};
let skew = f64::from(shift) * 0.3;
for line in a.lines.iter_mut().take(2) {
line[1][0] += skew;
}
for p in a.lines.iter_mut().skip(2).chain(&mut a.arrows).flatten() {
p[0] += skew;
}
a.label_box[0] += skew;
a.label_box[2] += skew;
a.label_at[0] += skew;
let mut b = a.clone();
for p in b.lines.iter_mut().chain(&mut b.arrows).flatten() {
p[0] += f64::from(shift);
p[1] += 3.;
}
b.label_box = [f64::from(shift), 14., f64::from(shift) + 8., 17.];
b.arrows.truncate(peer_arrows);
let indexed = PeerInk::new(std::iter::once(&b));
let mut scratch = a.clone();
for x in -20..=20 {
let mut moved = a.clone();
moved.label_box[0] += f64::from(x);
moved.label_box[2] += f64::from(x);
moved.lines[2][0][0] = moved.lines[2][0][0].min(moved.label_box[0]);
moved.lines[2][1][0] = moved.lines[2][1][0].max(moved.label_box[2]);
LinearStroke::new(&moved).write(&mut scratch);
assert_eq!(
serde_json::to_vec(&scratch).unwrap(),
serde_json::to_vec(&moved).unwrap()
);
assert_eq!(
WitnessPeers::new(std::iter::once(&b))
.contact(&scratch, |_| allow_transverse),
linear_witness_contact(&moved, &b, allow_transverse)
);
assert_eq!(
linear_pair_contact(&moved, &b, allow_transverse),
linear_witness_contact(&moved, &b, allow_transverse)
|| indexed.mutable_contact(&moved)
);
}
}
}
}
}
#[test]
fn repeated_linear_label_bounds_match_the_full_standard_font_ink() {
for terms in [
vec!["21.0"],
vec!["⌀10 H7", "+0.015\n0"],
vec!["4×", "R3.94"],
vec!["(25.0)"],
vec!["WIDTH", "12 ±0.1"],
] {
let terms: Vec<_> = terms.into_iter().map(str::to_owned).collect();
let full = label_ink(&terms, font());
for _ in 0..2 {
assert_eq!(linear_label_bounds(&terms), (full.min, full.max));
}
}
}
#[test]
fn shared_corridor_orders_sizes_then_locations_then_overall() {
let a: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding": {"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,10]], "iso_bounds":[0,0,100,60]
}))
.unwrap();
let frames = BTreeMap::from([(
"front",
Frame::looking(&a, [0., -1., 0.], [0., 0., 1.], [0., 0.], 1.),
)]);
let want = |id: &str, span: f64, order| Want {
units: Default::default(),
id: id.into(),
views: vec!["front"],
value: span,
terms: vec![span.to_string()],
measurements: vec![],
basis: String::new(),
turning: None,
shape: Shape::Linear {
a: [0., 0., 0.],
b: [span, 0., 0.],
axis: 0,
a_edge: false,
b_edge: false,
order,
},
};
let items = [
want("overall", 100., 2),
want("far", 70., 1),
want("size", 40., 0),
want("near", 20., 1),
want("middle", 50., 1),
];
let (sides, lane) = lanes(
&a,
&frames,
items.iter().map(|w| (w, Some("front"))),
&[None; 5],
);
assert!(sides.iter().all(|s| *s == Some(Side::Below)));
assert_eq!(lane, [4, 3, 0, 1, 2]);
}
#[test]
fn shared_lines_counts_overlapping_spans_on_one_dimension_line() {
let vertical = |x: f64, y0: f64, y1: f64| Placed {
lines: vec![
[[x - 20., y0], [x + 1., y0]],
[[x - 20., y1], [x + 1., y1]],
[[x, y0], [x, y1]],
],
arrows: vec![[[x, y0], [x, y0 + 2.7]], [[x, y1], [x, y1 - 2.7]]],
label_at: [x - 1., (y0 + y1) / 2.],
rotation: 90.,
label_box: [x - 3., y0 + 2., x - 1., y0 + 8.],
};
let a = vertical(236.58, 197.27, 213.5);
let b = vertical(236.58, 197.27, 216.67);
let apart = vertical(243.58, 197.27, 216.67);
assert_eq!(shared_lines(&[Some(a.clone()), Some(b.clone())]), 1);
assert_eq!(shared_lines(&[Some(a.clone()), None, Some(apart)]), 0);
let next = vertical(236.58, 213.5, 230.);
assert_eq!(shared_lines(&[Some(a), Some(next)]), 0);
}
#[test]
fn a_near_face_start_moves_only_the_witness_starts_beyond_the_body() {
let c = LinearStroke {
lines: [
[[58., 10.], [20., 10.]],
[[220., 80.], [20., 80.]],
[[22., 10.], [22., 80.]],
],
arrows: [[[22., 10.], [22., 12.7]], [[22., 80.], [22., 77.3]]],
label_at: [21., 45.],
rotation: 90.,
label_box: [19., 40., 21., 50.],
};
let outline = [60., 0., 240., 100.];
let near = near_face_start(&c, outline, Side::Left).unwrap();
assert_eq!(
near.lines[0], c.lines[0],
"a start already beyond the face stays"
);
assert_eq!(near.lines[1][0], [60. - GAP, 80.]);
assert_eq!(near.lines[1][1], c.lines[1][1]);
assert_eq!(near.lines[2], c.lines[2]);
assert_eq!(near.arrows, c.arrows);
assert_eq!(near.label_box, c.label_box);
let mut short = c;
short.lines[1][1][0] = 60. - GAP - 0.5;
assert!(near_face_start(&short, outline, Side::Left).is_none());
}
#[test]
fn oblique_arrow_clearance_uses_the_actual_witness_corridor() {
let witness = [[0., 0.], [10., 10.]];
assert!(!touches_witness([[1., 8.], [2., 8.]], witness, 0.2));
assert!(touches_witness([[4., 4.1], [6., 6.1]], witness, 0.2));
assert!(touches_witness([[10., 10.1], [10.1, 10.1]], witness, 0.2));
assert!(!touches_witness([[4., 4.4], [6., 6.4]], witness, 0.2));
}
#[test]
fn automatic_chains_share_lanes_in_both_page_directions() {
let a: Analysis = serde_json::from_value(serde_json::json!({
"schema": crate::model::ANALYSIS_SCHEMA,
"binding": {"source_sha256":"", "part":"plate", "face_count":0, "loader":"test"},
"envelope":[[0,0,0],[100,60,100]], "iso_bounds":[0,0,100,100]
}))
.unwrap();
let frames = BTreeMap::from([(
"front",
Frame::looking(&a, [0., -1., 0.], [0., 0., 1.], [0., 0.], 1.),
)]);
for axis in [0, 2] {
let want = |lo: f64, hi: f64, text: &str| {
let (mut a, mut b) = ([0.; 3], [0.; 3]);
a[axis] = lo;
b[axis] = hi;
Want {
units: Default::default(),
id: format!("{lo}-{hi}"),
views: vec!["front"],
value: (hi - lo).abs(),
terms: vec![text.into()],
measurements: vec![],
basis: String::new(),
turning: None,
shape: Shape::Linear {
a,
b,
axis,
a_edge: false,
b_edge: false,
order: 1,
},
}
};
let items = [
want(0., 20., "20"),
want(80., 50., "30"),
want(20., 50., "30"),
want(0., 80., "80"),
want(80., 82., "2"),
];
let (_, lane) = lanes(
&a,
&frames,
items.iter().map(|w| (w, Some("front"))),
&[None; 5],
);
assert_eq!(lane[0], lane[1]);
assert_eq!(lane[0], lane[2]);
assert_ne!(lane[0], lane[3]);
assert_ne!(lane[0], lane[4]);
assert_eq!(
chain_lanes(&frames["front"], &items.iter().collect::<Vec<_>>()),
[0, 0, 0, 1, 2]
);
}
}
#[test]
fn front_left_lanes_do_not_reserve_the_plan_band() {
let front = Some("front");
let bands = super::band_lanes([((&front, Some(super::Side::Left)), 2)].into_iter());
assert_eq!(bands.plate_front_left, 3);
assert_eq!(bands.plan_left, 0);
}
use super::*;
#[test]
fn slides_are_bounded_by_the_sheet() {
let drawable = [10.0, 10.0, 584.0, 410.0];
let (y, len) = (50.0, 2.0e8);
let p = Placed {
lines: vec![
[[0.0, 40.0], [0.0, 52.0]],
[[len, 40.0], [len, 52.0]],
[[0.0, y], [len, y]],
],
arrows: vec![],
label_at: [100.0, y + LIFT],
rotation: 0.0,
label_box: [100.0, y + LIFT, 110.0, y + LIFT + CAP],
};
let bound = (2.0 * (drawable[2] - drawable[0]) / SLIDE_STEP) as usize + 4;
let places: Vec<Placed> = slides(&p, drawable).take(bound + 1).collect();
assert!(
!places.is_empty() && places.len() <= bound,
"{}",
places.len()
);
assert!(
places
.iter()
.all(|c| c.label_box[0] >= drawable[0] && c.label_box[2] <= drawable[2])
);
assert_eq!(places[0].label_box[0], 100.0 + SLIDE_STEP);
}
#[test]
fn an_arrowhead_and_a_leader_through_other_ink_are_reported() {
let analysis: Analysis = serde_json::from_value(serde_json::json!({
"schema": "draftwright.analysis/5",
"binding": {"source_sha256": "", "part": "", "face_count": 0, "loader": ""},
"envelope": [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]],
"iso_bounds": [0.0, 0.0, 0.0, 0.0]
}))
.unwrap();
let sheet: PlannedSheet = serde_json::from_value(serde_json::json!({
"size": "A4", "page": [297.0, 210.0], "scale": "1:1", "scale_factor": 1.0,
"arrangement": "columns", "title_block": [200.0, 10.0, 287.0, 40.0],
"default": true, "basis": "", "state": { "kind": "shown" }, "title": {}
}))
.unwrap();
let item = |id: &str, kind: &str, placed: Placed| PlannedDimension {
callout_offset: None,
reference: false,
determined_by: Vec::new(),
region: None,
id: id.into(),
kind: kind.into(),
view: "v:plan".into(),
value: 1.0,
label: id.into(),
terms: vec![id.into()],
measurements: vec![],
default: true,
from_spec: vec![],
basis: String::new(),
state: crate::items::State::Shown,
placed: Some(placed),
};
let dim = item(
"d:a",
"linear",
Placed {
lines: vec![[[30.0, 100.0], [55.0, 100.0]]],
arrows: vec![[[55.0, 100.0], [52.3, 100.0]]],
label_at: [35.0, 101.0],
rotation: 0.0,
label_box: [35.0, 101.0, 45.0, 103.0],
},
);
let label = item(
"d:b",
"linear",
Placed {
lines: vec![],
arrows: vec![],
label_at: [50.0, 99.0],
rotation: 0.0,
label_box: [50.0, 99.0, 60.0, 101.5],
},
);
let callout = item(
"d:c",
"callout",
Placed {
lines: vec![
[[40.0, 90.0], [42.0, 110.0]],
[[42.0, 110.0], [45.0, 110.0]],
],
arrows: vec![[[40.0, 90.0], [40.3, 92.7]]],
label_at: [46.0, 109.0],
rotation: 0.0,
label_box: [46.0, 109.0, 56.0, 111.5],
},
);
let problems = collisions(&analysis, &[dim, label, callout], &sheet, &[]);
let has = |code: &str, a: &str, b: &str| {
problems
.iter()
.any(|p| p.code == code && p.subjects == [a.to_string(), b.to_string()])
};
assert!(has("annotation_ink_overlap", "d:a", "d:b"), "{problems:#?}");
assert!(
has("annotation_leader_crossing", "d:c", "d:a"),
"{problems:#?}"
);
assert!(!has("annotation_leader_crossing", "d:a", "d:c"));
}
#[test]
fn a_callouts_arrowhead_flare_is_collision_evidence() {
let leader = |tip: P2, elbow: P2| Placed {
lines: vec![[tip, elbow], [elbow, [elbow[0] + 10.0, elbow[1]]]],
arrows: vec![arrow(tip, elbow)],
label_at: [elbow[0] + 1.0, elbow[1] + 0.5],
rotation: 0.0,
label_box: [
elbow[0] + 1.0,
elbow[1] + 0.5,
elbow[0] + 9.0,
elbow[1] + 3.0,
],
};
let other = |b: [f64; 4]| Placed {
lines: vec![],
arrows: vec![],
label_at: [b[0], b[1]],
rotation: 0.0,
label_box: b,
};
let up = leader([50.0, 50.0], [50.0, 80.0]);
assert!(ink_through(&up, [50.3, 51.0, 60.0, 53.0]));
assert!(
!up.lines
.iter()
.any(|s| crosses(*s, [50.3, 51.0, 60.0, 53.0]))
);
assert!(!ink_through(&up, [50.3, 60.0, 60.0, 62.0]));
assert!(ink_through(&up, [49.9, 52.0, 50.1, 52.2]));
let analysis: Analysis = serde_json::from_value(serde_json::json!({
"schema": "draftwright.analysis/5",
"binding": {"source_sha256": "", "part": "", "face_count": 0, "loader": ""},
"envelope": [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]],
"iso_bounds": [0.0, 0.0, 0.0, 0.0]
}))
.unwrap();
let sheet: PlannedSheet = serde_json::from_value(serde_json::json!({
"size": "A4", "page": [297.0, 210.0], "scale": "1:1", "scale_factor": 1.0,
"arrangement": "columns", "title_block": [200.0, 10.0, 287.0, 40.0],
"default": true, "basis": "", "state": { "kind": "shown" }, "title": {}
}))
.unwrap();
let item = |id: &str, kind: &str, placed: Placed| PlannedDimension {
callout_offset: None,
reference: false,
determined_by: Vec::new(),
region: None,
id: id.into(),
kind: kind.into(),
view: "v:plan".into(),
value: 1.0,
label: id.into(),
terms: vec![id.into()],
measurements: vec![],
default: true,
from_spec: vec![],
basis: String::new(),
state: crate::items::State::Shown,
placed: Some(placed),
};
let problems = collisions(
&analysis,
&[
item("d:c", "callout", up),
item("d:l", "linear", other([50.3, 51.0, 60.0, 53.0])),
],
&sheet,
&[],
);
let overlap = problems
.iter()
.find(|p| p.code == "annotation_ink_overlap")
.expect("the flare's contact is reported");
assert_eq!(overlap.subjects, ["d:c", "d:l"]);
assert_eq!(overlap.ink_is_leader, Some(false));
assert!(
overlap.message.starts_with("The arrowhead of"),
"{}",
overlap.message
);
}
#[test]
fn an_overlapping_pair_has_one_diagnostic_with_a_consistent_remedy() {
use serde_json::json;
let analysis: Analysis = serde_json::from_value(json!({
"schema": "draftwright.analysis/5",
"binding": {"source_sha256": "", "part": "", "face_count": 0, "loader": ""},
"envelope": [[0., 0., 0.], [1., 1., 1.]], "iso_bounds": [0., 0., 0., 0.]
}))
.unwrap();
let sheet: PlannedSheet = serde_json::from_value(json!({
"size": "A4", "page": [297., 210.], "scale": "1:1", "scale_factor": 1.,
"arrangement": "columns", "title_block": [200., 10., 287., 40.],
"default": true, "basis": "", "state": {"kind": "shown"}, "title": {}
}))
.unwrap();
let item = |id: &str, label_box: [f64; 4], lines: Vec<[P2; 2]>| PlannedDimension {
callout_offset: None,
id: id.into(),
kind: "linear".into(),
view: "v:plan".into(),
value: 1.,
label: id.into(),
terms: vec![id.into()],
measurements: vec![],
default: true,
from_spec: vec![],
basis: String::new(),
state: crate::items::State::Shown,
reference: false,
determined_by: vec![],
region: None,
placed: Some(Placed {
lines,
arrows: vec![],
label_at: [label_box[0], label_box[1]],
rotation: 0.,
label_box,
}),
};
for crossing in [false, true] {
let a = item(
"d:a",
[10., 10., 20., 12.2],
vec![[
[0., if crossing { 11. } else { 40. }],
[40., if crossing { 11. } else { 40. }],
]],
);
let b = item(
"d:b",
[12., 10.5, 22., 12.7],
vec![[[30., 30.], [31., 30.]]],
);
for items in [vec![a.clone(), b.clone()], vec![b.clone(), a.clone()]] {
let found: Vec<_> = collisions(&analysis, &items, &sheet, &[])
.into_iter()
.filter(|p| {
matches!(
p.code.as_str(),
"annotation_overlap" | "annotation_ink_overlap"
)
})
.collect();
assert_eq!(found.len(), 1, "{found:?}");
assert_eq!(found[0].code, "annotation_overlap");
assert_eq!(found[0].message.contains("move what is drawn"), crossing);
assert_eq!(
found[0]
.message
.contains("separating the text is not enough"),
crossing
);
assert!(
found[0].fix.is_empty(),
"no unsupported edit is offered as a remedy"
);
}
}
}
}