Skip to main content

semantic_scene/scene/
level.rs

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