Files
solidcraft/crates/sketch/src/dims.rs
DanielandClaude Opus 5.5 4d738cd210 Sketch: an ellipse is drawn with its axes, and D measures its half-axes (#9291) (#9292)
* Sketch: an ellipse comes with its axes, and D measures its half-axes

An ellipse could only be sized by its minor radius, drawn like a circle's
radius, and there was no way to give its full axes. sketch.ellipse now also
adds the major and minor axes as construction lines through the centre.
The major axis runs from the major end to the point opposite it; the minor
one is perpendicular to it, ends on the ellipse, and both are bisected by
the centre. That adds six coordinates and six equations, so the ellipse
keeps its five degrees of freedom, and it needs no new constraint kind and
no format change.

With the axes in place every ellipse size is an ordinary dimension. D on an
axis line gives its length, the whole axis. D on the outline gives a half-
axis, the distance from the centre to an axis end, chosen the way a line
chooses horizontal or vertical but in the ellipse's own frame: text pulled
across the major axis measures along it (the major half-axis), text pulled
out past an end of it measures across it (the minor one). The end on the
text's side is used. Near the boundary between the two, a dimension being
placed keeps the half-axis it had (sketch.dimension's previous_axis, which
the Dimension tool sends), so the preview does not flip. A half-axis and
its whole axis determine each other, so the second becomes a driven
dimension through the existing redundancy path.

Deleting the ellipse leaves its axes, as deleting a Center Rectangle's
edges leaves its construction diagonals. Ellipses without axes, from
existing designs, keep the minor radius as before, and type radius still
gives it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Guide: an ellipse's axes and half-axis dimensions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: test that an ellipse's half-axis preview keeps its axis near the boundary

The Dimension tool sends the half-axis its last preview chose, so near the
boundary between the major and the minor half-axis the preview keeps it.
This covers that path from the tool, from both sides of the boundary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's axes select and drag with it, as a rectangle's diagonals do

A double-click on a Center Rectangle's edge or diagonal selects its edges
and both diagonals, and dragging a diagonal moves the rectangle without
changing it, because its lines share corner points. An ellipse's axes are
held by constraints instead, so a double-click selected only the ellipse
(and Delete then left the axes behind), and dragging the major axis moved
the ellipse but stretched its minor half-axis.

Sketch::ellipse_axes now finds an ellipse's construction axes (the
detection sketch.dimension already used, moved into the sketch crate).
Sketch::chain treats an ellipse and its two axes as joined, so a
double-click on any of them selects all three. Dragging either axis moves
the ellipse and both axes as one, keeping both half-axes. Dragging the
outline still sets the minor half-axis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a corner test reads its line's id instead of assuming l1

sketch.ellipse now adds two axis lines, which take the first line ids, so
the extend test reads the id of the line it draws from the reply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's axes cut a trim, as a Center Rectangle's diagonals do

Trim, extend and break take every other curve as a cutter, construction
lines included, so an ellipse's construction axes stop them as a Center
Rectangle's diagonals do. A test now states it for both shapes, and the
guide says it. The freeform cutter test keeps the axes it used to delete
and asserts the piece a trim removes: from where the major axis crosses
the circle to where the ellipse does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: test dragging an ellipse's outline with its axes present

Dragging the outline sets the minor half-axis, and the minor axis line,
held on the ellipse at one end, follows it; the centre and the major
half-axis stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: other dimension types on an ellipse with axes keep the minor radius

Only auto, major and minor give a half-axis on an ellipse drawn with its
axes. Any other type (radius, diameter, horizontal and the rest) gives
what it gives on an ellipse without axes, as the parameter doc says,
instead of quietly becoming a half-axis chosen by the text. The arm for
a missing text_at is dropped: with no text the rule already picks the
minor half-axis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: test an ellipse's half-axis dead zone from the major side too

The tests only showed a previous minor half-axis being kept near the
boundary; the point they used on the major side is major by geometry
alone. A point inside the band where geometry says minor now shows a
previous major half-axis kept, in the engine and in the Dimension
tool's preview.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a placed ellipse half-axis is the one its preview showed

On an ellipse already sized, a half-axis is added as a driven dimension
on the click, and that command left out the half-axis the preview kept
near the boundary, so the click could place the other one. It now
passes it as the preview does.

The remembered half-axis also comes only from a draft whose text was
placed (one without text is the minor half-axis by default and biased
the first placement), is kept per sketch as well as per picks, and is
forgotten once a dimension is placed or the picks are cleared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: say how an ellipse's axes are recognised and what that costs

The axes are read from the constraints, with no mark in the design, so
the doc comment says which lines count, that deleting the midpoint or
point-on-curve constraint leaves an ellipse without axes, and that each
lookup scans curves times constraints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse with one axis deleted still picks its half-axis by the text

With one construction axis deleted, D on the ellipse fell back to the minor radius wherever the
text was pulled, because the half-axis choice needed both axes. It now reads what is left of
them and chooses as before: the major half-axis goes to the major end (also from the far side,
when the point opposite is gone with its line), the minor one to its axis end, or as the minor
radius when the minor axis is the one deleted. An ellipse with neither axis keeps the minor
radius.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the one-axis half-axis test checks what is measured to

The test for an ellipse with one axis deleted checked only the reply's axis and value, which
both ends of an axis give alike. It now checks the dimension's kind and end: the axis end on
the text's side, the major end from either side once the point opposite is gone, a radius once
the minor axis is. With the major axis deleted and no text the reply names the minor axis and
measures to its end, where it used to be a radius with no axis named.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's minor radius is drawn as its half-axis, without R

A radius on an ellipse is the minor half-axis, and it was drawn as a circle's radius: one
leader from the centre towards the text, marked R, which measures nothing a reader can see.
It is now drawn as the distance from the centre to the minor axis end on the text's side,
the way the half-axis is drawn while the ellipse has its axes, and labelled with the length
alone. This is what D gives once the minor axis line is deleted, and on an ellipse with no
axes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's half-axis keeps a radius's stored text

The half-axis frame read the stored text from the middle of the half-axis, so a radius placed
on an ellipse in an existing design would show its text somewhere else. The text is stored from
the centre again, as a radius's always was, and the side drawn to follows it. An unplaced one
sits where a linear dimension to the minor axis end puts its text, and stacked texts step out
from the centre as a radius's do. A view test reads the painted label, so the drawing cannot
go back to R unnoticed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse moved, turned or scaled takes its axes along

Move, Sketch Transform and Scale moved only the curves picked, so an ellipse moved without its
axes left them where they were: its centre no longer halved them, the minor end left the
ellipse, and the sketch stayed unsolved. A Center Rectangle's diagonals share its corners and
always go along; an ellipse's axes share only the major end. The axes now go with the ellipse,
as they do when it is dragged. Copy is unchanged: it copies no constraints, for a rectangle
either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a preference draws ellipses without their axes, as before

Drawing an ellipse with its construction axes changes what the Ellipse tool makes out of the
box, so the old ellipse stays one setting away: Preferences › Sketch › Draw ellipses with their
axes, on by default. Off, the tool draws the ellipse alone. sketch.ellipse takes axes: false
for the same, and keeps drawing the axes when it is not given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: sketch.ellipse refuses an axes value that is not true or false

A string such as "false" was read as not given, so the axes were drawn; it is now refused as
the text commands refuse a non-boolean bold or italic. The move test also covers an ellipse
whose minor axis was deleted: the major axis left goes along.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: D picks a half-axis on an ellipse without axes too

An ellipse with no axis lines (both deleted, a mirrored copy, one from an older design) gave the
minor radius wherever the text was pulled, so its major half-axis could not be dimensioned on
it. The half-axis is now chosen by where the text is on every ellipse: the major one to the
ellipse's own major end, the minor one as its minor radius. With no text it is the minor
radius, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: with one half-axis dimensioned, D on the ellipse gives the other

D on an ellipse chose the half-axis by where the text was pulled even when that half-axis was
already held by a dimension, so the second D usually guessed the one already there and was
refused as over-constrained. With a driving dimension already on one half-axis (to an axis
end, the minor radius, or the whole axis along its line or between its ends), D with no type
now gives the other, in either order and with or without the axis lines. With both held it
chooses as before; an explicit major or minor is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the held half-axis rule is documented and its cases tested

The Dimension command's parameters and the guide now say that with one half-axis held, D on
the ellipse gives the other. Tests cover the whole axis held as a distance between its ends
and as its line's length with the other axis deleted, and check that a mirrored ellipse has
no axes. The placement test undoes its first dimension, so its second half still tests where
the text is rather than the held half-axis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: Transform previews an ellipse with its axes

OK in Sketch Transform moves, turns and scales an ellipse's axes with it, but the window drew
where OK would leave only the picked curves, so the axes stayed put in the preview until OK. An
ellipse picked alone is now drawn with the axis lines it has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the typed-radius ellipse test counts the ellipse's axes

Main's #10282 test drew an ellipse with typed radii and expected one curve; with the
ellipse_axes preference on by default the tool also draws the two axes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's remaining axis still drags and selects it

With one axis deleted, dragging the other axis did not hold the ellipse (with the minor axis
left, the drag squashed it), and a double-click on the ellipse or that axis selected only the
one curve. The drag and Sketch::chain now read whichever axes are left through
ellipse_axis_lines, as Transform and Move already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: drop the unreachable third arm of the held half-axis check

A distance from the centre to the major end or the point opposite it is already matched by the first arm, so the third arm and its major_end closure never ran.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: D on an ellipse's axis line is the whole axis wherever the value is pulled

An axis line took the free line's rule, so on a turned ellipse a value pulled beside or above the box the axis spans gave its horizontal or vertical distance. An ellipse's axis is a construction axis, so auto now always gives its length, which the held half-axis check already counts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's axis is never a side of a Fillet or Chamfer corner

A line drawn from an ellipse's major end shares that point with the major axis, so Fillet or Chamfer there took the two as a corner (two lines skip the construction filter) and rounded the line into the axis. The axes are left out of a corner, picked by its point or as a and b.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: test the move reply, extend and the one-axis Transform preview for ellipse axes

The reply of sketch.move, sketch.transform and sketch.scale lists the axis lines, Extend stops at an axis, and Sketch Transform previews an ellipse with the one axis left after the other was deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: with Draw ellipses with their axes off, D on an ellipse is its minor radius as before

The preference brought back the old drawing only; D on an ellipse had changed with no way back. sketch.dimension takes half_axes (default true, refused when not true or false); false makes auto on an ellipse the minor radius wherever the text is. The Dimension tool and the context menu's Sketch Dimension pass it from the preference, and with it off a radius on an ellipse is drawn and labelled R as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: read an ellipse's axes in one pass over the constraints

ellipse_axis_lines scanned every constraint for every curve, so 500 ellipses with their axes (1500 curves, 2000 constraints) took 1.6 s to read and Move on all of them 2.0 s. It now collects the lines the centre bisects first: 3 ms and 0.36 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the dimension and corner parameters say how an ellipse's axis lines take part

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Preferences: Draw ellipses with their axes says D gives the minor radius when off

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: ask only the ellipses centred on a line's midpoint whether it is their axis

is_ellipse_axis read every ellipse's axes to answer for one line. An axis is bisected by its ellipse's centre, so it now asks only the ellipses centred on a point that bisects the line. The answer is the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a horizontal or vertical dimension across a level major axis holds it for D on the ellipse

half_axes_held counted a distance, a length and a radius, but not a horizontal or vertical dimension across the major axis, which is its length when the axis lies that way. D on the outline could then take the held major half-axis and be refused as determined; it now gives the minor one. Across a slanted axis such a dimension is only a projection and does not count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Dimension: a draft on an ellipse is drawn with the axes preference it was made with

The frame read the preference as it is now, while the dimension added is the draft's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a horizontal or vertical dimension across an upright minor axis holds it too

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: drop the unused Sketch::ellipse_axes; ellipse_axis_lines says how the axes are read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse's own axes do not cut it, so Trim takes a lone ellipse whole and stops at real crossings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: Fillet and Chamfer offer no corner at an ellipse's axis end, as the engine takes none

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a deleted ellipse leaves its centre on its axes, as a Center Rectangle's centre stays

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: ellipse axis docs say what they do, and the corner picker tests a curve's ends before its axis role

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the trim-at-a-crossing test checks that part of the ellipse is left

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: an ellipse axis moved, turned or scaled on its own takes its ellipse along, as a drag does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: a selection dragged by an ellipse axis takes the ellipse along, as the axis alone does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Docs: the Sketch preferences page lists Draw ellipses with their axes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Sketch: the ellipse axes help text says D gives the minor radius, drawn and labelled R as before, wherever the value is pulled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 16:54:46 +00:00

618 lines
33 KiB
Rust

//! Where a sketch dimension is drawn: extension lines from the measured geometry, the dimension
//! line (or arc) with its arrows, and the value text. Shared by the UI (drawing and hit testing)
//! and the engine (storing a dragged text position), all in sketch coordinates.
//!
//! A dimension's text position is kept relative to its own frame ([`Constraint::text`]), so it
//! follows the geometry: for a linear dimension it is (along, across) from the middle of the
//! measured points, for the others an offset from the centre or the vertex.
//!
//! [`Constraint::text`]: crate::Constraint::text
use solidcore_record::Vec2;
use solidcraft_geom::prelude::*;
use crate::model::{ConstraintKind, CurveKind, Sketch, TwoCircles};
use crate::union_find::{find, union};
/// What a dimension measures, in a form that can be drawn.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DimFrame {
/// The distance from `p0` to `p1` along the unit direction `dir` (aligned, horizontal or
/// vertical).
Linear { p0: Vec2, p1: Vec2, dir: Vec2 },
/// Radius or diameter of a circle or arc. `arc` is the arc's (start angle, counter-clockwise
/// sweep), None for a full circle: an arrow past the arc's ends gets an extension of the arc.
Radial { center: Vec2, radius: f64, diameter: bool, arc: Option<(f64, f64)> },
/// The angle at `vertex` from the unit ray `d0`, counter-clockwise by `sweep` radians.
Angular { vertex: Vec2, d0: Vec2, sweep: f64 },
/// Length along an arc around `center` from angle `start` by `sweep`.
ArcLength { center: Vec2, radius: f64, start: f64, sweep: f64 },
/// An ellipse's minor half-axis: `len` from `center` along the unit `dir` or against it,
/// drawn as a linear dimension to the minor axis end on the text's side, as the distance
/// to that end is when the ellipse has its axes. The text is stored from `center`, as a
/// radius's is, so a radius placed before it was drawn this way keeps its text.
HalfAxis { center: Vec2, dir: Vec2, len: f64 },
}
/// A drawn dimension (sketch coordinates).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DimLayout {
/// Extension lines, dimension lines and arcs as polylines.
pub lines: Vec<Vec<Vec2>>,
/// Arrowheads: tip and the unit direction they point in.
pub arrows: Vec<(Vec2, Vec2)>,
/// Centre of the value text.
pub text: Vec2,
}
fn line_ends(sk: &Sketch, l: usize) -> Option<(Vec2, Vec2)> {
match sk.curves.get(l)?.kind {
CurveKind::Line { a, b } => Some((sk.point(a)?, sk.point(b)?)),
_ => None,
}
}
/// The frame of a dimension constraint (None for geometric constraints).
pub fn dim_frame(sk: &Sketch, k: &ConstraintKind) -> Option<DimFrame> {
use ConstraintKind::*;
let unit = |v: Vec2, or: Vec2| v.normalized().unwrap_or(or);
Some(match *k {
OffsetDistance { ref output, .. } => {
let (p0, p1) = sk.offset_dimension_points(output)?;
DimFrame::Linear { p0, p1, dir: unit(p1 - p0, Vec2::X) }
}
Distance { p, q, .. } => {
let (a, b) = (sk.point(p)?, sk.point(q)?);
DimFrame::Linear { p0: a, p1: b, dir: unit(b - a, Vec2::X) }
}
DistanceX { p, q, .. } => DimFrame::Linear { p0: sk.point(p)?, p1: sk.point(q)?, dir: Vec2::X },
DistanceY { p, q, .. } => DimFrame::Linear { p0: sk.point(p)?, p1: sk.point(q)?, dir: Vec2::Y },
Length { l, .. } => {
let (a, b) = line_ends(sk, l)?;
DimFrame::Linear { p0: a, p1: b, dir: unit(b - a, Vec2::X) }
}
LineAngle { l, value } => {
let (a, b) = line_ends(sk, l)?;
let d = unit(b - a, Vec2::X);
if value < 0.0 {
DimFrame::Angular { vertex: a, d0: d, sweep: (-value).rem_euclid(std::f64::consts::TAU) }
} else {
DimFrame::Angular { vertex: a, d0: Vec2::X, sweep: value.rem_euclid(std::f64::consts::TAU) }
}
}
ArcSweep { c, .. } => match sk.segs(c).first()? {
solidcore_record::Seg2::Arc { center, start, sweep, .. } => {
DimFrame::Angular { vertex: *center, d0: Vec2::from_angle(*start), sweep: *sweep }
}
_ => return None,
},
SlotOverallLength { a, b, .. } => {
let (ca, cb) = (sk.center(a)?, sk.center(b)?);
let dir = unit(cb - ca, Vec2::X);
DimFrame::Linear { p0: ca - dir * sk.radius(a)?, p1: cb + dir * sk.radius(b)?, dir }
}
PointLineDistance { p, l, .. } | LinearDiameter { p, l, .. } => {
let (a, b) = line_ends(sk, l)?;
let q = sk.point(p)?;
let d = unit(b - a, Vec2::X);
let foot = a + d * (q - a).dot(d);
let p0 = if matches!(k, LinearDiameter { .. }) { foot * 2.0 - q } else { foot };
DimFrame::Linear { p0, p1: q, dir: unit(q - foot, d.perp()) }
}
LineCircleDistance { l, c, .. } => {
// From the line to the circle's edge point nearest it (beyond it when the line cuts
// the circle), along the line's normal.
let (a, b) = line_ends(sk, l)?;
let (centre, r) = (sk.center(c)?, sk.radius(c)?);
let d = unit(b - a, Vec2::X);
let normal = unit(centre - (a + d * (centre - a).dot(d)), d.perp());
let p1 = centre - normal * r;
let p0 = a + d * (p1 - a).dot(d);
DimFrame::Linear { p0, p1, dir: unit(p1 - p0, normal) }
}
PointCircleDistance { p, c, .. } => {
// From the circle's edge, where the radius through the point crosses it, to the point.
let q = sk.point(p)?;
let (centre, r) = (sk.center(c)?, sk.radius(c)?);
let u = unit(q - centre, Vec2::X);
let edge = centre + u * r;
DimFrame::Linear { p0: edge, p1: q, dir: unit(q - edge, u) }
}
CircleCircleDistance { a, b, circles, .. } => {
// Along the line through the centres, between the edge points that face each other:
// for nested circles, where the inner one comes nearest the outer one.
let ((ca, ra), (cb, rb)) = ((sk.center(a)?, sk.radius(a)?), (sk.center(b)?, sk.radius(b)?));
let u = unit(cb - ca, Vec2::X);
let (p0, p1) = match circles {
TwoCircles::Nested if ra >= rb => (cb + u * rb, ca + u * ra),
TwoCircles::Nested => (ca - u * ra, cb - u * rb),
TwoCircles::Apart | TwoCircles::Crossing => (ca + u * ra, cb - u * rb),
};
DimFrame::Linear { p0, p1, dir: u }
}
Radius { c, .. } | Diameter { c, .. } => {
if let (Radius { .. }, CurveKind::Ellipse { c: centre, m, r }) = (k, &sk.curves.get(c)?.kind) {
let center = sk.point(*centre)?;
return Some(DimFrame::HalfAxis { center, dir: unit(sk.point(*m)? - center, Vec2::X).perp(), len: *r });
}
let (radius, arc) = match sk.curves.get(c)?.kind {
CurveKind::Ellipse { r, .. } => (r, None),
CurveKind::Arc { .. } => match sk.segs(c).first()? {
solidcore_record::Seg2::Arc { radius, start, sweep, .. } => (*radius, Some((*start, *sweep))),
_ => return None,
},
_ => (sk.radius(c)?, None),
};
DimFrame::Radial { center: sk.center(c)?, radius, diameter: matches!(k, Diameter { .. }), arc }
}
ArcLength { c, .. } => match sk.segs(c).first()? {
solidcore_record::Seg2::Arc { center, radius, start, sweep } => {
DimFrame::ArcLength { center: *center, radius: *radius, start: *start, sweep: *sweep }
}
_ => return None,
},
Angle { a, b, flip, .. } => {
let (a0, a1) = line_ends(sk, a)?;
let (b0, b1) = line_ends(sk, b)?;
let (da, mut db) = (unit(a1 - a0, Vec2::X), unit(b1 - b0, Vec2::Y));
if flip {
db = -db;
}
let den = da.cross(db);
let (ma, mb) = ((a0 + a1) * 0.5, (b0 + b1) * 0.5);
let vertex = if den.abs() < 1e-12 { (ma + mb) * 0.5 } else { a0 + da * ((b0 - a0).cross(db) / den) };
// The angle from `da` to `db` equals the one from `-da` to `-db`: draw the pair of
// rays that point at the lines themselves.
let score = |s: f64| (ma - vertex).dot(da * s) + (mb - vertex).dot(db * s);
let s = if score(1.0) >= score(-1.0) { 1.0 } else { -1.0 };
let (d0, d1) = (da * s, db * s);
DimFrame::Angular { vertex, d0, sweep: d0.cross(d1).atan2(d0.dot(d1)).rem_euclid(std::f64::consts::TAU) }
}
_ => return None,
})
}
/// The stored text position for text centred at `at`.
pub fn encode_text(f: &DimFrame, at: Vec2) -> Vec2 {
match *f {
DimFrame::Linear { p0, p1, dir } => {
let m = (p0 + p1) * 0.5;
Vec2::new((at - m).dot(dir), (at - m).dot(dir.perp()))
}
DimFrame::Radial { center, .. } | DimFrame::ArcLength { center, .. } => at - center,
DimFrame::Angular { vertex, .. } => at - vertex,
DimFrame::HalfAxis { center, .. } => at - center,
}
}
/// The linear frame a [`DimFrame::HalfAxis`] is drawn as for the stored text `t`: to the end
/// on the text's side, with `t` re-expressed for that frame.
fn half_axis_side(center: Vec2, dir: Vec2, len: f64, t: Vec2) -> (DimFrame, Vec2) {
let dir = if t.dot(dir) < 0.0 { -dir } else { dir };
let linear = DimFrame::Linear { p0: center, p1: center + dir * len, dir };
(linear, encode_text(&linear, center + t))
}
/// Per point: the centre of the connected geometry it belongs to (points joined by curves),
/// which a dimension's default place keeps away from.
pub fn chain_centres(sk: &Sketch) -> Vec<Vec2> {
let n = sk.points.len();
let mut parent: Vec<usize> = (0..n).collect();
for c in &sk.curves {
let ids = c.kind.point_ids();
for w in ids.windows(2) {
union(&mut parent, w[0], w[1]);
}
}
let mut sum = vec![(Vec2::ZERO, 0usize); n];
let roots: Vec<usize> = (0..n).map(|i| find(&mut parent, i)).collect();
for (i, r) in roots.iter().enumerate() {
if let (Some(s), Some(p)) = (sum.get_mut(*r), sk.points.get(i)) {
s.0 += p.pos;
s.1 += 1;
}
}
roots.iter().map(|r| sum.get(*r).map(|(s, k)| *s / (*k).max(1) as f64).unwrap_or_default()).collect()
}
/// The default text place of a dimension: a linear one on the side of its points away from
/// `away` (the centre of the geometry it measures), the others where [`dim_layout`] puts them.
pub fn default_text(f: &DimFrame, away: Vec2, px: f64) -> Option<Vec2> {
match *f {
DimFrame::Linear { p0, p1, dir } => {
let side = if ((p0 + p1) * 0.5 - away).dot(dir.perp()) < -1e-9 { -1.0 } else { 1.0 };
Some(Vec2::new(0.0, side * 22.0 * px))
}
_ => None,
}
}
/// The text offset [`dim_layout_reaching`] uses when a dimension's text was never placed:
/// frame-relative, as in [`encode_text`].
pub fn default_text_offset(f: &DimFrame, px: f64) -> Vec2 {
match *f {
DimFrame::Linear { .. } => Vec2::new(0.0, 22.0 * px),
// Where a linear dimension to the minor axis end puts it.
DimFrame::HalfAxis { dir, len, .. } => dir * (len * 0.5) + dir.perp() * (22.0 * px),
DimFrame::Radial { radius, .. } => Vec2::new(1.0, 1.0) * ((radius + 18.0 * px) * std::f64::consts::FRAC_1_SQRT_2),
DimFrame::Angular { d0, sweep, .. } => Vec2::from_angle(d0.angle() + sweep * 0.5) * (48.0 * px),
DimFrame::ArcLength { radius, start, sweep, .. } => Vec2::from_angle(start + sweep * 0.5) * (radius + 20.0 * px),
}
}
/// Points along an arc around `c` (counter-clockwise from `a0` by `sweep`).
fn arc_pts(c: Vec2, r: f64, a0: f64, sweep: f64) -> Vec<Vec2> {
let n = ((sweep.abs() / 0.08).ceil() as usize).clamp(2, 200);
(0..=n).map(|i| c + Vec2::from_angle(a0 + sweep * i as f64 / n as f64) * r).collect()
}
/// The arc around `c` from the nearer end of the arc (`start`, counter-clockwise by `sweep`) to
/// angle `at`, and `over` past it; None when `at` lies on the arc.
fn arc_extension(c: Vec2, r: f64, start: f64, sweep: f64, at: f64, over: f64) -> Option<Vec<Vec2>> {
if !(r > 0.0 && r.is_finite() && sweep.is_finite() && at.is_finite()) {
return None;
}
let ta = (at - start).rem_euclid(std::f64::consts::TAU);
if ta <= sweep + 1e-9 {
return None;
}
let past = over / r;
let (after_end, before_start) = (ta - sweep, std::f64::consts::TAU - ta);
Some(if after_end <= before_start { arc_pts(c, r, start + sweep, after_end + past) } else { arc_pts(c, r, at - past, before_start + past) })
}
/// The lines a linear dimension's two extension lines may start from (#1361), for its `p0` and
/// `p1` sides: a dimension to a line reaches the nearest part of that line, not the foot of the
/// measured point, and a dimension between two parallel lines reaches the nearest part of the
/// line whose end it measures from. `None` for a side that measures a point.
pub fn dim_reach(sk: &Sketch, k: &ConstraintKind) -> [Option<(Vec2, Vec2)>; 2] {
use ConstraintKind::*;
let (p, l, to_line) = match *k {
PointLineDistance { p, l, .. } => (p, l, true),
LinearDiameter { p, l, .. } => (p, l, false),
LineCircleDistance { l, .. } => return [line_ends(sk, l), None],
_ => return [None, None],
};
let Some((a, b)) = line_ends(sk, l) else { return [None, None] };
let d = b - a;
// A line ending at `p` that runs parallel to `l`.
let parallel = sk.curves.iter().enumerate().find_map(|(i, c)| match c.kind {
CurveKind::Line { a: pa, b: pb } if i != l && (pa == p || pb == p) => {
let (u, v) = line_ends(sk, i)?;
let e = v - u;
(e.len() > 1e-12 && d.cross(e).abs() <= 1e-9 * d.len() * e.len()).then_some((u, v))
}
_ => None,
});
[to_line.then_some((a, b)), parallel]
}
/// The point of segment `s` nearest `q`.
fn nearest_on(s: (Vec2, Vec2), q: Vec2) -> Vec2 {
let d = s.1 - s.0;
let l2 = d.dot(d);
if !(l2 > 1e-24) {
return s.0;
}
s.0 + d * ((q - s.0).dot(d) / l2).clamp(0.0, 1.0)
}
/// Lay a dimension out. `text` is the stored text position (None: the default place), `px`
/// the sketch length of one screen pixel and `half` the half size of the text box (sketch
/// units), which the dimension line leaves a gap for.
pub fn dim_layout(f: &DimFrame, text: Option<Vec2>, px: f64, half: Vec2) -> DimLayout {
dim_layout_reaching(f, text, px, half, [None, None])
}
/// [`dim_layout`] with the extension lines of a linear dimension starting from the nearest
/// point of the geometry in `reach` ([`dim_reach`]) instead of the measured points.
pub fn dim_layout_reaching(f: &DimFrame, text: Option<Vec2>, px: f64, half: Vec2, reach: [Option<(Vec2, Vec2)>; 2]) -> DimLayout {
let px = if px.is_finite() && px > 0.0 { px } else { 1.0 };
let arrow = 9.0 * px;
let gap = 3.0 * px;
let over = 4.0 * px;
let mut out = DimLayout::default();
match *f {
DimFrame::HalfAxis { center, dir, len } => {
let t = text.unwrap_or_else(|| default_text_offset(f, px));
let (linear, t) = half_axis_side(center, dir, len, t);
return dim_layout_reaching(&linear, Some(t), px, half, reach);
}
DimFrame::Linear { p0, p1, dir } => {
let n = dir.perp();
let m = (p0 + p1) * 0.5;
let t = text.unwrap_or_else(|| default_text_offset(&DimFrame::Linear { p0, p1, dir }, px));
let (s, h) = (t.x, t.y);
// The dimension line sits `h` across from the middle of the measured points.
let q0 = p0 + n * (h - (p0 - m).dot(n));
let q1 = p1 + n * (h - (p1 - m).dot(n));
for ((p, q), r) in [(p0, q0), (p1, q1)].into_iter().zip(reach) {
let p = r.map_or(p, |r| nearest_on(r, q));
let v = q - p;
if let Some(u) = v.normalized() {
out.lines.push(vec![p + u * gap.min(v.len()), q + u * over]);
}
}
out.text = m + n * h + dir * s;
let len = (q1 - q0).dot(dir);
let (lo, hi) = if len >= 0.0 { (q0, q1) } else { (q1, q0) };
let span = len.abs();
// Half the text along the line (it is drawn level on screen).
let along = (half.x * dir.x.abs() + half.y * dir.y.abs()) + gap;
let inside = s.abs() + along <= span * 0.5;
if span > 2.0 * arrow + 2.0 * along && inside {
// Arrows inside, pointing out, the line broken around the text.
out.arrows.push((lo, -dir));
out.arrows.push((hi, dir));
let tc = m + n * h + dir * s;
out.lines.push(vec![lo, tc - dir * along]);
out.lines.push(vec![tc + dir * along, hi]);
} else {
// Too tight (or the text pulled out): arrows outside pointing in, short tails.
out.arrows.push((lo, dir));
out.arrows.push((hi, -dir));
let tail = arrow * 1.8;
out.lines.push(vec![lo - dir * tail, hi + dir * tail]);
// A text outside the extension lines gets a leader along the line.
let tc = m + n * h + dir * s;
let reach = (tc - m - n * h).dot(dir);
if reach.abs() > span * 0.5 + tail {
let end = if reach > 0.0 { hi + dir * tail } else { lo - dir * tail };
out.lines.push(vec![end, tc - dir * (along * reach.signum())]);
}
}
}
DimFrame::Radial { center, radius, diameter, arc } => {
let t = text.unwrap_or_else(|| default_text_offset(&DimFrame::Radial { center, radius, diameter, arc }, px));
let u = t.normalized().unwrap_or(Vec2::new(1.0, 1.0) * std::f64::consts::FRAC_1_SQRT_2);
let d = t.len();
let at = center + u * radius;
out.text = center + t;
// An arrow past the ends of an arc points at the arc's extension (#3393).
if let Some((start, sweep)) = arc {
let tips: &[Vec2] = if diameter { &[u, -u] } else { &[u] };
for tip in tips {
if let Some(ext) = arc_extension(center, radius, start, sweep, tip.angle(), over) {
out.lines.push(ext);
}
}
}
let rim = half.x * u.x.abs() + half.y * u.y.abs() + gap;
let from = if diameter { center - u * radius } else { center };
if d > radius {
// Leader from the far side (or centre) out to the text, arrows on the rim.
out.lines.push(vec![from, center + u * (d - rim).max(radius)]);
out.arrows.push((at, u));
if diameter {
out.arrows.push((center - u * radius, -u));
}
} else {
// Text inside: the line runs rim to rim (or centre to rim) around it.
let end = center + u * radius;
let t0 = (d - rim).max(0.0);
let start_d = if diameter { -radius } else { 0.0 };
if t0 > start_d {
out.lines.push(vec![from, center + u * t0]);
}
if d + rim < radius {
out.lines.push(vec![center + u * (d + rim), end]);
}
out.arrows.push((at, u));
if diameter {
out.arrows.push((center - u * radius, -u));
}
}
}
DimFrame::Angular { vertex, d0, sweep } => {
let a0 = d0.angle();
let t = text.unwrap_or_else(|| default_text_offset(&DimFrame::Angular { vertex, d0, sweep }, px));
let r = t.len().max(arrow);
out.text = vertex + t;
// Extension rays from the vertex out past the arc.
for a in [a0, a0 + sweep] {
let u = Vec2::from_angle(a);
out.lines.push(vec![vertex + u * (r * 0.15).min(gap * 3.0), vertex + u * (r + over)]);
}
let arc_len = r * sweep;
if arc_len > 2.5 * arrow {
out.lines.push(arc_pts(vertex, r, a0, sweep));
out.arrows.push((vertex + Vec2::from_angle(a0) * r, -Vec2::from_angle(a0).perp()));
out.arrows.push((vertex + Vec2::from_angle(a0 + sweep) * r, Vec2::from_angle(a0 + sweep).perp()));
} else {
// A narrow angle: arrows outside the sector pointing in.
let tail = 2.0 * arrow / r;
out.lines.push(arc_pts(vertex, r, a0 - tail, sweep + 2.0 * tail));
out.arrows.push((vertex + Vec2::from_angle(a0) * r, Vec2::from_angle(a0).perp()));
out.arrows.push((vertex + Vec2::from_angle(a0 + sweep) * r, -Vec2::from_angle(a0 + sweep).perp()));
}
// Text beyond the sector: the arc reaches round to it.
let ta = (t.angle() - a0).rem_euclid(std::f64::consts::TAU);
if ta > sweep {
let (from, by) =
if ta - sweep < std::f64::consts::TAU - ta { (a0 + sweep, ta - sweep) } else { (t.angle(), std::f64::consts::TAU - ta) };
out.lines.push(arc_pts(vertex, r, from, by));
}
}
DimFrame::ArcLength { center, radius, start, sweep } => {
let t = text.unwrap_or_else(|| default_text_offset(&DimFrame::ArcLength { center, radius, start, sweep }, px));
let r = t.len().max(arrow);
out.text = center + t;
for a in [start, start + sweep] {
let u = Vec2::from_angle(a);
let (from, to) = if r >= radius { (radius + gap, r + over) } else { (radius - gap, r - over) };
out.lines.push(vec![center + u * from, center + u * to]);
}
out.lines.push(arc_pts(center, r, start, sweep));
out.arrows.push((center + Vec2::from_angle(start) * r, -Vec2::from_angle(start).perp()));
out.arrows.push((center + Vec2::from_angle(start + sweep) * r, Vec2::from_angle(start + sweep).perp()));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: Vec2, b: Vec2) -> bool {
a.dist(b) < 1e-9
}
#[test]
fn linear_dimension_has_extension_lines_and_inward_arrows() {
let f = DimFrame::Linear { p0: Vec2::new(0.0, 0.0), p1: Vec2::new(100.0, 0.0), dir: Vec2::X };
let l = dim_layout(&f, Some(Vec2::new(0.0, 10.0)), 0.1, Vec2::new(2.0, 1.0));
assert!(close(l.text, Vec2::new(50.0, 10.0)));
assert_eq!(l.arrows.len(), 2);
// Arrows at the dimension line ends, pointing outwards.
assert!(close(l.arrows[0].0, Vec2::new(0.0, 10.0)) && close(l.arrows[0].1, -Vec2::X));
assert!(close(l.arrows[1].0, Vec2::new(100.0, 10.0)) && close(l.arrows[1].1, Vec2::X));
// Two extension lines going up from the points, then the line broken around the text.
assert_eq!(l.lines.len(), 4);
assert!(l.lines[0][0].y > 0.0 && l.lines[0][1].y > 10.0);
assert!(l.lines[2][1].x < 48.0 && l.lines[3][0].x > 52.0);
}
#[test]
fn short_linear_dimension_flips_arrows_outside() {
let f = DimFrame::Linear { p0: Vec2::new(0.0, 0.0), p1: Vec2::new(2.0, 0.0), dir: Vec2::X };
let l = dim_layout(&f, None, 0.1, Vec2::new(2.0, 1.0));
assert!(close(l.arrows[0].1, Vec2::X) && close(l.arrows[1].1, -Vec2::X));
}
#[test]
fn text_position_round_trips_through_the_frame() {
let f = DimFrame::Linear { p0: Vec2::new(1.0, 2.0), p1: Vec2::new(31.0, 42.0), dir: Vec2::new(0.6, 0.8) };
let at = Vec2::new(7.0, 30.0);
let enc = encode_text(&f, at);
assert!(close(dim_layout(&f, Some(enc), 0.1, Vec2::ZERO).text, at));
let r = DimFrame::Radial { center: Vec2::new(5.0, 5.0), radius: 3.0, diameter: true, arc: None };
assert!(close(dim_layout(&r, Some(encode_text(&r, at)), 0.1, Vec2::ZERO).text, at));
}
#[test]
fn an_ellipse_s_minor_radius_is_drawn_as_the_distance_to_its_axis_end() {
// A turned ellipse whose minor axis line was deleted: its minor radius is drawn as the
// distance from the centre to the minor axis end would be, on whichever side the text is.
let mut sk = Sketch::new();
let major = Vec2::new(1.0, 1.0) * (20.0 * std::f64::consts::FRAC_1_SQRT_2);
let c = sk.add_point(Vec2::ZERO, None).unwrap_or(0);
let m = sk.add_point(major, None).unwrap_or(0);
let e = sk.add_curve(CurveKind::Ellipse { c, m, r: 8.0 }, None).unwrap_or(0);
let f =
dim_frame(&sk, &ConstraintKind::Radius { c: e, value: 8.0 }).unwrap_or(DimFrame::Linear { p0: Vec2::ZERO, p1: Vec2::ZERO, dir: Vec2::X });
assert!(matches!(f, DimFrame::HalfAxis { .. }), "{f:?}");
// Its text is stored from the centre, as it was while it was drawn as a radius, so a
// design saved then keeps the text where it was placed.
let at = Vec2::new(3.0, 17.0);
let radial = DimFrame::Radial { center: Vec2::ZERO, radius: 8.0, diameter: false, arc: None };
assert!(close(encode_text(&f, at), encode_text(&radial, at)));
assert!(close(dim_layout(&f, Some(encode_text(&radial, at)), 0.1, Vec2::ZERO).text, at));
let minor = Vec2::new(-1.0, 1.0) * (8.0 * std::f64::consts::FRAC_1_SQRT_2);
for end in [minor, minor * -1.0] {
let to_end = DimFrame::Linear { p0: Vec2::ZERO, p1: end, dir: end * (1.0 / 8.0) };
for at in [end * 1.6 + major * 0.2, end * 0.5 - major * 0.9] {
let half = dim_layout(&f, Some(encode_text(&f, at)), 0.1, Vec2::new(2.0, 1.0));
let axis = dim_layout(&to_end, Some(encode_text(&to_end, at)), 0.1, Vec2::new(2.0, 1.0));
let same = |a: &[Vec2], b: &[Vec2]| a.len() == b.len() && a.iter().zip(b).all(|(p, q)| close(*p, *q));
assert!(close(half.text, at) && close(half.text, axis.text), "{at:?}");
assert!(
half.lines.len() == axis.lines.len() && half.lines.iter().zip(&axis.lines).all(|(a, b)| same(a, b)),
"{at:?}: {half:?} {axis:?}"
);
let tips = |l: &DimLayout| l.arrows.iter().map(|a| a.0).collect::<Vec<_>>();
assert!(same(&tips(&half), &tips(&axis)), "{at:?}");
}
}
}
#[test]
fn diameter_has_arrows_on_both_sides() {
let r = DimFrame::Radial { center: Vec2::ZERO, radius: 10.0, diameter: true, arc: None };
let l = dim_layout(&r, Some(Vec2::new(20.0, 0.0)), 0.1, Vec2::new(2.0, 1.0));
assert_eq!(l.arrows.len(), 2);
assert!(close(l.arrows[0].0, Vec2::new(10.0, 0.0)) && close(l.arrows[1].0, Vec2::new(-10.0, 0.0)));
let r = DimFrame::Radial { center: Vec2::ZERO, radius: 10.0, diameter: false, arc: None };
assert_eq!(dim_layout(&r, None, 0.1, Vec2::ZERO).arrows.len(), 1);
}
#[test]
fn an_arrow_past_the_end_of_an_arc_points_at_its_extension() {
// #3393: a quarter arc from 0° to 90°; a radius dimension dragged to 135° gets the arc
// continued from its 90° end round to the arrow, and a little past it.
let mut sk = Sketch::new();
let c = sk.add_arc(Vec2::ZERO, Vec2::new(10.0, 0.0), Vec2::new(0.0, 10.0), [None, None, None], None).unwrap_or(0);
let f =
dim_frame(&sk, &ConstraintKind::Radius { c, value: 10.0 }).unwrap_or(DimFrame::Linear { p0: Vec2::ZERO, p1: Vec2::ZERO, dir: Vec2::X });
let DimFrame::Radial { arc: Some((start, sweep)), .. } = f else { panic!("an arc's frame keeps its span: {f:?}") };
assert!(start.abs() < 1e-9 && (sweep - std::f64::consts::FRAC_PI_2).abs() < 1e-9);
let on_circle = |line: &Vec<Vec2>| line.len() > 2 && line.iter().all(|p| (p.len() - 10.0).abs() < 1e-9);
let lay = dim_layout(&f, Some(Vec2::from_angle(0.75 * std::f64::consts::PI) * 20.0), 0.1, Vec2::new(2.0, 1.0));
let ext: Vec<_> = lay.lines.iter().filter(|l| on_circle(l)).collect();
assert_eq!(ext.len(), 1, "{:?}", lay.lines);
let (first, last) = (ext[0][0], ext[0][ext[0].len() - 1]);
assert!(first.dist(Vec2::new(0.0, 10.0)) < 1e-9, "starts at the arc's end: {first:?}");
assert!((last.angle() - (0.75 * std::f64::consts::PI + 0.4 / 10.0)).abs() < 1e-9, "ends just past the arrow: {last:?}");
// Dragged before the start (at -45°) the extension runs from the arrow to the start.
let lay = dim_layout(&f, Some(Vec2::from_angle(-0.25 * std::f64::consts::PI) * 20.0), 0.1, Vec2::new(2.0, 1.0));
let ext: Vec<_> = lay.lines.iter().filter(|l| on_circle(l)).collect();
assert_eq!(ext.len(), 1);
assert!(ext[0][ext[0].len() - 1].dist(Vec2::new(10.0, 0.0)) < 1e-9);
// On the arc, or on a full circle, nothing is added.
let on_arc = dim_layout(&f, Some(Vec2::from_angle(0.25 * std::f64::consts::PI) * 20.0), 0.1, Vec2::new(2.0, 1.0));
assert!(!on_arc.lines.iter().any(on_circle));
let circle = DimFrame::Radial { center: Vec2::ZERO, radius: 10.0, diameter: false, arc: None };
assert!(!dim_layout(&circle, Some(Vec2::new(-20.0, 0.0)), 0.1, Vec2::ZERO).lines.iter().any(on_circle));
// A diameter of the arc extends towards both arrows.
let DimFrame::Radial { center, radius, arc, .. } = f else { return };
let dia = DimFrame::Radial { center, radius, diameter: true, arc };
let lay = dim_layout(&dia, Some(Vec2::from_angle(0.75 * std::f64::consts::PI) * 20.0), 0.1, Vec2::new(2.0, 1.0));
assert_eq!(lay.lines.iter().filter(|l| on_circle(l)).count(), 2);
}
#[test]
fn angle_frame_uses_the_rays_toward_the_lines() {
let mut sk = Sketch::new();
let a = sk.add_line(Vec2::new(0.0, 0.0), Vec2::new(10.0, 0.0), None, None, None).unwrap_or(0);
let b = sk.add_line(Vec2::new(0.0, 0.0), Vec2::new(0.0, 10.0), Some(1), None, None).unwrap_or(0);
let k = ConstraintKind::Angle { a, b, value: std::f64::consts::FRAC_PI_2, flip: false };
let Some(DimFrame::Angular { vertex, d0, sweep }) = dim_frame(&sk, &k) else { panic!("no frame") };
assert!(close(vertex, Vec2::ZERO) && close(d0, Vec2::X));
assert!((sweep - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
let l = dim_layout(&DimFrame::Angular { vertex, d0, sweep }, None, 0.1, Vec2::ZERO);
assert_eq!(l.arrows.len(), 2);
assert!(l.text.x > 0.0 && l.text.y > 0.0);
}
#[test]
fn extension_lines_reach_the_nearest_part_of_the_lines_measured() {
// #1361: two parallel lines dimensioned from the low end of one; the dimension placed
// across their middles needs no extension lines, one placed above them reaches their
// top ends, not the low end.
let mut sk = Sketch::new();
let l = sk.add_line(Vec2::new(0.0, -100.0), Vec2::new(0.0, 100.0), None, None, None).unwrap_or(0);
let m = sk.add_line(Vec2::new(30.0, -80.0), Vec2::new(30.0, 90.0), None, None, None).unwrap_or(0);
let p = sk.curves[m].kind.point_ids()[0];
let k = ConstraintKind::PointLineDistance { p, l, value: 30.0 };
let f = dim_frame(&sk, &k).unwrap_or(DimFrame::Radial { center: Vec2::ZERO, radius: 1.0, diameter: false, arc: None });
let reach = dim_reach(&sk, &k);
assert!(reach[0].is_some() && reach[1].is_some());
let extension_ys = |at: Vec2| -> Vec<(f64, f64)> {
let lay = dim_layout_reaching(&f, Some(encode_text(&f, at)), 0.1, Vec2::new(2.0, 1.0), reach);
lay.lines.iter().filter(|line| line.len() == 2 && (line[0].x - line[1].x).abs() < 1e-9).map(|line| (line[0].y, line[1].y)).collect()
};
assert!(extension_ys(Vec2::new(15.0, 50.0)).is_empty(), "{:?}", extension_ys(Vec2::new(15.0, 50.0)));
let above = extension_ys(Vec2::new(15.0, 120.0));
assert_eq!(above.len(), 2);
assert!((above[0].0 - 100.3).abs() < 1e-9 && (above[1].0 - 90.3).abs() < 1e-9, "{above:?}");
// Without the lines' reach the extension lines start at the measured point's height.
let lay = dim_layout(&f, Some(encode_text(&f, Vec2::new(15.0, 120.0))), 0.1, Vec2::new(2.0, 1.0));
assert!(lay.lines.iter().any(|line| (line[0].y + 79.7).abs() < 1e-9));
// A point that is not the end of a parallel line keeps its own extension line.
let mut sk = Sketch::new();
let l = sk.add_line(Vec2::new(0.0, -100.0), Vec2::new(0.0, 100.0), None, None, None).unwrap_or(0);
let q = sk.add_point(Vec2::new(30.0, -80.0), None).unwrap_or(0);
assert!(dim_reach(&sk, &ConstraintKind::PointLineDistance { p: q, l, value: 30.0 })[1].is_none());
}
}