semantic-scene 0.1.1

Rust parser for semantic scene descriptors, currently focused on Habitat-Sim Matterport3D .house files.
Documentation
//! Semantic object model.

use std::fmt;

use crate::{Aabb, Obb};

/// Represents a distinct semantically annotated object.
///
/// The source index is also the object's semantic id.
#[derive(Debug, Clone, PartialEq)]
pub struct SemanticObject<ObjectCategory> {
    index: i32,
    category: Option<ObjectCategory>,
    obb: Obb,
}

impl<ObjectCategory> SemanticObject<ObjectCategory> {
    /// Creates a semantic object.
    ///
    /// `index` is the original descriptor index for this object.
    #[must_use]
    pub const fn new(index: i32, category: Option<ObjectCategory>, obb: Obb) -> Self {
        Self {
            index,
            category,
            obb,
        }
    }

    /// Returns the original descriptor index.
    #[must_use]
    pub const fn index(&self) -> i32 {
        self.index
    }

    /// Returns the unique semantic id corresponding to this object.
    #[must_use]
    pub const fn semantic_id(&self) -> i32 {
        self.index
    }

    /// Returns this object's dataset-specific category.
    #[must_use]
    pub const fn category(&self) -> Option<&ObjectCategory> {
        self.category.as_ref()
    }

    /// Returns the axis-aligned bounding box enclosing this object.
    #[must_use]
    pub fn aabb(&self) -> Aabb {
        self.obb.to_aabb()
    }

    /// Returns the object's oriented bounding box.
    #[must_use]
    pub const fn obb(&self) -> &Obb {
        &self.obb
    }
}

impl<OC: fmt::Display> fmt::Display for SemanticObject<OC> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let category = self
            .category
            .as_ref()
            .map_or_else(|| "none".to_string(), ToString::to_string);
        write!(
            formatter,
            "object {}: category={}, {}",
            self.index, category, self.obb
        )
    }
}