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
use crate::core::fill_rule::FillRule;
use crate::core::solver::Solver;
use crate::float::scale::FixedScaleOverlayError;
use crate::float::string_overlay::FloatStringOverlay;
use crate::string::clip::ClipRule;
use i_float::float::compatible::FloatPointCompatible;
use i_shape::base::data::Paths;
use i_shape::source::resource::ShapeResource;
pub trait FloatClip<R, P>
where
R: ShapeResource<P>,
P: FloatPointCompatible,
{
/// Clips paths according to the specified build and clip rules.
/// - `resource`: A clipping shape.
/// `ShapeResource` can be one of the following:
/// - `Contour`: A contour representing a closed path. This path is interpreted as closed, so it doesn’t require the start and endpoint to be the same for processing.
/// - `Contours`: A collection of contours, each representing a closed path.
/// - `Shapes`: A collection of shapes, where each shape may consist of multiple contours.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `clip_rule`: Clip rule to determine how boundary and inversion settings affect the result.
///
/// # Returns
/// A `Paths<P>` collection of string lines that meet the clipping conditions.
fn clip_by(&self, source: &R, fill_rule: FillRule, clip_rule: ClipRule) -> Paths<P>;
/// Clips paths according to the specified build and clip rules using a fixed float-to-integer scale.
/// - `resource`: A clipping shape.
/// `ShapeResource` can be one of the following:
/// - `Contour`: A contour representing a closed path. This path is interpreted as closed, so it doesn’t require the start and endpoint to be the same for processing.
/// - `Contours`: A collection of contours, each representing a closed path.
/// - `Shapes`: A collection of shapes, where each shape may consist of multiple contours.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `clip_rule`: Clip rule to determine how boundary and inversion settings affect the result.
/// - `scale`: Fixed float-to-integer scale. Use `scale = 1.0 / grid_size` if you prefer grid size semantics.
///
/// # Returns
/// A `Paths<P>` collection of string lines that meet the clipping conditions.
fn clip_by_fixed_scale(
&self,
source: &R,
fill_rule: FillRule,
clip_rule: ClipRule,
scale: P::Scalar,
) -> Result<Paths<P>, FixedScaleOverlayError>;
/// Clips paths according to the specified build and clip rules.
/// - `resource`: A clipping shape.
/// `ShapeResource` can be one of the following:
/// - `Contour`: A contour representing a closed path. This path is interpreted as closed, so it doesn’t require the start and endpoint to be the same for processing.
/// - `Contours`: A collection of contours, each representing a closed path.
/// - `Shapes`: A collection of shapes, where each shape may consist of multiple contours.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `clip_rule`: Clip rule to determine how boundary and inversion settings affect the result.
/// - `solver`: Type of solver to use.
///
/// # Returns
/// A `Paths<P>` collection of string lines that meet the clipping conditions.
fn clip_by_with_solver(
&self,
source: &R,
fill_rule: FillRule,
clip_rule: ClipRule,
solver: Solver,
) -> Paths<P>;
/// Clips paths according to the specified build and clip rules using a fixed float-to-integer scale.
/// - `resource`: A clipping shape.
/// `ShapeResource` can be one of the following:
/// - `Contour`: A contour representing a closed path. This path is interpreted as closed, so it doesn’t require the start and endpoint to be the same for processing.
/// - `Contours`: A collection of contours, each representing a closed path.
/// - `Shapes`: A collection of shapes, where each shape may consist of multiple contours.
/// - `fill_rule`: Fill rule to determine filled areas (non-zero, even-odd, positive, negative).
/// - `clip_rule`: Clip rule to determine how boundary and inversion settings affect the result.
/// - `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 `Paths<P>` collection of string lines that meet the clipping conditions.
fn clip_by_fixed_scale_with_solver(
&self,
source: &R,
fill_rule: FillRule,
clip_rule: ClipRule,
solver: Solver,
scale: P::Scalar,
) -> Result<Paths<P>, FixedScaleOverlayError>;
}
#[cfg(test)]
mod tests {
use crate::core::fill_rule::FillRule;
use crate::float::clip::FloatClip;
use crate::string::clip::ClipRule;
use alloc::vec;
#[test]
fn test_clip_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 clip_rule = ClipRule {
invert: false,
boundary_included: false,
};
let paths = string
.clip_by_fixed_scale(&shape, FillRule::EvenOdd, clip_rule, 10.0)
.unwrap();
assert_eq!(paths.len(), 1);
assert_eq!(paths[0], [[0.0, 1.0], [2.0, 1.0]]);
}
#[test]
fn test_clip_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]];
let clip_rule = ClipRule {
invert: false,
boundary_included: false,
};
assert!(
string
.clip_by_fixed_scale(&shape, FillRule::EvenOdd, clip_rule, 0.0)
.is_err()
);
assert!(
string
.clip_by_fixed_scale(&shape, FillRule::EvenOdd, clip_rule, -1.0)
.is_err()
);
assert!(
string
.clip_by_fixed_scale(&shape, FillRule::EvenOdd, clip_rule, f64::NAN)
.is_err()
);
assert!(
string
.clip_by_fixed_scale(&shape, FillRule::EvenOdd, clip_rule, f64::INFINITY)
.is_err()
);
}
}
impl<R0, R1, P> FloatClip<R0, P> for R1
where
R0: ShapeResource<P>,
R1: ShapeResource<P>,
P: FloatPointCompatible,
{
#[inline]
fn clip_by(&self, resource: &R0, fill_rule: FillRule, clip_rule: ClipRule) -> Paths<P> {
self.clip_by_with_solver(resource, fill_rule, clip_rule, Default::default())
}
#[inline]
fn clip_by_with_solver(
&self,
resource: &R0,
fill_rule: FillRule,
clip_rule: ClipRule,
solver: Solver,
) -> Paths<P> {
FloatStringOverlay::with_shape_and_string(resource, self)
.clip_string_lines_with_solver(fill_rule, clip_rule, solver)
}
#[inline]
fn clip_by_fixed_scale(
&self,
resource: &R0,
fill_rule: FillRule,
clip_rule: ClipRule,
scale: P::Scalar,
) -> Result<Paths<P>, FixedScaleOverlayError> {
self.clip_by_fixed_scale_with_solver(resource, fill_rule, clip_rule, Default::default(), scale)
}
#[inline]
fn clip_by_fixed_scale_with_solver(
&self,
resource: &R0,
fill_rule: FillRule,
clip_rule: ClipRule,
solver: Solver,
scale: P::Scalar,
) -> Result<Paths<P>, FixedScaleOverlayError> {
Ok(
FloatStringOverlay::with_shape_and_string_fixed_scale(resource, self, scale)?
.clip_string_lines_with_solver(fill_rule, clip_rule, solver),
)
}
}