Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 92 additions & 15 deletions iOverlay/src/split/cross_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,26 @@ impl<I: IntNumber> CrossSolver<I> {
let r1 = ra1.min(rb1);

if r0 <= r1 {
let p = if ra0 < rb0 { target.a } else { target.b };
let e = if ra0 < rb0 { target.a } else { target.b };
// ignore if it's a clean point
if Triangle::is_not_line(other.a, p, other.b) {
if Triangle::is_not_line(other.a, e, other.b)
&& Self::is_closer_than_grid_node(other, e, target, p)
{
return Some(CrossResult {
point: p,
point: e,
cross_type: CrossType::TargetEnd,
is_round: true,
});
}
} else {
let p = if ra1 < rb1 { other.a } else { other.b };
let e = if ra1 < rb1 { other.a } else { other.b };

// ignore if it's a clean point
if Triangle::is_not_line(target.a, p, target.b) {
if Triangle::is_not_line(target.a, e, target.b)
&& Self::is_closer_than_grid_node(target, e, other, p)
{
return Some(CrossResult {
point: p,
point: e,
cross_type: CrossType::OtherEnd,
is_round: true,
});
Expand All @@ -220,6 +224,46 @@ impl<I: IntNumber> CrossSolver<I> {
})
}

/// Whether snapping to the endpoint `e` of `owner` bends `segment` no more than splitting
/// both segments at the rounded crossing `p` moves them.
///
/// Snapping a crossing to an endpoint of one segment splits the other segment there, which
/// moves it by the endpoint's distance from its line. The rounded crossing moves each
/// segment by its distance from that segment's line. Prefer the grid node unless the
/// endpoint is at least as close.
#[inline]
fn is_closer_than_grid_node(
segment: &XSegment<I>,
e: IntPoint<I>,
owner: &XSegment<I>,
p: IntPoint<I>,
) -> bool {
let snap = Self::sqr_line_distance(segment, e);
let grid = Self::sqr_line_distance(segment, p).max(Self::sqr_line_distance(owner, p));
snap <= grid
}

/// Squared distance from `p` to the line through `segment`.
#[inline]
fn sqr_line_distance(segment: &XSegment<I>, p: IntPoint<I>) -> f64 {
let v = segment.b - segment.a;
let cross = v.cross_product(p - segment.a).to_f64();
cross * cross / segment.a.sqr_distance(segment.b).to_f64()
}

/// Divides, rounding to the nearest integer, with ties toward zero.
///
/// Truncating would move a crossing on an axis-aligned segment by up to a unit. Rounding
/// ties toward zero, i.e. toward the segment's first endpoint, keeps the truncated result
/// whenever it is one of the two nearest integers.
#[inline]
fn divide_round_half_toward_zero(numerator: I::Wide, divisor: I::Wide) -> I::Wide {
let n = numerator.unsigned_abs();
let d = divisor.unsigned_abs();
let q = (n + ((d - <I::Wide as WideIntNumber>::UInt::ONE) >> 1)) / d;
numerator.signum() * divisor.signum() * I::Wide::from_uint(q)
}

fn cross_point(target: &XSegment<I>, other: &XSegment<I>) -> IntPoint<I> {
// edges are not parallel
// Input coordinates follow the N-bit range documented in core::integer.
Expand Down Expand Up @@ -282,11 +326,11 @@ impl<I: IntNumber> CrossSolver<I> {
if a1x == I::Wide::ZERO {
// dxB is not zero because it will be parallel case and it's impossible
x0 = I::Wide::ZERO;
y0 = xy_b / dx_b;
y0 = Self::divide_round_half_toward_zero(xy_b, dx_b);
} else if a1y == I::Wide::ZERO {
// dyB is not zero because it will be parallel case and it's impossible
y0 = I::Wide::ZERO;
x0 = -xy_b / dy_b;
x0 = Self::divide_round_half_toward_zero(-xy_b, dy_b);
} else {
// divider
let div = a1y * dx_b - a1x * dy_b;
Expand Down Expand Up @@ -325,16 +369,48 @@ mod tests {

#[test]
fn snapping_compares_squared_distance_directly_with_the_threshold() {
let target = XSegment::new(IntPoint::new(0, 0), IntPoint::new(5, 5));
let other = XSegment::new(IntPoint::new(0, 3), IntPoint::new(5, -2));
// The rounded crossing is (2, 2). Its nearest endpoint is (0, 3),
let target = XSegment::new(IntPoint::new(0, 0), IntPoint::new(6, 3));
let other = XSegment::new(IntPoint::new(0, -1), IntPoint::new(5, 3));
// The crossing (3 1/3, 1 2/3) rounds to (3, 2). Its nearest endpoint is (5, 3),
// at squared distance 5. A threshold of 4 must not be squared again.
// Snapping moves the target by 3/√45 ≈ 0.45, less than the rounded crossing moves the
// other segment, 3/√41 ≈ 0.47, so it is allowed.
let crossing = CrossSolver::cross(&target, &other, 4).unwrap();
assert!(matches!(crossing.cross_type, CrossType::Pure));
assert_eq!(crossing.point, IntPoint::new(2, 2));
assert_eq!(crossing.point, IntPoint::new(3, 2));
let snapped = CrossSolver::cross(&target, &other, 5).unwrap();
assert!(matches!(snapped.cross_type, CrossType::OtherEnd));
assert_eq!(snapped.point, other.a);
assert_eq!(snapped.point, other.b);
}

#[test]
fn snapping_prefers_a_closer_grid_node() {
// The crossing (1, 2 1/11) rounds to (1, 2), a unit from the endpoint (0, 2) of the
// target. Snapping to (0, 2) would bend the vertical segment a unit off its line, while
// (1, 2) is within 1/11 of both lines.
let target = XSegment::new(IntPoint::new(0, 2), IntPoint::new(11, 3));
let other = XSegment::new(IntPoint::new(1, 1), IntPoint::new(1, 4));
for radius_squared in [1, 2, 4, 16] {
let crossing = CrossSolver::cross(&target, &other, radius_squared).unwrap();
assert!(matches!(crossing.cross_type, CrossType::Pure));
assert_eq!(crossing.point, IntPoint::new(1, 2));
}
}

#[test]
fn axis_aligned_crossings_are_rounded() {
// The crossing is (-1.25, -1); truncating relative to the first endpoint gave (-2, -1).
let horizontal = XSegment::new(IntPoint::new(-2, -1), IntPoint::new(-1, -1));
let other = XSegment::new(IntPoint::new(-2, 2), IntPoint::new(-1, -2));
assert_eq!(
CrossSolver::cross_point(&horizontal, &other),
IntPoint::new(-1, -1)
);

// The same, transposed.
let vertical = XSegment::new(IntPoint::new(-1, -2), IntPoint::new(-1, -1));
let other = XSegment::new(IntPoint::new(-2, -2), IntPoint::new(2, 1));
assert_eq!(CrossSolver::cross_point(&vertical, &other), IntPoint::new(-1, -1));
}

#[test]
Expand Down Expand Up @@ -376,9 +452,10 @@ mod tests {
b: point(hi, b1),
};
let result = CrossSolver::<I>::cross_point(&a, &b);
// For a horizontal target x is truncated, not rounded.
// For a horizontal target x is rounded with ties toward a0.
let expected_x = if a0 == a1 {
lo as i128 + (hi as i128 - lo as i128) * n.abs() / d.abs()
let product = (hi as i128 - lo as i128) * n.abs();
lo as i128 + (product + (d.abs() - 1) / 2) / d.abs()
} else {
expected_x
};
Expand Down
25 changes: 21 additions & 4 deletions iOverlay/src/string/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,27 @@ mod tests {
FillRule::NonZero,
);

assert_eq!(shapes.len(), 3);
assert_eq!(shapes[0][0].len(), 5);
assert_eq!(shapes[1][0].len(), 3);
assert_eq!(shapes[2][0].len(), 3);
// The line crosses the edges at (0.5, 2) and (1.5, 2), which round to (0, 2) and (1, 2).
// The top-left part, a triangle half a unit wide, collapses onto the left edge. The
// crossing isn't snapped to the vertex (1, 1), a unit away from the line.
assert_eq!(
shapes,
vec![
vec![vec![
IntPoint::new(2, 0),
IntPoint::new(2, 2),
IntPoint::new(1, 2),
IntPoint::new(1, 1),
IntPoint::new(0, 2),
IntPoint::new(0, 0),
]],
vec![vec![
IntPoint::new(1, 2),
IntPoint::new(2, 2),
IntPoint::new(2, 3)
]],
]
);
}

#[test]
Expand Down
Loading