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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! Boolean fragment classification.
//!
//! This module is the split/classify/select layer before graph traversal and
//! loop assembly. It deliberately does not resolve shared-boundary fragments:
//! those need overlap-aware traversal, not a midpoint guess.
use crate::boolean_boundary::{BooleanBoundaryFragmentSet, DirectedBooleanFragment};
use crate::{
Classification, CurveError, CurvePolicy, CurveResult, RegionContourRole, RegionFragmentSet,
RegionPointLocation, RegionSide, RegionView2,
};
/// Boolean operation requested between two regions.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BooleanOp {
/// Filled area in either operand.
Union,
/// Filled area common to both operands.
Intersection,
/// Filled area in the first operand but not the second.
Difference,
/// Filled area in exactly one operand.
Xor,
}
/// How a classified source fragment participates in a boolean result.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BooleanFragmentAction {
/// The fragment is not part of this operation's boundary.
Discard,
/// Emit the fragment in its source traversal direction.
KeepSourceDirection,
/// Emit the fragment in the reverse of its source traversal direction.
KeepReversed,
/// The representative point lies on the other region's boundary.
///
/// Shared boundaries need a dedicated overlap resolver. Treating them as
/// inside or outside would recreate the tolerance-first ambiguity this
/// crate is avoiding.
BoundaryNeedsResolution,
}
impl BooleanFragmentAction {
/// Returns true when this action emits a directed fragment immediately.
pub const fn emits_fragment(self) -> bool {
matches!(self, Self::KeepSourceDirection | Self::KeepReversed)
}
}
/// Boolean classification for one source fragment.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BooleanFragmentClassification {
/// Which keyed source contour owns this fragment.
pub key: crate::RegionContourKey,
/// Index within [`crate::RegionContourFragments::fragments`].
pub fragment_index: usize,
/// Location of the fragment representative point in the opposite region.
pub opposite_location: RegionPointLocation,
/// Selection action for the requested operation.
pub action: BooleanFragmentAction,
}
/// Boolean classification for all fragments in a region-pair fragment set.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct BooleanFragmentSelection {
classifications: Vec<BooleanFragmentClassification>,
}
impl BooleanFragmentSelection {
/// Constructs a selection from already-classified fragments.
pub const fn new(classifications: Vec<BooleanFragmentClassification>) -> Self {
Self { classifications }
}
/// Returns all fragment classifications in region-fragment order.
pub fn classifications(&self) -> &[BooleanFragmentClassification] {
&self.classifications
}
/// Consumes the selection and returns the fragment classifications.
pub fn into_classifications(self) -> Vec<BooleanFragmentClassification> {
self.classifications
}
/// Returns true when no fragments were classified.
pub fn is_empty(&self) -> bool {
self.classifications.is_empty()
}
/// Returns the number of classified fragments.
pub fn len(&self) -> usize {
self.classifications.len()
}
/// Counts classifications with the given action.
pub fn count_action(&self, action: BooleanFragmentAction) -> usize {
self.classifications
.iter()
.filter(|classification| classification.action == action)
.count()
}
/// Converts selected classifications into directed boundary fragments.
///
/// This performs the "emit in source direction or reverse direction" step
/// after local boolean classification. Greiner-Hormann style traversal
/// follows selected directed polygon chains after entry/exit classification
/// (G. Greiner and K. Hormann, "Efficient clipping of arbitrary polygons,"
/// ACM Transactions on Graphics 17(2), 71-83, 1998). We keep shared
/// boundaries in `unresolved_boundaries` instead of applying a local
/// tie-breaker because degenerate polygon clipping papers, including
/// Foster, Hormann, and Popa, "Clipping simple polygons with degenerate
/// intersections," Computers & Graphics: X 2, 100007, 2019, show that
/// boundary coincidences need explicit handling separate from ordinary
/// enter/exit classification.
pub fn emit_boundary_fragments(
&self,
fragments: &RegionFragmentSet,
) -> CurveResult<BooleanBoundaryFragmentSet> {
let mut directed_fragments = Vec::new();
let mut unresolved_boundaries = Vec::new();
for classification in &self.classifications {
match classification.action {
BooleanFragmentAction::Discard => {}
BooleanFragmentAction::BoundaryNeedsResolution => {
unresolved_boundaries.push(classification.clone());
}
BooleanFragmentAction::KeepSourceDirection
| BooleanFragmentAction::KeepReversed => {
let source = fragment_for_classification(fragments, classification)?;
let segment = match classification.action {
BooleanFragmentAction::KeepSourceDirection => source.segment.clone(),
BooleanFragmentAction::KeepReversed => source.segment.reversed(),
BooleanFragmentAction::Discard
| BooleanFragmentAction::BoundaryNeedsResolution => unreachable!(),
};
directed_fragments.push(DirectedBooleanFragment {
key: classification.key,
fragment_index: classification.fragment_index,
segment,
});
}
}
}
Ok(BooleanBoundaryFragmentSet::new(
directed_fragments,
unresolved_boundaries,
))
}
}
impl BooleanOp {
fn action_for(
self,
source_side: RegionSide,
source_role: RegionContourRole,
opposite_location: RegionPointLocation,
) -> BooleanFragmentAction {
use BooleanFragmentAction::{
BoundaryNeedsResolution, Discard, KeepReversed, KeepSourceDirection,
};
use RegionPointLocation::{Boundary, Inside, Outside};
use RegionSide::{First, Second};
let material_action = match opposite_location {
Boundary => BoundaryNeedsResolution,
Outside => match self {
Self::Union | Self::Difference | Self::Xor => {
if source_side == Second && self == Self::Difference {
Discard
} else {
KeepSourceDirection
}
}
Self::Intersection => Discard,
},
Inside => match self {
Self::Intersection => KeepSourceDirection,
Self::Difference => {
if source_side == First {
Discard
} else {
KeepReversed
}
}
Self::Union => Discard,
Self::Xor => KeepReversed,
},
};
match source_role {
RegionContourRole::Material => material_action,
RegionContourRole::Hole => reverse_emitted_action(material_action),
}
}
}
fn reverse_emitted_action(action: BooleanFragmentAction) -> BooleanFragmentAction {
use BooleanFragmentAction::{
BoundaryNeedsResolution, Discard, KeepReversed, KeepSourceDirection,
};
// Region contour bins carry signed fill roles independently of storage
// direction. When a selected source fragment belongs to a hole contour, the
// local boundary orientation is the opposite of an equivalent material
// contour. Vatti frames clipping output as fill-state transitions
// (B. R. Vatti, "A generic solution to polygon clipping," Communications
// of the ACM 35(7), 56-63, 1992); this is the signed-contour equivalent of
// flipping the transition direction for negative fill edges.
match action {
KeepSourceDirection => KeepReversed,
KeepReversed => KeepSourceDirection,
Discard => Discard,
BoundaryNeedsResolution => BoundaryNeedsResolution,
}
}
fn fragment_for_classification<'a>(
fragments: &'a RegionFragmentSet,
classification: &BooleanFragmentClassification,
) -> CurveResult<&'a crate::ContourFragment> {
let contour_fragments = fragments
.fragments_for_contour(classification.key)
.ok_or_else(|| {
CurveError::Topology("boolean classification references a missing contour".into())
})?;
contour_fragments
.fragments
.fragments()
.get(classification.fragment_index)
.ok_or_else(|| {
CurveError::Topology("boolean classification references a missing fragment".into())
})
}
impl RegionFragmentSet {
/// Classifies fragments against the opposite region for a boolean operation.
///
/// Algorithm note: this is the local selection stage used by many planar
/// clipping algorithms after intersection insertion. Greiner and Hormann
/// mark split polygon chains as entry/exit after intersections are inserted
/// (G. Greiner and K. Hormann, "Efficient clipping of arbitrary polygons,"
/// ACM Transactions on Graphics 17(2), 71-83, 1998). Vatti's sweep-line
/// algorithm also reduces result construction to sorted edge events and
/// fill-state transitions (B. R. Vatti, "A generic solution to polygon
/// clipping," Communications of the ACM 35(7), 56-63, 1992). Martinez,
/// Rueda, and Feito formalize boolean selection from segment classifications
/// for general polygons (F. Martinez, A. J. Rueda, and F. R. Feito, "A new
/// algorithm for computing Boolean operations on polygons," Computers &
/// Geosciences 35(6), 1177-1185, 2009). `hypercurve` keeps this stage
/// explicit and returns `BoundaryNeedsResolution` instead of folding shared
/// boundaries into an epsilon-based inside/outside decision.
pub fn classify_for_boolean(
&self,
first: &RegionView2<'_>,
second: &RegionView2<'_>,
op: BooleanOp,
policy: &CurvePolicy,
) -> CurveResult<Classification<BooleanFragmentSelection>> {
self.classify_for_boolean_with_point_classifier(op, policy, |source_side, sample| {
let opposite = match source_side {
RegionSide::First => second,
RegionSide::Second => first,
};
opposite.classify_point(sample, policy)
})
}
/// Classifies fragments using a caller-supplied opposite-region point
/// classifier.
///
/// Prepared boolean paths use this hook to keep the exact same fragment
/// selection rules while reusing cached region classifiers.
pub(crate) fn classify_for_boolean_with_point_classifier<F>(
&self,
op: BooleanOp,
policy: &CurvePolicy,
mut classify_opposite: F,
) -> CurveResult<Classification<BooleanFragmentSelection>>
where
F: FnMut(RegionSide, &crate::Point2) -> Classification<RegionPointLocation>,
{
let mut classifications = Vec::new();
for contour_fragments in self.contours() {
for (fragment_index, fragment) in
contour_fragments.fragments.fragments().iter().enumerate()
{
let sample = match fragment.segment.representative_point(policy)? {
Classification::Decided(sample) => sample,
Classification::Uncertain(reason) => {
return Ok(Classification::Uncertain(reason));
}
};
let source_side = contour_fragments.key.side;
let opposite_location = match classify_opposite(source_side, &sample) {
Classification::Decided(location) => location,
Classification::Uncertain(reason) => {
return Ok(Classification::Uncertain(reason));
}
};
let action =
op.action_for(source_side, contour_fragments.key.role, opposite_location);
classifications.push(BooleanFragmentClassification {
key: contour_fragments.key,
fragment_index,
opposite_location,
action,
});
}
}
Ok(Classification::Decided(BooleanFragmentSelection::new(
classifications,
)))
}
}