oxigdal-algorithms 0.1.4

High-performance SIMD-optimized raster and vector algorithms for OxiGDAL - Pure Rust geospatial processing
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Split operations for dividing geometric features
//!
//! This module implements splitting operations that divide geometries using
//! other geometries as cutting tools.

use crate::error::{AlgorithmError, Result};
use oxigdal_core::vector::{Coordinate, LineString, Point, Polygon};

/// Options for split operations
#[derive(Debug, Clone)]
pub struct SplitOptions {
    /// Tolerance for coordinate comparison
    pub tolerance: f64,
    /// Whether to snap split points to grid
    pub snap_to_grid: bool,
    /// Grid size for snapping (if enabled)
    pub grid_size: f64,
    /// Minimum length for resulting line segments
    pub min_segment_length: f64,
    /// Whether to preserve all split parts (even very small ones)
    pub preserve_all: bool,
}

impl Default for SplitOptions {
    fn default() -> Self {
        Self {
            tolerance: 1e-10,
            snap_to_grid: false,
            grid_size: 1e-6,
            min_segment_length: 0.0,
            preserve_all: true,
        }
    }
}

/// Result of a split operation
#[derive(Debug, Clone)]
pub struct SplitResult {
    /// The resulting geometries from the split
    pub geometries: Vec<SplitGeometry>,
    /// Number of split points used
    pub num_splits: usize,
    /// Whether all splits were successful
    pub complete: bool,
}

/// A geometry resulting from a split operation
#[derive(Debug, Clone)]
pub enum SplitGeometry {
    /// A linestring segment
    LineString(LineString),
    /// A polygon
    Polygon(Polygon),
}

/// Split a linestring by point locations
///
/// Divides a linestring into multiple segments at specified points.
///
/// # Arguments
///
/// * `linestring` - The linestring to split
/// * `split_points` - Points where the linestring should be split
/// * `options` - Split options
///
/// # Returns
///
/// Result containing the split result with multiple linestring segments
///
/// # Examples
///
/// ```
/// use oxigdal_algorithms::vector::topology::{split_linestring_by_points, SplitOptions};
/// use oxigdal_algorithms::{Coordinate, LineString, Point};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let coords = vec![
///     Coordinate::new_2d(0.0, 0.0),
///     Coordinate::new_2d(10.0, 0.0),
/// ];
/// let linestring = LineString::new(coords)?;
///
/// let split_points = vec![
///     Point::new(5.0, 0.0),
/// ];
///
/// let result = split_linestring_by_points(&linestring, &split_points, &SplitOptions::default())?;
/// assert_eq!(result.num_splits, 1);
/// assert!(result.geometries.len() >= 2);
/// # Ok(())
/// # }
/// ```
pub fn split_linestring_by_points(
    linestring: &LineString,
    split_points: &[Point],
    options: &SplitOptions,
) -> Result<SplitResult> {
    if split_points.is_empty() {
        return Ok(SplitResult {
            geometries: vec![SplitGeometry::LineString(linestring.clone())],
            num_splits: 0,
            complete: true,
        });
    }

    let coords = &linestring.coords;
    if coords.len() < 2 {
        return Err(AlgorithmError::InvalidGeometry(
            "Linestring must have at least 2 coordinates".to_string(),
        ));
    }

    // Find all split locations along the linestring
    let mut split_locations = Vec::new();

    for point in split_points {
        if let Some(location) = find_point_on_linestring(linestring, point, options.tolerance)? {
            split_locations.push(location);
        }
    }

    // Sort split locations by segment index and parameter
    // Validate for NaN values before sorting
    for loc in &split_locations {
        if loc.parameter.is_nan() {
            return Err(AlgorithmError::InvalidGeometry(
                "Split location parameter contains NaN value".to_string(),
            ));
        }
    }

    split_locations.sort_by(|a, b| {
        a.segment_index.cmp(&b.segment_index).then(
            a.parameter
                .partial_cmp(&b.parameter)
                .unwrap_or(std::cmp::Ordering::Equal),
        )
    });

    // Remove duplicates
    split_locations.dedup_by(|a, b| {
        a.segment_index == b.segment_index && (a.parameter - b.parameter).abs() < options.tolerance
    });

    if split_locations.is_empty() {
        return Ok(SplitResult {
            geometries: vec![SplitGeometry::LineString(linestring.clone())],
            num_splits: 0,
            complete: false,
        });
    }

    // Build split segments
    let mut result_geometries = Vec::new();
    let mut current_coords = Vec::new();
    let mut current_segment_idx = 0;
    let mut split_idx = 0;

    current_coords.push(coords[0]);

    for i in 0..coords.len().saturating_sub(1) {
        // Add splits on this segment
        while split_idx < split_locations.len() && split_locations[split_idx].segment_index == i {
            let split_loc = &split_locations[split_idx];
            let split_coord = split_loc.coordinate;

            // Add split point
            current_coords.push(split_coord);

            // Create a new linestring if we have enough points
            if current_coords.len() >= 2 {
                if let Ok(ls) = LineString::new(current_coords.clone()) {
                    if options.preserve_all
                        || compute_linestring_length(&ls) >= options.min_segment_length
                    {
                        result_geometries.push(SplitGeometry::LineString(ls));
                    }
                }
            }

            // Start new segment
            current_coords.clear();
            current_coords.push(split_coord);

            split_idx += 1;
        }

        // Add end point of current segment
        current_coords.push(coords[i + 1]);
        current_segment_idx = i + 1;
    }

    // Add final segment
    if current_coords.len() >= 2 {
        if let Ok(ls) = LineString::new(current_coords) {
            if options.preserve_all || compute_linestring_length(&ls) >= options.min_segment_length
            {
                result_geometries.push(SplitGeometry::LineString(ls));
            }
        }
    }

    Ok(SplitResult {
        geometries: result_geometries,
        num_splits: split_locations.len(),
        complete: true,
    })
}

/// Location of a point on a linestring
#[derive(Debug, Clone)]
struct PointLocation {
    /// Index of the segment (0-based)
    segment_index: usize,
    /// Parameter along the segment (0.0 to 1.0)
    parameter: f64,
    /// The coordinate at this location
    coordinate: Coordinate,
}

/// Find a point on a linestring
fn find_point_on_linestring(
    linestring: &LineString,
    point: &Point,
    tolerance: f64,
) -> Result<Option<PointLocation>> {
    let coords = &linestring.coords;

    for i in 0..coords.len().saturating_sub(1) {
        let p1 = coords[i];
        let p2 = coords[i + 1];

        // Check if point is on this segment
        if let Some((param, coord)) = point_on_segment(point, &p1, &p2, tolerance) {
            return Ok(Some(PointLocation {
                segment_index: i,
                parameter: param,
                coordinate: coord,
            }));
        }
    }

    Ok(None)
}

/// Check if a point is on a line segment
fn point_on_segment(
    point: &Point,
    p1: &Coordinate,
    p2: &Coordinate,
    tolerance: f64,
) -> Option<(f64, Coordinate)> {
    let px = point.coord.x;
    let py = point.coord.y;

    // Vector from p1 to p2
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;

    // Length squared of segment
    let len_sq = dx * dx + dy * dy;

    if len_sq < tolerance * tolerance {
        // Degenerate segment
        return None;
    }

    // Parameter t along the segment
    let t = ((px - p1.x) * dx + (py - p1.y) * dy) / len_sq;

    // Check if point projects onto the segment (not before or after)
    if t < -tolerance || t > 1.0 + tolerance {
        return None;
    }

    // Clamp t to [0, 1]
    let t = t.clamp(0.0, 1.0);

    // Compute closest point on segment
    let closest_x = p1.x + t * dx;
    let closest_y = p1.y + t * dy;

    // Check distance from point to closest point
    let dist_sq = (px - closest_x).powi(2) + (py - closest_y).powi(2);

    if dist_sq < tolerance * tolerance {
        Some((t, Coordinate::new_2d(closest_x, closest_y)))
    } else {
        None
    }
}

/// Compute the length of a linestring
fn compute_linestring_length(linestring: &LineString) -> f64 {
    let coords = &linestring.coords;
    let mut length = 0.0;

    for i in 0..coords.len().saturating_sub(1) {
        let dx = coords[i + 1].x - coords[i].x;
        let dy = coords[i + 1].y - coords[i].y;
        length += (dx * dx + dy * dy).sqrt();
    }

    length
}

/// Split a polygon by a linestring
///
/// Divides a polygon into multiple parts using a linestring as a cutting tool.
///
/// # Arguments
///
/// * `polygon` - The polygon to split
/// * `split_line` - The linestring to use for splitting
/// * `options` - Split options
///
/// # Returns
///
/// Result containing the split polygons
///
/// # Examples
///
/// ```no_run
/// use oxigdal_algorithms::vector::topology::{split_polygon_by_line, SplitOptions};
/// use oxigdal_algorithms::{Coordinate, LineString, Polygon};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let coords = vec![
///     Coordinate::new_2d(0.0, 0.0),
///     Coordinate::new_2d(10.0, 0.0),
///     Coordinate::new_2d(10.0, 10.0),
///     Coordinate::new_2d(0.0, 10.0),
///     Coordinate::new_2d(0.0, 0.0),
/// ];
/// let exterior = LineString::new(coords)?;
/// let polygon = Polygon::new(exterior, vec![])?;
///
/// let split_coords = vec![
///     Coordinate::new_2d(0.0, 5.0),
///     Coordinate::new_2d(10.0, 5.0),
/// ];
/// let split_line = LineString::new(split_coords)?;
///
/// let result = split_polygon_by_line(&polygon, &split_line, &SplitOptions::default())?;
/// assert!(result.geometries.len() >= 1);
/// # Ok(())
/// # }
/// ```
pub fn split_polygon_by_line(
    polygon: &Polygon,
    split_line: &LineString,
    options: &SplitOptions,
) -> Result<SplitResult> {
    // Find intersection points between polygon boundary and split line
    let intersection_points = find_polygon_line_intersections(polygon, split_line, options)?;

    if intersection_points.len() < 2 {
        // Not enough intersections to split
        return Ok(SplitResult {
            geometries: vec![SplitGeometry::Polygon(polygon.clone())],
            num_splits: 0,
            complete: false,
        });
    }

    // Create polygons from split
    let result_polygons =
        create_split_polygons(polygon, split_line, &intersection_points, options)?;

    let geometries = result_polygons
        .into_iter()
        .map(SplitGeometry::Polygon)
        .collect();

    Ok(SplitResult {
        geometries,
        num_splits: intersection_points.len(),
        complete: true,
    })
}

/// Find intersection points between polygon boundary and a linestring
fn find_polygon_line_intersections(
    polygon: &Polygon,
    line: &LineString,
    options: &SplitOptions,
) -> Result<Vec<Coordinate>> {
    let mut intersections = Vec::new();

    // Check exterior ring
    let exterior_intersections = find_linestring_intersections(&polygon.exterior, line, options)?;
    intersections.extend(exterior_intersections);

    // Check interior rings (holes)
    for interior in &polygon.interiors {
        let interior_intersections = find_linestring_intersections(interior, line, options)?;
        intersections.extend(interior_intersections);
    }

    // Remove duplicates
    // Validate for NaN values before sorting
    for coord in &intersections {
        if coord.x.is_nan() || coord.y.is_nan() {
            return Err(AlgorithmError::InvalidGeometry(
                "Intersection coordinate contains NaN value".to_string(),
            ));
        }
    }

    intersections.sort_by(|a, b| {
        a.x.partial_cmp(&b.x)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then(a.y.partial_cmp(&b.y).unwrap_or(std::cmp::Ordering::Equal))
    });

    intersections.dedup_by(|a, b| {
        (a.x - b.x).abs() < options.tolerance && (a.y - b.y).abs() < options.tolerance
    });

    Ok(intersections)
}

/// Find intersections between two linestrings
fn find_linestring_intersections(
    line1: &LineString,
    line2: &LineString,
    options: &SplitOptions,
) -> Result<Vec<Coordinate>> {
    let mut intersections = Vec::new();
    let coords1 = &line1.coords;
    let coords2 = &line2.coords;

    for i in 0..coords1.len().saturating_sub(1) {
        for j in 0..coords2.len().saturating_sub(1) {
            if let Some(intersection) = compute_segment_intersection(
                &coords1[i],
                &coords1[i + 1],
                &coords2[j],
                &coords2[j + 1],
                options.tolerance,
            ) {
                intersections.push(intersection);
            }
        }
    }

    Ok(intersections)
}

/// Compute intersection point between two line segments
fn compute_segment_intersection(
    p1: &Coordinate,
    p2: &Coordinate,
    p3: &Coordinate,
    p4: &Coordinate,
    tolerance: f64,
) -> Option<Coordinate> {
    let d = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);

    if d.abs() < tolerance {
        // Parallel or coincident
        return None;
    }

    let t = ((p1.x - p3.x) * (p3.y - p4.y) - (p1.y - p3.y) * (p3.x - p4.x)) / d;
    let u = -((p1.x - p2.x) * (p1.y - p3.y) - (p1.y - p2.y) * (p1.x - p3.x)) / d;

    if (0.0..=1.0).contains(&t) && (0.0..=1.0).contains(&u) {
        let x = p1.x + t * (p2.x - p1.x);
        let y = p1.y + t * (p2.y - p1.y);
        Some(Coordinate::new_2d(x, y))
    } else {
        None
    }
}

/// Create split polygons from intersection points
fn create_split_polygons(
    _polygon: &Polygon,
    _split_line: &LineString,
    _intersections: &[Coordinate],
    _options: &SplitOptions,
) -> Result<Vec<Polygon>> {
    // This is a simplified implementation
    // A full implementation would need to:
    // 1. Build a planar graph from the polygon and split line
    // 2. Find faces in the graph
    // 3. Construct polygons from faces

    // For now, return empty result as a placeholder
    Ok(Vec::new())
}

/// Split a polygon by another polygon
///
/// Divides a polygon using another polygon's boundary as cutting edges.
pub fn split_polygon_by_polygon(
    target_poly: &Polygon,
    split_poly: &Polygon,
    options: &SplitOptions,
) -> Result<SplitResult> {
    // Use the exterior ring of the split polygon as the splitting linestring
    let split_line = &split_poly.exterior.clone();

    split_polygon_by_line(target_poly, split_line, options)
}

/// Batch split operation for multiple polygons
pub fn split_polygons_batch(
    polygons: &[Polygon],
    split_line: &LineString,
    options: &SplitOptions,
) -> Result<Vec<SplitResult>> {
    polygons
        .iter()
        .map(|poly| split_polygon_by_line(poly, split_line, options))
        .collect()
}

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

    fn create_linestring(coords: Vec<(f64, f64)>) -> LineString {
        let coords: Vec<Coordinate> = coords
            .iter()
            .map(|(x, y)| Coordinate::new_2d(*x, *y))
            .collect();
        LineString::new(coords).expect("Failed to create linestring")
    }

    fn create_polygon(coords: Vec<(f64, f64)>) -> Polygon {
        let coords: Vec<Coordinate> = coords
            .iter()
            .map(|(x, y)| Coordinate::new_2d(*x, *y))
            .collect();
        let exterior = LineString::new(coords).expect("Failed to create linestring");
        Polygon::new(exterior, vec![]).expect("Failed to create polygon")
    }

    #[test]
    fn test_split_linestring_single_point() {
        let linestring = create_linestring(vec![(0.0, 0.0), (10.0, 0.0)]);
        let split_points = vec![Point::new(5.0, 0.0)];

        let result =
            split_linestring_by_points(&linestring, &split_points, &SplitOptions::default());
        assert!(result.is_ok());

        let split_result = result.expect("Split failed");
        assert_eq!(split_result.num_splits, 1);
        assert!(split_result.geometries.len() >= 2);
    }

    #[test]
    fn test_split_linestring_multiple_points() {
        let linestring = create_linestring(vec![(0.0, 0.0), (10.0, 0.0)]);
        let split_points = vec![Point::new(3.0, 0.0), Point::new(7.0, 0.0)];

        let result =
            split_linestring_by_points(&linestring, &split_points, &SplitOptions::default());
        assert!(result.is_ok());

        let split_result = result.expect("Split failed");
        assert_eq!(split_result.num_splits, 2);
    }

    #[test]
    fn test_split_linestring_no_intersection() {
        let linestring = create_linestring(vec![(0.0, 0.0), (10.0, 0.0)]);
        let split_points = vec![Point::new(5.0, 5.0)]; // Not on line

        let result =
            split_linestring_by_points(&linestring, &split_points, &SplitOptions::default());
        assert!(result.is_ok());

        let split_result = result.expect("Split failed");
        assert_eq!(split_result.num_splits, 0);
        assert_eq!(split_result.geometries.len(), 1); // Unchanged
    }

    #[test]
    fn test_split_linestring_empty_splits() {
        let linestring = create_linestring(vec![(0.0, 0.0), (10.0, 0.0)]);
        let split_points = vec![];

        let result =
            split_linestring_by_points(&linestring, &split_points, &SplitOptions::default());
        assert!(result.is_ok());

        let split_result = result.expect("Split failed");
        assert_eq!(split_result.num_splits, 0);
        assert_eq!(split_result.geometries.len(), 1);
    }

    #[test]
    fn test_point_on_segment() {
        let p1 = Coordinate::new_2d(0.0, 0.0);
        let p2 = Coordinate::new_2d(10.0, 0.0);
        let point = Point::new(5.0, 0.0);

        let result = point_on_segment(&point, &p1, &p2, 1e-10);
        assert!(result.is_some());

        if let Some((param, coord)) = result {
            assert!((param - 0.5).abs() < 1e-10);
            assert!((coord.x - 5.0).abs() < 1e-10);
            assert!((coord.y - 0.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_point_not_on_segment() {
        let p1 = Coordinate::new_2d(0.0, 0.0);
        let p2 = Coordinate::new_2d(10.0, 0.0);
        let point = Point::new(5.0, 5.0); // Off the line

        let result = point_on_segment(&point, &p1, &p2, 1e-10);
        assert!(result.is_none());
    }

    #[test]
    fn test_compute_segment_intersection() {
        let p1 = Coordinate::new_2d(0.0, 0.0);
        let p2 = Coordinate::new_2d(10.0, 10.0);
        let p3 = Coordinate::new_2d(0.0, 10.0);
        let p4 = Coordinate::new_2d(10.0, 0.0);

        let result = compute_segment_intersection(&p1, &p2, &p3, &p4, 1e-10);
        assert!(result.is_some());

        if let Some(intersection) = result {
            // Intersection should be at (5, 5)
            assert!((intersection.x - 5.0).abs() < 1e-6);
            assert!((intersection.y - 5.0).abs() < 1e-6);
        }
    }

    #[test]
    fn test_split_polygon_by_line() {
        let polygon = create_polygon(vec![
            (0.0, 0.0),
            (10.0, 0.0),
            (10.0, 10.0),
            (0.0, 10.0),
            (0.0, 0.0),
        ]);

        let split_line = create_linestring(vec![(0.0, 5.0), (10.0, 5.0)]);

        let result = split_polygon_by_line(&polygon, &split_line, &SplitOptions::default());
        assert!(result.is_ok());

        let split_result = result.expect("Split failed");
        assert_eq!(split_result.num_splits, 2); // Two intersection points
    }

    #[test]
    fn test_compute_linestring_length() {
        let linestring = create_linestring(vec![(0.0, 0.0), (3.0, 0.0), (3.0, 4.0)]);
        let length = compute_linestring_length(&linestring);

        // Length is 3 + 4 = 7
        assert!((length - 7.0).abs() < 1e-6);
    }
}