Skip to main content

spatialrust_core/
metadata.rs

1use spatialrust_math::Vec3;
2
3/// Coordinate frame identifier.
4#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct FrameId(pub String);
7
8impl FrameId {
9    /// Creates a new frame identifier.
10    #[must_use]
11    pub fn new(value: impl Into<String>) -> Self {
12        Self(value.into())
13    }
14}
15
16/// Timestamp in nanoseconds since an arbitrary epoch.
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Timestamp(pub u64);
20
21impl Timestamp {
22    /// Creates a timestamp from nanoseconds.
23    #[must_use]
24    pub const fn from_nanos(value: u64) -> Self {
25        Self(value)
26    }
27
28    /// Returns the timestamp in nanoseconds.
29    #[must_use]
30    pub const fn as_nanos(self) -> u64 {
31        self.0
32    }
33}
34
35/// Spatial metadata attached to point clouds and maps.
36#[derive(Clone, Debug, Default, PartialEq)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38pub struct SpatialMetadata {
39    /// Coordinate frame identifier.
40    pub frame_id: FrameId,
41    /// Capture or observation timestamp.
42    pub timestamp: Timestamp,
43    /// Sensor origin in the frame.
44    pub sensor_origin: Option<Vec3<f32>>,
45    /// Length unit, defaulting to meters.
46    pub unit: String,
47}
48
49impl SpatialMetadata {
50    /// Creates metadata with the given frame and timestamp.
51    #[must_use]
52    pub fn new(frame_id: impl Into<FrameId>, timestamp: Timestamp) -> Self {
53        Self { frame_id: frame_id.into(), timestamp, unit: "meter".to_owned(), ..Self::default() }
54    }
55}
56
57impl From<String> for FrameId {
58    fn from(value: String) -> Self {
59        Self(value)
60    }
61}
62
63impl From<&str> for FrameId {
64    fn from(value: &str) -> Self {
65        Self(value.to_owned())
66    }
67}