|
| 1 | +use anyhow::Context as _; |
| 2 | +use arrow::array::{ |
| 3 | + FixedSizeListArray, FixedSizeListBuilder, Float64Builder, Int8Builder, UInt8Builder, |
| 4 | + UInt16Builder, |
| 5 | +}; |
| 6 | +use re_chunk::{Chunk, ChunkId}; |
| 7 | +use re_log_types::TimeCell; |
| 8 | +use re_types::{ |
| 9 | + ComponentDescriptor, SerializedComponentColumn, archetypes::GeoPoints, components::LatLon, |
| 10 | +}; |
| 11 | + |
| 12 | +use crate::parsers::{ |
| 13 | + cdr, |
| 14 | + decode::{MessageParser, ParserContext}, |
| 15 | + ros2msg::definitions::sensor_msgs, |
| 16 | + util::fixed_size_list_builder, |
| 17 | +}; |
| 18 | + |
| 19 | +/// Plugin that parses `sensor_msgs/msg/NavSatFix` messages. |
| 20 | +#[derive(Default)] |
| 21 | +pub struct NavSatFixSchemaPlugin; |
| 22 | + |
| 23 | +pub struct NavSatFixMessageParser { |
| 24 | + geo_points: Vec<LatLon>, |
| 25 | + latitude: FixedSizeListBuilder<Float64Builder>, |
| 26 | + longitude: FixedSizeListBuilder<Float64Builder>, |
| 27 | + altitude: FixedSizeListBuilder<Float64Builder>, |
| 28 | + status: FixedSizeListBuilder<Int8Builder>, |
| 29 | + service: FixedSizeListBuilder<UInt16Builder>, |
| 30 | + position_covariance: FixedSizeListBuilder<Float64Builder>, |
| 31 | + position_covariance_type: FixedSizeListBuilder<UInt8Builder>, |
| 32 | +} |
| 33 | + |
| 34 | +impl NavSatFixMessageParser { |
| 35 | + const ARCHETYPE_NAME: &str = "sensor_msgs.msg.NavSatFix"; |
| 36 | + |
| 37 | + pub fn new(num_rows: usize) -> Self { |
| 38 | + Self { |
| 39 | + geo_points: Vec::with_capacity(num_rows), |
| 40 | + latitude: fixed_size_list_builder(1, num_rows), |
| 41 | + longitude: fixed_size_list_builder(1, num_rows), |
| 42 | + altitude: fixed_size_list_builder(1, num_rows), |
| 43 | + status: fixed_size_list_builder(1, num_rows), |
| 44 | + service: fixed_size_list_builder(1, num_rows), |
| 45 | + position_covariance: fixed_size_list_builder(9, num_rows), |
| 46 | + position_covariance_type: fixed_size_list_builder(1, num_rows), |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn create_metadata_column(name: &str, array: FixedSizeListArray) -> SerializedComponentColumn { |
| 51 | + SerializedComponentColumn { |
| 52 | + list_array: array.into(), |
| 53 | + descriptor: ComponentDescriptor::partial(name) |
| 54 | + .with_archetype(Self::ARCHETYPE_NAME.into()), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl MessageParser for NavSatFixMessageParser { |
| 60 | + fn append(&mut self, ctx: &mut ParserContext, msg: &mcap::Message<'_>) -> anyhow::Result<()> { |
| 61 | + re_tracing::profile_function!(); |
| 62 | + let sensor_msgs::NavSatFix { |
| 63 | + header, |
| 64 | + status, |
| 65 | + latitude, |
| 66 | + longitude, |
| 67 | + altitude, |
| 68 | + position_covariance, |
| 69 | + position_covariance_type, |
| 70 | + } = cdr::try_decode_message::<sensor_msgs::NavSatFix>(&msg.data) |
| 71 | + .context("Failed to decode sensor_msgs::NavSatFix message from CDR data")?; |
| 72 | + |
| 73 | + // add the sensor timestamp to the context, `log_time` and `publish_time` are added automatically |
| 74 | + ctx.add_time_cell( |
| 75 | + "timestamp", |
| 76 | + TimeCell::from_timestamp_nanos_since_epoch(header.stamp.as_nanos()), |
| 77 | + ); |
| 78 | + |
| 79 | + // Store latitude/longitude as geographic points |
| 80 | + let geo_point = LatLon::new(latitude, longitude); |
| 81 | + self.geo_points.push(geo_point); |
| 82 | + |
| 83 | + self.latitude.values().append_slice(&[latitude]); |
| 84 | + self.latitude.append(true); |
| 85 | + |
| 86 | + self.longitude.values().append_slice(&[longitude]); |
| 87 | + self.longitude.append(true); |
| 88 | + |
| 89 | + self.altitude.values().append_slice(&[altitude]); |
| 90 | + self.altitude.append(true); |
| 91 | + |
| 92 | + self.status.values().append_slice(&[status.status as i8]); |
| 93 | + self.status.append(true); |
| 94 | + |
| 95 | + self.service.values().append_slice(&[status.service as u16]); |
| 96 | + self.service.append(true); |
| 97 | + |
| 98 | + self.position_covariance |
| 99 | + .values() |
| 100 | + .append_slice(&position_covariance); |
| 101 | + self.position_covariance.append(true); |
| 102 | + |
| 103 | + self.position_covariance_type |
| 104 | + .values() |
| 105 | + .append_slice(&[position_covariance_type as u8]); |
| 106 | + self.position_covariance_type.append(true); |
| 107 | + |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | + |
| 111 | + fn finalize(self: Box<Self>, ctx: ParserContext) -> anyhow::Result<Vec<re_chunk::Chunk>> { |
| 112 | + re_tracing::profile_function!(); |
| 113 | + let Self { |
| 114 | + geo_points, |
| 115 | + mut latitude, |
| 116 | + mut longitude, |
| 117 | + mut altitude, |
| 118 | + mut status, |
| 119 | + mut service, |
| 120 | + mut position_covariance, |
| 121 | + mut position_covariance_type, |
| 122 | + } = *self; |
| 123 | + |
| 124 | + let entity_path = ctx.entity_path().clone(); |
| 125 | + let timelines = ctx.build_timelines(); |
| 126 | + |
| 127 | + let mut chunk_components: Vec<_> = GeoPoints::update_fields() |
| 128 | + .with_positions(geo_points) |
| 129 | + .columns_of_unit_batches()? |
| 130 | + .collect(); |
| 131 | + |
| 132 | + chunk_components.extend([ |
| 133 | + Self::create_metadata_column("latitude", latitude.finish()), |
| 134 | + Self::create_metadata_column("longitude", longitude.finish()), |
| 135 | + Self::create_metadata_column("altitude", altitude.finish()), |
| 136 | + Self::create_metadata_column("status", status.finish()), |
| 137 | + Self::create_metadata_column("service", service.finish()), |
| 138 | + Self::create_metadata_column("position_covariance", position_covariance.finish()), |
| 139 | + Self::create_metadata_column( |
| 140 | + "position_covariance_type", |
| 141 | + position_covariance_type.finish(), |
| 142 | + ), |
| 143 | + ]); |
| 144 | + |
| 145 | + Ok(vec![Chunk::from_auto_row_ids( |
| 146 | + ChunkId::new(), |
| 147 | + entity_path.clone(), |
| 148 | + timelines.clone(), |
| 149 | + chunk_components.into_iter().collect(), |
| 150 | + )?]) |
| 151 | + } |
| 152 | +} |
0 commit comments