1use bincode::de::Decoder;
2use bincode::error::DecodeError;
3use bincode::{Decode, Encode};
4use core::fmt::Debug;
5use cu_sensor_payloads::{CuDepthInteger, CuDepthMap, CuDepthMillimeter};
6use cu_transform::FrameTransform;
7use cu29::prelude::*;
8use serde::{Deserialize, Serialize, Serializer};
9
10#[derive(Default, Debug, Encode, Decode, Clone, Copy, Serialize, Deserialize, Reflect)]
11pub struct ZedRasterFormat {
12 pub width: u32,
13 pub height: u32,
14 pub stride: u32,
15}
16
17impl ZedRasterFormat {
18 pub fn len_elements(&self) -> usize {
19 self.stride as usize * self.height as usize
20 }
21}
22
23macro_rules! impl_f32_raster_payload {
24 ($name:ident, $type_path:literal, $ident:literal) => {
25 #[derive(Debug, Default, Clone, Encode, Reflect)]
26 #[reflect(from_reflect = false, no_field_bounds, type_path = false)]
27 pub struct $name<A>
28 where
29 A: ArrayLike<Element = f32> + Send + Sync + 'static,
30 {
31 pub seq: u64,
32 pub format: ZedRasterFormat,
33 #[reflect(ignore)]
34 pub buffer_handle: CuHandle<A>,
35 }
36
37 impl<A> TypePath for $name<A>
38 where
39 A: ArrayLike<Element = f32> + Send + Sync + 'static,
40 {
41 fn type_path() -> &'static str {
42 $type_path
43 }
44
45 fn short_type_path() -> &'static str {
46 $ident
47 }
48
49 fn type_ident() -> Option<&'static str> {
50 Some($ident)
51 }
52
53 fn crate_name() -> Option<&'static str> {
54 Some("cu_zed")
55 }
56
57 fn module_path() -> Option<&'static str> {
58 Some("cu_zed")
59 }
60 }
61
62 impl<A> Decode<()> for $name<A>
63 where
64 A: ArrayLike<Element = f32> + Send + Sync + 'static,
65 CuHandle<A>: Decode<()>,
66 {
67 fn decode<D: Decoder<Context = ()>>(decoder: &mut D) -> Result<Self, DecodeError> {
68 let seq: u64 = Decode::decode(decoder)?;
69 let format: ZedRasterFormat = Decode::decode(decoder)?;
70 let buffer_handle: CuHandle<A> = Decode::decode(decoder)?;
71
72 Ok(Self {
73 seq,
74 format,
75 buffer_handle,
76 })
77 }
78 }
79
80 impl<'de, A> Deserialize<'de> for $name<A>
81 where
82 A: ArrayLike<Element = f32> + Send + Sync + 'static,
83 CuHandle<A>: Deserialize<'de>,
84 {
85 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
86 where
87 D: serde::Deserializer<'de>,
88 {
89 #[derive(Deserialize)]
90 struct Wire<H> {
91 seq: u64,
92 format: ZedRasterFormat,
93 handle: H,
94 }
95
96 let wire = Wire::<CuHandle<A>>::deserialize(deserializer)?;
97 Ok(Self {
98 seq: wire.seq,
99 format: wire.format,
100 buffer_handle: wire.handle,
101 })
102 }
103 }
104
105 impl<A> Serialize for $name<A>
106 where
107 A: ArrayLike<Element = f32> + Send + Sync + 'static,
108 CuHandle<A>: Serialize,
109 {
110 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
111 where
112 S: Serializer,
113 {
114 use serde::ser::SerializeStruct;
115 let mut struct_ = serializer.serialize_struct($ident, 3)?;
116 struct_.serialize_field("seq", &self.seq)?;
117 struct_.serialize_field("format", &self.format)?;
118 struct_.serialize_field("handle", &self.buffer_handle)?;
119 struct_.end()
120 }
121 }
122
123 impl<A> $name<A>
124 where
125 A: ArrayLike<Element = f32> + Send + Sync + 'static,
126 {
127 pub fn new(format: ZedRasterFormat, buffer_handle: CuHandle<A>) -> Self {
128 assert!(
129 format.len_elements() <= buffer_handle.with_inner(|i| i.len()),
130 "Buffer size must at least match the format."
131 );
132 Self {
133 seq: 0,
134 format,
135 buffer_handle,
136 }
137 }
138 }
139 };
140}
141
142impl_f32_raster_payload!(
143 ZedConfidenceMap,
144 "cu_zed::ZedConfidenceMap",
145 "ZedConfidenceMap"
146);
147
148pub type ZedDepthMap = CuDepthMap<Vec<u16>, CuDepthInteger<u16, CuDepthMillimeter>>;
150
151#[derive(
152 Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Encode, Decode, Reflect,
153)]
154#[reflect(from_reflect = false)]
155pub enum ZedCoordinateSystem {
156 Image,
157 #[default]
158 LeftHandedYUp,
159 RightHandedYUp,
160 RightHandedZUp,
161 LeftHandedZUp,
162 RightHandedZUpXForward,
163}
164
165#[derive(
166 Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Encode, Decode, Reflect,
167)]
168#[reflect(from_reflect = false)]
169pub enum ZedCoordinateUnit {
170 Millimeter,
171 Centimeter,
172 #[default]
173 Meter,
174 Inch,
175 Foot,
176}
177
178#[derive(Default, Clone, Debug, Serialize, Deserialize, Encode, Decode, Reflect)]
179#[reflect(from_reflect = false)]
180pub struct ZedCameraIntrinsics {
181 pub fx: f32,
182 pub fy: f32,
183 pub cx: f32,
184 pub cy: f32,
185 pub disto: [f64; 12],
186 pub v_fov: f32,
187 pub h_fov: f32,
188 pub d_fov: f32,
189 pub width: u32,
190 pub height: u32,
191 pub focal_length_metric: f32,
192}
193
194#[derive(Default, Clone, Debug, Serialize, Deserialize, Encode, Decode, Reflect)]
195#[reflect(from_reflect = false)]
196pub struct ZedCalibrationBundle {
197 pub serial_number: u32,
198 pub width: u32,
199 pub height: u32,
200 pub fps: f32,
201 pub coordinate_system: ZedCoordinateSystem,
202 pub coordinate_unit: ZedCoordinateUnit,
203 pub left: ZedCameraIntrinsics,
204 pub right: ZedCameraIntrinsics,
205 pub stereo_rotation_rodrigues: [f32; 3],
206 pub stereo_translation_m: [f32; 3],
207 pub camera_to_imu_translation_m: Option<[f32; 3]>,
208 pub camera_to_imu_quaternion_xyzw: Option<[f32; 4]>,
209}
210
211#[derive(Default, Clone, Debug, Serialize, Deserialize, Encode, Decode, Reflect)]
212#[reflect(from_reflect = false)]
213pub struct ZedFrameMeta {
214 pub seq: u64,
215 pub image_timestamp_ns: u64,
216 pub current_timestamp_ns: u64,
217 pub current_fps: f32,
218 pub camera_moving_state: Option<i32>,
219 pub image_sync_trigger: Option<i32>,
220 pub imu_temp_c: Option<f32>,
221 pub barometer_temp_c: Option<f32>,
222 pub onboard_left_temp_c: Option<f32>,
223 pub onboard_right_temp_c: Option<f32>,
224}
225
226#[derive(Default, Clone, Debug, Serialize, Deserialize, Encode, Decode, Reflect)]
227#[reflect(from_reflect = false)]
228pub struct ZedNamedTransform {
229 pub matrix: [[f32; 4]; 4],
230 pub parent_frame: String,
231 pub child_frame: String,
232}
233
234impl ZedNamedTransform {
235 pub fn from_frame_transform(transform: &FrameTransform<f32>) -> Self {
236 Self {
237 matrix: transform.transform.to_matrix(),
238 parent_frame: transform.parent_frame.as_str().to_string(),
239 child_frame: transform.child_frame.as_str().to_string(),
240 }
241 }
242
243 pub fn to_transform3d(&self) -> cu_transform::Transform3D<f32> {
244 cu_transform::Transform3D::from_matrix(self.matrix)
245 }
246}
247
248#[derive(Default, Clone, Debug, Serialize, Deserialize, Encode, Decode, Reflect)]
249#[reflect(from_reflect = false)]
250pub struct ZedRigTransforms {
251 pub left_to_right: ZedNamedTransform,
252 pub camera_to_imu: ZedNamedTransform,
253 pub has_camera_to_imu: bool,
254}