Bezier-rs: Add SubpathTValue and euclidean parameterization for subpaths (#1027)

* Added SubpathTValue and euclidean parameterization for subpaths

* Small fix

* Added bounds checking to get_segment

* Code review

* code review nit for clarity

---------

Co-authored-by: Hannah Li <hannahli2010@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Rob Nadal 2023-02-17 15:33:52 -05:00 committed by Keavon Chambers
parent 344f243432
commit 9a52cae9b9
10 changed files with 334 additions and 247 deletions

View file

@ -10,8 +10,7 @@ const subpathFeatures = {
name: "Insert",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined, tVariant: TVariant): string => subpath.insert(options.t, tVariant),
sliderOptions: [tSliderOptions],
// TODO: Uncomment this after implementing the Euclidean version
// chooseTVariant: true,
chooseTVariant: true,
},
length: {
name: "Length",
@ -31,13 +30,15 @@ const subpathFeatures = {
},
tangent: {
name: "Tangent",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string => subpath.tangent(options.t),
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined, tVariant: TVariant): string => subpath.tangent(options.t, tVariant),
sliderOptions: [tSliderOptions],
chooseTVariant: true,
},
normal: {
name: "Normal",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string => subpath.normal(options.t),
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined, tVariant: TVariant): string => subpath.normal(options.t, tVariant),
sliderOptions: [tSliderOptions],
chooseTVariant: true,
},
"intersect-linear": {
name: "Intersect (Line Segment)",
@ -70,8 +71,7 @@ const subpathFeatures = {
name: "Split",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined, tVariant: TVariant): string => subpath.split(options.t, tVariant),
sliderOptions: [tSliderOptions],
// TODO: Uncomment this after implementing the Euclidean version
// chooseTVariant: true,
chooseTVariant: true,
},
};

View file

@ -1,6 +1,6 @@
use crate::svg_drawing::*;
use bezier_rs::{Bezier, ManipulatorGroup, ProjectionOptions, Subpath, TValue};
use bezier_rs::{Bezier, ManipulatorGroup, ProjectionOptions, Subpath, SubpathTValue};
use glam::DVec2;
use std::fmt::Write;
@ -12,6 +12,14 @@ pub struct WasmSubpath(Subpath);
const SCALE_UNIT_VECTOR_FACTOR: f64 = 50.;
fn parse_t_variant(t_variant: &String, t: f64) -> SubpathTValue {
match t_variant.as_str() {
"Parametric" => SubpathTValue::GlobalParametric(t),
"Euclidean" => SubpathTValue::GlobalEuclidean(t),
_ => panic!("Unexpected TValue string: '{}'", t_variant),
}
}
#[wasm_bindgen]
impl WasmSubpath {
/// Expects js_points to be an unbounded list of triples, where each item is a tuple of floats.
@ -58,21 +66,12 @@ impl WasmSubpath {
pub fn insert(&self, t: f64, t_variant: String) -> String {
let mut subpath = self.0.clone();
let point = match t_variant.as_str() {
"Euclidean" => {
let parameter = TValue::Euclidean(t);
subpath.insert(parameter);
self.0.evaluate(parameter)
}
"Parametric" => {
let parameter = TValue::Parametric(t);
subpath.insert(parameter);
self.0.evaluate(parameter)
}
_ => panic!("Unexpected TValue string: '{}'", t_variant),
};
let point_text = draw_circle(point, 4., RED, 1.5, WHITE);
let t = parse_t_variant(&t_variant, t);
subpath.insert(t);
let point = self.0.evaluate(t);
let point_text = draw_circle(point, 4., RED, 1.5, WHITE);
wrap_svg_tag(format!("{}{}", WasmSubpath(subpath).to_default_svg(), point_text))
}
@ -82,18 +81,18 @@ impl WasmSubpath {
}
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 t = parse_t_variant(&t_variant, t);
let point = self.0.evaluate(t);
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(TValue::Parametric(t));
let tangent_point = self.0.tangent(TValue::Parametric(t));
pub fn tangent(&self, t: f64, t_variant: String) -> String {
let t = parse_t_variant(&t_variant, t);
let intersection_point = self.0.evaluate(t);
let tangent_point = self.0.tangent(t);
let tangent_end = intersection_point + tangent_point * SCALE_UNIT_VECTOR_FACTOR;
let point_text = draw_circle(intersection_point, 4., RED, 1.5, WHITE);
@ -102,9 +101,11 @@ impl WasmSubpath {
wrap_svg_tag(format!("{}{}{}{}", self.to_default_svg(), point_text, line_text, tangent_end_point))
}
pub fn normal(&self, t: f64) -> String {
let intersection_point = self.0.evaluate(TValue::Parametric(t));
let normal_point = self.0.normal(TValue::Parametric(t));
pub fn normal(&self, t: f64, t_variant: String) -> String {
let t = parse_t_variant(&t_variant, t);
let intersection_point = self.0.evaluate(t);
let normal_point = self.0.normal(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 +116,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(TValue::Parametric((segment_index as f64 + projected_t) / (self.0.len_segments() as f64)));
let projected_point = self.0.evaluate(SubpathTValue::Parametric { segment_index, t: projected_t });
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 +144,7 @@ impl WasmSubpath {
.intersections(&line, None, None)
.iter()
.map(|intersection_t| {
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
let point = self.0.evaluate(SubpathTValue::GlobalParametric(*intersection_t));
draw_circle(point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
@ -172,7 +173,7 @@ impl WasmSubpath {
.intersections(&line, None, None)
.iter()
.map(|intersection_t| {
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
let point = self.0.evaluate(SubpathTValue::GlobalParametric(*intersection_t));
draw_circle(point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
@ -201,7 +202,7 @@ impl WasmSubpath {
.intersections(&line, None, None)
.iter()
.map(|intersection_t| {
let point = self.0.evaluate(TValue::Parametric(*intersection_t));
let point = self.0.evaluate(SubpathTValue::GlobalParametric(*intersection_t));
draw_circle(point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
@ -210,11 +211,8 @@ impl WasmSubpath {
}
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 t = parse_t_variant(&t_variant, t);
let (main_subpath, optional_subpath) = self.0.split(t);
let mut main_subpath_svg = String::new();
let mut other_subpath_svg = String::new();