1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::core::fill_rule::FillRule;
use crate::core::solver::Solver;
use crate::float::overlay::OverlayOptions;
use crate::float::scale::FixedScaleOverlayError;
use crate::float::string_overlay::FloatStringOverlay;
use crate::string::rule::StringRule;
use i_float::float::compatible::FloatPointCompatible;
use i_shape::base::data::Shapes;
use i_shape::source::resource::ShapeResource;
/// The `FloatSlice` trait provides methods to slice geometric shapes using a given path or set of paths,
/// allowing for boolean operations based on the specified build rule.
pub trait FloatSlice<R, P>
where
R: ShapeResource<P>,
P: FloatPointCompatible,
{
/// Slices the current shapes by string lines.
///
/// - `resource`: A string lines.
/// `ShapeResource` can be one of the following:
/// - `Path`: A path representing a string line.
/// - `Paths`: A collection of paths, each representing a string line.
/// - `Vec<Paths>`: A collection of grouped paths, where each group may consist of multiple paths.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
///
/// Returns a `Shapes<P>` collection representing the sliced geometry.
///
/// Note: Outer boundary paths have a counterclockwise order, and holes have a clockwise order.
fn slice_by(&self, resource: &R, fill_rule: FillRule) -> Shapes<P>;
/// Slices the current shapes by string lines with a fixed float-to-integer scale.
///
/// - `resource`: A string lines.
/// `ShapeResource` can be one of the following:
/// - `Path`: A path representing a string line.
/// - `Paths`: A collection of paths, each representing a string line.
/// - `Vec<Paths>`: A collection of grouped paths, where each group may consist of multiple paths.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `scale`: Fixed float-to-integer scale. Use `scale = 1.0 / grid_size` if you prefer grid size semantics.
///
/// Returns a `Shapes<P>` collection representing the sliced geometry.
///
/// Note: Outer boundary paths have a counterclockwise order, and holes have a clockwise order.
fn slice_by_fixed_scale(
&self,
resource: &R,
fill_rule: FillRule,
scale: P::Scalar,
) -> Result<Shapes<P>, FixedScaleOverlayError>;
/// Slices the current shapes by string lines.
///
/// - `resource`: A string lines.
/// `ShapeResource` can be one of the following:
/// - `Path`: A path representing a string line.
/// - `Paths`: A collection of paths, each representing a string line.
/// - `Vec<Paths>`: A collection of grouped paths, where each group may consist of multiple paths.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `options`: Adjust custom behavior.
/// - `solver`: Type of solver to use.
/// - Returns a `Shapes<P>` collection representing the sliced geometry.
///
/// Note: Outer boundary paths have a **main_direction** order, and holes have an opposite to **main_direction** order.
fn slice_custom_by(
&self,
resource: &R,
fill_rule: FillRule,
options: OverlayOptions<P::Scalar>,
solver: Solver,
) -> Shapes<P>;
/// Slices the current shapes by string lines with a fixed float-to-integer scale.
///
/// - `resource`: A string lines.
/// `ShapeResource` can be one of the following:
/// - `Path`: A path representing a string line.
/// - `Paths`: A collection of paths, each representing a string line.
/// - `Vec<Paths>`: A collection of grouped paths, where each group may consist of multiple paths.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `options`: Adjust custom behavior.
/// - `solver`: Type of solver to use.
/// - `scale`: Fixed float-to-integer scale. Use `scale = 1.0 / grid_size` if you prefer grid size semantics.
/// - Returns a `Shapes<P>` collection representing the sliced geometry.
///
/// Note: Outer boundary paths have a **main_direction** order, and holes have an opposite to **main_direction** order.
fn slice_custom_by_fixed_scale(
&self,
resource: &R,
fill_rule: FillRule,
options: OverlayOptions<P::Scalar>,
solver: Solver,
scale: P::Scalar,
) -> Result<Shapes<P>, FixedScaleOverlayError>;
}
impl<R0, R1, P> FloatSlice<R0, P> for R1
where
R0: ShapeResource<P>,
R1: ShapeResource<P>,
P: FloatPointCompatible,
{
#[inline]
fn slice_by(&self, resource: &R0, fill_rule: FillRule) -> Shapes<P> {
FloatStringOverlay::with_shape_and_string(self, resource)
.build_graph_view(fill_rule)
.map(|graph| graph.extract_shapes(StringRule::Slice))
.unwrap_or_default()
}
#[inline]
fn slice_by_fixed_scale(
&self,
resource: &R0,
fill_rule: FillRule,
scale: P::Scalar,
) -> Result<Shapes<P>, FixedScaleOverlayError> {
Ok(
FloatStringOverlay::with_shape_and_string_fixed_scale(self, resource, scale)?
.build_graph_view(fill_rule)
.map(|graph| graph.extract_shapes(StringRule::Slice))
.unwrap_or_default(),
)
}
#[inline]
fn slice_custom_by(
&self,
resource: &R0,
fill_rule: FillRule,
options: OverlayOptions<P::Scalar>,
solver: Solver,
) -> Shapes<P> {
FloatStringOverlay::with_shape_and_string(self, resource)
.build_graph_view_with_solver(fill_rule, solver)
.map(|graph| graph.extract_shapes_custom(StringRule::Slice, options))
.unwrap_or_default()
}
#[inline]
fn slice_custom_by_fixed_scale(
&self,
resource: &R0,
fill_rule: FillRule,
options: OverlayOptions<P::Scalar>,
solver: Solver,
scale: P::Scalar,
) -> Result<Shapes<P>, FixedScaleOverlayError> {
Ok(
FloatStringOverlay::with_shape_and_string_fixed_scale(self, resource, scale)?
.build_graph_view_with_solver(fill_rule, solver)
.map(|graph| graph.extract_shapes_custom(StringRule::Slice, options))
.unwrap_or_default(),
)
}
}
#[cfg(test)]
mod tests {
use crate::core::fill_rule::FillRule;
use crate::float::simplify::SimplifyShape;
use crate::float::slice::FloatSlice;
use alloc::vec;
#[test]
fn test_contour_slice() {
let rect = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]];
let shapes = rect.as_slice().simplify_shape(FillRule::NonZero);
assert_eq!(shapes.len(), 1);
assert_eq!(shapes[0].len(), 1);
assert_eq!(shapes[0][0].len(), 4);
}
#[test]
fn test_contour_vec() {
let rect = vec![[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]];
let shapes = rect.simplify_shape(FillRule::NonZero);
assert_eq!(shapes.len(), 1);
assert_eq!(shapes[0].len(), 1);
assert_eq!(shapes[0][0].len(), 4);
}
#[test]
fn test_slice_fixed_scale_ok() {
let shape = vec![[0.0, 0.0], [0.0, 2.0], [2.0, 2.0], [2.0, 0.0]];
let string = vec![[-1.0, 1.0], [3.0, 1.0]];
let shapes = shape
.slice_by_fixed_scale(&string, FillRule::EvenOdd, 10.0)
.unwrap();
assert_eq!(shapes.len(), 2);
assert_eq!(shapes[0].len(), 1);
assert_eq!(shapes[1].len(), 1);
assert_eq!(shapes[0][0].len(), 4);
assert_eq!(shapes[1][0].len(), 4);
}
#[test]
fn test_slice_fixed_scale_invalid() {
let shape = vec![[0.0, 0.0], [0.0, 2.0], [2.0, 2.0], [2.0, 0.0]];
let string = vec![[-1.0, 1.0], [3.0, 1.0]];
assert!(
shape
.slice_by_fixed_scale(&string, FillRule::EvenOdd, 0.0)
.is_err()
);
assert!(
shape
.slice_by_fixed_scale(&string, FillRule::EvenOdd, -1.0)
.is_err()
);
assert!(
shape
.slice_by_fixed_scale(&string, FillRule::EvenOdd, f64::NAN)
.is_err()
);
assert!(
shape
.slice_by_fixed_scale(&string, FillRule::EvenOdd, f64::INFINITY)
.is_err()
);
}
}