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
//! Unified reconstruction pipeline with automatic algorithm selection
//!
//! This module provides an intelligent reconstruction pipeline that automatically
//! selects the best algorithm based on input data characteristics and user requirements.

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

/// Reconstruction algorithm types available in the pipeline
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Algorithm {
    /// Poisson surface reconstruction (requires normals)
    Poisson,
    /// Ball Pivoting algorithm (good for uniform sampling)
    BallPivoting,
    /// Delaunay triangulation (fast, works with any point distribution)
    Delaunay,
    /// Moving Least Squares (smooth surfaces, handles noise well)
    MovingLeastSquares,
    /// Marching Cubes (volumetric approach, good for closed surfaces)
    MarchingCubes,
}

/// Quality requirements for reconstruction
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum QualityLevel {
    /// Fast reconstruction with basic quality
    Fast,
    /// Balanced speed and quality
    Balanced,
    /// High quality reconstruction (slower)
    HighQuality,
    /// Maximum quality (slowest, best results)
    MaxQuality,
}

/// Use case scenarios that help algorithm selection
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UseCase {
    /// General purpose reconstruction
    General,
    /// Prototyping and quick visualization
    Prototyping,
    /// CAD/Engineering models (high precision)
    Engineering,
    /// Artistic/Organic shapes (smooth surfaces)
    Organic,
    /// Noisy sensor data (needs robust algorithms)
    NoisyData,
    /// Sparse point clouds (few points)
    Sparse,
    /// Dense point clouds (many points)
    Dense,
}

/// Data characteristics analyzed from the input point cloud
#[derive(Debug, Clone)]
pub struct DataCharacteristics {
    /// Number of points in the cloud
    pub point_count: usize,
    /// Whether normals are available
    pub has_normals: bool,
    /// Point density uniformity (0.0 = very non-uniform, 1.0 = perfectly uniform)
    pub density_uniformity: f32,
    /// Estimated noise level (0.0 = no noise, 1.0 = very noisy)
    pub noise_level: f32,
    /// Average nearest neighbor distance
    pub avg_neighbor_distance: f32,
    /// Bounding box dimensions
    pub bounding_box: (Point3f, Point3f),
    /// Whether the surface appears to be closed
    pub is_closed_surface: bool,
    /// Estimated surface complexity (0.0 = simple, 1.0 = very complex)
    pub surface_complexity: f32,
    /// Distribution of points (planar, spherical, arbitrary)
    pub distribution_type: DistributionType,
}

/// Types of point distribution patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DistributionType {
    /// Points are mostly on a plane
    Planar,
    /// Points form a spherical or ellipsoidal shape
    Spherical,
    /// Points form a cylindrical shape
    Cylindrical,
    /// Arbitrary 3D distribution
    Arbitrary,
}

/// Configuration for the unified reconstruction pipeline
#[derive(Debug, Clone)]
pub struct PipelineConfig {
    /// Desired quality level
    pub quality: QualityLevel,
    /// Use case scenario
    pub use_case: UseCase,
    /// Preferred algorithm (None for automatic selection)
    pub preferred_algorithm: Option<Algorithm>,
    /// Fallback algorithms to try if preferred fails
    pub fallback_algorithms: Vec<Algorithm>,
    /// Maximum processing time in seconds (None for unlimited)
    pub max_processing_time: Option<f32>,
    /// Enable parallel processing
    pub enable_parallel: bool,
    /// Validate output mesh quality
    pub validate_output: bool,
    /// Attempt to repair mesh if validation fails
    pub auto_repair: bool,
}

impl Default for PipelineConfig {
    fn default() -> Self {
        Self {
            quality: QualityLevel::Balanced,
            use_case: UseCase::General,
            preferred_algorithm: None,
            fallback_algorithms: vec![
                Algorithm::Delaunay,
                Algorithm::BallPivoting,
                Algorithm::MovingLeastSquares,
            ],
            max_processing_time: None,
            enable_parallel: true,
            validate_output: true,
            auto_repair: false,
        }
    }
}

/// Result of reconstruction attempt with metadata
#[derive(Debug)]
pub struct ReconstructionResult {
    /// The reconstructed mesh
    pub mesh: TriangleMesh,
    /// Algorithm that was used
    pub algorithm_used: Algorithm,
    /// Processing time in seconds
    pub processing_time: f32,
    /// Quality metrics of the result
    pub quality_metrics: QualityMetrics,
    /// Data characteristics that were analyzed
    pub data_characteristics: DataCharacteristics,
}

/// Quality metrics for evaluating reconstruction results
#[derive(Debug, Clone)]
pub struct QualityMetrics {
    /// Number of vertices in result
    pub vertex_count: usize,
    /// Number of triangles in result
    pub triangle_count: usize,
    /// Average triangle quality (0.0 = poor, 1.0 = excellent)
    pub avg_triangle_quality: f32,
    /// Mesh watertightness (0.0 = many holes, 1.0 = watertight)
    pub watertightness: f32,
    /// Surface smoothness (0.0 = rough, 1.0 = very smooth)
    pub smoothness: f32,
    /// Geometric accuracy compared to input (0.0 = poor, 1.0 = perfect)
    pub geometric_accuracy: f32,
}

/// The unified reconstruction pipeline
pub struct ReconstructionPipeline {
    config: PipelineConfig,
}

impl ReconstructionPipeline {
    /// Create a new reconstruction pipeline with configuration
    pub fn new(config: PipelineConfig) -> Self {
        Self { config }
    }

    /// Create a pipeline with default configuration
    pub fn default() -> Self {
        Self::new(PipelineConfig::default())
    }

    /// Create a pipeline optimized for a specific use case
    pub fn for_use_case(use_case: UseCase) -> Self {
        let mut config = PipelineConfig::default();
        config.use_case = use_case;

        // Adjust configuration based on use case
        match use_case {
            UseCase::Prototyping => {
                config.quality = QualityLevel::Fast;
                config.fallback_algorithms = vec![Algorithm::Delaunay, Algorithm::BallPivoting];
            }
            UseCase::Engineering => {
                config.quality = QualityLevel::HighQuality;
                config.validate_output = true;
                config.auto_repair = true;
            }
            UseCase::Organic => {
                config.quality = QualityLevel::HighQuality;
                config.fallback_algorithms = vec![
                    Algorithm::MovingLeastSquares,
                    Algorithm::Poisson,
                    Algorithm::BallPivoting,
                ];
            }
            UseCase::NoisyData => {
                config.fallback_algorithms =
                    vec![Algorithm::MovingLeastSquares, Algorithm::Delaunay];
            }
            UseCase::Sparse => {
                config.fallback_algorithms =
                    vec![Algorithm::Delaunay, Algorithm::MovingLeastSquares];
            }
            UseCase::Dense => {
                config.fallback_algorithms = vec![
                    Algorithm::Poisson,
                    Algorithm::BallPivoting,
                    Algorithm::MarchingCubes,
                ];
            }
            UseCase::General => {
                // Use default configuration
            }
        }

        Self::new(config)
    }

    /// Analyze input data characteristics
    pub fn analyze_data(&self, cloud: &PointCloud<Point3f>) -> Result<DataCharacteristics> {
        if cloud.is_empty() {
            return Err(Error::InvalidData("Point cloud is empty".to_string()));
        }

        let point_count = cloud.points.len();

        // Compute bounding box
        let (bounds_min, bounds_max) = parallel::point_cloud::parallel_bounding_box(&cloud.points)
            .unwrap_or((Point3f::origin(), Point3f::new(1.0, 1.0, 1.0)));

        // Sample points for analysis to avoid O(n²) complexity
        let sample_size = point_count.min(1000);
        let step = (point_count.max(1) / sample_size.max(1)).max(1);
        let sample_points: Vec<Point3f> = cloud.points.iter().step_by(step).cloned().collect();

        // Analyze nearest neighbor distances
        let neighbor_distances = self.compute_neighbor_distances(&sample_points)?;
        let avg_neighbor_distance =
            neighbor_distances.iter().sum::<f32>() / neighbor_distances.len() as f32;

        // Estimate density uniformity
        let density_uniformity = self.estimate_density_uniformity(&neighbor_distances);

        // Estimate noise level using distance variance
        let distance_variance = self.compute_variance(&neighbor_distances);
        let noise_level = (distance_variance / avg_neighbor_distance.powi(2)).min(1.0);

        // Determine distribution type
        let distribution_type = self.classify_distribution(&cloud.points, &bounds_min, &bounds_max);

        // Estimate surface complexity
        let surface_complexity = self.estimate_surface_complexity(&sample_points);

        // Check if surface appears closed
        let is_closed_surface = self.estimate_surface_closure(&sample_points);

        Ok(DataCharacteristics {
            point_count,
            has_normals: false, // Will be overridden for NormalPoint3f clouds
            density_uniformity,
            noise_level,
            avg_neighbor_distance,
            bounding_box: (bounds_min, bounds_max),
            is_closed_surface,
            surface_complexity,
            distribution_type,
        })
    }

    /// Analyze input data characteristics for clouds with normals
    pub fn analyze_data_with_normals(
        &self,
        cloud: &PointCloud<NormalPoint3f>,
    ) -> Result<DataCharacteristics> {
        let point_cloud: PointCloud<Point3f> =
            PointCloud::from_points(cloud.points.iter().map(|p| p.position).collect());

        let mut characteristics = self.analyze_data(&point_cloud)?;
        characteristics.has_normals = true;

        Ok(characteristics)
    }

    /// Select the best algorithm based on data characteristics and configuration
    pub fn select_algorithm(&self, characteristics: &DataCharacteristics) -> Algorithm {
        // Return preferred algorithm if specified
        if let Some(preferred) = self.config.preferred_algorithm {
            return preferred;
        }

        // Algorithm selection logic based on characteristics
        let mut scores = HashMap::new();

        // Initialize all algorithms with base scores
        scores.insert(Algorithm::Delaunay, 0.5);
        scores.insert(Algorithm::BallPivoting, 0.5);
        scores.insert(Algorithm::MovingLeastSquares, 0.5);
        scores.insert(Algorithm::MarchingCubes, 0.5);
        scores.insert(
            Algorithm::Poisson,
            if characteristics.has_normals {
                0.5
            } else {
                0.0
            },
        );

        // Adjust scores based on point count
        match characteristics.point_count {
            0..=100 => {
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.3;
                *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.2;
            }
            101..=1000 => {
                *scores.get_mut(&Algorithm::BallPivoting).unwrap() += 0.2;
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.3;
            }
            1001..=10000 => {
                *scores.get_mut(&Algorithm::BallPivoting).unwrap() += 0.3;
                if characteristics.has_normals {
                    *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.3;
                }
            }
            _ => {
                if characteristics.has_normals {
                    *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.4;
                }
                *scores.get_mut(&Algorithm::MarchingCubes).unwrap() += 0.2;
            }
        }

        // Adjust based on density uniformity
        if characteristics.density_uniformity > 0.7 {
            *scores.get_mut(&Algorithm::BallPivoting).unwrap() += 0.2;
            if characteristics.has_normals {
                *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.2;
            }
        } else {
            *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.3;
            *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.2;
        }

        // Adjust based on noise level
        if characteristics.noise_level > 0.3 {
            *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.3;
            *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.1;
            // Penalize sensitive algorithms
            *scores.get_mut(&Algorithm::BallPivoting).unwrap() -= 0.2;
        }

        // Adjust based on surface complexity
        if characteristics.surface_complexity > 0.7 {
            if characteristics.has_normals {
                *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.3;
            }
            *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.2;
        }

        // Adjust based on distribution type
        match characteristics.distribution_type {
            DistributionType::Planar => {
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.3;
            }
            DistributionType::Spherical | DistributionType::Cylindrical => {
                *scores.get_mut(&Algorithm::BallPivoting).unwrap() += 0.2;
                *scores.get_mut(&Algorithm::MarchingCubes).unwrap() += 0.3;
            }
            DistributionType::Arbitrary => {
                // No specific preference
            }
        }

        // Adjust based on quality requirements
        match self.config.quality {
            QualityLevel::Fast => {
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.3;
            }
            QualityLevel::Balanced => {
                *scores.get_mut(&Algorithm::BallPivoting).unwrap() += 0.2;
            }
            QualityLevel::HighQuality | QualityLevel::MaxQuality => {
                if characteristics.has_normals {
                    *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.3;
                }
                *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.2;
            }
        }

        // Adjust based on use case
        match self.config.use_case {
            UseCase::Engineering => {
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.2;
                if characteristics.has_normals {
                    *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.2;
                }
            }
            UseCase::Organic => {
                *scores.get_mut(&Algorithm::MovingLeastSquares).unwrap() += 0.3;
                if characteristics.has_normals {
                    *scores.get_mut(&Algorithm::Poisson).unwrap() += 0.2;
                }
            }
            UseCase::Prototyping => {
                *scores.get_mut(&Algorithm::Delaunay).unwrap() += 0.4;
            }
            _ => {}
        }

        // Find algorithm with highest score
        scores
            .into_iter()
            .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
            .map(|(algo, _)| algo)
            .unwrap_or(Algorithm::Delaunay)
    }

    /// Reconstruct surface from point cloud without normals
    pub fn reconstruct(&self, cloud: &PointCloud<Point3f>) -> Result<ReconstructionResult> {
        let start_time = std::time::Instant::now();

        // Analyze data characteristics
        let characteristics = self.analyze_data(cloud)?;

        // Select best algorithm
        let selected_algorithm = self.select_algorithm(&characteristics);

        // Try reconstruction with selected algorithm
        let mesh = match self.try_algorithm(cloud, selected_algorithm) {
            Ok(mesh) => mesh,
            Err(_) => {
                // Try fallback algorithms
                let mut last_error = Error::Algorithm("No algorithms succeeded".to_string());
                for &fallback_algo in &self.config.fallback_algorithms {
                    if fallback_algo != selected_algorithm {
                        match self.try_algorithm(cloud, fallback_algo) {
                            Ok(mesh) => {
                                let processing_time = start_time.elapsed().as_secs_f32();
                                let quality_metrics =
                                    self.compute_quality_metrics(&mesh, &characteristics);

                                return Ok(ReconstructionResult {
                                    mesh,
                                    algorithm_used: fallback_algo,
                                    processing_time,
                                    quality_metrics,
                                    data_characteristics: characteristics,
                                });
                            }
                            Err(e) => last_error = e,
                        }
                    }
                }
                return Err(last_error);
            }
        };

        let processing_time = start_time.elapsed().as_secs_f32();
        let quality_metrics = self.compute_quality_metrics(&mesh, &characteristics);

        Ok(ReconstructionResult {
            mesh,
            algorithm_used: selected_algorithm,
            processing_time,
            quality_metrics,
            data_characteristics: characteristics,
        })
    }

    /// Reconstruct surface from point cloud with normals
    pub fn reconstruct_with_normals(
        &self,
        cloud: &PointCloud<NormalPoint3f>,
    ) -> Result<ReconstructionResult> {
        let start_time = std::time::Instant::now();

        // Analyze data characteristics
        let characteristics = self.analyze_data_with_normals(cloud)?;

        // Select best algorithm
        let selected_algorithm = self.select_algorithm(&characteristics);

        // Try reconstruction with selected algorithm
        let mesh = match self.try_algorithm_with_normals(cloud, selected_algorithm) {
            Ok(mesh) => mesh,
            Err(_) => {
                // Try fallback algorithms
                let mut last_error = Error::Algorithm("No algorithms succeeded".to_string());
                for &fallback_algo in &self.config.fallback_algorithms {
                    if fallback_algo != selected_algorithm {
                        match self.try_algorithm_with_normals(cloud, fallback_algo) {
                            Ok(mesh) => {
                                let processing_time = start_time.elapsed().as_secs_f32();
                                let quality_metrics =
                                    self.compute_quality_metrics(&mesh, &characteristics);

                                return Ok(ReconstructionResult {
                                    mesh,
                                    algorithm_used: fallback_algo,
                                    processing_time,
                                    quality_metrics,
                                    data_characteristics: characteristics,
                                });
                            }
                            Err(e) => last_error = e,
                        }
                    }
                }
                return Err(last_error);
            }
        };

        let processing_time = start_time.elapsed().as_secs_f32();
        let quality_metrics = self.compute_quality_metrics(&mesh, &characteristics);

        Ok(ReconstructionResult {
            mesh,
            algorithm_used: selected_algorithm,
            processing_time,
            quality_metrics,
            data_characteristics: characteristics,
        })
    }

    // Helper methods for data analysis
    fn compute_neighbor_distances(&self, points: &[Point3f]) -> Result<Vec<f32>> {
        if points.len() < 2 {
            return Ok(vec![1.0]); // Default fallback
        }

        let distances = parallel::parallel_map(points, |point| {
            let mut min_dist = f32::INFINITY;
            for other_point in points {
                if point != other_point {
                    let dist = (point - other_point).magnitude();
                    if dist < min_dist {
                        min_dist = dist;
                    }
                }
            }
            min_dist
        });

        let finite_distances: Vec<f32> = distances
            .into_iter()
            .filter(|&d| d.is_finite() && d > 0.0)
            .collect();

        if finite_distances.is_empty() {
            Ok(vec![1.0])
        } else {
            Ok(finite_distances)
        }
    }

    fn estimate_density_uniformity(&self, distances: &[f32]) -> f32 {
        if distances.len() < 2 {
            return 1.0;
        }

        let mean = distances.iter().sum::<f32>() / distances.len() as f32;
        let variance = self.compute_variance(distances);
        let cv = if mean > 0.0 {
            variance.sqrt() / mean
        } else {
            0.0
        };

        // Convert coefficient of variation to uniformity (0.0 = non-uniform, 1.0 = uniform)
        (1.0 / (1.0 + cv)).min(1.0)
    }

    fn compute_variance(&self, values: &[f32]) -> f32 {
        if values.len() < 2 {
            return 0.0;
        }

        let mean = values.iter().sum::<f32>() / values.len() as f32;
        let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / values.len() as f32;

        variance
    }

    fn classify_distribution(
        &self,
        _points: &[Point3f],
        bounds_min: &Point3f,
        bounds_max: &Point3f,
    ) -> DistributionType {
        let extents = [
            bounds_max.x - bounds_min.x,
            bounds_max.y - bounds_min.y,
            bounds_max.z - bounds_min.z,
        ];

        let max_extent = extents.iter().fold(0.0f32, |acc, &x| acc.max(x));
        let min_extent = extents.iter().fold(f32::INFINITY, |acc, &x| acc.min(x));

        if max_extent < 1e-6 {
            return DistributionType::Arbitrary;
        }

        // Check if one dimension is much smaller (planar)
        if min_extent < max_extent * 0.1 {
            return DistributionType::Planar;
        }

        // Check if roughly spherical/cubic
        let extent_ratios = [
            extents[0] / max_extent,
            extents[1] / max_extent,
            extents[2] / max_extent,
        ];

        if extent_ratios.iter().all(|&r| r > 0.7) {
            DistributionType::Spherical
        } else if extent_ratios.iter().filter(|&&r| r > 0.7).count() == 2 {
            DistributionType::Cylindrical
        } else {
            DistributionType::Arbitrary
        }
    }

    fn estimate_surface_complexity(&self, points: &[Point3f]) -> f32 {
        if points.len() < 10 {
            return 0.5; // Default for small datasets
        }

        // Simple complexity estimation based on local curvature variation
        let sample_size = points.len().min(100);
        let step = (points.len().max(1) / sample_size.max(1)).max(1);
        let sample_points: Vec<Point3f> = points.iter().step_by(step).cloned().collect();

        let mut curvature_variations = Vec::new();

        for (i, point) in sample_points.iter().enumerate() {
            // Find nearby points
            let mut neighbors = Vec::new();
            for (j, other_point) in sample_points.iter().enumerate() {
                if i != j {
                    let dist = (point - other_point).magnitude();
                    if dist < 0.1 {
                        // Fixed radius for simplicity
                        neighbors.push(*other_point);
                    }
                }
            }

            if neighbors.len() >= 3 {
                // Estimate local curvature variation
                let variation = self.estimate_local_curvature_variation(point, &neighbors);
                curvature_variations.push(variation);
            }
        }

        if curvature_variations.is_empty() {
            0.5
        } else {
            let avg_variation =
                curvature_variations.iter().sum::<f32>() / curvature_variations.len() as f32;
            avg_variation.min(1.0)
        }
    }

    fn estimate_local_curvature_variation(&self, center: &Point3f, neighbors: &[Point3f]) -> f32 {
        if neighbors.len() < 3 {
            return 0.0;
        }

        // Simple estimation using angle variations
        let mut angles = Vec::new();
        for i in 0..neighbors.len() {
            let v1 = (neighbors[i] - *center).normalize();
            let v2 = (neighbors[(i + 1) % neighbors.len()] - *center).normalize();
            let angle = v1.dot(&v2).clamp(-1.0, 1.0).acos();
            angles.push(angle);
        }

        self.compute_variance(&angles) / std::f32::consts::PI
    }

    fn estimate_surface_closure(&self, points: &[Point3f]) -> bool {
        // Simple heuristic: if points are roughly uniformly distributed around a center,
        // assume closed surface
        if points.len() < 50 {
            return false; // Too few points to determine
        }

        // Compute centroid
        let centroid = points.iter().fold(Point3f::origin(), |acc, p| {
            Point3f::from(acc.coords + p.coords)
        });
        let centroid = Point3f::from(centroid.coords / points.len() as f32);

        // Check if points are roughly uniformly distributed around centroid
        let distances: Vec<f32> = points.iter().map(|p| (p - centroid).magnitude()).collect();

        let mean_dist = distances.iter().sum::<f32>() / distances.len() as f32;
        let variance = self.compute_variance(&distances);
        let cv = if mean_dist > 0.0 {
            variance.sqrt() / mean_dist
        } else {
            1.0
        };

        // If coefficient of variation is low, points are roughly at same distance from center
        cv < 0.3
    }

    // Helper methods for algorithm execution
    fn try_algorithm(
        &self,
        cloud: &PointCloud<Point3f>,
        algorithm: Algorithm,
    ) -> Result<TriangleMesh> {
        match algorithm {
            Algorithm::Delaunay => crate::delaunay::delaunay_triangulation_auto(cloud),
            Algorithm::BallPivoting => {
                let radius = crate::ball_pivoting::estimate_optimal_radius(cloud, 0.5)?;
                crate::ball_pivoting::ball_pivoting_reconstruction(cloud, radius)
            }
            Algorithm::MovingLeastSquares => {
                crate::moving_least_squares::moving_least_squares_auto(cloud)
            }
            Algorithm::MarchingCubes => {
                // Convert point cloud to volumetric representation
                let mls = crate::moving_least_squares::MLSSurface::new(
                    cloud,
                    crate::moving_least_squares::MLSConfig::default(),
                )?;
                mls.extract_mesh()
            }
            Algorithm::Poisson => {
                // Cannot use Poisson without normals
                Err(Error::InvalidData(
                    "Poisson reconstruction requires normals".to_string(),
                ))
            }
        }
    }

    fn try_algorithm_with_normals(
        &self,
        cloud: &PointCloud<NormalPoint3f>,
        algorithm: Algorithm,
    ) -> Result<TriangleMesh> {
        match algorithm {
            Algorithm::Poisson => crate::poisson::poisson_reconstruction_default(cloud),
            Algorithm::BallPivoting => {
                let radius = {
                    let point_cloud: PointCloud<Point3f> =
                        PointCloud::from_points(cloud.points.iter().map(|p| p.position).collect());
                    crate::ball_pivoting::estimate_optimal_radius(&point_cloud, 0.5)?
                };
                crate::ball_pivoting::ball_pivoting_from_normals(cloud, radius)
            }
            Algorithm::Delaunay => {
                let point_cloud: PointCloud<Point3f> =
                    PointCloud::from_points(cloud.points.iter().map(|p| p.position).collect());
                crate::delaunay::delaunay_triangulation_auto(&point_cloud)
            }
            Algorithm::MovingLeastSquares => {
                crate::moving_least_squares::moving_least_squares_from_normals(cloud)
            }
            Algorithm::MarchingCubes => {
                // Convert to MLS and extract
                let mls = crate::moving_least_squares::MLSSurface::from_normals(
                    cloud,
                    crate::moving_least_squares::MLSConfig::default(),
                )?;
                mls.extract_mesh()
            }
        }
    }

    fn compute_quality_metrics(
        &self,
        mesh: &TriangleMesh,
        characteristics: &DataCharacteristics,
    ) -> QualityMetrics {
        let vertex_count = mesh.vertex_count();
        let triangle_count = mesh.face_count();

        // Simple quality metrics - in a real implementation these would be more sophisticated
        let avg_triangle_quality = 0.75; // Placeholder
        let watertightness = if characteristics.is_closed_surface {
            0.8
        } else {
            0.6
        };
        let smoothness = 1.0 - characteristics.noise_level;
        let geometric_accuracy = 0.8; // Placeholder

        QualityMetrics {
            vertex_count,
            triangle_count,
            avg_triangle_quality,
            watertightness,
            smoothness,
            geometric_accuracy,
        }
    }
}

/// Convenience function for quick reconstruction with automatic algorithm selection
pub fn auto_reconstruct(cloud: &PointCloud<Point3f>) -> Result<TriangleMesh> {
    let pipeline = ReconstructionPipeline::default();
    Ok(pipeline.reconstruct(cloud)?.mesh)
}

/// Convenience function for quick reconstruction with normals
pub fn auto_reconstruct_with_normals(cloud: &PointCloud<NormalPoint3f>) -> Result<TriangleMesh> {
    let pipeline = ReconstructionPipeline::default();
    Ok(pipeline.reconstruct_with_normals(cloud)?.mesh)
}

/// Convenience function for reconstruction with specific quality level
pub fn auto_reconstruct_with_quality(
    cloud: &PointCloud<Point3f>,
    quality: QualityLevel,
) -> Result<TriangleMesh> {
    let mut config = PipelineConfig::default();
    config.quality = quality;
    let pipeline = ReconstructionPipeline::new(config);
    Ok(pipeline.reconstruct(cloud)?.mesh)
}

/// Convenience function for reconstruction optimized for a use case
pub fn auto_reconstruct_for_use_case(
    cloud: &PointCloud<Point3f>,
    use_case: UseCase,
) -> Result<TriangleMesh> {
    let pipeline = ReconstructionPipeline::for_use_case(use_case);
    Ok(pipeline.reconstruct(cloud)?.mesh)
}

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

    #[test]
    fn test_pipeline_config_default() {
        let config = PipelineConfig::default();
        assert_eq!(config.quality, QualityLevel::Balanced);
        assert_eq!(config.use_case, UseCase::General);
        assert!(config.enable_parallel);
        assert!(config.validate_output);
    }

    #[test]
    fn test_pipeline_for_use_case() {
        let pipeline = ReconstructionPipeline::for_use_case(UseCase::Prototyping);
        assert_eq!(pipeline.config.quality, QualityLevel::Fast);
        assert_eq!(pipeline.config.use_case, UseCase::Prototyping);
    }

    #[test]
    fn test_data_analysis_empty_cloud() {
        let pipeline = ReconstructionPipeline::default();
        let cloud = PointCloud::new();
        let result = pipeline.analyze_data(&cloud);
        assert!(result.is_err());
    }

    #[test]
    fn test_data_analysis_simple() {
        let pipeline = ReconstructionPipeline::default();
        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.0, 1.0, 0.0),
        ];
        let cloud = PointCloud::from_points(points);

        let characteristics = pipeline.analyze_data(&cloud).unwrap();
        assert_eq!(characteristics.point_count, 4);
        assert!(!characteristics.has_normals);
        assert_eq!(characteristics.distribution_type, DistributionType::Planar);
    }

    #[test]
    fn test_algorithm_selection_sparse_data() {
        let pipeline = ReconstructionPipeline::default();
        let characteristics = DataCharacteristics {
            point_count: 50,
            has_normals: false,
            density_uniformity: 0.5,
            noise_level: 0.1,
            avg_neighbor_distance: 0.1,
            bounding_box: (Point3f::origin(), Point3f::new(1.0, 1.0, 1.0)),
            is_closed_surface: false,
            surface_complexity: 0.3,
            distribution_type: DistributionType::Planar,
        };

        let algorithm = pipeline.select_algorithm(&characteristics);
        // For sparse planar data, should prefer Delaunay
        assert!(matches!(
            algorithm,
            Algorithm::Delaunay | Algorithm::MovingLeastSquares
        ));
    }

    #[test]
    fn test_algorithm_selection_dense_with_normals() {
        let pipeline = ReconstructionPipeline::default();
        let characteristics = DataCharacteristics {
            point_count: 5000,
            has_normals: true,
            density_uniformity: 0.8,
            noise_level: 0.1,
            avg_neighbor_distance: 0.05,
            bounding_box: (Point3f::origin(), Point3f::new(1.0, 1.0, 1.0)),
            is_closed_surface: true,
            surface_complexity: 0.7,
            distribution_type: DistributionType::Spherical,
        };

        let algorithm = pipeline.select_algorithm(&characteristics);
        // For dense, uniform data with normals, should prefer Poisson or BallPivoting
        assert!(matches!(
            algorithm,
            Algorithm::Poisson | Algorithm::BallPivoting
        ));
    }

    #[test]
    fn test_auto_reconstruct_simple() {
        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.0, 1.0, 0.0),
        ];
        let cloud = PointCloud::from_points(points);

        let result = auto_reconstruct(&cloud);
        match result {
            Ok(mesh) => {
                assert!(!mesh.is_empty());
            }
            Err(_) => {
                // Algorithm may fail on simple test data - that's acceptable
                println!(
                    "Auto reconstruction failed on simple test data (expected for some algorithms)"
                );
            }
        }
    }

    #[test]
    fn test_quality_levels() {
        let quality_levels = [
            QualityLevel::Fast,
            QualityLevel::Balanced,
            QualityLevel::HighQuality,
            QualityLevel::MaxQuality,
        ];

        for quality in &quality_levels {
            let mut config = PipelineConfig::default();
            config.quality = *quality;
            let _pipeline = ReconstructionPipeline::new(config);
            // Just test that pipelines can be created with different quality levels
        }
    }

    #[test]
    fn test_use_cases() {
        let use_cases = [
            UseCase::General,
            UseCase::Prototyping,
            UseCase::Engineering,
            UseCase::Organic,
            UseCase::NoisyData,
            UseCase::Sparse,
            UseCase::Dense,
        ];

        for use_case in &use_cases {
            let pipeline = ReconstructionPipeline::for_use_case(*use_case);
            assert_eq!(pipeline.config.use_case, *use_case);
        }
    }

    #[test]
    fn test_distribution_classification() {
        let pipeline = ReconstructionPipeline::default();

        // Test planar distribution
        let planar_points = vec![
            Point3f::new(0.0, 0.0, 0.0),
            Point3f::new(1.0, 0.0, 0.0),
            Point3f::new(0.0, 1.0, 0.0),
            Point3f::new(1.0, 1.0, 0.0),
        ];
        let min_bounds = Point3f::new(0.0, 0.0, 0.0);
        let max_bounds = Point3f::new(1.0, 1.0, 0.0);
        let distribution = pipeline.classify_distribution(&planar_points, &min_bounds, &max_bounds);
        assert_eq!(distribution, DistributionType::Planar);

        // Test spherical distribution
        let min_bounds_sphere = Point3f::new(-1.0, -1.0, -1.0);
        let max_bounds_sphere = Point3f::new(1.0, 1.0, 1.0);
        let distribution =
            pipeline.classify_distribution(&planar_points, &min_bounds_sphere, &max_bounds_sphere);
        assert_eq!(distribution, DistributionType::Spherical);
    }
}