didn't work

This commit is contained in:
indierusty
2025-08-03 12:57:13 +05:30
parent 33538a7381
commit 0c57d5e79e
2 changed files with 10 additions and 7 deletions

View File

@@ -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<f64>) -
/// 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<f64>, minimum_separation: Option<f64>) -> Option<(DVec2, f64)> {
pub fn bezpath_area_centroid_and_area(mut bezpath: BezPath, error: Option<f64>, minimum_separation: Option<f64>) -> 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<f64>,
.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()))
}

View File

@@ -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);