uvt 0.1.1

Utilities for interacting with Uncrewed Vehicle Trajectory (UVT) files.
Documentation
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
//! # uvt
//!
//! The `uvt` crate provides utilities for working with _Uncrewed Vehicle Trajectory_ (UVT) files.
//!
//! UVT files combine:
//! - A Lidar map of the environment (stored in VTK format).
//! - A trajectory (poses over time), extracted from ROS bag (`.bag`) or MCAP (`.mcap`) recordings.
//!
//! ## Features
//! - Read/write `.uvt` files
//! - Extract map and trajectory data from `.bag` and `.mcap` logs
//!
//! ## Example
//! ```no_run
//! use uvt::Uvt;
//!
//! // Generate a UVt from a bag file
//! let uvt = Uvt::read_rosbag("dataset.bag", "/map", "/trajectory").unwrap();
//!
//! // Write it back to UVT format
//! uvt.write_file("output.uvt").unwrap();
//! ```
use rayon::prelude::*;
use std::io::{Error, ErrorKind};
use std::path;
use std::{fs, time::Duration};

extern crate mcap as mcap_crate;

use rosbag::{ChunkRecord, IndexRecord, MessageRecord, RosBag};
use tqdm::Iter;
use vtkio::Vtk;

mod bag;
mod deserialization;
mod mcap;
mod pointcloud;
pub mod pose;
mod trajectory;
pub use pose::Point;

use memmap2::Mmap;

const TRAJ_DELIM: &str = "#############################";

/// A UVT (_Uncrewed Vehicle Trajectory_)
///
/// Contains:
/// - A map of the environment (`vtkio::Vtk`)
/// - A trajectory (sequence of stamped poses)
pub struct Uvt {
    /// The environment map
    pub map: vtkio::Vtk,
    /// The vehicle's trajectory, saved as a sequence of stamped poses.
    pub trajectory: Vec<pose::PoseStamped>,
}

impl Uvt {
    /// Read a UVT file from disk.
    /// A UVT file contains both a VTK map and a trajectory.
    ///
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the UVT file.
    ///
    /// # Example
    /// ```no_run
    /// use uvt::Uvt;
    ///
    /// let my_uvt = Uvt::read_file("my_uvt.uvt").unwrap();
    /// ```
    ///
    /// # Errors
    /// Returns an error if:
    /// - The file cannot be read
    /// - The VTK or trajectory data is malformed
    /// = The UVT file does not follow the UVT format
    ///
    /// # Example
    /// ```no_run
    /// let uvt = Uvt::read_file("my_file.uvt").unwrap();
    /// ```
    pub fn read_file<P: AsRef<path::Path>>(path: P) -> Result<Self, Error> {
        let fpath = path.as_ref();
        let content = fs::read_to_string(fpath)?;

        println!(
            "Reading uvt file in {}",
            path::absolute(fpath).unwrap().display()
        );

        let delimiter = content.find(TRAJ_DELIM).ok_or(Error::new(
            ErrorKind::InvalidData,
            "Could not find Trajectory delimiter in UVT file",
        ))?;
        let vtk_str = content[..delimiter].trim();
        let traj_str = content[delimiter + TRAJ_DELIM.len()..].trim();

        let vtk_file =
            Vtk::parse_legacy_be(vtk_str.as_bytes()).expect(&format!("Failed to parse vtk"));

        let frame_id = traj_str
            .lines()
            .next()
            .unwrap()
            .split_once(":")
            .expect("Expected frame_id line following 'frame_id : <value>'")
            .1
            .trim();

        let trajectory: Vec<pose::PoseStamped> = traj_str
            .lines()
            .skip(1)
            .enumerate()
            .map(|(i, line)| {
                let values: Vec<f64> = line
                    .split(",")
                    .map(|n| {
                        n.trim().parse::<f64>().unwrap_or_else(|_| {
                            panic!("Failed to parse floats in line {}: '{}'", i + 2, line)
                        })
                    })
                    .collect::<Vec<f64>>();
                if values.len() != 6 {
                    panic!(
                        "Line {}: expected 6 values, got {} - '{}'",
                        i + 2,
                        values.len(),
                        line
                    );
                }

                // TODO: Get more info, with time
                let header = pose::Header {
                    frame_id: frame_id.to_string(),
                    seq: (i + 2) as u32,
                    stamp: Duration::from_secs(0).into(),
                };

                pose::PoseStamped::new(
                    header,
                    pose::Pose::from_6dof((
                        values[0], values[1], values[2], // X, Y, Z
                        values[3], values[4], values[5], // Roll, Pitch, Yaw
                    )),
                )
            })
            .collect();

        Ok(Self {
            map: vtk_file,
            trajectory: trajectory,
        })
    }

    /// Retrieves messages for a given topic from a ROS bag.
    ///
    /// This internal method extracts messages that match a specified topic.
    ///
    /// # Arguments
    ///
    /// * `bag` - A reference to a `RosBag` instance.
    /// * `topic` - The name of the topic for which to retrieve messages.
    ///
    /// # Returns
    ///
    /// A vector of message data as byte vectors.
    fn retrieve_topic_messages<'a>(bag: &'a RosBag, topic: &str) -> Vec<Vec<u8>> {
        let connections: Vec<_> = bag
            .index_records()
            .filter_map(Result::ok)
            .filter_map(|record| match record {
                IndexRecord::Connection(conn) => Some(conn),
                _ => None,
            })
            .collect();

        let topic_conns: Vec<_> = connections
            .iter()
            .filter(|conn| conn.topic == topic)
            .collect();

        let conn_id = topic_conns[0].id;

        let topic_msgs: Vec<Vec<u8>> = bag
            .chunk_records()
            .filter_map(Result::ok)
            .filter_map(|record| match record {
                ChunkRecord::Chunk(chunk) => Some(chunk),
                _ => None,
            })
            .map(|chunk| {
                chunk
                    .messages()
                    .filter_map(Result::ok)
                    .filter_map(|msg| match msg {
                        MessageRecord::MessageData(msg_data) => Some(msg_data.clone()),
                        _ => None,
                    })
                    .filter(|msg| msg.conn_id == conn_id)
                    .map(|msg| msg.data.to_vec())
                    .collect::<Vec<Vec<u8>>>()
            })
            .flatten()
            .collect();
        topic_msgs
    }

    /// Reads a ROS bag file and extracts UVT data.
    ///
    /// The method reads messages for the map and trajectory topics, parses pointcloud data,
    /// and constructs a VTK map using the last pointcloud.
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the ROS bag file.
    /// * `map_topic` - The topic name for map messages.
    /// * `traj_topic` - The topic name for trajectory messages.
    ///
    /// # Returns
    ///
    /// A `Uvt` instance containing the map and trajectory.
    ///
    /// # Errors
    ///
    /// Returns an error if the ROS bag file cannot be read or parsed.
    ///
    /// # Example
    /// ```no_run
    /// let uvt = Uvt::read_rosbag("my_file.bag", "/map", "/odom").unwrap();
    /// ```
    pub fn read_rosbag<P: AsRef<path::Path>>(
        path: P,
        map_topic: &str,
        traj_topic: &str,
    ) -> Result<Self, Error> {
        let absolute_path = path::absolute(&path).unwrap();

        println!("Reading rosbag file in {}", absolute_path.clone().display());

        let fname = absolute_path.file_name().unwrap().to_str().unwrap();

        let bag = RosBag::new(path)?;

        let map_msgs = Self::retrieve_topic_messages(&bag, map_topic);
        let traj_msgs = Self::retrieve_topic_messages(&bag, traj_topic);

        // Collect maps and trajectory
        let maps: Vec<pointcloud::PointCloud2> = map_msgs
            .iter()
            .tqdm()
            .desc(Some("Reading map msgs"))
            .map(|msg| {
                pointcloud::parse_pointcloud::<bag::BagDeserializer>(bag::BagDeserializer::new(
                    msg.to_vec(),
                ))
                .unwrap()
            })
            .collect();
        let trajectory: Vec<pose::PoseStamped> = traj_msgs
            .iter()
            .tqdm()
            .desc(Some("Reading trajectory msgs"))
            .map(|msg| {
                trajectory::parse_trajectory::<bag::BagDeserializer>(bag::BagDeserializer::new(
                    msg.to_vec(),
                ))
                .unwrap()
            })
            .collect();

        // Retrieve points from pointclouds
        let pointclouds: Vec<Vec<pose::Point>> =
            maps.par_iter().map(|m| m.to_owned().into()).collect();
        println!("Retrieved points from pointclouds");

        // Use last pointcloud as the map
        let last_pcloud = pointclouds[pointclouds.len() - 1].clone();
        let pts: Vec<f32> = last_pcloud
            .par_iter()
            .map(|&pt| Into::<[f32; 3]>::into(pt))
            .flatten()
            .collect::<Vec<f32>>()
            .try_into()
            .unwrap();
        let data = vtkio::model::DataSet::inline(vtkio::model::PolyDataPiece {
            points: vtkio::IOBuffer::F32(pts),
            verts: None,
            lines: None,
            polys: None,
            strips: None,
            data: vtkio::model::Attributes::new(),
        });

        let map_vtk = Vtk {
            version: vtkio::model::Version { major: 3, minor: 0 },
            byte_order: vtkio::model::ByteOrder::BigEndian,
            title: String::from(format!("UVT file generated from {}", fname)),
            file_path: None,
            data: data,
        };

        Ok(Self {
            map: map_vtk,
            trajectory: trajectory,
        })
    }

    /// Retrieves messages for a given topic from an MCAP file.
    ///
    /// This internal method reads an MCAP memory-mapped file and extracts the messages
    /// matching the specified topic.
    ///
    /// # Arguments
    ///
    /// * `mcap_map` - A memory-mapped representation of an MCAP file.
    /// * `topic` - The name of the topic for which to retrieve messages.
    ///
    /// # Returns
    ///
    /// A vector of message data as byte vectors.
    fn retrieve_mcap_topic_messages<'a>(mcap_map: &Mmap, topic: &str) -> Vec<Vec<u8>> {
        let messages = mcap_crate::MessageStream::new(&mcap_map).unwrap();
        let topic_msgs = messages
            .into_iter()
            .filter_map(|stream_msg| {
                let msg = stream_msg.unwrap();
                let msg_topic = msg.channel.topic.as_str();
                if msg_topic == topic {
                    Some(msg.data.to_vec())
                } else {
                    None
                }
            })
            .collect();
        topic_msgs
    }

    /// Reads an MCAP file and extracts UVT data.
    ///
    /// The method reads messages for the map and trajectory topics, parses pointcloud data,
    /// and constructs a VTK map using the last pointcloud.
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the MCAP file.
    /// * `map_topic` - The topic name for map messages.
    /// * `traj_topic` - The topic name for trajectory messages.
    ///
    /// # Returns
    ///
    /// A `Uvt` instance containing the map and trajectory.
    ///
    /// # Errors
    ///
    /// Returns an error if the MCAP file cannot be read or parsed.
    ///
    /// # Example
    /// ```no_run
    /// let uvt = Uvt::read_mcap("my_file.mcap", "/map", "/odom").unwrap();
    /// ```
    pub fn read_mcap<P: AsRef<path::Path>>(
        path: P,
        map_topic: &str,
        traj_topic: &str,
    ) -> Result<Self, Error> {
        let absolute_path = path::absolute(&path).unwrap();
        println!("Reading MCAP file in {}", absolute_path.clone().display());

        let fname = absolute_path.file_name().unwrap().to_str().unwrap();

        let fd = fs::File::open(path.as_ref()).expect("Couldn't open MCAP file");
        let mapped = unsafe { Mmap::map(&fd) }?;
        println!("MCAP file opened !");

        let map_msgs = Self::retrieve_mcap_topic_messages(&mapped, map_topic);
        let traj_msgs = Self::retrieve_mcap_topic_messages(&mapped, traj_topic);

        // Collect maps and trajectory
        let maps: Vec<pointcloud::PointCloud2> = map_msgs
            .iter()
            .tqdm()
            .desc(Some("Reading map msgs"))
            .map(|msg| {
                pointcloud::parse_pointcloud::<mcap::McapDeserializer>(mcap::McapDeserializer::new(
                    msg.to_vec(),
                ))
                .unwrap()
            })
            .collect();
        let trajectory: Vec<pose::PoseStamped> = traj_msgs
            .iter()
            .tqdm()
            .desc(Some("Reading trajectory msgs"))
            .map(|msg| {
                trajectory::parse_trajectory::<mcap::McapDeserializer>(mcap::McapDeserializer::new(
                    msg.to_vec(),
                ))
                .unwrap()
            })
            .collect();

        // Retrieve points from pointclouds
        let pointclouds: Vec<Vec<pose::Point>> =
            maps.par_iter().map(|m| m.to_owned().into()).collect();
        println!("Retrieved points from pointclouds");

        // Use last pointcloud as the map
        let last_pcloud = pointclouds[pointclouds.len() - 1].clone();
        let pts: Vec<f32> = last_pcloud
            .par_iter()
            .map(|&pt| Into::<[f32; 3]>::into(pt))
            .flatten()
            .collect::<Vec<f32>>()
            .try_into()
            .unwrap();
        let data = vtkio::model::DataSet::inline(vtkio::model::PolyDataPiece {
            points: vtkio::IOBuffer::F32(pts),
            verts: None,
            lines: None,
            polys: None,
            strips: None,
            data: vtkio::model::Attributes::new(),
        });

        let map_vtk = Vtk {
            version: vtkio::model::Version { major: 3, minor: 0 },
            byte_order: vtkio::model::ByteOrder::BigEndian,
            title: String::from(format!("UVT file generated from {}", fname)),
            file_path: None,
            data: data,
        };

        Ok(Self {
            map: map_vtk,
            trajectory: trajectory,
        })
    }

    /// Writes the UVT data (map and trajectory) to a file.
    ///
    /// The output file contains a VTK map encoded in legacy ASCII format,
    /// followed by a delimiter and the trajectory data.
    ///
    /// # Arguments
    ///
    /// * `path` - The destination file path.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the file was written successfully, or an `Error` otherwise.
    pub fn write_file<P: AsRef<path::Path>>(&self, path: P) -> Result<(), std::io::Error> {
        let export_path = path::absolute(path)?.clone();
        println!("Writing file to {}", export_path.display());

        //
        // Map
        //
        let mut map_str = String::new();
        Vtk::write_legacy_ascii(self.map.clone(), &mut map_str)
            .expect(&format!("Failed to write file"));

        //
        // Trajectory
        //
        let uvt_trajectory = self.trajectory.clone();

        // Retrieve frame ID
        let frame_id = uvt_trajectory
            .first()
            .ok_or(Error::new(ErrorKind::InvalidData, "Missing poses"))?
            .header
            .frame_id
            .clone();
        let frame_str = format!("frame_id : {}", frame_id);

        let traj_poses: Vec<String> = uvt_trajectory
            .into_iter()
            .map(|pose| {
                let dofs = pose.pose.to_6dof();
                let x = pose::round(dofs.0, 6);
                let y = pose::round(dofs.1, 6);
                let z = pose::round(dofs.2, 6);
                let roll = pose::round(dofs.3, 6);
                let pitch = pose::round(dofs.4, 6);
                let yaw = pose::round(dofs.5, 6);

                format!("{x},{y},{z},{roll},{pitch},{yaw}")
            })
            .collect();
        let traj_str = traj_poses.join("\n");
        let uvt_str = vec![map_str, TRAJ_DELIM.to_string(), frame_str, traj_str].join("\n");

        fs::write(export_path, uvt_str)?;

        Ok(())
    }
}