From 0c57d5e79ec881864109a959111f2a9876c2f6fc Mon Sep 17 00:00:00 2001 From: indierusty Date: Sun, 3 Aug 2025 12:57:13 +0530 Subject: [PATCH] didn't work --- node-graph/gcore/src/vector/algorithms/centroid.rs | 8 ++++---- node-graph/gcore/src/vector/vector_nodes.rs | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/node-graph/gcore/src/vector/algorithms/centroid.rs b/node-graph/gcore/src/vector/algorithms/centroid.rs index a28b675784..3b69252e75 100644 --- a/node-graph/gcore/src/vector/algorithms/centroid.rs +++ b/node-graph/gcore/src/vector/algorithms/centroid.rs @@ -1,5 +1,5 @@ -use glam::DVec2; use kurbo::{BezPath, CubicBez, ParamCurve, PathEl, PathSeg, QuadBez, Vec2}; +use std::f64; use crate::math::polynomial::pathseg_to_parametric_polynomial; @@ -104,7 +104,7 @@ fn pathseg_length_centroid_and_length(segment: PathSeg, accuracy: Option) - /// If the comparison condition is not satisfied, the function takes the larger `t`-value of the two. /// /// **NOTE**: if an intersection were to occur within an `error` distance away from an anchor point, the algorithm will filter that intersection out. -pub fn bezpath_area_centroid_and_area(mut bezpath: BezPath, error: Option, minimum_separation: Option) -> Option<(DVec2, f64)> { +pub fn bezpath_area_centroid_and_area(mut bezpath: BezPath, error: Option, minimum_separation: Option) -> Option<(Vec2, f64)> { let all_intersections = bezpath_all_self_intersections(bezpath.clone(), error, minimum_separation); let mut current_sign: f64 = 1.; @@ -145,8 +145,8 @@ pub fn bezpath_area_centroid_and_area(mut bezpath: BezPath, error: Option, .reduce(|(x1, y1, area1), (x2, y2, area2)| (x1 + x2, y1 + y2, area1 + area2))?; if area.abs() < error.unwrap_or(MAX_ABSOLUTE_DIFFERENCE) { - return Some((DVec2::NAN, 0.)); + return Some((Vec2::new(f64::NAN, f64::NAN), 0.)); } - Some((DVec2::new(x_sum / area, y_sum / area), area.abs())) + Some((Vec2::new(x_sum / area, y_sum / area), area.abs())) } diff --git a/node-graph/gcore/src/vector/vector_nodes.rs b/node-graph/gcore/src/vector/vector_nodes.rs index c7af5a38d4..8687f3bcd2 100644 --- a/node-graph/gcore/src/vector/vector_nodes.rs +++ b/node-graph/gcore/src/vector/vector_nodes.rs @@ -1,4 +1,5 @@ use super::algorithms::bezpath_algorithms::{self, TValue, evaluate_bezpath, sample_polyline_on_bezpath, split_bezpath, tangent_on_bezpath}; +use super::algorithms::centroid::{bezpath_area_centroid_and_area, bezpath_length_centroid_and_length}; use super::algorithms::offset_subpath::offset_bezpath; use super::algorithms::spline::{solve_spline_first_handle_closed, solve_spline_first_handle_open}; use super::misc::{CentroidType, bezpath_from_manipulator_groups, bezpath_to_manipulator_groups, point_to_dvec2}; @@ -2075,11 +2076,13 @@ async fn centroid(ctx: impl Ctx + CloneVarArgs + ExtractAll, vector_data: impl N let mut sum = 0.; for vector_data_instance in vector_data.instance_ref_iter() { - for subpath in vector_data_instance.instance.stroke_bezier_paths() { + for bezpath in vector_data_instance.instance.stroke_bezpath_iter() { let partial = match centroid_type { - CentroidType::Area => subpath.area_centroid_and_area(Some(1e-3), Some(1e-3)).filter(|(_, area)| *area > 0.), - CentroidType::Length => subpath.length_centroid_and_length(None, true), + CentroidType::Area => bezpath_area_centroid_and_area(bezpath, Some(1e-3), Some(1e-3)).filter(|(_, area)| *area > 0.), + CentroidType::Length => bezpath_length_centroid_and_length(bezpath, None, true), }; + let partial = partial.map(|(centroid, area)| (DVec2::new(centroid.x, centroid.y), area)); + if let Some((subpath_centroid, area_or_length)) = partial { let subpath_centroid = vector_data_instance.transform.transform_point2(subpath_centroid);