Skip to content

Commit bea7d36

Browse files
indierustyKeavon
authored andcommitted
clean up
1 parent 98ba01e commit bea7d36

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ fn bezpath_t_value_to_parametric(bezpath: &kurbo::BezPath, t: BezPathTValue, pre
199199
/// While the conceptual process described above asymptotically slows down and is never guaranteed to produce a maximal set in finite time,
200200
/// 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.
201201
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+
}
202205
let bbox = bezpath.bounding_box();
203206
let (offset_x, offset_y) = (bbox.x0, bbox.y0);
204207
let (width, height) = (bbox.x1 - bbox.x0, bbox.y1 - bbox.y0);
@@ -212,9 +215,9 @@ pub fn poisson_disk_points(bezpath: &BezPath, separation_disk_diameter: f64, rng
212215
let point_in_shape_checker = |point: DVec2| {
213216
// Check against all paths the point is contained in to compute the correct winding number
214217
let mut number = 0;
215-
for (i, (shape, bb)) in subpaths.iter().enumerate() {
218+
for (i, (shape, bbox)) in subpaths.iter().enumerate() {
216219
let point = point + DVec2::new(bbox.x0, bbox.y0);
217-
if bb.x0 > point.x || bb.y0 > point.y || bb.x1 < point.x || bb.y1 < point.y {
220+
if bbox.x0 > point.x || bbox.y0 > point.y || bbox.x1 < point.x || bbox.y1 < point.y {
218221
continue;
219222
}
220223
let winding = shape.winding(dvec2_to_point(point));
@@ -227,9 +230,9 @@ pub fn poisson_disk_points(bezpath: &BezPath, separation_disk_diameter: f64, rng
227230
number != 0
228231
};
229232

230-
let square_edges_intersect_shape_checker = |corner1: DVec2, size: f64| {
231-
let corner2 = corner1 + DVec2::splat(size);
232-
bezpath_rectangle_intersections_exist(bezpath, 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)
233236
};
234237

235238
let mut points = poisson_disk_sample(width, height, separation_disk_diameter, point_in_shape_checker, square_edges_intersect_shape_checker, rng);
@@ -240,22 +243,33 @@ pub fn poisson_disk_points(bezpath: &BezPath, separation_disk_diameter: f64, rng
240243
points
241244
}
242245

243-
fn bezpath_rectangle_intersections_exist(bezpath: &BezPath, corner1: DVec2, corner2: DVec2) -> bool {
244-
info!("rect intersection => bezpath => {:?}, corner ({:?})", bezpath, (corner1, corner2));
245-
let a = corner1;
246-
let b = DVec2::new(corner2.x, corner1.y);
247-
let c = corner2;
248-
let d = DVec2::new(corner1.x, corner2.y);
246+
fn bezpath_rectangle_intersections_exist(bezpath: &BezPath, rect: Rect) -> bool {
247+
if !bezpath.bounding_box().overlaps(rect) {
248+
return false;
249+
}
249250

250-
let top_line = Line::new((a.x, a.y), (b.x, b.y));
251-
let right_line = Line::new((b.x, b.y), (c.x, c.y));
252-
let bottom_line = Line::new((c.x, c.y), (d.x, d.y));
253-
let left_line = Line::new((d.x, d.y), (a.x, a.y));
251+
// p1 p2
252+
// -------------
253+
// | |
254+
// | |
255+
// | |
256+
// -------------
257+
// p4 p3
258+
let p1 = Point::new(rect.x0, rect.y0);
259+
let p2 = Point::new(rect.x1, rect.y0);
260+
let p3 = Point::new(rect.x1, rect.y1);
261+
let p4 = Point::new(rect.x0, rect.y1);
262+
263+
let top_line = Line::new((p1.x, p1.y), (p2.x, p2.y));
264+
let right_line = Line::new((p2.x, p2.y), (p3.x, p3.y));
265+
let bottom_line = Line::new((p3.x, p3.y), (p4.x, p4.y));
266+
let left_line = Line::new((p4.x, p4.y), (p1.x, p1.y));
267+
268+
let lines = [top_line, right_line, bottom_line, left_line];
254269

255270
for segment in bezpath.segments() {
256-
for line in [top_line, right_line, bottom_line, left_line] {
271+
for line in lines {
257272
if !segment.intersect_line(line).is_empty() {
258-
info!("insected.");
259273
return true;
260274
}
261275
}

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

Lines changed: 12 additions & 20 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

@@ -1368,36 +1369,27 @@ async fn poisson_disk_points(
13681369
.map(|mut subpath| {
13691370
// TODO: apply transform to points instead of modifying the paths
13701371
subpath.apply_affine(Affine::new(vector_data_transform.to_cols_array()));
1371-
let bb = subpath.bounding_box();
1372-
(subpath, bb)
1372+
let bbox = subpath.bounding_box();
1373+
(subpath, bbox)
13731374
})
13741375
.collect();
13751376

1376-
use crate::vector::algorithms::bezpath_algorithms::poisson_disk_points;
1377-
13781377
for (i, (subpath, _)) in path_with_bounding_boxes.iter().enumerate() {
13791378
let segment_count = subpath.segments().count();
13801379
if segment_count < 2 {
13811380
continue;
13821381
}
13831382

1384-
let mut previous_point_index: Option<usize> = None;
1385-
1386-
for point in poisson_disk_points(subpath, separation_disk_diameter, || rng.random::<f64>(), &path_with_bounding_boxes, i) {
1387-
let point_id = PointId::generate();
1388-
result.point_domain.push(point_id, point);
1389-
1390-
// Get the index of the newly added point.
1391-
let point_index = result.point_domain.ids().len() - 1;
1383+
let mut poisson_disk_bezpath = BezPath::new();
13921384

1393-
// If there is a previous point, connect it with the current point by adding a segment.
1394-
if let Some(prev_point_index) = previous_point_index {
1395-
let segment_id = SegmentId::generate();
1396-
result.segment_domain.push(segment_id, prev_point_index, point_index, bezier_rs::BezierHandles::Linear, StrokeId::ZERO);
1385+
for point in bezpath_algorithms::poisson_disk_points(subpath, separation_disk_diameter, || rng.random::<f64>(), &path_with_bounding_boxes, i) {
1386+
if poisson_disk_bezpath.elements().is_empty() {
1387+
poisson_disk_bezpath.move_to(dvec2_to_point(point));
1388+
} else {
1389+
poisson_disk_bezpath.line_to(dvec2_to_point(point));
13971390
}
1398-
1399-
previous_point_index = Some(point_index);
14001391
}
1392+
result.append_bezpath(poisson_disk_bezpath);
14011393
}
14021394

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

0 commit comments

Comments
 (0)