Skip to main content

obj_convert/
obj_convert.rs

1use cjseq2::{conv::obj, CityJSON};
2use std::fs::File;
3use std::io::Read;
4use std::path::Path;
5
6fn main() -> std::io::Result<()> {
7    // Path to the sample CityJSON file
8    let file_path = Path::new("data/1b_w_texture.city.json");
9
10    // Ensure the file exists
11    if !file_path.exists() {
12        println!("Sample file not found: {:?}", file_path);
13        println!("Available files in data directory:");
14        for entry in std::fs::read_dir("data")? {
15            let entry = entry?;
16            println!("  {:?}", entry.path());
17        }
18        return Ok(());
19    }
20
21    // Read the CityJSON file
22    let mut file = File::open(file_path)?;
23    let mut contents = String::new();
24    file.read_to_string(&mut contents)?;
25
26    println!("Converting {} to OBJ format...", file_path.display());
27
28    // Parse into CityJSON
29    let city_json = CityJSON::from_str(&contents).unwrap();
30
31    // Output file path
32    let output_path = "output.obj";
33
34    // Convert to OBJ and save to file
35    obj::to_obj_file(&city_json, output_path)?;
36
37    println!("Conversion complete. OBJ file saved to: {}", output_path);
38
39    // Print some stats about the OBJ file
40    let metadata = std::fs::metadata(output_path)?;
41    println!("OBJ file size: {} bytes", metadata.len());
42
43    // Count number of vertices and faces in the OBJ file
44    let obj_contents = std::fs::read_to_string(output_path)?;
45    let vertex_count = obj_contents
46        .lines()
47        .filter(|line| line.starts_with("v "))
48        .count();
49    let face_count = obj_contents
50        .lines()
51        .filter(|line| line.starts_with("f "))
52        .count();
53
54    println!("OBJ statistics:");
55    println!("  Vertices: {}", vertex_count);
56    println!("  Faces: {}", face_count);
57
58    Ok(())
59}