-
-
Notifications
You must be signed in to change notification settings - Fork 524
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
New node: Merge by Distance #2307
Open
richard-uk1
wants to merge
1
commit into
GraphiteEditor:master
Choose a base branch
from
richard-uk1:merge_by_distance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,109 @@ | ||
//! Collapse all nodes with all edges < distance | ||
|
||
use glam::DVec2; | ||
use petgraph::prelude::UnGraphMap; | ||
use rustc_hash::FxHashSet; | ||
|
||
use super::PointId; | ||
use super::VectorData; | ||
|
||
use super::VectorDataIndex; | ||
|
||
impl VectorData { | ||
/// Collapse all nodes with all edges < distance | ||
pub(crate) fn merge_by_distance(&mut self, distance: f64) { | ||
// treat self as an undirected graph with point = node, and segment = edge | ||
let indices = VectorDataIndex::build_from(self); | ||
|
||
// Graph that will contain only short edges. References data graph | ||
let mut short_edges = UnGraphMap::new(); | ||
for seg_id in self.segment_ids().iter().copied() { | ||
let length = indices.segment_chord_length(seg_id); | ||
if length < distance { | ||
let [start, end] = indices.segment_ends(seg_id); | ||
let start = indices.point_graph.node_weight(start).unwrap().id; | ||
let end = indices.point_graph.node_weight(end).unwrap().id; | ||
|
||
short_edges.add_node(start); | ||
short_edges.add_node(end); | ||
short_edges.add_edge(start, end, seg_id); | ||
} | ||
} | ||
|
||
// Now group connected segments - all will be collapsed to a single point. | ||
// Note: there are a few algos for this - perhaps test empirically to find fastest | ||
let collapse: Vec<FxHashSet<PointId>> = petgraph::algo::tarjan_scc(&short_edges).into_iter().map(|connected| connected.into_iter().collect()).collect(); | ||
let average_position = collapse | ||
.iter() | ||
.map(|collapse_set| { | ||
let sum: DVec2 = collapse_set.iter().map(|&id| indices.point_position(id, self)).sum(); | ||
sum / collapse_set.len() as f64 | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
// we collect all points up and delete them at the end, so that our indices aren't invalidated | ||
let mut points_to_delete = FxHashSet::default(); | ||
let mut segments_to_delete = FxHashSet::default(); | ||
for (mut collapse_set, average_pos) in collapse.into_iter().zip(average_position.into_iter()) { | ||
// remove any segments where both endpoints are in the collapse set | ||
segments_to_delete.extend(self.segment_domain.iter().filter_map(|(id, start_offset, end_offset, _)| { | ||
let start = self.point_domain.ids()[start_offset]; | ||
let end = self.point_domain.ids()[end_offset]; | ||
if collapse_set.contains(&start) && collapse_set.contains(&end) { | ||
Some(id) | ||
} else { | ||
None | ||
} | ||
})); | ||
|
||
// Delete all points but the first (arbitrary). Set that point's position to the | ||
// average of the points, update segments to use replace all points with collapsed | ||
// point. | ||
|
||
// Unwrap: set created from connected algo will not be empty | ||
let first_id = collapse_set.iter().copied().next().unwrap(); | ||
// `first_id` the point we will collapse to. | ||
collapse_set.remove(&first_id); | ||
let first_offset = indices.point_to_offset[&first_id]; | ||
|
||
// look for segments with ends in collapse_set and replace them with the point we are collapsing to | ||
for (_, start_offset, end_offset, handles) in self.segment_domain.iter_mut() { | ||
let start_id = self.point_domain.ids()[*start_offset]; | ||
let end_id = self.point_domain.ids()[*end_offset]; | ||
|
||
// moved points (only need to update Bezier handles) | ||
if start_id == first_id { | ||
let point_position = self.point_domain.positions[*start_offset]; | ||
handles.move_start(average_pos - point_position); | ||
} | ||
if end_id == first_id { | ||
let point_position = self.point_domain.positions[*end_offset]; | ||
handles.move_end(average_pos - point_position); | ||
} | ||
|
||
// removed points | ||
if collapse_set.contains(&start_id) { | ||
let point_position = self.point_domain.positions[*start_offset]; | ||
*start_offset = first_offset; | ||
handles.move_start(average_pos - point_position); | ||
} | ||
if collapse_set.contains(&end_id) { | ||
let point_position = self.point_domain.positions[*end_offset]; | ||
*end_offset = first_offset; | ||
handles.move_end(average_pos - point_position); | ||
} | ||
} | ||
// This must come after iterating segments, so segments involving the point at | ||
// `first_offset` have their handles updated correctly. | ||
self.point_domain.positions[first_offset] = average_pos; | ||
|
||
points_to_delete.extend(collapse_set) | ||
} | ||
self.segment_domain.retain(|id| !segments_to_delete.contains(id), usize::MAX); | ||
self.point_domain.retain(&mut self.segment_domain, |id| !points_to_delete.contains(id)); | ||
|
||
log::debug!("{:?}", self.segment_domain.handles()); | ||
|
||
// TODO: don't forget about faces | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -12,3 +12,5 @@ mod vector_nodes; | |
pub use vector_nodes::*; | ||
|
||
pub use bezier_rs; | ||
|
||
mod merge_by_distance; |
This file contains 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 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.
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.
Tarjan will almost certainly be faster than Kosaraju because it only requires a single graph traversal