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
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

#![allow(trivial_numeric_casts)]
#![allow(unused_parens)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::iter_on_single_items)]
#![allow(clippy::map_flatten)]
#![allow(clippy::match_wildcard_for_single_variants)]
#![allow(clippy::needless_question_mark)]
#![allow(clippy::redundant_closure)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::unnecessary_cast)]

/// A 3D point cloud with positions and optional colors, radii, labels, etc.
///
/// ## Example
///
/// ```ignore
/// //! Log some very simple points.
/// use rerun::{archetypes::Points3D, MsgSender, RecordingStreamBuilder};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///    let (rec_stream, storage) = RecordingStreamBuilder::new("points").memory()?;
///
///    MsgSender::from_archetype("points", &Points3D::new([(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]))?
///        .send(&rec_stream)?;
///
///    rerun::native_viewer::show(storage.take())?;
///    Ok(())
/// }
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Points3D {
    /// All the actual 3D points that make up the point cloud.
    pub points: Vec<crate::components::Point3D>,

    /// Optional radii for the points, effectively turning them into circles.
    pub radii: Option<Vec<crate::components::Radius>>,

    /// Optional colors for the points.
    pub colors: Option<Vec<crate::components::Color>>,

    /// Optional text labels for the points.
    pub labels: Option<Vec<crate::components::Label>>,

    /// Optional class Ids for the points.
    ///
    /// The class ID provides colors and labels if not specified explicitly.
    pub class_ids: Option<Vec<crate::components::ClassId>>,

    /// Optional keypoint IDs for the points, identifying them within a class.
    ///
    /// If keypoint IDs are passed in but no class IDs were specified, the class ID will
    /// default to 0.
    /// This is useful to identify points within a single classification (which is identified
    /// with `class_id`).
    /// E.g. the classification might be 'Person' and the keypoints refer to joints on a
    /// detected skeleton.
    pub keypoint_ids: Option<Vec<crate::components::KeypointId>>,

    /// Unique identifiers for each individual point in the batch.
    pub instance_keys: Option<Vec<crate::components::InstanceKey>>,
}

static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[crate::ComponentName; 1usize]> =
    once_cell::sync::Lazy::new(|| ["rerun.point3d".into()]);
static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[crate::ComponentName; 2usize]> =
    once_cell::sync::Lazy::new(|| ["rerun.radius".into(), "rerun.colorrgba".into()]);
static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[crate::ComponentName; 4usize]> =
    once_cell::sync::Lazy::new(|| {
        [
            "rerun.label".into(),
            "rerun.class_id".into(),
            "rerun.keypoint_id".into(),
            "rerun.instance_key".into(),
        ]
    });
static ALL_COMPONENTS: once_cell::sync::Lazy<[crate::ComponentName; 7usize]> =
    once_cell::sync::Lazy::new(|| {
        [
            "rerun.point3d".into(),
            "rerun.radius".into(),
            "rerun.colorrgba".into(),
            "rerun.label".into(),
            "rerun.class_id".into(),
            "rerun.keypoint_id".into(),
            "rerun.instance_key".into(),
        ]
    });

impl Points3D {
    pub const NUM_COMPONENTS: usize = 7usize;
}

impl crate::Archetype for Points3D {
    #[inline]
    fn name() -> crate::ArchetypeName {
        crate::ArchetypeName::Borrowed("rerun.archetypes.Points3D")
    }

    #[inline]
    fn required_components() -> &'static [crate::ComponentName] {
        REQUIRED_COMPONENTS.as_slice()
    }

    #[inline]
    fn recommended_components() -> &'static [crate::ComponentName] {
        RECOMMENDED_COMPONENTS.as_slice()
    }

    #[inline]
    fn optional_components() -> &'static [crate::ComponentName] {
        OPTIONAL_COMPONENTS.as_slice()
    }

    #[inline]
    fn all_components() -> &'static [crate::ComponentName] {
        ALL_COMPONENTS.as_slice()
    }

    #[inline]
    fn try_to_arrow(
        &self,
    ) -> crate::SerializationResult<
        Vec<(::arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
    > {
        use crate::Loggable as _;
        Ok([
            {
                Some({
                    let array =
                        <crate::components::Point3D>::try_to_arrow(self.points.iter(), None);
                    array.map(|array| {
                        let datatype = ::arrow2::datatypes::DataType::Extension(
                            "rerun.components.Point3D".into(),
                            Box::new(array.data_type().clone()),
                            Some("rerun.point3d".into()),
                        );
                        (
                            ::arrow2::datatypes::Field::new("points", datatype, false),
                            array,
                        )
                    })
                })
                .transpose()
                .map_err(|err| crate::SerializationError::Context {
                    location: "rerun.archetypes.Points3D#points".into(),
                    source: Box::new(err),
                })?
            },
            {
                self.radii
                    .as_ref()
                    .map(|many| {
                        let array = <crate::components::Radius>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.Radius".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.radius".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("radii", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#radii".into(),
                        source: Box::new(err),
                    })?
            },
            {
                self.colors
                    .as_ref()
                    .map(|many| {
                        let array = <crate::components::Color>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.Color".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.colorrgba".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("colors", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#colors".into(),
                        source: Box::new(err),
                    })?
            },
            {
                self.labels
                    .as_ref()
                    .map(|many| {
                        let array = <crate::components::Label>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.Label".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.label".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("labels", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#labels".into(),
                        source: Box::new(err),
                    })?
            },
            {
                self.class_ids
                    .as_ref()
                    .map(|many| {
                        let array = <crate::components::ClassId>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.ClassId".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.class_id".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("class_ids", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#class_ids".into(),
                        source: Box::new(err),
                    })?
            },
            {
                self.keypoint_ids
                    .as_ref()
                    .map(|many| {
                        let array =
                            <crate::components::KeypointId>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.KeypointId".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.keypoint_id".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("keypoint_ids", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#keypoint_ids".into(),
                        source: Box::new(err),
                    })?
            },
            {
                self.instance_keys
                    .as_ref()
                    .map(|many| {
                        let array =
                            <crate::components::InstanceKey>::try_to_arrow(many.iter(), None);
                        array.map(|array| {
                            let datatype = ::arrow2::datatypes::DataType::Extension(
                                "rerun.components.InstanceKey".into(),
                                Box::new(array.data_type().clone()),
                                Some("rerun.instance_key".into()),
                            );
                            (
                                ::arrow2::datatypes::Field::new("instance_keys", datatype, false),
                                array,
                            )
                        })
                    })
                    .transpose()
                    .map_err(|err| crate::SerializationError::Context {
                        location: "rerun.archetypes.Points3D#instance_keys".into(),
                        source: Box::new(err),
                    })?
            },
        ]
        .into_iter()
        .flatten()
        .collect())
    }

    #[inline]
    fn try_from_arrow(
        data: impl IntoIterator<Item = (::arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
    ) -> crate::DeserializationResult<Self> {
        use crate::Loggable as _;
        let arrays_by_name: ::std::collections::HashMap<_, _> = data
            .into_iter()
            .map(|(field, array)| (field.name, array))
            .collect();
        let points = {
            let array = arrays_by_name
                .get("points")
                .ok_or_else(|| crate::DeserializationError::MissingData {
                    backtrace: ::backtrace::Backtrace::new_unresolved(),
                })
                .map_err(|err| crate::DeserializationError::Context {
                    location: "rerun.archetypes.Points3D#points".into(),
                    source: Box::new(err),
                })?;
            <crate::components::Point3D>::try_from_arrow_opt(&**array)
                .map_err(|err| crate::DeserializationError::Context {
                    location: "rerun.archetypes.Points3D#points".into(),
                    source: Box::new(err),
                })?
                .into_iter()
                .map(|v| {
                    v.ok_or_else(|| crate::DeserializationError::MissingData {
                        backtrace: ::backtrace::Backtrace::new_unresolved(),
                    })
                })
                .collect::<crate::DeserializationResult<Vec<_>>>()
                .map_err(|err| crate::DeserializationError::Context {
                    location: "rerun.archetypes.Points3D#points".into(),
                    source: Box::new(err),
                })?
        };
        let radii = if let Some(array) = arrays_by_name.get("radii") {
            Some(
                <crate::components::Radius>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#radii".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#radii".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        let colors = if let Some(array) = arrays_by_name.get("colors") {
            Some(
                <crate::components::Color>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#colors".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#colors".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        let labels = if let Some(array) = arrays_by_name.get("labels") {
            Some(
                <crate::components::Label>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#labels".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#labels".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        let class_ids = if let Some(array) = arrays_by_name.get("class_ids") {
            Some(
                <crate::components::ClassId>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#class_ids".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#class_ids".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        let keypoint_ids = if let Some(array) = arrays_by_name.get("keypoint_ids") {
            Some(
                <crate::components::KeypointId>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#keypoint_ids".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#keypoint_ids".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        let instance_keys = if let Some(array) = arrays_by_name.get("instance_keys") {
            Some(
                <crate::components::InstanceKey>::try_from_arrow_opt(&**array)
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#instance_keys".into(),
                        source: Box::new(err),
                    })?
                    .into_iter()
                    .map(|v| {
                        v.ok_or_else(|| crate::DeserializationError::MissingData {
                            backtrace: ::backtrace::Backtrace::new_unresolved(),
                        })
                    })
                    .collect::<crate::DeserializationResult<Vec<_>>>()
                    .map_err(|err| crate::DeserializationError::Context {
                        location: "rerun.archetypes.Points3D#instance_keys".into(),
                        source: Box::new(err),
                    })?,
            )
        } else {
            None
        };
        Ok(Self {
            points,
            radii,
            colors,
            labels,
            class_ids,
            keypoint_ids,
            instance_keys,
        })
    }
}

impl Points3D {
    pub fn new(points: impl IntoIterator<Item = impl Into<crate::components::Point3D>>) -> Self {
        Self {
            points: points.into_iter().map(Into::into).collect(),
            radii: None,
            colors: None,
            labels: None,
            class_ids: None,
            keypoint_ids: None,
            instance_keys: None,
        }
    }

    pub fn with_radii(
        mut self,
        radii: impl IntoIterator<Item = impl Into<crate::components::Radius>>,
    ) -> Self {
        self.radii = Some(radii.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_colors(
        mut self,
        colors: impl IntoIterator<Item = impl Into<crate::components::Color>>,
    ) -> Self {
        self.colors = Some(colors.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_labels(
        mut self,
        labels: impl IntoIterator<Item = impl Into<crate::components::Label>>,
    ) -> Self {
        self.labels = Some(labels.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_class_ids(
        mut self,
        class_ids: impl IntoIterator<Item = impl Into<crate::components::ClassId>>,
    ) -> Self {
        self.class_ids = Some(class_ids.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_keypoint_ids(
        mut self,
        keypoint_ids: impl IntoIterator<Item = impl Into<crate::components::KeypointId>>,
    ) -> Self {
        self.keypoint_ids = Some(keypoint_ids.into_iter().map(Into::into).collect());
        self
    }

    pub fn with_instance_keys(
        mut self,
        instance_keys: impl IntoIterator<Item = impl Into<crate::components::InstanceKey>>,
    ) -> Self {
        self.instance_keys = Some(instance_keys.into_iter().map(Into::into).collect());
        self
    }
}