mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-09 21:30:55 +08:00
Attribute-based vector format refactor (#1624)
* Initial vector format structure * Click targets * Code review pass * Remove subpaths from vector data * Morph node & vector node tests * Insignificant change --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Keavon Chambers
parent
c8ea9e05a6
commit
218e9675fd
@@ -1,4 +1,4 @@
|
||||
use crate::utils::{f64_compare, TValue, TValueType};
|
||||
use crate::utils::{TValue, TValueType};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -21,44 +21,57 @@ impl Bezier {
|
||||
return 1.;
|
||||
}
|
||||
|
||||
let mut low = 0.;
|
||||
let mut mid = 0.5;
|
||||
let mut high = 1.;
|
||||
match self.handles {
|
||||
BezierHandles::Linear => euclidean_t,
|
||||
BezierHandles::Quadratic { handle } => {
|
||||
// Use Casteljau subdivision, noting that the length is more than the straight line distance from start to end but less than the straight line distance through the handles
|
||||
fn recurse(a0: DVec2, a1: DVec2, a2: DVec2, level: u8, desired_len: f64) -> (f64, f64) {
|
||||
let lower = a0.distance(a2);
|
||||
let upper = a0.distance(a1) + a1.distance(a2);
|
||||
if level >= 8 {
|
||||
let approx_len = (lower + upper) / 2.;
|
||||
return (approx_len, desired_len / approx_len);
|
||||
}
|
||||
|
||||
// The euclidean t-value input generally correlates with the parametric t-value result.
|
||||
// So we can assume a low t-value has a short length from the start of the curve, and a high t-value has a short length from the end of the curve.
|
||||
// We'll use a strategy where we measure from either end of the curve depending on which side is closer than thus more likely to be proximate to the sought parametric t-value.
|
||||
// This allows us to use fewer segments to approximate the curve, which usually won't go much beyond half the curve.
|
||||
let result_likely_closer_to_start = euclidean_t < 0.5;
|
||||
// If the curve is near either end, we need even fewer segments to approximate the curve with reasonable accuracy.
|
||||
// A point that's likely near the center is the worst case where we need to use up to half the predefined number of max subdivisions.
|
||||
let subdivisions_proportional_to_likely_length = ((euclidean_t - 0.5).abs() * DEFAULT_LENGTH_SUBDIVISIONS as f64).round().max(1.) as usize;
|
||||
let b1 = 0.5 * (a0 + a1);
|
||||
let c1 = 0.5 * (a1 + a2);
|
||||
let b2 = 0.5 * (b1 + c1);
|
||||
let (first_len, t) = recurse(a0, b1, b2, level + 1, desired_len);
|
||||
if first_len > desired_len {
|
||||
return (first_len, t * 0.5);
|
||||
}
|
||||
let (second_len, t) = recurse(b2, c1, a2, level + 1, desired_len - first_len);
|
||||
(first_len + second_len, t * 0.5 + 0.5)
|
||||
}
|
||||
recurse(self.start, handle, self.end, 0, total_length * euclidean_t).1
|
||||
}
|
||||
BezierHandles::Cubic { handle_start, handle_end } => {
|
||||
// Use Casteljau subdivision, noting that the length is more than the straight line distance from start to end but less than the straight line distance through the handles
|
||||
fn recurse(a0: DVec2, a1: DVec2, a2: DVec2, a3: DVec2, level: u8, desired_len: f64) -> (f64, f64) {
|
||||
let lower = a0.distance(a3);
|
||||
let upper = a0.distance(a1) + a1.distance(a2) + a2.distance(a3);
|
||||
if level >= 8 {
|
||||
let approx_len = (lower + upper) / 2.;
|
||||
return (approx_len, desired_len / approx_len);
|
||||
}
|
||||
|
||||
// Binary search for the parametric t-value that corresponds to the euclidean distance ratio by trimming the curve between the start and the tested parametric t-value during each iteration of the search.
|
||||
while low < high {
|
||||
mid = (low + high) / 2.;
|
||||
|
||||
// We can search from the curve start to the sought point, or from the sought point to the curve end, depending on which side is likely closer to the result.
|
||||
let current_length = if result_likely_closer_to_start {
|
||||
let trimmed = self.trim(TValue::Parametric(0.), TValue::Parametric(mid));
|
||||
trimmed.length(Some(subdivisions_proportional_to_likely_length))
|
||||
} else {
|
||||
let trimmed = self.trim(TValue::Parametric(mid), TValue::Parametric(1.));
|
||||
let trimmed_length = trimmed.length(Some(subdivisions_proportional_to_likely_length));
|
||||
total_length - trimmed_length
|
||||
};
|
||||
let current_euclidean_t = current_length / total_length;
|
||||
|
||||
if f64_compare(current_euclidean_t, euclidean_t, error) {
|
||||
break;
|
||||
} else if current_euclidean_t < euclidean_t {
|
||||
low = mid;
|
||||
} else {
|
||||
high = mid;
|
||||
let b1 = 0.5 * (a0 + a1);
|
||||
let t0 = 0.5 * (a1 + a2);
|
||||
let c1 = 0.5 * (a2 + a3);
|
||||
let b2 = 0.5 * (b1 + t0);
|
||||
let c2 = 0.5 * (t0 + c1);
|
||||
let b3 = 0.5 * (b2 + c2);
|
||||
let (first_len, t) = recurse(a0, b1, b2, b3, level + 1, desired_len);
|
||||
if first_len > desired_len {
|
||||
return (first_len, t * 0.5);
|
||||
}
|
||||
let (second_len, t) = recurse(b3, c2, c1, a3, level + 1, desired_len - first_len);
|
||||
(first_len + second_len, t * 0.5 + 0.5)
|
||||
}
|
||||
recurse(self.start, handle_start, handle_end, self.end, 0, total_length * euclidean_t).1
|
||||
}
|
||||
}
|
||||
|
||||
mid
|
||||
.clamp(0., 1.)
|
||||
}
|
||||
|
||||
/// Convert a [TValue] to a parametric `t`-value.
|
||||
@@ -109,133 +122,86 @@ impl Bezier {
|
||||
/// Return a selection of equidistant points on the bezier curve.
|
||||
/// If no value is provided for `steps`, then the function will default `steps` to be 10.
|
||||
/// <iframe frameBorder="0" width="100%" height="350px" src="https://graphite.rs/libraries/bezier-rs#bezier/lookup-table/solo" title="Lookup-Table Demo"></iframe>
|
||||
pub fn compute_lookup_table(&self, steps: Option<usize>, tvalue_type: Option<TValueType>) -> Vec<DVec2> {
|
||||
pub fn compute_lookup_table(&self, steps: Option<usize>, tvalue_type: Option<TValueType>) -> impl Iterator<Item = DVec2> + '_ {
|
||||
let steps = steps.unwrap_or(DEFAULT_LUT_STEP_SIZE);
|
||||
let tvalue_type = tvalue_type.unwrap_or(TValueType::Parametric);
|
||||
|
||||
(0..=steps)
|
||||
.map(|t| {
|
||||
let tvalue = match tvalue_type {
|
||||
TValueType::Parametric => TValue::Parametric(t as f64 / steps as f64),
|
||||
TValueType::Euclidean => TValue::Euclidean(t as f64 / steps as f64),
|
||||
};
|
||||
self.evaluate(tvalue)
|
||||
})
|
||||
.collect()
|
||||
(0..=steps).map(move |t| {
|
||||
let tvalue = match tvalue_type {
|
||||
TValueType::Parametric => TValue::Parametric(t as f64 / steps as f64),
|
||||
TValueType::Euclidean => TValue::Euclidean(t as f64 / steps as f64),
|
||||
};
|
||||
self.evaluate(tvalue)
|
||||
})
|
||||
}
|
||||
|
||||
/// Return an approximation of the length of the bezier curve.
|
||||
/// - `num_subdivisions` - Number of subdivisions used to approximate the curve. The default value is 1000.
|
||||
/// - `tolerance` - Tolerance used to approximate the curve.
|
||||
/// <iframe frameBorder="0" width="100%" height="300px" src="https://graphite.rs/libraries/bezier-rs#bezier/length/solo" title="Length Demo"></iframe>
|
||||
pub fn length(&self, num_subdivisions: Option<usize>) -> f64 {
|
||||
pub fn length(&self, tolerance: Option<f64>) -> f64 {
|
||||
match self.handles {
|
||||
BezierHandles::Linear => (self.start - self.end).length(),
|
||||
_ => {
|
||||
// Code example from <https://gamedev.stackexchange.com/questions/5373/moving-ships-between-two-planets-along-a-bezier-missing-some-equations-for-acce/5427#5427>.
|
||||
BezierHandles::Quadratic { handle } => {
|
||||
// Use Casteljau subdivision, noting that the length is more than the straight line distance from start to end but less than the straight line distance through the handles
|
||||
fn recurse(a0: DVec2, a1: DVec2, a2: DVec2, tolerance: f64, level: u8) -> f64 {
|
||||
let lower = a0.distance(a2);
|
||||
let upper = a0.distance(a1) + a1.distance(a2);
|
||||
if upper - lower <= 2. * tolerance || level >= 8 {
|
||||
return (lower + upper) / 2.;
|
||||
}
|
||||
|
||||
// We will use an approximate approach where we split the curve into many subdivisions
|
||||
// and calculate the euclidean distance between the two endpoints of the subdivision
|
||||
let lookup_table = self.compute_lookup_table(Some(num_subdivisions.unwrap_or(DEFAULT_LENGTH_SUBDIVISIONS)), Some(TValueType::Parametric));
|
||||
let approx_curve_length: f64 = lookup_table.windows(2).map(|points| (points[1] - points[0]).length()).sum();
|
||||
let b1 = 0.5 * (a0 + a1);
|
||||
let c1 = 0.5 * (a1 + a2);
|
||||
let b2 = 0.5 * (b1 + c1);
|
||||
recurse(a0, b1, b2, 0.5 * tolerance, level + 1) + recurse(b2, c1, a2, 0.5 * tolerance, level + 1)
|
||||
}
|
||||
recurse(self.start, handle, self.end, tolerance.unwrap_or_default(), 0)
|
||||
}
|
||||
BezierHandles::Cubic { handle_start, handle_end } => {
|
||||
// Use Casteljau subdivision, noting that the length is more than the straight line distance from start to end but less than the straight line distance through the handles
|
||||
fn recurse(a0: DVec2, a1: DVec2, a2: DVec2, a3: DVec2, tolerance: f64, level: u8) -> f64 {
|
||||
let lower = a0.distance(a3);
|
||||
let upper = a0.distance(a1) + a1.distance(a2) + a2.distance(a3);
|
||||
if upper - lower <= 2. * tolerance || level >= 8 {
|
||||
return (lower + upper) / 2.;
|
||||
}
|
||||
|
||||
approx_curve_length
|
||||
let b1 = 0.5 * (a0 + a1);
|
||||
let t0 = 0.5 * (a1 + a2);
|
||||
let c1 = 0.5 * (a2 + a3);
|
||||
let b2 = 0.5 * (b1 + t0);
|
||||
let c2 = 0.5 * (t0 + c1);
|
||||
let b3 = 0.5 * (b2 + c2);
|
||||
recurse(a0, b1, b2, b3, 0.5 * tolerance, level + 1) + recurse(b3, c2, c1, a3, 0.5 * tolerance, level + 1)
|
||||
}
|
||||
recurse(self.start, handle_start, handle_end, self.end, tolerance.unwrap_or_default(), 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the parametric `t`-value that corresponds to the closest point on the curve to the provided point.
|
||||
/// Uses a searching algorithm akin to binary search that can be customized using the optional [ProjectionOptions] struct.
|
||||
/// <iframe frameBorder="0" width="100%" height="300px" src="https://graphite.rs/libraries/bezier-rs#bezier/project/solo" title="Project Demo"></iframe>
|
||||
pub fn project(&self, point: DVec2, options: Option<ProjectionOptions>) -> f64 {
|
||||
let options = options.unwrap_or_default();
|
||||
let ProjectionOptions {
|
||||
lut_size,
|
||||
convergence_epsilon,
|
||||
convergence_limit,
|
||||
iteration_limit,
|
||||
} = options;
|
||||
pub fn project(&self, point: DVec2) -> f64 {
|
||||
let sbasis = crate::symmetrical_basis::to_symmetrical_basis_pair(*self);
|
||||
let derivative = sbasis.derivative();
|
||||
let dd = (sbasis - point).dot(&derivative);
|
||||
let roots = dd.roots();
|
||||
|
||||
// TODO: Consider optimizations from precomputing useful values, or using the GPU
|
||||
// First find the closest point from the results of a lookup table
|
||||
let lut = self.compute_lookup_table(Some(lut_size), Some(TValueType::Parametric));
|
||||
let (minimum_position, minimum_distance) = utils::get_closest_point_in_lut(&lut, point);
|
||||
let mut closest = 0.;
|
||||
let mut min_dist_squared = self.evaluate(TValue::Parametric(0.)).distance_squared(point);
|
||||
|
||||
// Get the t values to the left and right of the closest result in the lookup table
|
||||
let lut_size_f64 = lut_size as f64;
|
||||
let minimum_position_f64 = minimum_position as f64;
|
||||
let mut left_t = (minimum_position_f64 - 1.).max(0.) / lut_size_f64;
|
||||
let mut right_t = (minimum_position_f64 + 1.).min(lut_size_f64) / lut_size_f64;
|
||||
|
||||
// Perform a finer search by finding closest t from 5 points between [left_t, right_t] inclusive
|
||||
// Choose new left_t and right_t for a smaller range around the closest t and repeat the process
|
||||
let mut final_t = left_t;
|
||||
let mut distance;
|
||||
|
||||
// Increment minimum_distance to ensure that the distance < minimum_distance comparison will be true for at least one iteration
|
||||
let mut new_minimum_distance = minimum_distance + 1.;
|
||||
// Maintain the previous distance to identify convergence
|
||||
let mut previous_distance;
|
||||
// Counter to limit the number of iterations
|
||||
let mut iteration_count = 0;
|
||||
// Counter to identify how many iterations have had a similar result. Used for convergence test
|
||||
let mut convergence_count = 0;
|
||||
|
||||
// Store calculated distances to minimize unnecessary recomputations
|
||||
let mut distances: [f64; NUM_DISTANCES] = [
|
||||
point.distance(lut[(minimum_position as i64 - 1).max(0) as usize]),
|
||||
0.,
|
||||
0.,
|
||||
0.,
|
||||
point.distance(lut[lut_size.min(minimum_position + 1)]),
|
||||
];
|
||||
|
||||
while left_t <= right_t && convergence_count < convergence_limit && iteration_count < iteration_limit {
|
||||
previous_distance = new_minimum_distance;
|
||||
let step = (right_t - left_t) / (NUM_DISTANCES as f64 - 1.);
|
||||
let mut iterator_t = left_t;
|
||||
let mut target_index = 0;
|
||||
// Iterate through first 4 points and will handle the right most point later
|
||||
for (step_index, table_distance) in distances.iter_mut().enumerate().take(4) {
|
||||
// Use previously computed distance for the left most point, and compute new values for the others
|
||||
if step_index == 0 {
|
||||
distance = *table_distance;
|
||||
} else {
|
||||
distance = point.distance(self.evaluate(TValue::Parametric(iterator_t)));
|
||||
*table_distance = distance;
|
||||
}
|
||||
if distance < new_minimum_distance {
|
||||
new_minimum_distance = distance;
|
||||
target_index = step_index;
|
||||
final_t = iterator_t
|
||||
}
|
||||
iterator_t += step;
|
||||
}
|
||||
// Check right most edge separately since step may not perfectly add up to it (floating point errors)
|
||||
if distances[NUM_DISTANCES - 1] < new_minimum_distance {
|
||||
new_minimum_distance = distances[NUM_DISTANCES - 1];
|
||||
final_t = right_t;
|
||||
}
|
||||
|
||||
// Update left_t and right_t to be the t values (final_t +/- step), while handling the edges (i.e. if final_t is 0, left_t will be 0 instead of -step)
|
||||
// Ensure that the t values never exceed the [0, 1] range
|
||||
left_t = (final_t - step).max(0.);
|
||||
right_t = (final_t + step).min(1.);
|
||||
|
||||
// Re-use the corresponding computed distances (target_index is the index corresponding to final_t)
|
||||
// Since target_index is a u_size, can't subtract one if it is zero
|
||||
distances[0] = distances[if target_index == 0 { 0 } else { target_index - 1 }];
|
||||
distances[NUM_DISTANCES - 1] = distances[(target_index + 1).min(NUM_DISTANCES - 1)];
|
||||
|
||||
iteration_count += 1;
|
||||
// update count for consecutive iterations of similar minimum distances
|
||||
if previous_distance - new_minimum_distance < convergence_epsilon {
|
||||
convergence_count += 1;
|
||||
} else {
|
||||
convergence_count = 0;
|
||||
for time in roots {
|
||||
let distance = self.evaluate(TValue::Parametric(time)).distance_squared(point);
|
||||
if distance < min_dist_squared {
|
||||
closest = time;
|
||||
min_dist_squared = distance;
|
||||
}
|
||||
}
|
||||
|
||||
final_t
|
||||
if self.evaluate(TValue::Parametric(1.)).distance_squared(point) < min_dist_squared {
|
||||
closest = 1.;
|
||||
}
|
||||
closest
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,11 +225,11 @@ mod tests {
|
||||
#[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), Some(TValueType::Parametric));
|
||||
let lookup_table1 = bezier1.compute_lookup_table(Some(2), Some(TValueType::Parametric)).collect::<Vec<_>>();
|
||||
assert_eq!(lookup_table1, vec![bezier1.start(), bezier1.evaluate(TValue::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), Some(TValueType::Parametric));
|
||||
let lookup_table2 = bezier2.compute_lookup_table(Some(4), Some(TValueType::Parametric)).collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
lookup_table2,
|
||||
vec![
|
||||
@@ -296,10 +262,10 @@ mod tests {
|
||||
#[test]
|
||||
fn test_project() {
|
||||
let bezier1 = Bezier::from_cubic_coordinates(4., 4., 23., 45., 10., 30., 56., 90.);
|
||||
assert_eq!(bezier1.project(DVec2::ZERO, None), 0.);
|
||||
assert_eq!(bezier1.project(DVec2::new(100., 100.), None), 1.);
|
||||
assert_eq!(bezier1.project(DVec2::ZERO), 0.);
|
||||
assert_eq!(bezier1.project(DVec2::new(100., 100.)), 1.);
|
||||
|
||||
let bezier2 = Bezier::from_quadratic_coordinates(0., 0., 0., 100., 100., 100.);
|
||||
assert_eq!(bezier2.project(DVec2::new(100., 0.), None), 0.);
|
||||
assert_eq!(bezier2.project(DVec2::new(100., 0.)), 0.);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user