mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-24 02:28:12 +08:00
Bezier-rs: Add Euclidean parameterization to Bezier::evaluate (#828)
* Add enum to evaluate for differenciating compute type * Add euclidean parameterization and update styling in the UI Co-authored-by: Rob Nadal <robnadal44@gmail.com> * Update usage of evaluate in graphite * Add description * Code review changes * Update tests * Improve ComputeType ergonomics * Large code review/cleanup pass Co-authored-by: Rob Nadal <robnadal44@gmail.com> Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
committed by
Keavon Chambers
co-authored by
Rob Nadal
Keavon Chambers
parent
a220bfa759
commit
01a9724389
@@ -203,6 +203,8 @@ impl Bezier {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::ComputeType;
|
||||
|
||||
use super::compare::compare_points;
|
||||
use super::*;
|
||||
|
||||
@@ -213,13 +215,13 @@ mod tests {
|
||||
let p3 = DVec2::new(160., 170.);
|
||||
|
||||
let bezier1 = Bezier::quadratic_through_points(p1, p2, p3, None);
|
||||
assert!(compare_points(bezier1.evaluate(0.5), p2));
|
||||
assert!(compare_points(bezier1.evaluate(ComputeType::Parametric(0.5)), p2));
|
||||
|
||||
let bezier2 = Bezier::quadratic_through_points(p1, p2, p3, Some(0.8));
|
||||
assert!(compare_points(bezier2.evaluate(0.8), p2));
|
||||
assert!(compare_points(bezier2.evaluate(ComputeType::Parametric(0.8)), p2));
|
||||
|
||||
let bezier3 = Bezier::quadratic_through_points(p1, p2, p3, Some(0.));
|
||||
assert!(compare_points(bezier3.evaluate(0.), p2));
|
||||
assert!(compare_points(bezier3.evaluate(ComputeType::Parametric(0.)), p2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -229,12 +231,12 @@ mod tests {
|
||||
let p3 = DVec2::new(160., 160.);
|
||||
|
||||
let bezier1 = Bezier::cubic_through_points(p1, p2, p3, Some(0.3), Some(10.));
|
||||
assert!(compare_points(bezier1.evaluate(0.3), p2));
|
||||
assert!(compare_points(bezier1.evaluate(ComputeType::Parametric(0.3)), p2));
|
||||
|
||||
let bezier2 = Bezier::cubic_through_points(p1, p2, p3, Some(0.8), Some(91.7));
|
||||
assert!(compare_points(bezier2.evaluate(0.8), p2));
|
||||
assert!(compare_points(bezier2.evaluate(ComputeType::Parametric(0.8)), p2));
|
||||
|
||||
let bezier3 = Bezier::cubic_through_points(p1, p2, p3, Some(0.), Some(91.7));
|
||||
assert!(compare_points(bezier3.evaluate(0.), p2));
|
||||
assert!(compare_points(bezier3.evaluate(ComputeType::Parametric(0.)), p2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::utils::{f64_compare, ComputeType};
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Functionality relating to looking up properties of the `Bezier` or points along the `Bezier`.
|
||||
impl Bezier {
|
||||
/// Calculate the point on the curve based on the `t`-value provided.
|
||||
pub(crate) fn unrestricted_evaluate(&self, t: f64) -> DVec2 {
|
||||
pub(crate) fn unrestricted_parametric_evaluate(&self, t: f64) -> DVec2 {
|
||||
// Basis code based off of pseudocode found here: <https://pomax.github.io/bezierinfo/#explanation>.
|
||||
|
||||
let t_squared = t * t;
|
||||
@@ -21,11 +23,48 @@ impl Bezier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the point along the curve that is a factor of `d` away from the start.
|
||||
pub(crate) fn unrestricted_euclidean_evaluate(&self, d: f64, error: f64) -> DVec2 {
|
||||
if let BezierHandles::Linear = self.handles {
|
||||
return self.unrestricted_parametric_evaluate(d);
|
||||
}
|
||||
|
||||
let mut low = 0.;
|
||||
let mut mid = 0.;
|
||||
let mut high = 1.;
|
||||
let total_length = self.length(None);
|
||||
|
||||
while low < high {
|
||||
mid = (low + high) / 2.;
|
||||
let test_d = self.trim(0., mid).length(None) / total_length;
|
||||
if f64_compare(test_d, d, error) {
|
||||
break;
|
||||
} else if test_d < d {
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
self.unrestricted_parametric_evaluate(mid)
|
||||
}
|
||||
|
||||
/// Calculate the point on the curve based on the `t`-value provided.
|
||||
/// Expects `t` to be within the inclusive range `[0, 1]`.
|
||||
pub fn evaluate(&self, t: f64) -> DVec2 {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
self.unrestricted_evaluate(t)
|
||||
pub fn evaluate(&self, t: ComputeType) -> DVec2 {
|
||||
match t {
|
||||
ComputeType::Parametric(t) => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
self.unrestricted_parametric_evaluate(t)
|
||||
}
|
||||
ComputeType::Euclidean(t) => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
self.unrestricted_euclidean_evaluate(t, 0.0001)
|
||||
}
|
||||
ComputeType::EuclideanWithinError { t, epsilon } => {
|
||||
assert!((0.0..=1.).contains(&t));
|
||||
self.unrestricted_euclidean_evaluate(t, epsilon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a selection of equidistant points on the bezier curve.
|
||||
@@ -36,7 +75,7 @@ impl Bezier {
|
||||
let mut steps_array = Vec::with_capacity(steps_unwrapped + 1);
|
||||
|
||||
for t in 0..steps_unwrapped + 1 {
|
||||
steps_array.push(self.evaluate(f64::from(t as i32) * ratio))
|
||||
steps_array.push(self.evaluate(ComputeType::Parametric(f64::from(t as i32) * ratio)))
|
||||
}
|
||||
|
||||
steps_array
|
||||
@@ -123,7 +162,7 @@ impl Bezier {
|
||||
if step_index == 0 {
|
||||
distance = *table_distance;
|
||||
} else {
|
||||
distance = point.distance(self.evaluate(iterator_t));
|
||||
distance = point.distance(self.evaluate(ComputeType::Parametric(iterator_t)));
|
||||
*table_distance = distance;
|
||||
}
|
||||
if distance < new_minimum_distance {
|
||||
@@ -173,23 +212,29 @@ mod tests {
|
||||
let p4 = DVec2::new(30., 21.);
|
||||
|
||||
let bezier1 = Bezier::from_quadratic_dvec2(p1, p2, p3);
|
||||
assert_eq!(bezier1.evaluate(0.5), DVec2::new(12.5, 6.25));
|
||||
assert_eq!(bezier1.evaluate(ComputeType::Parametric(0.5)), DVec2::new(12.5, 6.25));
|
||||
|
||||
let bezier2 = Bezier::from_cubic_dvec2(p1, p2, p3, p4);
|
||||
assert_eq!(bezier2.evaluate(0.5), DVec2::new(16.5, 9.625));
|
||||
assert_eq!(bezier2.evaluate(ComputeType::Parametric(0.5)), DVec2::new(16.5, 9.625));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compute_lookup_table() {
|
||||
let bezier1 = Bezier::from_quadratic_coordinates(10., 10., 30., 30., 50., 10.);
|
||||
let lookup_table1 = bezier1.compute_lookup_table(Some(2));
|
||||
assert_eq!(lookup_table1, vec![bezier1.start(), bezier1.evaluate(0.5), bezier1.end()]);
|
||||
assert_eq!(lookup_table1, vec![bezier1.start(), bezier1.evaluate(ComputeType::Parametric(0.5)), bezier1.end()]);
|
||||
|
||||
let bezier2 = Bezier::from_cubic_coordinates(10., 10., 30., 30., 70., 70., 90., 10.);
|
||||
let lookup_table2 = bezier2.compute_lookup_table(Some(4));
|
||||
assert_eq!(
|
||||
lookup_table2,
|
||||
vec![bezier2.start(), bezier2.evaluate(0.25), bezier2.evaluate(0.5), bezier2.evaluate(0.75), bezier2.end()]
|
||||
vec![
|
||||
bezier2.start(),
|
||||
bezier2.evaluate(ComputeType::Parametric(0.25)),
|
||||
bezier2.evaluate(ComputeType::Parametric(0.50)),
|
||||
bezier2.evaluate(ComputeType::Parametric(0.75)),
|
||||
bezier2.end()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use crate::utils::ComputeType;
|
||||
|
||||
use glam::DMat2;
|
||||
use std::ops::Range;
|
||||
@@ -52,7 +53,7 @@ impl Bezier {
|
||||
pub fn tangent(&self, t: f64) -> DVec2 {
|
||||
match self.handles {
|
||||
BezierHandles::Linear => self.end - self.start,
|
||||
_ => self.derivative().unwrap().evaluate(t),
|
||||
_ => self.derivative().unwrap().evaluate(ComputeType::Parametric(t)),
|
||||
}
|
||||
.normalize()
|
||||
}
|
||||
@@ -67,8 +68,8 @@ impl Bezier {
|
||||
pub fn curvature(&self, t: f64) -> f64 {
|
||||
let (d, dd) = match &self.derivative() {
|
||||
Some(first_derivative) => match first_derivative.derivative() {
|
||||
Some(second_derivative) => (first_derivative.evaluate(t), second_derivative.evaluate(t)),
|
||||
None => (first_derivative.evaluate(t), first_derivative.end - first_derivative.start),
|
||||
Some(second_derivative) => (first_derivative.evaluate(ComputeType::Parametric(t)), second_derivative.evaluate(ComputeType::Parametric(t))),
|
||||
None => (first_derivative.evaluate(ComputeType::Parametric(t)), first_derivative.end - first_derivative.start),
|
||||
},
|
||||
None => (self.end - self.start, DVec2::new(0., 0.)),
|
||||
};
|
||||
@@ -128,7 +129,7 @@ impl Bezier {
|
||||
let extrema = self.local_extrema();
|
||||
for t_values in extrema {
|
||||
for t in t_values {
|
||||
let point = self.evaluate(t);
|
||||
let point = self.evaluate(ComputeType::Parametric(t));
|
||||
// Update bounding box if new min/max is found.
|
||||
endpoints_min = endpoints_min.min(point);
|
||||
endpoints_max = endpoints_max.max(point);
|
||||
@@ -276,7 +277,7 @@ impl Bezier {
|
||||
// Accept the t value if it is approximately in [0, 1] and if the corresponding coordinates are within the range of the linear line
|
||||
.filter(|&t| {
|
||||
utils::f64_approximately_in_range(t, 0., 1., MAX_ABSOLUTE_DIFFERENCE)
|
||||
&& utils::dvec2_approximately_in_range(self.unrestricted_evaluate(t), min, max, MAX_ABSOLUTE_DIFFERENCE).all()
|
||||
&& utils::dvec2_approximately_in_range(self.unrestricted_parametric_evaluate(t), min, max, MAX_ABSOLUTE_DIFFERENCE).all()
|
||||
})
|
||||
// Ensure the returned value is within the correct range
|
||||
.map(|t| t.clamp(0., 1.))
|
||||
@@ -348,7 +349,7 @@ mod tests {
|
||||
];
|
||||
assert_eq!(&de_casteljau_points, &expected_de_casteljau_points);
|
||||
|
||||
assert_eq!(expected_de_casteljau_points[3][0], bezier.evaluate(0.5));
|
||||
assert_eq!(expected_de_casteljau_points[3][0], bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -564,12 +565,12 @@ mod tests {
|
||||
let line1 = Bezier::from_linear_coordinates(20., 60., 70., 60.);
|
||||
let intersections1 = bezier.intersections(&line1, None);
|
||||
assert!(intersections1.len() == 1);
|
||||
assert!(compare_points(bezier.evaluate(intersections1[0]), DVec2::new(30., 60.)));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections1[0])), DVec2::new(30., 60.)));
|
||||
|
||||
// Intersection in the middle of curve
|
||||
let line2 = Bezier::from_linear_coordinates(150., 150., 30., 30.);
|
||||
let intersections2 = bezier.intersections(&line2, None);
|
||||
assert!(compare_points(bezier.evaluate(intersections2[0]), DVec2::new(96., 96.)));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections2[0])), DVec2::new(96., 96.)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -583,12 +584,12 @@ mod tests {
|
||||
let line1 = Bezier::from_linear_coordinates(20., 50., 40., 50.);
|
||||
let intersections1 = bezier.intersections(&line1, None);
|
||||
assert!(intersections1.len() == 1);
|
||||
assert!(compare_points(bezier.evaluate(intersections1[0]), p1));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections1[0])), p1));
|
||||
|
||||
// Intersection in the middle of curve
|
||||
let line2 = Bezier::from_linear_coordinates(150., 150., 30., 30.);
|
||||
let intersections2 = bezier.intersections(&line2, None);
|
||||
assert!(compare_points(bezier.evaluate(intersections2[0]), DVec2::new(47.77355, 47.77354)));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections2[0])), DVec2::new(47.77355, 47.77354)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -603,14 +604,14 @@ mod tests {
|
||||
let line1 = Bezier::from_linear_coordinates(20., 30., 40., 30.);
|
||||
let intersections1 = bezier.intersections(&line1, None);
|
||||
assert!(intersections1.len() == 1);
|
||||
assert!(compare_points(bezier.evaluate(intersections1[0]), p1));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections1[0])), p1));
|
||||
|
||||
// Intersection at edge and in middle of curve, Discriminant < 0
|
||||
let line2 = Bezier::from_linear_coordinates(150., 150., 30., 30.);
|
||||
let intersections2 = bezier.intersections(&line2, None);
|
||||
assert!(intersections2.len() == 2);
|
||||
assert!(compare_points(bezier.evaluate(intersections2[0]), p1));
|
||||
assert!(compare_points(bezier.evaluate(intersections2[1]), DVec2::new(85.84, 85.84)));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections2[0])), p1));
|
||||
assert!(compare_points(bezier.evaluate(ComputeType::Parametric(intersections2[1])), DVec2::new(85.84, 85.84)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -621,8 +622,8 @@ mod tests {
|
||||
let intersections = bezier1.intersections(&bezier2, None);
|
||||
let intersections2 = bezier2.intersections(&bezier1, None);
|
||||
assert!(compare_vec_of_points(
|
||||
intersections.iter().map(|&t| bezier1.evaluate(t)).collect(),
|
||||
intersections2.iter().map(|&t| bezier2.evaluate(t)).collect(),
|
||||
intersections.iter().map(|&t| bezier1.evaluate(ComputeType::Parametric(t))).collect(),
|
||||
intersections2.iter().map(|&t| bezier2.evaluate(ComputeType::Parametric(t))).collect(),
|
||||
2.
|
||||
));
|
||||
}
|
||||
@@ -632,8 +633,8 @@ mod tests {
|
||||
let bezier = Bezier::from_cubic_coordinates(160., 180., 170., 10., 30., 90., 180., 140.);
|
||||
let intersections = bezier.self_intersections(Some(0.5));
|
||||
assert!(compare_vec_of_points(
|
||||
intersections.iter().map(|&t| bezier.evaluate(t[0])).collect(),
|
||||
intersections.iter().map(|&t| bezier.evaluate(t[1])).collect(),
|
||||
intersections.iter().map(|&t| bezier.evaluate(ComputeType::Parametric(t[0]))).collect(),
|
||||
intersections.iter().map(|&t| bezier.evaluate(ComputeType::Parametric(t[1]))).collect(),
|
||||
2.
|
||||
));
|
||||
assert!(Bezier::from_linear_coordinates(160., 180., 170., 10.).self_intersections(None).is_empty());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use crate::utils::f64_compare;
|
||||
use crate::utils::{f64_compare, ComputeType};
|
||||
|
||||
use glam::DMat2;
|
||||
use std::f64::consts::PI;
|
||||
@@ -8,7 +8,7 @@ use std::f64::consts::PI;
|
||||
impl Bezier {
|
||||
/// Returns the pair of Bezier curves that result from splitting the original curve at the point corresponding to `t`.
|
||||
pub fn split(&self, t: f64) -> [Bezier; 2] {
|
||||
let split_point = self.evaluate(t);
|
||||
let split_point = self.evaluate(ComputeType::Parametric(t));
|
||||
|
||||
match self.handles {
|
||||
BezierHandles::Linear => [Bezier::from_linear_dvec2(self.start, split_point), Bezier::from_linear_dvec2(split_point, self.end)],
|
||||
@@ -53,7 +53,7 @@ impl Bezier {
|
||||
pub fn trim(&self, t1: f64, t2: f64) -> Bezier {
|
||||
// If t1 is equal to t2, return a bezier comprised entirely of the same point
|
||||
if f64_compare(t1, t2, MAX_ABSOLUTE_DIFFERENCE) {
|
||||
let point = self.evaluate(t1);
|
||||
let point = self.evaluate(ComputeType::Parametric(t1));
|
||||
return match self.handles {
|
||||
BezierHandles::Linear => Bezier::from_linear_dvec2(point, point),
|
||||
BezierHandles::Quadratic { handle: _ } => Bezier::from_quadratic_dvec2(point, point, point),
|
||||
@@ -444,9 +444,9 @@ impl Bezier {
|
||||
// Inner loop to find the next maximal segment of the curve that can be approximated with a circular arc
|
||||
while iterations <= max_iterations {
|
||||
iterations += 1;
|
||||
let p1 = self.evaluate(low);
|
||||
let p2 = self.evaluate(middle);
|
||||
let p3 = self.evaluate(high);
|
||||
let p1 = self.evaluate(ComputeType::Parametric(low));
|
||||
let p2 = self.evaluate(ComputeType::Parametric(middle));
|
||||
let p3 = self.evaluate(ComputeType::Parametric(high));
|
||||
|
||||
let wrapped_center = utils::compute_circle_center_from_points(p1, p2, p3);
|
||||
// If the segment is linear, move on to next segment
|
||||
@@ -486,8 +486,8 @@ impl Bezier {
|
||||
};
|
||||
|
||||
// Use points in between low, middle, and high to evaluate how well the arc approximates the curve
|
||||
let e1 = self.evaluate((low + middle) / 2.);
|
||||
let e2 = self.evaluate((middle + high) / 2.);
|
||||
let e1 = self.evaluate(ComputeType::Parametric((low + middle) / 2.));
|
||||
let e2 = self.evaluate(ComputeType::Parametric((middle + high) / 2.));
|
||||
|
||||
// Iterate until we find the largest good approximation such that the next iteration is not a good approximation with an arc
|
||||
if utils::f64_compare(radius, e1.distance(center), error) && utils::f64_compare(radius, e2.distance(center), error) {
|
||||
@@ -537,6 +537,8 @@ impl Bezier {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::ComputeType;
|
||||
|
||||
use super::compare::{compare_arcs, compare_vector_of_beziers};
|
||||
use super::*;
|
||||
|
||||
@@ -546,34 +548,34 @@ mod tests {
|
||||
let [part1, part2] = line.split(0.5);
|
||||
|
||||
assert_eq!(part1.start(), line.start());
|
||||
assert_eq!(part1.end(), line.evaluate(0.5));
|
||||
assert_eq!(part1.evaluate(0.5), line.evaluate(0.25));
|
||||
assert_eq!(part1.end(), line.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part1.evaluate(ComputeType::Parametric(0.5)), line.evaluate(ComputeType::Parametric(0.25)));
|
||||
|
||||
assert_eq!(part2.start(), line.evaluate(0.5));
|
||||
assert_eq!(part2.start(), line.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part2.end(), line.end());
|
||||
assert_eq!(part2.evaluate(0.5), line.evaluate(0.75));
|
||||
assert_eq!(part2.evaluate(ComputeType::Parametric(0.5)), line.evaluate(ComputeType::Parametric(0.75)));
|
||||
|
||||
let quad_bezier = Bezier::from_quadratic_coordinates(10., 10., 50., 50., 90., 10.);
|
||||
let [part3, part4] = quad_bezier.split(0.5);
|
||||
|
||||
assert_eq!(part3.start(), quad_bezier.start());
|
||||
assert_eq!(part3.end(), quad_bezier.evaluate(0.5));
|
||||
assert_eq!(part3.evaluate(0.5), quad_bezier.evaluate(0.25));
|
||||
assert_eq!(part3.end(), quad_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part3.evaluate(ComputeType::Parametric(0.5)), quad_bezier.evaluate(ComputeType::Parametric(0.25)));
|
||||
|
||||
assert_eq!(part4.start(), quad_bezier.evaluate(0.5));
|
||||
assert_eq!(part4.start(), quad_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part4.end(), quad_bezier.end());
|
||||
assert_eq!(part4.evaluate(0.5), quad_bezier.evaluate(0.75));
|
||||
assert_eq!(part4.evaluate(ComputeType::Parametric(0.5)), quad_bezier.evaluate(ComputeType::Parametric(0.75)));
|
||||
|
||||
let cubic_bezier = Bezier::from_cubic_coordinates(10., 10., 50., 50., 90., 10., 40., 50.);
|
||||
let [part5, part6] = cubic_bezier.split(0.5);
|
||||
|
||||
assert_eq!(part5.start(), cubic_bezier.start());
|
||||
assert_eq!(part5.end(), cubic_bezier.evaluate(0.5));
|
||||
assert_eq!(part5.evaluate(0.5), cubic_bezier.evaluate(0.25));
|
||||
assert_eq!(part5.end(), cubic_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part5.evaluate(ComputeType::Parametric(0.5)), cubic_bezier.evaluate(ComputeType::Parametric(0.25)));
|
||||
|
||||
assert_eq!(part6.start(), cubic_bezier.evaluate(0.5));
|
||||
assert_eq!(part6.start(), cubic_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
assert_eq!(part6.end(), cubic_bezier.end());
|
||||
assert_eq!(part6.evaluate(0.5), cubic_bezier.evaluate(0.75));
|
||||
assert_eq!(part6.evaluate(ComputeType::Parametric(0.5)), cubic_bezier.evaluate(ComputeType::Parametric(0.75)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -611,23 +613,23 @@ mod tests {
|
||||
let line = Bezier::from_linear_coordinates(80., 80., 40., 40.);
|
||||
let trimmed1 = line.trim(0.25, 0.75);
|
||||
|
||||
assert_eq!(trimmed1.start(), line.evaluate(0.25));
|
||||
assert_eq!(trimmed1.end(), line.evaluate(0.75));
|
||||
assert_eq!(trimmed1.evaluate(0.5), line.evaluate(0.5));
|
||||
assert_eq!(trimmed1.start(), line.evaluate(ComputeType::Parametric(0.25)));
|
||||
assert_eq!(trimmed1.end(), line.evaluate(ComputeType::Parametric(0.75)));
|
||||
assert_eq!(trimmed1.evaluate(ComputeType::Parametric(0.5)), line.evaluate(ComputeType::Parametric(0.5)));
|
||||
|
||||
let quadratic_bezier = Bezier::from_quadratic_coordinates(80., 80., 40., 40., 70., 70.);
|
||||
let trimmed2 = quadratic_bezier.trim(0.25, 0.75);
|
||||
|
||||
assert_eq!(trimmed2.start(), quadratic_bezier.evaluate(0.25));
|
||||
assert_eq!(trimmed2.end(), quadratic_bezier.evaluate(0.75));
|
||||
assert_eq!(trimmed2.evaluate(0.5), quadratic_bezier.evaluate(0.5));
|
||||
assert_eq!(trimmed2.start(), quadratic_bezier.evaluate(ComputeType::Parametric(0.25)));
|
||||
assert_eq!(trimmed2.end(), quadratic_bezier.evaluate(ComputeType::Parametric(0.75)));
|
||||
assert_eq!(trimmed2.evaluate(ComputeType::Parametric(0.5)), quadratic_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
|
||||
let cubic_bezier = Bezier::from_cubic_coordinates(80., 80., 40., 40., 70., 70., 150., 150.);
|
||||
let trimmed3 = cubic_bezier.trim(0.25, 0.75);
|
||||
|
||||
assert_eq!(trimmed3.start(), cubic_bezier.evaluate(0.25));
|
||||
assert_eq!(trimmed3.end(), cubic_bezier.evaluate(0.75));
|
||||
assert_eq!(trimmed3.evaluate(0.5), cubic_bezier.evaluate(0.5));
|
||||
assert_eq!(trimmed3.start(), cubic_bezier.evaluate(ComputeType::Parametric(0.25)));
|
||||
assert_eq!(trimmed3.end(), cubic_bezier.evaluate(ComputeType::Parametric(0.75)));
|
||||
assert_eq!(trimmed3.evaluate(ComputeType::Parametric(0.5)), cubic_bezier.evaluate(ComputeType::Parametric(0.5)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -741,16 +743,24 @@ mod tests {
|
||||
assert_eq!(outline.len(), 4);
|
||||
|
||||
// Assert the first length-wise piece of the outline is 10 units from the line
|
||||
assert!(f64_compare(outline[0].evaluate(0.25).distance(line.evaluate(0.25)), 10., MAX_ABSOLUTE_DIFFERENCE)); // f64
|
||||
assert!(f64_compare(
|
||||
outline[0].evaluate(ComputeType::Parametric(0.25)).distance(line.evaluate(ComputeType::Parametric(0.25))),
|
||||
10.,
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)); // f64
|
||||
|
||||
// Assert the first cap touches the line end point at the halfway point
|
||||
assert!(outline[1].evaluate(0.5).abs_diff_eq(line.end(), MAX_ABSOLUTE_DIFFERENCE));
|
||||
assert!(outline[1].evaluate(ComputeType::Parametric(0.5)).abs_diff_eq(line.end(), MAX_ABSOLUTE_DIFFERENCE));
|
||||
|
||||
// Assert the second length-wise piece of the outline is 10 units from the line
|
||||
assert!(f64_compare(outline[2].evaluate(0.25).distance(line.evaluate(0.75)), 10., MAX_ABSOLUTE_DIFFERENCE)); // f64
|
||||
assert!(f64_compare(
|
||||
outline[2].evaluate(ComputeType::Parametric(0.25)).distance(line.evaluate(ComputeType::Parametric(0.75))),
|
||||
10.,
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
)); // f64
|
||||
|
||||
// Assert the second cap touches the line start point at the halfway point
|
||||
assert!(outline[3].evaluate(0.5).abs_diff_eq(line.start(), MAX_ABSOLUTE_DIFFERENCE));
|
||||
assert!(outline[3].evaluate(ComputeType::Parametric(0.5)).abs_diff_eq(line.start(), MAX_ABSOLUTE_DIFFERENCE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -767,9 +777,21 @@ mod tests {
|
||||
dbg!(scaled_bezier);
|
||||
|
||||
// Assert the scaled bezier is 30 units from the line
|
||||
assert!(f64_compare(scaled_bezier.evaluate(0.).distance(bezier.evaluate(0.)), 30., MAX_ABSOLUTE_DIFFERENCE));
|
||||
assert!(f64_compare(scaled_bezier.evaluate(1.).distance(bezier.evaluate(1.)), 30., MAX_ABSOLUTE_DIFFERENCE));
|
||||
assert!(f64_compare(scaled_bezier.evaluate(0.5).distance(bezier.evaluate(0.5)), 30., MAX_ABSOLUTE_DIFFERENCE));
|
||||
assert!(f64_compare(
|
||||
scaled_bezier.evaluate(ComputeType::Parametric(0.)).distance(bezier.evaluate(ComputeType::Parametric(0.))),
|
||||
30.,
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
));
|
||||
assert!(f64_compare(
|
||||
scaled_bezier.evaluate(ComputeType::Parametric(1.)).distance(bezier.evaluate(ComputeType::Parametric(1.))),
|
||||
30.,
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
));
|
||||
assert!(f64_compare(
|
||||
scaled_bezier.evaluate(ComputeType::Parametric(0.5)).distance(bezier.evaluate(ComputeType::Parametric(0.5))),
|
||||
30.,
|
||||
MAX_ABSOLUTE_DIFFERENCE
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -7,3 +7,4 @@ mod utils;
|
||||
|
||||
pub use bezier::*;
|
||||
pub use subpath::*;
|
||||
pub use utils::ComputeType;
|
||||
|
||||
@@ -3,6 +3,13 @@ use crate::consts::{MAX_ABSOLUTE_DIFFERENCE, STRICT_MAX_ABSOLUTE_DIFFERENCE};
|
||||
use glam::{BVec2, DMat2, DVec2};
|
||||
use std::f64::consts::PI;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum ComputeType {
|
||||
Parametric(f64),
|
||||
Euclidean(f64),
|
||||
EuclideanWithinError { t: f64, epsilon: f64 },
|
||||
}
|
||||
|
||||
/// Helper to perform the computation of a and c, where b is the provided point on the curve.
|
||||
/// Given the correct power of `t` and `(1-t)`, the computation is the same for quadratic and cubic cases.
|
||||
/// Relevant derivation and the definitions of a, b, and c can be found in [the projection identity section](https://pomax.github.io/bezierinfo/#abc) of Pomax's bezier curve primer.
|
||||
|
||||
Reference in New Issue
Block a user