mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-21 06:48:13 +08:00
Add Poisson-disk sampling node and Bezier-rs 0.4 release (#1586)
* Add Poisson-disk sampling node and Bezier-rs 0.4 release * Additional optimizations * More performance optimizations with help from 0Hypercube * Add comments
This commit is contained in:
@@ -409,7 +409,7 @@ const bezierFeatures = {
|
||||
})(),
|
||||
},
|
||||
"intersect-linear": {
|
||||
name: "Intersect (Line Segment)",
|
||||
name: "Intersect (Linear Segment)",
|
||||
callback: (bezier: WasmBezierInstance): string => {
|
||||
const line = [
|
||||
[45, 30],
|
||||
@@ -419,7 +419,7 @@ const bezierFeatures = {
|
||||
},
|
||||
},
|
||||
"intersect-quadratic": {
|
||||
name: "Intersect (Quadratic)",
|
||||
name: "Intersect (Quadratic Segment)",
|
||||
callback: (bezier: WasmBezierInstance, options: Record<string, number>): string => {
|
||||
const quadratic = [
|
||||
[45, 80],
|
||||
@@ -435,7 +435,7 @@ const bezierFeatures = {
|
||||
},
|
||||
},
|
||||
"intersect-cubic": {
|
||||
name: "Intersect (Cubic)",
|
||||
name: "Intersect (Cubic Segment)",
|
||||
callback: (bezier: WasmBezierInstance, options: Record<string, number>): string => {
|
||||
const cubic = [
|
||||
[65, 20],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { capOptions, joinOptions, tSliderOptions, subpathTValueVariantOptions, intersectionErrorOptions, minimumSeparationOptions } from "@/utils/options";
|
||||
import { capOptions, joinOptions, tSliderOptions, subpathTValueVariantOptions, intersectionErrorOptions, minimumSeparationOptions, separationDiskDiameter } from "@/utils/options";
|
||||
import type { SubpathCallback, SubpathInputOption, WasmSubpathInstance } from "@/utils/types";
|
||||
import { SUBPATH_T_VALUE_VARIANTS } from "@/utils/types";
|
||||
|
||||
@@ -59,12 +59,17 @@ const subpathFeatures = {
|
||||
name: "Bounding Box",
|
||||
callback: (subpath: WasmSubpathInstance): string => subpath.bounding_box(),
|
||||
},
|
||||
"poisson-disk-points": {
|
||||
name: "Poisson-Disk Points",
|
||||
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined): string => subpath.poisson_disk_points(options.separation_disk_diameter),
|
||||
inputOptions: [separationDiskDiameter],
|
||||
},
|
||||
inflections: {
|
||||
name: "Inflections",
|
||||
callback: (subpath: WasmSubpathInstance): string => subpath.inflections(),
|
||||
},
|
||||
"intersect-linear": {
|
||||
name: "Intersect (Line Segment)",
|
||||
name: "Intersect (Linear Segment)",
|
||||
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string =>
|
||||
subpath.intersect_line_segment(
|
||||
[
|
||||
@@ -105,11 +110,24 @@ const subpathFeatures = {
|
||||
),
|
||||
inputOptions: [intersectionErrorOptions, minimumSeparationOptions],
|
||||
},
|
||||
"self-intersect": {
|
||||
name: "Self Intersect",
|
||||
"intersect-self": {
|
||||
name: "Intersect (Self)",
|
||||
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string => subpath.self_intersections(options.error, options.minimum_separation),
|
||||
inputOptions: [intersectionErrorOptions, minimumSeparationOptions],
|
||||
},
|
||||
"intersect-rectangle": {
|
||||
name: "Intersect (Rectangle)",
|
||||
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string =>
|
||||
subpath.intersect_rectangle(
|
||||
[
|
||||
[75, 50],
|
||||
[175, 150],
|
||||
],
|
||||
options.error,
|
||||
options.minimum_separation,
|
||||
),
|
||||
inputOptions: [intersectionErrorOptions, minimumSeparationOptions],
|
||||
},
|
||||
curvature: {
|
||||
name: "Curvature",
|
||||
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined): string => subpath.curvature(options.t, SUBPATH_T_VALUE_VARIANTS[options.TVariant]),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { BEZIER_T_VALUE_VARIANTS, CAP_VARIANTS, JOIN_VARIANTS, SUBPATH_T_VALUE_VARIANTS } from "@/utils/types";
|
||||
|
||||
export const tSliderOptions = {
|
||||
variable: "t",
|
||||
min: 0,
|
||||
max: 1,
|
||||
step: 0.01,
|
||||
default: 0.5,
|
||||
variable: "t",
|
||||
};
|
||||
|
||||
export const errorOptions = {
|
||||
@@ -32,6 +32,14 @@ export const intersectionErrorOptions = {
|
||||
default: 0.02,
|
||||
};
|
||||
|
||||
export const separationDiskDiameter = {
|
||||
variable: "separation_disk_diameter",
|
||||
min: 2.5,
|
||||
max: 25,
|
||||
step: 0.1,
|
||||
default: 5,
|
||||
};
|
||||
|
||||
export const bezierTValueVariantOptions = {
|
||||
variable: "TVariant",
|
||||
default: 0,
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::utils::{parse_cap, parse_join};
|
||||
use bezier_rs::{Bezier, ManipulatorGroup, Subpath, SubpathTValue, TValueType};
|
||||
|
||||
use glam::DVec2;
|
||||
use js_sys::Math;
|
||||
use std::fmt::Write;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
@@ -171,7 +172,7 @@ impl WasmSubpath {
|
||||
None => wrap_svg_tag(subpath_svg),
|
||||
Some(bounding_box) => {
|
||||
let content = format!(
|
||||
"{subpath_svg}<rect x={} y ={} width=\"{}\" height=\"{}\" style=\"fill:{NONE};stroke:{RED};stroke-width:1\" />",
|
||||
"{subpath_svg}<rect x={} y={} width=\"{}\" height=\"{}\" style=\"fill:{NONE};stroke:{RED};stroke-width:1\" />",
|
||||
bounding_box[0].x,
|
||||
bounding_box[0].y,
|
||||
bounding_box[1].x - bounding_box[0].x,
|
||||
@@ -182,6 +183,21 @@ impl WasmSubpath {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poisson_disk_points(&self, separation_disk_diameter: f64) -> String {
|
||||
let r = separation_disk_diameter / 2.;
|
||||
|
||||
let subpath_svg = self.to_default_svg();
|
||||
let points = self.0.poisson_disk_points(separation_disk_diameter, Math::random);
|
||||
|
||||
let points_style = format!("<style class=\"poisson\">style.poisson ~ circle {{ fill: {RED}; opacity: 0.25; }}</style>");
|
||||
let content = points
|
||||
.iter()
|
||||
.map(|point| format!("<circle cx=\"{}\" cy=\"{}\" r=\"{r}\" />", point.x, point.y))
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
wrap_svg_tag(format!("{subpath_svg}{points_style}{content}"))
|
||||
}
|
||||
|
||||
pub fn inflections(&self) -> String {
|
||||
let inflections: Vec<f64> = self.0.inflections();
|
||||
|
||||
@@ -342,6 +358,37 @@ impl WasmSubpath {
|
||||
wrap_svg_tag(format!("{subpath_svg}{self_intersections_svg}"))
|
||||
}
|
||||
|
||||
pub fn intersect_rectangle(&self, js_points: JsValue, error: f64, minimum_separation: f64) -> String {
|
||||
let points: [DVec2; 2] = serde_wasm_bindgen::from_value(js_points).unwrap();
|
||||
|
||||
let subpath_svg = self.to_default_svg();
|
||||
|
||||
let mut rectangle_svg = String::new();
|
||||
[
|
||||
Bezier::from_linear_coordinates(points[0].x, points[0].y, points[1].x, points[0].y),
|
||||
Bezier::from_linear_coordinates(points[1].x, points[0].y, points[1].x, points[1].y),
|
||||
Bezier::from_linear_coordinates(points[1].x, points[1].y, points[0].x, points[1].y),
|
||||
Bezier::from_linear_coordinates(points[0].x, points[1].y, points[0].x, points[0].y),
|
||||
]
|
||||
.iter()
|
||||
.for_each(|line| line.to_svg(&mut rectangle_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new()));
|
||||
|
||||
let intersections_svg = self
|
||||
.0
|
||||
.rectangle_intersections(points[0], points[1], Some(error), Some(minimum_separation))
|
||||
.iter()
|
||||
.map(|(segment_index, intersection_t)| {
|
||||
let point = self.0.evaluate(SubpathTValue::Parametric {
|
||||
segment_index: *segment_index,
|
||||
t: *intersection_t,
|
||||
});
|
||||
draw_circle(point, 4., RED, 1.5, WHITE)
|
||||
})
|
||||
.fold(String::new(), |acc, item| format!("{acc}{item}"));
|
||||
|
||||
wrap_svg_tag(format!("{subpath_svg}{rectangle_svg}{intersections_svg}"))
|
||||
}
|
||||
|
||||
pub fn curvature(&self, t: f64, t_variant: String) -> String {
|
||||
let subpath = self.to_default_svg();
|
||||
let t = parse_t_variant(&t_variant, t);
|
||||
|
||||
Reference in New Issue
Block a user