Compare commits

...
7 Commits
Author SHA1 Message Date
Dennis Kobert e1e9ed9660 Merge branch 'master' into fix-colinear-handles-extension 2026-11-06 22:26:29 +01:00
Shubh Pingale 314e9f7866 Merge branch 'upstream/master' into fix-colinear-handles-extension 2026-10-21 20:08:30 +05:30
Shubh Pingale 69d3b465d6 Verify colinearity with is_almost_colinear and add get_handle_at helper 2026-10-14 20:39:02 +05:30
Shubh Pingale 49412afedf Extract get_anchor_near and get_segment_between helpers; add single-segment colinear handle test
Address review nits from 0HyperCube:

- Extract repeated point-lookup into a reusable get_anchor_near helper

- Extract repeated segment-lookup into a reusable get_segment_between helper

- Add test verifying single-segment anchors preserve colinear handles
2026-10-14 20:36:43 +05:30
Shubh Pingale 9efed51d8b Assert new segment handle remains at the anchor 2026-10-14 20:36:43 +05:30
Shubh Pingale 9f444bc964 Add test for extending from points connected to multiple segments 2026-10-14 20:36:43 +05:30
Shubh Pingale e8356e6efa Fix #4618: Prevent colinear handles when extending from points connected to multiple segments 2026-10-14 20:36:43 +05:30
@@ -1478,7 +1478,9 @@ impl PenToolData {
/// Perform extension of an existing path
fn extend_existing_path(&mut self, document: &DocumentMessageHandler, layer: LayerNodeIdentifier, point: PointId, position: DVec2) {
let vector: Option<VectorGeometry> = document.network_interface.compute_modified_vector(layer);
let (handle_start, in_segment) = if let Some(vector) = &vector {
let (handle_start, in_segment) = if let Some(vector) = &vector
&& vector.connected_count(point) <= 1
{
vector
.segment_iter()
.find_map(|(segment_id, bezier, start, end)| {
@@ -2496,6 +2498,7 @@ impl Fsm for PenToolFsmState {
#[cfg(test)]
mod test_pen_tool {
use super::is_almost_colinear;
use crate::messages::input_mapper::utility_types::pointer::EditorPointerState;
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
use crate::messages::portfolio::document::utility_types::misc::GroupFolderType;
@@ -2504,7 +2507,7 @@ mod test_pen_tool {
use crate::test_utils::test_prelude::*;
use glam::{DAffine2, DVec2};
use graphene_std::vector::misc::{BooleanOperation, HandleId, ManipulatorPointId};
use graphene_std::vector::{SegmentId, VectorGeometry};
use graphene_std::vector::{PointId, SegmentId, VectorGeometry};
fn assert_expected_anchors(editor: &EditorTestUtils, expected_anchors: &[DVec2]) {
let (layer, vector) = drawn_path(editor).expect("Expected a drawn path");
@@ -3422,6 +3425,37 @@ mod test_pen_tool {
assert!(leaving.distance(handle) < 1e-6, "the joining segment's handle is at {leaving}");
}
/// Finds the point ID whose viewport position is within tolerance of `target`.
fn get_anchor_near(vector: &VectorGeometry, layer_to_viewport: DAffine2, target: DVec2) -> PointId {
vector
.anchors()
.find(|&(_id, pos)| layer_to_viewport.transform_point2(pos).distance(target) < 1e-6)
.map(|(id, _pos)| id)
.unwrap_or_else(|| panic!("No anchor found near {target}"))
}
/// Finds the segment that connects two given point IDs (in either direction).
fn get_segment_between(vector: &VectorGeometry, point_a: PointId, point_b: PointId) -> SegmentId {
vector
.segment_ids()
.into_iter()
.find(|&segment| {
let start = vector.segment_start_from_id(segment);
let end = vector.segment_end_from_id(segment);
(start == Some(point_a) && end == Some(point_b)) || (start == Some(point_b) && end == Some(point_a))
})
.unwrap_or_else(|| panic!("No segment found between the given anchors"))
}
/// Finds the handle position on `segment` that corresponds to `point`.
fn get_handle_at(vector: &VectorGeometry, segment: SegmentId, point: PointId) -> Option<DVec2> {
if vector.segment_start_from_id(segment) == Some(point) {
vector.position_of(ManipulatorPointId::PrimaryHandle(segment))
} else {
vector.position_of(ManipulatorPointId::EndHandle(segment))
}
}
#[tokio::test]
async fn an_anchor_moved_with_space_lands_on_a_corner_and_carries_its_handles_along() {
let mut editor = EditorTestUtils::create();
@@ -3709,4 +3743,83 @@ mod test_pen_tool {
let stroke = position(graphene_std::vector_nodes::stroke::IDENTIFIER).expect("The path should have a Stroke node");
assert!(transform > fill && transform > stroke, "the Transform should be upstream of the Fill and Stroke in {chain:?}");
}
#[tokio::test]
async fn extending_from_anchor_with_multiple_segments_does_not_create_colinear_handle() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
// 1. Draw a path through A, B, and C with curved handles dragged out at B
click_pen(&mut editor, A).await;
let handle = B + DVec2::new(50., 0.);
editor.drag_path(&[B, handle], ModifierKeys::empty()).await;
click_pen(&mut editor, C).await;
editor.press(Key::Enter, ModifierKeys::empty()).await;
assert_path_shape(&editor, 3, 2);
// 2. Extend from anchor B to D by clicking on B and then D (without dragging)
click_pen(&mut editor, B).await;
click_pen(&mut editor, D).await;
editor.press(Key::Enter, ModifierKeys::empty()).await;
assert_path_shape(&editor, 4, 3);
// 3. Verify the new branch leaving B does not create a mirrored/colinear handle
let (layer, vector) = drawn_path(&editor).expect("Expected a drawn path");
let layer_to_viewport = editor.active_document().metadata().transform_to_viewport(layer);
let b_id = get_anchor_near(&vector, layer_to_viewport, B);
let d_id = get_anchor_near(&vector, layer_to_viewport, D);
let bd_segment = get_segment_between(&vector, b_id, d_id);
if let Some(handle_pos) = get_handle_at(&vector, bd_segment, b_id) {
let handle_pos = layer_to_viewport.transform_point2(handle_pos);
assert!(handle_pos.distance(B) < 1e-4, "Handle at B for the new segment should remain at the anchor");
}
}
#[tokio::test]
async fn extending_from_anchor_with_single_segment_uses_colinear_handle() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
// 1. Draw a single segment A→B with a curved handle dragged out at B
click_pen(&mut editor, A).await;
let handle = B + DVec2::new(50., 0.);
editor.drag_path(&[B, handle], ModifierKeys::empty()).await;
editor.press(Key::Enter, ModifierKeys::empty()).await;
assert_path_shape(&editor, 2, 1);
// 2. Extend from B to C by clicking B then C (only one segment is attached to B)
click_pen(&mut editor, B).await;
click_pen(&mut editor, C).await;
editor.press(Key::Enter, ModifierKeys::empty()).await;
assert_path_shape(&editor, 3, 2);
// 3. Verify the new segment leaving B carries a mirrored/colinear handle
let (layer, vector) = drawn_path(&editor).expect("Expected a drawn path");
let layer_to_viewport = editor.active_document().metadata().transform_to_viewport(layer);
let b_id = get_anchor_near(&vector, layer_to_viewport, B);
let c_id = get_anchor_near(&vector, layer_to_viewport, C);
let bc_segment = get_segment_between(&vector, b_id, c_id);
let a_id = get_anchor_near(&vector, layer_to_viewport, A);
let ab_segment = get_segment_between(&vector, a_id, b_id);
let existing_handle_at_b = get_handle_at(&vector, ab_segment, b_id).expect("Segment AB should have a handle at B");
let existing_handle_pos = layer_to_viewport.transform_point2(existing_handle_at_b);
let new_handle_at_b = get_handle_at(&vector, bc_segment, b_id).expect("The new segment should have a handle at B");
let new_handle_pos = layer_to_viewport.transform_point2(new_handle_at_b);
// When only one segment is connected, the handle should not collapse to the
// anchor, and must be colinear with the existing handle at B.
assert!(
new_handle_pos.distance(B) > 1e-4,
"Handle at B should not collapse to the anchor when only one segment is connected (got {new_handle_pos})"
);
assert!(
is_almost_colinear(B, existing_handle_pos, new_handle_pos),
"New handle should be colinear with the existing handle at B (existing: {existing_handle_pos}, new: {new_handle_pos})"
);
}
}