mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-09 01:08:11 +08:00
Bezier-rs: Updated Bezier function signatures to accept TValue (#967)
* Create helper for converting d to t values * Add euclidean option for tangent and normal * Modified bezier functions signatures to accept ComputeType * Stylistic changes per review * Added ComputeType documentation * Renamed ComputeType to TValue * Fixed comments * Fixed failing unit tests * Code review * Fix comments in code review * Renamed compute_type_to_parametric to t_value_to_parametric --------- Co-authored-by: Linda Zheng <thelindazheng@gmail.com> Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Linda Zheng
Keavon Chambers
parent
1c2b8f67b2
commit
76be1f8515
@@ -1,5 +1,5 @@
|
||||
use crate::svg_drawing::*;
|
||||
use bezier_rs::{ArcStrategy, ArcsOptions, Bezier, ComputeType, ProjectionOptions};
|
||||
use bezier_rs::{ArcStrategy, ArcsOptions, Bezier, ProjectionOptions, TValue};
|
||||
use glam::DVec2;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
@@ -41,6 +41,14 @@ fn convert_wasm_maximize_arcs(wasm_enum_value: WasmMaximizeArcs) -> ArcStrategy
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_t_variant(t_variant: &String, t: f64) -> TValue {
|
||||
match t_variant.as_str() {
|
||||
"Parametric" => TValue::Parametric(t),
|
||||
"Euclidean" => TValue::Euclidean(t),
|
||||
_ => panic!("Unexpected TValue string: '{}'", t_variant),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl WasmBezier {
|
||||
/// Expect js_points to be a list of 2 pairs.
|
||||
@@ -128,13 +136,10 @@ impl WasmBezier {
|
||||
wrap_svg_tag(format!("{bezier}{}", draw_text(format!("Length: {:.2}", self.0.length(None)), TEXT_OFFSET_X, TEXT_OFFSET_Y, BLACK)))
|
||||
}
|
||||
|
||||
pub fn evaluate(&self, t: f64, compute_type: String) -> String {
|
||||
pub fn evaluate(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let bezier = self.get_bezier_path();
|
||||
let point = match compute_type.as_str() {
|
||||
"Euclidean" => self.0.evaluate(ComputeType::Euclidean(t)),
|
||||
"Parametric" => self.0.evaluate(ComputeType::Parametric(t)),
|
||||
_ => panic!("Unexpected ComputeType string: '{}'", compute_type),
|
||||
};
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
let point = self.0.evaluate(t);
|
||||
let content = format!("{bezier}{}", draw_circle(point, 4., RED, 1.5, WHITE));
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
@@ -169,11 +174,12 @@ impl WasmBezier {
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
|
||||
pub fn tangent(&self, t: f64) -> String {
|
||||
pub fn tangent(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let bezier = self.get_bezier_path();
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
|
||||
let tangent_point = self.0.tangent(t);
|
||||
let intersection_point = self.0.evaluate(ComputeType::Parametric(t));
|
||||
let intersection_point = self.0.evaluate(t);
|
||||
let tangent_end = intersection_point + tangent_point * SCALE_UNIT_VECTOR_FACTOR;
|
||||
|
||||
let content = format!(
|
||||
@@ -185,11 +191,12 @@ impl WasmBezier {
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
|
||||
pub fn normal(&self, t: f64) -> String {
|
||||
pub fn normal(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let bezier = self.get_bezier_path();
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
|
||||
let normal_point = self.0.normal(t);
|
||||
let intersection_point = self.0.evaluate(ComputeType::Parametric(t));
|
||||
let intersection_point = self.0.evaluate(t);
|
||||
let normal_end = intersection_point + normal_point * SCALE_UNIT_VECTOR_FACTOR;
|
||||
|
||||
let content = format!(
|
||||
@@ -201,11 +208,13 @@ impl WasmBezier {
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
|
||||
pub fn curvature(&self, t: f64) -> String {
|
||||
pub fn curvature(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let bezier = self.get_bezier_path();
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
|
||||
let radius = 1. / self.0.curvature(t);
|
||||
let normal_point = self.0.normal(t);
|
||||
let intersection_point = self.0.evaluate(ComputeType::Parametric(t));
|
||||
let intersection_point = self.0.evaluate(t);
|
||||
|
||||
let curvature_center = intersection_point + normal_point * radius;
|
||||
|
||||
@@ -219,7 +228,8 @@ impl WasmBezier {
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
|
||||
pub fn split(&self, t: f64) -> String {
|
||||
pub fn split(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
let beziers: [Bezier; 2] = self.0.split(t);
|
||||
|
||||
let mut original_bezier_svg = String::new();
|
||||
@@ -252,7 +262,8 @@ impl WasmBezier {
|
||||
wrap_svg_tag(format!("{original_bezier_svg}{bezier_svg_1}{bezier_svg_2}"))
|
||||
}
|
||||
|
||||
pub fn trim(&self, t1: f64, t2: f64) -> String {
|
||||
pub fn trim(&self, raw_t1: f64, raw_t2: f64, t_variant: String) -> String {
|
||||
let (t1, t2) = (parse_t_variant(&t_variant, raw_t1), parse_t_variant(&t_variant, raw_t2));
|
||||
let trimmed_bezier = self.0.trim(t1, t2);
|
||||
|
||||
let mut trimmed_bezier_svg = String::new();
|
||||
@@ -269,7 +280,7 @@ impl WasmBezier {
|
||||
|
||||
pub fn project(&self, x: f64, y: f64) -> String {
|
||||
let projected_t_value = self.0.project(DVec2::new(x, y), ProjectionOptions::default());
|
||||
let projected_point = self.0.evaluate(ComputeType::Parametric(projected_t_value));
|
||||
let projected_point = self.0.evaluate(TValue::Parametric(projected_t_value));
|
||||
|
||||
let bezier = self.get_bezier_path();
|
||||
let content = format!("{bezier}{}", draw_line(projected_point.x, projected_point.y, x, y, RED, 1.),);
|
||||
@@ -285,7 +296,7 @@ impl WasmBezier {
|
||||
.zip([RED, GREEN])
|
||||
.flat_map(|(t_value_list, color)| {
|
||||
t_value_list.iter().map(|&t_value| {
|
||||
let point = self.0.evaluate(ComputeType::Parametric(t_value));
|
||||
let point = self.0.evaluate(TValue::Parametric(t_value));
|
||||
draw_circle(point, 3., color, 1.5, WHITE)
|
||||
})
|
||||
})
|
||||
@@ -320,7 +331,7 @@ impl WasmBezier {
|
||||
let circles: String = inflections
|
||||
.iter()
|
||||
.map(|&t_value| {
|
||||
let point = self.0.evaluate(ComputeType::Parametric(t_value));
|
||||
let point = self.0.evaluate(TValue::Parametric(t_value));
|
||||
draw_circle(point, 3., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold("".to_string(), |acc, circle| acc + &circle);
|
||||
@@ -328,7 +339,8 @@ impl WasmBezier {
|
||||
wrap_svg_tag(content)
|
||||
}
|
||||
|
||||
pub fn de_casteljau_points(&self, t: f64) -> String {
|
||||
pub fn de_casteljau_points(&self, raw_t: f64, t_variant: String) -> String {
|
||||
let t = parse_t_variant(&t_variant, raw_t);
|
||||
let points: Vec<Vec<DVec2>> = self.0.de_casteljau_points(t);
|
||||
|
||||
let bezier_svg = self.get_bezier_path();
|
||||
@@ -413,7 +425,7 @@ impl WasmBezier {
|
||||
.intersect(&line, None, None)
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = &self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = &self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(*point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -433,7 +445,7 @@ impl WasmBezier {
|
||||
.intersect(&quadratic, Some(error), Some(minimum_separation))
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = &self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = &self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(*point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -453,7 +465,7 @@ impl WasmBezier {
|
||||
.intersect(&cubic, Some(error), Some(minimum_separation))
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = &self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = &self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(*point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -469,7 +481,7 @@ impl WasmBezier {
|
||||
.self_intersections(Some(error))
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = &self.0.evaluate(ComputeType::Parametric(intersection_t[0]));
|
||||
let point = &self.0.evaluate(TValue::Parametric(intersection_t[0]));
|
||||
draw_circle(*point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(bezier_curve_svg, |acc, item| format!("{acc}{item}"));
|
||||
@@ -497,7 +509,7 @@ impl WasmBezier {
|
||||
.rectangle_intersections(points[0], points[1])
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = &self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = &self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(*point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::svg_drawing::*;
|
||||
|
||||
use bezier_rs::{Bezier, ComputeType, ManipulatorGroup, ProjectionOptions, Subpath};
|
||||
use bezier_rs::{Bezier, ManipulatorGroup, ProjectionOptions, Subpath, TValue};
|
||||
|
||||
use glam::DVec2;
|
||||
use std::fmt::Write;
|
||||
@@ -56,20 +56,20 @@ impl WasmSubpath {
|
||||
subpath_svg
|
||||
}
|
||||
|
||||
pub fn insert(&self, t: f64, compute_type: String) -> String {
|
||||
pub fn insert(&self, t: f64, t_variant: String) -> String {
|
||||
let mut subpath = self.0.clone();
|
||||
let point = match compute_type.as_str() {
|
||||
let point = match t_variant.as_str() {
|
||||
"Euclidean" => {
|
||||
let parameter = ComputeType::Euclidean(t);
|
||||
let parameter = TValue::Euclidean(t);
|
||||
subpath.insert(parameter);
|
||||
self.0.evaluate(parameter)
|
||||
}
|
||||
"Parametric" => {
|
||||
let parameter = ComputeType::Parametric(t);
|
||||
let parameter = TValue::Parametric(t);
|
||||
subpath.insert(parameter);
|
||||
self.0.evaluate(parameter)
|
||||
}
|
||||
_ => panic!("Unexpected ComputeType string: '{}'", compute_type),
|
||||
_ => panic!("Unexpected TValue string: '{}'", t_variant),
|
||||
};
|
||||
let point_text = draw_circle(point, 4., RED, 1.5, WHITE);
|
||||
|
||||
@@ -81,19 +81,19 @@ impl WasmSubpath {
|
||||
wrap_svg_tag(format!("{}{}", self.to_default_svg(), length_text))
|
||||
}
|
||||
|
||||
pub fn evaluate(&self, t: f64, compute_type: String) -> String {
|
||||
let point = match compute_type.as_str() {
|
||||
"Euclidean" => self.0.evaluate(ComputeType::Euclidean(t)),
|
||||
"Parametric" => self.0.evaluate(ComputeType::Parametric(t)),
|
||||
_ => panic!("Unexpected ComputeType string: '{}'", compute_type),
|
||||
pub fn evaluate(&self, t: f64, t_variant: String) -> String {
|
||||
let point = match t_variant.as_str() {
|
||||
"Euclidean" => self.0.evaluate(TValue::Euclidean(t)),
|
||||
"Parametric" => self.0.evaluate(TValue::Parametric(t)),
|
||||
_ => panic!("Unexpected TValue string: '{}'", t_variant),
|
||||
};
|
||||
let point_text = draw_circle(point, 4., RED, 1.5, WHITE);
|
||||
wrap_svg_tag(format!("{}{}", self.to_default_svg(), point_text))
|
||||
}
|
||||
|
||||
pub fn tangent(&self, t: f64) -> String {
|
||||
let intersection_point = self.0.evaluate(ComputeType::Parametric(t));
|
||||
let tangent_point = self.0.tangent(ComputeType::Parametric(t));
|
||||
let intersection_point = self.0.evaluate(TValue::Parametric(t));
|
||||
let tangent_point = self.0.tangent(TValue::Parametric(t));
|
||||
let tangent_end = intersection_point + tangent_point * SCALE_UNIT_VECTOR_FACTOR;
|
||||
|
||||
let point_text = draw_circle(intersection_point, 4., RED, 1.5, WHITE);
|
||||
@@ -103,8 +103,8 @@ impl WasmSubpath {
|
||||
}
|
||||
|
||||
pub fn normal(&self, t: f64) -> String {
|
||||
let intersection_point = self.0.evaluate(ComputeType::Parametric(t));
|
||||
let normal_point = self.0.normal(ComputeType::Parametric(t));
|
||||
let intersection_point = self.0.evaluate(TValue::Parametric(t));
|
||||
let normal_point = self.0.normal(TValue::Parametric(t));
|
||||
let normal_end = intersection_point + normal_point * SCALE_UNIT_VECTOR_FACTOR;
|
||||
|
||||
let point_text = draw_circle(intersection_point, 4., RED, 1.5, WHITE);
|
||||
@@ -115,7 +115,7 @@ impl WasmSubpath {
|
||||
|
||||
pub fn project(&self, x: f64, y: f64) -> String {
|
||||
let (segment_index, projected_t) = self.0.project(DVec2::new(x, y), ProjectionOptions::default()).unwrap();
|
||||
let projected_point = self.0.evaluate(ComputeType::Parametric((segment_index as f64 + projected_t) / (self.0.len_segments() as f64)));
|
||||
let projected_point = self.0.evaluate(TValue::Parametric((segment_index as f64 + projected_t) / (self.0.len_segments() as f64)));
|
||||
|
||||
let subpath_svg = self.to_default_svg();
|
||||
let content = format!("{subpath_svg}{}", draw_line(projected_point.x, projected_point.y, x, y, RED, 1.),);
|
||||
@@ -143,7 +143,7 @@ impl WasmSubpath {
|
||||
.intersections(&line, None, None)
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -172,7 +172,7 @@ impl WasmSubpath {
|
||||
.intersections(&line, None, None)
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -201,7 +201,7 @@ impl WasmSubpath {
|
||||
.intersections(&line, None, None)
|
||||
.iter()
|
||||
.map(|intersection_t| {
|
||||
let point = self.0.evaluate(ComputeType::Parametric(*intersection_t));
|
||||
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
|
||||
draw_circle(point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
@@ -209,11 +209,11 @@ impl WasmSubpath {
|
||||
wrap_svg_tag(format!("{subpath_svg}{line_svg}{intersections_svg}"))
|
||||
}
|
||||
|
||||
pub fn split(&self, t: f64, compute_type: String) -> String {
|
||||
let (main_subpath, optional_subpath) = match compute_type.as_str() {
|
||||
"Euclidean" => self.0.split(ComputeType::Euclidean(t)),
|
||||
"Parametric" => self.0.split(ComputeType::Parametric(t)),
|
||||
_ => panic!("Unexpected ComputeType string: '{}'", compute_type),
|
||||
pub fn split(&self, t: f64, t_variant: String) -> String {
|
||||
let (main_subpath, optional_subpath) = match t_variant.as_str() {
|
||||
"Euclidean" => self.0.split(TValue::Euclidean(t)),
|
||||
"Parametric" => self.0.split(TValue::Parametric(t)),
|
||||
_ => panic!("Unexpected ComputeType string: '{}'", t_variant),
|
||||
};
|
||||
|
||||
let mut main_subpath_svg = String::new();
|
||||
|
||||
Reference in New Issue
Block a user