Skip to content

Snap lines to tangents at any point of the line #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl AlignmentSnapper {
distance,
tolerance,
distance_to_align_target,
fully_constrained: false,
at_intersection: true,
alignment_target_x: Some(endpoint),
..Default::default()
Expand Down Expand Up @@ -130,7 +129,6 @@ impl AlignmentSnapper {
tolerance,
distance_to_align_target,
alignment_target_x: Some(target_position),
fully_constrained: true,
at_intersection: matches!(constraint, SnapConstraint::Line { .. }),
..Default::default()
});
Expand All @@ -149,7 +147,6 @@ impl AlignmentSnapper {
tolerance,
distance_to_align_target,
alignment_target_y: Some(target_position),
fully_constrained: true,
at_intersection: matches!(constraint, SnapConstraint::Line { .. }),
..Default::default()
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ impl LayerSnapper {
..Default::default()
},
});
normals_and_tangents(path, normals, tangents, point, tolerance, snap_results);
snap_normals(path, normals, point, tolerance, snap_results);
}
snap_tangents(path, tangents, point, tolerance, snap_results);
}
}

Expand Down Expand Up @@ -262,7 +263,7 @@ impl LayerSnapper {
}
}

fn normals_and_tangents(path: &SnapCandidatePath, normals: bool, tangents: bool, point: &SnapCandidatePoint, tolerance: f64, snap_results: &mut SnapResults) {
fn snap_normals(path: &SnapCandidatePath, normals: bool, point: &SnapCandidatePoint, tolerance: f64, snap_results: &mut SnapResults) {
if normals && path.bounds.is_none() {
for &neighbor in &point.neighbors {
for t in path.document_curve.normals_to_point(neighbor) {
Expand All @@ -284,29 +285,76 @@ fn normals_and_tangents(path: &SnapCandidatePath, normals: bool, tangents: bool,
}
}
}
if tangents && path.bounds.is_none() {
for &neighbor in &point.neighbors {
for t in path.document_curve.tangents_to_point(neighbor) {
let tangent_point = path.document_curve.evaluate(TValue::Parametric(t));
let distance = tangent_point.distance(point.document_point);
if distance > tolerance {
continue;
}
snap_results.points.push(SnappedPoint {
snapped_point_document: tangent_point,
target: SnapTarget::Path(PathSnapTarget::TangentToPath),
distance,
tolerance,
outline_layers: [Some(path.layer), None],
source: point.source,
constrained: true,
..Default::default()
});
}
}

// TODO: Snap rectangles and ellipses to ellipses tangents.
// TODO: Find out why `point.neighbors` is empty while drawing rectangles and ellipses.
fn snap_tangents(path: &SnapCandidatePath, tangents: bool, point: &SnapCandidatePoint, tolerance: f64, snap_results: &mut SnapResults) {
if !tangents || point.neighbors.len() != 1 {
return;
}

let neighbor = point.neighbors[0];

for t in path.document_curve.tangents_to_point(neighbor) {
let tangent_point = path.document_curve.evaluate(TValue::Parametric(t));

if closest_point_along_line(neighbor, point.document_point, &path.document_curve, tolerance, 20).is_some() {
let tangent = (tangent_point - neighbor).normalize();
let offset = (point.document_point - tangent_point).dot(tangent);
let snap_to = tangent_point + tangent * offset;

let distance = snap_to.distance(point.document_point);

snap_results.points.push(SnappedPoint {
snapped_point_document: snap_to,
source: point.source,
target: SnapTarget::Path(PathSnapTarget::TangentToPath),
at_intersection: true,
alignment_target_x: Some(tangent_point),
distance,
tolerance,
outline_layers: [Some(path.layer), None],
target_bounds: Some(Quad([neighbor, neighbor, snap_to, snap_to])),
..Default::default()
});
}
}
}

fn closest_point_along_line(start: DVec2, end: DVec2, curve: &Bezier, tolerance: f64, max_iterations: usize) -> Option<DVec2> {
let mut closest_point = None;
let mut closest_distance = f64::INFINITY;

for i in 0..=max_iterations {
let t = i as f64 / max_iterations as f64;

if curve.tangent(TValue::Parametric(t)).length_squared() == 0. {
continue;
}

let curve_point = curve.evaluate(TValue::Parametric(t));

let line_direction = end - start;
if line_direction.length_squared() == 0. {
break;
}

let v = curve_point - start;
let projected_distance = v.dot(line_direction.normalize());
let projected_point = start + projected_distance * line_direction.normalize();

let distance = projected_point.distance(curve_point);

if distance < closest_distance {
closest_distance = distance;
closest_point = Some(projected_point);
}
}

if closest_distance < tolerance { closest_point } else { None }
}

#[derive(Clone, Debug)]
struct SnapCandidatePath {
document_curve: Bezier,
Expand Down
Loading