1use arrow2::{array::PrimitiveArray, datatypes::DataType};
2use arrow2_convert::{
3 arrow_enable_vec_for_type,
4 deserialize::ArrowDeserialize,
5 field::{ArrowField, FixedSizeVec},
6 serialize::ArrowSerialize,
7};
8
9#[derive(Copy, Clone, Debug, PartialEq)]
26#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
27pub struct Box3D {
28 pub x: f32,
29 pub y: f32,
30 pub z: f32,
31}
32
33impl Box3D {
34 #[inline]
35 pub fn new(x: f32, y: f32, z: f32) -> Self {
36 Self { x, y, z }
37 }
38}
39
40impl re_log_types::LegacyComponent for Box3D {
41 #[inline]
42 fn legacy_name() -> re_log_types::ComponentName {
43 "rerun.box3d".into()
44 }
45}
46
47#[cfg(feature = "glam")]
48impl From<Box3D> for glam::Vec3 {
49 #[inline]
50 fn from(b: Box3D) -> Self {
51 Self::new(b.x, b.y, b.z)
52 }
53}
54
55#[cfg(feature = "glam")]
56impl From<glam::Vec3> for Box3D {
57 #[inline]
58 fn from(v: glam::Vec3) -> Self {
59 let (x, y, z) = v.into();
60 Self { x, y, z }
61 }
62}
63
64arrow_enable_vec_for_type!(Box3D);
65
66impl ArrowField for Box3D {
67 type Type = Self;
68
69 #[inline]
70 fn data_type() -> DataType {
71 <FixedSizeVec<f32, 3> as ArrowField>::data_type()
72 }
73}
74
75impl ArrowSerialize for Box3D {
76 type MutableArrayType = <FixedSizeVec<f32, 3> as ArrowSerialize>::MutableArrayType;
77
78 #[inline]
79 fn new_array() -> Self::MutableArrayType {
80 FixedSizeVec::<f32, 3>::new_array()
81 }
82
83 #[inline]
84 fn arrow_serialize(v: &Self, array: &mut Self::MutableArrayType) -> arrow2::error::Result<()> {
85 array.mut_values().extend_from_slice(&[v.x, v.y, v.z]);
86 array.try_push_valid()
87 }
88}
89
90impl ArrowDeserialize for Box3D {
91 type ArrayType = <FixedSizeVec<f32, 3> as ArrowDeserialize>::ArrayType;
92
93 #[inline]
94 fn arrow_deserialize(
95 v: <&Self::ArrayType as IntoIterator>::Item,
96 ) -> Option<<Self as ArrowField>::Type> {
97 v.map(|v| {
98 let v = v
99 .as_any()
100 .downcast_ref::<PrimitiveArray<f32>>()
101 .unwrap()
102 .values()
103 .as_slice();
104 Box3D {
105 x: v[0],
106 y: v[1],
107 z: v[2],
108 }
109 })
110 }
111}
112
113re_log_types::component_legacy_shim!(Box3D);