Bezier-rs: Remove unused legacy drawing components and structs (#853)

* Removed unused legacy drawing components

* Removed Point abstraction from frontend code

* Code review fixes

* Helper lambdas to functions

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Rob Nadal
2022-11-18 00:34:52 -08:00
committed by GitHub
co-authored by Keavon Chambers
parent 6cd0dbdcd0
commit 96457bab9f
11 changed files with 47 additions and 660 deletions
@@ -6,7 +6,7 @@ use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize)]
struct CircleSector {
center: Point,
center: DVec2,
radius: f64,
#[serde(rename = "startAngle")]
start_angle: f64,
@@ -14,12 +14,6 @@ struct CircleSector {
end_angle: f64,
}
#[derive(Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
#[wasm_bindgen]
pub enum WasmMaximizeArcs {
Automatic, // 0
@@ -34,11 +28,6 @@ const SCALE_UNIT_VECTOR_FACTOR: f64 = 50.;
#[derive(Clone)]
pub struct WasmBezier(Bezier);
/// Convert a `DVec2` into a `Point`.
fn vec_to_point(p: &DVec2) -> Point {
Point { x: p.x, y: p.y }
}
/// Serialize some data and then convert it to a JsValue.
fn to_js_value<T: Serialize>(data: T) -> JsValue {
JsValue::from_serde(&serde_json::to_string(&data).unwrap()).unwrap()
@@ -85,7 +74,7 @@ impl WasmBezier {
HANDLE_ATTRIBUTES.to_string().replace(GRAY, RED),
HANDLE_LINE_ATTRIBUTES.to_string().replace(GRAY, RED),
);
let through_point_circle = format!(r#"<circle cx="{}" cy="{}" {}/>"#, through_point.x, through_point.y, ANCHOR_ATTRIBUTES.to_string());
let through_point_circle = format!(r#"<circle cx="{}" cy="{}" {}/>"#, through_point.x, through_point.y, ANCHOR_ATTRIBUTES);
wrap_svg_tag(format!("{bezier_string}{through_point_circle}"))
}
@@ -118,10 +107,8 @@ impl WasmBezier {
self.0.set_handle_end(DVec2::new(x, y));
}
/// The wrapped return type is `Vec<Point>`.
pub fn get_points(&self) -> JsValue {
let points: Vec<Point> = self.0.get_points().map(|point| vec_to_point(&point)).collect();
to_js_value(points)
to_js_value(self.0.get_points().collect::<Vec<DVec2>>())
}
fn get_bezier_path(&self) -> String {
@@ -145,25 +132,19 @@ impl WasmBezier {
wrap_svg_tag(format!("{bezier}{}", draw_text(format!("Length: {:.2}", self.0.length(None)), TEXT_OFFSET_X, TEXT_OFFSET_Y, BLACK)))
}
/// The wrapped return type is `Point`.
pub fn evaluate_value(&self, t: f64) -> JsValue {
let point: Point = vec_to_point(&self.0.evaluate(t));
to_js_value(point)
}
pub fn evaluate(&self, t: f64) -> String {
let bezier = self.get_bezier_path();
let point = &self.0.evaluate(t);
let content = format!("{bezier}{}", draw_circle(point.x, point.y, 4., RED, 1.5, WHITE));
let content = format!("{bezier}{}", draw_circle(*point, 4., RED, 1.5, WHITE));
wrap_svg_tag(content)
}
pub fn compute_lookup_table(&self, steps: usize) -> String {
let bezier = self.get_bezier_path();
let table_values: Vec<Point> = self.0.compute_lookup_table(Some(steps)).iter().map(vec_to_point).collect();
let table_values: Vec<DVec2> = self.0.compute_lookup_table(Some(steps));
let circles: String = table_values
.iter()
.map(|point| draw_circle(point.x, point.y, 3., RED, 1.5, WHITE))
.map(|point| draw_circle(*point, 3., RED, 1.5, WHITE))
.fold("".to_string(), |acc, circle| acc + &circle);
let content = format!("{bezier}{circles}");
wrap_svg_tag(content)
@@ -188,7 +169,6 @@ impl WasmBezier {
wrap_svg_tag(content)
}
/// The wrapped return type is `Point`.
pub fn tangent(&self, t: f64) -> String {
let bezier = self.get_bezier_path();
@@ -198,9 +178,9 @@ impl WasmBezier {
let content = format!(
"{bezier}{}{}{}",
draw_circle(intersection_point.x, intersection_point.y, 3., RED, 1., WHITE),
draw_circle(intersection_point, 3., RED, 1., WHITE),
draw_line(intersection_point.x, intersection_point.y, tangent_end.x, tangent_end.y, RED, 1.),
draw_circle(tangent_end.x, tangent_end.y, 3., RED, 1., WHITE),
draw_circle(tangent_end, 3., RED, 1., WHITE),
);
wrap_svg_tag(content)
}
@@ -215,8 +195,8 @@ impl WasmBezier {
let content = format!(
"{bezier}{}{}{}",
draw_line(intersection_point.x, intersection_point.y, normal_end.x, normal_end.y, RED, 1.),
draw_circle(intersection_point.x, intersection_point.y, 3., RED, 1., WHITE),
draw_circle(normal_end.x, normal_end.y, 3., RED, 1., WHITE),
draw_circle(intersection_point, 3., RED, 1., WHITE),
draw_circle(normal_end, 3., RED, 1., WHITE),
);
wrap_svg_tag(content)
}
@@ -231,10 +211,10 @@ impl WasmBezier {
let content = format!(
"{bezier}{}{}{}{}",
draw_circle(curvature_center.x, curvature_center.y, radius.abs(), RED, 1., NONE),
draw_circle(curvature_center, radius.abs(), RED, 1., NONE),
draw_line(intersection_point.x, intersection_point.y, curvature_center.x, curvature_center.y, RED, 1.),
draw_circle(intersection_point.x, intersection_point.y, 3., RED, 1., WHITE),
draw_circle(curvature_center.x, curvature_center.y, 3., RED, 1., WHITE),
draw_circle(intersection_point, 3., RED, 1., WHITE),
draw_circle(curvature_center, 3., RED, 1., WHITE),
);
wrap_svg_tag(content)
}
@@ -306,7 +286,7 @@ impl WasmBezier {
.flat_map(|(t_value_list, color)| {
t_value_list.iter().map(|&t_value| {
let point = self.0.evaluate(t_value);
draw_circle(point.x, point.y, 3., color, 1.5, WHITE)
draw_circle(point, 3., color, 1.5, WHITE)
})
})
.fold("".to_string(), |acc, circle| acc + &circle);
@@ -341,7 +321,7 @@ impl WasmBezier {
.iter()
.map(|&t_value| {
let point = self.0.evaluate(t_value);
draw_circle(point.x, point.y, 3., RED, 1.5, WHITE)
draw_circle(point, 3., RED, 1.5, WHITE)
})
.fold("".to_string(), |acc, circle| acc + &circle);
let content = format!("{bezier}{circles}");
@@ -362,7 +342,7 @@ impl WasmBezier {
.iter()
.enumerate()
.map(|(index, point)| {
let circle = draw_circle(point.x, point.y, 3., &color_light, 1.5, WHITE);
let circle = draw_circle(*point, 3., &color_light, 1.5, WHITE);
if index != 0 {
let prev_point = points[index - 1];
let line = draw_line(prev_point.x, prev_point.y, point.x, point.y, &color_light, 1.5);
@@ -385,7 +365,7 @@ impl WasmBezier {
let rotated_bezier = self.0.rotate_about_point(angle, DVec2::new(pivot_x, pivot_y));
let mut rotated_bezier_svg = String::new();
rotated_bezier.to_svg(&mut rotated_bezier_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new());
let pivot = draw_circle(pivot_x, pivot_y, 3., GRAY, 1.5, WHITE);
let pivot = draw_circle(DVec2::new(pivot_x, pivot_y), 3., GRAY, 1.5, WHITE);
// Line between pivot and start point on curve
let original_dashed_line_start = format!(
@@ -434,7 +414,7 @@ impl WasmBezier {
.iter()
.map(|intersection_t| {
let point = &self.0.evaluate(*intersection_t);
draw_circle(point.x, point.y, 4., RED, 1.5, WHITE)
draw_circle(*point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
wrap_svg_tag(format!("{bezier_curve_svg}{line_svg}{intersections_svg}"))
@@ -454,7 +434,7 @@ impl WasmBezier {
.iter()
.map(|intersection_t| {
let point = &self.0.evaluate(*intersection_t);
draw_circle(point.x, point.y, 4., RED, 1.5, WHITE)
draw_circle(*point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
wrap_svg_tag(format!("{bezier_curve_svg}{quadratic_svg}{intersections_svg}"))
@@ -474,7 +454,7 @@ impl WasmBezier {
.iter()
.map(|intersection_t| {
let point = &self.0.evaluate(*intersection_t);
draw_circle(point.x, point.y, 4., RED, 1.5, WHITE)
draw_circle(*point, 4., RED, 1.5, WHITE)
})
.fold(String::new(), |acc, item| format!("{acc}{item}"));
@@ -490,7 +470,7 @@ impl WasmBezier {
.iter()
.map(|intersection_t| {
let point = &self.0.evaluate(intersection_t[0]);
draw_circle(point.x, point.y, 4., RED, 1.5, WHITE)
draw_circle(*point, 4., RED, 1.5, WHITE)
})
.fold(bezier_curve_svg, |acc, item| format!("{acc}{item}"));
@@ -577,7 +557,6 @@ impl WasmBezier {
wrap_svg_tag(format!("{bezier_svg}{outline_svg}"))
}
/// The wrapped return type is `Vec<CircleSector>`.
pub fn arcs(&self, error: f64, max_iterations: usize, maximize_arcs: WasmMaximizeArcs) -> String {
let original_curve_svg = self.get_bezier_path();
@@ -591,8 +570,7 @@ impl WasmBezier {
.enumerate()
.map(|(idx, sector)| {
draw_sector(
sector.center.x,
sector.center.y,
sector.center,
sector.radius,
-sector.start_angle,
-sector.end_angle,
@@ -1,4 +1,5 @@
use bezier_rs::Bezier;
use glam::DVec2;
use std::fmt::Write;
// SVG drawing constants
@@ -31,8 +32,11 @@ pub fn draw_text(text: String, x_pos: f64, y_pos: f64, fill: &str) -> String {
}
/// Helper function to create an SVG circle entity.
pub fn draw_circle(x_pos: f64, y_pos: f64, radius: f64, stroke: &str, stroke_width: f64, fill: &str) -> String {
format!(r#"<circle cx="{x_pos}" cy="{y_pos}" r="{radius}" stroke="{stroke}" stroke-width="{stroke_width}" fill="{fill}"/>"#)
pub fn draw_circle(position: DVec2, radius: f64, stroke: &str, stroke_width: f64, fill: &str) -> String {
format!(
r#"<circle cx="{}" cy="{}" r="{radius}" stroke="{stroke}" stroke-width="{stroke_width}" fill="{fill}"/>"#,
position.x, position.y
)
}
/// Helper function to create an SVG circle entity.
@@ -61,11 +65,14 @@ fn polar_to_cartesian(center_x: f64, center_y: f64, radius: f64, angle_in_rad: f
}
// Helper function to create an SVG drawing of a sector
pub fn draw_sector(center_x: f64, center_y: f64, radius: f64, start_angle: f64, end_angle: f64, stroke: &str, stroke_width: f64, fill: &str) -> String {
let [start_x, start_y] = polar_to_cartesian(center_x, center_y, radius, start_angle);
let [end_x, end_y] = polar_to_cartesian(center_x, center_y, radius, end_angle);
pub fn draw_sector(center: DVec2, radius: f64, start_angle: f64, end_angle: f64, stroke: &str, stroke_width: f64, fill: &str) -> String {
let [start_x, start_y] = polar_to_cartesian(center.x, center.y, radius, start_angle);
let [end_x, end_y] = polar_to_cartesian(center.x, center.y, radius, end_angle);
// draw sector with fill color
let sector_svg = format!(r#"<path d="M {start_x} {start_y} A {radius} {radius} 0 0 1 {end_x} {end_y} L {center_x} {center_y} L {start_x} {start_y} Z" stroke="none" fill="{fill}" />"#);
let sector_svg = format!(
r#"<path d="M {start_x} {start_y} A {radius} {radius} 0 0 1 {end_x} {end_y} L {} {} L {start_x} {start_y} Z" stroke="none" fill="{fill}" />"#,
center.x, center.y
);
// draw arc with stroke color
let arc_svg = format!(r#"<path d="M {start_x} {start_y} A {radius} {radius} 0 0 1 {end_x} {end_y}" stroke="{stroke}" stroke-width="{stroke_width}" fill="none"/>"#);
format!("{sector_svg}{arc_svg}")