|
| 1 | +//! Test that 2D content can be added to a 3D space and vice versa. |
| 2 | +
|
| 3 | +use re_log_types::{EntityPathFilter, TimePoint}; |
| 4 | +use re_test_context::{TestContext, external::egui_kittest::SnapshotOptions}; |
| 5 | +use re_test_viewport::TestContextExt as _; |
| 6 | +use re_types::{RowId, archetypes, components}; |
| 7 | +use re_viewer_context::{RecommendedView, ViewClass as _}; |
| 8 | +use re_viewport_blueprint::ViewBlueprint; |
| 9 | + |
| 10 | +fn setup_scene(test_context: &mut TestContext) { |
| 11 | + use ndarray::{Array, ShapeBuilder as _}; |
| 12 | + |
| 13 | + test_context.log_entity("boxes", |builder| { |
| 14 | + builder.with_archetype( |
| 15 | + RowId::new(), |
| 16 | + TimePoint::default(), |
| 17 | + &archetypes::Boxes3D::from_centers_and_half_sizes( |
| 18 | + [(-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (1.0, 1.0, 0.0)], |
| 19 | + [(0.2, 0.4, 0.2), (0.2, 0.2, 0.4), (0.4, 0.2, 0.2)], |
| 20 | + ) |
| 21 | + .with_colors([0xFF0000FF, 0x00FF00FF, 0x0000FFFF]) |
| 22 | + .with_fill_mode(components::FillMode::Solid), |
| 23 | + ) |
| 24 | + }); |
| 25 | + |
| 26 | + let eye_position = glam::vec3(0.0, -1.0, 0.2); |
| 27 | + |
| 28 | + test_context.log_entity("camera", |builder| { |
| 29 | + builder |
| 30 | + .with_archetype( |
| 31 | + RowId::new(), |
| 32 | + TimePoint::default(), |
| 33 | + &archetypes::Transform3D::from_mat3x3( |
| 34 | + // Look at the middle box. |
| 35 | + glam::Mat3::look_at_rh(eye_position, glam::vec3(0.0, 1.0, 0.0), glam::Vec3::Z), |
| 36 | + ) |
| 37 | + .with_translation(eye_position), |
| 38 | + ) |
| 39 | + .with_archetype( |
| 40 | + RowId::new(), |
| 41 | + TimePoint::default(), |
| 42 | + &archetypes::Pinhole::from_focal_length_and_resolution([2., 2.], [3., 2.]) |
| 43 | + .with_image_plane_distance(1.0), |
| 44 | + ) |
| 45 | + .with_archetype( |
| 46 | + RowId::new(), |
| 47 | + TimePoint::default(), |
| 48 | + &archetypes::Image::from_color_model_and_tensor( |
| 49 | + re_types::datatypes::ColorModel::RGB, |
| 50 | + Array::<u8, _>::zeros((2, 3, 3).f()), |
| 51 | + ) |
| 52 | + .expect("failed to create image"), |
| 53 | + ) |
| 54 | + }); |
| 55 | + test_context.log_entity("camera/points", |builder| { |
| 56 | + builder.with_archetype( |
| 57 | + RowId::new(), |
| 58 | + TimePoint::default(), |
| 59 | + &archetypes::Points2D::new([ |
| 60 | + [0.0, 0.0], |
| 61 | + [3.0, 0.0], |
| 62 | + [0.0, 2.0], |
| 63 | + [3.0, 2.0], |
| 64 | + [1.5, 1.0], |
| 65 | + ]) |
| 66 | + .with_radii([0.2]), |
| 67 | + ) |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +pub fn test_2d_in_3d() { |
| 73 | + let mut test_context = TestContext::new_with_view_class::<re_view_spatial::SpatialView3D>(); |
| 74 | + |
| 75 | + setup_scene(&mut test_context); |
| 76 | + |
| 77 | + let view_id = test_context.setup_viewport_blueprint(|_ctx, blueprint| { |
| 78 | + let view = |
| 79 | + ViewBlueprint::new_with_root_wildcard(re_view_spatial::SpatialView3D::identifier()); |
| 80 | + blueprint.add_view_at_root(view) |
| 81 | + }); |
| 82 | + |
| 83 | + let mut harness = test_context |
| 84 | + .setup_kittest_for_rendering() |
| 85 | + .with_size(egui::vec2(400.0, 300.0)) |
| 86 | + .build_ui(|ui| { |
| 87 | + test_context.run_ui(ui, |ctx, ui| { |
| 88 | + test_context.ui_for_single_view(ui, ctx, view_id); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + harness.run(); |
| 93 | + harness.snapshot_options( |
| 94 | + "2d_in_3d", |
| 95 | + &SnapshotOptions::new() |
| 96 | + .threshold(1.0) |
| 97 | + .failed_pixel_count_threshold(30), |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +pub fn test_3d_in_2d() { |
| 103 | + let mut test_context = TestContext::new_with_view_class::<re_view_spatial::SpatialView2D>(); |
| 104 | + |
| 105 | + setup_scene(&mut test_context); |
| 106 | + |
| 107 | + let view_id = test_context.setup_viewport_blueprint(|_ctx, blueprint| { |
| 108 | + let view = ViewBlueprint::new( |
| 109 | + re_view_spatial::SpatialView2D::identifier(), |
| 110 | + RecommendedView { |
| 111 | + origin: "camera".into(), |
| 112 | + query_filter: EntityPathFilter::all(), |
| 113 | + }, |
| 114 | + ); |
| 115 | + blueprint.add_view_at_root(view) |
| 116 | + }); |
| 117 | + |
| 118 | + let mut harness = test_context |
| 119 | + .setup_kittest_for_rendering() |
| 120 | + .with_size(egui::vec2(400.0, 300.0)) |
| 121 | + .build_ui(|ui| { |
| 122 | + test_context.run_ui(ui, |ctx, ui| { |
| 123 | + test_context.ui_for_single_view(ui, ctx, view_id); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + harness.run(); |
| 128 | + harness.snapshot("3d_in_2d"); |
| 129 | +} |
0 commit comments