re_view_spatial 0.32.2-rc.1

Views that show entities in a 2D or 3D spatial relationship.
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
use std::sync::Arc;

use re_entity_db::InstancePathHash;
use re_log_types::Instance;
use re_renderer::renderer::{GpuMeshInstance, LineStripFlags};
use re_renderer::{PickingLayerInstanceId, RenderContext};
use re_sdk_types::ComponentIdentifier;
use re_sdk_types::components::{self, FillMode};
use re_tf::convert;
use re_view::{clamped_or_nothing, process_annotation_slices, process_color_slice};
#[cfg(doc)]
use re_viewer_context::VisualizerSystem;
use re_viewer_context::{
    QueryContext, ViewQuery, ViewSystemExecutionError, VisualizerExecutionOutput,
    VisualizerReportSeverity, typed_fallback_for,
};
use vec1::smallvec_v1::SmallVec1;

use crate::contexts::SpatialSceneVisualizerInstructionContext;
use crate::proc_mesh::{self, ProcMeshKey};
use crate::visualizers::utilities::LabeledBatch;
use crate::visualizers::{SpatialViewVisualizerData, process_labels_3d, process_radius_slice};

/// Maximum number of instances a single batch will draw before being capped.
///
/// This limit exists to prevent the viewer from becoming unresponsive when a
/// single entity contains a very large number of 3D shape instances (boxes,
/// capsules, cylinders, ellipsoids). It can be lifted in the viewer settings
/// via [`AppOptions::visualizer_limits_enabled`](re_viewer_context::AppOptions::visualizer_limits_enabled).
const NUM_INSTANCE_LIMIT_PER_BATCH: usize = 60_000;

/// To be used within the scope of a single [`VisualizerSystem::execute()`] call
/// when the visualizer wishes to draw batches of [`ProcMeshKey`] meshes.
pub struct ProcMeshDrawableBuilder<'ctx> {
    /// Bounding box and label info here will be updated by the drawn batches.
    pub data: &'ctx mut SpatialViewVisualizerData,

    /// Accumulates lines to render.
    /// TODO(kpreid): Should be using instanced meshes kept in GPU buffers
    /// instead of this immediate-mode strategy that copies every vertex every frame.
    pub line_builder: re_renderer::LineDrawableBuilder<'ctx>,
    pub line_batch_debug_label: re_renderer::Label,

    /// Accumulates triangle mesh instances to render.
    pub solid_instances: Vec<GpuMeshInstance>,

    pub query: &'ctx ViewQuery<'ctx>,
    pub render_ctx: &'ctx RenderContext,
    pub output: &'ctx VisualizerExecutionOutput,
}

/// A [batch] of instances to draw. This struct is just arguments to
/// [`ProcMeshDrawableBuilder::add_batch()`].
///
/// TODO(#7026): Document how the number of instances is derived from this data.
///
/// [batch]: https://rerun.io/docs/concepts/batches
pub struct ProcMeshBatch<'a, IMesh, IFill> {
    pub half_sizes: &'a [components::HalfSize3D],

    pub centers: &'a [components::Translation3D],
    pub rotation_axis_angles: &'a [components::RotationAxisAngle],
    pub quaternions: &'a [components::RotationQuat],

    /// Iterator of meshes. Must be at least as long as `half_sizes`.
    pub meshes: IMesh,

    /// Iterator of mesh fill modes. Must be at least as long as `half_sizes`.
    pub fill_modes: IFill,

    pub line_radii: &'a [components::Radius],
    pub colors: &'a [components::Color],
    pub labels: &'a [re_sdk_types::ArrowString],
    pub show_labels: Option<components::ShowLabels>,
    pub class_ids: &'a [components::ClassId],
}

/// Combines transform-like components on the entity with instances pose to view-origin transforms
/// provided by the transform system.
// TODO(#7026): We should formalize this kind of hybrid joining better.
fn combine_instance_poses_with_archetype_transforms(
    num_half_sizes: usize,
    target_from_poses: &SmallVec1<[glam::DAffine3; 1]>,
    translations: &[components::Translation3D],
    rotation_axis_angles: &[components::RotationAxisAngle],
    quaternions: &[components::RotationQuat],
) -> vec1::Vec1<glam::DAffine3> {
    // Draw as many proc meshes as we have max(instance pose count, proc mesh count), all components get repeated over that number.
    let num_instances = num_half_sizes
        .max(target_from_poses.len())
        .max(translations.len())
        .max(rotation_axis_angles.len())
        .max(quaternions.len());

    let mut iter_translation = clamped_or_nothing(translations, num_instances);
    let mut iter_rotation_axis_angle = clamped_or_nothing(rotation_axis_angles, num_instances);
    let mut iter_rotation_quat = clamped_or_nothing(quaternions, num_instances);

    let last_target_from_instances = target_from_poses.last();
    let clamped_target_from_instances = target_from_poses
        .iter()
        .chain(std::iter::repeat(last_target_from_instances))
        .copied();

    let target_from_instances = clamped_target_from_instances
        .take(num_instances)
        .map(|mut transform| {
            if let Some(translation) = iter_translation.next() {
                transform *= convert::translation_3d_to_daffine3(*translation);
            }

            if let Some(rotation_axis_angle) = iter_rotation_axis_angle.next() {
                if let Ok(axis_angle) =
                    convert::rotation_axis_angle_to_daffine3(*rotation_axis_angle)
                {
                    transform *= axis_angle;
                } else {
                    transform = glam::DAffine3::ZERO;
                }
            }

            if let Some(rotation_quat) = iter_rotation_quat.next() {
                if let Ok(rotation_quat) = convert::rotation_quat_to_daffine3(*rotation_quat) {
                    transform *= rotation_quat;
                } else {
                    transform = glam::DAffine3::ZERO;
                }
            }

            transform
        })
        .collect();

    vec1::Vec1::try_from_vec(target_from_instances).expect("built from a SmallVec1, so can't fail")
}

impl<'ctx> ProcMeshDrawableBuilder<'ctx> {
    pub fn new(
        data: &'ctx mut SpatialViewVisualizerData,
        render_ctx: &'ctx re_renderer::RenderContext,
        view_query: &'ctx ViewQuery<'ctx>,
        output: &'ctx VisualizerExecutionOutput,
        line_batch_debug_label: impl Into<re_renderer::Label>,
    ) -> Self {
        let mut line_builder = re_renderer::LineDrawableBuilder::new(render_ctx);
        line_builder.radius_boost_in_ui_points_for_outlines(
            re_view::SIZE_BOOST_IN_POINTS_FOR_LINE_OUTLINES,
        );

        ProcMeshDrawableBuilder {
            data,
            line_builder,
            line_batch_debug_label: line_batch_debug_label.into(),
            solid_instances: Vec::new(),
            query: view_query,
            render_ctx,
            output,
        }
    }

    /// Add a batch of data to be drawn.
    #[expect(clippy::too_many_arguments)]
    pub fn add_batch(
        &mut self,
        query_context: &QueryContext<'_>,
        ent_context: &SpatialSceneVisualizerInstructionContext<'_>,
        color_component: ComponentIdentifier,
        line_radii_component: ComponentIdentifier,
        show_labels_component: ComponentIdentifier,
        constant_instance_transform: glam::Affine3A,
        batch: ProcMeshBatch<'_, impl Iterator<Item = ProcMeshKey>, impl Iterator<Item = FillMode>>,
    ) -> Result<(), ViewSystemExecutionError> {
        let entity_path = query_context.target_entity_path;

        if batch.half_sizes.is_empty() {
            return Ok(());
        }

        let target_from_poses = ent_context.transform_info.target_from_instances();
        let target_from_instances = combine_instance_poses_with_archetype_transforms(
            batch.half_sizes.len(),
            target_from_poses,
            batch.centers,
            batch.rotation_axis_angles,
            batch.quaternions,
        );
        let num_instances = target_from_instances.len();

        let num_instances = if query_context
            .app_ctx()
            .app_options
            .visualizer_limits_enabled
            && num_instances > NUM_INSTANCE_LIMIT_PER_BATCH
        {
            if let Some(instruction_id) = query_context.instruction_id {
                self.output.report_unspecified_source(
                    instruction_id,
                    VisualizerReportSeverity::Warning,
                    format!(
                        "Too many instances ({}), capping to {}. \
                             This limit can be lifted in Settings.",
                        re_format::format_uint(num_instances),
                        re_format::format_uint(NUM_INSTANCE_LIMIT_PER_BATCH),
                    ),
                );
            }
            NUM_INSTANCE_LIMIT_PER_BATCH
        } else {
            num_instances
        };

        re_tracing::profile_function_if!(10_000 < num_instances);

        let half_sizes = clamped_or_nothing(batch.half_sizes, num_instances);

        let annotation_infos = process_annotation_slices(
            self.query.latest_at,
            num_instances,
            batch.class_ids,
            &ent_context.annotations,
        );

        let line_radii = process_radius_slice(
            query_context,
            entity_path,
            num_instances,
            batch.line_radii,
            line_radii_component,
        );
        let colors = process_color_slice(
            query_context,
            color_component,
            num_instances,
            &annotation_infos,
            batch.colors,
        );

        let mut line_batch = self
            .line_builder
            .batch(self.line_batch_debug_label.clone())
            .depth_offset(ent_context.depth_offset)
            .outline_mask_ids(ent_context.highlight.overall)
            .picking_object_id(re_renderer::PickingLayerObjectId(entity_path.hash64()));

        let mut world_space_bounding_box = macaw::BoundingBox::nothing();

        let world_from_instances = target_from_instances
            .iter()
            .map(|transform| transform.as_affine3a())
            .chain(std::iter::repeat(
                target_from_instances.last().as_affine3a(),
            ));

        let mut num_instances = 0;
        for (
            instance_index,
            (half_size, world_from_instance, radius, &color, proc_mesh_key, fill_mode),
        ) in itertools::izip!(
            half_sizes,
            world_from_instances,
            line_radii,
            colors.iter(),
            batch.meshes,
            batch.fill_modes
        )
        .enumerate()
        {
            let instance = Instance::from(instance_index as u64);
            num_instances = instance_index + 1;

            let world_from_instance = world_from_instance
                * glam::Affine3A::from_scale(glam::Vec3::from(*half_size))
                * constant_instance_transform;
            world_space_bounding_box = world_space_bounding_box.union(
                proc_mesh_key
                    .simple_bounding_box()
                    .transform_affine3(&world_from_instance),
            );

            let draw_wireframe = fill_mode.has_wireframe();
            let draw_solid = fill_mode.has_solid();

            if draw_wireframe {
                let Some(wireframe_mesh) =
                    query_context
                        .store_ctx()
                        .memoizer(|c: &mut proc_mesh::WireframeCache| {
                            c.entry(proc_mesh_key, self.render_ctx)
                        })
                else {
                    return Err(ViewSystemExecutionError::DrawDataCreationError(Arc::new(
                        std::io::Error::other("Failed to allocate wireframe mesh"),
                    )));
                };

                for strip in &wireframe_mesh.line_strips {
                    let strip_builder = line_batch
                        .add_strip(
                            strip
                                .iter()
                                .map(|&point| world_from_instance.transform_point3(point)),
                        )
                        .color(color)
                        .radius(radius)
                        .picking_instance_id(PickingLayerInstanceId(instance_index as _))
                        // Looped lines should be connected with rounded corners.
                        .flags(LineStripFlags::STRIP_FLAGS_OUTWARD_EXTENDING_ROUND_CAPS);

                    if let Some(outline_mask_ids) = ent_context
                        .highlight
                        .instances
                        .get(&Instance::from(instance_index as u64))
                    {
                        // Not using ent_context.highlight.index_outline_mask() because
                        // that's already handled when the builder was created.
                        strip_builder.outline_mask_ids(*outline_mask_ids);
                    }
                }
            }

            if draw_solid {
                let store_ctx = query_context.store_ctx();
                let Some(solid_mesh) = store_ctx.memoizer(|c: &mut proc_mesh::SolidCache| {
                    c.entry(proc_mesh_key, self.render_ctx)
                }) else {
                    return Err(ViewSystemExecutionError::DrawDataCreationError(Arc::new(
                        std::io::Error::other("Failed to allocate solid mesh"),
                    )));
                };

                let tint = if fill_mode == FillMode::TransparentFillMajorWireframe {
                    // Make the solid fill transparent so the wireframe shows through.
                    #[expect(clippy::disallowed_methods)]
                    re_renderer::Color32::from_rgba_unmultiplied(
                        color.r(),
                        color.g(),
                        color.b(),
                        color.a() / 4, // TODO(emilk): make configurabe?
                    )
                } else {
                    color
                };

                self.solid_instances.push(GpuMeshInstance {
                    gpu_mesh: solid_mesh.gpu_mesh,
                    world_from_mesh: world_from_instance,
                    outline_mask_ids: ent_context.highlight.index_outline_mask(instance),
                    picking_layer_id: re_view::picking_layer_id_from_instance_path_hash(
                        InstancePathHash::instance(entity_path, instance),
                    ),
                    additive_tint: tint,
                    cull_mode: if tint.is_opaque() {
                        Some(re_renderer::external::wgpu::Face::Back)
                    } else {
                        None
                    },
                });
            }
        }

        self.data.add_bounding_box(
            entity_path.hash(),
            world_space_bounding_box,
            glam::Affine3A::IDENTITY,
        );

        self.data.ui_labels.extend(process_labels_3d(
            LabeledBatch {
                entity_path,
                visualizer_instruction: ent_context.visualizer_instruction,
                num_instances,
                overall_position: world_space_bounding_box.center(),
                instance_positions: target_from_instances
                    .iter()
                    .chain(std::iter::repeat(target_from_instances.last()))
                    .map(|t| t.translation.as_vec3()),
                labels: batch.labels,
                colors: &colors,
                show_labels: batch
                    .show_labels
                    .unwrap_or_else(|| typed_fallback_for(query_context, show_labels_component)),
                annotation_infos: &annotation_infos,
            },
            glam::Affine3A::IDENTITY,
        ));

        Ok(())
    }

    /// Final operation. Produce the [`re_renderer::QueueableDrawData`] to actually be drawn.
    pub fn into_draw_data(
        self,
    ) -> Result<Vec<re_renderer::QueueableDrawData>, ViewSystemExecutionError> {
        let Self {
            data: _,
            line_builder,
            line_batch_debug_label: _,
            solid_instances,
            query: _,
            render_ctx,
            output: _,
        } = self;
        let wireframe_draw_data: re_renderer::QueueableDrawData =
            line_builder.into_draw_data()?.into();

        let solid_draw_data: Option<re_renderer::QueueableDrawData> =
            match re_renderer::renderer::MeshDrawData::new(render_ctx, &solid_instances) {
                Ok(draw_data) => Some(draw_data.into()),
                Err(err) => {
                    re_log::error_once!(
                        "Failed to create mesh draw data from mesh instances: {err}"
                    );
                    None
                }
            };

        Ok([solid_draw_data, Some(wireframe_draw_data)]
            .into_iter()
            .flatten()
            .collect())
    }
}