Skip to content

Commit

Permalink
Path Bool library code cleanup (#2000)
Browse files Browse the repository at this point in the history
* Remove log statements

* Add feature gates to functions in path.rs

* Fix infinite parsing loop and add new test

* License tweaks

* Remove trailing zero in whole number floats

* Flatten visual-tests directory

* Code review

* Clean up printlines

* Add error handling to path parsing

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
  • Loading branch information
TrueDoctor and Keavon authored Sep 23, 2024
1 parent 3ddc052 commit 8a10899
Show file tree
Hide file tree
Showing 175 changed files with 442 additions and 346 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ graphene-core = { path = "node-graph/gcore" }
graph-craft = { path = "node-graph/graph-craft", features = ["serde"] }
wgpu-executor = { path = "node-graph/wgpu-executor" }
bezier-rs = { path = "libraries/bezier-rs", features = ["dyn-any"] }
path-bool = { path = "libraries/path-bool", features = ["parsing"] }
path-bool = { path = "libraries/path-bool", default-features = false }
node-macro = { path = "node-graph/node-macro" }

# Workspace dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1926,10 +1926,10 @@ impl NodeGraphMessageHandler {
// }

let curve_length = 24.;
let curve_falloff_rate = curve_length * std::f64::consts::PI * 2.0;
let curve_falloff_rate = curve_length * std::f64::consts::PI * 2.;

let horizontal_curve_amount = -(2.0f64.powf((-10. * horizontal_gap) / curve_falloff_rate)) + 1.;
let vertical_curve_amount = -(2.0f64.powf((-10. * vertical_gap) / curve_falloff_rate)) + 1.;
let horizontal_curve_amount = -(2_f64.powf((-10. * horizontal_gap) / curve_falloff_rate)) + 1.;
let vertical_curve_amount = -(2_f64.powf((-10. * vertical_gap) / curve_falloff_rate)) + 1.;
let horizontal_curve = horizontal_curve_amount * curve_length;
let vertical_curve = vertical_curve_amount * curve_length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn grid_overlay_isometric_dot(document: &DocumentMessageHandler, overlay_context
let max_x = bounds.0.iter().map(|&corner| corner.x).max_by(cmp).unwrap_or_default();
let spacing_x = isometric_spacing.x;
let tan = tan_a;
let multiply = -1.0;
let multiply = -1.;
let project = |corner: &DVec2| corner.y + multiply * tan * (corner.x - origin.x);
let inverse_project = |corner: &DVec2| corner.y - tan * multiply * (corner.x - origin.x);
let min_y = bounds.0.into_iter().min_by(|a, b| inverse_project(a).partial_cmp(&inverse_project(b)).unwrap()).unwrap_or_default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,7 @@ impl NodeNetworkInterface {
let chain_width_grid_spaces = self.chain_width(node_id, network_path);

let node_bottom_right = node_top_left + DVec2::new(width as f64, height as f64);
let chain_top_left = node_top_left - DVec2::new((chain_width_grid_spaces * crate::consts::GRID_SIZE) as f64, 0.0);
let chain_top_left = node_top_left - DVec2::new((chain_width_grid_spaces * crate::consts::GRID_SIZE) as f64, 0.);
let radius = 10.;
let subpath = bezier_rs::Subpath::new_rounded_rect(chain_top_left, node_bottom_right, [radius; 4]);
let node_click_target = ClickTarget::new(subpath, 0.);
Expand Down Expand Up @@ -5083,7 +5083,7 @@ impl Ports {

fn insert_layer_output(&mut self, node_top_left: DVec2) {
// The center of the click target is always 24 px down from the top left corner of the node
let center = node_top_left + DVec2::new(2. * 24., -8.0);
let center = node_top_left + DVec2::new(2. * 24., -8.);
self.insert_output_port_at_center(0, center);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/bezier-rs/src/bezier/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ mod tests {
let bezier2 = Bezier::from_quadratic_coordinates(0., 0., 0., 100., 100., 100.);
assert_eq!(bezier2.project(DVec2::new(100., 0.)), 0.);

let bezier3 = Bezier::from_cubic_coordinates(-50.0, -50.0, -50.0, -50.0, 50.0, -50.0, 50.0, -50.0);
let bezier3 = Bezier::from_cubic_coordinates(-50., -50., -50., -50., 50., -50., 50., -50.);
assert_eq!(DVec2::new(0., -50.), bezier3.evaluate(TValue::Parametric(bezier3.project(DVec2::new(0., -50.)))));
}
}
4 changes: 2 additions & 2 deletions libraries/bezier-rs/src/subpath/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ mod tests {

let subpath: Subpath<EmptyId> = Subpath::from_bezier(&Bezier::from_quadratic_dvec2(start, handle, end));

assert_eq!(subpath.evaluate(SubpathTValue::GlobalEuclidean(0.0)), start);
assert_eq!(subpath.evaluate(SubpathTValue::GlobalEuclidean(1.0)), end);
assert_eq!(subpath.evaluate(SubpathTValue::GlobalEuclidean(0.)), start);
assert_eq!(subpath.evaluate(SubpathTValue::GlobalEuclidean(1.)), end);
}
}
2 changes: 1 addition & 1 deletion libraries/bezier-rs/src/subpath/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ mod tests {
)
.all());

let t4 = 1.0;
let t4 = 1.;
assert!(utils::dvec2_compare(
subpath.evaluate(SubpathTValue::GlobalParametric(t4)),
quadratic_bezier.evaluate(TValue::Parametric(1.)),
Expand Down
4 changes: 2 additions & 2 deletions libraries/bezier-rs/src/symmetrical_basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl Bezier1d {
fn value_at(&self, t: f64) -> f64 {
let bz = self;
let order = bz.len() - 1;
let u = 1.0 - t;
let u = 1. - t;
let mut bc = 1.;
let mut tn = 1.;
let mut tmp = bz[0] * u;
Expand Down Expand Up @@ -611,7 +611,7 @@ mod tests {

#[test]
fn find_bernstein_roots() {
let bz = Bezier1d(vec![50.0, -100.0, 170.0]);
let bz = Bezier1d(vec![50., -100., 170.]);
let mut solutions = Vec::new();
bz.find_bernstein_roots(&mut solutions, 0, 0., 1.);

Expand Down
4 changes: 2 additions & 2 deletions libraries/path-bool/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
test-results
/target/
test-results/
18 changes: 13 additions & 5 deletions libraries/path-bool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
name = "path-bool"
version = "0.1.0"
rust-version = "1.81"
authors = ["Graphite Authors <contact@graphite.rs>"]
authors = ["Graphite Authors <contact@graphite.rs>", "Adam Platkevič"]
edition = "2021"
keywords = ["bezier", "boolean", "path", "ops", "operations", "2d"]
keywords = [
"bezier",
"curve",
"boolean",
"path",
"geometry",
"computational geometry",
"vector graphics",
"2d",
"graphics",
]
categories = ["graphics", "mathematics"]
license = "MIT OR Apache-2.0"


[features]
logging = ["parsing"]
parsing = []
Expand All @@ -26,13 +35,12 @@ resvg = "0.42"
image = "0.24"

# Required dependencies
criterion = { version = "0.5", features = ["html_reports"]}
criterion = { version = "0.5", features = ["html_reports"] }

# Benchmarks
[[bench]]
name = "painted_dreams"
harness = false

[[bench]]
name = "path_segment_intersection"
harness = false
15 changes: 12 additions & 3 deletions libraries/path-bool/NOTICE
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
NOTICE

This project includes software originally developed by Adam Platkevič.
The original project is licensed under the MIT License.

Rust port and modifications are (c) 2024 Graphite Authors.

This library is derived from software originally developed by Adam Platkevič which is licensed under the MIT License reproduced below:

MIT License

Copyright (c) 2024 Adam Platkevič

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 4 additions & 3 deletions libraries/path-bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Here's a basic example of performing an intersection operation on two paths:
use path_bool::{path_boolean, FillRule, PathBooleanOperation, path_from_path_data, path_to_path_data};

fn main() {
let path_a = path_from_path_data("M 10 10 L 50 10 L 30 40 Z");
let path_b = path_from_path_data("M 20 30 L 60 30 L 60 50 L 20 50 Z");
let path_a = path_from_path_data("M 10 10 L 50 10 L 30 40 Z").unwrap();
let path_b = path_from_path_data("M 20 30 L 60 30 L 60 50 L 20 50 Z").unwrap();

let result = path_boolean(
&path_a,
Expand All @@ -50,13 +50,14 @@ The boolean operations are implemented using a graph-based approach. After the p

## Development status

This project is a port of PathBool.js and is still in early stages of development. Contributions, bug reports, and feedback are welcome.
This project is a port of PathBool.js which is still in early stages of development. Contributions, bug reports, and feedback are welcome.

Future work includes:

- Comprehensive test suite
- Performance optimizations
- Additional examples and documentation
- Support for path builder tool features

## License and acknowledgements

Expand Down

This file was deleted.

This file was deleted.

7 changes: 4 additions & 3 deletions libraries/path-bool/benches/painted_dreams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use path_bool::*;

pub fn criterion_benchmark(c: &mut Criterion) {
let path_a = path_from_path_data("M0,340C161.737914,383.575765 107.564182,490.730587 273,476 C419,463 481.741198,514.692273 481.333333,768 C481.333333,768 -0,768 -0,768 C-0,768 0,340 0,340 Z ");
let path_a =
path_from_path_data("M0,340C161.737914,383.575765 107.564182,490.730587 273,476 C419,463 481.741198,514.692273 481.333333,768 C481.333333,768 -0,768 -0,768 C-0,768 0,340 0,340 Z").unwrap();
let path_b = path_from_path_data(
"M458.370270,572.165771C428.525848,486.720093 368.618805,467.485992 273,476 C107.564178,490.730591 161.737915,383.575775 0,340 C0,340 0,689 0,689 C56,700 106.513901,779.342590 188,694.666687 C306.607422,571.416260 372.033966,552.205139 458.370270,572.165771 Z",
);
"M458.370270,572.165771C428.525848,486.720093 368.618805,467.485992 273,476 C107.564178,490.730591 161.737915,383.575775 0,340 C0,340 0,689 0,689 C56,700 106.513901,779.342590 188,694.666687 C306.607422,571.416260 372.033966,552.205139 458.370270,572.165771 Z",
).unwrap();
c.bench_function("painted_dreams_diff", |b| {
b.iter(|| path_boolean(black_box(&path_a), FillRule::NonZero, black_box(&path_b), FillRule::NonZero, PathBooleanOperation::Difference))
});
Expand Down
8 changes: 4 additions & 4 deletions libraries/path-bool/benches/path_segment_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ fn a() -> PathSegment {
DVec2::new(458.37027, 572.165771),
DVec2::new(428.525848, 486.720093),
DVec2::new(368.618805, 467.485992),
DVec2::new(273.0, 476.0),
DVec2::new(273., 476.),
)
}
fn b() -> PathSegment {
PathSegment::Cubic(DVec2::new(273.0, 476.0), DVec2::new(419.0, 463.0), DVec2::new(481.741198, 514.692273), DVec2::new(481.333333, 768.0))
PathSegment::Cubic(DVec2::new(273., 476.), DVec2::new(419., 463.), DVec2::new(481.741198, 514.692273), DVec2::new(481.333333, 768.))
}
fn c() -> PathSegment {
PathSegment::Cubic(DVec2::new(273.0, 476.0), DVec2::new(107.564178, 490.730591), DVec2::new(161.737915, 383.575775), DVec2::new(0.0, 340.0))
PathSegment::Cubic(DVec2::new(273., 476.), DVec2::new(107.564178, 490.730591), DVec2::new(161.737915, 383.575775), DVec2::new(0., 340.))
}
fn d() -> PathSegment {
PathSegment::Cubic(DVec2::new(0.0, 340.0), DVec2::new(161.737914, 383.575765), DVec2::new(107.564182, 490.730587), DVec2::new(273.0, 476.0))
PathSegment::Cubic(DVec2::new(0., 340.), DVec2::new(161.737914, 383.575765), DVec2::new(107.564182, 490.730587), DVec2::new(273., 476.))
}
6 changes: 3 additions & 3 deletions libraries/path-bool/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
};
buildInputs = with pkgs; [
llvm
];
];
in {
devShells.default = pkgs.mkShell {
stdenv = pkgs.clangStdenv;
Expand All @@ -38,8 +38,8 @@
toolchain
llvm
cargo
];
inherit buildInputs;
];
inherit buildInputs;

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
};
Expand Down
Loading

0 comments on commit 8a10899

Please sign in to comment.