use std::fmt;
use crate::{Aabb, Vec2, Vec3};
use super::SemanticObject;
#[derive(Debug, Clone, PartialEq)]
pub struct SemanticRegion<ObjectCategory, RegionCategory> {
index: i32,
category: RegionCategory,
position: Vec3,
aabb: Aabb,
objects: Vec<SemanticObject<ObjectCategory>>,
poly_loop_points: Vec<Vec2>,
volume_edges: Vec<Vec<Vec3>>,
floor_height: f64,
extrusion_height: f64,
area: f64,
}
impl<ObjectCategory, RegionCategory> SemanticRegion<ObjectCategory, RegionCategory> {
#[must_use]
pub fn new(
index: i32,
category: RegionCategory,
position: Vec3,
aabb: Aabb,
objects: Vec<SemanticObject<ObjectCategory>>,
) -> Self {
Self {
index,
category,
position,
aabb,
objects,
poly_loop_points: Vec::new(),
volume_edges: Vec::new(),
floor_height: f64::from(aabb.min.1),
extrusion_height: f64::from(aabb.max.1 - aabb.min.1),
area: f64::from((aabb.max.0 - aabb.min.0).abs() * (aabb.max.2 - aabb.min.2).abs()),
}
}
#[must_use]
pub const fn index(&self) -> i32 {
self.index
}
#[must_use]
pub const fn category(&self) -> &RegionCategory {
&self.category
}
#[must_use]
pub const fn position(&self) -> Vec3 {
self.position
}
#[must_use]
pub const fn aabb(&self) -> &Aabb {
&self.aabb
}
#[must_use]
pub fn objects(&self) -> &[SemanticObject<ObjectCategory>] {
&self.objects
}
#[must_use]
pub fn contains(&self, point: Vec3) -> bool {
self.aabb.contains(point)
}
#[must_use]
pub fn poly_loop_points(&self) -> &[Vec2] {
&self.poly_loop_points
}
#[must_use]
pub fn volume_edges(&self) -> &[Vec<Vec3>] {
&self.volume_edges
}
#[must_use]
pub const fn floor_height(&self) -> f64 {
self.floor_height
}
#[must_use]
pub const fn extrusion_height(&self) -> f64 {
self.extrusion_height
}
#[must_use]
pub const fn area(&self) -> f64 {
self.area
}
#[must_use]
pub const fn volume(&self) -> f64 {
self.area * self.extrusion_height
}
}
impl<OC, RC: fmt::Display> fmt::Display for SemanticRegion<OC, RC> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"region {}: category={}, objects={}, bounds=[{}]",
self.index,
self.category,
self.objects.len(),
self.aabb
)
}
}