Skip to main content

semantic_scene/scene/
level.rs

1//! Semantic level model.
2
3use std::fmt;
4
5use crate::{Aabb, Vec3};
6
7use super::{SemanticObject, SemanticRegion};
8
9/// Represents a level of a semantic scene.
10///
11/// A level owns its semantic regions and exposes its original descriptor index,
12/// label, position, and axis-aligned bounding box.
13#[derive(Debug, Clone, PartialEq)]
14pub struct SemanticLevel<ObjectCategory, RegionCategory> {
15    index: i32,
16    label: String,
17    position: Vec3,
18    aabb: Aabb,
19    regions: Vec<SemanticRegion<ObjectCategory, RegionCategory>>,
20}
21
22impl<ObjectCategory, RegionCategory> SemanticLevel<ObjectCategory, RegionCategory> {
23    /// Creates a semantic level.
24    ///
25    /// `index` is the original descriptor index for this level.
26    #[must_use]
27    pub const fn new(
28        index: i32,
29        label: String,
30        position: Vec3,
31        aabb: Aabb,
32        regions: Vec<SemanticRegion<ObjectCategory, RegionCategory>>,
33    ) -> Self {
34        Self {
35            index,
36            label,
37            position,
38            aabb,
39            regions,
40        }
41    }
42
43    /// Returns this level's id.
44    ///
45    /// This is the original descriptor index converted to a string.
46    #[must_use]
47    pub fn id(&self) -> String {
48        self.index.to_string()
49    }
50
51    /// Returns the source descriptor index.
52    #[must_use]
53    pub const fn index(&self) -> i32 {
54        self.index
55    }
56
57    /// Returns the source descriptor label code.
58    #[must_use]
59    pub fn label(&self) -> &str {
60        &self.label
61    }
62
63    /// Returns the level position from the source descriptor.
64    #[must_use]
65    pub const fn position(&self) -> Vec3 {
66        self.position
67    }
68
69    /// Returns the level axis-aligned bounding box.
70    #[must_use]
71    pub const fn aabb(&self) -> &Aabb {
72        &self.aabb
73    }
74
75    /// Returns regions contained by this level.
76    #[must_use]
77    pub fn regions(&self) -> &[SemanticRegion<ObjectCategory, RegionCategory>] {
78        &self.regions
79    }
80
81    /// Returns objects contained by this level.
82    pub fn objects(&self) -> impl Iterator<Item = &SemanticObject<ObjectCategory>> {
83        self.regions.iter().flat_map(SemanticRegion::objects)
84    }
85}
86
87impl<OC, RC> fmt::Display for SemanticLevel<OC, RC> {
88    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(
90            formatter,
91            "level {}: regions={}, objects={}, bounds=[{}]",
92            self.index,
93            self.regions.len(),
94            self.objects().count(),
95            self.aabb
96        )
97    }
98}