bevy-sensor 0.4.10

Bevy library for capturing multi-view images of 3D OBJ models (YCB dataset) for sensor simulation
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
//! Integration test: Hardware rendering sanity check
//!
//! This test actually renders to GPU to verify rendering support on the current platform.
//! It's ignored by default (CI doesn't run it) but can be run locally with:
//!
//! ```sh
//! cargo test -- --ignored --test render_integration -- --nocapture
//! just test-render-integration  # or use justfile command
//! ```
//!
//! Renders are saved to `test_fixtures/test_renders/` so you can inspect output.
//!
//! This validates:
//! - GPU/rendering backend availability
//! - WebGPU backend selection works
//! - Output data format and dimensions are correct
//! - Depth buffer readback works

use bevy_sensor::{
    backend::detect_platform, batch::BatchRenderRequest, cache::ModelCache, render_batch,
    render_to_buffer, render_to_buffer_cached, BatchRenderConfig, ObjectRotation, RenderConfig,
    RenderOutput, ViewpointConfig,
};
use std::fs;
use std::path::PathBuf;
use std::time::Instant;

/// Save render output to test_fixtures/test_renders for inspection
fn save_render_output(output: &RenderOutput, name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let render_dir = PathBuf::from("test_fixtures/test_renders");
    fs::create_dir_all(&render_dir)?;

    // Save RGBA as PNG
    let rgb_image = output.to_rgb_image();
    let img = image::ImageBuffer::from_fn(output.width, output.height, |x, y| {
        let rgb = rgb_image[y as usize][x as usize];
        image::Rgb(rgb)
    });
    let rgba_path = render_dir.join(format!("{}.png", name));
    img.save(&rgba_path)?;
    println!("  Saved RGBA: {}", rgba_path.display());

    // Save depth as binary f64
    let depth_path = render_dir.join(format!("{}.depth", name));
    let depth_bytes: Vec<u8> = output.depth.iter().flat_map(|&d| d.to_le_bytes()).collect();
    fs::write(&depth_path, &depth_bytes)?;
    println!("  Saved depth: {}", depth_path.display());

    Ok(())
}

#[test]
#[ignore] // Skip in CI - run manually to verify rendering works
fn test_render_integration() {
    println!("\n=== Render Integration Test ===");
    println!("Platform: {:?}", detect_platform());

    // Check if YCB models exist
    let object_dir = PathBuf::from("/tmp/ycb/003_cracker_box");
    if !object_dir.exists() {
        println!("⚠ YCB models not found at {:?}", object_dir);
        println!("  Skipping render test (models required)");
        println!("  Run: cargo run --example test_render");
        return;
    }

    // Generate viewpoints and select first one
    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    assert!(!viewpoints.is_empty(), "No viewpoints generated");

    // Render config - TBP default (64x64)
    let config = RenderConfig::tbp_default();
    println!("Rendering {}x{}", config.width, config.height);

    // Actually render to GPU
    let output = render_to_buffer(
        &object_dir,
        &viewpoints[0],
        &ObjectRotation::identity(),
        &config,
    )
    .expect("Render failed - GPU/backend unavailable or rendering not supported");

    // Validate RGBA output
    assert_eq!(output.width, config.width, "Output width mismatch");
    assert_eq!(output.height, config.height, "Output height mismatch");

    let expected_rgba_size = (config.width * config.height * 4) as usize;
    assert_eq!(
        output.rgba.len(),
        expected_rgba_size,
        "RGBA buffer size mismatch: expected {} bytes, got {}",
        expected_rgba_size,
        output.rgba.len()
    );

    // Validate depth output
    let expected_depth_size = (config.width * config.height) as usize;
    assert_eq!(
        output.depth.len(),
        expected_depth_size,
        "Depth buffer size mismatch: expected {} values, got {}",
        expected_depth_size,
        output.depth.len()
    );

    // Sanity check: depth values should be reasonable (between 0.1 and 10 meters typically)
    let mut has_valid_depth = false;
    for &depth in output.depth.iter() {
        if depth > 0.1 && depth < 10.0 {
            has_valid_depth = true;
            break;
        }
    }
    assert!(has_valid_depth, "No valid depth values in output");

    // Sanity check: RGBA should have some non-zero pixels
    let mut has_color = false;
    for chunk in output.rgba.chunks(4) {
        if chunk[0] > 10 || chunk[1] > 10 || chunk[2] > 10 {
            has_color = true;
            break;
        }
    }
    assert!(has_color, "No color data in output");

    // Validate intrinsics (camera calibration)
    let intrinsics = &output.intrinsics;
    assert!(intrinsics.focal_length[0] > 0.0, "Invalid focal length X");
    assert!(intrinsics.focal_length[1] > 0.0, "Invalid focal length Y");
    assert!(
        intrinsics.principal_point[0] >= 0.0,
        "Invalid principal point X"
    );
    assert!(
        intrinsics.principal_point[1] >= 0.0,
        "Invalid principal point Y"
    );

    println!("✓ Render output valid!");
    println!("  RGBA: {} bytes", output.rgba.len());
    println!(
        "  Depth: {} values ({} bytes)",
        output.depth.len(),
        output.depth.len() * 8
    );
    println!(
        "  Focal length: [{:.2}, {:.2}]",
        intrinsics.focal_length[0], intrinsics.focal_length[1]
    );

    // Save render output for inspection
    if let Err(e) = save_render_output(&output, "test_render_basic") {
        println!("⚠ Failed to save render output: {}", e);
    }

    println!("✓ Integration test passed");
}

#[test]
#[ignore] // Skip in CI - run manually on hardware/render-capable environments
fn test_batch_render_matches_sequential_episode_outputs() {
    println!("\n=== Batch vs Sequential Render Test ===");

    let object_dir = PathBuf::from("/tmp/ycb/003_cracker_box");
    if !object_dir.exists() {
        println!("⚠ Skipping - YCB models not found");
        return;
    }

    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    let selected_viewpoints: Vec<_> = viewpoints.into_iter().take(3).collect();
    let rotation = ObjectRotation::identity();
    let config = RenderConfig::tbp_default();

    let sequential_start = Instant::now();
    let sequential_outputs: Vec<_> = selected_viewpoints
        .iter()
        .map(|viewpoint| {
            render_to_buffer(&object_dir, viewpoint, &rotation, &config)
                .expect("Sequential render failed")
        })
        .collect();
    let sequential_elapsed = sequential_start.elapsed();

    let batch_requests: Vec<_> = selected_viewpoints
        .iter()
        .map(|viewpoint| BatchRenderRequest {
            object_dir: object_dir.clone(),
            viewpoint: *viewpoint,
            object_rotation: rotation.clone(),
            render_config: config.clone(),
        })
        .collect();

    let batch_start = Instant::now();
    let batch_outputs =
        render_batch(batch_requests, &BatchRenderConfig::default()).expect("Batch render failed");
    let batch_elapsed = batch_start.elapsed();

    assert_eq!(batch_outputs.len(), sequential_outputs.len());

    for (idx, (batch_output, sequential_output)) in batch_outputs
        .iter()
        .zip(sequential_outputs.iter())
        .enumerate()
    {
        assert_eq!(batch_output.request.viewpoint, selected_viewpoints[idx]);
        assert_eq!(batch_output.request.object_rotation, rotation);
        assert_eq!(batch_output.width, sequential_output.width);
        assert_eq!(batch_output.height, sequential_output.height);
        assert_eq!(batch_output.intrinsics, sequential_output.intrinsics);
        assert_eq!(batch_output.rgba, sequential_output.rgba);
        assert_eq!(batch_output.depth.len(), sequential_output.depth.len());

        let max_depth_delta = batch_output
            .depth
            .iter()
            .zip(sequential_output.depth.iter())
            .map(|(lhs, rhs)| (lhs - rhs).abs())
            .fold(0.0_f64, f64::max);
        assert!(
            max_depth_delta <= 1e-9,
            "Depth mismatch at viewpoint {idx}: max delta {max_depth_delta}"
        );
    }

    println!(
        "  Sequential: {:.2}s for {} viewpoints",
        sequential_elapsed.as_secs_f64(),
        sequential_outputs.len()
    );
    println!(
        "  Batch: {:.2}s for {} viewpoints",
        batch_elapsed.as_secs_f64(),
        batch_outputs.len()
    );
    println!("✓ Batch and sequential outputs matched");
}

#[test]
#[ignore] // Skip in CI
fn test_render_multiple_viewpoints() {
    println!("\n=== Multiple Viewpoint Render Test ===");

    let object_dir = PathBuf::from("/tmp/ycb/003_cracker_box");
    if !object_dir.exists() {
        println!("⚠ Skipping - YCB models not found");
        return;
    }

    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    let config = RenderConfig::tbp_default();

    println!("Rendering {} viewpoints...", viewpoints.len().min(3));

    // Render first 3 viewpoints to verify consistency
    for (i, viewpoint) in viewpoints.iter().take(3).enumerate() {
        let output = render_to_buffer(&object_dir, viewpoint, &ObjectRotation::identity(), &config)
            .expect("Render failed");

        assert_eq!(output.width, config.width);
        assert_eq!(output.height, config.height);
        assert_eq!(
            output.rgba.len(),
            (config.width * config.height * 4) as usize
        );
        assert_eq!(output.depth.len(), (config.width * config.height) as usize);

        // Save each viewpoint
        if let Err(e) = save_render_output(&output, &format!("test_viewpoint_{}", i)) {
            println!("    ⚠ Failed to save: {}", e);
        }

        println!("  ✓ Viewpoint {} rendered successfully", i);
    }

    println!("✓ Multiple viewpoint test passed");
}

#[test]
#[ignore] // Skip in CI
fn test_render_with_rotation() {
    println!("\n=== Render with Object Rotation Test ===");

    let object_dir = PathBuf::from("/tmp/ycb/003_cracker_box");
    if !object_dir.exists() {
        println!("⚠ Skipping - YCB models not found");
        return;
    }

    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    let config = RenderConfig::tbp_default();

    // Get TBP benchmark rotations (3 rotations)
    let rotations = ObjectRotation::tbp_benchmark_rotations();
    println!("Rendering with {} rotations...", rotations.len());

    for (rot_idx, rotation) in rotations.iter().enumerate() {
        let output = render_to_buffer(&object_dir, &viewpoints[0], rotation, &config)
            .expect("Render with rotation failed");

        assert_eq!(output.width, config.width);
        assert_eq!(output.height, config.height);

        // Save each rotation
        if let Err(e) = save_render_output(&output, &format!("test_rotation_{}", rot_idx)) {
            println!("    ⚠ Failed to save: {}", e);
        }

        println!(
            "  ✓ Rotation {} rendered successfully (yaw: {}°)",
            rot_idx, rotation.yaw
        );
    }

    println!("✓ Rotation test passed");
}

#[test]
#[ignore] // Skip in CI
fn test_render_with_cache() {
    println!("\n=== Render with Cache Test ===");

    let object_dir = PathBuf::from("/tmp/ycb/003_cracker_box");
    if !object_dir.exists() {
        println!("⚠ Skipping - YCB models not found");
        return;
    }

    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    let config = RenderConfig::tbp_default();

    let mut cache = ModelCache::new();

    // Render first viewpoint with cache
    let output1 = render_to_buffer_cached(
        &object_dir,
        &viewpoints[0],
        &ObjectRotation::identity(),
        &config,
        &mut cache,
    )
    .expect("First cached render failed");

    println!("  ✓ First render completed");
    println!(
        "    Cache stats: {} scenes, {} textures",
        cache.scene_count(),
        cache.texture_count()
    );

    assert_eq!(cache.scene_count(), 1, "Should have 1 cached scene");
    assert_eq!(cache.texture_count(), 1, "Should have 1 cached texture");

    // Render second viewpoint - should use cache
    let output2 = render_to_buffer_cached(
        &object_dir,
        &viewpoints[1],
        &ObjectRotation::identity(),
        &config,
        &mut cache,
    )
    .expect("Second cached render failed");

    println!("  ✓ Second render completed");
    println!(
        "    Cache stats: {} scenes, {} textures",
        cache.scene_count(),
        cache.texture_count()
    );

    // Cache should still have same entries (not duplicated)
    assert_eq!(cache.scene_count(), 1, "Should still have 1 cached scene");
    assert_eq!(
        cache.texture_count(),
        1,
        "Should still have 1 cached texture"
    );

    // Both renders should produce valid output
    assert_eq!(output1.width, config.width);
    assert_eq!(output1.height, config.height);
    assert_eq!(output2.width, config.width);
    assert_eq!(output2.height, config.height);

    // Outputs should be different (different viewpoints)
    assert_ne!(
        output1.rgba, output2.rgba,
        "Different viewpoints should produce different RGBA"
    );

    if let Err(e) = save_render_output(&output1, "test_cache_vp0") {
        println!("    ⚠ Failed to save output1: {}", e);
    }
    if let Err(e) = save_render_output(&output2, "test_cache_vp1") {
        println!("    ⚠ Failed to save output2: {}", e);
    }

    // Test cache clear
    cache.clear();
    assert_eq!(cache.scene_count(), 0, "Cache should be empty after clear");
    assert_eq!(
        cache.texture_count(),
        0,
        "Cache should be empty after clear"
    );

    println!("✓ Cache test passed");
}

#[test]
#[ignore] // Skip in CI
fn test_cache_with_multiple_viewpoints() {
    println!("\n=== Cache with Multiple Viewpoints Test ===");

    let object_dir = PathBuf::from("/tmp/ycb/005_tomato_soup_can");
    if !object_dir.exists() {
        println!("⚠ Skipping - YCB model not found");
        return;
    }

    let viewpoint_config = ViewpointConfig::default();
    let viewpoints = bevy_sensor::generate_viewpoints(&viewpoint_config);
    let config = RenderConfig::tbp_default();

    let mut cache = ModelCache::new();

    // Render 5 viewpoints with cache
    let render_count = 5.min(viewpoints.len());
    println!("Rendering {} viewpoints with cache...", render_count);

    let mut outputs = Vec::new();
    for (i, viewpoint) in viewpoints.iter().take(render_count).enumerate() {
        let output = render_to_buffer_cached(
            &object_dir,
            viewpoint,
            &ObjectRotation::identity(),
            &config,
            &mut cache,
        )
        .expect("Cached render failed");

        outputs.push(output);

        if i == 0 {
            println!(
                "  Initial cache size: {} scenes, {} textures",
                cache.scene_count(),
                cache.texture_count()
            );
        }
    }

    // Cache should only have 1 scene and 1 texture (tomato soup can)
    assert_eq!(cache.scene_count(), 1, "Should cache 1 scene");
    assert_eq!(cache.texture_count(), 1, "Should cache 1 texture");

    // All outputs should be valid
    for (i, output) in outputs.iter().enumerate() {
        assert_eq!(output.width, config.width, "Output {} width mismatch", i);
        assert_eq!(output.height, config.height, "Output {} height mismatch", i);
        assert!(!output.rgba.is_empty(), "Output {} has no RGBA data", i);
        assert!(!output.depth.is_empty(), "Output {} has no depth data", i);
    }

    println!("  ✓ All {} renders successful", render_count);
    println!(
        "  ✓ Cache maintained 1 scene and 1 texture across {} viewpoints",
        render_count
    );
    println!("✓ Multiple viewpoints cache test passed");
}