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
//! Classification helpers for curve topology.
//!
//! These helpers centralize the "branch only after the sign/order relation is
//! known" rule that keeps geometry algorithms robust. The exact-predicate
//! discipline follows Shewchuk, "Adaptive Precision Floating-Point Arithmetic
//! and Fast Robust Geometric Predicates" (*Discrete & Computational Geometry*
//! 18(3), 305-363, 1997). `EdgePreview` is the named exception for UI and IO
//! boundaries where lossy finite-precision output is already part of the
//! contract; finite-precision intersection output and degeneracy issues are
//! discussed by Hobby, "Practical Segment Intersection with Finite Precision
//! Output" (*Computational Geometry* 13(4), 199-214, 1999).
use std::cmp::Ordering;
use hyperreal::{Real, RealSign, ZeroKnowledge as ZeroStatus};
use crate::{CurvePolicy, NumericMode, Point2};
/// Result of a classification step.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Classification<T> {
/// The classification was decided.
Decided(T),
/// The active policy could not decide the classification.
Uncertain(UncertaintyReason),
}
impl<T> Classification<T> {
/// Returns true when this classification contains a decided value.
pub const fn is_decided(&self) -> bool {
matches!(self, Self::Decided(_))
}
/// Returns true when this classification carries an explicit uncertainty reason.
pub const fn is_uncertain(&self) -> bool {
matches!(self, Self::Uncertain(_))
}
/// Maps a decided value while preserving uncertainty unchanged.
pub fn map<U, F>(self, f: F) -> Classification<U>
where
F: FnOnce(T) -> U,
{
match self {
Self::Decided(value) => Classification::Decided(f(value)),
Self::Uncertain(reason) => Classification::Uncertain(reason),
}
}
}
/// Reason an operation could not decide a topology branch.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UncertaintyReason {
/// A Real sign could not be proven under the active policy.
RealSign,
/// Predicate policy could not decide the branch.
Predicate,
/// Parameter ordering could not be decided.
Ordering,
/// The query lies on a boundary where the requested Real result is undefined.
Boundary,
/// The requested operation is not supported by this slice.
Unsupported,
}
/// Side of an oriented line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LineSide {
/// Point lies to the left of the oriented line.
Left,
/// Point lies to the right of the oriented line.
Right,
/// Point lies on the line.
On,
}
impl LineSide {
pub(crate) const fn from_real_sign(sign: RealSign) -> Self {
match sign {
RealSign::Positive => Self::Left,
RealSign::Negative => Self::Right,
RealSign::Zero => Self::On,
}
}
#[cfg(feature = "predicates")]
pub(crate) const fn from_predicate_sign(sign: hyperlimit::Sign) -> Self {
match sign {
hyperlimit::Sign::Positive => Self::Left,
hyperlimit::Sign::Negative => Self::Right,
hyperlimit::Sign::Zero => Self::On,
}
}
}
pub(crate) fn classify_oriented_line(
from: &Point2,
to: &Point2,
point: &Point2,
policy: &CurvePolicy,
) -> Classification<LineSide> {
if matches!(policy.numeric_mode, NumericMode::EdgePreview) {
// Preview mode is a display/editing classifier. Use the current Real
// approximation consistently here instead of sending rotated radical
// expressions into the certified predicate path, otherwise arc sweep
// checks can reject legitimate preview intersections before the exact
// segment relation has a chance to retain them as candidates.
let det = orient2d_real_expr(from, to, point);
return real_sign(&det, policy)
.map(LineSide::from_real_sign)
.map(Classification::Decided)
.unwrap_or(Classification::Uncertain(UncertaintyReason::RealSign));
}
#[cfg(feature = "predicates")]
{
// This is the orientation determinant used throughout planar
// computational geometry. When available, route it through hyperlimit's
// certified predicate path rather than comparing approximate floats,
// matching Shewchuk's robust-predicate recommendation for topology
// branches.
let predicate_outcome = hyperlimit::orient::orient2d_with_policy(
&predicate_point(from),
&predicate_point(to),
&predicate_point(point),
policy.predicate_policy,
);
match predicate_outcome {
hyperlimit::PredicateOutcome::Decided { value, .. } => {
Classification::Decided(LineSide::from_predicate_sign(value))
}
hyperlimit::PredicateOutcome::Unknown { .. } => {
Classification::Uncertain(UncertaintyReason::Predicate)
}
}
}
#[cfg(not(feature = "predicates"))]
{
let det = orient2d_real_expr(from, to, point);
real_sign(&det, policy)
.map(LineSide::from_real_sign)
.map(Classification::Decided)
.unwrap_or(Classification::Uncertain(UncertaintyReason::RealSign))
}
}
pub(crate) fn orient2d_real_expr(from: &Point2, to: &Point2, point: &Point2) -> Real {
let abx = to.x() - from.x();
let aby = to.y() - from.y();
let acx = point.x() - from.x();
let acy = point.y() - from.y();
(&abx * &acy) - (&aby * &acx)
}
pub(crate) fn real_sign(value: &Real, policy: &CurvePolicy) -> Option<RealSign> {
if matches!(policy.numeric_mode, NumericMode::EdgePreview)
&& let Some(value) = value.to_f64_lossy()
&& value.is_finite()
{
// Edge-preview mode is allowed to collapse a hyperreal value to the
// current `f64` approximation before committing a UI/display branch.
// This keeps radical expressions from carrying stale structural signs
// into broad-phase and sweep tests.
return if value > 0.0 {
Some(RealSign::Positive)
} else if value < 0.0 {
Some(RealSign::Negative)
} else {
Some(RealSign::Zero)
};
}
if let Some(sign) = value.structural_facts().sign {
return Some(sign);
}
if let Some(sign) = value.refine_sign_until(-4096) {
return Some(sign);
}
None
}
pub(crate) fn is_zero(value: &Real, policy: &CurvePolicy) -> Option<bool> {
match value.zero_status() {
ZeroStatus::Zero => Some(true),
ZeroStatus::NonZero => Some(false),
ZeroStatus::Unknown => real_sign(value, policy).map(|sign| sign == RealSign::Zero),
}
}
pub(crate) fn compare_reals(left: &Real, right: &Real, policy: &CurvePolicy) -> Option<Ordering> {
#[cfg(feature = "predicates")]
if !matches!(policy.numeric_mode, NumericMode::EdgePreview) {
// Curve parameter ordering is a topology predicate: it decides whether
// an intersection root lies on a segment, whether two split markers
// coincide, and how degenerate overlaps are classified. Route the sign
// of `left - right` through hyperlimit's predicate pipeline so scalar
// ordering has the same certified/unknown boundary as orientation.
// This follows Yap's exact geometric computation split between exact
// predicate decisions and approximate edge views; see Yap, "Towards
// Exact Geometric Computation," Computational Geometry 7.1-2 (1997).
return hyperlimit::compare_reals_with_policy(left, right, policy.predicate_policy).value();
}
let delta = left - right;
real_sign(&delta, policy).map(|sign| match sign {
RealSign::Negative => Ordering::Less,
RealSign::Zero => Ordering::Equal,
RealSign::Positive => Ordering::Greater,
})
}
pub(crate) fn compare_reals_for_split_ordering(
left: &Real,
right: &Real,
policy: &CurvePolicy,
) -> Option<Ordering> {
if matches!(policy.numeric_mode, NumericMode::EdgePreview)
&& let (Some(left), Some(right)) = (left.to_f64_lossy(), right.to_f64_lossy())
&& left.is_finite()
&& right.is_finite()
{
// Split marker ordering feeds display/event reconstruction, not a
// certified topology decision, in `EdgePreview`. Comparing the same
// finite values that will be rendered avoids artificial branch
// vertices from unsimplified radical expressions; this is the same
// finite-output boundary Hobby treats as separate from exact segment
// intersection predicates.
return left.partial_cmp(&right);
}
compare_reals(left, right, policy)
}
pub(crate) fn sort_pair(a: Real, b: Real, policy: &CurvePolicy) -> Option<(Real, Real)> {
match compare_reals(&a, &b, policy)? {
Ordering::Greater => Some((b, a)),
Ordering::Less | Ordering::Equal => Some((a, b)),
}
}
pub(crate) fn max_real(a: Real, b: Real, policy: &CurvePolicy) -> Option<Real> {
match compare_reals(&a, &b, policy)? {
Ordering::Less => Some(b),
Ordering::Equal | Ordering::Greater => Some(a),
}
}
pub(crate) fn min_real(a: Real, b: Real, policy: &CurvePolicy) -> Option<Real> {
match compare_reals(&a, &b, policy)? {
Ordering::Greater => Some(b),
Ordering::Less | Ordering::Equal => Some(a),
}
}
pub(crate) fn in_closed_unit_interval(value: &Real, policy: &CurvePolicy) -> Option<bool> {
// Edge-preview f64 parameters are candidate filters only: decisively
// out-of-range values cannot represent finite segment hits, while
// near-boundary values still fall through to exact comparison.
if matches!(policy.numeric_mode, NumericMode::EdgePreview)
&& let Some(approx) = value.to_f64_lossy()
{
let tolerance = policy
.tolerance
.map(|tolerance| tolerance.absolute.max(tolerance.relative))
.unwrap_or(1e-12);
if approx.is_finite() && (approx < -tolerance || approx > 1.0 + tolerance) {
return Some(false);
}
}
let zero = Real::zero();
let one = Real::one();
let lower = compare_reals(value, &zero, policy)?;
let upper = compare_reals(value, &one, policy)?;
Some(!matches!(lower, Ordering::Less) && !matches!(upper, Ordering::Greater))
}
pub(crate) fn at_unit_interval_endpoint(value: &Real, policy: &CurvePolicy) -> Option<bool> {
let zero = Real::zero();
let one = Real::one();
let at_zero = compare_reals(value, &zero, policy)? == Ordering::Equal;
let at_one = compare_reals(value, &one, policy)? == Ordering::Equal;
Some(at_zero || at_one)
}
#[cfg(feature = "predicates")]
fn predicate_point(point: &Point2) -> hyperlimit::Point2 {
hyperlimit::Point2::new(point.x().clone(), point.y().clone())
}