alice-sdf 1.9.2

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Bridges to alice-codec/physics/asp/cache/font are available via GitHub path dep only in 1.7.7 (scheduled restoration in 1.8.0 once transitive deps publish).
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
//! Wavefront OBJ export for ALICE-SDF meshes
//!
//! Exports meshes to standard .obj format with optional .mtl material files.
//! Compatible with Blender, Maya, 3ds Max, and all major DCC tools.
//!
//! Author: Moroya Sakamoto

use crate::io::IoError;
use crate::material::MaterialLibrary;
use crate::mesh::Mesh;
use std::io::Write;
use std::path::Path;

/// OBJ export configuration
#[derive(Debug, Clone)]
pub struct ObjConfig {
    /// Export normals (vn)
    pub export_normals: bool,
    /// Export UVs (vt)
    pub export_uvs: bool,
    /// Export materials (.mtl)
    pub export_materials: bool,
    /// Flip UV V coordinate (some engines use 1-v)
    pub flip_uv_v: bool,
}

impl Default for ObjConfig {
    fn default() -> Self {
        Self {
            export_normals: true,
            export_uvs: true,
            export_materials: true,
            flip_uv_v: false,
        }
    }
}

/// Export a mesh to Wavefront OBJ format
pub fn export_obj(
    mesh: &Mesh,
    path: impl AsRef<Path>,
    config: &ObjConfig,
    materials: Option<&MaterialLibrary>,
) -> Result<(), IoError> {
    let path = path.as_ref();
    let file = std::fs::File::create(path)?;
    let mut w = std::io::BufWriter::new(file);

    let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("mesh");

    // Header
    writeln!(w, "# ALICE-SDF OBJ Export")?;
    writeln!(w, "# Vertices: {}", mesh.vertices.len())?;
    writeln!(w, "# Triangles: {}", mesh.indices.len() / 3)?;

    // MTL reference
    if config.export_materials && materials.is_some() {
        writeln!(w, "mtllib {}.mtl", stem)?;
    }

    writeln!(w, "o {}", stem)?;

    // Positions
    for v in &mesh.vertices {
        writeln!(w, "v {} {} {}", v.position.x, v.position.y, v.position.z)?;
    }

    // UVs
    if config.export_uvs {
        for v in &mesh.vertices {
            let v_coord = if config.flip_uv_v {
                1.0 - v.uv.y
            } else {
                v.uv.y
            };
            writeln!(w, "vt {} {}", v.uv.x, v_coord)?;
        }
    }

    // Normals
    if config.export_normals {
        for v in &mesh.vertices {
            writeln!(w, "vn {} {} {}", v.normal.x, v.normal.y, v.normal.z)?;
        }
    }

    // Faces - group by material_id
    let tri_count = mesh.indices.len() / 3;

    if let Some(mat_lib) = materials.filter(|_| config.export_materials) {
        // Collect triangles by material
        let mut mat_tris: std::collections::BTreeMap<u32, Vec<usize>> =
            std::collections::BTreeMap::new();
        for i in 0..tri_count {
            let mat_id = mesh.vertices[mesh.indices[i * 3] as usize].material_id;
            mat_tris.entry(mat_id).or_default().push(i);
        }

        for (mat_id, tris) in &mat_tris {
            let mat_name = if (*mat_id as usize) < mat_lib.materials.len() {
                &mat_lib.materials[*mat_id as usize].name
            } else {
                "default"
            };
            writeln!(w, "usemtl {}", mat_name)?;

            for &i in tris {
                write_face(&mut w, mesh, i, config)?;
            }
        }
    } else {
        for i in 0..tri_count {
            write_face(&mut w, mesh, i, config)?;
        }
    }

    w.flush()?;

    // Export MTL file
    if config.export_materials {
        if let Some(mat_lib) = materials {
            let mtl_path = path.with_extension("mtl");
            export_mtl(mat_lib, &mtl_path)?;
        }
    }

    Ok(())
}

fn write_face(
    w: &mut impl Write,
    mesh: &Mesh,
    tri_idx: usize,
    config: &ObjConfig,
) -> Result<(), IoError> {
    let base = tri_idx * 3;
    let a = mesh.indices[base] as usize + 1; // OBJ is 1-indexed
    let b = mesh.indices[base + 1] as usize + 1;
    let c = mesh.indices[base + 2] as usize + 1;

    if config.export_normals && config.export_uvs {
        writeln!(w, "f {}/{}/{} {}/{}/{} {}/{}/{}", a, a, a, b, b, b, c, c, c)?;
    } else if config.export_normals {
        writeln!(w, "f {}//{} {}//{} {}//{}", a, a, b, b, c, c)?;
    } else if config.export_uvs {
        writeln!(w, "f {}/{} {}/{} {}/{}", a, a, b, b, c, c)?;
    } else {
        writeln!(w, "f {} {} {}", a, b, c)?;
    }

    Ok(())
}

/// Export materials to MTL format
fn export_mtl(mat_lib: &MaterialLibrary, path: impl AsRef<Path>) -> Result<(), IoError> {
    let file = std::fs::File::create(path)?;
    let mut w = std::io::BufWriter::new(file);

    writeln!(w, "# ALICE-SDF MTL Export")?;

    for mat in &mat_lib.materials {
        writeln!(w, "\nnewmtl {}", mat.name)?;

        // Diffuse (Kd)
        writeln!(
            w,
            "Kd {} {} {}",
            mat.base_color[0], mat.base_color[1], mat.base_color[2]
        )?;

        // Ambient (Ka) - use diffuse * 0.1
        writeln!(
            w,
            "Ka {} {} {}",
            mat.base_color[0] * 0.1,
            mat.base_color[1] * 0.1,
            mat.base_color[2] * 0.1
        )?;

        // Specular (Ks) - from metallic
        let spec = mat.metallic;
        writeln!(w, "Ks {} {} {}", spec, spec, spec)?;

        // Specular exponent (Ns) - from roughness
        let ns = (1.0 - mat.roughness).mul_add(900.0, 10.0);
        writeln!(w, "Ns {}", ns)?;

        // Opacity
        writeln!(w, "d {}", mat.opacity)?;

        // Illumination model
        if mat.metallic > 0.5 {
            writeln!(w, "illum 3")?; // Metallic
        } else {
            writeln!(w, "illum 2")?; // Dielectric
        }

        // Emission (Ke) - PBR extension
        if mat.emission_strength > 0.0 {
            writeln!(
                w,
                "Ke {} {} {}",
                mat.emission[0] * mat.emission_strength,
                mat.emission[1] * mat.emission_strength,
                mat.emission[2] * mat.emission_strength,
            )?;
        }

        // IOR (Ni)
        writeln!(w, "Ni {}", mat.ior)?;

        // Texture maps
        if let Some(ref tex) = mat.albedo_map {
            writeln!(w, "map_Kd {}", tex.path)?;
        }
        if let Some(ref tex) = mat.normal_map {
            writeln!(w, "bump {}", tex.path)?;
        }
        if let Some(ref tex) = mat.metallic_map {
            writeln!(w, "map_Ks {}", tex.path)?;
        }
        if let Some(ref tex) = mat.roughness_map {
            writeln!(w, "map_Ns {}", tex.path)?;
        }
        if let Some(ref tex) = mat.ao_map {
            writeln!(w, "map_Ka {}", tex.path)?;
        }
        if let Some(ref tex) = mat.emissive_map {
            writeln!(w, "map_Ke {}", tex.path)?;
        }
    }

    w.flush()?;
    Ok(())
}

/// Load a mesh from OBJ format
pub fn import_obj(path: impl AsRef<Path>) -> Result<Mesh, IoError> {
    use crate::mesh::Vertex;
    use glam::{Vec2, Vec3};

    let content = std::fs::read_to_string(path).map_err(IoError::Io)?;

    let mut positions: Vec<Vec3> = Vec::new();
    let mut normals: Vec<Vec3> = Vec::new();
    let mut uvs: Vec<Vec2> = Vec::new();

    let mut vertices: Vec<Vertex> = Vec::new();
    let mut indices: Vec<u32> = Vec::new();

    // Map (pos_idx, uv_idx, norm_idx) -> vertex index for deduplication
    let mut vertex_map: std::collections::HashMap<(usize, usize, usize), u32> =
        std::collections::HashMap::new();

    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        match parts[0] {
            "v" if parts.len() >= 4 => {
                let x: f32 = parts[1].parse().unwrap_or(0.0);
                let y: f32 = parts[2].parse().unwrap_or(0.0);
                let z: f32 = parts[3].parse().unwrap_or(0.0);
                positions.push(Vec3::new(x, y, z));
            }
            "vn" if parts.len() >= 4 => {
                let x: f32 = parts[1].parse().unwrap_or(0.0);
                let y: f32 = parts[2].parse().unwrap_or(0.0);
                let z: f32 = parts[3].parse().unwrap_or(0.0);
                normals.push(Vec3::new(x, y, z).normalize());
            }
            "vt" if parts.len() >= 3 => {
                let u: f32 = parts[1].parse().unwrap_or(0.0);
                let v: f32 = parts[2].parse().unwrap_or(0.0);
                uvs.push(Vec2::new(u, v));
            }
            "f" if parts.len() >= 4 => {
                // Triangulate face (fan triangulation for n-gons)
                let face_verts: Vec<u32> = parts[1..]
                    .iter()
                    .map(|&s| {
                        let (pi, ui, ni) = parse_face_vertex(s);
                        let key = (pi, ui, ni);

                        if let Some(&idx) = vertex_map.get(&key) {
                            idx
                        } else {
                            let pos = if pi > 0 && pi <= positions.len() {
                                positions[pi - 1]
                            } else {
                                Vec3::ZERO
                            };
                            let norm = if ni > 0 && ni <= normals.len() {
                                normals[ni - 1]
                            } else {
                                Vec3::Y
                            };
                            let uv = if ui > 0 && ui <= uvs.len() {
                                uvs[ui - 1]
                            } else {
                                Vec2::ZERO
                            };

                            let idx = vertices.len() as u32;
                            let mut vert = Vertex::new(pos, norm);
                            vert.uv = uv;
                            vertices.push(vert);
                            vertex_map.insert(key, idx);
                            idx
                        }
                    })
                    .collect();

                // Fan triangulation
                for i in 1..face_verts.len().saturating_sub(1) {
                    indices.push(face_verts[0]);
                    indices.push(face_verts[i]);
                    indices.push(face_verts[i + 1]);
                }
            }
            _ => {}
        }
    }

    Ok(Mesh { vertices, indices })
}

/// Parse OBJ face vertex: v, v/vt, v/vt/vn, v//vn
fn parse_face_vertex(s: &str) -> (usize, usize, usize) {
    let parts: Vec<&str> = s.split('/').collect();
    let v = parts.first().and_then(|s| s.parse().ok()).unwrap_or(0);
    let vt = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
    let vn = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
    (v, vt, vn)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mesh::{sdf_to_mesh, MarchingCubesConfig};
    use crate::types::SdfNode;
    use glam::Vec3;

    #[test]
    fn test_obj_export_import_roundtrip() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 8,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let path = std::env::temp_dir().join("alice_test_export.obj");
        export_obj(&mesh, &path, &ObjConfig::default(), None).unwrap();

        let imported = import_obj(&path).unwrap();
        assert!(imported.vertex_count() > 0);
        assert!(imported.triangle_count() > 0);

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn test_obj_with_materials() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 8,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let mut mat_lib = MaterialLibrary::new();
        mat_lib.add(crate::material::Material::metal(
            "gold", 1.0, 0.84, 0.0, 0.3,
        ));

        let path = std::env::temp_dir().join("alice_test_mat.obj");
        export_obj(&mesh, &path, &ObjConfig::default(), Some(&mat_lib)).unwrap();

        let mtl_path = path.with_extension("mtl");
        assert!(mtl_path.exists());

        std::fs::remove_file(&path).ok();
        std::fs::remove_file(&mtl_path).ok();
    }

    #[test]
    fn test_obj_minimal() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 4,
            iso_level: 0.0,
            compute_normals: false,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let path = std::env::temp_dir().join("alice_test_minimal.obj");
        let obj_config = ObjConfig {
            export_normals: false,
            export_uvs: false,
            export_materials: false,
            flip_uv_v: false,
        };
        export_obj(&mesh, &path, &obj_config, None).unwrap();

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("f "));
        assert!(!content.contains("vn "));
        assert!(!content.contains("vt "));

        std::fs::remove_file(&path).ok();
    }
}