tracktor 0.4.1

Multi-target tracking with random finite sets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Gaussian Mixture Probability Hypothesis Density (GM-PHD) Filter
//!
//! Implementation of the GM-PHD filter for multi-target tracking.
//!
//! Reference: Vo, B.-N., & Ma, W.-K. (2006). "The Gaussian Mixture
//! Probability Hypothesis Density Filter"

use ::core::marker::PhantomData;
use nalgebra::RealField;
use num_traits::Float;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use crate::models::{BirthModel, ClutterModel, ObservationModel, TransitionModel};
use crate::types::gaussian::{GaussianMixture, GaussianState, innovation_likelihood};
use crate::types::spaces::{ComputeInnovation, Measurement, StateVector};
use crate::types::transforms::{compute_innovation_covariance, compute_kalman_gain, joseph_update};

// ============================================================================
// Filter Phase Markers
// ============================================================================

/// Marker type indicating a predicted filter state.
#[derive(Debug, Clone, Copy)]
pub struct Predicted;

/// Marker type indicating an updated filter state.
#[derive(Debug, Clone, Copy)]
pub struct Updated;

// ============================================================================
// Update Result
// ============================================================================

/// Statistics from a filter update step, reporting any numerical issues.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, Default)]
pub struct UpdateStats {
    /// Number of components where Kalman gain computation failed (singular innovation covariance)
    pub singular_covariance_count: usize,
    /// Number of components where likelihood computation returned zero or failed
    pub zero_likelihood_count: usize,
    /// Whether LBP converged (None if LBP not used, Some(true) if converged, Some(false) if hit max iterations)
    pub lbp_converged: Option<bool>,
    /// Number of LBP iterations run (None if LBP not used)
    pub lbp_iterations: Option<usize>,
}

#[cfg(feature = "alloc")]
impl UpdateStats {
    /// Returns true if any numerical issues were encountered.
    pub fn has_issues(&self) -> bool {
        self.singular_covariance_count > 0
            || self.zero_likelihood_count > 0
            || self.lbp_converged == Some(false)
    }
}

// ============================================================================
// PHD Filter State
// ============================================================================

/// The state of a GM-PHD filter at a particular phase.
///
/// The `Phase` parameter encodes whether this is a predicted or updated state,
/// ensuring correct operation ordering at compile time.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub struct PhdFilterState<T: RealField, const N: usize, Phase> {
    /// Gaussian mixture representing the PHD
    pub mixture: GaussianMixture<T, N>,
    /// Current time step
    pub time_step: u32,
    /// Phase marker
    _phase: PhantomData<Phase>,
}

#[cfg(feature = "alloc")]
impl<T: RealField + Copy, const N: usize> PhdFilterState<T, N, Updated> {
    /// Creates a new filter state in the updated phase.
    pub fn new() -> Self {
        Self {
            mixture: GaussianMixture::new(),
            time_step: 0,
            _phase: PhantomData,
        }
    }

    /// Creates a filter state from an initial mixture.
    pub fn from_mixture(mixture: GaussianMixture<T, N>) -> Self {
        Self {
            mixture,
            time_step: 0,
            _phase: PhantomData,
        }
    }

    /// Returns the expected number of targets.
    pub fn expected_target_count(&self) -> T {
        self.mixture.total_weight()
    }

    /// Predicts the PHD to the next time step.
    ///
    /// This method consumes the updated state and returns a predicted state.
    pub fn predict<Trans, Birth>(
        self,
        transition_model: &Trans,
        birth_model: &Birth,
        dt: T,
    ) -> PhdFilterState<T, N, Predicted>
    where
        Trans: TransitionModel<T, N>,
        Birth: BirthModel<T, N>,
    {
        let transition_matrix = transition_model.transition_matrix(dt);
        let process_noise = transition_model.process_noise(dt);

        let birth_components = birth_model.birth_components_vec();

        let mut predicted =
            GaussianMixture::with_capacity(self.mixture.len() + birth_components.len());

        // Predict surviving components
        for component in self.mixture.iter() {
            let p_s = transition_model.survival_probability(&component.mean);

            let predicted_component = GaussianState {
                weight: component.weight * p_s,
                mean: transition_matrix.apply_state(&component.mean),
                covariance: transition_matrix
                    .propagate_covariance(&component.covariance)
                    .add(&process_noise),
            };

            predicted.push(predicted_component);
        }

        // Add birth components
        for birth_component in birth_components {
            predicted.push(birth_component);
        }

        PhdFilterState {
            mixture: predicted,
            time_step: self.time_step + 1,
            _phase: PhantomData,
        }
    }
}

#[cfg(feature = "alloc")]
impl<T: RealField + Copy, const N: usize> Default for PhdFilterState<T, N, Updated> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "alloc")]
impl<T: RealField + Float + Copy, const N: usize> PhdFilterState<T, N, Predicted> {
    /// Updates the PHD with a set of measurements.
    ///
    /// This method consumes the predicted state and returns an updated state along
    /// with statistics about any numerical issues encountered during the update.
    ///
    /// # Returns
    /// A tuple of (updated_state, stats) where stats contains counts of any
    /// numerical issues (singular covariances, zero likelihoods).
    pub fn update_with_stats<const M: usize, Obs, Clutter>(
        self,
        measurements: &[Measurement<T, M>],
        observation_model: &Obs,
        clutter_model: &Clutter,
    ) -> (PhdFilterState<T, N, Updated>, UpdateStats)
    where
        Obs: ObservationModel<T, N, M>,
        Clutter: ClutterModel<T, M>,
    {
        let obs_matrix = observation_model.observation_matrix();
        let meas_noise = observation_model.measurement_noise();
        let mut stats = UpdateStats::default();

        // Pre-compute per-component data
        let component_data: Vec<_> = self
            .mixture
            .iter()
            .map(|c| {
                let p_d = observation_model.detection_probability(&c.mean);
                let predicted_meas = obs_matrix.observe(&c.mean);
                let innovation_cov =
                    compute_innovation_covariance(&c.covariance, &obs_matrix, &meas_noise);
                let kalman_gain = compute_kalman_gain(&c.covariance, &obs_matrix, &innovation_cov);

                if kalman_gain.is_none() {
                    stats.singular_covariance_count += 1;
                }

                (p_d, predicted_meas, innovation_cov, kalman_gain)
            })
            .collect();

        let mut updated =
            GaussianMixture::with_capacity(self.mixture.len() * (measurements.len() + 1));

        // Missed detection components
        for (i, component) in self.mixture.iter().enumerate() {
            let p_d = component_data[i].0;
            let missed_weight = component.weight * (T::one() - p_d);

            if missed_weight > T::zero() {
                updated.push(GaussianState {
                    weight: missed_weight,
                    mean: component.mean,
                    covariance: component.covariance,
                });
            }
        }

        // Detection components (one for each component-measurement pair)
        for measurement in measurements {
            // Compute denominator for weight normalization
            let mut weight_sum = clutter_model.clutter_intensity(measurement);

            let detection_weights: Vec<T> = self
                .mixture
                .iter()
                .enumerate()
                .map(|(i, c)| {
                    let (p_d, ref predicted_meas, ref innovation_cov, _) = component_data[i];

                    let innovation = measurement.innovation(*predicted_meas);
                    let likelihood = innovation_likelihood(&innovation, innovation_cov.as_matrix());

                    // Track zero likelihoods (can indicate numerical issues)
                    if likelihood <= T::zero() {
                        stats.zero_likelihood_count += 1;
                    }

                    let detection_weight = p_d * c.weight * likelihood;
                    weight_sum += detection_weight;

                    detection_weight
                })
                .collect();

            // Add updated components
            for (i, component) in self.mixture.iter().enumerate() {
                let (_, ref predicted_meas, _, ref kalman_gain) = component_data[i];

                if let Some(gain) = kalman_gain {
                    let normalized_weight = detection_weights[i] / weight_sum;

                    if normalized_weight > T::zero() {
                        let innovation = measurement.innovation(*predicted_meas);
                        let correction = gain.correct(&innovation);
                        let updated_mean = StateVector::from_svector(
                            component.mean.as_svector() + correction.as_svector(),
                        );
                        let updated_cov =
                            joseph_update(&component.covariance, gain, &obs_matrix, &meas_noise);

                        updated.push(GaussianState {
                            weight: normalized_weight,
                            mean: updated_mean,
                            covariance: updated_cov,
                        });
                    }
                }
                // Note: If kalman_gain is None, the component is skipped for this measurement.
                // This is tracked in stats.singular_covariance_count above.
            }
        }

        (
            PhdFilterState {
                mixture: updated,
                time_step: self.time_step,
                _phase: PhantomData,
            },
            stats,
        )
    }

    /// Updates the PHD with a set of measurements.
    ///
    /// This method consumes the predicted state and returns an updated state.
    /// For access to numerical issue statistics, use `update_with_stats` instead.
    pub fn update<const M: usize, Obs, Clutter>(
        self,
        measurements: &[Measurement<T, M>],
        observation_model: &Obs,
        clutter_model: &Clutter,
    ) -> PhdFilterState<T, N, Updated>
    where
        Obs: ObservationModel<T, N, M>,
        Clutter: ClutterModel<T, M>,
    {
        self.update_with_stats(measurements, observation_model, clutter_model)
            .0
    }

    /// Returns the expected number of targets (before update).
    pub fn expected_target_count(&self) -> T {
        self.mixture.total_weight()
    }
}

// ============================================================================
// GM-PHD Filter
// ============================================================================

/// Complete GM-PHD filter with models.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
pub struct GmPhdFilter<T, Trans, Obs, Clutter, Birth, const N: usize, const M: usize>
where
    T: RealField,
    Trans: TransitionModel<T, N>,
    Obs: ObservationModel<T, N, M>,
    Clutter: ClutterModel<T, M>,
    Birth: BirthModel<T, N>,
{
    /// Transition model
    pub transition: Trans,
    /// Observation model
    pub observation: Obs,
    /// Clutter model
    pub clutter: Clutter,
    /// Birth model
    pub birth: Birth,
    _marker: PhantomData<T>,
}

#[cfg(feature = "alloc")]
impl<T, Trans, Obs, Clutter, Birth, const N: usize, const M: usize>
    GmPhdFilter<T, Trans, Obs, Clutter, Birth, N, M>
where
    T: RealField + Float + Copy,
    Trans: TransitionModel<T, N>,
    Obs: ObservationModel<T, N, M>,
    Clutter: ClutterModel<T, M>,
    Birth: BirthModel<T, N>,
{
    /// Creates a new GM-PHD filter with the specified models.
    pub fn new(transition: Trans, observation: Obs, clutter: Clutter, birth: Birth) -> Self {
        Self {
            transition,
            observation,
            clutter,
            birth,
            _marker: PhantomData,
        }
    }

    /// Creates an initial filter state.
    pub fn initial_state(&self) -> PhdFilterState<T, N, Updated> {
        PhdFilterState::new()
    }

    /// Creates an initial filter state from prior components.
    pub fn initial_state_from(
        &self,
        components: Vec<GaussianState<T, N>>,
    ) -> PhdFilterState<T, N, Updated> {
        PhdFilterState::from_mixture(GaussianMixture::from_components(components))
    }

    /// Runs one complete predict-update cycle with statistics.
    ///
    /// Returns both the updated state and statistics about any numerical issues.
    pub fn step_with_stats(
        &self,
        state: PhdFilterState<T, N, Updated>,
        measurements: &[Measurement<T, M>],
        dt: T,
    ) -> (PhdFilterState<T, N, Updated>, UpdateStats) {
        let predicted = state.predict(&self.transition, &self.birth, dt);
        predicted.update_with_stats(measurements, &self.observation, &self.clutter)
    }

    /// Runs one complete predict-update cycle.
    ///
    /// For access to numerical issue statistics, use `step_with_stats` instead.
    pub fn step(
        &self,
        state: PhdFilterState<T, N, Updated>,
        measurements: &[Measurement<T, M>],
        dt: T,
    ) -> PhdFilterState<T, N, Updated> {
        self.step_with_stats(state, measurements, dt).0
    }
}

// ============================================================================
// State Extraction (Legacy - prefer utils::extraction)
// ============================================================================

/// Extracts target states from a PHD filter state.
///
/// This is a simple extraction method. For more sophisticated extraction
/// strategies (local maxima, expected count, etc.), use `crate::utils::extract_targets`
/// with an `ExtractionConfig`.
#[cfg(feature = "alloc")]
#[deprecated(
    since = "0.2.0",
    note = "Use crate::utils::extract_targets with ExtractionConfig instead"
)]
pub fn extract_states<T: RealField + Float + Copy, const N: usize, Phase>(
    state: &PhdFilterState<T, N, Phase>,
    threshold: T,
) -> Vec<(StateVector<T, N>, T)> {
    state
        .mixture
        .iter()
        .filter(|c| c.weight >= threshold)
        .map(|c| (c.mean, c.weight))
        .collect()
}

/// Extracts states using MAP (Maximum A Posteriori) estimation.
///
/// Returns the N states with highest weights.
///
/// For more sophisticated extraction strategies, use `crate::utils::extract_targets`
/// with `ExtractionConfig::top_n()`.
#[cfg(feature = "alloc")]
#[deprecated(
    since = "0.2.0",
    note = "Use crate::utils::extract_targets with ExtractionConfig::top_n() instead"
)]
pub fn extract_states_map<T: RealField + Float + Copy, const N: usize, Phase>(
    state: &PhdFilterState<T, N, Phase>,
    n_targets: usize,
) -> Vec<(StateVector<T, N>, T)> {
    let mut components: Vec<_> = state.mixture.iter().map(|c| (c.mean, c.weight)).collect();

    // Sort by weight descending
    components.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(core::cmp::Ordering::Equal));

    components.into_iter().take(n_targets).collect()
}

/// Estimates the number of targets by rounding total weight.
///
/// For a more complete API, use `crate::utils::estimate_cardinality`.
#[cfg(feature = "alloc")]
#[deprecated(
    since = "0.2.0",
    note = "Use crate::utils::estimate_cardinality instead"
)]
pub fn estimate_target_count<T: RealField + Float + Copy, const N: usize, Phase>(
    state: &PhdFilterState<T, N, Phase>,
) -> usize {
    let total = state.mixture.total_weight();
    num_traits::Float::round(total).to_usize().unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{ConstantVelocity2D, FixedBirthModel, PositionSensor2D, UniformClutter2D};
    use crate::types::spaces::StateCovariance;

    #[cfg(feature = "alloc")]
    #[test]
    fn test_phd_filter_creation() {
        let transition = ConstantVelocity2D::new(1.0_f64, 0.99);
        let observation = PositionSensor2D::new(1.0, 0.9);
        let clutter = UniformClutter2D::new(10.0, (0.0, 100.0), (0.0, 100.0));
        let birth = FixedBirthModel::<f64, 4>::new();

        let filter = GmPhdFilter::new(transition, observation, clutter, birth);
        let state = filter.initial_state();

        assert_eq!(state.mixture.len(), 0);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_phd_predict() {
        let transition = ConstantVelocity2D::new(1.0_f64, 0.99);
        let observation = PositionSensor2D::new(1.0, 0.9);
        let clutter = UniformClutter2D::new(10.0, (0.0, 100.0), (0.0, 100.0));

        let mut birth = FixedBirthModel::<f64, 4>::new();
        birth.add_birth_location(
            0.1,
            StateVector::from_array([50.0, 50.0, 0.0, 0.0]),
            StateCovariance::identity(),
        );

        let filter = GmPhdFilter::new(transition, observation, clutter, birth);

        // Start with one component
        let initial = filter.initial_state_from(vec![GaussianState::new(
            1.0,
            StateVector::from_array([10.0, 20.0, 1.0, 2.0]),
            StateCovariance::identity(),
        )]);

        let predicted = initial.predict(&filter.transition, &filter.birth, 1.0);

        // Should have original (survived) + birth components
        assert_eq!(predicted.mixture.len(), 2);

        // Check that position was predicted correctly
        let target = &predicted.mixture.components[0];
        assert!((target.mean.index(0) - 11.0).abs() < 1e-5); // x + vx*dt
        assert!((target.mean.index(1) - 22.0).abs() < 1e-5); // y + vy*dt
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_phd_update() {
        let transition = ConstantVelocity2D::new(0.1_f64, 0.99);
        let observation = PositionSensor2D::new(1.0, 0.9);
        let clutter = UniformClutter2D::new(1.0, (0.0, 100.0), (0.0, 100.0));
        let birth = FixedBirthModel::<f64, 4>::new();

        let filter = GmPhdFilter::new(transition, observation, clutter, birth);

        // Start with one component
        let initial = filter.initial_state_from(vec![GaussianState::new(
            1.0,
            StateVector::from_array([10.0, 20.0, 0.0, 0.0]),
            StateCovariance::from_matrix(nalgebra::matrix![
                1.0, 0.0, 0.0, 0.0;
                0.0, 1.0, 0.0, 0.0;
                0.0, 0.0, 1.0, 0.0;
                0.0, 0.0, 0.0, 1.0
            ]),
        )]);

        let predicted = initial.predict(&filter.transition, &filter.birth, 1.0);

        // Update with a measurement near the target
        let measurements = [Measurement::from_array([10.5, 20.5])];
        let updated = predicted.update(&measurements, &filter.observation, &filter.clutter);

        // Should have components for missed detection and detection
        assert!(updated.mixture.len() >= 1);

        // Total weight should still be approximately 1 (one target)
        let total_weight = updated.expected_target_count();
        assert!(total_weight > 0.5 && total_weight < 1.5);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_state_extraction() {
        let mut mixture = GaussianMixture::new();
        mixture.push(GaussianState::new(
            0.8,
            StateVector::from_array([10.0, 20.0, 1.0, 2.0]),
            StateCovariance::identity(),
        ));
        mixture.push(GaussianState::new(
            0.3,
            StateVector::from_array([50.0, 60.0, 0.0, 0.0]),
            StateCovariance::identity(),
        ));

        let state: PhdFilterState<f64, 4, Updated> = PhdFilterState::from_mixture(mixture);

        // Extract with threshold 0.5
        let targets = extract_states(&state, 0.5);
        assert_eq!(targets.len(), 1);
        assert!((targets[0].0.index(0) - 10.0).abs() < 1e-10);

        // Extract MAP with n=2
        let targets = extract_states_map(&state, 2);
        assert_eq!(targets.len(), 2);
    }
}