mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-22 07:28:11 +08:00
Bezier-rs: Add normal and tangent to subpath (#1003)
tangent and normal for subpath Co-authored-by: Rob Nadal <Robnadal44@gmail.com>
This commit is contained in:
committed by
Keavon Chambers
co-authored by
Rob Nadal
parent
beab0f01c6
commit
511a8aa164
@@ -50,6 +50,18 @@ impl Subpath {
|
||||
number_of_curves
|
||||
}
|
||||
|
||||
pub fn find_curve_parametric(&self, t: f64) -> (Option<Bezier>, f64) {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
|
||||
let number_of_curves = self.len_segments() as f64;
|
||||
let scaled_t = t * number_of_curves;
|
||||
|
||||
let target_curve_index = scaled_t.floor() as i32;
|
||||
let target_curve_t = scaled_t % 1.;
|
||||
|
||||
(self.iter().nth(target_curve_index as usize), target_curve_t)
|
||||
}
|
||||
|
||||
/// Returns an iterator of the [Bezier]s along the `Subpath`.
|
||||
pub fn iter(&self) -> SubpathIter {
|
||||
SubpathIter { sub_path: self, index: 0 }
|
||||
|
||||
@@ -12,13 +12,7 @@ impl Subpath {
|
||||
ComputeType::Parametric(t) => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
|
||||
let number_of_curves = self.len_segments() as f64;
|
||||
let scaled_t = t * number_of_curves;
|
||||
|
||||
let target_curve_index = scaled_t.floor() as i32;
|
||||
let target_curve_t = scaled_t % 1.;
|
||||
|
||||
if let Some(curve) = self.iter().nth(target_curve_index as usize) {
|
||||
if let (Some(curve), target_curve_t) = self.find_curve_parametric(t) {
|
||||
curve.evaluate(ComputeType::Parametric(target_curve_t))
|
||||
} else {
|
||||
self.iter().last().unwrap().evaluate(ComputeType::Parametric(1.))
|
||||
@@ -59,6 +53,38 @@ impl Subpath {
|
||||
|
||||
intersection_t_values
|
||||
}
|
||||
|
||||
pub fn tangent(&self, t: ComputeType) -> DVec2 {
|
||||
match t {
|
||||
ComputeType::Parametric(t) => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
|
||||
if let (Some(curve), target_curve_t) = self.find_curve_parametric(t) {
|
||||
curve.tangent(target_curve_t)
|
||||
} else {
|
||||
self.iter().last().unwrap().tangent(1.)
|
||||
}
|
||||
}
|
||||
ComputeType::Euclidean(_t) => unimplemented!(),
|
||||
ComputeType::EuclideanWithinError { t: _, epsilon: _ } => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normal(&self, t: ComputeType) -> DVec2 {
|
||||
match t {
|
||||
ComputeType::Parametric(t) => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
|
||||
if let (Some(curve), target_curve_t) = self.find_curve_parametric(t) {
|
||||
curve.normal(target_curve_t)
|
||||
} else {
|
||||
self.iter().last().unwrap().normal(1.)
|
||||
}
|
||||
}
|
||||
ComputeType::Euclidean(_t) => unimplemented!(),
|
||||
ComputeType::EuclideanWithinError { t: _, epsilon: _ } => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user