1
- use super :: algorithms:: bezpath_algorithms:: { PERIMETER_ACCURACY , position_on_bezpath, tangent_on_bezpath} ;
1
+ use super :: algorithms:: bezpath_algorithms:: { PERIMETER_ACCURACY , position_on_bezpath, sample_points_on_bezpath , tangent_on_bezpath} ;
2
2
use super :: algorithms:: offset_subpath:: offset_subpath;
3
3
use super :: misc:: { CentroidType , point_to_dvec2} ;
4
4
use super :: style:: { Fill , Gradient , GradientStops , Stroke } ;
@@ -15,7 +15,7 @@ use bezier_rs::{Join, ManipulatorGroup, Subpath, SubpathTValue};
15
15
use core:: f64:: consts:: PI ;
16
16
use core:: hash:: { Hash , Hasher } ;
17
17
use glam:: { DAffine2 , DVec2 } ;
18
- use kurbo:: { Affine , BezPath , Shape } ;
18
+ use kurbo:: { Affine , Shape } ;
19
19
use rand:: { Rng , SeedableRng } ;
20
20
use std:: collections:: hash_map:: DefaultHasher ;
21
21
@@ -1161,63 +1161,21 @@ async fn sample_points(_: impl Ctx, vector_data: VectorDataTable, spacing: f64,
1161
1161
let mut next_segment_index = 0 ;
1162
1162
1163
1163
for mut bezpath in bezpaths {
1164
- let mut sample_bezpath = BezPath :: new ( ) ;
1165
-
1166
1164
// Apply the tranformation to the current bezpath to calculate points after transformation.
1167
1165
bezpath. apply_affine ( Affine :: new ( vector_data_transform. to_cols_array ( ) ) ) ;
1168
1166
1169
1167
let segment_count = bezpath. segments ( ) . count ( ) ;
1170
1168
1171
1169
// For the current bezpath we get its segment's length by calculating the start index and end index.
1172
- let lengths = & subpath_segment_lengths[ next_segment_index..next_segment_index + segment_count] ;
1170
+ let current_bezpath_segments_length = & subpath_segment_lengths[ next_segment_index..next_segment_index + segment_count] ;
1173
1171
1174
1172
// Increment the segment index by the number of segments in the current bezpath to calculate the next bezpath segment's length.
1175
1173
next_segment_index += segment_count;
1176
1174
1177
- // Calculate the total length of the collected segments.
1178
- let total_length: f64 = lengths. iter ( ) . sum ( ) ;
1179
-
1180
- // Adjust the usable length by subtracting start and stop offsets.
1181
- let mut used_length = total_length - start_offset - stop_offset;
1182
-
1183
- if used_length <= 0. {
1175
+ let Some ( mut sample_bezpath) = sample_points_on_bezpath ( bezpath, spacing, start_offset, stop_offset, adaptive_spacing, current_bezpath_segments_length) else {
1184
1176
continue ;
1185
- }
1186
-
1187
- // Determine the number of points to generate along the path.
1188
- let count = if adaptive_spacing {
1189
- // Calculate point count to evenly distribute points while covering the entire path.
1190
- // With adaptive spacing, we widen or narrow the points as necessary to ensure the last point is always at the end of the path.
1191
- ( used_length / spacing) . round ( )
1192
- } else {
1193
- // Calculate point count based on exact spacing, which may not cover the entire path.
1194
-
1195
- // Without adaptive spacing, we just evenly space the points at the exact specified spacing, usually falling short before the end of the path.
1196
- let count = ( used_length / spacing + f64:: EPSILON ) . floor ( ) ;
1197
- used_length -= used_length % spacing;
1198
- count
1199
1177
} ;
1200
1178
1201
- // Skip if there are no points to generate.
1202
- if count < 1. {
1203
- continue ;
1204
- }
1205
- // Generate points along the path based on calculated intervals.
1206
- let max_c = count as usize ;
1207
-
1208
- for c in 0 ..=max_c {
1209
- let fraction = c as f64 / count;
1210
- let current_length = fraction * used_length + start_offset;
1211
- let t = current_length / total_length;
1212
- let point = position_on_bezpath ( & bezpath, t, true , Some ( lengths) ) ;
1213
-
1214
- if sample_bezpath. elements ( ) . is_empty ( ) {
1215
- sample_bezpath. move_to ( point)
1216
- } else {
1217
- sample_bezpath. line_to ( point)
1218
- }
1219
- }
1220
-
1221
1179
// Reverse the transformation applied to the bezpath as the `result` already has the transformation set.
1222
1180
sample_bezpath. apply_affine ( Affine :: new ( vector_data_transform. to_cols_array ( ) ) . inverse ( ) ) ;
1223
1181
0 commit comments