threecrate-reconstruction 0.8.0

Surface reconstruction algorithms for threecrate
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! Enhanced Ball Pivoting Algorithm for surface reconstruction
//!
//! This module provides a comprehensive ball pivoting implementation with multi-scale
//! capabilities, adaptive radius selection, and improved quality metrics.

use crate::parallel;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use threecrate_core::{Error, NormalPoint3f, Point3f, PointCloud, Result, TriangleMesh};

/// Configuration for Enhanced Ball Pivoting Algorithm
#[derive(Debug, Clone)]
pub struct BallPivotingConfig {
    /// Primary ball radius for the pivoting algorithm
    pub radius: f32,
    /// Additional radii for multi-scale reconstruction
    pub additional_radii: Vec<f32>,
    /// Whether to use adaptive radius selection
    pub adaptive_radius: bool,
    /// Number of density percentiles to sample for adaptive radius
    pub adaptive_percentiles: Vec<f32>,
    /// Clustering threshold for points
    pub clustering: f32,
    /// Angle threshold for normal consistency (cosine)
    pub normal_threshold: f32,
    /// Whether to estimate normals if not present
    pub estimate_normals: bool,
    /// Quality threshold for triangle acceptance (0.0 to 1.0)
    pub quality_threshold: f32,
    /// Maximum edge length relative to local density
    pub max_edge_factor: f32,
    /// Minimum triangle area threshold
    pub min_triangle_area: f32,
    /// Enable aggressive hole filling
    pub fill_holes: bool,
    /// Maximum number of pivoting iterations per edge
    pub max_pivoting_iterations: usize,
    /// Enable spatial acceleration
    pub use_spatial_index: bool,
    /// Spatial grid cell size factor (relative to radius)
    pub spatial_cell_factor: f32,
}

/// Adaptive radius selection strategy
#[derive(Debug, Clone, PartialEq)]
pub enum AdaptiveStrategy {
    /// Use local point density percentiles
    DensityPercentile,
    /// Use k-nearest neighbor distances
    KNearestNeighbor,
    /// Use covariance-based analysis
    CovarianceBased,
    /// Combine multiple strategies
    Hybrid,
}

impl Default for BallPivotingConfig {
    fn default() -> Self {
        Self {
            radius: 0.1,
            additional_radii: vec![0.05, 0.15, 0.2],
            adaptive_radius: true,
            adaptive_percentiles: vec![0.25, 0.5, 0.75, 0.9],
            clustering: 0.05,
            normal_threshold: 0.866, // cos(30 degrees)
            estimate_normals: true,
            quality_threshold: 0.1,
            max_edge_factor: 3.0,
            min_triangle_area: 1e-8,
            fill_holes: true,
            max_pivoting_iterations: 20,
            use_spatial_index: true,
            spatial_cell_factor: 2.0,
        }
    }
}

/// Quality metrics for triangle evaluation
#[derive(Debug, Clone)]
pub struct TriangleQuality {
    /// Aspect ratio (higher is better, 1.0 is equilateral)
    pub aspect_ratio: f32,
    /// Minimum angle in radians
    pub min_angle: f32,
    /// Maximum angle in radians
    pub max_angle: f32,
    /// Area of the triangle
    pub area: f32,
    /// Edge length ratio (max/min)
    pub edge_ratio: f32,
    /// Overall quality score (0.0 to 1.0)
    pub quality_score: f32,
}

/// Spatial acceleration structure
#[derive(Debug)]
struct SpatialGrid {
    cells: HashMap<(i32, i32, i32), Vec<usize>>,
    cell_size: f32,
    bounds_min: Point3f,
}

/// Edge for ball pivoting frontier
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct PivotEdge {
    v1: usize,
    v2: usize,
    opposite_vertex: Option<usize>, // For boundary edges
}

/// Priority queue item for adaptive processing
#[derive(Debug, Clone)]
struct ProcessingItem {
    edge: PivotEdge,
    priority: OrderedFloat,
    #[allow(dead_code)]
    radius: f32,
}

impl PartialEq for ProcessingItem {
    fn eq(&self, other: &Self) -> bool {
        self.priority == other.priority
    }
}

impl Eq for ProcessingItem {}

impl PartialOrd for ProcessingItem {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ProcessingItem {
    fn cmp(&self, other: &Self) -> Ordering {
        self.priority.cmp(&other.priority)
    }
}

/// Wrapper for f32 to enable ordering in BinaryHeap
#[derive(Debug, Clone, Copy, PartialEq)]
struct OrderedFloat(f32);

impl Eq for OrderedFloat {}

impl PartialOrd for OrderedFloat {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for OrderedFloat {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

/// Enhanced Ball Pivoting Algorithm implementation
pub struct BallPivotingReconstructor {
    config: BallPivotingConfig,
    spatial_grid: Option<SpatialGrid>,
    adaptive_radii: Vec<f32>,
}

impl BallPivotingReconstructor {
    /// Create a new Ball Pivoting reconstructor
    pub fn new(config: BallPivotingConfig) -> Self {
        Self {
            config,
            spatial_grid: None,
            adaptive_radii: Vec::new(),
        }
    }

    /// Build spatial acceleration structure
    fn build_spatial_grid(&mut self, points: &[Point3f], cell_size: f32) {
        let mut grid = SpatialGrid {
            cells: HashMap::new(),
            cell_size,
            bounds_min: Point3f::new(f32::INFINITY, f32::INFINITY, f32::INFINITY),
        };

        // Find bounds
        let mut bounds_max = Point3f::new(f32::NEG_INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY);
        for point in points {
            grid.bounds_min.x = grid.bounds_min.x.min(point.x);
            grid.bounds_min.y = grid.bounds_min.y.min(point.y);
            grid.bounds_min.z = grid.bounds_min.z.min(point.z);
            bounds_max.x = bounds_max.x.max(point.x);
            bounds_max.y = bounds_max.y.max(point.y);
            bounds_max.z = bounds_max.z.max(point.z);
        }

        // Insert points into grid cells
        for (i, point) in points.iter().enumerate() {
            let cell_x = ((point.x - grid.bounds_min.x) / cell_size) as i32;
            let cell_y = ((point.y - grid.bounds_min.y) / cell_size) as i32;
            let cell_z = ((point.z - grid.bounds_min.z) / cell_size) as i32;

            grid.cells
                .entry((cell_x, cell_y, cell_z))
                .or_insert_with(Vec::new)
                .push(i);
        }

        self.spatial_grid = Some(grid);
    }

    /// Find neighbors using spatial grid
    fn find_neighbors(&self, point: &Point3f, radius: f32) -> Vec<usize> {
        let mut neighbors = Vec::new();

        if let Some(grid) = &self.spatial_grid {
            let search_range = (radius / grid.cell_size).ceil() as i32;
            let center_x = ((point.x - grid.bounds_min.x) / grid.cell_size) as i32;
            let center_y = ((point.y - grid.bounds_min.y) / grid.cell_size) as i32;
            let center_z = ((point.z - grid.bounds_min.z) / grid.cell_size) as i32;

            for dx in -search_range..=search_range {
                for dy in -search_range..=search_range {
                    for dz in -search_range..=search_range {
                        let cell_key = (center_x + dx, center_y + dy, center_z + dz);
                        if let Some(indices) = grid.cells.get(&cell_key) {
                            neighbors.extend(indices);
                        }
                    }
                }
            }
        }

        neighbors
    }

    /// Compute adaptive radii based on local density
    fn compute_adaptive_radii(&mut self, points: &[Point3f]) -> Result<()> {
        if !self.config.adaptive_radius {
            return Ok(());
        }

        let mut local_densities = Vec::new();
        let sample_size = points.len().min(1000);
        let step = (points.len().max(1) / sample_size.max(1)).max(1);

        for i in (0..points.len()).step_by(step) {
            let point = &points[i];
            let neighbors = if self.spatial_grid.is_some() {
                self.find_neighbors(point, self.config.radius * 3.0)
            } else {
                (0..points.len()).collect()
            };

            // Compute k-nearest neighbor distance
            let mut distances: Vec<f32> = neighbors
                .iter()
                .filter(|&&j| j != i)
                .map(|&j| (point - &points[j]).magnitude())
                .collect();
            distances.sort_by(|a, b| a.partial_cmp(b).unwrap());

            let k = 10.min(distances.len());
            if k > 0 {
                let avg_k_dist = distances.iter().take(k).sum::<f32>() / k as f32;
                local_densities.push(avg_k_dist);
            }
        }

        if local_densities.is_empty() {
            return Ok(());
        }

        // Sort densities to compute percentiles
        local_densities.sort_by(|a, b| a.partial_cmp(b).unwrap());

        // Generate adaptive radii based on percentiles
        self.adaptive_radii.clear();
        for &percentile in &self.config.adaptive_percentiles {
            let index = ((local_densities.len() - 1) as f32 * percentile) as usize;
            let radius = local_densities[index] * 2.0; // Scale factor
            self.adaptive_radii.push(radius);
        }

        // Add configured radii
        self.adaptive_radii.push(self.config.radius);
        self.adaptive_radii.extend(&self.config.additional_radii);

        // Remove duplicates and sort
        self.adaptive_radii
            .sort_by(|a, b| a.partial_cmp(b).unwrap());
        self.adaptive_radii.dedup_by(|a, b| (*a - *b).abs() < 1e-6);

        Ok(())
    }

    /// Compute triangle quality metrics
    fn compute_triangle_quality(
        &self,
        p1: &Point3f,
        p2: &Point3f,
        p3: &Point3f,
    ) -> TriangleQuality {
        let v1 = p2 - p1;
        let v2 = p3 - p2;
        let v3 = p1 - p3;

        let a = v1.magnitude();
        let b = v2.magnitude();
        let c = v3.magnitude();

        // Area
        let cross = v1.cross(&(p3 - p1));
        let area = cross.magnitude() * 0.5;

        if area < 1e-10 {
            return TriangleQuality {
                aspect_ratio: 0.0,
                min_angle: 0.0,
                max_angle: std::f32::consts::PI,
                area,
                edge_ratio: f32::INFINITY,
                quality_score: 0.0,
            };
        }

        // Angles using law of cosines
        let angle_a = ((b * b + c * c - a * a) / (2.0 * b * c))
            .clamp(-1.0, 1.0)
            .acos();
        let angle_b = ((a * a + c * c - b * b) / (2.0 * a * c))
            .clamp(-1.0, 1.0)
            .acos();
        let angle_c = std::f32::consts::PI - angle_a - angle_b;

        let min_angle = angle_a.min(angle_b).min(angle_c);
        let max_angle = angle_a.max(angle_b).max(angle_c);

        // Aspect ratio (ratio of inradius to circumradius)
        let circumradius = Self::compute_circumradius(p1, p2, p3);
        let inradius = area / ((a + b + c) * 0.5);
        let aspect_ratio = if circumradius > 1e-10 {
            inradius / circumradius
        } else {
            0.0
        };

        // Edge ratio
        let min_edge = a.min(b).min(c);
        let max_edge = a.max(b).max(c);
        let edge_ratio = if min_edge > 1e-10 {
            max_edge / min_edge
        } else {
            f32::INFINITY
        };

        // Overall quality score (0 to 1, higher is better)
        let angle_quality = (min_angle / (std::f32::consts::PI / 6.0)).min(1.0); // Normalize to 30 degrees
        let aspect_quality = aspect_ratio * 2.0; // Equilateral triangle has ratio ~0.5
        let edge_quality = (3.0 / edge_ratio).min(1.0); // Equilateral has ratio 1.0

        let quality_score = (angle_quality + aspect_quality + edge_quality) / 3.0;

        TriangleQuality {
            aspect_ratio,
            min_angle,
            max_angle,
            area,
            edge_ratio,
            quality_score,
        }
    }

    /// Check if triangle passes quality thresholds
    fn is_triangle_acceptable(&self, quality: &TriangleQuality, radius: f32) -> bool {
        if quality.area < self.config.min_triangle_area {
            return false;
        }

        if quality.quality_score < self.config.quality_threshold {
            return false;
        }

        // Check edge length constraint
        let max_edge_length = radius * self.config.max_edge_factor;
        if quality.edge_ratio * quality.area.sqrt() > max_edge_length {
            return false;
        }

        true
    }

    /// Advanced ball pivoting with multi-scale and adaptive features
    fn advanced_ball_pivoting(
        &mut self,
        points: &[Point3f],
        normals: &[Point3f],
    ) -> Result<Vec<[usize; 3]>> {
        let mut all_triangles = Vec::new();
        let mut used_points = HashSet::new();

        // Process each radius scale
        for &radius in &self.adaptive_radii {
            let mut triangles = Vec::new();
            let mut frontier = BinaryHeap::new();
            let mut processed_edges = HashSet::new();

            // Find seed triangle for this radius
            if let Some(seed) = self.find_seed_triangle(points, normals, radius, &used_points) {
                triangles.push(seed);
                used_points.insert(seed[0]);
                used_points.insert(seed[1]);
                used_points.insert(seed[2]);

                // Add edges to frontier
                self.add_triangle_edges_to_frontier(
                    &mut frontier,
                    &seed,
                    radius,
                    &mut processed_edges,
                );

                // Process frontier
                while let Some(item) = frontier.pop() {
                    if triangles.len() > 50000 {
                        break;
                    } // Prevent excessive triangulation

                    let edge = &item.edge;
                    if processed_edges.contains(&(edge.v1, edge.v2))
                        || processed_edges.contains(&(edge.v2, edge.v1))
                    {
                        continue;
                    }

                    if let Some(triangle) =
                        self.try_ball_pivot(points, normals, edge, radius, &used_points)
                    {
                        let quality = self.compute_triangle_quality(
                            &points[triangle[0]],
                            &points[triangle[1]],
                            &points[triangle[2]],
                        );

                        if self.is_triangle_acceptable(&quality, radius) {
                            triangles.push(triangle);
                            used_points.insert(triangle[2]);

                            // Add new edges to frontier
                            self.add_triangle_edges_to_frontier(
                                &mut frontier,
                                &triangle,
                                radius,
                                &mut processed_edges,
                            );
                        }
                    }

                    processed_edges.insert((edge.v1, edge.v2));
                }
            }

            all_triangles.extend(triangles);
        }

        Ok(all_triangles)
    }

    /// Find a good seed triangle
    fn find_seed_triangle(
        &self,
        points: &[Point3f],
        _normals: &[Point3f],
        radius: f32,
        used_points: &HashSet<usize>,
    ) -> Option<[usize; 3]> {
        let max_attempts = 1000;
        let mut attempts = 0;

        for i in 0..points.len() {
            if used_points.contains(&i) {
                continue;
            }
            if attempts >= max_attempts {
                break;
            }
            attempts += 1;

            let neighbors = if self.spatial_grid.is_some() {
                self.find_neighbors(&points[i], radius * 2.0)
            } else {
                (0..points.len()).collect()
            };

            for &j in &neighbors {
                if i == j || used_points.contains(&j) {
                    continue;
                }
                if (points[i] - points[j]).magnitude() > radius * 2.0 {
                    continue;
                }

                for &k in &neighbors {
                    if i == k || j == k || used_points.contains(&k) {
                        continue;
                    }
                    if (points[i] - points[k]).magnitude() > radius * 2.0 {
                        continue;
                    }
                    if (points[j] - points[k]).magnitude() > radius * 2.0 {
                        continue;
                    }

                    let quality = self.compute_triangle_quality(&points[i], &points[j], &points[k]);
                    if self.is_triangle_acceptable(&quality, radius) {
                        return Some([i, j, k]);
                    }
                }
            }
        }

        None
    }

    /// Add triangle edges to the frontier
    fn add_triangle_edges_to_frontier(
        &self,
        frontier: &mut BinaryHeap<ProcessingItem>,
        triangle: &[usize; 3],
        radius: f32,
        processed: &mut HashSet<(usize, usize)>,
    ) {
        let edges = [
            (triangle[0], triangle[1]),
            (triangle[1], triangle[2]),
            (triangle[2], triangle[0]),
        ];

        for &(v1, v2) in &edges {
            if !processed.contains(&(v1, v2)) && !processed.contains(&(v2, v1)) {
                let edge = PivotEdge {
                    v1,
                    v2,
                    opposite_vertex: Some(triangle[2]),
                };
                let priority = OrderedFloat(radius); // Simple priority based on radius
                frontier.push(ProcessingItem {
                    edge,
                    priority,
                    radius,
                });
            }
        }
    }

    /// Try to pivot a ball around an edge to find a new triangle
    fn try_ball_pivot(
        &self,
        points: &[Point3f],
        _normals: &[Point3f],
        edge: &PivotEdge,
        radius: f32,
        used_points: &HashSet<usize>,
    ) -> Option<[usize; 3]> {
        let p1 = &points[edge.v1];
        let p2 = &points[edge.v2];
        let edge_vec = p2 - p1;
        let edge_length = edge_vec.magnitude();

        if edge_length > radius * 2.0 || edge_length < 1e-10 {
            return None;
        }

        let edge_mid = Point3f::from((p1.coords + p2.coords) * 0.5);
        let _edge_dir = edge_vec.normalize();

        let candidates = if self.spatial_grid.is_some() {
            self.find_neighbors(&edge_mid, radius)
        } else {
            (0..points.len()).collect()
        };

        let mut best_candidate = None;
        let mut best_quality = 0.0;

        for &candidate in &candidates {
            if candidate == edge.v1 || candidate == edge.v2 || used_points.contains(&candidate) {
                continue;
            }

            let p3 = &points[candidate];
            let dist1 = (p3 - p1).magnitude();
            let dist2 = (p3 - p2).magnitude();

            if dist1 <= radius && dist2 <= radius {
                let quality = self.compute_triangle_quality(p1, p2, p3);
                if self.is_triangle_acceptable(&quality, radius)
                    && quality.quality_score > best_quality
                {
                    best_quality = quality.quality_score;
                    best_candidate = Some([edge.v1, edge.v2, candidate]);
                }
            }
        }

        best_candidate
    }

    /// Estimate normals using parallel processing and simple method if needed
    fn estimate_simple_normals(points: &[Point3f]) -> Result<Vec<Point3f>> {
        // Use parallel normal estimation from the parallel module
        let normals = parallel::point_cloud::parallel_compute_normals(
            points,
            0.2, // Fixed radius for simplicity
            |_point, neighbors, _radius| {
                if neighbors.len() >= 3 {
                    // Compute covariance matrix for PCA
                    let mut centroid = Point3f::origin();
                    for neighbor in neighbors {
                        centroid = Point3f::from(centroid.coords + neighbor.coords);
                    }
                    centroid = Point3f::from(centroid.coords / neighbors.len() as f32);

                    let mut covariance = nalgebra::Matrix3::zeros();
                    for neighbor in neighbors {
                        let diff = neighbor - centroid;
                        covariance += diff * diff.transpose();
                    }

                    // Find smallest eigenvalue's eigenvector (normal direction)
                    let eigen = covariance.symmetric_eigen();
                    let normal_idx = eigen.eigenvalues.imin();
                    let normal = eigen.eigenvectors.column(normal_idx).clone_owned();

                    let normal_point = Point3f::new(normal.x, normal.y, normal.z);
                    let normalized = normal_point.coords.normalize();
                    Point3f::from(normalized)
                } else {
                    Point3f::new(0.0, 0.0, 1.0)
                }
            },
        );

        Ok(normals)
    }

    /// Simple triangulation using Delaunay-like approach
    fn simple_triangulate(
        points: &[Point3f],
        _normals: &[Point3f],
        radius: f32,
    ) -> Result<Vec<[usize; 3]>> {
        if points.len() < 3 {
            return Ok(Vec::new());
        }

        let mut triangles = Vec::new();
        let max_triangles = 10000; // Prevent infinite loops

        // Simple approach: for each point, try to form triangles with nearby points
        for (i, center) in points.iter().enumerate() {
            if triangles.len() >= max_triangles {
                break;
            }

            // Find nearby points
            let mut candidates = Vec::new();
            for (j, point) in points.iter().enumerate() {
                if i != j {
                    let dist = (center - point).magnitude();
                    if dist <= radius * 2.0 {
                        candidates.push(j);
                    }
                }
            }

            // Try to form triangles
            for (idx_a, &a) in candidates.iter().enumerate() {
                if triangles.len() >= max_triangles {
                    break;
                }
                for &b in candidates.iter().skip(idx_a + 1) {
                    if triangles.len() >= max_triangles {
                        break;
                    }
                    if a >= i || b >= i {
                        continue;
                    } // Avoid duplicates

                    // Check if triangle is valid
                    let p1 = &points[i];
                    let p2 = &points[a];
                    let p3 = &points[b];

                    // Compute triangle normal
                    let v1 = p2 - p1;
                    let v2 = p3 - p1;
                    let tri_normal = v1.cross(&v2);

                    if tri_normal.magnitude() > 1e-6 {
                        // Check if circumradius is reasonable
                        let circumradius = Self::compute_circumradius(p1, p2, p3);
                        if circumradius <= radius * 1.5 {
                            triangles.push([i, a, b]);
                        }
                    }
                }
            }
        }

        Ok(triangles)
    }

    /// Compute circumradius of triangle
    fn compute_circumradius(p1: &Point3f, p2: &Point3f, p3: &Point3f) -> f32 {
        let a = (p2 - p1).magnitude();
        let b = (p3 - p2).magnitude();
        let c = (p1 - p3).magnitude();

        // Area using cross product
        let v1 = p2 - p1;
        let v2 = p3 - p1;
        let area = v1.cross(&v2).magnitude() * 0.5;

        if area < 1e-10 {
            return f32::INFINITY;
        }

        // Circumradius formula
        (a * b * c) / (4.0 * area)
    }

    /// Perform Ball Pivoting reconstruction
    pub fn reconstruct(&mut self, cloud: &PointCloud<Point3f>) -> Result<TriangleMesh> {
        if cloud.is_empty() {
            return Err(Error::InvalidData("Point cloud is empty".to_string()));
        }

        // Estimate normals if configured to do so
        let normals = if self.config.estimate_normals {
            Self::estimate_simple_normals(&cloud.points)?
        } else {
            // Use default upward normals if none provided
            vec![Point3f::new(0.0, 0.0, 1.0); cloud.points.len()]
        };

        // Build spatial acceleration if enabled
        if self.config.use_spatial_index {
            let cell_size = self.config.radius * self.config.spatial_cell_factor;
            self.build_spatial_grid(&cloud.points, cell_size);
        }

        // Compute adaptive radii
        self.compute_adaptive_radii(&cloud.points)?;

        // Use advanced ball pivoting if we have adaptive radii, fallback to simple otherwise
        let faces = if !self.adaptive_radii.is_empty() {
            self.advanced_ball_pivoting(&cloud.points, &normals)?
        } else {
            Self::simple_triangulate(&cloud.points, &normals, self.config.radius)?
        };

        if faces.is_empty() {
            return Err(Error::Algorithm(
                "Ball pivoting generated no triangles".to_string(),
            ));
        }

        let mut mesh = TriangleMesh::from_vertices_and_faces(cloud.points.clone(), faces);

        // Set normals if available
        mesh.set_normals(
            normals
                .iter()
                .map(|n| nalgebra::Vector3::new(n.x, n.y, n.z))
                .collect(),
        );

        Ok(mesh)
    }

    /// Perform Ball Pivoting reconstruction with existing normals
    pub fn reconstruct_with_normals(
        &mut self,
        cloud: &PointCloud<NormalPoint3f>,
    ) -> Result<TriangleMesh> {
        if cloud.is_empty() {
            return Err(Error::InvalidData("Point cloud is empty".to_string()));
        }

        // Extract points and normals
        let points: Vec<Point3f> = cloud.points.iter().map(|p| p.position).collect();
        let normals: Vec<Point3f> = cloud
            .points
            .iter()
            .map(|p| Point3f::from(p.normal))
            .collect();

        // Build spatial acceleration if enabled
        if self.config.use_spatial_index {
            let cell_size = self.config.radius * self.config.spatial_cell_factor;
            self.build_spatial_grid(&points, cell_size);
        }

        // Compute adaptive radii
        self.compute_adaptive_radii(&points)?;

        // Use advanced ball pivoting if we have adaptive radii, fallback to simple otherwise
        let faces = if !self.adaptive_radii.is_empty() {
            self.advanced_ball_pivoting(&points, &normals)?
        } else {
            Self::simple_triangulate(&points, &normals, self.config.radius)?
        };

        if faces.is_empty() {
            return Err(Error::Algorithm(
                "Ball pivoting generated no triangles".to_string(),
            ));
        }

        let mut mesh = TriangleMesh::from_vertices_and_faces(points, faces);

        // Set normals
        mesh.set_normals(
            normals
                .iter()
                .map(|n| nalgebra::Vector3::new(n.x, n.y, n.z))
                .collect(),
        );

        Ok(mesh)
    }
}

/// Convenience function for ball pivoting reconstruction
pub fn ball_pivoting_reconstruction(
    cloud: &PointCloud<Point3f>,
    radius: f32,
) -> Result<TriangleMesh> {
    let config = BallPivotingConfig {
        radius,
        ..Default::default()
    };
    let mut reconstructor = BallPivotingReconstructor::new(config);
    reconstructor.reconstruct(cloud)
}

/// Convenience function for ball pivoting reconstruction with configuration
pub fn ball_pivoting_reconstruction_with_config(
    cloud: &PointCloud<Point3f>,
    config: &BallPivotingConfig,
) -> Result<TriangleMesh> {
    let mut reconstructor = BallPivotingReconstructor::new(config.clone());
    reconstructor.reconstruct(cloud)
}

/// Convenience function for ball pivoting reconstruction with normals
pub fn ball_pivoting_from_normals(
    cloud: &PointCloud<NormalPoint3f>,
    radius: f32,
) -> Result<TriangleMesh> {
    let config = BallPivotingConfig {
        radius,
        estimate_normals: false, // We already have normals
        ..Default::default()
    };
    let mut reconstructor = BallPivotingReconstructor::new(config);
    reconstructor.reconstruct_with_normals(cloud)
}

/// Estimate optimal ball radius based on point density
pub fn estimate_optimal_radius(cloud: &PointCloud<Point3f>, percentile: f32) -> Result<f32> {
    if cloud.is_empty() {
        return Err(Error::InvalidData("Point cloud is empty".to_string()));
    }

    if !(0.0..=1.0).contains(&percentile) {
        return Err(Error::InvalidData(
            "Percentile must be between 0.0 and 1.0".to_string(),
        ));
    }

    // Simple approach: compute distances between points
    let mut distances = Vec::new();

    // Sample points to avoid O(n²) complexity
    let sample_size = (cloud.points.len() / 10).max(100).min(1000);
    let step = (cloud.points.len().max(1) / sample_size.max(1)).max(1);

    for i in (0..cloud.points.len()).step_by(step) {
        let point = &cloud.points[i];

        // Find nearest neighbor
        let mut min_dist = f32::INFINITY;
        for (j, other_point) in cloud.points.iter().enumerate() {
            if i != j {
                let dist = (point - other_point).magnitude();
                if dist < min_dist {
                    min_dist = dist;
                }
            }
        }

        if min_dist < f32::INFINITY {
            distances.push(min_dist);
        }
    }

    if distances.is_empty() {
        return Ok(0.1); // Default fallback
    }

    // Sort and take percentile
    distances.sort_by(|a, b| a.partial_cmp(b).unwrap());
    let index = ((distances.len() - 1) as f32 * percentile) as usize;

    // Optimal radius is typically 2-3 times the average nearest neighbor distance
    Ok(distances[index] * 2.5)
}

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

    #[test]
    fn test_ball_pivoting_config_default() {
        let config = BallPivotingConfig::default();

        assert_eq!(config.radius, 0.1);
        assert_eq!(config.clustering, 0.05);
        assert_eq!(config.normal_threshold, 0.866);
        assert!(config.estimate_normals);
    }

    #[test]
    fn test_empty_cloud() {
        let config = BallPivotingConfig::default();
        let mut reconstructor = BallPivotingReconstructor::new(config);
        let cloud = PointCloud::new();

        let result = reconstructor.reconstruct(&cloud);
        assert!(result.is_err());
    }

    #[test]
    fn test_estimate_optimal_radius_empty() {
        let cloud = PointCloud::new();
        let result = estimate_optimal_radius(&cloud, 0.5);
        assert!(result.is_err());
    }

    #[test]
    fn test_estimate_optimal_radius_invalid_percentile() {
        let points = vec![Point3::new(0.0, 0.0, 0.0)];
        let cloud = PointCloud::from_points(points);

        assert!(estimate_optimal_radius(&cloud, -0.1).is_err());
        assert!(estimate_optimal_radius(&cloud, 1.1).is_err());
    }

    #[test]
    fn test_ball_pivoting_simple() {
        // Create a simple triangle in 3D
        let points = vec![
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.5, 1.0, 0.0),
            Point3::new(0.5, 0.33, 0.5),
        ];
        let cloud = PointCloud::from_points(points);

        let result = ball_pivoting_reconstruction(&cloud, 2.0);

        // Should succeed and create some triangles
        match result {
            Ok(mesh) => {
                assert!(!mesh.is_empty());
                assert!(mesh.vertex_count() <= 4);
            }
            Err(_) => {
                // Algorithm might fail on simple cases - that's ok for testing
            }
        }
    }

    #[test]
    fn test_ball_pivoting_with_normals() {
        let normal_points = vec![
            NormalPoint3f {
                position: Point3::new(0.0, 0.0, 0.0),
                normal: nalgebra::Vector3::new(0.0, 0.0, 1.0),
            },
            NormalPoint3f {
                position: Point3::new(1.0, 0.0, 0.0),
                normal: nalgebra::Vector3::new(0.0, 0.0, 1.0),
            },
            NormalPoint3f {
                position: Point3::new(0.5, 1.0, 0.0),
                normal: nalgebra::Vector3::new(0.0, 0.0, 1.0),
            },
        ];
        let cloud = PointCloud::from_points(normal_points);

        let result = ball_pivoting_from_normals(&cloud, 2.0);

        // Should succeed or fail gracefully
        match result {
            Ok(mesh) => {
                assert_eq!(mesh.vertex_count(), 3);
            }
            Err(_) => {
                // Algorithm might fail - that's acceptable for testing
            }
        }
    }

    #[test]
    fn test_circumradius_calculation() {
        let p1 = Point3::new(0.0, 0.0, 0.0);
        let p2 = Point3::new(1.0, 0.0, 0.0);
        let p3 = Point3::new(0.0, 1.0, 0.0);

        let radius = BallPivotingReconstructor::compute_circumradius(&p1, &p2, &p3);

        // For a right triangle with legs of length 1, circumradius should be ~0.707
        assert!((radius - 0.707).abs() < 0.1);
    }
}