draco-core 1.0.2

Pure Rust core encoder and decoder for Draco geometry compression
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
use draco_core::draco_types::DataType;
use draco_core::encoder_buffer::EncoderBuffer;
use draco_core::geometry_attribute::{GeometryAttributeType, PointAttribute};
use draco_core::geometry_indices::{FaceIndex, PointIndex};
/// Tests for encoder options compatibility between Rust and C++ implementations.
/// This tests different quantization bits and compression levels.
use draco_core::mesh::Mesh;
use draco_core::mesh_encoder::MeshEncoder;
use draco_core::EncoderOptions;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;

static OUTPUT_LOCK: Mutex<()> = Mutex::new(());

fn print_size_table_header(title: &str) {
    println!("\n=== {title} ===");
    println!(
        "{:<28} {:>11} {:>11} {:>10}",
        "Case", "C++ bytes", "Rust bytes", "Status"
    );
    println!("{}", "-".repeat(66));
}

fn print_size_row(case: impl std::fmt::Display, cpp_size: usize, rust_size: usize, status: &str) {
    println!(
        "{:<28} {:>11} {:>11} {:>10}",
        case, cpp_size, rust_size, status
    );
}

fn print_case_status(case: impl std::fmt::Display, status: &str) {
    println!("{:<28} {:>11} {:>11} {:>10}", case, "-", "-", status);
}

fn get_cpp_tools_path() -> Option<PathBuf> {
    let path = Path::new("../../build-original/src/draco/Release");
    if path.exists() {
        Some(path.to_path_buf())
    } else {
        None
    }
}

/// Create a mesh with position, normals, and texture coordinates
/// Uses 50x50 grid to match the original encoding-speed compatibility test
fn create_mesh_with_attributes() -> Mesh {
    let mut mesh = Mesh::new();
    let grid_size = 50; // Must match compat_encoding_speed.rs for consistent results
    let num_points = grid_size * grid_size;
    let num_faces = (grid_size - 1) * (grid_size - 1) * 2;

    mesh.set_num_points(num_points);
    mesh.set_num_faces(num_faces);

    // Position attribute
    let mut pos_attr = PointAttribute::new();
    pos_attr.init(
        GeometryAttributeType::Position,
        3,
        DataType::Float32,
        false,
        num_points as usize,
    );

    for y in 0..grid_size {
        for x in 0..grid_size {
            let index = y * grid_size + x;
            let px = x as f32;
            let py = y as f32;
            // Use same sinusoidal variation as compat_encoding_speed.rs (0.2 not 0.3)
            let pz = (x as f32 * 0.2).sin() * (y as f32 * 0.2).cos() * 2.0;

            let offset = index as usize * 3 * 4;
            pos_attr
                .buffer_mut()
                .update(&px.to_le_bytes(), Some(offset));
            pos_attr
                .buffer_mut()
                .update(&py.to_le_bytes(), Some(offset + 4));
            pos_attr
                .buffer_mut()
                .update(&pz.to_le_bytes(), Some(offset + 8));
        }
    }
    mesh.add_attribute(pos_attr);

    // Set faces
    let mut face_idx = 0;
    for y in 0..grid_size - 1 {
        for x in 0..grid_size - 1 {
            let p0 = y * grid_size + x;
            let p1 = y * grid_size + x + 1;
            let p2 = (y + 1) * grid_size + x;
            let p3 = (y + 1) * grid_size + x + 1;

            mesh.set_face(
                FaceIndex(face_idx),
                [
                    PointIndex(p0 as u32),
                    PointIndex(p1 as u32),
                    PointIndex(p2 as u32),
                ],
            );
            face_idx += 1;
            mesh.set_face(
                FaceIndex(face_idx),
                [
                    PointIndex(p1 as u32),
                    PointIndex(p3 as u32),
                    PointIndex(p2 as u32),
                ],
            );
            face_idx += 1;
        }
    }

    mesh
}

/// Test different quantization bits for position attribute
#[test]
fn test_quantization_bits_compatibility() {
    let _output_lock = OUTPUT_LOCK.lock().unwrap();
    let tools_path = match get_cpp_tools_path() {
        Some(p) => p,
        None => {
            eprintln!("SKIPPING: C++ tools not found");
            return;
        }
    };
    let encoder_path = tools_path.join("draco_encoder.exe");
    let decoder_path = tools_path.join("draco_decoder.exe");

    if !encoder_path.exists() || !decoder_path.exists() {
        eprintln!("SKIPPING: C++ encoder/decoder not found");
        return;
    }

    let mesh = create_mesh_with_attributes();
    let obj_path = Path::new("temp_qbits_test.obj");
    if obj_path.exists() {
        let _ = std::fs::remove_file(obj_path);
    }
    draco_io::obj_writer::write_obj_mesh(obj_path, &mesh).expect("Failed to write obj");

    // Read back to match OBJ precision
    let mut obj_reader = draco_io::ObjReader::open(obj_path).expect("Failed to open obj");
    let mesh = obj_reader.read_mesh().expect("Failed to read obj");

    // Test different quantization bits: 8, 10, 11 (default), 12, 14, 16
    let quantization_values = [8, 10, 11, 12, 14, 16];

    print_size_table_header("C++ vs Rust Quantization Bits Compatibility");

    let mut all_passed = true;

    for qp in quantization_values {
        // Use a fixed speed for this test
        let speed = 5;
        let cpp_cl = 10 - speed;

        // Rust encode
        let mut options = EncoderOptions::new();
        options.set_global_int("encoding_speed", speed);
        options.set_global_int("decoding_speed", speed);
        options.set_attribute_int(0, "quantization_bits", qp);

        let mut encoder = MeshEncoder::new();
        encoder.set_mesh(mesh.clone());
        let mut buffer = EncoderBuffer::new();
        let res = encoder.encode(&options, &mut buffer);
        assert!(res.is_ok(), "Rust encode failed with qp={}", qp);
        let rust_data = buffer.data().to_vec();

        // C++ encode
        let cpp_drc_path = format!("temp_cpp_qp{}.drc", qp);
        let output = Command::new(&encoder_path)
            .arg("-i")
            .arg(obj_path)
            .arg("-o")
            .arg(&cpp_drc_path)
            .arg("-method")
            .arg("edgebreaker")
            .arg("-cl")
            .arg(cpp_cl.to_string())
            .arg("-qp")
            .arg(qp.to_string())
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                let mut file = File::open(&cpp_drc_path).expect("Failed to open cpp drc");
                let mut cpp_data = Vec::new();
                file.read_to_end(&mut cpp_data)
                    .expect("Failed to read cpp drc");

                let status = if rust_data == cpp_data {
                    "MATCH"
                } else {
                    "MISMATCH"
                };
                if rust_data != cpp_data {
                    all_passed = false;
                }
                print_size_row(
                    format!("qp={qp:2}"),
                    cpp_data.len(),
                    rust_data.len(),
                    status,
                );

                let _ = std::fs::remove_file(&cpp_drc_path);
            } else {
                eprintln!(
                    "C++ encoder failed for qp={}: {}",
                    qp,
                    String::from_utf8_lossy(&output.stderr)
                );
                all_passed = false;
            }
        }

        // Verify C++ can decode Rust output
        let rust_drc_path = format!("temp_rust_qp{}.drc", qp);
        std::fs::write(&rust_drc_path, &rust_data).expect("write failed");
        let decode_result = Command::new(&decoder_path)
            .arg("-i")
            .arg(&rust_drc_path)
            .output();
        if let Ok(output) = decode_result {
            if !output.status.success() {
                eprintln!("C++ decoder failed for Rust output qp={}", qp);
                all_passed = false;
            }
        }
        let _ = std::fs::remove_file(&rust_drc_path);
    }

    let _ = std::fs::remove_file(obj_path);

    assert!(
        all_passed,
        "Some quantization tests failed - see output above"
    );
}

/// Test all compression levels (0-10) at a specific quantization
#[test]
fn test_compression_levels_compatibility() {
    let _output_lock = OUTPUT_LOCK.lock().unwrap();
    let tools_path = match get_cpp_tools_path() {
        Some(p) => p,
        None => {
            eprintln!("SKIPPING: C++ tools not found");
            return;
        }
    };
    let encoder_path = tools_path.join("draco_encoder.exe");
    let decoder_path = tools_path.join("draco_decoder.exe");

    if !encoder_path.exists() || !decoder_path.exists() {
        eprintln!("SKIPPING: C++ encoder/decoder not found");
        return;
    }

    let mesh = create_mesh_with_attributes();
    let obj_path = Path::new("temp_cl_test.obj");
    if obj_path.exists() {
        let _ = std::fs::remove_file(obj_path);
    }
    draco_io::obj_writer::write_obj_mesh(obj_path, &mesh).expect("Failed to write obj");

    let mut obj_reader = draco_io::ObjReader::open(obj_path).expect("Failed to open obj");
    let mesh = obj_reader.read_mesh().expect("Failed to read obj");

    print_size_table_header("C++ vs Rust Compression Levels Compatibility");
    println!("Note: Rust speed = 10 - cpp_cl\n");

    let mut all_passed = true;

    // Test compression levels 0-10
    for cl in 0..=10 {
        let speed = 10 - cl; // Rust speed is inverse of C++ compression level
        let qp = 10; // Match original encoding-speed compatibility test

        // Rust encode
        let mut options = EncoderOptions::new();
        options.set_global_int("encoding_speed", speed);
        options.set_global_int("decoding_speed", speed);
        options.set_attribute_int(0, "quantization_bits", qp);

        let mut encoder = MeshEncoder::new();
        encoder.set_mesh(mesh.clone());
        let mut buffer = EncoderBuffer::new();
        let res = encoder.encode(&options, &mut buffer);
        assert!(
            res.is_ok(),
            "Rust encode failed with cl={} (speed={})",
            cl,
            speed
        );
        let rust_data = buffer.data().to_vec();

        // C++ encode
        let cpp_drc_path = format!("temp_cpp_cl{}.drc", cl);
        let output = Command::new(&encoder_path)
            .arg("-i")
            .arg(obj_path)
            .arg("-o")
            .arg(&cpp_drc_path)
            .arg("-method")
            .arg("edgebreaker")
            .arg("-cl")
            .arg(cl.to_string())
            .arg("-qp")
            .arg(qp.to_string())
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                let mut file = File::open(&cpp_drc_path).expect("Failed to open cpp drc");
                let mut cpp_data = Vec::new();
                file.read_to_end(&mut cpp_data)
                    .expect("Failed to read cpp drc");

                let status = if rust_data == cpp_data {
                    "MATCH"
                } else {
                    "MISMATCH"
                };
                if rust_data != cpp_data {
                    all_passed = false;
                }
                print_size_row(
                    format!("cl={cl:2} speed={speed:2}"),
                    cpp_data.len(),
                    rust_data.len(),
                    status,
                );

                let _ = std::fs::remove_file(&cpp_drc_path);
            } else {
                eprintln!(
                    "C++ encoder failed for cl={}: {}",
                    cl,
                    String::from_utf8_lossy(&output.stderr)
                );
                all_passed = false;
            }
        }

        // Verify C++ can decode Rust output
        let rust_drc_path = format!("temp_rust_cl{}.drc", cl);
        std::fs::write(&rust_drc_path, &rust_data).expect("write failed");
        let decode_result = Command::new(&decoder_path)
            .arg("-i")
            .arg(&rust_drc_path)
            .output();
        if let Ok(output) = decode_result {
            if !output.status.success() {
                eprintln!("C++ decoder failed for Rust output cl={}", cl);
                all_passed = false;
            }
        }
        let _ = std::fs::remove_file(&rust_drc_path);
    }

    let _ = std::fs::remove_file(obj_path);

    assert!(
        all_passed,
        "Some compression level tests failed - see output above"
    );
}

/// Test edge cases for quantization bits (minimum and maximum values)
#[test]
fn test_quantization_edge_cases() {
    let _output_lock = OUTPUT_LOCK.lock().unwrap();
    let tools_path = match get_cpp_tools_path() {
        Some(p) => p,
        None => {
            eprintln!("SKIPPING: C++ tools not found");
            return;
        }
    };
    let encoder_path = tools_path.join("draco_encoder.exe");
    let decoder_path = tools_path.join("draco_decoder.exe");

    if !encoder_path.exists() || !decoder_path.exists() {
        eprintln!("SKIPPING: C++ encoder/decoder not found");
        return;
    }

    let mesh = create_mesh_with_attributes();
    let obj_path = Path::new("temp_qedge_test.obj");
    if obj_path.exists() {
        let _ = std::fs::remove_file(obj_path);
    }
    draco_io::obj_writer::write_obj_mesh(obj_path, &mesh).expect("Failed to write obj");

    let mut obj_reader = draco_io::ObjReader::open(obj_path).expect("Failed to open obj");
    let mesh = obj_reader.read_mesh().expect("Failed to read obj");

    print_size_table_header("C++ vs Rust Quantization Edge Cases");

    // Test extreme quantization values
    // Note: C++ Draco supports 1-30 bits, but practical range is typically 8-20
    let edge_cases = [
        (1, "minimum bits"),
        (4, "very low bits"),
        (20, "high precision"),
        (24, "very high precision"),
    ];

    let mut all_passed = true;

    for (qp, desc) in edge_cases {
        let speed = 5;
        let cpp_cl = 10 - speed;

        // Rust encode
        let mut options = EncoderOptions::new();
        options.set_global_int("encoding_speed", speed);
        options.set_global_int("decoding_speed", speed);
        options.set_attribute_int(0, "quantization_bits", qp);

        let mut encoder = MeshEncoder::new();
        encoder.set_mesh(mesh.clone());
        let mut buffer = EncoderBuffer::new();
        let res = encoder.encode(&options, &mut buffer);

        if res.is_err() {
            print_case_status(format!("qp={qp:2} {desc}"), "RUST FAIL");
            continue;
        }
        let rust_data = buffer.data().to_vec();

        // C++ encode
        let cpp_drc_path = format!("temp_cpp_qedge{}.drc", qp);
        let output = Command::new(&encoder_path)
            .arg("-i")
            .arg(obj_path)
            .arg("-o")
            .arg(&cpp_drc_path)
            .arg("-method")
            .arg("edgebreaker")
            .arg("-cl")
            .arg(cpp_cl.to_string())
            .arg("-qp")
            .arg(qp.to_string())
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                let mut file = File::open(&cpp_drc_path).expect("Failed to open cpp drc");
                let mut cpp_data = Vec::new();
                file.read_to_end(&mut cpp_data)
                    .expect("Failed to read cpp drc");

                let status = if rust_data == cpp_data {
                    "MATCH"
                } else {
                    "MISMATCH"
                };
                if rust_data != cpp_data {
                    all_passed = false;
                }
                print_size_row(
                    format!("qp={qp:2} {desc}"),
                    cpp_data.len(),
                    rust_data.len(),
                    status,
                );

                let _ = std::fs::remove_file(&cpp_drc_path);
            } else {
                print_case_status(format!("qp={qp:2} {desc}"), "C++ FAIL");
            }
        }

        // Verify C++ can decode Rust output
        let rust_drc_path = format!("temp_rust_qedge{}.drc", qp);
        std::fs::write(&rust_drc_path, &rust_data).expect("write failed");
        let decode_result = Command::new(&decoder_path)
            .arg("-i")
            .arg(&rust_drc_path)
            .output();
        if let Ok(output) = decode_result {
            if !output.status.success() {
                println!("qp={:2} ({}): C++ decoder failed for Rust output", qp, desc);
                all_passed = false;
            }
        }
        let _ = std::fs::remove_file(&rust_drc_path);
    }

    let _ = std::fs::remove_file(obj_path);

    // Don't fail on edge cases - they may legitimately differ
    if !all_passed {
        println!("\nNote: Some edge case tests showed differences - this may be expected for extreme values");
    }
}

/// Combined test: different speeds with different quantization bits
#[test]
fn test_speed_quantization_matrix() {
    let _output_lock = OUTPUT_LOCK.lock().unwrap();
    let tools_path = match get_cpp_tools_path() {
        Some(p) => p,
        None => {
            eprintln!("SKIPPING: C++ tools not found");
            return;
        }
    };
    let encoder_path = tools_path.join("draco_encoder.exe");
    let decoder_path = tools_path.join("draco_decoder.exe");

    if !encoder_path.exists() || !decoder_path.exists() {
        eprintln!("SKIPPING: C++ encoder/decoder not found");
        return;
    }

    let mesh = create_mesh_with_attributes();
    let obj_path = Path::new("temp_matrix_test.obj");
    if obj_path.exists() {
        let _ = std::fs::remove_file(obj_path);
    }
    draco_io::obj_writer::write_obj_mesh(obj_path, &mesh).expect("Failed to write obj");

    let mut obj_reader = draco_io::ObjReader::open(obj_path).expect("Failed to open obj");
    let mesh = obj_reader.read_mesh().expect("Failed to read obj");

    print_size_table_header("C++ vs Rust Speed x Quantization Matrix");
    println!("Note: speed 0, 5, 10 with qp 8, 11, 14\n");

    let speeds = [0, 5, 10];
    let qp_values = [8, 11, 14];

    let mut match_count = 0;
    let mut total_count = 0;

    for speed in speeds {
        for qp in qp_values {
            let cpp_cl = 10 - speed;
            total_count += 1;

            // Rust encode
            let mut options = EncoderOptions::new();
            options.set_global_int("encoding_speed", speed);
            options.set_global_int("decoding_speed", speed);
            options.set_attribute_int(0, "quantization_bits", qp);

            let mut encoder = MeshEncoder::new();
            encoder.set_mesh(mesh.clone());
            let mut buffer = EncoderBuffer::new();
            let res = encoder.encode(&options, &mut buffer);
            assert!(res.is_ok(), "Rust encode failed");
            let rust_data = buffer.data().to_vec();

            // C++ encode
            let cpp_drc_path = format!("temp_cpp_s{}_qp{}.drc", speed, qp);
            let output = Command::new(&encoder_path)
                .arg("-i")
                .arg(obj_path)
                .arg("-o")
                .arg(&cpp_drc_path)
                .arg("-method")
                .arg("edgebreaker")
                .arg("-cl")
                .arg(cpp_cl.to_string())
                .arg("-qp")
                .arg(qp.to_string())
                .output();

            if let Ok(output) = output {
                if output.status.success() {
                    let mut file = File::open(&cpp_drc_path).expect("Failed to open cpp drc");
                    let mut cpp_data = Vec::new();
                    file.read_to_end(&mut cpp_data)
                        .expect("Failed to read cpp drc");

                    let matched = rust_data == cpp_data;
                    if matched {
                        match_count += 1;
                    }
                    let status = if matched { "MATCH" } else { "MISMATCH" };
                    print_size_row(
                        format!("speed={speed:2} qp={qp:2}"),
                        cpp_data.len(),
                        rust_data.len(),
                        status,
                    );

                    let _ = std::fs::remove_file(&cpp_drc_path);
                }
            }

            // Cleanup
            let rust_drc_path = format!("temp_rust_s{}_qp{}.drc", speed, qp);
            std::fs::write(&rust_drc_path, &rust_data).ok();
            let _ = std::fs::remove_file(&rust_drc_path);
        }
    }

    let _ = std::fs::remove_file(obj_path);

    println!(
        "\nMatrix results: {}/{} combinations matched",
        match_count, total_count
    );
    assert_eq!(
        match_count, total_count,
        "Not all speed/quantization combinations matched"
    );
}