Skip to content

Commit 54b4ef1

Browse files
indierustyKeavon
andauthored
Refactor the 'Scatter Points' node to use Kurbo instead of Bezier-rs (#2634)
* it works! * clean up * Code review --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
1 parent 5861e63 commit 54b4ef1

File tree

3 files changed

+67
-40
lines changed

3 files changed

+67
-40
lines changed

demo-artwork/red-dress.graphite

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-graph/gcore/src/vector/algorithms/bezpath_algorithms.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::poisson_disk::poisson_disk_sample;
2-
use crate::vector::PointId;
3-
use bezier_rs::Subpath;
4-
use glam::{DAffine2, DVec2};
5-
use kurbo::{BezPath, ParamCurve, ParamCurveDeriv, PathSeg, Point, Shape};
2+
use crate::vector::misc::dvec2_to_point;
3+
use glam::DVec2;
4+
use kurbo::{Affine, BezPath, Line, ParamCurve, ParamCurveDeriv, PathSeg, Point, Rect, Shape};
65

76
/// Accuracy to find the position on [kurbo::Bezpath].
87
const POSITION_ACCURACY: f64 = 1e-5;
@@ -199,26 +198,29 @@ fn bezpath_t_value_to_parametric(bezpath: &kurbo::BezPath, t: BezPathTValue, pre
199198
///
200199
/// While the conceptual process described above asymptotically slows down and is never guaranteed to produce a maximal set in finite time,
201200
/// this is implemented with an algorithm that produces a maximal set in O(n) time. The slowest part is actually checking if points are inside the subpath shape.
202-
pub fn poisson_disk_points(this: &Subpath<PointId>, separation_disk_diameter: f64, rng: impl FnMut() -> f64, subpaths: &[(Subpath<PointId>, [DVec2; 2])], subpath_index: usize) -> Vec<DVec2> {
203-
let Some(bounding_box) = this.bounding_box() else { return Vec::new() };
204-
let (offset_x, offset_y) = bounding_box[0].into();
205-
let (width, height) = (bounding_box[1] - bounding_box[0]).into();
201+
pub fn poisson_disk_points(bezpath: &BezPath, separation_disk_diameter: f64, rng: impl FnMut() -> f64, subpaths: &[(BezPath, Rect)], subpath_index: usize) -> Vec<DVec2> {
202+
if bezpath.elements().is_empty() {
203+
return Vec::new();
204+
}
205+
let bbox = bezpath.bounding_box();
206+
let (offset_x, offset_y) = (bbox.x0, bbox.y0);
207+
let (width, height) = (bbox.x1 - bbox.x0, bbox.y1 - bbox.y0);
206208

207209
// TODO: Optimize the following code and make it more robust
208210

209-
let mut shape = this.clone();
210-
shape.set_closed(true);
211-
shape.apply_transform(DAffine2::from_translation((-offset_x, -offset_y).into()));
211+
let mut shape = bezpath.clone();
212+
shape.close_path();
213+
shape.apply_affine(Affine::translate((-offset_x, -offset_y)));
212214

213215
let point_in_shape_checker = |point: DVec2| {
214216
// Check against all paths the point is contained in to compute the correct winding number
215217
let mut number = 0;
216-
for (i, (shape, bb)) in subpaths.iter().enumerate() {
217-
let point = point + bounding_box[0];
218-
if bb[0].x > point.x || bb[0].y > point.y || bb[1].x < point.x || bb[1].y < point.y {
218+
for (i, (shape, bbox)) in subpaths.iter().enumerate() {
219+
let point = point + DVec2::new(bbox.x0, bbox.y0);
220+
if bbox.x0 > point.x || bbox.y0 > point.y || bbox.x1 < point.x || bbox.y1 < point.y {
219221
continue;
220222
}
221-
let winding = shape.winding_order(point);
223+
let winding = shape.winding(dvec2_to_point(point));
222224

223225
if i == subpath_index && winding == 0 {
224226
return false;
@@ -228,9 +230,9 @@ pub fn poisson_disk_points(this: &Subpath<PointId>, separation_disk_diameter: f6
228230
number != 0
229231
};
230232

231-
let square_edges_intersect_shape_checker = |corner1: DVec2, size: f64| {
232-
let corner2 = corner1 + DVec2::splat(size);
233-
this.rectangle_intersections_exist(corner1, corner2)
233+
let square_edges_intersect_shape_checker = |position: DVec2, size: f64| {
234+
let rect = Rect::new(position.x, position.y, position.x + size, position.y + size);
235+
bezpath_rectangle_intersections_exist(bezpath, rect)
234236
};
235237

236238
let mut points = poisson_disk_sample(width, height, separation_disk_diameter, point_in_shape_checker, square_edges_intersect_shape_checker, rng);
@@ -240,3 +242,33 @@ pub fn poisson_disk_points(this: &Subpath<PointId>, separation_disk_diameter: f6
240242
}
241243
points
242244
}
245+
246+
fn bezpath_rectangle_intersections_exist(bezpath: &BezPath, rect: Rect) -> bool {
247+
if !bezpath.bounding_box().overlaps(rect) {
248+
return false;
249+
}
250+
251+
// Top left
252+
let p1 = Point::new(rect.x0, rect.y0);
253+
// Top right
254+
let p2 = Point::new(rect.x1, rect.y0);
255+
// Bottom right
256+
let p3 = Point::new(rect.x1, rect.y1);
257+
// Bottom left
258+
let p4 = Point::new(rect.x0, rect.y1);
259+
260+
let top_line = Line::new((p1.x, p1.y), (p2.x, p2.y));
261+
let right_line = Line::new((p2.x, p2.y), (p3.x, p3.y));
262+
let bottom_line = Line::new((p3.x, p3.y), (p4.x, p4.y));
263+
let left_line = Line::new((p4.x, p4.y), (p1.x, p1.y));
264+
265+
for segment in bezpath.segments() {
266+
for line in [top_line, right_line, bottom_line, left_line] {
267+
if !segment.intersect_line(line).is_empty() {
268+
return true;
269+
}
270+
}
271+
}
272+
273+
false
274+
}

node-graph/gcore/src/vector/vector_nodes.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::algorithms::bezpath_algorithms::{PERIMETER_ACCURACY, position_on_bezpath, sample_points_on_bezpath, tangent_on_bezpath};
1+
use super::algorithms::bezpath_algorithms::{self, PERIMETER_ACCURACY, position_on_bezpath, sample_points_on_bezpath, tangent_on_bezpath};
22
use super::algorithms::offset_subpath::offset_subpath;
33
use super::misc::{CentroidType, point_to_dvec2};
44
use super::style::{Fill, Gradient, GradientStops, Stroke};
@@ -9,13 +9,14 @@ use crate::registry::types::{Angle, Fraction, IntegerCount, Length, Multiplier,
99
use crate::renderer::GraphicElementRendered;
1010
use crate::transform::{Footprint, ReferencePoint, Transform, TransformMut};
1111
use crate::vector::PointDomain;
12+
use crate::vector::misc::dvec2_to_point;
1213
use crate::vector::style::{LineCap, LineJoin};
1314
use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, GraphicElement, GraphicGroupTable, OwnedContextImpl};
1415
use bezier_rs::{Join, ManipulatorGroup, Subpath, SubpathTValue};
1516
use core::f64::consts::PI;
1617
use core::hash::{Hash, Hasher};
1718
use glam::{DAffine2, DVec2};
18-
use kurbo::{Affine, Shape};
19+
use kurbo::{Affine, BezPath, Shape};
1920
use rand::{Rng, SeedableRng};
2021
use std::collections::hash_map::DefaultHasher;
2122

@@ -1364,36 +1365,30 @@ async fn poisson_disk_points(
13641365
return VectorDataTable::new(result);
13651366
}
13661367
let path_with_bounding_boxes: Vec<_> = vector_data
1367-
.stroke_bezier_paths()
1368-
.filter_map(|mut subpath| {
1368+
.stroke_bezpath_iter()
1369+
.map(|mut subpath| {
13691370
// TODO: apply transform to points instead of modifying the paths
1370-
subpath.apply_transform(vector_data_transform);
1371-
subpath.loose_bounding_box().map(|bb| (subpath, bb))
1371+
subpath.apply_affine(Affine::new(vector_data_transform.to_cols_array()));
1372+
let bbox = subpath.bounding_box();
1373+
(subpath, bbox)
13721374
})
13731375
.collect();
13741376

13751377
for (i, (subpath, _)) in path_with_bounding_boxes.iter().enumerate() {
1376-
if subpath.manipulator_groups().len() < 3 {
1378+
if subpath.segments().count() < 2 {
13771379
continue;
13781380
}
13791381

1380-
let mut previous_point_index: Option<usize> = None;
1381-
1382-
for point in subpath.poisson_disk_points(separation_disk_diameter, || rng.random::<f64>(), &path_with_bounding_boxes, i) {
1383-
let point_id = PointId::generate();
1384-
result.point_domain.push(point_id, point);
1382+
let mut poisson_disk_bezpath = BezPath::new();
13851383

1386-
// Get the index of the newly added point.
1387-
let point_index = result.point_domain.ids().len() - 1;
1388-
1389-
// If there is a previous point, connect it with the current point by adding a segment.
1390-
if let Some(prev_point_index) = previous_point_index {
1391-
let segment_id = SegmentId::generate();
1392-
result.segment_domain.push(segment_id, prev_point_index, point_index, bezier_rs::BezierHandles::Linear, StrokeId::ZERO);
1384+
for point in bezpath_algorithms::poisson_disk_points(subpath, separation_disk_diameter, || rng.random::<f64>(), &path_with_bounding_boxes, i) {
1385+
if poisson_disk_bezpath.elements().is_empty() {
1386+
poisson_disk_bezpath.move_to(dvec2_to_point(point));
1387+
} else {
1388+
poisson_disk_bezpath.line_to(dvec2_to_point(point));
13931389
}
1394-
1395-
previous_point_index = Some(point_index);
13961390
}
1391+
result.append_bezpath(poisson_disk_bezpath);
13971392
}
13981393

13991394
// Transfer the style from the input vector data to the result.

0 commit comments

Comments
 (0)