use anyhow::Result;
#[derive(Debug, Clone, PartialEq)]
pub struct TemporalInterval {
pub start: f64,
pub end: f64,
}
impl TemporalInterval {
pub fn new(start: f64, end: f64) -> Result<Self> {
if start > end {
return Err(anyhow::anyhow!(
"Invalid interval: start ({}) > end ({})",
start,
end
));
}
Ok(Self { start, end })
}
pub fn before(&self, other: &TemporalInterval) -> bool {
self.end < other.start
}
pub fn after(&self, other: &TemporalInterval) -> bool {
self.start > other.end
}
pub fn overlaps(&self, other: &TemporalInterval) -> bool {
self.start < other.end && other.start < self.end
}
pub fn meets(&self, other: &TemporalInterval) -> bool {
(self.end - other.start).abs() < f64::EPSILON
}
pub fn contains(&self, time: f64) -> bool {
time >= self.start && time <= self.end
}
pub fn duration(&self) -> f64 {
self.end - self.start
}
}