Skip to main content

semantic_scene/scene/
object.rs

1//! Semantic object model.
2
3use std::fmt;
4
5use crate::{Aabb, Obb};
6
7/// Represents a distinct semantically annotated object.
8///
9/// The source index is also the object's semantic id.
10#[derive(Debug, Clone, PartialEq)]
11pub struct SemanticObject<ObjectCategory> {
12    index: i32,
13    category: Option<ObjectCategory>,
14    obb: Obb,
15}
16
17impl<ObjectCategory> SemanticObject<ObjectCategory> {
18    /// Creates a semantic object.
19    ///
20    /// `index` is the original descriptor index for this object.
21    #[must_use]
22    pub const fn new(index: i32, category: Option<ObjectCategory>, obb: Obb) -> Self {
23        Self {
24            index,
25            category,
26            obb,
27        }
28    }
29
30    /// Returns the original descriptor index.
31    #[must_use]
32    pub const fn index(&self) -> i32 {
33        self.index
34    }
35
36    /// Returns the unique semantic id corresponding to this object.
37    #[must_use]
38    pub const fn semantic_id(&self) -> i32 {
39        self.index
40    }
41
42    /// Returns this object's dataset-specific category.
43    #[must_use]
44    pub const fn category(&self) -> Option<&ObjectCategory> {
45        self.category.as_ref()
46    }
47
48    /// Returns the axis-aligned bounding box enclosing this object.
49    #[must_use]
50    pub fn aabb(&self) -> Aabb {
51        self.obb.to_aabb()
52    }
53
54    /// Returns the object's oriented bounding box.
55    #[must_use]
56    pub const fn obb(&self) -> &Obb {
57        &self.obb
58    }
59}
60
61impl<OC: fmt::Display> fmt::Display for SemanticObject<OC> {
62    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
63        let category = self
64            .category
65            .as_ref()
66            .map_or_else(|| "none".to_string(), ToString::to_string);
67        write!(
68            formatter,
69            "object {}: category={}, {}",
70            self.index, category, self.obb
71        )
72    }
73}