u-nesting-core 0.3.5

Core traits and abstractions for U-Nesting spatial optimization engine
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Robust geometric predicates for numerical stability.
//!
//! This module provides numerically robust implementations of geometric predicates
//! using Shewchuk's adaptive precision floating-point arithmetic.
//!
//! ## Core Predicates
//!
//! The core predicates (`orient2d`, `orient2d_filtered`, `point_in_triangle`,
//! `is_convex`, `is_ccw`, `Orientation`) are provided by `u-geometry` and
//! re-exported here for API compatibility.
//!
//! ## Nesting-Specific Utilities
//!
//! - [`signed_area_robust`]: Signed polygon area with Kahan summation for numerical stability
//! - [`ScalingConfig`]: Coordinate scaling for integer-range robust arithmetic
//! - [`snap_to_grid`] / [`snap_polygon_to_grid`]: Grid-snapping utilities
//!
//! ## References
//!
//! - Shewchuk, J.R. (1997). "Adaptive Precision Floating-Point Arithmetic and
//!   Fast Robust Predicates for Computational Geometry"
//! - <https://www.cs.cmu.edu/~quake/robust.html>
//!
//! ## Example
//!
//! ```rust
//! use u_nesting_core::robust::{orient2d, Orientation};
//!
//! // Check orientation of three points
//! let a = (0.0, 0.0);
//! let b = (1.0, 0.0);
//! let c = (0.5, 1.0);
//!
//! match orient2d(a, b, c) {
//!     Orientation::CounterClockwise => println!("Left turn"),
//!     Orientation::Clockwise => println!("Right turn"),
//!     Orientation::Collinear => println!("Straight"),
//! }
//! ```

// ============================================================================
// Re-exports from u-geometry (canonical implementations)
// ============================================================================

pub use u_geometry::robust::{
    is_ccw, is_convex, orient2d, orient2d_filtered, orient2d_raw, point_in_triangle,
    point_in_triangle_inclusive, Orientation,
};

// ============================================================================
// Nesting-specific aliases (backward compatibility)
// ============================================================================

/// Checks if a point lies strictly inside a triangle.
///
/// This is an alias for [`point_in_triangle`] preserved for backward compatibility
/// with existing u-nesting code that uses the `_robust` suffix convention.
#[inline]
pub fn point_in_triangle_robust(
    p: (f64, f64),
    a: (f64, f64),
    b: (f64, f64),
    c: (f64, f64),
) -> bool {
    point_in_triangle(p, a, b, c)
}

/// Checks if a point lies inside or on the boundary of a triangle.
///
/// This is an alias for [`point_in_triangle_inclusive`] preserved for backward
/// compatibility with existing u-nesting code.
#[inline]
pub fn point_in_triangle_inclusive_robust(
    p: (f64, f64),
    a: (f64, f64),
    b: (f64, f64),
    c: (f64, f64),
) -> bool {
    point_in_triangle_inclusive(p, a, b, c)
}

/// Checks if a polygon is convex using robust orientation tests.
///
/// This is an alias for [`is_convex`] preserved for backward compatibility.
#[inline]
pub fn is_convex_robust(polygon: &[(f64, f64)]) -> bool {
    is_convex(polygon)
}

/// Checks if a polygon has counter-clockwise winding order.
///
/// This is an alias for [`is_ccw`] preserved for backward compatibility.
#[inline]
pub fn is_ccw_robust(polygon: &[(f64, f64)]) -> bool {
    is_ccw(polygon)
}

// ============================================================================
// Nesting-Specific Utilities (not in u-geometry)
// ============================================================================

/// Computes the signed area of a polygon using Kahan summation.
///
/// The shoelace formula is used with Kahan compensated summation for
/// better numerical stability on large polygons with diverse vertex scales.
///
/// # Arguments
///
/// * `polygon` - Slice of polygon vertices in order
///
/// # Returns
///
/// Positive area if counter-clockwise, negative if clockwise
///
/// # Reference
/// Kahan (1965), "Pracniques: Further Remarks on Reducing Truncation Errors"
pub fn signed_area_robust(polygon: &[(f64, f64)]) -> f64 {
    let n = polygon.len();
    if n < 3 {
        return 0.0;
    }

    // Kahan summation for better numerical stability
    let mut sum = 0.0;
    let mut c = 0.0; // Compensation for lost low-order bits

    for i in 0..n {
        let j = (i + 1) % n;
        let term = polygon[i].0 * polygon[j].1 - polygon[j].0 * polygon[i].1;

        let y = term - c;
        let t = sum + y;
        c = (t - sum) - y;
        sum = t;
    }

    sum / 2.0
}

// ============================================================================
// Integer Coordinate Scaling
// ============================================================================

/// Configuration for coordinate scaling.
#[derive(Debug, Clone, Copy)]
pub struct ScalingConfig {
    /// The scale factor (coordinates are multiplied by this value).
    pub scale: f64,
    /// The inverse scale factor (for converting back).
    pub inv_scale: f64,
}

impl ScalingConfig {
    /// Creates a new scaling configuration.
    ///
    /// # Arguments
    ///
    /// * `precision` - Number of decimal places to preserve
    ///
    /// # Example
    ///
    /// ```rust
    /// use u_nesting_core::robust::ScalingConfig;
    ///
    /// // Preserve 3 decimal places
    /// let config = ScalingConfig::new(3);
    /// assert_eq!(config.scale, 1000.0);
    /// ```
    pub fn new(precision: u32) -> Self {
        let scale = 10.0_f64.powi(precision as i32);
        Self {
            scale,
            inv_scale: 1.0 / scale,
        }
    }

    /// Scales a coordinate to integer range.
    #[inline]
    pub fn scale_coord(&self, x: f64) -> f64 {
        (x * self.scale).round()
    }

    /// Scales a point to integer range.
    #[inline]
    pub fn scale_point(&self, p: (f64, f64)) -> (f64, f64) {
        (self.scale_coord(p.0), self.scale_coord(p.1))
    }

    /// Unscales a coordinate back to original range.
    #[inline]
    pub fn unscale_coord(&self, x: f64) -> f64 {
        x * self.inv_scale
    }

    /// Unscales a point back to original range.
    #[inline]
    pub fn unscale_point(&self, p: (f64, f64)) -> (f64, f64) {
        (self.unscale_coord(p.0), self.unscale_coord(p.1))
    }

    /// Scales an entire polygon.
    pub fn scale_polygon(&self, polygon: &[(f64, f64)]) -> Vec<(f64, f64)> {
        polygon.iter().map(|&p| self.scale_point(p)).collect()
    }

    /// Unscales an entire polygon.
    pub fn unscale_polygon(&self, polygon: &[(f64, f64)]) -> Vec<(f64, f64)> {
        polygon.iter().map(|&p| self.unscale_point(p)).collect()
    }
}

impl Default for ScalingConfig {
    /// Default scaling preserves 6 decimal places.
    fn default() -> Self {
        Self::new(6)
    }
}

/// Snaps coordinates to a grid of the given resolution.
///
/// # Arguments
///
/// * `point` - The point to snap
/// * `resolution` - Grid cell size
///
/// # Returns
///
/// The point snapped to the nearest grid intersection
#[inline]
pub fn snap_to_grid(point: (f64, f64), resolution: f64) -> (f64, f64) {
    (
        (point.0 / resolution).round() * resolution,
        (point.1 / resolution).round() * resolution,
    )
}

/// Snaps an entire polygon to a grid.
pub fn snap_polygon_to_grid(polygon: &[(f64, f64)], resolution: f64) -> Vec<(f64, f64)> {
    polygon
        .iter()
        .map(|&p| snap_to_grid(p, resolution))
        .collect()
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_orient2d_basic() {
        // Counter-clockwise triangle
        let a = (0.0, 0.0);
        let b = (1.0, 0.0);
        let c = (0.5, 1.0);

        assert_eq!(orient2d(a, b, c), Orientation::CounterClockwise);
        assert_eq!(orient2d(a, c, b), Orientation::Clockwise);
    }

    #[test]
    fn test_orient2d_collinear() {
        let a = (0.0, 0.0);
        let b = (1.0, 1.0);
        let c = (2.0, 2.0);

        assert_eq!(orient2d(a, b, c), Orientation::Collinear);
    }

    #[test]
    fn test_orient2d_near_collinear() {
        let a = (0.0, 0.0);
        let b = (1.0, 1.0);
        let c = (2.0, 2.0 + 1e-15);

        let result = orient2d(a, b, c);
        assert!(
            result == Orientation::Collinear || result == Orientation::CounterClockwise,
            "Expected collinear or CCW, got {:?}",
            result
        );
    }

    #[test]
    fn test_orient2d_filtered_fast_path() {
        let a = (0.0, 0.0);
        let b = (10.0, 0.0);
        let c = (5.0, 10.0);

        assert_eq!(orient2d_filtered(a, b, c), Orientation::CounterClockwise);
    }

    #[test]
    fn test_point_in_triangle_robust() {
        let a = (0.0, 0.0);
        let b = (10.0, 0.0);
        let c = (5.0, 10.0);

        assert!(point_in_triangle_robust((5.0, 3.0), a, b, c));
        assert!(!point_in_triangle_robust((20.0, 5.0), a, b, c));
        assert!(!point_in_triangle_robust((5.0, 0.0), a, b, c));
    }

    #[test]
    fn test_point_in_triangle_inclusive() {
        let a = (0.0, 0.0);
        let b = (10.0, 0.0);
        let c = (5.0, 10.0);

        assert!(point_in_triangle_inclusive_robust((5.0, 3.0), a, b, c));
        assert!(point_in_triangle_inclusive_robust((5.0, 0.0), a, b, c));
        assert!(point_in_triangle_inclusive_robust((0.0, 0.0), a, b, c));
        assert!(!point_in_triangle_inclusive_robust((20.0, 5.0), a, b, c));
    }

    #[test]
    fn test_is_convex_robust() {
        let square = vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)];
        assert!(is_convex_robust(&square));

        let triangle = vec![(0.0, 0.0), (10.0, 0.0), (5.0, 10.0)];
        assert!(is_convex_robust(&triangle));

        let l_shape = vec![
            (0.0, 0.0),
            (10.0, 0.0),
            (10.0, 5.0),
            (5.0, 5.0),
            (5.0, 10.0),
            (0.0, 10.0),
        ];
        assert!(!is_convex_robust(&l_shape));
    }

    #[test]
    fn test_is_ccw_robust() {
        let ccw_square = vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)];
        assert!(is_ccw_robust(&ccw_square));

        let cw_square = vec![(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0)];
        assert!(!is_ccw_robust(&cw_square));
    }

    #[test]
    fn test_signed_area_robust() {
        let ccw_square = vec![(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)];
        let area = signed_area_robust(&ccw_square);
        assert!((area - 100.0).abs() < 1e-10);

        let cw_square = vec![(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0)];
        let area = signed_area_robust(&cw_square);
        assert!((area + 100.0).abs() < 1e-10);
    }

    #[test]
    fn test_scaling_config() {
        let config = ScalingConfig::new(3);

        let p = (1.234, 5.678);
        let scaled = config.scale_point(p);
        assert_eq!(scaled, (1234.0, 5678.0));

        let unscaled = config.unscale_point(scaled);
        assert!((unscaled.0 - p.0).abs() < 1e-10);
        assert!((unscaled.1 - p.1).abs() < 1e-10);
    }

    #[test]
    fn test_snap_to_grid() {
        let p = (1.23, 4.56);

        let snapped = snap_to_grid(p, 0.5);
        assert_eq!(snapped, (1.0, 4.5));

        let snapped = snap_to_grid(p, 1.0);
        assert_eq!(snapped, (1.0, 5.0));
    }

    #[test]
    fn test_orientation_methods() {
        assert!(Orientation::CounterClockwise.is_ccw());
        assert!(!Orientation::CounterClockwise.is_cw());
        assert!(!Orientation::CounterClockwise.is_collinear());

        assert!(!Orientation::Clockwise.is_ccw());
        assert!(Orientation::Clockwise.is_cw());
        assert!(!Orientation::Clockwise.is_collinear());

        assert!(!Orientation::Collinear.is_ccw());
        assert!(!Orientation::Collinear.is_cw());
        assert!(Orientation::Collinear.is_collinear());
    }

    #[test]
    fn test_degenerate_triangle() {
        let a = (0.0, 0.0);
        let b = (5.0, 0.0);
        let c = (10.0, 0.0);

        assert!(!point_in_triangle_robust((5.0, 0.0), a, b, c));
    }

    #[test]
    fn test_extreme_coordinates() {
        let a = (1e10, 1e10);
        let b = (1e10 + 1.0, 1e10);
        let c = (1e10 + 0.5, 1e10 + 1.0);

        assert_eq!(orient2d(a, b, c), Orientation::CounterClockwise);

        let a = (1e-10, 1e-10);
        let b = (1e-10 + 1e-12, 1e-10);
        let c = (1e-10 + 5e-13, 1e-10 + 1e-12);

        let result = orient2d(a, b, c);
        assert!(
            result == Orientation::CounterClockwise || result == Orientation::Collinear,
            "Unexpected orientation: {:?}",
            result
        );
    }
}