Bezier-rs: Add trim for Subpath (#1006)

* Move compare.rs

* Update traits for Subpath and ManipulatorGroup

* Implement trim

* UI adjustments and more tests

* Add reverse, refactor code, rename variables

* Improve comments

* Comment nits

* Address comments

* Update trim behavior

* Update doc comment for trim

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Hannah Li
2023-02-26 18:41:11 -05:00
committed by GitHub
co-authored by Keavon Chambers
parent ba50614af1
commit 8293f730f2
13 changed files with 571 additions and 64 deletions
+11 -15
View File
@@ -8,6 +8,7 @@ pub use structs::*;
use crate::Bezier;
use std::fmt::{Debug, Formatter, Result};
use std::ops::{Index, IndexMut};
/// Structure used to represent a path composed of [Bezier] curves.
@@ -20,7 +21,7 @@ pub struct Subpath<ManipulatorGroupId: crate::Identifier> {
/// Iteration structure for iterating across each curve of a `Subpath`, using an intermediate `Bezier` representation.
pub struct SubpathIter<'a, ManipulatorGroupId: crate::Identifier> {
index: usize,
sub_path: &'a Subpath<ManipulatorGroupId>,
subpath: &'a Subpath<ManipulatorGroupId>,
}
impl<ManipulatorGroupId: crate::Identifier> Index<usize> for Subpath<ManipulatorGroupId> {
@@ -44,8 +45,8 @@ impl<ManipulatorGroupId: crate::Identifier> Iterator for SubpathIter<'_, Manipul
// Returns the Bezier representation of each `Subpath` segment, defined between a pair of adjacent manipulator points.
fn next(&mut self) -> Option<Self::Item> {
let len = self.sub_path.len() - 1
+ match self.sub_path.closed {
let len = self.subpath.len() - 1
+ match self.subpath.closed {
true => 1,
false => 0,
};
@@ -53,20 +54,15 @@ impl<ManipulatorGroupId: crate::Identifier> Iterator for SubpathIter<'_, Manipul
return None;
}
let start_index = self.index;
let end_index = (self.index + 1) % self.sub_path.len();
let end_index = (self.index + 1) % self.subpath.len();
self.index += 1;
let start = self.sub_path[start_index].anchor;
let end = self.sub_path[end_index].anchor;
let out_handle = self.sub_path[start_index].out_handle;
let in_handle = self.sub_path[end_index].in_handle;
Some(self.subpath[start_index].to_bezier(&self.subpath[end_index]))
}
}
if let (Some(handle1), Some(handle2)) = (out_handle, in_handle) {
Some(Bezier::from_cubic_dvec2(start, handle1, handle2, end))
} else if let Some(handle) = out_handle.or(in_handle) {
Some(Bezier::from_quadratic_dvec2(start, handle, end))
} else {
Some(Bezier::from_linear_dvec2(start, end))
}
impl<ManipulatorGroupId: crate::Identifier> Debug for Subpath<ManipulatorGroupId> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("Subpath").field("closed", &self.closed).field("manipulator_groups", &self.manipulator_groups).finish()
}
}