vcad 0.1.0

Parametric CAD in Rust — CSG modeling with multi-format export
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
//! glTF/GLB export for visualization.
//!
//! Generates binary GLB files with PBR materials for web and app rendering.

use super::materials::Material;
use crate::{CadError, Part};
use std::path::Path;

/// Export a part to binary GLB format with PBR material.
pub fn export_glb(
    part: &Part,
    material: &Material,
    path: impl AsRef<Path>,
) -> Result<(), CadError> {
    let glb_data = to_glb_bytes(part, material)?;
    std::fs::write(path, glb_data)?;
    Ok(())
}

/// Convert a part to binary GLB bytes.
pub fn to_glb_bytes(part: &Part, material: &Material) -> Result<Vec<u8>, CadError> {
    let mesh = part.to_mesh();
    let vertices = mesh.vertices();
    let indices = mesh.indices();

    if vertices.is_empty() || indices.is_empty() {
        return Err(CadError::EmptyGeometry);
    }

    // Build the GLB using the gltf crate's JSON structures
    // GLB format: 12-byte header + JSON chunk + BIN chunk

    // Prepare vertex data (positions only for now)
    let vertex_count = vertices.len() / 3;
    let index_count = indices.len();

    // Calculate bounds for accessor
    let mut min = [f32::MAX; 3];
    let mut max = [f32::MIN; 3];
    for i in 0..vertex_count {
        let x = vertices[i * 3];
        let y = vertices[i * 3 + 1];
        let z = vertices[i * 3 + 2];
        min[0] = min[0].min(x);
        min[1] = min[1].min(y);
        min[2] = min[2].min(z);
        max[0] = max[0].max(x);
        max[1] = max[1].max(y);
        max[2] = max[2].max(z);
    }

    // Build binary buffer: indices (u32) + vertices (f32 * 3)
    let indices_byte_length = index_count * 4;
    let vertices_byte_length = vertex_count * 12;
    let total_buffer_length = indices_byte_length + vertices_byte_length;

    // Pad to 4-byte alignment
    let padded_buffer_length = (total_buffer_length + 3) & !3;

    let mut bin_buffer = Vec::with_capacity(padded_buffer_length);

    // Write indices as u32
    for &idx in &indices {
        bin_buffer.extend_from_slice(&idx.to_le_bytes());
    }

    // Write vertices as f32
    for &v in &vertices {
        bin_buffer.extend_from_slice(&v.to_le_bytes());
    }

    // Pad buffer
    while bin_buffer.len() < padded_buffer_length {
        bin_buffer.push(0);
    }

    // Build JSON
    let json = build_gltf_json(
        &part.name,
        material,
        vertex_count,
        index_count,
        indices_byte_length,
        vertices_byte_length,
        padded_buffer_length,
        &min,
        &max,
    );

    let json_bytes = json.as_bytes();
    let json_padded_length = (json_bytes.len() + 3) & !3;
    let mut json_chunk = json_bytes.to_vec();
    while json_chunk.len() < json_padded_length {
        json_chunk.push(b' '); // Pad with spaces
    }

    // Build GLB
    let total_length = 12 + 8 + json_padded_length + 8 + padded_buffer_length;
    let mut glb = Vec::with_capacity(total_length);

    // GLB header
    glb.extend_from_slice(b"glTF"); // magic
    glb.extend_from_slice(&2u32.to_le_bytes()); // version
    glb.extend_from_slice(&(total_length as u32).to_le_bytes()); // length

    // JSON chunk
    glb.extend_from_slice(&(json_padded_length as u32).to_le_bytes()); // chunk length
    glb.extend_from_slice(&0x4E4F534Au32.to_le_bytes()); // chunk type "JSON"
    glb.extend_from_slice(&json_chunk);

    // BIN chunk
    glb.extend_from_slice(&(padded_buffer_length as u32).to_le_bytes()); // chunk length
    glb.extend_from_slice(&0x004E4942u32.to_le_bytes()); // chunk type "BIN\0"
    glb.extend_from_slice(&bin_buffer);

    Ok(glb)
}

#[allow(clippy::too_many_arguments)]
fn build_gltf_json(
    name: &str,
    material: &Material,
    vertex_count: usize,
    index_count: usize,
    indices_byte_length: usize,
    vertices_byte_length: usize,
    buffer_length: usize,
    min: &[f32; 3],
    max: &[f32; 3],
) -> String {
    // Build JSON manually for control over output
    format!(
        r#"{{
  "asset": {{ "version": "2.0", "generator": "vcad" }},
  "scene": 0,
  "scenes": [{{ "nodes": [0] }}],
  "nodes": [{{ "mesh": 0, "name": "{name}" }}],
  "meshes": [{{
    "name": "{name}",
    "primitives": [{{
      "attributes": {{ "POSITION": 1 }},
      "indices": 0,
      "material": 0
    }}]
  }}],
  "materials": [{{
    "name": "{mat_name}",
    "pbrMetallicRoughness": {{
      "baseColorFactor": [{r}, {g}, {b}, 1.0],
      "metallicFactor": {metallic},
      "roughnessFactor": {roughness}
    }}
  }}],
  "accessors": [
    {{
      "bufferView": 0,
      "componentType": 5125,
      "count": {index_count},
      "type": "SCALAR"
    }},
    {{
      "bufferView": 1,
      "componentType": 5126,
      "count": {vertex_count},
      "type": "VEC3",
      "min": [{min0}, {min1}, {min2}],
      "max": [{max0}, {max1}, {max2}]
    }}
  ],
  "bufferViews": [
    {{
      "buffer": 0,
      "byteOffset": 0,
      "byteLength": {indices_byte_length},
      "target": 34963
    }},
    {{
      "buffer": 0,
      "byteOffset": {indices_byte_length},
      "byteLength": {vertices_byte_length},
      "target": 34962
    }}
  ],
  "buffers": [{{ "byteLength": {buffer_length} }}]
}}"#,
        name = name,
        mat_name = material.name,
        r = material.color[0],
        g = material.color[1],
        b = material.color[2],
        metallic = material.metallic,
        roughness = material.roughness,
        index_count = index_count,
        vertex_count = vertex_count,
        min0 = min[0],
        min1 = min[1],
        min2 = min[2],
        max0 = max[0],
        max1 = max[1],
        max2 = max[2],
        indices_byte_length = indices_byte_length,
        vertices_byte_length = vertices_byte_length,
        buffer_length = buffer_length,
    )
}

// =============================================================================
// Multi-material Scene export
// =============================================================================

use super::Materials;
use crate::Scene;

/// Export a scene with multiple parts and materials to GLB
pub fn export_scene_glb(
    scene: &Scene,
    materials_db: &Materials,
    path: impl AsRef<Path>,
) -> Result<(), CadError> {
    let glb_data = scene_to_glb_bytes(scene, materials_db)?;
    std::fs::write(path, glb_data)?;
    Ok(())
}

/// Convert a scene to GLB bytes with multiple meshes and materials
pub fn scene_to_glb_bytes(scene: &Scene, materials_db: &Materials) -> Result<Vec<u8>, CadError> {
    if scene.is_empty() {
        return Err(CadError::EmptyGeometry);
    }

    // Collect all unique materials used in the scene
    let mut material_indices: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    let mut materials_list: Vec<Material> = Vec::new();

    for node in &scene.nodes {
        if !material_indices.contains_key(&node.material_key) {
            let mat = materials_db.get_for_part_or_default(&node.material_key);
            material_indices.insert(node.material_key.clone(), materials_list.len());
            materials_list.push(mat);
        }
    }

    // Build binary buffer for all meshes
    let mut bin_buffer: Vec<u8> = Vec::new();
    let mut buffer_views: Vec<String> = Vec::new();
    let mut accessors: Vec<String> = Vec::new();
    let mut meshes: Vec<String> = Vec::new();
    let mut nodes: Vec<String> = Vec::new();

    let mut accessor_idx = 0;
    let mut buffer_view_idx = 0;

    for (mesh_idx, node) in scene.nodes.iter().enumerate() {
        let mesh = node.part.to_mesh();
        let vertices = mesh.vertices();
        let indices = mesh.indices();

        if vertices.is_empty() || indices.is_empty() {
            continue;
        }

        let vertex_count = vertices.len() / 3;
        let index_count = indices.len();

        // Calculate bounds
        let mut min = [f32::MAX; 3];
        let mut max = [f32::MIN; 3];
        for i in 0..vertex_count {
            let x = vertices[i * 3];
            let y = vertices[i * 3 + 1];
            let z = vertices[i * 3 + 2];
            min[0] = min[0].min(x);
            min[1] = min[1].min(y);
            min[2] = min[2].min(z);
            max[0] = max[0].max(x);
            max[1] = max[1].max(y);
            max[2] = max[2].max(z);
        }

        // Record byte offsets
        let indices_byte_offset = bin_buffer.len();
        let indices_byte_length = index_count * 4;

        // Write indices
        for &idx in &indices {
            bin_buffer.extend_from_slice(&idx.to_le_bytes());
        }

        // Pad to 4-byte alignment
        while !bin_buffer.len().is_multiple_of(4) {
            bin_buffer.push(0);
        }

        let vertices_byte_offset = bin_buffer.len();
        let vertices_byte_length = vertex_count * 12;

        // Write vertices
        for &v in &vertices {
            bin_buffer.extend_from_slice(&v.to_le_bytes());
        }

        // Pad to 4-byte alignment
        while !bin_buffer.len().is_multiple_of(4) {
            bin_buffer.push(0);
        }

        // Buffer views for this mesh
        buffer_views.push(format!(
            r#"{{ "buffer": 0, "byteOffset": {}, "byteLength": {}, "target": 34963 }}"#,
            indices_byte_offset, indices_byte_length
        ));
        let indices_bv = buffer_view_idx;
        buffer_view_idx += 1;

        buffer_views.push(format!(
            r#"{{ "buffer": 0, "byteOffset": {}, "byteLength": {}, "target": 34962 }}"#,
            vertices_byte_offset, vertices_byte_length
        ));
        let vertices_bv = buffer_view_idx;
        buffer_view_idx += 1;

        // Accessors for this mesh
        accessors.push(format!(
            r#"{{ "bufferView": {}, "componentType": 5125, "count": {}, "type": "SCALAR" }}"#,
            indices_bv, index_count
        ));
        let indices_acc = accessor_idx;
        accessor_idx += 1;

        accessors.push(format!(
            r#"{{ "bufferView": {}, "componentType": 5126, "count": {}, "type": "VEC3", "min": [{}, {}, {}], "max": [{}, {}, {}] }}"#,
            vertices_bv, vertex_count,
            min[0], min[1], min[2],
            max[0], max[1], max[2]
        ));
        let vertices_acc = accessor_idx;
        accessor_idx += 1;

        // Get material index for this node
        let mat_idx = material_indices
            .get(&node.material_key)
            .copied()
            .unwrap_or(0);

        // Mesh
        meshes.push(format!(
            r#"{{ "name": "{}", "primitives": [{{ "attributes": {{ "POSITION": {} }}, "indices": {}, "material": {} }}] }}"#,
            node.part.name, vertices_acc, indices_acc, mat_idx
        ));

        // Node
        nodes.push(format!(
            r#"{{ "mesh": {}, "name": "{}" }}"#,
            mesh_idx, node.part.name
        ));
    }

    if meshes.is_empty() {
        return Err(CadError::EmptyGeometry);
    }

    // Build materials JSON
    let materials_json: Vec<String> = materials_list.iter().map(|m| {
        format!(
            r#"{{ "name": "{}", "pbrMetallicRoughness": {{ "baseColorFactor": [{}, {}, {}, 1.0], "metallicFactor": {}, "roughnessFactor": {} }} }}"#,
            m.name, m.color[0], m.color[1], m.color[2], m.metallic, m.roughness
        )
    }).collect();

    // Build node indices for scene
    let node_indices: Vec<String> = (0..nodes.len()).map(|i| i.to_string()).collect();

    // Build JSON
    let json = format!(
        r#"{{
  "asset": {{ "version": "2.0", "generator": "vcad" }},
  "scene": 0,
  "scenes": [{{ "name": "{}", "nodes": [{}] }}],
  "nodes": [{}],
  "meshes": [{}],
  "materials": [{}],
  "accessors": [{}],
  "bufferViews": [{}],
  "buffers": [{{ "byteLength": {} }}]
}}"#,
        scene.name,
        node_indices.join(", "),
        nodes.join(",\n    "),
        meshes.join(",\n    "),
        materials_json.join(",\n    "),
        accessors.join(",\n    "),
        buffer_views.join(",\n    "),
        bin_buffer.len()
    );

    let json_bytes = json.as_bytes();
    let json_padded_length = (json_bytes.len() + 3) & !3;
    let mut json_chunk = json_bytes.to_vec();
    while json_chunk.len() < json_padded_length {
        json_chunk.push(b' ');
    }

    let bin_padded_length = (bin_buffer.len() + 3) & !3;
    while bin_buffer.len() < bin_padded_length {
        bin_buffer.push(0);
    }

    // Build GLB
    let total_length = 12 + 8 + json_padded_length + 8 + bin_padded_length;
    let mut glb = Vec::with_capacity(total_length);

    // GLB header
    glb.extend_from_slice(b"glTF");
    glb.extend_from_slice(&2u32.to_le_bytes());
    glb.extend_from_slice(&(total_length as u32).to_le_bytes());

    // JSON chunk
    glb.extend_from_slice(&(json_padded_length as u32).to_le_bytes());
    glb.extend_from_slice(&0x4E4F534Au32.to_le_bytes());
    glb.extend_from_slice(&json_chunk);

    // BIN chunk
    glb.extend_from_slice(&(bin_padded_length as u32).to_le_bytes());
    glb.extend_from_slice(&0x004E4942u32.to_le_bytes());
    glb.extend_from_slice(&bin_buffer);

    Ok(glb)
}

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

    #[test]
    fn test_glb_export() {
        let cube = Part::cube("test_cube", 10.0, 10.0, 10.0);
        let material = Material::default();
        let glb_data = to_glb_bytes(&cube, &material).unwrap();

        // Check GLB magic
        assert_eq!(&glb_data[0..4], b"glTF");

        // Check version
        let version = u32::from_le_bytes([glb_data[4], glb_data[5], glb_data[6], glb_data[7]]);
        assert_eq!(version, 2);
    }

    #[test]
    fn test_scene_glb_export() {
        let mut scene = Scene::new("test_scene");
        scene.add(Part::cube("cube1", 10.0, 10.0, 10.0), "aluminum_6061");
        scene.add(
            Part::cube("cube2", 5.0, 5.0, 5.0).translate(20.0, 0.0, 0.0),
            "aluminum_powder_orange",
        );

        let materials = Materials::parse(
            r#"
            [materials.aluminum_6061]
            color = [0.85, 0.85, 0.88]
            metallic = 0.95
            roughness = 0.35
            [materials.aluminum_powder_orange]
            color = [1.0, 0.4, 0.0]
            metallic = 0.3
            roughness = 0.6
        "#,
        )
        .unwrap();

        let glb_data = scene_to_glb_bytes(&scene, &materials).unwrap();

        // Check GLB magic
        assert_eq!(&glb_data[0..4], b"glTF");
    }
}