use super::gaussian::GaussianState;
use nalgebra::RealField;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Label {
pub birth_time: u32,
pub index: u32,
}
impl Label {
#[inline]
pub const fn new(birth_time: u32, index: u32) -> Self {
Self { birth_time, index }
}
}
impl core::fmt::Display for Label {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "({}, {})", self.birth_time, self.index)
}
}
#[derive(Debug, Clone)]
pub struct LabelGenerator {
current_time: u32,
next_index: u32,
}
impl LabelGenerator {
#[inline]
pub const fn new() -> Self {
Self {
current_time: 0,
next_index: 0,
}
}
#[inline]
pub const fn at_time(time: u32) -> Self {
Self {
current_time: time,
next_index: 0,
}
}
#[inline]
pub fn advance_time(&mut self) {
self.current_time += 1;
self.next_index = 0;
}
#[inline]
pub fn set_time(&mut self, time: u32) {
self.current_time = time;
self.next_index = 0;
}
#[inline]
pub fn current_time(&self) -> u32 {
self.current_time
}
#[inline]
pub fn next_label(&mut self) -> Label {
let label = Label::new(self.current_time, self.next_index);
self.next_index += 1;
label
}
#[cfg(feature = "alloc")]
pub fn next_labels(&mut self, n: usize) -> alloc::vec::Vec<Label> {
(0..n).map(|_| self.next_label()).collect()
}
}
impl Default for LabelGenerator {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct BernoulliTrack<T: RealField, const N: usize> {
pub label: Label,
pub existence: T,
pub state: GaussianState<T, N>,
}
impl<T: RealField + Copy, const N: usize> BernoulliTrack<T, N> {
#[inline]
pub fn new(label: Label, existence: T, state: GaussianState<T, N>) -> Self {
Self {
label,
existence,
state,
}
}
#[inline]
pub fn is_likely_existing(&self) -> bool {
self.existence > T::from_f64(0.5).unwrap()
}
#[inline]
pub fn expected_weight(&self) -> T {
self.existence * self.state.weight
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LabeledGaussian<T: RealField, const N: usize> {
pub label: Label,
pub state: GaussianState<T, N>,
}
impl<T: RealField + Copy, const N: usize> LabeledGaussian<T, N> {
#[inline]
pub fn new(label: Label, state: GaussianState<T, N>) -> Self {
Self { label, state }
}
}
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub struct Hypothesis {
pub weight: f64,
pub labels: alloc::vec::Vec<Label>,
}
#[cfg(feature = "alloc")]
impl Hypothesis {
#[inline]
pub fn new(weight: f64, labels: alloc::vec::Vec<Label>) -> Self {
Self { weight, labels }
}
#[inline]
pub fn empty(weight: f64) -> Self {
Self {
weight,
labels: alloc::vec::Vec::new(),
}
}
#[inline]
pub fn cardinality(&self) -> usize {
self.labels.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::spaces::{StateCovariance, StateVector};
#[test]
fn test_label_generation() {
let mut generator = LabelGenerator::new();
let l1 = generator.next_label();
let l2 = generator.next_label();
assert_eq!(l1, Label::new(0, 0));
assert_eq!(l2, Label::new(0, 1));
generator.advance_time();
let l3 = generator.next_label();
assert_eq!(l3, Label::new(1, 0));
}
#[test]
fn test_bernoulli_track() {
let label = Label::new(0, 0);
let mean: StateVector<f64, 4> = StateVector::from_array([0.0, 0.0, 1.0, 0.0]);
let cov: StateCovariance<f64, 4> = StateCovariance::identity();
let state = GaussianState::new(1.0, mean, cov);
let track = BernoulliTrack::new(label, 0.8, state);
assert!(track.is_likely_existing());
assert!((track.existence - 0.8).abs() < 1e-10);
}
#[test]
fn test_label_display() {
let label = Label::new(5, 3);
assert_eq!(format!("{}", label), "(5, 3)");
}
}