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
//! Unified IO registry for format-agnostic reading and writing
//! 
//! This module provides a registry-based approach to IO operations,
//! allowing downstream crates to work with any supported format
//! without knowing the specific implementation details.

use threecrate_core::{PointCloud, TriangleMesh, Result, Point3f};
use std::path::Path;
use std::collections::HashMap;

/// Trait for reading point clouds from files
pub trait PointCloudReader: Send + Sync {
    /// Read a point cloud from the given path
    fn read_point_cloud(&self, path: &Path) -> Result<PointCloud<Point3f>>;
    
    /// Check if this reader can handle the given file by examining its header
    fn can_read(&self, path: &Path) -> bool;
    
    /// Get the format name this reader handles
    fn format_name(&self) -> &'static str;
}

/// Trait for writing point clouds to files
pub trait PointCloudWriter: Send + Sync {
    /// Write a point cloud to the given path
    fn write_point_cloud(&self, cloud: &PointCloud<Point3f>, path: &Path) -> Result<()>;
    
    /// Get the format name this writer handles
    fn format_name(&self) -> &'static str;
}

/// Trait for reading meshes from files
pub trait MeshReader: Send + Sync {
    /// Read a mesh from the given path
    fn read_mesh(&self, path: &Path) -> Result<TriangleMesh>;
    
    /// Check if this reader can handle the given file by examining its header
    fn can_read(&self, path: &Path) -> bool;
    
    /// Get the format name this reader handles
    fn format_name(&self) -> &'static str;
}

/// Trait for writing meshes to files
pub trait MeshWriter: Send + Sync {
    /// Write a mesh to the given path
    fn write_mesh(&self, mesh: &TriangleMesh, path: &Path) -> Result<()>;
    
    /// Get the format name this writer handles
    fn format_name(&self) -> &'static str;
}

/// Format handler that can read and write both point clouds and meshes
pub trait FormatHandler: PointCloudReader + PointCloudWriter + MeshReader + MeshWriter + Send + Sync {
    /// Get the file extensions this handler supports
    fn supported_extensions(&self) -> &[&'static str];
    
    /// Get the magic bytes/header signature for format detection
    fn magic_bytes(&self) -> &[u8];
}

/// IO registry that manages format handlers and provides unified access
pub struct IoRegistry {
    point_cloud_readers: HashMap<String, Box<dyn PointCloudReader>>,
    point_cloud_writers: HashMap<String, Box<dyn PointCloudWriter>>,
    mesh_readers: HashMap<String, Box<dyn MeshReader>>,
    mesh_writers: HashMap<String, Box<dyn MeshWriter>>,
    #[allow(dead_code)] // Reserved for future use
    format_handlers: HashMap<String, Box<dyn FormatHandler>>,
}

impl IoRegistry {
    /// Create a new empty IO registry
    pub fn new() -> Self {
        Self {
            point_cloud_readers: HashMap::new(),
            point_cloud_writers: HashMap::new(),
            mesh_readers: HashMap::new(),
            mesh_writers: HashMap::new(),
            format_handlers: HashMap::new(),
        }
    }
    
    /// Register a point cloud reader for a specific format
    pub fn register_point_cloud_handler(&mut self, format: &str, handler: Box<dyn PointCloudReader>) {
        self.point_cloud_readers.insert(format.to_lowercase(), handler);
    }
    
    /// Register a point cloud writer for a specific format
    pub fn register_point_cloud_writer(&mut self, format: &str, handler: Box<dyn PointCloudWriter>) {
        self.point_cloud_writers.insert(format.to_lowercase(), handler);
    }
    
    /// Register a mesh reader for a specific format
    pub fn register_mesh_handler(&mut self, format: &str, handler: Box<dyn MeshReader>) {
        self.mesh_readers.insert(format.to_lowercase(), handler);
    }
    
    /// Register a mesh writer for a specific format
    pub fn register_mesh_writer(&mut self, format: &str, handler: Box<dyn MeshWriter>) {
        self.mesh_writers.insert(format.to_lowercase(), handler);
    }
    
    /// Register a complete format handler
    pub fn register_format_handler(&mut self, handler: Box<dyn FormatHandler>) {
        // For now, we'll skip the automatic registration of format handlers
        // since cloning trait objects is complex. Users can register each
        // capability separately using the specific register_* methods.
        let _handler = handler; // Suppress unused variable warning
    }
    
    /// Read a point cloud, auto-detecting the format
    pub fn read_point_cloud(&self, path: &Path, format_hint: &str) -> Result<PointCloud<Point3f>> {
        // First try the format hint
        if let Some(reader) = self.point_cloud_readers.get(&format_hint.to_lowercase()) {
            if reader.can_read(path) {
                return reader.read_point_cloud(path);
            }
        }
        
        // Try to detect format by header signature
        if let Some(detected_format) = self.detect_format_by_header(path) {
            if let Some(reader) = self.point_cloud_readers.get(&detected_format) {
                return reader.read_point_cloud(path);
            }
        }
        
        // Fall back to extension-based detection
        if let Some(reader) = self.point_cloud_readers.get(&format_hint.to_lowercase()) {
            return reader.read_point_cloud(path);
        }
        
        Err(threecrate_core::Error::UnsupportedFormat(
            format!("No point cloud reader found for format: {}", format_hint)
        ))
    }
    
    /// Read a mesh, auto-detecting the format
    pub fn read_mesh(&self, path: &Path, format_hint: &str) -> Result<TriangleMesh> {
        // First try the format hint
        if let Some(reader) = self.mesh_readers.get(&format_hint.to_lowercase()) {
            if reader.can_read(path) {
                return reader.read_mesh(path);
            }
        }
        
        // Try to detect format by header signature
        if let Some(detected_format) = self.detect_format_by_header(path) {
            if let Some(reader) = self.mesh_readers.get(&detected_format) {
                return reader.read_mesh(path);
            }
        }
        
        // Fall back to extension-based detection
        if let Some(reader) = self.mesh_readers.get(&format_hint.to_lowercase()) {
            return reader.read_mesh(path);
        }
        
        Err(threecrate_core::Error::UnsupportedFormat(
            format!("No mesh reader found for format: {}", format_hint)
        ))
    }
    
    /// Write a point cloud, auto-detecting the format
    pub fn write_point_cloud(&self, cloud: &PointCloud<Point3f>, path: &Path, format_hint: &str) -> Result<()> {
        if let Some(writer) = self.point_cloud_writers.get(&format_hint.to_lowercase()) {
            return writer.write_point_cloud(cloud, path);
        }
        
        Err(threecrate_core::Error::UnsupportedFormat(
            format!("No point cloud writer found for format: {}", format_hint)
        ))
    }
    
    /// Write a mesh, auto-detecting the format
    pub fn write_mesh(&self, mesh: &TriangleMesh, path: &Path, format_hint: &str) -> Result<()> {
        if let Some(writer) = self.mesh_writers.get(&format_hint.to_lowercase()) {
            return writer.write_mesh(mesh, path);
        }
        
        Err(threecrate_core::Error::UnsupportedFormat(
            format!("No mesh writer found for format: {}", format_hint)
        ))
    }
    
    /// Detect file format by examining the header/magic bytes
    fn detect_format_by_header(&self, path: &Path) -> Option<String> {
        use std::fs::File;
        use std::io::Read;
        
        let mut file = match File::open(path) {
            Ok(file) => file,
            Err(_) => return None,
        };
        
        let mut header = [0u8; 16];
        if let Ok(bytes_read) = file.read(&mut header) {
            if bytes_read < 4 {
                return None;
            }
            
            // Check magic bytes for various formats
            if header.starts_with(b"ply") {
                return Some("ply".to_string());
            } else if header.starts_with(b"#") || header.starts_with(b"v ") {
                // Check if it's an OBJ file by looking for vertex definitions
                let mut file = File::open(path).ok()?;
                let mut content = String::new();
                if file.read_to_string(&mut content).is_ok() {
                    if content.lines().any(|line| line.trim().starts_with("v ")) {
                        return Some("obj".to_string());
                    }
                }
            } else if header.starts_with(b"LASF") {
                return Some("las".to_string());
            } else if header.starts_with(b"# .PCD") {
                return Some("pcd".to_string());
            } else if header.starts_with(b"ASTM-E57") {
                return Some("e57".to_string());
            }
        }
        
        None
    }
    
    /// Get a list of supported formats for point clouds
    pub fn supported_point_cloud_formats(&self) -> Vec<String> {
        self.point_cloud_readers.keys().cloned().collect()
    }
    
    /// Get a list of supported formats for meshes
    pub fn supported_mesh_formats(&self) -> Vec<String> {
        self.mesh_readers.keys().cloned().collect()
    }
    
    /// Check if a format is supported for reading point clouds
    pub fn supports_point_cloud_reading(&self, format: &str) -> bool {
        self.point_cloud_readers.contains_key(&format.to_lowercase())
    }
    
    /// Check if a format is supported for writing point clouds
    pub fn supports_point_cloud_writing(&self, format: &str) -> bool {
        self.point_cloud_writers.contains_key(&format.to_lowercase())
    }
    
    /// Check if a format is supported for reading meshes
    pub fn supports_mesh_reading(&self, format: &str) -> bool {
        self.mesh_readers.contains_key(&format.to_lowercase())
    }
    
    /// Check if a format is supported for writing meshes
    pub fn supports_mesh_writing(&self, format: &str) -> bool {
        self.mesh_writers.contains_key(&format.to_lowercase())
    }
}

impl Default for IoRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// Note: FormatHandler cloning has been removed for simplicity.
// Users should register format handlers using the specific register_* methods.

#[cfg(test)]
mod tests {
    use super::*;
    use threecrate_core::Point3f;
    use std::fs;
    
    // Mock implementations for testing
    struct MockPlyHandler;
    
    impl PointCloudReader for MockPlyHandler {
        fn read_point_cloud(&self, _path: &Path) -> Result<PointCloud<Point3f>> {
            let mut cloud = PointCloud::new();
            cloud.push(Point3f::new(0.0, 0.0, 0.0));
            Ok(cloud)
        }
        
        fn can_read(&self, _path: &Path) -> bool {
            true
        }
        
        fn format_name(&self) -> &'static str {
            "ply"
        }
    }
    
    impl PointCloudWriter for MockPlyHandler {
        fn write_point_cloud(&self, _cloud: &PointCloud<Point3f>, _path: &Path) -> Result<()> {
            Ok(())
        }
        
        fn format_name(&self) -> &'static str {
            "ply"
        }
    }
    
    impl MeshReader for MockPlyHandler {
        fn read_mesh(&self, _path: &Path) -> Result<TriangleMesh> {
            let vertices = vec![Point3f::new(0.0, 0.0, 0.0)];
            let faces = vec![];
            Ok(TriangleMesh::from_vertices_and_faces(vertices, faces))
        }
        
        fn can_read(&self, _path: &Path) -> bool {
            true
        }
        
        fn format_name(&self) -> &'static str {
            "ply"
        }
    }
    
    impl MeshWriter for MockPlyHandler {
        fn write_mesh(&self, _mesh: &TriangleMesh, _path: &Path) -> Result<()> {
            Ok(())
        }
        
        fn format_name(&self) -> &'static str {
            "ply"
        }
    }
    
    impl FormatHandler for MockPlyHandler {
        fn supported_extensions(&self) -> &[&'static str] {
            &["ply"]
        }
        
        fn magic_bytes(&self) -> &[u8] {
            b"ply"
        }
    }
    
    impl Clone for MockPlyHandler {
        fn clone(&self) -> Self {
            Self
        }
    }
    
    #[test]
    fn test_registry_registration() {
        let mut registry = IoRegistry::new();
        
        // Register handlers
        registry.register_point_cloud_handler("ply", Box::new(MockPlyHandler));
        registry.register_mesh_handler("ply", Box::new(MockPlyHandler));
        registry.register_point_cloud_writer("ply", Box::new(MockPlyHandler));
        registry.register_mesh_writer("ply", Box::new(MockPlyHandler));
        
        // Check support
        assert!(registry.supports_point_cloud_reading("ply"));
        assert!(registry.supports_mesh_reading("ply"));
        assert!(registry.supports_point_cloud_writing("ply"));
        assert!(registry.supports_mesh_writing("ply"));
        
        // Check unsupported formats
        assert!(!registry.supports_point_cloud_reading("obj"));
        assert!(!registry.supports_mesh_reading("xyz"));
    }
    
    #[test]
    fn test_format_detection() {
        let mut registry = IoRegistry::new();
        registry.register_point_cloud_handler("ply", Box::new(MockPlyHandler));
        registry.register_mesh_handler("ply", Box::new(MockPlyHandler));
        
        // Create a test PLY file
        let temp_file = "test_detection.ply";
        let ply_content = "ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nend_header\n0.0 0.0 0.0\n";
        fs::write(temp_file, ply_content).unwrap();
        
        // Test reading with format detection
        let cloud = registry.read_point_cloud(Path::new(temp_file), "ply").unwrap();
        assert_eq!(cloud.len(), 1);
        
        let mesh = registry.read_mesh(Path::new(temp_file), "ply").unwrap();
        assert_eq!(mesh.vertex_count(), 1);
        
        // Cleanup
        let _ = fs::remove_file(temp_file);
    }
    
    #[test]
    fn test_unsupported_format() {
        let registry = IoRegistry::new();
        
        let result = registry.read_point_cloud(Path::new("test.xyz"), "xyz");
        assert!(result.is_err());
        
        let result = registry.read_mesh(Path::new("test.xyz"), "xyz");
        assert!(result.is_err());
    }
    
    #[test]
    fn test_supported_formats_list() {
        let mut registry = IoRegistry::new();
        registry.register_point_cloud_handler("ply", Box::new(MockPlyHandler));
        registry.register_mesh_handler("obj", Box::new(MockPlyHandler));
        
        let pc_formats = registry.supported_point_cloud_formats();
        let mesh_formats = registry.supported_mesh_formats();
        
        assert!(pc_formats.contains(&"ply".to_string()));
        assert!(mesh_formats.contains(&"obj".to_string()));
    }
}