threecrate-io 0.8.0

I/O operations for point clouds and meshes in 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
//! XYZ/CSV point cloud format support
//! 
//! This module provides comprehensive XYZ and CSV point cloud reading capabilities including:
//! - Auto-detection of delimiters (comma, space, tab)
//! - Header detection and parsing
//! - Support for x,y,z coordinates (required) and optional intensity, r,g,b, nx,ny,nz
//! - Schema hints for flexible parsing
//! - Streaming support for large files

use threecrate_core::{PointCloud, Result, Point3f, Vector3f, Error};
use std::path::Path;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};

/// Supported delimiters for CSV/XYZ files
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delimiter {
    Comma,
    Space,
    Tab,
    Semicolon,
}

impl Delimiter {
    /// Get the character representation of the delimiter
    pub fn as_char(&self) -> char {
        match self {
            Delimiter::Comma => ',',
            Delimiter::Space => ' ',
            Delimiter::Tab => '\t',
            Delimiter::Semicolon => ';',
        }
    }
    
    /// Detect delimiter from a line of text
    pub fn detect_from_line(line: &str) -> Option<Self> {
        let comma_count = line.matches(',').count();
        let space_count = line.matches(' ').count();
        let tab_count = line.matches('\t').count();
        let semicolon_count = line.matches(';').count();
        
        // Find the delimiter with the highest count
        let counts = [
            (comma_count, Delimiter::Comma),
            (space_count, Delimiter::Space),
            (tab_count, Delimiter::Tab),
            (semicolon_count, Delimiter::Semicolon),
        ];
        
        counts.iter()
            .max_by_key(|(count, _)| count)
            .filter(|(count, _)| *count > 0)
            .map(|(_, delimiter)| *delimiter)
    }
}

/// Column types that can be parsed from XYZ/CSV files
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnType {
    X,
    Y,
    Z,
    Intensity,
    Red,
    Green,
    Blue,
    NormalX,
    NormalY,
    NormalZ,
    Unknown,
}

impl ColumnType {
    /// Parse column type from header name
    pub fn from_header(header: &str) -> Self {
        let header_lower = header.to_lowercase();
        let header_trimmed = header_lower.trim();
        match header_trimmed {
            "x" | "px" | "pos_x" | "position_x" => ColumnType::X,
            "y" | "py" | "pos_y" | "position_y" => ColumnType::Y,
            "z" | "pz" | "pos_z" | "position_z" => ColumnType::Z,
            "i" | "intensity" | "int" => ColumnType::Intensity,
            "r" | "red" | "color_r" => ColumnType::Red,
            "g" | "green" | "color_g" => ColumnType::Green,
            "b" | "blue" | "color_b" => ColumnType::Blue,
            "nx" | "normal_x" | "n_x" => ColumnType::NormalX,
            "ny" | "normal_y" | "n_y" => ColumnType::NormalY,
            "nz" | "normal_z" | "n_z" => ColumnType::NormalZ,
            _ => ColumnType::Unknown,
        }
    }
}

/// Schema definition for parsing XYZ/CSV files
#[derive(Debug, Clone)]
pub struct XyzCsvSchema {
    pub columns: Vec<ColumnType>,
    pub has_header: bool,
    pub delimiter: Delimiter,
}

impl XyzCsvSchema {
    /// Create a new schema
    pub fn new(columns: Vec<ColumnType>, has_header: bool, delimiter: Delimiter) -> Self {
        Self {
            columns,
            has_header,
            delimiter,
        }
    }
    
    /// Auto-detect schema from file content
    pub fn detect_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path_ref = path.as_ref();
        let file = File::open(path_ref)?;
        let mut reader = BufReader::new(file);
        let mut first_line = String::new();
        reader.read_line(&mut first_line)?;
        
        // Detect delimiter
        let delimiter = Delimiter::detect_from_line(&first_line)
            .ok_or_else(|| Error::InvalidData("Could not detect delimiter".to_string()))?;
        
        // Determine if this is a header by checking if first line contains non-numeric data
        let has_header = Self::is_header_line(&first_line, &[], delimiter);
        
        let columns = if has_header {
            // Parse columns from header
            Self::parse_columns(&first_line, delimiter)?
        } else {
            // For files without headers, assume x,y,z format
            vec![ColumnType::X, ColumnType::Y, ColumnType::Z]
        };
        
        Ok(Self::new(columns, has_header, delimiter))
    }
    
    /// Parse columns from a header line
    fn parse_columns(line: &str, delimiter: Delimiter) -> Result<Vec<ColumnType>> {
        let parts: Vec<&str> = line.split(delimiter.as_char())
            .map(|s| s.trim())
            .collect();
        
        let columns: Vec<ColumnType> = parts.iter()
            .map(|header| ColumnType::from_header(header))
            .collect();
        
        // Validate that we have at least x, y, z columns
        let has_x = columns.contains(&ColumnType::X);
        let has_y = columns.contains(&ColumnType::Y);
        let has_z = columns.contains(&ColumnType::Z);
        
        if !has_x || !has_y || !has_z {
            return Err(Error::InvalidData(
                "XYZ/CSV file must contain x, y, z coordinates".to_string()
            ));
        }
        
        Ok(columns)
    }
    
    /// Determine if a line is a header by checking for non-numeric content
    fn is_header_line(line: &str, columns: &[ColumnType], delimiter: Delimiter) -> bool {
        let parts: Vec<&str> = line.split(delimiter.as_char())
            .map(|s| s.trim())
            .collect();
        
        // If we have fewer than 3 parts, it's likely not a valid point cloud line
        if parts.len() < 3 {
            return false;
        }
        
        // If we have columns defined, check against them
        if !columns.is_empty() {
            // If we have fewer parts than expected columns, it's likely not a header
            if parts.len() < columns.len() {
                return false;
            }
            
            // Check if any part that should be numeric is not numeric
            for (i, part) in parts.iter().enumerate() {
                if i < columns.len() {
                    match columns[i] {
                        ColumnType::X | ColumnType::Y | ColumnType::Z |
                        ColumnType::Intensity | ColumnType::Red | ColumnType::Green | ColumnType::Blue |
                        ColumnType::NormalX | ColumnType::NormalY | ColumnType::NormalZ => {
                            if part.parse::<f32>().is_err() {
                                return true; // Non-numeric data suggests this is a header
                            }
                        }
                        ColumnType::Unknown => {
                            // For unknown columns, try to parse as float
                            if part.parse::<f32>().is_err() {
                                return true;
                            }
                        }
                    }
                }
            }
        } else {
            // No columns defined, check if first 3 parts are numeric
            for (_i, part) in parts.iter().enumerate().take(3) {
                if part.parse::<f32>().is_err() {
                    return true; // Non-numeric data suggests this is a header
                }
            }
        }
        
        false
    }
}

/// Point data parsed from XYZ/CSV file
#[derive(Debug, Clone)]
pub struct XyzCsvPoint {
    pub position: Point3f,
    pub intensity: Option<f32>,
    pub color: Option<[u8; 3]>,
    pub normal: Option<Vector3f>,
}

impl XyzCsvPoint {
    /// Create a new point with only position
    pub fn new(position: Point3f) -> Self {
        Self {
            position,
            intensity: None,
            color: None,
            normal: None,
        }
    }
    
    /// Create a point with position and intensity
    pub fn with_intensity(position: Point3f, intensity: f32) -> Self {
        Self {
            position,
            intensity: Some(intensity),
            color: None,
            normal: None,
        }
    }
    
    /// Create a point with position and color
    pub fn with_color(position: Point3f, color: [u8; 3]) -> Self {
        Self {
            position,
            intensity: None,
            color: Some(color),
            normal: None,
        }
    }
    
    /// Create a point with position and normal
    pub fn with_normal(position: Point3f, normal: Vector3f) -> Self {
        Self {
            position,
            intensity: None,
            color: None,
            normal: Some(normal),
        }
    }
}

/// XYZ/CSV reader implementation
pub struct XyzCsvReader;

impl XyzCsvReader {
    /// Read a point cloud from an XYZ/CSV file with auto-detection
    pub fn read_point_cloud<P: AsRef<Path>>(path: P) -> Result<PointCloud<Point3f>> {
        let schema = XyzCsvSchema::detect_from_file(&path)?;
        Self::read_point_cloud_with_schema(path, &schema)
    }
    
    /// Read a point cloud with a specific schema
    pub fn read_point_cloud_with_schema<P: AsRef<Path>>(
        path: P, 
        schema: &XyzCsvSchema
    ) -> Result<PointCloud<Point3f>> {
        let file = File::open(path)?;
        let reader = BufReader::new(file);
        let mut lines = reader.lines();
        
        // Skip header if present
        if schema.has_header {
            lines.next();
        }
        
        let mut cloud = PointCloud::new();
        
        for line_result in lines {
            let line = line_result?;
            if line.trim().is_empty() {
                continue;
            }
            
            let point = Self::parse_line(&line, schema)?;
            cloud.push(point.position);
        }
        
        Ok(cloud)
    }
    
    /// Read detailed point data (with colors, normals, etc.)
    pub fn read_detailed_points<P: AsRef<Path>>(path: P) -> Result<Vec<XyzCsvPoint>> {
        let schema = XyzCsvSchema::detect_from_file(&path)?;
        Self::read_detailed_points_with_schema(path, &schema)
    }
    
    /// Read detailed point data with a specific schema
    pub fn read_detailed_points_with_schema<P: AsRef<Path>>(
        path: P,
        schema: &XyzCsvSchema
    ) -> Result<Vec<XyzCsvPoint>> {
        let file = File::open(path)?;
        let reader = BufReader::new(file);
        let mut lines = reader.lines();
        
        // Skip header if present
        if schema.has_header {
            lines.next();
        }
        
        let mut points = Vec::new();
        
        for line_result in lines {
            let line = line_result?;
            if line.trim().is_empty() {
                continue;
            }
            
            let point = Self::parse_line(&line, schema)?;
            points.push(point);
        }
        
        Ok(points)
    }
    
    /// Parse a single line into a point
    fn parse_line(line: &str, schema: &XyzCsvSchema) -> Result<XyzCsvPoint> {
        let parts: Vec<&str> = line.split(schema.delimiter.as_char())
            .map(|s| s.trim())
            .collect();
        
        if parts.len() < 3 {
            return Err(Error::InvalidData(
                "Line must have at least 3 columns (x, y, z)".to_string()
            ));
        }
        
        // Find x, y, z indices
        let mut x_idx = None;
        let mut y_idx = None;
        let mut z_idx = None;
        let mut intensity_idx = None;
        let mut red_idx = None;
        let mut green_idx = None;
        let mut blue_idx = None;
        let mut nx_idx = None;
        let mut ny_idx = None;
        let mut nz_idx = None;
        
        for (i, col_type) in schema.columns.iter().enumerate() {
            match *col_type {
                ColumnType::X => x_idx = Some(i),
                ColumnType::Y => y_idx = Some(i),
                ColumnType::Z => z_idx = Some(i),
                ColumnType::Intensity => intensity_idx = Some(i),
                ColumnType::Red => red_idx = Some(i),
                ColumnType::Green => green_idx = Some(i),
                ColumnType::Blue => blue_idx = Some(i),
                ColumnType::NormalX => nx_idx = Some(i),
                ColumnType::NormalY => ny_idx = Some(i),
                ColumnType::NormalZ => nz_idx = Some(i),
                ColumnType::Unknown => {}
            }
        }
        
        // Parse position (required)
        let x = parts[x_idx.ok_or_else(|| Error::InvalidData("Missing x coordinate".to_string()))?]
            .parse::<f32>()
            .map_err(|_| Error::InvalidData("Invalid x coordinate".to_string()))?;
        let y = parts[y_idx.ok_or_else(|| Error::InvalidData("Missing y coordinate".to_string()))?]
            .parse::<f32>()
            .map_err(|_| Error::InvalidData("Invalid y coordinate".to_string()))?;
        let z = parts[z_idx.ok_or_else(|| Error::InvalidData("Missing z coordinate".to_string()))?]
            .parse::<f32>()
            .map_err(|_| Error::InvalidData("Invalid z coordinate".to_string()))?;
        
        let position = Point3f::new(x, y, z);
        
        // Parse optional attributes
        let intensity = if let Some(idx) = intensity_idx {
            parts.get(idx).and_then(|s| s.parse::<f32>().ok())
        } else {
            None
        };
        
        let color = if let (Some(r_idx), Some(g_idx), Some(b_idx)) = (red_idx, green_idx, blue_idx) {
            if let (Some(r), Some(g), Some(b)) = (
                parts.get(r_idx).and_then(|s| s.parse::<f32>().ok()),
                parts.get(g_idx).and_then(|s| s.parse::<f32>().ok()),
                parts.get(b_idx).and_then(|s| s.parse::<f32>().ok()),
            ) {
                Some([
                    (r.clamp(0.0, 255.0) as u8),
                    (g.clamp(0.0, 255.0) as u8),
                    (b.clamp(0.0, 255.0) as u8),
                ])
            } else {
                None
            }
        } else {
            None
        };
        
        let normal = if let (Some(nx_idx), Some(ny_idx), Some(nz_idx)) = (nx_idx, ny_idx, nz_idx) {
            if let (Some(nx), Some(ny), Some(nz)) = (
                parts.get(nx_idx).and_then(|s| s.parse::<f32>().ok()),
                parts.get(ny_idx).and_then(|s| s.parse::<f32>().ok()),
                parts.get(nz_idx).and_then(|s| s.parse::<f32>().ok()),
            ) {
                Some(Vector3f::new(nx, ny, nz))
            } else {
                None
            }
        } else {
            None
        };
        
        Ok(XyzCsvPoint {
            position,
            intensity,
            color,
            normal,
        })
    }
}

/// XYZ/CSV streaming reader for large files
pub struct XyzCsvStreamingReader {
    reader: BufReader<File>,
    schema: XyzCsvSchema,
    buffer: Vec<String>,
    buffer_index: usize,
    header_skipped: bool,
}

impl XyzCsvStreamingReader {
    /// Create a new streaming reader
    pub fn new<P: AsRef<Path>>(path: P, chunk_size: usize) -> Result<Self> {
        let path_ref = path.as_ref();
        let file = File::open(path_ref)?;
        let reader = BufReader::with_capacity(chunk_size, file);
        let schema = XyzCsvSchema::detect_from_file(path_ref)?;
        
        Ok(Self {
            reader,
            schema,
            buffer: Vec::new(),
            buffer_index: 0,
            header_skipped: false,
        })
    }
    
    /// Fill the buffer with the next chunk of lines
    fn fill_buffer(&mut self) -> Result<bool> {
        self.buffer.clear();
        self.buffer_index = 0;
        
        for _ in 0..1000 { // Read up to 1000 lines at a time
            let mut line = String::new();
            match self.reader.read_line(&mut line)? {
                0 => break, // EOF
                _ => {
                    if !line.trim().is_empty() {
                        self.buffer.push(line);
                    }
                }
            }
        }
        
        Ok(!self.buffer.is_empty())
    }
}

impl Iterator for XyzCsvStreamingReader {
    type Item = Result<Point3f>;
    
    fn next(&mut self) -> Option<Self::Item> {
        // Skip header on first read
        if !self.header_skipped && self.schema.has_header {
            let mut line = String::new();
            if self.reader.read_line(&mut line).is_err() {
                return None;
            }
            self.header_skipped = true;
        }
        
        // Fill buffer if needed
        if self.buffer_index >= self.buffer.len() {
            match self.fill_buffer() {
                Ok(true) => {}, // Buffer filled successfully
                Ok(false) => return None, // EOF
                Err(e) => return Some(Err(e)),
            }
        }
        
        // Get next line from buffer
        if self.buffer_index < self.buffer.len() {
            let line = &self.buffer[self.buffer_index];
            self.buffer_index += 1;
            
            match XyzCsvReader::parse_line(line, &self.schema) {
                Ok(point) => Some(Ok(point.position)),
                Err(e) => Some(Err(e)),
            }
        } else {
            None
        }
    }
}

/// XYZ/CSV writer implementation
pub struct XyzCsvWriter;

impl XyzCsvWriter {
    /// Write a point cloud to an XYZ/CSV file
    pub fn write_point_cloud<P: AsRef<Path>>(
        cloud: &PointCloud<Point3f>, 
        path: P,
        options: &XyzCsvWriteOptions
    ) -> Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);
        
        // Write header if requested
        if options.include_header {
            let header = Self::generate_header(&options.schema);
            writeln!(writer, "{}", header)?;
        }
        
        // Write points
        for point in cloud.iter() {
            let line = Self::format_point(point, &options.schema);
            writeln!(writer, "{}", line)?;
        }
        
        writer.flush()?;
        Ok(())
    }
    
    /// Write detailed points to an XYZ/CSV file
    pub fn write_detailed_points<P: AsRef<Path>>(
        points: &[XyzCsvPoint],
        path: P,
        options: &XyzCsvWriteOptions
    ) -> Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);
        
        // Write header if requested
        if options.include_header {
            let header = Self::generate_header(&options.schema);
            writeln!(writer, "{}", header)?;
        }
        
        // Write points
        for point in points {
            let line = Self::format_detailed_point(point, &options.schema);
            writeln!(writer, "{}", line)?;
        }
        
        writer.flush()?;
        Ok(())
    }
    
    /// Generate header line
    fn generate_header(schema: &XyzCsvSchema) -> String {
        let headers: Vec<&str> = schema.columns.iter().map(|col| match col {
            ColumnType::X => "x",
            ColumnType::Y => "y",
            ColumnType::Z => "z",
            ColumnType::Intensity => "intensity",
            ColumnType::Red => "r",
            ColumnType::Green => "g",
            ColumnType::Blue => "b",
            ColumnType::NormalX => "nx",
            ColumnType::NormalY => "ny",
            ColumnType::NormalZ => "nz",
            ColumnType::Unknown => "unknown",
        }).collect();
        
        headers.join(&schema.delimiter.as_char().to_string())
    }
    
    /// Format a basic point
    fn format_point(point: &Point3f, schema: &XyzCsvSchema) -> String {
        let mut values = Vec::new();
        
        for col_type in &schema.columns {
            match col_type {
                ColumnType::X => values.push(point.x.to_string()),
                ColumnType::Y => values.push(point.y.to_string()),
                ColumnType::Z => values.push(point.z.to_string()),
                _ => values.push("0".to_string()), // Default value for missing columns
            }
        }
        
        values.join(&schema.delimiter.as_char().to_string())
    }
    
    /// Format a detailed point
    fn format_detailed_point(point: &XyzCsvPoint, schema: &XyzCsvSchema) -> String {
        let mut values = Vec::new();
        
        for col_type in &schema.columns {
            let value = match col_type {
                ColumnType::X => point.position.x.to_string(),
                ColumnType::Y => point.position.y.to_string(),
                ColumnType::Z => point.position.z.to_string(),
                ColumnType::Intensity => point.intensity.unwrap_or(0.0).to_string(),
                ColumnType::Red => point.color.map(|c| c[0] as f32).unwrap_or(0.0).to_string(),
                ColumnType::Green => point.color.map(|c| c[1] as f32).unwrap_or(0.0).to_string(),
                ColumnType::Blue => point.color.map(|c| c[2] as f32).unwrap_or(0.0).to_string(),
                ColumnType::NormalX => point.normal.map(|n| n.x).unwrap_or(0.0).to_string(),
                ColumnType::NormalY => point.normal.map(|n| n.y).unwrap_or(0.0).to_string(),
                ColumnType::NormalZ => point.normal.map(|n| n.z).unwrap_or(0.0).to_string(),
                ColumnType::Unknown => "0".to_string(),
            };
            values.push(value);
        }
        
        values.join(&schema.delimiter.as_char().to_string())
    }
}

/// Write options for XYZ/CSV files
#[derive(Debug, Clone)]
pub struct XyzCsvWriteOptions {
    pub schema: XyzCsvSchema,
    pub include_header: bool,
}

impl XyzCsvWriteOptions {
    /// Create basic options for XYZ format
    pub fn xyz() -> Self {
        Self {
            schema: XyzCsvSchema::new(
                vec![ColumnType::X, ColumnType::Y, ColumnType::Z],
                false,
                Delimiter::Space,
            ),
            include_header: false,
        }
    }
    
    /// Create options for CSV format with header
    pub fn csv_with_header() -> Self {
        Self {
            schema: XyzCsvSchema::new(
                vec![ColumnType::X, ColumnType::Y, ColumnType::Z],
                true,
                Delimiter::Comma,
            ),
            include_header: true,
        }
    }
    
    /// Create options for CSV with colors
    pub fn csv_with_colors() -> Self {
        Self {
            schema: XyzCsvSchema::new(
                vec![
                    ColumnType::X, ColumnType::Y, ColumnType::Z,
                    ColumnType::Red, ColumnType::Green, ColumnType::Blue,
                ],
                true,
                Delimiter::Comma,
            ),
            include_header: true,
        }
    }
    
    /// Create options for CSV with normals
    pub fn csv_with_normals() -> Self {
        Self {
            schema: XyzCsvSchema::new(
                vec![
                    ColumnType::X, ColumnType::Y, ColumnType::Z,
                    ColumnType::NormalX, ColumnType::NormalY, ColumnType::NormalZ,
                ],
                true,
                Delimiter::Comma,
            ),
            include_header: true,
        }
    }
    
    /// Create options for CSV with all attributes
    pub fn csv_complete() -> Self {
        Self {
            schema: XyzCsvSchema::new(
                vec![
                    ColumnType::X, ColumnType::Y, ColumnType::Z,
                    ColumnType::Intensity,
                    ColumnType::Red, ColumnType::Green, ColumnType::Blue,
                    ColumnType::NormalX, ColumnType::NormalY, ColumnType::NormalZ,
                ],
                true,
                Delimiter::Comma,
            ),
            include_header: true,
        }
    }
}

// Implement the registry traits
impl crate::registry::PointCloudReader for XyzCsvReader {
    fn read_point_cloud(&self, path: &Path) -> Result<PointCloud<Point3f>> {
        Self::read_point_cloud(path)
    }
    
    fn can_read(&self, path: &Path) -> bool {
        // Check file extension
        if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
            matches!(ext.to_lowercase().as_str(), "xyz" | "csv" | "txt")
        } else {
            false
        }
    }
    
    fn format_name(&self) -> &'static str {
        "xyz_csv"
    }
}

impl crate::registry::PointCloudWriter for XyzCsvWriter {
    fn write_point_cloud(&self, cloud: &PointCloud<Point3f>, path: &Path) -> Result<()> {
        // Auto-detect format based on extension
        let options = if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
            match ext.to_lowercase().as_str() {
                "xyz" => XyzCsvWriteOptions::xyz(),
                "csv" => XyzCsvWriteOptions::csv_with_header(),
                _ => XyzCsvWriteOptions::xyz(),
            }
        } else {
            XyzCsvWriteOptions::xyz()
        };
        
        Self::write_point_cloud(cloud, path, &options)
    }
    
    fn format_name(&self) -> &'static str {
        "xyz_csv"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    
    #[test]
    fn test_delimiter_detection() {
        assert_eq!(Delimiter::detect_from_line("1,2,3"), Some(Delimiter::Comma));
        assert_eq!(Delimiter::detect_from_line("1 2 3"), Some(Delimiter::Space));
        assert_eq!(Delimiter::detect_from_line("1\t2\t3"), Some(Delimiter::Tab));
        assert_eq!(Delimiter::detect_from_line("1;2;3"), Some(Delimiter::Semicolon));
    }
    
    #[test]
    fn test_column_type_detection() {
        assert_eq!(ColumnType::from_header("x"), ColumnType::X);
        assert_eq!(ColumnType::from_header("X"), ColumnType::X);
        assert_eq!(ColumnType::from_header("position_x"), ColumnType::X);
        assert_eq!(ColumnType::from_header("intensity"), ColumnType::Intensity);
        assert_eq!(ColumnType::from_header("red"), ColumnType::Red);
        assert_eq!(ColumnType::from_header("nx"), ColumnType::NormalX);
        assert_eq!(ColumnType::from_header("unknown"), ColumnType::Unknown);
    }
    
    #[test]
    fn test_xyz_reader_basic() {
        let temp_file = "test_basic.xyz";
        let content = "1.0 2.0 3.0\n4.0 5.0 6.0\n7.0 8.0 9.0\n";
        fs::write(temp_file, content).unwrap();
        
        let cloud = XyzCsvReader::read_point_cloud(temp_file).unwrap();
        assert_eq!(cloud.len(), 3);
        assert_eq!(cloud[0], Point3f::new(1.0, 2.0, 3.0));
        assert_eq!(cloud[1], Point3f::new(4.0, 5.0, 6.0));
        assert_eq!(cloud[2], Point3f::new(7.0, 8.0, 9.0));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_csv_reader_with_header() {
        let temp_file = "test_header.csv";
        let content = "x,y,z\n1.0,2.0,3.0\n4.0,5.0,6.0\n";
        fs::write(temp_file, content).unwrap();
        
        let cloud = XyzCsvReader::read_point_cloud(temp_file).unwrap();
        assert_eq!(cloud.len(), 2);
        assert_eq!(cloud[0], Point3f::new(1.0, 2.0, 3.0));
        assert_eq!(cloud[1], Point3f::new(4.0, 5.0, 6.0));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_csv_reader_with_colors() {
        let temp_file = "test_colors.csv";
        let content = "x,y,z,r,g,b\n1.0,2.0,3.0,255,0,0\n4.0,5.0,6.0,0,255,0\n";
        fs::write(temp_file, content).unwrap();
        
        let points = XyzCsvReader::read_detailed_points(temp_file).unwrap();
        assert_eq!(points.len(), 2);
        assert_eq!(points[0].position, Point3f::new(1.0, 2.0, 3.0));
        assert_eq!(points[0].color, Some([255, 0, 0]));
        assert_eq!(points[1].position, Point3f::new(4.0, 5.0, 6.0));
        assert_eq!(points[1].color, Some([0, 255, 0]));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_csv_reader_with_normals() {
        let temp_file = "test_normals.csv";
        let content = "x,y,z,nx,ny,nz\n1.0,2.0,3.0,0.0,0.0,1.0\n4.0,5.0,6.0,0.0,1.0,0.0\n";
        fs::write(temp_file, content).unwrap();
        
        let points = XyzCsvReader::read_detailed_points(temp_file).unwrap();
        assert_eq!(points.len(), 2);
        assert_eq!(points[0].position, Point3f::new(1.0, 2.0, 3.0));
        assert_eq!(points[0].normal, Some(Vector3f::new(0.0, 0.0, 1.0)));
        assert_eq!(points[1].position, Point3f::new(4.0, 5.0, 6.0));
        assert_eq!(points[1].normal, Some(Vector3f::new(0.0, 1.0, 0.0)));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_xyz_writer() {
        let temp_file = "test_write.xyz";
        let cloud = PointCloud::from_points(vec![
            Point3f::new(1.0, 2.0, 3.0),
            Point3f::new(4.0, 5.0, 6.0),
        ]);
        
        let options = XyzCsvWriteOptions::xyz();
        XyzCsvWriter::write_point_cloud(&cloud, temp_file, &options).unwrap();
        
        let content = fs::read_to_string(temp_file).unwrap();
        assert!(content.contains("1 2 3"));
        assert!(content.contains("4 5 6"));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_csv_writer_with_header() {
        let temp_file = "test_write_header.csv";
        let cloud = PointCloud::from_points(vec![
            Point3f::new(1.0, 2.0, 3.0),
            Point3f::new(4.0, 5.0, 6.0),
        ]);
        
        let options = XyzCsvWriteOptions::csv_with_header();
        XyzCsvWriter::write_point_cloud(&cloud, temp_file, &options).unwrap();
        
        let content = fs::read_to_string(temp_file).unwrap();
        assert!(content.starts_with("x,y,z"));
        assert!(content.contains("1,2,3"));
        assert!(content.contains("4,5,6"));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_detailed_points_writer() {
        let temp_file = "test_detailed.csv";
        let points = vec![
            XyzCsvPoint::with_color(Point3f::new(1.0, 2.0, 3.0), [255, 0, 0]),
            XyzCsvPoint::with_intensity(Point3f::new(4.0, 5.0, 6.0), 0.8),
        ];
        
        let options = XyzCsvWriteOptions::csv_complete();
        XyzCsvWriter::write_detailed_points(&points, temp_file, &options).unwrap();
        
        let content = fs::read_to_string(temp_file).unwrap();
        assert!(content.starts_with("x,y,z,intensity,r,g,b,nx,ny,nz"));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_schema_detection() {
        let temp_file = "test_schema.csv";
        let content = "x,y,z,intensity\n1.0,2.0,3.0,0.5\n4.0,5.0,6.0,0.8\n";
        fs::write(temp_file, content).unwrap();
        
        let schema = XyzCsvSchema::detect_from_file(temp_file).unwrap();
        assert_eq!(schema.delimiter, Delimiter::Comma);
        assert!(schema.has_header);
        assert!(schema.columns.contains(&ColumnType::X));
        assert!(schema.columns.contains(&ColumnType::Y));
        assert!(schema.columns.contains(&ColumnType::Z));
        assert!(schema.columns.contains(&ColumnType::Intensity));
        
        fs::remove_file(temp_file).unwrap();
    }
    
    #[test]
    fn test_error_handling() {
        // Test missing coordinates
        let temp_file = "test_error.xyz";
        let content = "1.0 2.0\n"; // Missing z coordinate
        fs::write(temp_file, content).unwrap();
        
        let result = XyzCsvReader::read_point_cloud(temp_file);
        assert!(result.is_err());
        
        let _ = fs::remove_file(temp_file);
    }
    
    #[test]
    fn test_registry_traits() {
        use crate::registry::{PointCloudReader, PointCloudWriter};
        
        let reader = XyzCsvReader;
        let writer = XyzCsvWriter;
        
        assert_eq!(reader.format_name(), "xyz_csv");
        assert_eq!(writer.format_name(), "xyz_csv");
        
        // Test can_read
        assert!(reader.can_read(Path::new("test.xyz")));
        assert!(reader.can_read(Path::new("test.csv")));
        assert!(reader.can_read(Path::new("test.txt")));
        assert!(!reader.can_read(Path::new("test.ply")));
    }
}