Skip to content

Port to linesweeper #2670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: linesweeper
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .nix/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ syn = { version = "2.0", default-features = false, features = [
"derive",
] }
kurbo = { version = "0.11.0", features = ["serde"] }
linesweeper = { git = "https://github.com/jneem/linesweeper", rev="3a9c0fd" }
petgraph = { version = "0.7.1", default-features = false, features = [
"graphmap",
] }
Expand Down
69 changes: 47 additions & 22 deletions editor/src/messages/portfolio/document/document_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use graphene_core::raster::BlendMode;
use graphene_core::raster::image::ImageFrameTable;
use graphene_core::vector::style::ViewMode;
use graphene_std::renderer::{ClickTarget, Quad};
use graphene_std::vector::{PointId, path_bool_lib};
use graphene_std::vector::{PointId, kurbo};
use std::time::Duration;

pub struct DocumentMessageData<'a> {
Expand Down Expand Up @@ -2847,7 +2847,7 @@ fn default_document_network_interface() -> NodeNetworkInterface {
enum XRayTarget {
Point(DVec2),
Quad(Quad),
Path(Vec<path_bool_lib::PathSegment>),
Path(kurbo::BezPath),
Polygon(Subpath<PointId>),
}

Expand All @@ -2865,20 +2865,38 @@ pub struct ClickXRayIter<'a> {
parent_targets: Vec<(LayerNodeIdentifier, XRayTarget)>,
}

fn quad_to_path_lib_segments(quad: Quad) -> Vec<path_bool_lib::PathSegment> {
quad.all_edges().into_iter().map(|[start, end]| path_bool_lib::PathSegment::Line(start, end)).collect()
fn quad_to_path_lib_segments(quad: Quad) -> kurbo::BezPath {
let kp = |dv: DVec2| -> kurbo::Point { (dv.x, dv.y).into() };
let mut ret = kurbo::BezPath::new();
for [start, end] in quad.all_edges().into_iter() {
if ret.is_empty() {
ret.move_to(kp(start));
}
ret.line_to(kp(end));
}
ret
}

fn click_targets_to_path_lib_segments<'a>(click_targets: impl Iterator<Item = &'a ClickTarget>, transform: DAffine2) -> Vec<path_bool_lib::PathSegment> {
let segment = |bezier: bezier_rs::Bezier| match bezier.handles {
bezier_rs::BezierHandles::Linear => path_bool_lib::PathSegment::Line(bezier.start, bezier.end),
bezier_rs::BezierHandles::Quadratic { handle } => path_bool_lib::PathSegment::Quadratic(bezier.start, handle, bezier.end),
bezier_rs::BezierHandles::Cubic { handle_start, handle_end } => path_bool_lib::PathSegment::Cubic(bezier.start, handle_start, handle_end, bezier.end),
fn click_targets_to_path_lib_segments<'a>(click_targets: impl Iterator<Item = &'a ClickTarget>, transform: DAffine2) -> kurbo::BezPath {
let mut ret = kurbo::BezPath::new();
let mut add_segment = |bezier: bezier_rs::Bezier| {
let kp = |dv: DVec2| -> kurbo::Point { (dv.x, dv.y).into() };
if ret.is_empty() {
ret.move_to(kp(bezier.start));
}
match bezier.handles {
bezier_rs::BezierHandles::Linear => ret.line_to(kp(bezier.end)),
bezier_rs::BezierHandles::Quadratic { handle } => ret.quad_to(kp(handle), kp(bezier.end)),
bezier_rs::BezierHandles::Cubic { handle_start, handle_end } => ret.curve_to(kp(handle_start), kp(handle_end), kp(bezier.end)),
}
};
click_targets
for seg in click_targets
.flat_map(|target| target.subpath().iter())
.map(|bezier| segment(bezier.apply_transformation(|x| transform.transform_point2(x))))
.collect()
.map(|bezier| bezier.apply_transformation(|x| transform.transform_point2(x)))
{
add_segment(seg);
}
ret
}

impl<'a> ClickXRayIter<'a> {
Expand All @@ -2899,15 +2917,15 @@ impl<'a> ClickXRayIter<'a> {
}

/// Handles the checking of the layer where the target is a rect or path
fn check_layer_area_target(&mut self, click_targets: Option<&Vec<ClickTarget>>, clip: bool, layer: LayerNodeIdentifier, path: Vec<path_bool_lib::PathSegment>, transform: DAffine2) -> XRayResult {
fn check_layer_area_target(&mut self, click_targets: Option<&Vec<ClickTarget>>, clip: bool, layer: LayerNodeIdentifier, path: kurbo::BezPath, transform: DAffine2) -> XRayResult {
// Convert back to Bezier-rs types for intersections
let segment = |bezier: &path_bool_lib::PathSegment| match *bezier {
path_bool_lib::PathSegment::Line(start, end) => bezier_rs::Bezier::from_linear_dvec2(start, end),
path_bool_lib::PathSegment::Cubic(start, h1, h2, end) => bezier_rs::Bezier::from_cubic_dvec2(start, h1, h2, end),
path_bool_lib::PathSegment::Quadratic(start, h1, end) => bezier_rs::Bezier::from_quadratic_dvec2(start, h1, end),
path_bool_lib::PathSegment::Arc(_, _, _, _, _, _, _) => unimplemented!(),
let dv = |p: kurbo::Point| -> DVec2 { DVec2::new(p.x, p.y) };
let segment = |bezier: kurbo::PathSeg| match bezier {
kurbo::PathSeg::Line(line) => bezier_rs::Bezier::from_linear_dvec2(dv(line.p0), dv(line.p1)),
kurbo::PathSeg::Quad(q) => bezier_rs::Bezier::from_quadratic_dvec2(dv(q.p0), dv(q.p1), dv(q.p2)),
kurbo::PathSeg::Cubic(c) => bezier_rs::Bezier::from_cubic_dvec2(dv(c.p0), dv(c.p1), dv(c.p2), dv(c.p3)),
};
let get_clip = || path.iter().map(segment);
let get_clip = || path.segments().map(segment);

let intersects = click_targets.is_some_and(|targets| targets.iter().any(|target| target.intersect_path(get_clip, transform)));
let clicked = intersects;
Expand All @@ -2917,7 +2935,7 @@ impl<'a> ClickXRayIter<'a> {
// We do this on this using the target area to reduce computation (as the target area is usually very simple).
if clip && intersects {
let clip_path = click_targets_to_path_lib_segments(click_targets.iter().flat_map(|x| x.iter()), transform);
let subtracted = graphene_std::vector::boolean_intersect(path, clip_path).into_iter().flatten().collect::<Vec<_>>();
let subtracted = graphene_std::vector::boolean_intersect(path, clip_path).into_iter().flatten().collect::<kurbo::BezPath>();
if subtracted.is_empty() {
use_children = false;
} else {
Expand Down Expand Up @@ -2953,8 +2971,15 @@ impl<'a> ClickXRayIter<'a> {
XRayTarget::Quad(quad) => self.check_layer_area_target(click_targets, clip, layer, quad_to_path_lib_segments(*quad), transform),
XRayTarget::Path(path) => self.check_layer_area_target(click_targets, clip, layer, path.clone(), transform),
XRayTarget::Polygon(polygon) => {
let polygon = polygon.iter_closed().map(|line| path_bool_lib::PathSegment::Line(line.start, line.end)).collect();
self.check_layer_area_target(click_targets, clip, layer, polygon, transform)
let mut path = kurbo::BezPath::new();
let kp = |dv: DVec2| -> kurbo::Point { (dv.x, dv.y).into() };
for line in polygon.iter_closed() {
if path.is_empty() {
path.move_to(kp(line.start));
}
path.line_to(kp(line.end));
}
self.check_layer_area_target(click_targets, clip, layer, path, transform)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions node-graph/gstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ node-macro = { workspace = true }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
reqwest = { workspace = true }
kurbo = { workspace = true }
linesweeper = { workspace = true, features = ["slow-asserts"] }
futures = { workspace = true }
wgpu-types = { workspace = true }
winit = { workspace = true }
Expand Down
62 changes: 33 additions & 29 deletions node-graph/gstd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use graphene_core::vector::misc::BooleanOperation;
use graphene_core::vector::style::Fill;
pub use graphene_core::vector::*;
use graphene_core::{Color, Ctx, GraphicElement, GraphicGroupTable};
pub use path_bool as path_bool_lib;
use path_bool::{FillRule, PathBooleanOperation};
pub use kurbo;
use linesweeper::{BinaryOp, FillRule};
use std::ops::Mul;

#[node_macro::node(category(""))]
Expand Down Expand Up @@ -211,36 +211,34 @@ async fn boolean_operation(_: impl Ctx, group_of_paths: GraphicGroupTable, opera
result_vector_data_table
}

fn to_path(vector: &VectorData, transform: DAffine2) -> Vec<path_bool::PathSegment> {
let mut path = Vec::new();
fn to_path(vector: &VectorData, transform: DAffine2) -> kurbo::BezPath {
let mut path = kurbo::BezPath::new();
for subpath in vector.stroke_bezier_paths() {
to_path_segments(&mut path, &subpath, transform);
}
path
}

fn to_path_segments(path: &mut Vec<path_bool::PathSegment>, subpath: &bezier_rs::Subpath<PointId>, transform: DAffine2) {
use path_bool::PathSegment;
let mut global_start = None;
let mut global_end = DVec2::ZERO;
fn to_path_segments(path: &mut kurbo::BezPath, subpath: &bezier_rs::Subpath<PointId>, transform: DAffine2) {
let kp = |dv: DVec2| -> kurbo::Point { (dv.x, dv.y).into() };
let mut first = true;
for bezier in subpath.iter() {
const EPS: f64 = 1e-8;
let transformed = bezier.apply_transformation(|pos| transform.transform_point2(pos).mul(EPS.recip()).round().mul(EPS));
let start = transformed.start;
let end = transformed.end;
if global_start.is_none() {
global_start = Some(start);
if first {
first = false;
path.move_to(kp(start));
}
global_end = end;
let segment = match transformed.handles {
bezier_rs::BezierHandles::Linear => PathSegment::Line(start, end),
bezier_rs::BezierHandles::Quadratic { handle } => PathSegment::Quadratic(start, handle, end),
bezier_rs::BezierHandles::Cubic { handle_start, handle_end } => PathSegment::Cubic(start, handle_start, handle_end, end),
match transformed.handles {
bezier_rs::BezierHandles::Linear => path.line_to(kp(end)),
bezier_rs::BezierHandles::Quadratic { handle } => path.quad_to(kp(handle), kp(end)),
bezier_rs::BezierHandles::Cubic { handle_start, handle_end } => path.curve_to(kp(handle_start), kp(handle_end), kp(end)),
};
path.push(segment);
}
if let Some(start) = global_start {
path.push(PathSegment::Line(global_end, start));
if !first {
path.close_path();
}
}

Expand All @@ -254,7 +252,14 @@ fn from_path(path_data: &[Path]) -> VectorData {
let mut all_subpaths = Vec::new();

for path in path_data.iter().filter(|path| !path.is_empty()) {
let cubics: Vec<[DVec2; 4]> = path.iter().map(|segment| segment.to_cubic()).collect();
let cubics: Vec<[DVec2; 4]> = path
.segments()
.map(|segment| {
let dv = |p: kurbo::Point| -> DVec2 { DVec2::new(p.x, p.y) };
let c = segment.to_cubic();
[dv(c.p0), dv(c.p1), dv(c.p2), dv(c.p3)]
})
.collect();
let mut groups = Vec::new();
let mut current_start = None;

Expand Down Expand Up @@ -335,28 +340,27 @@ pub fn convert_usvg_path(path: &usvg::Path) -> Vec<Subpath<PointId>> {
subpaths
}

type Path = Vec<path_bool::PathSegment>;
type Path = kurbo::BezPath;

fn boolean_union(a: Path, b: Path) -> Vec<Path> {
path_bool(a, b, PathBooleanOperation::Union)
path_bool(a, b, BinaryOp::Union)
}

fn path_bool(a: Path, b: Path, op: PathBooleanOperation) -> Vec<Path> {
match path_bool::path_boolean(&a, FillRule::NonZero, &b, FillRule::NonZero, op) {
Ok(results) => results,
fn path_bool(a: Path, b: Path, op: BinaryOp) -> Vec<Path> {
log::warn!("bool op! {:?} {:?} {op:?}", a.to_svg(), b.to_svg());
match linesweeper::binary_op(&a, &b, FillRule::NonZero, op) {
Ok(contours) => contours.contours().map(|c| c.path.clone()).collect(),
Err(e) => {
let a_path = path_bool::path_to_path_data(&a, 0.001);
let b_path = path_bool::path_to_path_data(&b, 0.001);
log::error!("Boolean error {e:?} encountered while processing {a_path}\n {op:?}\n {b_path}");
log::error!("Boolean error {e:?} encountered while processing paths");
Vec::new()
}
}
}

fn boolean_subtract(a: Path, b: Path) -> Vec<Path> {
path_bool(a, b, PathBooleanOperation::Difference)
path_bool(a, b, BinaryOp::Difference)
}

pub fn boolean_intersect(a: Path, b: Path) -> Vec<Path> {
path_bool(a, b, PathBooleanOperation::Intersection)
path_bool(a, b, BinaryOp::Intersection)
}