Repository navigation
fix(edit): preserve same-feature targets when snapping vertices #818
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
Merged
giswqs
merged 2 commits into
opengeos:main
from
yongyin-leon:codex/vertex-snap-identity
Oct 25, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| //! Source-addressed candidates for the one feature whose node is being moved. | ||
| //! Rendered parts cannot supply identities: world-projection cuts can add parts and vertices. | ||
|
|
||
| use rustgis_data::{Geometry, VertexId}; | ||
| use rustgis_geom::Coord; | ||
| use rustgis_map::{design::Snapping, projection::Flat}; | ||
|
|
||
| /// The same projection, switches and map-coordinate pointer apply to every source path. | ||
| #[derive(Clone, Copy)] | ||
| struct SnapView<'a> { | ||
| flat: &'a Flat, | ||
| snapping: Snapping, | ||
| pointer: Coord, | ||
| } | ||
|
|
||
| /// Visits eligible line/polygon paths in source order, without an index or whole-feature clone. | ||
| /// Other geometry kinds retain their previous whole-feature exclusion during a node move. | ||
| pub(super) fn consider( | ||
| geometry: &Geometry, | ||
| excluded: VertexId, | ||
| flat: &Flat, | ||
| snapping: Snapping, | ||
| pointer: Coord, | ||
| visit: &mut impl FnMut(f64, Coord, &'static str), | ||
| ) { | ||
| let view = SnapView { flat, snapping, pointer }; | ||
| let mut path = |points: &[Coord], part, ring, polygon| { | ||
| consider_path(points, VertexId::new(part, ring, 0), polygon, excluded, view, visit); | ||
| }; | ||
| match geometry { | ||
| Geometry::Line(parts) => { | ||
| for (part, points) in parts.iter().enumerate() { | ||
| path(points, part, 0, false); | ||
| } | ||
| } | ||
| Geometry::Polygon(polygons) => { | ||
| for (part, rings) in polygons.iter().enumerate() { | ||
| for (ring, points) in rings.iter().enumerate() { | ||
| path(points, part, ring, true); | ||
| } | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| /// Excludes source endpoints before projection, preserving endpoint-before-vertex-before-edge | ||
| /// visitation. Only a polygon's actual repeated closing coordinate aliases its first node. | ||
| fn consider_path( | ||
| path: &[Coord], | ||
| address: VertexId, | ||
| polygon: bool, | ||
| excluded: VertexId, | ||
| view: SnapView<'_>, | ||
| visit: &mut impl FnMut(f64, Coord, &'static str), | ||
| ) { | ||
| let SnapView { flat, snapping, pointer } = view; | ||
| let closed = polygon && path.len() > 1 && path.first() == path.last(); | ||
| let count = path.len().saturating_sub(usize::from(closed)); | ||
| let skip = |index| { | ||
| address.part == excluded.part && address.ring == excluded.ring && excluded.index == if closed && index == count { 0 } else { index } | ||
| }; | ||
| let mut point = |index, kind| { | ||
| if !skip(index) | ||
| && let Some(at) = path.get(index).and_then(|at| flat.forward_checked(*at)) | ||
| { | ||
| visit((at.x - pointer.x).powi(2) + (at.y - pointer.y).powi(2), at, kind); | ||
| } | ||
| }; | ||
| if snapping.end && !polygon { | ||
| for index in [0, path.len().saturating_sub(1)] { | ||
| point(index, "end"); | ||
| } | ||
| } | ||
| if snapping.vertex { | ||
| for index in 0..count { | ||
| point(index, "vertex"); | ||
| } | ||
| } | ||
| if snapping.edge { | ||
| for (index, pair) in path.windows(2).enumerate() { | ||
| if !skip(index) | ||
| && !skip(index + 1) | ||
| && let (Some(a), Some(b)) = (pair.first(), pair.get(1)) | ||
| { | ||
| consider_edge(*a, *b, flat, pointer, visit); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Projects a surviving source edge using the same world-edge cuts as ordinary snapping. | ||
| /// Each projected piece is considered separately; no segment joins opposite sides of the map. | ||
| fn consider_edge(a: Coord, b: Coord, flat: &Flat, pointer: Coord, visit: &mut impl FnMut(f64, Coord, &'static str)) { | ||
| let mut segment = |a: Coord, b: Coord| { | ||
| let (dx, dy) = (b.x - a.x, b.y - a.y); | ||
| let length = dx * dx + dy * dy; | ||
| let t = if length > 0.0 { (((pointer.x - a.x) * dx + (pointer.y - a.y) * dy) / length).clamp(0.0, 1.0) } else { 0.0 }; | ||
| let at = Coord::new(a.x + t * dx, a.y + t * dy); | ||
| visit((at.x - pointer.x).powi(2) + (at.y - pointer.y).powi(2), at, "edge"); | ||
| }; | ||
| if matches!(flat, Flat::Custom(projector) if projector.edge().is_some()) { | ||
| if let Geometry::Line(parts) = flat.project(&Geometry::Line(vec![vec![a, b]])) { | ||
| for path in parts { | ||
| for pair in path.windows(2) { | ||
| if let (Some(a), Some(b)) = (pair.first(), pair.get(1)) { | ||
| segment(*a, *b); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } else if let (Some(a), Some(b)) = (flat.forward_checked(a), flat.forward_checked(b)) { | ||
| segment(a, b); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low-confidence edge case:
skipnormalises the path indexcountto 0, butexcluded.indexis compared as given. If a caller addresses a polygon's stored closing copy (index == count) andGeometry::vertex/move_vertexaccept that address as an alias of node 0, thenexcluded.index == countnever matches. Nothing is excluded and the moving node snaps to itself. Normaliseexcludedthe same way, for example by mappingexcluded.index == countto 0 for a closed polygon ring. The tests only cover index 0 for polygons, so please add a case for the closing index. Ignore this ifvertex()rejects that index.