-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathcustom_data.rs
115 lines (99 loc) · 3.09 KB
/
custom_data.rs
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
//! Demonstrates how to implement custom archetypes and components, and extend existing ones.
use rerun::{
demo_util::grid,
external::{arrow, glam, re_types},
ComponentBatch, SerializedComponentBatch,
};
// ---
/// A custom [component bundle] that extends Rerun's builtin [`rerun::Points3D`] archetype with extra
/// [`rerun::Component`]s.
///
/// [component bundle]: [`AsComponents`]
struct CustomPoints3D {
points3d: rerun::Points3D,
confidences: Option<Vec<Confidence>>,
}
impl rerun::AsComponents for CustomPoints3D {
fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
self.points3d
.as_serialized_batches()
.into_iter()
.chain(
[self
.confidences
.as_ref()
.and_then(|batch| batch.serialized())
.map(|batch|
// Optionally override the descriptor with extra information.
batch
.or_with_archetype_name(|| "user.CustomPoints3D".into())
.or_with_archetype_field_name(|| "confidences".into()))]
.into_iter()
.flatten(),
)
.collect()
}
}
// ---
/// A custom [`rerun::Component`] that is backed by a builtin [`rerun::Float32`] scalar.
#[derive(Debug, Clone, Copy)]
struct Confidence(rerun::Float32);
impl From<f32> for Confidence {
fn from(v: f32) -> Self {
Self(rerun::Float32(v))
}
}
impl rerun::SizeBytes for Confidence {
#[inline]
fn heap_size_bytes(&self) -> u64 {
0
}
}
impl rerun::Loggable for Confidence {
#[inline]
fn arrow_datatype() -> arrow::datatypes::DataType {
rerun::Float32::arrow_datatype()
}
#[inline]
fn to_arrow_opt<'a>(
data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
) -> re_types::SerializationResult<arrow::array::ArrayRef>
where
Self: 'a,
{
rerun::Float32::to_arrow_opt(data.into_iter().map(|opt| opt.map(Into::into).map(|c| c.0)))
}
}
impl rerun::Component for Confidence {
#[inline]
fn descriptor() -> rerun::ComponentDescriptor {
rerun::ComponentDescriptor::new("user.Confidence")
}
}
// ---
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rec = rerun::RecordingStreamBuilder::new("rerun_example_custom_data").spawn()?;
rec.log(
"left/my_confident_point_cloud",
&CustomPoints3D {
points3d: rerun::Points3D::new(grid(
glam::Vec3::splat(-5.0),
glam::Vec3::splat(5.0),
3,
)),
confidences: Some(vec![42f32.into()]),
},
)?;
rec.log(
"right/my_polarized_point_cloud",
&CustomPoints3D {
points3d: rerun::Points3D::new(grid(
glam::Vec3::splat(-5.0),
glam::Vec3::splat(5.0),
3,
)),
confidences: Some((0..27).map(|i| i as f32).map(Into::into).collect()),
},
)?;
Ok(())
}